gimple-expr.h (create_tmp_var_name, [...]): Relocate prototypes from gimple.h.
[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 #include "gimplify.h"
46
47 /* The type of functions taking a tree, and some additional data, and
48 returning an int. */
49 typedef int (*tree_fn_t) (tree, void*);
50
51 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
52 instantiations have been deferred, either because their definitions
53 were not yet available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template {
55 struct pending_template *next;
56 struct tinst_level *tinst;
57 };
58
59 static GTY(()) struct pending_template *pending_templates;
60 static GTY(()) struct pending_template *last_pending_template;
61
62 int processing_template_parmlist;
63 static int template_header_count;
64
65 static GTY(()) tree saved_trees;
66 static vec<int> inline_parm_levels;
67
68 static GTY(()) struct tinst_level *current_tinst_level;
69
70 static GTY(()) tree saved_access_scope;
71
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr;
76
77 /* A map from local variable declarations in the body of the template
78 presently being instantiated to the corresponding instantiated
79 local variables. */
80 static struct pointer_map_t *local_specializations;
81
82 /* True if we've recursed into fn_type_unification too many times. */
83 static bool excessive_deduction_depth;
84
85 typedef struct GTY(()) spec_entry
86 {
87 tree tmpl;
88 tree args;
89 tree spec;
90 } spec_entry;
91
92 static GTY ((param_is (spec_entry)))
93 htab_t decl_specializations;
94
95 static GTY ((param_is (spec_entry)))
96 htab_t type_specializations;
97
98 /* Contains canonical template parameter types. The vector is indexed by
99 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
100 TREE_LIST, whose TREE_VALUEs contain the canonical template
101 parameters of various types and levels. */
102 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
103
104 #define UNIFY_ALLOW_NONE 0
105 #define UNIFY_ALLOW_MORE_CV_QUAL 1
106 #define UNIFY_ALLOW_LESS_CV_QUAL 2
107 #define UNIFY_ALLOW_DERIVED 4
108 #define UNIFY_ALLOW_INTEGER 8
109 #define UNIFY_ALLOW_OUTER_LEVEL 16
110 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
111 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112
113 enum template_base_result {
114 tbr_incomplete_type,
115 tbr_ambiguous_baseclass,
116 tbr_success
117 };
118
119 static void push_access_scope (tree);
120 static void pop_access_scope (tree);
121 static bool resolve_overloaded_unification (tree, tree, tree, tree,
122 unification_kind_t, int,
123 bool);
124 static int try_one_overload (tree, tree, tree, tree, tree,
125 unification_kind_t, int, bool, bool);
126 static int unify (tree, tree, tree, tree, int, bool);
127 static void add_pending_template (tree);
128 static tree reopen_tinst_level (struct tinst_level *);
129 static tree tsubst_initializer_list (tree, tree);
130 static tree get_class_bindings (tree, tree, tree, tree);
131 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
132 bool, bool);
133 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
134 bool, bool);
135 static void tsubst_enum (tree, tree, tree);
136 static tree add_to_template_args (tree, tree);
137 static tree add_outermost_template_args (tree, tree);
138 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
139 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
140 tree);
141 static int type_unification_real (tree, tree, tree, const tree *,
142 unsigned int, int, unification_kind_t, int,
143 vec<deferred_access_check, va_gc> **,
144 bool);
145 static void note_template_header (int);
146 static tree convert_nontype_argument_function (tree, tree);
147 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
148 static tree convert_template_argument (tree, tree, tree,
149 tsubst_flags_t, int, tree);
150 static int for_each_template_parm (tree, tree_fn_t, void*,
151 struct pointer_set_t*, bool);
152 static tree expand_template_argument_pack (tree);
153 static tree build_template_parm_index (int, int, int, tree, tree);
154 static bool inline_needs_template_parms (tree);
155 static void push_inline_template_parms_recursive (tree, int);
156 static tree retrieve_local_specialization (tree);
157 static void register_local_specialization (tree, tree);
158 static hashval_t hash_specialization (const void *p);
159 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
160 static int mark_template_parm (tree, void *);
161 static int template_parm_this_level_p (tree, void *);
162 static tree tsubst_friend_function (tree, tree);
163 static tree tsubst_friend_class (tree, tree);
164 static int can_complete_type_without_circularity (tree);
165 static tree get_bindings (tree, tree, tree, bool);
166 static int template_decl_level (tree);
167 static int check_cv_quals_for_unify (int, tree, tree);
168 static void template_parm_level_and_index (tree, int*, int*);
169 static int unify_pack_expansion (tree, tree, tree,
170 tree, unification_kind_t, bool, bool);
171 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
172 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
173 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
174 static void regenerate_decl_from_template (tree, tree);
175 static tree most_specialized_class (tree, tree, tsubst_flags_t);
176 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
177 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
178 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
179 static bool check_specialization_scope (void);
180 static tree process_partial_specialization (tree);
181 static void set_current_access_from_decl (tree);
182 static enum template_base_result get_template_base (tree, tree, tree, tree,
183 bool , tree *);
184 static tree try_class_unification (tree, tree, tree, tree, bool);
185 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
186 tree, tree);
187 static bool template_template_parm_bindings_ok_p (tree, tree);
188 static int template_args_equal (tree, tree);
189 static void tsubst_default_arguments (tree, tsubst_flags_t);
190 static tree for_each_template_parm_r (tree *, int *, void *);
191 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
192 static void copy_default_args_to_explicit_spec (tree);
193 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
194 static bool dependent_template_arg_p (tree);
195 static bool any_template_arguments_need_structural_equality_p (tree);
196 static bool dependent_type_p_r (tree);
197 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
198 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
199 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
200 static tree tsubst_decl (tree, tree, tsubst_flags_t);
201 static void perform_typedefs_access_check (tree tmpl, tree targs);
202 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
203 location_t);
204 static tree listify (tree);
205 static tree listify_autos (tree, tree);
206 static tree template_parm_to_arg (tree t);
207 static tree current_template_args (void);
208 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
209 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
210
211 /* Make the current scope suitable for access checking when we are
212 processing T. T can be FUNCTION_DECL for instantiated function
213 template, VAR_DECL for static member variable, or TYPE_DECL for
214 alias template (needed by instantiate_decl). */
215
216 static void
217 push_access_scope (tree t)
218 {
219 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
220 || TREE_CODE (t) == TYPE_DECL);
221
222 if (DECL_FRIEND_CONTEXT (t))
223 push_nested_class (DECL_FRIEND_CONTEXT (t));
224 else if (DECL_CLASS_SCOPE_P (t))
225 push_nested_class (DECL_CONTEXT (t));
226 else
227 push_to_top_level ();
228
229 if (TREE_CODE (t) == FUNCTION_DECL)
230 {
231 saved_access_scope = tree_cons
232 (NULL_TREE, current_function_decl, saved_access_scope);
233 current_function_decl = t;
234 }
235 }
236
237 /* Restore the scope set up by push_access_scope. T is the node we
238 are processing. */
239
240 static void
241 pop_access_scope (tree t)
242 {
243 if (TREE_CODE (t) == FUNCTION_DECL)
244 {
245 current_function_decl = TREE_VALUE (saved_access_scope);
246 saved_access_scope = TREE_CHAIN (saved_access_scope);
247 }
248
249 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
250 pop_nested_class ();
251 else
252 pop_from_top_level ();
253 }
254
255 /* Do any processing required when DECL (a member template
256 declaration) is finished. Returns the TEMPLATE_DECL corresponding
257 to DECL, unless it is a specialization, in which case the DECL
258 itself is returned. */
259
260 tree
261 finish_member_template_decl (tree decl)
262 {
263 if (decl == error_mark_node)
264 return error_mark_node;
265
266 gcc_assert (DECL_P (decl));
267
268 if (TREE_CODE (decl) == TYPE_DECL)
269 {
270 tree type;
271
272 type = TREE_TYPE (decl);
273 if (type == error_mark_node)
274 return error_mark_node;
275 if (MAYBE_CLASS_TYPE_P (type)
276 && CLASSTYPE_TEMPLATE_INFO (type)
277 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
278 {
279 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
280 check_member_template (tmpl);
281 return tmpl;
282 }
283 return NULL_TREE;
284 }
285 else if (TREE_CODE (decl) == FIELD_DECL)
286 error ("data member %qD cannot be a member template", decl);
287 else if (DECL_TEMPLATE_INFO (decl))
288 {
289 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
290 {
291 check_member_template (DECL_TI_TEMPLATE (decl));
292 return DECL_TI_TEMPLATE (decl);
293 }
294 else
295 return decl;
296 }
297 else
298 error ("invalid member template declaration %qD", decl);
299
300 return error_mark_node;
301 }
302
303 /* Create a template info node. */
304
305 tree
306 build_template_info (tree template_decl, tree template_args)
307 {
308 tree result = make_node (TEMPLATE_INFO);
309 TI_TEMPLATE (result) = template_decl;
310 TI_ARGS (result) = template_args;
311 return result;
312 }
313
314 /* Return the template info node corresponding to T, whatever T is. */
315
316 tree
317 get_template_info (const_tree t)
318 {
319 tree tinfo = NULL_TREE;
320
321 if (!t || t == error_mark_node)
322 return NULL;
323
324 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
325 tinfo = DECL_TEMPLATE_INFO (t);
326
327 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
328 t = TREE_TYPE (t);
329
330 if (OVERLOAD_TYPE_P (t))
331 tinfo = TYPE_TEMPLATE_INFO (t);
332 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
333 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
334
335 return tinfo;
336 }
337
338 /* Returns the template nesting level of the indicated class TYPE.
339
340 For example, in:
341 template <class T>
342 struct A
343 {
344 template <class U>
345 struct B {};
346 };
347
348 A<T>::B<U> has depth two, while A<T> has depth one.
349 Both A<T>::B<int> and A<int>::B<U> have depth one, if
350 they are instantiations, not specializations.
351
352 This function is guaranteed to return 0 if passed NULL_TREE so
353 that, for example, `template_class_depth (current_class_type)' is
354 always safe. */
355
356 int
357 template_class_depth (tree type)
358 {
359 int depth;
360
361 for (depth = 0;
362 type && TREE_CODE (type) != NAMESPACE_DECL;
363 type = (TREE_CODE (type) == FUNCTION_DECL)
364 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
365 {
366 tree tinfo = get_template_info (type);
367
368 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
369 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
370 ++depth;
371 }
372
373 return depth;
374 }
375
376 /* Subroutine of maybe_begin_member_template_processing.
377 Returns true if processing DECL needs us to push template parms. */
378
379 static bool
380 inline_needs_template_parms (tree decl)
381 {
382 if (! DECL_TEMPLATE_INFO (decl))
383 return false;
384
385 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
386 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
387 }
388
389 /* Subroutine of maybe_begin_member_template_processing.
390 Push the template parms in PARMS, starting from LEVELS steps into the
391 chain, and ending at the beginning, since template parms are listed
392 innermost first. */
393
394 static void
395 push_inline_template_parms_recursive (tree parmlist, int levels)
396 {
397 tree parms = TREE_VALUE (parmlist);
398 int i;
399
400 if (levels > 1)
401 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
402
403 ++processing_template_decl;
404 current_template_parms
405 = tree_cons (size_int (processing_template_decl),
406 parms, current_template_parms);
407 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
408
409 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
410 NULL);
411 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
412 {
413 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
414
415 if (parm == error_mark_node)
416 continue;
417
418 gcc_assert (DECL_P (parm));
419
420 switch (TREE_CODE (parm))
421 {
422 case TYPE_DECL:
423 case TEMPLATE_DECL:
424 pushdecl (parm);
425 break;
426
427 case PARM_DECL:
428 {
429 /* Make a CONST_DECL as is done in process_template_parm.
430 It is ugly that we recreate this here; the original
431 version built in process_template_parm is no longer
432 available. */
433 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
434 CONST_DECL, DECL_NAME (parm),
435 TREE_TYPE (parm));
436 DECL_ARTIFICIAL (decl) = 1;
437 TREE_CONSTANT (decl) = 1;
438 TREE_READONLY (decl) = 1;
439 DECL_INITIAL (decl) = DECL_INITIAL (parm);
440 SET_DECL_TEMPLATE_PARM_P (decl);
441 pushdecl (decl);
442 }
443 break;
444
445 default:
446 gcc_unreachable ();
447 }
448 }
449 }
450
451 /* Restore the template parameter context for a member template or
452 a friend template defined in a class definition. */
453
454 void
455 maybe_begin_member_template_processing (tree decl)
456 {
457 tree parms;
458 int levels = 0;
459
460 if (inline_needs_template_parms (decl))
461 {
462 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
463 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
464
465 if (DECL_TEMPLATE_SPECIALIZATION (decl))
466 {
467 --levels;
468 parms = TREE_CHAIN (parms);
469 }
470
471 push_inline_template_parms_recursive (parms, levels);
472 }
473
474 /* Remember how many levels of template parameters we pushed so that
475 we can pop them later. */
476 inline_parm_levels.safe_push (levels);
477 }
478
479 /* Undo the effects of maybe_begin_member_template_processing. */
480
481 void
482 maybe_end_member_template_processing (void)
483 {
484 int i;
485 int last;
486
487 if (inline_parm_levels.length () == 0)
488 return;
489
490 last = inline_parm_levels.pop ();
491 for (i = 0; i < last; ++i)
492 {
493 --processing_template_decl;
494 current_template_parms = TREE_CHAIN (current_template_parms);
495 poplevel (0, 0, 0);
496 }
497 }
498
499 /* Return a new template argument vector which contains all of ARGS,
500 but has as its innermost set of arguments the EXTRA_ARGS. */
501
502 static tree
503 add_to_template_args (tree args, tree extra_args)
504 {
505 tree new_args;
506 int extra_depth;
507 int i;
508 int j;
509
510 if (args == NULL_TREE || extra_args == error_mark_node)
511 return extra_args;
512
513 extra_depth = TMPL_ARGS_DEPTH (extra_args);
514 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
515
516 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
517 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
518
519 for (j = 1; j <= extra_depth; ++j, ++i)
520 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
521
522 return new_args;
523 }
524
525 /* Like add_to_template_args, but only the outermost ARGS are added to
526 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
527 (EXTRA_ARGS) levels are added. This function is used to combine
528 the template arguments from a partial instantiation with the
529 template arguments used to attain the full instantiation from the
530 partial instantiation. */
531
532 static tree
533 add_outermost_template_args (tree args, tree extra_args)
534 {
535 tree new_args;
536
537 /* If there are more levels of EXTRA_ARGS than there are ARGS,
538 something very fishy is going on. */
539 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
540
541 /* If *all* the new arguments will be the EXTRA_ARGS, just return
542 them. */
543 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
544 return extra_args;
545
546 /* For the moment, we make ARGS look like it contains fewer levels. */
547 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
548
549 new_args = add_to_template_args (args, extra_args);
550
551 /* Now, we restore ARGS to its full dimensions. */
552 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
553
554 return new_args;
555 }
556
557 /* Return the N levels of innermost template arguments from the ARGS. */
558
559 tree
560 get_innermost_template_args (tree args, int n)
561 {
562 tree new_args;
563 int extra_levels;
564 int i;
565
566 gcc_assert (n >= 0);
567
568 /* If N is 1, just return the innermost set of template arguments. */
569 if (n == 1)
570 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
571
572 /* If we're not removing anything, just return the arguments we were
573 given. */
574 extra_levels = TMPL_ARGS_DEPTH (args) - n;
575 gcc_assert (extra_levels >= 0);
576 if (extra_levels == 0)
577 return args;
578
579 /* Make a new set of arguments, not containing the outer arguments. */
580 new_args = make_tree_vec (n);
581 for (i = 1; i <= n; ++i)
582 SET_TMPL_ARGS_LEVEL (new_args, i,
583 TMPL_ARGS_LEVEL (args, i + extra_levels));
584
585 return new_args;
586 }
587
588 /* The inverse of get_innermost_template_args: Return all but the innermost
589 EXTRA_LEVELS levels of template arguments from the ARGS. */
590
591 static tree
592 strip_innermost_template_args (tree args, int extra_levels)
593 {
594 tree new_args;
595 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
596 int i;
597
598 gcc_assert (n >= 0);
599
600 /* If N is 1, just return the outermost set of template arguments. */
601 if (n == 1)
602 return TMPL_ARGS_LEVEL (args, 1);
603
604 /* If we're not removing anything, just return the arguments we were
605 given. */
606 gcc_assert (extra_levels >= 0);
607 if (extra_levels == 0)
608 return args;
609
610 /* Make a new set of arguments, not containing the inner arguments. */
611 new_args = make_tree_vec (n);
612 for (i = 1; i <= n; ++i)
613 SET_TMPL_ARGS_LEVEL (new_args, i,
614 TMPL_ARGS_LEVEL (args, i));
615
616 return new_args;
617 }
618
619 /* We've got a template header coming up; push to a new level for storing
620 the parms. */
621
622 void
623 begin_template_parm_list (void)
624 {
625 /* We use a non-tag-transparent scope here, which causes pushtag to
626 put tags in this scope, rather than in the enclosing class or
627 namespace scope. This is the right thing, since we want
628 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
629 global template class, push_template_decl handles putting the
630 TEMPLATE_DECL into top-level scope. For a nested template class,
631 e.g.:
632
633 template <class T> struct S1 {
634 template <class T> struct S2 {};
635 };
636
637 pushtag contains special code to call pushdecl_with_scope on the
638 TEMPLATE_DECL for S2. */
639 begin_scope (sk_template_parms, NULL);
640 ++processing_template_decl;
641 ++processing_template_parmlist;
642 note_template_header (0);
643 }
644
645 /* This routine is called when a specialization is declared. If it is
646 invalid to declare a specialization here, an error is reported and
647 false is returned, otherwise this routine will return true. */
648
649 static bool
650 check_specialization_scope (void)
651 {
652 tree scope = current_scope ();
653
654 /* [temp.expl.spec]
655
656 An explicit specialization shall be declared in the namespace of
657 which the template is a member, or, for member templates, in the
658 namespace of which the enclosing class or enclosing class
659 template is a member. An explicit specialization of a member
660 function, member class or static data member of a class template
661 shall be declared in the namespace of which the class template
662 is a member. */
663 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
664 {
665 error ("explicit specialization in non-namespace scope %qD", scope);
666 return false;
667 }
668
669 /* [temp.expl.spec]
670
671 In an explicit specialization declaration for a member of a class
672 template or a member template that appears in namespace scope,
673 the member template and some of its enclosing class templates may
674 remain unspecialized, except that the declaration shall not
675 explicitly specialize a class member template if its enclosing
676 class templates are not explicitly specialized as well. */
677 if (current_template_parms)
678 {
679 error ("enclosing class templates are not explicitly specialized");
680 return false;
681 }
682
683 return true;
684 }
685
686 /* We've just seen template <>. */
687
688 bool
689 begin_specialization (void)
690 {
691 begin_scope (sk_template_spec, NULL);
692 note_template_header (1);
693 return check_specialization_scope ();
694 }
695
696 /* Called at then end of processing a declaration preceded by
697 template<>. */
698
699 void
700 end_specialization (void)
701 {
702 finish_scope ();
703 reset_specialization ();
704 }
705
706 /* Any template <>'s that we have seen thus far are not referring to a
707 function specialization. */
708
709 void
710 reset_specialization (void)
711 {
712 processing_specialization = 0;
713 template_header_count = 0;
714 }
715
716 /* We've just seen a template header. If SPECIALIZATION is nonzero,
717 it was of the form template <>. */
718
719 static void
720 note_template_header (int specialization)
721 {
722 processing_specialization = specialization;
723 template_header_count++;
724 }
725
726 /* We're beginning an explicit instantiation. */
727
728 void
729 begin_explicit_instantiation (void)
730 {
731 gcc_assert (!processing_explicit_instantiation);
732 processing_explicit_instantiation = true;
733 }
734
735
736 void
737 end_explicit_instantiation (void)
738 {
739 gcc_assert (processing_explicit_instantiation);
740 processing_explicit_instantiation = false;
741 }
742
743 /* An explicit specialization or partial specialization of TMPL is being
744 declared. Check that the namespace in which the specialization is
745 occurring is permissible. Returns false iff it is invalid to
746 specialize TMPL in the current namespace. */
747
748 static bool
749 check_specialization_namespace (tree tmpl)
750 {
751 tree tpl_ns = decl_namespace_context (tmpl);
752
753 /* [tmpl.expl.spec]
754
755 An explicit specialization shall be declared in the namespace of
756 which the template is a member, or, for member templates, in the
757 namespace of which the enclosing class or enclosing class
758 template is a member. An explicit specialization of a member
759 function, member class or static data member of a class template
760 shall be declared in the namespace of which the class template is
761 a member. */
762 if (current_scope() != DECL_CONTEXT (tmpl)
763 && !at_namespace_scope_p ())
764 {
765 error ("specialization of %qD must appear at namespace scope", tmpl);
766 return false;
767 }
768 if (is_associated_namespace (current_namespace, tpl_ns))
769 /* Same or super-using namespace. */
770 return true;
771 else
772 {
773 permerror (input_location, "specialization of %qD in different namespace", tmpl);
774 permerror (input_location, " from definition of %q+#D", tmpl);
775 return false;
776 }
777 }
778
779 /* SPEC is an explicit instantiation. Check that it is valid to
780 perform this explicit instantiation in the current namespace. */
781
782 static void
783 check_explicit_instantiation_namespace (tree spec)
784 {
785 tree ns;
786
787 /* DR 275: An explicit instantiation shall appear in an enclosing
788 namespace of its template. */
789 ns = decl_namespace_context (spec);
790 if (!is_ancestor (current_namespace, ns))
791 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
792 "(which does not enclose namespace %qD)",
793 spec, current_namespace, ns);
794 }
795
796 /* The TYPE is being declared. If it is a template type, that means it
797 is a partial specialization. Do appropriate error-checking. */
798
799 tree
800 maybe_process_partial_specialization (tree type)
801 {
802 tree context;
803
804 if (type == error_mark_node)
805 return error_mark_node;
806
807 /* A lambda that appears in specialization context is not itself a
808 specialization. */
809 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
810 return type;
811
812 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
813 {
814 error ("name of class shadows template template parameter %qD",
815 TYPE_NAME (type));
816 return error_mark_node;
817 }
818
819 context = TYPE_CONTEXT (type);
820
821 if (TYPE_ALIAS_P (type))
822 {
823 if (TYPE_TEMPLATE_INFO (type)
824 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
825 error ("specialization of alias template %qD",
826 TYPE_TI_TEMPLATE (type));
827 else
828 error ("explicit specialization of non-template %qT", type);
829 return error_mark_node;
830 }
831 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
832 {
833 /* This is for ordinary explicit specialization and partial
834 specialization of a template class such as:
835
836 template <> class C<int>;
837
838 or:
839
840 template <class T> class C<T*>;
841
842 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
843
844 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
845 && !COMPLETE_TYPE_P (type))
846 {
847 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
848 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
849 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
850 if (processing_template_decl)
851 {
852 if (push_template_decl (TYPE_MAIN_DECL (type))
853 == error_mark_node)
854 return error_mark_node;
855 }
856 }
857 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
858 error ("specialization of %qT after instantiation", type);
859 else if (errorcount && !processing_specialization
860 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
861 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
862 /* Trying to define a specialization either without a template<> header
863 or in an inappropriate place. We've already given an error, so just
864 bail now so we don't actually define the specialization. */
865 return error_mark_node;
866 }
867 else if (CLASS_TYPE_P (type)
868 && !CLASSTYPE_USE_TEMPLATE (type)
869 && CLASSTYPE_TEMPLATE_INFO (type)
870 && context && CLASS_TYPE_P (context)
871 && CLASSTYPE_TEMPLATE_INFO (context))
872 {
873 /* This is for an explicit specialization of member class
874 template according to [temp.expl.spec/18]:
875
876 template <> template <class U> class C<int>::D;
877
878 The context `C<int>' must be an implicit instantiation.
879 Otherwise this is just a member class template declared
880 earlier like:
881
882 template <> class C<int> { template <class U> class D; };
883 template <> template <class U> class C<int>::D;
884
885 In the first case, `C<int>::D' is a specialization of `C<T>::D'
886 while in the second case, `C<int>::D' is a primary template
887 and `C<T>::D' may not exist. */
888
889 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
890 && !COMPLETE_TYPE_P (type))
891 {
892 tree t;
893 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
894
895 if (current_namespace
896 != decl_namespace_context (tmpl))
897 {
898 permerror (input_location, "specializing %q#T in different namespace", type);
899 permerror (input_location, " from definition of %q+#D", tmpl);
900 }
901
902 /* Check for invalid specialization after instantiation:
903
904 template <> template <> class C<int>::D<int>;
905 template <> template <class U> class C<int>::D; */
906
907 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
908 t; t = TREE_CHAIN (t))
909 {
910 tree inst = TREE_VALUE (t);
911 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
912 {
913 /* We already have a full specialization of this partial
914 instantiation. Reassign it to the new member
915 specialization template. */
916 spec_entry elt;
917 spec_entry *entry;
918 void **slot;
919
920 elt.tmpl = most_general_template (tmpl);
921 elt.args = CLASSTYPE_TI_ARGS (inst);
922 elt.spec = inst;
923
924 htab_remove_elt (type_specializations, &elt);
925
926 elt.tmpl = tmpl;
927 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
928
929 slot = htab_find_slot (type_specializations, &elt, INSERT);
930 entry = ggc_alloc_spec_entry ();
931 *entry = elt;
932 *slot = entry;
933 }
934 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
935 /* But if we've had an implicit instantiation, that's a
936 problem ([temp.expl.spec]/6). */
937 error ("specialization %qT after instantiation %qT",
938 type, inst);
939 }
940
941 /* Mark TYPE as a specialization. And as a result, we only
942 have one level of template argument for the innermost
943 class template. */
944 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
945 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
946 CLASSTYPE_TI_ARGS (type)
947 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
948 }
949 }
950 else if (processing_specialization)
951 {
952 /* Someday C++0x may allow for enum template specialization. */
953 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
954 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
955 pedwarn (input_location, OPT_Wpedantic, "template specialization "
956 "of %qD not allowed by ISO C++", type);
957 else
958 {
959 error ("explicit specialization of non-template %qT", type);
960 return error_mark_node;
961 }
962 }
963
964 return type;
965 }
966
967 /* Returns nonzero if we can optimize the retrieval of specializations
968 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
969 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
970
971 static inline bool
972 optimize_specialization_lookup_p (tree tmpl)
973 {
974 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
975 && DECL_CLASS_SCOPE_P (tmpl)
976 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
977 parameter. */
978 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
979 /* The optimized lookup depends on the fact that the
980 template arguments for the member function template apply
981 purely to the containing class, which is not true if the
982 containing class is an explicit or partial
983 specialization. */
984 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
985 && !DECL_MEMBER_TEMPLATE_P (tmpl)
986 && !DECL_CONV_FN_P (tmpl)
987 /* It is possible to have a template that is not a member
988 template and is not a member of a template class:
989
990 template <typename T>
991 struct S { friend A::f(); };
992
993 Here, the friend function is a template, but the context does
994 not have template information. The optimized lookup relies
995 on having ARGS be the template arguments for both the class
996 and the function template. */
997 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
998 }
999
1000 /* Retrieve the specialization (in the sense of [temp.spec] - a
1001 specialization is either an instantiation or an explicit
1002 specialization) of TMPL for the given template ARGS. If there is
1003 no such specialization, return NULL_TREE. The ARGS are a vector of
1004 arguments, or a vector of vectors of arguments, in the case of
1005 templates with more than one level of parameters.
1006
1007 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1008 then we search for a partial specialization matching ARGS. This
1009 parameter is ignored if TMPL is not a class template.
1010
1011 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1012 result is a NONTYPE_ARGUMENT_PACK. */
1013
1014 static tree
1015 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1016 {
1017 if (tmpl == NULL_TREE)
1018 return NULL_TREE;
1019
1020 if (args == error_mark_node)
1021 return NULL_TREE;
1022
1023 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1024 || TREE_CODE (tmpl) == FIELD_DECL);
1025
1026 /* There should be as many levels of arguments as there are
1027 levels of parameters. */
1028 gcc_assert (TMPL_ARGS_DEPTH (args)
1029 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1030 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1031 : template_class_depth (DECL_CONTEXT (tmpl))));
1032
1033 if (optimize_specialization_lookup_p (tmpl))
1034 {
1035 tree class_template;
1036 tree class_specialization;
1037 vec<tree, va_gc> *methods;
1038 tree fns;
1039 int idx;
1040
1041 /* The template arguments actually apply to the containing
1042 class. Find the class specialization with those
1043 arguments. */
1044 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1045 class_specialization
1046 = retrieve_specialization (class_template, args, 0);
1047 if (!class_specialization)
1048 return NULL_TREE;
1049 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1050 for the specialization. */
1051 idx = class_method_index_for_fn (class_specialization, tmpl);
1052 if (idx == -1)
1053 return NULL_TREE;
1054 /* Iterate through the methods with the indicated name, looking
1055 for the one that has an instance of TMPL. */
1056 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1057 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1058 {
1059 tree fn = OVL_CURRENT (fns);
1060 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1061 /* using-declarations can add base methods to the method vec,
1062 and we don't want those here. */
1063 && DECL_CONTEXT (fn) == class_specialization)
1064 return fn;
1065 }
1066 return NULL_TREE;
1067 }
1068 else
1069 {
1070 spec_entry *found;
1071 spec_entry elt;
1072 htab_t specializations;
1073
1074 elt.tmpl = tmpl;
1075 elt.args = args;
1076 elt.spec = NULL_TREE;
1077
1078 if (DECL_CLASS_TEMPLATE_P (tmpl))
1079 specializations = type_specializations;
1080 else
1081 specializations = decl_specializations;
1082
1083 if (hash == 0)
1084 hash = hash_specialization (&elt);
1085 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1086 if (found)
1087 return found->spec;
1088 }
1089
1090 return NULL_TREE;
1091 }
1092
1093 /* Like retrieve_specialization, but for local declarations. */
1094
1095 static tree
1096 retrieve_local_specialization (tree tmpl)
1097 {
1098 void **slot;
1099
1100 if (local_specializations == NULL)
1101 return NULL_TREE;
1102
1103 slot = pointer_map_contains (local_specializations, tmpl);
1104 return slot ? (tree) *slot : NULL_TREE;
1105 }
1106
1107 /* Returns nonzero iff DECL is a specialization of TMPL. */
1108
1109 int
1110 is_specialization_of (tree decl, tree tmpl)
1111 {
1112 tree t;
1113
1114 if (TREE_CODE (decl) == FUNCTION_DECL)
1115 {
1116 for (t = decl;
1117 t != NULL_TREE;
1118 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1119 if (t == tmpl)
1120 return 1;
1121 }
1122 else
1123 {
1124 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1125
1126 for (t = TREE_TYPE (decl);
1127 t != NULL_TREE;
1128 t = CLASSTYPE_USE_TEMPLATE (t)
1129 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1130 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1131 return 1;
1132 }
1133
1134 return 0;
1135 }
1136
1137 /* Returns nonzero iff DECL is a specialization of friend declaration
1138 FRIEND_DECL according to [temp.friend]. */
1139
1140 bool
1141 is_specialization_of_friend (tree decl, tree friend_decl)
1142 {
1143 bool need_template = true;
1144 int template_depth;
1145
1146 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1147 || TREE_CODE (decl) == TYPE_DECL);
1148
1149 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1150 of a template class, we want to check if DECL is a specialization
1151 if this. */
1152 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1153 && DECL_TEMPLATE_INFO (friend_decl)
1154 && !DECL_USE_TEMPLATE (friend_decl))
1155 {
1156 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1157 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1158 need_template = false;
1159 }
1160 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1161 && !PRIMARY_TEMPLATE_P (friend_decl))
1162 need_template = false;
1163
1164 /* There is nothing to do if this is not a template friend. */
1165 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1166 return false;
1167
1168 if (is_specialization_of (decl, friend_decl))
1169 return true;
1170
1171 /* [temp.friend/6]
1172 A member of a class template may be declared to be a friend of a
1173 non-template class. In this case, the corresponding member of
1174 every specialization of the class template is a friend of the
1175 class granting friendship.
1176
1177 For example, given a template friend declaration
1178
1179 template <class T> friend void A<T>::f();
1180
1181 the member function below is considered a friend
1182
1183 template <> struct A<int> {
1184 void f();
1185 };
1186
1187 For this type of template friend, TEMPLATE_DEPTH below will be
1188 nonzero. To determine if DECL is a friend of FRIEND, we first
1189 check if the enclosing class is a specialization of another. */
1190
1191 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1192 if (template_depth
1193 && DECL_CLASS_SCOPE_P (decl)
1194 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1195 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1196 {
1197 /* Next, we check the members themselves. In order to handle
1198 a few tricky cases, such as when FRIEND_DECL's are
1199
1200 template <class T> friend void A<T>::g(T t);
1201 template <class T> template <T t> friend void A<T>::h();
1202
1203 and DECL's are
1204
1205 void A<int>::g(int);
1206 template <int> void A<int>::h();
1207
1208 we need to figure out ARGS, the template arguments from
1209 the context of DECL. This is required for template substitution
1210 of `T' in the function parameter of `g' and template parameter
1211 of `h' in the above examples. Here ARGS corresponds to `int'. */
1212
1213 tree context = DECL_CONTEXT (decl);
1214 tree args = NULL_TREE;
1215 int current_depth = 0;
1216
1217 while (current_depth < template_depth)
1218 {
1219 if (CLASSTYPE_TEMPLATE_INFO (context))
1220 {
1221 if (current_depth == 0)
1222 args = TYPE_TI_ARGS (context);
1223 else
1224 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1225 current_depth++;
1226 }
1227 context = TYPE_CONTEXT (context);
1228 }
1229
1230 if (TREE_CODE (decl) == FUNCTION_DECL)
1231 {
1232 bool is_template;
1233 tree friend_type;
1234 tree decl_type;
1235 tree friend_args_type;
1236 tree decl_args_type;
1237
1238 /* Make sure that both DECL and FRIEND_DECL are templates or
1239 non-templates. */
1240 is_template = DECL_TEMPLATE_INFO (decl)
1241 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1242 if (need_template ^ is_template)
1243 return false;
1244 else if (is_template)
1245 {
1246 /* If both are templates, check template parameter list. */
1247 tree friend_parms
1248 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1249 args, tf_none);
1250 if (!comp_template_parms
1251 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1252 friend_parms))
1253 return false;
1254
1255 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1256 }
1257 else
1258 decl_type = TREE_TYPE (decl);
1259
1260 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1261 tf_none, NULL_TREE);
1262 if (friend_type == error_mark_node)
1263 return false;
1264
1265 /* Check if return types match. */
1266 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1267 return false;
1268
1269 /* Check if function parameter types match, ignoring the
1270 `this' parameter. */
1271 friend_args_type = TYPE_ARG_TYPES (friend_type);
1272 decl_args_type = TYPE_ARG_TYPES (decl_type);
1273 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1274 friend_args_type = TREE_CHAIN (friend_args_type);
1275 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1276 decl_args_type = TREE_CHAIN (decl_args_type);
1277
1278 return compparms (decl_args_type, friend_args_type);
1279 }
1280 else
1281 {
1282 /* DECL is a TYPE_DECL */
1283 bool is_template;
1284 tree decl_type = TREE_TYPE (decl);
1285
1286 /* Make sure that both DECL and FRIEND_DECL are templates or
1287 non-templates. */
1288 is_template
1289 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1290 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1291
1292 if (need_template ^ is_template)
1293 return false;
1294 else if (is_template)
1295 {
1296 tree friend_parms;
1297 /* If both are templates, check the name of the two
1298 TEMPLATE_DECL's first because is_friend didn't. */
1299 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1300 != DECL_NAME (friend_decl))
1301 return false;
1302
1303 /* Now check template parameter list. */
1304 friend_parms
1305 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1306 args, tf_none);
1307 return comp_template_parms
1308 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1309 friend_parms);
1310 }
1311 else
1312 return (DECL_NAME (decl)
1313 == DECL_NAME (friend_decl));
1314 }
1315 }
1316 return false;
1317 }
1318
1319 /* Register the specialization SPEC as a specialization of TMPL with
1320 the indicated ARGS. IS_FRIEND indicates whether the specialization
1321 is actually just a friend declaration. Returns SPEC, or an
1322 equivalent prior declaration, if available.
1323
1324 We also store instantiations of field packs in the hash table, even
1325 though they are not themselves templates, to make lookup easier. */
1326
1327 static tree
1328 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1329 hashval_t hash)
1330 {
1331 tree fn;
1332 void **slot = NULL;
1333 spec_entry elt;
1334
1335 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1336 || (TREE_CODE (tmpl) == FIELD_DECL
1337 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1338
1339 if (TREE_CODE (spec) == FUNCTION_DECL
1340 && uses_template_parms (DECL_TI_ARGS (spec)))
1341 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1342 register it; we want the corresponding TEMPLATE_DECL instead.
1343 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1344 the more obvious `uses_template_parms (spec)' to avoid problems
1345 with default function arguments. In particular, given
1346 something like this:
1347
1348 template <class T> void f(T t1, T t = T())
1349
1350 the default argument expression is not substituted for in an
1351 instantiation unless and until it is actually needed. */
1352 return spec;
1353
1354 if (optimize_specialization_lookup_p (tmpl))
1355 /* We don't put these specializations in the hash table, but we might
1356 want to give an error about a mismatch. */
1357 fn = retrieve_specialization (tmpl, args, 0);
1358 else
1359 {
1360 elt.tmpl = tmpl;
1361 elt.args = args;
1362 elt.spec = spec;
1363
1364 if (hash == 0)
1365 hash = hash_specialization (&elt);
1366
1367 slot =
1368 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1369 if (*slot)
1370 fn = ((spec_entry *) *slot)->spec;
1371 else
1372 fn = NULL_TREE;
1373 }
1374
1375 /* We can sometimes try to re-register a specialization that we've
1376 already got. In particular, regenerate_decl_from_template calls
1377 duplicate_decls which will update the specialization list. But,
1378 we'll still get called again here anyhow. It's more convenient
1379 to simply allow this than to try to prevent it. */
1380 if (fn == spec)
1381 return spec;
1382 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1383 {
1384 if (DECL_TEMPLATE_INSTANTIATION (fn))
1385 {
1386 if (DECL_ODR_USED (fn)
1387 || DECL_EXPLICIT_INSTANTIATION (fn))
1388 {
1389 error ("specialization of %qD after instantiation",
1390 fn);
1391 return error_mark_node;
1392 }
1393 else
1394 {
1395 tree clone;
1396 /* This situation should occur only if the first
1397 specialization is an implicit instantiation, the
1398 second is an explicit specialization, and the
1399 implicit instantiation has not yet been used. That
1400 situation can occur if we have implicitly
1401 instantiated a member function and then specialized
1402 it later.
1403
1404 We can also wind up here if a friend declaration that
1405 looked like an instantiation turns out to be a
1406 specialization:
1407
1408 template <class T> void foo(T);
1409 class S { friend void foo<>(int) };
1410 template <> void foo(int);
1411
1412 We transform the existing DECL in place so that any
1413 pointers to it become pointers to the updated
1414 declaration.
1415
1416 If there was a definition for the template, but not
1417 for the specialization, we want this to look as if
1418 there were no definition, and vice versa. */
1419 DECL_INITIAL (fn) = NULL_TREE;
1420 duplicate_decls (spec, fn, is_friend);
1421 /* The call to duplicate_decls will have applied
1422 [temp.expl.spec]:
1423
1424 An explicit specialization of a function template
1425 is inline only if it is explicitly declared to be,
1426 and independently of whether its function template
1427 is.
1428
1429 to the primary function; now copy the inline bits to
1430 the various clones. */
1431 FOR_EACH_CLONE (clone, fn)
1432 {
1433 DECL_DECLARED_INLINE_P (clone)
1434 = DECL_DECLARED_INLINE_P (fn);
1435 DECL_SOURCE_LOCATION (clone)
1436 = DECL_SOURCE_LOCATION (fn);
1437 }
1438 check_specialization_namespace (tmpl);
1439
1440 return fn;
1441 }
1442 }
1443 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1444 {
1445 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1446 /* Dup decl failed, but this is a new definition. Set the
1447 line number so any errors match this new
1448 definition. */
1449 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1450
1451 return fn;
1452 }
1453 }
1454 else if (fn)
1455 return duplicate_decls (spec, fn, is_friend);
1456
1457 /* A specialization must be declared in the same namespace as the
1458 template it is specializing. */
1459 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1460 && !check_specialization_namespace (tmpl))
1461 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1462
1463 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1464 {
1465 spec_entry *entry = ggc_alloc_spec_entry ();
1466 gcc_assert (tmpl && args && spec);
1467 *entry = elt;
1468 *slot = entry;
1469 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1470 && PRIMARY_TEMPLATE_P (tmpl)
1471 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1472 /* TMPL is a forward declaration of a template function; keep a list
1473 of all specializations in case we need to reassign them to a friend
1474 template later in tsubst_friend_function. */
1475 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1476 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1477 }
1478
1479 return spec;
1480 }
1481
1482 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1483 TMPL and ARGS members, ignores SPEC. */
1484
1485 int comparing_specializations;
1486
1487 static int
1488 eq_specializations (const void *p1, const void *p2)
1489 {
1490 const spec_entry *e1 = (const spec_entry *)p1;
1491 const spec_entry *e2 = (const spec_entry *)p2;
1492 int equal;
1493
1494 ++comparing_specializations;
1495 equal = (e1->tmpl == e2->tmpl
1496 && comp_template_args (e1->args, e2->args));
1497 --comparing_specializations;
1498
1499 return equal;
1500 }
1501
1502 /* Returns a hash for a template TMPL and template arguments ARGS. */
1503
1504 static hashval_t
1505 hash_tmpl_and_args (tree tmpl, tree args)
1506 {
1507 hashval_t val = DECL_UID (tmpl);
1508 return iterative_hash_template_arg (args, val);
1509 }
1510
1511 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1512 ignoring SPEC. */
1513
1514 static hashval_t
1515 hash_specialization (const void *p)
1516 {
1517 const spec_entry *e = (const spec_entry *)p;
1518 return hash_tmpl_and_args (e->tmpl, e->args);
1519 }
1520
1521 /* Recursively calculate a hash value for a template argument ARG, for use
1522 in the hash tables of template specializations. */
1523
1524 hashval_t
1525 iterative_hash_template_arg (tree arg, hashval_t val)
1526 {
1527 unsigned HOST_WIDE_INT i;
1528 enum tree_code code;
1529 char tclass;
1530
1531 if (arg == NULL_TREE)
1532 return iterative_hash_object (arg, val);
1533
1534 if (!TYPE_P (arg))
1535 STRIP_NOPS (arg);
1536
1537 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1538 /* We can get one of these when re-hashing a previous entry in the middle
1539 of substituting into a pack expansion. Just look through it. */
1540 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1541
1542 code = TREE_CODE (arg);
1543 tclass = TREE_CODE_CLASS (code);
1544
1545 val = iterative_hash_object (code, val);
1546
1547 switch (code)
1548 {
1549 case ERROR_MARK:
1550 return val;
1551
1552 case IDENTIFIER_NODE:
1553 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1554
1555 case TREE_VEC:
1556 {
1557 int i, len = TREE_VEC_LENGTH (arg);
1558 for (i = 0; i < len; ++i)
1559 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1560 return val;
1561 }
1562
1563 case TYPE_PACK_EXPANSION:
1564 case EXPR_PACK_EXPANSION:
1565 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1566 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1567
1568 case TYPE_ARGUMENT_PACK:
1569 case NONTYPE_ARGUMENT_PACK:
1570 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1571
1572 case TREE_LIST:
1573 for (; arg; arg = TREE_CHAIN (arg))
1574 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1575 return val;
1576
1577 case OVERLOAD:
1578 for (; arg; arg = OVL_NEXT (arg))
1579 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1580 return val;
1581
1582 case CONSTRUCTOR:
1583 {
1584 tree field, value;
1585 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1586 {
1587 val = iterative_hash_template_arg (field, val);
1588 val = iterative_hash_template_arg (value, val);
1589 }
1590 return val;
1591 }
1592
1593 case PARM_DECL:
1594 if (!DECL_ARTIFICIAL (arg))
1595 {
1596 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1597 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1598 }
1599 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1600
1601 case TARGET_EXPR:
1602 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1603
1604 case PTRMEM_CST:
1605 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1606 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1607
1608 case TEMPLATE_PARM_INDEX:
1609 val = iterative_hash_template_arg
1610 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1611 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1612 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1613
1614 case TRAIT_EXPR:
1615 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1616 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1617 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1618
1619 case BASELINK:
1620 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1621 val);
1622 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1623 val);
1624
1625 case MODOP_EXPR:
1626 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1627 code = TREE_CODE (TREE_OPERAND (arg, 1));
1628 val = iterative_hash_object (code, val);
1629 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1630
1631 case LAMBDA_EXPR:
1632 /* A lambda can't appear in a template arg, but don't crash on
1633 erroneous input. */
1634 gcc_assert (seen_error ());
1635 return val;
1636
1637 case CAST_EXPR:
1638 case IMPLICIT_CONV_EXPR:
1639 case STATIC_CAST_EXPR:
1640 case REINTERPRET_CAST_EXPR:
1641 case CONST_CAST_EXPR:
1642 case DYNAMIC_CAST_EXPR:
1643 case NEW_EXPR:
1644 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1645 /* Now hash operands as usual. */
1646 break;
1647
1648 default:
1649 break;
1650 }
1651
1652 switch (tclass)
1653 {
1654 case tcc_type:
1655 if (TYPE_CANONICAL (arg))
1656 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1657 val);
1658 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1659 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1660 /* Otherwise just compare the types during lookup. */
1661 return val;
1662
1663 case tcc_declaration:
1664 case tcc_constant:
1665 return iterative_hash_expr (arg, val);
1666
1667 default:
1668 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1669 {
1670 unsigned n = cp_tree_operand_length (arg);
1671 for (i = 0; i < n; ++i)
1672 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1673 return val;
1674 }
1675 }
1676 gcc_unreachable ();
1677 return 0;
1678 }
1679
1680 /* Unregister the specialization SPEC as a specialization of TMPL.
1681 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1682 if the SPEC was listed as a specialization of TMPL.
1683
1684 Note that SPEC has been ggc_freed, so we can't look inside it. */
1685
1686 bool
1687 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1688 {
1689 spec_entry *entry;
1690 spec_entry elt;
1691
1692 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1693 elt.args = TI_ARGS (tinfo);
1694 elt.spec = NULL_TREE;
1695
1696 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1697 if (entry != NULL)
1698 {
1699 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1700 gcc_assert (new_spec != NULL_TREE);
1701 entry->spec = new_spec;
1702 return 1;
1703 }
1704
1705 return 0;
1706 }
1707
1708 /* Like register_specialization, but for local declarations. We are
1709 registering SPEC, an instantiation of TMPL. */
1710
1711 static void
1712 register_local_specialization (tree spec, tree tmpl)
1713 {
1714 void **slot;
1715
1716 slot = pointer_map_insert (local_specializations, tmpl);
1717 *slot = spec;
1718 }
1719
1720 /* TYPE is a class type. Returns true if TYPE is an explicitly
1721 specialized class. */
1722
1723 bool
1724 explicit_class_specialization_p (tree type)
1725 {
1726 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1727 return false;
1728 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1729 }
1730
1731 /* Print the list of functions at FNS, going through all the overloads
1732 for each element of the list. Alternatively, FNS can not be a
1733 TREE_LIST, in which case it will be printed together with all the
1734 overloads.
1735
1736 MORE and *STR should respectively be FALSE and NULL when the function
1737 is called from the outside. They are used internally on recursive
1738 calls. print_candidates manages the two parameters and leaves NULL
1739 in *STR when it ends. */
1740
1741 static void
1742 print_candidates_1 (tree fns, bool more, const char **str)
1743 {
1744 tree fn, fn2;
1745 char *spaces = NULL;
1746
1747 for (fn = fns; fn; fn = OVL_NEXT (fn))
1748 if (TREE_CODE (fn) == TREE_LIST)
1749 {
1750 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1751 print_candidates_1 (TREE_VALUE (fn2),
1752 TREE_CHAIN (fn2) || more, str);
1753 }
1754 else
1755 {
1756 tree cand = OVL_CURRENT (fn);
1757 if (!*str)
1758 {
1759 /* Pick the prefix string. */
1760 if (!more && !OVL_NEXT (fns))
1761 {
1762 inform (DECL_SOURCE_LOCATION (cand),
1763 "candidate is: %#D", cand);
1764 continue;
1765 }
1766
1767 *str = _("candidates are:");
1768 spaces = get_spaces (*str);
1769 }
1770 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1771 *str = spaces ? spaces : *str;
1772 }
1773
1774 if (!more)
1775 {
1776 free (spaces);
1777 *str = NULL;
1778 }
1779 }
1780
1781 /* Print the list of candidate FNS in an error message. FNS can also
1782 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1783
1784 void
1785 print_candidates (tree fns)
1786 {
1787 const char *str = NULL;
1788 print_candidates_1 (fns, false, &str);
1789 gcc_assert (str == NULL);
1790 }
1791
1792 /* Returns the template (one of the functions given by TEMPLATE_ID)
1793 which can be specialized to match the indicated DECL with the
1794 explicit template args given in TEMPLATE_ID. The DECL may be
1795 NULL_TREE if none is available. In that case, the functions in
1796 TEMPLATE_ID are non-members.
1797
1798 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1799 specialization of a member template.
1800
1801 The TEMPLATE_COUNT is the number of references to qualifying
1802 template classes that appeared in the name of the function. See
1803 check_explicit_specialization for a more accurate description.
1804
1805 TSK indicates what kind of template declaration (if any) is being
1806 declared. TSK_TEMPLATE indicates that the declaration given by
1807 DECL, though a FUNCTION_DECL, has template parameters, and is
1808 therefore a template function.
1809
1810 The template args (those explicitly specified and those deduced)
1811 are output in a newly created vector *TARGS_OUT.
1812
1813 If it is impossible to determine the result, an error message is
1814 issued. The error_mark_node is returned to indicate failure. */
1815
1816 static tree
1817 determine_specialization (tree template_id,
1818 tree decl,
1819 tree* targs_out,
1820 int need_member_template,
1821 int template_count,
1822 tmpl_spec_kind tsk)
1823 {
1824 tree fns;
1825 tree targs;
1826 tree explicit_targs;
1827 tree candidates = NULL_TREE;
1828 /* A TREE_LIST of templates of which DECL may be a specialization.
1829 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1830 corresponding TREE_PURPOSE is the set of template arguments that,
1831 when used to instantiate the template, would produce a function
1832 with the signature of DECL. */
1833 tree templates = NULL_TREE;
1834 int header_count;
1835 cp_binding_level *b;
1836
1837 *targs_out = NULL_TREE;
1838
1839 if (template_id == error_mark_node || decl == error_mark_node)
1840 return error_mark_node;
1841
1842 /* We shouldn't be specializing a member template of an
1843 unspecialized class template; we already gave an error in
1844 check_specialization_scope, now avoid crashing. */
1845 if (template_count && DECL_CLASS_SCOPE_P (decl)
1846 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1847 {
1848 gcc_assert (errorcount);
1849 return error_mark_node;
1850 }
1851
1852 fns = TREE_OPERAND (template_id, 0);
1853 explicit_targs = TREE_OPERAND (template_id, 1);
1854
1855 if (fns == error_mark_node)
1856 return error_mark_node;
1857
1858 /* Check for baselinks. */
1859 if (BASELINK_P (fns))
1860 fns = BASELINK_FUNCTIONS (fns);
1861
1862 if (!is_overloaded_fn (fns))
1863 {
1864 error ("%qD is not a function template", fns);
1865 return error_mark_node;
1866 }
1867
1868 /* Count the number of template headers specified for this
1869 specialization. */
1870 header_count = 0;
1871 for (b = current_binding_level;
1872 b->kind == sk_template_parms;
1873 b = b->level_chain)
1874 ++header_count;
1875
1876 for (; fns; fns = OVL_NEXT (fns))
1877 {
1878 tree fn = OVL_CURRENT (fns);
1879
1880 if (TREE_CODE (fn) == TEMPLATE_DECL)
1881 {
1882 tree decl_arg_types;
1883 tree fn_arg_types;
1884 tree insttype;
1885
1886 /* In case of explicit specialization, we need to check if
1887 the number of template headers appearing in the specialization
1888 is correct. This is usually done in check_explicit_specialization,
1889 but the check done there cannot be exhaustive when specializing
1890 member functions. Consider the following code:
1891
1892 template <> void A<int>::f(int);
1893 template <> template <> void A<int>::f(int);
1894
1895 Assuming that A<int> is not itself an explicit specialization
1896 already, the first line specializes "f" which is a non-template
1897 member function, whilst the second line specializes "f" which
1898 is a template member function. So both lines are syntactically
1899 correct, and check_explicit_specialization does not reject
1900 them.
1901
1902 Here, we can do better, as we are matching the specialization
1903 against the declarations. We count the number of template
1904 headers, and we check if they match TEMPLATE_COUNT + 1
1905 (TEMPLATE_COUNT is the number of qualifying template classes,
1906 plus there must be another header for the member template
1907 itself).
1908
1909 Notice that if header_count is zero, this is not a
1910 specialization but rather a template instantiation, so there
1911 is no check we can perform here. */
1912 if (header_count && header_count != template_count + 1)
1913 continue;
1914
1915 /* Check that the number of template arguments at the
1916 innermost level for DECL is the same as for FN. */
1917 if (current_binding_level->kind == sk_template_parms
1918 && !current_binding_level->explicit_spec_p
1919 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1920 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1921 (current_template_parms))))
1922 continue;
1923
1924 /* DECL might be a specialization of FN. */
1925 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1926 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1927
1928 /* For a non-static member function, we need to make sure
1929 that the const qualification is the same. Since
1930 get_bindings does not try to merge the "this" parameter,
1931 we must do the comparison explicitly. */
1932 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1933 && !same_type_p (TREE_VALUE (fn_arg_types),
1934 TREE_VALUE (decl_arg_types)))
1935 continue;
1936
1937 /* Skip the "this" parameter and, for constructors of
1938 classes with virtual bases, the VTT parameter. A
1939 full specialization of a constructor will have a VTT
1940 parameter, but a template never will. */
1941 decl_arg_types
1942 = skip_artificial_parms_for (decl, decl_arg_types);
1943 fn_arg_types
1944 = skip_artificial_parms_for (fn, fn_arg_types);
1945
1946 /* Function templates cannot be specializations; there are
1947 no partial specializations of functions. Therefore, if
1948 the type of DECL does not match FN, there is no
1949 match. */
1950 if (tsk == tsk_template)
1951 {
1952 if (compparms (fn_arg_types, decl_arg_types))
1953 candidates = tree_cons (NULL_TREE, fn, candidates);
1954 continue;
1955 }
1956
1957 /* See whether this function might be a specialization of this
1958 template. Suppress access control because we might be trying
1959 to make this specialization a friend, and we have already done
1960 access control for the declaration of the specialization. */
1961 push_deferring_access_checks (dk_no_check);
1962 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1963 pop_deferring_access_checks ();
1964
1965 if (!targs)
1966 /* We cannot deduce template arguments that when used to
1967 specialize TMPL will produce DECL. */
1968 continue;
1969
1970 /* Make sure that the deduced arguments actually work. */
1971 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1972 if (insttype == error_mark_node)
1973 continue;
1974 fn_arg_types
1975 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1976 if (!compparms (fn_arg_types, decl_arg_types))
1977 continue;
1978
1979 /* Save this template, and the arguments deduced. */
1980 templates = tree_cons (targs, fn, templates);
1981 }
1982 else if (need_member_template)
1983 /* FN is an ordinary member function, and we need a
1984 specialization of a member template. */
1985 ;
1986 else if (TREE_CODE (fn) != FUNCTION_DECL)
1987 /* We can get IDENTIFIER_NODEs here in certain erroneous
1988 cases. */
1989 ;
1990 else if (!DECL_FUNCTION_MEMBER_P (fn))
1991 /* This is just an ordinary non-member function. Nothing can
1992 be a specialization of that. */
1993 ;
1994 else if (DECL_ARTIFICIAL (fn))
1995 /* Cannot specialize functions that are created implicitly. */
1996 ;
1997 else
1998 {
1999 tree decl_arg_types;
2000
2001 /* This is an ordinary member function. However, since
2002 we're here, we can assume its enclosing class is a
2003 template class. For example,
2004
2005 template <typename T> struct S { void f(); };
2006 template <> void S<int>::f() {}
2007
2008 Here, S<int>::f is a non-template, but S<int> is a
2009 template class. If FN has the same type as DECL, we
2010 might be in business. */
2011
2012 if (!DECL_TEMPLATE_INFO (fn))
2013 /* Its enclosing class is an explicit specialization
2014 of a template class. This is not a candidate. */
2015 continue;
2016
2017 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2018 TREE_TYPE (TREE_TYPE (fn))))
2019 /* The return types differ. */
2020 continue;
2021
2022 /* Adjust the type of DECL in case FN is a static member. */
2023 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2024 if (DECL_STATIC_FUNCTION_P (fn)
2025 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2026 decl_arg_types = TREE_CHAIN (decl_arg_types);
2027
2028 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2029 decl_arg_types))
2030 /* They match! */
2031 candidates = tree_cons (NULL_TREE, fn, candidates);
2032 }
2033 }
2034
2035 if (templates && TREE_CHAIN (templates))
2036 {
2037 /* We have:
2038
2039 [temp.expl.spec]
2040
2041 It is possible for a specialization with a given function
2042 signature to be instantiated from more than one function
2043 template. In such cases, explicit specification of the
2044 template arguments must be used to uniquely identify the
2045 function template specialization being specialized.
2046
2047 Note that here, there's no suggestion that we're supposed to
2048 determine which of the candidate templates is most
2049 specialized. However, we, also have:
2050
2051 [temp.func.order]
2052
2053 Partial ordering of overloaded function template
2054 declarations is used in the following contexts to select
2055 the function template to which a function template
2056 specialization refers:
2057
2058 -- when an explicit specialization refers to a function
2059 template.
2060
2061 So, we do use the partial ordering rules, at least for now.
2062 This extension can only serve to make invalid programs valid,
2063 so it's safe. And, there is strong anecdotal evidence that
2064 the committee intended the partial ordering rules to apply;
2065 the EDG front end has that behavior, and John Spicer claims
2066 that the committee simply forgot to delete the wording in
2067 [temp.expl.spec]. */
2068 tree tmpl = most_specialized_instantiation (templates);
2069 if (tmpl != error_mark_node)
2070 {
2071 templates = tmpl;
2072 TREE_CHAIN (templates) = NULL_TREE;
2073 }
2074 }
2075
2076 if (templates == NULL_TREE && candidates == NULL_TREE)
2077 {
2078 error ("template-id %qD for %q+D does not match any template "
2079 "declaration", template_id, decl);
2080 if (header_count && header_count != template_count + 1)
2081 inform (input_location, "saw %d %<template<>%>, need %d for "
2082 "specializing a member function template",
2083 header_count, template_count + 1);
2084 return error_mark_node;
2085 }
2086 else if ((templates && TREE_CHAIN (templates))
2087 || (candidates && TREE_CHAIN (candidates))
2088 || (templates && candidates))
2089 {
2090 error ("ambiguous template specialization %qD for %q+D",
2091 template_id, decl);
2092 candidates = chainon (candidates, templates);
2093 print_candidates (candidates);
2094 return error_mark_node;
2095 }
2096
2097 /* We have one, and exactly one, match. */
2098 if (candidates)
2099 {
2100 tree fn = TREE_VALUE (candidates);
2101 *targs_out = copy_node (DECL_TI_ARGS (fn));
2102 /* DECL is a re-declaration or partial instantiation of a template
2103 function. */
2104 if (TREE_CODE (fn) == TEMPLATE_DECL)
2105 return fn;
2106 /* It was a specialization of an ordinary member function in a
2107 template class. */
2108 return DECL_TI_TEMPLATE (fn);
2109 }
2110
2111 /* It was a specialization of a template. */
2112 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2113 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2114 {
2115 *targs_out = copy_node (targs);
2116 SET_TMPL_ARGS_LEVEL (*targs_out,
2117 TMPL_ARGS_DEPTH (*targs_out),
2118 TREE_PURPOSE (templates));
2119 }
2120 else
2121 *targs_out = TREE_PURPOSE (templates);
2122 return TREE_VALUE (templates);
2123 }
2124
2125 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2126 but with the default argument values filled in from those in the
2127 TMPL_TYPES. */
2128
2129 static tree
2130 copy_default_args_to_explicit_spec_1 (tree spec_types,
2131 tree tmpl_types)
2132 {
2133 tree new_spec_types;
2134
2135 if (!spec_types)
2136 return NULL_TREE;
2137
2138 if (spec_types == void_list_node)
2139 return void_list_node;
2140
2141 /* Substitute into the rest of the list. */
2142 new_spec_types =
2143 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2144 TREE_CHAIN (tmpl_types));
2145
2146 /* Add the default argument for this parameter. */
2147 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2148 TREE_VALUE (spec_types),
2149 new_spec_types);
2150 }
2151
2152 /* DECL is an explicit specialization. Replicate default arguments
2153 from the template it specializes. (That way, code like:
2154
2155 template <class T> void f(T = 3);
2156 template <> void f(double);
2157 void g () { f (); }
2158
2159 works, as required.) An alternative approach would be to look up
2160 the correct default arguments at the call-site, but this approach
2161 is consistent with how implicit instantiations are handled. */
2162
2163 static void
2164 copy_default_args_to_explicit_spec (tree decl)
2165 {
2166 tree tmpl;
2167 tree spec_types;
2168 tree tmpl_types;
2169 tree new_spec_types;
2170 tree old_type;
2171 tree new_type;
2172 tree t;
2173 tree object_type = NULL_TREE;
2174 tree in_charge = NULL_TREE;
2175 tree vtt = NULL_TREE;
2176
2177 /* See if there's anything we need to do. */
2178 tmpl = DECL_TI_TEMPLATE (decl);
2179 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2180 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2181 if (TREE_PURPOSE (t))
2182 break;
2183 if (!t)
2184 return;
2185
2186 old_type = TREE_TYPE (decl);
2187 spec_types = TYPE_ARG_TYPES (old_type);
2188
2189 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2190 {
2191 /* Remove the this pointer, but remember the object's type for
2192 CV quals. */
2193 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2194 spec_types = TREE_CHAIN (spec_types);
2195 tmpl_types = TREE_CHAIN (tmpl_types);
2196
2197 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2198 {
2199 /* DECL may contain more parameters than TMPL due to the extra
2200 in-charge parameter in constructors and destructors. */
2201 in_charge = spec_types;
2202 spec_types = TREE_CHAIN (spec_types);
2203 }
2204 if (DECL_HAS_VTT_PARM_P (decl))
2205 {
2206 vtt = spec_types;
2207 spec_types = TREE_CHAIN (spec_types);
2208 }
2209 }
2210
2211 /* Compute the merged default arguments. */
2212 new_spec_types =
2213 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2214
2215 /* Compute the new FUNCTION_TYPE. */
2216 if (object_type)
2217 {
2218 if (vtt)
2219 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2220 TREE_VALUE (vtt),
2221 new_spec_types);
2222
2223 if (in_charge)
2224 /* Put the in-charge parameter back. */
2225 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2226 TREE_VALUE (in_charge),
2227 new_spec_types);
2228
2229 new_type = build_method_type_directly (object_type,
2230 TREE_TYPE (old_type),
2231 new_spec_types);
2232 }
2233 else
2234 new_type = build_function_type (TREE_TYPE (old_type),
2235 new_spec_types);
2236 new_type = cp_build_type_attribute_variant (new_type,
2237 TYPE_ATTRIBUTES (old_type));
2238 new_type = build_exception_variant (new_type,
2239 TYPE_RAISES_EXCEPTIONS (old_type));
2240 TREE_TYPE (decl) = new_type;
2241 }
2242
2243 /* Return the number of template headers we expect to see for a definition
2244 or specialization of CTYPE or one of its non-template members. */
2245
2246 int
2247 num_template_headers_for_class (tree ctype)
2248 {
2249 int num_templates = 0;
2250
2251 while (ctype && CLASS_TYPE_P (ctype))
2252 {
2253 /* You're supposed to have one `template <...>' for every
2254 template class, but you don't need one for a full
2255 specialization. For example:
2256
2257 template <class T> struct S{};
2258 template <> struct S<int> { void f(); };
2259 void S<int>::f () {}
2260
2261 is correct; there shouldn't be a `template <>' for the
2262 definition of `S<int>::f'. */
2263 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2264 /* If CTYPE does not have template information of any
2265 kind, then it is not a template, nor is it nested
2266 within a template. */
2267 break;
2268 if (explicit_class_specialization_p (ctype))
2269 break;
2270 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2271 ++num_templates;
2272
2273 ctype = TYPE_CONTEXT (ctype);
2274 }
2275
2276 return num_templates;
2277 }
2278
2279 /* Do a simple sanity check on the template headers that precede the
2280 variable declaration DECL. */
2281
2282 void
2283 check_template_variable (tree decl)
2284 {
2285 tree ctx = CP_DECL_CONTEXT (decl);
2286 int wanted = num_template_headers_for_class (ctx);
2287 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2288 permerror (DECL_SOURCE_LOCATION (decl),
2289 "%qD is not a static data member of a class template", decl);
2290 else if (template_header_count > wanted)
2291 {
2292 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2293 "too many template headers for %D (should be %d)",
2294 decl, wanted);
2295 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2296 inform (DECL_SOURCE_LOCATION (decl),
2297 "members of an explicitly specialized class are defined "
2298 "without a template header");
2299 }
2300 }
2301
2302 /* Check to see if the function just declared, as indicated in
2303 DECLARATOR, and in DECL, is a specialization of a function
2304 template. We may also discover that the declaration is an explicit
2305 instantiation at this point.
2306
2307 Returns DECL, or an equivalent declaration that should be used
2308 instead if all goes well. Issues an error message if something is
2309 amiss. Returns error_mark_node if the error is not easily
2310 recoverable.
2311
2312 FLAGS is a bitmask consisting of the following flags:
2313
2314 2: The function has a definition.
2315 4: The function is a friend.
2316
2317 The TEMPLATE_COUNT is the number of references to qualifying
2318 template classes that appeared in the name of the function. For
2319 example, in
2320
2321 template <class T> struct S { void f(); };
2322 void S<int>::f();
2323
2324 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2325 classes are not counted in the TEMPLATE_COUNT, so that in
2326
2327 template <class T> struct S {};
2328 template <> struct S<int> { void f(); }
2329 template <> void S<int>::f();
2330
2331 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2332 invalid; there should be no template <>.)
2333
2334 If the function is a specialization, it is marked as such via
2335 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2336 is set up correctly, and it is added to the list of specializations
2337 for that template. */
2338
2339 tree
2340 check_explicit_specialization (tree declarator,
2341 tree decl,
2342 int template_count,
2343 int flags)
2344 {
2345 int have_def = flags & 2;
2346 int is_friend = flags & 4;
2347 int specialization = 0;
2348 int explicit_instantiation = 0;
2349 int member_specialization = 0;
2350 tree ctype = DECL_CLASS_CONTEXT (decl);
2351 tree dname = DECL_NAME (decl);
2352 tmpl_spec_kind tsk;
2353
2354 if (is_friend)
2355 {
2356 if (!processing_specialization)
2357 tsk = tsk_none;
2358 else
2359 tsk = tsk_excessive_parms;
2360 }
2361 else
2362 tsk = current_tmpl_spec_kind (template_count);
2363
2364 switch (tsk)
2365 {
2366 case tsk_none:
2367 if (processing_specialization)
2368 {
2369 specialization = 1;
2370 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2371 }
2372 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2373 {
2374 if (is_friend)
2375 /* This could be something like:
2376
2377 template <class T> void f(T);
2378 class S { friend void f<>(int); } */
2379 specialization = 1;
2380 else
2381 {
2382 /* This case handles bogus declarations like template <>
2383 template <class T> void f<int>(); */
2384
2385 error ("template-id %qD in declaration of primary template",
2386 declarator);
2387 return decl;
2388 }
2389 }
2390 break;
2391
2392 case tsk_invalid_member_spec:
2393 /* The error has already been reported in
2394 check_specialization_scope. */
2395 return error_mark_node;
2396
2397 case tsk_invalid_expl_inst:
2398 error ("template parameter list used in explicit instantiation");
2399
2400 /* Fall through. */
2401
2402 case tsk_expl_inst:
2403 if (have_def)
2404 error ("definition provided for explicit instantiation");
2405
2406 explicit_instantiation = 1;
2407 break;
2408
2409 case tsk_excessive_parms:
2410 case tsk_insufficient_parms:
2411 if (tsk == tsk_excessive_parms)
2412 error ("too many template parameter lists in declaration of %qD",
2413 decl);
2414 else if (template_header_count)
2415 error("too few template parameter lists in declaration of %qD", decl);
2416 else
2417 error("explicit specialization of %qD must be introduced by "
2418 "%<template <>%>", decl);
2419
2420 /* Fall through. */
2421 case tsk_expl_spec:
2422 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2423 if (ctype)
2424 member_specialization = 1;
2425 else
2426 specialization = 1;
2427 break;
2428
2429 case tsk_template:
2430 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2431 {
2432 /* This case handles bogus declarations like template <>
2433 template <class T> void f<int>(); */
2434
2435 if (uses_template_parms (declarator))
2436 error ("function template partial specialization %qD "
2437 "is not allowed", declarator);
2438 else
2439 error ("template-id %qD in declaration of primary template",
2440 declarator);
2441 return decl;
2442 }
2443
2444 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2445 /* This is a specialization of a member template, without
2446 specialization the containing class. Something like:
2447
2448 template <class T> struct S {
2449 template <class U> void f (U);
2450 };
2451 template <> template <class U> void S<int>::f(U) {}
2452
2453 That's a specialization -- but of the entire template. */
2454 specialization = 1;
2455 break;
2456
2457 default:
2458 gcc_unreachable ();
2459 }
2460
2461 if (specialization || member_specialization)
2462 {
2463 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2464 for (; t; t = TREE_CHAIN (t))
2465 if (TREE_PURPOSE (t))
2466 {
2467 permerror (input_location,
2468 "default argument specified in explicit specialization");
2469 break;
2470 }
2471 }
2472
2473 if (specialization || member_specialization || explicit_instantiation)
2474 {
2475 tree tmpl = NULL_TREE;
2476 tree targs = NULL_TREE;
2477
2478 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2479 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2480 {
2481 tree fns;
2482
2483 gcc_assert (identifier_p (declarator));
2484 if (ctype)
2485 fns = dname;
2486 else
2487 {
2488 /* If there is no class context, the explicit instantiation
2489 must be at namespace scope. */
2490 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2491
2492 /* Find the namespace binding, using the declaration
2493 context. */
2494 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2495 false, true);
2496 if (fns == error_mark_node || !is_overloaded_fn (fns))
2497 {
2498 error ("%qD is not a template function", dname);
2499 fns = error_mark_node;
2500 }
2501 else
2502 {
2503 tree fn = OVL_CURRENT (fns);
2504 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2505 CP_DECL_CONTEXT (fn)))
2506 error ("%qD is not declared in %qD",
2507 decl, current_namespace);
2508 }
2509 }
2510
2511 declarator = lookup_template_function (fns, NULL_TREE);
2512 }
2513
2514 if (declarator == error_mark_node)
2515 return error_mark_node;
2516
2517 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2518 {
2519 if (!explicit_instantiation)
2520 /* A specialization in class scope. This is invalid,
2521 but the error will already have been flagged by
2522 check_specialization_scope. */
2523 return error_mark_node;
2524 else
2525 {
2526 /* It's not valid to write an explicit instantiation in
2527 class scope, e.g.:
2528
2529 class C { template void f(); }
2530
2531 This case is caught by the parser. However, on
2532 something like:
2533
2534 template class C { void f(); };
2535
2536 (which is invalid) we can get here. The error will be
2537 issued later. */
2538 ;
2539 }
2540
2541 return decl;
2542 }
2543 else if (ctype != NULL_TREE
2544 && (identifier_p (TREE_OPERAND (declarator, 0))))
2545 {
2546 /* Find the list of functions in ctype that have the same
2547 name as the declared function. */
2548 tree name = TREE_OPERAND (declarator, 0);
2549 tree fns = NULL_TREE;
2550 int idx;
2551
2552 if (constructor_name_p (name, ctype))
2553 {
2554 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2555
2556 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2557 : !CLASSTYPE_DESTRUCTORS (ctype))
2558 {
2559 /* From [temp.expl.spec]:
2560
2561 If such an explicit specialization for the member
2562 of a class template names an implicitly-declared
2563 special member function (clause _special_), the
2564 program is ill-formed.
2565
2566 Similar language is found in [temp.explicit]. */
2567 error ("specialization of implicitly-declared special member function");
2568 return error_mark_node;
2569 }
2570
2571 name = is_constructor ? ctor_identifier : dtor_identifier;
2572 }
2573
2574 if (!DECL_CONV_FN_P (decl))
2575 {
2576 idx = lookup_fnfields_1 (ctype, name);
2577 if (idx >= 0)
2578 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2579 }
2580 else
2581 {
2582 vec<tree, va_gc> *methods;
2583 tree ovl;
2584
2585 /* For a type-conversion operator, we cannot do a
2586 name-based lookup. We might be looking for `operator
2587 int' which will be a specialization of `operator T'.
2588 So, we find *all* the conversion operators, and then
2589 select from them. */
2590 fns = NULL_TREE;
2591
2592 methods = CLASSTYPE_METHOD_VEC (ctype);
2593 if (methods)
2594 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2595 methods->iterate (idx, &ovl);
2596 ++idx)
2597 {
2598 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2599 /* There are no more conversion functions. */
2600 break;
2601
2602 /* Glue all these conversion functions together
2603 with those we already have. */
2604 for (; ovl; ovl = OVL_NEXT (ovl))
2605 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2606 }
2607 }
2608
2609 if (fns == NULL_TREE)
2610 {
2611 error ("no member function %qD declared in %qT", name, ctype);
2612 return error_mark_node;
2613 }
2614 else
2615 TREE_OPERAND (declarator, 0) = fns;
2616 }
2617
2618 /* Figure out what exactly is being specialized at this point.
2619 Note that for an explicit instantiation, even one for a
2620 member function, we cannot tell apriori whether the
2621 instantiation is for a member template, or just a member
2622 function of a template class. Even if a member template is
2623 being instantiated, the member template arguments may be
2624 elided if they can be deduced from the rest of the
2625 declaration. */
2626 tmpl = determine_specialization (declarator, decl,
2627 &targs,
2628 member_specialization,
2629 template_count,
2630 tsk);
2631
2632 if (!tmpl || tmpl == error_mark_node)
2633 /* We couldn't figure out what this declaration was
2634 specializing. */
2635 return error_mark_node;
2636 else
2637 {
2638 tree gen_tmpl = most_general_template (tmpl);
2639
2640 if (explicit_instantiation)
2641 {
2642 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2643 is done by do_decl_instantiation later. */
2644
2645 int arg_depth = TMPL_ARGS_DEPTH (targs);
2646 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2647
2648 if (arg_depth > parm_depth)
2649 {
2650 /* If TMPL is not the most general template (for
2651 example, if TMPL is a friend template that is
2652 injected into namespace scope), then there will
2653 be too many levels of TARGS. Remove some of them
2654 here. */
2655 int i;
2656 tree new_targs;
2657
2658 new_targs = make_tree_vec (parm_depth);
2659 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2660 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2661 = TREE_VEC_ELT (targs, i);
2662 targs = new_targs;
2663 }
2664
2665 return instantiate_template (tmpl, targs, tf_error);
2666 }
2667
2668 /* If we thought that the DECL was a member function, but it
2669 turns out to be specializing a static member function,
2670 make DECL a static member function as well. */
2671 if (DECL_STATIC_FUNCTION_P (tmpl)
2672 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2673 revert_static_member_fn (decl);
2674
2675 /* If this is a specialization of a member template of a
2676 template class, we want to return the TEMPLATE_DECL, not
2677 the specialization of it. */
2678 if (tsk == tsk_template)
2679 {
2680 tree result = DECL_TEMPLATE_RESULT (tmpl);
2681 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2682 DECL_INITIAL (result) = NULL_TREE;
2683 if (have_def)
2684 {
2685 tree parm;
2686 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2687 DECL_SOURCE_LOCATION (result)
2688 = DECL_SOURCE_LOCATION (decl);
2689 /* We want to use the argument list specified in the
2690 definition, not in the original declaration. */
2691 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2692 for (parm = DECL_ARGUMENTS (result); parm;
2693 parm = DECL_CHAIN (parm))
2694 DECL_CONTEXT (parm) = result;
2695 }
2696 return register_specialization (tmpl, gen_tmpl, targs,
2697 is_friend, 0);
2698 }
2699
2700 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2701 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2702
2703 /* Inherit default function arguments from the template
2704 DECL is specializing. */
2705 copy_default_args_to_explicit_spec (decl);
2706
2707 /* This specialization has the same protection as the
2708 template it specializes. */
2709 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2710 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2711
2712 /* 7.1.1-1 [dcl.stc]
2713
2714 A storage-class-specifier shall not be specified in an
2715 explicit specialization...
2716
2717 The parser rejects these, so unless action is taken here,
2718 explicit function specializations will always appear with
2719 global linkage.
2720
2721 The action recommended by the C++ CWG in response to C++
2722 defect report 605 is to make the storage class and linkage
2723 of the explicit specialization match the templated function:
2724
2725 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2726 */
2727 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2728 {
2729 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2730 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2731
2732 /* This specialization has the same linkage and visibility as
2733 the function template it specializes. */
2734 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2735 if (! TREE_PUBLIC (decl))
2736 {
2737 DECL_INTERFACE_KNOWN (decl) = 1;
2738 DECL_NOT_REALLY_EXTERN (decl) = 1;
2739 }
2740 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2741 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2742 {
2743 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2744 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2745 }
2746 }
2747
2748 /* If DECL is a friend declaration, declared using an
2749 unqualified name, the namespace associated with DECL may
2750 have been set incorrectly. For example, in:
2751
2752 template <typename T> void f(T);
2753 namespace N {
2754 struct S { friend void f<int>(int); }
2755 }
2756
2757 we will have set the DECL_CONTEXT for the friend
2758 declaration to N, rather than to the global namespace. */
2759 if (DECL_NAMESPACE_SCOPE_P (decl))
2760 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2761
2762 if (is_friend && !have_def)
2763 /* This is not really a declaration of a specialization.
2764 It's just the name of an instantiation. But, it's not
2765 a request for an instantiation, either. */
2766 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2767 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2768 /* This is indeed a specialization. In case of constructors
2769 and destructors, we need in-charge and not-in-charge
2770 versions in V3 ABI. */
2771 clone_function_decl (decl, /*update_method_vec_p=*/0);
2772
2773 /* Register this specialization so that we can find it
2774 again. */
2775 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2776 }
2777 }
2778
2779 return decl;
2780 }
2781
2782 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2783 parameters. These are represented in the same format used for
2784 DECL_TEMPLATE_PARMS. */
2785
2786 int
2787 comp_template_parms (const_tree parms1, const_tree parms2)
2788 {
2789 const_tree p1;
2790 const_tree p2;
2791
2792 if (parms1 == parms2)
2793 return 1;
2794
2795 for (p1 = parms1, p2 = parms2;
2796 p1 != NULL_TREE && p2 != NULL_TREE;
2797 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2798 {
2799 tree t1 = TREE_VALUE (p1);
2800 tree t2 = TREE_VALUE (p2);
2801 int i;
2802
2803 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2804 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2805
2806 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2807 return 0;
2808
2809 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2810 {
2811 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2812 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2813
2814 /* If either of the template parameters are invalid, assume
2815 they match for the sake of error recovery. */
2816 if (parm1 == error_mark_node || parm2 == error_mark_node)
2817 return 1;
2818
2819 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2820 return 0;
2821
2822 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2823 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2824 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2825 continue;
2826 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2827 return 0;
2828 }
2829 }
2830
2831 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2832 /* One set of parameters has more parameters lists than the
2833 other. */
2834 return 0;
2835
2836 return 1;
2837 }
2838
2839 /* Determine whether PARM is a parameter pack. */
2840
2841 bool
2842 template_parameter_pack_p (const_tree parm)
2843 {
2844 /* Determine if we have a non-type template parameter pack. */
2845 if (TREE_CODE (parm) == PARM_DECL)
2846 return (DECL_TEMPLATE_PARM_P (parm)
2847 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2848 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2849 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2850
2851 /* If this is a list of template parameters, we could get a
2852 TYPE_DECL or a TEMPLATE_DECL. */
2853 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2854 parm = TREE_TYPE (parm);
2855
2856 /* Otherwise it must be a type template parameter. */
2857 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2858 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2859 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2860 }
2861
2862 /* Determine if T is a function parameter pack. */
2863
2864 bool
2865 function_parameter_pack_p (const_tree t)
2866 {
2867 if (t && TREE_CODE (t) == PARM_DECL)
2868 return DECL_PACK_P (t);
2869 return false;
2870 }
2871
2872 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2873 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2874
2875 tree
2876 get_function_template_decl (const_tree primary_func_tmpl_inst)
2877 {
2878 if (! primary_func_tmpl_inst
2879 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2880 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2881 return NULL;
2882
2883 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2884 }
2885
2886 /* Return true iff the function parameter PARAM_DECL was expanded
2887 from the function parameter pack PACK. */
2888
2889 bool
2890 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2891 {
2892 if (DECL_ARTIFICIAL (param_decl)
2893 || !function_parameter_pack_p (pack))
2894 return false;
2895
2896 /* The parameter pack and its pack arguments have the same
2897 DECL_PARM_INDEX. */
2898 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2899 }
2900
2901 /* Determine whether ARGS describes a variadic template args list,
2902 i.e., one that is terminated by a template argument pack. */
2903
2904 static bool
2905 template_args_variadic_p (tree args)
2906 {
2907 int nargs;
2908 tree last_parm;
2909
2910 if (args == NULL_TREE)
2911 return false;
2912
2913 args = INNERMOST_TEMPLATE_ARGS (args);
2914 nargs = TREE_VEC_LENGTH (args);
2915
2916 if (nargs == 0)
2917 return false;
2918
2919 last_parm = TREE_VEC_ELT (args, nargs - 1);
2920
2921 return ARGUMENT_PACK_P (last_parm);
2922 }
2923
2924 /* Generate a new name for the parameter pack name NAME (an
2925 IDENTIFIER_NODE) that incorporates its */
2926
2927 static tree
2928 make_ith_pack_parameter_name (tree name, int i)
2929 {
2930 /* Munge the name to include the parameter index. */
2931 #define NUMBUF_LEN 128
2932 char numbuf[NUMBUF_LEN];
2933 char* newname;
2934 int newname_len;
2935
2936 if (name == NULL_TREE)
2937 return name;
2938 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2939 newname_len = IDENTIFIER_LENGTH (name)
2940 + strlen (numbuf) + 2;
2941 newname = (char*)alloca (newname_len);
2942 snprintf (newname, newname_len,
2943 "%s#%i", IDENTIFIER_POINTER (name), i);
2944 return get_identifier (newname);
2945 }
2946
2947 /* Return true if T is a primary function, class or alias template
2948 instantiation. */
2949
2950 bool
2951 primary_template_instantiation_p (const_tree t)
2952 {
2953 if (!t)
2954 return false;
2955
2956 if (TREE_CODE (t) == FUNCTION_DECL)
2957 return DECL_LANG_SPECIFIC (t)
2958 && DECL_TEMPLATE_INSTANTIATION (t)
2959 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2960 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2961 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2962 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2963 else if (alias_template_specialization_p (t))
2964 return true;
2965 return false;
2966 }
2967
2968 /* Return true if PARM is a template template parameter. */
2969
2970 bool
2971 template_template_parameter_p (const_tree parm)
2972 {
2973 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2974 }
2975
2976 /* Return true iff PARM is a DECL representing a type template
2977 parameter. */
2978
2979 bool
2980 template_type_parameter_p (const_tree parm)
2981 {
2982 return (parm
2983 && (TREE_CODE (parm) == TYPE_DECL
2984 || TREE_CODE (parm) == TEMPLATE_DECL)
2985 && DECL_TEMPLATE_PARM_P (parm));
2986 }
2987
2988 /* Return the template parameters of T if T is a
2989 primary template instantiation, NULL otherwise. */
2990
2991 tree
2992 get_primary_template_innermost_parameters (const_tree t)
2993 {
2994 tree parms = NULL, template_info = NULL;
2995
2996 if ((template_info = get_template_info (t))
2997 && primary_template_instantiation_p (t))
2998 parms = INNERMOST_TEMPLATE_PARMS
2999 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3000
3001 return parms;
3002 }
3003
3004 /* Return the template parameters of the LEVELth level from the full list
3005 of template parameters PARMS. */
3006
3007 tree
3008 get_template_parms_at_level (tree parms, int level)
3009 {
3010 tree p;
3011 if (!parms
3012 || TREE_CODE (parms) != TREE_LIST
3013 || level > TMPL_PARMS_DEPTH (parms))
3014 return NULL_TREE;
3015
3016 for (p = parms; p; p = TREE_CHAIN (p))
3017 if (TMPL_PARMS_DEPTH (p) == level)
3018 return p;
3019
3020 return NULL_TREE;
3021 }
3022
3023 /* Returns the template arguments of T if T is a template instantiation,
3024 NULL otherwise. */
3025
3026 tree
3027 get_template_innermost_arguments (const_tree t)
3028 {
3029 tree args = NULL, template_info = NULL;
3030
3031 if ((template_info = get_template_info (t))
3032 && TI_ARGS (template_info))
3033 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3034
3035 return args;
3036 }
3037
3038 /* Return the argument pack elements of T if T is a template argument pack,
3039 NULL otherwise. */
3040
3041 tree
3042 get_template_argument_pack_elems (const_tree t)
3043 {
3044 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3045 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3046 return NULL;
3047
3048 return ARGUMENT_PACK_ARGS (t);
3049 }
3050
3051 /* Structure used to track the progress of find_parameter_packs_r. */
3052 struct find_parameter_pack_data
3053 {
3054 /* TREE_LIST that will contain all of the parameter packs found by
3055 the traversal. */
3056 tree* parameter_packs;
3057
3058 /* Set of AST nodes that have been visited by the traversal. */
3059 struct pointer_set_t *visited;
3060 };
3061
3062 /* Identifies all of the argument packs that occur in a template
3063 argument and appends them to the TREE_LIST inside DATA, which is a
3064 find_parameter_pack_data structure. This is a subroutine of
3065 make_pack_expansion and uses_parameter_packs. */
3066 static tree
3067 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3068 {
3069 tree t = *tp;
3070 struct find_parameter_pack_data* ppd =
3071 (struct find_parameter_pack_data*)data;
3072 bool parameter_pack_p = false;
3073
3074 /* Handle type aliases/typedefs. */
3075 if (TYPE_ALIAS_P (t))
3076 {
3077 if (TYPE_TEMPLATE_INFO (t))
3078 cp_walk_tree (&TYPE_TI_ARGS (t),
3079 &find_parameter_packs_r,
3080 ppd, ppd->visited);
3081 *walk_subtrees = 0;
3082 return NULL_TREE;
3083 }
3084
3085 /* Identify whether this is a parameter pack or not. */
3086 switch (TREE_CODE (t))
3087 {
3088 case TEMPLATE_PARM_INDEX:
3089 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3090 parameter_pack_p = true;
3091 break;
3092
3093 case TEMPLATE_TYPE_PARM:
3094 t = TYPE_MAIN_VARIANT (t);
3095 case TEMPLATE_TEMPLATE_PARM:
3096 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3097 parameter_pack_p = true;
3098 break;
3099
3100 case FIELD_DECL:
3101 case PARM_DECL:
3102 if (DECL_PACK_P (t))
3103 {
3104 /* We don't want to walk into the type of a PARM_DECL,
3105 because we don't want to see the type parameter pack. */
3106 *walk_subtrees = 0;
3107 parameter_pack_p = true;
3108 }
3109 break;
3110
3111 /* Look through a lambda capture proxy to the field pack. */
3112 case VAR_DECL:
3113 if (DECL_HAS_VALUE_EXPR_P (t))
3114 {
3115 tree v = DECL_VALUE_EXPR (t);
3116 cp_walk_tree (&v,
3117 &find_parameter_packs_r,
3118 ppd, ppd->visited);
3119 *walk_subtrees = 0;
3120 }
3121 break;
3122
3123 case BASES:
3124 parameter_pack_p = true;
3125 break;
3126 default:
3127 /* Not a parameter pack. */
3128 break;
3129 }
3130
3131 if (parameter_pack_p)
3132 {
3133 /* Add this parameter pack to the list. */
3134 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3135 }
3136
3137 if (TYPE_P (t))
3138 cp_walk_tree (&TYPE_CONTEXT (t),
3139 &find_parameter_packs_r, ppd, ppd->visited);
3140
3141 /* This switch statement will return immediately if we don't find a
3142 parameter pack. */
3143 switch (TREE_CODE (t))
3144 {
3145 case TEMPLATE_PARM_INDEX:
3146 return NULL_TREE;
3147
3148 case BOUND_TEMPLATE_TEMPLATE_PARM:
3149 /* Check the template itself. */
3150 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3151 &find_parameter_packs_r, ppd, ppd->visited);
3152 /* Check the template arguments. */
3153 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3154 ppd->visited);
3155 *walk_subtrees = 0;
3156 return NULL_TREE;
3157
3158 case TEMPLATE_TYPE_PARM:
3159 case TEMPLATE_TEMPLATE_PARM:
3160 return NULL_TREE;
3161
3162 case PARM_DECL:
3163 return NULL_TREE;
3164
3165 case RECORD_TYPE:
3166 if (TYPE_PTRMEMFUNC_P (t))
3167 return NULL_TREE;
3168 /* Fall through. */
3169
3170 case UNION_TYPE:
3171 case ENUMERAL_TYPE:
3172 if (TYPE_TEMPLATE_INFO (t))
3173 cp_walk_tree (&TYPE_TI_ARGS (t),
3174 &find_parameter_packs_r, ppd, ppd->visited);
3175
3176 *walk_subtrees = 0;
3177 return NULL_TREE;
3178
3179 case CONSTRUCTOR:
3180 case TEMPLATE_DECL:
3181 cp_walk_tree (&TREE_TYPE (t),
3182 &find_parameter_packs_r, ppd, ppd->visited);
3183 return NULL_TREE;
3184
3185 case TYPENAME_TYPE:
3186 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3187 ppd, ppd->visited);
3188 *walk_subtrees = 0;
3189 return NULL_TREE;
3190
3191 case TYPE_PACK_EXPANSION:
3192 case EXPR_PACK_EXPANSION:
3193 *walk_subtrees = 0;
3194 return NULL_TREE;
3195
3196 case INTEGER_TYPE:
3197 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3198 ppd, ppd->visited);
3199 *walk_subtrees = 0;
3200 return NULL_TREE;
3201
3202 case IDENTIFIER_NODE:
3203 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3204 ppd->visited);
3205 *walk_subtrees = 0;
3206 return NULL_TREE;
3207
3208 default:
3209 return NULL_TREE;
3210 }
3211
3212 return NULL_TREE;
3213 }
3214
3215 /* Determines if the expression or type T uses any parameter packs. */
3216 bool
3217 uses_parameter_packs (tree t)
3218 {
3219 tree parameter_packs = NULL_TREE;
3220 struct find_parameter_pack_data ppd;
3221 ppd.parameter_packs = &parameter_packs;
3222 ppd.visited = pointer_set_create ();
3223 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3224 pointer_set_destroy (ppd.visited);
3225 return parameter_packs != NULL_TREE;
3226 }
3227
3228 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3229 representation a base-class initializer into a parameter pack
3230 expansion. If all goes well, the resulting node will be an
3231 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3232 respectively. */
3233 tree
3234 make_pack_expansion (tree arg)
3235 {
3236 tree result;
3237 tree parameter_packs = NULL_TREE;
3238 bool for_types = false;
3239 struct find_parameter_pack_data ppd;
3240
3241 if (!arg || arg == error_mark_node)
3242 return arg;
3243
3244 if (TREE_CODE (arg) == TREE_LIST)
3245 {
3246 /* The only time we will see a TREE_LIST here is for a base
3247 class initializer. In this case, the TREE_PURPOSE will be a
3248 _TYPE node (representing the base class expansion we're
3249 initializing) and the TREE_VALUE will be a TREE_LIST
3250 containing the initialization arguments.
3251
3252 The resulting expansion looks somewhat different from most
3253 expansions. Rather than returning just one _EXPANSION, we
3254 return a TREE_LIST whose TREE_PURPOSE is a
3255 TYPE_PACK_EXPANSION containing the bases that will be
3256 initialized. The TREE_VALUE will be identical to the
3257 original TREE_VALUE, which is a list of arguments that will
3258 be passed to each base. We do not introduce any new pack
3259 expansion nodes into the TREE_VALUE (although it is possible
3260 that some already exist), because the TREE_PURPOSE and
3261 TREE_VALUE all need to be expanded together with the same
3262 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3263 resulting TREE_PURPOSE will mention the parameter packs in
3264 both the bases and the arguments to the bases. */
3265 tree purpose;
3266 tree value;
3267 tree parameter_packs = NULL_TREE;
3268
3269 /* Determine which parameter packs will be used by the base
3270 class expansion. */
3271 ppd.visited = pointer_set_create ();
3272 ppd.parameter_packs = &parameter_packs;
3273 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3274 &ppd, ppd.visited);
3275
3276 if (parameter_packs == NULL_TREE)
3277 {
3278 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3279 pointer_set_destroy (ppd.visited);
3280 return error_mark_node;
3281 }
3282
3283 if (TREE_VALUE (arg) != void_type_node)
3284 {
3285 /* Collect the sets of parameter packs used in each of the
3286 initialization arguments. */
3287 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3288 {
3289 /* Determine which parameter packs will be expanded in this
3290 argument. */
3291 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3292 &ppd, ppd.visited);
3293 }
3294 }
3295
3296 pointer_set_destroy (ppd.visited);
3297
3298 /* Create the pack expansion type for the base type. */
3299 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3300 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3301 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3302
3303 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3304 they will rarely be compared to anything. */
3305 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3306
3307 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3308 }
3309
3310 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3311 for_types = true;
3312
3313 /* Build the PACK_EXPANSION_* node. */
3314 result = for_types
3315 ? cxx_make_type (TYPE_PACK_EXPANSION)
3316 : make_node (EXPR_PACK_EXPANSION);
3317 SET_PACK_EXPANSION_PATTERN (result, arg);
3318 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3319 {
3320 /* Propagate type and const-expression information. */
3321 TREE_TYPE (result) = TREE_TYPE (arg);
3322 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3323 }
3324 else
3325 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3326 they will rarely be compared to anything. */
3327 SET_TYPE_STRUCTURAL_EQUALITY (result);
3328
3329 /* Determine which parameter packs will be expanded. */
3330 ppd.parameter_packs = &parameter_packs;
3331 ppd.visited = pointer_set_create ();
3332 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3333 pointer_set_destroy (ppd.visited);
3334
3335 /* Make sure we found some parameter packs. */
3336 if (parameter_packs == NULL_TREE)
3337 {
3338 if (TYPE_P (arg))
3339 error ("expansion pattern %<%T%> contains no argument packs", arg);
3340 else
3341 error ("expansion pattern %<%E%> contains no argument packs", arg);
3342 return error_mark_node;
3343 }
3344 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3345
3346 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3347
3348 return result;
3349 }
3350
3351 /* Checks T for any "bare" parameter packs, which have not yet been
3352 expanded, and issues an error if any are found. This operation can
3353 only be done on full expressions or types (e.g., an expression
3354 statement, "if" condition, etc.), because we could have expressions like:
3355
3356 foo(f(g(h(args)))...)
3357
3358 where "args" is a parameter pack. check_for_bare_parameter_packs
3359 should not be called for the subexpressions args, h(args),
3360 g(h(args)), or f(g(h(args))), because we would produce erroneous
3361 error messages.
3362
3363 Returns TRUE and emits an error if there were bare parameter packs,
3364 returns FALSE otherwise. */
3365 bool
3366 check_for_bare_parameter_packs (tree t)
3367 {
3368 tree parameter_packs = NULL_TREE;
3369 struct find_parameter_pack_data ppd;
3370
3371 if (!processing_template_decl || !t || t == error_mark_node)
3372 return false;
3373
3374 if (TREE_CODE (t) == TYPE_DECL)
3375 t = TREE_TYPE (t);
3376
3377 ppd.parameter_packs = &parameter_packs;
3378 ppd.visited = pointer_set_create ();
3379 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3380 pointer_set_destroy (ppd.visited);
3381
3382 if (parameter_packs)
3383 {
3384 error ("parameter packs not expanded with %<...%>:");
3385 while (parameter_packs)
3386 {
3387 tree pack = TREE_VALUE (parameter_packs);
3388 tree name = NULL_TREE;
3389
3390 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3391 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3392 name = TYPE_NAME (pack);
3393 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3394 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3395 else
3396 name = DECL_NAME (pack);
3397
3398 if (name)
3399 inform (input_location, " %qD", name);
3400 else
3401 inform (input_location, " <anonymous>");
3402
3403 parameter_packs = TREE_CHAIN (parameter_packs);
3404 }
3405
3406 return true;
3407 }
3408
3409 return false;
3410 }
3411
3412 /* Expand any parameter packs that occur in the template arguments in
3413 ARGS. */
3414 tree
3415 expand_template_argument_pack (tree args)
3416 {
3417 tree result_args = NULL_TREE;
3418 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3419 int num_result_args = -1;
3420 int non_default_args_count = -1;
3421
3422 /* First, determine if we need to expand anything, and the number of
3423 slots we'll need. */
3424 for (in_arg = 0; in_arg < nargs; ++in_arg)
3425 {
3426 tree arg = TREE_VEC_ELT (args, in_arg);
3427 if (arg == NULL_TREE)
3428 return args;
3429 if (ARGUMENT_PACK_P (arg))
3430 {
3431 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3432 if (num_result_args < 0)
3433 num_result_args = in_arg + num_packed;
3434 else
3435 num_result_args += num_packed;
3436 }
3437 else
3438 {
3439 if (num_result_args >= 0)
3440 num_result_args++;
3441 }
3442 }
3443
3444 /* If no expansion is necessary, we're done. */
3445 if (num_result_args < 0)
3446 return args;
3447
3448 /* Expand arguments. */
3449 result_args = make_tree_vec (num_result_args);
3450 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3451 non_default_args_count =
3452 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3453 for (in_arg = 0; in_arg < nargs; ++in_arg)
3454 {
3455 tree arg = TREE_VEC_ELT (args, in_arg);
3456 if (ARGUMENT_PACK_P (arg))
3457 {
3458 tree packed = ARGUMENT_PACK_ARGS (arg);
3459 int i, num_packed = TREE_VEC_LENGTH (packed);
3460 for (i = 0; i < num_packed; ++i, ++out_arg)
3461 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3462 if (non_default_args_count > 0)
3463 non_default_args_count += num_packed;
3464 }
3465 else
3466 {
3467 TREE_VEC_ELT (result_args, out_arg) = arg;
3468 ++out_arg;
3469 }
3470 }
3471 if (non_default_args_count >= 0)
3472 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3473 return result_args;
3474 }
3475
3476 /* Checks if DECL shadows a template parameter.
3477
3478 [temp.local]: A template-parameter shall not be redeclared within its
3479 scope (including nested scopes).
3480
3481 Emits an error and returns TRUE if the DECL shadows a parameter,
3482 returns FALSE otherwise. */
3483
3484 bool
3485 check_template_shadow (tree decl)
3486 {
3487 tree olddecl;
3488
3489 /* If we're not in a template, we can't possibly shadow a template
3490 parameter. */
3491 if (!current_template_parms)
3492 return true;
3493
3494 /* Figure out what we're shadowing. */
3495 if (TREE_CODE (decl) == OVERLOAD)
3496 decl = OVL_CURRENT (decl);
3497 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3498
3499 /* If there's no previous binding for this name, we're not shadowing
3500 anything, let alone a template parameter. */
3501 if (!olddecl)
3502 return true;
3503
3504 /* If we're not shadowing a template parameter, we're done. Note
3505 that OLDDECL might be an OVERLOAD (or perhaps even an
3506 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3507 node. */
3508 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3509 return true;
3510
3511 /* We check for decl != olddecl to avoid bogus errors for using a
3512 name inside a class. We check TPFI to avoid duplicate errors for
3513 inline member templates. */
3514 if (decl == olddecl
3515 || (DECL_TEMPLATE_PARM_P (decl)
3516 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3517 return true;
3518
3519 error ("declaration of %q+#D", decl);
3520 error (" shadows template parm %q+#D", olddecl);
3521 return false;
3522 }
3523
3524 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3525 ORIG_LEVEL, DECL, and TYPE. */
3526
3527 static tree
3528 build_template_parm_index (int index,
3529 int level,
3530 int orig_level,
3531 tree decl,
3532 tree type)
3533 {
3534 tree t = make_node (TEMPLATE_PARM_INDEX);
3535 TEMPLATE_PARM_IDX (t) = index;
3536 TEMPLATE_PARM_LEVEL (t) = level;
3537 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3538 TEMPLATE_PARM_DECL (t) = decl;
3539 TREE_TYPE (t) = type;
3540 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3541 TREE_READONLY (t) = TREE_READONLY (decl);
3542
3543 return t;
3544 }
3545
3546 /* Find the canonical type parameter for the given template type
3547 parameter. Returns the canonical type parameter, which may be TYPE
3548 if no such parameter existed. */
3549
3550 static tree
3551 canonical_type_parameter (tree type)
3552 {
3553 tree list;
3554 int idx = TEMPLATE_TYPE_IDX (type);
3555 if (!canonical_template_parms)
3556 vec_alloc (canonical_template_parms, idx+1);
3557
3558 while (canonical_template_parms->length () <= (unsigned)idx)
3559 vec_safe_push (canonical_template_parms, NULL_TREE);
3560
3561 list = (*canonical_template_parms)[idx];
3562 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3563 list = TREE_CHAIN (list);
3564
3565 if (list)
3566 return TREE_VALUE (list);
3567 else
3568 {
3569 (*canonical_template_parms)[idx]
3570 = tree_cons (NULL_TREE, type,
3571 (*canonical_template_parms)[idx]);
3572 return type;
3573 }
3574 }
3575
3576 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3577 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3578 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3579 new one is created. */
3580
3581 static tree
3582 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3583 tsubst_flags_t complain)
3584 {
3585 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3586 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3587 != TEMPLATE_PARM_LEVEL (index) - levels)
3588 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3589 {
3590 tree orig_decl = TEMPLATE_PARM_DECL (index);
3591 tree decl, t;
3592
3593 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3594 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3595 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3596 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3597 DECL_ARTIFICIAL (decl) = 1;
3598 SET_DECL_TEMPLATE_PARM_P (decl);
3599
3600 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3601 TEMPLATE_PARM_LEVEL (index) - levels,
3602 TEMPLATE_PARM_ORIG_LEVEL (index),
3603 decl, type);
3604 TEMPLATE_PARM_DESCENDANTS (index) = t;
3605 TEMPLATE_PARM_PARAMETER_PACK (t)
3606 = TEMPLATE_PARM_PARAMETER_PACK (index);
3607
3608 /* Template template parameters need this. */
3609 if (TREE_CODE (decl) == TEMPLATE_DECL)
3610 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3611 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3612 args, complain);
3613 }
3614
3615 return TEMPLATE_PARM_DESCENDANTS (index);
3616 }
3617
3618 /* Process information from new template parameter PARM and append it
3619 to the LIST being built. This new parameter is a non-type
3620 parameter iff IS_NON_TYPE is true. This new parameter is a
3621 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3622 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3623 parameter list PARM belongs to. This is used used to create a
3624 proper canonical type for the type of PARM that is to be created,
3625 iff PARM is a type. If the size is not known, this parameter shall
3626 be set to 0. */
3627
3628 tree
3629 process_template_parm (tree list, location_t parm_loc, tree parm,
3630 bool is_non_type, bool is_parameter_pack)
3631 {
3632 tree decl = 0;
3633 tree defval;
3634 tree err_parm_list;
3635 int idx = 0;
3636
3637 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3638 defval = TREE_PURPOSE (parm);
3639
3640 if (list)
3641 {
3642 tree p = tree_last (list);
3643
3644 if (p && TREE_VALUE (p) != error_mark_node)
3645 {
3646 p = TREE_VALUE (p);
3647 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3648 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3649 else
3650 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3651 }
3652
3653 ++idx;
3654 }
3655 else
3656 idx = 0;
3657
3658 if (is_non_type)
3659 {
3660 parm = TREE_VALUE (parm);
3661
3662 SET_DECL_TEMPLATE_PARM_P (parm);
3663
3664 if (TREE_TYPE (parm) == error_mark_node)
3665 {
3666 err_parm_list = build_tree_list (defval, parm);
3667 TREE_VALUE (err_parm_list) = error_mark_node;
3668 return chainon (list, err_parm_list);
3669 }
3670 else
3671 {
3672 /* [temp.param]
3673
3674 The top-level cv-qualifiers on the template-parameter are
3675 ignored when determining its type. */
3676 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3677 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3678 {
3679 err_parm_list = build_tree_list (defval, parm);
3680 TREE_VALUE (err_parm_list) = error_mark_node;
3681 return chainon (list, err_parm_list);
3682 }
3683
3684 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3685 {
3686 /* This template parameter is not a parameter pack, but it
3687 should be. Complain about "bare" parameter packs. */
3688 check_for_bare_parameter_packs (TREE_TYPE (parm));
3689
3690 /* Recover by calling this a parameter pack. */
3691 is_parameter_pack = true;
3692 }
3693 }
3694
3695 /* A template parameter is not modifiable. */
3696 TREE_CONSTANT (parm) = 1;
3697 TREE_READONLY (parm) = 1;
3698 decl = build_decl (parm_loc,
3699 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3700 TREE_CONSTANT (decl) = 1;
3701 TREE_READONLY (decl) = 1;
3702 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3703 = build_template_parm_index (idx, processing_template_decl,
3704 processing_template_decl,
3705 decl, TREE_TYPE (parm));
3706
3707 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3708 = is_parameter_pack;
3709 }
3710 else
3711 {
3712 tree t;
3713 parm = TREE_VALUE (TREE_VALUE (parm));
3714
3715 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3716 {
3717 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3718 /* This is for distinguishing between real templates and template
3719 template parameters */
3720 TREE_TYPE (parm) = t;
3721 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3722 decl = parm;
3723 }
3724 else
3725 {
3726 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3727 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3728 decl = build_decl (parm_loc,
3729 TYPE_DECL, parm, t);
3730 }
3731
3732 TYPE_NAME (t) = decl;
3733 TYPE_STUB_DECL (t) = decl;
3734 parm = decl;
3735 TEMPLATE_TYPE_PARM_INDEX (t)
3736 = build_template_parm_index (idx, processing_template_decl,
3737 processing_template_decl,
3738 decl, TREE_TYPE (parm));
3739 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3740 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3741 }
3742 DECL_ARTIFICIAL (decl) = 1;
3743 SET_DECL_TEMPLATE_PARM_P (decl);
3744 pushdecl (decl);
3745 parm = build_tree_list (defval, parm);
3746 return chainon (list, parm);
3747 }
3748
3749 /* The end of a template parameter list has been reached. Process the
3750 tree list into a parameter vector, converting each parameter into a more
3751 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3752 as PARM_DECLs. */
3753
3754 tree
3755 end_template_parm_list (tree parms)
3756 {
3757 int nparms;
3758 tree parm, next;
3759 tree saved_parmlist = make_tree_vec (list_length (parms));
3760
3761 current_template_parms
3762 = tree_cons (size_int (processing_template_decl),
3763 saved_parmlist, current_template_parms);
3764
3765 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3766 {
3767 next = TREE_CHAIN (parm);
3768 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3769 TREE_CHAIN (parm) = NULL_TREE;
3770 }
3771
3772 --processing_template_parmlist;
3773
3774 return saved_parmlist;
3775 }
3776
3777 /* end_template_decl is called after a template declaration is seen. */
3778
3779 void
3780 end_template_decl (void)
3781 {
3782 reset_specialization ();
3783
3784 if (! processing_template_decl)
3785 return;
3786
3787 /* This matches the pushlevel in begin_template_parm_list. */
3788 finish_scope ();
3789
3790 --processing_template_decl;
3791 current_template_parms = TREE_CHAIN (current_template_parms);
3792 }
3793
3794 /* Takes a TREE_LIST representing a template parameter and convert it
3795 into an argument suitable to be passed to the type substitution
3796 functions. Note that If the TREE_LIST contains an error_mark
3797 node, the returned argument is error_mark_node. */
3798
3799 static tree
3800 template_parm_to_arg (tree t)
3801 {
3802
3803 if (t == NULL_TREE
3804 || TREE_CODE (t) != TREE_LIST)
3805 return t;
3806
3807 if (error_operand_p (TREE_VALUE (t)))
3808 return error_mark_node;
3809
3810 t = TREE_VALUE (t);
3811
3812 if (TREE_CODE (t) == TYPE_DECL
3813 || TREE_CODE (t) == TEMPLATE_DECL)
3814 {
3815 t = TREE_TYPE (t);
3816
3817 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3818 {
3819 /* Turn this argument into a TYPE_ARGUMENT_PACK
3820 with a single element, which expands T. */
3821 tree vec = make_tree_vec (1);
3822 #ifdef ENABLE_CHECKING
3823 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3824 (vec, TREE_VEC_LENGTH (vec));
3825 #endif
3826 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3827
3828 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3829 SET_ARGUMENT_PACK_ARGS (t, vec);
3830 }
3831 }
3832 else
3833 {
3834 t = DECL_INITIAL (t);
3835
3836 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3837 {
3838 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3839 with a single element, which expands T. */
3840 tree vec = make_tree_vec (1);
3841 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3842 #ifdef ENABLE_CHECKING
3843 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3844 (vec, TREE_VEC_LENGTH (vec));
3845 #endif
3846 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3847
3848 t = make_node (NONTYPE_ARGUMENT_PACK);
3849 SET_ARGUMENT_PACK_ARGS (t, vec);
3850 TREE_TYPE (t) = type;
3851 }
3852 }
3853 return t;
3854 }
3855
3856 /* Given a set of template parameters, return them as a set of template
3857 arguments. The template parameters are represented as a TREE_VEC, in
3858 the form documented in cp-tree.h for template arguments. */
3859
3860 static tree
3861 template_parms_to_args (tree parms)
3862 {
3863 tree header;
3864 tree args = NULL_TREE;
3865 int length = TMPL_PARMS_DEPTH (parms);
3866 int l = length;
3867
3868 /* If there is only one level of template parameters, we do not
3869 create a TREE_VEC of TREE_VECs. Instead, we return a single
3870 TREE_VEC containing the arguments. */
3871 if (length > 1)
3872 args = make_tree_vec (length);
3873
3874 for (header = parms; header; header = TREE_CHAIN (header))
3875 {
3876 tree a = copy_node (TREE_VALUE (header));
3877 int i;
3878
3879 TREE_TYPE (a) = NULL_TREE;
3880 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3881 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3882
3883 #ifdef ENABLE_CHECKING
3884 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3885 #endif
3886
3887 if (length > 1)
3888 TREE_VEC_ELT (args, --l) = a;
3889 else
3890 args = a;
3891 }
3892
3893 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3894 /* This can happen for template parms of a template template
3895 parameter, e.g:
3896
3897 template<template<class T, class U> class TT> struct S;
3898
3899 Consider the level of the parms of TT; T and U both have
3900 level 2; TT has no template parm of level 1. So in this case
3901 the first element of full_template_args is NULL_TREE. If we
3902 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3903 of 2. This will make tsubst wrongly consider that T and U
3904 have level 1. Instead, let's create a dummy vector as the
3905 first element of full_template_args so that TMPL_ARGS_DEPTH
3906 returns the correct depth for args. */
3907 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3908 return args;
3909 }
3910
3911 /* Within the declaration of a template, return the currently active
3912 template parameters as an argument TREE_VEC. */
3913
3914 static tree
3915 current_template_args (void)
3916 {
3917 return template_parms_to_args (current_template_parms);
3918 }
3919
3920 /* Update the declared TYPE by doing any lookups which were thought to be
3921 dependent, but are not now that we know the SCOPE of the declarator. */
3922
3923 tree
3924 maybe_update_decl_type (tree orig_type, tree scope)
3925 {
3926 tree type = orig_type;
3927
3928 if (type == NULL_TREE)
3929 return type;
3930
3931 if (TREE_CODE (orig_type) == TYPE_DECL)
3932 type = TREE_TYPE (type);
3933
3934 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3935 && dependent_type_p (type)
3936 /* Don't bother building up the args in this case. */
3937 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3938 {
3939 /* tsubst in the args corresponding to the template parameters,
3940 including auto if present. Most things will be unchanged, but
3941 make_typename_type and tsubst_qualified_id will resolve
3942 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3943 tree args = current_template_args ();
3944 tree auto_node = type_uses_auto (type);
3945 tree pushed;
3946 if (auto_node)
3947 {
3948 tree auto_vec = make_tree_vec (1);
3949 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3950 args = add_to_template_args (args, auto_vec);
3951 }
3952 pushed = push_scope (scope);
3953 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3954 if (pushed)
3955 pop_scope (scope);
3956 }
3957
3958 if (type == error_mark_node)
3959 return orig_type;
3960
3961 if (TREE_CODE (orig_type) == TYPE_DECL)
3962 {
3963 if (same_type_p (type, TREE_TYPE (orig_type)))
3964 type = orig_type;
3965 else
3966 type = TYPE_NAME (type);
3967 }
3968 return type;
3969 }
3970
3971 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3972 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3973 a member template. Used by push_template_decl below. */
3974
3975 static tree
3976 build_template_decl (tree decl, tree parms, bool member_template_p)
3977 {
3978 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3979 DECL_TEMPLATE_PARMS (tmpl) = parms;
3980 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3981 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3982 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3983
3984 return tmpl;
3985 }
3986
3987 struct template_parm_data
3988 {
3989 /* The level of the template parameters we are currently
3990 processing. */
3991 int level;
3992
3993 /* The index of the specialization argument we are currently
3994 processing. */
3995 int current_arg;
3996
3997 /* An array whose size is the number of template parameters. The
3998 elements are nonzero if the parameter has been used in any one
3999 of the arguments processed so far. */
4000 int* parms;
4001
4002 /* An array whose size is the number of template arguments. The
4003 elements are nonzero if the argument makes use of template
4004 parameters of this level. */
4005 int* arg_uses_template_parms;
4006 };
4007
4008 /* Subroutine of push_template_decl used to see if each template
4009 parameter in a partial specialization is used in the explicit
4010 argument list. If T is of the LEVEL given in DATA (which is
4011 treated as a template_parm_data*), then DATA->PARMS is marked
4012 appropriately. */
4013
4014 static int
4015 mark_template_parm (tree t, void* data)
4016 {
4017 int level;
4018 int idx;
4019 struct template_parm_data* tpd = (struct template_parm_data*) data;
4020
4021 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4022 {
4023 level = TEMPLATE_PARM_LEVEL (t);
4024 idx = TEMPLATE_PARM_IDX (t);
4025 }
4026 else
4027 {
4028 level = TEMPLATE_TYPE_LEVEL (t);
4029 idx = TEMPLATE_TYPE_IDX (t);
4030 }
4031
4032 if (level == tpd->level)
4033 {
4034 tpd->parms[idx] = 1;
4035 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4036 }
4037
4038 /* Return zero so that for_each_template_parm will continue the
4039 traversal of the tree; we want to mark *every* template parm. */
4040 return 0;
4041 }
4042
4043 /* Process the partial specialization DECL. */
4044
4045 static tree
4046 process_partial_specialization (tree decl)
4047 {
4048 tree type = TREE_TYPE (decl);
4049 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4050 tree specargs = CLASSTYPE_TI_ARGS (type);
4051 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4052 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4053 tree inner_parms;
4054 tree inst;
4055 int nargs = TREE_VEC_LENGTH (inner_args);
4056 int ntparms;
4057 int i;
4058 bool did_error_intro = false;
4059 struct template_parm_data tpd;
4060 struct template_parm_data tpd2;
4061
4062 gcc_assert (current_template_parms);
4063
4064 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4065 ntparms = TREE_VEC_LENGTH (inner_parms);
4066
4067 /* We check that each of the template parameters given in the
4068 partial specialization is used in the argument list to the
4069 specialization. For example:
4070
4071 template <class T> struct S;
4072 template <class T> struct S<T*>;
4073
4074 The second declaration is OK because `T*' uses the template
4075 parameter T, whereas
4076
4077 template <class T> struct S<int>;
4078
4079 is no good. Even trickier is:
4080
4081 template <class T>
4082 struct S1
4083 {
4084 template <class U>
4085 struct S2;
4086 template <class U>
4087 struct S2<T>;
4088 };
4089
4090 The S2<T> declaration is actually invalid; it is a
4091 full-specialization. Of course,
4092
4093 template <class U>
4094 struct S2<T (*)(U)>;
4095
4096 or some such would have been OK. */
4097 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4098 tpd.parms = XALLOCAVEC (int, ntparms);
4099 memset (tpd.parms, 0, sizeof (int) * ntparms);
4100
4101 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4102 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4103 for (i = 0; i < nargs; ++i)
4104 {
4105 tpd.current_arg = i;
4106 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4107 &mark_template_parm,
4108 &tpd,
4109 NULL,
4110 /*include_nondeduced_p=*/false);
4111 }
4112 for (i = 0; i < ntparms; ++i)
4113 if (tpd.parms[i] == 0)
4114 {
4115 /* One of the template parms was not used in the
4116 specialization. */
4117 if (!did_error_intro)
4118 {
4119 error ("template parameters not used in partial specialization:");
4120 did_error_intro = true;
4121 }
4122
4123 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4124 }
4125
4126 if (did_error_intro)
4127 return error_mark_node;
4128
4129 /* [temp.class.spec]
4130
4131 The argument list of the specialization shall not be identical to
4132 the implicit argument list of the primary template. */
4133 if (comp_template_args
4134 (inner_args,
4135 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4136 (maintmpl)))))
4137 error ("partial specialization %qT does not specialize any template arguments", type);
4138
4139 /* A partial specialization that replaces multiple parameters of the
4140 primary template with a pack expansion is less specialized for those
4141 parameters. */
4142 if (nargs < DECL_NTPARMS (maintmpl))
4143 {
4144 error ("partial specialization is not more specialized than the "
4145 "primary template because it replaces multiple parameters "
4146 "with a pack expansion");
4147 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4148 return decl;
4149 }
4150
4151 /* [temp.class.spec]
4152
4153 A partially specialized non-type argument expression shall not
4154 involve template parameters of the partial specialization except
4155 when the argument expression is a simple identifier.
4156
4157 The type of a template parameter corresponding to a specialized
4158 non-type argument shall not be dependent on a parameter of the
4159 specialization.
4160
4161 Also, we verify that pack expansions only occur at the
4162 end of the argument list. */
4163 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4164 tpd2.parms = 0;
4165 for (i = 0; i < nargs; ++i)
4166 {
4167 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4168 tree arg = TREE_VEC_ELT (inner_args, i);
4169 tree packed_args = NULL_TREE;
4170 int j, len = 1;
4171
4172 if (ARGUMENT_PACK_P (arg))
4173 {
4174 /* Extract the arguments from the argument pack. We'll be
4175 iterating over these in the following loop. */
4176 packed_args = ARGUMENT_PACK_ARGS (arg);
4177 len = TREE_VEC_LENGTH (packed_args);
4178 }
4179
4180 for (j = 0; j < len; j++)
4181 {
4182 if (packed_args)
4183 /* Get the Jth argument in the parameter pack. */
4184 arg = TREE_VEC_ELT (packed_args, j);
4185
4186 if (PACK_EXPANSION_P (arg))
4187 {
4188 /* Pack expansions must come at the end of the
4189 argument list. */
4190 if ((packed_args && j < len - 1)
4191 || (!packed_args && i < nargs - 1))
4192 {
4193 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4194 error ("parameter pack argument %qE must be at the "
4195 "end of the template argument list", arg);
4196 else
4197 error ("parameter pack argument %qT must be at the "
4198 "end of the template argument list", arg);
4199 }
4200 }
4201
4202 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4203 /* We only care about the pattern. */
4204 arg = PACK_EXPANSION_PATTERN (arg);
4205
4206 if (/* These first two lines are the `non-type' bit. */
4207 !TYPE_P (arg)
4208 && TREE_CODE (arg) != TEMPLATE_DECL
4209 /* This next line is the `argument expression is not just a
4210 simple identifier' condition and also the `specialized
4211 non-type argument' bit. */
4212 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4213 {
4214 if ((!packed_args && tpd.arg_uses_template_parms[i])
4215 || (packed_args && uses_template_parms (arg)))
4216 error ("template argument %qE involves template parameter(s)",
4217 arg);
4218 else
4219 {
4220 /* Look at the corresponding template parameter,
4221 marking which template parameters its type depends
4222 upon. */
4223 tree type = TREE_TYPE (parm);
4224
4225 if (!tpd2.parms)
4226 {
4227 /* We haven't yet initialized TPD2. Do so now. */
4228 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4229 /* The number of parameters here is the number in the
4230 main template, which, as checked in the assertion
4231 above, is NARGS. */
4232 tpd2.parms = XALLOCAVEC (int, nargs);
4233 tpd2.level =
4234 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4235 }
4236
4237 /* Mark the template parameters. But this time, we're
4238 looking for the template parameters of the main
4239 template, not in the specialization. */
4240 tpd2.current_arg = i;
4241 tpd2.arg_uses_template_parms[i] = 0;
4242 memset (tpd2.parms, 0, sizeof (int) * nargs);
4243 for_each_template_parm (type,
4244 &mark_template_parm,
4245 &tpd2,
4246 NULL,
4247 /*include_nondeduced_p=*/false);
4248
4249 if (tpd2.arg_uses_template_parms [i])
4250 {
4251 /* The type depended on some template parameters.
4252 If they are fully specialized in the
4253 specialization, that's OK. */
4254 int j;
4255 int count = 0;
4256 for (j = 0; j < nargs; ++j)
4257 if (tpd2.parms[j] != 0
4258 && tpd.arg_uses_template_parms [j])
4259 ++count;
4260 if (count != 0)
4261 error_n (input_location, count,
4262 "type %qT of template argument %qE depends "
4263 "on a template parameter",
4264 "type %qT of template argument %qE depends "
4265 "on template parameters",
4266 type,
4267 arg);
4268 }
4269 }
4270 }
4271 }
4272 }
4273
4274 /* We should only get here once. */
4275 gcc_assert (!COMPLETE_TYPE_P (type));
4276
4277 tree tmpl = build_template_decl (decl, current_template_parms,
4278 DECL_MEMBER_TEMPLATE_P (maintmpl));
4279 TREE_TYPE (tmpl) = type;
4280 DECL_TEMPLATE_RESULT (tmpl) = decl;
4281 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4282 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4283 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4284
4285 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4286 = tree_cons (specargs, tmpl,
4287 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4288 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4289
4290 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4291 inst = TREE_CHAIN (inst))
4292 {
4293 tree inst_type = TREE_VALUE (inst);
4294 if (COMPLETE_TYPE_P (inst_type)
4295 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4296 {
4297 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4298 if (spec && TREE_TYPE (spec) == type)
4299 permerror (input_location,
4300 "partial specialization of %qT after instantiation "
4301 "of %qT", type, inst_type);
4302 }
4303 }
4304
4305 return decl;
4306 }
4307
4308 /* Check that a template declaration's use of default arguments and
4309 parameter packs is not invalid. Here, PARMS are the template
4310 parameters. IS_PRIMARY is true if DECL is the thing declared by
4311 a primary template. IS_PARTIAL is true if DECL is a partial
4312 specialization.
4313
4314 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4315 declaration (but not a definition); 1 indicates a declaration, 2
4316 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4317 emitted for extraneous default arguments.
4318
4319 Returns TRUE if there were no errors found, FALSE otherwise. */
4320
4321 bool
4322 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4323 bool is_partial, int is_friend_decl)
4324 {
4325 const char *msg;
4326 int last_level_to_check;
4327 tree parm_level;
4328 bool no_errors = true;
4329
4330 /* [temp.param]
4331
4332 A default template-argument shall not be specified in a
4333 function template declaration or a function template definition, nor
4334 in the template-parameter-list of the definition of a member of a
4335 class template. */
4336
4337 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4338 /* You can't have a function template declaration in a local
4339 scope, nor you can you define a member of a class template in a
4340 local scope. */
4341 return true;
4342
4343 if (TREE_CODE (decl) == TYPE_DECL
4344 && TREE_TYPE (decl)
4345 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4346 /* A lambda doesn't have an explicit declaration; don't complain
4347 about the parms of the enclosing class. */
4348 return true;
4349
4350 if (current_class_type
4351 && !TYPE_BEING_DEFINED (current_class_type)
4352 && DECL_LANG_SPECIFIC (decl)
4353 && DECL_DECLARES_FUNCTION_P (decl)
4354 /* If this is either a friend defined in the scope of the class
4355 or a member function. */
4356 && (DECL_FUNCTION_MEMBER_P (decl)
4357 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4358 : DECL_FRIEND_CONTEXT (decl)
4359 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4360 : false)
4361 /* And, if it was a member function, it really was defined in
4362 the scope of the class. */
4363 && (!DECL_FUNCTION_MEMBER_P (decl)
4364 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4365 /* We already checked these parameters when the template was
4366 declared, so there's no need to do it again now. This function
4367 was defined in class scope, but we're processing its body now
4368 that the class is complete. */
4369 return true;
4370
4371 /* Core issue 226 (C++0x only): the following only applies to class
4372 templates. */
4373 if (is_primary
4374 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4375 {
4376 /* [temp.param]
4377
4378 If a template-parameter has a default template-argument, all
4379 subsequent template-parameters shall have a default
4380 template-argument supplied. */
4381 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4382 {
4383 tree inner_parms = TREE_VALUE (parm_level);
4384 int ntparms = TREE_VEC_LENGTH (inner_parms);
4385 int seen_def_arg_p = 0;
4386 int i;
4387
4388 for (i = 0; i < ntparms; ++i)
4389 {
4390 tree parm = TREE_VEC_ELT (inner_parms, i);
4391
4392 if (parm == error_mark_node)
4393 continue;
4394
4395 if (TREE_PURPOSE (parm))
4396 seen_def_arg_p = 1;
4397 else if (seen_def_arg_p
4398 && !template_parameter_pack_p (TREE_VALUE (parm)))
4399 {
4400 error ("no default argument for %qD", TREE_VALUE (parm));
4401 /* For better subsequent error-recovery, we indicate that
4402 there should have been a default argument. */
4403 TREE_PURPOSE (parm) = error_mark_node;
4404 no_errors = false;
4405 }
4406 else if (!is_partial
4407 && !is_friend_decl
4408 /* Don't complain about an enclosing partial
4409 specialization. */
4410 && parm_level == parms
4411 && TREE_CODE (decl) == TYPE_DECL
4412 && i < ntparms - 1
4413 && template_parameter_pack_p (TREE_VALUE (parm)))
4414 {
4415 /* A primary class template can only have one
4416 parameter pack, at the end of the template
4417 parameter list. */
4418
4419 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4420 error ("parameter pack %qE must be at the end of the"
4421 " template parameter list", TREE_VALUE (parm));
4422 else
4423 error ("parameter pack %qT must be at the end of the"
4424 " template parameter list",
4425 TREE_TYPE (TREE_VALUE (parm)));
4426
4427 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4428 = error_mark_node;
4429 no_errors = false;
4430 }
4431 }
4432 }
4433 }
4434
4435 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4436 || is_partial
4437 || !is_primary
4438 || is_friend_decl)
4439 /* For an ordinary class template, default template arguments are
4440 allowed at the innermost level, e.g.:
4441 template <class T = int>
4442 struct S {};
4443 but, in a partial specialization, they're not allowed even
4444 there, as we have in [temp.class.spec]:
4445
4446 The template parameter list of a specialization shall not
4447 contain default template argument values.
4448
4449 So, for a partial specialization, or for a function template
4450 (in C++98/C++03), we look at all of them. */
4451 ;
4452 else
4453 /* But, for a primary class template that is not a partial
4454 specialization we look at all template parameters except the
4455 innermost ones. */
4456 parms = TREE_CHAIN (parms);
4457
4458 /* Figure out what error message to issue. */
4459 if (is_friend_decl == 2)
4460 msg = G_("default template arguments may not be used in function template "
4461 "friend re-declaration");
4462 else if (is_friend_decl)
4463 msg = G_("default template arguments may not be used in function template "
4464 "friend declarations");
4465 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4466 msg = G_("default template arguments may not be used in function templates "
4467 "without -std=c++11 or -std=gnu++11");
4468 else if (is_partial)
4469 msg = G_("default template arguments may not be used in "
4470 "partial specializations");
4471 else
4472 msg = G_("default argument for template parameter for class enclosing %qD");
4473
4474 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4475 /* If we're inside a class definition, there's no need to
4476 examine the parameters to the class itself. On the one
4477 hand, they will be checked when the class is defined, and,
4478 on the other, default arguments are valid in things like:
4479 template <class T = double>
4480 struct S { template <class U> void f(U); };
4481 Here the default argument for `S' has no bearing on the
4482 declaration of `f'. */
4483 last_level_to_check = template_class_depth (current_class_type) + 1;
4484 else
4485 /* Check everything. */
4486 last_level_to_check = 0;
4487
4488 for (parm_level = parms;
4489 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4490 parm_level = TREE_CHAIN (parm_level))
4491 {
4492 tree inner_parms = TREE_VALUE (parm_level);
4493 int i;
4494 int ntparms;
4495
4496 ntparms = TREE_VEC_LENGTH (inner_parms);
4497 for (i = 0; i < ntparms; ++i)
4498 {
4499 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4500 continue;
4501
4502 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4503 {
4504 if (msg)
4505 {
4506 no_errors = false;
4507 if (is_friend_decl == 2)
4508 return no_errors;
4509
4510 error (msg, decl);
4511 msg = 0;
4512 }
4513
4514 /* Clear out the default argument so that we are not
4515 confused later. */
4516 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4517 }
4518 }
4519
4520 /* At this point, if we're still interested in issuing messages,
4521 they must apply to classes surrounding the object declared. */
4522 if (msg)
4523 msg = G_("default argument for template parameter for class "
4524 "enclosing %qD");
4525 }
4526
4527 return no_errors;
4528 }
4529
4530 /* Worker for push_template_decl_real, called via
4531 for_each_template_parm. DATA is really an int, indicating the
4532 level of the parameters we are interested in. If T is a template
4533 parameter of that level, return nonzero. */
4534
4535 static int
4536 template_parm_this_level_p (tree t, void* data)
4537 {
4538 int this_level = *(int *)data;
4539 int level;
4540
4541 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4542 level = TEMPLATE_PARM_LEVEL (t);
4543 else
4544 level = TEMPLATE_TYPE_LEVEL (t);
4545 return level == this_level;
4546 }
4547
4548 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4549 parameters given by current_template_args, or reuses a
4550 previously existing one, if appropriate. Returns the DECL, or an
4551 equivalent one, if it is replaced via a call to duplicate_decls.
4552
4553 If IS_FRIEND is true, DECL is a friend declaration. */
4554
4555 tree
4556 push_template_decl_real (tree decl, bool is_friend)
4557 {
4558 tree tmpl;
4559 tree args;
4560 tree info;
4561 tree ctx;
4562 bool is_primary;
4563 bool is_partial;
4564 int new_template_p = 0;
4565 /* True if the template is a member template, in the sense of
4566 [temp.mem]. */
4567 bool member_template_p = false;
4568
4569 if (decl == error_mark_node || !current_template_parms)
4570 return error_mark_node;
4571
4572 /* See if this is a partial specialization. */
4573 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4574 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4575 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4576
4577 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4578 is_friend = true;
4579
4580 if (is_friend)
4581 /* For a friend, we want the context of the friend function, not
4582 the type of which it is a friend. */
4583 ctx = CP_DECL_CONTEXT (decl);
4584 else if (CP_DECL_CONTEXT (decl)
4585 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4586 /* In the case of a virtual function, we want the class in which
4587 it is defined. */
4588 ctx = CP_DECL_CONTEXT (decl);
4589 else
4590 /* Otherwise, if we're currently defining some class, the DECL
4591 is assumed to be a member of the class. */
4592 ctx = current_scope ();
4593
4594 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4595 ctx = NULL_TREE;
4596
4597 if (!DECL_CONTEXT (decl))
4598 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4599
4600 /* See if this is a primary template. */
4601 if (is_friend && ctx)
4602 /* A friend template that specifies a class context, i.e.
4603 template <typename T> friend void A<T>::f();
4604 is not primary. */
4605 is_primary = false;
4606 else
4607 is_primary = template_parm_scope_p ();
4608
4609 if (is_primary)
4610 {
4611 if (DECL_CLASS_SCOPE_P (decl))
4612 member_template_p = true;
4613 if (TREE_CODE (decl) == TYPE_DECL
4614 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4615 {
4616 error ("template class without a name");
4617 return error_mark_node;
4618 }
4619 else if (TREE_CODE (decl) == FUNCTION_DECL)
4620 {
4621 if (DECL_DESTRUCTOR_P (decl))
4622 {
4623 /* [temp.mem]
4624
4625 A destructor shall not be a member template. */
4626 error ("destructor %qD declared as member template", decl);
4627 return error_mark_node;
4628 }
4629 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4630 && (!prototype_p (TREE_TYPE (decl))
4631 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4632 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4633 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4634 == void_list_node)))
4635 {
4636 /* [basic.stc.dynamic.allocation]
4637
4638 An allocation function can be a function
4639 template. ... Template allocation functions shall
4640 have two or more parameters. */
4641 error ("invalid template declaration of %qD", decl);
4642 return error_mark_node;
4643 }
4644 }
4645 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4646 && CLASS_TYPE_P (TREE_TYPE (decl)))
4647 /* OK */;
4648 else if (TREE_CODE (decl) == TYPE_DECL
4649 && TYPE_DECL_ALIAS_P (decl))
4650 /* alias-declaration */
4651 gcc_assert (!DECL_ARTIFICIAL (decl));
4652 else
4653 {
4654 error ("template declaration of %q#D", decl);
4655 return error_mark_node;
4656 }
4657 }
4658
4659 /* Check to see that the rules regarding the use of default
4660 arguments are not being violated. */
4661 check_default_tmpl_args (decl, current_template_parms,
4662 is_primary, is_partial, /*is_friend_decl=*/0);
4663
4664 /* Ensure that there are no parameter packs in the type of this
4665 declaration that have not been expanded. */
4666 if (TREE_CODE (decl) == FUNCTION_DECL)
4667 {
4668 /* Check each of the arguments individually to see if there are
4669 any bare parameter packs. */
4670 tree type = TREE_TYPE (decl);
4671 tree arg = DECL_ARGUMENTS (decl);
4672 tree argtype = TYPE_ARG_TYPES (type);
4673
4674 while (arg && argtype)
4675 {
4676 if (!DECL_PACK_P (arg)
4677 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4678 {
4679 /* This is a PARM_DECL that contains unexpanded parameter
4680 packs. We have already complained about this in the
4681 check_for_bare_parameter_packs call, so just replace
4682 these types with ERROR_MARK_NODE. */
4683 TREE_TYPE (arg) = error_mark_node;
4684 TREE_VALUE (argtype) = error_mark_node;
4685 }
4686
4687 arg = DECL_CHAIN (arg);
4688 argtype = TREE_CHAIN (argtype);
4689 }
4690
4691 /* Check for bare parameter packs in the return type and the
4692 exception specifiers. */
4693 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4694 /* Errors were already issued, set return type to int
4695 as the frontend doesn't expect error_mark_node as
4696 the return type. */
4697 TREE_TYPE (type) = integer_type_node;
4698 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4699 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4700 }
4701 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4702 && TYPE_DECL_ALIAS_P (decl))
4703 ? DECL_ORIGINAL_TYPE (decl)
4704 : TREE_TYPE (decl)))
4705 {
4706 TREE_TYPE (decl) = error_mark_node;
4707 return error_mark_node;
4708 }
4709
4710 if (is_partial)
4711 return process_partial_specialization (decl);
4712
4713 args = current_template_args ();
4714
4715 if (!ctx
4716 || TREE_CODE (ctx) == FUNCTION_DECL
4717 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4718 || (TREE_CODE (decl) == TYPE_DECL
4719 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4720 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4721 {
4722 if (DECL_LANG_SPECIFIC (decl)
4723 && DECL_TEMPLATE_INFO (decl)
4724 && DECL_TI_TEMPLATE (decl))
4725 tmpl = DECL_TI_TEMPLATE (decl);
4726 /* If DECL is a TYPE_DECL for a class-template, then there won't
4727 be DECL_LANG_SPECIFIC. The information equivalent to
4728 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4729 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4730 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4731 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4732 {
4733 /* Since a template declaration already existed for this
4734 class-type, we must be redeclaring it here. Make sure
4735 that the redeclaration is valid. */
4736 redeclare_class_template (TREE_TYPE (decl),
4737 current_template_parms);
4738 /* We don't need to create a new TEMPLATE_DECL; just use the
4739 one we already had. */
4740 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4741 }
4742 else
4743 {
4744 tmpl = build_template_decl (decl, current_template_parms,
4745 member_template_p);
4746 new_template_p = 1;
4747
4748 if (DECL_LANG_SPECIFIC (decl)
4749 && DECL_TEMPLATE_SPECIALIZATION (decl))
4750 {
4751 /* A specialization of a member template of a template
4752 class. */
4753 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4754 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4755 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4756 }
4757 }
4758 }
4759 else
4760 {
4761 tree a, t, current, parms;
4762 int i;
4763 tree tinfo = get_template_info (decl);
4764
4765 if (!tinfo)
4766 {
4767 error ("template definition of non-template %q#D", decl);
4768 return error_mark_node;
4769 }
4770
4771 tmpl = TI_TEMPLATE (tinfo);
4772
4773 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4774 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4775 && DECL_TEMPLATE_SPECIALIZATION (decl)
4776 && DECL_MEMBER_TEMPLATE_P (tmpl))
4777 {
4778 tree new_tmpl;
4779
4780 /* The declaration is a specialization of a member
4781 template, declared outside the class. Therefore, the
4782 innermost template arguments will be NULL, so we
4783 replace them with the arguments determined by the
4784 earlier call to check_explicit_specialization. */
4785 args = DECL_TI_ARGS (decl);
4786
4787 new_tmpl
4788 = build_template_decl (decl, current_template_parms,
4789 member_template_p);
4790 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4791 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4792 DECL_TI_TEMPLATE (decl) = new_tmpl;
4793 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4794 DECL_TEMPLATE_INFO (new_tmpl)
4795 = build_template_info (tmpl, args);
4796
4797 register_specialization (new_tmpl,
4798 most_general_template (tmpl),
4799 args,
4800 is_friend, 0);
4801 return decl;
4802 }
4803
4804 /* Make sure the template headers we got make sense. */
4805
4806 parms = DECL_TEMPLATE_PARMS (tmpl);
4807 i = TMPL_PARMS_DEPTH (parms);
4808 if (TMPL_ARGS_DEPTH (args) != i)
4809 {
4810 error ("expected %d levels of template parms for %q#D, got %d",
4811 i, decl, TMPL_ARGS_DEPTH (args));
4812 }
4813 else
4814 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4815 {
4816 a = TMPL_ARGS_LEVEL (args, i);
4817 t = INNERMOST_TEMPLATE_PARMS (parms);
4818
4819 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4820 {
4821 if (current == decl)
4822 error ("got %d template parameters for %q#D",
4823 TREE_VEC_LENGTH (a), decl);
4824 else
4825 error ("got %d template parameters for %q#T",
4826 TREE_VEC_LENGTH (a), current);
4827 error (" but %d required", TREE_VEC_LENGTH (t));
4828 /* Avoid crash in import_export_decl. */
4829 DECL_INTERFACE_KNOWN (decl) = 1;
4830 return error_mark_node;
4831 }
4832
4833 if (current == decl)
4834 current = ctx;
4835 else if (current == NULL_TREE)
4836 /* Can happen in erroneous input. */
4837 break;
4838 else
4839 current = get_containing_scope (current);
4840 }
4841
4842 /* Check that the parms are used in the appropriate qualifying scopes
4843 in the declarator. */
4844 if (!comp_template_args
4845 (TI_ARGS (tinfo),
4846 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4847 {
4848 error ("\
4849 template arguments to %qD do not match original template %qD",
4850 decl, DECL_TEMPLATE_RESULT (tmpl));
4851 if (!uses_template_parms (TI_ARGS (tinfo)))
4852 inform (input_location, "use template<> for an explicit specialization");
4853 /* Avoid crash in import_export_decl. */
4854 DECL_INTERFACE_KNOWN (decl) = 1;
4855 return error_mark_node;
4856 }
4857 }
4858
4859 DECL_TEMPLATE_RESULT (tmpl) = decl;
4860 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4861
4862 /* Push template declarations for global functions and types. Note
4863 that we do not try to push a global template friend declared in a
4864 template class; such a thing may well depend on the template
4865 parameters of the class. */
4866 if (new_template_p && !ctx
4867 && !(is_friend && template_class_depth (current_class_type) > 0))
4868 {
4869 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4870 if (tmpl == error_mark_node)
4871 return error_mark_node;
4872
4873 /* Hide template friend classes that haven't been declared yet. */
4874 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4875 {
4876 DECL_ANTICIPATED (tmpl) = 1;
4877 DECL_FRIEND_P (tmpl) = 1;
4878 }
4879 }
4880
4881 if (is_primary)
4882 {
4883 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4884 int i;
4885
4886 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4887 if (DECL_CONV_FN_P (tmpl))
4888 {
4889 int depth = TMPL_PARMS_DEPTH (parms);
4890
4891 /* It is a conversion operator. See if the type converted to
4892 depends on innermost template operands. */
4893
4894 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4895 depth))
4896 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4897 }
4898
4899 /* Give template template parms a DECL_CONTEXT of the template
4900 for which they are a parameter. */
4901 parms = INNERMOST_TEMPLATE_PARMS (parms);
4902 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4903 {
4904 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4905 if (TREE_CODE (parm) == TEMPLATE_DECL)
4906 DECL_CONTEXT (parm) = tmpl;
4907 }
4908 }
4909
4910 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4911 back to its most general template. If TMPL is a specialization,
4912 ARGS may only have the innermost set of arguments. Add the missing
4913 argument levels if necessary. */
4914 if (DECL_TEMPLATE_INFO (tmpl))
4915 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4916
4917 info = build_template_info (tmpl, args);
4918
4919 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4920 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4921 else
4922 {
4923 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4924 retrofit_lang_decl (decl);
4925 if (DECL_LANG_SPECIFIC (decl))
4926 DECL_TEMPLATE_INFO (decl) = info;
4927 }
4928
4929 return DECL_TEMPLATE_RESULT (tmpl);
4930 }
4931
4932 tree
4933 push_template_decl (tree decl)
4934 {
4935 return push_template_decl_real (decl, false);
4936 }
4937
4938 /* FN is an inheriting constructor that inherits from the constructor
4939 template INHERITED; turn FN into a constructor template with a matching
4940 template header. */
4941
4942 tree
4943 add_inherited_template_parms (tree fn, tree inherited)
4944 {
4945 tree inner_parms
4946 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
4947 inner_parms = copy_node (inner_parms);
4948 tree parms
4949 = tree_cons (size_int (processing_template_decl + 1),
4950 inner_parms, current_template_parms);
4951 tree tmpl = build_template_decl (fn, parms, /*member*/true);
4952 tree args = template_parms_to_args (parms);
4953 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
4954 TREE_TYPE (tmpl) = TREE_TYPE (fn);
4955 DECL_TEMPLATE_RESULT (tmpl) = fn;
4956 DECL_ARTIFICIAL (tmpl) = true;
4957 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4958 return tmpl;
4959 }
4960
4961 /* Called when a class template TYPE is redeclared with the indicated
4962 template PARMS, e.g.:
4963
4964 template <class T> struct S;
4965 template <class T> struct S {}; */
4966
4967 bool
4968 redeclare_class_template (tree type, tree parms)
4969 {
4970 tree tmpl;
4971 tree tmpl_parms;
4972 int i;
4973
4974 if (!TYPE_TEMPLATE_INFO (type))
4975 {
4976 error ("%qT is not a template type", type);
4977 return false;
4978 }
4979
4980 tmpl = TYPE_TI_TEMPLATE (type);
4981 if (!PRIMARY_TEMPLATE_P (tmpl))
4982 /* The type is nested in some template class. Nothing to worry
4983 about here; there are no new template parameters for the nested
4984 type. */
4985 return true;
4986
4987 if (!parms)
4988 {
4989 error ("template specifiers not specified in declaration of %qD",
4990 tmpl);
4991 return false;
4992 }
4993
4994 parms = INNERMOST_TEMPLATE_PARMS (parms);
4995 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4996
4997 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4998 {
4999 error_n (input_location, TREE_VEC_LENGTH (parms),
5000 "redeclared with %d template parameter",
5001 "redeclared with %d template parameters",
5002 TREE_VEC_LENGTH (parms));
5003 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5004 "previous declaration %q+D used %d template parameter",
5005 "previous declaration %q+D used %d template parameters",
5006 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5007 return false;
5008 }
5009
5010 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5011 {
5012 tree tmpl_parm;
5013 tree parm;
5014 tree tmpl_default;
5015 tree parm_default;
5016
5017 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5018 || TREE_VEC_ELT (parms, i) == error_mark_node)
5019 continue;
5020
5021 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5022 if (tmpl_parm == error_mark_node)
5023 return false;
5024
5025 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5026 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5027 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5028
5029 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5030 TEMPLATE_DECL. */
5031 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5032 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5033 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5034 || (TREE_CODE (tmpl_parm) != PARM_DECL
5035 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5036 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5037 || (TREE_CODE (tmpl_parm) == PARM_DECL
5038 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5039 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5040 {
5041 error ("template parameter %q+#D", tmpl_parm);
5042 error ("redeclared here as %q#D", parm);
5043 return false;
5044 }
5045
5046 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5047 {
5048 /* We have in [temp.param]:
5049
5050 A template-parameter may not be given default arguments
5051 by two different declarations in the same scope. */
5052 error_at (input_location, "redefinition of default argument for %q#D", parm);
5053 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5054 "original definition appeared here");
5055 return false;
5056 }
5057
5058 if (parm_default != NULL_TREE)
5059 /* Update the previous template parameters (which are the ones
5060 that will really count) with the new default value. */
5061 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5062 else if (tmpl_default != NULL_TREE)
5063 /* Update the new parameters, too; they'll be used as the
5064 parameters for any members. */
5065 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5066 }
5067
5068 return true;
5069 }
5070
5071 /* Simplify EXPR if it is a non-dependent expression. Returns the
5072 (possibly simplified) expression. */
5073
5074 tree
5075 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5076 {
5077 if (expr == NULL_TREE)
5078 return NULL_TREE;
5079
5080 /* If we're in a template, but EXPR isn't value dependent, simplify
5081 it. We're supposed to treat:
5082
5083 template <typename T> void f(T[1 + 1]);
5084 template <typename T> void f(T[2]);
5085
5086 as two declarations of the same function, for example. */
5087 if (processing_template_decl
5088 && !instantiation_dependent_expression_p (expr)
5089 && potential_constant_expression (expr))
5090 {
5091 HOST_WIDE_INT saved_processing_template_decl;
5092
5093 saved_processing_template_decl = processing_template_decl;
5094 processing_template_decl = 0;
5095 expr = tsubst_copy_and_build (expr,
5096 /*args=*/NULL_TREE,
5097 complain,
5098 /*in_decl=*/NULL_TREE,
5099 /*function_p=*/false,
5100 /*integral_constant_expression_p=*/true);
5101 processing_template_decl = saved_processing_template_decl;
5102 }
5103 return expr;
5104 }
5105
5106 tree
5107 fold_non_dependent_expr (tree expr)
5108 {
5109 return fold_non_dependent_expr_sfinae (expr, tf_error);
5110 }
5111
5112 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5113 template declaration, or a TYPE_DECL for an alias declaration. */
5114
5115 bool
5116 alias_type_or_template_p (tree t)
5117 {
5118 if (t == NULL_TREE)
5119 return false;
5120 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5121 || (TYPE_P (t)
5122 && TYPE_NAME (t)
5123 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5124 || DECL_ALIAS_TEMPLATE_P (t));
5125 }
5126
5127 /* Return TRUE iff is a specialization of an alias template. */
5128
5129 bool
5130 alias_template_specialization_p (const_tree t)
5131 {
5132 if (t == NULL_TREE)
5133 return false;
5134
5135 return (TYPE_P (t)
5136 && TYPE_TEMPLATE_INFO (t)
5137 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5138 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5139 }
5140
5141 /* Return either TMPL or another template that it is equivalent to under DR
5142 1286: An alias that just changes the name of a template is equivalent to
5143 the other template. */
5144
5145 static tree
5146 get_underlying_template (tree tmpl)
5147 {
5148 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5149 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5150 {
5151 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5152 if (TYPE_TEMPLATE_INFO (result))
5153 {
5154 tree sub = TYPE_TI_TEMPLATE (result);
5155 if (PRIMARY_TEMPLATE_P (sub)
5156 && same_type_p (result, TREE_TYPE (sub)))
5157 {
5158 /* The alias type is equivalent to the pattern of the
5159 underlying template, so strip the alias. */
5160 tmpl = sub;
5161 continue;
5162 }
5163 }
5164 break;
5165 }
5166 return tmpl;
5167 }
5168
5169 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5170 must be a function or a pointer-to-function type, as specified
5171 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5172 and check that the resulting function has external linkage. */
5173
5174 static tree
5175 convert_nontype_argument_function (tree type, tree expr)
5176 {
5177 tree fns = expr;
5178 tree fn, fn_no_ptr;
5179 linkage_kind linkage;
5180
5181 fn = instantiate_type (type, fns, tf_none);
5182 if (fn == error_mark_node)
5183 return error_mark_node;
5184
5185 fn_no_ptr = fn;
5186 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5187 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5188 if (BASELINK_P (fn_no_ptr))
5189 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5190
5191 /* [temp.arg.nontype]/1
5192
5193 A template-argument for a non-type, non-template template-parameter
5194 shall be one of:
5195 [...]
5196 -- the address of an object or function with external [C++11: or
5197 internal] linkage. */
5198
5199 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5200 {
5201 error ("%qE is not a valid template argument for type %qT", expr, type);
5202 if (TYPE_PTR_P (type))
5203 error ("it must be the address of a function with external linkage");
5204 else
5205 error ("it must be the name of a function with external linkage");
5206 return NULL_TREE;
5207 }
5208
5209 linkage = decl_linkage (fn_no_ptr);
5210 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5211 {
5212 if (cxx_dialect >= cxx11)
5213 error ("%qE is not a valid template argument for type %qT "
5214 "because %qD has no linkage",
5215 expr, type, fn_no_ptr);
5216 else
5217 error ("%qE is not a valid template argument for type %qT "
5218 "because %qD does not have external linkage",
5219 expr, type, fn_no_ptr);
5220 return NULL_TREE;
5221 }
5222
5223 return fn;
5224 }
5225
5226 /* Subroutine of convert_nontype_argument.
5227 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5228 Emit an error otherwise. */
5229
5230 static bool
5231 check_valid_ptrmem_cst_expr (tree type, tree expr,
5232 tsubst_flags_t complain)
5233 {
5234 STRIP_NOPS (expr);
5235 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5236 return true;
5237 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5238 return true;
5239 if (complain & tf_error)
5240 {
5241 error ("%qE is not a valid template argument for type %qT",
5242 expr, type);
5243 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5244 }
5245 return false;
5246 }
5247
5248 /* Returns TRUE iff the address of OP is value-dependent.
5249
5250 14.6.2.4 [temp.dep.temp]:
5251 A non-integral non-type template-argument is dependent if its type is
5252 dependent or it has either of the following forms
5253 qualified-id
5254 & qualified-id
5255 and contains a nested-name-specifier which specifies a class-name that
5256 names a dependent type.
5257
5258 We generalize this to just say that the address of a member of a
5259 dependent class is value-dependent; the above doesn't cover the
5260 address of a static data member named with an unqualified-id. */
5261
5262 static bool
5263 has_value_dependent_address (tree op)
5264 {
5265 /* We could use get_inner_reference here, but there's no need;
5266 this is only relevant for template non-type arguments, which
5267 can only be expressed as &id-expression. */
5268 if (DECL_P (op))
5269 {
5270 tree ctx = CP_DECL_CONTEXT (op);
5271 if (TYPE_P (ctx) && dependent_type_p (ctx))
5272 return true;
5273 }
5274
5275 return false;
5276 }
5277
5278 /* The next set of functions are used for providing helpful explanatory
5279 diagnostics for failed overload resolution. Their messages should be
5280 indented by two spaces for consistency with the messages in
5281 call.c */
5282
5283 static int
5284 unify_success (bool /*explain_p*/)
5285 {
5286 return 0;
5287 }
5288
5289 static int
5290 unify_parameter_deduction_failure (bool explain_p, tree parm)
5291 {
5292 if (explain_p)
5293 inform (input_location,
5294 " couldn't deduce template parameter %qD", parm);
5295 return 1;
5296 }
5297
5298 static int
5299 unify_invalid (bool /*explain_p*/)
5300 {
5301 return 1;
5302 }
5303
5304 static int
5305 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5306 {
5307 if (explain_p)
5308 inform (input_location,
5309 " types %qT and %qT have incompatible cv-qualifiers",
5310 parm, arg);
5311 return 1;
5312 }
5313
5314 static int
5315 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5316 {
5317 if (explain_p)
5318 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5319 return 1;
5320 }
5321
5322 static int
5323 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5324 {
5325 if (explain_p)
5326 inform (input_location,
5327 " template parameter %qD is not a parameter pack, but "
5328 "argument %qD is",
5329 parm, arg);
5330 return 1;
5331 }
5332
5333 static int
5334 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5335 {
5336 if (explain_p)
5337 inform (input_location,
5338 " template argument %qE does not match "
5339 "pointer-to-member constant %qE",
5340 arg, parm);
5341 return 1;
5342 }
5343
5344 static int
5345 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5346 {
5347 if (explain_p)
5348 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5349 return 1;
5350 }
5351
5352 static int
5353 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5354 {
5355 if (explain_p)
5356 inform (input_location,
5357 " inconsistent parameter pack deduction with %qT and %qT",
5358 old_arg, new_arg);
5359 return 1;
5360 }
5361
5362 static int
5363 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5364 {
5365 if (explain_p)
5366 {
5367 if (TYPE_P (parm))
5368 inform (input_location,
5369 " deduced conflicting types for parameter %qT (%qT and %qT)",
5370 parm, first, second);
5371 else
5372 inform (input_location,
5373 " deduced conflicting values for non-type parameter "
5374 "%qE (%qE and %qE)", parm, first, second);
5375 }
5376 return 1;
5377 }
5378
5379 static int
5380 unify_vla_arg (bool explain_p, tree arg)
5381 {
5382 if (explain_p)
5383 inform (input_location,
5384 " variable-sized array type %qT is not "
5385 "a valid template argument",
5386 arg);
5387 return 1;
5388 }
5389
5390 static int
5391 unify_method_type_error (bool explain_p, tree arg)
5392 {
5393 if (explain_p)
5394 inform (input_location,
5395 " member function type %qT is not a valid template argument",
5396 arg);
5397 return 1;
5398 }
5399
5400 static int
5401 unify_arity (bool explain_p, int have, int wanted)
5402 {
5403 if (explain_p)
5404 inform_n (input_location, wanted,
5405 " candidate expects %d argument, %d provided",
5406 " candidate expects %d arguments, %d provided",
5407 wanted, have);
5408 return 1;
5409 }
5410
5411 static int
5412 unify_too_many_arguments (bool explain_p, int have, int wanted)
5413 {
5414 return unify_arity (explain_p, have, wanted);
5415 }
5416
5417 static int
5418 unify_too_few_arguments (bool explain_p, int have, int wanted)
5419 {
5420 return unify_arity (explain_p, have, wanted);
5421 }
5422
5423 static int
5424 unify_arg_conversion (bool explain_p, tree to_type,
5425 tree from_type, tree arg)
5426 {
5427 if (explain_p)
5428 inform (EXPR_LOC_OR_HERE (arg),
5429 " cannot convert %qE (type %qT) to type %qT",
5430 arg, from_type, to_type);
5431 return 1;
5432 }
5433
5434 static int
5435 unify_no_common_base (bool explain_p, enum template_base_result r,
5436 tree parm, tree arg)
5437 {
5438 if (explain_p)
5439 switch (r)
5440 {
5441 case tbr_ambiguous_baseclass:
5442 inform (input_location, " %qT is an ambiguous base class of %qT",
5443 parm, arg);
5444 break;
5445 default:
5446 inform (input_location, " %qT is not derived from %qT", arg, parm);
5447 break;
5448 }
5449 return 1;
5450 }
5451
5452 static int
5453 unify_inconsistent_template_template_parameters (bool explain_p)
5454 {
5455 if (explain_p)
5456 inform (input_location,
5457 " template parameters of a template template argument are "
5458 "inconsistent with other deduced template arguments");
5459 return 1;
5460 }
5461
5462 static int
5463 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5464 {
5465 if (explain_p)
5466 inform (input_location,
5467 " can't deduce a template for %qT from non-template type %qT",
5468 parm, arg);
5469 return 1;
5470 }
5471
5472 static int
5473 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5474 {
5475 if (explain_p)
5476 inform (input_location,
5477 " template argument %qE does not match %qD", arg, parm);
5478 return 1;
5479 }
5480
5481 static int
5482 unify_overload_resolution_failure (bool explain_p, tree arg)
5483 {
5484 if (explain_p)
5485 inform (input_location,
5486 " could not resolve address from overloaded function %qE",
5487 arg);
5488 return 1;
5489 }
5490
5491 /* Attempt to convert the non-type template parameter EXPR to the
5492 indicated TYPE. If the conversion is successful, return the
5493 converted value. If the conversion is unsuccessful, return
5494 NULL_TREE if we issued an error message, or error_mark_node if we
5495 did not. We issue error messages for out-and-out bad template
5496 parameters, but not simply because the conversion failed, since we
5497 might be just trying to do argument deduction. Both TYPE and EXPR
5498 must be non-dependent.
5499
5500 The conversion follows the special rules described in
5501 [temp.arg.nontype], and it is much more strict than an implicit
5502 conversion.
5503
5504 This function is called twice for each template argument (see
5505 lookup_template_class for a more accurate description of this
5506 problem). This means that we need to handle expressions which
5507 are not valid in a C++ source, but can be created from the
5508 first call (for instance, casts to perform conversions). These
5509 hacks can go away after we fix the double coercion problem. */
5510
5511 static tree
5512 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5513 {
5514 tree expr_type;
5515
5516 /* Detect immediately string literals as invalid non-type argument.
5517 This special-case is not needed for correctness (we would easily
5518 catch this later), but only to provide better diagnostic for this
5519 common user mistake. As suggested by DR 100, we do not mention
5520 linkage issues in the diagnostic as this is not the point. */
5521 /* FIXME we're making this OK. */
5522 if (TREE_CODE (expr) == STRING_CST)
5523 {
5524 if (complain & tf_error)
5525 error ("%qE is not a valid template argument for type %qT "
5526 "because string literals can never be used in this context",
5527 expr, type);
5528 return NULL_TREE;
5529 }
5530
5531 /* Add the ADDR_EXPR now for the benefit of
5532 value_dependent_expression_p. */
5533 if (TYPE_PTROBV_P (type)
5534 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5535 {
5536 expr = decay_conversion (expr, complain);
5537 if (expr == error_mark_node)
5538 return error_mark_node;
5539 }
5540
5541 /* If we are in a template, EXPR may be non-dependent, but still
5542 have a syntactic, rather than semantic, form. For example, EXPR
5543 might be a SCOPE_REF, rather than the VAR_DECL to which the
5544 SCOPE_REF refers. Preserving the qualifying scope is necessary
5545 so that access checking can be performed when the template is
5546 instantiated -- but here we need the resolved form so that we can
5547 convert the argument. */
5548 if (TYPE_REF_OBJ_P (type)
5549 && has_value_dependent_address (expr))
5550 /* If we want the address and it's value-dependent, don't fold. */;
5551 else if (!type_unknown_p (expr))
5552 expr = fold_non_dependent_expr_sfinae (expr, complain);
5553 if (error_operand_p (expr))
5554 return error_mark_node;
5555 expr_type = TREE_TYPE (expr);
5556 if (TREE_CODE (type) == REFERENCE_TYPE)
5557 expr = mark_lvalue_use (expr);
5558 else
5559 expr = mark_rvalue_use (expr);
5560
5561 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5562 to a non-type argument of "nullptr". */
5563 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5564 expr = convert (type, expr);
5565
5566 /* In C++11, integral or enumeration non-type template arguments can be
5567 arbitrary constant expressions. Pointer and pointer to
5568 member arguments can be general constant expressions that evaluate
5569 to a null value, but otherwise still need to be of a specific form. */
5570 if (cxx_dialect >= cxx11)
5571 {
5572 if (TREE_CODE (expr) == PTRMEM_CST)
5573 /* A PTRMEM_CST is already constant, and a valid template
5574 argument for a parameter of pointer to member type, we just want
5575 to leave it in that form rather than lower it to a
5576 CONSTRUCTOR. */;
5577 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5578 expr = maybe_constant_value (expr);
5579 else if (TYPE_PTR_OR_PTRMEM_P (type))
5580 {
5581 tree folded = maybe_constant_value (expr);
5582 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5583 : null_member_pointer_value_p (folded))
5584 expr = folded;
5585 }
5586 }
5587
5588 /* HACK: Due to double coercion, we can get a
5589 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5590 which is the tree that we built on the first call (see
5591 below when coercing to reference to object or to reference to
5592 function). We just strip everything and get to the arg.
5593 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5594 for examples. */
5595 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5596 {
5597 tree probe_type, probe = expr;
5598 if (REFERENCE_REF_P (probe))
5599 probe = TREE_OPERAND (probe, 0);
5600 probe_type = TREE_TYPE (probe);
5601 if (TREE_CODE (probe) == NOP_EXPR)
5602 {
5603 /* ??? Maybe we could use convert_from_reference here, but we
5604 would need to relax its constraints because the NOP_EXPR
5605 could actually change the type to something more cv-qualified,
5606 and this is not folded by convert_from_reference. */
5607 tree addr = TREE_OPERAND (probe, 0);
5608 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5609 && TREE_CODE (addr) == ADDR_EXPR
5610 && TYPE_PTR_P (TREE_TYPE (addr))
5611 && (same_type_ignoring_top_level_qualifiers_p
5612 (TREE_TYPE (probe_type),
5613 TREE_TYPE (TREE_TYPE (addr)))))
5614 {
5615 expr = TREE_OPERAND (addr, 0);
5616 expr_type = TREE_TYPE (probe_type);
5617 }
5618 }
5619 }
5620
5621 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5622 parameter is a pointer to object, through decay and
5623 qualification conversion. Let's strip everything. */
5624 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5625 {
5626 tree probe = expr;
5627 STRIP_NOPS (probe);
5628 if (TREE_CODE (probe) == ADDR_EXPR
5629 && TYPE_PTR_P (TREE_TYPE (probe)))
5630 {
5631 /* Skip the ADDR_EXPR only if it is part of the decay for
5632 an array. Otherwise, it is part of the original argument
5633 in the source code. */
5634 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5635 probe = TREE_OPERAND (probe, 0);
5636 expr = probe;
5637 expr_type = TREE_TYPE (expr);
5638 }
5639 }
5640
5641 /* [temp.arg.nontype]/5, bullet 1
5642
5643 For a non-type template-parameter of integral or enumeration type,
5644 integral promotions (_conv.prom_) and integral conversions
5645 (_conv.integral_) are applied. */
5646 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5647 {
5648 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5649 t = maybe_constant_value (t);
5650 if (t != error_mark_node)
5651 expr = t;
5652
5653 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5654 return error_mark_node;
5655
5656 /* Notice that there are constant expressions like '4 % 0' which
5657 do not fold into integer constants. */
5658 if (TREE_CODE (expr) != INTEGER_CST)
5659 {
5660 if (complain & tf_error)
5661 {
5662 int errs = errorcount, warns = warningcount + werrorcount;
5663 if (processing_template_decl
5664 && !require_potential_constant_expression (expr))
5665 return NULL_TREE;
5666 expr = cxx_constant_value (expr);
5667 if (errorcount > errs || warningcount + werrorcount > warns)
5668 inform (EXPR_LOC_OR_HERE (expr),
5669 "in template argument for type %qT ", type);
5670 if (expr == error_mark_node)
5671 return NULL_TREE;
5672 /* else cxx_constant_value complained but gave us
5673 a real constant, so go ahead. */
5674 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5675 }
5676 else
5677 return NULL_TREE;
5678 }
5679
5680 /* Avoid typedef problems. */
5681 if (TREE_TYPE (expr) != type)
5682 expr = fold_convert (type, expr);
5683 }
5684 /* [temp.arg.nontype]/5, bullet 2
5685
5686 For a non-type template-parameter of type pointer to object,
5687 qualification conversions (_conv.qual_) and the array-to-pointer
5688 conversion (_conv.array_) are applied. */
5689 else if (TYPE_PTROBV_P (type))
5690 {
5691 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5692
5693 A template-argument for a non-type, non-template template-parameter
5694 shall be one of: [...]
5695
5696 -- the name of a non-type template-parameter;
5697 -- the address of an object or function with external linkage, [...]
5698 expressed as "& id-expression" where the & is optional if the name
5699 refers to a function or array, or if the corresponding
5700 template-parameter is a reference.
5701
5702 Here, we do not care about functions, as they are invalid anyway
5703 for a parameter of type pointer-to-object. */
5704
5705 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5706 /* Non-type template parameters are OK. */
5707 ;
5708 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5709 /* Null pointer values are OK in C++11. */;
5710 else if (TREE_CODE (expr) != ADDR_EXPR
5711 && TREE_CODE (expr_type) != ARRAY_TYPE)
5712 {
5713 if (VAR_P (expr))
5714 {
5715 error ("%qD is not a valid template argument "
5716 "because %qD is a variable, not the address of "
5717 "a variable",
5718 expr, expr);
5719 return NULL_TREE;
5720 }
5721 if (POINTER_TYPE_P (expr_type))
5722 {
5723 error ("%qE is not a valid template argument for %qT "
5724 "because it is not the address of a variable",
5725 expr, type);
5726 return NULL_TREE;
5727 }
5728 /* Other values, like integer constants, might be valid
5729 non-type arguments of some other type. */
5730 return error_mark_node;
5731 }
5732 else
5733 {
5734 tree decl;
5735
5736 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5737 ? TREE_OPERAND (expr, 0) : expr);
5738 if (!VAR_P (decl))
5739 {
5740 error ("%qE is not a valid template argument of type %qT "
5741 "because %qE is not a variable",
5742 expr, type, decl);
5743 return NULL_TREE;
5744 }
5745 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5746 {
5747 error ("%qE is not a valid template argument of type %qT "
5748 "because %qD does not have external linkage",
5749 expr, type, decl);
5750 return NULL_TREE;
5751 }
5752 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5753 {
5754 error ("%qE is not a valid template argument of type %qT "
5755 "because %qD has no linkage",
5756 expr, type, decl);
5757 return NULL_TREE;
5758 }
5759 }
5760
5761 expr = decay_conversion (expr, complain);
5762 if (expr == error_mark_node)
5763 return error_mark_node;
5764
5765 expr = perform_qualification_conversions (type, expr);
5766 if (expr == error_mark_node)
5767 return error_mark_node;
5768 }
5769 /* [temp.arg.nontype]/5, bullet 3
5770
5771 For a non-type template-parameter of type reference to object, no
5772 conversions apply. The type referred to by the reference may be more
5773 cv-qualified than the (otherwise identical) type of the
5774 template-argument. The template-parameter is bound directly to the
5775 template-argument, which must be an lvalue. */
5776 else if (TYPE_REF_OBJ_P (type))
5777 {
5778 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5779 expr_type))
5780 return error_mark_node;
5781
5782 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5783 {
5784 error ("%qE is not a valid template argument for type %qT "
5785 "because of conflicts in cv-qualification", expr, type);
5786 return NULL_TREE;
5787 }
5788
5789 if (!real_lvalue_p (expr))
5790 {
5791 error ("%qE is not a valid template argument for type %qT "
5792 "because it is not an lvalue", expr, type);
5793 return NULL_TREE;
5794 }
5795
5796 /* [temp.arg.nontype]/1
5797
5798 A template-argument for a non-type, non-template template-parameter
5799 shall be one of: [...]
5800
5801 -- the address of an object or function with external linkage. */
5802 if (INDIRECT_REF_P (expr)
5803 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5804 {
5805 expr = TREE_OPERAND (expr, 0);
5806 if (DECL_P (expr))
5807 {
5808 error ("%q#D is not a valid template argument for type %qT "
5809 "because a reference variable does not have a constant "
5810 "address", expr, type);
5811 return NULL_TREE;
5812 }
5813 }
5814
5815 if (!DECL_P (expr))
5816 {
5817 error ("%qE is not a valid template argument for type %qT "
5818 "because it is not an object with external linkage",
5819 expr, type);
5820 return NULL_TREE;
5821 }
5822
5823 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5824 {
5825 error ("%qE is not a valid template argument for type %qT "
5826 "because object %qD has not external linkage",
5827 expr, type, expr);
5828 return NULL_TREE;
5829 }
5830
5831 expr = build_nop (type, build_address (expr));
5832 }
5833 /* [temp.arg.nontype]/5, bullet 4
5834
5835 For a non-type template-parameter of type pointer to function, only
5836 the function-to-pointer conversion (_conv.func_) is applied. If the
5837 template-argument represents a set of overloaded functions (or a
5838 pointer to such), the matching function is selected from the set
5839 (_over.over_). */
5840 else if (TYPE_PTRFN_P (type))
5841 {
5842 /* If the argument is a template-id, we might not have enough
5843 context information to decay the pointer. */
5844 if (!type_unknown_p (expr_type))
5845 {
5846 expr = decay_conversion (expr, complain);
5847 if (expr == error_mark_node)
5848 return error_mark_node;
5849 }
5850
5851 if (cxx_dialect >= cxx11 && integer_zerop (expr))
5852 /* Null pointer values are OK in C++11. */
5853 return perform_qualification_conversions (type, expr);
5854
5855 expr = convert_nontype_argument_function (type, expr);
5856 if (!expr || expr == error_mark_node)
5857 return expr;
5858 }
5859 /* [temp.arg.nontype]/5, bullet 5
5860
5861 For a non-type template-parameter of type reference to function, no
5862 conversions apply. If the template-argument represents a set of
5863 overloaded functions, the matching function is selected from the set
5864 (_over.over_). */
5865 else if (TYPE_REFFN_P (type))
5866 {
5867 if (TREE_CODE (expr) == ADDR_EXPR)
5868 {
5869 error ("%qE is not a valid template argument for type %qT "
5870 "because it is a pointer", expr, type);
5871 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5872 return NULL_TREE;
5873 }
5874
5875 expr = convert_nontype_argument_function (type, expr);
5876 if (!expr || expr == error_mark_node)
5877 return expr;
5878
5879 expr = build_nop (type, build_address (expr));
5880 }
5881 /* [temp.arg.nontype]/5, bullet 6
5882
5883 For a non-type template-parameter of type pointer to member function,
5884 no conversions apply. If the template-argument represents a set of
5885 overloaded member functions, the matching member function is selected
5886 from the set (_over.over_). */
5887 else if (TYPE_PTRMEMFUNC_P (type))
5888 {
5889 expr = instantiate_type (type, expr, tf_none);
5890 if (expr == error_mark_node)
5891 return error_mark_node;
5892
5893 /* [temp.arg.nontype] bullet 1 says the pointer to member
5894 expression must be a pointer-to-member constant. */
5895 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5896 return error_mark_node;
5897
5898 /* There is no way to disable standard conversions in
5899 resolve_address_of_overloaded_function (called by
5900 instantiate_type). It is possible that the call succeeded by
5901 converting &B::I to &D::I (where B is a base of D), so we need
5902 to reject this conversion here.
5903
5904 Actually, even if there was a way to disable standard conversions,
5905 it would still be better to reject them here so that we can
5906 provide a superior diagnostic. */
5907 if (!same_type_p (TREE_TYPE (expr), type))
5908 {
5909 error ("%qE is not a valid template argument for type %qT "
5910 "because it is of type %qT", expr, type,
5911 TREE_TYPE (expr));
5912 /* If we are just one standard conversion off, explain. */
5913 if (can_convert_standard (type, TREE_TYPE (expr), complain))
5914 inform (input_location,
5915 "standard conversions are not allowed in this context");
5916 return NULL_TREE;
5917 }
5918 }
5919 /* [temp.arg.nontype]/5, bullet 7
5920
5921 For a non-type template-parameter of type pointer to data member,
5922 qualification conversions (_conv.qual_) are applied. */
5923 else if (TYPE_PTRDATAMEM_P (type))
5924 {
5925 /* [temp.arg.nontype] bullet 1 says the pointer to member
5926 expression must be a pointer-to-member constant. */
5927 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5928 return error_mark_node;
5929
5930 expr = perform_qualification_conversions (type, expr);
5931 if (expr == error_mark_node)
5932 return expr;
5933 }
5934 else if (NULLPTR_TYPE_P (type))
5935 {
5936 if (expr != nullptr_node)
5937 {
5938 error ("%qE is not a valid template argument for type %qT "
5939 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5940 return NULL_TREE;
5941 }
5942 return expr;
5943 }
5944 /* A template non-type parameter must be one of the above. */
5945 else
5946 gcc_unreachable ();
5947
5948 /* Sanity check: did we actually convert the argument to the
5949 right type? */
5950 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5951 (type, TREE_TYPE (expr)));
5952 return expr;
5953 }
5954
5955 /* Subroutine of coerce_template_template_parms, which returns 1 if
5956 PARM_PARM and ARG_PARM match using the rule for the template
5957 parameters of template template parameters. Both PARM and ARG are
5958 template parameters; the rest of the arguments are the same as for
5959 coerce_template_template_parms.
5960 */
5961 static int
5962 coerce_template_template_parm (tree parm,
5963 tree arg,
5964 tsubst_flags_t complain,
5965 tree in_decl,
5966 tree outer_args)
5967 {
5968 if (arg == NULL_TREE || arg == error_mark_node
5969 || parm == NULL_TREE || parm == error_mark_node)
5970 return 0;
5971
5972 if (TREE_CODE (arg) != TREE_CODE (parm))
5973 return 0;
5974
5975 switch (TREE_CODE (parm))
5976 {
5977 case TEMPLATE_DECL:
5978 /* We encounter instantiations of templates like
5979 template <template <template <class> class> class TT>
5980 class C; */
5981 {
5982 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5983 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5984
5985 if (!coerce_template_template_parms
5986 (parmparm, argparm, complain, in_decl, outer_args))
5987 return 0;
5988 }
5989 /* Fall through. */
5990
5991 case TYPE_DECL:
5992 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5993 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5994 /* Argument is a parameter pack but parameter is not. */
5995 return 0;
5996 break;
5997
5998 case PARM_DECL:
5999 /* The tsubst call is used to handle cases such as
6000
6001 template <int> class C {};
6002 template <class T, template <T> class TT> class D {};
6003 D<int, C> d;
6004
6005 i.e. the parameter list of TT depends on earlier parameters. */
6006 if (!uses_template_parms (TREE_TYPE (arg))
6007 && !same_type_p
6008 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6009 TREE_TYPE (arg)))
6010 return 0;
6011
6012 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6013 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6014 /* Argument is a parameter pack but parameter is not. */
6015 return 0;
6016
6017 break;
6018
6019 default:
6020 gcc_unreachable ();
6021 }
6022
6023 return 1;
6024 }
6025
6026
6027 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6028 template template parameters. Both PARM_PARMS and ARG_PARMS are
6029 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6030 or PARM_DECL.
6031
6032 Consider the example:
6033 template <class T> class A;
6034 template<template <class U> class TT> class B;
6035
6036 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6037 the parameters to A, and OUTER_ARGS contains A. */
6038
6039 static int
6040 coerce_template_template_parms (tree parm_parms,
6041 tree arg_parms,
6042 tsubst_flags_t complain,
6043 tree in_decl,
6044 tree outer_args)
6045 {
6046 int nparms, nargs, i;
6047 tree parm, arg;
6048 int variadic_p = 0;
6049
6050 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6051 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6052
6053 nparms = TREE_VEC_LENGTH (parm_parms);
6054 nargs = TREE_VEC_LENGTH (arg_parms);
6055
6056 /* Determine whether we have a parameter pack at the end of the
6057 template template parameter's template parameter list. */
6058 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6059 {
6060 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6061
6062 if (parm == error_mark_node)
6063 return 0;
6064
6065 switch (TREE_CODE (parm))
6066 {
6067 case TEMPLATE_DECL:
6068 case TYPE_DECL:
6069 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6070 variadic_p = 1;
6071 break;
6072
6073 case PARM_DECL:
6074 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6075 variadic_p = 1;
6076 break;
6077
6078 default:
6079 gcc_unreachable ();
6080 }
6081 }
6082
6083 if (nargs != nparms
6084 && !(variadic_p && nargs >= nparms - 1))
6085 return 0;
6086
6087 /* Check all of the template parameters except the parameter pack at
6088 the end (if any). */
6089 for (i = 0; i < nparms - variadic_p; ++i)
6090 {
6091 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6092 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6093 continue;
6094
6095 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6096 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6097
6098 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6099 outer_args))
6100 return 0;
6101
6102 }
6103
6104 if (variadic_p)
6105 {
6106 /* Check each of the template parameters in the template
6107 argument against the template parameter pack at the end of
6108 the template template parameter. */
6109 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6110 return 0;
6111
6112 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6113
6114 for (; i < nargs; ++i)
6115 {
6116 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6117 continue;
6118
6119 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6120
6121 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6122 outer_args))
6123 return 0;
6124 }
6125 }
6126
6127 return 1;
6128 }
6129
6130 /* Verifies that the deduced template arguments (in TARGS) for the
6131 template template parameters (in TPARMS) represent valid bindings,
6132 by comparing the template parameter list of each template argument
6133 to the template parameter list of its corresponding template
6134 template parameter, in accordance with DR150. This
6135 routine can only be called after all template arguments have been
6136 deduced. It will return TRUE if all of the template template
6137 parameter bindings are okay, FALSE otherwise. */
6138 bool
6139 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6140 {
6141 int i, ntparms = TREE_VEC_LENGTH (tparms);
6142 bool ret = true;
6143
6144 /* We're dealing with template parms in this process. */
6145 ++processing_template_decl;
6146
6147 targs = INNERMOST_TEMPLATE_ARGS (targs);
6148
6149 for (i = 0; i < ntparms; ++i)
6150 {
6151 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6152 tree targ = TREE_VEC_ELT (targs, i);
6153
6154 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6155 {
6156 tree packed_args = NULL_TREE;
6157 int idx, len = 1;
6158
6159 if (ARGUMENT_PACK_P (targ))
6160 {
6161 /* Look inside the argument pack. */
6162 packed_args = ARGUMENT_PACK_ARGS (targ);
6163 len = TREE_VEC_LENGTH (packed_args);
6164 }
6165
6166 for (idx = 0; idx < len; ++idx)
6167 {
6168 tree targ_parms = NULL_TREE;
6169
6170 if (packed_args)
6171 /* Extract the next argument from the argument
6172 pack. */
6173 targ = TREE_VEC_ELT (packed_args, idx);
6174
6175 if (PACK_EXPANSION_P (targ))
6176 /* Look at the pattern of the pack expansion. */
6177 targ = PACK_EXPANSION_PATTERN (targ);
6178
6179 /* Extract the template parameters from the template
6180 argument. */
6181 if (TREE_CODE (targ) == TEMPLATE_DECL)
6182 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6183 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6184 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6185
6186 /* Verify that we can coerce the template template
6187 parameters from the template argument to the template
6188 parameter. This requires an exact match. */
6189 if (targ_parms
6190 && !coerce_template_template_parms
6191 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6192 targ_parms,
6193 tf_none,
6194 tparm,
6195 targs))
6196 {
6197 ret = false;
6198 goto out;
6199 }
6200 }
6201 }
6202 }
6203
6204 out:
6205
6206 --processing_template_decl;
6207 return ret;
6208 }
6209
6210 /* Since type attributes aren't mangled, we need to strip them from
6211 template type arguments. */
6212
6213 static tree
6214 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6215 {
6216 tree mv;
6217 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6218 return arg;
6219 mv = TYPE_MAIN_VARIANT (arg);
6220 arg = strip_typedefs (arg);
6221 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6222 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6223 {
6224 if (complain & tf_warning)
6225 warning (0, "ignoring attributes on template argument %qT", arg);
6226 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6227 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6228 }
6229 return arg;
6230 }
6231
6232 /* Convert the indicated template ARG as necessary to match the
6233 indicated template PARM. Returns the converted ARG, or
6234 error_mark_node if the conversion was unsuccessful. Error and
6235 warning messages are issued under control of COMPLAIN. This
6236 conversion is for the Ith parameter in the parameter list. ARGS is
6237 the full set of template arguments deduced so far. */
6238
6239 static tree
6240 convert_template_argument (tree parm,
6241 tree arg,
6242 tree args,
6243 tsubst_flags_t complain,
6244 int i,
6245 tree in_decl)
6246 {
6247 tree orig_arg;
6248 tree val;
6249 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6250
6251 if (TREE_CODE (arg) == TREE_LIST
6252 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6253 {
6254 /* The template argument was the name of some
6255 member function. That's usually
6256 invalid, but static members are OK. In any
6257 case, grab the underlying fields/functions
6258 and issue an error later if required. */
6259 orig_arg = TREE_VALUE (arg);
6260 TREE_TYPE (arg) = unknown_type_node;
6261 }
6262
6263 orig_arg = arg;
6264
6265 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6266 requires_type = (TREE_CODE (parm) == TYPE_DECL
6267 || requires_tmpl_type);
6268
6269 /* When determining whether an argument pack expansion is a template,
6270 look at the pattern. */
6271 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6272 arg = PACK_EXPANSION_PATTERN (arg);
6273
6274 /* Deal with an injected-class-name used as a template template arg. */
6275 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6276 {
6277 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6278 if (TREE_CODE (t) == TEMPLATE_DECL)
6279 {
6280 if (cxx_dialect >= cxx11)
6281 /* OK under DR 1004. */;
6282 else if (complain & tf_warning_or_error)
6283 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6284 " used as template template argument", TYPE_NAME (arg));
6285 else if (flag_pedantic_errors)
6286 t = arg;
6287
6288 arg = t;
6289 }
6290 }
6291
6292 is_tmpl_type =
6293 ((TREE_CODE (arg) == TEMPLATE_DECL
6294 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6295 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6296 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6297 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6298
6299 if (is_tmpl_type
6300 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6301 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6302 arg = TYPE_STUB_DECL (arg);
6303
6304 is_type = TYPE_P (arg) || is_tmpl_type;
6305
6306 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6307 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6308 {
6309 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6310 {
6311 if (complain & tf_error)
6312 error ("invalid use of destructor %qE as a type", orig_arg);
6313 return error_mark_node;
6314 }
6315
6316 permerror (input_location,
6317 "to refer to a type member of a template parameter, "
6318 "use %<typename %E%>", orig_arg);
6319
6320 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6321 TREE_OPERAND (arg, 1),
6322 typename_type,
6323 complain);
6324 arg = orig_arg;
6325 is_type = 1;
6326 }
6327 if (is_type != requires_type)
6328 {
6329 if (in_decl)
6330 {
6331 if (complain & tf_error)
6332 {
6333 error ("type/value mismatch at argument %d in template "
6334 "parameter list for %qD",
6335 i + 1, in_decl);
6336 if (is_type)
6337 error (" expected a constant of type %qT, got %qT",
6338 TREE_TYPE (parm),
6339 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6340 else if (requires_tmpl_type)
6341 error (" expected a class template, got %qE", orig_arg);
6342 else
6343 error (" expected a type, got %qE", orig_arg);
6344 }
6345 }
6346 return error_mark_node;
6347 }
6348 if (is_tmpl_type ^ requires_tmpl_type)
6349 {
6350 if (in_decl && (complain & tf_error))
6351 {
6352 error ("type/value mismatch at argument %d in template "
6353 "parameter list for %qD",
6354 i + 1, in_decl);
6355 if (is_tmpl_type)
6356 error (" expected a type, got %qT", DECL_NAME (arg));
6357 else
6358 error (" expected a class template, got %qT", orig_arg);
6359 }
6360 return error_mark_node;
6361 }
6362
6363 if (is_type)
6364 {
6365 if (requires_tmpl_type)
6366 {
6367 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6368 val = orig_arg;
6369 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6370 /* The number of argument required is not known yet.
6371 Just accept it for now. */
6372 val = TREE_TYPE (arg);
6373 else
6374 {
6375 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6376 tree argparm;
6377
6378 /* Strip alias templates that are equivalent to another
6379 template. */
6380 arg = get_underlying_template (arg);
6381 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6382
6383 if (coerce_template_template_parms (parmparm, argparm,
6384 complain, in_decl,
6385 args))
6386 {
6387 val = arg;
6388
6389 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6390 TEMPLATE_DECL. */
6391 if (val != error_mark_node)
6392 {
6393 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6394 val = TREE_TYPE (val);
6395 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6396 val = make_pack_expansion (val);
6397 }
6398 }
6399 else
6400 {
6401 if (in_decl && (complain & tf_error))
6402 {
6403 error ("type/value mismatch at argument %d in "
6404 "template parameter list for %qD",
6405 i + 1, in_decl);
6406 error (" expected a template of type %qD, got %qT",
6407 parm, orig_arg);
6408 }
6409
6410 val = error_mark_node;
6411 }
6412 }
6413 }
6414 else
6415 val = orig_arg;
6416 /* We only form one instance of each template specialization.
6417 Therefore, if we use a non-canonical variant (i.e., a
6418 typedef), any future messages referring to the type will use
6419 the typedef, which is confusing if those future uses do not
6420 themselves also use the typedef. */
6421 if (TYPE_P (val))
6422 val = canonicalize_type_argument (val, complain);
6423 }
6424 else
6425 {
6426 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6427
6428 if (invalid_nontype_parm_type_p (t, complain))
6429 return error_mark_node;
6430
6431 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6432 {
6433 if (same_type_p (t, TREE_TYPE (orig_arg)))
6434 val = orig_arg;
6435 else
6436 {
6437 /* Not sure if this is reachable, but it doesn't hurt
6438 to be robust. */
6439 error ("type mismatch in nontype parameter pack");
6440 val = error_mark_node;
6441 }
6442 }
6443 else if (!dependent_template_arg_p (orig_arg)
6444 && !uses_template_parms (t))
6445 /* We used to call digest_init here. However, digest_init
6446 will report errors, which we don't want when complain
6447 is zero. More importantly, digest_init will try too
6448 hard to convert things: for example, `0' should not be
6449 converted to pointer type at this point according to
6450 the standard. Accepting this is not merely an
6451 extension, since deciding whether or not these
6452 conversions can occur is part of determining which
6453 function template to call, or whether a given explicit
6454 argument specification is valid. */
6455 val = convert_nontype_argument (t, orig_arg, complain);
6456 else
6457 val = strip_typedefs_expr (orig_arg);
6458
6459 if (val == NULL_TREE)
6460 val = error_mark_node;
6461 else if (val == error_mark_node && (complain & tf_error))
6462 error ("could not convert template argument %qE to %qT", orig_arg, t);
6463
6464 if (TREE_CODE (val) == SCOPE_REF)
6465 {
6466 /* Strip typedefs from the SCOPE_REF. */
6467 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6468 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6469 complain);
6470 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6471 QUALIFIED_NAME_IS_TEMPLATE (val));
6472 }
6473 }
6474
6475 return val;
6476 }
6477
6478 /* Coerces the remaining template arguments in INNER_ARGS (from
6479 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6480 Returns the coerced argument pack. PARM_IDX is the position of this
6481 parameter in the template parameter list. ARGS is the original
6482 template argument list. */
6483 static tree
6484 coerce_template_parameter_pack (tree parms,
6485 int parm_idx,
6486 tree args,
6487 tree inner_args,
6488 int arg_idx,
6489 tree new_args,
6490 int* lost,
6491 tree in_decl,
6492 tsubst_flags_t complain)
6493 {
6494 tree parm = TREE_VEC_ELT (parms, parm_idx);
6495 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6496 tree packed_args;
6497 tree argument_pack;
6498 tree packed_types = NULL_TREE;
6499
6500 if (arg_idx > nargs)
6501 arg_idx = nargs;
6502
6503 packed_args = make_tree_vec (nargs - arg_idx);
6504
6505 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6506 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6507 {
6508 /* When the template parameter is a non-type template
6509 parameter pack whose type uses parameter packs, we need
6510 to look at each of the template arguments
6511 separately. Build a vector of the types for these
6512 non-type template parameters in PACKED_TYPES. */
6513 tree expansion
6514 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6515 packed_types = tsubst_pack_expansion (expansion, args,
6516 complain, in_decl);
6517
6518 if (packed_types == error_mark_node)
6519 return error_mark_node;
6520
6521 /* Check that we have the right number of arguments. */
6522 if (arg_idx < nargs
6523 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6524 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6525 {
6526 int needed_parms
6527 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6528 error ("wrong number of template arguments (%d, should be %d)",
6529 nargs, needed_parms);
6530 return error_mark_node;
6531 }
6532
6533 /* If we aren't able to check the actual arguments now
6534 (because they haven't been expanded yet), we can at least
6535 verify that all of the types used for the non-type
6536 template parameter pack are, in fact, valid for non-type
6537 template parameters. */
6538 if (arg_idx < nargs
6539 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6540 {
6541 int j, len = TREE_VEC_LENGTH (packed_types);
6542 for (j = 0; j < len; ++j)
6543 {
6544 tree t = TREE_VEC_ELT (packed_types, j);
6545 if (invalid_nontype_parm_type_p (t, complain))
6546 return error_mark_node;
6547 }
6548 }
6549 }
6550
6551 /* Convert the remaining arguments, which will be a part of the
6552 parameter pack "parm". */
6553 for (; arg_idx < nargs; ++arg_idx)
6554 {
6555 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6556 tree actual_parm = TREE_VALUE (parm);
6557
6558 if (packed_types && !PACK_EXPANSION_P (arg))
6559 {
6560 /* When we have a vector of types (corresponding to the
6561 non-type template parameter pack that uses parameter
6562 packs in its type, as mention above), and the
6563 argument is not an expansion (which expands to a
6564 currently unknown number of arguments), clone the
6565 parm and give it the next type in PACKED_TYPES. */
6566 actual_parm = copy_node (actual_parm);
6567 TREE_TYPE (actual_parm) =
6568 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6569 }
6570
6571 if (arg == error_mark_node)
6572 {
6573 if (complain & tf_error)
6574 error ("template argument %d is invalid", arg_idx + 1);
6575 }
6576 else
6577 arg = convert_template_argument (actual_parm,
6578 arg, new_args, complain, parm_idx,
6579 in_decl);
6580 if (arg == error_mark_node)
6581 (*lost)++;
6582 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6583 }
6584
6585 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6586 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6587 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6588 else
6589 {
6590 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6591 TREE_TYPE (argument_pack)
6592 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6593 TREE_CONSTANT (argument_pack) = 1;
6594 }
6595
6596 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6597 #ifdef ENABLE_CHECKING
6598 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6599 TREE_VEC_LENGTH (packed_args));
6600 #endif
6601 return argument_pack;
6602 }
6603
6604 /* Returns the number of pack expansions in the template argument vector
6605 ARGS. */
6606
6607 static int
6608 pack_expansion_args_count (tree args)
6609 {
6610 int i;
6611 int count = 0;
6612 if (args)
6613 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6614 {
6615 tree elt = TREE_VEC_ELT (args, i);
6616 if (elt && PACK_EXPANSION_P (elt))
6617 ++count;
6618 }
6619 return count;
6620 }
6621
6622 /* Convert all template arguments to their appropriate types, and
6623 return a vector containing the innermost resulting template
6624 arguments. If any error occurs, return error_mark_node. Error and
6625 warning messages are issued under control of COMPLAIN.
6626
6627 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6628 for arguments not specified in ARGS. Otherwise, if
6629 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6630 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6631 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6632 ARGS. */
6633
6634 static tree
6635 coerce_template_parms (tree parms,
6636 tree args,
6637 tree in_decl,
6638 tsubst_flags_t complain,
6639 bool require_all_args,
6640 bool use_default_args)
6641 {
6642 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6643 tree inner_args;
6644 tree new_args;
6645 tree new_inner_args;
6646 int saved_unevaluated_operand;
6647 int saved_inhibit_evaluation_warnings;
6648
6649 /* When used as a boolean value, indicates whether this is a
6650 variadic template parameter list. Since it's an int, we can also
6651 subtract it from nparms to get the number of non-variadic
6652 parameters. */
6653 int variadic_p = 0;
6654 int variadic_args_p = 0;
6655 int post_variadic_parms = 0;
6656
6657 if (args == error_mark_node)
6658 return error_mark_node;
6659
6660 nparms = TREE_VEC_LENGTH (parms);
6661
6662 /* Determine if there are any parameter packs. */
6663 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6664 {
6665 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6666 if (variadic_p)
6667 ++post_variadic_parms;
6668 if (template_parameter_pack_p (tparm))
6669 ++variadic_p;
6670 }
6671
6672 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6673 /* If there are no parameters that follow a parameter pack, we need to
6674 expand any argument packs so that we can deduce a parameter pack from
6675 some non-packed args followed by an argument pack, as in variadic85.C.
6676 If there are such parameters, we need to leave argument packs intact
6677 so the arguments are assigned properly. This can happen when dealing
6678 with a nested class inside a partial specialization of a class
6679 template, as in variadic92.C, or when deducing a template parameter pack
6680 from a sub-declarator, as in variadic114.C. */
6681 if (!post_variadic_parms)
6682 inner_args = expand_template_argument_pack (inner_args);
6683
6684 /* Count any pack expansion args. */
6685 variadic_args_p = pack_expansion_args_count (inner_args);
6686
6687 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6688 if ((nargs > nparms && !variadic_p)
6689 || (nargs < nparms - variadic_p
6690 && require_all_args
6691 && !variadic_args_p
6692 && (!use_default_args
6693 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6694 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6695 {
6696 if (complain & tf_error)
6697 {
6698 if (variadic_p)
6699 {
6700 nparms -= variadic_p;
6701 error ("wrong number of template arguments "
6702 "(%d, should be %d or more)", nargs, nparms);
6703 }
6704 else
6705 error ("wrong number of template arguments "
6706 "(%d, should be %d)", nargs, nparms);
6707
6708 if (in_decl)
6709 error ("provided for %q+D", in_decl);
6710 }
6711
6712 return error_mark_node;
6713 }
6714 /* We can't pass a pack expansion to a non-pack parameter of an alias
6715 template (DR 1430). */
6716 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
6717 && variadic_args_p
6718 && nargs - variadic_args_p < nparms - variadic_p)
6719 {
6720 if (complain & tf_error)
6721 {
6722 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
6723 {
6724 tree arg = TREE_VEC_ELT (inner_args, i);
6725 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6726
6727 if (PACK_EXPANSION_P (arg)
6728 && !template_parameter_pack_p (parm))
6729 {
6730 error ("pack expansion argument for non-pack parameter "
6731 "%qD of alias template %qD", parm, in_decl);
6732 inform (DECL_SOURCE_LOCATION (parm), "declared here");
6733 goto found;
6734 }
6735 }
6736 gcc_unreachable ();
6737 found:;
6738 }
6739 return error_mark_node;
6740 }
6741
6742 /* We need to evaluate the template arguments, even though this
6743 template-id may be nested within a "sizeof". */
6744 saved_unevaluated_operand = cp_unevaluated_operand;
6745 cp_unevaluated_operand = 0;
6746 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6747 c_inhibit_evaluation_warnings = 0;
6748 new_inner_args = make_tree_vec (nparms);
6749 new_args = add_outermost_template_args (args, new_inner_args);
6750 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6751 {
6752 tree arg;
6753 tree parm;
6754
6755 /* Get the Ith template parameter. */
6756 parm = TREE_VEC_ELT (parms, parm_idx);
6757
6758 if (parm == error_mark_node)
6759 {
6760 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6761 continue;
6762 }
6763
6764 /* Calculate the next argument. */
6765 if (arg_idx < nargs)
6766 arg = TREE_VEC_ELT (inner_args, arg_idx);
6767 else
6768 arg = NULL_TREE;
6769
6770 if (template_parameter_pack_p (TREE_VALUE (parm))
6771 && !(arg && ARGUMENT_PACK_P (arg)))
6772 {
6773 /* All remaining arguments will be placed in the
6774 template parameter pack PARM. */
6775 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6776 inner_args, arg_idx,
6777 new_args, &lost,
6778 in_decl, complain);
6779
6780 /* Store this argument. */
6781 if (arg == error_mark_node)
6782 lost++;
6783 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6784
6785 /* We are done with all of the arguments. */
6786 arg_idx = nargs;
6787
6788 continue;
6789 }
6790 else if (arg)
6791 {
6792 if (PACK_EXPANSION_P (arg))
6793 {
6794 /* We don't know how many args we have yet, just
6795 use the unconverted ones for now. */
6796 new_inner_args = inner_args;
6797 break;
6798 }
6799 }
6800 else if (require_all_args)
6801 {
6802 /* There must be a default arg in this case. */
6803 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6804 complain, in_decl);
6805 /* The position of the first default template argument,
6806 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6807 Record that. */
6808 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6809 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6810 }
6811 else
6812 break;
6813
6814 if (arg == error_mark_node)
6815 {
6816 if (complain & tf_error)
6817 error ("template argument %d is invalid", arg_idx + 1);
6818 }
6819 else if (!arg)
6820 /* This only occurs if there was an error in the template
6821 parameter list itself (which we would already have
6822 reported) that we are trying to recover from, e.g., a class
6823 template with a parameter list such as
6824 template<typename..., typename>. */
6825 ++lost;
6826 else
6827 arg = convert_template_argument (TREE_VALUE (parm),
6828 arg, new_args, complain,
6829 parm_idx, in_decl);
6830
6831 if (arg == error_mark_node)
6832 lost++;
6833 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6834 }
6835 cp_unevaluated_operand = saved_unevaluated_operand;
6836 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6837
6838 if (lost)
6839 return error_mark_node;
6840
6841 #ifdef ENABLE_CHECKING
6842 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6843 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6844 TREE_VEC_LENGTH (new_inner_args));
6845 #endif
6846
6847 return new_inner_args;
6848 }
6849
6850 /* Like coerce_template_parms. If PARMS represents all template
6851 parameters levels, this function returns a vector of vectors
6852 representing all the resulting argument levels. Note that in this
6853 case, only the innermost arguments are coerced because the
6854 outermost ones are supposed to have been coerced already.
6855
6856 Otherwise, if PARMS represents only (the innermost) vector of
6857 parameters, this function returns a vector containing just the
6858 innermost resulting arguments. */
6859
6860 static tree
6861 coerce_innermost_template_parms (tree parms,
6862 tree args,
6863 tree in_decl,
6864 tsubst_flags_t complain,
6865 bool require_all_args,
6866 bool use_default_args)
6867 {
6868 int parms_depth = TMPL_PARMS_DEPTH (parms);
6869 int args_depth = TMPL_ARGS_DEPTH (args);
6870 tree coerced_args;
6871
6872 if (parms_depth > 1)
6873 {
6874 coerced_args = make_tree_vec (parms_depth);
6875 tree level;
6876 int cur_depth;
6877
6878 for (level = parms, cur_depth = parms_depth;
6879 parms_depth > 0 && level != NULL_TREE;
6880 level = TREE_CHAIN (level), --cur_depth)
6881 {
6882 tree l;
6883 if (cur_depth == args_depth)
6884 l = coerce_template_parms (TREE_VALUE (level),
6885 args, in_decl, complain,
6886 require_all_args,
6887 use_default_args);
6888 else
6889 l = TMPL_ARGS_LEVEL (args, cur_depth);
6890
6891 if (l == error_mark_node)
6892 return error_mark_node;
6893
6894 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
6895 }
6896 }
6897 else
6898 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
6899 args, in_decl, complain,
6900 require_all_args,
6901 use_default_args);
6902 return coerced_args;
6903 }
6904
6905 /* Returns 1 if template args OT and NT are equivalent. */
6906
6907 static int
6908 template_args_equal (tree ot, tree nt)
6909 {
6910 if (nt == ot)
6911 return 1;
6912 if (nt == NULL_TREE || ot == NULL_TREE)
6913 return false;
6914
6915 if (TREE_CODE (nt) == TREE_VEC)
6916 /* For member templates */
6917 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6918 else if (PACK_EXPANSION_P (ot))
6919 return (PACK_EXPANSION_P (nt)
6920 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6921 PACK_EXPANSION_PATTERN (nt))
6922 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6923 PACK_EXPANSION_EXTRA_ARGS (nt)));
6924 else if (ARGUMENT_PACK_P (ot))
6925 {
6926 int i, len;
6927 tree opack, npack;
6928
6929 if (!ARGUMENT_PACK_P (nt))
6930 return 0;
6931
6932 opack = ARGUMENT_PACK_ARGS (ot);
6933 npack = ARGUMENT_PACK_ARGS (nt);
6934 len = TREE_VEC_LENGTH (opack);
6935 if (TREE_VEC_LENGTH (npack) != len)
6936 return 0;
6937 for (i = 0; i < len; ++i)
6938 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6939 TREE_VEC_ELT (npack, i)))
6940 return 0;
6941 return 1;
6942 }
6943 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6944 {
6945 /* We get here probably because we are in the middle of substituting
6946 into the pattern of a pack expansion. In that case the
6947 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6948 interested in. So we want to use the initial pack argument for
6949 the comparison. */
6950 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6951 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6952 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6953 return template_args_equal (ot, nt);
6954 }
6955 else if (TYPE_P (nt))
6956 return TYPE_P (ot) && same_type_p (ot, nt);
6957 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6958 return 0;
6959 else
6960 return cp_tree_equal (ot, nt);
6961 }
6962
6963 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6964 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6965 NEWARG_PTR with the offending arguments if they are non-NULL. */
6966
6967 static int
6968 comp_template_args_with_info (tree oldargs, tree newargs,
6969 tree *oldarg_ptr, tree *newarg_ptr)
6970 {
6971 int i;
6972
6973 if (oldargs == newargs)
6974 return 1;
6975
6976 if (!oldargs || !newargs)
6977 return 0;
6978
6979 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6980 return 0;
6981
6982 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6983 {
6984 tree nt = TREE_VEC_ELT (newargs, i);
6985 tree ot = TREE_VEC_ELT (oldargs, i);
6986
6987 if (! template_args_equal (ot, nt))
6988 {
6989 if (oldarg_ptr != NULL)
6990 *oldarg_ptr = ot;
6991 if (newarg_ptr != NULL)
6992 *newarg_ptr = nt;
6993 return 0;
6994 }
6995 }
6996 return 1;
6997 }
6998
6999 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7000 of template arguments. Returns 0 otherwise. */
7001
7002 int
7003 comp_template_args (tree oldargs, tree newargs)
7004 {
7005 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7006 }
7007
7008 static void
7009 add_pending_template (tree d)
7010 {
7011 tree ti = (TYPE_P (d)
7012 ? CLASSTYPE_TEMPLATE_INFO (d)
7013 : DECL_TEMPLATE_INFO (d));
7014 struct pending_template *pt;
7015 int level;
7016
7017 if (TI_PENDING_TEMPLATE_FLAG (ti))
7018 return;
7019
7020 /* We are called both from instantiate_decl, where we've already had a
7021 tinst_level pushed, and instantiate_template, where we haven't.
7022 Compensate. */
7023 level = !current_tinst_level || current_tinst_level->decl != d;
7024
7025 if (level)
7026 push_tinst_level (d);
7027
7028 pt = ggc_alloc_pending_template ();
7029 pt->next = NULL;
7030 pt->tinst = current_tinst_level;
7031 if (last_pending_template)
7032 last_pending_template->next = pt;
7033 else
7034 pending_templates = pt;
7035
7036 last_pending_template = pt;
7037
7038 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7039
7040 if (level)
7041 pop_tinst_level ();
7042 }
7043
7044
7045 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7046 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7047 documentation for TEMPLATE_ID_EXPR. */
7048
7049 tree
7050 lookup_template_function (tree fns, tree arglist)
7051 {
7052 tree type;
7053
7054 if (fns == error_mark_node || arglist == error_mark_node)
7055 return error_mark_node;
7056
7057 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7058
7059 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7060 {
7061 error ("%q#D is not a function template", fns);
7062 return error_mark_node;
7063 }
7064
7065 if (BASELINK_P (fns))
7066 {
7067 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7068 unknown_type_node,
7069 BASELINK_FUNCTIONS (fns),
7070 arglist);
7071 return fns;
7072 }
7073
7074 type = TREE_TYPE (fns);
7075 if (TREE_CODE (fns) == OVERLOAD || !type)
7076 type = unknown_type_node;
7077
7078 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7079 }
7080
7081 /* Within the scope of a template class S<T>, the name S gets bound
7082 (in build_self_reference) to a TYPE_DECL for the class, not a
7083 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7084 or one of its enclosing classes, and that type is a template,
7085 return the associated TEMPLATE_DECL. Otherwise, the original
7086 DECL is returned.
7087
7088 Also handle the case when DECL is a TREE_LIST of ambiguous
7089 injected-class-names from different bases. */
7090
7091 tree
7092 maybe_get_template_decl_from_type_decl (tree decl)
7093 {
7094 if (decl == NULL_TREE)
7095 return decl;
7096
7097 /* DR 176: A lookup that finds an injected-class-name (10.2
7098 [class.member.lookup]) can result in an ambiguity in certain cases
7099 (for example, if it is found in more than one base class). If all of
7100 the injected-class-names that are found refer to specializations of
7101 the same class template, and if the name is followed by a
7102 template-argument-list, the reference refers to the class template
7103 itself and not a specialization thereof, and is not ambiguous. */
7104 if (TREE_CODE (decl) == TREE_LIST)
7105 {
7106 tree t, tmpl = NULL_TREE;
7107 for (t = decl; t; t = TREE_CHAIN (t))
7108 {
7109 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7110 if (!tmpl)
7111 tmpl = elt;
7112 else if (tmpl != elt)
7113 break;
7114 }
7115 if (tmpl && t == NULL_TREE)
7116 return tmpl;
7117 else
7118 return decl;
7119 }
7120
7121 return (decl != NULL_TREE
7122 && DECL_SELF_REFERENCE_P (decl)
7123 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7124 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7125 }
7126
7127 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7128 parameters, find the desired type.
7129
7130 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7131
7132 IN_DECL, if non-NULL, is the template declaration we are trying to
7133 instantiate.
7134
7135 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7136 the class we are looking up.
7137
7138 Issue error and warning messages under control of COMPLAIN.
7139
7140 If the template class is really a local class in a template
7141 function, then the FUNCTION_CONTEXT is the function in which it is
7142 being instantiated.
7143
7144 ??? Note that this function is currently called *twice* for each
7145 template-id: the first time from the parser, while creating the
7146 incomplete type (finish_template_type), and the second type during the
7147 real instantiation (instantiate_template_class). This is surely something
7148 that we want to avoid. It also causes some problems with argument
7149 coercion (see convert_nontype_argument for more information on this). */
7150
7151 static tree
7152 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7153 int entering_scope, tsubst_flags_t complain)
7154 {
7155 tree templ = NULL_TREE, parmlist;
7156 tree t;
7157 void **slot;
7158 spec_entry *entry;
7159 spec_entry elt;
7160 hashval_t hash;
7161
7162 if (identifier_p (d1))
7163 {
7164 tree value = innermost_non_namespace_value (d1);
7165 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7166 templ = value;
7167 else
7168 {
7169 if (context)
7170 push_decl_namespace (context);
7171 templ = lookup_name (d1);
7172 templ = maybe_get_template_decl_from_type_decl (templ);
7173 if (context)
7174 pop_decl_namespace ();
7175 }
7176 if (templ)
7177 context = DECL_CONTEXT (templ);
7178 }
7179 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7180 {
7181 tree type = TREE_TYPE (d1);
7182
7183 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7184 an implicit typename for the second A. Deal with it. */
7185 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7186 type = TREE_TYPE (type);
7187
7188 if (CLASSTYPE_TEMPLATE_INFO (type))
7189 {
7190 templ = CLASSTYPE_TI_TEMPLATE (type);
7191 d1 = DECL_NAME (templ);
7192 }
7193 }
7194 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7195 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7196 {
7197 templ = TYPE_TI_TEMPLATE (d1);
7198 d1 = DECL_NAME (templ);
7199 }
7200 else if (TREE_CODE (d1) == TEMPLATE_DECL
7201 && DECL_TEMPLATE_RESULT (d1)
7202 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7203 {
7204 templ = d1;
7205 d1 = DECL_NAME (templ);
7206 context = DECL_CONTEXT (templ);
7207 }
7208 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7209 {
7210 templ = d1;
7211 d1 = DECL_NAME (templ);
7212 }
7213
7214 /* Issue an error message if we didn't find a template. */
7215 if (! templ)
7216 {
7217 if (complain & tf_error)
7218 error ("%qT is not a template", d1);
7219 return error_mark_node;
7220 }
7221
7222 if (TREE_CODE (templ) != TEMPLATE_DECL
7223 /* Make sure it's a user visible template, if it was named by
7224 the user. */
7225 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7226 && !PRIMARY_TEMPLATE_P (templ)))
7227 {
7228 if (complain & tf_error)
7229 {
7230 error ("non-template type %qT used as a template", d1);
7231 if (in_decl)
7232 error ("for template declaration %q+D", in_decl);
7233 }
7234 return error_mark_node;
7235 }
7236
7237 complain &= ~tf_user;
7238
7239 /* An alias that just changes the name of a template is equivalent to the
7240 other template, so if any of the arguments are pack expansions, strip
7241 the alias to avoid problems with a pack expansion passed to a non-pack
7242 alias template parameter (DR 1430). */
7243 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7244 templ = get_underlying_template (templ);
7245
7246 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7247 {
7248 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7249 template arguments */
7250
7251 tree parm;
7252 tree arglist2;
7253 tree outer;
7254
7255 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7256
7257 /* Consider an example where a template template parameter declared as
7258
7259 template <class T, class U = std::allocator<T> > class TT
7260
7261 The template parameter level of T and U are one level larger than
7262 of TT. To proper process the default argument of U, say when an
7263 instantiation `TT<int>' is seen, we need to build the full
7264 arguments containing {int} as the innermost level. Outer levels,
7265 available when not appearing as default template argument, can be
7266 obtained from the arguments of the enclosing template.
7267
7268 Suppose that TT is later substituted with std::vector. The above
7269 instantiation is `TT<int, std::allocator<T> >' with TT at
7270 level 1, and T at level 2, while the template arguments at level 1
7271 becomes {std::vector} and the inner level 2 is {int}. */
7272
7273 outer = DECL_CONTEXT (templ);
7274 if (outer)
7275 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7276 else if (current_template_parms)
7277 /* This is an argument of the current template, so we haven't set
7278 DECL_CONTEXT yet. */
7279 outer = current_template_args ();
7280
7281 if (outer)
7282 arglist = add_to_template_args (outer, arglist);
7283
7284 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7285 complain,
7286 /*require_all_args=*/true,
7287 /*use_default_args=*/true);
7288 if (arglist2 == error_mark_node
7289 || (!uses_template_parms (arglist2)
7290 && check_instantiated_args (templ, arglist2, complain)))
7291 return error_mark_node;
7292
7293 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7294 return parm;
7295 }
7296 else
7297 {
7298 tree template_type = TREE_TYPE (templ);
7299 tree gen_tmpl;
7300 tree type_decl;
7301 tree found = NULL_TREE;
7302 int arg_depth;
7303 int parm_depth;
7304 int is_dependent_type;
7305 int use_partial_inst_tmpl = false;
7306
7307 if (template_type == error_mark_node)
7308 /* An error occurred while building the template TEMPL, and a
7309 diagnostic has most certainly been emitted for that
7310 already. Let's propagate that error. */
7311 return error_mark_node;
7312
7313 gen_tmpl = most_general_template (templ);
7314 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7315 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7316 arg_depth = TMPL_ARGS_DEPTH (arglist);
7317
7318 if (arg_depth == 1 && parm_depth > 1)
7319 {
7320 /* We've been given an incomplete set of template arguments.
7321 For example, given:
7322
7323 template <class T> struct S1 {
7324 template <class U> struct S2 {};
7325 template <class U> struct S2<U*> {};
7326 };
7327
7328 we will be called with an ARGLIST of `U*', but the
7329 TEMPLATE will be `template <class T> template
7330 <class U> struct S1<T>::S2'. We must fill in the missing
7331 arguments. */
7332 arglist
7333 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7334 arglist);
7335 arg_depth = TMPL_ARGS_DEPTH (arglist);
7336 }
7337
7338 /* Now we should have enough arguments. */
7339 gcc_assert (parm_depth == arg_depth);
7340
7341 /* From here on, we're only interested in the most general
7342 template. */
7343
7344 /* Calculate the BOUND_ARGS. These will be the args that are
7345 actually tsubst'd into the definition to create the
7346 instantiation. */
7347 if (parm_depth > 1)
7348 {
7349 /* We have multiple levels of arguments to coerce, at once. */
7350 int i;
7351 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7352
7353 tree bound_args = make_tree_vec (parm_depth);
7354
7355 for (i = saved_depth,
7356 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7357 i > 0 && t != NULL_TREE;
7358 --i, t = TREE_CHAIN (t))
7359 {
7360 tree a;
7361 if (i == saved_depth)
7362 a = coerce_template_parms (TREE_VALUE (t),
7363 arglist, gen_tmpl,
7364 complain,
7365 /*require_all_args=*/true,
7366 /*use_default_args=*/true);
7367 else
7368 /* Outer levels should have already been coerced. */
7369 a = TMPL_ARGS_LEVEL (arglist, i);
7370
7371 /* Don't process further if one of the levels fails. */
7372 if (a == error_mark_node)
7373 {
7374 /* Restore the ARGLIST to its full size. */
7375 TREE_VEC_LENGTH (arglist) = saved_depth;
7376 return error_mark_node;
7377 }
7378
7379 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7380
7381 /* We temporarily reduce the length of the ARGLIST so
7382 that coerce_template_parms will see only the arguments
7383 corresponding to the template parameters it is
7384 examining. */
7385 TREE_VEC_LENGTH (arglist)--;
7386 }
7387
7388 /* Restore the ARGLIST to its full size. */
7389 TREE_VEC_LENGTH (arglist) = saved_depth;
7390
7391 arglist = bound_args;
7392 }
7393 else
7394 arglist
7395 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7396 INNERMOST_TEMPLATE_ARGS (arglist),
7397 gen_tmpl,
7398 complain,
7399 /*require_all_args=*/true,
7400 /*use_default_args=*/true);
7401
7402 if (arglist == error_mark_node)
7403 /* We were unable to bind the arguments. */
7404 return error_mark_node;
7405
7406 /* In the scope of a template class, explicit references to the
7407 template class refer to the type of the template, not any
7408 instantiation of it. For example, in:
7409
7410 template <class T> class C { void f(C<T>); }
7411
7412 the `C<T>' is just the same as `C'. Outside of the
7413 class, however, such a reference is an instantiation. */
7414 if ((entering_scope
7415 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7416 || currently_open_class (template_type))
7417 /* comp_template_args is expensive, check it last. */
7418 && comp_template_args (TYPE_TI_ARGS (template_type),
7419 arglist))
7420 return template_type;
7421
7422 /* If we already have this specialization, return it. */
7423 elt.tmpl = gen_tmpl;
7424 elt.args = arglist;
7425 hash = hash_specialization (&elt);
7426 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7427 &elt, hash);
7428
7429 if (entry)
7430 return entry->spec;
7431
7432 is_dependent_type = uses_template_parms (arglist);
7433
7434 /* If the deduced arguments are invalid, then the binding
7435 failed. */
7436 if (!is_dependent_type
7437 && check_instantiated_args (gen_tmpl,
7438 INNERMOST_TEMPLATE_ARGS (arglist),
7439 complain))
7440 return error_mark_node;
7441
7442 if (!is_dependent_type
7443 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7444 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7445 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7446 {
7447 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7448 DECL_NAME (gen_tmpl),
7449 /*tag_scope=*/ts_global);
7450 return found;
7451 }
7452
7453 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7454 complain, in_decl);
7455 if (context == error_mark_node)
7456 return error_mark_node;
7457
7458 if (!context)
7459 context = global_namespace;
7460
7461 /* Create the type. */
7462 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7463 {
7464 /* The user referred to a specialization of an alias
7465 template represented by GEN_TMPL.
7466
7467 [temp.alias]/2 says:
7468
7469 When a template-id refers to the specialization of an
7470 alias template, it is equivalent to the associated
7471 type obtained by substitution of its
7472 template-arguments for the template-parameters in the
7473 type-id of the alias template. */
7474
7475 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7476 /* Note that the call above (by indirectly calling
7477 register_specialization in tsubst_decl) registers the
7478 TYPE_DECL representing the specialization of the alias
7479 template. So next time someone substitutes ARGLIST for
7480 the template parms into the alias template (GEN_TMPL),
7481 she'll get that TYPE_DECL back. */
7482
7483 if (t == error_mark_node)
7484 return t;
7485 }
7486 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7487 {
7488 if (!is_dependent_type)
7489 {
7490 set_current_access_from_decl (TYPE_NAME (template_type));
7491 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7492 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7493 arglist, complain, in_decl),
7494 SCOPED_ENUM_P (template_type), NULL);
7495 }
7496 else
7497 {
7498 /* We don't want to call start_enum for this type, since
7499 the values for the enumeration constants may involve
7500 template parameters. And, no one should be interested
7501 in the enumeration constants for such a type. */
7502 t = cxx_make_type (ENUMERAL_TYPE);
7503 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7504 }
7505 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7506 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7507 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7508 }
7509 else if (CLASS_TYPE_P (template_type))
7510 {
7511 t = make_class_type (TREE_CODE (template_type));
7512 CLASSTYPE_DECLARED_CLASS (t)
7513 = CLASSTYPE_DECLARED_CLASS (template_type);
7514 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7515 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7516
7517 /* A local class. Make sure the decl gets registered properly. */
7518 if (context == current_function_decl)
7519 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7520
7521 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7522 /* This instantiation is another name for the primary
7523 template type. Set the TYPE_CANONICAL field
7524 appropriately. */
7525 TYPE_CANONICAL (t) = template_type;
7526 else if (any_template_arguments_need_structural_equality_p (arglist))
7527 /* Some of the template arguments require structural
7528 equality testing, so this template class requires
7529 structural equality testing. */
7530 SET_TYPE_STRUCTURAL_EQUALITY (t);
7531 }
7532 else
7533 gcc_unreachable ();
7534
7535 /* If we called start_enum or pushtag above, this information
7536 will already be set up. */
7537 if (!TYPE_NAME (t))
7538 {
7539 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7540
7541 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7542 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7543 DECL_SOURCE_LOCATION (type_decl)
7544 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7545 }
7546 else
7547 type_decl = TYPE_NAME (t);
7548
7549 if (CLASS_TYPE_P (template_type))
7550 {
7551 TREE_PRIVATE (type_decl)
7552 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7553 TREE_PROTECTED (type_decl)
7554 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7555 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7556 {
7557 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7558 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7559 }
7560 }
7561
7562 /* Let's consider the explicit specialization of a member
7563 of a class template specialization that is implicitely instantiated,
7564 e.g.:
7565 template<class T>
7566 struct S
7567 {
7568 template<class U> struct M {}; //#0
7569 };
7570
7571 template<>
7572 template<>
7573 struct S<int>::M<char> //#1
7574 {
7575 int i;
7576 };
7577 [temp.expl.spec]/4 says this is valid.
7578
7579 In this case, when we write:
7580 S<int>::M<char> m;
7581
7582 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7583 the one of #0.
7584
7585 When we encounter #1, we want to store the partial instantiation
7586 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7587
7588 For all cases other than this "explicit specialization of member of a
7589 class template", we just want to store the most general template into
7590 the CLASSTYPE_TI_TEMPLATE of M.
7591
7592 This case of "explicit specialization of member of a class template"
7593 only happens when:
7594 1/ the enclosing class is an instantiation of, and therefore not
7595 the same as, the context of the most general template, and
7596 2/ we aren't looking at the partial instantiation itself, i.e.
7597 the innermost arguments are not the same as the innermost parms of
7598 the most general template.
7599
7600 So it's only when 1/ and 2/ happens that we want to use the partial
7601 instantiation of the member template in lieu of its most general
7602 template. */
7603
7604 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7605 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7606 /* the enclosing class must be an instantiation... */
7607 && CLASS_TYPE_P (context)
7608 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7609 {
7610 tree partial_inst_args;
7611 TREE_VEC_LENGTH (arglist)--;
7612 ++processing_template_decl;
7613 partial_inst_args =
7614 tsubst (INNERMOST_TEMPLATE_ARGS
7615 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7616 arglist, complain, NULL_TREE);
7617 --processing_template_decl;
7618 TREE_VEC_LENGTH (arglist)++;
7619 use_partial_inst_tmpl =
7620 /*...and we must not be looking at the partial instantiation
7621 itself. */
7622 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7623 partial_inst_args);
7624 }
7625
7626 if (!use_partial_inst_tmpl)
7627 /* This case is easy; there are no member templates involved. */
7628 found = gen_tmpl;
7629 else
7630 {
7631 /* This is a full instantiation of a member template. Find
7632 the partial instantiation of which this is an instance. */
7633
7634 /* Temporarily reduce by one the number of levels in the ARGLIST
7635 so as to avoid comparing the last set of arguments. */
7636 TREE_VEC_LENGTH (arglist)--;
7637 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7638 TREE_VEC_LENGTH (arglist)++;
7639 /* FOUND is either a proper class type, or an alias
7640 template specialization. In the later case, it's a
7641 TYPE_DECL, resulting from the substituting of arguments
7642 for parameters in the TYPE_DECL of the alias template
7643 done earlier. So be careful while getting the template
7644 of FOUND. */
7645 found = TREE_CODE (found) == TYPE_DECL
7646 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7647 : CLASSTYPE_TI_TEMPLATE (found);
7648 }
7649
7650 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7651
7652 elt.spec = t;
7653 slot = htab_find_slot_with_hash (type_specializations,
7654 &elt, hash, INSERT);
7655 entry = ggc_alloc_spec_entry ();
7656 *entry = elt;
7657 *slot = entry;
7658
7659 /* Note this use of the partial instantiation so we can check it
7660 later in maybe_process_partial_specialization. */
7661 DECL_TEMPLATE_INSTANTIATIONS (templ)
7662 = tree_cons (arglist, t,
7663 DECL_TEMPLATE_INSTANTIATIONS (templ));
7664
7665 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
7666 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7667 /* Now that the type has been registered on the instantiations
7668 list, we set up the enumerators. Because the enumeration
7669 constants may involve the enumeration type itself, we make
7670 sure to register the type first, and then create the
7671 constants. That way, doing tsubst_expr for the enumeration
7672 constants won't result in recursive calls here; we'll find
7673 the instantiation and exit above. */
7674 tsubst_enum (template_type, t, arglist);
7675
7676 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7677 /* If the type makes use of template parameters, the
7678 code that generates debugging information will crash. */
7679 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7680
7681 /* Possibly limit visibility based on template args. */
7682 TREE_PUBLIC (type_decl) = 1;
7683 determine_visibility (type_decl);
7684
7685 return t;
7686 }
7687 }
7688
7689 /* Wrapper for lookup_template_class_1. */
7690
7691 tree
7692 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7693 int entering_scope, tsubst_flags_t complain)
7694 {
7695 tree ret;
7696 timevar_push (TV_TEMPLATE_INST);
7697 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7698 entering_scope, complain);
7699 timevar_pop (TV_TEMPLATE_INST);
7700 return ret;
7701 }
7702 \f
7703 struct pair_fn_data
7704 {
7705 tree_fn_t fn;
7706 void *data;
7707 /* True when we should also visit template parameters that occur in
7708 non-deduced contexts. */
7709 bool include_nondeduced_p;
7710 struct pointer_set_t *visited;
7711 };
7712
7713 /* Called from for_each_template_parm via walk_tree. */
7714
7715 static tree
7716 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7717 {
7718 tree t = *tp;
7719 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7720 tree_fn_t fn = pfd->fn;
7721 void *data = pfd->data;
7722
7723 if (TYPE_P (t)
7724 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7725 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7726 pfd->include_nondeduced_p))
7727 return error_mark_node;
7728
7729 switch (TREE_CODE (t))
7730 {
7731 case RECORD_TYPE:
7732 if (TYPE_PTRMEMFUNC_P (t))
7733 break;
7734 /* Fall through. */
7735
7736 case UNION_TYPE:
7737 case ENUMERAL_TYPE:
7738 if (!TYPE_TEMPLATE_INFO (t))
7739 *walk_subtrees = 0;
7740 else if (for_each_template_parm (TYPE_TI_ARGS (t),
7741 fn, data, pfd->visited,
7742 pfd->include_nondeduced_p))
7743 return error_mark_node;
7744 break;
7745
7746 case INTEGER_TYPE:
7747 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7748 fn, data, pfd->visited,
7749 pfd->include_nondeduced_p)
7750 || for_each_template_parm (TYPE_MAX_VALUE (t),
7751 fn, data, pfd->visited,
7752 pfd->include_nondeduced_p))
7753 return error_mark_node;
7754 break;
7755
7756 case METHOD_TYPE:
7757 /* Since we're not going to walk subtrees, we have to do this
7758 explicitly here. */
7759 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7760 pfd->visited, pfd->include_nondeduced_p))
7761 return error_mark_node;
7762 /* Fall through. */
7763
7764 case FUNCTION_TYPE:
7765 /* Check the return type. */
7766 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7767 pfd->include_nondeduced_p))
7768 return error_mark_node;
7769
7770 /* Check the parameter types. Since default arguments are not
7771 instantiated until they are needed, the TYPE_ARG_TYPES may
7772 contain expressions that involve template parameters. But,
7773 no-one should be looking at them yet. And, once they're
7774 instantiated, they don't contain template parameters, so
7775 there's no point in looking at them then, either. */
7776 {
7777 tree parm;
7778
7779 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7780 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7781 pfd->visited, pfd->include_nondeduced_p))
7782 return error_mark_node;
7783
7784 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7785 want walk_tree walking into them itself. */
7786 *walk_subtrees = 0;
7787 }
7788 break;
7789
7790 case TYPEOF_TYPE:
7791 case UNDERLYING_TYPE:
7792 if (pfd->include_nondeduced_p
7793 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7794 pfd->visited,
7795 pfd->include_nondeduced_p))
7796 return error_mark_node;
7797 break;
7798
7799 case FUNCTION_DECL:
7800 case VAR_DECL:
7801 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7802 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7803 pfd->visited, pfd->include_nondeduced_p))
7804 return error_mark_node;
7805 /* Fall through. */
7806
7807 case PARM_DECL:
7808 case CONST_DECL:
7809 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7810 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7811 pfd->visited, pfd->include_nondeduced_p))
7812 return error_mark_node;
7813 if (DECL_CONTEXT (t)
7814 && pfd->include_nondeduced_p
7815 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7816 pfd->visited, pfd->include_nondeduced_p))
7817 return error_mark_node;
7818 break;
7819
7820 case BOUND_TEMPLATE_TEMPLATE_PARM:
7821 /* Record template parameters such as `T' inside `TT<T>'. */
7822 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7823 pfd->include_nondeduced_p))
7824 return error_mark_node;
7825 /* Fall through. */
7826
7827 case TEMPLATE_TEMPLATE_PARM:
7828 case TEMPLATE_TYPE_PARM:
7829 case TEMPLATE_PARM_INDEX:
7830 if (fn && (*fn)(t, data))
7831 return error_mark_node;
7832 else if (!fn)
7833 return error_mark_node;
7834 break;
7835
7836 case TEMPLATE_DECL:
7837 /* A template template parameter is encountered. */
7838 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7839 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7840 pfd->include_nondeduced_p))
7841 return error_mark_node;
7842
7843 /* Already substituted template template parameter */
7844 *walk_subtrees = 0;
7845 break;
7846
7847 case TYPENAME_TYPE:
7848 if (!fn
7849 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7850 data, pfd->visited,
7851 pfd->include_nondeduced_p))
7852 return error_mark_node;
7853 break;
7854
7855 case CONSTRUCTOR:
7856 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7857 && pfd->include_nondeduced_p
7858 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7859 (TREE_TYPE (t)), fn, data,
7860 pfd->visited, pfd->include_nondeduced_p))
7861 return error_mark_node;
7862 break;
7863
7864 case INDIRECT_REF:
7865 case COMPONENT_REF:
7866 /* If there's no type, then this thing must be some expression
7867 involving template parameters. */
7868 if (!fn && !TREE_TYPE (t))
7869 return error_mark_node;
7870 break;
7871
7872 case MODOP_EXPR:
7873 case CAST_EXPR:
7874 case IMPLICIT_CONV_EXPR:
7875 case REINTERPRET_CAST_EXPR:
7876 case CONST_CAST_EXPR:
7877 case STATIC_CAST_EXPR:
7878 case DYNAMIC_CAST_EXPR:
7879 case ARROW_EXPR:
7880 case DOTSTAR_EXPR:
7881 case TYPEID_EXPR:
7882 case PSEUDO_DTOR_EXPR:
7883 if (!fn)
7884 return error_mark_node;
7885 break;
7886
7887 default:
7888 break;
7889 }
7890
7891 /* We didn't find any template parameters we liked. */
7892 return NULL_TREE;
7893 }
7894
7895 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7896 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7897 call FN with the parameter and the DATA.
7898 If FN returns nonzero, the iteration is terminated, and
7899 for_each_template_parm returns 1. Otherwise, the iteration
7900 continues. If FN never returns a nonzero value, the value
7901 returned by for_each_template_parm is 0. If FN is NULL, it is
7902 considered to be the function which always returns 1.
7903
7904 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7905 parameters that occur in non-deduced contexts. When false, only
7906 visits those template parameters that can be deduced. */
7907
7908 static int
7909 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7910 struct pointer_set_t *visited,
7911 bool include_nondeduced_p)
7912 {
7913 struct pair_fn_data pfd;
7914 int result;
7915
7916 /* Set up. */
7917 pfd.fn = fn;
7918 pfd.data = data;
7919 pfd.include_nondeduced_p = include_nondeduced_p;
7920
7921 /* Walk the tree. (Conceptually, we would like to walk without
7922 duplicates, but for_each_template_parm_r recursively calls
7923 for_each_template_parm, so we would need to reorganize a fair
7924 bit to use walk_tree_without_duplicates, so we keep our own
7925 visited list.) */
7926 if (visited)
7927 pfd.visited = visited;
7928 else
7929 pfd.visited = pointer_set_create ();
7930 result = cp_walk_tree (&t,
7931 for_each_template_parm_r,
7932 &pfd,
7933 pfd.visited) != NULL_TREE;
7934
7935 /* Clean up. */
7936 if (!visited)
7937 {
7938 pointer_set_destroy (pfd.visited);
7939 pfd.visited = 0;
7940 }
7941
7942 return result;
7943 }
7944
7945 /* Returns true if T depends on any template parameter. */
7946
7947 int
7948 uses_template_parms (tree t)
7949 {
7950 bool dependent_p;
7951 int saved_processing_template_decl;
7952
7953 saved_processing_template_decl = processing_template_decl;
7954 if (!saved_processing_template_decl)
7955 processing_template_decl = 1;
7956 if (TYPE_P (t))
7957 dependent_p = dependent_type_p (t);
7958 else if (TREE_CODE (t) == TREE_VEC)
7959 dependent_p = any_dependent_template_arguments_p (t);
7960 else if (TREE_CODE (t) == TREE_LIST)
7961 dependent_p = (uses_template_parms (TREE_VALUE (t))
7962 || uses_template_parms (TREE_CHAIN (t)));
7963 else if (TREE_CODE (t) == TYPE_DECL)
7964 dependent_p = dependent_type_p (TREE_TYPE (t));
7965 else if (DECL_P (t)
7966 || EXPR_P (t)
7967 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7968 || TREE_CODE (t) == OVERLOAD
7969 || BASELINK_P (t)
7970 || identifier_p (t)
7971 || TREE_CODE (t) == TRAIT_EXPR
7972 || TREE_CODE (t) == CONSTRUCTOR
7973 || CONSTANT_CLASS_P (t))
7974 dependent_p = (type_dependent_expression_p (t)
7975 || value_dependent_expression_p (t));
7976 else
7977 {
7978 gcc_assert (t == error_mark_node);
7979 dependent_p = false;
7980 }
7981
7982 processing_template_decl = saved_processing_template_decl;
7983
7984 return dependent_p;
7985 }
7986
7987 /* Returns true iff current_function_decl is an incompletely instantiated
7988 template. Useful instead of processing_template_decl because the latter
7989 is set to 0 during fold_non_dependent_expr. */
7990
7991 bool
7992 in_template_function (void)
7993 {
7994 tree fn = current_function_decl;
7995 bool ret;
7996 ++processing_template_decl;
7997 ret = (fn && DECL_LANG_SPECIFIC (fn)
7998 && DECL_TEMPLATE_INFO (fn)
7999 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8000 --processing_template_decl;
8001 return ret;
8002 }
8003
8004 /* Returns true if T depends on any template parameter with level LEVEL. */
8005
8006 int
8007 uses_template_parms_level (tree t, int level)
8008 {
8009 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8010 /*include_nondeduced_p=*/true);
8011 }
8012
8013 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8014 ill-formed translation unit, i.e. a variable or function that isn't
8015 usable in a constant expression. */
8016
8017 static inline bool
8018 neglectable_inst_p (tree d)
8019 {
8020 return (DECL_P (d)
8021 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8022 : decl_maybe_constant_var_p (d)));
8023 }
8024
8025 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8026 neglectable and instantiated from within an erroneous instantiation. */
8027
8028 static bool
8029 limit_bad_template_recursion (tree decl)
8030 {
8031 struct tinst_level *lev = current_tinst_level;
8032 int errs = errorcount + sorrycount;
8033 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8034 return false;
8035
8036 for (; lev; lev = lev->next)
8037 if (neglectable_inst_p (lev->decl))
8038 break;
8039
8040 return (lev && errs > lev->errors);
8041 }
8042
8043 static int tinst_depth;
8044 extern int max_tinst_depth;
8045 int depth_reached;
8046
8047 static GTY(()) struct tinst_level *last_error_tinst_level;
8048
8049 /* We're starting to instantiate D; record the template instantiation context
8050 for diagnostics and to restore it later. */
8051
8052 int
8053 push_tinst_level (tree d)
8054 {
8055 struct tinst_level *new_level;
8056
8057 if (tinst_depth >= max_tinst_depth)
8058 {
8059 last_error_tinst_level = current_tinst_level;
8060 if (TREE_CODE (d) == TREE_LIST)
8061 error ("template instantiation depth exceeds maximum of %d (use "
8062 "-ftemplate-depth= to increase the maximum) substituting %qS",
8063 max_tinst_depth, d);
8064 else
8065 error ("template instantiation depth exceeds maximum of %d (use "
8066 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8067 max_tinst_depth, d);
8068
8069 print_instantiation_context ();
8070
8071 return 0;
8072 }
8073
8074 /* If the current instantiation caused problems, don't let it instantiate
8075 anything else. Do allow deduction substitution and decls usable in
8076 constant expressions. */
8077 if (limit_bad_template_recursion (d))
8078 return 0;
8079
8080 new_level = ggc_alloc_tinst_level ();
8081 new_level->decl = d;
8082 new_level->locus = input_location;
8083 new_level->errors = errorcount+sorrycount;
8084 new_level->in_system_header_p = in_system_header;
8085 new_level->next = current_tinst_level;
8086 current_tinst_level = new_level;
8087
8088 ++tinst_depth;
8089 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8090 depth_reached = tinst_depth;
8091
8092 return 1;
8093 }
8094
8095 /* We're done instantiating this template; return to the instantiation
8096 context. */
8097
8098 void
8099 pop_tinst_level (void)
8100 {
8101 /* Restore the filename and line number stashed away when we started
8102 this instantiation. */
8103 input_location = current_tinst_level->locus;
8104 current_tinst_level = current_tinst_level->next;
8105 --tinst_depth;
8106 }
8107
8108 /* We're instantiating a deferred template; restore the template
8109 instantiation context in which the instantiation was requested, which
8110 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8111
8112 static tree
8113 reopen_tinst_level (struct tinst_level *level)
8114 {
8115 struct tinst_level *t;
8116
8117 tinst_depth = 0;
8118 for (t = level; t; t = t->next)
8119 ++tinst_depth;
8120
8121 current_tinst_level = level;
8122 pop_tinst_level ();
8123 if (current_tinst_level)
8124 current_tinst_level->errors = errorcount+sorrycount;
8125 return level->decl;
8126 }
8127
8128 /* Returns the TINST_LEVEL which gives the original instantiation
8129 context. */
8130
8131 struct tinst_level *
8132 outermost_tinst_level (void)
8133 {
8134 struct tinst_level *level = current_tinst_level;
8135 if (level)
8136 while (level->next)
8137 level = level->next;
8138 return level;
8139 }
8140
8141 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8142 vector of template arguments, as for tsubst.
8143
8144 Returns an appropriate tsubst'd friend declaration. */
8145
8146 static tree
8147 tsubst_friend_function (tree decl, tree args)
8148 {
8149 tree new_friend;
8150
8151 if (TREE_CODE (decl) == FUNCTION_DECL
8152 && DECL_TEMPLATE_INSTANTIATION (decl)
8153 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8154 /* This was a friend declared with an explicit template
8155 argument list, e.g.:
8156
8157 friend void f<>(T);
8158
8159 to indicate that f was a template instantiation, not a new
8160 function declaration. Now, we have to figure out what
8161 instantiation of what template. */
8162 {
8163 tree template_id, arglist, fns;
8164 tree new_args;
8165 tree tmpl;
8166 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8167
8168 /* Friend functions are looked up in the containing namespace scope.
8169 We must enter that scope, to avoid finding member functions of the
8170 current class with same name. */
8171 push_nested_namespace (ns);
8172 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8173 tf_warning_or_error, NULL_TREE,
8174 /*integral_constant_expression_p=*/false);
8175 pop_nested_namespace (ns);
8176 arglist = tsubst (DECL_TI_ARGS (decl), args,
8177 tf_warning_or_error, NULL_TREE);
8178 template_id = lookup_template_function (fns, arglist);
8179
8180 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8181 tmpl = determine_specialization (template_id, new_friend,
8182 &new_args,
8183 /*need_member_template=*/0,
8184 TREE_VEC_LENGTH (args),
8185 tsk_none);
8186 return instantiate_template (tmpl, new_args, tf_error);
8187 }
8188
8189 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8190
8191 /* The NEW_FRIEND will look like an instantiation, to the
8192 compiler, but is not an instantiation from the point of view of
8193 the language. For example, we might have had:
8194
8195 template <class T> struct S {
8196 template <class U> friend void f(T, U);
8197 };
8198
8199 Then, in S<int>, template <class U> void f(int, U) is not an
8200 instantiation of anything. */
8201 if (new_friend == error_mark_node)
8202 return error_mark_node;
8203
8204 DECL_USE_TEMPLATE (new_friend) = 0;
8205 if (TREE_CODE (decl) == TEMPLATE_DECL)
8206 {
8207 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8208 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8209 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8210 }
8211
8212 /* The mangled name for the NEW_FRIEND is incorrect. The function
8213 is not a template instantiation and should not be mangled like
8214 one. Therefore, we forget the mangling here; we'll recompute it
8215 later if we need it. */
8216 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8217 {
8218 SET_DECL_RTL (new_friend, NULL);
8219 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8220 }
8221
8222 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8223 {
8224 tree old_decl;
8225 tree new_friend_template_info;
8226 tree new_friend_result_template_info;
8227 tree ns;
8228 int new_friend_is_defn;
8229
8230 /* We must save some information from NEW_FRIEND before calling
8231 duplicate decls since that function will free NEW_FRIEND if
8232 possible. */
8233 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8234 new_friend_is_defn =
8235 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8236 (template_for_substitution (new_friend)))
8237 != NULL_TREE);
8238 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8239 {
8240 /* This declaration is a `primary' template. */
8241 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8242
8243 new_friend_result_template_info
8244 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8245 }
8246 else
8247 new_friend_result_template_info = NULL_TREE;
8248
8249 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8250 if (new_friend_is_defn)
8251 DECL_INITIAL (new_friend) = error_mark_node;
8252
8253 /* Inside pushdecl_namespace_level, we will push into the
8254 current namespace. However, the friend function should go
8255 into the namespace of the template. */
8256 ns = decl_namespace_context (new_friend);
8257 push_nested_namespace (ns);
8258 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8259 pop_nested_namespace (ns);
8260
8261 if (old_decl == error_mark_node)
8262 return error_mark_node;
8263
8264 if (old_decl != new_friend)
8265 {
8266 /* This new friend declaration matched an existing
8267 declaration. For example, given:
8268
8269 template <class T> void f(T);
8270 template <class U> class C {
8271 template <class T> friend void f(T) {}
8272 };
8273
8274 the friend declaration actually provides the definition
8275 of `f', once C has been instantiated for some type. So,
8276 old_decl will be the out-of-class template declaration,
8277 while new_friend is the in-class definition.
8278
8279 But, if `f' was called before this point, the
8280 instantiation of `f' will have DECL_TI_ARGS corresponding
8281 to `T' but not to `U', references to which might appear
8282 in the definition of `f'. Previously, the most general
8283 template for an instantiation of `f' was the out-of-class
8284 version; now it is the in-class version. Therefore, we
8285 run through all specialization of `f', adding to their
8286 DECL_TI_ARGS appropriately. In particular, they need a
8287 new set of outer arguments, corresponding to the
8288 arguments for this class instantiation.
8289
8290 The same situation can arise with something like this:
8291
8292 friend void f(int);
8293 template <class T> class C {
8294 friend void f(T) {}
8295 };
8296
8297 when `C<int>' is instantiated. Now, `f(int)' is defined
8298 in the class. */
8299
8300 if (!new_friend_is_defn)
8301 /* On the other hand, if the in-class declaration does
8302 *not* provide a definition, then we don't want to alter
8303 existing definitions. We can just leave everything
8304 alone. */
8305 ;
8306 else
8307 {
8308 tree new_template = TI_TEMPLATE (new_friend_template_info);
8309 tree new_args = TI_ARGS (new_friend_template_info);
8310
8311 /* Overwrite whatever template info was there before, if
8312 any, with the new template information pertaining to
8313 the declaration. */
8314 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8315
8316 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8317 {
8318 /* We should have called reregister_specialization in
8319 duplicate_decls. */
8320 gcc_assert (retrieve_specialization (new_template,
8321 new_args, 0)
8322 == old_decl);
8323
8324 /* Instantiate it if the global has already been used. */
8325 if (DECL_ODR_USED (old_decl))
8326 instantiate_decl (old_decl, /*defer_ok=*/true,
8327 /*expl_inst_class_mem_p=*/false);
8328 }
8329 else
8330 {
8331 tree t;
8332
8333 /* Indicate that the old function template is a partial
8334 instantiation. */
8335 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8336 = new_friend_result_template_info;
8337
8338 gcc_assert (new_template
8339 == most_general_template (new_template));
8340 gcc_assert (new_template != old_decl);
8341
8342 /* Reassign any specializations already in the hash table
8343 to the new more general template, and add the
8344 additional template args. */
8345 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8346 t != NULL_TREE;
8347 t = TREE_CHAIN (t))
8348 {
8349 tree spec = TREE_VALUE (t);
8350 spec_entry elt;
8351
8352 elt.tmpl = old_decl;
8353 elt.args = DECL_TI_ARGS (spec);
8354 elt.spec = NULL_TREE;
8355
8356 htab_remove_elt (decl_specializations, &elt);
8357
8358 DECL_TI_ARGS (spec)
8359 = add_outermost_template_args (new_args,
8360 DECL_TI_ARGS (spec));
8361
8362 register_specialization
8363 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8364
8365 }
8366 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8367 }
8368 }
8369
8370 /* The information from NEW_FRIEND has been merged into OLD_DECL
8371 by duplicate_decls. */
8372 new_friend = old_decl;
8373 }
8374 }
8375 else
8376 {
8377 tree context = DECL_CONTEXT (new_friend);
8378 bool dependent_p;
8379
8380 /* In the code
8381 template <class T> class C {
8382 template <class U> friend void C1<U>::f (); // case 1
8383 friend void C2<T>::f (); // case 2
8384 };
8385 we only need to make sure CONTEXT is a complete type for
8386 case 2. To distinguish between the two cases, we note that
8387 CONTEXT of case 1 remains dependent type after tsubst while
8388 this isn't true for case 2. */
8389 ++processing_template_decl;
8390 dependent_p = dependent_type_p (context);
8391 --processing_template_decl;
8392
8393 if (!dependent_p
8394 && !complete_type_or_else (context, NULL_TREE))
8395 return error_mark_node;
8396
8397 if (COMPLETE_TYPE_P (context))
8398 {
8399 /* Check to see that the declaration is really present, and,
8400 possibly obtain an improved declaration. */
8401 tree fn = check_classfn (context,
8402 new_friend, NULL_TREE);
8403
8404 if (fn)
8405 new_friend = fn;
8406 }
8407 }
8408
8409 return new_friend;
8410 }
8411
8412 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8413 template arguments, as for tsubst.
8414
8415 Returns an appropriate tsubst'd friend type or error_mark_node on
8416 failure. */
8417
8418 static tree
8419 tsubst_friend_class (tree friend_tmpl, tree args)
8420 {
8421 tree friend_type;
8422 tree tmpl;
8423 tree context;
8424
8425 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8426 {
8427 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8428 return TREE_TYPE (t);
8429 }
8430
8431 context = CP_DECL_CONTEXT (friend_tmpl);
8432
8433 if (context != global_namespace)
8434 {
8435 if (TREE_CODE (context) == NAMESPACE_DECL)
8436 push_nested_namespace (context);
8437 else
8438 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8439 }
8440
8441 /* Look for a class template declaration. We look for hidden names
8442 because two friend declarations of the same template are the
8443 same. For example, in:
8444
8445 struct A {
8446 template <typename> friend class F;
8447 };
8448 template <typename> struct B {
8449 template <typename> friend class F;
8450 };
8451
8452 both F templates are the same. */
8453 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8454 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8455
8456 /* But, if we don't find one, it might be because we're in a
8457 situation like this:
8458
8459 template <class T>
8460 struct S {
8461 template <class U>
8462 friend struct S;
8463 };
8464
8465 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8466 for `S<int>', not the TEMPLATE_DECL. */
8467 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8468 {
8469 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8470 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8471 }
8472
8473 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8474 {
8475 /* The friend template has already been declared. Just
8476 check to see that the declarations match, and install any new
8477 default parameters. We must tsubst the default parameters,
8478 of course. We only need the innermost template parameters
8479 because that is all that redeclare_class_template will look
8480 at. */
8481 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8482 > TMPL_ARGS_DEPTH (args))
8483 {
8484 tree parms;
8485 location_t saved_input_location;
8486 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8487 args, tf_warning_or_error);
8488
8489 saved_input_location = input_location;
8490 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8491 redeclare_class_template (TREE_TYPE (tmpl), parms);
8492 input_location = saved_input_location;
8493
8494 }
8495
8496 friend_type = TREE_TYPE (tmpl);
8497 }
8498 else
8499 {
8500 /* The friend template has not already been declared. In this
8501 case, the instantiation of the template class will cause the
8502 injection of this template into the global scope. */
8503 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8504 if (tmpl == error_mark_node)
8505 return error_mark_node;
8506
8507 /* The new TMPL is not an instantiation of anything, so we
8508 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8509 the new type because that is supposed to be the corresponding
8510 template decl, i.e., TMPL. */
8511 DECL_USE_TEMPLATE (tmpl) = 0;
8512 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8513 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8514 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8515 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8516
8517 /* Inject this template into the global scope. */
8518 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8519 }
8520
8521 if (context != global_namespace)
8522 {
8523 if (TREE_CODE (context) == NAMESPACE_DECL)
8524 pop_nested_namespace (context);
8525 else
8526 pop_nested_class ();
8527 }
8528
8529 return friend_type;
8530 }
8531
8532 /* Returns zero if TYPE cannot be completed later due to circularity.
8533 Otherwise returns one. */
8534
8535 static int
8536 can_complete_type_without_circularity (tree type)
8537 {
8538 if (type == NULL_TREE || type == error_mark_node)
8539 return 0;
8540 else if (COMPLETE_TYPE_P (type))
8541 return 1;
8542 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8543 return can_complete_type_without_circularity (TREE_TYPE (type));
8544 else if (CLASS_TYPE_P (type)
8545 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8546 return 0;
8547 else
8548 return 1;
8549 }
8550
8551 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8552
8553 /* Apply any attributes which had to be deferred until instantiation
8554 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8555 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8556
8557 static void
8558 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8559 tree args, tsubst_flags_t complain, tree in_decl)
8560 {
8561 tree last_dep = NULL_TREE;
8562 tree t;
8563 tree *p;
8564
8565 for (t = attributes; t; t = TREE_CHAIN (t))
8566 if (ATTR_IS_DEPENDENT (t))
8567 {
8568 last_dep = t;
8569 attributes = copy_list (attributes);
8570 break;
8571 }
8572
8573 if (DECL_P (*decl_p))
8574 {
8575 if (TREE_TYPE (*decl_p) == error_mark_node)
8576 return;
8577 p = &DECL_ATTRIBUTES (*decl_p);
8578 }
8579 else
8580 p = &TYPE_ATTRIBUTES (*decl_p);
8581
8582 if (last_dep)
8583 {
8584 tree late_attrs = NULL_TREE;
8585 tree *q = &late_attrs;
8586
8587 for (*p = attributes; *p; )
8588 {
8589 t = *p;
8590 if (ATTR_IS_DEPENDENT (t))
8591 {
8592 *p = TREE_CHAIN (t);
8593 TREE_CHAIN (t) = NULL_TREE;
8594 if (flag_openmp
8595 && is_attribute_p ("omp declare simd",
8596 get_attribute_name (t))
8597 && TREE_VALUE (t))
8598 {
8599 tree clauses = TREE_VALUE (TREE_VALUE (t));
8600 clauses = tsubst_omp_clauses (clauses, true, args,
8601 complain, in_decl);
8602 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8603 clauses = finish_omp_clauses (clauses);
8604 tree parms = DECL_ARGUMENTS (*decl_p);
8605 clauses
8606 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8607 if (clauses)
8608 TREE_VALUE (TREE_VALUE (t)) = clauses;
8609 else
8610 TREE_VALUE (t) = NULL_TREE;
8611 }
8612 /* If the first attribute argument is an identifier, don't
8613 pass it through tsubst. Attributes like mode, format,
8614 cleanup and several target specific attributes expect it
8615 unmodified. */
8616 else if (attribute_takes_identifier_p (get_attribute_name (t)))
8617 {
8618 tree chain
8619 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8620 in_decl,
8621 /*integral_constant_expression_p=*/false);
8622 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8623 TREE_VALUE (t)
8624 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8625 chain);
8626 }
8627 else
8628 TREE_VALUE (t)
8629 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8630 /*integral_constant_expression_p=*/false);
8631 *q = t;
8632 q = &TREE_CHAIN (t);
8633 }
8634 else
8635 p = &TREE_CHAIN (t);
8636 }
8637
8638 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8639 }
8640 }
8641
8642 /* Perform (or defer) access check for typedefs that were referenced
8643 from within the template TMPL code.
8644 This is a subroutine of instantiate_decl and instantiate_class_template.
8645 TMPL is the template to consider and TARGS is the list of arguments of
8646 that template. */
8647
8648 static void
8649 perform_typedefs_access_check (tree tmpl, tree targs)
8650 {
8651 location_t saved_location;
8652 unsigned i;
8653 qualified_typedef_usage_t *iter;
8654
8655 if (!tmpl
8656 || (!CLASS_TYPE_P (tmpl)
8657 && TREE_CODE (tmpl) != FUNCTION_DECL))
8658 return;
8659
8660 saved_location = input_location;
8661 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8662 {
8663 tree type_decl = iter->typedef_decl;
8664 tree type_scope = iter->context;
8665
8666 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8667 continue;
8668
8669 if (uses_template_parms (type_decl))
8670 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8671 if (uses_template_parms (type_scope))
8672 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8673
8674 /* Make access check error messages point to the location
8675 of the use of the typedef. */
8676 input_location = iter->locus;
8677 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8678 type_decl, type_decl,
8679 tf_warning_or_error);
8680 }
8681 input_location = saved_location;
8682 }
8683
8684 static tree
8685 instantiate_class_template_1 (tree type)
8686 {
8687 tree templ, args, pattern, t, member;
8688 tree typedecl;
8689 tree pbinfo;
8690 tree base_list;
8691 unsigned int saved_maximum_field_alignment;
8692 tree fn_context;
8693
8694 if (type == error_mark_node)
8695 return error_mark_node;
8696
8697 if (COMPLETE_OR_OPEN_TYPE_P (type)
8698 || uses_template_parms (type))
8699 return type;
8700
8701 /* Figure out which template is being instantiated. */
8702 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8703 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8704
8705 /* Determine what specialization of the original template to
8706 instantiate. */
8707 t = most_specialized_class (type, templ, tf_warning_or_error);
8708 if (t == error_mark_node)
8709 {
8710 TYPE_BEING_DEFINED (type) = 1;
8711 return error_mark_node;
8712 }
8713 else if (t)
8714 {
8715 /* This TYPE is actually an instantiation of a partial
8716 specialization. We replace the innermost set of ARGS with
8717 the arguments appropriate for substitution. For example,
8718 given:
8719
8720 template <class T> struct S {};
8721 template <class T> struct S<T*> {};
8722
8723 and supposing that we are instantiating S<int*>, ARGS will
8724 presently be {int*} -- but we need {int}. */
8725 pattern = TREE_TYPE (t);
8726 args = TREE_PURPOSE (t);
8727 }
8728 else
8729 {
8730 pattern = TREE_TYPE (templ);
8731 args = CLASSTYPE_TI_ARGS (type);
8732 }
8733
8734 /* If the template we're instantiating is incomplete, then clearly
8735 there's nothing we can do. */
8736 if (!COMPLETE_TYPE_P (pattern))
8737 return type;
8738
8739 /* If we've recursively instantiated too many templates, stop. */
8740 if (! push_tinst_level (type))
8741 return type;
8742
8743 /* Now we're really doing the instantiation. Mark the type as in
8744 the process of being defined. */
8745 TYPE_BEING_DEFINED (type) = 1;
8746
8747 /* We may be in the middle of deferred access check. Disable
8748 it now. */
8749 push_deferring_access_checks (dk_no_deferred);
8750
8751 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8752 if (!fn_context)
8753 push_to_top_level ();
8754 /* Use #pragma pack from the template context. */
8755 saved_maximum_field_alignment = maximum_field_alignment;
8756 maximum_field_alignment = TYPE_PRECISION (pattern);
8757
8758 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8759
8760 /* Set the input location to the most specialized template definition.
8761 This is needed if tsubsting causes an error. */
8762 typedecl = TYPE_MAIN_DECL (pattern);
8763 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8764 DECL_SOURCE_LOCATION (typedecl);
8765
8766 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8767 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8768 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8769 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8770 if (ANON_AGGR_TYPE_P (pattern))
8771 SET_ANON_AGGR_TYPE_P (type);
8772 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8773 {
8774 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8775 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8776 /* Adjust visibility for template arguments. */
8777 determine_visibility (TYPE_MAIN_DECL (type));
8778 }
8779 if (CLASS_TYPE_P (type))
8780 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8781
8782 pbinfo = TYPE_BINFO (pattern);
8783
8784 /* We should never instantiate a nested class before its enclosing
8785 class; we need to look up the nested class by name before we can
8786 instantiate it, and that lookup should instantiate the enclosing
8787 class. */
8788 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8789 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8790
8791 base_list = NULL_TREE;
8792 if (BINFO_N_BASE_BINFOS (pbinfo))
8793 {
8794 tree pbase_binfo;
8795 tree pushed_scope;
8796 int i;
8797
8798 /* We must enter the scope containing the type, as that is where
8799 the accessibility of types named in dependent bases are
8800 looked up from. */
8801 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8802
8803 /* Substitute into each of the bases to determine the actual
8804 basetypes. */
8805 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8806 {
8807 tree base;
8808 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8809 tree expanded_bases = NULL_TREE;
8810 int idx, len = 1;
8811
8812 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8813 {
8814 expanded_bases =
8815 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8816 args, tf_error, NULL_TREE);
8817 if (expanded_bases == error_mark_node)
8818 continue;
8819
8820 len = TREE_VEC_LENGTH (expanded_bases);
8821 }
8822
8823 for (idx = 0; idx < len; idx++)
8824 {
8825 if (expanded_bases)
8826 /* Extract the already-expanded base class. */
8827 base = TREE_VEC_ELT (expanded_bases, idx);
8828 else
8829 /* Substitute to figure out the base class. */
8830 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8831 NULL_TREE);
8832
8833 if (base == error_mark_node)
8834 continue;
8835
8836 base_list = tree_cons (access, base, base_list);
8837 if (BINFO_VIRTUAL_P (pbase_binfo))
8838 TREE_TYPE (base_list) = integer_type_node;
8839 }
8840 }
8841
8842 /* The list is now in reverse order; correct that. */
8843 base_list = nreverse (base_list);
8844
8845 if (pushed_scope)
8846 pop_scope (pushed_scope);
8847 }
8848 /* Now call xref_basetypes to set up all the base-class
8849 information. */
8850 xref_basetypes (type, base_list);
8851
8852 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8853 (int) ATTR_FLAG_TYPE_IN_PLACE,
8854 args, tf_error, NULL_TREE);
8855 fixup_attribute_variants (type);
8856
8857 /* Now that our base classes are set up, enter the scope of the
8858 class, so that name lookups into base classes, etc. will work
8859 correctly. This is precisely analogous to what we do in
8860 begin_class_definition when defining an ordinary non-template
8861 class, except we also need to push the enclosing classes. */
8862 push_nested_class (type);
8863
8864 /* Now members are processed in the order of declaration. */
8865 for (member = CLASSTYPE_DECL_LIST (pattern);
8866 member; member = TREE_CHAIN (member))
8867 {
8868 tree t = TREE_VALUE (member);
8869
8870 if (TREE_PURPOSE (member))
8871 {
8872 if (TYPE_P (t))
8873 {
8874 /* Build new CLASSTYPE_NESTED_UTDS. */
8875
8876 tree newtag;
8877 bool class_template_p;
8878
8879 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8880 && TYPE_LANG_SPECIFIC (t)
8881 && CLASSTYPE_IS_TEMPLATE (t));
8882 /* If the member is a class template, then -- even after
8883 substitution -- there may be dependent types in the
8884 template argument list for the class. We increment
8885 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8886 that function will assume that no types are dependent
8887 when outside of a template. */
8888 if (class_template_p)
8889 ++processing_template_decl;
8890 newtag = tsubst (t, args, tf_error, NULL_TREE);
8891 if (class_template_p)
8892 --processing_template_decl;
8893 if (newtag == error_mark_node)
8894 continue;
8895
8896 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8897 {
8898 tree name = TYPE_IDENTIFIER (t);
8899
8900 if (class_template_p)
8901 /* Unfortunately, lookup_template_class sets
8902 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8903 instantiation (i.e., for the type of a member
8904 template class nested within a template class.)
8905 This behavior is required for
8906 maybe_process_partial_specialization to work
8907 correctly, but is not accurate in this case;
8908 the TAG is not an instantiation of anything.
8909 (The corresponding TEMPLATE_DECL is an
8910 instantiation, but the TYPE is not.) */
8911 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8912
8913 /* Now, we call pushtag to put this NEWTAG into the scope of
8914 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8915 pushtag calling push_template_decl. We don't have to do
8916 this for enums because it will already have been done in
8917 tsubst_enum. */
8918 if (name)
8919 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8920 pushtag (name, newtag, /*tag_scope=*/ts_current);
8921 }
8922 }
8923 else if (DECL_DECLARES_FUNCTION_P (t))
8924 {
8925 /* Build new TYPE_METHODS. */
8926 tree r;
8927
8928 if (TREE_CODE (t) == TEMPLATE_DECL)
8929 ++processing_template_decl;
8930 r = tsubst (t, args, tf_error, NULL_TREE);
8931 if (TREE_CODE (t) == TEMPLATE_DECL)
8932 --processing_template_decl;
8933 set_current_access_from_decl (r);
8934 finish_member_declaration (r);
8935 /* Instantiate members marked with attribute used. */
8936 if (r != error_mark_node && DECL_PRESERVE_P (r))
8937 mark_used (r);
8938 if (TREE_CODE (r) == FUNCTION_DECL
8939 && DECL_OMP_DECLARE_REDUCTION_P (r))
8940 cp_check_omp_declare_reduction (r);
8941 }
8942 else
8943 {
8944 /* Build new TYPE_FIELDS. */
8945 if (TREE_CODE (t) == STATIC_ASSERT)
8946 {
8947 tree condition;
8948
8949 ++c_inhibit_evaluation_warnings;
8950 condition =
8951 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8952 tf_warning_or_error, NULL_TREE,
8953 /*integral_constant_expression_p=*/true);
8954 --c_inhibit_evaluation_warnings;
8955
8956 finish_static_assert (condition,
8957 STATIC_ASSERT_MESSAGE (t),
8958 STATIC_ASSERT_SOURCE_LOCATION (t),
8959 /*member_p=*/true);
8960 }
8961 else if (TREE_CODE (t) != CONST_DECL)
8962 {
8963 tree r;
8964 tree vec = NULL_TREE;
8965 int len = 1;
8966
8967 /* The file and line for this declaration, to
8968 assist in error message reporting. Since we
8969 called push_tinst_level above, we don't need to
8970 restore these. */
8971 input_location = DECL_SOURCE_LOCATION (t);
8972
8973 if (TREE_CODE (t) == TEMPLATE_DECL)
8974 ++processing_template_decl;
8975 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8976 if (TREE_CODE (t) == TEMPLATE_DECL)
8977 --processing_template_decl;
8978
8979 if (TREE_CODE (r) == TREE_VEC)
8980 {
8981 /* A capture pack became multiple fields. */
8982 vec = r;
8983 len = TREE_VEC_LENGTH (vec);
8984 }
8985
8986 for (int i = 0; i < len; ++i)
8987 {
8988 if (vec)
8989 r = TREE_VEC_ELT (vec, i);
8990 if (VAR_P (r))
8991 {
8992 /* In [temp.inst]:
8993
8994 [t]he initialization (and any associated
8995 side-effects) of a static data member does
8996 not occur unless the static data member is
8997 itself used in a way that requires the
8998 definition of the static data member to
8999 exist.
9000
9001 Therefore, we do not substitute into the
9002 initialized for the static data member here. */
9003 finish_static_data_member_decl
9004 (r,
9005 /*init=*/NULL_TREE,
9006 /*init_const_expr_p=*/false,
9007 /*asmspec_tree=*/NULL_TREE,
9008 /*flags=*/0);
9009 /* Instantiate members marked with attribute used. */
9010 if (r != error_mark_node && DECL_PRESERVE_P (r))
9011 mark_used (r);
9012 }
9013 else if (TREE_CODE (r) == FIELD_DECL)
9014 {
9015 /* Determine whether R has a valid type and can be
9016 completed later. If R is invalid, then its type
9017 is replaced by error_mark_node. */
9018 tree rtype = TREE_TYPE (r);
9019 if (can_complete_type_without_circularity (rtype))
9020 complete_type (rtype);
9021
9022 if (!COMPLETE_TYPE_P (rtype))
9023 {
9024 cxx_incomplete_type_error (r, rtype);
9025 TREE_TYPE (r) = error_mark_node;
9026 }
9027 }
9028
9029 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9030 such a thing will already have been added to the field
9031 list by tsubst_enum in finish_member_declaration in the
9032 CLASSTYPE_NESTED_UTDS case above. */
9033 if (!(TREE_CODE (r) == TYPE_DECL
9034 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9035 && DECL_ARTIFICIAL (r)))
9036 {
9037 set_current_access_from_decl (r);
9038 finish_member_declaration (r);
9039 }
9040 }
9041 }
9042 }
9043 }
9044 else
9045 {
9046 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9047 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9048 {
9049 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9050
9051 tree friend_type = t;
9052 bool adjust_processing_template_decl = false;
9053
9054 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9055 {
9056 /* template <class T> friend class C; */
9057 friend_type = tsubst_friend_class (friend_type, args);
9058 adjust_processing_template_decl = true;
9059 }
9060 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9061 {
9062 /* template <class T> friend class C::D; */
9063 friend_type = tsubst (friend_type, args,
9064 tf_warning_or_error, NULL_TREE);
9065 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9066 friend_type = TREE_TYPE (friend_type);
9067 adjust_processing_template_decl = true;
9068 }
9069 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9070 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9071 {
9072 /* This could be either
9073
9074 friend class T::C;
9075
9076 when dependent_type_p is false or
9077
9078 template <class U> friend class T::C;
9079
9080 otherwise. */
9081 friend_type = tsubst (friend_type, args,
9082 tf_warning_or_error, NULL_TREE);
9083 /* Bump processing_template_decl for correct
9084 dependent_type_p calculation. */
9085 ++processing_template_decl;
9086 if (dependent_type_p (friend_type))
9087 adjust_processing_template_decl = true;
9088 --processing_template_decl;
9089 }
9090 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9091 && hidden_name_p (TYPE_NAME (friend_type)))
9092 {
9093 /* friend class C;
9094
9095 where C hasn't been declared yet. Let's lookup name
9096 from namespace scope directly, bypassing any name that
9097 come from dependent base class. */
9098 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9099
9100 /* The call to xref_tag_from_type does injection for friend
9101 classes. */
9102 push_nested_namespace (ns);
9103 friend_type =
9104 xref_tag_from_type (friend_type, NULL_TREE,
9105 /*tag_scope=*/ts_current);
9106 pop_nested_namespace (ns);
9107 }
9108 else if (uses_template_parms (friend_type))
9109 /* friend class C<T>; */
9110 friend_type = tsubst (friend_type, args,
9111 tf_warning_or_error, NULL_TREE);
9112 /* Otherwise it's
9113
9114 friend class C;
9115
9116 where C is already declared or
9117
9118 friend class C<int>;
9119
9120 We don't have to do anything in these cases. */
9121
9122 if (adjust_processing_template_decl)
9123 /* Trick make_friend_class into realizing that the friend
9124 we're adding is a template, not an ordinary class. It's
9125 important that we use make_friend_class since it will
9126 perform some error-checking and output cross-reference
9127 information. */
9128 ++processing_template_decl;
9129
9130 if (friend_type != error_mark_node)
9131 make_friend_class (type, friend_type, /*complain=*/false);
9132
9133 if (adjust_processing_template_decl)
9134 --processing_template_decl;
9135 }
9136 else
9137 {
9138 /* Build new DECL_FRIENDLIST. */
9139 tree r;
9140
9141 /* The file and line for this declaration, to
9142 assist in error message reporting. Since we
9143 called push_tinst_level above, we don't need to
9144 restore these. */
9145 input_location = DECL_SOURCE_LOCATION (t);
9146
9147 if (TREE_CODE (t) == TEMPLATE_DECL)
9148 {
9149 ++processing_template_decl;
9150 push_deferring_access_checks (dk_no_check);
9151 }
9152
9153 r = tsubst_friend_function (t, args);
9154 add_friend (type, r, /*complain=*/false);
9155 if (TREE_CODE (t) == TEMPLATE_DECL)
9156 {
9157 pop_deferring_access_checks ();
9158 --processing_template_decl;
9159 }
9160 }
9161 }
9162 }
9163
9164 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9165 {
9166 tree decl = lambda_function (type);
9167 if (decl)
9168 {
9169 if (!DECL_TEMPLATE_INFO (decl)
9170 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9171 instantiate_decl (decl, false, false);
9172
9173 /* We need to instantiate the capture list from the template
9174 after we've instantiated the closure members, but before we
9175 consider adding the conversion op. Also keep any captures
9176 that may have been added during instantiation of the op(). */
9177 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9178 tree tmpl_cap
9179 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9180 args, tf_warning_or_error, NULL_TREE,
9181 false, false);
9182
9183 LAMBDA_EXPR_CAPTURE_LIST (expr)
9184 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9185
9186 maybe_add_lambda_conv_op (type);
9187 }
9188 else
9189 gcc_assert (errorcount);
9190 }
9191
9192 /* Set the file and line number information to whatever is given for
9193 the class itself. This puts error messages involving generated
9194 implicit functions at a predictable point, and the same point
9195 that would be used for non-template classes. */
9196 input_location = DECL_SOURCE_LOCATION (typedecl);
9197
9198 unreverse_member_declarations (type);
9199 finish_struct_1 (type);
9200 TYPE_BEING_DEFINED (type) = 0;
9201
9202 /* We don't instantiate default arguments for member functions. 14.7.1:
9203
9204 The implicit instantiation of a class template specialization causes
9205 the implicit instantiation of the declarations, but not of the
9206 definitions or default arguments, of the class member functions,
9207 member classes, static data members and member templates.... */
9208
9209 /* Some typedefs referenced from within the template code need to be access
9210 checked at template instantiation time, i.e now. These types were
9211 added to the template at parsing time. Let's get those and perform
9212 the access checks then. */
9213 perform_typedefs_access_check (pattern, args);
9214 perform_deferred_access_checks (tf_warning_or_error);
9215 pop_nested_class ();
9216 maximum_field_alignment = saved_maximum_field_alignment;
9217 if (!fn_context)
9218 pop_from_top_level ();
9219 pop_deferring_access_checks ();
9220 pop_tinst_level ();
9221
9222 /* The vtable for a template class can be emitted in any translation
9223 unit in which the class is instantiated. When there is no key
9224 method, however, finish_struct_1 will already have added TYPE to
9225 the keyed_classes list. */
9226 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9227 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9228
9229 return type;
9230 }
9231
9232 /* Wrapper for instantiate_class_template_1. */
9233
9234 tree
9235 instantiate_class_template (tree type)
9236 {
9237 tree ret;
9238 timevar_push (TV_TEMPLATE_INST);
9239 ret = instantiate_class_template_1 (type);
9240 timevar_pop (TV_TEMPLATE_INST);
9241 return ret;
9242 }
9243
9244 static tree
9245 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9246 {
9247 tree r;
9248
9249 if (!t)
9250 r = t;
9251 else if (TYPE_P (t))
9252 r = tsubst (t, args, complain, in_decl);
9253 else
9254 {
9255 if (!(complain & tf_warning))
9256 ++c_inhibit_evaluation_warnings;
9257 r = tsubst_expr (t, args, complain, in_decl,
9258 /*integral_constant_expression_p=*/true);
9259 if (!(complain & tf_warning))
9260 --c_inhibit_evaluation_warnings;
9261 /* Preserve the raw-reference nature of T. */
9262 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
9263 && REFERENCE_REF_P (r))
9264 r = TREE_OPERAND (r, 0);
9265 }
9266 return r;
9267 }
9268
9269 /* Given a function parameter pack TMPL_PARM and some function parameters
9270 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9271 and set *SPEC_P to point at the next point in the list. */
9272
9273 static tree
9274 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9275 {
9276 /* Collect all of the extra "packed" parameters into an
9277 argument pack. */
9278 tree parmvec;
9279 tree parmtypevec;
9280 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9281 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9282 tree spec_parm = *spec_p;
9283 int i, len;
9284
9285 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9286 if (tmpl_parm
9287 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9288 break;
9289
9290 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9291 parmvec = make_tree_vec (len);
9292 parmtypevec = make_tree_vec (len);
9293 spec_parm = *spec_p;
9294 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9295 {
9296 TREE_VEC_ELT (parmvec, i) = spec_parm;
9297 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9298 }
9299
9300 /* Build the argument packs. */
9301 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9302 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9303 TREE_TYPE (argpack) = argtypepack;
9304 *spec_p = spec_parm;
9305
9306 return argpack;
9307 }
9308
9309 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9310 NONTYPE_ARGUMENT_PACK. */
9311
9312 static tree
9313 make_fnparm_pack (tree spec_parm)
9314 {
9315 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9316 }
9317
9318 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9319 pack expansion. */
9320
9321 static bool
9322 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9323 {
9324 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9325 if (i >= TREE_VEC_LENGTH (vec))
9326 return false;
9327 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9328 }
9329
9330
9331 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9332
9333 static tree
9334 make_argument_pack_select (tree arg_pack, unsigned index)
9335 {
9336 tree aps = make_node (ARGUMENT_PACK_SELECT);
9337
9338 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9339 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9340
9341 return aps;
9342 }
9343
9344 /* This is a subroutine of tsubst_pack_expansion.
9345
9346 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9347 mechanism to store the (non complete list of) arguments of the
9348 substitution and return a non substituted pack expansion, in order
9349 to wait for when we have enough arguments to really perform the
9350 substitution. */
9351
9352 static bool
9353 use_pack_expansion_extra_args_p (tree parm_packs,
9354 int arg_pack_len,
9355 bool has_empty_arg)
9356 {
9357 /* If one pack has an expansion and another pack has a normal
9358 argument or if one pack has an empty argument and an another
9359 one hasn't then tsubst_pack_expansion cannot perform the
9360 substitution and need to fall back on the
9361 PACK_EXPANSION_EXTRA mechanism. */
9362 if (parm_packs == NULL_TREE)
9363 return false;
9364 else if (has_empty_arg)
9365 return true;
9366
9367 bool has_expansion_arg = false;
9368 for (int i = 0 ; i < arg_pack_len; ++i)
9369 {
9370 bool has_non_expansion_arg = false;
9371 for (tree parm_pack = parm_packs;
9372 parm_pack;
9373 parm_pack = TREE_CHAIN (parm_pack))
9374 {
9375 tree arg = TREE_VALUE (parm_pack);
9376
9377 if (argument_pack_element_is_expansion_p (arg, i))
9378 has_expansion_arg = true;
9379 else
9380 has_non_expansion_arg = true;
9381 }
9382
9383 if (has_expansion_arg && has_non_expansion_arg)
9384 return true;
9385 }
9386 return false;
9387 }
9388
9389 /* [temp.variadic]/6 says that:
9390
9391 The instantiation of a pack expansion [...]
9392 produces a list E1,E2, ..., En, where N is the number of elements
9393 in the pack expansion parameters.
9394
9395 This subroutine of tsubst_pack_expansion produces one of these Ei.
9396
9397 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9398 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9399 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9400 INDEX is the index 'i' of the element Ei to produce. ARGS,
9401 COMPLAIN, and IN_DECL are the same parameters as for the
9402 tsubst_pack_expansion function.
9403
9404 The function returns the resulting Ei upon successful completion,
9405 or error_mark_node.
9406
9407 Note that this function possibly modifies the ARGS parameter, so
9408 it's the responsibility of the caller to restore it. */
9409
9410 static tree
9411 gen_elem_of_pack_expansion_instantiation (tree pattern,
9412 tree parm_packs,
9413 unsigned index,
9414 tree args /* This parm gets
9415 modified. */,
9416 tsubst_flags_t complain,
9417 tree in_decl)
9418 {
9419 tree t;
9420 bool ith_elem_is_expansion = false;
9421
9422 /* For each parameter pack, change the substitution of the parameter
9423 pack to the ith argument in its argument pack, then expand the
9424 pattern. */
9425 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9426 {
9427 tree parm = TREE_PURPOSE (pack);
9428 tree arg_pack = TREE_VALUE (pack);
9429 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9430
9431 ith_elem_is_expansion |=
9432 argument_pack_element_is_expansion_p (arg_pack, index);
9433
9434 /* Select the Ith argument from the pack. */
9435 if (TREE_CODE (parm) == PARM_DECL
9436 || TREE_CODE (parm) == FIELD_DECL)
9437 {
9438 if (index == 0)
9439 {
9440 aps = make_argument_pack_select (arg_pack, index);
9441 mark_used (parm);
9442 register_local_specialization (aps, parm);
9443 }
9444 else
9445 aps = retrieve_local_specialization (parm);
9446 }
9447 else
9448 {
9449 int idx, level;
9450 template_parm_level_and_index (parm, &level, &idx);
9451
9452 if (index == 0)
9453 {
9454 aps = make_argument_pack_select (arg_pack, index);
9455 /* Update the corresponding argument. */
9456 TMPL_ARG (args, level, idx) = aps;
9457 }
9458 else
9459 /* Re-use the ARGUMENT_PACK_SELECT. */
9460 aps = TMPL_ARG (args, level, idx);
9461 }
9462 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9463 }
9464
9465 /* Substitute into the PATTERN with the (possibly altered)
9466 arguments. */
9467 if (!TYPE_P (pattern))
9468 t = tsubst_expr (pattern, args, complain, in_decl,
9469 /*integral_constant_expression_p=*/false);
9470 else
9471 t = tsubst (pattern, args, complain, in_decl);
9472
9473 /* If the Ith argument pack element is a pack expansion, then
9474 the Ith element resulting from the substituting is going to
9475 be a pack expansion as well. */
9476 if (ith_elem_is_expansion)
9477 t = make_pack_expansion (t);
9478
9479 return t;
9480 }
9481
9482 /* Substitute ARGS into T, which is an pack expansion
9483 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9484 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9485 (if only a partial substitution could be performed) or
9486 ERROR_MARK_NODE if there was an error. */
9487 tree
9488 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9489 tree in_decl)
9490 {
9491 tree pattern;
9492 tree pack, packs = NULL_TREE;
9493 bool unsubstituted_packs = false;
9494 int i, len = -1;
9495 tree result;
9496 struct pointer_map_t *saved_local_specializations = NULL;
9497 bool need_local_specializations = false;
9498 int levels;
9499
9500 gcc_assert (PACK_EXPANSION_P (t));
9501 pattern = PACK_EXPANSION_PATTERN (t);
9502
9503 /* Add in any args remembered from an earlier partial instantiation. */
9504 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9505
9506 levels = TMPL_ARGS_DEPTH (args);
9507
9508 /* Determine the argument packs that will instantiate the parameter
9509 packs used in the expansion expression. While we're at it,
9510 compute the number of arguments to be expanded and make sure it
9511 is consistent. */
9512 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9513 pack = TREE_CHAIN (pack))
9514 {
9515 tree parm_pack = TREE_VALUE (pack);
9516 tree arg_pack = NULL_TREE;
9517 tree orig_arg = NULL_TREE;
9518 int level = 0;
9519
9520 if (TREE_CODE (parm_pack) == BASES)
9521 {
9522 if (BASES_DIRECT (parm_pack))
9523 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9524 args, complain, in_decl, false));
9525 else
9526 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9527 args, complain, in_decl, false));
9528 }
9529 if (TREE_CODE (parm_pack) == PARM_DECL)
9530 {
9531 if (PACK_EXPANSION_LOCAL_P (t))
9532 arg_pack = retrieve_local_specialization (parm_pack);
9533 else
9534 {
9535 /* We can't rely on local_specializations for a parameter
9536 name used later in a function declaration (such as in a
9537 late-specified return type). Even if it exists, it might
9538 have the wrong value for a recursive call. Just make a
9539 dummy decl, since it's only used for its type. */
9540 arg_pack = tsubst_decl (parm_pack, args, complain);
9541 if (arg_pack && DECL_PACK_P (arg_pack))
9542 /* Partial instantiation of the parm_pack, we can't build
9543 up an argument pack yet. */
9544 arg_pack = NULL_TREE;
9545 else
9546 arg_pack = make_fnparm_pack (arg_pack);
9547 need_local_specializations = true;
9548 }
9549 }
9550 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9551 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9552 else
9553 {
9554 int idx;
9555 template_parm_level_and_index (parm_pack, &level, &idx);
9556
9557 if (level <= levels)
9558 arg_pack = TMPL_ARG (args, level, idx);
9559 }
9560
9561 orig_arg = arg_pack;
9562 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9563 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9564
9565 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9566 /* This can only happen if we forget to expand an argument
9567 pack somewhere else. Just return an error, silently. */
9568 {
9569 result = make_tree_vec (1);
9570 TREE_VEC_ELT (result, 0) = error_mark_node;
9571 return result;
9572 }
9573
9574 if (arg_pack)
9575 {
9576 int my_len =
9577 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9578
9579 /* Don't bother trying to do a partial substitution with
9580 incomplete packs; we'll try again after deduction. */
9581 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9582 return t;
9583
9584 if (len < 0)
9585 len = my_len;
9586 else if (len != my_len)
9587 {
9588 if (!(complain & tf_error))
9589 /* Fail quietly. */;
9590 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9591 error ("mismatched argument pack lengths while expanding "
9592 "%<%T%>",
9593 pattern);
9594 else
9595 error ("mismatched argument pack lengths while expanding "
9596 "%<%E%>",
9597 pattern);
9598 return error_mark_node;
9599 }
9600
9601 /* Keep track of the parameter packs and their corresponding
9602 argument packs. */
9603 packs = tree_cons (parm_pack, arg_pack, packs);
9604 TREE_TYPE (packs) = orig_arg;
9605 }
9606 else
9607 {
9608 /* We can't substitute for this parameter pack. We use a flag as
9609 well as the missing_level counter because function parameter
9610 packs don't have a level. */
9611 unsubstituted_packs = true;
9612 }
9613 }
9614
9615 /* We cannot expand this expansion expression, because we don't have
9616 all of the argument packs we need. */
9617 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9618 {
9619 /* We got some full packs, but we can't substitute them in until we
9620 have values for all the packs. So remember these until then. */
9621
9622 t = make_pack_expansion (pattern);
9623 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9624 return t;
9625 }
9626 else if (unsubstituted_packs)
9627 {
9628 /* There were no real arguments, we're just replacing a parameter
9629 pack with another version of itself. Substitute into the
9630 pattern and return a PACK_EXPANSION_*. The caller will need to
9631 deal with that. */
9632 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9633 t = tsubst_expr (pattern, args, complain, in_decl,
9634 /*integral_constant_expression_p=*/false);
9635 else
9636 t = tsubst (pattern, args, complain, in_decl);
9637 t = make_pack_expansion (t);
9638 return t;
9639 }
9640
9641 gcc_assert (len >= 0);
9642
9643 if (need_local_specializations)
9644 {
9645 /* We're in a late-specified return type, so create our own local
9646 specializations map; the current map is either NULL or (in the
9647 case of recursive unification) might have bindings that we don't
9648 want to use or alter. */
9649 saved_local_specializations = local_specializations;
9650 local_specializations = pointer_map_create ();
9651 }
9652
9653 /* For each argument in each argument pack, substitute into the
9654 pattern. */
9655 result = make_tree_vec (len);
9656 for (i = 0; i < len; ++i)
9657 {
9658 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9659 i,
9660 args, complain,
9661 in_decl);
9662 TREE_VEC_ELT (result, i) = t;
9663 if (t == error_mark_node)
9664 {
9665 result = error_mark_node;
9666 break;
9667 }
9668 }
9669
9670 /* Update ARGS to restore the substitution from parameter packs to
9671 their argument packs. */
9672 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9673 {
9674 tree parm = TREE_PURPOSE (pack);
9675
9676 if (TREE_CODE (parm) == PARM_DECL
9677 || TREE_CODE (parm) == FIELD_DECL)
9678 register_local_specialization (TREE_TYPE (pack), parm);
9679 else
9680 {
9681 int idx, level;
9682
9683 if (TREE_VALUE (pack) == NULL_TREE)
9684 continue;
9685
9686 template_parm_level_and_index (parm, &level, &idx);
9687
9688 /* Update the corresponding argument. */
9689 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9690 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9691 TREE_TYPE (pack);
9692 else
9693 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9694 }
9695 }
9696
9697 if (need_local_specializations)
9698 {
9699 pointer_map_destroy (local_specializations);
9700 local_specializations = saved_local_specializations;
9701 }
9702
9703 return result;
9704 }
9705
9706 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9707 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9708 parameter packs; all parms generated from a function parameter pack will
9709 have the same DECL_PARM_INDEX. */
9710
9711 tree
9712 get_pattern_parm (tree parm, tree tmpl)
9713 {
9714 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9715 tree patparm;
9716
9717 if (DECL_ARTIFICIAL (parm))
9718 {
9719 for (patparm = DECL_ARGUMENTS (pattern);
9720 patparm; patparm = DECL_CHAIN (patparm))
9721 if (DECL_ARTIFICIAL (patparm)
9722 && DECL_NAME (parm) == DECL_NAME (patparm))
9723 break;
9724 }
9725 else
9726 {
9727 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9728 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9729 gcc_assert (DECL_PARM_INDEX (patparm)
9730 == DECL_PARM_INDEX (parm));
9731 }
9732
9733 return patparm;
9734 }
9735
9736 /* Substitute ARGS into the vector or list of template arguments T. */
9737
9738 static tree
9739 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9740 {
9741 tree orig_t = t;
9742 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9743 tree *elts;
9744
9745 if (t == error_mark_node)
9746 return error_mark_node;
9747
9748 len = TREE_VEC_LENGTH (t);
9749 elts = XALLOCAVEC (tree, len);
9750
9751 for (i = 0; i < len; i++)
9752 {
9753 tree orig_arg = TREE_VEC_ELT (t, i);
9754 tree new_arg;
9755
9756 if (TREE_CODE (orig_arg) == TREE_VEC)
9757 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9758 else if (PACK_EXPANSION_P (orig_arg))
9759 {
9760 /* Substitute into an expansion expression. */
9761 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9762
9763 if (TREE_CODE (new_arg) == TREE_VEC)
9764 /* Add to the expanded length adjustment the number of
9765 expanded arguments. We subtract one from this
9766 measurement, because the argument pack expression
9767 itself is already counted as 1 in
9768 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9769 the argument pack is empty. */
9770 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9771 }
9772 else if (ARGUMENT_PACK_P (orig_arg))
9773 {
9774 /* Substitute into each of the arguments. */
9775 new_arg = TYPE_P (orig_arg)
9776 ? cxx_make_type (TREE_CODE (orig_arg))
9777 : make_node (TREE_CODE (orig_arg));
9778
9779 SET_ARGUMENT_PACK_ARGS (
9780 new_arg,
9781 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9782 args, complain, in_decl));
9783
9784 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9785 new_arg = error_mark_node;
9786
9787 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9788 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9789 complain, in_decl);
9790 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9791
9792 if (TREE_TYPE (new_arg) == error_mark_node)
9793 new_arg = error_mark_node;
9794 }
9795 }
9796 else
9797 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9798
9799 if (new_arg == error_mark_node)
9800 return error_mark_node;
9801
9802 elts[i] = new_arg;
9803 if (new_arg != orig_arg)
9804 need_new = 1;
9805 }
9806
9807 if (!need_new)
9808 return t;
9809
9810 /* Make space for the expanded arguments coming from template
9811 argument packs. */
9812 t = make_tree_vec (len + expanded_len_adjust);
9813 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9814 arguments for a member template.
9815 In that case each TREE_VEC in ORIG_T represents a level of template
9816 arguments, and ORIG_T won't carry any non defaulted argument count.
9817 It will rather be the nested TREE_VECs that will carry one.
9818 In other words, ORIG_T carries a non defaulted argument count only
9819 if it doesn't contain any nested TREE_VEC. */
9820 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9821 {
9822 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9823 count += expanded_len_adjust;
9824 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9825 }
9826 for (i = 0, out = 0; i < len; i++)
9827 {
9828 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9829 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9830 && TREE_CODE (elts[i]) == TREE_VEC)
9831 {
9832 int idx;
9833
9834 /* Now expand the template argument pack "in place". */
9835 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9836 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9837 }
9838 else
9839 {
9840 TREE_VEC_ELT (t, out) = elts[i];
9841 out++;
9842 }
9843 }
9844
9845 return t;
9846 }
9847
9848 /* Return the result of substituting ARGS into the template parameters
9849 given by PARMS. If there are m levels of ARGS and m + n levels of
9850 PARMS, then the result will contain n levels of PARMS. For
9851 example, if PARMS is `template <class T> template <class U>
9852 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9853 result will be `template <int*, double, class V>'. */
9854
9855 static tree
9856 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9857 {
9858 tree r = NULL_TREE;
9859 tree* new_parms;
9860
9861 /* When substituting into a template, we must set
9862 PROCESSING_TEMPLATE_DECL as the template parameters may be
9863 dependent if they are based on one-another, and the dependency
9864 predicates are short-circuit outside of templates. */
9865 ++processing_template_decl;
9866
9867 for (new_parms = &r;
9868 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9869 new_parms = &(TREE_CHAIN (*new_parms)),
9870 parms = TREE_CHAIN (parms))
9871 {
9872 tree new_vec =
9873 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9874 int i;
9875
9876 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9877 {
9878 tree tuple;
9879
9880 if (parms == error_mark_node)
9881 continue;
9882
9883 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9884
9885 if (tuple == error_mark_node)
9886 continue;
9887
9888 TREE_VEC_ELT (new_vec, i) =
9889 tsubst_template_parm (tuple, args, complain);
9890 }
9891
9892 *new_parms =
9893 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9894 - TMPL_ARGS_DEPTH (args)),
9895 new_vec, NULL_TREE);
9896 }
9897
9898 --processing_template_decl;
9899
9900 return r;
9901 }
9902
9903 /* Return the result of substituting ARGS into one template parameter
9904 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9905 parameter and which TREE_PURPOSE is the default argument of the
9906 template parameter. */
9907
9908 static tree
9909 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9910 {
9911 tree default_value, parm_decl;
9912
9913 if (args == NULL_TREE
9914 || t == NULL_TREE
9915 || t == error_mark_node)
9916 return t;
9917
9918 gcc_assert (TREE_CODE (t) == TREE_LIST);
9919
9920 default_value = TREE_PURPOSE (t);
9921 parm_decl = TREE_VALUE (t);
9922
9923 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9924 if (TREE_CODE (parm_decl) == PARM_DECL
9925 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9926 parm_decl = error_mark_node;
9927 default_value = tsubst_template_arg (default_value, args,
9928 complain, NULL_TREE);
9929
9930 return build_tree_list (default_value, parm_decl);
9931 }
9932
9933 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9934 type T. If T is not an aggregate or enumeration type, it is
9935 handled as if by tsubst. IN_DECL is as for tsubst. If
9936 ENTERING_SCOPE is nonzero, T is the context for a template which
9937 we are presently tsubst'ing. Return the substituted value. */
9938
9939 static tree
9940 tsubst_aggr_type (tree t,
9941 tree args,
9942 tsubst_flags_t complain,
9943 tree in_decl,
9944 int entering_scope)
9945 {
9946 if (t == NULL_TREE)
9947 return NULL_TREE;
9948
9949 switch (TREE_CODE (t))
9950 {
9951 case RECORD_TYPE:
9952 if (TYPE_PTRMEMFUNC_P (t))
9953 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9954
9955 /* Else fall through. */
9956 case ENUMERAL_TYPE:
9957 case UNION_TYPE:
9958 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9959 {
9960 tree argvec;
9961 tree context;
9962 tree r;
9963 int saved_unevaluated_operand;
9964 int saved_inhibit_evaluation_warnings;
9965
9966 /* In "sizeof(X<I>)" we need to evaluate "I". */
9967 saved_unevaluated_operand = cp_unevaluated_operand;
9968 cp_unevaluated_operand = 0;
9969 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9970 c_inhibit_evaluation_warnings = 0;
9971
9972 /* First, determine the context for the type we are looking
9973 up. */
9974 context = TYPE_CONTEXT (t);
9975 if (context && TYPE_P (context))
9976 {
9977 context = tsubst_aggr_type (context, args, complain,
9978 in_decl, /*entering_scope=*/1);
9979 /* If context is a nested class inside a class template,
9980 it may still need to be instantiated (c++/33959). */
9981 context = complete_type (context);
9982 }
9983
9984 /* Then, figure out what arguments are appropriate for the
9985 type we are trying to find. For example, given:
9986
9987 template <class T> struct S;
9988 template <class T, class U> void f(T, U) { S<U> su; }
9989
9990 and supposing that we are instantiating f<int, double>,
9991 then our ARGS will be {int, double}, but, when looking up
9992 S we only want {double}. */
9993 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9994 complain, in_decl);
9995 if (argvec == error_mark_node)
9996 r = error_mark_node;
9997 else
9998 {
9999 r = lookup_template_class (t, argvec, in_decl, context,
10000 entering_scope, complain);
10001 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10002 }
10003
10004 cp_unevaluated_operand = saved_unevaluated_operand;
10005 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10006
10007 return r;
10008 }
10009 else
10010 /* This is not a template type, so there's nothing to do. */
10011 return t;
10012
10013 default:
10014 return tsubst (t, args, complain, in_decl);
10015 }
10016 }
10017
10018 /* Substitute into the default argument ARG (a default argument for
10019 FN), which has the indicated TYPE. */
10020
10021 tree
10022 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10023 {
10024 tree saved_class_ptr = NULL_TREE;
10025 tree saved_class_ref = NULL_TREE;
10026 int errs = errorcount + sorrycount;
10027
10028 /* This can happen in invalid code. */
10029 if (TREE_CODE (arg) == DEFAULT_ARG)
10030 return arg;
10031
10032 /* This default argument came from a template. Instantiate the
10033 default argument here, not in tsubst. In the case of
10034 something like:
10035
10036 template <class T>
10037 struct S {
10038 static T t();
10039 void f(T = t());
10040 };
10041
10042 we must be careful to do name lookup in the scope of S<T>,
10043 rather than in the current class. */
10044 push_access_scope (fn);
10045 /* The "this" pointer is not valid in a default argument. */
10046 if (cfun)
10047 {
10048 saved_class_ptr = current_class_ptr;
10049 cp_function_chain->x_current_class_ptr = NULL_TREE;
10050 saved_class_ref = current_class_ref;
10051 cp_function_chain->x_current_class_ref = NULL_TREE;
10052 }
10053
10054 push_deferring_access_checks(dk_no_deferred);
10055 /* The default argument expression may cause implicitly defined
10056 member functions to be synthesized, which will result in garbage
10057 collection. We must treat this situation as if we were within
10058 the body of function so as to avoid collecting live data on the
10059 stack. */
10060 ++function_depth;
10061 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10062 complain, NULL_TREE,
10063 /*integral_constant_expression_p=*/false);
10064 --function_depth;
10065 pop_deferring_access_checks();
10066
10067 /* Restore the "this" pointer. */
10068 if (cfun)
10069 {
10070 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10071 cp_function_chain->x_current_class_ref = saved_class_ref;
10072 }
10073
10074 if (errorcount+sorrycount > errs
10075 && (complain & tf_warning_or_error))
10076 inform (input_location,
10077 " when instantiating default argument for call to %D", fn);
10078
10079 /* Make sure the default argument is reasonable. */
10080 arg = check_default_argument (type, arg, complain);
10081
10082 pop_access_scope (fn);
10083
10084 return arg;
10085 }
10086
10087 /* Substitute into all the default arguments for FN. */
10088
10089 static void
10090 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10091 {
10092 tree arg;
10093 tree tmpl_args;
10094
10095 tmpl_args = DECL_TI_ARGS (fn);
10096
10097 /* If this function is not yet instantiated, we certainly don't need
10098 its default arguments. */
10099 if (uses_template_parms (tmpl_args))
10100 return;
10101 /* Don't do this again for clones. */
10102 if (DECL_CLONED_FUNCTION_P (fn))
10103 return;
10104
10105 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10106 arg;
10107 arg = TREE_CHAIN (arg))
10108 if (TREE_PURPOSE (arg))
10109 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10110 TREE_VALUE (arg),
10111 TREE_PURPOSE (arg),
10112 complain);
10113 }
10114
10115 /* Substitute the ARGS into the T, which is a _DECL. Return the
10116 result of the substitution. Issue error and warning messages under
10117 control of COMPLAIN. */
10118
10119 static tree
10120 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10121 {
10122 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10123 location_t saved_loc;
10124 tree r = NULL_TREE;
10125 tree in_decl = t;
10126 hashval_t hash = 0;
10127
10128 /* Set the filename and linenumber to improve error-reporting. */
10129 saved_loc = input_location;
10130 input_location = DECL_SOURCE_LOCATION (t);
10131
10132 switch (TREE_CODE (t))
10133 {
10134 case TEMPLATE_DECL:
10135 {
10136 /* We can get here when processing a member function template,
10137 member class template, or template template parameter. */
10138 tree decl = DECL_TEMPLATE_RESULT (t);
10139 tree spec;
10140 tree tmpl_args;
10141 tree full_args;
10142
10143 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10144 {
10145 /* Template template parameter is treated here. */
10146 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10147 if (new_type == error_mark_node)
10148 RETURN (error_mark_node);
10149 /* If we get a real template back, return it. This can happen in
10150 the context of most_specialized_class. */
10151 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10152 return new_type;
10153
10154 r = copy_decl (t);
10155 DECL_CHAIN (r) = NULL_TREE;
10156 TREE_TYPE (r) = new_type;
10157 DECL_TEMPLATE_RESULT (r)
10158 = build_decl (DECL_SOURCE_LOCATION (decl),
10159 TYPE_DECL, DECL_NAME (decl), new_type);
10160 DECL_TEMPLATE_PARMS (r)
10161 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10162 complain);
10163 TYPE_NAME (new_type) = r;
10164 break;
10165 }
10166
10167 /* We might already have an instance of this template.
10168 The ARGS are for the surrounding class type, so the
10169 full args contain the tsubst'd args for the context,
10170 plus the innermost args from the template decl. */
10171 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10172 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10173 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10174 /* Because this is a template, the arguments will still be
10175 dependent, even after substitution. If
10176 PROCESSING_TEMPLATE_DECL is not set, the dependency
10177 predicates will short-circuit. */
10178 ++processing_template_decl;
10179 full_args = tsubst_template_args (tmpl_args, args,
10180 complain, in_decl);
10181 --processing_template_decl;
10182 if (full_args == error_mark_node)
10183 RETURN (error_mark_node);
10184
10185 /* If this is a default template template argument,
10186 tsubst might not have changed anything. */
10187 if (full_args == tmpl_args)
10188 RETURN (t);
10189
10190 hash = hash_tmpl_and_args (t, full_args);
10191 spec = retrieve_specialization (t, full_args, hash);
10192 if (spec != NULL_TREE)
10193 {
10194 r = spec;
10195 break;
10196 }
10197
10198 /* Make a new template decl. It will be similar to the
10199 original, but will record the current template arguments.
10200 We also create a new function declaration, which is just
10201 like the old one, but points to this new template, rather
10202 than the old one. */
10203 r = copy_decl (t);
10204 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10205 DECL_CHAIN (r) = NULL_TREE;
10206
10207 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10208
10209 if (TREE_CODE (decl) == TYPE_DECL
10210 && !TYPE_DECL_ALIAS_P (decl))
10211 {
10212 tree new_type;
10213 ++processing_template_decl;
10214 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10215 --processing_template_decl;
10216 if (new_type == error_mark_node)
10217 RETURN (error_mark_node);
10218
10219 TREE_TYPE (r) = new_type;
10220 /* For a partial specialization, we need to keep pointing to
10221 the primary template. */
10222 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10223 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10224 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10225 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10226 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10227 }
10228 else
10229 {
10230 tree new_decl;
10231 ++processing_template_decl;
10232 new_decl = tsubst (decl, args, complain, in_decl);
10233 --processing_template_decl;
10234 if (new_decl == error_mark_node)
10235 RETURN (error_mark_node);
10236
10237 DECL_TEMPLATE_RESULT (r) = new_decl;
10238 DECL_TI_TEMPLATE (new_decl) = r;
10239 TREE_TYPE (r) = TREE_TYPE (new_decl);
10240 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10241 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10242 }
10243
10244 SET_DECL_IMPLICIT_INSTANTIATION (r);
10245 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10246 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10247
10248 /* The template parameters for this new template are all the
10249 template parameters for the old template, except the
10250 outermost level of parameters. */
10251 DECL_TEMPLATE_PARMS (r)
10252 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10253 complain);
10254
10255 if (PRIMARY_TEMPLATE_P (t))
10256 DECL_PRIMARY_TEMPLATE (r) = r;
10257
10258 if (TREE_CODE (decl) != TYPE_DECL)
10259 /* Record this non-type partial instantiation. */
10260 register_specialization (r, t,
10261 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10262 false, hash);
10263 }
10264 break;
10265
10266 case FUNCTION_DECL:
10267 {
10268 tree ctx;
10269 tree argvec = NULL_TREE;
10270 tree *friends;
10271 tree gen_tmpl;
10272 tree type;
10273 int member;
10274 int args_depth;
10275 int parms_depth;
10276
10277 /* Nobody should be tsubst'ing into non-template functions. */
10278 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10279
10280 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10281 {
10282 tree spec;
10283 bool dependent_p;
10284
10285 /* If T is not dependent, just return it. We have to
10286 increment PROCESSING_TEMPLATE_DECL because
10287 value_dependent_expression_p assumes that nothing is
10288 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10289 ++processing_template_decl;
10290 dependent_p = value_dependent_expression_p (t);
10291 --processing_template_decl;
10292 if (!dependent_p)
10293 RETURN (t);
10294
10295 /* Calculate the most general template of which R is a
10296 specialization, and the complete set of arguments used to
10297 specialize R. */
10298 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10299 argvec = tsubst_template_args (DECL_TI_ARGS
10300 (DECL_TEMPLATE_RESULT
10301 (DECL_TI_TEMPLATE (t))),
10302 args, complain, in_decl);
10303 if (argvec == error_mark_node)
10304 RETURN (error_mark_node);
10305
10306 /* Check to see if we already have this specialization. */
10307 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10308 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10309
10310 if (spec)
10311 {
10312 r = spec;
10313 break;
10314 }
10315
10316 /* We can see more levels of arguments than parameters if
10317 there was a specialization of a member template, like
10318 this:
10319
10320 template <class T> struct S { template <class U> void f(); }
10321 template <> template <class U> void S<int>::f(U);
10322
10323 Here, we'll be substituting into the specialization,
10324 because that's where we can find the code we actually
10325 want to generate, but we'll have enough arguments for
10326 the most general template.
10327
10328 We also deal with the peculiar case:
10329
10330 template <class T> struct S {
10331 template <class U> friend void f();
10332 };
10333 template <class U> void f() {}
10334 template S<int>;
10335 template void f<double>();
10336
10337 Here, the ARGS for the instantiation of will be {int,
10338 double}. But, we only need as many ARGS as there are
10339 levels of template parameters in CODE_PATTERN. We are
10340 careful not to get fooled into reducing the ARGS in
10341 situations like:
10342
10343 template <class T> struct S { template <class U> void f(U); }
10344 template <class T> template <> void S<T>::f(int) {}
10345
10346 which we can spot because the pattern will be a
10347 specialization in this case. */
10348 args_depth = TMPL_ARGS_DEPTH (args);
10349 parms_depth =
10350 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10351 if (args_depth > parms_depth
10352 && !DECL_TEMPLATE_SPECIALIZATION (t))
10353 args = get_innermost_template_args (args, parms_depth);
10354 }
10355 else
10356 {
10357 /* This special case arises when we have something like this:
10358
10359 template <class T> struct S {
10360 friend void f<int>(int, double);
10361 };
10362
10363 Here, the DECL_TI_TEMPLATE for the friend declaration
10364 will be an IDENTIFIER_NODE. We are being called from
10365 tsubst_friend_function, and we want only to create a
10366 new decl (R) with appropriate types so that we can call
10367 determine_specialization. */
10368 gen_tmpl = NULL_TREE;
10369 }
10370
10371 if (DECL_CLASS_SCOPE_P (t))
10372 {
10373 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10374 member = 2;
10375 else
10376 member = 1;
10377 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10378 complain, t, /*entering_scope=*/1);
10379 }
10380 else
10381 {
10382 member = 0;
10383 ctx = DECL_CONTEXT (t);
10384 }
10385 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10386 if (type == error_mark_node)
10387 RETURN (error_mark_node);
10388
10389 /* If we hit excessive deduction depth, the type is bogus even if
10390 it isn't error_mark_node, so don't build a decl. */
10391 if (excessive_deduction_depth)
10392 RETURN (error_mark_node);
10393
10394 /* We do NOT check for matching decls pushed separately at this
10395 point, as they may not represent instantiations of this
10396 template, and in any case are considered separate under the
10397 discrete model. */
10398 r = copy_decl (t);
10399 DECL_USE_TEMPLATE (r) = 0;
10400 TREE_TYPE (r) = type;
10401 /* Clear out the mangled name and RTL for the instantiation. */
10402 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10403 SET_DECL_RTL (r, NULL);
10404 /* Leave DECL_INITIAL set on deleted instantiations. */
10405 if (!DECL_DELETED_FN (r))
10406 DECL_INITIAL (r) = NULL_TREE;
10407 DECL_CONTEXT (r) = ctx;
10408
10409 /* OpenMP UDRs have the only argument a reference to the declared
10410 type. We want to diagnose if the declared type is a reference,
10411 which is invalid, but as references to references are usually
10412 quietly merged, diagnose it here. */
10413 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10414 {
10415 tree argtype
10416 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10417 argtype = tsubst (argtype, args, complain, in_decl);
10418 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10419 error_at (DECL_SOURCE_LOCATION (t),
10420 "reference type %qT in "
10421 "%<#pragma omp declare reduction%>", argtype);
10422 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10423 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10424 argtype);
10425 }
10426
10427 if (member && DECL_CONV_FN_P (r))
10428 /* Type-conversion operator. Reconstruct the name, in
10429 case it's the name of one of the template's parameters. */
10430 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10431
10432 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10433 complain, t);
10434 DECL_RESULT (r) = NULL_TREE;
10435
10436 TREE_STATIC (r) = 0;
10437 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10438 DECL_EXTERNAL (r) = 1;
10439 /* If this is an instantiation of a function with internal
10440 linkage, we already know what object file linkage will be
10441 assigned to the instantiation. */
10442 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10443 DECL_DEFER_OUTPUT (r) = 0;
10444 DECL_CHAIN (r) = NULL_TREE;
10445 DECL_PENDING_INLINE_INFO (r) = 0;
10446 DECL_PENDING_INLINE_P (r) = 0;
10447 DECL_SAVED_TREE (r) = NULL_TREE;
10448 DECL_STRUCT_FUNCTION (r) = NULL;
10449 TREE_USED (r) = 0;
10450 /* We'll re-clone as appropriate in instantiate_template. */
10451 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10452
10453 /* If we aren't complaining now, return on error before we register
10454 the specialization so that we'll complain eventually. */
10455 if ((complain & tf_error) == 0
10456 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10457 && !grok_op_properties (r, /*complain=*/false))
10458 RETURN (error_mark_node);
10459
10460 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10461 this in the special friend case mentioned above where
10462 GEN_TMPL is NULL. */
10463 if (gen_tmpl)
10464 {
10465 DECL_TEMPLATE_INFO (r)
10466 = build_template_info (gen_tmpl, argvec);
10467 SET_DECL_IMPLICIT_INSTANTIATION (r);
10468
10469 tree new_r
10470 = register_specialization (r, gen_tmpl, argvec, false, hash);
10471 if (new_r != r)
10472 /* We instantiated this while substituting into
10473 the type earlier (template/friend54.C). */
10474 RETURN (new_r);
10475
10476 /* We're not supposed to instantiate default arguments
10477 until they are called, for a template. But, for a
10478 declaration like:
10479
10480 template <class T> void f ()
10481 { extern void g(int i = T()); }
10482
10483 we should do the substitution when the template is
10484 instantiated. We handle the member function case in
10485 instantiate_class_template since the default arguments
10486 might refer to other members of the class. */
10487 if (!member
10488 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10489 && !uses_template_parms (argvec))
10490 tsubst_default_arguments (r, complain);
10491 }
10492 else
10493 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10494
10495 /* Copy the list of befriending classes. */
10496 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10497 *friends;
10498 friends = &TREE_CHAIN (*friends))
10499 {
10500 *friends = copy_node (*friends);
10501 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10502 args, complain,
10503 in_decl);
10504 }
10505
10506 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10507 {
10508 maybe_retrofit_in_chrg (r);
10509 if (DECL_CONSTRUCTOR_P (r))
10510 grok_ctor_properties (ctx, r);
10511 if (DECL_INHERITED_CTOR_BASE (r))
10512 deduce_inheriting_ctor (r);
10513 /* If this is an instantiation of a member template, clone it.
10514 If it isn't, that'll be handled by
10515 clone_constructors_and_destructors. */
10516 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10517 clone_function_decl (r, /*update_method_vec_p=*/0);
10518 }
10519 else if ((complain & tf_error) != 0
10520 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10521 && !grok_op_properties (r, /*complain=*/true))
10522 RETURN (error_mark_node);
10523
10524 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10525 SET_DECL_FRIEND_CONTEXT (r,
10526 tsubst (DECL_FRIEND_CONTEXT (t),
10527 args, complain, in_decl));
10528
10529 /* Possibly limit visibility based on template args. */
10530 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10531 if (DECL_VISIBILITY_SPECIFIED (t))
10532 {
10533 DECL_VISIBILITY_SPECIFIED (r) = 0;
10534 DECL_ATTRIBUTES (r)
10535 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10536 }
10537 determine_visibility (r);
10538 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10539 && !processing_template_decl)
10540 defaulted_late_check (r);
10541
10542 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10543 args, complain, in_decl);
10544 }
10545 break;
10546
10547 case PARM_DECL:
10548 {
10549 tree type = NULL_TREE;
10550 int i, len = 1;
10551 tree expanded_types = NULL_TREE;
10552 tree prev_r = NULL_TREE;
10553 tree first_r = NULL_TREE;
10554
10555 if (DECL_PACK_P (t))
10556 {
10557 /* If there is a local specialization that isn't a
10558 parameter pack, it means that we're doing a "simple"
10559 substitution from inside tsubst_pack_expansion. Just
10560 return the local specialization (which will be a single
10561 parm). */
10562 tree spec = retrieve_local_specialization (t);
10563 if (spec
10564 && TREE_CODE (spec) == PARM_DECL
10565 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10566 RETURN (spec);
10567
10568 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10569 the parameters in this function parameter pack. */
10570 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10571 complain, in_decl);
10572 if (TREE_CODE (expanded_types) == TREE_VEC)
10573 {
10574 len = TREE_VEC_LENGTH (expanded_types);
10575
10576 /* Zero-length parameter packs are boring. Just substitute
10577 into the chain. */
10578 if (len == 0)
10579 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10580 TREE_CHAIN (t)));
10581 }
10582 else
10583 {
10584 /* All we did was update the type. Make a note of that. */
10585 type = expanded_types;
10586 expanded_types = NULL_TREE;
10587 }
10588 }
10589
10590 /* Loop through all of the parameters we'll build. When T is
10591 a function parameter pack, LEN is the number of expanded
10592 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10593 r = NULL_TREE;
10594 for (i = 0; i < len; ++i)
10595 {
10596 prev_r = r;
10597 r = copy_node (t);
10598 if (DECL_TEMPLATE_PARM_P (t))
10599 SET_DECL_TEMPLATE_PARM_P (r);
10600
10601 if (expanded_types)
10602 /* We're on the Ith parameter of the function parameter
10603 pack. */
10604 {
10605 /* Get the Ith type. */
10606 type = TREE_VEC_ELT (expanded_types, i);
10607
10608 /* Rename the parameter to include the index. */
10609 DECL_NAME (r)
10610 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10611 }
10612 else if (!type)
10613 /* We're dealing with a normal parameter. */
10614 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10615
10616 type = type_decays_to (type);
10617 TREE_TYPE (r) = type;
10618 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10619
10620 if (DECL_INITIAL (r))
10621 {
10622 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10623 DECL_INITIAL (r) = TREE_TYPE (r);
10624 else
10625 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10626 complain, in_decl);
10627 }
10628
10629 DECL_CONTEXT (r) = NULL_TREE;
10630
10631 if (!DECL_TEMPLATE_PARM_P (r))
10632 DECL_ARG_TYPE (r) = type_passed_as (type);
10633
10634 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10635 args, complain, in_decl);
10636
10637 /* Keep track of the first new parameter we
10638 generate. That's what will be returned to the
10639 caller. */
10640 if (!first_r)
10641 first_r = r;
10642
10643 /* Build a proper chain of parameters when substituting
10644 into a function parameter pack. */
10645 if (prev_r)
10646 DECL_CHAIN (prev_r) = r;
10647 }
10648
10649 /* If cp_unevaluated_operand is set, we're just looking for a
10650 single dummy parameter, so don't keep going. */
10651 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10652 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10653 complain, DECL_CHAIN (t));
10654
10655 /* FIRST_R contains the start of the chain we've built. */
10656 r = first_r;
10657 }
10658 break;
10659
10660 case FIELD_DECL:
10661 {
10662 tree type = NULL_TREE;
10663 tree vec = NULL_TREE;
10664 tree expanded_types = NULL_TREE;
10665 int len = 1;
10666
10667 if (PACK_EXPANSION_P (TREE_TYPE (t)))
10668 {
10669 /* This field is a lambda capture pack. Return a TREE_VEC of
10670 the expanded fields to instantiate_class_template_1 and
10671 store them in the specializations hash table as a
10672 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10673 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10674 complain, in_decl);
10675 if (TREE_CODE (expanded_types) == TREE_VEC)
10676 {
10677 len = TREE_VEC_LENGTH (expanded_types);
10678 vec = make_tree_vec (len);
10679 }
10680 else
10681 {
10682 /* All we did was update the type. Make a note of that. */
10683 type = expanded_types;
10684 expanded_types = NULL_TREE;
10685 }
10686 }
10687
10688 for (int i = 0; i < len; ++i)
10689 {
10690 r = copy_decl (t);
10691 if (expanded_types)
10692 {
10693 type = TREE_VEC_ELT (expanded_types, i);
10694 DECL_NAME (r)
10695 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10696 }
10697 else if (!type)
10698 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10699
10700 if (type == error_mark_node)
10701 RETURN (error_mark_node);
10702 TREE_TYPE (r) = type;
10703 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10704
10705 if (DECL_C_BIT_FIELD (r))
10706 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10707 non-bit-fields DECL_INITIAL is a non-static data member
10708 initializer, which gets deferred instantiation. */
10709 DECL_INITIAL (r)
10710 = tsubst_expr (DECL_INITIAL (t), args,
10711 complain, in_decl,
10712 /*integral_constant_expression_p=*/true);
10713 else if (DECL_INITIAL (t))
10714 {
10715 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10716 NSDMI in perform_member_init. Still set DECL_INITIAL
10717 so that we know there is one. */
10718 DECL_INITIAL (r) = void_zero_node;
10719 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10720 retrofit_lang_decl (r);
10721 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10722 }
10723 /* We don't have to set DECL_CONTEXT here; it is set by
10724 finish_member_declaration. */
10725 DECL_CHAIN (r) = NULL_TREE;
10726
10727 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10728 args, complain, in_decl);
10729
10730 if (vec)
10731 TREE_VEC_ELT (vec, i) = r;
10732 }
10733
10734 if (vec)
10735 {
10736 r = vec;
10737 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
10738 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
10739 SET_ARGUMENT_PACK_ARGS (pack, vec);
10740 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
10741 TREE_TYPE (pack) = tpack;
10742 register_specialization (pack, t, args, false, 0);
10743 }
10744 }
10745 break;
10746
10747 case USING_DECL:
10748 /* We reach here only for member using decls. We also need to check
10749 uses_template_parms because DECL_DEPENDENT_P is not set for a
10750 using-declaration that designates a member of the current
10751 instantiation (c++/53549). */
10752 if (DECL_DEPENDENT_P (t)
10753 || uses_template_parms (USING_DECL_SCOPE (t)))
10754 {
10755 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10756 complain, in_decl);
10757 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10758 r = do_class_using_decl (inst_scope, name);
10759 if (!r)
10760 r = error_mark_node;
10761 else
10762 {
10763 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10764 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10765 }
10766 }
10767 else
10768 {
10769 r = copy_node (t);
10770 DECL_CHAIN (r) = NULL_TREE;
10771 }
10772 break;
10773
10774 case TYPE_DECL:
10775 case VAR_DECL:
10776 {
10777 tree argvec = NULL_TREE;
10778 tree gen_tmpl = NULL_TREE;
10779 tree spec;
10780 tree tmpl = NULL_TREE;
10781 tree ctx;
10782 tree type = NULL_TREE;
10783 bool local_p;
10784
10785 if (TREE_CODE (t) == TYPE_DECL
10786 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10787 {
10788 /* If this is the canonical decl, we don't have to
10789 mess with instantiations, and often we can't (for
10790 typename, template type parms and such). Note that
10791 TYPE_NAME is not correct for the above test if
10792 we've copied the type for a typedef. */
10793 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10794 if (type == error_mark_node)
10795 RETURN (error_mark_node);
10796 r = TYPE_NAME (type);
10797 break;
10798 }
10799
10800 /* Check to see if we already have the specialization we
10801 need. */
10802 spec = NULL_TREE;
10803 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10804 {
10805 /* T is a static data member or namespace-scope entity.
10806 We have to substitute into namespace-scope variables
10807 (even though such entities are never templates) because
10808 of cases like:
10809
10810 template <class T> void f() { extern T t; }
10811
10812 where the entity referenced is not known until
10813 instantiation time. */
10814 local_p = false;
10815 ctx = DECL_CONTEXT (t);
10816 if (DECL_CLASS_SCOPE_P (t))
10817 {
10818 ctx = tsubst_aggr_type (ctx, args,
10819 complain,
10820 in_decl, /*entering_scope=*/1);
10821 /* If CTX is unchanged, then T is in fact the
10822 specialization we want. That situation occurs when
10823 referencing a static data member within in its own
10824 class. We can use pointer equality, rather than
10825 same_type_p, because DECL_CONTEXT is always
10826 canonical... */
10827 if (ctx == DECL_CONTEXT (t)
10828 && (TREE_CODE (t) != TYPE_DECL
10829 /* ... unless T is a member template; in which
10830 case our caller can be willing to create a
10831 specialization of that template represented
10832 by T. */
10833 || !(DECL_TI_TEMPLATE (t)
10834 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10835 spec = t;
10836 }
10837
10838 if (!spec)
10839 {
10840 tmpl = DECL_TI_TEMPLATE (t);
10841 gen_tmpl = most_general_template (tmpl);
10842 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10843 if (argvec == error_mark_node)
10844 RETURN (error_mark_node);
10845 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10846 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10847 }
10848 }
10849 else
10850 {
10851 /* A local variable. */
10852 local_p = true;
10853 /* Subsequent calls to pushdecl will fill this in. */
10854 ctx = NULL_TREE;
10855 spec = retrieve_local_specialization (t);
10856 }
10857 /* If we already have the specialization we need, there is
10858 nothing more to do. */
10859 if (spec)
10860 {
10861 r = spec;
10862 break;
10863 }
10864
10865 /* Create a new node for the specialization we need. */
10866 r = copy_decl (t);
10867 if (type == NULL_TREE)
10868 {
10869 if (is_typedef_decl (t))
10870 type = DECL_ORIGINAL_TYPE (t);
10871 else
10872 type = TREE_TYPE (t);
10873 if (VAR_P (t)
10874 && VAR_HAD_UNKNOWN_BOUND (t)
10875 && type != error_mark_node)
10876 type = strip_array_domain (type);
10877 type = tsubst (type, args, complain, in_decl);
10878 }
10879 if (VAR_P (r))
10880 {
10881 /* Even if the original location is out of scope, the
10882 newly substituted one is not. */
10883 DECL_DEAD_FOR_LOCAL (r) = 0;
10884 DECL_INITIALIZED_P (r) = 0;
10885 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10886 if (type == error_mark_node)
10887 RETURN (error_mark_node);
10888 if (TREE_CODE (type) == FUNCTION_TYPE)
10889 {
10890 /* It may seem that this case cannot occur, since:
10891
10892 typedef void f();
10893 void g() { f x; }
10894
10895 declares a function, not a variable. However:
10896
10897 typedef void f();
10898 template <typename T> void g() { T t; }
10899 template void g<f>();
10900
10901 is an attempt to declare a variable with function
10902 type. */
10903 error ("variable %qD has function type",
10904 /* R is not yet sufficiently initialized, so we
10905 just use its name. */
10906 DECL_NAME (r));
10907 RETURN (error_mark_node);
10908 }
10909 type = complete_type (type);
10910 /* Wait until cp_finish_decl to set this again, to handle
10911 circular dependency (template/instantiate6.C). */
10912 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10913 type = check_var_type (DECL_NAME (r), type);
10914
10915 if (DECL_HAS_VALUE_EXPR_P (t))
10916 {
10917 tree ve = DECL_VALUE_EXPR (t);
10918 ve = tsubst_expr (ve, args, complain, in_decl,
10919 /*constant_expression_p=*/false);
10920 if (REFERENCE_REF_P (ve))
10921 {
10922 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10923 ve = TREE_OPERAND (ve, 0);
10924 }
10925 SET_DECL_VALUE_EXPR (r, ve);
10926 }
10927 }
10928 else if (DECL_SELF_REFERENCE_P (t))
10929 SET_DECL_SELF_REFERENCE_P (r);
10930 TREE_TYPE (r) = type;
10931 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10932 DECL_CONTEXT (r) = ctx;
10933 /* Clear out the mangled name and RTL for the instantiation. */
10934 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10935 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10936 SET_DECL_RTL (r, NULL);
10937 /* The initializer must not be expanded until it is required;
10938 see [temp.inst]. */
10939 DECL_INITIAL (r) = NULL_TREE;
10940 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10941 SET_DECL_RTL (r, NULL);
10942 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10943 if (VAR_P (r))
10944 {
10945 /* Possibly limit visibility based on template args. */
10946 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10947 if (DECL_VISIBILITY_SPECIFIED (t))
10948 {
10949 DECL_VISIBILITY_SPECIFIED (r) = 0;
10950 DECL_ATTRIBUTES (r)
10951 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10952 }
10953 determine_visibility (r);
10954 }
10955
10956 if (!local_p)
10957 {
10958 /* A static data member declaration is always marked
10959 external when it is declared in-class, even if an
10960 initializer is present. We mimic the non-template
10961 processing here. */
10962 DECL_EXTERNAL (r) = 1;
10963
10964 register_specialization (r, gen_tmpl, argvec, false, hash);
10965 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10966 SET_DECL_IMPLICIT_INSTANTIATION (r);
10967 }
10968 else if (cp_unevaluated_operand)
10969 gcc_unreachable ();
10970 else
10971 register_local_specialization (r, t);
10972
10973 DECL_CHAIN (r) = NULL_TREE;
10974
10975 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10976 /*flags=*/0,
10977 args, complain, in_decl);
10978
10979 /* Preserve a typedef that names a type. */
10980 if (is_typedef_decl (r))
10981 {
10982 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10983 set_underlying_type (r);
10984 }
10985
10986 layout_decl (r, 0);
10987 }
10988 break;
10989
10990 default:
10991 gcc_unreachable ();
10992 }
10993 #undef RETURN
10994
10995 out:
10996 /* Restore the file and line information. */
10997 input_location = saved_loc;
10998
10999 return r;
11000 }
11001
11002 /* Substitute into the ARG_TYPES of a function type.
11003 If END is a TREE_CHAIN, leave it and any following types
11004 un-substituted. */
11005
11006 static tree
11007 tsubst_arg_types (tree arg_types,
11008 tree args,
11009 tree end,
11010 tsubst_flags_t complain,
11011 tree in_decl)
11012 {
11013 tree remaining_arg_types;
11014 tree type = NULL_TREE;
11015 int i = 1;
11016 tree expanded_args = NULL_TREE;
11017 tree default_arg;
11018
11019 if (!arg_types || arg_types == void_list_node || arg_types == end)
11020 return arg_types;
11021
11022 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11023 args, end, complain, in_decl);
11024 if (remaining_arg_types == error_mark_node)
11025 return error_mark_node;
11026
11027 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11028 {
11029 /* For a pack expansion, perform substitution on the
11030 entire expression. Later on, we'll handle the arguments
11031 one-by-one. */
11032 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11033 args, complain, in_decl);
11034
11035 if (TREE_CODE (expanded_args) == TREE_VEC)
11036 /* So that we'll spin through the parameters, one by one. */
11037 i = TREE_VEC_LENGTH (expanded_args);
11038 else
11039 {
11040 /* We only partially substituted into the parameter
11041 pack. Our type is TYPE_PACK_EXPANSION. */
11042 type = expanded_args;
11043 expanded_args = NULL_TREE;
11044 }
11045 }
11046
11047 while (i > 0) {
11048 --i;
11049
11050 if (expanded_args)
11051 type = TREE_VEC_ELT (expanded_args, i);
11052 else if (!type)
11053 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11054
11055 if (type == error_mark_node)
11056 return error_mark_node;
11057 if (VOID_TYPE_P (type))
11058 {
11059 if (complain & tf_error)
11060 {
11061 error ("invalid parameter type %qT", type);
11062 if (in_decl)
11063 error ("in declaration %q+D", in_decl);
11064 }
11065 return error_mark_node;
11066 }
11067 /* DR 657. */
11068 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11069 return error_mark_node;
11070
11071 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11072 top-level qualifiers as required. */
11073 type = cv_unqualified (type_decays_to (type));
11074
11075 /* We do not substitute into default arguments here. The standard
11076 mandates that they be instantiated only when needed, which is
11077 done in build_over_call. */
11078 default_arg = TREE_PURPOSE (arg_types);
11079
11080 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11081 {
11082 /* We've instantiated a template before its default arguments
11083 have been parsed. This can happen for a nested template
11084 class, and is not an error unless we require the default
11085 argument in a call of this function. */
11086 remaining_arg_types =
11087 tree_cons (default_arg, type, remaining_arg_types);
11088 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11089 }
11090 else
11091 remaining_arg_types =
11092 hash_tree_cons (default_arg, type, remaining_arg_types);
11093 }
11094
11095 return remaining_arg_types;
11096 }
11097
11098 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11099 *not* handle the exception-specification for FNTYPE, because the
11100 initial substitution of explicitly provided template parameters
11101 during argument deduction forbids substitution into the
11102 exception-specification:
11103
11104 [temp.deduct]
11105
11106 All references in the function type of the function template to the
11107 corresponding template parameters are replaced by the specified tem-
11108 plate argument values. If a substitution in a template parameter or
11109 in the function type of the function template results in an invalid
11110 type, type deduction fails. [Note: The equivalent substitution in
11111 exception specifications is done only when the function is instanti-
11112 ated, at which point a program is ill-formed if the substitution
11113 results in an invalid type.] */
11114
11115 static tree
11116 tsubst_function_type (tree t,
11117 tree args,
11118 tsubst_flags_t complain,
11119 tree in_decl)
11120 {
11121 tree return_type;
11122 tree arg_types;
11123 tree fntype;
11124
11125 /* The TYPE_CONTEXT is not used for function/method types. */
11126 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11127
11128 /* Substitute the return type. */
11129 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11130 if (return_type == error_mark_node)
11131 return error_mark_node;
11132 /* DR 486 clarifies that creation of a function type with an
11133 invalid return type is a deduction failure. */
11134 if (TREE_CODE (return_type) == ARRAY_TYPE
11135 || TREE_CODE (return_type) == FUNCTION_TYPE)
11136 {
11137 if (complain & tf_error)
11138 {
11139 if (TREE_CODE (return_type) == ARRAY_TYPE)
11140 error ("function returning an array");
11141 else
11142 error ("function returning a function");
11143 }
11144 return error_mark_node;
11145 }
11146 /* And DR 657. */
11147 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11148 return error_mark_node;
11149
11150 /* Substitute the argument types. */
11151 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11152 complain, in_decl);
11153 if (arg_types == error_mark_node)
11154 return error_mark_node;
11155
11156 /* Construct a new type node and return it. */
11157 if (TREE_CODE (t) == FUNCTION_TYPE)
11158 {
11159 fntype = build_function_type (return_type, arg_types);
11160 fntype = apply_memfn_quals (fntype,
11161 type_memfn_quals (t),
11162 type_memfn_rqual (t));
11163 }
11164 else
11165 {
11166 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11167 if (! MAYBE_CLASS_TYPE_P (r))
11168 {
11169 /* [temp.deduct]
11170
11171 Type deduction may fail for any of the following
11172 reasons:
11173
11174 -- Attempting to create "pointer to member of T" when T
11175 is not a class type. */
11176 if (complain & tf_error)
11177 error ("creating pointer to member function of non-class type %qT",
11178 r);
11179 return error_mark_node;
11180 }
11181
11182 fntype = build_method_type_directly (r, return_type,
11183 TREE_CHAIN (arg_types));
11184 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11185 }
11186 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11187
11188 return fntype;
11189 }
11190
11191 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11192 ARGS into that specification, and return the substituted
11193 specification. If there is no specification, return NULL_TREE. */
11194
11195 static tree
11196 tsubst_exception_specification (tree fntype,
11197 tree args,
11198 tsubst_flags_t complain,
11199 tree in_decl,
11200 bool defer_ok)
11201 {
11202 tree specs;
11203 tree new_specs;
11204
11205 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11206 new_specs = NULL_TREE;
11207 if (specs && TREE_PURPOSE (specs))
11208 {
11209 /* A noexcept-specifier. */
11210 tree expr = TREE_PURPOSE (specs);
11211 if (TREE_CODE (expr) == INTEGER_CST)
11212 new_specs = expr;
11213 else if (defer_ok)
11214 {
11215 /* Defer instantiation of noexcept-specifiers to avoid
11216 excessive instantiations (c++/49107). */
11217 new_specs = make_node (DEFERRED_NOEXCEPT);
11218 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11219 {
11220 /* We already partially instantiated this member template,
11221 so combine the new args with the old. */
11222 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11223 = DEFERRED_NOEXCEPT_PATTERN (expr);
11224 DEFERRED_NOEXCEPT_ARGS (new_specs)
11225 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11226 }
11227 else
11228 {
11229 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11230 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11231 }
11232 }
11233 else
11234 new_specs = tsubst_copy_and_build
11235 (expr, args, complain, in_decl, /*function_p=*/false,
11236 /*integral_constant_expression_p=*/true);
11237 new_specs = build_noexcept_spec (new_specs, complain);
11238 }
11239 else if (specs)
11240 {
11241 if (! TREE_VALUE (specs))
11242 new_specs = specs;
11243 else
11244 while (specs)
11245 {
11246 tree spec;
11247 int i, len = 1;
11248 tree expanded_specs = NULL_TREE;
11249
11250 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11251 {
11252 /* Expand the pack expansion type. */
11253 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11254 args, complain,
11255 in_decl);
11256
11257 if (expanded_specs == error_mark_node)
11258 return error_mark_node;
11259 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11260 len = TREE_VEC_LENGTH (expanded_specs);
11261 else
11262 {
11263 /* We're substituting into a member template, so
11264 we got a TYPE_PACK_EXPANSION back. Add that
11265 expansion and move on. */
11266 gcc_assert (TREE_CODE (expanded_specs)
11267 == TYPE_PACK_EXPANSION);
11268 new_specs = add_exception_specifier (new_specs,
11269 expanded_specs,
11270 complain);
11271 specs = TREE_CHAIN (specs);
11272 continue;
11273 }
11274 }
11275
11276 for (i = 0; i < len; ++i)
11277 {
11278 if (expanded_specs)
11279 spec = TREE_VEC_ELT (expanded_specs, i);
11280 else
11281 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11282 if (spec == error_mark_node)
11283 return spec;
11284 new_specs = add_exception_specifier (new_specs, spec,
11285 complain);
11286 }
11287
11288 specs = TREE_CHAIN (specs);
11289 }
11290 }
11291 return new_specs;
11292 }
11293
11294 /* Take the tree structure T and replace template parameters used
11295 therein with the argument vector ARGS. IN_DECL is an associated
11296 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11297 Issue error and warning messages under control of COMPLAIN. Note
11298 that we must be relatively non-tolerant of extensions here, in
11299 order to preserve conformance; if we allow substitutions that
11300 should not be allowed, we may allow argument deductions that should
11301 not succeed, and therefore report ambiguous overload situations
11302 where there are none. In theory, we could allow the substitution,
11303 but indicate that it should have failed, and allow our caller to
11304 make sure that the right thing happens, but we don't try to do this
11305 yet.
11306
11307 This function is used for dealing with types, decls and the like;
11308 for expressions, use tsubst_expr or tsubst_copy. */
11309
11310 tree
11311 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11312 {
11313 enum tree_code code;
11314 tree type, r = NULL_TREE;
11315
11316 if (t == NULL_TREE || t == error_mark_node
11317 || t == integer_type_node
11318 || t == void_type_node
11319 || t == char_type_node
11320 || t == unknown_type_node
11321 || TREE_CODE (t) == NAMESPACE_DECL
11322 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11323 return t;
11324
11325 if (DECL_P (t))
11326 return tsubst_decl (t, args, complain);
11327
11328 if (args == NULL_TREE)
11329 return t;
11330
11331 code = TREE_CODE (t);
11332
11333 if (code == IDENTIFIER_NODE)
11334 type = IDENTIFIER_TYPE_VALUE (t);
11335 else
11336 type = TREE_TYPE (t);
11337
11338 gcc_assert (type != unknown_type_node);
11339
11340 /* Reuse typedefs. We need to do this to handle dependent attributes,
11341 such as attribute aligned. */
11342 if (TYPE_P (t)
11343 && typedef_variant_p (t))
11344 {
11345 tree decl = TYPE_NAME (t);
11346
11347 if (alias_template_specialization_p (t))
11348 {
11349 /* DECL represents an alias template and we want to
11350 instantiate it. */
11351 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11352 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11353 r = instantiate_alias_template (tmpl, gen_args, complain);
11354 }
11355 else if (DECL_CLASS_SCOPE_P (decl)
11356 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11357 && uses_template_parms (DECL_CONTEXT (decl)))
11358 {
11359 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11360 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11361 r = retrieve_specialization (tmpl, gen_args, 0);
11362 }
11363 else if (DECL_FUNCTION_SCOPE_P (decl)
11364 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11365 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11366 r = retrieve_local_specialization (decl);
11367 else
11368 /* The typedef is from a non-template context. */
11369 return t;
11370
11371 if (r)
11372 {
11373 r = TREE_TYPE (r);
11374 r = cp_build_qualified_type_real
11375 (r, cp_type_quals (t) | cp_type_quals (r),
11376 complain | tf_ignore_bad_quals);
11377 return r;
11378 }
11379 else
11380 {
11381 /* We don't have an instantiation yet, so drop the typedef. */
11382 int quals = cp_type_quals (t);
11383 t = DECL_ORIGINAL_TYPE (decl);
11384 t = cp_build_qualified_type_real (t, quals,
11385 complain | tf_ignore_bad_quals);
11386 }
11387 }
11388
11389 if (type
11390 && code != TYPENAME_TYPE
11391 && code != TEMPLATE_TYPE_PARM
11392 && code != IDENTIFIER_NODE
11393 && code != FUNCTION_TYPE
11394 && code != METHOD_TYPE)
11395 type = tsubst (type, args, complain, in_decl);
11396 if (type == error_mark_node)
11397 return error_mark_node;
11398
11399 switch (code)
11400 {
11401 case RECORD_TYPE:
11402 case UNION_TYPE:
11403 case ENUMERAL_TYPE:
11404 return tsubst_aggr_type (t, args, complain, in_decl,
11405 /*entering_scope=*/0);
11406
11407 case ERROR_MARK:
11408 case IDENTIFIER_NODE:
11409 case VOID_TYPE:
11410 case REAL_TYPE:
11411 case COMPLEX_TYPE:
11412 case VECTOR_TYPE:
11413 case BOOLEAN_TYPE:
11414 case NULLPTR_TYPE:
11415 case LANG_TYPE:
11416 return t;
11417
11418 case INTEGER_TYPE:
11419 if (t == integer_type_node)
11420 return t;
11421
11422 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11423 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11424 return t;
11425
11426 {
11427 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11428
11429 max = tsubst_expr (omax, args, complain, in_decl,
11430 /*integral_constant_expression_p=*/false);
11431
11432 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11433 needed. */
11434 if (TREE_CODE (max) == NOP_EXPR
11435 && TREE_SIDE_EFFECTS (omax)
11436 && !TREE_TYPE (max))
11437 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11438
11439 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11440 with TREE_SIDE_EFFECTS that indicates this is not an integral
11441 constant expression. */
11442 if (processing_template_decl
11443 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11444 {
11445 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11446 TREE_SIDE_EFFECTS (max) = 1;
11447 }
11448
11449 return compute_array_index_type (NULL_TREE, max, complain);
11450 }
11451
11452 case TEMPLATE_TYPE_PARM:
11453 case TEMPLATE_TEMPLATE_PARM:
11454 case BOUND_TEMPLATE_TEMPLATE_PARM:
11455 case TEMPLATE_PARM_INDEX:
11456 {
11457 int idx;
11458 int level;
11459 int levels;
11460 tree arg = NULL_TREE;
11461
11462 r = NULL_TREE;
11463
11464 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11465 template_parm_level_and_index (t, &level, &idx);
11466
11467 levels = TMPL_ARGS_DEPTH (args);
11468 if (level <= levels)
11469 {
11470 arg = TMPL_ARG (args, level, idx);
11471
11472 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11473 {
11474 /* See through ARGUMENT_PACK_SELECT arguments. */
11475 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11476 /* If the selected argument is an expansion E, that most
11477 likely means we were called from
11478 gen_elem_of_pack_expansion_instantiation during the
11479 substituting of pack an argument pack (which Ith
11480 element is a pack expansion, where I is
11481 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11482 In this case, the Ith element resulting from this
11483 substituting is going to be a pack expansion, which
11484 pattern is the pattern of E. Let's return the
11485 pattern of E, and
11486 gen_elem_of_pack_expansion_instantiation will
11487 build the resulting pack expansion from it. */
11488 if (PACK_EXPANSION_P (arg))
11489 arg = PACK_EXPANSION_PATTERN (arg);
11490 }
11491 }
11492
11493 if (arg == error_mark_node)
11494 return error_mark_node;
11495 else if (arg != NULL_TREE)
11496 {
11497 if (ARGUMENT_PACK_P (arg))
11498 /* If ARG is an argument pack, we don't actually want to
11499 perform a substitution here, because substitutions
11500 for argument packs are only done
11501 element-by-element. We can get to this point when
11502 substituting the type of a non-type template
11503 parameter pack, when that type actually contains
11504 template parameter packs from an outer template, e.g.,
11505
11506 template<typename... Types> struct A {
11507 template<Types... Values> struct B { };
11508 }; */
11509 return t;
11510
11511 if (code == TEMPLATE_TYPE_PARM)
11512 {
11513 int quals;
11514 gcc_assert (TYPE_P (arg));
11515
11516 quals = cp_type_quals (arg) | cp_type_quals (t);
11517
11518 return cp_build_qualified_type_real
11519 (arg, quals, complain | tf_ignore_bad_quals);
11520 }
11521 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11522 {
11523 /* We are processing a type constructed from a
11524 template template parameter. */
11525 tree argvec = tsubst (TYPE_TI_ARGS (t),
11526 args, complain, in_decl);
11527 if (argvec == error_mark_node)
11528 return error_mark_node;
11529
11530 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11531 || TREE_CODE (arg) == TEMPLATE_DECL
11532 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11533
11534 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11535 /* Consider this code:
11536
11537 template <template <class> class Template>
11538 struct Internal {
11539 template <class Arg> using Bind = Template<Arg>;
11540 };
11541
11542 template <template <class> class Template, class Arg>
11543 using Instantiate = Template<Arg>; //#0
11544
11545 template <template <class> class Template,
11546 class Argument>
11547 using Bind =
11548 Instantiate<Internal<Template>::template Bind,
11549 Argument>; //#1
11550
11551 When #1 is parsed, the
11552 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11553 parameter `Template' in #0 matches the
11554 UNBOUND_CLASS_TEMPLATE representing the argument
11555 `Internal<Template>::template Bind'; We then want
11556 to assemble the type `Bind<Argument>' that can't
11557 be fully created right now, because
11558 `Internal<Template>' not being complete, the Bind
11559 template cannot be looked up in that context. So
11560 we need to "store" `Bind<Argument>' for later
11561 when the context of Bind becomes complete. Let's
11562 store that in a TYPENAME_TYPE. */
11563 return make_typename_type (TYPE_CONTEXT (arg),
11564 build_nt (TEMPLATE_ID_EXPR,
11565 TYPE_IDENTIFIER (arg),
11566 argvec),
11567 typename_type,
11568 complain);
11569
11570 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11571 are resolving nested-types in the signature of a
11572 member function templates. Otherwise ARG is a
11573 TEMPLATE_DECL and is the real template to be
11574 instantiated. */
11575 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11576 arg = TYPE_NAME (arg);
11577
11578 r = lookup_template_class (arg,
11579 argvec, in_decl,
11580 DECL_CONTEXT (arg),
11581 /*entering_scope=*/0,
11582 complain);
11583 return cp_build_qualified_type_real
11584 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11585 }
11586 else
11587 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11588 return convert_from_reference (unshare_expr (arg));
11589 }
11590
11591 if (level == 1)
11592 /* This can happen during the attempted tsubst'ing in
11593 unify. This means that we don't yet have any information
11594 about the template parameter in question. */
11595 return t;
11596
11597 /* Early in template argument deduction substitution, we don't
11598 want to reduce the level of 'auto', or it will be confused
11599 with a normal template parm in subsequent deduction. */
11600 if (is_auto (t) && (complain & tf_partial))
11601 return t;
11602
11603 /* If we get here, we must have been looking at a parm for a
11604 more deeply nested template. Make a new version of this
11605 template parameter, but with a lower level. */
11606 switch (code)
11607 {
11608 case TEMPLATE_TYPE_PARM:
11609 case TEMPLATE_TEMPLATE_PARM:
11610 case BOUND_TEMPLATE_TEMPLATE_PARM:
11611 if (cp_type_quals (t))
11612 {
11613 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11614 r = cp_build_qualified_type_real
11615 (r, cp_type_quals (t),
11616 complain | (code == TEMPLATE_TYPE_PARM
11617 ? tf_ignore_bad_quals : 0));
11618 }
11619 else
11620 {
11621 r = copy_type (t);
11622 TEMPLATE_TYPE_PARM_INDEX (r)
11623 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11624 r, levels, args, complain);
11625 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11626 TYPE_MAIN_VARIANT (r) = r;
11627 TYPE_POINTER_TO (r) = NULL_TREE;
11628 TYPE_REFERENCE_TO (r) = NULL_TREE;
11629
11630 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11631 /* We have reduced the level of the template
11632 template parameter, but not the levels of its
11633 template parameters, so canonical_type_parameter
11634 will not be able to find the canonical template
11635 template parameter for this level. Thus, we
11636 require structural equality checking to compare
11637 TEMPLATE_TEMPLATE_PARMs. */
11638 SET_TYPE_STRUCTURAL_EQUALITY (r);
11639 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11640 SET_TYPE_STRUCTURAL_EQUALITY (r);
11641 else
11642 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11643
11644 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11645 {
11646 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11647 complain, in_decl);
11648 if (argvec == error_mark_node)
11649 return error_mark_node;
11650
11651 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11652 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11653 }
11654 }
11655 break;
11656
11657 case TEMPLATE_PARM_INDEX:
11658 r = reduce_template_parm_level (t, type, levels, args, complain);
11659 break;
11660
11661 default:
11662 gcc_unreachable ();
11663 }
11664
11665 return r;
11666 }
11667
11668 case TREE_LIST:
11669 {
11670 tree purpose, value, chain;
11671
11672 if (t == void_list_node)
11673 return t;
11674
11675 purpose = TREE_PURPOSE (t);
11676 if (purpose)
11677 {
11678 purpose = tsubst (purpose, args, complain, in_decl);
11679 if (purpose == error_mark_node)
11680 return error_mark_node;
11681 }
11682 value = TREE_VALUE (t);
11683 if (value)
11684 {
11685 value = tsubst (value, args, complain, in_decl);
11686 if (value == error_mark_node)
11687 return error_mark_node;
11688 }
11689 chain = TREE_CHAIN (t);
11690 if (chain && chain != void_type_node)
11691 {
11692 chain = tsubst (chain, args, complain, in_decl);
11693 if (chain == error_mark_node)
11694 return error_mark_node;
11695 }
11696 if (purpose == TREE_PURPOSE (t)
11697 && value == TREE_VALUE (t)
11698 && chain == TREE_CHAIN (t))
11699 return t;
11700 return hash_tree_cons (purpose, value, chain);
11701 }
11702
11703 case TREE_BINFO:
11704 /* We should never be tsubsting a binfo. */
11705 gcc_unreachable ();
11706
11707 case TREE_VEC:
11708 /* A vector of template arguments. */
11709 gcc_assert (!type);
11710 return tsubst_template_args (t, args, complain, in_decl);
11711
11712 case POINTER_TYPE:
11713 case REFERENCE_TYPE:
11714 {
11715 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11716 return t;
11717
11718 /* [temp.deduct]
11719
11720 Type deduction may fail for any of the following
11721 reasons:
11722
11723 -- Attempting to create a pointer to reference type.
11724 -- Attempting to create a reference to a reference type or
11725 a reference to void.
11726
11727 Core issue 106 says that creating a reference to a reference
11728 during instantiation is no longer a cause for failure. We
11729 only enforce this check in strict C++98 mode. */
11730 if ((TREE_CODE (type) == REFERENCE_TYPE
11731 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11732 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
11733 {
11734 static location_t last_loc;
11735
11736 /* We keep track of the last time we issued this error
11737 message to avoid spewing a ton of messages during a
11738 single bad template instantiation. */
11739 if (complain & tf_error
11740 && last_loc != input_location)
11741 {
11742 if (VOID_TYPE_P (type))
11743 error ("forming reference to void");
11744 else if (code == POINTER_TYPE)
11745 error ("forming pointer to reference type %qT", type);
11746 else
11747 error ("forming reference to reference type %qT", type);
11748 last_loc = input_location;
11749 }
11750
11751 return error_mark_node;
11752 }
11753 else if (TREE_CODE (type) == FUNCTION_TYPE
11754 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11755 || type_memfn_rqual (type) != REF_QUAL_NONE))
11756 {
11757 if (complain & tf_error)
11758 {
11759 if (code == POINTER_TYPE)
11760 error ("forming pointer to qualified function type %qT",
11761 type);
11762 else
11763 error ("forming reference to qualified function type %qT",
11764 type);
11765 }
11766 return error_mark_node;
11767 }
11768 else if (code == POINTER_TYPE)
11769 {
11770 r = build_pointer_type (type);
11771 if (TREE_CODE (type) == METHOD_TYPE)
11772 r = build_ptrmemfunc_type (r);
11773 }
11774 else if (TREE_CODE (type) == REFERENCE_TYPE)
11775 /* In C++0x, during template argument substitution, when there is an
11776 attempt to create a reference to a reference type, reference
11777 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11778
11779 "If a template-argument for a template-parameter T names a type
11780 that is a reference to a type A, an attempt to create the type
11781 'lvalue reference to cv T' creates the type 'lvalue reference to
11782 A,' while an attempt to create the type type rvalue reference to
11783 cv T' creates the type T"
11784 */
11785 r = cp_build_reference_type
11786 (TREE_TYPE (type),
11787 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11788 else
11789 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11790 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11791
11792 if (cxx_dialect >= cxx1y
11793 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
11794 && array_of_runtime_bound_p (type))
11795 {
11796 if (complain & tf_warning_or_error)
11797 pedwarn
11798 (input_location, OPT_Wvla,
11799 code == REFERENCE_TYPE
11800 ? G_("cannot declare reference to array of runtime bound")
11801 : G_("cannot declare pointer to array of runtime bound"));
11802 else
11803 r = error_mark_node;
11804 }
11805
11806 if (r != error_mark_node)
11807 /* Will this ever be needed for TYPE_..._TO values? */
11808 layout_type (r);
11809
11810 return r;
11811 }
11812 case OFFSET_TYPE:
11813 {
11814 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11815 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11816 {
11817 /* [temp.deduct]
11818
11819 Type deduction may fail for any of the following
11820 reasons:
11821
11822 -- Attempting to create "pointer to member of T" when T
11823 is not a class type. */
11824 if (complain & tf_error)
11825 error ("creating pointer to member of non-class type %qT", r);
11826 return error_mark_node;
11827 }
11828 if (TREE_CODE (type) == REFERENCE_TYPE)
11829 {
11830 if (complain & tf_error)
11831 error ("creating pointer to member reference type %qT", type);
11832 return error_mark_node;
11833 }
11834 if (VOID_TYPE_P (type))
11835 {
11836 if (complain & tf_error)
11837 error ("creating pointer to member of type void");
11838 return error_mark_node;
11839 }
11840 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11841 if (TREE_CODE (type) == FUNCTION_TYPE)
11842 {
11843 /* The type of the implicit object parameter gets its
11844 cv-qualifiers from the FUNCTION_TYPE. */
11845 tree memptr;
11846 tree method_type
11847 = build_memfn_type (type, r, type_memfn_quals (type),
11848 type_memfn_rqual (type));
11849 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11850 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11851 complain);
11852 }
11853 else
11854 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11855 cp_type_quals (t),
11856 complain);
11857 }
11858 case FUNCTION_TYPE:
11859 case METHOD_TYPE:
11860 {
11861 tree fntype;
11862 tree specs;
11863 fntype = tsubst_function_type (t, args, complain, in_decl);
11864 if (fntype == error_mark_node)
11865 return error_mark_node;
11866
11867 /* Substitute the exception specification. */
11868 specs = tsubst_exception_specification (t, args, complain,
11869 in_decl, /*defer_ok*/true);
11870 if (specs == error_mark_node)
11871 return error_mark_node;
11872 if (specs)
11873 fntype = build_exception_variant (fntype, specs);
11874 return fntype;
11875 }
11876 case ARRAY_TYPE:
11877 {
11878 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11879 if (domain == error_mark_node)
11880 return error_mark_node;
11881
11882 /* As an optimization, we avoid regenerating the array type if
11883 it will obviously be the same as T. */
11884 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11885 return t;
11886
11887 /* These checks should match the ones in grokdeclarator.
11888
11889 [temp.deduct]
11890
11891 The deduction may fail for any of the following reasons:
11892
11893 -- Attempting to create an array with an element type that
11894 is void, a function type, or a reference type, or [DR337]
11895 an abstract class type. */
11896 if (VOID_TYPE_P (type)
11897 || TREE_CODE (type) == FUNCTION_TYPE
11898 || TREE_CODE (type) == REFERENCE_TYPE)
11899 {
11900 if (complain & tf_error)
11901 error ("creating array of %qT", type);
11902 return error_mark_node;
11903 }
11904
11905 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
11906 return error_mark_node;
11907
11908 r = build_cplus_array_type (type, domain);
11909
11910 if (TYPE_USER_ALIGN (t))
11911 {
11912 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11913 TYPE_USER_ALIGN (r) = 1;
11914 }
11915
11916 return r;
11917 }
11918
11919 case TYPENAME_TYPE:
11920 {
11921 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11922 in_decl, /*entering_scope=*/1);
11923 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11924 complain, in_decl);
11925
11926 if (ctx == error_mark_node || f == error_mark_node)
11927 return error_mark_node;
11928
11929 if (!MAYBE_CLASS_TYPE_P (ctx))
11930 {
11931 if (complain & tf_error)
11932 error ("%qT is not a class, struct, or union type", ctx);
11933 return error_mark_node;
11934 }
11935 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11936 {
11937 /* Normally, make_typename_type does not require that the CTX
11938 have complete type in order to allow things like:
11939
11940 template <class T> struct S { typename S<T>::X Y; };
11941
11942 But, such constructs have already been resolved by this
11943 point, so here CTX really should have complete type, unless
11944 it's a partial instantiation. */
11945 ctx = complete_type (ctx);
11946 if (!COMPLETE_TYPE_P (ctx))
11947 {
11948 if (complain & tf_error)
11949 cxx_incomplete_type_error (NULL_TREE, ctx);
11950 return error_mark_node;
11951 }
11952 }
11953
11954 f = make_typename_type (ctx, f, typename_type,
11955 complain | tf_keep_type_decl);
11956 if (f == error_mark_node)
11957 return f;
11958 if (TREE_CODE (f) == TYPE_DECL)
11959 {
11960 complain |= tf_ignore_bad_quals;
11961 f = TREE_TYPE (f);
11962 }
11963
11964 if (TREE_CODE (f) != TYPENAME_TYPE)
11965 {
11966 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11967 {
11968 if (complain & tf_error)
11969 error ("%qT resolves to %qT, which is not an enumeration type",
11970 t, f);
11971 else
11972 return error_mark_node;
11973 }
11974 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11975 {
11976 if (complain & tf_error)
11977 error ("%qT resolves to %qT, which is is not a class type",
11978 t, f);
11979 else
11980 return error_mark_node;
11981 }
11982 }
11983
11984 return cp_build_qualified_type_real
11985 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11986 }
11987
11988 case UNBOUND_CLASS_TEMPLATE:
11989 {
11990 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11991 in_decl, /*entering_scope=*/1);
11992 tree name = TYPE_IDENTIFIER (t);
11993 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
11994
11995 if (ctx == error_mark_node || name == error_mark_node)
11996 return error_mark_node;
11997
11998 if (parm_list)
11999 parm_list = tsubst_template_parms (parm_list, args, complain);
12000 return make_unbound_class_template (ctx, name, parm_list, complain);
12001 }
12002
12003 case TYPEOF_TYPE:
12004 {
12005 tree type;
12006
12007 ++cp_unevaluated_operand;
12008 ++c_inhibit_evaluation_warnings;
12009
12010 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12011 complain, in_decl,
12012 /*integral_constant_expression_p=*/false);
12013
12014 --cp_unevaluated_operand;
12015 --c_inhibit_evaluation_warnings;
12016
12017 type = finish_typeof (type);
12018 return cp_build_qualified_type_real (type,
12019 cp_type_quals (t)
12020 | cp_type_quals (type),
12021 complain);
12022 }
12023
12024 case DECLTYPE_TYPE:
12025 {
12026 tree type;
12027
12028 ++cp_unevaluated_operand;
12029 ++c_inhibit_evaluation_warnings;
12030
12031 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12032 complain|tf_decltype, in_decl,
12033 /*function_p*/false,
12034 /*integral_constant_expression*/false);
12035
12036 --cp_unevaluated_operand;
12037 --c_inhibit_evaluation_warnings;
12038
12039 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12040 type = lambda_capture_field_type (type,
12041 DECLTYPE_FOR_INIT_CAPTURE (t));
12042 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12043 type = lambda_proxy_type (type);
12044 else
12045 {
12046 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12047 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12048 && EXPR_P (type))
12049 /* In a template ~id could be either a complement expression
12050 or an unqualified-id naming a destructor; if instantiating
12051 it produces an expression, it's not an id-expression or
12052 member access. */
12053 id = false;
12054 type = finish_decltype_type (type, id, complain);
12055 }
12056 return cp_build_qualified_type_real (type,
12057 cp_type_quals (t)
12058 | cp_type_quals (type),
12059 complain);
12060 }
12061
12062 case UNDERLYING_TYPE:
12063 {
12064 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12065 complain, in_decl);
12066 return finish_underlying_type (type);
12067 }
12068
12069 case TYPE_ARGUMENT_PACK:
12070 case NONTYPE_ARGUMENT_PACK:
12071 {
12072 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12073 tree packed_out =
12074 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12075 args,
12076 complain,
12077 in_decl);
12078 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12079
12080 /* For template nontype argument packs, also substitute into
12081 the type. */
12082 if (code == NONTYPE_ARGUMENT_PACK)
12083 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12084
12085 return r;
12086 }
12087 break;
12088
12089 case INTEGER_CST:
12090 case REAL_CST:
12091 case STRING_CST:
12092 case PLUS_EXPR:
12093 case MINUS_EXPR:
12094 case NEGATE_EXPR:
12095 case NOP_EXPR:
12096 case INDIRECT_REF:
12097 case ADDR_EXPR:
12098 case CALL_EXPR:
12099 case ARRAY_REF:
12100 case SCOPE_REF:
12101 /* We should use one of the expression tsubsts for these codes. */
12102 gcc_unreachable ();
12103
12104 default:
12105 sorry ("use of %qs in template", get_tree_code_name (code));
12106 return error_mark_node;
12107 }
12108 }
12109
12110 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12111 type of the expression on the left-hand side of the "." or "->"
12112 operator. */
12113
12114 static tree
12115 tsubst_baselink (tree baselink, tree object_type,
12116 tree args, tsubst_flags_t complain, tree in_decl)
12117 {
12118 tree name;
12119 tree qualifying_scope;
12120 tree fns;
12121 tree optype;
12122 tree template_args = 0;
12123 bool template_id_p = false;
12124 bool qualified = BASELINK_QUALIFIED_P (baselink);
12125
12126 /* A baselink indicates a function from a base class. Both the
12127 BASELINK_ACCESS_BINFO and the base class referenced may
12128 indicate bases of the template class, rather than the
12129 instantiated class. In addition, lookups that were not
12130 ambiguous before may be ambiguous now. Therefore, we perform
12131 the lookup again. */
12132 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12133 qualifying_scope = tsubst (qualifying_scope, args,
12134 complain, in_decl);
12135 fns = BASELINK_FUNCTIONS (baselink);
12136 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12137 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12138 {
12139 template_id_p = true;
12140 template_args = TREE_OPERAND (fns, 1);
12141 fns = TREE_OPERAND (fns, 0);
12142 if (template_args)
12143 template_args = tsubst_template_args (template_args, args,
12144 complain, in_decl);
12145 }
12146 name = DECL_NAME (get_first_fn (fns));
12147 if (IDENTIFIER_TYPENAME_P (name))
12148 name = mangle_conv_op_name_for_type (optype);
12149 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12150 if (!baselink)
12151 return error_mark_node;
12152
12153 /* If lookup found a single function, mark it as used at this
12154 point. (If it lookup found multiple functions the one selected
12155 later by overload resolution will be marked as used at that
12156 point.) */
12157 if (BASELINK_P (baselink))
12158 fns = BASELINK_FUNCTIONS (baselink);
12159 if (!template_id_p && !really_overloaded_fn (fns))
12160 mark_used (OVL_CURRENT (fns));
12161
12162 /* Add back the template arguments, if present. */
12163 if (BASELINK_P (baselink) && template_id_p)
12164 BASELINK_FUNCTIONS (baselink)
12165 = build_nt (TEMPLATE_ID_EXPR,
12166 BASELINK_FUNCTIONS (baselink),
12167 template_args);
12168 /* Update the conversion operator type. */
12169 BASELINK_OPTYPE (baselink) = optype;
12170
12171 if (!object_type)
12172 object_type = current_class_type;
12173
12174 if (qualified)
12175 baselink = adjust_result_of_qualified_name_lookup (baselink,
12176 qualifying_scope,
12177 object_type);
12178 return baselink;
12179 }
12180
12181 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12182 true if the qualified-id will be a postfix-expression in-and-of
12183 itself; false if more of the postfix-expression follows the
12184 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12185 of "&". */
12186
12187 static tree
12188 tsubst_qualified_id (tree qualified_id, tree args,
12189 tsubst_flags_t complain, tree in_decl,
12190 bool done, bool address_p)
12191 {
12192 tree expr;
12193 tree scope;
12194 tree name;
12195 bool is_template;
12196 tree template_args;
12197 location_t loc = UNKNOWN_LOCATION;
12198
12199 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12200
12201 /* Figure out what name to look up. */
12202 name = TREE_OPERAND (qualified_id, 1);
12203 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12204 {
12205 is_template = true;
12206 loc = EXPR_LOCATION (name);
12207 template_args = TREE_OPERAND (name, 1);
12208 if (template_args)
12209 template_args = tsubst_template_args (template_args, args,
12210 complain, in_decl);
12211 name = TREE_OPERAND (name, 0);
12212 }
12213 else
12214 {
12215 is_template = false;
12216 template_args = NULL_TREE;
12217 }
12218
12219 /* Substitute into the qualifying scope. When there are no ARGS, we
12220 are just trying to simplify a non-dependent expression. In that
12221 case the qualifying scope may be dependent, and, in any case,
12222 substituting will not help. */
12223 scope = TREE_OPERAND (qualified_id, 0);
12224 if (args)
12225 {
12226 scope = tsubst (scope, args, complain, in_decl);
12227 expr = tsubst_copy (name, args, complain, in_decl);
12228 }
12229 else
12230 expr = name;
12231
12232 if (dependent_scope_p (scope))
12233 {
12234 if (is_template)
12235 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12236 return build_qualified_name (NULL_TREE, scope, expr,
12237 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12238 }
12239
12240 if (!BASELINK_P (name) && !DECL_P (expr))
12241 {
12242 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12243 {
12244 /* A BIT_NOT_EXPR is used to represent a destructor. */
12245 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12246 {
12247 error ("qualifying type %qT does not match destructor name ~%qT",
12248 scope, TREE_OPERAND (expr, 0));
12249 expr = error_mark_node;
12250 }
12251 else
12252 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12253 /*is_type_p=*/0, false);
12254 }
12255 else
12256 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12257 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12258 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12259 {
12260 if (complain & tf_error)
12261 {
12262 error ("dependent-name %qE is parsed as a non-type, but "
12263 "instantiation yields a type", qualified_id);
12264 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12265 }
12266 return error_mark_node;
12267 }
12268 }
12269
12270 if (DECL_P (expr))
12271 {
12272 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12273 scope);
12274 /* Remember that there was a reference to this entity. */
12275 mark_used (expr);
12276 }
12277
12278 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12279 {
12280 if (complain & tf_error)
12281 qualified_name_lookup_error (scope,
12282 TREE_OPERAND (qualified_id, 1),
12283 expr, input_location);
12284 return error_mark_node;
12285 }
12286
12287 if (is_template)
12288 expr = lookup_template_function (expr, template_args);
12289
12290 if (expr == error_mark_node && complain & tf_error)
12291 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12292 expr, input_location);
12293 else if (TYPE_P (scope))
12294 {
12295 expr = (adjust_result_of_qualified_name_lookup
12296 (expr, scope, current_nonlambda_class_type ()));
12297 expr = (finish_qualified_id_expr
12298 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12299 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12300 /*template_arg_p=*/false, complain));
12301 }
12302
12303 /* Expressions do not generally have reference type. */
12304 if (TREE_CODE (expr) != SCOPE_REF
12305 /* However, if we're about to form a pointer-to-member, we just
12306 want the referenced member referenced. */
12307 && TREE_CODE (expr) != OFFSET_REF)
12308 expr = convert_from_reference (expr);
12309
12310 return expr;
12311 }
12312
12313 /* Like tsubst, but deals with expressions. This function just replaces
12314 template parms; to finish processing the resultant expression, use
12315 tsubst_copy_and_build or tsubst_expr. */
12316
12317 static tree
12318 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12319 {
12320 enum tree_code code;
12321 tree r;
12322
12323 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12324 return t;
12325
12326 code = TREE_CODE (t);
12327
12328 switch (code)
12329 {
12330 case PARM_DECL:
12331 r = retrieve_local_specialization (t);
12332
12333 if (r == NULL_TREE)
12334 {
12335 /* We get here for a use of 'this' in an NSDMI. */
12336 if (DECL_NAME (t) == this_identifier
12337 && at_function_scope_p ()
12338 && DECL_CONSTRUCTOR_P (current_function_decl))
12339 return current_class_ptr;
12340
12341 /* This can happen for a parameter name used later in a function
12342 declaration (such as in a late-specified return type). Just
12343 make a dummy decl, since it's only used for its type. */
12344 gcc_assert (cp_unevaluated_operand != 0);
12345 r = tsubst_decl (t, args, complain);
12346 /* Give it the template pattern as its context; its true context
12347 hasn't been instantiated yet and this is good enough for
12348 mangling. */
12349 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12350 }
12351
12352 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12353 r = ARGUMENT_PACK_SELECT_ARG (r);
12354 mark_used (r);
12355 return r;
12356
12357 case CONST_DECL:
12358 {
12359 tree enum_type;
12360 tree v;
12361
12362 if (DECL_TEMPLATE_PARM_P (t))
12363 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12364 /* There is no need to substitute into namespace-scope
12365 enumerators. */
12366 if (DECL_NAMESPACE_SCOPE_P (t))
12367 return t;
12368 /* If ARGS is NULL, then T is known to be non-dependent. */
12369 if (args == NULL_TREE)
12370 return integral_constant_value (t);
12371
12372 /* Unfortunately, we cannot just call lookup_name here.
12373 Consider:
12374
12375 template <int I> int f() {
12376 enum E { a = I };
12377 struct S { void g() { E e = a; } };
12378 };
12379
12380 When we instantiate f<7>::S::g(), say, lookup_name is not
12381 clever enough to find f<7>::a. */
12382 enum_type
12383 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12384 /*entering_scope=*/0);
12385
12386 for (v = TYPE_VALUES (enum_type);
12387 v != NULL_TREE;
12388 v = TREE_CHAIN (v))
12389 if (TREE_PURPOSE (v) == DECL_NAME (t))
12390 return TREE_VALUE (v);
12391
12392 /* We didn't find the name. That should never happen; if
12393 name-lookup found it during preliminary parsing, we
12394 should find it again here during instantiation. */
12395 gcc_unreachable ();
12396 }
12397 return t;
12398
12399 case FIELD_DECL:
12400 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12401 {
12402 /* Check for a local specialization set up by
12403 tsubst_pack_expansion. */
12404 tree r = retrieve_local_specialization (t);
12405 if (r)
12406 {
12407 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12408 r = ARGUMENT_PACK_SELECT_ARG (r);
12409 return r;
12410 }
12411
12412 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12413 tsubst_decl put in the hash table. */
12414 return retrieve_specialization (t, args, 0);
12415 }
12416
12417 if (DECL_CONTEXT (t))
12418 {
12419 tree ctx;
12420
12421 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12422 /*entering_scope=*/1);
12423 if (ctx != DECL_CONTEXT (t))
12424 {
12425 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12426 if (!r)
12427 {
12428 if (complain & tf_error)
12429 error ("using invalid field %qD", t);
12430 return error_mark_node;
12431 }
12432 return r;
12433 }
12434 }
12435
12436 return t;
12437
12438 case VAR_DECL:
12439 case FUNCTION_DECL:
12440 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12441 r = tsubst (t, args, complain, in_decl);
12442 else if (local_variable_p (t))
12443 {
12444 r = retrieve_local_specialization (t);
12445 if (r == NULL_TREE)
12446 {
12447 if (DECL_ANON_UNION_VAR_P (t))
12448 {
12449 /* Just use name lookup to find a member alias for an
12450 anonymous union, but then add it to the hash table. */
12451 r = lookup_name (DECL_NAME (t));
12452 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12453 register_local_specialization (r, t);
12454 }
12455 else
12456 {
12457 gcc_assert (errorcount || sorrycount);
12458 return error_mark_node;
12459 }
12460 }
12461 }
12462 else
12463 r = t;
12464 mark_used (r);
12465 return r;
12466
12467 case NAMESPACE_DECL:
12468 return t;
12469
12470 case OVERLOAD:
12471 /* An OVERLOAD will always be a non-dependent overload set; an
12472 overload set from function scope will just be represented with an
12473 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12474 gcc_assert (!uses_template_parms (t));
12475 return t;
12476
12477 case BASELINK:
12478 return tsubst_baselink (t, current_nonlambda_class_type (),
12479 args, complain, in_decl);
12480
12481 case TEMPLATE_DECL:
12482 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12483 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12484 args, complain, in_decl);
12485 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12486 return tsubst (t, args, complain, in_decl);
12487 else if (DECL_CLASS_SCOPE_P (t)
12488 && uses_template_parms (DECL_CONTEXT (t)))
12489 {
12490 /* Template template argument like the following example need
12491 special treatment:
12492
12493 template <template <class> class TT> struct C {};
12494 template <class T> struct D {
12495 template <class U> struct E {};
12496 C<E> c; // #1
12497 };
12498 D<int> d; // #2
12499
12500 We are processing the template argument `E' in #1 for
12501 the template instantiation #2. Originally, `E' is a
12502 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12503 have to substitute this with one having context `D<int>'. */
12504
12505 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12506 return lookup_field (context, DECL_NAME(t), 0, false);
12507 }
12508 else
12509 /* Ordinary template template argument. */
12510 return t;
12511
12512 case CAST_EXPR:
12513 case REINTERPRET_CAST_EXPR:
12514 case CONST_CAST_EXPR:
12515 case STATIC_CAST_EXPR:
12516 case DYNAMIC_CAST_EXPR:
12517 case IMPLICIT_CONV_EXPR:
12518 case CONVERT_EXPR:
12519 case NOP_EXPR:
12520 return build1
12521 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12522 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12523
12524 case SIZEOF_EXPR:
12525 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12526 {
12527
12528 tree expanded, op = TREE_OPERAND (t, 0);
12529 int len = 0;
12530
12531 if (SIZEOF_EXPR_TYPE_P (t))
12532 op = TREE_TYPE (op);
12533
12534 ++cp_unevaluated_operand;
12535 ++c_inhibit_evaluation_warnings;
12536 /* We only want to compute the number of arguments. */
12537 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12538 --cp_unevaluated_operand;
12539 --c_inhibit_evaluation_warnings;
12540
12541 if (TREE_CODE (expanded) == TREE_VEC)
12542 len = TREE_VEC_LENGTH (expanded);
12543
12544 if (expanded == error_mark_node)
12545 return error_mark_node;
12546 else if (PACK_EXPANSION_P (expanded)
12547 || (TREE_CODE (expanded) == TREE_VEC
12548 && len > 0
12549 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12550 {
12551 if (TREE_CODE (expanded) == TREE_VEC)
12552 expanded = TREE_VEC_ELT (expanded, len - 1);
12553
12554 if (TYPE_P (expanded))
12555 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12556 complain & tf_error);
12557 else
12558 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12559 complain & tf_error);
12560 }
12561 else
12562 return build_int_cst (size_type_node, len);
12563 }
12564 if (SIZEOF_EXPR_TYPE_P (t))
12565 {
12566 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12567 args, complain, in_decl);
12568 r = build1 (NOP_EXPR, r, error_mark_node);
12569 r = build1 (SIZEOF_EXPR,
12570 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12571 SIZEOF_EXPR_TYPE_P (r) = 1;
12572 return r;
12573 }
12574 /* Fall through */
12575
12576 case INDIRECT_REF:
12577 case NEGATE_EXPR:
12578 case TRUTH_NOT_EXPR:
12579 case BIT_NOT_EXPR:
12580 case ADDR_EXPR:
12581 case UNARY_PLUS_EXPR: /* Unary + */
12582 case ALIGNOF_EXPR:
12583 case AT_ENCODE_EXPR:
12584 case ARROW_EXPR:
12585 case THROW_EXPR:
12586 case TYPEID_EXPR:
12587 case REALPART_EXPR:
12588 case IMAGPART_EXPR:
12589 case PAREN_EXPR:
12590 return build1
12591 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12592 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12593
12594 case COMPONENT_REF:
12595 {
12596 tree object;
12597 tree name;
12598
12599 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12600 name = TREE_OPERAND (t, 1);
12601 if (TREE_CODE (name) == BIT_NOT_EXPR)
12602 {
12603 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12604 complain, in_decl);
12605 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12606 }
12607 else if (TREE_CODE (name) == SCOPE_REF
12608 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12609 {
12610 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12611 complain, in_decl);
12612 name = TREE_OPERAND (name, 1);
12613 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12614 complain, in_decl);
12615 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12616 name = build_qualified_name (/*type=*/NULL_TREE,
12617 base, name,
12618 /*template_p=*/false);
12619 }
12620 else if (BASELINK_P (name))
12621 name = tsubst_baselink (name,
12622 non_reference (TREE_TYPE (object)),
12623 args, complain,
12624 in_decl);
12625 else
12626 name = tsubst_copy (name, args, complain, in_decl);
12627 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12628 }
12629
12630 case PLUS_EXPR:
12631 case MINUS_EXPR:
12632 case MULT_EXPR:
12633 case TRUNC_DIV_EXPR:
12634 case CEIL_DIV_EXPR:
12635 case FLOOR_DIV_EXPR:
12636 case ROUND_DIV_EXPR:
12637 case EXACT_DIV_EXPR:
12638 case BIT_AND_EXPR:
12639 case BIT_IOR_EXPR:
12640 case BIT_XOR_EXPR:
12641 case TRUNC_MOD_EXPR:
12642 case FLOOR_MOD_EXPR:
12643 case TRUTH_ANDIF_EXPR:
12644 case TRUTH_ORIF_EXPR:
12645 case TRUTH_AND_EXPR:
12646 case TRUTH_OR_EXPR:
12647 case RSHIFT_EXPR:
12648 case LSHIFT_EXPR:
12649 case RROTATE_EXPR:
12650 case LROTATE_EXPR:
12651 case EQ_EXPR:
12652 case NE_EXPR:
12653 case MAX_EXPR:
12654 case MIN_EXPR:
12655 case LE_EXPR:
12656 case GE_EXPR:
12657 case LT_EXPR:
12658 case GT_EXPR:
12659 case COMPOUND_EXPR:
12660 case DOTSTAR_EXPR:
12661 case MEMBER_REF:
12662 case PREDECREMENT_EXPR:
12663 case PREINCREMENT_EXPR:
12664 case POSTDECREMENT_EXPR:
12665 case POSTINCREMENT_EXPR:
12666 return build_nt
12667 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12668 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12669
12670 case SCOPE_REF:
12671 return build_qualified_name (/*type=*/NULL_TREE,
12672 tsubst_copy (TREE_OPERAND (t, 0),
12673 args, complain, in_decl),
12674 tsubst_copy (TREE_OPERAND (t, 1),
12675 args, complain, in_decl),
12676 QUALIFIED_NAME_IS_TEMPLATE (t));
12677
12678 case ARRAY_REF:
12679 return build_nt
12680 (ARRAY_REF,
12681 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12682 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12683 NULL_TREE, NULL_TREE);
12684
12685 case CALL_EXPR:
12686 {
12687 int n = VL_EXP_OPERAND_LENGTH (t);
12688 tree result = build_vl_exp (CALL_EXPR, n);
12689 int i;
12690 for (i = 0; i < n; i++)
12691 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12692 complain, in_decl);
12693 return result;
12694 }
12695
12696 case COND_EXPR:
12697 case MODOP_EXPR:
12698 case PSEUDO_DTOR_EXPR:
12699 case VEC_PERM_EXPR:
12700 {
12701 r = build_nt
12702 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12703 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12704 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12705 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12706 return r;
12707 }
12708
12709 case NEW_EXPR:
12710 {
12711 r = build_nt
12712 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12713 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12714 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12715 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12716 return r;
12717 }
12718
12719 case DELETE_EXPR:
12720 {
12721 r = build_nt
12722 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12723 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12724 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12725 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12726 return r;
12727 }
12728
12729 case TEMPLATE_ID_EXPR:
12730 {
12731 /* Substituted template arguments */
12732 tree fn = TREE_OPERAND (t, 0);
12733 tree targs = TREE_OPERAND (t, 1);
12734
12735 fn = tsubst_copy (fn, args, complain, in_decl);
12736 if (targs)
12737 targs = tsubst_template_args (targs, args, complain, in_decl);
12738
12739 return lookup_template_function (fn, targs);
12740 }
12741
12742 case TREE_LIST:
12743 {
12744 tree purpose, value, chain;
12745
12746 if (t == void_list_node)
12747 return t;
12748
12749 purpose = TREE_PURPOSE (t);
12750 if (purpose)
12751 purpose = tsubst_copy (purpose, args, complain, in_decl);
12752 value = TREE_VALUE (t);
12753 if (value)
12754 value = tsubst_copy (value, args, complain, in_decl);
12755 chain = TREE_CHAIN (t);
12756 if (chain && chain != void_type_node)
12757 chain = tsubst_copy (chain, args, complain, in_decl);
12758 if (purpose == TREE_PURPOSE (t)
12759 && value == TREE_VALUE (t)
12760 && chain == TREE_CHAIN (t))
12761 return t;
12762 return tree_cons (purpose, value, chain);
12763 }
12764
12765 case RECORD_TYPE:
12766 case UNION_TYPE:
12767 case ENUMERAL_TYPE:
12768 case INTEGER_TYPE:
12769 case TEMPLATE_TYPE_PARM:
12770 case TEMPLATE_TEMPLATE_PARM:
12771 case BOUND_TEMPLATE_TEMPLATE_PARM:
12772 case TEMPLATE_PARM_INDEX:
12773 case POINTER_TYPE:
12774 case REFERENCE_TYPE:
12775 case OFFSET_TYPE:
12776 case FUNCTION_TYPE:
12777 case METHOD_TYPE:
12778 case ARRAY_TYPE:
12779 case TYPENAME_TYPE:
12780 case UNBOUND_CLASS_TEMPLATE:
12781 case TYPEOF_TYPE:
12782 case DECLTYPE_TYPE:
12783 case TYPE_DECL:
12784 return tsubst (t, args, complain, in_decl);
12785
12786 case USING_DECL:
12787 t = DECL_NAME (t);
12788 /* Fall through. */
12789 case IDENTIFIER_NODE:
12790 if (IDENTIFIER_TYPENAME_P (t))
12791 {
12792 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12793 return mangle_conv_op_name_for_type (new_type);
12794 }
12795 else
12796 return t;
12797
12798 case CONSTRUCTOR:
12799 /* This is handled by tsubst_copy_and_build. */
12800 gcc_unreachable ();
12801
12802 case VA_ARG_EXPR:
12803 return build_x_va_arg (EXPR_LOCATION (t),
12804 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12805 in_decl),
12806 tsubst (TREE_TYPE (t), args, complain, in_decl));
12807
12808 case CLEANUP_POINT_EXPR:
12809 /* We shouldn't have built any of these during initial template
12810 generation. Instead, they should be built during instantiation
12811 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12812 gcc_unreachable ();
12813
12814 case OFFSET_REF:
12815 r = build2
12816 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12817 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12818 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12819 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12820 mark_used (TREE_OPERAND (r, 1));
12821 return r;
12822
12823 case EXPR_PACK_EXPANSION:
12824 error ("invalid use of pack expansion expression");
12825 return error_mark_node;
12826
12827 case NONTYPE_ARGUMENT_PACK:
12828 error ("use %<...%> to expand argument pack");
12829 return error_mark_node;
12830
12831 case INTEGER_CST:
12832 case REAL_CST:
12833 case STRING_CST:
12834 case COMPLEX_CST:
12835 {
12836 /* Instantiate any typedefs in the type. */
12837 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12838 r = fold_convert (type, t);
12839 gcc_assert (TREE_CODE (r) == code);
12840 return r;
12841 }
12842
12843 case PTRMEM_CST:
12844 /* These can sometimes show up in a partial instantiation, but never
12845 involve template parms. */
12846 gcc_assert (!uses_template_parms (t));
12847 return t;
12848
12849 default:
12850 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12851 gcc_checking_assert (false);
12852 return t;
12853 }
12854 }
12855
12856 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12857
12858 static tree
12859 tsubst_omp_clauses (tree clauses, bool declare_simd,
12860 tree args, tsubst_flags_t complain, tree in_decl)
12861 {
12862 tree new_clauses = NULL, nc, oc;
12863
12864 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12865 {
12866 nc = copy_node (oc);
12867 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12868 new_clauses = nc;
12869
12870 switch (OMP_CLAUSE_CODE (nc))
12871 {
12872 case OMP_CLAUSE_LASTPRIVATE:
12873 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12874 {
12875 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12876 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12877 in_decl, /*integral_constant_expression_p=*/false);
12878 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12879 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12880 }
12881 /* FALLTHRU */
12882 case OMP_CLAUSE_PRIVATE:
12883 case OMP_CLAUSE_SHARED:
12884 case OMP_CLAUSE_FIRSTPRIVATE:
12885 case OMP_CLAUSE_COPYIN:
12886 case OMP_CLAUSE_COPYPRIVATE:
12887 case OMP_CLAUSE_IF:
12888 case OMP_CLAUSE_NUM_THREADS:
12889 case OMP_CLAUSE_SCHEDULE:
12890 case OMP_CLAUSE_COLLAPSE:
12891 case OMP_CLAUSE_FINAL:
12892 case OMP_CLAUSE_DEPEND:
12893 case OMP_CLAUSE_FROM:
12894 case OMP_CLAUSE_TO:
12895 case OMP_CLAUSE_UNIFORM:
12896 case OMP_CLAUSE_MAP:
12897 case OMP_CLAUSE_DEVICE:
12898 case OMP_CLAUSE_DIST_SCHEDULE:
12899 case OMP_CLAUSE_NUM_TEAMS:
12900 case OMP_CLAUSE_THREAD_LIMIT:
12901 case OMP_CLAUSE_SAFELEN:
12902 case OMP_CLAUSE_SIMDLEN:
12903 OMP_CLAUSE_OPERAND (nc, 0)
12904 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12905 in_decl, /*integral_constant_expression_p=*/false);
12906 break;
12907 case OMP_CLAUSE_REDUCTION:
12908 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
12909 {
12910 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
12911 if (TREE_CODE (placeholder) == SCOPE_REF)
12912 {
12913 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
12914 complain, in_decl);
12915 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
12916 = build_qualified_name (NULL_TREE, scope,
12917 TREE_OPERAND (placeholder, 1),
12918 false);
12919 }
12920 else
12921 gcc_assert (identifier_p (placeholder));
12922 }
12923 OMP_CLAUSE_OPERAND (nc, 0)
12924 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12925 in_decl, /*integral_constant_expression_p=*/false);
12926 break;
12927 case OMP_CLAUSE_LINEAR:
12928 case OMP_CLAUSE_ALIGNED:
12929 OMP_CLAUSE_OPERAND (nc, 0)
12930 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12931 in_decl, /*integral_constant_expression_p=*/false);
12932 OMP_CLAUSE_OPERAND (nc, 1)
12933 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
12934 in_decl, /*integral_constant_expression_p=*/false);
12935 break;
12936
12937 case OMP_CLAUSE_NOWAIT:
12938 case OMP_CLAUSE_ORDERED:
12939 case OMP_CLAUSE_DEFAULT:
12940 case OMP_CLAUSE_UNTIED:
12941 case OMP_CLAUSE_MERGEABLE:
12942 case OMP_CLAUSE_INBRANCH:
12943 case OMP_CLAUSE_NOTINBRANCH:
12944 case OMP_CLAUSE_PROC_BIND:
12945 case OMP_CLAUSE_FOR:
12946 case OMP_CLAUSE_PARALLEL:
12947 case OMP_CLAUSE_SECTIONS:
12948 case OMP_CLAUSE_TASKGROUP:
12949 break;
12950 default:
12951 gcc_unreachable ();
12952 }
12953 }
12954
12955 new_clauses = nreverse (new_clauses);
12956 if (!declare_simd)
12957 new_clauses = finish_omp_clauses (new_clauses);
12958 return new_clauses;
12959 }
12960
12961 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12962
12963 static tree
12964 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12965 tree in_decl)
12966 {
12967 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12968
12969 tree purpose, value, chain;
12970
12971 if (t == NULL)
12972 return t;
12973
12974 if (TREE_CODE (t) != TREE_LIST)
12975 return tsubst_copy_and_build (t, args, complain, in_decl,
12976 /*function_p=*/false,
12977 /*integral_constant_expression_p=*/false);
12978
12979 if (t == void_list_node)
12980 return t;
12981
12982 purpose = TREE_PURPOSE (t);
12983 if (purpose)
12984 purpose = RECUR (purpose);
12985 value = TREE_VALUE (t);
12986 if (value)
12987 {
12988 if (TREE_CODE (value) != LABEL_DECL)
12989 value = RECUR (value);
12990 else
12991 {
12992 value = lookup_label (DECL_NAME (value));
12993 gcc_assert (TREE_CODE (value) == LABEL_DECL);
12994 TREE_USED (value) = 1;
12995 }
12996 }
12997 chain = TREE_CHAIN (t);
12998 if (chain && chain != void_type_node)
12999 chain = RECUR (chain);
13000 return tree_cons (purpose, value, chain);
13001 #undef RECUR
13002 }
13003
13004 /* Substitute one OMP_FOR iterator. */
13005
13006 static void
13007 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13008 tree condv, tree incrv, tree *clauses,
13009 tree args, tsubst_flags_t complain, tree in_decl,
13010 bool integral_constant_expression_p)
13011 {
13012 #define RECUR(NODE) \
13013 tsubst_expr ((NODE), args, complain, in_decl, \
13014 integral_constant_expression_p)
13015 tree decl, init, cond, incr;
13016 bool init_decl;
13017
13018 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13019 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13020 decl = TREE_OPERAND (init, 0);
13021 init = TREE_OPERAND (init, 1);
13022 /* Do this before substituting into decl to handle 'auto'. */
13023 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
13024 init = RECUR (init);
13025 decl = RECUR (decl);
13026 if (init_decl)
13027 {
13028 gcc_assert (!processing_template_decl);
13029 init = DECL_INITIAL (decl);
13030 DECL_INITIAL (decl) = NULL_TREE;
13031 }
13032
13033 gcc_assert (!type_dependent_expression_p (decl));
13034
13035 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13036 {
13037 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13038 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13039 if (TREE_CODE (incr) == MODIFY_EXPR)
13040 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13041 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13042 RECUR (TREE_OPERAND (incr, 1)),
13043 complain);
13044 else
13045 incr = RECUR (incr);
13046 TREE_VEC_ELT (declv, i) = decl;
13047 TREE_VEC_ELT (initv, i) = init;
13048 TREE_VEC_ELT (condv, i) = cond;
13049 TREE_VEC_ELT (incrv, i) = incr;
13050 return;
13051 }
13052
13053 if (init && !init_decl)
13054 {
13055 tree c;
13056 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13057 {
13058 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13059 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13060 && OMP_CLAUSE_DECL (c) == decl)
13061 break;
13062 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13063 && OMP_CLAUSE_DECL (c) == decl)
13064 error ("iteration variable %qD should not be firstprivate", decl);
13065 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13066 && OMP_CLAUSE_DECL (c) == decl)
13067 error ("iteration variable %qD should not be reduction", decl);
13068 }
13069 if (c == NULL)
13070 {
13071 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13072 OMP_CLAUSE_DECL (c) = decl;
13073 c = finish_omp_clauses (c);
13074 if (c)
13075 {
13076 OMP_CLAUSE_CHAIN (c) = *clauses;
13077 *clauses = c;
13078 }
13079 }
13080 }
13081 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13082 if (COMPARISON_CLASS_P (cond))
13083 cond = build2 (TREE_CODE (cond), boolean_type_node,
13084 RECUR (TREE_OPERAND (cond, 0)),
13085 RECUR (TREE_OPERAND (cond, 1)));
13086 else
13087 cond = RECUR (cond);
13088 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13089 switch (TREE_CODE (incr))
13090 {
13091 case PREINCREMENT_EXPR:
13092 case PREDECREMENT_EXPR:
13093 case POSTINCREMENT_EXPR:
13094 case POSTDECREMENT_EXPR:
13095 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13096 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13097 break;
13098 case MODIFY_EXPR:
13099 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13100 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13101 {
13102 tree rhs = TREE_OPERAND (incr, 1);
13103 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13104 RECUR (TREE_OPERAND (incr, 0)),
13105 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13106 RECUR (TREE_OPERAND (rhs, 0)),
13107 RECUR (TREE_OPERAND (rhs, 1))));
13108 }
13109 else
13110 incr = RECUR (incr);
13111 break;
13112 case MODOP_EXPR:
13113 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13114 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13115 {
13116 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13117 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13118 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13119 TREE_TYPE (decl), lhs,
13120 RECUR (TREE_OPERAND (incr, 2))));
13121 }
13122 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13123 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13124 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13125 {
13126 tree rhs = TREE_OPERAND (incr, 2);
13127 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13128 RECUR (TREE_OPERAND (incr, 0)),
13129 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13130 RECUR (TREE_OPERAND (rhs, 0)),
13131 RECUR (TREE_OPERAND (rhs, 1))));
13132 }
13133 else
13134 incr = RECUR (incr);
13135 break;
13136 default:
13137 incr = RECUR (incr);
13138 break;
13139 }
13140
13141 TREE_VEC_ELT (declv, i) = decl;
13142 TREE_VEC_ELT (initv, i) = init;
13143 TREE_VEC_ELT (condv, i) = cond;
13144 TREE_VEC_ELT (incrv, i) = incr;
13145 #undef RECUR
13146 }
13147
13148 /* Like tsubst_copy for expressions, etc. but also does semantic
13149 processing. */
13150
13151 static tree
13152 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13153 bool integral_constant_expression_p)
13154 {
13155 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13156 #define RECUR(NODE) \
13157 tsubst_expr ((NODE), args, complain, in_decl, \
13158 integral_constant_expression_p)
13159
13160 tree stmt, tmp;
13161 tree r;
13162 location_t loc;
13163
13164 if (t == NULL_TREE || t == error_mark_node)
13165 return t;
13166
13167 loc = input_location;
13168 if (EXPR_HAS_LOCATION (t))
13169 input_location = EXPR_LOCATION (t);
13170 if (STATEMENT_CODE_P (TREE_CODE (t)))
13171 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13172
13173 switch (TREE_CODE (t))
13174 {
13175 case STATEMENT_LIST:
13176 {
13177 tree_stmt_iterator i;
13178 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13179 RECUR (tsi_stmt (i));
13180 break;
13181 }
13182
13183 case CTOR_INITIALIZER:
13184 finish_mem_initializers (tsubst_initializer_list
13185 (TREE_OPERAND (t, 0), args));
13186 break;
13187
13188 case RETURN_EXPR:
13189 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13190 break;
13191
13192 case EXPR_STMT:
13193 tmp = RECUR (EXPR_STMT_EXPR (t));
13194 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13195 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13196 else
13197 finish_expr_stmt (tmp);
13198 break;
13199
13200 case USING_STMT:
13201 do_using_directive (USING_STMT_NAMESPACE (t));
13202 break;
13203
13204 case DECL_EXPR:
13205 {
13206 tree decl, pattern_decl;
13207 tree init;
13208
13209 pattern_decl = decl = DECL_EXPR_DECL (t);
13210 if (TREE_CODE (decl) == LABEL_DECL)
13211 finish_label_decl (DECL_NAME (decl));
13212 else if (TREE_CODE (decl) == USING_DECL)
13213 {
13214 tree scope = USING_DECL_SCOPE (decl);
13215 tree name = DECL_NAME (decl);
13216 tree decl;
13217
13218 scope = tsubst (scope, args, complain, in_decl);
13219 decl = lookup_qualified_name (scope, name,
13220 /*is_type_p=*/false,
13221 /*complain=*/false);
13222 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13223 qualified_name_lookup_error (scope, name, decl, input_location);
13224 else
13225 do_local_using_decl (decl, scope, name);
13226 }
13227 else if (DECL_PACK_P (decl))
13228 {
13229 /* Don't build up decls for a variadic capture proxy, we'll
13230 instantiate the elements directly as needed. */
13231 break;
13232 }
13233 else
13234 {
13235 init = DECL_INITIAL (decl);
13236 decl = tsubst (decl, args, complain, in_decl);
13237 if (decl != error_mark_node)
13238 {
13239 /* By marking the declaration as instantiated, we avoid
13240 trying to instantiate it. Since instantiate_decl can't
13241 handle local variables, and since we've already done
13242 all that needs to be done, that's the right thing to
13243 do. */
13244 if (VAR_P (decl))
13245 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13246 if (VAR_P (decl)
13247 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13248 /* Anonymous aggregates are a special case. */
13249 finish_anon_union (decl);
13250 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13251 {
13252 DECL_CONTEXT (decl) = current_function_decl;
13253 if (DECL_NAME (decl) == this_identifier)
13254 {
13255 tree lam = DECL_CONTEXT (current_function_decl);
13256 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13257 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13258 }
13259 insert_capture_proxy (decl);
13260 }
13261 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13262 /* We already did a pushtag. */;
13263 else if (TREE_CODE (decl) == FUNCTION_DECL
13264 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13265 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13266 {
13267 DECL_CONTEXT (decl) = NULL_TREE;
13268 pushdecl (decl);
13269 DECL_CONTEXT (decl) = current_function_decl;
13270 cp_check_omp_declare_reduction (decl);
13271 }
13272 else
13273 {
13274 int const_init = false;
13275 maybe_push_decl (decl);
13276 if (VAR_P (decl)
13277 && DECL_PRETTY_FUNCTION_P (decl))
13278 {
13279 /* For __PRETTY_FUNCTION__ we have to adjust the
13280 initializer. */
13281 const char *const name
13282 = cxx_printable_name (current_function_decl, 2);
13283 init = cp_fname_init (name, &TREE_TYPE (decl));
13284 }
13285 else
13286 {
13287 tree t = RECUR (init);
13288
13289 if (init && !t)
13290 {
13291 /* If we had an initializer but it
13292 instantiated to nothing,
13293 value-initialize the object. This will
13294 only occur when the initializer was a
13295 pack expansion where the parameter packs
13296 used in that expansion were of length
13297 zero. */
13298 init = build_value_init (TREE_TYPE (decl),
13299 complain);
13300 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13301 init = get_target_expr_sfinae (init, complain);
13302 }
13303 else
13304 init = t;
13305 }
13306
13307 if (VAR_P (decl))
13308 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13309 (pattern_decl));
13310 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13311 }
13312 }
13313 }
13314
13315 break;
13316 }
13317
13318 case FOR_STMT:
13319 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13320 RECUR (FOR_INIT_STMT (t));
13321 finish_for_init_stmt (stmt);
13322 tmp = RECUR (FOR_COND (t));
13323 finish_for_cond (tmp, stmt, false);
13324 tmp = RECUR (FOR_EXPR (t));
13325 finish_for_expr (tmp, stmt);
13326 RECUR (FOR_BODY (t));
13327 finish_for_stmt (stmt);
13328 break;
13329
13330 case RANGE_FOR_STMT:
13331 {
13332 tree decl, expr;
13333 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13334 decl = RANGE_FOR_DECL (t);
13335 decl = tsubst (decl, args, complain, in_decl);
13336 maybe_push_decl (decl);
13337 expr = RECUR (RANGE_FOR_EXPR (t));
13338 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13339 RECUR (RANGE_FOR_BODY (t));
13340 finish_for_stmt (stmt);
13341 }
13342 break;
13343
13344 case WHILE_STMT:
13345 stmt = begin_while_stmt ();
13346 tmp = RECUR (WHILE_COND (t));
13347 finish_while_stmt_cond (tmp, stmt, false);
13348 RECUR (WHILE_BODY (t));
13349 finish_while_stmt (stmt);
13350 break;
13351
13352 case DO_STMT:
13353 stmt = begin_do_stmt ();
13354 RECUR (DO_BODY (t));
13355 finish_do_body (stmt);
13356 tmp = RECUR (DO_COND (t));
13357 finish_do_stmt (tmp, stmt, false);
13358 break;
13359
13360 case IF_STMT:
13361 stmt = begin_if_stmt ();
13362 tmp = RECUR (IF_COND (t));
13363 finish_if_stmt_cond (tmp, stmt);
13364 RECUR (THEN_CLAUSE (t));
13365 finish_then_clause (stmt);
13366
13367 if (ELSE_CLAUSE (t))
13368 {
13369 begin_else_clause (stmt);
13370 RECUR (ELSE_CLAUSE (t));
13371 finish_else_clause (stmt);
13372 }
13373
13374 finish_if_stmt (stmt);
13375 break;
13376
13377 case BIND_EXPR:
13378 if (BIND_EXPR_BODY_BLOCK (t))
13379 stmt = begin_function_body ();
13380 else
13381 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13382 ? BCS_TRY_BLOCK : 0);
13383
13384 RECUR (BIND_EXPR_BODY (t));
13385
13386 if (BIND_EXPR_BODY_BLOCK (t))
13387 finish_function_body (stmt);
13388 else
13389 finish_compound_stmt (stmt);
13390 break;
13391
13392 case BREAK_STMT:
13393 finish_break_stmt ();
13394 break;
13395
13396 case CONTINUE_STMT:
13397 finish_continue_stmt ();
13398 break;
13399
13400 case SWITCH_STMT:
13401 stmt = begin_switch_stmt ();
13402 tmp = RECUR (SWITCH_STMT_COND (t));
13403 finish_switch_cond (tmp, stmt);
13404 RECUR (SWITCH_STMT_BODY (t));
13405 finish_switch_stmt (stmt);
13406 break;
13407
13408 case CASE_LABEL_EXPR:
13409 finish_case_label (EXPR_LOCATION (t),
13410 RECUR (CASE_LOW (t)),
13411 RECUR (CASE_HIGH (t)));
13412 break;
13413
13414 case LABEL_EXPR:
13415 {
13416 tree decl = LABEL_EXPR_LABEL (t);
13417 tree label;
13418
13419 label = finish_label_stmt (DECL_NAME (decl));
13420 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13421 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13422 }
13423 break;
13424
13425 case GOTO_EXPR:
13426 tmp = GOTO_DESTINATION (t);
13427 if (TREE_CODE (tmp) != LABEL_DECL)
13428 /* Computed goto's must be tsubst'd into. On the other hand,
13429 non-computed gotos must not be; the identifier in question
13430 will have no binding. */
13431 tmp = RECUR (tmp);
13432 else
13433 tmp = DECL_NAME (tmp);
13434 finish_goto_stmt (tmp);
13435 break;
13436
13437 case ASM_EXPR:
13438 tmp = finish_asm_stmt
13439 (ASM_VOLATILE_P (t),
13440 RECUR (ASM_STRING (t)),
13441 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13442 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13443 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13444 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13445 {
13446 tree asm_expr = tmp;
13447 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13448 asm_expr = TREE_OPERAND (asm_expr, 0);
13449 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13450 }
13451 break;
13452
13453 case TRY_BLOCK:
13454 if (CLEANUP_P (t))
13455 {
13456 stmt = begin_try_block ();
13457 RECUR (TRY_STMTS (t));
13458 finish_cleanup_try_block (stmt);
13459 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13460 }
13461 else
13462 {
13463 tree compound_stmt = NULL_TREE;
13464
13465 if (FN_TRY_BLOCK_P (t))
13466 stmt = begin_function_try_block (&compound_stmt);
13467 else
13468 stmt = begin_try_block ();
13469
13470 RECUR (TRY_STMTS (t));
13471
13472 if (FN_TRY_BLOCK_P (t))
13473 finish_function_try_block (stmt);
13474 else
13475 finish_try_block (stmt);
13476
13477 RECUR (TRY_HANDLERS (t));
13478 if (FN_TRY_BLOCK_P (t))
13479 finish_function_handler_sequence (stmt, compound_stmt);
13480 else
13481 finish_handler_sequence (stmt);
13482 }
13483 break;
13484
13485 case HANDLER:
13486 {
13487 tree decl = HANDLER_PARMS (t);
13488
13489 if (decl)
13490 {
13491 decl = tsubst (decl, args, complain, in_decl);
13492 /* Prevent instantiate_decl from trying to instantiate
13493 this variable. We've already done all that needs to be
13494 done. */
13495 if (decl != error_mark_node)
13496 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13497 }
13498 stmt = begin_handler ();
13499 finish_handler_parms (decl, stmt);
13500 RECUR (HANDLER_BODY (t));
13501 finish_handler (stmt);
13502 }
13503 break;
13504
13505 case TAG_DEFN:
13506 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13507 if (CLASS_TYPE_P (tmp))
13508 {
13509 /* Local classes are not independent templates; they are
13510 instantiated along with their containing function. And this
13511 way we don't have to deal with pushing out of one local class
13512 to instantiate a member of another local class. */
13513 tree fn;
13514 /* Closures are handled by the LAMBDA_EXPR. */
13515 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13516 complete_type (tmp);
13517 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13518 if (!DECL_ARTIFICIAL (fn))
13519 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13520 }
13521 break;
13522
13523 case STATIC_ASSERT:
13524 {
13525 tree condition;
13526
13527 ++c_inhibit_evaluation_warnings;
13528 condition =
13529 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13530 args,
13531 complain, in_decl,
13532 /*integral_constant_expression_p=*/true);
13533 --c_inhibit_evaluation_warnings;
13534
13535 finish_static_assert (condition,
13536 STATIC_ASSERT_MESSAGE (t),
13537 STATIC_ASSERT_SOURCE_LOCATION (t),
13538 /*member_p=*/false);
13539 }
13540 break;
13541
13542 case OMP_PARALLEL:
13543 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13544 args, complain, in_decl);
13545 stmt = begin_omp_parallel ();
13546 RECUR (OMP_PARALLEL_BODY (t));
13547 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13548 = OMP_PARALLEL_COMBINED (t);
13549 break;
13550
13551 case OMP_TASK:
13552 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13553 args, complain, in_decl);
13554 stmt = begin_omp_task ();
13555 RECUR (OMP_TASK_BODY (t));
13556 finish_omp_task (tmp, stmt);
13557 break;
13558
13559 case OMP_FOR:
13560 case OMP_SIMD:
13561 case OMP_DISTRIBUTE:
13562 {
13563 tree clauses, body, pre_body;
13564 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13565 tree incrv = NULL_TREE;
13566 int i;
13567
13568 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13569 args, complain, in_decl);
13570 if (OMP_FOR_INIT (t) != NULL_TREE)
13571 {
13572 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13573 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13574 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13575 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13576 }
13577
13578 stmt = begin_omp_structured_block ();
13579
13580 pre_body = push_stmt_list ();
13581 RECUR (OMP_FOR_PRE_BODY (t));
13582 pre_body = pop_stmt_list (pre_body);
13583
13584 if (OMP_FOR_INIT (t) != NULL_TREE)
13585 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13586 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13587 &clauses, args, complain, in_decl,
13588 integral_constant_expression_p);
13589
13590 body = push_stmt_list ();
13591 RECUR (OMP_FOR_BODY (t));
13592 body = pop_stmt_list (body);
13593
13594 if (OMP_FOR_INIT (t) != NULL_TREE)
13595 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13596 condv, incrv, body, pre_body, clauses);
13597 else
13598 {
13599 t = make_node (TREE_CODE (t));
13600 TREE_TYPE (t) = void_type_node;
13601 OMP_FOR_BODY (t) = body;
13602 OMP_FOR_PRE_BODY (t) = pre_body;
13603 OMP_FOR_CLAUSES (t) = clauses;
13604 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13605 add_stmt (t);
13606 }
13607
13608 add_stmt (finish_omp_structured_block (stmt));
13609 }
13610 break;
13611
13612 case OMP_SECTIONS:
13613 case OMP_SINGLE:
13614 case OMP_TEAMS:
13615 case OMP_TARGET_DATA:
13616 case OMP_TARGET:
13617 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13618 args, complain, in_decl);
13619 stmt = push_stmt_list ();
13620 RECUR (OMP_BODY (t));
13621 stmt = pop_stmt_list (stmt);
13622
13623 t = copy_node (t);
13624 OMP_BODY (t) = stmt;
13625 OMP_CLAUSES (t) = tmp;
13626 add_stmt (t);
13627 break;
13628
13629 case OMP_TARGET_UPDATE:
13630 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
13631 args, complain, in_decl);
13632 t = copy_node (t);
13633 OMP_CLAUSES (t) = tmp;
13634 add_stmt (t);
13635 break;
13636
13637 case OMP_SECTION:
13638 case OMP_CRITICAL:
13639 case OMP_MASTER:
13640 case OMP_TASKGROUP:
13641 case OMP_ORDERED:
13642 stmt = push_stmt_list ();
13643 RECUR (OMP_BODY (t));
13644 stmt = pop_stmt_list (stmt);
13645
13646 t = copy_node (t);
13647 OMP_BODY (t) = stmt;
13648 add_stmt (t);
13649 break;
13650
13651 case OMP_ATOMIC:
13652 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13653 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13654 {
13655 tree op1 = TREE_OPERAND (t, 1);
13656 tree rhs1 = NULL_TREE;
13657 tree lhs, rhs;
13658 if (TREE_CODE (op1) == COMPOUND_EXPR)
13659 {
13660 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13661 op1 = TREE_OPERAND (op1, 1);
13662 }
13663 lhs = RECUR (TREE_OPERAND (op1, 0));
13664 rhs = RECUR (TREE_OPERAND (op1, 1));
13665 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13666 NULL_TREE, NULL_TREE, rhs1,
13667 OMP_ATOMIC_SEQ_CST (t));
13668 }
13669 else
13670 {
13671 tree op1 = TREE_OPERAND (t, 1);
13672 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13673 tree rhs1 = NULL_TREE;
13674 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13675 enum tree_code opcode = NOP_EXPR;
13676 if (code == OMP_ATOMIC_READ)
13677 {
13678 v = RECUR (TREE_OPERAND (op1, 0));
13679 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13680 }
13681 else if (code == OMP_ATOMIC_CAPTURE_OLD
13682 || code == OMP_ATOMIC_CAPTURE_NEW)
13683 {
13684 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13685 v = RECUR (TREE_OPERAND (op1, 0));
13686 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13687 if (TREE_CODE (op11) == COMPOUND_EXPR)
13688 {
13689 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13690 op11 = TREE_OPERAND (op11, 1);
13691 }
13692 lhs = RECUR (TREE_OPERAND (op11, 0));
13693 rhs = RECUR (TREE_OPERAND (op11, 1));
13694 opcode = TREE_CODE (op11);
13695 if (opcode == MODIFY_EXPR)
13696 opcode = NOP_EXPR;
13697 }
13698 else
13699 {
13700 code = OMP_ATOMIC;
13701 lhs = RECUR (TREE_OPERAND (op1, 0));
13702 rhs = RECUR (TREE_OPERAND (op1, 1));
13703 }
13704 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
13705 OMP_ATOMIC_SEQ_CST (t));
13706 }
13707 break;
13708
13709 case TRANSACTION_EXPR:
13710 {
13711 int flags = 0;
13712 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13713 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13714
13715 if (TRANSACTION_EXPR_IS_STMT (t))
13716 {
13717 tree body = TRANSACTION_EXPR_BODY (t);
13718 tree noex = NULL_TREE;
13719 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13720 {
13721 noex = MUST_NOT_THROW_COND (body);
13722 if (noex == NULL_TREE)
13723 noex = boolean_true_node;
13724 body = TREE_OPERAND (body, 0);
13725 }
13726 stmt = begin_transaction_stmt (input_location, NULL, flags);
13727 RECUR (body);
13728 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13729 }
13730 else
13731 {
13732 stmt = build_transaction_expr (EXPR_LOCATION (t),
13733 RECUR (TRANSACTION_EXPR_BODY (t)),
13734 flags, NULL_TREE);
13735 RETURN (stmt);
13736 }
13737 }
13738 break;
13739
13740 case MUST_NOT_THROW_EXPR:
13741 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13742 RECUR (MUST_NOT_THROW_COND (t))));
13743
13744 case EXPR_PACK_EXPANSION:
13745 error ("invalid use of pack expansion expression");
13746 RETURN (error_mark_node);
13747
13748 case NONTYPE_ARGUMENT_PACK:
13749 error ("use %<...%> to expand argument pack");
13750 RETURN (error_mark_node);
13751
13752 case COMPOUND_EXPR:
13753 tmp = RECUR (TREE_OPERAND (t, 0));
13754 if (tmp == NULL_TREE)
13755 /* If the first operand was a statement, we're done with it. */
13756 RETURN (RECUR (TREE_OPERAND (t, 1)));
13757 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13758 RECUR (TREE_OPERAND (t, 1)),
13759 complain));
13760
13761 default:
13762 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13763
13764 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13765 /*function_p=*/false,
13766 integral_constant_expression_p));
13767 }
13768
13769 RETURN (NULL_TREE);
13770 out:
13771 input_location = loc;
13772 return r;
13773 #undef RECUR
13774 #undef RETURN
13775 }
13776
13777 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13778 function. For description of the body see comment above
13779 cp_parser_omp_declare_reduction_exprs. */
13780
13781 static void
13782 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13783 {
13784 if (t == NULL_TREE || t == error_mark_node)
13785 return;
13786
13787 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
13788
13789 tree_stmt_iterator tsi;
13790 int i;
13791 tree stmts[7];
13792 memset (stmts, 0, sizeof stmts);
13793 for (i = 0, tsi = tsi_start (t);
13794 i < 7 && !tsi_end_p (tsi);
13795 i++, tsi_next (&tsi))
13796 stmts[i] = tsi_stmt (tsi);
13797 gcc_assert (tsi_end_p (tsi));
13798
13799 if (i >= 3)
13800 {
13801 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
13802 && TREE_CODE (stmts[1]) == DECL_EXPR);
13803 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
13804 args, complain, in_decl);
13805 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
13806 args, complain, in_decl);
13807 DECL_CONTEXT (omp_out) = current_function_decl;
13808 DECL_CONTEXT (omp_in) = current_function_decl;
13809 keep_next_level (true);
13810 tree block = begin_omp_structured_block ();
13811 tsubst_expr (stmts[2], args, complain, in_decl, false);
13812 block = finish_omp_structured_block (block);
13813 block = maybe_cleanup_point_expr_void (block);
13814 add_decl_expr (omp_out);
13815 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
13816 TREE_NO_WARNING (omp_out) = 1;
13817 add_decl_expr (omp_in);
13818 finish_expr_stmt (block);
13819 }
13820 if (i >= 6)
13821 {
13822 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
13823 && TREE_CODE (stmts[4]) == DECL_EXPR);
13824 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
13825 args, complain, in_decl);
13826 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
13827 args, complain, in_decl);
13828 DECL_CONTEXT (omp_priv) = current_function_decl;
13829 DECL_CONTEXT (omp_orig) = current_function_decl;
13830 keep_next_level (true);
13831 tree block = begin_omp_structured_block ();
13832 tsubst_expr (stmts[5], args, complain, in_decl, false);
13833 block = finish_omp_structured_block (block);
13834 block = maybe_cleanup_point_expr_void (block);
13835 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
13836 add_decl_expr (omp_priv);
13837 add_decl_expr (omp_orig);
13838 finish_expr_stmt (block);
13839 if (i == 7)
13840 add_decl_expr (omp_orig);
13841 }
13842 }
13843
13844 /* T is a postfix-expression that is not being used in a function
13845 call. Return the substituted version of T. */
13846
13847 static tree
13848 tsubst_non_call_postfix_expression (tree t, tree args,
13849 tsubst_flags_t complain,
13850 tree in_decl)
13851 {
13852 if (TREE_CODE (t) == SCOPE_REF)
13853 t = tsubst_qualified_id (t, args, complain, in_decl,
13854 /*done=*/false, /*address_p=*/false);
13855 else
13856 t = tsubst_copy_and_build (t, args, complain, in_decl,
13857 /*function_p=*/false,
13858 /*integral_constant_expression_p=*/false);
13859
13860 return t;
13861 }
13862
13863 /* Like tsubst but deals with expressions and performs semantic
13864 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13865
13866 tree
13867 tsubst_copy_and_build (tree t,
13868 tree args,
13869 tsubst_flags_t complain,
13870 tree in_decl,
13871 bool function_p,
13872 bool integral_constant_expression_p)
13873 {
13874 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13875 #define RECUR(NODE) \
13876 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13877 /*function_p=*/false, \
13878 integral_constant_expression_p)
13879
13880 tree retval, op1;
13881 location_t loc;
13882
13883 if (t == NULL_TREE || t == error_mark_node)
13884 return t;
13885
13886 loc = input_location;
13887 if (EXPR_HAS_LOCATION (t))
13888 input_location = EXPR_LOCATION (t);
13889
13890 /* N3276 decltype magic only applies to calls at the top level or on the
13891 right side of a comma. */
13892 tsubst_flags_t decltype_flag = (complain & tf_decltype);
13893 complain &= ~tf_decltype;
13894
13895 switch (TREE_CODE (t))
13896 {
13897 case USING_DECL:
13898 t = DECL_NAME (t);
13899 /* Fall through. */
13900 case IDENTIFIER_NODE:
13901 {
13902 tree decl;
13903 cp_id_kind idk;
13904 bool non_integral_constant_expression_p;
13905 const char *error_msg;
13906
13907 if (IDENTIFIER_TYPENAME_P (t))
13908 {
13909 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13910 t = mangle_conv_op_name_for_type (new_type);
13911 }
13912
13913 /* Look up the name. */
13914 decl = lookup_name (t);
13915
13916 /* By convention, expressions use ERROR_MARK_NODE to indicate
13917 failure, not NULL_TREE. */
13918 if (decl == NULL_TREE)
13919 decl = error_mark_node;
13920
13921 decl = finish_id_expression (t, decl, NULL_TREE,
13922 &idk,
13923 integral_constant_expression_p,
13924 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
13925 &non_integral_constant_expression_p,
13926 /*template_p=*/false,
13927 /*done=*/true,
13928 /*address_p=*/false,
13929 /*template_arg_p=*/false,
13930 &error_msg,
13931 input_location);
13932 if (error_msg)
13933 error (error_msg);
13934 if (!function_p && identifier_p (decl))
13935 {
13936 if (complain & tf_error)
13937 unqualified_name_lookup_error (decl);
13938 decl = error_mark_node;
13939 }
13940 RETURN (decl);
13941 }
13942
13943 case TEMPLATE_ID_EXPR:
13944 {
13945 tree object;
13946 tree templ = RECUR (TREE_OPERAND (t, 0));
13947 tree targs = TREE_OPERAND (t, 1);
13948
13949 if (targs)
13950 targs = tsubst_template_args (targs, args, complain, in_decl);
13951
13952 if (TREE_CODE (templ) == COMPONENT_REF)
13953 {
13954 object = TREE_OPERAND (templ, 0);
13955 templ = TREE_OPERAND (templ, 1);
13956 }
13957 else
13958 object = NULL_TREE;
13959 templ = lookup_template_function (templ, targs);
13960
13961 if (object)
13962 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
13963 object, templ, NULL_TREE));
13964 else
13965 RETURN (baselink_for_fns (templ));
13966 }
13967
13968 case INDIRECT_REF:
13969 {
13970 tree r = RECUR (TREE_OPERAND (t, 0));
13971
13972 if (REFERENCE_REF_P (t))
13973 {
13974 /* A type conversion to reference type will be enclosed in
13975 such an indirect ref, but the substitution of the cast
13976 will have also added such an indirect ref. */
13977 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
13978 r = convert_from_reference (r);
13979 }
13980 else
13981 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
13982 complain|decltype_flag);
13983 RETURN (r);
13984 }
13985
13986 case NOP_EXPR:
13987 RETURN (build_nop
13988 (tsubst (TREE_TYPE (t), args, complain, in_decl),
13989 RECUR (TREE_OPERAND (t, 0))));
13990
13991 case IMPLICIT_CONV_EXPR:
13992 {
13993 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13994 tree expr = RECUR (TREE_OPERAND (t, 0));
13995 int flags = LOOKUP_IMPLICIT;
13996 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
13997 flags = LOOKUP_NORMAL;
13998 RETURN (perform_implicit_conversion_flags (type, expr, complain,
13999 flags));
14000 }
14001
14002 case CONVERT_EXPR:
14003 RETURN (build1
14004 (CONVERT_EXPR,
14005 tsubst (TREE_TYPE (t), args, complain, in_decl),
14006 RECUR (TREE_OPERAND (t, 0))));
14007
14008 case CAST_EXPR:
14009 case REINTERPRET_CAST_EXPR:
14010 case CONST_CAST_EXPR:
14011 case DYNAMIC_CAST_EXPR:
14012 case STATIC_CAST_EXPR:
14013 {
14014 tree type;
14015 tree op, r = NULL_TREE;
14016
14017 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14018 if (integral_constant_expression_p
14019 && !cast_valid_in_integral_constant_expression_p (type))
14020 {
14021 if (complain & tf_error)
14022 error ("a cast to a type other than an integral or "
14023 "enumeration type cannot appear in a constant-expression");
14024 RETURN (error_mark_node);
14025 }
14026
14027 op = RECUR (TREE_OPERAND (t, 0));
14028
14029 ++c_inhibit_evaluation_warnings;
14030 switch (TREE_CODE (t))
14031 {
14032 case CAST_EXPR:
14033 r = build_functional_cast (type, op, complain);
14034 break;
14035 case REINTERPRET_CAST_EXPR:
14036 r = build_reinterpret_cast (type, op, complain);
14037 break;
14038 case CONST_CAST_EXPR:
14039 r = build_const_cast (type, op, complain);
14040 break;
14041 case DYNAMIC_CAST_EXPR:
14042 r = build_dynamic_cast (type, op, complain);
14043 break;
14044 case STATIC_CAST_EXPR:
14045 r = build_static_cast (type, op, complain);
14046 break;
14047 default:
14048 gcc_unreachable ();
14049 }
14050 --c_inhibit_evaluation_warnings;
14051
14052 RETURN (r);
14053 }
14054
14055 case POSTDECREMENT_EXPR:
14056 case POSTINCREMENT_EXPR:
14057 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14058 args, complain, in_decl);
14059 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14060 complain|decltype_flag));
14061
14062 case PREDECREMENT_EXPR:
14063 case PREINCREMENT_EXPR:
14064 case NEGATE_EXPR:
14065 case BIT_NOT_EXPR:
14066 case ABS_EXPR:
14067 case TRUTH_NOT_EXPR:
14068 case UNARY_PLUS_EXPR: /* Unary + */
14069 case REALPART_EXPR:
14070 case IMAGPART_EXPR:
14071 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14072 RECUR (TREE_OPERAND (t, 0)),
14073 complain|decltype_flag));
14074
14075 case FIX_TRUNC_EXPR:
14076 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14077 0, complain));
14078
14079 case ADDR_EXPR:
14080 op1 = TREE_OPERAND (t, 0);
14081 if (TREE_CODE (op1) == LABEL_DECL)
14082 RETURN (finish_label_address_expr (DECL_NAME (op1),
14083 EXPR_LOCATION (op1)));
14084 if (TREE_CODE (op1) == SCOPE_REF)
14085 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14086 /*done=*/true, /*address_p=*/true);
14087 else
14088 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14089 in_decl);
14090 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14091 complain|decltype_flag));
14092
14093 case PLUS_EXPR:
14094 case MINUS_EXPR:
14095 case MULT_EXPR:
14096 case TRUNC_DIV_EXPR:
14097 case CEIL_DIV_EXPR:
14098 case FLOOR_DIV_EXPR:
14099 case ROUND_DIV_EXPR:
14100 case EXACT_DIV_EXPR:
14101 case BIT_AND_EXPR:
14102 case BIT_IOR_EXPR:
14103 case BIT_XOR_EXPR:
14104 case TRUNC_MOD_EXPR:
14105 case FLOOR_MOD_EXPR:
14106 case TRUTH_ANDIF_EXPR:
14107 case TRUTH_ORIF_EXPR:
14108 case TRUTH_AND_EXPR:
14109 case TRUTH_OR_EXPR:
14110 case RSHIFT_EXPR:
14111 case LSHIFT_EXPR:
14112 case RROTATE_EXPR:
14113 case LROTATE_EXPR:
14114 case EQ_EXPR:
14115 case NE_EXPR:
14116 case MAX_EXPR:
14117 case MIN_EXPR:
14118 case LE_EXPR:
14119 case GE_EXPR:
14120 case LT_EXPR:
14121 case GT_EXPR:
14122 case MEMBER_REF:
14123 case DOTSTAR_EXPR:
14124 {
14125 tree r;
14126
14127 ++c_inhibit_evaluation_warnings;
14128
14129 r = build_x_binary_op
14130 (input_location, TREE_CODE (t),
14131 RECUR (TREE_OPERAND (t, 0)),
14132 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14133 ? ERROR_MARK
14134 : TREE_CODE (TREE_OPERAND (t, 0))),
14135 RECUR (TREE_OPERAND (t, 1)),
14136 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14137 ? ERROR_MARK
14138 : TREE_CODE (TREE_OPERAND (t, 1))),
14139 /*overload=*/NULL,
14140 complain|decltype_flag);
14141 if (EXPR_P (r) && TREE_NO_WARNING (t))
14142 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14143
14144 --c_inhibit_evaluation_warnings;
14145
14146 RETURN (r);
14147 }
14148
14149 case SCOPE_REF:
14150 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14151 /*address_p=*/false));
14152 case ARRAY_REF:
14153 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14154 args, complain, in_decl);
14155 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14156 RECUR (TREE_OPERAND (t, 1)),
14157 complain|decltype_flag));
14158
14159 case ARRAY_NOTATION_REF:
14160 {
14161 tree start_index, length, stride;
14162 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14163 args, complain, in_decl);
14164 start_index = RECUR (ARRAY_NOTATION_START (t));
14165 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14166 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14167 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14168 length, stride, TREE_TYPE (op1)));
14169 }
14170 case SIZEOF_EXPR:
14171 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14172 RETURN (tsubst_copy (t, args, complain, in_decl));
14173 /* Fall through */
14174
14175 case ALIGNOF_EXPR:
14176 {
14177 tree r;
14178
14179 op1 = TREE_OPERAND (t, 0);
14180 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14181 op1 = TREE_TYPE (op1);
14182 if (!args)
14183 {
14184 /* When there are no ARGS, we are trying to evaluate a
14185 non-dependent expression from the parser. Trying to do
14186 the substitutions may not work. */
14187 if (!TYPE_P (op1))
14188 op1 = TREE_TYPE (op1);
14189 }
14190 else
14191 {
14192 ++cp_unevaluated_operand;
14193 ++c_inhibit_evaluation_warnings;
14194 if (TYPE_P (op1))
14195 op1 = tsubst (op1, args, complain, in_decl);
14196 else
14197 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14198 /*function_p=*/false,
14199 /*integral_constant_expression_p=*/
14200 false);
14201 --cp_unevaluated_operand;
14202 --c_inhibit_evaluation_warnings;
14203 }
14204 if (TYPE_P (op1))
14205 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14206 complain & tf_error);
14207 else
14208 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14209 complain & tf_error);
14210 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14211 {
14212 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14213 {
14214 if (!processing_template_decl && TYPE_P (op1))
14215 {
14216 r = build_min (SIZEOF_EXPR, size_type_node,
14217 build1 (NOP_EXPR, op1, error_mark_node));
14218 SIZEOF_EXPR_TYPE_P (r) = 1;
14219 }
14220 else
14221 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14222 TREE_SIDE_EFFECTS (r) = 0;
14223 TREE_READONLY (r) = 1;
14224 }
14225 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14226 }
14227 RETURN (r);
14228 }
14229
14230 case AT_ENCODE_EXPR:
14231 {
14232 op1 = TREE_OPERAND (t, 0);
14233 ++cp_unevaluated_operand;
14234 ++c_inhibit_evaluation_warnings;
14235 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14236 /*function_p=*/false,
14237 /*integral_constant_expression_p=*/false);
14238 --cp_unevaluated_operand;
14239 --c_inhibit_evaluation_warnings;
14240 RETURN (objc_build_encode_expr (op1));
14241 }
14242
14243 case NOEXCEPT_EXPR:
14244 op1 = TREE_OPERAND (t, 0);
14245 ++cp_unevaluated_operand;
14246 ++c_inhibit_evaluation_warnings;
14247 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14248 /*function_p=*/false,
14249 /*integral_constant_expression_p=*/false);
14250 --cp_unevaluated_operand;
14251 --c_inhibit_evaluation_warnings;
14252 RETURN (finish_noexcept_expr (op1, complain));
14253
14254 case MODOP_EXPR:
14255 {
14256 tree r;
14257
14258 ++c_inhibit_evaluation_warnings;
14259
14260 r = build_x_modify_expr
14261 (EXPR_LOCATION (t),
14262 RECUR (TREE_OPERAND (t, 0)),
14263 TREE_CODE (TREE_OPERAND (t, 1)),
14264 RECUR (TREE_OPERAND (t, 2)),
14265 complain|decltype_flag);
14266 /* TREE_NO_WARNING must be set if either the expression was
14267 parenthesized or it uses an operator such as >>= rather
14268 than plain assignment. In the former case, it was already
14269 set and must be copied. In the latter case,
14270 build_x_modify_expr sets it and it must not be reset
14271 here. */
14272 if (TREE_NO_WARNING (t))
14273 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14274
14275 --c_inhibit_evaluation_warnings;
14276
14277 RETURN (r);
14278 }
14279
14280 case ARROW_EXPR:
14281 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14282 args, complain, in_decl);
14283 /* Remember that there was a reference to this entity. */
14284 if (DECL_P (op1))
14285 mark_used (op1);
14286 RETURN (build_x_arrow (input_location, op1, complain));
14287
14288 case NEW_EXPR:
14289 {
14290 tree placement = RECUR (TREE_OPERAND (t, 0));
14291 tree init = RECUR (TREE_OPERAND (t, 3));
14292 vec<tree, va_gc> *placement_vec;
14293 vec<tree, va_gc> *init_vec;
14294 tree ret;
14295
14296 if (placement == NULL_TREE)
14297 placement_vec = NULL;
14298 else
14299 {
14300 placement_vec = make_tree_vector ();
14301 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14302 vec_safe_push (placement_vec, TREE_VALUE (placement));
14303 }
14304
14305 /* If there was an initializer in the original tree, but it
14306 instantiated to an empty list, then we should pass a
14307 non-NULL empty vector to tell build_new that it was an
14308 empty initializer() rather than no initializer. This can
14309 only happen when the initializer is a pack expansion whose
14310 parameter packs are of length zero. */
14311 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14312 init_vec = NULL;
14313 else
14314 {
14315 init_vec = make_tree_vector ();
14316 if (init == void_zero_node)
14317 gcc_assert (init_vec != NULL);
14318 else
14319 {
14320 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14321 vec_safe_push (init_vec, TREE_VALUE (init));
14322 }
14323 }
14324
14325 ret = build_new (&placement_vec,
14326 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14327 RECUR (TREE_OPERAND (t, 2)),
14328 &init_vec,
14329 NEW_EXPR_USE_GLOBAL (t),
14330 complain);
14331
14332 if (placement_vec != NULL)
14333 release_tree_vector (placement_vec);
14334 if (init_vec != NULL)
14335 release_tree_vector (init_vec);
14336
14337 RETURN (ret);
14338 }
14339
14340 case DELETE_EXPR:
14341 RETURN (delete_sanity
14342 (RECUR (TREE_OPERAND (t, 0)),
14343 RECUR (TREE_OPERAND (t, 1)),
14344 DELETE_EXPR_USE_VEC (t),
14345 DELETE_EXPR_USE_GLOBAL (t),
14346 complain));
14347
14348 case COMPOUND_EXPR:
14349 {
14350 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14351 complain & ~tf_decltype, in_decl,
14352 /*function_p=*/false,
14353 integral_constant_expression_p);
14354 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14355 op0,
14356 RECUR (TREE_OPERAND (t, 1)),
14357 complain|decltype_flag));
14358 }
14359
14360 case CALL_EXPR:
14361 {
14362 tree function;
14363 vec<tree, va_gc> *call_args;
14364 unsigned int nargs, i;
14365 bool qualified_p;
14366 bool koenig_p;
14367 tree ret;
14368
14369 function = CALL_EXPR_FN (t);
14370 /* When we parsed the expression, we determined whether or
14371 not Koenig lookup should be performed. */
14372 koenig_p = KOENIG_LOOKUP_P (t);
14373 if (TREE_CODE (function) == SCOPE_REF)
14374 {
14375 qualified_p = true;
14376 function = tsubst_qualified_id (function, args, complain, in_decl,
14377 /*done=*/false,
14378 /*address_p=*/false);
14379 }
14380 else if (koenig_p && identifier_p (function))
14381 {
14382 /* Do nothing; calling tsubst_copy_and_build on an identifier
14383 would incorrectly perform unqualified lookup again.
14384
14385 Note that we can also have an IDENTIFIER_NODE if the earlier
14386 unqualified lookup found a member function; in that case
14387 koenig_p will be false and we do want to do the lookup
14388 again to find the instantiated member function.
14389
14390 FIXME but doing that causes c++/15272, so we need to stop
14391 using IDENTIFIER_NODE in that situation. */
14392 qualified_p = false;
14393 }
14394 else
14395 {
14396 if (TREE_CODE (function) == COMPONENT_REF)
14397 {
14398 tree op = TREE_OPERAND (function, 1);
14399
14400 qualified_p = (TREE_CODE (op) == SCOPE_REF
14401 || (BASELINK_P (op)
14402 && BASELINK_QUALIFIED_P (op)));
14403 }
14404 else
14405 qualified_p = false;
14406
14407 if (TREE_CODE (function) == ADDR_EXPR
14408 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14409 /* Avoid error about taking the address of a constructor. */
14410 function = TREE_OPERAND (function, 0);
14411
14412 function = tsubst_copy_and_build (function, args, complain,
14413 in_decl,
14414 !qualified_p,
14415 integral_constant_expression_p);
14416
14417 if (BASELINK_P (function))
14418 qualified_p = true;
14419 }
14420
14421 nargs = call_expr_nargs (t);
14422 call_args = make_tree_vector ();
14423 for (i = 0; i < nargs; ++i)
14424 {
14425 tree arg = CALL_EXPR_ARG (t, i);
14426
14427 if (!PACK_EXPANSION_P (arg))
14428 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14429 else
14430 {
14431 /* Expand the pack expansion and push each entry onto
14432 CALL_ARGS. */
14433 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14434 if (TREE_CODE (arg) == TREE_VEC)
14435 {
14436 unsigned int len, j;
14437
14438 len = TREE_VEC_LENGTH (arg);
14439 for (j = 0; j < len; ++j)
14440 {
14441 tree value = TREE_VEC_ELT (arg, j);
14442 if (value != NULL_TREE)
14443 value = convert_from_reference (value);
14444 vec_safe_push (call_args, value);
14445 }
14446 }
14447 else
14448 {
14449 /* A partial substitution. Add one entry. */
14450 vec_safe_push (call_args, arg);
14451 }
14452 }
14453 }
14454
14455 /* We do not perform argument-dependent lookup if normal
14456 lookup finds a non-function, in accordance with the
14457 expected resolution of DR 218. */
14458 if (koenig_p
14459 && ((is_overloaded_fn (function)
14460 /* If lookup found a member function, the Koenig lookup is
14461 not appropriate, even if an unqualified-name was used
14462 to denote the function. */
14463 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14464 || identifier_p (function))
14465 /* Only do this when substitution turns a dependent call
14466 into a non-dependent call. */
14467 && type_dependent_expression_p_push (t)
14468 && !any_type_dependent_arguments_p (call_args))
14469 function = perform_koenig_lookup (function, call_args, false,
14470 tf_none);
14471
14472 if (identifier_p (function)
14473 && !any_type_dependent_arguments_p (call_args))
14474 {
14475 if (koenig_p && (complain & tf_warning_or_error))
14476 {
14477 /* For backwards compatibility and good diagnostics, try
14478 the unqualified lookup again if we aren't in SFINAE
14479 context. */
14480 tree unq = (tsubst_copy_and_build
14481 (function, args, complain, in_decl, true,
14482 integral_constant_expression_p));
14483 if (unq == error_mark_node)
14484 RETURN (error_mark_node);
14485
14486 if (unq != function)
14487 {
14488 tree fn = unq;
14489 if (INDIRECT_REF_P (fn))
14490 fn = TREE_OPERAND (fn, 0);
14491 if (TREE_CODE (fn) == COMPONENT_REF)
14492 fn = TREE_OPERAND (fn, 1);
14493 if (is_overloaded_fn (fn))
14494 fn = get_first_fn (fn);
14495 if (permerror (EXPR_LOC_OR_HERE (t),
14496 "%qD was not declared in this scope, "
14497 "and no declarations were found by "
14498 "argument-dependent lookup at the point "
14499 "of instantiation", function))
14500 {
14501 if (!DECL_P (fn))
14502 /* Can't say anything more. */;
14503 else if (DECL_CLASS_SCOPE_P (fn))
14504 {
14505 inform (EXPR_LOC_OR_HERE (t),
14506 "declarations in dependent base %qT are "
14507 "not found by unqualified lookup",
14508 DECL_CLASS_CONTEXT (fn));
14509 if (current_class_ptr)
14510 inform (EXPR_LOC_OR_HERE (t),
14511 "use %<this->%D%> instead", function);
14512 else
14513 inform (EXPR_LOC_OR_HERE (t),
14514 "use %<%T::%D%> instead",
14515 current_class_name, function);
14516 }
14517 else
14518 inform (0, "%q+D declared here, later in the "
14519 "translation unit", fn);
14520 }
14521 function = unq;
14522 }
14523 }
14524 if (identifier_p (function))
14525 {
14526 if (complain & tf_error)
14527 unqualified_name_lookup_error (function);
14528 release_tree_vector (call_args);
14529 RETURN (error_mark_node);
14530 }
14531 }
14532
14533 /* Remember that there was a reference to this entity. */
14534 if (DECL_P (function))
14535 mark_used (function);
14536
14537 /* Put back tf_decltype for the actual call. */
14538 complain |= decltype_flag;
14539
14540 if (TREE_CODE (function) == OFFSET_REF)
14541 ret = build_offset_ref_call_from_tree (function, &call_args,
14542 complain);
14543 else if (TREE_CODE (function) == COMPONENT_REF)
14544 {
14545 tree instance = TREE_OPERAND (function, 0);
14546 tree fn = TREE_OPERAND (function, 1);
14547
14548 if (processing_template_decl
14549 && (type_dependent_expression_p (instance)
14550 || (!BASELINK_P (fn)
14551 && TREE_CODE (fn) != FIELD_DECL)
14552 || type_dependent_expression_p (fn)
14553 || any_type_dependent_arguments_p (call_args)))
14554 ret = build_nt_call_vec (function, call_args);
14555 else if (!BASELINK_P (fn))
14556 ret = finish_call_expr (function, &call_args,
14557 /*disallow_virtual=*/false,
14558 /*koenig_p=*/false,
14559 complain);
14560 else
14561 ret = (build_new_method_call
14562 (instance, fn,
14563 &call_args, NULL_TREE,
14564 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14565 /*fn_p=*/NULL,
14566 complain));
14567 }
14568 else
14569 ret = finish_call_expr (function, &call_args,
14570 /*disallow_virtual=*/qualified_p,
14571 koenig_p,
14572 complain);
14573
14574 release_tree_vector (call_args);
14575
14576 RETURN (ret);
14577 }
14578
14579 case COND_EXPR:
14580 {
14581 tree cond = RECUR (TREE_OPERAND (t, 0));
14582 tree exp1, exp2;
14583
14584 if (TREE_CODE (cond) == INTEGER_CST)
14585 {
14586 if (integer_zerop (cond))
14587 {
14588 ++c_inhibit_evaluation_warnings;
14589 exp1 = RECUR (TREE_OPERAND (t, 1));
14590 --c_inhibit_evaluation_warnings;
14591 exp2 = RECUR (TREE_OPERAND (t, 2));
14592 }
14593 else
14594 {
14595 exp1 = RECUR (TREE_OPERAND (t, 1));
14596 ++c_inhibit_evaluation_warnings;
14597 exp2 = RECUR (TREE_OPERAND (t, 2));
14598 --c_inhibit_evaluation_warnings;
14599 }
14600 }
14601 else
14602 {
14603 exp1 = RECUR (TREE_OPERAND (t, 1));
14604 exp2 = RECUR (TREE_OPERAND (t, 2));
14605 }
14606
14607 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14608 cond, exp1, exp2, complain));
14609 }
14610
14611 case PSEUDO_DTOR_EXPR:
14612 RETURN (finish_pseudo_destructor_expr
14613 (RECUR (TREE_OPERAND (t, 0)),
14614 RECUR (TREE_OPERAND (t, 1)),
14615 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14616 input_location));
14617
14618 case TREE_LIST:
14619 {
14620 tree purpose, value, chain;
14621
14622 if (t == void_list_node)
14623 RETURN (t);
14624
14625 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14626 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14627 {
14628 /* We have pack expansions, so expand those and
14629 create a new list out of it. */
14630 tree purposevec = NULL_TREE;
14631 tree valuevec = NULL_TREE;
14632 tree chain;
14633 int i, len = -1;
14634
14635 /* Expand the argument expressions. */
14636 if (TREE_PURPOSE (t))
14637 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14638 complain, in_decl);
14639 if (TREE_VALUE (t))
14640 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14641 complain, in_decl);
14642
14643 /* Build the rest of the list. */
14644 chain = TREE_CHAIN (t);
14645 if (chain && chain != void_type_node)
14646 chain = RECUR (chain);
14647
14648 /* Determine the number of arguments. */
14649 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14650 {
14651 len = TREE_VEC_LENGTH (purposevec);
14652 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14653 }
14654 else if (TREE_CODE (valuevec) == TREE_VEC)
14655 len = TREE_VEC_LENGTH (valuevec);
14656 else
14657 {
14658 /* Since we only performed a partial substitution into
14659 the argument pack, we only RETURN (a single list
14660 node. */
14661 if (purposevec == TREE_PURPOSE (t)
14662 && valuevec == TREE_VALUE (t)
14663 && chain == TREE_CHAIN (t))
14664 RETURN (t);
14665
14666 RETURN (tree_cons (purposevec, valuevec, chain));
14667 }
14668
14669 /* Convert the argument vectors into a TREE_LIST */
14670 i = len;
14671 while (i > 0)
14672 {
14673 /* Grab the Ith values. */
14674 i--;
14675 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14676 : NULL_TREE;
14677 value
14678 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14679 : NULL_TREE;
14680
14681 /* Build the list (backwards). */
14682 chain = tree_cons (purpose, value, chain);
14683 }
14684
14685 RETURN (chain);
14686 }
14687
14688 purpose = TREE_PURPOSE (t);
14689 if (purpose)
14690 purpose = RECUR (purpose);
14691 value = TREE_VALUE (t);
14692 if (value)
14693 value = RECUR (value);
14694 chain = TREE_CHAIN (t);
14695 if (chain && chain != void_type_node)
14696 chain = RECUR (chain);
14697 if (purpose == TREE_PURPOSE (t)
14698 && value == TREE_VALUE (t)
14699 && chain == TREE_CHAIN (t))
14700 RETURN (t);
14701 RETURN (tree_cons (purpose, value, chain));
14702 }
14703
14704 case COMPONENT_REF:
14705 {
14706 tree object;
14707 tree object_type;
14708 tree member;
14709
14710 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14711 args, complain, in_decl);
14712 /* Remember that there was a reference to this entity. */
14713 if (DECL_P (object))
14714 mark_used (object);
14715 object_type = TREE_TYPE (object);
14716
14717 member = TREE_OPERAND (t, 1);
14718 if (BASELINK_P (member))
14719 member = tsubst_baselink (member,
14720 non_reference (TREE_TYPE (object)),
14721 args, complain, in_decl);
14722 else
14723 member = tsubst_copy (member, args, complain, in_decl);
14724 if (member == error_mark_node)
14725 RETURN (error_mark_node);
14726
14727 if (type_dependent_expression_p (object))
14728 /* We can't do much here. */;
14729 else if (!CLASS_TYPE_P (object_type))
14730 {
14731 if (scalarish_type_p (object_type))
14732 {
14733 tree s = NULL_TREE;
14734 tree dtor = member;
14735
14736 if (TREE_CODE (dtor) == SCOPE_REF)
14737 {
14738 s = TREE_OPERAND (dtor, 0);
14739 dtor = TREE_OPERAND (dtor, 1);
14740 }
14741 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14742 {
14743 dtor = TREE_OPERAND (dtor, 0);
14744 if (TYPE_P (dtor))
14745 RETURN (finish_pseudo_destructor_expr
14746 (object, s, dtor, input_location));
14747 }
14748 }
14749 }
14750 else if (TREE_CODE (member) == SCOPE_REF
14751 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14752 {
14753 /* Lookup the template functions now that we know what the
14754 scope is. */
14755 tree scope = TREE_OPERAND (member, 0);
14756 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14757 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14758 member = lookup_qualified_name (scope, tmpl,
14759 /*is_type_p=*/false,
14760 /*complain=*/false);
14761 if (BASELINK_P (member))
14762 {
14763 BASELINK_FUNCTIONS (member)
14764 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14765 args);
14766 member = (adjust_result_of_qualified_name_lookup
14767 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14768 object_type));
14769 }
14770 else
14771 {
14772 qualified_name_lookup_error (scope, tmpl, member,
14773 input_location);
14774 RETURN (error_mark_node);
14775 }
14776 }
14777 else if (TREE_CODE (member) == SCOPE_REF
14778 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14779 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14780 {
14781 if (complain & tf_error)
14782 {
14783 if (TYPE_P (TREE_OPERAND (member, 0)))
14784 error ("%qT is not a class or namespace",
14785 TREE_OPERAND (member, 0));
14786 else
14787 error ("%qD is not a class or namespace",
14788 TREE_OPERAND (member, 0));
14789 }
14790 RETURN (error_mark_node);
14791 }
14792 else if (TREE_CODE (member) == FIELD_DECL)
14793 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14794
14795 RETURN (finish_class_member_access_expr (object, member,
14796 /*template_p=*/false,
14797 complain));
14798 }
14799
14800 case THROW_EXPR:
14801 RETURN (build_throw
14802 (RECUR (TREE_OPERAND (t, 0))));
14803
14804 case CONSTRUCTOR:
14805 {
14806 vec<constructor_elt, va_gc> *n;
14807 constructor_elt *ce;
14808 unsigned HOST_WIDE_INT idx;
14809 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14810 bool process_index_p;
14811 int newlen;
14812 bool need_copy_p = false;
14813 tree r;
14814
14815 if (type == error_mark_node)
14816 RETURN (error_mark_node);
14817
14818 /* digest_init will do the wrong thing if we let it. */
14819 if (type && TYPE_PTRMEMFUNC_P (type))
14820 RETURN (t);
14821
14822 /* We do not want to process the index of aggregate
14823 initializers as they are identifier nodes which will be
14824 looked up by digest_init. */
14825 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14826
14827 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14828 newlen = vec_safe_length (n);
14829 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14830 {
14831 if (ce->index && process_index_p
14832 /* An identifier index is looked up in the type
14833 being initialized, not the current scope. */
14834 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
14835 ce->index = RECUR (ce->index);
14836
14837 if (PACK_EXPANSION_P (ce->value))
14838 {
14839 /* Substitute into the pack expansion. */
14840 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14841 in_decl);
14842
14843 if (ce->value == error_mark_node
14844 || PACK_EXPANSION_P (ce->value))
14845 ;
14846 else if (TREE_VEC_LENGTH (ce->value) == 1)
14847 /* Just move the argument into place. */
14848 ce->value = TREE_VEC_ELT (ce->value, 0);
14849 else
14850 {
14851 /* Update the length of the final CONSTRUCTOR
14852 arguments vector, and note that we will need to
14853 copy.*/
14854 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14855 need_copy_p = true;
14856 }
14857 }
14858 else
14859 ce->value = RECUR (ce->value);
14860 }
14861
14862 if (need_copy_p)
14863 {
14864 vec<constructor_elt, va_gc> *old_n = n;
14865
14866 vec_alloc (n, newlen);
14867 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14868 {
14869 if (TREE_CODE (ce->value) == TREE_VEC)
14870 {
14871 int i, len = TREE_VEC_LENGTH (ce->value);
14872 for (i = 0; i < len; ++i)
14873 CONSTRUCTOR_APPEND_ELT (n, 0,
14874 TREE_VEC_ELT (ce->value, i));
14875 }
14876 else
14877 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14878 }
14879 }
14880
14881 r = build_constructor (init_list_type_node, n);
14882 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14883
14884 if (TREE_HAS_CONSTRUCTOR (t))
14885 RETURN (finish_compound_literal (type, r, complain));
14886
14887 TREE_TYPE (r) = type;
14888 RETURN (r);
14889 }
14890
14891 case TYPEID_EXPR:
14892 {
14893 tree operand_0 = TREE_OPERAND (t, 0);
14894 if (TYPE_P (operand_0))
14895 {
14896 operand_0 = tsubst (operand_0, args, complain, in_decl);
14897 RETURN (get_typeid (operand_0, complain));
14898 }
14899 else
14900 {
14901 operand_0 = RECUR (operand_0);
14902 RETURN (build_typeid (operand_0, complain));
14903 }
14904 }
14905
14906 case VAR_DECL:
14907 if (!args)
14908 RETURN (t);
14909 else if (DECL_PACK_P (t))
14910 {
14911 /* We don't build decls for an instantiation of a
14912 variadic capture proxy, we instantiate the elements
14913 when needed. */
14914 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
14915 return RECUR (DECL_VALUE_EXPR (t));
14916 }
14917 /* Fall through */
14918
14919 case PARM_DECL:
14920 {
14921 tree r = tsubst_copy (t, args, complain, in_decl);
14922
14923 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14924 /* If the original type was a reference, we'll be wrapped in
14925 the appropriate INDIRECT_REF. */
14926 r = convert_from_reference (r);
14927 RETURN (r);
14928 }
14929
14930 case VA_ARG_EXPR:
14931 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14932 RECUR (TREE_OPERAND (t, 0)),
14933 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14934
14935 case OFFSETOF_EXPR:
14936 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14937
14938 case TRAIT_EXPR:
14939 {
14940 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14941 complain, in_decl);
14942
14943 tree type2 = TRAIT_EXPR_TYPE2 (t);
14944 if (type2)
14945 type2 = tsubst_copy (type2, args, complain, in_decl);
14946
14947 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14948 }
14949
14950 case STMT_EXPR:
14951 {
14952 tree old_stmt_expr = cur_stmt_expr;
14953 tree stmt_expr = begin_stmt_expr ();
14954
14955 cur_stmt_expr = stmt_expr;
14956 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14957 integral_constant_expression_p);
14958 stmt_expr = finish_stmt_expr (stmt_expr, false);
14959 cur_stmt_expr = old_stmt_expr;
14960
14961 /* If the resulting list of expression statement is empty,
14962 fold it further into void_zero_node. */
14963 if (empty_expr_stmt_p (stmt_expr))
14964 stmt_expr = void_zero_node;
14965
14966 RETURN (stmt_expr);
14967 }
14968
14969 case LAMBDA_EXPR:
14970 {
14971 tree r = build_lambda_expr ();
14972
14973 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14974 LAMBDA_EXPR_CLOSURE (r) = type;
14975 CLASSTYPE_LAMBDA_EXPR (type) = r;
14976
14977 LAMBDA_EXPR_LOCATION (r)
14978 = LAMBDA_EXPR_LOCATION (t);
14979 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14980 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14981 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14982 LAMBDA_EXPR_DISCRIMINATOR (r)
14983 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14984 /* For a function scope, we want to use tsubst so that we don't
14985 complain about referring to an auto function before its return
14986 type has been deduced. Otherwise, we want to use tsubst_copy so
14987 that we look up the existing field/parameter/variable rather
14988 than build a new one. */
14989 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
14990 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
14991 scope = tsubst (scope, args, complain, in_decl);
14992 else if (scope && TREE_CODE (scope) == PARM_DECL)
14993 {
14994 /* Look up the parameter we want directly, as tsubst_copy
14995 doesn't do what we need. */
14996 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
14997 tree parm = FUNCTION_FIRST_USER_PARM (fn);
14998 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
14999 parm = DECL_CHAIN (parm);
15000 scope = parm;
15001 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15002 if (DECL_CONTEXT (scope) == NULL_TREE)
15003 DECL_CONTEXT (scope) = fn;
15004 }
15005 else
15006 scope = RECUR (scope);
15007 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15008 LAMBDA_EXPR_RETURN_TYPE (r)
15009 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15010
15011 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15012 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15013
15014 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15015 determine_visibility (TYPE_NAME (type));
15016 /* Now that we know visibility, instantiate the type so we have a
15017 declaration of the op() for later calls to lambda_function. */
15018 complete_type (type);
15019
15020 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15021
15022 RETURN (build_lambda_object (r));
15023 }
15024
15025 case TARGET_EXPR:
15026 /* We can get here for a constant initializer of non-dependent type.
15027 FIXME stop folding in cp_parser_initializer_clause. */
15028 {
15029 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15030 complain);
15031 RETURN (r);
15032 }
15033
15034 case TRANSACTION_EXPR:
15035 RETURN (tsubst_expr(t, args, complain, in_decl,
15036 integral_constant_expression_p));
15037
15038 case PAREN_EXPR:
15039 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15040
15041 case VEC_PERM_EXPR:
15042 RETURN (build_x_vec_perm_expr (input_location,
15043 RECUR (TREE_OPERAND (t, 0)),
15044 RECUR (TREE_OPERAND (t, 1)),
15045 RECUR (TREE_OPERAND (t, 2)),
15046 complain));
15047
15048 default:
15049 /* Handle Objective-C++ constructs, if appropriate. */
15050 {
15051 tree subst
15052 = objcp_tsubst_copy_and_build (t, args, complain,
15053 in_decl, /*function_p=*/false);
15054 if (subst)
15055 RETURN (subst);
15056 }
15057 RETURN (tsubst_copy (t, args, complain, in_decl));
15058 }
15059
15060 #undef RECUR
15061 #undef RETURN
15062 out:
15063 input_location = loc;
15064 return retval;
15065 }
15066
15067 /* Verify that the instantiated ARGS are valid. For type arguments,
15068 make sure that the type's linkage is ok. For non-type arguments,
15069 make sure they are constants if they are integral or enumerations.
15070 Emit an error under control of COMPLAIN, and return TRUE on error. */
15071
15072 static bool
15073 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15074 {
15075 if (dependent_template_arg_p (t))
15076 return false;
15077 if (ARGUMENT_PACK_P (t))
15078 {
15079 tree vec = ARGUMENT_PACK_ARGS (t);
15080 int len = TREE_VEC_LENGTH (vec);
15081 bool result = false;
15082 int i;
15083
15084 for (i = 0; i < len; ++i)
15085 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15086 result = true;
15087 return result;
15088 }
15089 else if (TYPE_P (t))
15090 {
15091 /* [basic.link]: A name with no linkage (notably, the name
15092 of a class or enumeration declared in a local scope)
15093 shall not be used to declare an entity with linkage.
15094 This implies that names with no linkage cannot be used as
15095 template arguments
15096
15097 DR 757 relaxes this restriction for C++0x. */
15098 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15099 : no_linkage_check (t, /*relaxed_p=*/false));
15100
15101 if (nt)
15102 {
15103 /* DR 488 makes use of a type with no linkage cause
15104 type deduction to fail. */
15105 if (complain & tf_error)
15106 {
15107 if (TYPE_ANONYMOUS_P (nt))
15108 error ("%qT is/uses anonymous type", t);
15109 else
15110 error ("template argument for %qD uses local type %qT",
15111 tmpl, t);
15112 }
15113 return true;
15114 }
15115 /* In order to avoid all sorts of complications, we do not
15116 allow variably-modified types as template arguments. */
15117 else if (variably_modified_type_p (t, NULL_TREE))
15118 {
15119 if (complain & tf_error)
15120 error ("%qT is a variably modified type", t);
15121 return true;
15122 }
15123 }
15124 /* Class template and alias template arguments should be OK. */
15125 else if (DECL_TYPE_TEMPLATE_P (t))
15126 ;
15127 /* A non-type argument of integral or enumerated type must be a
15128 constant. */
15129 else if (TREE_TYPE (t)
15130 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15131 && !TREE_CONSTANT (t))
15132 {
15133 if (complain & tf_error)
15134 error ("integral expression %qE is not constant", t);
15135 return true;
15136 }
15137 return false;
15138 }
15139
15140 static bool
15141 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15142 {
15143 int ix, len = DECL_NTPARMS (tmpl);
15144 bool result = false;
15145
15146 for (ix = 0; ix != len; ix++)
15147 {
15148 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15149 result = true;
15150 }
15151 if (result && (complain & tf_error))
15152 error (" trying to instantiate %qD", tmpl);
15153 return result;
15154 }
15155
15156 /* We're out of SFINAE context now, so generate diagnostics for the access
15157 errors we saw earlier when instantiating D from TMPL and ARGS. */
15158
15159 static void
15160 recheck_decl_substitution (tree d, tree tmpl, tree args)
15161 {
15162 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15163 tree type = TREE_TYPE (pattern);
15164 location_t loc = input_location;
15165
15166 push_access_scope (d);
15167 push_deferring_access_checks (dk_no_deferred);
15168 input_location = DECL_SOURCE_LOCATION (pattern);
15169 tsubst (type, args, tf_warning_or_error, d);
15170 input_location = loc;
15171 pop_deferring_access_checks ();
15172 pop_access_scope (d);
15173 }
15174
15175 /* Instantiate the indicated variable, function, or alias template TMPL with
15176 the template arguments in TARG_PTR. */
15177
15178 static tree
15179 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15180 {
15181 tree targ_ptr = orig_args;
15182 tree fndecl;
15183 tree gen_tmpl;
15184 tree spec;
15185 bool access_ok = true;
15186
15187 if (tmpl == error_mark_node)
15188 return error_mark_node;
15189
15190 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15191
15192 /* If this function is a clone, handle it specially. */
15193 if (DECL_CLONED_FUNCTION_P (tmpl))
15194 {
15195 tree spec;
15196 tree clone;
15197
15198 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15199 DECL_CLONED_FUNCTION. */
15200 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15201 targ_ptr, complain);
15202 if (spec == error_mark_node)
15203 return error_mark_node;
15204
15205 /* Look for the clone. */
15206 FOR_EACH_CLONE (clone, spec)
15207 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15208 return clone;
15209 /* We should always have found the clone by now. */
15210 gcc_unreachable ();
15211 return NULL_TREE;
15212 }
15213
15214 /* Check to see if we already have this specialization. */
15215 gen_tmpl = most_general_template (tmpl);
15216 if (tmpl != gen_tmpl)
15217 /* The TMPL is a partial instantiation. To get a full set of
15218 arguments we must add the arguments used to perform the
15219 partial instantiation. */
15220 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15221 targ_ptr);
15222
15223 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15224 but it doesn't seem to be on the hot path. */
15225 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15226
15227 gcc_assert (tmpl == gen_tmpl
15228 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15229 == spec)
15230 || fndecl == NULL_TREE);
15231
15232 if (spec != NULL_TREE)
15233 {
15234 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15235 {
15236 if (complain & tf_error)
15237 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15238 return error_mark_node;
15239 }
15240 return spec;
15241 }
15242
15243 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15244 complain))
15245 return error_mark_node;
15246
15247 /* We are building a FUNCTION_DECL, during which the access of its
15248 parameters and return types have to be checked. However this
15249 FUNCTION_DECL which is the desired context for access checking
15250 is not built yet. We solve this chicken-and-egg problem by
15251 deferring all checks until we have the FUNCTION_DECL. */
15252 push_deferring_access_checks (dk_deferred);
15253
15254 /* Instantiation of the function happens in the context of the function
15255 template, not the context of the overload resolution we're doing. */
15256 push_to_top_level ();
15257 /* If there are dependent arguments, e.g. because we're doing partial
15258 ordering, make sure processing_template_decl stays set. */
15259 if (uses_template_parms (targ_ptr))
15260 ++processing_template_decl;
15261 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15262 {
15263 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15264 complain, gen_tmpl);
15265 push_nested_class (ctx);
15266 }
15267 /* Substitute template parameters to obtain the specialization. */
15268 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15269 targ_ptr, complain, gen_tmpl);
15270 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15271 pop_nested_class ();
15272 pop_from_top_level ();
15273
15274 if (fndecl == error_mark_node)
15275 {
15276 pop_deferring_access_checks ();
15277 return error_mark_node;
15278 }
15279
15280 /* The DECL_TI_TEMPLATE should always be the immediate parent
15281 template, not the most general template. */
15282 DECL_TI_TEMPLATE (fndecl) = tmpl;
15283
15284 /* Now we know the specialization, compute access previously
15285 deferred. */
15286 push_access_scope (fndecl);
15287 if (!perform_deferred_access_checks (complain))
15288 access_ok = false;
15289 pop_access_scope (fndecl);
15290 pop_deferring_access_checks ();
15291
15292 /* If we've just instantiated the main entry point for a function,
15293 instantiate all the alternate entry points as well. We do this
15294 by cloning the instantiation of the main entry point, not by
15295 instantiating the template clones. */
15296 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15297 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15298
15299 if (!access_ok)
15300 {
15301 if (!(complain & tf_error))
15302 {
15303 /* Remember to reinstantiate when we're out of SFINAE so the user
15304 can see the errors. */
15305 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15306 }
15307 return error_mark_node;
15308 }
15309 return fndecl;
15310 }
15311
15312 /* Wrapper for instantiate_template_1. */
15313
15314 tree
15315 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15316 {
15317 tree ret;
15318 timevar_push (TV_TEMPLATE_INST);
15319 ret = instantiate_template_1 (tmpl, orig_args, complain);
15320 timevar_pop (TV_TEMPLATE_INST);
15321 return ret;
15322 }
15323
15324 /* Instantiate the alias template TMPL with ARGS. Also push a template
15325 instantiation level, which instantiate_template doesn't do because
15326 functions and variables have sufficient context established by the
15327 callers. */
15328
15329 static tree
15330 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15331 {
15332 struct pending_template *old_last_pend = last_pending_template;
15333 struct tinst_level *old_error_tinst = last_error_tinst_level;
15334 if (tmpl == error_mark_node || args == error_mark_node)
15335 return error_mark_node;
15336 tree tinst = build_tree_list (tmpl, args);
15337 if (!push_tinst_level (tinst))
15338 {
15339 ggc_free (tinst);
15340 return error_mark_node;
15341 }
15342
15343 args =
15344 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15345 args, tmpl, complain,
15346 /*require_all_args=*/true,
15347 /*use_default_args=*/true);
15348
15349 tree r = instantiate_template (tmpl, args, complain);
15350 pop_tinst_level ();
15351 /* We can't free this if a pending_template entry or last_error_tinst_level
15352 is pointing at it. */
15353 if (last_pending_template == old_last_pend
15354 && last_error_tinst_level == old_error_tinst)
15355 ggc_free (tinst);
15356
15357 return r;
15358 }
15359
15360 /* PARM is a template parameter pack for FN. Returns true iff
15361 PARM is used in a deducible way in the argument list of FN. */
15362
15363 static bool
15364 pack_deducible_p (tree parm, tree fn)
15365 {
15366 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15367 for (; t; t = TREE_CHAIN (t))
15368 {
15369 tree type = TREE_VALUE (t);
15370 tree packs;
15371 if (!PACK_EXPANSION_P (type))
15372 continue;
15373 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15374 packs; packs = TREE_CHAIN (packs))
15375 if (TREE_VALUE (packs) == parm)
15376 {
15377 /* The template parameter pack is used in a function parameter
15378 pack. If this is the end of the parameter list, the
15379 template parameter pack is deducible. */
15380 if (TREE_CHAIN (t) == void_list_node)
15381 return true;
15382 else
15383 /* Otherwise, not. Well, it could be deduced from
15384 a non-pack parameter, but doing so would end up with
15385 a deduction mismatch, so don't bother. */
15386 return false;
15387 }
15388 }
15389 /* The template parameter pack isn't used in any function parameter
15390 packs, but it might be used deeper, e.g. tuple<Args...>. */
15391 return true;
15392 }
15393
15394 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15395 NARGS elements of the arguments that are being used when calling
15396 it. TARGS is a vector into which the deduced template arguments
15397 are placed.
15398
15399 Return zero for success, 2 for an incomplete match that doesn't resolve
15400 all the types, and 1 for complete failure. An error message will be
15401 printed only for an incomplete match.
15402
15403 If FN is a conversion operator, or we are trying to produce a specific
15404 specialization, RETURN_TYPE is the return type desired.
15405
15406 The EXPLICIT_TARGS are explicit template arguments provided via a
15407 template-id.
15408
15409 The parameter STRICT is one of:
15410
15411 DEDUCE_CALL:
15412 We are deducing arguments for a function call, as in
15413 [temp.deduct.call].
15414
15415 DEDUCE_CONV:
15416 We are deducing arguments for a conversion function, as in
15417 [temp.deduct.conv].
15418
15419 DEDUCE_EXACT:
15420 We are deducing arguments when doing an explicit instantiation
15421 as in [temp.explicit], when determining an explicit specialization
15422 as in [temp.expl.spec], or when taking the address of a function
15423 template, as in [temp.deduct.funcaddr]. */
15424
15425 tree
15426 fn_type_unification (tree fn,
15427 tree explicit_targs,
15428 tree targs,
15429 const tree *args,
15430 unsigned int nargs,
15431 tree return_type,
15432 unification_kind_t strict,
15433 int flags,
15434 bool explain_p,
15435 bool decltype_p)
15436 {
15437 tree parms;
15438 tree fntype;
15439 tree decl = NULL_TREE;
15440 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15441 bool ok;
15442 static int deduction_depth;
15443 struct pending_template *old_last_pend = last_pending_template;
15444 struct tinst_level *old_error_tinst = last_error_tinst_level;
15445 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15446 tree tinst;
15447 tree r = error_mark_node;
15448
15449 if (decltype_p)
15450 complain |= tf_decltype;
15451
15452 /* In C++0x, it's possible to have a function template whose type depends
15453 on itself recursively. This is most obvious with decltype, but can also
15454 occur with enumeration scope (c++/48969). So we need to catch infinite
15455 recursion and reject the substitution at deduction time; this function
15456 will return error_mark_node for any repeated substitution.
15457
15458 This also catches excessive recursion such as when f<N> depends on
15459 f<N-1> across all integers, and returns error_mark_node for all the
15460 substitutions back up to the initial one.
15461
15462 This is, of course, not reentrant. */
15463 if (excessive_deduction_depth)
15464 return error_mark_node;
15465 tinst = build_tree_list (fn, NULL_TREE);
15466 ++deduction_depth;
15467
15468 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15469
15470 fntype = TREE_TYPE (fn);
15471 if (explicit_targs)
15472 {
15473 /* [temp.deduct]
15474
15475 The specified template arguments must match the template
15476 parameters in kind (i.e., type, nontype, template), and there
15477 must not be more arguments than there are parameters;
15478 otherwise type deduction fails.
15479
15480 Nontype arguments must match the types of the corresponding
15481 nontype template parameters, or must be convertible to the
15482 types of the corresponding nontype parameters as specified in
15483 _temp.arg.nontype_, otherwise type deduction fails.
15484
15485 All references in the function type of the function template
15486 to the corresponding template parameters are replaced by the
15487 specified template argument values. If a substitution in a
15488 template parameter or in the function type of the function
15489 template results in an invalid type, type deduction fails. */
15490 int i, len = TREE_VEC_LENGTH (tparms);
15491 location_t loc = input_location;
15492 bool incomplete = false;
15493
15494 /* Adjust any explicit template arguments before entering the
15495 substitution context. */
15496 explicit_targs
15497 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15498 complain,
15499 /*require_all_args=*/false,
15500 /*use_default_args=*/false));
15501 if (explicit_targs == error_mark_node)
15502 goto fail;
15503
15504 /* Substitute the explicit args into the function type. This is
15505 necessary so that, for instance, explicitly declared function
15506 arguments can match null pointed constants. If we were given
15507 an incomplete set of explicit args, we must not do semantic
15508 processing during substitution as we could create partial
15509 instantiations. */
15510 for (i = 0; i < len; i++)
15511 {
15512 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15513 bool parameter_pack = false;
15514 tree targ = TREE_VEC_ELT (explicit_targs, i);
15515
15516 /* Dig out the actual parm. */
15517 if (TREE_CODE (parm) == TYPE_DECL
15518 || TREE_CODE (parm) == TEMPLATE_DECL)
15519 {
15520 parm = TREE_TYPE (parm);
15521 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15522 }
15523 else if (TREE_CODE (parm) == PARM_DECL)
15524 {
15525 parm = DECL_INITIAL (parm);
15526 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15527 }
15528
15529 if (!parameter_pack && targ == NULL_TREE)
15530 /* No explicit argument for this template parameter. */
15531 incomplete = true;
15532
15533 if (parameter_pack && pack_deducible_p (parm, fn))
15534 {
15535 /* Mark the argument pack as "incomplete". We could
15536 still deduce more arguments during unification.
15537 We remove this mark in type_unification_real. */
15538 if (targ)
15539 {
15540 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15541 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15542 = ARGUMENT_PACK_ARGS (targ);
15543 }
15544
15545 /* We have some incomplete argument packs. */
15546 incomplete = true;
15547 }
15548 }
15549
15550 TREE_VALUE (tinst) = explicit_targs;
15551 if (!push_tinst_level (tinst))
15552 {
15553 excessive_deduction_depth = true;
15554 goto fail;
15555 }
15556 processing_template_decl += incomplete;
15557 input_location = DECL_SOURCE_LOCATION (fn);
15558 /* Ignore any access checks; we'll see them again in
15559 instantiate_template and they might have the wrong
15560 access path at this point. */
15561 push_deferring_access_checks (dk_deferred);
15562 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15563 complain | tf_partial, NULL_TREE);
15564 pop_deferring_access_checks ();
15565 input_location = loc;
15566 processing_template_decl -= incomplete;
15567 pop_tinst_level ();
15568
15569 if (fntype == error_mark_node)
15570 goto fail;
15571
15572 /* Place the explicitly specified arguments in TARGS. */
15573 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15574 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15575 }
15576
15577 /* Never do unification on the 'this' parameter. */
15578 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15579
15580 if (return_type)
15581 {
15582 tree *new_args;
15583
15584 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15585 new_args = XALLOCAVEC (tree, nargs + 1);
15586 new_args[0] = return_type;
15587 memcpy (new_args + 1, args, nargs * sizeof (tree));
15588 args = new_args;
15589 ++nargs;
15590 }
15591
15592 /* We allow incomplete unification without an error message here
15593 because the standard doesn't seem to explicitly prohibit it. Our
15594 callers must be ready to deal with unification failures in any
15595 event. */
15596
15597 TREE_VALUE (tinst) = targs;
15598 /* If we aren't explaining yet, push tinst context so we can see where
15599 any errors (e.g. from class instantiations triggered by instantiation
15600 of default template arguments) come from. If we are explaining, this
15601 context is redundant. */
15602 if (!explain_p && !push_tinst_level (tinst))
15603 {
15604 excessive_deduction_depth = true;
15605 goto fail;
15606 }
15607
15608 /* type_unification_real will pass back any access checks from default
15609 template argument substitution. */
15610 vec<deferred_access_check, va_gc> *checks;
15611 checks = NULL;
15612
15613 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15614 targs, parms, args, nargs, /*subr=*/0,
15615 strict, flags, &checks, explain_p);
15616 if (!explain_p)
15617 pop_tinst_level ();
15618 if (!ok)
15619 goto fail;
15620
15621 /* Now that we have bindings for all of the template arguments,
15622 ensure that the arguments deduced for the template template
15623 parameters have compatible template parameter lists. We cannot
15624 check this property before we have deduced all template
15625 arguments, because the template parameter types of a template
15626 template parameter might depend on prior template parameters
15627 deduced after the template template parameter. The following
15628 ill-formed example illustrates this issue:
15629
15630 template<typename T, template<T> class C> void f(C<5>, T);
15631
15632 template<int N> struct X {};
15633
15634 void g() {
15635 f(X<5>(), 5l); // error: template argument deduction fails
15636 }
15637
15638 The template parameter list of 'C' depends on the template type
15639 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15640 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15641 time that we deduce 'C'. */
15642 if (!template_template_parm_bindings_ok_p
15643 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15644 {
15645 unify_inconsistent_template_template_parameters (explain_p);
15646 goto fail;
15647 }
15648
15649 /* All is well so far. Now, check:
15650
15651 [temp.deduct]
15652
15653 When all template arguments have been deduced, all uses of
15654 template parameters in nondeduced contexts are replaced with
15655 the corresponding deduced argument values. If the
15656 substitution results in an invalid type, as described above,
15657 type deduction fails. */
15658 TREE_VALUE (tinst) = targs;
15659 if (!push_tinst_level (tinst))
15660 {
15661 excessive_deduction_depth = true;
15662 goto fail;
15663 }
15664
15665 /* Also collect access checks from the instantiation. */
15666 reopen_deferring_access_checks (checks);
15667
15668 decl = instantiate_template (fn, targs, complain);
15669
15670 checks = get_deferred_access_checks ();
15671 pop_deferring_access_checks ();
15672
15673 pop_tinst_level ();
15674
15675 if (decl == error_mark_node)
15676 goto fail;
15677
15678 /* Now perform any access checks encountered during substitution. */
15679 push_access_scope (decl);
15680 ok = perform_access_checks (checks, complain);
15681 pop_access_scope (decl);
15682 if (!ok)
15683 goto fail;
15684
15685 /* If we're looking for an exact match, check that what we got
15686 is indeed an exact match. It might not be if some template
15687 parameters are used in non-deduced contexts. */
15688 if (strict == DEDUCE_EXACT)
15689 {
15690 tree substed = TREE_TYPE (decl);
15691 unsigned int i;
15692
15693 tree sarg
15694 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15695 if (return_type)
15696 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15697 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15698 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15699 {
15700 unify_type_mismatch (explain_p, args[i],
15701 TREE_VALUE (sarg));
15702 goto fail;
15703 }
15704 }
15705
15706 r = decl;
15707
15708 fail:
15709 --deduction_depth;
15710 if (excessive_deduction_depth)
15711 {
15712 if (deduction_depth == 0)
15713 /* Reset once we're all the way out. */
15714 excessive_deduction_depth = false;
15715 }
15716
15717 /* We can't free this if a pending_template entry or last_error_tinst_level
15718 is pointing at it. */
15719 if (last_pending_template == old_last_pend
15720 && last_error_tinst_level == old_error_tinst)
15721 ggc_free (tinst);
15722
15723 return r;
15724 }
15725
15726 /* Adjust types before performing type deduction, as described in
15727 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15728 sections are symmetric. PARM is the type of a function parameter
15729 or the return type of the conversion function. ARG is the type of
15730 the argument passed to the call, or the type of the value
15731 initialized with the result of the conversion function.
15732 ARG_EXPR is the original argument expression, which may be null. */
15733
15734 static int
15735 maybe_adjust_types_for_deduction (unification_kind_t strict,
15736 tree* parm,
15737 tree* arg,
15738 tree arg_expr)
15739 {
15740 int result = 0;
15741
15742 switch (strict)
15743 {
15744 case DEDUCE_CALL:
15745 break;
15746
15747 case DEDUCE_CONV:
15748 {
15749 /* Swap PARM and ARG throughout the remainder of this
15750 function; the handling is precisely symmetric since PARM
15751 will initialize ARG rather than vice versa. */
15752 tree* temp = parm;
15753 parm = arg;
15754 arg = temp;
15755 break;
15756 }
15757
15758 case DEDUCE_EXACT:
15759 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15760 too, but here handle it by stripping the reference from PARM
15761 rather than by adding it to ARG. */
15762 if (TREE_CODE (*parm) == REFERENCE_TYPE
15763 && TYPE_REF_IS_RVALUE (*parm)
15764 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15765 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15766 && TREE_CODE (*arg) == REFERENCE_TYPE
15767 && !TYPE_REF_IS_RVALUE (*arg))
15768 *parm = TREE_TYPE (*parm);
15769 /* Nothing else to do in this case. */
15770 return 0;
15771
15772 default:
15773 gcc_unreachable ();
15774 }
15775
15776 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15777 {
15778 /* [temp.deduct.call]
15779
15780 If P is not a reference type:
15781
15782 --If A is an array type, the pointer type produced by the
15783 array-to-pointer standard conversion (_conv.array_) is
15784 used in place of A for type deduction; otherwise,
15785
15786 --If A is a function type, the pointer type produced by
15787 the function-to-pointer standard conversion
15788 (_conv.func_) is used in place of A for type deduction;
15789 otherwise,
15790
15791 --If A is a cv-qualified type, the top level
15792 cv-qualifiers of A's type are ignored for type
15793 deduction. */
15794 if (TREE_CODE (*arg) == ARRAY_TYPE)
15795 *arg = build_pointer_type (TREE_TYPE (*arg));
15796 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15797 *arg = build_pointer_type (*arg);
15798 else
15799 *arg = TYPE_MAIN_VARIANT (*arg);
15800 }
15801
15802 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15803 of the form T&&, where T is a template parameter, and the argument
15804 is an lvalue, T is deduced as A& */
15805 if (TREE_CODE (*parm) == REFERENCE_TYPE
15806 && TYPE_REF_IS_RVALUE (*parm)
15807 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15808 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15809 && (arg_expr ? real_lvalue_p (arg_expr)
15810 /* try_one_overload doesn't provide an arg_expr, but
15811 functions are always lvalues. */
15812 : TREE_CODE (*arg) == FUNCTION_TYPE))
15813 *arg = build_reference_type (*arg);
15814
15815 /* [temp.deduct.call]
15816
15817 If P is a cv-qualified type, the top level cv-qualifiers
15818 of P's type are ignored for type deduction. If P is a
15819 reference type, the type referred to by P is used for
15820 type deduction. */
15821 *parm = TYPE_MAIN_VARIANT (*parm);
15822 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15823 {
15824 *parm = TREE_TYPE (*parm);
15825 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15826 }
15827
15828 /* DR 322. For conversion deduction, remove a reference type on parm
15829 too (which has been swapped into ARG). */
15830 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15831 *arg = TREE_TYPE (*arg);
15832
15833 return result;
15834 }
15835
15836 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15837 template which does contain any deducible template parameters; check if
15838 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15839 unify_one_argument. */
15840
15841 static int
15842 check_non_deducible_conversion (tree parm, tree arg, int strict,
15843 int flags, bool explain_p)
15844 {
15845 tree type;
15846
15847 if (!TYPE_P (arg))
15848 type = TREE_TYPE (arg);
15849 else
15850 type = arg;
15851
15852 if (same_type_p (parm, type))
15853 return unify_success (explain_p);
15854
15855 if (strict == DEDUCE_CONV)
15856 {
15857 if (can_convert_arg (type, parm, NULL_TREE, flags,
15858 explain_p ? tf_warning_or_error : tf_none))
15859 return unify_success (explain_p);
15860 }
15861 else if (strict != DEDUCE_EXACT)
15862 {
15863 if (can_convert_arg (parm, type,
15864 TYPE_P (arg) ? NULL_TREE : arg,
15865 flags, explain_p ? tf_warning_or_error : tf_none))
15866 return unify_success (explain_p);
15867 }
15868
15869 if (strict == DEDUCE_EXACT)
15870 return unify_type_mismatch (explain_p, parm, arg);
15871 else
15872 return unify_arg_conversion (explain_p, parm, type, arg);
15873 }
15874
15875 static bool uses_deducible_template_parms (tree type);
15876
15877 /* Returns true iff the expression EXPR is one from which a template
15878 argument can be deduced. In other words, if it's an undecorated
15879 use of a template non-type parameter. */
15880
15881 static bool
15882 deducible_expression (tree expr)
15883 {
15884 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
15885 }
15886
15887 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15888 deducible way; that is, if it has a max value of <PARM> - 1. */
15889
15890 static bool
15891 deducible_array_bound (tree domain)
15892 {
15893 if (domain == NULL_TREE)
15894 return false;
15895
15896 tree max = TYPE_MAX_VALUE (domain);
15897 if (TREE_CODE (max) != MINUS_EXPR)
15898 return false;
15899
15900 return deducible_expression (TREE_OPERAND (max, 0));
15901 }
15902
15903 /* Returns true iff the template arguments ARGS use a template parameter
15904 in a deducible way. */
15905
15906 static bool
15907 deducible_template_args (tree args)
15908 {
15909 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
15910 {
15911 bool deducible;
15912 tree elt = TREE_VEC_ELT (args, i);
15913 if (ARGUMENT_PACK_P (elt))
15914 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
15915 else
15916 {
15917 if (PACK_EXPANSION_P (elt))
15918 elt = PACK_EXPANSION_PATTERN (elt);
15919 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
15920 deducible = true;
15921 else if (TYPE_P (elt))
15922 deducible = uses_deducible_template_parms (elt);
15923 else
15924 deducible = deducible_expression (elt);
15925 }
15926 if (deducible)
15927 return true;
15928 }
15929 return false;
15930 }
15931
15932 /* Returns true iff TYPE contains any deducible references to template
15933 parameters, as per 14.8.2.5. */
15934
15935 static bool
15936 uses_deducible_template_parms (tree type)
15937 {
15938 if (PACK_EXPANSION_P (type))
15939 type = PACK_EXPANSION_PATTERN (type);
15940
15941 /* T
15942 cv-list T
15943 TT<T>
15944 TT<i>
15945 TT<> */
15946 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
15947 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
15948 return true;
15949
15950 /* T*
15951 T&
15952 T&& */
15953 if (POINTER_TYPE_P (type))
15954 return uses_deducible_template_parms (TREE_TYPE (type));
15955
15956 /* T[integer-constant ]
15957 type [i] */
15958 if (TREE_CODE (type) == ARRAY_TYPE)
15959 return (uses_deducible_template_parms (TREE_TYPE (type))
15960 || deducible_array_bound (TYPE_DOMAIN (type)));
15961
15962 /* T type ::*
15963 type T::*
15964 T T::*
15965 T (type ::*)()
15966 type (T::*)()
15967 type (type ::*)(T)
15968 type (T::*)(T)
15969 T (type ::*)(T)
15970 T (T::*)()
15971 T (T::*)(T) */
15972 if (TYPE_PTRMEM_P (type))
15973 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
15974 || (uses_deducible_template_parms
15975 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
15976
15977 /* template-name <T> (where template-name refers to a class template)
15978 template-name <i> (where template-name refers to a class template) */
15979 if (CLASS_TYPE_P (type)
15980 && CLASSTYPE_TEMPLATE_INFO (type)
15981 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
15982 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
15983 (CLASSTYPE_TI_ARGS (type)));
15984
15985 /* type (T)
15986 T()
15987 T(T) */
15988 if (TREE_CODE (type) == FUNCTION_TYPE
15989 || TREE_CODE (type) == METHOD_TYPE)
15990 {
15991 if (uses_deducible_template_parms (TREE_TYPE (type)))
15992 return true;
15993 tree parm = TYPE_ARG_TYPES (type);
15994 if (TREE_CODE (type) == METHOD_TYPE)
15995 parm = TREE_CHAIN (parm);
15996 for (; parm; parm = TREE_CHAIN (parm))
15997 if (uses_deducible_template_parms (TREE_VALUE (parm)))
15998 return true;
15999 }
16000
16001 return false;
16002 }
16003
16004 /* Subroutine of type_unification_real and unify_pack_expansion to
16005 handle unification of a single P/A pair. Parameters are as
16006 for those functions. */
16007
16008 static int
16009 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16010 int subr, unification_kind_t strict, int flags,
16011 bool explain_p)
16012 {
16013 tree arg_expr = NULL_TREE;
16014 int arg_strict;
16015
16016 if (arg == error_mark_node || parm == error_mark_node)
16017 return unify_invalid (explain_p);
16018 if (arg == unknown_type_node)
16019 /* We can't deduce anything from this, but we might get all the
16020 template args from other function args. */
16021 return unify_success (explain_p);
16022
16023 /* Implicit conversions (Clause 4) will be performed on a function
16024 argument to convert it to the type of the corresponding function
16025 parameter if the parameter type contains no template-parameters that
16026 participate in template argument deduction. */
16027 if (TYPE_P (parm) && !uses_template_parms (parm))
16028 /* For function parameters that contain no template-parameters at all,
16029 we have historically checked for convertibility in order to shortcut
16030 consideration of this candidate. */
16031 return check_non_deducible_conversion (parm, arg, strict, flags,
16032 explain_p);
16033 else if (strict == DEDUCE_CALL
16034 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16035 /* For function parameters with only non-deducible template parameters,
16036 just return. */
16037 return unify_success (explain_p);
16038
16039 switch (strict)
16040 {
16041 case DEDUCE_CALL:
16042 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16043 | UNIFY_ALLOW_MORE_CV_QUAL
16044 | UNIFY_ALLOW_DERIVED);
16045 break;
16046
16047 case DEDUCE_CONV:
16048 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16049 break;
16050
16051 case DEDUCE_EXACT:
16052 arg_strict = UNIFY_ALLOW_NONE;
16053 break;
16054
16055 default:
16056 gcc_unreachable ();
16057 }
16058
16059 /* We only do these transformations if this is the top-level
16060 parameter_type_list in a call or declaration matching; in other
16061 situations (nested function declarators, template argument lists) we
16062 won't be comparing a type to an expression, and we don't do any type
16063 adjustments. */
16064 if (!subr)
16065 {
16066 if (!TYPE_P (arg))
16067 {
16068 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16069 if (type_unknown_p (arg))
16070 {
16071 /* [temp.deduct.type] A template-argument can be
16072 deduced from a pointer to function or pointer
16073 to member function argument if the set of
16074 overloaded functions does not contain function
16075 templates and at most one of a set of
16076 overloaded functions provides a unique
16077 match. */
16078
16079 if (resolve_overloaded_unification
16080 (tparms, targs, parm, arg, strict,
16081 arg_strict, explain_p))
16082 return unify_success (explain_p);
16083 return unify_overload_resolution_failure (explain_p, arg);
16084 }
16085
16086 arg_expr = arg;
16087 arg = unlowered_expr_type (arg);
16088 if (arg == error_mark_node)
16089 return unify_invalid (explain_p);
16090 }
16091
16092 arg_strict |=
16093 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16094 }
16095 else
16096 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16097 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16098
16099 /* For deduction from an init-list we need the actual list. */
16100 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16101 arg = arg_expr;
16102 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16103 }
16104
16105 /* Most parms like fn_type_unification.
16106
16107 If SUBR is 1, we're being called recursively (to unify the
16108 arguments of a function or method parameter of a function
16109 template).
16110
16111 CHECKS is a pointer to a vector of access checks encountered while
16112 substituting default template arguments. */
16113
16114 static int
16115 type_unification_real (tree tparms,
16116 tree targs,
16117 tree xparms,
16118 const tree *xargs,
16119 unsigned int xnargs,
16120 int subr,
16121 unification_kind_t strict,
16122 int flags,
16123 vec<deferred_access_check, va_gc> **checks,
16124 bool explain_p)
16125 {
16126 tree parm, arg;
16127 int i;
16128 int ntparms = TREE_VEC_LENGTH (tparms);
16129 int saw_undeduced = 0;
16130 tree parms;
16131 const tree *args;
16132 unsigned int nargs;
16133 unsigned int ia;
16134
16135 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16136 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16137 gcc_assert (ntparms > 0);
16138
16139 /* Reset the number of non-defaulted template arguments contained
16140 in TARGS. */
16141 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16142
16143 again:
16144 parms = xparms;
16145 args = xargs;
16146 nargs = xnargs;
16147
16148 ia = 0;
16149 while (parms && parms != void_list_node
16150 && ia < nargs)
16151 {
16152 parm = TREE_VALUE (parms);
16153
16154 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16155 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16156 /* For a function parameter pack that occurs at the end of the
16157 parameter-declaration-list, the type A of each remaining
16158 argument of the call is compared with the type P of the
16159 declarator-id of the function parameter pack. */
16160 break;
16161
16162 parms = TREE_CHAIN (parms);
16163
16164 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16165 /* For a function parameter pack that does not occur at the
16166 end of the parameter-declaration-list, the type of the
16167 parameter pack is a non-deduced context. */
16168 continue;
16169
16170 arg = args[ia];
16171 ++ia;
16172
16173 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16174 flags, explain_p))
16175 return 1;
16176 }
16177
16178 if (parms
16179 && parms != void_list_node
16180 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16181 {
16182 /* Unify the remaining arguments with the pack expansion type. */
16183 tree argvec;
16184 tree parmvec = make_tree_vec (1);
16185
16186 /* Allocate a TREE_VEC and copy in all of the arguments */
16187 argvec = make_tree_vec (nargs - ia);
16188 for (i = 0; ia < nargs; ++ia, ++i)
16189 TREE_VEC_ELT (argvec, i) = args[ia];
16190
16191 /* Copy the parameter into parmvec. */
16192 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16193 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16194 /*subr=*/subr, explain_p))
16195 return 1;
16196
16197 /* Advance to the end of the list of parameters. */
16198 parms = TREE_CHAIN (parms);
16199 }
16200
16201 /* Fail if we've reached the end of the parm list, and more args
16202 are present, and the parm list isn't variadic. */
16203 if (ia < nargs && parms == void_list_node)
16204 return unify_too_many_arguments (explain_p, nargs, ia);
16205 /* Fail if parms are left and they don't have default values. */
16206 if (parms && parms != void_list_node
16207 && TREE_PURPOSE (parms) == NULL_TREE)
16208 {
16209 unsigned int count = nargs;
16210 tree p = parms;
16211 while (p && p != void_list_node)
16212 {
16213 count++;
16214 p = TREE_CHAIN (p);
16215 }
16216 return unify_too_few_arguments (explain_p, ia, count);
16217 }
16218
16219 if (!subr)
16220 {
16221 tsubst_flags_t complain = (explain_p
16222 ? tf_warning_or_error
16223 : tf_none);
16224
16225 for (i = 0; i < ntparms; i++)
16226 {
16227 tree targ = TREE_VEC_ELT (targs, i);
16228 tree tparm = TREE_VEC_ELT (tparms, i);
16229
16230 /* Clear the "incomplete" flags on all argument packs now so that
16231 substituting them into later default arguments works. */
16232 if (targ && ARGUMENT_PACK_P (targ))
16233 {
16234 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16235 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16236 }
16237
16238 if (targ || tparm == error_mark_node)
16239 continue;
16240 tparm = TREE_VALUE (tparm);
16241
16242 /* If this is an undeduced nontype parameter that depends on
16243 a type parameter, try another pass; its type may have been
16244 deduced from a later argument than the one from which
16245 this parameter can be deduced. */
16246 if (TREE_CODE (tparm) == PARM_DECL
16247 && uses_template_parms (TREE_TYPE (tparm))
16248 && !saw_undeduced++)
16249 goto again;
16250
16251 /* Core issue #226 (C++0x) [temp.deduct]:
16252
16253 If a template argument has not been deduced, its
16254 default template argument, if any, is used.
16255
16256 When we are in C++98 mode, TREE_PURPOSE will either
16257 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16258 to explicitly check cxx_dialect here. */
16259 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16260 {
16261 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16262 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16263 reopen_deferring_access_checks (*checks);
16264 location_t save_loc = input_location;
16265 if (DECL_P (parm))
16266 input_location = DECL_SOURCE_LOCATION (parm);
16267 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16268 arg = convert_template_argument (parm, arg, targs, complain,
16269 i, NULL_TREE);
16270 input_location = save_loc;
16271 *checks = get_deferred_access_checks ();
16272 pop_deferring_access_checks ();
16273 if (arg == error_mark_node)
16274 return 1;
16275 else
16276 {
16277 TREE_VEC_ELT (targs, i) = arg;
16278 /* The position of the first default template argument,
16279 is also the number of non-defaulted arguments in TARGS.
16280 Record that. */
16281 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16282 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16283 continue;
16284 }
16285 }
16286
16287 /* If the type parameter is a parameter pack, then it will
16288 be deduced to an empty parameter pack. */
16289 if (template_parameter_pack_p (tparm))
16290 {
16291 tree arg;
16292
16293 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16294 {
16295 arg = make_node (NONTYPE_ARGUMENT_PACK);
16296 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16297 TREE_CONSTANT (arg) = 1;
16298 }
16299 else
16300 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16301
16302 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16303
16304 TREE_VEC_ELT (targs, i) = arg;
16305 continue;
16306 }
16307
16308 return unify_parameter_deduction_failure (explain_p, tparm);
16309 }
16310 }
16311 #ifdef ENABLE_CHECKING
16312 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16313 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16314 #endif
16315
16316 return unify_success (explain_p);
16317 }
16318
16319 /* Subroutine of type_unification_real. Args are like the variables
16320 at the call site. ARG is an overloaded function (or template-id);
16321 we try deducing template args from each of the overloads, and if
16322 only one succeeds, we go with that. Modifies TARGS and returns
16323 true on success. */
16324
16325 static bool
16326 resolve_overloaded_unification (tree tparms,
16327 tree targs,
16328 tree parm,
16329 tree arg,
16330 unification_kind_t strict,
16331 int sub_strict,
16332 bool explain_p)
16333 {
16334 tree tempargs = copy_node (targs);
16335 int good = 0;
16336 tree goodfn = NULL_TREE;
16337 bool addr_p;
16338
16339 if (TREE_CODE (arg) == ADDR_EXPR)
16340 {
16341 arg = TREE_OPERAND (arg, 0);
16342 addr_p = true;
16343 }
16344 else
16345 addr_p = false;
16346
16347 if (TREE_CODE (arg) == COMPONENT_REF)
16348 /* Handle `&x' where `x' is some static or non-static member
16349 function name. */
16350 arg = TREE_OPERAND (arg, 1);
16351
16352 if (TREE_CODE (arg) == OFFSET_REF)
16353 arg = TREE_OPERAND (arg, 1);
16354
16355 /* Strip baselink information. */
16356 if (BASELINK_P (arg))
16357 arg = BASELINK_FUNCTIONS (arg);
16358
16359 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16360 {
16361 /* If we got some explicit template args, we need to plug them into
16362 the affected templates before we try to unify, in case the
16363 explicit args will completely resolve the templates in question. */
16364
16365 int ok = 0;
16366 tree expl_subargs = TREE_OPERAND (arg, 1);
16367 arg = TREE_OPERAND (arg, 0);
16368
16369 for (; arg; arg = OVL_NEXT (arg))
16370 {
16371 tree fn = OVL_CURRENT (arg);
16372 tree subargs, elem;
16373
16374 if (TREE_CODE (fn) != TEMPLATE_DECL)
16375 continue;
16376
16377 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16378 expl_subargs, NULL_TREE, tf_none,
16379 /*require_all_args=*/true,
16380 /*use_default_args=*/true);
16381 if (subargs != error_mark_node
16382 && !any_dependent_template_arguments_p (subargs))
16383 {
16384 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
16385 if (try_one_overload (tparms, targs, tempargs, parm,
16386 elem, strict, sub_strict, addr_p, explain_p)
16387 && (!goodfn || !same_type_p (goodfn, elem)))
16388 {
16389 goodfn = elem;
16390 ++good;
16391 }
16392 }
16393 else if (subargs)
16394 ++ok;
16395 }
16396 /* If no templates (or more than one) are fully resolved by the
16397 explicit arguments, this template-id is a non-deduced context; it
16398 could still be OK if we deduce all template arguments for the
16399 enclosing call through other arguments. */
16400 if (good != 1)
16401 good = ok;
16402 }
16403 else if (TREE_CODE (arg) != OVERLOAD
16404 && TREE_CODE (arg) != FUNCTION_DECL)
16405 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16406 -- but the deduction does not succeed because the expression is
16407 not just the function on its own. */
16408 return false;
16409 else
16410 for (; arg; arg = OVL_NEXT (arg))
16411 if (try_one_overload (tparms, targs, tempargs, parm,
16412 TREE_TYPE (OVL_CURRENT (arg)),
16413 strict, sub_strict, addr_p, explain_p)
16414 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16415 {
16416 goodfn = OVL_CURRENT (arg);
16417 ++good;
16418 }
16419
16420 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16421 to function or pointer to member function argument if the set of
16422 overloaded functions does not contain function templates and at most
16423 one of a set of overloaded functions provides a unique match.
16424
16425 So if we found multiple possibilities, we return success but don't
16426 deduce anything. */
16427
16428 if (good == 1)
16429 {
16430 int i = TREE_VEC_LENGTH (targs);
16431 for (; i--; )
16432 if (TREE_VEC_ELT (tempargs, i))
16433 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16434 }
16435 if (good)
16436 return true;
16437
16438 return false;
16439 }
16440
16441 /* Core DR 115: In contexts where deduction is done and fails, or in
16442 contexts where deduction is not done, if a template argument list is
16443 specified and it, along with any default template arguments, identifies
16444 a single function template specialization, then the template-id is an
16445 lvalue for the function template specialization. */
16446
16447 tree
16448 resolve_nondeduced_context (tree orig_expr)
16449 {
16450 tree expr, offset, baselink;
16451 bool addr;
16452
16453 if (!type_unknown_p (orig_expr))
16454 return orig_expr;
16455
16456 expr = orig_expr;
16457 addr = false;
16458 offset = NULL_TREE;
16459 baselink = NULL_TREE;
16460
16461 if (TREE_CODE (expr) == ADDR_EXPR)
16462 {
16463 expr = TREE_OPERAND (expr, 0);
16464 addr = true;
16465 }
16466 if (TREE_CODE (expr) == OFFSET_REF)
16467 {
16468 offset = expr;
16469 expr = TREE_OPERAND (expr, 1);
16470 }
16471 if (BASELINK_P (expr))
16472 {
16473 baselink = expr;
16474 expr = BASELINK_FUNCTIONS (expr);
16475 }
16476
16477 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16478 {
16479 int good = 0;
16480 tree goodfn = NULL_TREE;
16481
16482 /* If we got some explicit template args, we need to plug them into
16483 the affected templates before we try to unify, in case the
16484 explicit args will completely resolve the templates in question. */
16485
16486 tree expl_subargs = TREE_OPERAND (expr, 1);
16487 tree arg = TREE_OPERAND (expr, 0);
16488 tree badfn = NULL_TREE;
16489 tree badargs = NULL_TREE;
16490
16491 for (; arg; arg = OVL_NEXT (arg))
16492 {
16493 tree fn = OVL_CURRENT (arg);
16494 tree subargs, elem;
16495
16496 if (TREE_CODE (fn) != TEMPLATE_DECL)
16497 continue;
16498
16499 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16500 expl_subargs, NULL_TREE, tf_none,
16501 /*require_all_args=*/true,
16502 /*use_default_args=*/true);
16503 if (subargs != error_mark_node
16504 && !any_dependent_template_arguments_p (subargs))
16505 {
16506 elem = instantiate_template (fn, subargs, tf_none);
16507 if (elem == error_mark_node)
16508 {
16509 badfn = fn;
16510 badargs = subargs;
16511 }
16512 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16513 {
16514 goodfn = elem;
16515 ++good;
16516 }
16517 }
16518 }
16519 if (good == 1)
16520 {
16521 mark_used (goodfn);
16522 expr = goodfn;
16523 if (baselink)
16524 expr = build_baselink (BASELINK_BINFO (baselink),
16525 BASELINK_ACCESS_BINFO (baselink),
16526 expr, BASELINK_OPTYPE (baselink));
16527 if (offset)
16528 {
16529 tree base
16530 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16531 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16532 }
16533 if (addr)
16534 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16535 return expr;
16536 }
16537 else if (good == 0 && badargs)
16538 /* There were no good options and at least one bad one, so let the
16539 user know what the problem is. */
16540 instantiate_template (badfn, badargs, tf_warning_or_error);
16541 }
16542 return orig_expr;
16543 }
16544
16545 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16546 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16547 different overloads deduce different arguments for a given parm.
16548 ADDR_P is true if the expression for which deduction is being
16549 performed was of the form "& fn" rather than simply "fn".
16550
16551 Returns 1 on success. */
16552
16553 static int
16554 try_one_overload (tree tparms,
16555 tree orig_targs,
16556 tree targs,
16557 tree parm,
16558 tree arg,
16559 unification_kind_t strict,
16560 int sub_strict,
16561 bool addr_p,
16562 bool explain_p)
16563 {
16564 int nargs;
16565 tree tempargs;
16566 int i;
16567
16568 if (arg == error_mark_node)
16569 return 0;
16570
16571 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16572 to function or pointer to member function argument if the set of
16573 overloaded functions does not contain function templates and at most
16574 one of a set of overloaded functions provides a unique match.
16575
16576 So if this is a template, just return success. */
16577
16578 if (uses_template_parms (arg))
16579 return 1;
16580
16581 if (TREE_CODE (arg) == METHOD_TYPE)
16582 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16583 else if (addr_p)
16584 arg = build_pointer_type (arg);
16585
16586 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16587
16588 /* We don't copy orig_targs for this because if we have already deduced
16589 some template args from previous args, unify would complain when we
16590 try to deduce a template parameter for the same argument, even though
16591 there isn't really a conflict. */
16592 nargs = TREE_VEC_LENGTH (targs);
16593 tempargs = make_tree_vec (nargs);
16594
16595 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16596 return 0;
16597
16598 /* First make sure we didn't deduce anything that conflicts with
16599 explicitly specified args. */
16600 for (i = nargs; i--; )
16601 {
16602 tree elt = TREE_VEC_ELT (tempargs, i);
16603 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16604
16605 if (!elt)
16606 /*NOP*/;
16607 else if (uses_template_parms (elt))
16608 /* Since we're unifying against ourselves, we will fill in
16609 template args used in the function parm list with our own
16610 template parms. Discard them. */
16611 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16612 else if (oldelt && !template_args_equal (oldelt, elt))
16613 return 0;
16614 }
16615
16616 for (i = nargs; i--; )
16617 {
16618 tree elt = TREE_VEC_ELT (tempargs, i);
16619
16620 if (elt)
16621 TREE_VEC_ELT (targs, i) = elt;
16622 }
16623
16624 return 1;
16625 }
16626
16627 /* PARM is a template class (perhaps with unbound template
16628 parameters). ARG is a fully instantiated type. If ARG can be
16629 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16630 TARGS are as for unify. */
16631
16632 static tree
16633 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16634 bool explain_p)
16635 {
16636 tree copy_of_targs;
16637
16638 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16639 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16640 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16641 return NULL_TREE;
16642
16643 /* We need to make a new template argument vector for the call to
16644 unify. If we used TARGS, we'd clutter it up with the result of
16645 the attempted unification, even if this class didn't work out.
16646 We also don't want to commit ourselves to all the unifications
16647 we've already done, since unification is supposed to be done on
16648 an argument-by-argument basis. In other words, consider the
16649 following pathological case:
16650
16651 template <int I, int J, int K>
16652 struct S {};
16653
16654 template <int I, int J>
16655 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16656
16657 template <int I, int J, int K>
16658 void f(S<I, J, K>, S<I, I, I>);
16659
16660 void g() {
16661 S<0, 0, 0> s0;
16662 S<0, 1, 2> s2;
16663
16664 f(s0, s2);
16665 }
16666
16667 Now, by the time we consider the unification involving `s2', we
16668 already know that we must have `f<0, 0, 0>'. But, even though
16669 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16670 because there are two ways to unify base classes of S<0, 1, 2>
16671 with S<I, I, I>. If we kept the already deduced knowledge, we
16672 would reject the possibility I=1. */
16673 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16674
16675 /* If unification failed, we're done. */
16676 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16677 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16678 return NULL_TREE;
16679
16680 return arg;
16681 }
16682
16683 /* Given a template type PARM and a class type ARG, find the unique
16684 base type in ARG that is an instance of PARM. We do not examine
16685 ARG itself; only its base-classes. If there is not exactly one
16686 appropriate base class, return NULL_TREE. PARM may be the type of
16687 a partial specialization, as well as a plain template type. Used
16688 by unify. */
16689
16690 static enum template_base_result
16691 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16692 bool explain_p, tree *result)
16693 {
16694 tree rval = NULL_TREE;
16695 tree binfo;
16696
16697 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16698
16699 binfo = TYPE_BINFO (complete_type (arg));
16700 if (!binfo)
16701 {
16702 /* The type could not be completed. */
16703 *result = NULL_TREE;
16704 return tbr_incomplete_type;
16705 }
16706
16707 /* Walk in inheritance graph order. The search order is not
16708 important, and this avoids multiple walks of virtual bases. */
16709 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16710 {
16711 tree r = try_class_unification (tparms, targs, parm,
16712 BINFO_TYPE (binfo), explain_p);
16713
16714 if (r)
16715 {
16716 /* If there is more than one satisfactory baseclass, then:
16717
16718 [temp.deduct.call]
16719
16720 If they yield more than one possible deduced A, the type
16721 deduction fails.
16722
16723 applies. */
16724 if (rval && !same_type_p (r, rval))
16725 {
16726 *result = NULL_TREE;
16727 return tbr_ambiguous_baseclass;
16728 }
16729
16730 rval = r;
16731 }
16732 }
16733
16734 *result = rval;
16735 return tbr_success;
16736 }
16737
16738 /* Returns the level of DECL, which declares a template parameter. */
16739
16740 static int
16741 template_decl_level (tree decl)
16742 {
16743 switch (TREE_CODE (decl))
16744 {
16745 case TYPE_DECL:
16746 case TEMPLATE_DECL:
16747 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16748
16749 case PARM_DECL:
16750 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16751
16752 default:
16753 gcc_unreachable ();
16754 }
16755 return 0;
16756 }
16757
16758 /* Decide whether ARG can be unified with PARM, considering only the
16759 cv-qualifiers of each type, given STRICT as documented for unify.
16760 Returns nonzero iff the unification is OK on that basis. */
16761
16762 static int
16763 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16764 {
16765 int arg_quals = cp_type_quals (arg);
16766 int parm_quals = cp_type_quals (parm);
16767
16768 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16769 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16770 {
16771 /* Although a CVR qualifier is ignored when being applied to a
16772 substituted template parameter ([8.3.2]/1 for example), that
16773 does not allow us to unify "const T" with "int&" because both
16774 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16775 It is ok when we're allowing additional CV qualifiers
16776 at the outer level [14.8.2.1]/3,1st bullet. */
16777 if ((TREE_CODE (arg) == REFERENCE_TYPE
16778 || TREE_CODE (arg) == FUNCTION_TYPE
16779 || TREE_CODE (arg) == METHOD_TYPE)
16780 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16781 return 0;
16782
16783 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16784 && (parm_quals & TYPE_QUAL_RESTRICT))
16785 return 0;
16786 }
16787
16788 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16789 && (arg_quals & parm_quals) != parm_quals)
16790 return 0;
16791
16792 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
16793 && (parm_quals & arg_quals) != arg_quals)
16794 return 0;
16795
16796 return 1;
16797 }
16798
16799 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16800 void
16801 template_parm_level_and_index (tree parm, int* level, int* index)
16802 {
16803 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16804 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16805 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16806 {
16807 *index = TEMPLATE_TYPE_IDX (parm);
16808 *level = TEMPLATE_TYPE_LEVEL (parm);
16809 }
16810 else
16811 {
16812 *index = TEMPLATE_PARM_IDX (parm);
16813 *level = TEMPLATE_PARM_LEVEL (parm);
16814 }
16815 }
16816
16817 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16818 do { \
16819 if (unify (TP, TA, P, A, S, EP)) \
16820 return 1; \
16821 } while (0);
16822
16823 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16824 expansion at the end of PACKED_PARMS. Returns 0 if the type
16825 deduction succeeds, 1 otherwise. STRICT is the same as in
16826 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16827 call argument list. We'll need to adjust the arguments to make them
16828 types. SUBR tells us if this is from a recursive call to
16829 type_unification_real, or for comparing two template argument
16830 lists. */
16831
16832 static int
16833 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
16834 tree packed_args, unification_kind_t strict,
16835 bool subr, bool explain_p)
16836 {
16837 tree parm
16838 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
16839 tree pattern = PACK_EXPANSION_PATTERN (parm);
16840 tree pack, packs = NULL_TREE;
16841 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
16842 int len = TREE_VEC_LENGTH (packed_args);
16843
16844 /* Determine the parameter packs we will be deducing from the
16845 pattern, and record their current deductions. */
16846 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
16847 pack; pack = TREE_CHAIN (pack))
16848 {
16849 tree parm_pack = TREE_VALUE (pack);
16850 int idx, level;
16851
16852 /* Determine the index and level of this parameter pack. */
16853 template_parm_level_and_index (parm_pack, &level, &idx);
16854
16855 /* Keep track of the parameter packs and their corresponding
16856 argument packs. */
16857 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
16858 TREE_TYPE (packs) = make_tree_vec (len - start);
16859 }
16860
16861 /* Loop through all of the arguments that have not yet been
16862 unified and unify each with the pattern. */
16863 for (i = start; i < len; i++)
16864 {
16865 tree parm;
16866 bool any_explicit = false;
16867 tree arg = TREE_VEC_ELT (packed_args, i);
16868
16869 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16870 or the element of its argument pack at the current index if
16871 this argument was explicitly specified. */
16872 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16873 {
16874 int idx, level;
16875 tree arg, pargs;
16876 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16877
16878 arg = NULL_TREE;
16879 if (TREE_VALUE (pack)
16880 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
16881 && (i - start < TREE_VEC_LENGTH (pargs)))
16882 {
16883 any_explicit = true;
16884 arg = TREE_VEC_ELT (pargs, i - start);
16885 }
16886 TMPL_ARG (targs, level, idx) = arg;
16887 }
16888
16889 /* If we had explicit template arguments, substitute them into the
16890 pattern before deduction. */
16891 if (any_explicit)
16892 {
16893 /* Some arguments might still be unspecified or dependent. */
16894 bool dependent;
16895 ++processing_template_decl;
16896 dependent = any_dependent_template_arguments_p (targs);
16897 if (!dependent)
16898 --processing_template_decl;
16899 parm = tsubst (pattern, targs,
16900 explain_p ? tf_warning_or_error : tf_none,
16901 NULL_TREE);
16902 if (dependent)
16903 --processing_template_decl;
16904 if (parm == error_mark_node)
16905 return 1;
16906 }
16907 else
16908 parm = pattern;
16909
16910 /* Unify the pattern with the current argument. */
16911 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16912 LOOKUP_IMPLICIT, explain_p))
16913 return 1;
16914
16915 /* For each parameter pack, collect the deduced value. */
16916 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16917 {
16918 int idx, level;
16919 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16920
16921 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16922 TMPL_ARG (targs, level, idx);
16923 }
16924 }
16925
16926 /* Verify that the results of unification with the parameter packs
16927 produce results consistent with what we've seen before, and make
16928 the deduced argument packs available. */
16929 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16930 {
16931 tree old_pack = TREE_VALUE (pack);
16932 tree new_args = TREE_TYPE (pack);
16933 int i, len = TREE_VEC_LENGTH (new_args);
16934 int idx, level;
16935 bool nondeduced_p = false;
16936
16937 /* By default keep the original deduced argument pack.
16938 If necessary, more specific code is going to update the
16939 resulting deduced argument later down in this function. */
16940 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16941 TMPL_ARG (targs, level, idx) = old_pack;
16942
16943 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16944 actually deduce anything. */
16945 for (i = 0; i < len && !nondeduced_p; ++i)
16946 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
16947 nondeduced_p = true;
16948 if (nondeduced_p)
16949 continue;
16950
16951 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
16952 {
16953 /* If we had fewer function args than explicit template args,
16954 just use the explicits. */
16955 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16956 int explicit_len = TREE_VEC_LENGTH (explicit_args);
16957 if (len < explicit_len)
16958 new_args = explicit_args;
16959 }
16960
16961 if (!old_pack)
16962 {
16963 tree result;
16964 /* Build the deduced *_ARGUMENT_PACK. */
16965 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
16966 {
16967 result = make_node (NONTYPE_ARGUMENT_PACK);
16968 TREE_TYPE (result) =
16969 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
16970 TREE_CONSTANT (result) = 1;
16971 }
16972 else
16973 result = cxx_make_type (TYPE_ARGUMENT_PACK);
16974
16975 SET_ARGUMENT_PACK_ARGS (result, new_args);
16976
16977 /* Note the deduced argument packs for this parameter
16978 pack. */
16979 TMPL_ARG (targs, level, idx) = result;
16980 }
16981 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
16982 && (ARGUMENT_PACK_ARGS (old_pack)
16983 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
16984 {
16985 /* We only had the explicitly-provided arguments before, but
16986 now we have a complete set of arguments. */
16987 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16988
16989 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
16990 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
16991 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
16992 }
16993 else
16994 {
16995 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
16996 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
16997
16998 if (!comp_template_args_with_info (old_args, new_args,
16999 &bad_old_arg, &bad_new_arg))
17000 /* Inconsistent unification of this parameter pack. */
17001 return unify_parameter_pack_inconsistent (explain_p,
17002 bad_old_arg,
17003 bad_new_arg);
17004 }
17005 }
17006
17007 return unify_success (explain_p);
17008 }
17009
17010 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17011 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17012 parameters and return value are as for unify. */
17013
17014 static int
17015 unify_array_domain (tree tparms, tree targs,
17016 tree parm_dom, tree arg_dom,
17017 bool explain_p)
17018 {
17019 tree parm_max;
17020 tree arg_max;
17021 bool parm_cst;
17022 bool arg_cst;
17023
17024 /* Our representation of array types uses "N - 1" as the
17025 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17026 not an integer constant. We cannot unify arbitrarily
17027 complex expressions, so we eliminate the MINUS_EXPRs
17028 here. */
17029 parm_max = TYPE_MAX_VALUE (parm_dom);
17030 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17031 if (!parm_cst)
17032 {
17033 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17034 parm_max = TREE_OPERAND (parm_max, 0);
17035 }
17036 arg_max = TYPE_MAX_VALUE (arg_dom);
17037 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17038 if (!arg_cst)
17039 {
17040 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17041 trying to unify the type of a variable with the type
17042 of a template parameter. For example:
17043
17044 template <unsigned int N>
17045 void f (char (&) [N]);
17046 int g();
17047 void h(int i) {
17048 char a[g(i)];
17049 f(a);
17050 }
17051
17052 Here, the type of the ARG will be "int [g(i)]", and
17053 may be a SAVE_EXPR, etc. */
17054 if (TREE_CODE (arg_max) != MINUS_EXPR)
17055 return unify_vla_arg (explain_p, arg_dom);
17056 arg_max = TREE_OPERAND (arg_max, 0);
17057 }
17058
17059 /* If only one of the bounds used a MINUS_EXPR, compensate
17060 by adding one to the other bound. */
17061 if (parm_cst && !arg_cst)
17062 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17063 integer_type_node,
17064 parm_max,
17065 integer_one_node);
17066 else if (arg_cst && !parm_cst)
17067 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17068 integer_type_node,
17069 arg_max,
17070 integer_one_node);
17071
17072 return unify (tparms, targs, parm_max, arg_max,
17073 UNIFY_ALLOW_INTEGER, explain_p);
17074 }
17075
17076 /* Deduce the value of template parameters. TPARMS is the (innermost)
17077 set of template parameters to a template. TARGS is the bindings
17078 for those template parameters, as determined thus far; TARGS may
17079 include template arguments for outer levels of template parameters
17080 as well. PARM is a parameter to a template function, or a
17081 subcomponent of that parameter; ARG is the corresponding argument.
17082 This function attempts to match PARM with ARG in a manner
17083 consistent with the existing assignments in TARGS. If more values
17084 are deduced, then TARGS is updated.
17085
17086 Returns 0 if the type deduction succeeds, 1 otherwise. The
17087 parameter STRICT is a bitwise or of the following flags:
17088
17089 UNIFY_ALLOW_NONE:
17090 Require an exact match between PARM and ARG.
17091 UNIFY_ALLOW_MORE_CV_QUAL:
17092 Allow the deduced ARG to be more cv-qualified (by qualification
17093 conversion) than ARG.
17094 UNIFY_ALLOW_LESS_CV_QUAL:
17095 Allow the deduced ARG to be less cv-qualified than ARG.
17096 UNIFY_ALLOW_DERIVED:
17097 Allow the deduced ARG to be a template base class of ARG,
17098 or a pointer to a template base class of the type pointed to by
17099 ARG.
17100 UNIFY_ALLOW_INTEGER:
17101 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17102 case for more information.
17103 UNIFY_ALLOW_OUTER_LEVEL:
17104 This is the outermost level of a deduction. Used to determine validity
17105 of qualification conversions. A valid qualification conversion must
17106 have const qualified pointers leading up to the inner type which
17107 requires additional CV quals, except at the outer level, where const
17108 is not required [conv.qual]. It would be normal to set this flag in
17109 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17110 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17111 This is the outermost level of a deduction, and PARM can be more CV
17112 qualified at this point.
17113 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17114 This is the outermost level of a deduction, and PARM can be less CV
17115 qualified at this point. */
17116
17117 static int
17118 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17119 bool explain_p)
17120 {
17121 int idx;
17122 tree targ;
17123 tree tparm;
17124 int strict_in = strict;
17125
17126 /* I don't think this will do the right thing with respect to types.
17127 But the only case I've seen it in so far has been array bounds, where
17128 signedness is the only information lost, and I think that will be
17129 okay. */
17130 while (TREE_CODE (parm) == NOP_EXPR)
17131 parm = TREE_OPERAND (parm, 0);
17132
17133 if (arg == error_mark_node)
17134 return unify_invalid (explain_p);
17135 if (arg == unknown_type_node
17136 || arg == init_list_type_node)
17137 /* We can't deduce anything from this, but we might get all the
17138 template args from other function args. */
17139 return unify_success (explain_p);
17140
17141 /* If PARM uses template parameters, then we can't bail out here,
17142 even if ARG == PARM, since we won't record unifications for the
17143 template parameters. We might need them if we're trying to
17144 figure out which of two things is more specialized. */
17145 if (arg == parm && !uses_template_parms (parm))
17146 return unify_success (explain_p);
17147
17148 /* Handle init lists early, so the rest of the function can assume
17149 we're dealing with a type. */
17150 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17151 {
17152 tree elt, elttype;
17153 unsigned i;
17154 tree orig_parm = parm;
17155
17156 /* Replace T with std::initializer_list<T> for deduction. */
17157 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17158 && flag_deduce_init_list)
17159 parm = listify (parm);
17160
17161 if (!is_std_init_list (parm)
17162 && TREE_CODE (parm) != ARRAY_TYPE)
17163 /* We can only deduce from an initializer list argument if the
17164 parameter is std::initializer_list or an array; otherwise this
17165 is a non-deduced context. */
17166 return unify_success (explain_p);
17167
17168 if (TREE_CODE (parm) == ARRAY_TYPE)
17169 elttype = TREE_TYPE (parm);
17170 else
17171 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17172
17173 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17174 {
17175 int elt_strict = strict;
17176
17177 if (elt == error_mark_node)
17178 return unify_invalid (explain_p);
17179
17180 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17181 {
17182 tree type = TREE_TYPE (elt);
17183 /* It should only be possible to get here for a call. */
17184 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17185 elt_strict |= maybe_adjust_types_for_deduction
17186 (DEDUCE_CALL, &elttype, &type, elt);
17187 elt = type;
17188 }
17189
17190 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17191 explain_p);
17192 }
17193
17194 if (TREE_CODE (parm) == ARRAY_TYPE)
17195 {
17196 /* Also deduce from the length of the initializer list. */
17197 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17198 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17199 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17200 idx, explain_p);
17201 }
17202
17203 /* If the std::initializer_list<T> deduction worked, replace the
17204 deduced A with std::initializer_list<A>. */
17205 if (orig_parm != parm)
17206 {
17207 idx = TEMPLATE_TYPE_IDX (orig_parm);
17208 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17209 targ = listify (targ);
17210 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17211 }
17212 return unify_success (explain_p);
17213 }
17214
17215 /* Immediately reject some pairs that won't unify because of
17216 cv-qualification mismatches. */
17217 if (TREE_CODE (arg) == TREE_CODE (parm)
17218 && TYPE_P (arg)
17219 /* It is the elements of the array which hold the cv quals of an array
17220 type, and the elements might be template type parms. We'll check
17221 when we recurse. */
17222 && TREE_CODE (arg) != ARRAY_TYPE
17223 /* We check the cv-qualifiers when unifying with template type
17224 parameters below. We want to allow ARG `const T' to unify with
17225 PARM `T' for example, when computing which of two templates
17226 is more specialized, for example. */
17227 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17228 && !check_cv_quals_for_unify (strict_in, arg, parm))
17229 return unify_cv_qual_mismatch (explain_p, parm, arg);
17230
17231 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17232 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17233 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17234 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17235 strict &= ~UNIFY_ALLOW_DERIVED;
17236 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17237 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17238
17239 switch (TREE_CODE (parm))
17240 {
17241 case TYPENAME_TYPE:
17242 case SCOPE_REF:
17243 case UNBOUND_CLASS_TEMPLATE:
17244 /* In a type which contains a nested-name-specifier, template
17245 argument values cannot be deduced for template parameters used
17246 within the nested-name-specifier. */
17247 return unify_success (explain_p);
17248
17249 case TEMPLATE_TYPE_PARM:
17250 case TEMPLATE_TEMPLATE_PARM:
17251 case BOUND_TEMPLATE_TEMPLATE_PARM:
17252 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17253 if (tparm == error_mark_node)
17254 return unify_invalid (explain_p);
17255
17256 if (TEMPLATE_TYPE_LEVEL (parm)
17257 != template_decl_level (tparm))
17258 /* The PARM is not one we're trying to unify. Just check
17259 to see if it matches ARG. */
17260 {
17261 if (TREE_CODE (arg) == TREE_CODE (parm)
17262 && (is_auto (parm) ? is_auto (arg)
17263 : same_type_p (parm, arg)))
17264 return unify_success (explain_p);
17265 else
17266 return unify_type_mismatch (explain_p, parm, arg);
17267 }
17268 idx = TEMPLATE_TYPE_IDX (parm);
17269 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17270 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17271 if (tparm == error_mark_node)
17272 return unify_invalid (explain_p);
17273
17274 /* Check for mixed types and values. */
17275 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17276 && TREE_CODE (tparm) != TYPE_DECL)
17277 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17278 && TREE_CODE (tparm) != TEMPLATE_DECL))
17279 gcc_unreachable ();
17280
17281 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17282 {
17283 /* ARG must be constructed from a template class or a template
17284 template parameter. */
17285 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17286 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17287 return unify_template_deduction_failure (explain_p, parm, arg);
17288
17289 {
17290 tree parmvec = TYPE_TI_ARGS (parm);
17291 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17292 tree full_argvec = add_to_template_args (targs, argvec);
17293 tree parm_parms
17294 = DECL_INNERMOST_TEMPLATE_PARMS
17295 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17296 int i, len;
17297 int parm_variadic_p = 0;
17298
17299 /* The resolution to DR150 makes clear that default
17300 arguments for an N-argument may not be used to bind T
17301 to a template template parameter with fewer than N
17302 parameters. It is not safe to permit the binding of
17303 default arguments as an extension, as that may change
17304 the meaning of a conforming program. Consider:
17305
17306 struct Dense { static const unsigned int dim = 1; };
17307
17308 template <template <typename> class View,
17309 typename Block>
17310 void operator+(float, View<Block> const&);
17311
17312 template <typename Block,
17313 unsigned int Dim = Block::dim>
17314 struct Lvalue_proxy { operator float() const; };
17315
17316 void
17317 test_1d (void) {
17318 Lvalue_proxy<Dense> p;
17319 float b;
17320 b + p;
17321 }
17322
17323 Here, if Lvalue_proxy is permitted to bind to View, then
17324 the global operator+ will be used; if they are not, the
17325 Lvalue_proxy will be converted to float. */
17326 if (coerce_template_parms (parm_parms,
17327 full_argvec,
17328 TYPE_TI_TEMPLATE (parm),
17329 (explain_p
17330 ? tf_warning_or_error
17331 : tf_none),
17332 /*require_all_args=*/true,
17333 /*use_default_args=*/false)
17334 == error_mark_node)
17335 return 1;
17336
17337 /* Deduce arguments T, i from TT<T> or TT<i>.
17338 We check each element of PARMVEC and ARGVEC individually
17339 rather than the whole TREE_VEC since they can have
17340 different number of elements. */
17341
17342 parmvec = expand_template_argument_pack (parmvec);
17343 argvec = expand_template_argument_pack (argvec);
17344
17345 len = TREE_VEC_LENGTH (parmvec);
17346
17347 /* Check if the parameters end in a pack, making them
17348 variadic. */
17349 if (len > 0
17350 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17351 parm_variadic_p = 1;
17352
17353 for (i = 0; i < len - parm_variadic_p; ++i)
17354 /* If the template argument list of P contains a pack
17355 expansion that is not the last template argument, the
17356 entire template argument list is a non-deduced
17357 context. */
17358 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17359 return unify_success (explain_p);
17360
17361 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17362 return unify_too_few_arguments (explain_p,
17363 TREE_VEC_LENGTH (argvec), len);
17364
17365 for (i = 0; i < len - parm_variadic_p; ++i)
17366 {
17367 RECUR_AND_CHECK_FAILURE (tparms, targs,
17368 TREE_VEC_ELT (parmvec, i),
17369 TREE_VEC_ELT (argvec, i),
17370 UNIFY_ALLOW_NONE, explain_p);
17371 }
17372
17373 if (parm_variadic_p
17374 && unify_pack_expansion (tparms, targs,
17375 parmvec, argvec,
17376 DEDUCE_EXACT,
17377 /*subr=*/true, explain_p))
17378 return 1;
17379 }
17380 arg = TYPE_TI_TEMPLATE (arg);
17381
17382 /* Fall through to deduce template name. */
17383 }
17384
17385 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17386 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17387 {
17388 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17389
17390 /* Simple cases: Value already set, does match or doesn't. */
17391 if (targ != NULL_TREE && template_args_equal (targ, arg))
17392 return unify_success (explain_p);
17393 else if (targ)
17394 return unify_inconsistency (explain_p, parm, targ, arg);
17395 }
17396 else
17397 {
17398 /* If PARM is `const T' and ARG is only `int', we don't have
17399 a match unless we are allowing additional qualification.
17400 If ARG is `const int' and PARM is just `T' that's OK;
17401 that binds `const int' to `T'. */
17402 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17403 arg, parm))
17404 return unify_cv_qual_mismatch (explain_p, parm, arg);
17405
17406 /* Consider the case where ARG is `const volatile int' and
17407 PARM is `const T'. Then, T should be `volatile int'. */
17408 arg = cp_build_qualified_type_real
17409 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17410 if (arg == error_mark_node)
17411 return unify_invalid (explain_p);
17412
17413 /* Simple cases: Value already set, does match or doesn't. */
17414 if (targ != NULL_TREE && same_type_p (targ, arg))
17415 return unify_success (explain_p);
17416 else if (targ)
17417 return unify_inconsistency (explain_p, parm, targ, arg);
17418
17419 /* Make sure that ARG is not a variable-sized array. (Note
17420 that were talking about variable-sized arrays (like
17421 `int[n]'), rather than arrays of unknown size (like
17422 `int[]').) We'll get very confused by such a type since
17423 the bound of the array is not constant, and therefore
17424 not mangleable. Besides, such types are not allowed in
17425 ISO C++, so we can do as we please here. We do allow
17426 them for 'auto' deduction, since that isn't ABI-exposed. */
17427 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17428 return unify_vla_arg (explain_p, arg);
17429
17430 /* Strip typedefs as in convert_template_argument. */
17431 arg = canonicalize_type_argument (arg, tf_none);
17432 }
17433
17434 /* If ARG is a parameter pack or an expansion, we cannot unify
17435 against it unless PARM is also a parameter pack. */
17436 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17437 && !template_parameter_pack_p (parm))
17438 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17439
17440 /* If the argument deduction results is a METHOD_TYPE,
17441 then there is a problem.
17442 METHOD_TYPE doesn't map to any real C++ type the result of
17443 the deduction can not be of that type. */
17444 if (TREE_CODE (arg) == METHOD_TYPE)
17445 return unify_method_type_error (explain_p, arg);
17446
17447 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17448 return unify_success (explain_p);
17449
17450 case TEMPLATE_PARM_INDEX:
17451 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17452 if (tparm == error_mark_node)
17453 return unify_invalid (explain_p);
17454
17455 if (TEMPLATE_PARM_LEVEL (parm)
17456 != template_decl_level (tparm))
17457 {
17458 /* The PARM is not one we're trying to unify. Just check
17459 to see if it matches ARG. */
17460 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17461 && cp_tree_equal (parm, arg));
17462 if (result)
17463 unify_expression_unequal (explain_p, parm, arg);
17464 return result;
17465 }
17466
17467 idx = TEMPLATE_PARM_IDX (parm);
17468 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17469
17470 if (targ)
17471 {
17472 int x = !cp_tree_equal (targ, arg);
17473 if (x)
17474 unify_inconsistency (explain_p, parm, targ, arg);
17475 return x;
17476 }
17477
17478 /* [temp.deduct.type] If, in the declaration of a function template
17479 with a non-type template-parameter, the non-type
17480 template-parameter is used in an expression in the function
17481 parameter-list and, if the corresponding template-argument is
17482 deduced, the template-argument type shall match the type of the
17483 template-parameter exactly, except that a template-argument
17484 deduced from an array bound may be of any integral type.
17485 The non-type parameter might use already deduced type parameters. */
17486 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17487 if (!TREE_TYPE (arg))
17488 /* Template-parameter dependent expression. Just accept it for now.
17489 It will later be processed in convert_template_argument. */
17490 ;
17491 else if (same_type_p (TREE_TYPE (arg), tparm))
17492 /* OK */;
17493 else if ((strict & UNIFY_ALLOW_INTEGER)
17494 && CP_INTEGRAL_TYPE_P (tparm))
17495 /* Convert the ARG to the type of PARM; the deduced non-type
17496 template argument must exactly match the types of the
17497 corresponding parameter. */
17498 arg = fold (build_nop (tparm, arg));
17499 else if (uses_template_parms (tparm))
17500 /* We haven't deduced the type of this parameter yet. Try again
17501 later. */
17502 return unify_success (explain_p);
17503 else
17504 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17505
17506 /* If ARG is a parameter pack or an expansion, we cannot unify
17507 against it unless PARM is also a parameter pack. */
17508 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17509 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17510 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17511
17512 arg = strip_typedefs_expr (arg);
17513 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17514 return unify_success (explain_p);
17515
17516 case PTRMEM_CST:
17517 {
17518 /* A pointer-to-member constant can be unified only with
17519 another constant. */
17520 if (TREE_CODE (arg) != PTRMEM_CST)
17521 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17522
17523 /* Just unify the class member. It would be useless (and possibly
17524 wrong, depending on the strict flags) to unify also
17525 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17526 arg refer to the same variable, even if through different
17527 classes. For instance:
17528
17529 struct A { int x; };
17530 struct B : A { };
17531
17532 Unification of &A::x and &B::x must succeed. */
17533 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17534 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17535 }
17536
17537 case POINTER_TYPE:
17538 {
17539 if (!TYPE_PTR_P (arg))
17540 return unify_type_mismatch (explain_p, parm, arg);
17541
17542 /* [temp.deduct.call]
17543
17544 A can be another pointer or pointer to member type that can
17545 be converted to the deduced A via a qualification
17546 conversion (_conv.qual_).
17547
17548 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17549 This will allow for additional cv-qualification of the
17550 pointed-to types if appropriate. */
17551
17552 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17553 /* The derived-to-base conversion only persists through one
17554 level of pointers. */
17555 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17556
17557 return unify (tparms, targs, TREE_TYPE (parm),
17558 TREE_TYPE (arg), strict, explain_p);
17559 }
17560
17561 case REFERENCE_TYPE:
17562 if (TREE_CODE (arg) != REFERENCE_TYPE)
17563 return unify_type_mismatch (explain_p, parm, arg);
17564 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17565 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17566
17567 case ARRAY_TYPE:
17568 if (TREE_CODE (arg) != ARRAY_TYPE)
17569 return unify_type_mismatch (explain_p, parm, arg);
17570 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17571 != (TYPE_DOMAIN (arg) == NULL_TREE))
17572 return unify_type_mismatch (explain_p, parm, arg);
17573 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17574 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17575 if (TYPE_DOMAIN (parm) != NULL_TREE)
17576 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17577 TYPE_DOMAIN (arg), explain_p);
17578 return unify_success (explain_p);
17579
17580 case REAL_TYPE:
17581 case COMPLEX_TYPE:
17582 case VECTOR_TYPE:
17583 case INTEGER_TYPE:
17584 case BOOLEAN_TYPE:
17585 case ENUMERAL_TYPE:
17586 case VOID_TYPE:
17587 case NULLPTR_TYPE:
17588 if (TREE_CODE (arg) != TREE_CODE (parm))
17589 return unify_type_mismatch (explain_p, parm, arg);
17590
17591 /* We have already checked cv-qualification at the top of the
17592 function. */
17593 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17594 return unify_type_mismatch (explain_p, parm, arg);
17595
17596 /* As far as unification is concerned, this wins. Later checks
17597 will invalidate it if necessary. */
17598 return unify_success (explain_p);
17599
17600 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17601 /* Type INTEGER_CST can come from ordinary constant template args. */
17602 case INTEGER_CST:
17603 while (TREE_CODE (arg) == NOP_EXPR)
17604 arg = TREE_OPERAND (arg, 0);
17605
17606 if (TREE_CODE (arg) != INTEGER_CST)
17607 return unify_template_argument_mismatch (explain_p, parm, arg);
17608 return (tree_int_cst_equal (parm, arg)
17609 ? unify_success (explain_p)
17610 : unify_template_argument_mismatch (explain_p, parm, arg));
17611
17612 case TREE_VEC:
17613 {
17614 int i, len, argslen;
17615 int parm_variadic_p = 0;
17616
17617 if (TREE_CODE (arg) != TREE_VEC)
17618 return unify_template_argument_mismatch (explain_p, parm, arg);
17619
17620 len = TREE_VEC_LENGTH (parm);
17621 argslen = TREE_VEC_LENGTH (arg);
17622
17623 /* Check for pack expansions in the parameters. */
17624 for (i = 0; i < len; ++i)
17625 {
17626 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17627 {
17628 if (i == len - 1)
17629 /* We can unify against something with a trailing
17630 parameter pack. */
17631 parm_variadic_p = 1;
17632 else
17633 /* [temp.deduct.type]/9: If the template argument list of
17634 P contains a pack expansion that is not the last
17635 template argument, the entire template argument list
17636 is a non-deduced context. */
17637 return unify_success (explain_p);
17638 }
17639 }
17640
17641 /* If we don't have enough arguments to satisfy the parameters
17642 (not counting the pack expression at the end), or we have
17643 too many arguments for a parameter list that doesn't end in
17644 a pack expression, we can't unify. */
17645 if (parm_variadic_p
17646 ? argslen < len - parm_variadic_p
17647 : argslen != len)
17648 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17649
17650 /* Unify all of the parameters that precede the (optional)
17651 pack expression. */
17652 for (i = 0; i < len - parm_variadic_p; ++i)
17653 {
17654 RECUR_AND_CHECK_FAILURE (tparms, targs,
17655 TREE_VEC_ELT (parm, i),
17656 TREE_VEC_ELT (arg, i),
17657 UNIFY_ALLOW_NONE, explain_p);
17658 }
17659 if (parm_variadic_p)
17660 return unify_pack_expansion (tparms, targs, parm, arg,
17661 DEDUCE_EXACT,
17662 /*subr=*/true, explain_p);
17663 return unify_success (explain_p);
17664 }
17665
17666 case RECORD_TYPE:
17667 case UNION_TYPE:
17668 if (TREE_CODE (arg) != TREE_CODE (parm))
17669 return unify_type_mismatch (explain_p, parm, arg);
17670
17671 if (TYPE_PTRMEMFUNC_P (parm))
17672 {
17673 if (!TYPE_PTRMEMFUNC_P (arg))
17674 return unify_type_mismatch (explain_p, parm, arg);
17675
17676 return unify (tparms, targs,
17677 TYPE_PTRMEMFUNC_FN_TYPE (parm),
17678 TYPE_PTRMEMFUNC_FN_TYPE (arg),
17679 strict, explain_p);
17680 }
17681
17682 if (CLASSTYPE_TEMPLATE_INFO (parm))
17683 {
17684 tree t = NULL_TREE;
17685
17686 if (strict_in & UNIFY_ALLOW_DERIVED)
17687 {
17688 /* First, we try to unify the PARM and ARG directly. */
17689 t = try_class_unification (tparms, targs,
17690 parm, arg, explain_p);
17691
17692 if (!t)
17693 {
17694 /* Fallback to the special case allowed in
17695 [temp.deduct.call]:
17696
17697 If P is a class, and P has the form
17698 template-id, then A can be a derived class of
17699 the deduced A. Likewise, if P is a pointer to
17700 a class of the form template-id, A can be a
17701 pointer to a derived class pointed to by the
17702 deduced A. */
17703 enum template_base_result r;
17704 r = get_template_base (tparms, targs, parm, arg,
17705 explain_p, &t);
17706
17707 if (!t)
17708 return unify_no_common_base (explain_p, r, parm, arg);
17709 }
17710 }
17711 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17712 && (CLASSTYPE_TI_TEMPLATE (parm)
17713 == CLASSTYPE_TI_TEMPLATE (arg)))
17714 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17715 Then, we should unify `int' and `U'. */
17716 t = arg;
17717 else
17718 /* There's no chance of unification succeeding. */
17719 return unify_type_mismatch (explain_p, parm, arg);
17720
17721 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17722 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17723 }
17724 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17725 return unify_type_mismatch (explain_p, parm, arg);
17726 return unify_success (explain_p);
17727
17728 case METHOD_TYPE:
17729 case FUNCTION_TYPE:
17730 {
17731 unsigned int nargs;
17732 tree *args;
17733 tree a;
17734 unsigned int i;
17735
17736 if (TREE_CODE (arg) != TREE_CODE (parm))
17737 return unify_type_mismatch (explain_p, parm, arg);
17738
17739 /* CV qualifications for methods can never be deduced, they must
17740 match exactly. We need to check them explicitly here,
17741 because type_unification_real treats them as any other
17742 cv-qualified parameter. */
17743 if (TREE_CODE (parm) == METHOD_TYPE
17744 && (!check_cv_quals_for_unify
17745 (UNIFY_ALLOW_NONE,
17746 class_of_this_parm (arg),
17747 class_of_this_parm (parm))))
17748 return unify_cv_qual_mismatch (explain_p, parm, arg);
17749
17750 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17751 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17752
17753 nargs = list_length (TYPE_ARG_TYPES (arg));
17754 args = XALLOCAVEC (tree, nargs);
17755 for (a = TYPE_ARG_TYPES (arg), i = 0;
17756 a != NULL_TREE && a != void_list_node;
17757 a = TREE_CHAIN (a), ++i)
17758 args[i] = TREE_VALUE (a);
17759 nargs = i;
17760
17761 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17762 args, nargs, 1, DEDUCE_EXACT,
17763 LOOKUP_NORMAL, NULL, explain_p);
17764 }
17765
17766 case OFFSET_TYPE:
17767 /* Unify a pointer to member with a pointer to member function, which
17768 deduces the type of the member as a function type. */
17769 if (TYPE_PTRMEMFUNC_P (arg))
17770 {
17771 /* Check top-level cv qualifiers */
17772 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17773 return unify_cv_qual_mismatch (explain_p, parm, arg);
17774
17775 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17776 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17777 UNIFY_ALLOW_NONE, explain_p);
17778
17779 /* Determine the type of the function we are unifying against. */
17780 tree fntype = static_fn_type (arg);
17781
17782 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
17783 }
17784
17785 if (TREE_CODE (arg) != OFFSET_TYPE)
17786 return unify_type_mismatch (explain_p, parm, arg);
17787 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17788 TYPE_OFFSET_BASETYPE (arg),
17789 UNIFY_ALLOW_NONE, explain_p);
17790 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17791 strict, explain_p);
17792
17793 case CONST_DECL:
17794 if (DECL_TEMPLATE_PARM_P (parm))
17795 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
17796 if (arg != integral_constant_value (parm))
17797 return unify_template_argument_mismatch (explain_p, parm, arg);
17798 return unify_success (explain_p);
17799
17800 case FIELD_DECL:
17801 case TEMPLATE_DECL:
17802 /* Matched cases are handled by the ARG == PARM test above. */
17803 return unify_template_argument_mismatch (explain_p, parm, arg);
17804
17805 case VAR_DECL:
17806 /* A non-type template parameter that is a variable should be a
17807 an integral constant, in which case, it whould have been
17808 folded into its (constant) value. So we should not be getting
17809 a variable here. */
17810 gcc_unreachable ();
17811
17812 case TYPE_ARGUMENT_PACK:
17813 case NONTYPE_ARGUMENT_PACK:
17814 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
17815 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
17816
17817 case TYPEOF_TYPE:
17818 case DECLTYPE_TYPE:
17819 case UNDERLYING_TYPE:
17820 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17821 or UNDERLYING_TYPE nodes. */
17822 return unify_success (explain_p);
17823
17824 case ERROR_MARK:
17825 /* Unification fails if we hit an error node. */
17826 return unify_invalid (explain_p);
17827
17828 default:
17829 /* An unresolved overload is a nondeduced context. */
17830 if (is_overloaded_fn (parm) || type_unknown_p (parm))
17831 return unify_success (explain_p);
17832 gcc_assert (EXPR_P (parm));
17833
17834 /* We must be looking at an expression. This can happen with
17835 something like:
17836
17837 template <int I>
17838 void foo(S<I>, S<I + 2>);
17839
17840 This is a "nondeduced context":
17841
17842 [deduct.type]
17843
17844 The nondeduced contexts are:
17845
17846 --A type that is a template-id in which one or more of
17847 the template-arguments is an expression that references
17848 a template-parameter.
17849
17850 In these cases, we assume deduction succeeded, but don't
17851 actually infer any unifications. */
17852
17853 if (!uses_template_parms (parm)
17854 && !template_args_equal (parm, arg))
17855 return unify_expression_unequal (explain_p, parm, arg);
17856 else
17857 return unify_success (explain_p);
17858 }
17859 }
17860 #undef RECUR_AND_CHECK_FAILURE
17861 \f
17862 /* Note that DECL can be defined in this translation unit, if
17863 required. */
17864
17865 static void
17866 mark_definable (tree decl)
17867 {
17868 tree clone;
17869 DECL_NOT_REALLY_EXTERN (decl) = 1;
17870 FOR_EACH_CLONE (clone, decl)
17871 DECL_NOT_REALLY_EXTERN (clone) = 1;
17872 }
17873
17874 /* Called if RESULT is explicitly instantiated, or is a member of an
17875 explicitly instantiated class. */
17876
17877 void
17878 mark_decl_instantiated (tree result, int extern_p)
17879 {
17880 SET_DECL_EXPLICIT_INSTANTIATION (result);
17881
17882 /* If this entity has already been written out, it's too late to
17883 make any modifications. */
17884 if (TREE_ASM_WRITTEN (result))
17885 return;
17886
17887 /* For anonymous namespace we don't need to do anything. */
17888 if (decl_anon_ns_mem_p (result))
17889 {
17890 gcc_assert (!TREE_PUBLIC (result));
17891 return;
17892 }
17893
17894 if (TREE_CODE (result) != FUNCTION_DECL)
17895 /* The TREE_PUBLIC flag for function declarations will have been
17896 set correctly by tsubst. */
17897 TREE_PUBLIC (result) = 1;
17898
17899 /* This might have been set by an earlier implicit instantiation. */
17900 DECL_COMDAT (result) = 0;
17901
17902 if (extern_p)
17903 DECL_NOT_REALLY_EXTERN (result) = 0;
17904 else
17905 {
17906 mark_definable (result);
17907 /* Always make artificials weak. */
17908 if (DECL_ARTIFICIAL (result) && flag_weak)
17909 comdat_linkage (result);
17910 /* For WIN32 we also want to put explicit instantiations in
17911 linkonce sections. */
17912 else if (TREE_PUBLIC (result))
17913 maybe_make_one_only (result);
17914 }
17915
17916 /* If EXTERN_P, then this function will not be emitted -- unless
17917 followed by an explicit instantiation, at which point its linkage
17918 will be adjusted. If !EXTERN_P, then this function will be
17919 emitted here. In neither circumstance do we want
17920 import_export_decl to adjust the linkage. */
17921 DECL_INTERFACE_KNOWN (result) = 1;
17922 }
17923
17924 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17925 important template arguments. If any are missing, we check whether
17926 they're important by using error_mark_node for substituting into any
17927 args that were used for partial ordering (the ones between ARGS and END)
17928 and seeing if it bubbles up. */
17929
17930 static bool
17931 check_undeduced_parms (tree targs, tree args, tree end)
17932 {
17933 bool found = false;
17934 int i;
17935 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
17936 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
17937 {
17938 found = true;
17939 TREE_VEC_ELT (targs, i) = error_mark_node;
17940 }
17941 if (found)
17942 {
17943 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
17944 if (substed == error_mark_node)
17945 return true;
17946 }
17947 return false;
17948 }
17949
17950 /* Given two function templates PAT1 and PAT2, return:
17951
17952 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17953 -1 if PAT2 is more specialized than PAT1.
17954 0 if neither is more specialized.
17955
17956 LEN indicates the number of parameters we should consider
17957 (defaulted parameters should not be considered).
17958
17959 The 1998 std underspecified function template partial ordering, and
17960 DR214 addresses the issue. We take pairs of arguments, one from
17961 each of the templates, and deduce them against each other. One of
17962 the templates will be more specialized if all the *other*
17963 template's arguments deduce against its arguments and at least one
17964 of its arguments *does* *not* deduce against the other template's
17965 corresponding argument. Deduction is done as for class templates.
17966 The arguments used in deduction have reference and top level cv
17967 qualifiers removed. Iff both arguments were originally reference
17968 types *and* deduction succeeds in both directions, an lvalue reference
17969 wins against an rvalue reference and otherwise the template
17970 with the more cv-qualified argument wins for that pairing (if
17971 neither is more cv-qualified, they both are equal). Unlike regular
17972 deduction, after all the arguments have been deduced in this way,
17973 we do *not* verify the deduced template argument values can be
17974 substituted into non-deduced contexts.
17975
17976 The logic can be a bit confusing here, because we look at deduce1 and
17977 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17978 can find template arguments for pat1 to make arg1 look like arg2, that
17979 means that arg2 is at least as specialized as arg1. */
17980
17981 int
17982 more_specialized_fn (tree pat1, tree pat2, int len)
17983 {
17984 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
17985 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
17986 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
17987 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
17988 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
17989 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
17990 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
17991 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
17992 tree origs1, origs2;
17993 bool lose1 = false;
17994 bool lose2 = false;
17995
17996 /* Remove the this parameter from non-static member functions. If
17997 one is a non-static member function and the other is not a static
17998 member function, remove the first parameter from that function
17999 also. This situation occurs for operator functions where we
18000 locate both a member function (with this pointer) and non-member
18001 operator (with explicit first operand). */
18002 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18003 {
18004 len--; /* LEN is the number of significant arguments for DECL1 */
18005 args1 = TREE_CHAIN (args1);
18006 if (!DECL_STATIC_FUNCTION_P (decl2))
18007 args2 = TREE_CHAIN (args2);
18008 }
18009 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18010 {
18011 args2 = TREE_CHAIN (args2);
18012 if (!DECL_STATIC_FUNCTION_P (decl1))
18013 {
18014 len--;
18015 args1 = TREE_CHAIN (args1);
18016 }
18017 }
18018
18019 /* If only one is a conversion operator, they are unordered. */
18020 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18021 return 0;
18022
18023 /* Consider the return type for a conversion function */
18024 if (DECL_CONV_FN_P (decl1))
18025 {
18026 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18027 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18028 len++;
18029 }
18030
18031 processing_template_decl++;
18032
18033 origs1 = args1;
18034 origs2 = args2;
18035
18036 while (len--
18037 /* Stop when an ellipsis is seen. */
18038 && args1 != NULL_TREE && args2 != NULL_TREE)
18039 {
18040 tree arg1 = TREE_VALUE (args1);
18041 tree arg2 = TREE_VALUE (args2);
18042 int deduce1, deduce2;
18043 int quals1 = -1;
18044 int quals2 = -1;
18045 int ref1 = 0;
18046 int ref2 = 0;
18047
18048 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18049 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18050 {
18051 /* When both arguments are pack expansions, we need only
18052 unify the patterns themselves. */
18053 arg1 = PACK_EXPANSION_PATTERN (arg1);
18054 arg2 = PACK_EXPANSION_PATTERN (arg2);
18055
18056 /* This is the last comparison we need to do. */
18057 len = 0;
18058 }
18059
18060 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18061 {
18062 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18063 arg1 = TREE_TYPE (arg1);
18064 quals1 = cp_type_quals (arg1);
18065 }
18066
18067 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18068 {
18069 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18070 arg2 = TREE_TYPE (arg2);
18071 quals2 = cp_type_quals (arg2);
18072 }
18073
18074 arg1 = TYPE_MAIN_VARIANT (arg1);
18075 arg2 = TYPE_MAIN_VARIANT (arg2);
18076
18077 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18078 {
18079 int i, len2 = list_length (args2);
18080 tree parmvec = make_tree_vec (1);
18081 tree argvec = make_tree_vec (len2);
18082 tree ta = args2;
18083
18084 /* Setup the parameter vector, which contains only ARG1. */
18085 TREE_VEC_ELT (parmvec, 0) = arg1;
18086
18087 /* Setup the argument vector, which contains the remaining
18088 arguments. */
18089 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18090 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18091
18092 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18093 argvec, DEDUCE_EXACT,
18094 /*subr=*/true, /*explain_p=*/false)
18095 == 0);
18096
18097 /* We cannot deduce in the other direction, because ARG1 is
18098 a pack expansion but ARG2 is not. */
18099 deduce2 = 0;
18100 }
18101 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18102 {
18103 int i, len1 = list_length (args1);
18104 tree parmvec = make_tree_vec (1);
18105 tree argvec = make_tree_vec (len1);
18106 tree ta = args1;
18107
18108 /* Setup the parameter vector, which contains only ARG1. */
18109 TREE_VEC_ELT (parmvec, 0) = arg2;
18110
18111 /* Setup the argument vector, which contains the remaining
18112 arguments. */
18113 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18114 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18115
18116 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18117 argvec, DEDUCE_EXACT,
18118 /*subr=*/true, /*explain_p=*/false)
18119 == 0);
18120
18121 /* We cannot deduce in the other direction, because ARG2 is
18122 a pack expansion but ARG1 is not.*/
18123 deduce1 = 0;
18124 }
18125
18126 else
18127 {
18128 /* The normal case, where neither argument is a pack
18129 expansion. */
18130 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18131 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18132 == 0);
18133 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18134 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18135 == 0);
18136 }
18137
18138 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18139 arg2, then arg2 is not as specialized as arg1. */
18140 if (!deduce1)
18141 lose2 = true;
18142 if (!deduce2)
18143 lose1 = true;
18144
18145 /* "If, for a given type, deduction succeeds in both directions
18146 (i.e., the types are identical after the transformations above)
18147 and both P and A were reference types (before being replaced with
18148 the type referred to above):
18149 - if the type from the argument template was an lvalue reference and
18150 the type from the parameter template was not, the argument type is
18151 considered to be more specialized than the other; otherwise,
18152 - if the type from the argument template is more cv-qualified
18153 than the type from the parameter template (as described above),
18154 the argument type is considered to be more specialized than the other;
18155 otherwise,
18156 - neither type is more specialized than the other." */
18157
18158 if (deduce1 && deduce2)
18159 {
18160 if (ref1 && ref2 && ref1 != ref2)
18161 {
18162 if (ref1 > ref2)
18163 lose1 = true;
18164 else
18165 lose2 = true;
18166 }
18167 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18168 {
18169 if ((quals1 & quals2) == quals2)
18170 lose2 = true;
18171 if ((quals1 & quals2) == quals1)
18172 lose1 = true;
18173 }
18174 }
18175
18176 if (lose1 && lose2)
18177 /* We've failed to deduce something in either direction.
18178 These must be unordered. */
18179 break;
18180
18181 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18182 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18183 /* We have already processed all of the arguments in our
18184 handing of the pack expansion type. */
18185 len = 0;
18186
18187 args1 = TREE_CHAIN (args1);
18188 args2 = TREE_CHAIN (args2);
18189 }
18190
18191 /* "In most cases, all template parameters must have values in order for
18192 deduction to succeed, but for partial ordering purposes a template
18193 parameter may remain without a value provided it is not used in the
18194 types being used for partial ordering."
18195
18196 Thus, if we are missing any of the targs1 we need to substitute into
18197 origs1, then pat2 is not as specialized as pat1. This can happen when
18198 there is a nondeduced context. */
18199 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18200 lose2 = true;
18201 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18202 lose1 = true;
18203
18204 processing_template_decl--;
18205
18206 /* All things being equal, if the next argument is a pack expansion
18207 for one function but not for the other, prefer the
18208 non-variadic function. FIXME this is bogus; see c++/41958. */
18209 if (lose1 == lose2
18210 && args1 && TREE_VALUE (args1)
18211 && args2 && TREE_VALUE (args2))
18212 {
18213 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18214 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18215 }
18216
18217 if (lose1 == lose2)
18218 return 0;
18219 else if (!lose1)
18220 return 1;
18221 else
18222 return -1;
18223 }
18224
18225 /* Determine which of two partial specializations of MAIN_TMPL is more
18226 specialized.
18227
18228 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18229 to the first partial specialization. The TREE_VALUE is the
18230 innermost set of template parameters for the partial
18231 specialization. PAT2 is similar, but for the second template.
18232
18233 Return 1 if the first partial specialization is more specialized;
18234 -1 if the second is more specialized; 0 if neither is more
18235 specialized.
18236
18237 See [temp.class.order] for information about determining which of
18238 two templates is more specialized. */
18239
18240 static int
18241 more_specialized_class (tree main_tmpl, tree pat1, tree pat2)
18242 {
18243 tree targs;
18244 tree tmpl1, tmpl2;
18245 int winner = 0;
18246 bool any_deductions = false;
18247
18248 tmpl1 = TREE_TYPE (pat1);
18249 tmpl2 = TREE_TYPE (pat2);
18250
18251 /* Just like what happens for functions, if we are ordering between
18252 different class template specializations, we may encounter dependent
18253 types in the arguments, and we need our dependency check functions
18254 to behave correctly. */
18255 ++processing_template_decl;
18256 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat1),
18257 CLASSTYPE_TI_ARGS (tmpl1),
18258 CLASSTYPE_TI_ARGS (tmpl2));
18259 if (targs)
18260 {
18261 --winner;
18262 any_deductions = true;
18263 }
18264
18265 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat2),
18266 CLASSTYPE_TI_ARGS (tmpl2),
18267 CLASSTYPE_TI_ARGS (tmpl1));
18268 if (targs)
18269 {
18270 ++winner;
18271 any_deductions = true;
18272 }
18273 --processing_template_decl;
18274
18275 /* In the case of a tie where at least one of the class templates
18276 has a parameter pack at the end, the template with the most
18277 non-packed parameters wins. */
18278 if (winner == 0
18279 && any_deductions
18280 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18281 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18282 {
18283 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18284 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18285 int len1 = TREE_VEC_LENGTH (args1);
18286 int len2 = TREE_VEC_LENGTH (args2);
18287
18288 /* We don't count the pack expansion at the end. */
18289 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18290 --len1;
18291 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18292 --len2;
18293
18294 if (len1 > len2)
18295 return 1;
18296 else if (len1 < len2)
18297 return -1;
18298 }
18299
18300 return winner;
18301 }
18302
18303 /* Return the template arguments that will produce the function signature
18304 DECL from the function template FN, with the explicit template
18305 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18306 also match. Return NULL_TREE if no satisfactory arguments could be
18307 found. */
18308
18309 static tree
18310 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18311 {
18312 int ntparms = DECL_NTPARMS (fn);
18313 tree targs = make_tree_vec (ntparms);
18314 tree decl_type = TREE_TYPE (decl);
18315 tree decl_arg_types;
18316 tree *args;
18317 unsigned int nargs, ix;
18318 tree arg;
18319
18320 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18321
18322 /* Never do unification on the 'this' parameter. */
18323 decl_arg_types = skip_artificial_parms_for (decl,
18324 TYPE_ARG_TYPES (decl_type));
18325
18326 nargs = list_length (decl_arg_types);
18327 args = XALLOCAVEC (tree, nargs);
18328 for (arg = decl_arg_types, ix = 0;
18329 arg != NULL_TREE && arg != void_list_node;
18330 arg = TREE_CHAIN (arg), ++ix)
18331 args[ix] = TREE_VALUE (arg);
18332
18333 if (fn_type_unification (fn, explicit_args, targs,
18334 args, ix,
18335 (check_rettype || DECL_CONV_FN_P (fn)
18336 ? TREE_TYPE (decl_type) : NULL_TREE),
18337 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18338 /*decltype*/false)
18339 == error_mark_node)
18340 return NULL_TREE;
18341
18342 return targs;
18343 }
18344
18345 /* Return the innermost template arguments that, when applied to a partial
18346 specialization of MAIN_TMPL whose innermost template parameters are
18347 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18348 ARGS.
18349
18350 For example, suppose we have:
18351
18352 template <class T, class U> struct S {};
18353 template <class T> struct S<T*, int> {};
18354
18355 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18356 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18357 int}. The resulting vector will be {double}, indicating that `T'
18358 is bound to `double'. */
18359
18360 static tree
18361 get_class_bindings (tree main_tmpl, tree tparms, tree spec_args, tree args)
18362 {
18363 int i, ntparms = TREE_VEC_LENGTH (tparms);
18364 tree deduced_args;
18365 tree innermost_deduced_args;
18366
18367 innermost_deduced_args = make_tree_vec (ntparms);
18368 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18369 {
18370 deduced_args = copy_node (args);
18371 SET_TMPL_ARGS_LEVEL (deduced_args,
18372 TMPL_ARGS_DEPTH (deduced_args),
18373 innermost_deduced_args);
18374 }
18375 else
18376 deduced_args = innermost_deduced_args;
18377
18378 if (unify (tparms, deduced_args,
18379 INNERMOST_TEMPLATE_ARGS (spec_args),
18380 INNERMOST_TEMPLATE_ARGS (args),
18381 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18382 return NULL_TREE;
18383
18384 for (i = 0; i < ntparms; ++i)
18385 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18386 return NULL_TREE;
18387
18388 /* Verify that nondeduced template arguments agree with the type
18389 obtained from argument deduction.
18390
18391 For example:
18392
18393 struct A { typedef int X; };
18394 template <class T, class U> struct C {};
18395 template <class T> struct C<T, typename T::X> {};
18396
18397 Then with the instantiation `C<A, int>', we can deduce that
18398 `T' is `A' but unify () does not check whether `typename T::X'
18399 is `int'. */
18400 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18401 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl),
18402 spec_args, main_tmpl,
18403 tf_none, false, false);
18404 if (spec_args == error_mark_node
18405 /* We only need to check the innermost arguments; the other
18406 arguments will always agree. */
18407 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18408 INNERMOST_TEMPLATE_ARGS (args)))
18409 return NULL_TREE;
18410
18411 /* Now that we have bindings for all of the template arguments,
18412 ensure that the arguments deduced for the template template
18413 parameters have compatible template parameter lists. See the use
18414 of template_template_parm_bindings_ok_p in fn_type_unification
18415 for more information. */
18416 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18417 return NULL_TREE;
18418
18419 return deduced_args;
18420 }
18421
18422 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18423 Return the TREE_LIST node with the most specialized template, if
18424 any. If there is no most specialized template, the error_mark_node
18425 is returned.
18426
18427 Note that this function does not look at, or modify, the
18428 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18429 returned is one of the elements of INSTANTIATIONS, callers may
18430 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18431 and retrieve it from the value returned. */
18432
18433 tree
18434 most_specialized_instantiation (tree templates)
18435 {
18436 tree fn, champ;
18437
18438 ++processing_template_decl;
18439
18440 champ = templates;
18441 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18442 {
18443 int fate = 0;
18444
18445 if (get_bindings (TREE_VALUE (champ),
18446 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18447 NULL_TREE, /*check_ret=*/true))
18448 fate--;
18449
18450 if (get_bindings (TREE_VALUE (fn),
18451 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18452 NULL_TREE, /*check_ret=*/true))
18453 fate++;
18454
18455 if (fate == -1)
18456 champ = fn;
18457 else if (!fate)
18458 {
18459 /* Equally specialized, move to next function. If there
18460 is no next function, nothing's most specialized. */
18461 fn = TREE_CHAIN (fn);
18462 champ = fn;
18463 if (!fn)
18464 break;
18465 }
18466 }
18467
18468 if (champ)
18469 /* Now verify that champ is better than everything earlier in the
18470 instantiation list. */
18471 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18472 if (get_bindings (TREE_VALUE (champ),
18473 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18474 NULL_TREE, /*check_ret=*/true)
18475 || !get_bindings (TREE_VALUE (fn),
18476 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18477 NULL_TREE, /*check_ret=*/true))
18478 {
18479 champ = NULL_TREE;
18480 break;
18481 }
18482
18483 processing_template_decl--;
18484
18485 if (!champ)
18486 return error_mark_node;
18487
18488 return champ;
18489 }
18490
18491 /* If DECL is a specialization of some template, return the most
18492 general such template. Otherwise, returns NULL_TREE.
18493
18494 For example, given:
18495
18496 template <class T> struct S { template <class U> void f(U); };
18497
18498 if TMPL is `template <class U> void S<int>::f(U)' this will return
18499 the full template. This function will not trace past partial
18500 specializations, however. For example, given in addition:
18501
18502 template <class T> struct S<T*> { template <class U> void f(U); };
18503
18504 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18505 `template <class T> template <class U> S<T*>::f(U)'. */
18506
18507 tree
18508 most_general_template (tree decl)
18509 {
18510 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18511 an immediate specialization. */
18512 if (TREE_CODE (decl) == FUNCTION_DECL)
18513 {
18514 if (DECL_TEMPLATE_INFO (decl)) {
18515 decl = DECL_TI_TEMPLATE (decl);
18516
18517 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18518 template friend. */
18519 if (TREE_CODE (decl) != TEMPLATE_DECL)
18520 return NULL_TREE;
18521 } else
18522 return NULL_TREE;
18523 }
18524
18525 /* Look for more and more general templates. */
18526 while (DECL_TEMPLATE_INFO (decl))
18527 {
18528 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18529 (See cp-tree.h for details.) */
18530 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18531 break;
18532
18533 if (CLASS_TYPE_P (TREE_TYPE (decl))
18534 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18535 break;
18536
18537 /* Stop if we run into an explicitly specialized class template. */
18538 if (!DECL_NAMESPACE_SCOPE_P (decl)
18539 && DECL_CONTEXT (decl)
18540 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18541 break;
18542
18543 decl = DECL_TI_TEMPLATE (decl);
18544 }
18545
18546 return decl;
18547 }
18548
18549 /* Return the most specialized of the class template partial
18550 specializations of TMPL which can produce TYPE, a specialization of
18551 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
18552 a _TYPE node corresponding to the partial specialization, while the
18553 TREE_PURPOSE is the set of template arguments that must be
18554 substituted into the TREE_TYPE in order to generate TYPE.
18555
18556 If the choice of partial specialization is ambiguous, a diagnostic
18557 is issued, and the error_mark_node is returned. If there are no
18558 partial specializations of TMPL matching TYPE, then NULL_TREE is
18559 returned. */
18560
18561 static tree
18562 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
18563 {
18564 tree list = NULL_TREE;
18565 tree t;
18566 tree champ;
18567 int fate;
18568 bool ambiguous_p;
18569 tree args;
18570 tree outer_args = NULL_TREE;
18571
18572 tmpl = most_general_template (tmpl);
18573 args = CLASSTYPE_TI_ARGS (type);
18574
18575 /* For determining which partial specialization to use, only the
18576 innermost args are interesting. */
18577 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18578 {
18579 outer_args = strip_innermost_template_args (args, 1);
18580 args = INNERMOST_TEMPLATE_ARGS (args);
18581 }
18582
18583 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
18584 {
18585 tree partial_spec_args;
18586 tree spec_args;
18587 tree spec_tmpl = TREE_VALUE (t);
18588 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18589
18590 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18591
18592 ++processing_template_decl;
18593
18594 if (outer_args)
18595 {
18596 /* Discard the outer levels of args, and then substitute in the
18597 template args from the enclosing class. */
18598 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18599 partial_spec_args = tsubst_template_args
18600 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18601
18602 /* And the same for the partial specialization TEMPLATE_DECL. */
18603 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18604 }
18605
18606 partial_spec_args =
18607 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18608 add_to_template_args (outer_args,
18609 partial_spec_args),
18610 tmpl, tf_none,
18611 /*require_all_args=*/true,
18612 /*use_default_args=*/true);
18613
18614 --processing_template_decl;
18615
18616 if (partial_spec_args == error_mark_node)
18617 return error_mark_node;
18618 if (spec_tmpl == error_mark_node)
18619 return error_mark_node;
18620
18621 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18622 spec_args = get_class_bindings (tmpl, parms,
18623 partial_spec_args,
18624 args);
18625 if (spec_args)
18626 {
18627 if (outer_args)
18628 spec_args = add_to_template_args (outer_args, spec_args);
18629 list = tree_cons (spec_args, orig_parms, list);
18630 TREE_TYPE (list) = TREE_TYPE (t);
18631 }
18632 }
18633
18634 if (! list)
18635 return NULL_TREE;
18636
18637 ambiguous_p = false;
18638 t = list;
18639 champ = t;
18640 t = TREE_CHAIN (t);
18641 for (; t; t = TREE_CHAIN (t))
18642 {
18643 fate = more_specialized_class (tmpl, champ, t);
18644 if (fate == 1)
18645 ;
18646 else
18647 {
18648 if (fate == 0)
18649 {
18650 t = TREE_CHAIN (t);
18651 if (! t)
18652 {
18653 ambiguous_p = true;
18654 break;
18655 }
18656 }
18657 champ = t;
18658 }
18659 }
18660
18661 if (!ambiguous_p)
18662 for (t = list; t && t != champ; t = TREE_CHAIN (t))
18663 {
18664 fate = more_specialized_class (tmpl, champ, t);
18665 if (fate != 1)
18666 {
18667 ambiguous_p = true;
18668 break;
18669 }
18670 }
18671
18672 if (ambiguous_p)
18673 {
18674 const char *str;
18675 char *spaces = NULL;
18676 if (!(complain & tf_error))
18677 return error_mark_node;
18678 error ("ambiguous class template instantiation for %q#T", type);
18679 str = ngettext ("candidate is:", "candidates are:", list_length (list));
18680 for (t = list; t; t = TREE_CHAIN (t))
18681 {
18682 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
18683 spaces = spaces ? spaces : get_spaces (str);
18684 }
18685 free (spaces);
18686 return error_mark_node;
18687 }
18688
18689 return champ;
18690 }
18691
18692 /* Explicitly instantiate DECL. */
18693
18694 void
18695 do_decl_instantiation (tree decl, tree storage)
18696 {
18697 tree result = NULL_TREE;
18698 int extern_p = 0;
18699
18700 if (!decl || decl == error_mark_node)
18701 /* An error occurred, for which grokdeclarator has already issued
18702 an appropriate message. */
18703 return;
18704 else if (! DECL_LANG_SPECIFIC (decl))
18705 {
18706 error ("explicit instantiation of non-template %q#D", decl);
18707 return;
18708 }
18709 else if (VAR_P (decl))
18710 {
18711 /* There is an asymmetry here in the way VAR_DECLs and
18712 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18713 the latter, the DECL we get back will be marked as a
18714 template instantiation, and the appropriate
18715 DECL_TEMPLATE_INFO will be set up. This does not happen for
18716 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18717 should handle VAR_DECLs as it currently handles
18718 FUNCTION_DECLs. */
18719 if (!DECL_CLASS_SCOPE_P (decl))
18720 {
18721 error ("%qD is not a static data member of a class template", decl);
18722 return;
18723 }
18724 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18725 if (!result || !VAR_P (result))
18726 {
18727 error ("no matching template for %qD found", decl);
18728 return;
18729 }
18730 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18731 {
18732 error ("type %qT for explicit instantiation %qD does not match "
18733 "declared type %qT", TREE_TYPE (result), decl,
18734 TREE_TYPE (decl));
18735 return;
18736 }
18737 }
18738 else if (TREE_CODE (decl) != FUNCTION_DECL)
18739 {
18740 error ("explicit instantiation of %q#D", decl);
18741 return;
18742 }
18743 else
18744 result = decl;
18745
18746 /* Check for various error cases. Note that if the explicit
18747 instantiation is valid the RESULT will currently be marked as an
18748 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18749 until we get here. */
18750
18751 if (DECL_TEMPLATE_SPECIALIZATION (result))
18752 {
18753 /* DR 259 [temp.spec].
18754
18755 Both an explicit instantiation and a declaration of an explicit
18756 specialization shall not appear in a program unless the explicit
18757 instantiation follows a declaration of the explicit specialization.
18758
18759 For a given set of template parameters, if an explicit
18760 instantiation of a template appears after a declaration of an
18761 explicit specialization for that template, the explicit
18762 instantiation has no effect. */
18763 return;
18764 }
18765 else if (DECL_EXPLICIT_INSTANTIATION (result))
18766 {
18767 /* [temp.spec]
18768
18769 No program shall explicitly instantiate any template more
18770 than once.
18771
18772 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18773 the first instantiation was `extern' and the second is not,
18774 and EXTERN_P for the opposite case. */
18775 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
18776 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
18777 /* If an "extern" explicit instantiation follows an ordinary
18778 explicit instantiation, the template is instantiated. */
18779 if (extern_p)
18780 return;
18781 }
18782 else if (!DECL_IMPLICIT_INSTANTIATION (result))
18783 {
18784 error ("no matching template for %qD found", result);
18785 return;
18786 }
18787 else if (!DECL_TEMPLATE_INFO (result))
18788 {
18789 permerror (input_location, "explicit instantiation of non-template %q#D", result);
18790 return;
18791 }
18792
18793 if (storage == NULL_TREE)
18794 ;
18795 else if (storage == ridpointers[(int) RID_EXTERN])
18796 {
18797 if (!in_system_header && (cxx_dialect == cxx98))
18798 pedwarn (input_location, OPT_Wpedantic,
18799 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18800 "instantiations");
18801 extern_p = 1;
18802 }
18803 else
18804 error ("storage class %qD applied to template instantiation", storage);
18805
18806 check_explicit_instantiation_namespace (result);
18807 mark_decl_instantiated (result, extern_p);
18808 if (! extern_p)
18809 instantiate_decl (result, /*defer_ok=*/1,
18810 /*expl_inst_class_mem_p=*/false);
18811 }
18812
18813 static void
18814 mark_class_instantiated (tree t, int extern_p)
18815 {
18816 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
18817 SET_CLASSTYPE_INTERFACE_KNOWN (t);
18818 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
18819 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
18820 if (! extern_p)
18821 {
18822 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
18823 rest_of_type_compilation (t, 1);
18824 }
18825 }
18826
18827 /* Called from do_type_instantiation through binding_table_foreach to
18828 do recursive instantiation for the type bound in ENTRY. */
18829 static void
18830 bt_instantiate_type_proc (binding_entry entry, void *data)
18831 {
18832 tree storage = *(tree *) data;
18833
18834 if (MAYBE_CLASS_TYPE_P (entry->type)
18835 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
18836 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
18837 }
18838
18839 /* Called from do_type_instantiation to instantiate a member
18840 (a member function or a static member variable) of an
18841 explicitly instantiated class template. */
18842 static void
18843 instantiate_class_member (tree decl, int extern_p)
18844 {
18845 mark_decl_instantiated (decl, extern_p);
18846 if (! extern_p)
18847 instantiate_decl (decl, /*defer_ok=*/1,
18848 /*expl_inst_class_mem_p=*/true);
18849 }
18850
18851 /* Perform an explicit instantiation of template class T. STORAGE, if
18852 non-null, is the RID for extern, inline or static. COMPLAIN is
18853 nonzero if this is called from the parser, zero if called recursively,
18854 since the standard is unclear (as detailed below). */
18855
18856 void
18857 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
18858 {
18859 int extern_p = 0;
18860 int nomem_p = 0;
18861 int static_p = 0;
18862 int previous_instantiation_extern_p = 0;
18863
18864 if (TREE_CODE (t) == TYPE_DECL)
18865 t = TREE_TYPE (t);
18866
18867 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
18868 {
18869 tree tmpl =
18870 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
18871 if (tmpl)
18872 error ("explicit instantiation of non-class template %qD", tmpl);
18873 else
18874 error ("explicit instantiation of non-template type %qT", t);
18875 return;
18876 }
18877
18878 complete_type (t);
18879
18880 if (!COMPLETE_TYPE_P (t))
18881 {
18882 if (complain & tf_error)
18883 error ("explicit instantiation of %q#T before definition of template",
18884 t);
18885 return;
18886 }
18887
18888 if (storage != NULL_TREE)
18889 {
18890 if (!in_system_header)
18891 {
18892 if (storage == ridpointers[(int) RID_EXTERN])
18893 {
18894 if (cxx_dialect == cxx98)
18895 pedwarn (input_location, OPT_Wpedantic,
18896 "ISO C++ 1998 forbids the use of %<extern%> on "
18897 "explicit instantiations");
18898 }
18899 else
18900 pedwarn (input_location, OPT_Wpedantic,
18901 "ISO C++ forbids the use of %qE"
18902 " on explicit instantiations", storage);
18903 }
18904
18905 if (storage == ridpointers[(int) RID_INLINE])
18906 nomem_p = 1;
18907 else if (storage == ridpointers[(int) RID_EXTERN])
18908 extern_p = 1;
18909 else if (storage == ridpointers[(int) RID_STATIC])
18910 static_p = 1;
18911 else
18912 {
18913 error ("storage class %qD applied to template instantiation",
18914 storage);
18915 extern_p = 0;
18916 }
18917 }
18918
18919 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
18920 {
18921 /* DR 259 [temp.spec].
18922
18923 Both an explicit instantiation and a declaration of an explicit
18924 specialization shall not appear in a program unless the explicit
18925 instantiation follows a declaration of the explicit specialization.
18926
18927 For a given set of template parameters, if an explicit
18928 instantiation of a template appears after a declaration of an
18929 explicit specialization for that template, the explicit
18930 instantiation has no effect. */
18931 return;
18932 }
18933 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
18934 {
18935 /* [temp.spec]
18936
18937 No program shall explicitly instantiate any template more
18938 than once.
18939
18940 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18941 instantiation was `extern'. If EXTERN_P then the second is.
18942 These cases are OK. */
18943 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
18944
18945 if (!previous_instantiation_extern_p && !extern_p
18946 && (complain & tf_error))
18947 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
18948
18949 /* If we've already instantiated the template, just return now. */
18950 if (!CLASSTYPE_INTERFACE_ONLY (t))
18951 return;
18952 }
18953
18954 check_explicit_instantiation_namespace (TYPE_NAME (t));
18955 mark_class_instantiated (t, extern_p);
18956
18957 if (nomem_p)
18958 return;
18959
18960 {
18961 tree tmp;
18962
18963 /* In contrast to implicit instantiation, where only the
18964 declarations, and not the definitions, of members are
18965 instantiated, we have here:
18966
18967 [temp.explicit]
18968
18969 The explicit instantiation of a class template specialization
18970 implies the instantiation of all of its members not
18971 previously explicitly specialized in the translation unit
18972 containing the explicit instantiation.
18973
18974 Of course, we can't instantiate member template classes, since
18975 we don't have any arguments for them. Note that the standard
18976 is unclear on whether the instantiation of the members are
18977 *explicit* instantiations or not. However, the most natural
18978 interpretation is that it should be an explicit instantiation. */
18979
18980 if (! static_p)
18981 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
18982 if (TREE_CODE (tmp) == FUNCTION_DECL
18983 && DECL_TEMPLATE_INSTANTIATION (tmp))
18984 instantiate_class_member (tmp, extern_p);
18985
18986 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
18987 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
18988 instantiate_class_member (tmp, extern_p);
18989
18990 if (CLASSTYPE_NESTED_UTDS (t))
18991 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
18992 bt_instantiate_type_proc, &storage);
18993 }
18994 }
18995
18996 /* Given a function DECL, which is a specialization of TMPL, modify
18997 DECL to be a re-instantiation of TMPL with the same template
18998 arguments. TMPL should be the template into which tsubst'ing
18999 should occur for DECL, not the most general template.
19000
19001 One reason for doing this is a scenario like this:
19002
19003 template <class T>
19004 void f(const T&, int i);
19005
19006 void g() { f(3, 7); }
19007
19008 template <class T>
19009 void f(const T& t, const int i) { }
19010
19011 Note that when the template is first instantiated, with
19012 instantiate_template, the resulting DECL will have no name for the
19013 first parameter, and the wrong type for the second. So, when we go
19014 to instantiate the DECL, we regenerate it. */
19015
19016 static void
19017 regenerate_decl_from_template (tree decl, tree tmpl)
19018 {
19019 /* The arguments used to instantiate DECL, from the most general
19020 template. */
19021 tree args;
19022 tree code_pattern;
19023
19024 args = DECL_TI_ARGS (decl);
19025 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19026
19027 /* Make sure that we can see identifiers, and compute access
19028 correctly. */
19029 push_access_scope (decl);
19030
19031 if (TREE_CODE (decl) == FUNCTION_DECL)
19032 {
19033 tree decl_parm;
19034 tree pattern_parm;
19035 tree specs;
19036 int args_depth;
19037 int parms_depth;
19038
19039 args_depth = TMPL_ARGS_DEPTH (args);
19040 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19041 if (args_depth > parms_depth)
19042 args = get_innermost_template_args (args, parms_depth);
19043
19044 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19045 args, tf_error, NULL_TREE,
19046 /*defer_ok*/false);
19047 if (specs && specs != error_mark_node)
19048 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19049 specs);
19050
19051 /* Merge parameter declarations. */
19052 decl_parm = skip_artificial_parms_for (decl,
19053 DECL_ARGUMENTS (decl));
19054 pattern_parm
19055 = skip_artificial_parms_for (code_pattern,
19056 DECL_ARGUMENTS (code_pattern));
19057 while (decl_parm && !DECL_PACK_P (pattern_parm))
19058 {
19059 tree parm_type;
19060 tree attributes;
19061
19062 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19063 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19064 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19065 NULL_TREE);
19066 parm_type = type_decays_to (parm_type);
19067 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19068 TREE_TYPE (decl_parm) = parm_type;
19069 attributes = DECL_ATTRIBUTES (pattern_parm);
19070 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19071 {
19072 DECL_ATTRIBUTES (decl_parm) = attributes;
19073 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19074 }
19075 decl_parm = DECL_CHAIN (decl_parm);
19076 pattern_parm = DECL_CHAIN (pattern_parm);
19077 }
19078 /* Merge any parameters that match with the function parameter
19079 pack. */
19080 if (pattern_parm && DECL_PACK_P (pattern_parm))
19081 {
19082 int i, len;
19083 tree expanded_types;
19084 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19085 the parameters in this function parameter pack. */
19086 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19087 args, tf_error, NULL_TREE);
19088 len = TREE_VEC_LENGTH (expanded_types);
19089 for (i = 0; i < len; i++)
19090 {
19091 tree parm_type;
19092 tree attributes;
19093
19094 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19095 /* Rename the parameter to include the index. */
19096 DECL_NAME (decl_parm) =
19097 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19098 parm_type = TREE_VEC_ELT (expanded_types, i);
19099 parm_type = type_decays_to (parm_type);
19100 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19101 TREE_TYPE (decl_parm) = parm_type;
19102 attributes = DECL_ATTRIBUTES (pattern_parm);
19103 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19104 {
19105 DECL_ATTRIBUTES (decl_parm) = attributes;
19106 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19107 }
19108 decl_parm = DECL_CHAIN (decl_parm);
19109 }
19110 }
19111 /* Merge additional specifiers from the CODE_PATTERN. */
19112 if (DECL_DECLARED_INLINE_P (code_pattern)
19113 && !DECL_DECLARED_INLINE_P (decl))
19114 DECL_DECLARED_INLINE_P (decl) = 1;
19115 }
19116 else if (VAR_P (decl))
19117 {
19118 DECL_INITIAL (decl) =
19119 tsubst_expr (DECL_INITIAL (code_pattern), args,
19120 tf_error, DECL_TI_TEMPLATE (decl),
19121 /*integral_constant_expression_p=*/false);
19122 if (VAR_HAD_UNKNOWN_BOUND (decl))
19123 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19124 tf_error, DECL_TI_TEMPLATE (decl));
19125 }
19126 else
19127 gcc_unreachable ();
19128
19129 pop_access_scope (decl);
19130 }
19131
19132 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19133 substituted to get DECL. */
19134
19135 tree
19136 template_for_substitution (tree decl)
19137 {
19138 tree tmpl = DECL_TI_TEMPLATE (decl);
19139
19140 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19141 for the instantiation. This is not always the most general
19142 template. Consider, for example:
19143
19144 template <class T>
19145 struct S { template <class U> void f();
19146 template <> void f<int>(); };
19147
19148 and an instantiation of S<double>::f<int>. We want TD to be the
19149 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19150 while (/* An instantiation cannot have a definition, so we need a
19151 more general template. */
19152 DECL_TEMPLATE_INSTANTIATION (tmpl)
19153 /* We must also deal with friend templates. Given:
19154
19155 template <class T> struct S {
19156 template <class U> friend void f() {};
19157 };
19158
19159 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19160 so far as the language is concerned, but that's still
19161 where we get the pattern for the instantiation from. On
19162 other hand, if the definition comes outside the class, say:
19163
19164 template <class T> struct S {
19165 template <class U> friend void f();
19166 };
19167 template <class U> friend void f() {}
19168
19169 we don't need to look any further. That's what the check for
19170 DECL_INITIAL is for. */
19171 || (TREE_CODE (decl) == FUNCTION_DECL
19172 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19173 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19174 {
19175 /* The present template, TD, should not be a definition. If it
19176 were a definition, we should be using it! Note that we
19177 cannot restructure the loop to just keep going until we find
19178 a template with a definition, since that might go too far if
19179 a specialization was declared, but not defined. */
19180 gcc_assert (!VAR_P (decl)
19181 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19182
19183 /* Fetch the more general template. */
19184 tmpl = DECL_TI_TEMPLATE (tmpl);
19185 }
19186
19187 return tmpl;
19188 }
19189
19190 /* Returns true if we need to instantiate this template instance even if we
19191 know we aren't going to emit it.. */
19192
19193 bool
19194 always_instantiate_p (tree decl)
19195 {
19196 /* We always instantiate inline functions so that we can inline them. An
19197 explicit instantiation declaration prohibits implicit instantiation of
19198 non-inline functions. With high levels of optimization, we would
19199 normally inline non-inline functions -- but we're not allowed to do
19200 that for "extern template" functions. Therefore, we check
19201 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19202 return ((TREE_CODE (decl) == FUNCTION_DECL
19203 && (DECL_DECLARED_INLINE_P (decl)
19204 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19205 /* And we need to instantiate static data members so that
19206 their initializers are available in integral constant
19207 expressions. */
19208 || (VAR_P (decl)
19209 && decl_maybe_constant_var_p (decl)));
19210 }
19211
19212 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19213 instantiate it now, modifying TREE_TYPE (fn). */
19214
19215 void
19216 maybe_instantiate_noexcept (tree fn)
19217 {
19218 tree fntype, spec, noex, clone;
19219
19220 if (DECL_CLONED_FUNCTION_P (fn))
19221 fn = DECL_CLONED_FUNCTION (fn);
19222 fntype = TREE_TYPE (fn);
19223 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19224
19225 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19226 return;
19227
19228 noex = TREE_PURPOSE (spec);
19229
19230 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19231 {
19232 if (push_tinst_level (fn))
19233 {
19234 push_access_scope (fn);
19235 push_deferring_access_checks (dk_no_deferred);
19236 input_location = DECL_SOURCE_LOCATION (fn);
19237 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19238 DEFERRED_NOEXCEPT_ARGS (noex),
19239 tf_warning_or_error, fn,
19240 /*function_p=*/false,
19241 /*integral_constant_expression_p=*/true);
19242 pop_deferring_access_checks ();
19243 pop_access_scope (fn);
19244 pop_tinst_level ();
19245 spec = build_noexcept_spec (noex, tf_warning_or_error);
19246 if (spec == error_mark_node)
19247 spec = noexcept_false_spec;
19248 }
19249 else
19250 spec = noexcept_false_spec;
19251 }
19252 else
19253 {
19254 /* This is an implicitly declared function, so NOEX is a list of
19255 other functions to evaluate and merge. */
19256 tree elt;
19257 spec = noexcept_true_spec;
19258 for (elt = noex; elt; elt = OVL_NEXT (elt))
19259 {
19260 tree fn = OVL_CURRENT (elt);
19261 tree subspec;
19262 maybe_instantiate_noexcept (fn);
19263 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19264 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19265 }
19266 }
19267
19268 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19269
19270 FOR_EACH_CLONE (clone, fn)
19271 {
19272 if (TREE_TYPE (clone) == fntype)
19273 TREE_TYPE (clone) = TREE_TYPE (fn);
19274 else
19275 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19276 }
19277 }
19278
19279 /* Produce the definition of D, a _DECL generated from a template. If
19280 DEFER_OK is nonzero, then we don't have to actually do the
19281 instantiation now; we just have to do it sometime. Normally it is
19282 an error if this is an explicit instantiation but D is undefined.
19283 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19284 explicitly instantiated class template. */
19285
19286 tree
19287 instantiate_decl (tree d, int defer_ok,
19288 bool expl_inst_class_mem_p)
19289 {
19290 tree tmpl = DECL_TI_TEMPLATE (d);
19291 tree gen_args;
19292 tree args;
19293 tree td;
19294 tree code_pattern;
19295 tree spec;
19296 tree gen_tmpl;
19297 bool pattern_defined;
19298 location_t saved_loc = input_location;
19299 int saved_unevaluated_operand = cp_unevaluated_operand;
19300 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19301 bool external_p;
19302 tree fn_context;
19303 bool nested;
19304
19305 /* This function should only be used to instantiate templates for
19306 functions and static member variables. */
19307 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19308
19309 /* Variables are never deferred; if instantiation is required, they
19310 are instantiated right away. That allows for better code in the
19311 case that an expression refers to the value of the variable --
19312 if the variable has a constant value the referring expression can
19313 take advantage of that fact. */
19314 if (VAR_P (d)
19315 || DECL_DECLARED_CONSTEXPR_P (d))
19316 defer_ok = 0;
19317
19318 /* Don't instantiate cloned functions. Instead, instantiate the
19319 functions they cloned. */
19320 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19321 d = DECL_CLONED_FUNCTION (d);
19322
19323 if (DECL_TEMPLATE_INSTANTIATED (d)
19324 || (TREE_CODE (d) == FUNCTION_DECL
19325 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19326 || DECL_TEMPLATE_SPECIALIZATION (d))
19327 /* D has already been instantiated or explicitly specialized, so
19328 there's nothing for us to do here.
19329
19330 It might seem reasonable to check whether or not D is an explicit
19331 instantiation, and, if so, stop here. But when an explicit
19332 instantiation is deferred until the end of the compilation,
19333 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19334 the instantiation. */
19335 return d;
19336
19337 /* Check to see whether we know that this template will be
19338 instantiated in some other file, as with "extern template"
19339 extension. */
19340 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19341
19342 /* In general, we do not instantiate such templates. */
19343 if (external_p && !always_instantiate_p (d))
19344 return d;
19345
19346 gen_tmpl = most_general_template (tmpl);
19347 gen_args = DECL_TI_ARGS (d);
19348
19349 if (tmpl != gen_tmpl)
19350 /* We should already have the extra args. */
19351 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19352 == TMPL_ARGS_DEPTH (gen_args));
19353 /* And what's in the hash table should match D. */
19354 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19355 || spec == NULL_TREE);
19356
19357 /* This needs to happen before any tsubsting. */
19358 if (! push_tinst_level (d))
19359 return d;
19360
19361 timevar_push (TV_TEMPLATE_INST);
19362
19363 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19364 for the instantiation. */
19365 td = template_for_substitution (d);
19366 code_pattern = DECL_TEMPLATE_RESULT (td);
19367
19368 /* We should never be trying to instantiate a member of a class
19369 template or partial specialization. */
19370 gcc_assert (d != code_pattern);
19371
19372 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19373 || DECL_TEMPLATE_SPECIALIZATION (td))
19374 /* In the case of a friend template whose definition is provided
19375 outside the class, we may have too many arguments. Drop the
19376 ones we don't need. The same is true for specializations. */
19377 args = get_innermost_template_args
19378 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19379 else
19380 args = gen_args;
19381
19382 if (TREE_CODE (d) == FUNCTION_DECL)
19383 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19384 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19385 else
19386 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19387
19388 /* We may be in the middle of deferred access check. Disable it now. */
19389 push_deferring_access_checks (dk_no_deferred);
19390
19391 /* Unless an explicit instantiation directive has already determined
19392 the linkage of D, remember that a definition is available for
19393 this entity. */
19394 if (pattern_defined
19395 && !DECL_INTERFACE_KNOWN (d)
19396 && !DECL_NOT_REALLY_EXTERN (d))
19397 mark_definable (d);
19398
19399 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19400 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19401 input_location = DECL_SOURCE_LOCATION (d);
19402
19403 /* If D is a member of an explicitly instantiated class template,
19404 and no definition is available, treat it like an implicit
19405 instantiation. */
19406 if (!pattern_defined && expl_inst_class_mem_p
19407 && DECL_EXPLICIT_INSTANTIATION (d))
19408 {
19409 /* Leave linkage flags alone on instantiations with anonymous
19410 visibility. */
19411 if (TREE_PUBLIC (d))
19412 {
19413 DECL_NOT_REALLY_EXTERN (d) = 0;
19414 DECL_INTERFACE_KNOWN (d) = 0;
19415 }
19416 SET_DECL_IMPLICIT_INSTANTIATION (d);
19417 }
19418
19419 if (TREE_CODE (d) == FUNCTION_DECL)
19420 maybe_instantiate_noexcept (d);
19421
19422 /* Defer all other templates, unless we have been explicitly
19423 forbidden from doing so. */
19424 if (/* If there is no definition, we cannot instantiate the
19425 template. */
19426 ! pattern_defined
19427 /* If it's OK to postpone instantiation, do so. */
19428 || defer_ok
19429 /* If this is a static data member that will be defined
19430 elsewhere, we don't want to instantiate the entire data
19431 member, but we do want to instantiate the initializer so that
19432 we can substitute that elsewhere. */
19433 || (external_p && VAR_P (d)))
19434 {
19435 /* The definition of the static data member is now required so
19436 we must substitute the initializer. */
19437 if (VAR_P (d)
19438 && !DECL_INITIAL (d)
19439 && DECL_INITIAL (code_pattern))
19440 {
19441 tree ns;
19442 tree init;
19443 bool const_init = false;
19444
19445 ns = decl_namespace_context (d);
19446 push_nested_namespace (ns);
19447 push_nested_class (DECL_CONTEXT (d));
19448 init = tsubst_expr (DECL_INITIAL (code_pattern),
19449 args,
19450 tf_warning_or_error, NULL_TREE,
19451 /*integral_constant_expression_p=*/false);
19452 /* Make sure the initializer is still constant, in case of
19453 circular dependency (template/instantiate6.C). */
19454 const_init
19455 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19456 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19457 /*asmspec_tree=*/NULL_TREE,
19458 LOOKUP_ONLYCONVERTING);
19459 pop_nested_class ();
19460 pop_nested_namespace (ns);
19461 }
19462
19463 /* We restore the source position here because it's used by
19464 add_pending_template. */
19465 input_location = saved_loc;
19466
19467 if (at_eof && !pattern_defined
19468 && DECL_EXPLICIT_INSTANTIATION (d)
19469 && DECL_NOT_REALLY_EXTERN (d))
19470 /* [temp.explicit]
19471
19472 The definition of a non-exported function template, a
19473 non-exported member function template, or a non-exported
19474 member function or static data member of a class template
19475 shall be present in every translation unit in which it is
19476 explicitly instantiated. */
19477 permerror (input_location, "explicit instantiation of %qD "
19478 "but no definition available", d);
19479
19480 /* If we're in unevaluated context, we just wanted to get the
19481 constant value; this isn't an odr use, so don't queue
19482 a full instantiation. */
19483 if (cp_unevaluated_operand != 0)
19484 goto out;
19485 /* ??? Historically, we have instantiated inline functions, even
19486 when marked as "extern template". */
19487 if (!(external_p && VAR_P (d)))
19488 add_pending_template (d);
19489 goto out;
19490 }
19491 /* Tell the repository that D is available in this translation unit
19492 -- and see if it is supposed to be instantiated here. */
19493 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19494 {
19495 /* In a PCH file, despite the fact that the repository hasn't
19496 requested instantiation in the PCH it is still possible that
19497 an instantiation will be required in a file that includes the
19498 PCH. */
19499 if (pch_file)
19500 add_pending_template (d);
19501 /* Instantiate inline functions so that the inliner can do its
19502 job, even though we'll not be emitting a copy of this
19503 function. */
19504 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19505 goto out;
19506 }
19507
19508 fn_context = decl_function_context (d);
19509 nested = (current_function_decl != NULL_TREE);
19510 if (!fn_context)
19511 push_to_top_level ();
19512 else
19513 {
19514 if (nested)
19515 push_function_context ();
19516 cp_unevaluated_operand = 0;
19517 c_inhibit_evaluation_warnings = 0;
19518 }
19519
19520 /* Mark D as instantiated so that recursive calls to
19521 instantiate_decl do not try to instantiate it again. */
19522 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19523
19524 /* Regenerate the declaration in case the template has been modified
19525 by a subsequent redeclaration. */
19526 regenerate_decl_from_template (d, td);
19527
19528 /* We already set the file and line above. Reset them now in case
19529 they changed as a result of calling regenerate_decl_from_template. */
19530 input_location = DECL_SOURCE_LOCATION (d);
19531
19532 if (VAR_P (d))
19533 {
19534 tree init;
19535 bool const_init = false;
19536
19537 /* Clear out DECL_RTL; whatever was there before may not be right
19538 since we've reset the type of the declaration. */
19539 SET_DECL_RTL (d, NULL);
19540 DECL_IN_AGGR_P (d) = 0;
19541
19542 /* The initializer is placed in DECL_INITIAL by
19543 regenerate_decl_from_template so we don't need to
19544 push/pop_access_scope again here. Pull it out so that
19545 cp_finish_decl can process it. */
19546 init = DECL_INITIAL (d);
19547 DECL_INITIAL (d) = NULL_TREE;
19548 DECL_INITIALIZED_P (d) = 0;
19549
19550 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19551 initializer. That function will defer actual emission until
19552 we have a chance to determine linkage. */
19553 DECL_EXTERNAL (d) = 0;
19554
19555 /* Enter the scope of D so that access-checking works correctly. */
19556 push_nested_class (DECL_CONTEXT (d));
19557 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19558 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19559 pop_nested_class ();
19560 }
19561 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19562 synthesize_method (d);
19563 else if (TREE_CODE (d) == FUNCTION_DECL)
19564 {
19565 struct pointer_map_t *saved_local_specializations;
19566 tree subst_decl;
19567 tree tmpl_parm;
19568 tree spec_parm;
19569 tree block = NULL_TREE;
19570
19571 /* Save away the current list, in case we are instantiating one
19572 template from within the body of another. */
19573 saved_local_specializations = local_specializations;
19574
19575 /* Set up the list of local specializations. */
19576 local_specializations = pointer_map_create ();
19577
19578 /* Set up context. */
19579 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19580 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19581 block = push_stmt_list ();
19582 else
19583 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19584
19585 /* Some typedefs referenced from within the template code need to be
19586 access checked at template instantiation time, i.e now. These
19587 types were added to the template at parsing time. Let's get those
19588 and perform the access checks then. */
19589 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19590 gen_args);
19591
19592 /* Create substitution entries for the parameters. */
19593 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19594 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19595 spec_parm = DECL_ARGUMENTS (d);
19596 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19597 {
19598 register_local_specialization (spec_parm, tmpl_parm);
19599 spec_parm = skip_artificial_parms_for (d, spec_parm);
19600 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19601 }
19602 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19603 {
19604 if (!DECL_PACK_P (tmpl_parm))
19605 {
19606 register_local_specialization (spec_parm, tmpl_parm);
19607 spec_parm = DECL_CHAIN (spec_parm);
19608 }
19609 else
19610 {
19611 /* Register the (value) argument pack as a specialization of
19612 TMPL_PARM, then move on. */
19613 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19614 register_local_specialization (argpack, tmpl_parm);
19615 }
19616 }
19617 gcc_assert (!spec_parm);
19618
19619 /* Substitute into the body of the function. */
19620 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19621 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19622 tf_warning_or_error, tmpl);
19623 else
19624 {
19625 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19626 tf_warning_or_error, tmpl,
19627 /*integral_constant_expression_p=*/false);
19628
19629 /* Set the current input_location to the end of the function
19630 so that finish_function knows where we are. */
19631 input_location
19632 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19633 }
19634
19635 /* We don't need the local specializations any more. */
19636 pointer_map_destroy (local_specializations);
19637 local_specializations = saved_local_specializations;
19638
19639 /* Finish the function. */
19640 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19641 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19642 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19643 else
19644 {
19645 d = finish_function (0);
19646 expand_or_defer_fn (d);
19647 }
19648
19649 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19650 cp_check_omp_declare_reduction (d);
19651 }
19652
19653 /* We're not deferring instantiation any more. */
19654 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
19655
19656 if (!fn_context)
19657 pop_from_top_level ();
19658 else if (nested)
19659 pop_function_context ();
19660
19661 out:
19662 input_location = saved_loc;
19663 cp_unevaluated_operand = saved_unevaluated_operand;
19664 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19665 pop_deferring_access_checks ();
19666 pop_tinst_level ();
19667
19668 timevar_pop (TV_TEMPLATE_INST);
19669
19670 return d;
19671 }
19672
19673 /* Run through the list of templates that we wish we could
19674 instantiate, and instantiate any we can. RETRIES is the
19675 number of times we retry pending template instantiation. */
19676
19677 void
19678 instantiate_pending_templates (int retries)
19679 {
19680 int reconsider;
19681 location_t saved_loc = input_location;
19682
19683 /* Instantiating templates may trigger vtable generation. This in turn
19684 may require further template instantiations. We place a limit here
19685 to avoid infinite loop. */
19686 if (pending_templates && retries >= max_tinst_depth)
19687 {
19688 tree decl = pending_templates->tinst->decl;
19689
19690 error ("template instantiation depth exceeds maximum of %d"
19691 " instantiating %q+D, possibly from virtual table generation"
19692 " (use -ftemplate-depth= to increase the maximum)",
19693 max_tinst_depth, decl);
19694 if (TREE_CODE (decl) == FUNCTION_DECL)
19695 /* Pretend that we defined it. */
19696 DECL_INITIAL (decl) = error_mark_node;
19697 return;
19698 }
19699
19700 do
19701 {
19702 struct pending_template **t = &pending_templates;
19703 struct pending_template *last = NULL;
19704 reconsider = 0;
19705 while (*t)
19706 {
19707 tree instantiation = reopen_tinst_level ((*t)->tinst);
19708 bool complete = false;
19709
19710 if (TYPE_P (instantiation))
19711 {
19712 tree fn;
19713
19714 if (!COMPLETE_TYPE_P (instantiation))
19715 {
19716 instantiate_class_template (instantiation);
19717 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
19718 for (fn = TYPE_METHODS (instantiation);
19719 fn;
19720 fn = TREE_CHAIN (fn))
19721 if (! DECL_ARTIFICIAL (fn))
19722 instantiate_decl (fn,
19723 /*defer_ok=*/0,
19724 /*expl_inst_class_mem_p=*/false);
19725 if (COMPLETE_TYPE_P (instantiation))
19726 reconsider = 1;
19727 }
19728
19729 complete = COMPLETE_TYPE_P (instantiation);
19730 }
19731 else
19732 {
19733 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
19734 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
19735 {
19736 instantiation
19737 = instantiate_decl (instantiation,
19738 /*defer_ok=*/0,
19739 /*expl_inst_class_mem_p=*/false);
19740 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
19741 reconsider = 1;
19742 }
19743
19744 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
19745 || DECL_TEMPLATE_INSTANTIATED (instantiation));
19746 }
19747
19748 if (complete)
19749 /* If INSTANTIATION has been instantiated, then we don't
19750 need to consider it again in the future. */
19751 *t = (*t)->next;
19752 else
19753 {
19754 last = *t;
19755 t = &(*t)->next;
19756 }
19757 tinst_depth = 0;
19758 current_tinst_level = NULL;
19759 }
19760 last_pending_template = last;
19761 }
19762 while (reconsider);
19763
19764 input_location = saved_loc;
19765 }
19766
19767 /* Substitute ARGVEC into T, which is a list of initializers for
19768 either base class or a non-static data member. The TREE_PURPOSEs
19769 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19770 instantiate_decl. */
19771
19772 static tree
19773 tsubst_initializer_list (tree t, tree argvec)
19774 {
19775 tree inits = NULL_TREE;
19776
19777 for (; t; t = TREE_CHAIN (t))
19778 {
19779 tree decl;
19780 tree init;
19781 tree expanded_bases = NULL_TREE;
19782 tree expanded_arguments = NULL_TREE;
19783 int i, len = 1;
19784
19785 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
19786 {
19787 tree expr;
19788 tree arg;
19789
19790 /* Expand the base class expansion type into separate base
19791 classes. */
19792 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
19793 tf_warning_or_error,
19794 NULL_TREE);
19795 if (expanded_bases == error_mark_node)
19796 continue;
19797
19798 /* We'll be building separate TREE_LISTs of arguments for
19799 each base. */
19800 len = TREE_VEC_LENGTH (expanded_bases);
19801 expanded_arguments = make_tree_vec (len);
19802 for (i = 0; i < len; i++)
19803 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
19804
19805 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19806 expand each argument in the TREE_VALUE of t. */
19807 expr = make_node (EXPR_PACK_EXPANSION);
19808 PACK_EXPANSION_LOCAL_P (expr) = true;
19809 PACK_EXPANSION_PARAMETER_PACKS (expr) =
19810 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
19811
19812 if (TREE_VALUE (t) == void_type_node)
19813 /* VOID_TYPE_NODE is used to indicate
19814 value-initialization. */
19815 {
19816 for (i = 0; i < len; i++)
19817 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
19818 }
19819 else
19820 {
19821 /* Substitute parameter packs into each argument in the
19822 TREE_LIST. */
19823 in_base_initializer = 1;
19824 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
19825 {
19826 tree expanded_exprs;
19827
19828 /* Expand the argument. */
19829 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
19830 expanded_exprs
19831 = tsubst_pack_expansion (expr, argvec,
19832 tf_warning_or_error,
19833 NULL_TREE);
19834 if (expanded_exprs == error_mark_node)
19835 continue;
19836
19837 /* Prepend each of the expanded expressions to the
19838 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19839 for (i = 0; i < len; i++)
19840 {
19841 TREE_VEC_ELT (expanded_arguments, i) =
19842 tree_cons (NULL_TREE,
19843 TREE_VEC_ELT (expanded_exprs, i),
19844 TREE_VEC_ELT (expanded_arguments, i));
19845 }
19846 }
19847 in_base_initializer = 0;
19848
19849 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19850 since we built them backwards. */
19851 for (i = 0; i < len; i++)
19852 {
19853 TREE_VEC_ELT (expanded_arguments, i) =
19854 nreverse (TREE_VEC_ELT (expanded_arguments, i));
19855 }
19856 }
19857 }
19858
19859 for (i = 0; i < len; ++i)
19860 {
19861 if (expanded_bases)
19862 {
19863 decl = TREE_VEC_ELT (expanded_bases, i);
19864 decl = expand_member_init (decl);
19865 init = TREE_VEC_ELT (expanded_arguments, i);
19866 }
19867 else
19868 {
19869 tree tmp;
19870 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
19871 tf_warning_or_error, NULL_TREE);
19872
19873 decl = expand_member_init (decl);
19874 if (decl && !DECL_P (decl))
19875 in_base_initializer = 1;
19876
19877 init = TREE_VALUE (t);
19878 tmp = init;
19879 if (init != void_type_node)
19880 init = tsubst_expr (init, argvec,
19881 tf_warning_or_error, NULL_TREE,
19882 /*integral_constant_expression_p=*/false);
19883 if (init == NULL_TREE && tmp != NULL_TREE)
19884 /* If we had an initializer but it instantiated to nothing,
19885 value-initialize the object. This will only occur when
19886 the initializer was a pack expansion where the parameter
19887 packs used in that expansion were of length zero. */
19888 init = void_type_node;
19889 in_base_initializer = 0;
19890 }
19891
19892 if (decl)
19893 {
19894 init = build_tree_list (decl, init);
19895 TREE_CHAIN (init) = inits;
19896 inits = init;
19897 }
19898 }
19899 }
19900 return inits;
19901 }
19902
19903 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19904
19905 static void
19906 set_current_access_from_decl (tree decl)
19907 {
19908 if (TREE_PRIVATE (decl))
19909 current_access_specifier = access_private_node;
19910 else if (TREE_PROTECTED (decl))
19911 current_access_specifier = access_protected_node;
19912 else
19913 current_access_specifier = access_public_node;
19914 }
19915
19916 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19917 is the instantiation (which should have been created with
19918 start_enum) and ARGS are the template arguments to use. */
19919
19920 static void
19921 tsubst_enum (tree tag, tree newtag, tree args)
19922 {
19923 tree e;
19924
19925 if (SCOPED_ENUM_P (newtag))
19926 begin_scope (sk_scoped_enum, newtag);
19927
19928 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
19929 {
19930 tree value;
19931 tree decl;
19932
19933 decl = TREE_VALUE (e);
19934 /* Note that in a template enum, the TREE_VALUE is the
19935 CONST_DECL, not the corresponding INTEGER_CST. */
19936 value = tsubst_expr (DECL_INITIAL (decl),
19937 args, tf_warning_or_error, NULL_TREE,
19938 /*integral_constant_expression_p=*/true);
19939
19940 /* Give this enumeration constant the correct access. */
19941 set_current_access_from_decl (decl);
19942
19943 /* Actually build the enumerator itself. */
19944 build_enumerator
19945 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
19946 }
19947
19948 if (SCOPED_ENUM_P (newtag))
19949 finish_scope ();
19950
19951 finish_enum_value_list (newtag);
19952 finish_enum (newtag);
19953
19954 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
19955 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
19956 }
19957
19958 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19959 its type -- but without substituting the innermost set of template
19960 arguments. So, innermost set of template parameters will appear in
19961 the type. */
19962
19963 tree
19964 get_mostly_instantiated_function_type (tree decl)
19965 {
19966 tree fn_type;
19967 tree tmpl;
19968 tree targs;
19969 tree tparms;
19970 int parm_depth;
19971
19972 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
19973 targs = DECL_TI_ARGS (decl);
19974 tparms = DECL_TEMPLATE_PARMS (tmpl);
19975 parm_depth = TMPL_PARMS_DEPTH (tparms);
19976
19977 /* There should be as many levels of arguments as there are levels
19978 of parameters. */
19979 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
19980
19981 fn_type = TREE_TYPE (tmpl);
19982
19983 if (parm_depth == 1)
19984 /* No substitution is necessary. */
19985 ;
19986 else
19987 {
19988 int i;
19989 tree partial_args;
19990
19991 /* Replace the innermost level of the TARGS with NULL_TREEs to
19992 let tsubst know not to substitute for those parameters. */
19993 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
19994 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
19995 SET_TMPL_ARGS_LEVEL (partial_args, i,
19996 TMPL_ARGS_LEVEL (targs, i));
19997 SET_TMPL_ARGS_LEVEL (partial_args,
19998 TMPL_ARGS_DEPTH (targs),
19999 make_tree_vec (DECL_NTPARMS (tmpl)));
20000
20001 /* Make sure that we can see identifiers, and compute access
20002 correctly. */
20003 push_access_scope (decl);
20004
20005 ++processing_template_decl;
20006 /* Now, do the (partial) substitution to figure out the
20007 appropriate function type. */
20008 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20009 --processing_template_decl;
20010
20011 /* Substitute into the template parameters to obtain the real
20012 innermost set of parameters. This step is important if the
20013 innermost set of template parameters contains value
20014 parameters whose types depend on outer template parameters. */
20015 TREE_VEC_LENGTH (partial_args)--;
20016 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20017
20018 pop_access_scope (decl);
20019 }
20020
20021 return fn_type;
20022 }
20023
20024 /* Return truthvalue if we're processing a template different from
20025 the last one involved in diagnostics. */
20026 int
20027 problematic_instantiation_changed (void)
20028 {
20029 return current_tinst_level != last_error_tinst_level;
20030 }
20031
20032 /* Remember current template involved in diagnostics. */
20033 void
20034 record_last_problematic_instantiation (void)
20035 {
20036 last_error_tinst_level = current_tinst_level;
20037 }
20038
20039 struct tinst_level *
20040 current_instantiation (void)
20041 {
20042 return current_tinst_level;
20043 }
20044
20045 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20046 type. Return zero for ok, nonzero for disallowed. Issue error and
20047 warning messages under control of COMPLAIN. */
20048
20049 static int
20050 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20051 {
20052 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20053 return 0;
20054 else if (POINTER_TYPE_P (type))
20055 return 0;
20056 else if (TYPE_PTRMEM_P (type))
20057 return 0;
20058 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20059 return 0;
20060 else if (TREE_CODE (type) == TYPENAME_TYPE)
20061 return 0;
20062 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20063 return 0;
20064 else if (TREE_CODE (type) == NULLPTR_TYPE)
20065 return 0;
20066
20067 if (complain & tf_error)
20068 {
20069 if (type == error_mark_node)
20070 inform (input_location, "invalid template non-type parameter");
20071 else
20072 error ("%q#T is not a valid type for a template non-type parameter",
20073 type);
20074 }
20075 return 1;
20076 }
20077
20078 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20079 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20080
20081 static bool
20082 dependent_type_p_r (tree type)
20083 {
20084 tree scope;
20085
20086 /* [temp.dep.type]
20087
20088 A type is dependent if it is:
20089
20090 -- a template parameter. Template template parameters are types
20091 for us (since TYPE_P holds true for them) so we handle
20092 them here. */
20093 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20094 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20095 return true;
20096 /* -- a qualified-id with a nested-name-specifier which contains a
20097 class-name that names a dependent type or whose unqualified-id
20098 names a dependent type. */
20099 if (TREE_CODE (type) == TYPENAME_TYPE)
20100 return true;
20101 /* -- a cv-qualified type where the cv-unqualified type is
20102 dependent. */
20103 type = TYPE_MAIN_VARIANT (type);
20104 /* -- a compound type constructed from any dependent type. */
20105 if (TYPE_PTRMEM_P (type))
20106 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20107 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20108 (type)));
20109 else if (TYPE_PTR_P (type)
20110 || TREE_CODE (type) == REFERENCE_TYPE)
20111 return dependent_type_p (TREE_TYPE (type));
20112 else if (TREE_CODE (type) == FUNCTION_TYPE
20113 || TREE_CODE (type) == METHOD_TYPE)
20114 {
20115 tree arg_type;
20116
20117 if (dependent_type_p (TREE_TYPE (type)))
20118 return true;
20119 for (arg_type = TYPE_ARG_TYPES (type);
20120 arg_type;
20121 arg_type = TREE_CHAIN (arg_type))
20122 if (dependent_type_p (TREE_VALUE (arg_type)))
20123 return true;
20124 return false;
20125 }
20126 /* -- an array type constructed from any dependent type or whose
20127 size is specified by a constant expression that is
20128 value-dependent.
20129
20130 We checked for type- and value-dependence of the bounds in
20131 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20132 if (TREE_CODE (type) == ARRAY_TYPE)
20133 {
20134 if (TYPE_DOMAIN (type)
20135 && dependent_type_p (TYPE_DOMAIN (type)))
20136 return true;
20137 return dependent_type_p (TREE_TYPE (type));
20138 }
20139
20140 /* -- a template-id in which either the template name is a template
20141 parameter ... */
20142 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20143 return true;
20144 /* ... or any of the template arguments is a dependent type or
20145 an expression that is type-dependent or value-dependent. */
20146 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20147 && (any_dependent_template_arguments_p
20148 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20149 return true;
20150
20151 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20152 dependent; if the argument of the `typeof' expression is not
20153 type-dependent, then it should already been have resolved. */
20154 if (TREE_CODE (type) == TYPEOF_TYPE
20155 || TREE_CODE (type) == DECLTYPE_TYPE
20156 || TREE_CODE (type) == UNDERLYING_TYPE)
20157 return true;
20158
20159 /* A template argument pack is dependent if any of its packed
20160 arguments are. */
20161 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20162 {
20163 tree args = ARGUMENT_PACK_ARGS (type);
20164 int i, len = TREE_VEC_LENGTH (args);
20165 for (i = 0; i < len; ++i)
20166 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20167 return true;
20168 }
20169
20170 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20171 be template parameters. */
20172 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20173 return true;
20174
20175 /* The standard does not specifically mention types that are local
20176 to template functions or local classes, but they should be
20177 considered dependent too. For example:
20178
20179 template <int I> void f() {
20180 enum E { a = I };
20181 S<sizeof (E)> s;
20182 }
20183
20184 The size of `E' cannot be known until the value of `I' has been
20185 determined. Therefore, `E' must be considered dependent. */
20186 scope = TYPE_CONTEXT (type);
20187 if (scope && TYPE_P (scope))
20188 return dependent_type_p (scope);
20189 /* Don't use type_dependent_expression_p here, as it can lead
20190 to infinite recursion trying to determine whether a lambda
20191 nested in a lambda is dependent (c++/47687). */
20192 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20193 && DECL_LANG_SPECIFIC (scope)
20194 && DECL_TEMPLATE_INFO (scope)
20195 && (any_dependent_template_arguments_p
20196 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20197 return true;
20198
20199 /* Other types are non-dependent. */
20200 return false;
20201 }
20202
20203 /* Returns TRUE if TYPE is dependent, in the sense of
20204 [temp.dep.type]. Note that a NULL type is considered dependent. */
20205
20206 bool
20207 dependent_type_p (tree type)
20208 {
20209 /* If there are no template parameters in scope, then there can't be
20210 any dependent types. */
20211 if (!processing_template_decl)
20212 {
20213 /* If we are not processing a template, then nobody should be
20214 providing us with a dependent type. */
20215 gcc_assert (type);
20216 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20217 return false;
20218 }
20219
20220 /* If the type is NULL, we have not computed a type for the entity
20221 in question; in that case, the type is dependent. */
20222 if (!type)
20223 return true;
20224
20225 /* Erroneous types can be considered non-dependent. */
20226 if (type == error_mark_node)
20227 return false;
20228
20229 /* If we have not already computed the appropriate value for TYPE,
20230 do so now. */
20231 if (!TYPE_DEPENDENT_P_VALID (type))
20232 {
20233 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20234 TYPE_DEPENDENT_P_VALID (type) = 1;
20235 }
20236
20237 return TYPE_DEPENDENT_P (type);
20238 }
20239
20240 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20241 lookup. In other words, a dependent type that is not the current
20242 instantiation. */
20243
20244 bool
20245 dependent_scope_p (tree scope)
20246 {
20247 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20248 && !currently_open_class (scope));
20249 }
20250
20251 /* T is a SCOPE_REF; return whether we need to consider it
20252 instantiation-dependent so that we can check access at instantiation
20253 time even though we know which member it resolves to. */
20254
20255 static bool
20256 instantiation_dependent_scope_ref_p (tree t)
20257 {
20258 if (DECL_P (TREE_OPERAND (t, 1))
20259 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20260 && accessible_in_template_p (TREE_OPERAND (t, 0),
20261 TREE_OPERAND (t, 1)))
20262 return false;
20263 else
20264 return true;
20265 }
20266
20267 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20268 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20269 expression. */
20270
20271 /* Note that this predicate is not appropriate for general expressions;
20272 only constant expressions (that satisfy potential_constant_expression)
20273 can be tested for value dependence. */
20274
20275 bool
20276 value_dependent_expression_p (tree expression)
20277 {
20278 if (!processing_template_decl)
20279 return false;
20280
20281 /* A name declared with a dependent type. */
20282 if (DECL_P (expression) && type_dependent_expression_p (expression))
20283 return true;
20284
20285 switch (TREE_CODE (expression))
20286 {
20287 case IDENTIFIER_NODE:
20288 /* A name that has not been looked up -- must be dependent. */
20289 return true;
20290
20291 case TEMPLATE_PARM_INDEX:
20292 /* A non-type template parm. */
20293 return true;
20294
20295 case CONST_DECL:
20296 /* A non-type template parm. */
20297 if (DECL_TEMPLATE_PARM_P (expression))
20298 return true;
20299 return value_dependent_expression_p (DECL_INITIAL (expression));
20300
20301 case VAR_DECL:
20302 /* A constant with literal type and is initialized
20303 with an expression that is value-dependent.
20304
20305 Note that a non-dependent parenthesized initializer will have
20306 already been replaced with its constant value, so if we see
20307 a TREE_LIST it must be dependent. */
20308 if (DECL_INITIAL (expression)
20309 && decl_constant_var_p (expression)
20310 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20311 || value_dependent_expression_p (DECL_INITIAL (expression))))
20312 return true;
20313 return false;
20314
20315 case DYNAMIC_CAST_EXPR:
20316 case STATIC_CAST_EXPR:
20317 case CONST_CAST_EXPR:
20318 case REINTERPRET_CAST_EXPR:
20319 case CAST_EXPR:
20320 /* These expressions are value-dependent if the type to which
20321 the cast occurs is dependent or the expression being casted
20322 is value-dependent. */
20323 {
20324 tree type = TREE_TYPE (expression);
20325
20326 if (dependent_type_p (type))
20327 return true;
20328
20329 /* A functional cast has a list of operands. */
20330 expression = TREE_OPERAND (expression, 0);
20331 if (!expression)
20332 {
20333 /* If there are no operands, it must be an expression such
20334 as "int()". This should not happen for aggregate types
20335 because it would form non-constant expressions. */
20336 gcc_assert (cxx_dialect >= cxx11
20337 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20338
20339 return false;
20340 }
20341
20342 if (TREE_CODE (expression) == TREE_LIST)
20343 return any_value_dependent_elements_p (expression);
20344
20345 return value_dependent_expression_p (expression);
20346 }
20347
20348 case SIZEOF_EXPR:
20349 if (SIZEOF_EXPR_TYPE_P (expression))
20350 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20351 /* FALLTHRU */
20352 case ALIGNOF_EXPR:
20353 case TYPEID_EXPR:
20354 /* A `sizeof' expression is value-dependent if the operand is
20355 type-dependent or is a pack expansion. */
20356 expression = TREE_OPERAND (expression, 0);
20357 if (PACK_EXPANSION_P (expression))
20358 return true;
20359 else if (TYPE_P (expression))
20360 return dependent_type_p (expression);
20361 return instantiation_dependent_expression_p (expression);
20362
20363 case AT_ENCODE_EXPR:
20364 /* An 'encode' expression is value-dependent if the operand is
20365 type-dependent. */
20366 expression = TREE_OPERAND (expression, 0);
20367 return dependent_type_p (expression);
20368
20369 case NOEXCEPT_EXPR:
20370 expression = TREE_OPERAND (expression, 0);
20371 return instantiation_dependent_expression_p (expression);
20372
20373 case SCOPE_REF:
20374 /* All instantiation-dependent expressions should also be considered
20375 value-dependent. */
20376 return instantiation_dependent_scope_ref_p (expression);
20377
20378 case COMPONENT_REF:
20379 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20380 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20381
20382 case NONTYPE_ARGUMENT_PACK:
20383 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20384 is value-dependent. */
20385 {
20386 tree values = ARGUMENT_PACK_ARGS (expression);
20387 int i, len = TREE_VEC_LENGTH (values);
20388
20389 for (i = 0; i < len; ++i)
20390 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20391 return true;
20392
20393 return false;
20394 }
20395
20396 case TRAIT_EXPR:
20397 {
20398 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20399 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20400 || (type2 ? dependent_type_p (type2) : false));
20401 }
20402
20403 case MODOP_EXPR:
20404 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20405 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20406
20407 case ARRAY_REF:
20408 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20409 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20410
20411 case ADDR_EXPR:
20412 {
20413 tree op = TREE_OPERAND (expression, 0);
20414 return (value_dependent_expression_p (op)
20415 || has_value_dependent_address (op));
20416 }
20417
20418 case CALL_EXPR:
20419 {
20420 tree fn = get_callee_fndecl (expression);
20421 int i, nargs;
20422 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20423 return true;
20424 nargs = call_expr_nargs (expression);
20425 for (i = 0; i < nargs; ++i)
20426 {
20427 tree op = CALL_EXPR_ARG (expression, i);
20428 /* In a call to a constexpr member function, look through the
20429 implicit ADDR_EXPR on the object argument so that it doesn't
20430 cause the call to be considered value-dependent. We also
20431 look through it in potential_constant_expression. */
20432 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20433 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20434 && TREE_CODE (op) == ADDR_EXPR)
20435 op = TREE_OPERAND (op, 0);
20436 if (value_dependent_expression_p (op))
20437 return true;
20438 }
20439 return false;
20440 }
20441
20442 case TEMPLATE_ID_EXPR:
20443 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20444 type-dependent. */
20445 return type_dependent_expression_p (expression);
20446
20447 case CONSTRUCTOR:
20448 {
20449 unsigned ix;
20450 tree val;
20451 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20452 if (value_dependent_expression_p (val))
20453 return true;
20454 return false;
20455 }
20456
20457 case STMT_EXPR:
20458 /* Treat a GNU statement expression as dependent to avoid crashing
20459 under fold_non_dependent_expr; it can't be constant. */
20460 return true;
20461
20462 default:
20463 /* A constant expression is value-dependent if any subexpression is
20464 value-dependent. */
20465 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20466 {
20467 case tcc_reference:
20468 case tcc_unary:
20469 case tcc_comparison:
20470 case tcc_binary:
20471 case tcc_expression:
20472 case tcc_vl_exp:
20473 {
20474 int i, len = cp_tree_operand_length (expression);
20475
20476 for (i = 0; i < len; i++)
20477 {
20478 tree t = TREE_OPERAND (expression, i);
20479
20480 /* In some cases, some of the operands may be missing.l
20481 (For example, in the case of PREDECREMENT_EXPR, the
20482 amount to increment by may be missing.) That doesn't
20483 make the expression dependent. */
20484 if (t && value_dependent_expression_p (t))
20485 return true;
20486 }
20487 }
20488 break;
20489 default:
20490 break;
20491 }
20492 break;
20493 }
20494
20495 /* The expression is not value-dependent. */
20496 return false;
20497 }
20498
20499 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20500 [temp.dep.expr]. Note that an expression with no type is
20501 considered dependent. Other parts of the compiler arrange for an
20502 expression with type-dependent subexpressions to have no type, so
20503 this function doesn't have to be fully recursive. */
20504
20505 bool
20506 type_dependent_expression_p (tree expression)
20507 {
20508 if (!processing_template_decl)
20509 return false;
20510
20511 if (expression == NULL_TREE || expression == error_mark_node)
20512 return false;
20513
20514 /* An unresolved name is always dependent. */
20515 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20516 return true;
20517
20518 /* Some expression forms are never type-dependent. */
20519 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20520 || TREE_CODE (expression) == SIZEOF_EXPR
20521 || TREE_CODE (expression) == ALIGNOF_EXPR
20522 || TREE_CODE (expression) == AT_ENCODE_EXPR
20523 || TREE_CODE (expression) == NOEXCEPT_EXPR
20524 || TREE_CODE (expression) == TRAIT_EXPR
20525 || TREE_CODE (expression) == TYPEID_EXPR
20526 || TREE_CODE (expression) == DELETE_EXPR
20527 || TREE_CODE (expression) == VEC_DELETE_EXPR
20528 || TREE_CODE (expression) == THROW_EXPR)
20529 return false;
20530
20531 /* The types of these expressions depends only on the type to which
20532 the cast occurs. */
20533 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20534 || TREE_CODE (expression) == STATIC_CAST_EXPR
20535 || TREE_CODE (expression) == CONST_CAST_EXPR
20536 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20537 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20538 || TREE_CODE (expression) == CAST_EXPR)
20539 return dependent_type_p (TREE_TYPE (expression));
20540
20541 /* The types of these expressions depends only on the type created
20542 by the expression. */
20543 if (TREE_CODE (expression) == NEW_EXPR
20544 || TREE_CODE (expression) == VEC_NEW_EXPR)
20545 {
20546 /* For NEW_EXPR tree nodes created inside a template, either
20547 the object type itself or a TREE_LIST may appear as the
20548 operand 1. */
20549 tree type = TREE_OPERAND (expression, 1);
20550 if (TREE_CODE (type) == TREE_LIST)
20551 /* This is an array type. We need to check array dimensions
20552 as well. */
20553 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20554 || value_dependent_expression_p
20555 (TREE_OPERAND (TREE_VALUE (type), 1));
20556 else
20557 return dependent_type_p (type);
20558 }
20559
20560 if (TREE_CODE (expression) == SCOPE_REF)
20561 {
20562 tree scope = TREE_OPERAND (expression, 0);
20563 tree name = TREE_OPERAND (expression, 1);
20564
20565 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20566 contains an identifier associated by name lookup with one or more
20567 declarations declared with a dependent type, or...a
20568 nested-name-specifier or qualified-id that names a member of an
20569 unknown specialization. */
20570 return (type_dependent_expression_p (name)
20571 || dependent_scope_p (scope));
20572 }
20573
20574 if (TREE_CODE (expression) == FUNCTION_DECL
20575 && DECL_LANG_SPECIFIC (expression)
20576 && DECL_TEMPLATE_INFO (expression)
20577 && (any_dependent_template_arguments_p
20578 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20579 return true;
20580
20581 if (TREE_CODE (expression) == TEMPLATE_DECL
20582 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20583 return false;
20584
20585 if (TREE_CODE (expression) == STMT_EXPR)
20586 expression = stmt_expr_value_expr (expression);
20587
20588 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20589 {
20590 tree elt;
20591 unsigned i;
20592
20593 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20594 {
20595 if (type_dependent_expression_p (elt))
20596 return true;
20597 }
20598 return false;
20599 }
20600
20601 /* A static data member of the current instantiation with incomplete
20602 array type is type-dependent, as the definition and specializations
20603 can have different bounds. */
20604 if (VAR_P (expression)
20605 && DECL_CLASS_SCOPE_P (expression)
20606 && dependent_type_p (DECL_CONTEXT (expression))
20607 && VAR_HAD_UNKNOWN_BOUND (expression))
20608 return true;
20609
20610 /* An array of unknown bound depending on a variadic parameter, eg:
20611
20612 template<typename... Args>
20613 void foo (Args... args)
20614 {
20615 int arr[] = { args... };
20616 }
20617
20618 template<int... vals>
20619 void bar ()
20620 {
20621 int arr[] = { vals... };
20622 }
20623
20624 If the array has no length and has an initializer, it must be that
20625 we couldn't determine its length in cp_complete_array_type because
20626 it is dependent. */
20627 if (VAR_P (expression)
20628 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20629 && !TYPE_DOMAIN (TREE_TYPE (expression))
20630 && DECL_INITIAL (expression))
20631 return true;
20632
20633 if (TREE_TYPE (expression) == unknown_type_node)
20634 {
20635 if (TREE_CODE (expression) == ADDR_EXPR)
20636 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20637 if (TREE_CODE (expression) == COMPONENT_REF
20638 || TREE_CODE (expression) == OFFSET_REF)
20639 {
20640 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20641 return true;
20642 expression = TREE_OPERAND (expression, 1);
20643 if (identifier_p (expression))
20644 return false;
20645 }
20646 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20647 if (TREE_CODE (expression) == SCOPE_REF)
20648 return false;
20649
20650 /* Always dependent, on the number of arguments if nothing else. */
20651 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
20652 return true;
20653
20654 if (BASELINK_P (expression))
20655 expression = BASELINK_FUNCTIONS (expression);
20656
20657 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
20658 {
20659 if (any_dependent_template_arguments_p
20660 (TREE_OPERAND (expression, 1)))
20661 return true;
20662 expression = TREE_OPERAND (expression, 0);
20663 }
20664 gcc_assert (TREE_CODE (expression) == OVERLOAD
20665 || TREE_CODE (expression) == FUNCTION_DECL);
20666
20667 while (expression)
20668 {
20669 if (type_dependent_expression_p (OVL_CURRENT (expression)))
20670 return true;
20671 expression = OVL_NEXT (expression);
20672 }
20673 return false;
20674 }
20675
20676 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
20677
20678 return (dependent_type_p (TREE_TYPE (expression)));
20679 }
20680
20681 /* walk_tree callback function for instantiation_dependent_expression_p,
20682 below. Returns non-zero if a dependent subexpression is found. */
20683
20684 static tree
20685 instantiation_dependent_r (tree *tp, int *walk_subtrees,
20686 void * /*data*/)
20687 {
20688 if (TYPE_P (*tp))
20689 {
20690 /* We don't have to worry about decltype currently because decltype
20691 of an instantiation-dependent expr is a dependent type. This
20692 might change depending on the resolution of DR 1172. */
20693 *walk_subtrees = false;
20694 return NULL_TREE;
20695 }
20696 enum tree_code code = TREE_CODE (*tp);
20697 switch (code)
20698 {
20699 /* Don't treat an argument list as dependent just because it has no
20700 TREE_TYPE. */
20701 case TREE_LIST:
20702 case TREE_VEC:
20703 return NULL_TREE;
20704
20705 case VAR_DECL:
20706 case CONST_DECL:
20707 /* A constant with a dependent initializer is dependent. */
20708 if (value_dependent_expression_p (*tp))
20709 return *tp;
20710 break;
20711
20712 case TEMPLATE_PARM_INDEX:
20713 return *tp;
20714
20715 /* Handle expressions with type operands. */
20716 case SIZEOF_EXPR:
20717 case ALIGNOF_EXPR:
20718 case TYPEID_EXPR:
20719 case AT_ENCODE_EXPR:
20720 {
20721 tree op = TREE_OPERAND (*tp, 0);
20722 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
20723 op = TREE_TYPE (op);
20724 if (TYPE_P (op))
20725 {
20726 if (dependent_type_p (op))
20727 return *tp;
20728 else
20729 {
20730 *walk_subtrees = false;
20731 return NULL_TREE;
20732 }
20733 }
20734 break;
20735 }
20736
20737 case TRAIT_EXPR:
20738 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
20739 || (TRAIT_EXPR_TYPE2 (*tp)
20740 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
20741 return *tp;
20742 *walk_subtrees = false;
20743 return NULL_TREE;
20744
20745 case COMPONENT_REF:
20746 if (identifier_p (TREE_OPERAND (*tp, 1)))
20747 /* In a template, finish_class_member_access_expr creates a
20748 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20749 type-dependent, so that we can check access control at
20750 instantiation time (PR 42277). See also Core issue 1273. */
20751 return *tp;
20752 break;
20753
20754 case SCOPE_REF:
20755 if (instantiation_dependent_scope_ref_p (*tp))
20756 return *tp;
20757 else
20758 break;
20759
20760 /* Treat statement-expressions as dependent. */
20761 case BIND_EXPR:
20762 return *tp;
20763
20764 default:
20765 break;
20766 }
20767
20768 if (type_dependent_expression_p (*tp))
20769 return *tp;
20770 else
20771 return NULL_TREE;
20772 }
20773
20774 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20775 sense defined by the ABI:
20776
20777 "An expression is instantiation-dependent if it is type-dependent
20778 or value-dependent, or it has a subexpression that is type-dependent
20779 or value-dependent." */
20780
20781 bool
20782 instantiation_dependent_expression_p (tree expression)
20783 {
20784 tree result;
20785
20786 if (!processing_template_decl)
20787 return false;
20788
20789 if (expression == error_mark_node)
20790 return false;
20791
20792 result = cp_walk_tree_without_duplicates (&expression,
20793 instantiation_dependent_r, NULL);
20794 return result != NULL_TREE;
20795 }
20796
20797 /* Like type_dependent_expression_p, but it also works while not processing
20798 a template definition, i.e. during substitution or mangling. */
20799
20800 bool
20801 type_dependent_expression_p_push (tree expr)
20802 {
20803 bool b;
20804 ++processing_template_decl;
20805 b = type_dependent_expression_p (expr);
20806 --processing_template_decl;
20807 return b;
20808 }
20809
20810 /* Returns TRUE if ARGS contains a type-dependent expression. */
20811
20812 bool
20813 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
20814 {
20815 unsigned int i;
20816 tree arg;
20817
20818 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
20819 {
20820 if (type_dependent_expression_p (arg))
20821 return true;
20822 }
20823 return false;
20824 }
20825
20826 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20827 expressions) contains any type-dependent expressions. */
20828
20829 bool
20830 any_type_dependent_elements_p (const_tree list)
20831 {
20832 for (; list; list = TREE_CHAIN (list))
20833 if (type_dependent_expression_p (TREE_VALUE (list)))
20834 return true;
20835
20836 return false;
20837 }
20838
20839 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20840 expressions) contains any value-dependent expressions. */
20841
20842 bool
20843 any_value_dependent_elements_p (const_tree list)
20844 {
20845 for (; list; list = TREE_CHAIN (list))
20846 if (value_dependent_expression_p (TREE_VALUE (list)))
20847 return true;
20848
20849 return false;
20850 }
20851
20852 /* Returns TRUE if the ARG (a template argument) is dependent. */
20853
20854 bool
20855 dependent_template_arg_p (tree arg)
20856 {
20857 if (!processing_template_decl)
20858 return false;
20859
20860 /* Assume a template argument that was wrongly written by the user
20861 is dependent. This is consistent with what
20862 any_dependent_template_arguments_p [that calls this function]
20863 does. */
20864 if (!arg || arg == error_mark_node)
20865 return true;
20866
20867 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
20868 arg = ARGUMENT_PACK_SELECT_ARG (arg);
20869
20870 if (TREE_CODE (arg) == TEMPLATE_DECL
20871 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20872 return dependent_template_p (arg);
20873 else if (ARGUMENT_PACK_P (arg))
20874 {
20875 tree args = ARGUMENT_PACK_ARGS (arg);
20876 int i, len = TREE_VEC_LENGTH (args);
20877 for (i = 0; i < len; ++i)
20878 {
20879 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20880 return true;
20881 }
20882
20883 return false;
20884 }
20885 else if (TYPE_P (arg))
20886 return dependent_type_p (arg);
20887 else
20888 return (type_dependent_expression_p (arg)
20889 || value_dependent_expression_p (arg));
20890 }
20891
20892 /* Returns true if ARGS (a collection of template arguments) contains
20893 any types that require structural equality testing. */
20894
20895 bool
20896 any_template_arguments_need_structural_equality_p (tree args)
20897 {
20898 int i;
20899 int j;
20900
20901 if (!args)
20902 return false;
20903 if (args == error_mark_node)
20904 return true;
20905
20906 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20907 {
20908 tree level = TMPL_ARGS_LEVEL (args, i + 1);
20909 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20910 {
20911 tree arg = TREE_VEC_ELT (level, j);
20912 tree packed_args = NULL_TREE;
20913 int k, len = 1;
20914
20915 if (ARGUMENT_PACK_P (arg))
20916 {
20917 /* Look inside the argument pack. */
20918 packed_args = ARGUMENT_PACK_ARGS (arg);
20919 len = TREE_VEC_LENGTH (packed_args);
20920 }
20921
20922 for (k = 0; k < len; ++k)
20923 {
20924 if (packed_args)
20925 arg = TREE_VEC_ELT (packed_args, k);
20926
20927 if (error_operand_p (arg))
20928 return true;
20929 else if (TREE_CODE (arg) == TEMPLATE_DECL)
20930 continue;
20931 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
20932 return true;
20933 else if (!TYPE_P (arg) && TREE_TYPE (arg)
20934 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
20935 return true;
20936 }
20937 }
20938 }
20939
20940 return false;
20941 }
20942
20943 /* Returns true if ARGS (a collection of template arguments) contains
20944 any dependent arguments. */
20945
20946 bool
20947 any_dependent_template_arguments_p (const_tree args)
20948 {
20949 int i;
20950 int j;
20951
20952 if (!args)
20953 return false;
20954 if (args == error_mark_node)
20955 return true;
20956
20957 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20958 {
20959 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
20960 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20961 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
20962 return true;
20963 }
20964
20965 return false;
20966 }
20967
20968 /* Returns TRUE if the template TMPL is dependent. */
20969
20970 bool
20971 dependent_template_p (tree tmpl)
20972 {
20973 if (TREE_CODE (tmpl) == OVERLOAD)
20974 {
20975 while (tmpl)
20976 {
20977 if (dependent_template_p (OVL_CURRENT (tmpl)))
20978 return true;
20979 tmpl = OVL_NEXT (tmpl);
20980 }
20981 return false;
20982 }
20983
20984 /* Template template parameters are dependent. */
20985 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
20986 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
20987 return true;
20988 /* So are names that have not been looked up. */
20989 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
20990 return true;
20991 /* So are member templates of dependent classes. */
20992 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
20993 return dependent_type_p (DECL_CONTEXT (tmpl));
20994 return false;
20995 }
20996
20997 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
20998
20999 bool
21000 dependent_template_id_p (tree tmpl, tree args)
21001 {
21002 return (dependent_template_p (tmpl)
21003 || any_dependent_template_arguments_p (args));
21004 }
21005
21006 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21007 is dependent. */
21008
21009 bool
21010 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21011 {
21012 int i;
21013
21014 if (!processing_template_decl)
21015 return false;
21016
21017 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21018 {
21019 tree decl = TREE_VEC_ELT (declv, i);
21020 tree init = TREE_VEC_ELT (initv, i);
21021 tree cond = TREE_VEC_ELT (condv, i);
21022 tree incr = TREE_VEC_ELT (incrv, i);
21023
21024 if (type_dependent_expression_p (decl))
21025 return true;
21026
21027 if (init && type_dependent_expression_p (init))
21028 return true;
21029
21030 if (type_dependent_expression_p (cond))
21031 return true;
21032
21033 if (COMPARISON_CLASS_P (cond)
21034 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21035 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21036 return true;
21037
21038 if (TREE_CODE (incr) == MODOP_EXPR)
21039 {
21040 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21041 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21042 return true;
21043 }
21044 else if (type_dependent_expression_p (incr))
21045 return true;
21046 else if (TREE_CODE (incr) == MODIFY_EXPR)
21047 {
21048 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21049 return true;
21050 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21051 {
21052 tree t = TREE_OPERAND (incr, 1);
21053 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21054 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21055 return true;
21056 }
21057 }
21058 }
21059
21060 return false;
21061 }
21062
21063 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21064 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21065 no such TYPE can be found. Note that this function peers inside
21066 uninstantiated templates and therefore should be used only in
21067 extremely limited situations. ONLY_CURRENT_P restricts this
21068 peering to the currently open classes hierarchy (which is required
21069 when comparing types). */
21070
21071 tree
21072 resolve_typename_type (tree type, bool only_current_p)
21073 {
21074 tree scope;
21075 tree name;
21076 tree decl;
21077 int quals;
21078 tree pushed_scope;
21079 tree result;
21080
21081 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21082
21083 scope = TYPE_CONTEXT (type);
21084 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21085 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21086 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21087 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21088 identifier of the TYPENAME_TYPE anymore.
21089 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21090 TYPENAME_TYPE instead, we avoid messing up with a possible
21091 typedef variant case. */
21092 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21093
21094 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21095 it first before we can figure out what NAME refers to. */
21096 if (TREE_CODE (scope) == TYPENAME_TYPE)
21097 {
21098 if (TYPENAME_IS_RESOLVING_P (scope))
21099 /* Given a class template A with a dependent base with nested type C,
21100 typedef typename A::C::C C will land us here, as trying to resolve
21101 the initial A::C leads to the local C typedef, which leads back to
21102 A::C::C. So we break the recursion now. */
21103 return type;
21104 else
21105 scope = resolve_typename_type (scope, only_current_p);
21106 }
21107 /* If we don't know what SCOPE refers to, then we cannot resolve the
21108 TYPENAME_TYPE. */
21109 if (TREE_CODE (scope) == TYPENAME_TYPE)
21110 return type;
21111 /* If the SCOPE is a template type parameter, we have no way of
21112 resolving the name. */
21113 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21114 return type;
21115 /* If the SCOPE is not the current instantiation, there's no reason
21116 to look inside it. */
21117 if (only_current_p && !currently_open_class (scope))
21118 return type;
21119 /* If this is a typedef, we don't want to look inside (c++/11987). */
21120 if (typedef_variant_p (type))
21121 return type;
21122 /* If SCOPE isn't the template itself, it will not have a valid
21123 TYPE_FIELDS list. */
21124 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21125 /* scope is either the template itself or a compatible instantiation
21126 like X<T>, so look up the name in the original template. */
21127 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21128 else
21129 /* scope is a partial instantiation, so we can't do the lookup or we
21130 will lose the template arguments. */
21131 return type;
21132 /* Enter the SCOPE so that name lookup will be resolved as if we
21133 were in the class definition. In particular, SCOPE will no
21134 longer be considered a dependent type. */
21135 pushed_scope = push_scope (scope);
21136 /* Look up the declaration. */
21137 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21138 tf_warning_or_error);
21139
21140 result = NULL_TREE;
21141
21142 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21143 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21144 if (!decl)
21145 /*nop*/;
21146 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21147 && TREE_CODE (decl) == TYPE_DECL)
21148 {
21149 result = TREE_TYPE (decl);
21150 if (result == error_mark_node)
21151 result = NULL_TREE;
21152 }
21153 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21154 && DECL_CLASS_TEMPLATE_P (decl))
21155 {
21156 tree tmpl;
21157 tree args;
21158 /* Obtain the template and the arguments. */
21159 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21160 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21161 /* Instantiate the template. */
21162 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21163 /*entering_scope=*/0,
21164 tf_error | tf_user);
21165 if (result == error_mark_node)
21166 result = NULL_TREE;
21167 }
21168
21169 /* Leave the SCOPE. */
21170 if (pushed_scope)
21171 pop_scope (pushed_scope);
21172
21173 /* If we failed to resolve it, return the original typename. */
21174 if (!result)
21175 return type;
21176
21177 /* If lookup found a typename type, resolve that too. */
21178 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21179 {
21180 /* Ill-formed programs can cause infinite recursion here, so we
21181 must catch that. */
21182 TYPENAME_IS_RESOLVING_P (type) = 1;
21183 result = resolve_typename_type (result, only_current_p);
21184 TYPENAME_IS_RESOLVING_P (type) = 0;
21185 }
21186
21187 /* Qualify the resulting type. */
21188 quals = cp_type_quals (type);
21189 if (quals)
21190 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21191
21192 return result;
21193 }
21194
21195 /* EXPR is an expression which is not type-dependent. Return a proxy
21196 for EXPR that can be used to compute the types of larger
21197 expressions containing EXPR. */
21198
21199 tree
21200 build_non_dependent_expr (tree expr)
21201 {
21202 tree inner_expr;
21203
21204 #ifdef ENABLE_CHECKING
21205 /* Try to get a constant value for all non-dependent expressions in
21206 order to expose bugs in *_dependent_expression_p and constexpr. */
21207 if (cxx_dialect >= cxx11)
21208 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21209 #endif
21210
21211 /* Preserve OVERLOADs; the functions must be available to resolve
21212 types. */
21213 inner_expr = expr;
21214 if (TREE_CODE (inner_expr) == STMT_EXPR)
21215 inner_expr = stmt_expr_value_expr (inner_expr);
21216 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21217 inner_expr = TREE_OPERAND (inner_expr, 0);
21218 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21219 inner_expr = TREE_OPERAND (inner_expr, 1);
21220 if (is_overloaded_fn (inner_expr)
21221 || TREE_CODE (inner_expr) == OFFSET_REF)
21222 return expr;
21223 /* There is no need to return a proxy for a variable. */
21224 if (VAR_P (expr))
21225 return expr;
21226 /* Preserve string constants; conversions from string constants to
21227 "char *" are allowed, even though normally a "const char *"
21228 cannot be used to initialize a "char *". */
21229 if (TREE_CODE (expr) == STRING_CST)
21230 return expr;
21231 /* Preserve arithmetic constants, as an optimization -- there is no
21232 reason to create a new node. */
21233 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21234 return expr;
21235 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21236 There is at least one place where we want to know that a
21237 particular expression is a throw-expression: when checking a ?:
21238 expression, there are special rules if the second or third
21239 argument is a throw-expression. */
21240 if (TREE_CODE (expr) == THROW_EXPR)
21241 return expr;
21242
21243 /* Don't wrap an initializer list, we need to be able to look inside. */
21244 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21245 return expr;
21246
21247 /* Don't wrap a dummy object, we need to be able to test for it. */
21248 if (is_dummy_object (expr))
21249 return expr;
21250
21251 if (TREE_CODE (expr) == COND_EXPR)
21252 return build3 (COND_EXPR,
21253 TREE_TYPE (expr),
21254 TREE_OPERAND (expr, 0),
21255 (TREE_OPERAND (expr, 1)
21256 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21257 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21258 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21259 if (TREE_CODE (expr) == COMPOUND_EXPR
21260 && !COMPOUND_EXPR_OVERLOADED (expr))
21261 return build2 (COMPOUND_EXPR,
21262 TREE_TYPE (expr),
21263 TREE_OPERAND (expr, 0),
21264 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21265
21266 /* If the type is unknown, it can't really be non-dependent */
21267 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21268
21269 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21270 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21271 }
21272
21273 /* ARGS is a vector of expressions as arguments to a function call.
21274 Replace the arguments with equivalent non-dependent expressions.
21275 This modifies ARGS in place. */
21276
21277 void
21278 make_args_non_dependent (vec<tree, va_gc> *args)
21279 {
21280 unsigned int ix;
21281 tree arg;
21282
21283 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21284 {
21285 tree newarg = build_non_dependent_expr (arg);
21286 if (newarg != arg)
21287 (*args)[ix] = newarg;
21288 }
21289 }
21290
21291 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21292 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21293 parms. */
21294
21295 static tree
21296 make_auto_1 (tree name)
21297 {
21298 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21299 TYPE_NAME (au) = build_decl (input_location,
21300 TYPE_DECL, name, au);
21301 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21302 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21303 (0, processing_template_decl + 1, processing_template_decl + 1,
21304 TYPE_NAME (au), NULL_TREE);
21305 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21306 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21307 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21308
21309 return au;
21310 }
21311
21312 tree
21313 make_decltype_auto (void)
21314 {
21315 return make_auto_1 (get_identifier ("decltype(auto)"));
21316 }
21317
21318 tree
21319 make_auto (void)
21320 {
21321 return make_auto_1 (get_identifier ("auto"));
21322 }
21323
21324 /* Given type ARG, return std::initializer_list<ARG>. */
21325
21326 static tree
21327 listify (tree arg)
21328 {
21329 tree std_init_list = namespace_binding
21330 (get_identifier ("initializer_list"), std_node);
21331 tree argvec;
21332 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21333 {
21334 error ("deducing from brace-enclosed initializer list requires "
21335 "#include <initializer_list>");
21336 return error_mark_node;
21337 }
21338 argvec = make_tree_vec (1);
21339 TREE_VEC_ELT (argvec, 0) = arg;
21340 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21341 NULL_TREE, 0, tf_warning_or_error);
21342 }
21343
21344 /* Replace auto in TYPE with std::initializer_list<auto>. */
21345
21346 static tree
21347 listify_autos (tree type, tree auto_node)
21348 {
21349 tree init_auto = listify (auto_node);
21350 tree argvec = make_tree_vec (1);
21351 TREE_VEC_ELT (argvec, 0) = init_auto;
21352 if (processing_template_decl)
21353 argvec = add_to_template_args (current_template_args (), argvec);
21354 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21355 }
21356
21357 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21358 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21359
21360 tree
21361 do_auto_deduction (tree type, tree init, tree auto_node)
21362 {
21363 tree targs;
21364
21365 if (init == error_mark_node)
21366 return error_mark_node;
21367
21368 if (type_dependent_expression_p (init))
21369 /* Defining a subset of type-dependent expressions that we can deduce
21370 from ahead of time isn't worth the trouble. */
21371 return type;
21372
21373 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21374 with either a new invented type template parameter U or, if the
21375 initializer is a braced-init-list (8.5.4), with
21376 std::initializer_list<U>. */
21377 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21378 type = listify_autos (type, auto_node);
21379
21380 init = resolve_nondeduced_context (init);
21381
21382 targs = make_tree_vec (1);
21383 if (AUTO_IS_DECLTYPE (auto_node))
21384 {
21385 bool id = (DECL_P (init) || TREE_CODE (init) == COMPONENT_REF);
21386 TREE_VEC_ELT (targs, 0)
21387 = finish_decltype_type (init, id, tf_warning_or_error);
21388 if (type != auto_node)
21389 {
21390 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21391 return error_mark_node;
21392 }
21393 }
21394 else
21395 {
21396 tree parms = build_tree_list (NULL_TREE, type);
21397 tree tparms = make_tree_vec (1);
21398 int val;
21399
21400 TREE_VEC_ELT (tparms, 0)
21401 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21402 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21403 DEDUCE_CALL, LOOKUP_NORMAL,
21404 NULL, /*explain_p=*/false);
21405 if (val > 0)
21406 {
21407 if (processing_template_decl)
21408 /* Try again at instantiation time. */
21409 return type;
21410 if (type && type != error_mark_node)
21411 /* If type is error_mark_node a diagnostic must have been
21412 emitted by now. Also, having a mention to '<type error>'
21413 in the diagnostic is not really useful to the user. */
21414 {
21415 if (cfun && auto_node == current_function_auto_return_pattern
21416 && LAMBDA_FUNCTION_P (current_function_decl))
21417 error ("unable to deduce lambda return type from %qE", init);
21418 else
21419 error ("unable to deduce %qT from %qE", type, init);
21420 }
21421 return error_mark_node;
21422 }
21423 }
21424
21425 /* If the list of declarators contains more than one declarator, the type
21426 of each declared variable is determined as described above. If the
21427 type deduced for the template parameter U is not the same in each
21428 deduction, the program is ill-formed. */
21429 if (TREE_TYPE (auto_node)
21430 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21431 {
21432 if (cfun && auto_node == current_function_auto_return_pattern
21433 && LAMBDA_FUNCTION_P (current_function_decl))
21434 error ("inconsistent types %qT and %qT deduced for "
21435 "lambda return type", TREE_TYPE (auto_node),
21436 TREE_VEC_ELT (targs, 0));
21437 else
21438 error ("inconsistent deduction for %qT: %qT and then %qT",
21439 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21440 return error_mark_node;
21441 }
21442 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21443
21444 if (processing_template_decl)
21445 targs = add_to_template_args (current_template_args (), targs);
21446 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21447 }
21448
21449 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21450 result. */
21451
21452 tree
21453 splice_late_return_type (tree type, tree late_return_type)
21454 {
21455 tree argvec;
21456
21457 if (late_return_type == NULL_TREE)
21458 return type;
21459 argvec = make_tree_vec (1);
21460 TREE_VEC_ELT (argvec, 0) = late_return_type;
21461 if (processing_template_parmlist)
21462 /* For a late-specified return type in a template type-parameter, we
21463 need to add a dummy argument level for its parmlist. */
21464 argvec = add_to_template_args
21465 (make_tree_vec (processing_template_parmlist), argvec);
21466 if (current_template_parms)
21467 argvec = add_to_template_args (current_template_args (), argvec);
21468 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21469 }
21470
21471 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21472 'decltype(auto)'. */
21473
21474 bool
21475 is_auto (const_tree type)
21476 {
21477 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21478 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21479 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21480 return true;
21481 else
21482 return false;
21483 }
21484
21485 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21486 a use of `auto'. Returns NULL_TREE otherwise. */
21487
21488 tree
21489 type_uses_auto (tree type)
21490 {
21491 return find_type_usage (type, is_auto);
21492 }
21493
21494 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21495 'decltype(auto)' or a concept. */
21496
21497 bool
21498 is_auto_or_concept (const_tree type)
21499 {
21500 return is_auto (type); // or concept
21501 }
21502
21503 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21504 a concept identifier) iff TYPE contains a use of a generic type. Returns
21505 NULL_TREE otherwise. */
21506
21507 tree
21508 type_uses_auto_or_concept (tree type)
21509 {
21510 return find_type_usage (type, is_auto_or_concept);
21511 }
21512
21513
21514 /* For a given template T, return the vector of typedefs referenced
21515 in T for which access check is needed at T instantiation time.
21516 T is either a FUNCTION_DECL or a RECORD_TYPE.
21517 Those typedefs were added to T by the function
21518 append_type_to_template_for_access_check. */
21519
21520 vec<qualified_typedef_usage_t, va_gc> *
21521 get_types_needing_access_check (tree t)
21522 {
21523 tree ti;
21524 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21525
21526 if (!t || t == error_mark_node)
21527 return NULL;
21528
21529 if (!(ti = get_template_info (t)))
21530 return NULL;
21531
21532 if (CLASS_TYPE_P (t)
21533 || TREE_CODE (t) == FUNCTION_DECL)
21534 {
21535 if (!TI_TEMPLATE (ti))
21536 return NULL;
21537
21538 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21539 }
21540
21541 return result;
21542 }
21543
21544 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21545 tied to T. That list of typedefs will be access checked at
21546 T instantiation time.
21547 T is either a FUNCTION_DECL or a RECORD_TYPE.
21548 TYPE_DECL is a TYPE_DECL node representing a typedef.
21549 SCOPE is the scope through which TYPE_DECL is accessed.
21550 LOCATION is the location of the usage point of TYPE_DECL.
21551
21552 This function is a subroutine of
21553 append_type_to_template_for_access_check. */
21554
21555 static void
21556 append_type_to_template_for_access_check_1 (tree t,
21557 tree type_decl,
21558 tree scope,
21559 location_t location)
21560 {
21561 qualified_typedef_usage_t typedef_usage;
21562 tree ti;
21563
21564 if (!t || t == error_mark_node)
21565 return;
21566
21567 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21568 || CLASS_TYPE_P (t))
21569 && type_decl
21570 && TREE_CODE (type_decl) == TYPE_DECL
21571 && scope);
21572
21573 if (!(ti = get_template_info (t)))
21574 return;
21575
21576 gcc_assert (TI_TEMPLATE (ti));
21577
21578 typedef_usage.typedef_decl = type_decl;
21579 typedef_usage.context = scope;
21580 typedef_usage.locus = location;
21581
21582 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21583 }
21584
21585 /* Append TYPE_DECL to the template TEMPL.
21586 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21587 At TEMPL instanciation time, TYPE_DECL will be checked to see
21588 if it can be accessed through SCOPE.
21589 LOCATION is the location of the usage point of TYPE_DECL.
21590
21591 e.g. consider the following code snippet:
21592
21593 class C
21594 {
21595 typedef int myint;
21596 };
21597
21598 template<class U> struct S
21599 {
21600 C::myint mi; // <-- usage point of the typedef C::myint
21601 };
21602
21603 S<char> s;
21604
21605 At S<char> instantiation time, we need to check the access of C::myint
21606 In other words, we need to check the access of the myint typedef through
21607 the C scope. For that purpose, this function will add the myint typedef
21608 and the scope C through which its being accessed to a list of typedefs
21609 tied to the template S. That list will be walked at template instantiation
21610 time and access check performed on each typedefs it contains.
21611 Note that this particular code snippet should yield an error because
21612 myint is private to C. */
21613
21614 void
21615 append_type_to_template_for_access_check (tree templ,
21616 tree type_decl,
21617 tree scope,
21618 location_t location)
21619 {
21620 qualified_typedef_usage_t *iter;
21621 unsigned i;
21622
21623 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21624
21625 /* Make sure we don't append the type to the template twice. */
21626 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21627 if (iter->typedef_decl == type_decl && scope == iter->context)
21628 return;
21629
21630 append_type_to_template_for_access_check_1 (templ, type_decl,
21631 scope, location);
21632 }
21633
21634 /* Convert the generic type parameters in PARM that match the types given in the
21635 range [START_IDX, END_IDX) from the current_template_parms into generic type
21636 packs. */
21637
21638 tree
21639 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
21640 {
21641 tree current = current_template_parms;
21642 int depth = TMPL_PARMS_DEPTH (current);
21643 current = INNERMOST_TEMPLATE_PARMS (current);
21644 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
21645
21646 for (int i = 0; i < start_idx; ++i)
21647 TREE_VEC_ELT (replacement, i)
21648 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21649
21650 for (int i = start_idx; i < end_idx; ++i)
21651 {
21652 /* Create a distinct parameter pack type from the current parm and add it
21653 to the replacement args to tsubst below into the generic function
21654 parameter. */
21655
21656 tree o = TREE_TYPE (TREE_VALUE
21657 (TREE_VEC_ELT (current, i)));
21658 tree t = copy_type (o);
21659 TEMPLATE_TYPE_PARM_INDEX (t)
21660 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
21661 o, 0, 0, tf_none);
21662 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
21663 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
21664 TYPE_MAIN_VARIANT (t) = t;
21665 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
21666 TYPE_CANONICAL (t) = canonical_type_parameter (t);
21667 TREE_VEC_ELT (replacement, i) = t;
21668 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
21669 }
21670
21671 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
21672 TREE_VEC_ELT (replacement, i)
21673 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21674
21675 /* If there are more levels then build up the replacement with the outer
21676 template parms. */
21677 if (depth > 1)
21678 replacement = add_to_template_args (template_parms_to_args
21679 (TREE_CHAIN (current_template_parms)),
21680 replacement);
21681
21682 return tsubst (parm, replacement, tf_none, NULL_TREE);
21683 }
21684
21685
21686 /* Set up the hash tables for template instantiations. */
21687
21688 void
21689 init_template_processing (void)
21690 {
21691 decl_specializations = htab_create_ggc (37,
21692 hash_specialization,
21693 eq_specializations,
21694 ggc_free);
21695 type_specializations = htab_create_ggc (37,
21696 hash_specialization,
21697 eq_specializations,
21698 ggc_free);
21699 }
21700
21701 /* Print stats about the template hash tables for -fstats. */
21702
21703 void
21704 print_template_statistics (void)
21705 {
21706 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
21707 "%f collisions\n", (long) htab_size (decl_specializations),
21708 (long) htab_elements (decl_specializations),
21709 htab_collisions (decl_specializations));
21710 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
21711 "%f collisions\n", (long) htab_size (type_specializations),
21712 (long) htab_elements (type_specializations),
21713 htab_collisions (type_specializations));
21714 }
21715
21716 #include "gt-cp-pt.h"