re PR c++/54440 ([c++11] g++ prematurely applying rule that a template parameter...
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "pointer-set.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
44 #include "decl.h"
45 #include "toplev.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
48 #include "type-utils.h"
49 #include "gimplify.h"
50
51 /* The type of functions taking a tree, and some additional data, and
52 returning an int. */
53 typedef int (*tree_fn_t) (tree, void*);
54
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY ((chain_next ("%h.next"))) pending_template {
59 struct pending_template *next;
60 struct tinst_level *tinst;
61 };
62
63 static GTY(()) struct pending_template *pending_templates;
64 static GTY(()) struct pending_template *last_pending_template;
65
66 int processing_template_parmlist;
67 static int template_header_count;
68
69 static GTY(()) tree saved_trees;
70 static vec<int> inline_parm_levels;
71
72 static GTY(()) struct tinst_level *current_tinst_level;
73
74 static GTY(()) tree saved_access_scope;
75
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr;
80
81 /* True if we've recursed into fn_type_unification too many times. */
82 static bool excessive_deduction_depth;
83
84 typedef struct GTY(()) spec_entry
85 {
86 tree tmpl;
87 tree args;
88 tree spec;
89 } spec_entry;
90
91 static GTY ((param_is (spec_entry)))
92 htab_t decl_specializations;
93
94 static GTY ((param_is (spec_entry)))
95 htab_t type_specializations;
96
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
102
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
111
112 enum template_base_result {
113 tbr_incomplete_type,
114 tbr_ambiguous_baseclass,
115 tbr_success
116 };
117
118 static void push_access_scope (tree);
119 static void pop_access_scope (tree);
120 static bool resolve_overloaded_unification (tree, tree, tree, tree,
121 unification_kind_t, int,
122 bool);
123 static int try_one_overload (tree, tree, tree, tree, tree,
124 unification_kind_t, int, bool, bool);
125 static int unify (tree, tree, tree, tree, int, bool);
126 static void add_pending_template (tree);
127 static tree reopen_tinst_level (struct tinst_level *);
128 static tree tsubst_initializer_list (tree, tree);
129 static tree get_class_bindings (tree, tree, tree, tree);
130 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
131 bool, bool);
132 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
133 bool, bool);
134 static void tsubst_enum (tree, tree, tree);
135 static tree add_to_template_args (tree, tree);
136 static tree add_outermost_template_args (tree, tree);
137 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
138 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
139 tree);
140 static int type_unification_real (tree, tree, tree, const tree *,
141 unsigned int, int, unification_kind_t, int,
142 vec<deferred_access_check, va_gc> **,
143 bool);
144 static void note_template_header (int);
145 static tree convert_nontype_argument_function (tree, tree);
146 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
147 static tree convert_template_argument (tree, tree, tree,
148 tsubst_flags_t, int, tree);
149 static int for_each_template_parm (tree, tree_fn_t, void*,
150 struct pointer_set_t*, bool);
151 static tree expand_template_argument_pack (tree);
152 static tree build_template_parm_index (int, int, int, tree, tree);
153 static bool inline_needs_template_parms (tree, bool);
154 static void push_inline_template_parms_recursive (tree, int);
155 static tree retrieve_local_specialization (tree);
156 static void register_local_specialization (tree, tree);
157 static hashval_t hash_specialization (const void *p);
158 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
159 static int mark_template_parm (tree, void *);
160 static int template_parm_this_level_p (tree, void *);
161 static tree tsubst_friend_function (tree, tree);
162 static tree tsubst_friend_class (tree, tree);
163 static int can_complete_type_without_circularity (tree);
164 static tree get_bindings (tree, tree, tree, bool);
165 static int template_decl_level (tree);
166 static int check_cv_quals_for_unify (int, tree, tree);
167 static void template_parm_level_and_index (tree, int*, int*);
168 static int unify_pack_expansion (tree, tree, tree,
169 tree, unification_kind_t, bool, bool);
170 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
171 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
172 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
173 static void regenerate_decl_from_template (tree, tree);
174 static tree most_specialized_class (tree, tsubst_flags_t);
175 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
176 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
177 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
178 static bool check_specialization_scope (void);
179 static tree process_partial_specialization (tree);
180 static void set_current_access_from_decl (tree);
181 static enum template_base_result get_template_base (tree, tree, tree, tree,
182 bool , tree *);
183 static tree try_class_unification (tree, tree, tree, tree, bool);
184 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
185 tree, tree);
186 static bool template_template_parm_bindings_ok_p (tree, tree);
187 static int template_args_equal (tree, tree);
188 static void tsubst_default_arguments (tree, tsubst_flags_t);
189 static tree for_each_template_parm_r (tree *, int *, void *);
190 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
191 static void copy_default_args_to_explicit_spec (tree);
192 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
193 static bool dependent_template_arg_p (tree);
194 static bool any_template_arguments_need_structural_equality_p (tree);
195 static bool dependent_type_p_r (tree);
196 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
197 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
198 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
199 static tree tsubst_decl (tree, tree, tsubst_flags_t);
200 static void perform_typedefs_access_check (tree tmpl, tree targs);
201 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
202 location_t);
203 static tree listify (tree);
204 static tree listify_autos (tree, tree);
205 static tree template_parm_to_arg (tree t);
206 static tree current_template_args (void);
207 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
208 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
209
210 /* Make the current scope suitable for access checking when we are
211 processing T. T can be FUNCTION_DECL for instantiated function
212 template, VAR_DECL for static member variable, or TYPE_DECL for
213 alias template (needed by instantiate_decl). */
214
215 static void
216 push_access_scope (tree t)
217 {
218 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
219 || TREE_CODE (t) == TYPE_DECL);
220
221 if (DECL_FRIEND_CONTEXT (t))
222 push_nested_class (DECL_FRIEND_CONTEXT (t));
223 else if (DECL_CLASS_SCOPE_P (t))
224 push_nested_class (DECL_CONTEXT (t));
225 else
226 push_to_top_level ();
227
228 if (TREE_CODE (t) == FUNCTION_DECL)
229 {
230 saved_access_scope = tree_cons
231 (NULL_TREE, current_function_decl, saved_access_scope);
232 current_function_decl = t;
233 }
234 }
235
236 /* Restore the scope set up by push_access_scope. T is the node we
237 are processing. */
238
239 static void
240 pop_access_scope (tree t)
241 {
242 if (TREE_CODE (t) == FUNCTION_DECL)
243 {
244 current_function_decl = TREE_VALUE (saved_access_scope);
245 saved_access_scope = TREE_CHAIN (saved_access_scope);
246 }
247
248 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
249 pop_nested_class ();
250 else
251 pop_from_top_level ();
252 }
253
254 /* Do any processing required when DECL (a member template
255 declaration) is finished. Returns the TEMPLATE_DECL corresponding
256 to DECL, unless it is a specialization, in which case the DECL
257 itself is returned. */
258
259 tree
260 finish_member_template_decl (tree decl)
261 {
262 if (decl == error_mark_node)
263 return error_mark_node;
264
265 gcc_assert (DECL_P (decl));
266
267 if (TREE_CODE (decl) == TYPE_DECL)
268 {
269 tree type;
270
271 type = TREE_TYPE (decl);
272 if (type == error_mark_node)
273 return error_mark_node;
274 if (MAYBE_CLASS_TYPE_P (type)
275 && CLASSTYPE_TEMPLATE_INFO (type)
276 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
277 {
278 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
279 check_member_template (tmpl);
280 return tmpl;
281 }
282 return NULL_TREE;
283 }
284 else if (TREE_CODE (decl) == FIELD_DECL)
285 error ("data member %qD cannot be a member template", decl);
286 else if (DECL_TEMPLATE_INFO (decl))
287 {
288 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
289 {
290 check_member_template (DECL_TI_TEMPLATE (decl));
291 return DECL_TI_TEMPLATE (decl);
292 }
293 else
294 return decl;
295 }
296 else
297 error ("invalid member template declaration %qD", decl);
298
299 return error_mark_node;
300 }
301
302 /* Create a template info node. */
303
304 tree
305 build_template_info (tree template_decl, tree template_args)
306 {
307 tree result = make_node (TEMPLATE_INFO);
308 TI_TEMPLATE (result) = template_decl;
309 TI_ARGS (result) = template_args;
310 return result;
311 }
312
313 /* Return the template info node corresponding to T, whatever T is. */
314
315 tree
316 get_template_info (const_tree t)
317 {
318 tree tinfo = NULL_TREE;
319
320 if (!t || t == error_mark_node)
321 return NULL;
322
323 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
324 tinfo = DECL_TEMPLATE_INFO (t);
325
326 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
327 t = TREE_TYPE (t);
328
329 if (OVERLOAD_TYPE_P (t))
330 tinfo = TYPE_TEMPLATE_INFO (t);
331 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
332 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
333
334 return tinfo;
335 }
336
337 /* Returns the template nesting level of the indicated class TYPE.
338
339 For example, in:
340 template <class T>
341 struct A
342 {
343 template <class U>
344 struct B {};
345 };
346
347 A<T>::B<U> has depth two, while A<T> has depth one.
348 Both A<T>::B<int> and A<int>::B<U> have depth one, if
349 they are instantiations, not specializations.
350
351 This function is guaranteed to return 0 if passed NULL_TREE so
352 that, for example, `template_class_depth (current_class_type)' is
353 always safe. */
354
355 int
356 template_class_depth (tree type)
357 {
358 int depth;
359
360 for (depth = 0;
361 type && TREE_CODE (type) != NAMESPACE_DECL;
362 type = (TREE_CODE (type) == FUNCTION_DECL)
363 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
364 {
365 tree tinfo = get_template_info (type);
366
367 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
368 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
369 ++depth;
370 }
371
372 return depth;
373 }
374
375 /* Subroutine of maybe_begin_member_template_processing.
376 Returns true if processing DECL needs us to push template parms. */
377
378 static bool
379 inline_needs_template_parms (tree decl, bool nsdmi)
380 {
381 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
382 return false;
383
384 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
385 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
386 }
387
388 /* Subroutine of maybe_begin_member_template_processing.
389 Push the template parms in PARMS, starting from LEVELS steps into the
390 chain, and ending at the beginning, since template parms are listed
391 innermost first. */
392
393 static void
394 push_inline_template_parms_recursive (tree parmlist, int levels)
395 {
396 tree parms = TREE_VALUE (parmlist);
397 int i;
398
399 if (levels > 1)
400 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
401
402 ++processing_template_decl;
403 current_template_parms
404 = tree_cons (size_int (processing_template_decl),
405 parms, current_template_parms);
406 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
407
408 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
409 NULL);
410 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
411 {
412 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
413
414 if (parm == error_mark_node)
415 continue;
416
417 gcc_assert (DECL_P (parm));
418
419 switch (TREE_CODE (parm))
420 {
421 case TYPE_DECL:
422 case TEMPLATE_DECL:
423 pushdecl (parm);
424 break;
425
426 case PARM_DECL:
427 {
428 /* Make a CONST_DECL as is done in process_template_parm.
429 It is ugly that we recreate this here; the original
430 version built in process_template_parm is no longer
431 available. */
432 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
433 CONST_DECL, DECL_NAME (parm),
434 TREE_TYPE (parm));
435 DECL_ARTIFICIAL (decl) = 1;
436 TREE_CONSTANT (decl) = 1;
437 TREE_READONLY (decl) = 1;
438 DECL_INITIAL (decl) = DECL_INITIAL (parm);
439 SET_DECL_TEMPLATE_PARM_P (decl);
440 pushdecl (decl);
441 }
442 break;
443
444 default:
445 gcc_unreachable ();
446 }
447 }
448 }
449
450 /* Restore the template parameter context for a member template, a
451 friend template defined in a class definition, or a non-template
452 member of template class. */
453
454 void
455 maybe_begin_member_template_processing (tree decl)
456 {
457 tree parms;
458 int levels = 0;
459 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
460
461 if (nsdmi)
462 decl = (CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
463 ? CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (decl))
464 : NULL_TREE);
465
466 if (inline_needs_template_parms (decl, nsdmi))
467 {
468 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
469 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
470
471 if (DECL_TEMPLATE_SPECIALIZATION (decl))
472 {
473 --levels;
474 parms = TREE_CHAIN (parms);
475 }
476
477 push_inline_template_parms_recursive (parms, levels);
478 }
479
480 /* Remember how many levels of template parameters we pushed so that
481 we can pop them later. */
482 inline_parm_levels.safe_push (levels);
483 }
484
485 /* Undo the effects of maybe_begin_member_template_processing. */
486
487 void
488 maybe_end_member_template_processing (void)
489 {
490 int i;
491 int last;
492
493 if (inline_parm_levels.length () == 0)
494 return;
495
496 last = inline_parm_levels.pop ();
497 for (i = 0; i < last; ++i)
498 {
499 --processing_template_decl;
500 current_template_parms = TREE_CHAIN (current_template_parms);
501 poplevel (0, 0, 0);
502 }
503 }
504
505 /* Return a new template argument vector which contains all of ARGS,
506 but has as its innermost set of arguments the EXTRA_ARGS. */
507
508 static tree
509 add_to_template_args (tree args, tree extra_args)
510 {
511 tree new_args;
512 int extra_depth;
513 int i;
514 int j;
515
516 if (args == NULL_TREE || extra_args == error_mark_node)
517 return extra_args;
518
519 extra_depth = TMPL_ARGS_DEPTH (extra_args);
520 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
521
522 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
523 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
524
525 for (j = 1; j <= extra_depth; ++j, ++i)
526 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
527
528 return new_args;
529 }
530
531 /* Like add_to_template_args, but only the outermost ARGS are added to
532 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
533 (EXTRA_ARGS) levels are added. This function is used to combine
534 the template arguments from a partial instantiation with the
535 template arguments used to attain the full instantiation from the
536 partial instantiation. */
537
538 static tree
539 add_outermost_template_args (tree args, tree extra_args)
540 {
541 tree new_args;
542
543 /* If there are more levels of EXTRA_ARGS than there are ARGS,
544 something very fishy is going on. */
545 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
546
547 /* If *all* the new arguments will be the EXTRA_ARGS, just return
548 them. */
549 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
550 return extra_args;
551
552 /* For the moment, we make ARGS look like it contains fewer levels. */
553 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
554
555 new_args = add_to_template_args (args, extra_args);
556
557 /* Now, we restore ARGS to its full dimensions. */
558 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
559
560 return new_args;
561 }
562
563 /* Return the N levels of innermost template arguments from the ARGS. */
564
565 tree
566 get_innermost_template_args (tree args, int n)
567 {
568 tree new_args;
569 int extra_levels;
570 int i;
571
572 gcc_assert (n >= 0);
573
574 /* If N is 1, just return the innermost set of template arguments. */
575 if (n == 1)
576 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
577
578 /* If we're not removing anything, just return the arguments we were
579 given. */
580 extra_levels = TMPL_ARGS_DEPTH (args) - n;
581 gcc_assert (extra_levels >= 0);
582 if (extra_levels == 0)
583 return args;
584
585 /* Make a new set of arguments, not containing the outer arguments. */
586 new_args = make_tree_vec (n);
587 for (i = 1; i <= n; ++i)
588 SET_TMPL_ARGS_LEVEL (new_args, i,
589 TMPL_ARGS_LEVEL (args, i + extra_levels));
590
591 return new_args;
592 }
593
594 /* The inverse of get_innermost_template_args: Return all but the innermost
595 EXTRA_LEVELS levels of template arguments from the ARGS. */
596
597 static tree
598 strip_innermost_template_args (tree args, int extra_levels)
599 {
600 tree new_args;
601 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
602 int i;
603
604 gcc_assert (n >= 0);
605
606 /* If N is 1, just return the outermost set of template arguments. */
607 if (n == 1)
608 return TMPL_ARGS_LEVEL (args, 1);
609
610 /* If we're not removing anything, just return the arguments we were
611 given. */
612 gcc_assert (extra_levels >= 0);
613 if (extra_levels == 0)
614 return args;
615
616 /* Make a new set of arguments, not containing the inner arguments. */
617 new_args = make_tree_vec (n);
618 for (i = 1; i <= n; ++i)
619 SET_TMPL_ARGS_LEVEL (new_args, i,
620 TMPL_ARGS_LEVEL (args, i));
621
622 return new_args;
623 }
624
625 /* We've got a template header coming up; push to a new level for storing
626 the parms. */
627
628 void
629 begin_template_parm_list (void)
630 {
631 /* We use a non-tag-transparent scope here, which causes pushtag to
632 put tags in this scope, rather than in the enclosing class or
633 namespace scope. This is the right thing, since we want
634 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
635 global template class, push_template_decl handles putting the
636 TEMPLATE_DECL into top-level scope. For a nested template class,
637 e.g.:
638
639 template <class T> struct S1 {
640 template <class T> struct S2 {};
641 };
642
643 pushtag contains special code to call pushdecl_with_scope on the
644 TEMPLATE_DECL for S2. */
645 begin_scope (sk_template_parms, NULL);
646 ++processing_template_decl;
647 ++processing_template_parmlist;
648 note_template_header (0);
649 }
650
651 /* This routine is called when a specialization is declared. If it is
652 invalid to declare a specialization here, an error is reported and
653 false is returned, otherwise this routine will return true. */
654
655 static bool
656 check_specialization_scope (void)
657 {
658 tree scope = current_scope ();
659
660 /* [temp.expl.spec]
661
662 An explicit specialization shall be declared in the namespace of
663 which the template is a member, or, for member templates, in the
664 namespace of which the enclosing class or enclosing class
665 template is a member. An explicit specialization of a member
666 function, member class or static data member of a class template
667 shall be declared in the namespace of which the class template
668 is a member. */
669 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
670 {
671 error ("explicit specialization in non-namespace scope %qD", scope);
672 return false;
673 }
674
675 /* [temp.expl.spec]
676
677 In an explicit specialization declaration for a member of a class
678 template or a member template that appears in namespace scope,
679 the member template and some of its enclosing class templates may
680 remain unspecialized, except that the declaration shall not
681 explicitly specialize a class member template if its enclosing
682 class templates are not explicitly specialized as well. */
683 if (current_template_parms)
684 {
685 error ("enclosing class templates are not explicitly specialized");
686 return false;
687 }
688
689 return true;
690 }
691
692 /* We've just seen template <>. */
693
694 bool
695 begin_specialization (void)
696 {
697 begin_scope (sk_template_spec, NULL);
698 note_template_header (1);
699 return check_specialization_scope ();
700 }
701
702 /* Called at then end of processing a declaration preceded by
703 template<>. */
704
705 void
706 end_specialization (void)
707 {
708 finish_scope ();
709 reset_specialization ();
710 }
711
712 /* Any template <>'s that we have seen thus far are not referring to a
713 function specialization. */
714
715 void
716 reset_specialization (void)
717 {
718 processing_specialization = 0;
719 template_header_count = 0;
720 }
721
722 /* We've just seen a template header. If SPECIALIZATION is nonzero,
723 it was of the form template <>. */
724
725 static void
726 note_template_header (int specialization)
727 {
728 processing_specialization = specialization;
729 template_header_count++;
730 }
731
732 /* We're beginning an explicit instantiation. */
733
734 void
735 begin_explicit_instantiation (void)
736 {
737 gcc_assert (!processing_explicit_instantiation);
738 processing_explicit_instantiation = true;
739 }
740
741
742 void
743 end_explicit_instantiation (void)
744 {
745 gcc_assert (processing_explicit_instantiation);
746 processing_explicit_instantiation = false;
747 }
748
749 /* An explicit specialization or partial specialization of TMPL is being
750 declared. Check that the namespace in which the specialization is
751 occurring is permissible. Returns false iff it is invalid to
752 specialize TMPL in the current namespace. */
753
754 static bool
755 check_specialization_namespace (tree tmpl)
756 {
757 tree tpl_ns = decl_namespace_context (tmpl);
758
759 /* [tmpl.expl.spec]
760
761 An explicit specialization shall be declared in the namespace of
762 which the template is a member, or, for member templates, in the
763 namespace of which the enclosing class or enclosing class
764 template is a member. An explicit specialization of a member
765 function, member class or static data member of a class template
766 shall be declared in the namespace of which the class template is
767 a member. */
768 if (current_scope() != DECL_CONTEXT (tmpl)
769 && !at_namespace_scope_p ())
770 {
771 error ("specialization of %qD must appear at namespace scope", tmpl);
772 return false;
773 }
774 if (is_associated_namespace (current_namespace, tpl_ns))
775 /* Same or super-using namespace. */
776 return true;
777 else
778 {
779 permerror (input_location, "specialization of %qD in different namespace", tmpl);
780 permerror (input_location, " from definition of %q+#D", tmpl);
781 return false;
782 }
783 }
784
785 /* SPEC is an explicit instantiation. Check that it is valid to
786 perform this explicit instantiation in the current namespace. */
787
788 static void
789 check_explicit_instantiation_namespace (tree spec)
790 {
791 tree ns;
792
793 /* DR 275: An explicit instantiation shall appear in an enclosing
794 namespace of its template. */
795 ns = decl_namespace_context (spec);
796 if (!is_ancestor (current_namespace, ns))
797 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
798 "(which does not enclose namespace %qD)",
799 spec, current_namespace, ns);
800 }
801
802 /* The TYPE is being declared. If it is a template type, that means it
803 is a partial specialization. Do appropriate error-checking. */
804
805 tree
806 maybe_process_partial_specialization (tree type)
807 {
808 tree context;
809
810 if (type == error_mark_node)
811 return error_mark_node;
812
813 /* A lambda that appears in specialization context is not itself a
814 specialization. */
815 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
816 return type;
817
818 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
819 {
820 error ("name of class shadows template template parameter %qD",
821 TYPE_NAME (type));
822 return error_mark_node;
823 }
824
825 context = TYPE_CONTEXT (type);
826
827 if (TYPE_ALIAS_P (type))
828 {
829 if (TYPE_TEMPLATE_INFO (type)
830 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
831 error ("specialization of alias template %qD",
832 TYPE_TI_TEMPLATE (type));
833 else
834 error ("explicit specialization of non-template %qT", type);
835 return error_mark_node;
836 }
837 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
838 {
839 /* This is for ordinary explicit specialization and partial
840 specialization of a template class such as:
841
842 template <> class C<int>;
843
844 or:
845
846 template <class T> class C<T*>;
847
848 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
849
850 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
851 && !COMPLETE_TYPE_P (type))
852 {
853 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
854 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
855 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
856 if (processing_template_decl)
857 {
858 if (push_template_decl (TYPE_MAIN_DECL (type))
859 == error_mark_node)
860 return error_mark_node;
861 }
862 }
863 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
864 error ("specialization of %qT after instantiation", type);
865 else if (errorcount && !processing_specialization
866 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
867 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
868 /* Trying to define a specialization either without a template<> header
869 or in an inappropriate place. We've already given an error, so just
870 bail now so we don't actually define the specialization. */
871 return error_mark_node;
872 }
873 else if (CLASS_TYPE_P (type)
874 && !CLASSTYPE_USE_TEMPLATE (type)
875 && CLASSTYPE_TEMPLATE_INFO (type)
876 && context && CLASS_TYPE_P (context)
877 && CLASSTYPE_TEMPLATE_INFO (context))
878 {
879 /* This is for an explicit specialization of member class
880 template according to [temp.expl.spec/18]:
881
882 template <> template <class U> class C<int>::D;
883
884 The context `C<int>' must be an implicit instantiation.
885 Otherwise this is just a member class template declared
886 earlier like:
887
888 template <> class C<int> { template <class U> class D; };
889 template <> template <class U> class C<int>::D;
890
891 In the first case, `C<int>::D' is a specialization of `C<T>::D'
892 while in the second case, `C<int>::D' is a primary template
893 and `C<T>::D' may not exist. */
894
895 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
896 && !COMPLETE_TYPE_P (type))
897 {
898 tree t;
899 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
900
901 if (current_namespace
902 != decl_namespace_context (tmpl))
903 {
904 permerror (input_location, "specializing %q#T in different namespace", type);
905 permerror (input_location, " from definition of %q+#D", tmpl);
906 }
907
908 /* Check for invalid specialization after instantiation:
909
910 template <> template <> class C<int>::D<int>;
911 template <> template <class U> class C<int>::D; */
912
913 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
914 t; t = TREE_CHAIN (t))
915 {
916 tree inst = TREE_VALUE (t);
917 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
918 || !COMPLETE_OR_OPEN_TYPE_P (inst))
919 {
920 /* We already have a full specialization of this partial
921 instantiation, or a full specialization has been
922 looked up but not instantiated. Reassign it to the
923 new member specialization template. */
924 spec_entry elt;
925 spec_entry *entry;
926 void **slot;
927
928 elt.tmpl = most_general_template (tmpl);
929 elt.args = CLASSTYPE_TI_ARGS (inst);
930 elt.spec = inst;
931
932 htab_remove_elt (type_specializations, &elt);
933
934 elt.tmpl = tmpl;
935 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
936
937 slot = htab_find_slot (type_specializations, &elt, INSERT);
938 entry = ggc_alloc_spec_entry ();
939 *entry = elt;
940 *slot = entry;
941 }
942 else
943 /* But if we've had an implicit instantiation, that's a
944 problem ([temp.expl.spec]/6). */
945 error ("specialization %qT after instantiation %qT",
946 type, inst);
947 }
948
949 /* Mark TYPE as a specialization. And as a result, we only
950 have one level of template argument for the innermost
951 class template. */
952 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
953 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
954 CLASSTYPE_TI_ARGS (type)
955 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
956 }
957 }
958 else if (processing_specialization)
959 {
960 /* Someday C++0x may allow for enum template specialization. */
961 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
962 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
963 pedwarn (input_location, OPT_Wpedantic, "template specialization "
964 "of %qD not allowed by ISO C++", type);
965 else
966 {
967 error ("explicit specialization of non-template %qT", type);
968 return error_mark_node;
969 }
970 }
971
972 return type;
973 }
974
975 /* Returns nonzero if we can optimize the retrieval of specializations
976 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
977 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
978
979 static inline bool
980 optimize_specialization_lookup_p (tree tmpl)
981 {
982 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
983 && DECL_CLASS_SCOPE_P (tmpl)
984 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
985 parameter. */
986 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
987 /* The optimized lookup depends on the fact that the
988 template arguments for the member function template apply
989 purely to the containing class, which is not true if the
990 containing class is an explicit or partial
991 specialization. */
992 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
993 && !DECL_MEMBER_TEMPLATE_P (tmpl)
994 && !DECL_CONV_FN_P (tmpl)
995 /* It is possible to have a template that is not a member
996 template and is not a member of a template class:
997
998 template <typename T>
999 struct S { friend A::f(); };
1000
1001 Here, the friend function is a template, but the context does
1002 not have template information. The optimized lookup relies
1003 on having ARGS be the template arguments for both the class
1004 and the function template. */
1005 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1006 }
1007
1008 /* Retrieve the specialization (in the sense of [temp.spec] - a
1009 specialization is either an instantiation or an explicit
1010 specialization) of TMPL for the given template ARGS. If there is
1011 no such specialization, return NULL_TREE. The ARGS are a vector of
1012 arguments, or a vector of vectors of arguments, in the case of
1013 templates with more than one level of parameters.
1014
1015 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1016 then we search for a partial specialization matching ARGS. This
1017 parameter is ignored if TMPL is not a class template.
1018
1019 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1020 result is a NONTYPE_ARGUMENT_PACK. */
1021
1022 static tree
1023 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1024 {
1025 if (tmpl == NULL_TREE)
1026 return NULL_TREE;
1027
1028 if (args == error_mark_node)
1029 return NULL_TREE;
1030
1031 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1032 || TREE_CODE (tmpl) == FIELD_DECL);
1033
1034 /* There should be as many levels of arguments as there are
1035 levels of parameters. */
1036 gcc_assert (TMPL_ARGS_DEPTH (args)
1037 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1038 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1039 : template_class_depth (DECL_CONTEXT (tmpl))));
1040
1041 if (optimize_specialization_lookup_p (tmpl))
1042 {
1043 tree class_template;
1044 tree class_specialization;
1045 vec<tree, va_gc> *methods;
1046 tree fns;
1047 int idx;
1048
1049 /* The template arguments actually apply to the containing
1050 class. Find the class specialization with those
1051 arguments. */
1052 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1053 class_specialization
1054 = retrieve_specialization (class_template, args, 0);
1055 if (!class_specialization)
1056 return NULL_TREE;
1057 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1058 for the specialization. */
1059 idx = class_method_index_for_fn (class_specialization, tmpl);
1060 if (idx == -1)
1061 return NULL_TREE;
1062 /* Iterate through the methods with the indicated name, looking
1063 for the one that has an instance of TMPL. */
1064 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1065 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1066 {
1067 tree fn = OVL_CURRENT (fns);
1068 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1069 /* using-declarations can add base methods to the method vec,
1070 and we don't want those here. */
1071 && DECL_CONTEXT (fn) == class_specialization)
1072 return fn;
1073 }
1074 return NULL_TREE;
1075 }
1076 else
1077 {
1078 spec_entry *found;
1079 spec_entry elt;
1080 htab_t specializations;
1081
1082 elt.tmpl = tmpl;
1083 elt.args = args;
1084 elt.spec = NULL_TREE;
1085
1086 if (DECL_CLASS_TEMPLATE_P (tmpl))
1087 specializations = type_specializations;
1088 else
1089 specializations = decl_specializations;
1090
1091 if (hash == 0)
1092 hash = hash_specialization (&elt);
1093 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1094 if (found)
1095 return found->spec;
1096 }
1097
1098 return NULL_TREE;
1099 }
1100
1101 /* Like retrieve_specialization, but for local declarations. */
1102
1103 static tree
1104 retrieve_local_specialization (tree tmpl)
1105 {
1106 void **slot;
1107
1108 if (local_specializations == NULL)
1109 return NULL_TREE;
1110
1111 slot = pointer_map_contains (local_specializations, tmpl);
1112 return slot ? (tree) *slot : NULL_TREE;
1113 }
1114
1115 /* Returns nonzero iff DECL is a specialization of TMPL. */
1116
1117 int
1118 is_specialization_of (tree decl, tree tmpl)
1119 {
1120 tree t;
1121
1122 if (TREE_CODE (decl) == FUNCTION_DECL)
1123 {
1124 for (t = decl;
1125 t != NULL_TREE;
1126 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1127 if (t == tmpl)
1128 return 1;
1129 }
1130 else
1131 {
1132 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1133
1134 for (t = TREE_TYPE (decl);
1135 t != NULL_TREE;
1136 t = CLASSTYPE_USE_TEMPLATE (t)
1137 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1138 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1139 return 1;
1140 }
1141
1142 return 0;
1143 }
1144
1145 /* Returns nonzero iff DECL is a specialization of friend declaration
1146 FRIEND_DECL according to [temp.friend]. */
1147
1148 bool
1149 is_specialization_of_friend (tree decl, tree friend_decl)
1150 {
1151 bool need_template = true;
1152 int template_depth;
1153
1154 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1155 || TREE_CODE (decl) == TYPE_DECL);
1156
1157 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1158 of a template class, we want to check if DECL is a specialization
1159 if this. */
1160 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1161 && DECL_TEMPLATE_INFO (friend_decl)
1162 && !DECL_USE_TEMPLATE (friend_decl))
1163 {
1164 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1165 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1166 need_template = false;
1167 }
1168 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1169 && !PRIMARY_TEMPLATE_P (friend_decl))
1170 need_template = false;
1171
1172 /* There is nothing to do if this is not a template friend. */
1173 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1174 return false;
1175
1176 if (is_specialization_of (decl, friend_decl))
1177 return true;
1178
1179 /* [temp.friend/6]
1180 A member of a class template may be declared to be a friend of a
1181 non-template class. In this case, the corresponding member of
1182 every specialization of the class template is a friend of the
1183 class granting friendship.
1184
1185 For example, given a template friend declaration
1186
1187 template <class T> friend void A<T>::f();
1188
1189 the member function below is considered a friend
1190
1191 template <> struct A<int> {
1192 void f();
1193 };
1194
1195 For this type of template friend, TEMPLATE_DEPTH below will be
1196 nonzero. To determine if DECL is a friend of FRIEND, we first
1197 check if the enclosing class is a specialization of another. */
1198
1199 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1200 if (template_depth
1201 && DECL_CLASS_SCOPE_P (decl)
1202 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1203 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1204 {
1205 /* Next, we check the members themselves. In order to handle
1206 a few tricky cases, such as when FRIEND_DECL's are
1207
1208 template <class T> friend void A<T>::g(T t);
1209 template <class T> template <T t> friend void A<T>::h();
1210
1211 and DECL's are
1212
1213 void A<int>::g(int);
1214 template <int> void A<int>::h();
1215
1216 we need to figure out ARGS, the template arguments from
1217 the context of DECL. This is required for template substitution
1218 of `T' in the function parameter of `g' and template parameter
1219 of `h' in the above examples. Here ARGS corresponds to `int'. */
1220
1221 tree context = DECL_CONTEXT (decl);
1222 tree args = NULL_TREE;
1223 int current_depth = 0;
1224
1225 while (current_depth < template_depth)
1226 {
1227 if (CLASSTYPE_TEMPLATE_INFO (context))
1228 {
1229 if (current_depth == 0)
1230 args = TYPE_TI_ARGS (context);
1231 else
1232 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1233 current_depth++;
1234 }
1235 context = TYPE_CONTEXT (context);
1236 }
1237
1238 if (TREE_CODE (decl) == FUNCTION_DECL)
1239 {
1240 bool is_template;
1241 tree friend_type;
1242 tree decl_type;
1243 tree friend_args_type;
1244 tree decl_args_type;
1245
1246 /* Make sure that both DECL and FRIEND_DECL are templates or
1247 non-templates. */
1248 is_template = DECL_TEMPLATE_INFO (decl)
1249 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1250 if (need_template ^ is_template)
1251 return false;
1252 else if (is_template)
1253 {
1254 /* If both are templates, check template parameter list. */
1255 tree friend_parms
1256 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1257 args, tf_none);
1258 if (!comp_template_parms
1259 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1260 friend_parms))
1261 return false;
1262
1263 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1264 }
1265 else
1266 decl_type = TREE_TYPE (decl);
1267
1268 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1269 tf_none, NULL_TREE);
1270 if (friend_type == error_mark_node)
1271 return false;
1272
1273 /* Check if return types match. */
1274 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1275 return false;
1276
1277 /* Check if function parameter types match, ignoring the
1278 `this' parameter. */
1279 friend_args_type = TYPE_ARG_TYPES (friend_type);
1280 decl_args_type = TYPE_ARG_TYPES (decl_type);
1281 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1282 friend_args_type = TREE_CHAIN (friend_args_type);
1283 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1284 decl_args_type = TREE_CHAIN (decl_args_type);
1285
1286 return compparms (decl_args_type, friend_args_type);
1287 }
1288 else
1289 {
1290 /* DECL is a TYPE_DECL */
1291 bool is_template;
1292 tree decl_type = TREE_TYPE (decl);
1293
1294 /* Make sure that both DECL and FRIEND_DECL are templates or
1295 non-templates. */
1296 is_template
1297 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1298 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1299
1300 if (need_template ^ is_template)
1301 return false;
1302 else if (is_template)
1303 {
1304 tree friend_parms;
1305 /* If both are templates, check the name of the two
1306 TEMPLATE_DECL's first because is_friend didn't. */
1307 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1308 != DECL_NAME (friend_decl))
1309 return false;
1310
1311 /* Now check template parameter list. */
1312 friend_parms
1313 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1314 args, tf_none);
1315 return comp_template_parms
1316 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1317 friend_parms);
1318 }
1319 else
1320 return (DECL_NAME (decl)
1321 == DECL_NAME (friend_decl));
1322 }
1323 }
1324 return false;
1325 }
1326
1327 /* Register the specialization SPEC as a specialization of TMPL with
1328 the indicated ARGS. IS_FRIEND indicates whether the specialization
1329 is actually just a friend declaration. Returns SPEC, or an
1330 equivalent prior declaration, if available.
1331
1332 We also store instantiations of field packs in the hash table, even
1333 though they are not themselves templates, to make lookup easier. */
1334
1335 static tree
1336 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1337 hashval_t hash)
1338 {
1339 tree fn;
1340 void **slot = NULL;
1341 spec_entry elt;
1342
1343 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1344 || (TREE_CODE (tmpl) == FIELD_DECL
1345 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1346
1347 if (TREE_CODE (spec) == FUNCTION_DECL
1348 && uses_template_parms (DECL_TI_ARGS (spec)))
1349 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1350 register it; we want the corresponding TEMPLATE_DECL instead.
1351 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1352 the more obvious `uses_template_parms (spec)' to avoid problems
1353 with default function arguments. In particular, given
1354 something like this:
1355
1356 template <class T> void f(T t1, T t = T())
1357
1358 the default argument expression is not substituted for in an
1359 instantiation unless and until it is actually needed. */
1360 return spec;
1361
1362 if (optimize_specialization_lookup_p (tmpl))
1363 /* We don't put these specializations in the hash table, but we might
1364 want to give an error about a mismatch. */
1365 fn = retrieve_specialization (tmpl, args, 0);
1366 else
1367 {
1368 elt.tmpl = tmpl;
1369 elt.args = args;
1370 elt.spec = spec;
1371
1372 if (hash == 0)
1373 hash = hash_specialization (&elt);
1374
1375 slot =
1376 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1377 if (*slot)
1378 fn = ((spec_entry *) *slot)->spec;
1379 else
1380 fn = NULL_TREE;
1381 }
1382
1383 /* We can sometimes try to re-register a specialization that we've
1384 already got. In particular, regenerate_decl_from_template calls
1385 duplicate_decls which will update the specialization list. But,
1386 we'll still get called again here anyhow. It's more convenient
1387 to simply allow this than to try to prevent it. */
1388 if (fn == spec)
1389 return spec;
1390 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1391 {
1392 if (DECL_TEMPLATE_INSTANTIATION (fn))
1393 {
1394 if (DECL_ODR_USED (fn)
1395 || DECL_EXPLICIT_INSTANTIATION (fn))
1396 {
1397 error ("specialization of %qD after instantiation",
1398 fn);
1399 return error_mark_node;
1400 }
1401 else
1402 {
1403 tree clone;
1404 /* This situation should occur only if the first
1405 specialization is an implicit instantiation, the
1406 second is an explicit specialization, and the
1407 implicit instantiation has not yet been used. That
1408 situation can occur if we have implicitly
1409 instantiated a member function and then specialized
1410 it later.
1411
1412 We can also wind up here if a friend declaration that
1413 looked like an instantiation turns out to be a
1414 specialization:
1415
1416 template <class T> void foo(T);
1417 class S { friend void foo<>(int) };
1418 template <> void foo(int);
1419
1420 We transform the existing DECL in place so that any
1421 pointers to it become pointers to the updated
1422 declaration.
1423
1424 If there was a definition for the template, but not
1425 for the specialization, we want this to look as if
1426 there were no definition, and vice versa. */
1427 DECL_INITIAL (fn) = NULL_TREE;
1428 duplicate_decls (spec, fn, is_friend);
1429 /* The call to duplicate_decls will have applied
1430 [temp.expl.spec]:
1431
1432 An explicit specialization of a function template
1433 is inline only if it is explicitly declared to be,
1434 and independently of whether its function template
1435 is.
1436
1437 to the primary function; now copy the inline bits to
1438 the various clones. */
1439 FOR_EACH_CLONE (clone, fn)
1440 {
1441 DECL_DECLARED_INLINE_P (clone)
1442 = DECL_DECLARED_INLINE_P (fn);
1443 DECL_SOURCE_LOCATION (clone)
1444 = DECL_SOURCE_LOCATION (fn);
1445 DECL_DELETED_FN (clone)
1446 = DECL_DELETED_FN (fn);
1447 }
1448 check_specialization_namespace (tmpl);
1449
1450 return fn;
1451 }
1452 }
1453 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1454 {
1455 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1456 /* Dup decl failed, but this is a new definition. Set the
1457 line number so any errors match this new
1458 definition. */
1459 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1460
1461 return fn;
1462 }
1463 }
1464 else if (fn)
1465 return duplicate_decls (spec, fn, is_friend);
1466
1467 /* A specialization must be declared in the same namespace as the
1468 template it is specializing. */
1469 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1470 && !check_specialization_namespace (tmpl))
1471 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1472
1473 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1474 {
1475 spec_entry *entry = ggc_alloc_spec_entry ();
1476 gcc_assert (tmpl && args && spec);
1477 *entry = elt;
1478 *slot = entry;
1479 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1480 && PRIMARY_TEMPLATE_P (tmpl)
1481 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1482 /* TMPL is a forward declaration of a template function; keep a list
1483 of all specializations in case we need to reassign them to a friend
1484 template later in tsubst_friend_function. */
1485 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1486 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1487 }
1488
1489 return spec;
1490 }
1491
1492 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1493 TMPL and ARGS members, ignores SPEC. */
1494
1495 int comparing_specializations;
1496
1497 static int
1498 eq_specializations (const void *p1, const void *p2)
1499 {
1500 const spec_entry *e1 = (const spec_entry *)p1;
1501 const spec_entry *e2 = (const spec_entry *)p2;
1502 int equal;
1503
1504 ++comparing_specializations;
1505 equal = (e1->tmpl == e2->tmpl
1506 && comp_template_args (e1->args, e2->args));
1507 --comparing_specializations;
1508
1509 return equal;
1510 }
1511
1512 /* Returns a hash for a template TMPL and template arguments ARGS. */
1513
1514 static hashval_t
1515 hash_tmpl_and_args (tree tmpl, tree args)
1516 {
1517 hashval_t val = DECL_UID (tmpl);
1518 return iterative_hash_template_arg (args, val);
1519 }
1520
1521 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1522 ignoring SPEC. */
1523
1524 static hashval_t
1525 hash_specialization (const void *p)
1526 {
1527 const spec_entry *e = (const spec_entry *)p;
1528 return hash_tmpl_and_args (e->tmpl, e->args);
1529 }
1530
1531 /* Recursively calculate a hash value for a template argument ARG, for use
1532 in the hash tables of template specializations. */
1533
1534 hashval_t
1535 iterative_hash_template_arg (tree arg, hashval_t val)
1536 {
1537 unsigned HOST_WIDE_INT i;
1538 enum tree_code code;
1539 char tclass;
1540
1541 if (arg == NULL_TREE)
1542 return iterative_hash_object (arg, val);
1543
1544 if (!TYPE_P (arg))
1545 STRIP_NOPS (arg);
1546
1547 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1548 /* We can get one of these when re-hashing a previous entry in the middle
1549 of substituting into a pack expansion. Just look through it. */
1550 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1551
1552 code = TREE_CODE (arg);
1553 tclass = TREE_CODE_CLASS (code);
1554
1555 val = iterative_hash_object (code, val);
1556
1557 switch (code)
1558 {
1559 case ERROR_MARK:
1560 return val;
1561
1562 case IDENTIFIER_NODE:
1563 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1564
1565 case TREE_VEC:
1566 {
1567 int i, len = TREE_VEC_LENGTH (arg);
1568 for (i = 0; i < len; ++i)
1569 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1570 return val;
1571 }
1572
1573 case TYPE_PACK_EXPANSION:
1574 case EXPR_PACK_EXPANSION:
1575 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1576 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1577
1578 case TYPE_ARGUMENT_PACK:
1579 case NONTYPE_ARGUMENT_PACK:
1580 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1581
1582 case TREE_LIST:
1583 for (; arg; arg = TREE_CHAIN (arg))
1584 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1585 return val;
1586
1587 case OVERLOAD:
1588 for (; arg; arg = OVL_NEXT (arg))
1589 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1590 return val;
1591
1592 case CONSTRUCTOR:
1593 {
1594 tree field, value;
1595 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1596 {
1597 val = iterative_hash_template_arg (field, val);
1598 val = iterative_hash_template_arg (value, val);
1599 }
1600 return val;
1601 }
1602
1603 case PARM_DECL:
1604 if (!DECL_ARTIFICIAL (arg))
1605 {
1606 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1607 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1608 }
1609 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1610
1611 case TARGET_EXPR:
1612 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1613
1614 case PTRMEM_CST:
1615 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1616 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1617
1618 case TEMPLATE_PARM_INDEX:
1619 val = iterative_hash_template_arg
1620 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1621 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1622 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1623
1624 case TRAIT_EXPR:
1625 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1626 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1627 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1628
1629 case BASELINK:
1630 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1631 val);
1632 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1633 val);
1634
1635 case MODOP_EXPR:
1636 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1637 code = TREE_CODE (TREE_OPERAND (arg, 1));
1638 val = iterative_hash_object (code, val);
1639 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1640
1641 case LAMBDA_EXPR:
1642 /* A lambda can't appear in a template arg, but don't crash on
1643 erroneous input. */
1644 gcc_assert (seen_error ());
1645 return val;
1646
1647 case CAST_EXPR:
1648 case IMPLICIT_CONV_EXPR:
1649 case STATIC_CAST_EXPR:
1650 case REINTERPRET_CAST_EXPR:
1651 case CONST_CAST_EXPR:
1652 case DYNAMIC_CAST_EXPR:
1653 case NEW_EXPR:
1654 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1655 /* Now hash operands as usual. */
1656 break;
1657
1658 default:
1659 break;
1660 }
1661
1662 switch (tclass)
1663 {
1664 case tcc_type:
1665 if (TYPE_CANONICAL (arg))
1666 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1667 val);
1668 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1669 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1670 /* Otherwise just compare the types during lookup. */
1671 return val;
1672
1673 case tcc_declaration:
1674 case tcc_constant:
1675 return iterative_hash_expr (arg, val);
1676
1677 default:
1678 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1679 {
1680 unsigned n = cp_tree_operand_length (arg);
1681 for (i = 0; i < n; ++i)
1682 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1683 return val;
1684 }
1685 }
1686 gcc_unreachable ();
1687 return 0;
1688 }
1689
1690 /* Unregister the specialization SPEC as a specialization of TMPL.
1691 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1692 if the SPEC was listed as a specialization of TMPL.
1693
1694 Note that SPEC has been ggc_freed, so we can't look inside it. */
1695
1696 bool
1697 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1698 {
1699 spec_entry *entry;
1700 spec_entry elt;
1701
1702 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1703 elt.args = TI_ARGS (tinfo);
1704 elt.spec = NULL_TREE;
1705
1706 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1707 if (entry != NULL)
1708 {
1709 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1710 gcc_assert (new_spec != NULL_TREE);
1711 entry->spec = new_spec;
1712 return 1;
1713 }
1714
1715 return 0;
1716 }
1717
1718 /* Like register_specialization, but for local declarations. We are
1719 registering SPEC, an instantiation of TMPL. */
1720
1721 static void
1722 register_local_specialization (tree spec, tree tmpl)
1723 {
1724 void **slot;
1725
1726 slot = pointer_map_insert (local_specializations, tmpl);
1727 *slot = spec;
1728 }
1729
1730 /* TYPE is a class type. Returns true if TYPE is an explicitly
1731 specialized class. */
1732
1733 bool
1734 explicit_class_specialization_p (tree type)
1735 {
1736 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1737 return false;
1738 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1739 }
1740
1741 /* Print the list of functions at FNS, going through all the overloads
1742 for each element of the list. Alternatively, FNS can not be a
1743 TREE_LIST, in which case it will be printed together with all the
1744 overloads.
1745
1746 MORE and *STR should respectively be FALSE and NULL when the function
1747 is called from the outside. They are used internally on recursive
1748 calls. print_candidates manages the two parameters and leaves NULL
1749 in *STR when it ends. */
1750
1751 static void
1752 print_candidates_1 (tree fns, bool more, const char **str)
1753 {
1754 tree fn, fn2;
1755 char *spaces = NULL;
1756
1757 for (fn = fns; fn; fn = OVL_NEXT (fn))
1758 if (TREE_CODE (fn) == TREE_LIST)
1759 {
1760 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1761 print_candidates_1 (TREE_VALUE (fn2),
1762 TREE_CHAIN (fn2) || more, str);
1763 }
1764 else
1765 {
1766 tree cand = OVL_CURRENT (fn);
1767 if (!*str)
1768 {
1769 /* Pick the prefix string. */
1770 if (!more && !OVL_NEXT (fns))
1771 {
1772 inform (DECL_SOURCE_LOCATION (cand),
1773 "candidate is: %#D", cand);
1774 continue;
1775 }
1776
1777 *str = _("candidates are:");
1778 spaces = get_spaces (*str);
1779 }
1780 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1781 *str = spaces ? spaces : *str;
1782 }
1783
1784 if (!more)
1785 {
1786 free (spaces);
1787 *str = NULL;
1788 }
1789 }
1790
1791 /* Print the list of candidate FNS in an error message. FNS can also
1792 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1793
1794 void
1795 print_candidates (tree fns)
1796 {
1797 const char *str = NULL;
1798 print_candidates_1 (fns, false, &str);
1799 gcc_assert (str == NULL);
1800 }
1801
1802 /* Returns the template (one of the functions given by TEMPLATE_ID)
1803 which can be specialized to match the indicated DECL with the
1804 explicit template args given in TEMPLATE_ID. The DECL may be
1805 NULL_TREE if none is available. In that case, the functions in
1806 TEMPLATE_ID are non-members.
1807
1808 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1809 specialization of a member template.
1810
1811 The TEMPLATE_COUNT is the number of references to qualifying
1812 template classes that appeared in the name of the function. See
1813 check_explicit_specialization for a more accurate description.
1814
1815 TSK indicates what kind of template declaration (if any) is being
1816 declared. TSK_TEMPLATE indicates that the declaration given by
1817 DECL, though a FUNCTION_DECL, has template parameters, and is
1818 therefore a template function.
1819
1820 The template args (those explicitly specified and those deduced)
1821 are output in a newly created vector *TARGS_OUT.
1822
1823 If it is impossible to determine the result, an error message is
1824 issued. The error_mark_node is returned to indicate failure. */
1825
1826 static tree
1827 determine_specialization (tree template_id,
1828 tree decl,
1829 tree* targs_out,
1830 int need_member_template,
1831 int template_count,
1832 tmpl_spec_kind tsk)
1833 {
1834 tree fns;
1835 tree targs;
1836 tree explicit_targs;
1837 tree candidates = NULL_TREE;
1838 /* A TREE_LIST of templates of which DECL may be a specialization.
1839 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1840 corresponding TREE_PURPOSE is the set of template arguments that,
1841 when used to instantiate the template, would produce a function
1842 with the signature of DECL. */
1843 tree templates = NULL_TREE;
1844 int header_count;
1845 cp_binding_level *b;
1846
1847 *targs_out = NULL_TREE;
1848
1849 if (template_id == error_mark_node || decl == error_mark_node)
1850 return error_mark_node;
1851
1852 /* We shouldn't be specializing a member template of an
1853 unspecialized class template; we already gave an error in
1854 check_specialization_scope, now avoid crashing. */
1855 if (template_count && DECL_CLASS_SCOPE_P (decl)
1856 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1857 {
1858 gcc_assert (errorcount);
1859 return error_mark_node;
1860 }
1861
1862 fns = TREE_OPERAND (template_id, 0);
1863 explicit_targs = TREE_OPERAND (template_id, 1);
1864
1865 if (fns == error_mark_node)
1866 return error_mark_node;
1867
1868 /* Check for baselinks. */
1869 if (BASELINK_P (fns))
1870 fns = BASELINK_FUNCTIONS (fns);
1871
1872 if (!is_overloaded_fn (fns))
1873 {
1874 error ("%qD is not a function template", fns);
1875 return error_mark_node;
1876 }
1877
1878 /* Count the number of template headers specified for this
1879 specialization. */
1880 header_count = 0;
1881 for (b = current_binding_level;
1882 b->kind == sk_template_parms;
1883 b = b->level_chain)
1884 ++header_count;
1885
1886 for (; fns; fns = OVL_NEXT (fns))
1887 {
1888 tree fn = OVL_CURRENT (fns);
1889
1890 if (TREE_CODE (fn) == TEMPLATE_DECL)
1891 {
1892 tree decl_arg_types;
1893 tree fn_arg_types;
1894 tree insttype;
1895
1896 /* In case of explicit specialization, we need to check if
1897 the number of template headers appearing in the specialization
1898 is correct. This is usually done in check_explicit_specialization,
1899 but the check done there cannot be exhaustive when specializing
1900 member functions. Consider the following code:
1901
1902 template <> void A<int>::f(int);
1903 template <> template <> void A<int>::f(int);
1904
1905 Assuming that A<int> is not itself an explicit specialization
1906 already, the first line specializes "f" which is a non-template
1907 member function, whilst the second line specializes "f" which
1908 is a template member function. So both lines are syntactically
1909 correct, and check_explicit_specialization does not reject
1910 them.
1911
1912 Here, we can do better, as we are matching the specialization
1913 against the declarations. We count the number of template
1914 headers, and we check if they match TEMPLATE_COUNT + 1
1915 (TEMPLATE_COUNT is the number of qualifying template classes,
1916 plus there must be another header for the member template
1917 itself).
1918
1919 Notice that if header_count is zero, this is not a
1920 specialization but rather a template instantiation, so there
1921 is no check we can perform here. */
1922 if (header_count && header_count != template_count + 1)
1923 continue;
1924
1925 /* Check that the number of template arguments at the
1926 innermost level for DECL is the same as for FN. */
1927 if (current_binding_level->kind == sk_template_parms
1928 && !current_binding_level->explicit_spec_p
1929 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1930 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1931 (current_template_parms))))
1932 continue;
1933
1934 /* DECL might be a specialization of FN. */
1935 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1936 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1937
1938 /* For a non-static member function, we need to make sure
1939 that the const qualification is the same. Since
1940 get_bindings does not try to merge the "this" parameter,
1941 we must do the comparison explicitly. */
1942 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1943 && !same_type_p (TREE_VALUE (fn_arg_types),
1944 TREE_VALUE (decl_arg_types)))
1945 continue;
1946
1947 /* Skip the "this" parameter and, for constructors of
1948 classes with virtual bases, the VTT parameter. A
1949 full specialization of a constructor will have a VTT
1950 parameter, but a template never will. */
1951 decl_arg_types
1952 = skip_artificial_parms_for (decl, decl_arg_types);
1953 fn_arg_types
1954 = skip_artificial_parms_for (fn, fn_arg_types);
1955
1956 /* Function templates cannot be specializations; there are
1957 no partial specializations of functions. Therefore, if
1958 the type of DECL does not match FN, there is no
1959 match. */
1960 if (tsk == tsk_template)
1961 {
1962 if (compparms (fn_arg_types, decl_arg_types))
1963 candidates = tree_cons (NULL_TREE, fn, candidates);
1964 continue;
1965 }
1966
1967 /* See whether this function might be a specialization of this
1968 template. Suppress access control because we might be trying
1969 to make this specialization a friend, and we have already done
1970 access control for the declaration of the specialization. */
1971 push_deferring_access_checks (dk_no_check);
1972 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1973 pop_deferring_access_checks ();
1974
1975 if (!targs)
1976 /* We cannot deduce template arguments that when used to
1977 specialize TMPL will produce DECL. */
1978 continue;
1979
1980 /* Make sure that the deduced arguments actually work. */
1981 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1982 if (insttype == error_mark_node)
1983 continue;
1984 fn_arg_types
1985 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1986 if (!compparms (fn_arg_types, decl_arg_types))
1987 continue;
1988
1989 /* Save this template, and the arguments deduced. */
1990 templates = tree_cons (targs, fn, templates);
1991 }
1992 else if (need_member_template)
1993 /* FN is an ordinary member function, and we need a
1994 specialization of a member template. */
1995 ;
1996 else if (TREE_CODE (fn) != FUNCTION_DECL)
1997 /* We can get IDENTIFIER_NODEs here in certain erroneous
1998 cases. */
1999 ;
2000 else if (!DECL_FUNCTION_MEMBER_P (fn))
2001 /* This is just an ordinary non-member function. Nothing can
2002 be a specialization of that. */
2003 ;
2004 else if (DECL_ARTIFICIAL (fn))
2005 /* Cannot specialize functions that are created implicitly. */
2006 ;
2007 else
2008 {
2009 tree decl_arg_types;
2010
2011 /* This is an ordinary member function. However, since
2012 we're here, we can assume its enclosing class is a
2013 template class. For example,
2014
2015 template <typename T> struct S { void f(); };
2016 template <> void S<int>::f() {}
2017
2018 Here, S<int>::f is a non-template, but S<int> is a
2019 template class. If FN has the same type as DECL, we
2020 might be in business. */
2021
2022 if (!DECL_TEMPLATE_INFO (fn))
2023 /* Its enclosing class is an explicit specialization
2024 of a template class. This is not a candidate. */
2025 continue;
2026
2027 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2028 TREE_TYPE (TREE_TYPE (fn))))
2029 /* The return types differ. */
2030 continue;
2031
2032 /* Adjust the type of DECL in case FN is a static member. */
2033 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2034 if (DECL_STATIC_FUNCTION_P (fn)
2035 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2036 decl_arg_types = TREE_CHAIN (decl_arg_types);
2037
2038 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2039 decl_arg_types))
2040 /* They match! */
2041 candidates = tree_cons (NULL_TREE, fn, candidates);
2042 }
2043 }
2044
2045 if (templates && TREE_CHAIN (templates))
2046 {
2047 /* We have:
2048
2049 [temp.expl.spec]
2050
2051 It is possible for a specialization with a given function
2052 signature to be instantiated from more than one function
2053 template. In such cases, explicit specification of the
2054 template arguments must be used to uniquely identify the
2055 function template specialization being specialized.
2056
2057 Note that here, there's no suggestion that we're supposed to
2058 determine which of the candidate templates is most
2059 specialized. However, we, also have:
2060
2061 [temp.func.order]
2062
2063 Partial ordering of overloaded function template
2064 declarations is used in the following contexts to select
2065 the function template to which a function template
2066 specialization refers:
2067
2068 -- when an explicit specialization refers to a function
2069 template.
2070
2071 So, we do use the partial ordering rules, at least for now.
2072 This extension can only serve to make invalid programs valid,
2073 so it's safe. And, there is strong anecdotal evidence that
2074 the committee intended the partial ordering rules to apply;
2075 the EDG front end has that behavior, and John Spicer claims
2076 that the committee simply forgot to delete the wording in
2077 [temp.expl.spec]. */
2078 tree tmpl = most_specialized_instantiation (templates);
2079 if (tmpl != error_mark_node)
2080 {
2081 templates = tmpl;
2082 TREE_CHAIN (templates) = NULL_TREE;
2083 }
2084 }
2085
2086 if (templates == NULL_TREE && candidates == NULL_TREE)
2087 {
2088 error ("template-id %qD for %q+D does not match any template "
2089 "declaration", template_id, decl);
2090 if (header_count && header_count != template_count + 1)
2091 inform (input_location, "saw %d %<template<>%>, need %d for "
2092 "specializing a member function template",
2093 header_count, template_count + 1);
2094 return error_mark_node;
2095 }
2096 else if ((templates && TREE_CHAIN (templates))
2097 || (candidates && TREE_CHAIN (candidates))
2098 || (templates && candidates))
2099 {
2100 error ("ambiguous template specialization %qD for %q+D",
2101 template_id, decl);
2102 candidates = chainon (candidates, templates);
2103 print_candidates (candidates);
2104 return error_mark_node;
2105 }
2106
2107 /* We have one, and exactly one, match. */
2108 if (candidates)
2109 {
2110 tree fn = TREE_VALUE (candidates);
2111 *targs_out = copy_node (DECL_TI_ARGS (fn));
2112 /* DECL is a re-declaration or partial instantiation of a template
2113 function. */
2114 if (TREE_CODE (fn) == TEMPLATE_DECL)
2115 return fn;
2116 /* It was a specialization of an ordinary member function in a
2117 template class. */
2118 return DECL_TI_TEMPLATE (fn);
2119 }
2120
2121 /* It was a specialization of a template. */
2122 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2123 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2124 {
2125 *targs_out = copy_node (targs);
2126 SET_TMPL_ARGS_LEVEL (*targs_out,
2127 TMPL_ARGS_DEPTH (*targs_out),
2128 TREE_PURPOSE (templates));
2129 }
2130 else
2131 *targs_out = TREE_PURPOSE (templates);
2132 return TREE_VALUE (templates);
2133 }
2134
2135 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2136 but with the default argument values filled in from those in the
2137 TMPL_TYPES. */
2138
2139 static tree
2140 copy_default_args_to_explicit_spec_1 (tree spec_types,
2141 tree tmpl_types)
2142 {
2143 tree new_spec_types;
2144
2145 if (!spec_types)
2146 return NULL_TREE;
2147
2148 if (spec_types == void_list_node)
2149 return void_list_node;
2150
2151 /* Substitute into the rest of the list. */
2152 new_spec_types =
2153 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2154 TREE_CHAIN (tmpl_types));
2155
2156 /* Add the default argument for this parameter. */
2157 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2158 TREE_VALUE (spec_types),
2159 new_spec_types);
2160 }
2161
2162 /* DECL is an explicit specialization. Replicate default arguments
2163 from the template it specializes. (That way, code like:
2164
2165 template <class T> void f(T = 3);
2166 template <> void f(double);
2167 void g () { f (); }
2168
2169 works, as required.) An alternative approach would be to look up
2170 the correct default arguments at the call-site, but this approach
2171 is consistent with how implicit instantiations are handled. */
2172
2173 static void
2174 copy_default_args_to_explicit_spec (tree decl)
2175 {
2176 tree tmpl;
2177 tree spec_types;
2178 tree tmpl_types;
2179 tree new_spec_types;
2180 tree old_type;
2181 tree new_type;
2182 tree t;
2183 tree object_type = NULL_TREE;
2184 tree in_charge = NULL_TREE;
2185 tree vtt = NULL_TREE;
2186
2187 /* See if there's anything we need to do. */
2188 tmpl = DECL_TI_TEMPLATE (decl);
2189 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2190 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2191 if (TREE_PURPOSE (t))
2192 break;
2193 if (!t)
2194 return;
2195
2196 old_type = TREE_TYPE (decl);
2197 spec_types = TYPE_ARG_TYPES (old_type);
2198
2199 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2200 {
2201 /* Remove the this pointer, but remember the object's type for
2202 CV quals. */
2203 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2204 spec_types = TREE_CHAIN (spec_types);
2205 tmpl_types = TREE_CHAIN (tmpl_types);
2206
2207 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2208 {
2209 /* DECL may contain more parameters than TMPL due to the extra
2210 in-charge parameter in constructors and destructors. */
2211 in_charge = spec_types;
2212 spec_types = TREE_CHAIN (spec_types);
2213 }
2214 if (DECL_HAS_VTT_PARM_P (decl))
2215 {
2216 vtt = spec_types;
2217 spec_types = TREE_CHAIN (spec_types);
2218 }
2219 }
2220
2221 /* Compute the merged default arguments. */
2222 new_spec_types =
2223 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2224
2225 /* Compute the new FUNCTION_TYPE. */
2226 if (object_type)
2227 {
2228 if (vtt)
2229 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2230 TREE_VALUE (vtt),
2231 new_spec_types);
2232
2233 if (in_charge)
2234 /* Put the in-charge parameter back. */
2235 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2236 TREE_VALUE (in_charge),
2237 new_spec_types);
2238
2239 new_type = build_method_type_directly (object_type,
2240 TREE_TYPE (old_type),
2241 new_spec_types);
2242 }
2243 else
2244 new_type = build_function_type (TREE_TYPE (old_type),
2245 new_spec_types);
2246 new_type = cp_build_type_attribute_variant (new_type,
2247 TYPE_ATTRIBUTES (old_type));
2248 new_type = build_exception_variant (new_type,
2249 TYPE_RAISES_EXCEPTIONS (old_type));
2250 TREE_TYPE (decl) = new_type;
2251 }
2252
2253 /* Return the number of template headers we expect to see for a definition
2254 or specialization of CTYPE or one of its non-template members. */
2255
2256 int
2257 num_template_headers_for_class (tree ctype)
2258 {
2259 int num_templates = 0;
2260
2261 while (ctype && CLASS_TYPE_P (ctype))
2262 {
2263 /* You're supposed to have one `template <...>' for every
2264 template class, but you don't need one for a full
2265 specialization. For example:
2266
2267 template <class T> struct S{};
2268 template <> struct S<int> { void f(); };
2269 void S<int>::f () {}
2270
2271 is correct; there shouldn't be a `template <>' for the
2272 definition of `S<int>::f'. */
2273 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2274 /* If CTYPE does not have template information of any
2275 kind, then it is not a template, nor is it nested
2276 within a template. */
2277 break;
2278 if (explicit_class_specialization_p (ctype))
2279 break;
2280 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2281 ++num_templates;
2282
2283 ctype = TYPE_CONTEXT (ctype);
2284 }
2285
2286 return num_templates;
2287 }
2288
2289 /* Do a simple sanity check on the template headers that precede the
2290 variable declaration DECL. */
2291
2292 void
2293 check_template_variable (tree decl)
2294 {
2295 tree ctx = CP_DECL_CONTEXT (decl);
2296 int wanted = num_template_headers_for_class (ctx);
2297 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2298 permerror (DECL_SOURCE_LOCATION (decl),
2299 "%qD is not a static data member of a class template", decl);
2300 else if (template_header_count > wanted)
2301 {
2302 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2303 "too many template headers for %D (should be %d)",
2304 decl, wanted);
2305 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2306 inform (DECL_SOURCE_LOCATION (decl),
2307 "members of an explicitly specialized class are defined "
2308 "without a template header");
2309 }
2310 }
2311
2312 /* Check to see if the function just declared, as indicated in
2313 DECLARATOR, and in DECL, is a specialization of a function
2314 template. We may also discover that the declaration is an explicit
2315 instantiation at this point.
2316
2317 Returns DECL, or an equivalent declaration that should be used
2318 instead if all goes well. Issues an error message if something is
2319 amiss. Returns error_mark_node if the error is not easily
2320 recoverable.
2321
2322 FLAGS is a bitmask consisting of the following flags:
2323
2324 2: The function has a definition.
2325 4: The function is a friend.
2326
2327 The TEMPLATE_COUNT is the number of references to qualifying
2328 template classes that appeared in the name of the function. For
2329 example, in
2330
2331 template <class T> struct S { void f(); };
2332 void S<int>::f();
2333
2334 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2335 classes are not counted in the TEMPLATE_COUNT, so that in
2336
2337 template <class T> struct S {};
2338 template <> struct S<int> { void f(); }
2339 template <> void S<int>::f();
2340
2341 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2342 invalid; there should be no template <>.)
2343
2344 If the function is a specialization, it is marked as such via
2345 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2346 is set up correctly, and it is added to the list of specializations
2347 for that template. */
2348
2349 tree
2350 check_explicit_specialization (tree declarator,
2351 tree decl,
2352 int template_count,
2353 int flags)
2354 {
2355 int have_def = flags & 2;
2356 int is_friend = flags & 4;
2357 int specialization = 0;
2358 int explicit_instantiation = 0;
2359 int member_specialization = 0;
2360 tree ctype = DECL_CLASS_CONTEXT (decl);
2361 tree dname = DECL_NAME (decl);
2362 tmpl_spec_kind tsk;
2363
2364 if (is_friend)
2365 {
2366 if (!processing_specialization)
2367 tsk = tsk_none;
2368 else
2369 tsk = tsk_excessive_parms;
2370 }
2371 else
2372 tsk = current_tmpl_spec_kind (template_count);
2373
2374 switch (tsk)
2375 {
2376 case tsk_none:
2377 if (processing_specialization)
2378 {
2379 specialization = 1;
2380 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2381 }
2382 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2383 {
2384 if (is_friend)
2385 /* This could be something like:
2386
2387 template <class T> void f(T);
2388 class S { friend void f<>(int); } */
2389 specialization = 1;
2390 else
2391 {
2392 /* This case handles bogus declarations like template <>
2393 template <class T> void f<int>(); */
2394
2395 error ("template-id %qD in declaration of primary template",
2396 declarator);
2397 return decl;
2398 }
2399 }
2400 break;
2401
2402 case tsk_invalid_member_spec:
2403 /* The error has already been reported in
2404 check_specialization_scope. */
2405 return error_mark_node;
2406
2407 case tsk_invalid_expl_inst:
2408 error ("template parameter list used in explicit instantiation");
2409
2410 /* Fall through. */
2411
2412 case tsk_expl_inst:
2413 if (have_def)
2414 error ("definition provided for explicit instantiation");
2415
2416 explicit_instantiation = 1;
2417 break;
2418
2419 case tsk_excessive_parms:
2420 case tsk_insufficient_parms:
2421 if (tsk == tsk_excessive_parms)
2422 error ("too many template parameter lists in declaration of %qD",
2423 decl);
2424 else if (template_header_count)
2425 error("too few template parameter lists in declaration of %qD", decl);
2426 else
2427 error("explicit specialization of %qD must be introduced by "
2428 "%<template <>%>", decl);
2429
2430 /* Fall through. */
2431 case tsk_expl_spec:
2432 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2433 if (ctype)
2434 member_specialization = 1;
2435 else
2436 specialization = 1;
2437 break;
2438
2439 case tsk_template:
2440 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2441 {
2442 /* This case handles bogus declarations like template <>
2443 template <class T> void f<int>(); */
2444
2445 if (uses_template_parms (declarator))
2446 error ("function template partial specialization %qD "
2447 "is not allowed", declarator);
2448 else
2449 error ("template-id %qD in declaration of primary template",
2450 declarator);
2451 return decl;
2452 }
2453
2454 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2455 /* This is a specialization of a member template, without
2456 specialization the containing class. Something like:
2457
2458 template <class T> struct S {
2459 template <class U> void f (U);
2460 };
2461 template <> template <class U> void S<int>::f(U) {}
2462
2463 That's a specialization -- but of the entire template. */
2464 specialization = 1;
2465 break;
2466
2467 default:
2468 gcc_unreachable ();
2469 }
2470
2471 if (specialization || member_specialization)
2472 {
2473 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2474 for (; t; t = TREE_CHAIN (t))
2475 if (TREE_PURPOSE (t))
2476 {
2477 permerror (input_location,
2478 "default argument specified in explicit specialization");
2479 break;
2480 }
2481 }
2482
2483 if (specialization || member_specialization || explicit_instantiation)
2484 {
2485 tree tmpl = NULL_TREE;
2486 tree targs = NULL_TREE;
2487
2488 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2489 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2490 {
2491 tree fns;
2492
2493 gcc_assert (identifier_p (declarator));
2494 if (ctype)
2495 fns = dname;
2496 else
2497 {
2498 /* If there is no class context, the explicit instantiation
2499 must be at namespace scope. */
2500 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2501
2502 /* Find the namespace binding, using the declaration
2503 context. */
2504 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2505 false, true);
2506 if (fns == error_mark_node || !is_overloaded_fn (fns))
2507 {
2508 error ("%qD is not a template function", dname);
2509 fns = error_mark_node;
2510 }
2511 else
2512 {
2513 tree fn = OVL_CURRENT (fns);
2514 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2515 CP_DECL_CONTEXT (fn)))
2516 error ("%qD is not declared in %qD",
2517 decl, current_namespace);
2518 }
2519 }
2520
2521 declarator = lookup_template_function (fns, NULL_TREE);
2522 }
2523
2524 if (declarator == error_mark_node)
2525 return error_mark_node;
2526
2527 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2528 {
2529 if (!explicit_instantiation)
2530 /* A specialization in class scope. This is invalid,
2531 but the error will already have been flagged by
2532 check_specialization_scope. */
2533 return error_mark_node;
2534 else
2535 {
2536 /* It's not valid to write an explicit instantiation in
2537 class scope, e.g.:
2538
2539 class C { template void f(); }
2540
2541 This case is caught by the parser. However, on
2542 something like:
2543
2544 template class C { void f(); };
2545
2546 (which is invalid) we can get here. The error will be
2547 issued later. */
2548 ;
2549 }
2550
2551 return decl;
2552 }
2553 else if (ctype != NULL_TREE
2554 && (identifier_p (TREE_OPERAND (declarator, 0))))
2555 {
2556 /* Find the list of functions in ctype that have the same
2557 name as the declared function. */
2558 tree name = TREE_OPERAND (declarator, 0);
2559 tree fns = NULL_TREE;
2560 int idx;
2561
2562 if (constructor_name_p (name, ctype))
2563 {
2564 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2565
2566 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2567 : !CLASSTYPE_DESTRUCTORS (ctype))
2568 {
2569 /* From [temp.expl.spec]:
2570
2571 If such an explicit specialization for the member
2572 of a class template names an implicitly-declared
2573 special member function (clause _special_), the
2574 program is ill-formed.
2575
2576 Similar language is found in [temp.explicit]. */
2577 error ("specialization of implicitly-declared special member function");
2578 return error_mark_node;
2579 }
2580
2581 name = is_constructor ? ctor_identifier : dtor_identifier;
2582 }
2583
2584 if (!DECL_CONV_FN_P (decl))
2585 {
2586 idx = lookup_fnfields_1 (ctype, name);
2587 if (idx >= 0)
2588 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2589 }
2590 else
2591 {
2592 vec<tree, va_gc> *methods;
2593 tree ovl;
2594
2595 /* For a type-conversion operator, we cannot do a
2596 name-based lookup. We might be looking for `operator
2597 int' which will be a specialization of `operator T'.
2598 So, we find *all* the conversion operators, and then
2599 select from them. */
2600 fns = NULL_TREE;
2601
2602 methods = CLASSTYPE_METHOD_VEC (ctype);
2603 if (methods)
2604 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2605 methods->iterate (idx, &ovl);
2606 ++idx)
2607 {
2608 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2609 /* There are no more conversion functions. */
2610 break;
2611
2612 /* Glue all these conversion functions together
2613 with those we already have. */
2614 for (; ovl; ovl = OVL_NEXT (ovl))
2615 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2616 }
2617 }
2618
2619 if (fns == NULL_TREE)
2620 {
2621 error ("no member function %qD declared in %qT", name, ctype);
2622 return error_mark_node;
2623 }
2624 else
2625 TREE_OPERAND (declarator, 0) = fns;
2626 }
2627
2628 /* Figure out what exactly is being specialized at this point.
2629 Note that for an explicit instantiation, even one for a
2630 member function, we cannot tell apriori whether the
2631 instantiation is for a member template, or just a member
2632 function of a template class. Even if a member template is
2633 being instantiated, the member template arguments may be
2634 elided if they can be deduced from the rest of the
2635 declaration. */
2636 tmpl = determine_specialization (declarator, decl,
2637 &targs,
2638 member_specialization,
2639 template_count,
2640 tsk);
2641
2642 if (!tmpl || tmpl == error_mark_node)
2643 /* We couldn't figure out what this declaration was
2644 specializing. */
2645 return error_mark_node;
2646 else
2647 {
2648 tree gen_tmpl = most_general_template (tmpl);
2649
2650 if (explicit_instantiation)
2651 {
2652 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2653 is done by do_decl_instantiation later. */
2654
2655 int arg_depth = TMPL_ARGS_DEPTH (targs);
2656 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2657
2658 if (arg_depth > parm_depth)
2659 {
2660 /* If TMPL is not the most general template (for
2661 example, if TMPL is a friend template that is
2662 injected into namespace scope), then there will
2663 be too many levels of TARGS. Remove some of them
2664 here. */
2665 int i;
2666 tree new_targs;
2667
2668 new_targs = make_tree_vec (parm_depth);
2669 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2670 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2671 = TREE_VEC_ELT (targs, i);
2672 targs = new_targs;
2673 }
2674
2675 return instantiate_template (tmpl, targs, tf_error);
2676 }
2677
2678 /* If we thought that the DECL was a member function, but it
2679 turns out to be specializing a static member function,
2680 make DECL a static member function as well. */
2681 if (DECL_STATIC_FUNCTION_P (tmpl)
2682 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2683 revert_static_member_fn (decl);
2684
2685 /* If this is a specialization of a member template of a
2686 template class, we want to return the TEMPLATE_DECL, not
2687 the specialization of it. */
2688 if (tsk == tsk_template)
2689 {
2690 tree result = DECL_TEMPLATE_RESULT (tmpl);
2691 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2692 DECL_INITIAL (result) = NULL_TREE;
2693 if (have_def)
2694 {
2695 tree parm;
2696 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2697 DECL_SOURCE_LOCATION (result)
2698 = DECL_SOURCE_LOCATION (decl);
2699 /* We want to use the argument list specified in the
2700 definition, not in the original declaration. */
2701 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2702 for (parm = DECL_ARGUMENTS (result); parm;
2703 parm = DECL_CHAIN (parm))
2704 DECL_CONTEXT (parm) = result;
2705 }
2706 return register_specialization (tmpl, gen_tmpl, targs,
2707 is_friend, 0);
2708 }
2709
2710 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2711 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2712
2713 /* Inherit default function arguments from the template
2714 DECL is specializing. */
2715 copy_default_args_to_explicit_spec (decl);
2716
2717 /* This specialization has the same protection as the
2718 template it specializes. */
2719 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2720 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2721
2722 /* 7.1.1-1 [dcl.stc]
2723
2724 A storage-class-specifier shall not be specified in an
2725 explicit specialization...
2726
2727 The parser rejects these, so unless action is taken here,
2728 explicit function specializations will always appear with
2729 global linkage.
2730
2731 The action recommended by the C++ CWG in response to C++
2732 defect report 605 is to make the storage class and linkage
2733 of the explicit specialization match the templated function:
2734
2735 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2736 */
2737 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2738 {
2739 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2740 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2741
2742 /* This specialization has the same linkage and visibility as
2743 the function template it specializes. */
2744 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2745 if (! TREE_PUBLIC (decl))
2746 {
2747 DECL_INTERFACE_KNOWN (decl) = 1;
2748 DECL_NOT_REALLY_EXTERN (decl) = 1;
2749 }
2750 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2751 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2752 {
2753 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2754 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2755 }
2756 }
2757
2758 /* If DECL is a friend declaration, declared using an
2759 unqualified name, the namespace associated with DECL may
2760 have been set incorrectly. For example, in:
2761
2762 template <typename T> void f(T);
2763 namespace N {
2764 struct S { friend void f<int>(int); }
2765 }
2766
2767 we will have set the DECL_CONTEXT for the friend
2768 declaration to N, rather than to the global namespace. */
2769 if (DECL_NAMESPACE_SCOPE_P (decl))
2770 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2771
2772 if (is_friend && !have_def)
2773 /* This is not really a declaration of a specialization.
2774 It's just the name of an instantiation. But, it's not
2775 a request for an instantiation, either. */
2776 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2777
2778 /* Register this specialization so that we can find it
2779 again. */
2780 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2781
2782 /* A 'structor should already have clones. */
2783 gcc_assert (decl == error_mark_node
2784 || !(DECL_CONSTRUCTOR_P (decl)
2785 || DECL_DESTRUCTOR_P (decl))
2786 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
2787 }
2788 }
2789
2790 return decl;
2791 }
2792
2793 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2794 parameters. These are represented in the same format used for
2795 DECL_TEMPLATE_PARMS. */
2796
2797 int
2798 comp_template_parms (const_tree parms1, const_tree parms2)
2799 {
2800 const_tree p1;
2801 const_tree p2;
2802
2803 if (parms1 == parms2)
2804 return 1;
2805
2806 for (p1 = parms1, p2 = parms2;
2807 p1 != NULL_TREE && p2 != NULL_TREE;
2808 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2809 {
2810 tree t1 = TREE_VALUE (p1);
2811 tree t2 = TREE_VALUE (p2);
2812 int i;
2813
2814 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2815 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2816
2817 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2818 return 0;
2819
2820 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2821 {
2822 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2823 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2824
2825 /* If either of the template parameters are invalid, assume
2826 they match for the sake of error recovery. */
2827 if (parm1 == error_mark_node || parm2 == error_mark_node)
2828 return 1;
2829
2830 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2831 return 0;
2832
2833 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2834 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2835 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2836 continue;
2837 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2838 return 0;
2839 }
2840 }
2841
2842 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2843 /* One set of parameters has more parameters lists than the
2844 other. */
2845 return 0;
2846
2847 return 1;
2848 }
2849
2850 /* Determine whether PARM is a parameter pack. */
2851
2852 bool
2853 template_parameter_pack_p (const_tree parm)
2854 {
2855 /* Determine if we have a non-type template parameter pack. */
2856 if (TREE_CODE (parm) == PARM_DECL)
2857 return (DECL_TEMPLATE_PARM_P (parm)
2858 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2859 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2860 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2861
2862 /* If this is a list of template parameters, we could get a
2863 TYPE_DECL or a TEMPLATE_DECL. */
2864 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2865 parm = TREE_TYPE (parm);
2866
2867 /* Otherwise it must be a type template parameter. */
2868 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2869 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2870 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2871 }
2872
2873 /* Determine if T is a function parameter pack. */
2874
2875 bool
2876 function_parameter_pack_p (const_tree t)
2877 {
2878 if (t && TREE_CODE (t) == PARM_DECL)
2879 return DECL_PACK_P (t);
2880 return false;
2881 }
2882
2883 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2884 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2885
2886 tree
2887 get_function_template_decl (const_tree primary_func_tmpl_inst)
2888 {
2889 if (! primary_func_tmpl_inst
2890 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2891 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2892 return NULL;
2893
2894 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2895 }
2896
2897 /* Return true iff the function parameter PARAM_DECL was expanded
2898 from the function parameter pack PACK. */
2899
2900 bool
2901 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2902 {
2903 if (DECL_ARTIFICIAL (param_decl)
2904 || !function_parameter_pack_p (pack))
2905 return false;
2906
2907 /* The parameter pack and its pack arguments have the same
2908 DECL_PARM_INDEX. */
2909 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2910 }
2911
2912 /* Determine whether ARGS describes a variadic template args list,
2913 i.e., one that is terminated by a template argument pack. */
2914
2915 static bool
2916 template_args_variadic_p (tree args)
2917 {
2918 int nargs;
2919 tree last_parm;
2920
2921 if (args == NULL_TREE)
2922 return false;
2923
2924 args = INNERMOST_TEMPLATE_ARGS (args);
2925 nargs = TREE_VEC_LENGTH (args);
2926
2927 if (nargs == 0)
2928 return false;
2929
2930 last_parm = TREE_VEC_ELT (args, nargs - 1);
2931
2932 return ARGUMENT_PACK_P (last_parm);
2933 }
2934
2935 /* Generate a new name for the parameter pack name NAME (an
2936 IDENTIFIER_NODE) that incorporates its */
2937
2938 static tree
2939 make_ith_pack_parameter_name (tree name, int i)
2940 {
2941 /* Munge the name to include the parameter index. */
2942 #define NUMBUF_LEN 128
2943 char numbuf[NUMBUF_LEN];
2944 char* newname;
2945 int newname_len;
2946
2947 if (name == NULL_TREE)
2948 return name;
2949 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2950 newname_len = IDENTIFIER_LENGTH (name)
2951 + strlen (numbuf) + 2;
2952 newname = (char*)alloca (newname_len);
2953 snprintf (newname, newname_len,
2954 "%s#%i", IDENTIFIER_POINTER (name), i);
2955 return get_identifier (newname);
2956 }
2957
2958 /* Return true if T is a primary function, class or alias template
2959 instantiation. */
2960
2961 bool
2962 primary_template_instantiation_p (const_tree t)
2963 {
2964 if (!t)
2965 return false;
2966
2967 if (TREE_CODE (t) == FUNCTION_DECL)
2968 return DECL_LANG_SPECIFIC (t)
2969 && DECL_TEMPLATE_INSTANTIATION (t)
2970 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2971 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2972 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2973 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2974 else if (alias_template_specialization_p (t))
2975 return true;
2976 return false;
2977 }
2978
2979 /* Return true if PARM is a template template parameter. */
2980
2981 bool
2982 template_template_parameter_p (const_tree parm)
2983 {
2984 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2985 }
2986
2987 /* Return true iff PARM is a DECL representing a type template
2988 parameter. */
2989
2990 bool
2991 template_type_parameter_p (const_tree parm)
2992 {
2993 return (parm
2994 && (TREE_CODE (parm) == TYPE_DECL
2995 || TREE_CODE (parm) == TEMPLATE_DECL)
2996 && DECL_TEMPLATE_PARM_P (parm));
2997 }
2998
2999 /* Return the template parameters of T if T is a
3000 primary template instantiation, NULL otherwise. */
3001
3002 tree
3003 get_primary_template_innermost_parameters (const_tree t)
3004 {
3005 tree parms = NULL, template_info = NULL;
3006
3007 if ((template_info = get_template_info (t))
3008 && primary_template_instantiation_p (t))
3009 parms = INNERMOST_TEMPLATE_PARMS
3010 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3011
3012 return parms;
3013 }
3014
3015 /* Return the template parameters of the LEVELth level from the full list
3016 of template parameters PARMS. */
3017
3018 tree
3019 get_template_parms_at_level (tree parms, int level)
3020 {
3021 tree p;
3022 if (!parms
3023 || TREE_CODE (parms) != TREE_LIST
3024 || level > TMPL_PARMS_DEPTH (parms))
3025 return NULL_TREE;
3026
3027 for (p = parms; p; p = TREE_CHAIN (p))
3028 if (TMPL_PARMS_DEPTH (p) == level)
3029 return p;
3030
3031 return NULL_TREE;
3032 }
3033
3034 /* Returns the template arguments of T if T is a template instantiation,
3035 NULL otherwise. */
3036
3037 tree
3038 get_template_innermost_arguments (const_tree t)
3039 {
3040 tree args = NULL, template_info = NULL;
3041
3042 if ((template_info = get_template_info (t))
3043 && TI_ARGS (template_info))
3044 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3045
3046 return args;
3047 }
3048
3049 /* Return the argument pack elements of T if T is a template argument pack,
3050 NULL otherwise. */
3051
3052 tree
3053 get_template_argument_pack_elems (const_tree t)
3054 {
3055 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3056 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3057 return NULL;
3058
3059 return ARGUMENT_PACK_ARGS (t);
3060 }
3061
3062 /* Structure used to track the progress of find_parameter_packs_r. */
3063 struct find_parameter_pack_data
3064 {
3065 /* TREE_LIST that will contain all of the parameter packs found by
3066 the traversal. */
3067 tree* parameter_packs;
3068
3069 /* Set of AST nodes that have been visited by the traversal. */
3070 struct pointer_set_t *visited;
3071 };
3072
3073 /* Identifies all of the argument packs that occur in a template
3074 argument and appends them to the TREE_LIST inside DATA, which is a
3075 find_parameter_pack_data structure. This is a subroutine of
3076 make_pack_expansion and uses_parameter_packs. */
3077 static tree
3078 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3079 {
3080 tree t = *tp;
3081 struct find_parameter_pack_data* ppd =
3082 (struct find_parameter_pack_data*)data;
3083 bool parameter_pack_p = false;
3084
3085 /* Handle type aliases/typedefs. */
3086 if (TYPE_ALIAS_P (t))
3087 {
3088 if (TYPE_TEMPLATE_INFO (t))
3089 cp_walk_tree (&TYPE_TI_ARGS (t),
3090 &find_parameter_packs_r,
3091 ppd, ppd->visited);
3092 *walk_subtrees = 0;
3093 return NULL_TREE;
3094 }
3095
3096 /* Identify whether this is a parameter pack or not. */
3097 switch (TREE_CODE (t))
3098 {
3099 case TEMPLATE_PARM_INDEX:
3100 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3101 parameter_pack_p = true;
3102 break;
3103
3104 case TEMPLATE_TYPE_PARM:
3105 t = TYPE_MAIN_VARIANT (t);
3106 case TEMPLATE_TEMPLATE_PARM:
3107 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3108 parameter_pack_p = true;
3109 break;
3110
3111 case FIELD_DECL:
3112 case PARM_DECL:
3113 if (DECL_PACK_P (t))
3114 {
3115 /* We don't want to walk into the type of a PARM_DECL,
3116 because we don't want to see the type parameter pack. */
3117 *walk_subtrees = 0;
3118 parameter_pack_p = true;
3119 }
3120 break;
3121
3122 /* Look through a lambda capture proxy to the field pack. */
3123 case VAR_DECL:
3124 if (DECL_HAS_VALUE_EXPR_P (t))
3125 {
3126 tree v = DECL_VALUE_EXPR (t);
3127 cp_walk_tree (&v,
3128 &find_parameter_packs_r,
3129 ppd, ppd->visited);
3130 *walk_subtrees = 0;
3131 }
3132 break;
3133
3134 case BASES:
3135 parameter_pack_p = true;
3136 break;
3137 default:
3138 /* Not a parameter pack. */
3139 break;
3140 }
3141
3142 if (parameter_pack_p)
3143 {
3144 /* Add this parameter pack to the list. */
3145 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3146 }
3147
3148 if (TYPE_P (t))
3149 cp_walk_tree (&TYPE_CONTEXT (t),
3150 &find_parameter_packs_r, ppd, ppd->visited);
3151
3152 /* This switch statement will return immediately if we don't find a
3153 parameter pack. */
3154 switch (TREE_CODE (t))
3155 {
3156 case TEMPLATE_PARM_INDEX:
3157 return NULL_TREE;
3158
3159 case BOUND_TEMPLATE_TEMPLATE_PARM:
3160 /* Check the template itself. */
3161 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3162 &find_parameter_packs_r, ppd, ppd->visited);
3163 /* Check the template arguments. */
3164 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3165 ppd->visited);
3166 *walk_subtrees = 0;
3167 return NULL_TREE;
3168
3169 case TEMPLATE_TYPE_PARM:
3170 case TEMPLATE_TEMPLATE_PARM:
3171 return NULL_TREE;
3172
3173 case PARM_DECL:
3174 return NULL_TREE;
3175
3176 case RECORD_TYPE:
3177 if (TYPE_PTRMEMFUNC_P (t))
3178 return NULL_TREE;
3179 /* Fall through. */
3180
3181 case UNION_TYPE:
3182 case ENUMERAL_TYPE:
3183 if (TYPE_TEMPLATE_INFO (t))
3184 cp_walk_tree (&TYPE_TI_ARGS (t),
3185 &find_parameter_packs_r, ppd, ppd->visited);
3186
3187 *walk_subtrees = 0;
3188 return NULL_TREE;
3189
3190 case CONSTRUCTOR:
3191 case TEMPLATE_DECL:
3192 cp_walk_tree (&TREE_TYPE (t),
3193 &find_parameter_packs_r, ppd, ppd->visited);
3194 return NULL_TREE;
3195
3196 case TYPENAME_TYPE:
3197 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3198 ppd, ppd->visited);
3199 *walk_subtrees = 0;
3200 return NULL_TREE;
3201
3202 case TYPE_PACK_EXPANSION:
3203 case EXPR_PACK_EXPANSION:
3204 *walk_subtrees = 0;
3205 return NULL_TREE;
3206
3207 case INTEGER_TYPE:
3208 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3209 ppd, ppd->visited);
3210 *walk_subtrees = 0;
3211 return NULL_TREE;
3212
3213 case IDENTIFIER_NODE:
3214 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3215 ppd->visited);
3216 *walk_subtrees = 0;
3217 return NULL_TREE;
3218
3219 default:
3220 return NULL_TREE;
3221 }
3222
3223 return NULL_TREE;
3224 }
3225
3226 /* Determines if the expression or type T uses any parameter packs. */
3227 bool
3228 uses_parameter_packs (tree t)
3229 {
3230 tree parameter_packs = NULL_TREE;
3231 struct find_parameter_pack_data ppd;
3232 ppd.parameter_packs = &parameter_packs;
3233 ppd.visited = pointer_set_create ();
3234 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3235 pointer_set_destroy (ppd.visited);
3236 return parameter_packs != NULL_TREE;
3237 }
3238
3239 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3240 representation a base-class initializer into a parameter pack
3241 expansion. If all goes well, the resulting node will be an
3242 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3243 respectively. */
3244 tree
3245 make_pack_expansion (tree arg)
3246 {
3247 tree result;
3248 tree parameter_packs = NULL_TREE;
3249 bool for_types = false;
3250 struct find_parameter_pack_data ppd;
3251
3252 if (!arg || arg == error_mark_node)
3253 return arg;
3254
3255 if (TREE_CODE (arg) == TREE_LIST)
3256 {
3257 /* The only time we will see a TREE_LIST here is for a base
3258 class initializer. In this case, the TREE_PURPOSE will be a
3259 _TYPE node (representing the base class expansion we're
3260 initializing) and the TREE_VALUE will be a TREE_LIST
3261 containing the initialization arguments.
3262
3263 The resulting expansion looks somewhat different from most
3264 expansions. Rather than returning just one _EXPANSION, we
3265 return a TREE_LIST whose TREE_PURPOSE is a
3266 TYPE_PACK_EXPANSION containing the bases that will be
3267 initialized. The TREE_VALUE will be identical to the
3268 original TREE_VALUE, which is a list of arguments that will
3269 be passed to each base. We do not introduce any new pack
3270 expansion nodes into the TREE_VALUE (although it is possible
3271 that some already exist), because the TREE_PURPOSE and
3272 TREE_VALUE all need to be expanded together with the same
3273 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3274 resulting TREE_PURPOSE will mention the parameter packs in
3275 both the bases and the arguments to the bases. */
3276 tree purpose;
3277 tree value;
3278 tree parameter_packs = NULL_TREE;
3279
3280 /* Determine which parameter packs will be used by the base
3281 class expansion. */
3282 ppd.visited = pointer_set_create ();
3283 ppd.parameter_packs = &parameter_packs;
3284 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3285 &ppd, ppd.visited);
3286
3287 if (parameter_packs == NULL_TREE)
3288 {
3289 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3290 pointer_set_destroy (ppd.visited);
3291 return error_mark_node;
3292 }
3293
3294 if (TREE_VALUE (arg) != void_type_node)
3295 {
3296 /* Collect the sets of parameter packs used in each of the
3297 initialization arguments. */
3298 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3299 {
3300 /* Determine which parameter packs will be expanded in this
3301 argument. */
3302 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3303 &ppd, ppd.visited);
3304 }
3305 }
3306
3307 pointer_set_destroy (ppd.visited);
3308
3309 /* Create the pack expansion type for the base type. */
3310 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3311 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3312 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3313
3314 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3315 they will rarely be compared to anything. */
3316 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3317
3318 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3319 }
3320
3321 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3322 for_types = true;
3323
3324 /* Build the PACK_EXPANSION_* node. */
3325 result = for_types
3326 ? cxx_make_type (TYPE_PACK_EXPANSION)
3327 : make_node (EXPR_PACK_EXPANSION);
3328 SET_PACK_EXPANSION_PATTERN (result, arg);
3329 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3330 {
3331 /* Propagate type and const-expression information. */
3332 TREE_TYPE (result) = TREE_TYPE (arg);
3333 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3334 }
3335 else
3336 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3337 they will rarely be compared to anything. */
3338 SET_TYPE_STRUCTURAL_EQUALITY (result);
3339
3340 /* Determine which parameter packs will be expanded. */
3341 ppd.parameter_packs = &parameter_packs;
3342 ppd.visited = pointer_set_create ();
3343 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3344 pointer_set_destroy (ppd.visited);
3345
3346 /* Make sure we found some parameter packs. */
3347 if (parameter_packs == NULL_TREE)
3348 {
3349 if (TYPE_P (arg))
3350 error ("expansion pattern %<%T%> contains no argument packs", arg);
3351 else
3352 error ("expansion pattern %<%E%> contains no argument packs", arg);
3353 return error_mark_node;
3354 }
3355 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3356
3357 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3358
3359 return result;
3360 }
3361
3362 /* Checks T for any "bare" parameter packs, which have not yet been
3363 expanded, and issues an error if any are found. This operation can
3364 only be done on full expressions or types (e.g., an expression
3365 statement, "if" condition, etc.), because we could have expressions like:
3366
3367 foo(f(g(h(args)))...)
3368
3369 where "args" is a parameter pack. check_for_bare_parameter_packs
3370 should not be called for the subexpressions args, h(args),
3371 g(h(args)), or f(g(h(args))), because we would produce erroneous
3372 error messages.
3373
3374 Returns TRUE and emits an error if there were bare parameter packs,
3375 returns FALSE otherwise. */
3376 bool
3377 check_for_bare_parameter_packs (tree t)
3378 {
3379 tree parameter_packs = NULL_TREE;
3380 struct find_parameter_pack_data ppd;
3381
3382 if (!processing_template_decl || !t || t == error_mark_node)
3383 return false;
3384
3385 if (TREE_CODE (t) == TYPE_DECL)
3386 t = TREE_TYPE (t);
3387
3388 ppd.parameter_packs = &parameter_packs;
3389 ppd.visited = pointer_set_create ();
3390 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3391 pointer_set_destroy (ppd.visited);
3392
3393 if (parameter_packs)
3394 {
3395 error ("parameter packs not expanded with %<...%>:");
3396 while (parameter_packs)
3397 {
3398 tree pack = TREE_VALUE (parameter_packs);
3399 tree name = NULL_TREE;
3400
3401 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3402 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3403 name = TYPE_NAME (pack);
3404 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3405 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3406 else
3407 name = DECL_NAME (pack);
3408
3409 if (name)
3410 inform (input_location, " %qD", name);
3411 else
3412 inform (input_location, " <anonymous>");
3413
3414 parameter_packs = TREE_CHAIN (parameter_packs);
3415 }
3416
3417 return true;
3418 }
3419
3420 return false;
3421 }
3422
3423 /* Expand any parameter packs that occur in the template arguments in
3424 ARGS. */
3425 tree
3426 expand_template_argument_pack (tree args)
3427 {
3428 tree result_args = NULL_TREE;
3429 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3430 int num_result_args = -1;
3431 int non_default_args_count = -1;
3432
3433 /* First, determine if we need to expand anything, and the number of
3434 slots we'll need. */
3435 for (in_arg = 0; in_arg < nargs; ++in_arg)
3436 {
3437 tree arg = TREE_VEC_ELT (args, in_arg);
3438 if (arg == NULL_TREE)
3439 return args;
3440 if (ARGUMENT_PACK_P (arg))
3441 {
3442 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3443 if (num_result_args < 0)
3444 num_result_args = in_arg + num_packed;
3445 else
3446 num_result_args += num_packed;
3447 }
3448 else
3449 {
3450 if (num_result_args >= 0)
3451 num_result_args++;
3452 }
3453 }
3454
3455 /* If no expansion is necessary, we're done. */
3456 if (num_result_args < 0)
3457 return args;
3458
3459 /* Expand arguments. */
3460 result_args = make_tree_vec (num_result_args);
3461 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3462 non_default_args_count =
3463 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3464 for (in_arg = 0; in_arg < nargs; ++in_arg)
3465 {
3466 tree arg = TREE_VEC_ELT (args, in_arg);
3467 if (ARGUMENT_PACK_P (arg))
3468 {
3469 tree packed = ARGUMENT_PACK_ARGS (arg);
3470 int i, num_packed = TREE_VEC_LENGTH (packed);
3471 for (i = 0; i < num_packed; ++i, ++out_arg)
3472 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3473 if (non_default_args_count > 0)
3474 non_default_args_count += num_packed - 1;
3475 }
3476 else
3477 {
3478 TREE_VEC_ELT (result_args, out_arg) = arg;
3479 ++out_arg;
3480 }
3481 }
3482 if (non_default_args_count >= 0)
3483 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3484 return result_args;
3485 }
3486
3487 /* Checks if DECL shadows a template parameter.
3488
3489 [temp.local]: A template-parameter shall not be redeclared within its
3490 scope (including nested scopes).
3491
3492 Emits an error and returns TRUE if the DECL shadows a parameter,
3493 returns FALSE otherwise. */
3494
3495 bool
3496 check_template_shadow (tree decl)
3497 {
3498 tree olddecl;
3499
3500 /* If we're not in a template, we can't possibly shadow a template
3501 parameter. */
3502 if (!current_template_parms)
3503 return true;
3504
3505 /* Figure out what we're shadowing. */
3506 if (TREE_CODE (decl) == OVERLOAD)
3507 decl = OVL_CURRENT (decl);
3508 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3509
3510 /* If there's no previous binding for this name, we're not shadowing
3511 anything, let alone a template parameter. */
3512 if (!olddecl)
3513 return true;
3514
3515 /* If we're not shadowing a template parameter, we're done. Note
3516 that OLDDECL might be an OVERLOAD (or perhaps even an
3517 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3518 node. */
3519 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3520 return true;
3521
3522 /* We check for decl != olddecl to avoid bogus errors for using a
3523 name inside a class. We check TPFI to avoid duplicate errors for
3524 inline member templates. */
3525 if (decl == olddecl
3526 || (DECL_TEMPLATE_PARM_P (decl)
3527 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3528 return true;
3529
3530 /* Don't complain about the injected class name, as we've already
3531 complained about the class itself. */
3532 if (DECL_SELF_REFERENCE_P (decl))
3533 return false;
3534
3535 error ("declaration of %q+#D", decl);
3536 error (" shadows template parm %q+#D", olddecl);
3537 return false;
3538 }
3539
3540 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3541 ORIG_LEVEL, DECL, and TYPE. */
3542
3543 static tree
3544 build_template_parm_index (int index,
3545 int level,
3546 int orig_level,
3547 tree decl,
3548 tree type)
3549 {
3550 tree t = make_node (TEMPLATE_PARM_INDEX);
3551 TEMPLATE_PARM_IDX (t) = index;
3552 TEMPLATE_PARM_LEVEL (t) = level;
3553 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3554 TEMPLATE_PARM_DECL (t) = decl;
3555 TREE_TYPE (t) = type;
3556 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3557 TREE_READONLY (t) = TREE_READONLY (decl);
3558
3559 return t;
3560 }
3561
3562 /* Find the canonical type parameter for the given template type
3563 parameter. Returns the canonical type parameter, which may be TYPE
3564 if no such parameter existed. */
3565
3566 static tree
3567 canonical_type_parameter (tree type)
3568 {
3569 tree list;
3570 int idx = TEMPLATE_TYPE_IDX (type);
3571 if (!canonical_template_parms)
3572 vec_alloc (canonical_template_parms, idx+1);
3573
3574 while (canonical_template_parms->length () <= (unsigned)idx)
3575 vec_safe_push (canonical_template_parms, NULL_TREE);
3576
3577 list = (*canonical_template_parms)[idx];
3578 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3579 list = TREE_CHAIN (list);
3580
3581 if (list)
3582 return TREE_VALUE (list);
3583 else
3584 {
3585 (*canonical_template_parms)[idx]
3586 = tree_cons (NULL_TREE, type,
3587 (*canonical_template_parms)[idx]);
3588 return type;
3589 }
3590 }
3591
3592 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3593 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3594 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3595 new one is created. */
3596
3597 static tree
3598 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3599 tsubst_flags_t complain)
3600 {
3601 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3602 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3603 != TEMPLATE_PARM_LEVEL (index) - levels)
3604 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3605 {
3606 tree orig_decl = TEMPLATE_PARM_DECL (index);
3607 tree decl, t;
3608
3609 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3610 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3611 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3612 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3613 DECL_ARTIFICIAL (decl) = 1;
3614 SET_DECL_TEMPLATE_PARM_P (decl);
3615
3616 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3617 TEMPLATE_PARM_LEVEL (index) - levels,
3618 TEMPLATE_PARM_ORIG_LEVEL (index),
3619 decl, type);
3620 TEMPLATE_PARM_DESCENDANTS (index) = t;
3621 TEMPLATE_PARM_PARAMETER_PACK (t)
3622 = TEMPLATE_PARM_PARAMETER_PACK (index);
3623
3624 /* Template template parameters need this. */
3625 if (TREE_CODE (decl) == TEMPLATE_DECL)
3626 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3627 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3628 args, complain);
3629 }
3630
3631 return TEMPLATE_PARM_DESCENDANTS (index);
3632 }
3633
3634 /* Process information from new template parameter PARM and append it
3635 to the LIST being built. This new parameter is a non-type
3636 parameter iff IS_NON_TYPE is true. This new parameter is a
3637 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3638 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3639 parameter list PARM belongs to. This is used used to create a
3640 proper canonical type for the type of PARM that is to be created,
3641 iff PARM is a type. If the size is not known, this parameter shall
3642 be set to 0. */
3643
3644 tree
3645 process_template_parm (tree list, location_t parm_loc, tree parm,
3646 bool is_non_type, bool is_parameter_pack)
3647 {
3648 tree decl = 0;
3649 tree defval;
3650 tree err_parm_list;
3651 int idx = 0;
3652
3653 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3654 defval = TREE_PURPOSE (parm);
3655
3656 if (list)
3657 {
3658 tree p = tree_last (list);
3659
3660 if (p && TREE_VALUE (p) != error_mark_node)
3661 {
3662 p = TREE_VALUE (p);
3663 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3664 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3665 else
3666 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3667 }
3668
3669 ++idx;
3670 }
3671 else
3672 idx = 0;
3673
3674 if (is_non_type)
3675 {
3676 parm = TREE_VALUE (parm);
3677
3678 SET_DECL_TEMPLATE_PARM_P (parm);
3679
3680 if (TREE_TYPE (parm) == error_mark_node)
3681 {
3682 err_parm_list = build_tree_list (defval, parm);
3683 TREE_VALUE (err_parm_list) = error_mark_node;
3684 return chainon (list, err_parm_list);
3685 }
3686 else
3687 {
3688 /* [temp.param]
3689
3690 The top-level cv-qualifiers on the template-parameter are
3691 ignored when determining its type. */
3692 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3693 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3694 {
3695 err_parm_list = build_tree_list (defval, parm);
3696 TREE_VALUE (err_parm_list) = error_mark_node;
3697 return chainon (list, err_parm_list);
3698 }
3699
3700 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack
3701 /* If we're in a nested template parameter list, the template
3702 template parameter could be a parameter pack. */
3703 && processing_template_parmlist == 1)
3704 {
3705 /* This template parameter is not a parameter pack, but it
3706 should be. Complain about "bare" parameter packs. */
3707 check_for_bare_parameter_packs (TREE_TYPE (parm));
3708
3709 /* Recover by calling this a parameter pack. */
3710 is_parameter_pack = true;
3711 }
3712 }
3713
3714 /* A template parameter is not modifiable. */
3715 TREE_CONSTANT (parm) = 1;
3716 TREE_READONLY (parm) = 1;
3717 decl = build_decl (parm_loc,
3718 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3719 TREE_CONSTANT (decl) = 1;
3720 TREE_READONLY (decl) = 1;
3721 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3722 = build_template_parm_index (idx, processing_template_decl,
3723 processing_template_decl,
3724 decl, TREE_TYPE (parm));
3725
3726 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3727 = is_parameter_pack;
3728 }
3729 else
3730 {
3731 tree t;
3732 parm = TREE_VALUE (TREE_VALUE (parm));
3733
3734 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3735 {
3736 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3737 /* This is for distinguishing between real templates and template
3738 template parameters */
3739 TREE_TYPE (parm) = t;
3740 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3741 decl = parm;
3742 }
3743 else
3744 {
3745 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3746 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3747 decl = build_decl (parm_loc,
3748 TYPE_DECL, parm, t);
3749 }
3750
3751 TYPE_NAME (t) = decl;
3752 TYPE_STUB_DECL (t) = decl;
3753 parm = decl;
3754 TEMPLATE_TYPE_PARM_INDEX (t)
3755 = build_template_parm_index (idx, processing_template_decl,
3756 processing_template_decl,
3757 decl, TREE_TYPE (parm));
3758 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3759 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3760 }
3761 DECL_ARTIFICIAL (decl) = 1;
3762 SET_DECL_TEMPLATE_PARM_P (decl);
3763 pushdecl (decl);
3764 parm = build_tree_list (defval, parm);
3765 return chainon (list, parm);
3766 }
3767
3768 /* The end of a template parameter list has been reached. Process the
3769 tree list into a parameter vector, converting each parameter into a more
3770 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3771 as PARM_DECLs. */
3772
3773 tree
3774 end_template_parm_list (tree parms)
3775 {
3776 int nparms;
3777 tree parm, next;
3778 tree saved_parmlist = make_tree_vec (list_length (parms));
3779
3780 current_template_parms
3781 = tree_cons (size_int (processing_template_decl),
3782 saved_parmlist, current_template_parms);
3783
3784 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3785 {
3786 next = TREE_CHAIN (parm);
3787 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3788 TREE_CHAIN (parm) = NULL_TREE;
3789 }
3790
3791 --processing_template_parmlist;
3792
3793 return saved_parmlist;
3794 }
3795
3796 /* end_template_decl is called after a template declaration is seen. */
3797
3798 void
3799 end_template_decl (void)
3800 {
3801 reset_specialization ();
3802
3803 if (! processing_template_decl)
3804 return;
3805
3806 /* This matches the pushlevel in begin_template_parm_list. */
3807 finish_scope ();
3808
3809 --processing_template_decl;
3810 current_template_parms = TREE_CHAIN (current_template_parms);
3811 }
3812
3813 /* Takes a TREE_LIST representing a template parameter and convert it
3814 into an argument suitable to be passed to the type substitution
3815 functions. Note that If the TREE_LIST contains an error_mark
3816 node, the returned argument is error_mark_node. */
3817
3818 static tree
3819 template_parm_to_arg (tree t)
3820 {
3821
3822 if (t == NULL_TREE
3823 || TREE_CODE (t) != TREE_LIST)
3824 return t;
3825
3826 if (error_operand_p (TREE_VALUE (t)))
3827 return error_mark_node;
3828
3829 t = TREE_VALUE (t);
3830
3831 if (TREE_CODE (t) == TYPE_DECL
3832 || TREE_CODE (t) == TEMPLATE_DECL)
3833 {
3834 t = TREE_TYPE (t);
3835
3836 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3837 {
3838 /* Turn this argument into a TYPE_ARGUMENT_PACK
3839 with a single element, which expands T. */
3840 tree vec = make_tree_vec (1);
3841 #ifdef ENABLE_CHECKING
3842 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3843 (vec, TREE_VEC_LENGTH (vec));
3844 #endif
3845 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3846
3847 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3848 SET_ARGUMENT_PACK_ARGS (t, vec);
3849 }
3850 }
3851 else
3852 {
3853 t = DECL_INITIAL (t);
3854
3855 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3856 {
3857 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3858 with a single element, which expands T. */
3859 tree vec = make_tree_vec (1);
3860 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3861 #ifdef ENABLE_CHECKING
3862 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3863 (vec, TREE_VEC_LENGTH (vec));
3864 #endif
3865 t = convert_from_reference (t);
3866 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3867
3868 t = make_node (NONTYPE_ARGUMENT_PACK);
3869 SET_ARGUMENT_PACK_ARGS (t, vec);
3870 TREE_TYPE (t) = type;
3871 }
3872 else
3873 t = convert_from_reference (t);
3874 }
3875 return t;
3876 }
3877
3878 /* Given a set of template parameters, return them as a set of template
3879 arguments. The template parameters are represented as a TREE_VEC, in
3880 the form documented in cp-tree.h for template arguments. */
3881
3882 static tree
3883 template_parms_to_args (tree parms)
3884 {
3885 tree header;
3886 tree args = NULL_TREE;
3887 int length = TMPL_PARMS_DEPTH (parms);
3888 int l = length;
3889
3890 /* If there is only one level of template parameters, we do not
3891 create a TREE_VEC of TREE_VECs. Instead, we return a single
3892 TREE_VEC containing the arguments. */
3893 if (length > 1)
3894 args = make_tree_vec (length);
3895
3896 for (header = parms; header; header = TREE_CHAIN (header))
3897 {
3898 tree a = copy_node (TREE_VALUE (header));
3899 int i;
3900
3901 TREE_TYPE (a) = NULL_TREE;
3902 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3903 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3904
3905 #ifdef ENABLE_CHECKING
3906 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3907 #endif
3908
3909 if (length > 1)
3910 TREE_VEC_ELT (args, --l) = a;
3911 else
3912 args = a;
3913 }
3914
3915 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3916 /* This can happen for template parms of a template template
3917 parameter, e.g:
3918
3919 template<template<class T, class U> class TT> struct S;
3920
3921 Consider the level of the parms of TT; T and U both have
3922 level 2; TT has no template parm of level 1. So in this case
3923 the first element of full_template_args is NULL_TREE. If we
3924 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3925 of 2. This will make tsubst wrongly consider that T and U
3926 have level 1. Instead, let's create a dummy vector as the
3927 first element of full_template_args so that TMPL_ARGS_DEPTH
3928 returns the correct depth for args. */
3929 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3930 return args;
3931 }
3932
3933 /* Within the declaration of a template, return the currently active
3934 template parameters as an argument TREE_VEC. */
3935
3936 static tree
3937 current_template_args (void)
3938 {
3939 return template_parms_to_args (current_template_parms);
3940 }
3941
3942 /* Update the declared TYPE by doing any lookups which were thought to be
3943 dependent, but are not now that we know the SCOPE of the declarator. */
3944
3945 tree
3946 maybe_update_decl_type (tree orig_type, tree scope)
3947 {
3948 tree type = orig_type;
3949
3950 if (type == NULL_TREE)
3951 return type;
3952
3953 if (TREE_CODE (orig_type) == TYPE_DECL)
3954 type = TREE_TYPE (type);
3955
3956 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3957 && dependent_type_p (type)
3958 /* Don't bother building up the args in this case. */
3959 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3960 {
3961 /* tsubst in the args corresponding to the template parameters,
3962 including auto if present. Most things will be unchanged, but
3963 make_typename_type and tsubst_qualified_id will resolve
3964 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3965 tree args = current_template_args ();
3966 tree auto_node = type_uses_auto (type);
3967 tree pushed;
3968 if (auto_node)
3969 {
3970 tree auto_vec = make_tree_vec (1);
3971 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3972 args = add_to_template_args (args, auto_vec);
3973 }
3974 pushed = push_scope (scope);
3975 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3976 if (pushed)
3977 pop_scope (scope);
3978 }
3979
3980 if (type == error_mark_node)
3981 return orig_type;
3982
3983 if (TREE_CODE (orig_type) == TYPE_DECL)
3984 {
3985 if (same_type_p (type, TREE_TYPE (orig_type)))
3986 type = orig_type;
3987 else
3988 type = TYPE_NAME (type);
3989 }
3990 return type;
3991 }
3992
3993 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3994 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3995 a member template. Used by push_template_decl below. */
3996
3997 static tree
3998 build_template_decl (tree decl, tree parms, bool member_template_p)
3999 {
4000 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4001 DECL_TEMPLATE_PARMS (tmpl) = parms;
4002 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4003 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4004 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4005
4006 return tmpl;
4007 }
4008
4009 struct template_parm_data
4010 {
4011 /* The level of the template parameters we are currently
4012 processing. */
4013 int level;
4014
4015 /* The index of the specialization argument we are currently
4016 processing. */
4017 int current_arg;
4018
4019 /* An array whose size is the number of template parameters. The
4020 elements are nonzero if the parameter has been used in any one
4021 of the arguments processed so far. */
4022 int* parms;
4023
4024 /* An array whose size is the number of template arguments. The
4025 elements are nonzero if the argument makes use of template
4026 parameters of this level. */
4027 int* arg_uses_template_parms;
4028 };
4029
4030 /* Subroutine of push_template_decl used to see if each template
4031 parameter in a partial specialization is used in the explicit
4032 argument list. If T is of the LEVEL given in DATA (which is
4033 treated as a template_parm_data*), then DATA->PARMS is marked
4034 appropriately. */
4035
4036 static int
4037 mark_template_parm (tree t, void* data)
4038 {
4039 int level;
4040 int idx;
4041 struct template_parm_data* tpd = (struct template_parm_data*) data;
4042
4043 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4044 {
4045 level = TEMPLATE_PARM_LEVEL (t);
4046 idx = TEMPLATE_PARM_IDX (t);
4047 }
4048 else
4049 {
4050 level = TEMPLATE_TYPE_LEVEL (t);
4051 idx = TEMPLATE_TYPE_IDX (t);
4052 }
4053
4054 if (level == tpd->level)
4055 {
4056 tpd->parms[idx] = 1;
4057 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4058 }
4059
4060 /* Return zero so that for_each_template_parm will continue the
4061 traversal of the tree; we want to mark *every* template parm. */
4062 return 0;
4063 }
4064
4065 /* Process the partial specialization DECL. */
4066
4067 static tree
4068 process_partial_specialization (tree decl)
4069 {
4070 tree type = TREE_TYPE (decl);
4071 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4072 tree specargs = CLASSTYPE_TI_ARGS (type);
4073 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4074 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4075 tree inner_parms;
4076 tree inst;
4077 int nargs = TREE_VEC_LENGTH (inner_args);
4078 int ntparms;
4079 int i;
4080 bool did_error_intro = false;
4081 struct template_parm_data tpd;
4082 struct template_parm_data tpd2;
4083
4084 gcc_assert (current_template_parms);
4085
4086 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4087 ntparms = TREE_VEC_LENGTH (inner_parms);
4088
4089 /* We check that each of the template parameters given in the
4090 partial specialization is used in the argument list to the
4091 specialization. For example:
4092
4093 template <class T> struct S;
4094 template <class T> struct S<T*>;
4095
4096 The second declaration is OK because `T*' uses the template
4097 parameter T, whereas
4098
4099 template <class T> struct S<int>;
4100
4101 is no good. Even trickier is:
4102
4103 template <class T>
4104 struct S1
4105 {
4106 template <class U>
4107 struct S2;
4108 template <class U>
4109 struct S2<T>;
4110 };
4111
4112 The S2<T> declaration is actually invalid; it is a
4113 full-specialization. Of course,
4114
4115 template <class U>
4116 struct S2<T (*)(U)>;
4117
4118 or some such would have been OK. */
4119 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4120 tpd.parms = XALLOCAVEC (int, ntparms);
4121 memset (tpd.parms, 0, sizeof (int) * ntparms);
4122
4123 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4124 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4125 for (i = 0; i < nargs; ++i)
4126 {
4127 tpd.current_arg = i;
4128 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4129 &mark_template_parm,
4130 &tpd,
4131 NULL,
4132 /*include_nondeduced_p=*/false);
4133 }
4134 for (i = 0; i < ntparms; ++i)
4135 if (tpd.parms[i] == 0)
4136 {
4137 /* One of the template parms was not used in the
4138 specialization. */
4139 if (!did_error_intro)
4140 {
4141 error ("template parameters not used in partial specialization:");
4142 did_error_intro = true;
4143 }
4144
4145 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4146 }
4147
4148 if (did_error_intro)
4149 return error_mark_node;
4150
4151 /* [temp.class.spec]
4152
4153 The argument list of the specialization shall not be identical to
4154 the implicit argument list of the primary template. */
4155 if (comp_template_args
4156 (inner_args,
4157 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4158 (maintmpl)))))
4159 error ("partial specialization %qT does not specialize any template arguments", type);
4160
4161 /* A partial specialization that replaces multiple parameters of the
4162 primary template with a pack expansion is less specialized for those
4163 parameters. */
4164 if (nargs < DECL_NTPARMS (maintmpl))
4165 {
4166 error ("partial specialization is not more specialized than the "
4167 "primary template because it replaces multiple parameters "
4168 "with a pack expansion");
4169 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4170 return decl;
4171 }
4172
4173 /* [temp.class.spec]
4174
4175 A partially specialized non-type argument expression shall not
4176 involve template parameters of the partial specialization except
4177 when the argument expression is a simple identifier.
4178
4179 The type of a template parameter corresponding to a specialized
4180 non-type argument shall not be dependent on a parameter of the
4181 specialization.
4182
4183 Also, we verify that pack expansions only occur at the
4184 end of the argument list. */
4185 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4186 tpd2.parms = 0;
4187 for (i = 0; i < nargs; ++i)
4188 {
4189 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4190 tree arg = TREE_VEC_ELT (inner_args, i);
4191 tree packed_args = NULL_TREE;
4192 int j, len = 1;
4193
4194 if (ARGUMENT_PACK_P (arg))
4195 {
4196 /* Extract the arguments from the argument pack. We'll be
4197 iterating over these in the following loop. */
4198 packed_args = ARGUMENT_PACK_ARGS (arg);
4199 len = TREE_VEC_LENGTH (packed_args);
4200 }
4201
4202 for (j = 0; j < len; j++)
4203 {
4204 if (packed_args)
4205 /* Get the Jth argument in the parameter pack. */
4206 arg = TREE_VEC_ELT (packed_args, j);
4207
4208 if (PACK_EXPANSION_P (arg))
4209 {
4210 /* Pack expansions must come at the end of the
4211 argument list. */
4212 if ((packed_args && j < len - 1)
4213 || (!packed_args && i < nargs - 1))
4214 {
4215 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4216 error ("parameter pack argument %qE must be at the "
4217 "end of the template argument list", arg);
4218 else
4219 error ("parameter pack argument %qT must be at the "
4220 "end of the template argument list", arg);
4221 }
4222 }
4223
4224 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4225 /* We only care about the pattern. */
4226 arg = PACK_EXPANSION_PATTERN (arg);
4227
4228 if (/* These first two lines are the `non-type' bit. */
4229 !TYPE_P (arg)
4230 && TREE_CODE (arg) != TEMPLATE_DECL
4231 /* This next two lines are the `argument expression is not just a
4232 simple identifier' condition and also the `specialized
4233 non-type argument' bit. */
4234 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4235 && !(REFERENCE_REF_P (arg)
4236 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4237 {
4238 if ((!packed_args && tpd.arg_uses_template_parms[i])
4239 || (packed_args && uses_template_parms (arg)))
4240 error ("template argument %qE involves template parameter(s)",
4241 arg);
4242 else
4243 {
4244 /* Look at the corresponding template parameter,
4245 marking which template parameters its type depends
4246 upon. */
4247 tree type = TREE_TYPE (parm);
4248
4249 if (!tpd2.parms)
4250 {
4251 /* We haven't yet initialized TPD2. Do so now. */
4252 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4253 /* The number of parameters here is the number in the
4254 main template, which, as checked in the assertion
4255 above, is NARGS. */
4256 tpd2.parms = XALLOCAVEC (int, nargs);
4257 tpd2.level =
4258 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4259 }
4260
4261 /* Mark the template parameters. But this time, we're
4262 looking for the template parameters of the main
4263 template, not in the specialization. */
4264 tpd2.current_arg = i;
4265 tpd2.arg_uses_template_parms[i] = 0;
4266 memset (tpd2.parms, 0, sizeof (int) * nargs);
4267 for_each_template_parm (type,
4268 &mark_template_parm,
4269 &tpd2,
4270 NULL,
4271 /*include_nondeduced_p=*/false);
4272
4273 if (tpd2.arg_uses_template_parms [i])
4274 {
4275 /* The type depended on some template parameters.
4276 If they are fully specialized in the
4277 specialization, that's OK. */
4278 int j;
4279 int count = 0;
4280 for (j = 0; j < nargs; ++j)
4281 if (tpd2.parms[j] != 0
4282 && tpd.arg_uses_template_parms [j])
4283 ++count;
4284 if (count != 0)
4285 error_n (input_location, count,
4286 "type %qT of template argument %qE depends "
4287 "on a template parameter",
4288 "type %qT of template argument %qE depends "
4289 "on template parameters",
4290 type,
4291 arg);
4292 }
4293 }
4294 }
4295 }
4296 }
4297
4298 /* We should only get here once. */
4299 gcc_assert (!COMPLETE_TYPE_P (type));
4300
4301 tree tmpl = build_template_decl (decl, current_template_parms,
4302 DECL_MEMBER_TEMPLATE_P (maintmpl));
4303 TREE_TYPE (tmpl) = type;
4304 DECL_TEMPLATE_RESULT (tmpl) = decl;
4305 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4306 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4307 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4308
4309 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4310 = tree_cons (specargs, tmpl,
4311 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4312 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4313
4314 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4315 inst = TREE_CHAIN (inst))
4316 {
4317 tree inst_type = TREE_VALUE (inst);
4318 if (COMPLETE_TYPE_P (inst_type)
4319 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4320 {
4321 tree spec = most_specialized_class (inst_type, tf_none);
4322 if (spec && TREE_TYPE (spec) == type)
4323 permerror (input_location,
4324 "partial specialization of %qT after instantiation "
4325 "of %qT", type, inst_type);
4326 }
4327 }
4328
4329 return decl;
4330 }
4331
4332 /* PARM is a template parameter of some form; return the corresponding
4333 TEMPLATE_PARM_INDEX. */
4334
4335 static tree
4336 get_template_parm_index (tree parm)
4337 {
4338 if (TREE_CODE (parm) == PARM_DECL
4339 || TREE_CODE (parm) == CONST_DECL)
4340 parm = DECL_INITIAL (parm);
4341 else if (TREE_CODE (parm) == TYPE_DECL
4342 || TREE_CODE (parm) == TEMPLATE_DECL)
4343 parm = TREE_TYPE (parm);
4344 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4345 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4346 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4347 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4348 return parm;
4349 }
4350
4351 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4352 parameter packs used by the template parameter PARM. */
4353
4354 static void
4355 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4356 {
4357 /* A type parm can't refer to another parm. */
4358 if (TREE_CODE (parm) == TYPE_DECL)
4359 return;
4360 else if (TREE_CODE (parm) == PARM_DECL)
4361 {
4362 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4363 ppd, ppd->visited);
4364 return;
4365 }
4366
4367 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4368
4369 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4370 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4371 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4372 }
4373
4374 /* PARM is a template parameter pack. Return any parameter packs used in
4375 its type or the type of any of its template parameters. If there are
4376 any such packs, it will be instantiated into a fixed template parameter
4377 list by partial instantiation rather than be fully deduced. */
4378
4379 tree
4380 fixed_parameter_pack_p (tree parm)
4381 {
4382 /* This can only be true in a member template. */
4383 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4384 return NULL_TREE;
4385 /* This can only be true for a parameter pack. */
4386 if (!template_parameter_pack_p (parm))
4387 return NULL_TREE;
4388 /* A type parm can't refer to another parm. */
4389 if (TREE_CODE (parm) == TYPE_DECL)
4390 return NULL_TREE;
4391
4392 tree parameter_packs = NULL_TREE;
4393 struct find_parameter_pack_data ppd;
4394 ppd.parameter_packs = &parameter_packs;
4395 ppd.visited = pointer_set_create ();
4396
4397 fixed_parameter_pack_p_1 (parm, &ppd);
4398
4399 pointer_set_destroy (ppd.visited);
4400 return parameter_packs;
4401 }
4402
4403 /* Check that a template declaration's use of default arguments and
4404 parameter packs is not invalid. Here, PARMS are the template
4405 parameters. IS_PRIMARY is true if DECL is the thing declared by
4406 a primary template. IS_PARTIAL is true if DECL is a partial
4407 specialization.
4408
4409 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4410 declaration (but not a definition); 1 indicates a declaration, 2
4411 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4412 emitted for extraneous default arguments.
4413
4414 Returns TRUE if there were no errors found, FALSE otherwise. */
4415
4416 bool
4417 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4418 bool is_partial, int is_friend_decl)
4419 {
4420 const char *msg;
4421 int last_level_to_check;
4422 tree parm_level;
4423 bool no_errors = true;
4424
4425 /* [temp.param]
4426
4427 A default template-argument shall not be specified in a
4428 function template declaration or a function template definition, nor
4429 in the template-parameter-list of the definition of a member of a
4430 class template. */
4431
4432 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4433 /* You can't have a function template declaration in a local
4434 scope, nor you can you define a member of a class template in a
4435 local scope. */
4436 return true;
4437
4438 if (TREE_CODE (decl) == TYPE_DECL
4439 && TREE_TYPE (decl)
4440 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4441 /* A lambda doesn't have an explicit declaration; don't complain
4442 about the parms of the enclosing class. */
4443 return true;
4444
4445 if (current_class_type
4446 && !TYPE_BEING_DEFINED (current_class_type)
4447 && DECL_LANG_SPECIFIC (decl)
4448 && DECL_DECLARES_FUNCTION_P (decl)
4449 /* If this is either a friend defined in the scope of the class
4450 or a member function. */
4451 && (DECL_FUNCTION_MEMBER_P (decl)
4452 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4453 : DECL_FRIEND_CONTEXT (decl)
4454 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4455 : false)
4456 /* And, if it was a member function, it really was defined in
4457 the scope of the class. */
4458 && (!DECL_FUNCTION_MEMBER_P (decl)
4459 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4460 /* We already checked these parameters when the template was
4461 declared, so there's no need to do it again now. This function
4462 was defined in class scope, but we're processing its body now
4463 that the class is complete. */
4464 return true;
4465
4466 /* Core issue 226 (C++0x only): the following only applies to class
4467 templates. */
4468 if (is_primary
4469 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4470 {
4471 /* [temp.param]
4472
4473 If a template-parameter has a default template-argument, all
4474 subsequent template-parameters shall have a default
4475 template-argument supplied. */
4476 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4477 {
4478 tree inner_parms = TREE_VALUE (parm_level);
4479 int ntparms = TREE_VEC_LENGTH (inner_parms);
4480 int seen_def_arg_p = 0;
4481 int i;
4482
4483 for (i = 0; i < ntparms; ++i)
4484 {
4485 tree parm = TREE_VEC_ELT (inner_parms, i);
4486
4487 if (parm == error_mark_node)
4488 continue;
4489
4490 if (TREE_PURPOSE (parm))
4491 seen_def_arg_p = 1;
4492 else if (seen_def_arg_p
4493 && !template_parameter_pack_p (TREE_VALUE (parm)))
4494 {
4495 error ("no default argument for %qD", TREE_VALUE (parm));
4496 /* For better subsequent error-recovery, we indicate that
4497 there should have been a default argument. */
4498 TREE_PURPOSE (parm) = error_mark_node;
4499 no_errors = false;
4500 }
4501 else if (!is_partial
4502 && !is_friend_decl
4503 /* Don't complain about an enclosing partial
4504 specialization. */
4505 && parm_level == parms
4506 && TREE_CODE (decl) == TYPE_DECL
4507 && i < ntparms - 1
4508 && template_parameter_pack_p (TREE_VALUE (parm))
4509 /* A fixed parameter pack will be partially
4510 instantiated into a fixed length list. */
4511 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4512 {
4513 /* A primary class template can only have one
4514 parameter pack, at the end of the template
4515 parameter list. */
4516
4517 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4518 error ("parameter pack %qE must be at the end of the"
4519 " template parameter list", TREE_VALUE (parm));
4520 else
4521 error ("parameter pack %qT must be at the end of the"
4522 " template parameter list",
4523 TREE_TYPE (TREE_VALUE (parm)));
4524
4525 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4526 = error_mark_node;
4527 no_errors = false;
4528 }
4529 }
4530 }
4531 }
4532
4533 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4534 || is_partial
4535 || !is_primary
4536 || is_friend_decl)
4537 /* For an ordinary class template, default template arguments are
4538 allowed at the innermost level, e.g.:
4539 template <class T = int>
4540 struct S {};
4541 but, in a partial specialization, they're not allowed even
4542 there, as we have in [temp.class.spec]:
4543
4544 The template parameter list of a specialization shall not
4545 contain default template argument values.
4546
4547 So, for a partial specialization, or for a function template
4548 (in C++98/C++03), we look at all of them. */
4549 ;
4550 else
4551 /* But, for a primary class template that is not a partial
4552 specialization we look at all template parameters except the
4553 innermost ones. */
4554 parms = TREE_CHAIN (parms);
4555
4556 /* Figure out what error message to issue. */
4557 if (is_friend_decl == 2)
4558 msg = G_("default template arguments may not be used in function template "
4559 "friend re-declaration");
4560 else if (is_friend_decl)
4561 msg = G_("default template arguments may not be used in function template "
4562 "friend declarations");
4563 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4564 msg = G_("default template arguments may not be used in function templates "
4565 "without -std=c++11 or -std=gnu++11");
4566 else if (is_partial)
4567 msg = G_("default template arguments may not be used in "
4568 "partial specializations");
4569 else
4570 msg = G_("default argument for template parameter for class enclosing %qD");
4571
4572 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4573 /* If we're inside a class definition, there's no need to
4574 examine the parameters to the class itself. On the one
4575 hand, they will be checked when the class is defined, and,
4576 on the other, default arguments are valid in things like:
4577 template <class T = double>
4578 struct S { template <class U> void f(U); };
4579 Here the default argument for `S' has no bearing on the
4580 declaration of `f'. */
4581 last_level_to_check = template_class_depth (current_class_type) + 1;
4582 else
4583 /* Check everything. */
4584 last_level_to_check = 0;
4585
4586 for (parm_level = parms;
4587 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4588 parm_level = TREE_CHAIN (parm_level))
4589 {
4590 tree inner_parms = TREE_VALUE (parm_level);
4591 int i;
4592 int ntparms;
4593
4594 ntparms = TREE_VEC_LENGTH (inner_parms);
4595 for (i = 0; i < ntparms; ++i)
4596 {
4597 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4598 continue;
4599
4600 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4601 {
4602 if (msg)
4603 {
4604 no_errors = false;
4605 if (is_friend_decl == 2)
4606 return no_errors;
4607
4608 error (msg, decl);
4609 msg = 0;
4610 }
4611
4612 /* Clear out the default argument so that we are not
4613 confused later. */
4614 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4615 }
4616 }
4617
4618 /* At this point, if we're still interested in issuing messages,
4619 they must apply to classes surrounding the object declared. */
4620 if (msg)
4621 msg = G_("default argument for template parameter for class "
4622 "enclosing %qD");
4623 }
4624
4625 return no_errors;
4626 }
4627
4628 /* Worker for push_template_decl_real, called via
4629 for_each_template_parm. DATA is really an int, indicating the
4630 level of the parameters we are interested in. If T is a template
4631 parameter of that level, return nonzero. */
4632
4633 static int
4634 template_parm_this_level_p (tree t, void* data)
4635 {
4636 int this_level = *(int *)data;
4637 int level;
4638
4639 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4640 level = TEMPLATE_PARM_LEVEL (t);
4641 else
4642 level = TEMPLATE_TYPE_LEVEL (t);
4643 return level == this_level;
4644 }
4645
4646 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4647 parameters given by current_template_args, or reuses a
4648 previously existing one, if appropriate. Returns the DECL, or an
4649 equivalent one, if it is replaced via a call to duplicate_decls.
4650
4651 If IS_FRIEND is true, DECL is a friend declaration. */
4652
4653 tree
4654 push_template_decl_real (tree decl, bool is_friend)
4655 {
4656 tree tmpl;
4657 tree args;
4658 tree info;
4659 tree ctx;
4660 bool is_primary;
4661 bool is_partial;
4662 int new_template_p = 0;
4663 /* True if the template is a member template, in the sense of
4664 [temp.mem]. */
4665 bool member_template_p = false;
4666
4667 if (decl == error_mark_node || !current_template_parms)
4668 return error_mark_node;
4669
4670 /* See if this is a partial specialization. */
4671 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4672 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4673 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4674
4675 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4676 is_friend = true;
4677
4678 if (is_friend)
4679 /* For a friend, we want the context of the friend function, not
4680 the type of which it is a friend. */
4681 ctx = CP_DECL_CONTEXT (decl);
4682 else if (CP_DECL_CONTEXT (decl)
4683 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4684 /* In the case of a virtual function, we want the class in which
4685 it is defined. */
4686 ctx = CP_DECL_CONTEXT (decl);
4687 else
4688 /* Otherwise, if we're currently defining some class, the DECL
4689 is assumed to be a member of the class. */
4690 ctx = current_scope ();
4691
4692 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4693 ctx = NULL_TREE;
4694
4695 if (!DECL_CONTEXT (decl))
4696 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4697
4698 /* See if this is a primary template. */
4699 if (is_friend && ctx
4700 && uses_template_parms_level (ctx, processing_template_decl))
4701 /* A friend template that specifies a class context, i.e.
4702 template <typename T> friend void A<T>::f();
4703 is not primary. */
4704 is_primary = false;
4705 else
4706 is_primary = template_parm_scope_p ();
4707
4708 if (is_primary)
4709 {
4710 if (DECL_CLASS_SCOPE_P (decl))
4711 member_template_p = true;
4712 if (TREE_CODE (decl) == TYPE_DECL
4713 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4714 {
4715 error ("template class without a name");
4716 return error_mark_node;
4717 }
4718 else if (TREE_CODE (decl) == FUNCTION_DECL)
4719 {
4720 if (DECL_DESTRUCTOR_P (decl))
4721 {
4722 /* [temp.mem]
4723
4724 A destructor shall not be a member template. */
4725 error ("destructor %qD declared as member template", decl);
4726 return error_mark_node;
4727 }
4728 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4729 && (!prototype_p (TREE_TYPE (decl))
4730 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4731 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4732 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4733 == void_list_node)))
4734 {
4735 /* [basic.stc.dynamic.allocation]
4736
4737 An allocation function can be a function
4738 template. ... Template allocation functions shall
4739 have two or more parameters. */
4740 error ("invalid template declaration of %qD", decl);
4741 return error_mark_node;
4742 }
4743 }
4744 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4745 && CLASS_TYPE_P (TREE_TYPE (decl)))
4746 /* OK */;
4747 else if (TREE_CODE (decl) == TYPE_DECL
4748 && TYPE_DECL_ALIAS_P (decl))
4749 /* alias-declaration */
4750 gcc_assert (!DECL_ARTIFICIAL (decl));
4751 else
4752 {
4753 error ("template declaration of %q#D", decl);
4754 return error_mark_node;
4755 }
4756 }
4757
4758 /* Check to see that the rules regarding the use of default
4759 arguments are not being violated. */
4760 check_default_tmpl_args (decl, current_template_parms,
4761 is_primary, is_partial, /*is_friend_decl=*/0);
4762
4763 /* Ensure that there are no parameter packs in the type of this
4764 declaration that have not been expanded. */
4765 if (TREE_CODE (decl) == FUNCTION_DECL)
4766 {
4767 /* Check each of the arguments individually to see if there are
4768 any bare parameter packs. */
4769 tree type = TREE_TYPE (decl);
4770 tree arg = DECL_ARGUMENTS (decl);
4771 tree argtype = TYPE_ARG_TYPES (type);
4772
4773 while (arg && argtype)
4774 {
4775 if (!DECL_PACK_P (arg)
4776 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4777 {
4778 /* This is a PARM_DECL that contains unexpanded parameter
4779 packs. We have already complained about this in the
4780 check_for_bare_parameter_packs call, so just replace
4781 these types with ERROR_MARK_NODE. */
4782 TREE_TYPE (arg) = error_mark_node;
4783 TREE_VALUE (argtype) = error_mark_node;
4784 }
4785
4786 arg = DECL_CHAIN (arg);
4787 argtype = TREE_CHAIN (argtype);
4788 }
4789
4790 /* Check for bare parameter packs in the return type and the
4791 exception specifiers. */
4792 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4793 /* Errors were already issued, set return type to int
4794 as the frontend doesn't expect error_mark_node as
4795 the return type. */
4796 TREE_TYPE (type) = integer_type_node;
4797 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4798 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4799 }
4800 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4801 && TYPE_DECL_ALIAS_P (decl))
4802 ? DECL_ORIGINAL_TYPE (decl)
4803 : TREE_TYPE (decl)))
4804 {
4805 TREE_TYPE (decl) = error_mark_node;
4806 return error_mark_node;
4807 }
4808
4809 if (is_partial)
4810 return process_partial_specialization (decl);
4811
4812 args = current_template_args ();
4813
4814 if (!ctx
4815 || TREE_CODE (ctx) == FUNCTION_DECL
4816 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4817 || (TREE_CODE (decl) == TYPE_DECL
4818 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4819 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4820 {
4821 if (DECL_LANG_SPECIFIC (decl)
4822 && DECL_TEMPLATE_INFO (decl)
4823 && DECL_TI_TEMPLATE (decl))
4824 tmpl = DECL_TI_TEMPLATE (decl);
4825 /* If DECL is a TYPE_DECL for a class-template, then there won't
4826 be DECL_LANG_SPECIFIC. The information equivalent to
4827 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4828 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4829 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4830 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4831 {
4832 /* Since a template declaration already existed for this
4833 class-type, we must be redeclaring it here. Make sure
4834 that the redeclaration is valid. */
4835 redeclare_class_template (TREE_TYPE (decl),
4836 current_template_parms);
4837 /* We don't need to create a new TEMPLATE_DECL; just use the
4838 one we already had. */
4839 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4840 }
4841 else
4842 {
4843 tmpl = build_template_decl (decl, current_template_parms,
4844 member_template_p);
4845 new_template_p = 1;
4846
4847 if (DECL_LANG_SPECIFIC (decl)
4848 && DECL_TEMPLATE_SPECIALIZATION (decl))
4849 {
4850 /* A specialization of a member template of a template
4851 class. */
4852 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4853 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4854 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4855 }
4856 }
4857 }
4858 else
4859 {
4860 tree a, t, current, parms;
4861 int i;
4862 tree tinfo = get_template_info (decl);
4863
4864 if (!tinfo)
4865 {
4866 error ("template definition of non-template %q#D", decl);
4867 return error_mark_node;
4868 }
4869
4870 tmpl = TI_TEMPLATE (tinfo);
4871
4872 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4873 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4874 && DECL_TEMPLATE_SPECIALIZATION (decl)
4875 && DECL_MEMBER_TEMPLATE_P (tmpl))
4876 {
4877 tree new_tmpl;
4878
4879 /* The declaration is a specialization of a member
4880 template, declared outside the class. Therefore, the
4881 innermost template arguments will be NULL, so we
4882 replace them with the arguments determined by the
4883 earlier call to check_explicit_specialization. */
4884 args = DECL_TI_ARGS (decl);
4885
4886 new_tmpl
4887 = build_template_decl (decl, current_template_parms,
4888 member_template_p);
4889 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4890 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4891 DECL_TI_TEMPLATE (decl) = new_tmpl;
4892 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4893 DECL_TEMPLATE_INFO (new_tmpl)
4894 = build_template_info (tmpl, args);
4895
4896 register_specialization (new_tmpl,
4897 most_general_template (tmpl),
4898 args,
4899 is_friend, 0);
4900 return decl;
4901 }
4902
4903 /* Make sure the template headers we got make sense. */
4904
4905 parms = DECL_TEMPLATE_PARMS (tmpl);
4906 i = TMPL_PARMS_DEPTH (parms);
4907 if (TMPL_ARGS_DEPTH (args) != i)
4908 {
4909 error ("expected %d levels of template parms for %q#D, got %d",
4910 i, decl, TMPL_ARGS_DEPTH (args));
4911 }
4912 else
4913 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4914 {
4915 a = TMPL_ARGS_LEVEL (args, i);
4916 t = INNERMOST_TEMPLATE_PARMS (parms);
4917
4918 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4919 {
4920 if (current == decl)
4921 error ("got %d template parameters for %q#D",
4922 TREE_VEC_LENGTH (a), decl);
4923 else
4924 error ("got %d template parameters for %q#T",
4925 TREE_VEC_LENGTH (a), current);
4926 error (" but %d required", TREE_VEC_LENGTH (t));
4927 /* Avoid crash in import_export_decl. */
4928 DECL_INTERFACE_KNOWN (decl) = 1;
4929 return error_mark_node;
4930 }
4931
4932 if (current == decl)
4933 current = ctx;
4934 else if (current == NULL_TREE)
4935 /* Can happen in erroneous input. */
4936 break;
4937 else
4938 current = get_containing_scope (current);
4939 }
4940
4941 /* Check that the parms are used in the appropriate qualifying scopes
4942 in the declarator. */
4943 if (!comp_template_args
4944 (TI_ARGS (tinfo),
4945 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4946 {
4947 error ("\
4948 template arguments to %qD do not match original template %qD",
4949 decl, DECL_TEMPLATE_RESULT (tmpl));
4950 if (!uses_template_parms (TI_ARGS (tinfo)))
4951 inform (input_location, "use template<> for an explicit specialization");
4952 /* Avoid crash in import_export_decl. */
4953 DECL_INTERFACE_KNOWN (decl) = 1;
4954 return error_mark_node;
4955 }
4956 }
4957
4958 DECL_TEMPLATE_RESULT (tmpl) = decl;
4959 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4960
4961 /* Push template declarations for global functions and types. Note
4962 that we do not try to push a global template friend declared in a
4963 template class; such a thing may well depend on the template
4964 parameters of the class. */
4965 if (new_template_p && !ctx
4966 && !(is_friend && template_class_depth (current_class_type) > 0))
4967 {
4968 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4969 if (tmpl == error_mark_node)
4970 return error_mark_node;
4971
4972 /* Hide template friend classes that haven't been declared yet. */
4973 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4974 {
4975 DECL_ANTICIPATED (tmpl) = 1;
4976 DECL_FRIEND_P (tmpl) = 1;
4977 }
4978 }
4979
4980 if (is_primary)
4981 {
4982 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4983 int i;
4984
4985 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4986 if (DECL_CONV_FN_P (tmpl))
4987 {
4988 int depth = TMPL_PARMS_DEPTH (parms);
4989
4990 /* It is a conversion operator. See if the type converted to
4991 depends on innermost template operands. */
4992
4993 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4994 depth))
4995 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4996 }
4997
4998 /* Give template template parms a DECL_CONTEXT of the template
4999 for which they are a parameter. */
5000 parms = INNERMOST_TEMPLATE_PARMS (parms);
5001 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5002 {
5003 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5004 if (TREE_CODE (parm) == TEMPLATE_DECL)
5005 DECL_CONTEXT (parm) = tmpl;
5006 }
5007 }
5008
5009 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5010 back to its most general template. If TMPL is a specialization,
5011 ARGS may only have the innermost set of arguments. Add the missing
5012 argument levels if necessary. */
5013 if (DECL_TEMPLATE_INFO (tmpl))
5014 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5015
5016 info = build_template_info (tmpl, args);
5017
5018 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5019 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5020 else
5021 {
5022 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5023 retrofit_lang_decl (decl);
5024 if (DECL_LANG_SPECIFIC (decl))
5025 DECL_TEMPLATE_INFO (decl) = info;
5026 }
5027
5028 return DECL_TEMPLATE_RESULT (tmpl);
5029 }
5030
5031 tree
5032 push_template_decl (tree decl)
5033 {
5034 return push_template_decl_real (decl, false);
5035 }
5036
5037 /* FN is an inheriting constructor that inherits from the constructor
5038 template INHERITED; turn FN into a constructor template with a matching
5039 template header. */
5040
5041 tree
5042 add_inherited_template_parms (tree fn, tree inherited)
5043 {
5044 tree inner_parms
5045 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5046 inner_parms = copy_node (inner_parms);
5047 tree parms
5048 = tree_cons (size_int (processing_template_decl + 1),
5049 inner_parms, current_template_parms);
5050 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5051 tree args = template_parms_to_args (parms);
5052 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5053 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5054 DECL_TEMPLATE_RESULT (tmpl) = fn;
5055 DECL_ARTIFICIAL (tmpl) = true;
5056 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5057 return tmpl;
5058 }
5059
5060 /* Called when a class template TYPE is redeclared with the indicated
5061 template PARMS, e.g.:
5062
5063 template <class T> struct S;
5064 template <class T> struct S {}; */
5065
5066 bool
5067 redeclare_class_template (tree type, tree parms)
5068 {
5069 tree tmpl;
5070 tree tmpl_parms;
5071 int i;
5072
5073 if (!TYPE_TEMPLATE_INFO (type))
5074 {
5075 error ("%qT is not a template type", type);
5076 return false;
5077 }
5078
5079 tmpl = TYPE_TI_TEMPLATE (type);
5080 if (!PRIMARY_TEMPLATE_P (tmpl))
5081 /* The type is nested in some template class. Nothing to worry
5082 about here; there are no new template parameters for the nested
5083 type. */
5084 return true;
5085
5086 if (!parms)
5087 {
5088 error ("template specifiers not specified in declaration of %qD",
5089 tmpl);
5090 return false;
5091 }
5092
5093 parms = INNERMOST_TEMPLATE_PARMS (parms);
5094 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5095
5096 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5097 {
5098 error_n (input_location, TREE_VEC_LENGTH (parms),
5099 "redeclared with %d template parameter",
5100 "redeclared with %d template parameters",
5101 TREE_VEC_LENGTH (parms));
5102 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5103 "previous declaration %q+D used %d template parameter",
5104 "previous declaration %q+D used %d template parameters",
5105 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5106 return false;
5107 }
5108
5109 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5110 {
5111 tree tmpl_parm;
5112 tree parm;
5113 tree tmpl_default;
5114 tree parm_default;
5115
5116 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5117 || TREE_VEC_ELT (parms, i) == error_mark_node)
5118 continue;
5119
5120 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5121 if (tmpl_parm == error_mark_node)
5122 return false;
5123
5124 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5125 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5126 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5127
5128 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5129 TEMPLATE_DECL. */
5130 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5131 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5132 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5133 || (TREE_CODE (tmpl_parm) != PARM_DECL
5134 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5135 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5136 || (TREE_CODE (tmpl_parm) == PARM_DECL
5137 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5138 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5139 {
5140 error ("template parameter %q+#D", tmpl_parm);
5141 error ("redeclared here as %q#D", parm);
5142 return false;
5143 }
5144
5145 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5146 {
5147 /* We have in [temp.param]:
5148
5149 A template-parameter may not be given default arguments
5150 by two different declarations in the same scope. */
5151 error_at (input_location, "redefinition of default argument for %q#D", parm);
5152 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5153 "original definition appeared here");
5154 return false;
5155 }
5156
5157 if (parm_default != NULL_TREE)
5158 /* Update the previous template parameters (which are the ones
5159 that will really count) with the new default value. */
5160 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5161 else if (tmpl_default != NULL_TREE)
5162 /* Update the new parameters, too; they'll be used as the
5163 parameters for any members. */
5164 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5165 }
5166
5167 return true;
5168 }
5169
5170 /* Simplify EXPR if it is a non-dependent expression. Returns the
5171 (possibly simplified) expression. */
5172
5173 tree
5174 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5175 {
5176 if (expr == NULL_TREE)
5177 return NULL_TREE;
5178
5179 /* If we're in a template, but EXPR isn't value dependent, simplify
5180 it. We're supposed to treat:
5181
5182 template <typename T> void f(T[1 + 1]);
5183 template <typename T> void f(T[2]);
5184
5185 as two declarations of the same function, for example. */
5186 if (processing_template_decl
5187 && !instantiation_dependent_expression_p (expr)
5188 && potential_constant_expression (expr))
5189 {
5190 HOST_WIDE_INT saved_processing_template_decl;
5191
5192 saved_processing_template_decl = processing_template_decl;
5193 processing_template_decl = 0;
5194 expr = tsubst_copy_and_build (expr,
5195 /*args=*/NULL_TREE,
5196 complain,
5197 /*in_decl=*/NULL_TREE,
5198 /*function_p=*/false,
5199 /*integral_constant_expression_p=*/true);
5200 processing_template_decl = saved_processing_template_decl;
5201 }
5202 return expr;
5203 }
5204
5205 tree
5206 fold_non_dependent_expr (tree expr)
5207 {
5208 return fold_non_dependent_expr_sfinae (expr, tf_error);
5209 }
5210
5211 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5212 template declaration, or a TYPE_DECL for an alias declaration. */
5213
5214 bool
5215 alias_type_or_template_p (tree t)
5216 {
5217 if (t == NULL_TREE)
5218 return false;
5219 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5220 || (TYPE_P (t)
5221 && TYPE_NAME (t)
5222 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5223 || DECL_ALIAS_TEMPLATE_P (t));
5224 }
5225
5226 /* Return TRUE iff is a specialization of an alias template. */
5227
5228 bool
5229 alias_template_specialization_p (const_tree t)
5230 {
5231 if (t == NULL_TREE)
5232 return false;
5233
5234 return (TYPE_P (t)
5235 && TYPE_TEMPLATE_INFO (t)
5236 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5237 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5238 }
5239
5240 /* Return the number of innermost template parameters in TMPL. */
5241
5242 static int
5243 num_innermost_template_parms (tree tmpl)
5244 {
5245 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5246 return TREE_VEC_LENGTH (parms);
5247 }
5248
5249 /* Return either TMPL or another template that it is equivalent to under DR
5250 1286: An alias that just changes the name of a template is equivalent to
5251 the other template. */
5252
5253 static tree
5254 get_underlying_template (tree tmpl)
5255 {
5256 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5257 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5258 {
5259 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5260 if (TYPE_TEMPLATE_INFO (result))
5261 {
5262 tree sub = TYPE_TI_TEMPLATE (result);
5263 if (PRIMARY_TEMPLATE_P (sub)
5264 && (num_innermost_template_parms (tmpl)
5265 == num_innermost_template_parms (sub)))
5266 {
5267 tree alias_args = INNERMOST_TEMPLATE_ARGS
5268 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5269 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5270 break;
5271 /* The alias type is equivalent to the pattern of the
5272 underlying template, so strip the alias. */
5273 tmpl = sub;
5274 continue;
5275 }
5276 }
5277 break;
5278 }
5279 return tmpl;
5280 }
5281
5282 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5283 must be a function or a pointer-to-function type, as specified
5284 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5285 and check that the resulting function has external linkage. */
5286
5287 static tree
5288 convert_nontype_argument_function (tree type, tree expr)
5289 {
5290 tree fns = expr;
5291 tree fn, fn_no_ptr;
5292 linkage_kind linkage;
5293
5294 fn = instantiate_type (type, fns, tf_none);
5295 if (fn == error_mark_node)
5296 return error_mark_node;
5297
5298 fn_no_ptr = fn;
5299 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5300 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5301 if (BASELINK_P (fn_no_ptr))
5302 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5303
5304 /* [temp.arg.nontype]/1
5305
5306 A template-argument for a non-type, non-template template-parameter
5307 shall be one of:
5308 [...]
5309 -- the address of an object or function with external [C++11: or
5310 internal] linkage. */
5311
5312 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5313 {
5314 error ("%qE is not a valid template argument for type %qT", expr, type);
5315 if (TYPE_PTR_P (type))
5316 error ("it must be the address of a function with external linkage");
5317 else
5318 error ("it must be the name of a function with external linkage");
5319 return NULL_TREE;
5320 }
5321
5322 linkage = decl_linkage (fn_no_ptr);
5323 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5324 {
5325 if (cxx_dialect >= cxx11)
5326 error ("%qE is not a valid template argument for type %qT "
5327 "because %qD has no linkage",
5328 expr, type, fn_no_ptr);
5329 else
5330 error ("%qE is not a valid template argument for type %qT "
5331 "because %qD does not have external linkage",
5332 expr, type, fn_no_ptr);
5333 return NULL_TREE;
5334 }
5335
5336 return fn;
5337 }
5338
5339 /* Subroutine of convert_nontype_argument.
5340 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5341 Emit an error otherwise. */
5342
5343 static bool
5344 check_valid_ptrmem_cst_expr (tree type, tree expr,
5345 tsubst_flags_t complain)
5346 {
5347 STRIP_NOPS (expr);
5348 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5349 return true;
5350 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5351 return true;
5352 if (complain & tf_error)
5353 {
5354 error ("%qE is not a valid template argument for type %qT",
5355 expr, type);
5356 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5357 }
5358 return false;
5359 }
5360
5361 /* Returns TRUE iff the address of OP is value-dependent.
5362
5363 14.6.2.4 [temp.dep.temp]:
5364 A non-integral non-type template-argument is dependent if its type is
5365 dependent or it has either of the following forms
5366 qualified-id
5367 & qualified-id
5368 and contains a nested-name-specifier which specifies a class-name that
5369 names a dependent type.
5370
5371 We generalize this to just say that the address of a member of a
5372 dependent class is value-dependent; the above doesn't cover the
5373 address of a static data member named with an unqualified-id. */
5374
5375 static bool
5376 has_value_dependent_address (tree op)
5377 {
5378 /* We could use get_inner_reference here, but there's no need;
5379 this is only relevant for template non-type arguments, which
5380 can only be expressed as &id-expression. */
5381 if (DECL_P (op))
5382 {
5383 tree ctx = CP_DECL_CONTEXT (op);
5384 if (TYPE_P (ctx) && dependent_type_p (ctx))
5385 return true;
5386 }
5387
5388 return false;
5389 }
5390
5391 /* The next set of functions are used for providing helpful explanatory
5392 diagnostics for failed overload resolution. Their messages should be
5393 indented by two spaces for consistency with the messages in
5394 call.c */
5395
5396 static int
5397 unify_success (bool /*explain_p*/)
5398 {
5399 return 0;
5400 }
5401
5402 static int
5403 unify_parameter_deduction_failure (bool explain_p, tree parm)
5404 {
5405 if (explain_p)
5406 inform (input_location,
5407 " couldn't deduce template parameter %qD", parm);
5408 return 1;
5409 }
5410
5411 static int
5412 unify_invalid (bool /*explain_p*/)
5413 {
5414 return 1;
5415 }
5416
5417 static int
5418 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5419 {
5420 if (explain_p)
5421 inform (input_location,
5422 " types %qT and %qT have incompatible cv-qualifiers",
5423 parm, arg);
5424 return 1;
5425 }
5426
5427 static int
5428 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5429 {
5430 if (explain_p)
5431 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5432 return 1;
5433 }
5434
5435 static int
5436 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5437 {
5438 if (explain_p)
5439 inform (input_location,
5440 " template parameter %qD is not a parameter pack, but "
5441 "argument %qD is",
5442 parm, arg);
5443 return 1;
5444 }
5445
5446 static int
5447 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5448 {
5449 if (explain_p)
5450 inform (input_location,
5451 " template argument %qE does not match "
5452 "pointer-to-member constant %qE",
5453 arg, parm);
5454 return 1;
5455 }
5456
5457 static int
5458 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5459 {
5460 if (explain_p)
5461 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5462 return 1;
5463 }
5464
5465 static int
5466 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5467 {
5468 if (explain_p)
5469 inform (input_location,
5470 " inconsistent parameter pack deduction with %qT and %qT",
5471 old_arg, new_arg);
5472 return 1;
5473 }
5474
5475 static int
5476 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5477 {
5478 if (explain_p)
5479 {
5480 if (TYPE_P (parm))
5481 inform (input_location,
5482 " deduced conflicting types for parameter %qT (%qT and %qT)",
5483 parm, first, second);
5484 else
5485 inform (input_location,
5486 " deduced conflicting values for non-type parameter "
5487 "%qE (%qE and %qE)", parm, first, second);
5488 }
5489 return 1;
5490 }
5491
5492 static int
5493 unify_vla_arg (bool explain_p, tree arg)
5494 {
5495 if (explain_p)
5496 inform (input_location,
5497 " variable-sized array type %qT is not "
5498 "a valid template argument",
5499 arg);
5500 return 1;
5501 }
5502
5503 static int
5504 unify_method_type_error (bool explain_p, tree arg)
5505 {
5506 if (explain_p)
5507 inform (input_location,
5508 " member function type %qT is not a valid template argument",
5509 arg);
5510 return 1;
5511 }
5512
5513 static int
5514 unify_arity (bool explain_p, int have, int wanted)
5515 {
5516 if (explain_p)
5517 inform_n (input_location, wanted,
5518 " candidate expects %d argument, %d provided",
5519 " candidate expects %d arguments, %d provided",
5520 wanted, have);
5521 return 1;
5522 }
5523
5524 static int
5525 unify_too_many_arguments (bool explain_p, int have, int wanted)
5526 {
5527 return unify_arity (explain_p, have, wanted);
5528 }
5529
5530 static int
5531 unify_too_few_arguments (bool explain_p, int have, int wanted)
5532 {
5533 return unify_arity (explain_p, have, wanted);
5534 }
5535
5536 static int
5537 unify_arg_conversion (bool explain_p, tree to_type,
5538 tree from_type, tree arg)
5539 {
5540 if (explain_p)
5541 inform (EXPR_LOC_OR_LOC (arg, input_location),
5542 " cannot convert %qE (type %qT) to type %qT",
5543 arg, from_type, to_type);
5544 return 1;
5545 }
5546
5547 static int
5548 unify_no_common_base (bool explain_p, enum template_base_result r,
5549 tree parm, tree arg)
5550 {
5551 if (explain_p)
5552 switch (r)
5553 {
5554 case tbr_ambiguous_baseclass:
5555 inform (input_location, " %qT is an ambiguous base class of %qT",
5556 parm, arg);
5557 break;
5558 default:
5559 inform (input_location, " %qT is not derived from %qT", arg, parm);
5560 break;
5561 }
5562 return 1;
5563 }
5564
5565 static int
5566 unify_inconsistent_template_template_parameters (bool explain_p)
5567 {
5568 if (explain_p)
5569 inform (input_location,
5570 " template parameters of a template template argument are "
5571 "inconsistent with other deduced template arguments");
5572 return 1;
5573 }
5574
5575 static int
5576 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5577 {
5578 if (explain_p)
5579 inform (input_location,
5580 " can't deduce a template for %qT from non-template type %qT",
5581 parm, arg);
5582 return 1;
5583 }
5584
5585 static int
5586 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5587 {
5588 if (explain_p)
5589 inform (input_location,
5590 " template argument %qE does not match %qD", arg, parm);
5591 return 1;
5592 }
5593
5594 static int
5595 unify_overload_resolution_failure (bool explain_p, tree arg)
5596 {
5597 if (explain_p)
5598 inform (input_location,
5599 " could not resolve address from overloaded function %qE",
5600 arg);
5601 return 1;
5602 }
5603
5604 /* Attempt to convert the non-type template parameter EXPR to the
5605 indicated TYPE. If the conversion is successful, return the
5606 converted value. If the conversion is unsuccessful, return
5607 NULL_TREE if we issued an error message, or error_mark_node if we
5608 did not. We issue error messages for out-and-out bad template
5609 parameters, but not simply because the conversion failed, since we
5610 might be just trying to do argument deduction. Both TYPE and EXPR
5611 must be non-dependent.
5612
5613 The conversion follows the special rules described in
5614 [temp.arg.nontype], and it is much more strict than an implicit
5615 conversion.
5616
5617 This function is called twice for each template argument (see
5618 lookup_template_class for a more accurate description of this
5619 problem). This means that we need to handle expressions which
5620 are not valid in a C++ source, but can be created from the
5621 first call (for instance, casts to perform conversions). These
5622 hacks can go away after we fix the double coercion problem. */
5623
5624 static tree
5625 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5626 {
5627 tree expr_type;
5628
5629 /* Detect immediately string literals as invalid non-type argument.
5630 This special-case is not needed for correctness (we would easily
5631 catch this later), but only to provide better diagnostic for this
5632 common user mistake. As suggested by DR 100, we do not mention
5633 linkage issues in the diagnostic as this is not the point. */
5634 /* FIXME we're making this OK. */
5635 if (TREE_CODE (expr) == STRING_CST)
5636 {
5637 if (complain & tf_error)
5638 error ("%qE is not a valid template argument for type %qT "
5639 "because string literals can never be used in this context",
5640 expr, type);
5641 return NULL_TREE;
5642 }
5643
5644 /* Add the ADDR_EXPR now for the benefit of
5645 value_dependent_expression_p. */
5646 if (TYPE_PTROBV_P (type)
5647 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5648 {
5649 expr = decay_conversion (expr, complain);
5650 if (expr == error_mark_node)
5651 return error_mark_node;
5652 }
5653
5654 /* If we are in a template, EXPR may be non-dependent, but still
5655 have a syntactic, rather than semantic, form. For example, EXPR
5656 might be a SCOPE_REF, rather than the VAR_DECL to which the
5657 SCOPE_REF refers. Preserving the qualifying scope is necessary
5658 so that access checking can be performed when the template is
5659 instantiated -- but here we need the resolved form so that we can
5660 convert the argument. */
5661 if (TYPE_REF_OBJ_P (type)
5662 && has_value_dependent_address (expr))
5663 /* If we want the address and it's value-dependent, don't fold. */;
5664 else if (!type_unknown_p (expr))
5665 expr = fold_non_dependent_expr_sfinae (expr, complain);
5666 if (error_operand_p (expr))
5667 return error_mark_node;
5668 expr_type = TREE_TYPE (expr);
5669 if (TREE_CODE (type) == REFERENCE_TYPE)
5670 expr = mark_lvalue_use (expr);
5671 else
5672 expr = mark_rvalue_use (expr);
5673
5674 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5675 to a non-type argument of "nullptr". */
5676 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5677 expr = convert (type, expr);
5678
5679 /* In C++11, integral or enumeration non-type template arguments can be
5680 arbitrary constant expressions. Pointer and pointer to
5681 member arguments can be general constant expressions that evaluate
5682 to a null value, but otherwise still need to be of a specific form. */
5683 if (cxx_dialect >= cxx11)
5684 {
5685 if (TREE_CODE (expr) == PTRMEM_CST)
5686 /* A PTRMEM_CST is already constant, and a valid template
5687 argument for a parameter of pointer to member type, we just want
5688 to leave it in that form rather than lower it to a
5689 CONSTRUCTOR. */;
5690 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5691 expr = maybe_constant_value (expr);
5692 else if (TYPE_PTR_OR_PTRMEM_P (type))
5693 {
5694 tree folded = maybe_constant_value (expr);
5695 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5696 : null_member_pointer_value_p (folded))
5697 expr = folded;
5698 }
5699 }
5700
5701 /* HACK: Due to double coercion, we can get a
5702 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5703 which is the tree that we built on the first call (see
5704 below when coercing to reference to object or to reference to
5705 function). We just strip everything and get to the arg.
5706 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5707 for examples. */
5708 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5709 {
5710 tree probe_type, probe = expr;
5711 if (REFERENCE_REF_P (probe))
5712 probe = TREE_OPERAND (probe, 0);
5713 probe_type = TREE_TYPE (probe);
5714 if (TREE_CODE (probe) == NOP_EXPR)
5715 {
5716 /* ??? Maybe we could use convert_from_reference here, but we
5717 would need to relax its constraints because the NOP_EXPR
5718 could actually change the type to something more cv-qualified,
5719 and this is not folded by convert_from_reference. */
5720 tree addr = TREE_OPERAND (probe, 0);
5721 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5722 && TREE_CODE (addr) == ADDR_EXPR
5723 && TYPE_PTR_P (TREE_TYPE (addr))
5724 && (same_type_ignoring_top_level_qualifiers_p
5725 (TREE_TYPE (probe_type),
5726 TREE_TYPE (TREE_TYPE (addr)))))
5727 {
5728 expr = TREE_OPERAND (addr, 0);
5729 expr_type = TREE_TYPE (probe_type);
5730 }
5731 }
5732 }
5733
5734 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5735 parameter is a pointer to object, through decay and
5736 qualification conversion. Let's strip everything. */
5737 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5738 {
5739 tree probe = expr;
5740 STRIP_NOPS (probe);
5741 if (TREE_CODE (probe) == ADDR_EXPR
5742 && TYPE_PTR_P (TREE_TYPE (probe)))
5743 {
5744 /* Skip the ADDR_EXPR only if it is part of the decay for
5745 an array. Otherwise, it is part of the original argument
5746 in the source code. */
5747 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5748 probe = TREE_OPERAND (probe, 0);
5749 expr = probe;
5750 expr_type = TREE_TYPE (expr);
5751 }
5752 }
5753
5754 /* [temp.arg.nontype]/5, bullet 1
5755
5756 For a non-type template-parameter of integral or enumeration type,
5757 integral promotions (_conv.prom_) and integral conversions
5758 (_conv.integral_) are applied. */
5759 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5760 {
5761 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5762 t = maybe_constant_value (t);
5763 if (t != error_mark_node)
5764 expr = t;
5765
5766 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5767 return error_mark_node;
5768
5769 /* Notice that there are constant expressions like '4 % 0' which
5770 do not fold into integer constants. */
5771 if (TREE_CODE (expr) != INTEGER_CST)
5772 {
5773 if (complain & tf_error)
5774 {
5775 int errs = errorcount, warns = warningcount + werrorcount;
5776 if (processing_template_decl
5777 && !require_potential_constant_expression (expr))
5778 return NULL_TREE;
5779 expr = cxx_constant_value (expr);
5780 if (errorcount > errs || warningcount + werrorcount > warns)
5781 inform (EXPR_LOC_OR_LOC (expr, input_location),
5782 "in template argument for type %qT ", type);
5783 if (expr == error_mark_node)
5784 return NULL_TREE;
5785 /* else cxx_constant_value complained but gave us
5786 a real constant, so go ahead. */
5787 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5788 }
5789 else
5790 return NULL_TREE;
5791 }
5792
5793 /* Avoid typedef problems. */
5794 if (TREE_TYPE (expr) != type)
5795 expr = fold_convert (type, expr);
5796 }
5797 /* [temp.arg.nontype]/5, bullet 2
5798
5799 For a non-type template-parameter of type pointer to object,
5800 qualification conversions (_conv.qual_) and the array-to-pointer
5801 conversion (_conv.array_) are applied. */
5802 else if (TYPE_PTROBV_P (type))
5803 {
5804 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5805
5806 A template-argument for a non-type, non-template template-parameter
5807 shall be one of: [...]
5808
5809 -- the name of a non-type template-parameter;
5810 -- the address of an object or function with external linkage, [...]
5811 expressed as "& id-expression" where the & is optional if the name
5812 refers to a function or array, or if the corresponding
5813 template-parameter is a reference.
5814
5815 Here, we do not care about functions, as they are invalid anyway
5816 for a parameter of type pointer-to-object. */
5817
5818 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5819 /* Non-type template parameters are OK. */
5820 ;
5821 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5822 /* Null pointer values are OK in C++11. */;
5823 else if (TREE_CODE (expr) != ADDR_EXPR
5824 && TREE_CODE (expr_type) != ARRAY_TYPE)
5825 {
5826 if (VAR_P (expr))
5827 {
5828 error ("%qD is not a valid template argument "
5829 "because %qD is a variable, not the address of "
5830 "a variable",
5831 expr, expr);
5832 return NULL_TREE;
5833 }
5834 if (POINTER_TYPE_P (expr_type))
5835 {
5836 error ("%qE is not a valid template argument for %qT "
5837 "because it is not the address of a variable",
5838 expr, type);
5839 return NULL_TREE;
5840 }
5841 /* Other values, like integer constants, might be valid
5842 non-type arguments of some other type. */
5843 return error_mark_node;
5844 }
5845 else
5846 {
5847 tree decl;
5848
5849 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5850 ? TREE_OPERAND (expr, 0) : expr);
5851 if (!VAR_P (decl))
5852 {
5853 error ("%qE is not a valid template argument of type %qT "
5854 "because %qE is not a variable",
5855 expr, type, decl);
5856 return NULL_TREE;
5857 }
5858 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5859 {
5860 error ("%qE is not a valid template argument of type %qT "
5861 "because %qD does not have external linkage",
5862 expr, type, decl);
5863 return NULL_TREE;
5864 }
5865 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5866 {
5867 error ("%qE is not a valid template argument of type %qT "
5868 "because %qD has no linkage",
5869 expr, type, decl);
5870 return NULL_TREE;
5871 }
5872 }
5873
5874 expr = decay_conversion (expr, complain);
5875 if (expr == error_mark_node)
5876 return error_mark_node;
5877
5878 expr = perform_qualification_conversions (type, expr);
5879 if (expr == error_mark_node)
5880 return error_mark_node;
5881 }
5882 /* [temp.arg.nontype]/5, bullet 3
5883
5884 For a non-type template-parameter of type reference to object, no
5885 conversions apply. The type referred to by the reference may be more
5886 cv-qualified than the (otherwise identical) type of the
5887 template-argument. The template-parameter is bound directly to the
5888 template-argument, which must be an lvalue. */
5889 else if (TYPE_REF_OBJ_P (type))
5890 {
5891 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5892 expr_type))
5893 return error_mark_node;
5894
5895 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5896 {
5897 error ("%qE is not a valid template argument for type %qT "
5898 "because of conflicts in cv-qualification", expr, type);
5899 return NULL_TREE;
5900 }
5901
5902 if (!real_lvalue_p (expr))
5903 {
5904 error ("%qE is not a valid template argument for type %qT "
5905 "because it is not an lvalue", expr, type);
5906 return NULL_TREE;
5907 }
5908
5909 /* [temp.arg.nontype]/1
5910
5911 A template-argument for a non-type, non-template template-parameter
5912 shall be one of: [...]
5913
5914 -- the address of an object or function with external linkage. */
5915 if (INDIRECT_REF_P (expr)
5916 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5917 {
5918 expr = TREE_OPERAND (expr, 0);
5919 if (DECL_P (expr))
5920 {
5921 error ("%q#D is not a valid template argument for type %qT "
5922 "because a reference variable does not have a constant "
5923 "address", expr, type);
5924 return NULL_TREE;
5925 }
5926 }
5927
5928 if (!DECL_P (expr))
5929 {
5930 error ("%qE is not a valid template argument for type %qT "
5931 "because it is not an object with external linkage",
5932 expr, type);
5933 return NULL_TREE;
5934 }
5935
5936 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5937 {
5938 error ("%qE is not a valid template argument for type %qT "
5939 "because object %qD has not external linkage",
5940 expr, type, expr);
5941 return NULL_TREE;
5942 }
5943
5944 expr = build_nop (type, build_address (expr));
5945 }
5946 /* [temp.arg.nontype]/5, bullet 4
5947
5948 For a non-type template-parameter of type pointer to function, only
5949 the function-to-pointer conversion (_conv.func_) is applied. If the
5950 template-argument represents a set of overloaded functions (or a
5951 pointer to such), the matching function is selected from the set
5952 (_over.over_). */
5953 else if (TYPE_PTRFN_P (type))
5954 {
5955 /* If the argument is a template-id, we might not have enough
5956 context information to decay the pointer. */
5957 if (!type_unknown_p (expr_type))
5958 {
5959 expr = decay_conversion (expr, complain);
5960 if (expr == error_mark_node)
5961 return error_mark_node;
5962 }
5963
5964 if (cxx_dialect >= cxx11 && integer_zerop (expr))
5965 /* Null pointer values are OK in C++11. */
5966 return perform_qualification_conversions (type, expr);
5967
5968 expr = convert_nontype_argument_function (type, expr);
5969 if (!expr || expr == error_mark_node)
5970 return expr;
5971 }
5972 /* [temp.arg.nontype]/5, bullet 5
5973
5974 For a non-type template-parameter of type reference to function, no
5975 conversions apply. If the template-argument represents a set of
5976 overloaded functions, the matching function is selected from the set
5977 (_over.over_). */
5978 else if (TYPE_REFFN_P (type))
5979 {
5980 if (TREE_CODE (expr) == ADDR_EXPR)
5981 {
5982 error ("%qE is not a valid template argument for type %qT "
5983 "because it is a pointer", expr, type);
5984 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5985 return NULL_TREE;
5986 }
5987
5988 expr = convert_nontype_argument_function (type, expr);
5989 if (!expr || expr == error_mark_node)
5990 return expr;
5991
5992 expr = build_nop (type, build_address (expr));
5993 }
5994 /* [temp.arg.nontype]/5, bullet 6
5995
5996 For a non-type template-parameter of type pointer to member function,
5997 no conversions apply. If the template-argument represents a set of
5998 overloaded member functions, the matching member function is selected
5999 from the set (_over.over_). */
6000 else if (TYPE_PTRMEMFUNC_P (type))
6001 {
6002 expr = instantiate_type (type, expr, tf_none);
6003 if (expr == error_mark_node)
6004 return error_mark_node;
6005
6006 /* [temp.arg.nontype] bullet 1 says the pointer to member
6007 expression must be a pointer-to-member constant. */
6008 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6009 return error_mark_node;
6010
6011 /* There is no way to disable standard conversions in
6012 resolve_address_of_overloaded_function (called by
6013 instantiate_type). It is possible that the call succeeded by
6014 converting &B::I to &D::I (where B is a base of D), so we need
6015 to reject this conversion here.
6016
6017 Actually, even if there was a way to disable standard conversions,
6018 it would still be better to reject them here so that we can
6019 provide a superior diagnostic. */
6020 if (!same_type_p (TREE_TYPE (expr), type))
6021 {
6022 error ("%qE is not a valid template argument for type %qT "
6023 "because it is of type %qT", expr, type,
6024 TREE_TYPE (expr));
6025 /* If we are just one standard conversion off, explain. */
6026 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6027 inform (input_location,
6028 "standard conversions are not allowed in this context");
6029 return NULL_TREE;
6030 }
6031 }
6032 /* [temp.arg.nontype]/5, bullet 7
6033
6034 For a non-type template-parameter of type pointer to data member,
6035 qualification conversions (_conv.qual_) are applied. */
6036 else if (TYPE_PTRDATAMEM_P (type))
6037 {
6038 /* [temp.arg.nontype] bullet 1 says the pointer to member
6039 expression must be a pointer-to-member constant. */
6040 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6041 return error_mark_node;
6042
6043 expr = perform_qualification_conversions (type, expr);
6044 if (expr == error_mark_node)
6045 return expr;
6046 }
6047 else if (NULLPTR_TYPE_P (type))
6048 {
6049 if (expr != nullptr_node)
6050 {
6051 error ("%qE is not a valid template argument for type %qT "
6052 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6053 return NULL_TREE;
6054 }
6055 return expr;
6056 }
6057 /* A template non-type parameter must be one of the above. */
6058 else
6059 gcc_unreachable ();
6060
6061 /* Sanity check: did we actually convert the argument to the
6062 right type? */
6063 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6064 (type, TREE_TYPE (expr)));
6065 return expr;
6066 }
6067
6068 /* Subroutine of coerce_template_template_parms, which returns 1 if
6069 PARM_PARM and ARG_PARM match using the rule for the template
6070 parameters of template template parameters. Both PARM and ARG are
6071 template parameters; the rest of the arguments are the same as for
6072 coerce_template_template_parms.
6073 */
6074 static int
6075 coerce_template_template_parm (tree parm,
6076 tree arg,
6077 tsubst_flags_t complain,
6078 tree in_decl,
6079 tree outer_args)
6080 {
6081 if (arg == NULL_TREE || arg == error_mark_node
6082 || parm == NULL_TREE || parm == error_mark_node)
6083 return 0;
6084
6085 if (TREE_CODE (arg) != TREE_CODE (parm))
6086 return 0;
6087
6088 switch (TREE_CODE (parm))
6089 {
6090 case TEMPLATE_DECL:
6091 /* We encounter instantiations of templates like
6092 template <template <template <class> class> class TT>
6093 class C; */
6094 {
6095 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6096 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6097
6098 if (!coerce_template_template_parms
6099 (parmparm, argparm, complain, in_decl, outer_args))
6100 return 0;
6101 }
6102 /* Fall through. */
6103
6104 case TYPE_DECL:
6105 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6106 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6107 /* Argument is a parameter pack but parameter is not. */
6108 return 0;
6109 break;
6110
6111 case PARM_DECL:
6112 /* The tsubst call is used to handle cases such as
6113
6114 template <int> class C {};
6115 template <class T, template <T> class TT> class D {};
6116 D<int, C> d;
6117
6118 i.e. the parameter list of TT depends on earlier parameters. */
6119 if (!uses_template_parms (TREE_TYPE (arg))
6120 && !same_type_p
6121 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6122 TREE_TYPE (arg)))
6123 return 0;
6124
6125 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6126 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6127 /* Argument is a parameter pack but parameter is not. */
6128 return 0;
6129
6130 break;
6131
6132 default:
6133 gcc_unreachable ();
6134 }
6135
6136 return 1;
6137 }
6138
6139
6140 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6141 template template parameters. Both PARM_PARMS and ARG_PARMS are
6142 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6143 or PARM_DECL.
6144
6145 Consider the example:
6146 template <class T> class A;
6147 template<template <class U> class TT> class B;
6148
6149 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6150 the parameters to A, and OUTER_ARGS contains A. */
6151
6152 static int
6153 coerce_template_template_parms (tree parm_parms,
6154 tree arg_parms,
6155 tsubst_flags_t complain,
6156 tree in_decl,
6157 tree outer_args)
6158 {
6159 int nparms, nargs, i;
6160 tree parm, arg;
6161 int variadic_p = 0;
6162
6163 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6164 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6165
6166 nparms = TREE_VEC_LENGTH (parm_parms);
6167 nargs = TREE_VEC_LENGTH (arg_parms);
6168
6169 /* Determine whether we have a parameter pack at the end of the
6170 template template parameter's template parameter list. */
6171 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6172 {
6173 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6174
6175 if (parm == error_mark_node)
6176 return 0;
6177
6178 switch (TREE_CODE (parm))
6179 {
6180 case TEMPLATE_DECL:
6181 case TYPE_DECL:
6182 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6183 variadic_p = 1;
6184 break;
6185
6186 case PARM_DECL:
6187 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6188 variadic_p = 1;
6189 break;
6190
6191 default:
6192 gcc_unreachable ();
6193 }
6194 }
6195
6196 if (nargs != nparms
6197 && !(variadic_p && nargs >= nparms - 1))
6198 return 0;
6199
6200 /* Check all of the template parameters except the parameter pack at
6201 the end (if any). */
6202 for (i = 0; i < nparms - variadic_p; ++i)
6203 {
6204 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6205 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6206 continue;
6207
6208 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6209 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6210
6211 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6212 outer_args))
6213 return 0;
6214
6215 }
6216
6217 if (variadic_p)
6218 {
6219 /* Check each of the template parameters in the template
6220 argument against the template parameter pack at the end of
6221 the template template parameter. */
6222 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6223 return 0;
6224
6225 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6226
6227 for (; i < nargs; ++i)
6228 {
6229 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6230 continue;
6231
6232 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6233
6234 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6235 outer_args))
6236 return 0;
6237 }
6238 }
6239
6240 return 1;
6241 }
6242
6243 /* Verifies that the deduced template arguments (in TARGS) for the
6244 template template parameters (in TPARMS) represent valid bindings,
6245 by comparing the template parameter list of each template argument
6246 to the template parameter list of its corresponding template
6247 template parameter, in accordance with DR150. This
6248 routine can only be called after all template arguments have been
6249 deduced. It will return TRUE if all of the template template
6250 parameter bindings are okay, FALSE otherwise. */
6251 bool
6252 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6253 {
6254 int i, ntparms = TREE_VEC_LENGTH (tparms);
6255 bool ret = true;
6256
6257 /* We're dealing with template parms in this process. */
6258 ++processing_template_decl;
6259
6260 targs = INNERMOST_TEMPLATE_ARGS (targs);
6261
6262 for (i = 0; i < ntparms; ++i)
6263 {
6264 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6265 tree targ = TREE_VEC_ELT (targs, i);
6266
6267 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6268 {
6269 tree packed_args = NULL_TREE;
6270 int idx, len = 1;
6271
6272 if (ARGUMENT_PACK_P (targ))
6273 {
6274 /* Look inside the argument pack. */
6275 packed_args = ARGUMENT_PACK_ARGS (targ);
6276 len = TREE_VEC_LENGTH (packed_args);
6277 }
6278
6279 for (idx = 0; idx < len; ++idx)
6280 {
6281 tree targ_parms = NULL_TREE;
6282
6283 if (packed_args)
6284 /* Extract the next argument from the argument
6285 pack. */
6286 targ = TREE_VEC_ELT (packed_args, idx);
6287
6288 if (PACK_EXPANSION_P (targ))
6289 /* Look at the pattern of the pack expansion. */
6290 targ = PACK_EXPANSION_PATTERN (targ);
6291
6292 /* Extract the template parameters from the template
6293 argument. */
6294 if (TREE_CODE (targ) == TEMPLATE_DECL)
6295 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6296 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6297 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6298
6299 /* Verify that we can coerce the template template
6300 parameters from the template argument to the template
6301 parameter. This requires an exact match. */
6302 if (targ_parms
6303 && !coerce_template_template_parms
6304 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6305 targ_parms,
6306 tf_none,
6307 tparm,
6308 targs))
6309 {
6310 ret = false;
6311 goto out;
6312 }
6313 }
6314 }
6315 }
6316
6317 out:
6318
6319 --processing_template_decl;
6320 return ret;
6321 }
6322
6323 /* Since type attributes aren't mangled, we need to strip them from
6324 template type arguments. */
6325
6326 static tree
6327 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6328 {
6329 tree mv;
6330 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6331 return arg;
6332 mv = TYPE_MAIN_VARIANT (arg);
6333 arg = strip_typedefs (arg);
6334 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6335 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6336 {
6337 if (complain & tf_warning)
6338 warning (0, "ignoring attributes on template argument %qT", arg);
6339 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6340 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6341 }
6342 return arg;
6343 }
6344
6345 /* Convert the indicated template ARG as necessary to match the
6346 indicated template PARM. Returns the converted ARG, or
6347 error_mark_node if the conversion was unsuccessful. Error and
6348 warning messages are issued under control of COMPLAIN. This
6349 conversion is for the Ith parameter in the parameter list. ARGS is
6350 the full set of template arguments deduced so far. */
6351
6352 static tree
6353 convert_template_argument (tree parm,
6354 tree arg,
6355 tree args,
6356 tsubst_flags_t complain,
6357 int i,
6358 tree in_decl)
6359 {
6360 tree orig_arg;
6361 tree val;
6362 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6363
6364 if (TREE_CODE (arg) == TREE_LIST
6365 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6366 {
6367 /* The template argument was the name of some
6368 member function. That's usually
6369 invalid, but static members are OK. In any
6370 case, grab the underlying fields/functions
6371 and issue an error later if required. */
6372 orig_arg = TREE_VALUE (arg);
6373 TREE_TYPE (arg) = unknown_type_node;
6374 }
6375
6376 orig_arg = arg;
6377
6378 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6379 requires_type = (TREE_CODE (parm) == TYPE_DECL
6380 || requires_tmpl_type);
6381
6382 /* When determining whether an argument pack expansion is a template,
6383 look at the pattern. */
6384 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6385 arg = PACK_EXPANSION_PATTERN (arg);
6386
6387 /* Deal with an injected-class-name used as a template template arg. */
6388 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6389 {
6390 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6391 if (TREE_CODE (t) == TEMPLATE_DECL)
6392 {
6393 if (cxx_dialect >= cxx11)
6394 /* OK under DR 1004. */;
6395 else if (complain & tf_warning_or_error)
6396 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6397 " used as template template argument", TYPE_NAME (arg));
6398 else if (flag_pedantic_errors)
6399 t = arg;
6400
6401 arg = t;
6402 }
6403 }
6404
6405 is_tmpl_type =
6406 ((TREE_CODE (arg) == TEMPLATE_DECL
6407 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6408 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6409 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6410 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6411
6412 if (is_tmpl_type
6413 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6414 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6415 arg = TYPE_STUB_DECL (arg);
6416
6417 is_type = TYPE_P (arg) || is_tmpl_type;
6418
6419 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6420 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6421 {
6422 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6423 {
6424 if (complain & tf_error)
6425 error ("invalid use of destructor %qE as a type", orig_arg);
6426 return error_mark_node;
6427 }
6428
6429 permerror (input_location,
6430 "to refer to a type member of a template parameter, "
6431 "use %<typename %E%>", orig_arg);
6432
6433 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6434 TREE_OPERAND (arg, 1),
6435 typename_type,
6436 complain);
6437 arg = orig_arg;
6438 is_type = 1;
6439 }
6440 if (is_type != requires_type)
6441 {
6442 if (in_decl)
6443 {
6444 if (complain & tf_error)
6445 {
6446 error ("type/value mismatch at argument %d in template "
6447 "parameter list for %qD",
6448 i + 1, in_decl);
6449 if (is_type)
6450 error (" expected a constant of type %qT, got %qT",
6451 TREE_TYPE (parm),
6452 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6453 else if (requires_tmpl_type)
6454 error (" expected a class template, got %qE", orig_arg);
6455 else
6456 error (" expected a type, got %qE", orig_arg);
6457 }
6458 }
6459 return error_mark_node;
6460 }
6461 if (is_tmpl_type ^ requires_tmpl_type)
6462 {
6463 if (in_decl && (complain & tf_error))
6464 {
6465 error ("type/value mismatch at argument %d in template "
6466 "parameter list for %qD",
6467 i + 1, in_decl);
6468 if (is_tmpl_type)
6469 error (" expected a type, got %qT", DECL_NAME (arg));
6470 else
6471 error (" expected a class template, got %qT", orig_arg);
6472 }
6473 return error_mark_node;
6474 }
6475
6476 if (is_type)
6477 {
6478 if (requires_tmpl_type)
6479 {
6480 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6481 val = orig_arg;
6482 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6483 /* The number of argument required is not known yet.
6484 Just accept it for now. */
6485 val = TREE_TYPE (arg);
6486 else
6487 {
6488 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6489 tree argparm;
6490
6491 /* Strip alias templates that are equivalent to another
6492 template. */
6493 arg = get_underlying_template (arg);
6494 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6495
6496 if (coerce_template_template_parms (parmparm, argparm,
6497 complain, in_decl,
6498 args))
6499 {
6500 val = arg;
6501
6502 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6503 TEMPLATE_DECL. */
6504 if (val != error_mark_node)
6505 {
6506 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6507 val = TREE_TYPE (val);
6508 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6509 val = make_pack_expansion (val);
6510 }
6511 }
6512 else
6513 {
6514 if (in_decl && (complain & tf_error))
6515 {
6516 error ("type/value mismatch at argument %d in "
6517 "template parameter list for %qD",
6518 i + 1, in_decl);
6519 error (" expected a template of type %qD, got %qT",
6520 parm, orig_arg);
6521 }
6522
6523 val = error_mark_node;
6524 }
6525 }
6526 }
6527 else
6528 val = orig_arg;
6529 /* We only form one instance of each template specialization.
6530 Therefore, if we use a non-canonical variant (i.e., a
6531 typedef), any future messages referring to the type will use
6532 the typedef, which is confusing if those future uses do not
6533 themselves also use the typedef. */
6534 if (TYPE_P (val))
6535 val = canonicalize_type_argument (val, complain);
6536 }
6537 else
6538 {
6539 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6540
6541 if (invalid_nontype_parm_type_p (t, complain))
6542 return error_mark_node;
6543
6544 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6545 {
6546 if (same_type_p (t, TREE_TYPE (orig_arg)))
6547 val = orig_arg;
6548 else
6549 {
6550 /* Not sure if this is reachable, but it doesn't hurt
6551 to be robust. */
6552 error ("type mismatch in nontype parameter pack");
6553 val = error_mark_node;
6554 }
6555 }
6556 else if (!dependent_template_arg_p (orig_arg)
6557 && !uses_template_parms (t))
6558 /* We used to call digest_init here. However, digest_init
6559 will report errors, which we don't want when complain
6560 is zero. More importantly, digest_init will try too
6561 hard to convert things: for example, `0' should not be
6562 converted to pointer type at this point according to
6563 the standard. Accepting this is not merely an
6564 extension, since deciding whether or not these
6565 conversions can occur is part of determining which
6566 function template to call, or whether a given explicit
6567 argument specification is valid. */
6568 val = convert_nontype_argument (t, orig_arg, complain);
6569 else
6570 val = strip_typedefs_expr (orig_arg);
6571
6572 if (val == NULL_TREE)
6573 val = error_mark_node;
6574 else if (val == error_mark_node && (complain & tf_error))
6575 error ("could not convert template argument %qE to %qT", orig_arg, t);
6576
6577 if (TREE_CODE (val) == SCOPE_REF)
6578 {
6579 /* Strip typedefs from the SCOPE_REF. */
6580 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6581 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6582 complain);
6583 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6584 QUALIFIED_NAME_IS_TEMPLATE (val));
6585 }
6586 }
6587
6588 return val;
6589 }
6590
6591 /* Coerces the remaining template arguments in INNER_ARGS (from
6592 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6593 Returns the coerced argument pack. PARM_IDX is the position of this
6594 parameter in the template parameter list. ARGS is the original
6595 template argument list. */
6596 static tree
6597 coerce_template_parameter_pack (tree parms,
6598 int parm_idx,
6599 tree args,
6600 tree inner_args,
6601 int arg_idx,
6602 tree new_args,
6603 int* lost,
6604 tree in_decl,
6605 tsubst_flags_t complain)
6606 {
6607 tree parm = TREE_VEC_ELT (parms, parm_idx);
6608 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6609 tree packed_args;
6610 tree argument_pack;
6611 tree packed_parms = NULL_TREE;
6612
6613 if (arg_idx > nargs)
6614 arg_idx = nargs;
6615
6616 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
6617 {
6618 /* When the template parameter is a non-type template parameter pack
6619 or template template parameter pack whose type or template
6620 parameters use parameter packs, we know exactly how many arguments
6621 we are looking for. Build a vector of the instantiated decls for
6622 these template parameters in PACKED_PARMS. */
6623 /* We can't use make_pack_expansion here because it would interpret a
6624 _DECL as a use rather than a declaration. */
6625 tree decl = TREE_VALUE (parm);
6626 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
6627 SET_PACK_EXPANSION_PATTERN (exp, decl);
6628 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
6629 SET_TYPE_STRUCTURAL_EQUALITY (exp);
6630
6631 TREE_VEC_LENGTH (args)--;
6632 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
6633 TREE_VEC_LENGTH (args)++;
6634
6635 if (packed_parms == error_mark_node)
6636 return error_mark_node;
6637
6638 /* If we're doing a partial instantiation of a member template,
6639 verify that all of the types used for the non-type
6640 template parameter pack are, in fact, valid for non-type
6641 template parameters. */
6642 if (arg_idx < nargs
6643 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6644 {
6645 int j, len = TREE_VEC_LENGTH (packed_parms);
6646 for (j = 0; j < len; ++j)
6647 {
6648 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
6649 if (invalid_nontype_parm_type_p (t, complain))
6650 return error_mark_node;
6651 }
6652 }
6653
6654 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
6655 }
6656 else
6657 packed_args = make_tree_vec (nargs - arg_idx);
6658
6659 /* Convert the remaining arguments, which will be a part of the
6660 parameter pack "parm". */
6661 for (; arg_idx < nargs; ++arg_idx)
6662 {
6663 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6664 tree actual_parm = TREE_VALUE (parm);
6665 int pack_idx = arg_idx - parm_idx;
6666
6667 if (packed_parms)
6668 {
6669 /* Once we've packed as many args as we have types, stop. */
6670 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
6671 break;
6672 else if (PACK_EXPANSION_P (arg))
6673 /* We don't know how many args we have yet, just
6674 use the unconverted ones for now. */
6675 return NULL_TREE;
6676 else
6677 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
6678 }
6679
6680 if (arg == error_mark_node)
6681 {
6682 if (complain & tf_error)
6683 error ("template argument %d is invalid", arg_idx + 1);
6684 }
6685 else
6686 arg = convert_template_argument (actual_parm,
6687 arg, new_args, complain, parm_idx,
6688 in_decl);
6689 if (arg == error_mark_node)
6690 (*lost)++;
6691 TREE_VEC_ELT (packed_args, pack_idx) = arg;
6692 }
6693
6694 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
6695 && TREE_VEC_LENGTH (packed_args) > 0)
6696 {
6697 error ("wrong number of template arguments (%d, should be %d)",
6698 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
6699 return error_mark_node;
6700 }
6701
6702 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6703 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6704 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6705 else
6706 {
6707 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6708 TREE_TYPE (argument_pack)
6709 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6710 TREE_CONSTANT (argument_pack) = 1;
6711 }
6712
6713 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6714 #ifdef ENABLE_CHECKING
6715 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6716 TREE_VEC_LENGTH (packed_args));
6717 #endif
6718 return argument_pack;
6719 }
6720
6721 /* Returns the number of pack expansions in the template argument vector
6722 ARGS. */
6723
6724 static int
6725 pack_expansion_args_count (tree args)
6726 {
6727 int i;
6728 int count = 0;
6729 if (args)
6730 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6731 {
6732 tree elt = TREE_VEC_ELT (args, i);
6733 if (elt && PACK_EXPANSION_P (elt))
6734 ++count;
6735 }
6736 return count;
6737 }
6738
6739 /* Convert all template arguments to their appropriate types, and
6740 return a vector containing the innermost resulting template
6741 arguments. If any error occurs, return error_mark_node. Error and
6742 warning messages are issued under control of COMPLAIN.
6743
6744 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6745 for arguments not specified in ARGS. Otherwise, if
6746 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6747 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6748 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6749 ARGS. */
6750
6751 static tree
6752 coerce_template_parms (tree parms,
6753 tree args,
6754 tree in_decl,
6755 tsubst_flags_t complain,
6756 bool require_all_args,
6757 bool use_default_args)
6758 {
6759 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6760 tree orig_inner_args;
6761 tree inner_args;
6762 tree new_args;
6763 tree new_inner_args;
6764 int saved_unevaluated_operand;
6765 int saved_inhibit_evaluation_warnings;
6766
6767 /* When used as a boolean value, indicates whether this is a
6768 variadic template parameter list. Since it's an int, we can also
6769 subtract it from nparms to get the number of non-variadic
6770 parameters. */
6771 int variadic_p = 0;
6772 int variadic_args_p = 0;
6773 int post_variadic_parms = 0;
6774
6775 if (args == error_mark_node)
6776 return error_mark_node;
6777
6778 nparms = TREE_VEC_LENGTH (parms);
6779
6780 /* Determine if there are any parameter packs. */
6781 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6782 {
6783 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6784 if (variadic_p)
6785 ++post_variadic_parms;
6786 if (template_parameter_pack_p (tparm))
6787 ++variadic_p;
6788 }
6789
6790 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
6791 /* If there are no parameters that follow a parameter pack, we need to
6792 expand any argument packs so that we can deduce a parameter pack from
6793 some non-packed args followed by an argument pack, as in variadic85.C.
6794 If there are such parameters, we need to leave argument packs intact
6795 so the arguments are assigned properly. This can happen when dealing
6796 with a nested class inside a partial specialization of a class
6797 template, as in variadic92.C, or when deducing a template parameter pack
6798 from a sub-declarator, as in variadic114.C. */
6799 if (!post_variadic_parms)
6800 inner_args = expand_template_argument_pack (inner_args);
6801
6802 /* Count any pack expansion args. */
6803 variadic_args_p = pack_expansion_args_count (inner_args);
6804
6805 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6806 if ((nargs > nparms && !variadic_p)
6807 || (nargs < nparms - variadic_p
6808 && require_all_args
6809 && !variadic_args_p
6810 && (!use_default_args
6811 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6812 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6813 {
6814 if (complain & tf_error)
6815 {
6816 if (variadic_p)
6817 {
6818 nparms -= variadic_p;
6819 error ("wrong number of template arguments "
6820 "(%d, should be %d or more)", nargs, nparms);
6821 }
6822 else
6823 error ("wrong number of template arguments "
6824 "(%d, should be %d)", nargs, nparms);
6825
6826 if (in_decl)
6827 error ("provided for %q+D", in_decl);
6828 }
6829
6830 return error_mark_node;
6831 }
6832 /* We can't pass a pack expansion to a non-pack parameter of an alias
6833 template (DR 1430). */
6834 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
6835 && variadic_args_p
6836 && nargs - variadic_args_p < nparms - variadic_p)
6837 {
6838 if (complain & tf_error)
6839 {
6840 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
6841 {
6842 tree arg = TREE_VEC_ELT (inner_args, i);
6843 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6844
6845 if (PACK_EXPANSION_P (arg)
6846 && !template_parameter_pack_p (parm))
6847 {
6848 error ("pack expansion argument for non-pack parameter "
6849 "%qD of alias template %qD", parm, in_decl);
6850 inform (DECL_SOURCE_LOCATION (parm), "declared here");
6851 goto found;
6852 }
6853 }
6854 gcc_unreachable ();
6855 found:;
6856 }
6857 return error_mark_node;
6858 }
6859
6860 /* We need to evaluate the template arguments, even though this
6861 template-id may be nested within a "sizeof". */
6862 saved_unevaluated_operand = cp_unevaluated_operand;
6863 cp_unevaluated_operand = 0;
6864 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6865 c_inhibit_evaluation_warnings = 0;
6866 new_inner_args = make_tree_vec (nparms);
6867 new_args = add_outermost_template_args (args, new_inner_args);
6868 int pack_adjust = 0;
6869 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6870 {
6871 tree arg;
6872 tree parm;
6873
6874 /* Get the Ith template parameter. */
6875 parm = TREE_VEC_ELT (parms, parm_idx);
6876
6877 if (parm == error_mark_node)
6878 {
6879 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6880 continue;
6881 }
6882
6883 /* Calculate the next argument. */
6884 if (arg_idx < nargs)
6885 arg = TREE_VEC_ELT (inner_args, arg_idx);
6886 else
6887 arg = NULL_TREE;
6888
6889 if (template_parameter_pack_p (TREE_VALUE (parm))
6890 && !(arg && ARGUMENT_PACK_P (arg)))
6891 {
6892 /* Some arguments will be placed in the
6893 template parameter pack PARM. */
6894 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6895 inner_args, arg_idx,
6896 new_args, &lost,
6897 in_decl, complain);
6898
6899 if (arg == NULL_TREE)
6900 {
6901 /* We don't know how many args we have yet, just use the
6902 unconverted (and still packed) ones for now. */
6903 new_inner_args = orig_inner_args;
6904 arg_idx = nargs;
6905 break;
6906 }
6907
6908 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6909
6910 /* Store this argument. */
6911 if (arg == error_mark_node)
6912 {
6913 lost++;
6914 /* We are done with all of the arguments. */
6915 arg_idx = nargs;
6916 }
6917 else
6918 {
6919 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
6920 arg_idx += pack_adjust;
6921 }
6922
6923 continue;
6924 }
6925 else if (arg)
6926 {
6927 if (PACK_EXPANSION_P (arg))
6928 {
6929 /* We don't know how many args we have yet, just
6930 use the unconverted ones for now. */
6931 new_inner_args = inner_args;
6932 arg_idx = nargs;
6933 break;
6934 }
6935 }
6936 else if (require_all_args)
6937 {
6938 /* There must be a default arg in this case. */
6939 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6940 complain, in_decl);
6941 /* The position of the first default template argument,
6942 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6943 Record that. */
6944 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6945 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6946 arg_idx - pack_adjust);
6947 }
6948 else
6949 break;
6950
6951 if (arg == error_mark_node)
6952 {
6953 if (complain & tf_error)
6954 error ("template argument %d is invalid", arg_idx + 1);
6955 }
6956 else if (!arg)
6957 /* This only occurs if there was an error in the template
6958 parameter list itself (which we would already have
6959 reported) that we are trying to recover from, e.g., a class
6960 template with a parameter list such as
6961 template<typename..., typename>. */
6962 ++lost;
6963 else
6964 arg = convert_template_argument (TREE_VALUE (parm),
6965 arg, new_args, complain,
6966 parm_idx, in_decl);
6967
6968 if (arg == error_mark_node)
6969 lost++;
6970 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
6971 }
6972 cp_unevaluated_operand = saved_unevaluated_operand;
6973 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6974
6975 if (variadic_p && arg_idx < nargs)
6976 {
6977 if (complain & tf_error)
6978 {
6979 error ("wrong number of template arguments "
6980 "(%d, should be %d)", nargs, arg_idx);
6981 if (in_decl)
6982 error ("provided for %q+D", in_decl);
6983 }
6984 return error_mark_node;
6985 }
6986
6987 if (lost)
6988 return error_mark_node;
6989
6990 #ifdef ENABLE_CHECKING
6991 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6992 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6993 TREE_VEC_LENGTH (new_inner_args));
6994 #endif
6995
6996 return new_inner_args;
6997 }
6998
6999 /* Like coerce_template_parms. If PARMS represents all template
7000 parameters levels, this function returns a vector of vectors
7001 representing all the resulting argument levels. Note that in this
7002 case, only the innermost arguments are coerced because the
7003 outermost ones are supposed to have been coerced already.
7004
7005 Otherwise, if PARMS represents only (the innermost) vector of
7006 parameters, this function returns a vector containing just the
7007 innermost resulting arguments. */
7008
7009 static tree
7010 coerce_innermost_template_parms (tree parms,
7011 tree args,
7012 tree in_decl,
7013 tsubst_flags_t complain,
7014 bool require_all_args,
7015 bool use_default_args)
7016 {
7017 int parms_depth = TMPL_PARMS_DEPTH (parms);
7018 int args_depth = TMPL_ARGS_DEPTH (args);
7019 tree coerced_args;
7020
7021 if (parms_depth > 1)
7022 {
7023 coerced_args = make_tree_vec (parms_depth);
7024 tree level;
7025 int cur_depth;
7026
7027 for (level = parms, cur_depth = parms_depth;
7028 parms_depth > 0 && level != NULL_TREE;
7029 level = TREE_CHAIN (level), --cur_depth)
7030 {
7031 tree l;
7032 if (cur_depth == args_depth)
7033 l = coerce_template_parms (TREE_VALUE (level),
7034 args, in_decl, complain,
7035 require_all_args,
7036 use_default_args);
7037 else
7038 l = TMPL_ARGS_LEVEL (args, cur_depth);
7039
7040 if (l == error_mark_node)
7041 return error_mark_node;
7042
7043 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7044 }
7045 }
7046 else
7047 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7048 args, in_decl, complain,
7049 require_all_args,
7050 use_default_args);
7051 return coerced_args;
7052 }
7053
7054 /* Returns 1 if template args OT and NT are equivalent. */
7055
7056 static int
7057 template_args_equal (tree ot, tree nt)
7058 {
7059 if (nt == ot)
7060 return 1;
7061 if (nt == NULL_TREE || ot == NULL_TREE)
7062 return false;
7063
7064 if (TREE_CODE (nt) == TREE_VEC)
7065 /* For member templates */
7066 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7067 else if (PACK_EXPANSION_P (ot))
7068 return (PACK_EXPANSION_P (nt)
7069 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7070 PACK_EXPANSION_PATTERN (nt))
7071 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7072 PACK_EXPANSION_EXTRA_ARGS (nt)));
7073 else if (ARGUMENT_PACK_P (ot))
7074 {
7075 int i, len;
7076 tree opack, npack;
7077
7078 if (!ARGUMENT_PACK_P (nt))
7079 return 0;
7080
7081 opack = ARGUMENT_PACK_ARGS (ot);
7082 npack = ARGUMENT_PACK_ARGS (nt);
7083 len = TREE_VEC_LENGTH (opack);
7084 if (TREE_VEC_LENGTH (npack) != len)
7085 return 0;
7086 for (i = 0; i < len; ++i)
7087 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7088 TREE_VEC_ELT (npack, i)))
7089 return 0;
7090 return 1;
7091 }
7092 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7093 {
7094 /* We get here probably because we are in the middle of substituting
7095 into the pattern of a pack expansion. In that case the
7096 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7097 interested in. So we want to use the initial pack argument for
7098 the comparison. */
7099 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7100 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7101 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7102 return template_args_equal (ot, nt);
7103 }
7104 else if (TYPE_P (nt))
7105 return TYPE_P (ot) && same_type_p (ot, nt);
7106 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7107 return 0;
7108 else
7109 return cp_tree_equal (ot, nt);
7110 }
7111
7112 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7113 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7114 NEWARG_PTR with the offending arguments if they are non-NULL. */
7115
7116 static int
7117 comp_template_args_with_info (tree oldargs, tree newargs,
7118 tree *oldarg_ptr, tree *newarg_ptr)
7119 {
7120 int i;
7121
7122 if (oldargs == newargs)
7123 return 1;
7124
7125 if (!oldargs || !newargs)
7126 return 0;
7127
7128 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7129 return 0;
7130
7131 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7132 {
7133 tree nt = TREE_VEC_ELT (newargs, i);
7134 tree ot = TREE_VEC_ELT (oldargs, i);
7135
7136 if (! template_args_equal (ot, nt))
7137 {
7138 if (oldarg_ptr != NULL)
7139 *oldarg_ptr = ot;
7140 if (newarg_ptr != NULL)
7141 *newarg_ptr = nt;
7142 return 0;
7143 }
7144 }
7145 return 1;
7146 }
7147
7148 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7149 of template arguments. Returns 0 otherwise. */
7150
7151 int
7152 comp_template_args (tree oldargs, tree newargs)
7153 {
7154 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7155 }
7156
7157 static void
7158 add_pending_template (tree d)
7159 {
7160 tree ti = (TYPE_P (d)
7161 ? CLASSTYPE_TEMPLATE_INFO (d)
7162 : DECL_TEMPLATE_INFO (d));
7163 struct pending_template *pt;
7164 int level;
7165
7166 if (TI_PENDING_TEMPLATE_FLAG (ti))
7167 return;
7168
7169 /* We are called both from instantiate_decl, where we've already had a
7170 tinst_level pushed, and instantiate_template, where we haven't.
7171 Compensate. */
7172 level = !current_tinst_level || current_tinst_level->decl != d;
7173
7174 if (level)
7175 push_tinst_level (d);
7176
7177 pt = ggc_alloc_pending_template ();
7178 pt->next = NULL;
7179 pt->tinst = current_tinst_level;
7180 if (last_pending_template)
7181 last_pending_template->next = pt;
7182 else
7183 pending_templates = pt;
7184
7185 last_pending_template = pt;
7186
7187 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7188
7189 if (level)
7190 pop_tinst_level ();
7191 }
7192
7193
7194 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7195 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7196 documentation for TEMPLATE_ID_EXPR. */
7197
7198 tree
7199 lookup_template_function (tree fns, tree arglist)
7200 {
7201 tree type;
7202
7203 if (fns == error_mark_node || arglist == error_mark_node)
7204 return error_mark_node;
7205
7206 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7207
7208 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7209 {
7210 error ("%q#D is not a function template", fns);
7211 return error_mark_node;
7212 }
7213
7214 if (BASELINK_P (fns))
7215 {
7216 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7217 unknown_type_node,
7218 BASELINK_FUNCTIONS (fns),
7219 arglist);
7220 return fns;
7221 }
7222
7223 type = TREE_TYPE (fns);
7224 if (TREE_CODE (fns) == OVERLOAD || !type)
7225 type = unknown_type_node;
7226
7227 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7228 }
7229
7230 /* Within the scope of a template class S<T>, the name S gets bound
7231 (in build_self_reference) to a TYPE_DECL for the class, not a
7232 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7233 or one of its enclosing classes, and that type is a template,
7234 return the associated TEMPLATE_DECL. Otherwise, the original
7235 DECL is returned.
7236
7237 Also handle the case when DECL is a TREE_LIST of ambiguous
7238 injected-class-names from different bases. */
7239
7240 tree
7241 maybe_get_template_decl_from_type_decl (tree decl)
7242 {
7243 if (decl == NULL_TREE)
7244 return decl;
7245
7246 /* DR 176: A lookup that finds an injected-class-name (10.2
7247 [class.member.lookup]) can result in an ambiguity in certain cases
7248 (for example, if it is found in more than one base class). If all of
7249 the injected-class-names that are found refer to specializations of
7250 the same class template, and if the name is followed by a
7251 template-argument-list, the reference refers to the class template
7252 itself and not a specialization thereof, and is not ambiguous. */
7253 if (TREE_CODE (decl) == TREE_LIST)
7254 {
7255 tree t, tmpl = NULL_TREE;
7256 for (t = decl; t; t = TREE_CHAIN (t))
7257 {
7258 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7259 if (!tmpl)
7260 tmpl = elt;
7261 else if (tmpl != elt)
7262 break;
7263 }
7264 if (tmpl && t == NULL_TREE)
7265 return tmpl;
7266 else
7267 return decl;
7268 }
7269
7270 return (decl != NULL_TREE
7271 && DECL_SELF_REFERENCE_P (decl)
7272 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7273 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7274 }
7275
7276 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7277 parameters, find the desired type.
7278
7279 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7280
7281 IN_DECL, if non-NULL, is the template declaration we are trying to
7282 instantiate.
7283
7284 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7285 the class we are looking up.
7286
7287 Issue error and warning messages under control of COMPLAIN.
7288
7289 If the template class is really a local class in a template
7290 function, then the FUNCTION_CONTEXT is the function in which it is
7291 being instantiated.
7292
7293 ??? Note that this function is currently called *twice* for each
7294 template-id: the first time from the parser, while creating the
7295 incomplete type (finish_template_type), and the second type during the
7296 real instantiation (instantiate_template_class). This is surely something
7297 that we want to avoid. It also causes some problems with argument
7298 coercion (see convert_nontype_argument for more information on this). */
7299
7300 static tree
7301 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7302 int entering_scope, tsubst_flags_t complain)
7303 {
7304 tree templ = NULL_TREE, parmlist;
7305 tree t;
7306 void **slot;
7307 spec_entry *entry;
7308 spec_entry elt;
7309 hashval_t hash;
7310
7311 if (identifier_p (d1))
7312 {
7313 tree value = innermost_non_namespace_value (d1);
7314 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7315 templ = value;
7316 else
7317 {
7318 if (context)
7319 push_decl_namespace (context);
7320 templ = lookup_name (d1);
7321 templ = maybe_get_template_decl_from_type_decl (templ);
7322 if (context)
7323 pop_decl_namespace ();
7324 }
7325 if (templ)
7326 context = DECL_CONTEXT (templ);
7327 }
7328 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7329 {
7330 tree type = TREE_TYPE (d1);
7331
7332 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7333 an implicit typename for the second A. Deal with it. */
7334 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7335 type = TREE_TYPE (type);
7336
7337 if (CLASSTYPE_TEMPLATE_INFO (type))
7338 {
7339 templ = CLASSTYPE_TI_TEMPLATE (type);
7340 d1 = DECL_NAME (templ);
7341 }
7342 }
7343 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7344 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7345 {
7346 templ = TYPE_TI_TEMPLATE (d1);
7347 d1 = DECL_NAME (templ);
7348 }
7349 else if (TREE_CODE (d1) == TEMPLATE_DECL
7350 && DECL_TEMPLATE_RESULT (d1)
7351 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7352 {
7353 templ = d1;
7354 d1 = DECL_NAME (templ);
7355 context = DECL_CONTEXT (templ);
7356 }
7357 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7358 {
7359 templ = d1;
7360 d1 = DECL_NAME (templ);
7361 }
7362
7363 /* Issue an error message if we didn't find a template. */
7364 if (! templ)
7365 {
7366 if (complain & tf_error)
7367 error ("%qT is not a template", d1);
7368 return error_mark_node;
7369 }
7370
7371 if (TREE_CODE (templ) != TEMPLATE_DECL
7372 /* Make sure it's a user visible template, if it was named by
7373 the user. */
7374 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7375 && !PRIMARY_TEMPLATE_P (templ)))
7376 {
7377 if (complain & tf_error)
7378 {
7379 error ("non-template type %qT used as a template", d1);
7380 if (in_decl)
7381 error ("for template declaration %q+D", in_decl);
7382 }
7383 return error_mark_node;
7384 }
7385
7386 complain &= ~tf_user;
7387
7388 /* An alias that just changes the name of a template is equivalent to the
7389 other template, so if any of the arguments are pack expansions, strip
7390 the alias to avoid problems with a pack expansion passed to a non-pack
7391 alias template parameter (DR 1430). */
7392 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7393 templ = get_underlying_template (templ);
7394
7395 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7396 {
7397 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7398 template arguments */
7399
7400 tree parm;
7401 tree arglist2;
7402 tree outer;
7403
7404 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7405
7406 /* Consider an example where a template template parameter declared as
7407
7408 template <class T, class U = std::allocator<T> > class TT
7409
7410 The template parameter level of T and U are one level larger than
7411 of TT. To proper process the default argument of U, say when an
7412 instantiation `TT<int>' is seen, we need to build the full
7413 arguments containing {int} as the innermost level. Outer levels,
7414 available when not appearing as default template argument, can be
7415 obtained from the arguments of the enclosing template.
7416
7417 Suppose that TT is later substituted with std::vector. The above
7418 instantiation is `TT<int, std::allocator<T> >' with TT at
7419 level 1, and T at level 2, while the template arguments at level 1
7420 becomes {std::vector} and the inner level 2 is {int}. */
7421
7422 outer = DECL_CONTEXT (templ);
7423 if (outer)
7424 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7425 else if (current_template_parms)
7426 /* This is an argument of the current template, so we haven't set
7427 DECL_CONTEXT yet. */
7428 outer = current_template_args ();
7429
7430 if (outer)
7431 arglist = add_to_template_args (outer, arglist);
7432
7433 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7434 complain,
7435 /*require_all_args=*/true,
7436 /*use_default_args=*/true);
7437 if (arglist2 == error_mark_node
7438 || (!uses_template_parms (arglist2)
7439 && check_instantiated_args (templ, arglist2, complain)))
7440 return error_mark_node;
7441
7442 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7443 return parm;
7444 }
7445 else
7446 {
7447 tree template_type = TREE_TYPE (templ);
7448 tree gen_tmpl;
7449 tree type_decl;
7450 tree found = NULL_TREE;
7451 int arg_depth;
7452 int parm_depth;
7453 int is_dependent_type;
7454 int use_partial_inst_tmpl = false;
7455
7456 if (template_type == error_mark_node)
7457 /* An error occurred while building the template TEMPL, and a
7458 diagnostic has most certainly been emitted for that
7459 already. Let's propagate that error. */
7460 return error_mark_node;
7461
7462 gen_tmpl = most_general_template (templ);
7463 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7464 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7465 arg_depth = TMPL_ARGS_DEPTH (arglist);
7466
7467 if (arg_depth == 1 && parm_depth > 1)
7468 {
7469 /* We've been given an incomplete set of template arguments.
7470 For example, given:
7471
7472 template <class T> struct S1 {
7473 template <class U> struct S2 {};
7474 template <class U> struct S2<U*> {};
7475 };
7476
7477 we will be called with an ARGLIST of `U*', but the
7478 TEMPLATE will be `template <class T> template
7479 <class U> struct S1<T>::S2'. We must fill in the missing
7480 arguments. */
7481 arglist
7482 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7483 arglist);
7484 arg_depth = TMPL_ARGS_DEPTH (arglist);
7485 }
7486
7487 /* Now we should have enough arguments. */
7488 gcc_assert (parm_depth == arg_depth);
7489
7490 /* From here on, we're only interested in the most general
7491 template. */
7492
7493 /* Calculate the BOUND_ARGS. These will be the args that are
7494 actually tsubst'd into the definition to create the
7495 instantiation. */
7496 if (parm_depth > 1)
7497 {
7498 /* We have multiple levels of arguments to coerce, at once. */
7499 int i;
7500 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7501
7502 tree bound_args = make_tree_vec (parm_depth);
7503
7504 for (i = saved_depth,
7505 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7506 i > 0 && t != NULL_TREE;
7507 --i, t = TREE_CHAIN (t))
7508 {
7509 tree a;
7510 if (i == saved_depth)
7511 a = coerce_template_parms (TREE_VALUE (t),
7512 arglist, gen_tmpl,
7513 complain,
7514 /*require_all_args=*/true,
7515 /*use_default_args=*/true);
7516 else
7517 /* Outer levels should have already been coerced. */
7518 a = TMPL_ARGS_LEVEL (arglist, i);
7519
7520 /* Don't process further if one of the levels fails. */
7521 if (a == error_mark_node)
7522 {
7523 /* Restore the ARGLIST to its full size. */
7524 TREE_VEC_LENGTH (arglist) = saved_depth;
7525 return error_mark_node;
7526 }
7527
7528 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7529
7530 /* We temporarily reduce the length of the ARGLIST so
7531 that coerce_template_parms will see only the arguments
7532 corresponding to the template parameters it is
7533 examining. */
7534 TREE_VEC_LENGTH (arglist)--;
7535 }
7536
7537 /* Restore the ARGLIST to its full size. */
7538 TREE_VEC_LENGTH (arglist) = saved_depth;
7539
7540 arglist = bound_args;
7541 }
7542 else
7543 arglist
7544 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7545 INNERMOST_TEMPLATE_ARGS (arglist),
7546 gen_tmpl,
7547 complain,
7548 /*require_all_args=*/true,
7549 /*use_default_args=*/true);
7550
7551 if (arglist == error_mark_node)
7552 /* We were unable to bind the arguments. */
7553 return error_mark_node;
7554
7555 /* In the scope of a template class, explicit references to the
7556 template class refer to the type of the template, not any
7557 instantiation of it. For example, in:
7558
7559 template <class T> class C { void f(C<T>); }
7560
7561 the `C<T>' is just the same as `C'. Outside of the
7562 class, however, such a reference is an instantiation. */
7563 if ((entering_scope
7564 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7565 || currently_open_class (template_type))
7566 /* comp_template_args is expensive, check it last. */
7567 && comp_template_args (TYPE_TI_ARGS (template_type),
7568 arglist))
7569 return template_type;
7570
7571 /* If we already have this specialization, return it. */
7572 elt.tmpl = gen_tmpl;
7573 elt.args = arglist;
7574 hash = hash_specialization (&elt);
7575 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7576 &elt, hash);
7577
7578 if (entry)
7579 return entry->spec;
7580
7581 is_dependent_type = uses_template_parms (arglist);
7582
7583 /* If the deduced arguments are invalid, then the binding
7584 failed. */
7585 if (!is_dependent_type
7586 && check_instantiated_args (gen_tmpl,
7587 INNERMOST_TEMPLATE_ARGS (arglist),
7588 complain))
7589 return error_mark_node;
7590
7591 if (!is_dependent_type
7592 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7593 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7594 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7595 {
7596 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7597 DECL_NAME (gen_tmpl),
7598 /*tag_scope=*/ts_global);
7599 return found;
7600 }
7601
7602 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7603 complain, in_decl);
7604 if (context == error_mark_node)
7605 return error_mark_node;
7606
7607 if (!context)
7608 context = global_namespace;
7609
7610 /* Create the type. */
7611 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7612 {
7613 /* The user referred to a specialization of an alias
7614 template represented by GEN_TMPL.
7615
7616 [temp.alias]/2 says:
7617
7618 When a template-id refers to the specialization of an
7619 alias template, it is equivalent to the associated
7620 type obtained by substitution of its
7621 template-arguments for the template-parameters in the
7622 type-id of the alias template. */
7623
7624 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7625 /* Note that the call above (by indirectly calling
7626 register_specialization in tsubst_decl) registers the
7627 TYPE_DECL representing the specialization of the alias
7628 template. So next time someone substitutes ARGLIST for
7629 the template parms into the alias template (GEN_TMPL),
7630 she'll get that TYPE_DECL back. */
7631
7632 if (t == error_mark_node)
7633 return t;
7634 }
7635 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7636 {
7637 if (!is_dependent_type)
7638 {
7639 set_current_access_from_decl (TYPE_NAME (template_type));
7640 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7641 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7642 arglist, complain, in_decl),
7643 SCOPED_ENUM_P (template_type), NULL);
7644
7645 if (t == error_mark_node)
7646 return t;
7647 }
7648 else
7649 {
7650 /* We don't want to call start_enum for this type, since
7651 the values for the enumeration constants may involve
7652 template parameters. And, no one should be interested
7653 in the enumeration constants for such a type. */
7654 t = cxx_make_type (ENUMERAL_TYPE);
7655 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7656 }
7657 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7658 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7659 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7660 }
7661 else if (CLASS_TYPE_P (template_type))
7662 {
7663 t = make_class_type (TREE_CODE (template_type));
7664 CLASSTYPE_DECLARED_CLASS (t)
7665 = CLASSTYPE_DECLARED_CLASS (template_type);
7666 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7667 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7668
7669 /* A local class. Make sure the decl gets registered properly. */
7670 if (context == current_function_decl)
7671 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7672
7673 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7674 /* This instantiation is another name for the primary
7675 template type. Set the TYPE_CANONICAL field
7676 appropriately. */
7677 TYPE_CANONICAL (t) = template_type;
7678 else if (any_template_arguments_need_structural_equality_p (arglist))
7679 /* Some of the template arguments require structural
7680 equality testing, so this template class requires
7681 structural equality testing. */
7682 SET_TYPE_STRUCTURAL_EQUALITY (t);
7683 }
7684 else
7685 gcc_unreachable ();
7686
7687 /* If we called start_enum or pushtag above, this information
7688 will already be set up. */
7689 if (!TYPE_NAME (t))
7690 {
7691 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7692
7693 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7694 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7695 DECL_SOURCE_LOCATION (type_decl)
7696 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7697 }
7698 else
7699 type_decl = TYPE_NAME (t);
7700
7701 if (CLASS_TYPE_P (template_type))
7702 {
7703 TREE_PRIVATE (type_decl)
7704 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7705 TREE_PROTECTED (type_decl)
7706 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7707 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7708 {
7709 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7710 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7711 }
7712 }
7713
7714 /* Let's consider the explicit specialization of a member
7715 of a class template specialization that is implicitly instantiated,
7716 e.g.:
7717 template<class T>
7718 struct S
7719 {
7720 template<class U> struct M {}; //#0
7721 };
7722
7723 template<>
7724 template<>
7725 struct S<int>::M<char> //#1
7726 {
7727 int i;
7728 };
7729 [temp.expl.spec]/4 says this is valid.
7730
7731 In this case, when we write:
7732 S<int>::M<char> m;
7733
7734 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7735 the one of #0.
7736
7737 When we encounter #1, we want to store the partial instantiation
7738 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7739
7740 For all cases other than this "explicit specialization of member of a
7741 class template", we just want to store the most general template into
7742 the CLASSTYPE_TI_TEMPLATE of M.
7743
7744 This case of "explicit specialization of member of a class template"
7745 only happens when:
7746 1/ the enclosing class is an instantiation of, and therefore not
7747 the same as, the context of the most general template, and
7748 2/ we aren't looking at the partial instantiation itself, i.e.
7749 the innermost arguments are not the same as the innermost parms of
7750 the most general template.
7751
7752 So it's only when 1/ and 2/ happens that we want to use the partial
7753 instantiation of the member template in lieu of its most general
7754 template. */
7755
7756 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7757 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7758 /* the enclosing class must be an instantiation... */
7759 && CLASS_TYPE_P (context)
7760 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7761 {
7762 tree partial_inst_args;
7763 TREE_VEC_LENGTH (arglist)--;
7764 ++processing_template_decl;
7765 partial_inst_args =
7766 tsubst (INNERMOST_TEMPLATE_ARGS
7767 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7768 arglist, complain, NULL_TREE);
7769 --processing_template_decl;
7770 TREE_VEC_LENGTH (arglist)++;
7771 use_partial_inst_tmpl =
7772 /*...and we must not be looking at the partial instantiation
7773 itself. */
7774 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7775 partial_inst_args);
7776 }
7777
7778 if (!use_partial_inst_tmpl)
7779 /* This case is easy; there are no member templates involved. */
7780 found = gen_tmpl;
7781 else
7782 {
7783 /* This is a full instantiation of a member template. Find
7784 the partial instantiation of which this is an instance. */
7785
7786 /* Temporarily reduce by one the number of levels in the ARGLIST
7787 so as to avoid comparing the last set of arguments. */
7788 TREE_VEC_LENGTH (arglist)--;
7789 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7790 TREE_VEC_LENGTH (arglist)++;
7791 /* FOUND is either a proper class type, or an alias
7792 template specialization. In the later case, it's a
7793 TYPE_DECL, resulting from the substituting of arguments
7794 for parameters in the TYPE_DECL of the alias template
7795 done earlier. So be careful while getting the template
7796 of FOUND. */
7797 found = TREE_CODE (found) == TYPE_DECL
7798 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7799 : CLASSTYPE_TI_TEMPLATE (found);
7800 }
7801
7802 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7803
7804 elt.spec = t;
7805 slot = htab_find_slot_with_hash (type_specializations,
7806 &elt, hash, INSERT);
7807 entry = ggc_alloc_spec_entry ();
7808 *entry = elt;
7809 *slot = entry;
7810
7811 /* Note this use of the partial instantiation so we can check it
7812 later in maybe_process_partial_specialization. */
7813 DECL_TEMPLATE_INSTANTIATIONS (found)
7814 = tree_cons (arglist, t,
7815 DECL_TEMPLATE_INSTANTIATIONS (found));
7816
7817 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
7818 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7819 /* Now that the type has been registered on the instantiations
7820 list, we set up the enumerators. Because the enumeration
7821 constants may involve the enumeration type itself, we make
7822 sure to register the type first, and then create the
7823 constants. That way, doing tsubst_expr for the enumeration
7824 constants won't result in recursive calls here; we'll find
7825 the instantiation and exit above. */
7826 tsubst_enum (template_type, t, arglist);
7827
7828 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7829 /* If the type makes use of template parameters, the
7830 code that generates debugging information will crash. */
7831 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7832
7833 /* Possibly limit visibility based on template args. */
7834 TREE_PUBLIC (type_decl) = 1;
7835 determine_visibility (type_decl);
7836
7837 return t;
7838 }
7839 }
7840
7841 /* Wrapper for lookup_template_class_1. */
7842
7843 tree
7844 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7845 int entering_scope, tsubst_flags_t complain)
7846 {
7847 tree ret;
7848 timevar_push (TV_TEMPLATE_INST);
7849 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7850 entering_scope, complain);
7851 timevar_pop (TV_TEMPLATE_INST);
7852 return ret;
7853 }
7854 \f
7855 struct pair_fn_data
7856 {
7857 tree_fn_t fn;
7858 void *data;
7859 /* True when we should also visit template parameters that occur in
7860 non-deduced contexts. */
7861 bool include_nondeduced_p;
7862 struct pointer_set_t *visited;
7863 };
7864
7865 /* Called from for_each_template_parm via walk_tree. */
7866
7867 static tree
7868 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7869 {
7870 tree t = *tp;
7871 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7872 tree_fn_t fn = pfd->fn;
7873 void *data = pfd->data;
7874
7875 if (TYPE_P (t)
7876 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7877 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7878 pfd->include_nondeduced_p))
7879 return error_mark_node;
7880
7881 switch (TREE_CODE (t))
7882 {
7883 case RECORD_TYPE:
7884 if (TYPE_PTRMEMFUNC_P (t))
7885 break;
7886 /* Fall through. */
7887
7888 case UNION_TYPE:
7889 case ENUMERAL_TYPE:
7890 if (!TYPE_TEMPLATE_INFO (t))
7891 *walk_subtrees = 0;
7892 else if (for_each_template_parm (TYPE_TI_ARGS (t),
7893 fn, data, pfd->visited,
7894 pfd->include_nondeduced_p))
7895 return error_mark_node;
7896 break;
7897
7898 case INTEGER_TYPE:
7899 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7900 fn, data, pfd->visited,
7901 pfd->include_nondeduced_p)
7902 || for_each_template_parm (TYPE_MAX_VALUE (t),
7903 fn, data, pfd->visited,
7904 pfd->include_nondeduced_p))
7905 return error_mark_node;
7906 break;
7907
7908 case METHOD_TYPE:
7909 /* Since we're not going to walk subtrees, we have to do this
7910 explicitly here. */
7911 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7912 pfd->visited, pfd->include_nondeduced_p))
7913 return error_mark_node;
7914 /* Fall through. */
7915
7916 case FUNCTION_TYPE:
7917 /* Check the return type. */
7918 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7919 pfd->include_nondeduced_p))
7920 return error_mark_node;
7921
7922 /* Check the parameter types. Since default arguments are not
7923 instantiated until they are needed, the TYPE_ARG_TYPES may
7924 contain expressions that involve template parameters. But,
7925 no-one should be looking at them yet. And, once they're
7926 instantiated, they don't contain template parameters, so
7927 there's no point in looking at them then, either. */
7928 {
7929 tree parm;
7930
7931 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7932 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7933 pfd->visited, pfd->include_nondeduced_p))
7934 return error_mark_node;
7935
7936 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7937 want walk_tree walking into them itself. */
7938 *walk_subtrees = 0;
7939 }
7940 break;
7941
7942 case TYPEOF_TYPE:
7943 case UNDERLYING_TYPE:
7944 if (pfd->include_nondeduced_p
7945 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7946 pfd->visited,
7947 pfd->include_nondeduced_p))
7948 return error_mark_node;
7949 break;
7950
7951 case FUNCTION_DECL:
7952 case VAR_DECL:
7953 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7954 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7955 pfd->visited, pfd->include_nondeduced_p))
7956 return error_mark_node;
7957 /* Fall through. */
7958
7959 case PARM_DECL:
7960 case CONST_DECL:
7961 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7962 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7963 pfd->visited, pfd->include_nondeduced_p))
7964 return error_mark_node;
7965 if (DECL_CONTEXT (t)
7966 && pfd->include_nondeduced_p
7967 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7968 pfd->visited, pfd->include_nondeduced_p))
7969 return error_mark_node;
7970 break;
7971
7972 case BOUND_TEMPLATE_TEMPLATE_PARM:
7973 /* Record template parameters such as `T' inside `TT<T>'. */
7974 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7975 pfd->include_nondeduced_p))
7976 return error_mark_node;
7977 /* Fall through. */
7978
7979 case TEMPLATE_TEMPLATE_PARM:
7980 case TEMPLATE_TYPE_PARM:
7981 case TEMPLATE_PARM_INDEX:
7982 if (fn && (*fn)(t, data))
7983 return error_mark_node;
7984 else if (!fn)
7985 return error_mark_node;
7986 break;
7987
7988 case TEMPLATE_DECL:
7989 /* A template template parameter is encountered. */
7990 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7991 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7992 pfd->include_nondeduced_p))
7993 return error_mark_node;
7994
7995 /* Already substituted template template parameter */
7996 *walk_subtrees = 0;
7997 break;
7998
7999 case TYPENAME_TYPE:
8000 if (!fn
8001 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
8002 data, pfd->visited,
8003 pfd->include_nondeduced_p))
8004 return error_mark_node;
8005 break;
8006
8007 case CONSTRUCTOR:
8008 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8009 && pfd->include_nondeduced_p
8010 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8011 (TREE_TYPE (t)), fn, data,
8012 pfd->visited, pfd->include_nondeduced_p))
8013 return error_mark_node;
8014 break;
8015
8016 case INDIRECT_REF:
8017 case COMPONENT_REF:
8018 /* If there's no type, then this thing must be some expression
8019 involving template parameters. */
8020 if (!fn && !TREE_TYPE (t))
8021 return error_mark_node;
8022 break;
8023
8024 case MODOP_EXPR:
8025 case CAST_EXPR:
8026 case IMPLICIT_CONV_EXPR:
8027 case REINTERPRET_CAST_EXPR:
8028 case CONST_CAST_EXPR:
8029 case STATIC_CAST_EXPR:
8030 case DYNAMIC_CAST_EXPR:
8031 case ARROW_EXPR:
8032 case DOTSTAR_EXPR:
8033 case TYPEID_EXPR:
8034 case PSEUDO_DTOR_EXPR:
8035 if (!fn)
8036 return error_mark_node;
8037 break;
8038
8039 default:
8040 break;
8041 }
8042
8043 /* We didn't find any template parameters we liked. */
8044 return NULL_TREE;
8045 }
8046
8047 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8048 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8049 call FN with the parameter and the DATA.
8050 If FN returns nonzero, the iteration is terminated, and
8051 for_each_template_parm returns 1. Otherwise, the iteration
8052 continues. If FN never returns a nonzero value, the value
8053 returned by for_each_template_parm is 0. If FN is NULL, it is
8054 considered to be the function which always returns 1.
8055
8056 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8057 parameters that occur in non-deduced contexts. When false, only
8058 visits those template parameters that can be deduced. */
8059
8060 static int
8061 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8062 struct pointer_set_t *visited,
8063 bool include_nondeduced_p)
8064 {
8065 struct pair_fn_data pfd;
8066 int result;
8067
8068 /* Set up. */
8069 pfd.fn = fn;
8070 pfd.data = data;
8071 pfd.include_nondeduced_p = include_nondeduced_p;
8072
8073 /* Walk the tree. (Conceptually, we would like to walk without
8074 duplicates, but for_each_template_parm_r recursively calls
8075 for_each_template_parm, so we would need to reorganize a fair
8076 bit to use walk_tree_without_duplicates, so we keep our own
8077 visited list.) */
8078 if (visited)
8079 pfd.visited = visited;
8080 else
8081 pfd.visited = pointer_set_create ();
8082 result = cp_walk_tree (&t,
8083 for_each_template_parm_r,
8084 &pfd,
8085 pfd.visited) != NULL_TREE;
8086
8087 /* Clean up. */
8088 if (!visited)
8089 {
8090 pointer_set_destroy (pfd.visited);
8091 pfd.visited = 0;
8092 }
8093
8094 return result;
8095 }
8096
8097 /* Returns true if T depends on any template parameter. */
8098
8099 int
8100 uses_template_parms (tree t)
8101 {
8102 bool dependent_p;
8103 int saved_processing_template_decl;
8104
8105 saved_processing_template_decl = processing_template_decl;
8106 if (!saved_processing_template_decl)
8107 processing_template_decl = 1;
8108 if (TYPE_P (t))
8109 dependent_p = dependent_type_p (t);
8110 else if (TREE_CODE (t) == TREE_VEC)
8111 dependent_p = any_dependent_template_arguments_p (t);
8112 else if (TREE_CODE (t) == TREE_LIST)
8113 dependent_p = (uses_template_parms (TREE_VALUE (t))
8114 || uses_template_parms (TREE_CHAIN (t)));
8115 else if (TREE_CODE (t) == TYPE_DECL)
8116 dependent_p = dependent_type_p (TREE_TYPE (t));
8117 else if (DECL_P (t)
8118 || EXPR_P (t)
8119 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8120 || TREE_CODE (t) == OVERLOAD
8121 || BASELINK_P (t)
8122 || identifier_p (t)
8123 || TREE_CODE (t) == TRAIT_EXPR
8124 || TREE_CODE (t) == CONSTRUCTOR
8125 || CONSTANT_CLASS_P (t))
8126 dependent_p = (type_dependent_expression_p (t)
8127 || value_dependent_expression_p (t));
8128 else
8129 {
8130 gcc_assert (t == error_mark_node);
8131 dependent_p = false;
8132 }
8133
8134 processing_template_decl = saved_processing_template_decl;
8135
8136 return dependent_p;
8137 }
8138
8139 /* Returns true iff current_function_decl is an incompletely instantiated
8140 template. Useful instead of processing_template_decl because the latter
8141 is set to 0 during fold_non_dependent_expr. */
8142
8143 bool
8144 in_template_function (void)
8145 {
8146 tree fn = current_function_decl;
8147 bool ret;
8148 ++processing_template_decl;
8149 ret = (fn && DECL_LANG_SPECIFIC (fn)
8150 && DECL_TEMPLATE_INFO (fn)
8151 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8152 --processing_template_decl;
8153 return ret;
8154 }
8155
8156 /* Returns true if T depends on any template parameter with level LEVEL. */
8157
8158 int
8159 uses_template_parms_level (tree t, int level)
8160 {
8161 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8162 /*include_nondeduced_p=*/true);
8163 }
8164
8165 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8166 ill-formed translation unit, i.e. a variable or function that isn't
8167 usable in a constant expression. */
8168
8169 static inline bool
8170 neglectable_inst_p (tree d)
8171 {
8172 return (DECL_P (d)
8173 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8174 : decl_maybe_constant_var_p (d)));
8175 }
8176
8177 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8178 neglectable and instantiated from within an erroneous instantiation. */
8179
8180 static bool
8181 limit_bad_template_recursion (tree decl)
8182 {
8183 struct tinst_level *lev = current_tinst_level;
8184 int errs = errorcount + sorrycount;
8185 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8186 return false;
8187
8188 for (; lev; lev = lev->next)
8189 if (neglectable_inst_p (lev->decl))
8190 break;
8191
8192 return (lev && errs > lev->errors);
8193 }
8194
8195 static int tinst_depth;
8196 extern int max_tinst_depth;
8197 int depth_reached;
8198
8199 static GTY(()) struct tinst_level *last_error_tinst_level;
8200
8201 /* We're starting to instantiate D; record the template instantiation context
8202 for diagnostics and to restore it later. */
8203
8204 int
8205 push_tinst_level (tree d)
8206 {
8207 struct tinst_level *new_level;
8208
8209 if (tinst_depth >= max_tinst_depth)
8210 {
8211 last_error_tinst_level = current_tinst_level;
8212 if (TREE_CODE (d) == TREE_LIST)
8213 error ("template instantiation depth exceeds maximum of %d (use "
8214 "-ftemplate-depth= to increase the maximum) substituting %qS",
8215 max_tinst_depth, d);
8216 else
8217 error ("template instantiation depth exceeds maximum of %d (use "
8218 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8219 max_tinst_depth, d);
8220
8221 print_instantiation_context ();
8222
8223 return 0;
8224 }
8225
8226 /* If the current instantiation caused problems, don't let it instantiate
8227 anything else. Do allow deduction substitution and decls usable in
8228 constant expressions. */
8229 if (limit_bad_template_recursion (d))
8230 return 0;
8231
8232 new_level = ggc_alloc_tinst_level ();
8233 new_level->decl = d;
8234 new_level->locus = input_location;
8235 new_level->errors = errorcount+sorrycount;
8236 new_level->in_system_header_p = in_system_header_at (input_location);
8237 new_level->next = current_tinst_level;
8238 current_tinst_level = new_level;
8239
8240 ++tinst_depth;
8241 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8242 depth_reached = tinst_depth;
8243
8244 return 1;
8245 }
8246
8247 /* We're done instantiating this template; return to the instantiation
8248 context. */
8249
8250 void
8251 pop_tinst_level (void)
8252 {
8253 /* Restore the filename and line number stashed away when we started
8254 this instantiation. */
8255 input_location = current_tinst_level->locus;
8256 current_tinst_level = current_tinst_level->next;
8257 --tinst_depth;
8258 }
8259
8260 /* We're instantiating a deferred template; restore the template
8261 instantiation context in which the instantiation was requested, which
8262 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8263
8264 static tree
8265 reopen_tinst_level (struct tinst_level *level)
8266 {
8267 struct tinst_level *t;
8268
8269 tinst_depth = 0;
8270 for (t = level; t; t = t->next)
8271 ++tinst_depth;
8272
8273 current_tinst_level = level;
8274 pop_tinst_level ();
8275 if (current_tinst_level)
8276 current_tinst_level->errors = errorcount+sorrycount;
8277 return level->decl;
8278 }
8279
8280 /* Returns the TINST_LEVEL which gives the original instantiation
8281 context. */
8282
8283 struct tinst_level *
8284 outermost_tinst_level (void)
8285 {
8286 struct tinst_level *level = current_tinst_level;
8287 if (level)
8288 while (level->next)
8289 level = level->next;
8290 return level;
8291 }
8292
8293 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8294 vector of template arguments, as for tsubst.
8295
8296 Returns an appropriate tsubst'd friend declaration. */
8297
8298 static tree
8299 tsubst_friend_function (tree decl, tree args)
8300 {
8301 tree new_friend;
8302
8303 if (TREE_CODE (decl) == FUNCTION_DECL
8304 && DECL_TEMPLATE_INSTANTIATION (decl)
8305 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8306 /* This was a friend declared with an explicit template
8307 argument list, e.g.:
8308
8309 friend void f<>(T);
8310
8311 to indicate that f was a template instantiation, not a new
8312 function declaration. Now, we have to figure out what
8313 instantiation of what template. */
8314 {
8315 tree template_id, arglist, fns;
8316 tree new_args;
8317 tree tmpl;
8318 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8319
8320 /* Friend functions are looked up in the containing namespace scope.
8321 We must enter that scope, to avoid finding member functions of the
8322 current class with same name. */
8323 push_nested_namespace (ns);
8324 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8325 tf_warning_or_error, NULL_TREE,
8326 /*integral_constant_expression_p=*/false);
8327 pop_nested_namespace (ns);
8328 arglist = tsubst (DECL_TI_ARGS (decl), args,
8329 tf_warning_or_error, NULL_TREE);
8330 template_id = lookup_template_function (fns, arglist);
8331
8332 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8333 tmpl = determine_specialization (template_id, new_friend,
8334 &new_args,
8335 /*need_member_template=*/0,
8336 TREE_VEC_LENGTH (args),
8337 tsk_none);
8338 return instantiate_template (tmpl, new_args, tf_error);
8339 }
8340
8341 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8342
8343 /* The NEW_FRIEND will look like an instantiation, to the
8344 compiler, but is not an instantiation from the point of view of
8345 the language. For example, we might have had:
8346
8347 template <class T> struct S {
8348 template <class U> friend void f(T, U);
8349 };
8350
8351 Then, in S<int>, template <class U> void f(int, U) is not an
8352 instantiation of anything. */
8353 if (new_friend == error_mark_node)
8354 return error_mark_node;
8355
8356 DECL_USE_TEMPLATE (new_friend) = 0;
8357 if (TREE_CODE (decl) == TEMPLATE_DECL)
8358 {
8359 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8360 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8361 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8362 }
8363
8364 /* The mangled name for the NEW_FRIEND is incorrect. The function
8365 is not a template instantiation and should not be mangled like
8366 one. Therefore, we forget the mangling here; we'll recompute it
8367 later if we need it. */
8368 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8369 {
8370 SET_DECL_RTL (new_friend, NULL);
8371 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8372 }
8373
8374 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8375 {
8376 tree old_decl;
8377 tree new_friend_template_info;
8378 tree new_friend_result_template_info;
8379 tree ns;
8380 int new_friend_is_defn;
8381
8382 /* We must save some information from NEW_FRIEND before calling
8383 duplicate decls since that function will free NEW_FRIEND if
8384 possible. */
8385 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8386 new_friend_is_defn =
8387 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8388 (template_for_substitution (new_friend)))
8389 != NULL_TREE);
8390 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8391 {
8392 /* This declaration is a `primary' template. */
8393 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8394
8395 new_friend_result_template_info
8396 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8397 }
8398 else
8399 new_friend_result_template_info = NULL_TREE;
8400
8401 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8402 if (new_friend_is_defn)
8403 DECL_INITIAL (new_friend) = error_mark_node;
8404
8405 /* Inside pushdecl_namespace_level, we will push into the
8406 current namespace. However, the friend function should go
8407 into the namespace of the template. */
8408 ns = decl_namespace_context (new_friend);
8409 push_nested_namespace (ns);
8410 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8411 pop_nested_namespace (ns);
8412
8413 if (old_decl == error_mark_node)
8414 return error_mark_node;
8415
8416 if (old_decl != new_friend)
8417 {
8418 /* This new friend declaration matched an existing
8419 declaration. For example, given:
8420
8421 template <class T> void f(T);
8422 template <class U> class C {
8423 template <class T> friend void f(T) {}
8424 };
8425
8426 the friend declaration actually provides the definition
8427 of `f', once C has been instantiated for some type. So,
8428 old_decl will be the out-of-class template declaration,
8429 while new_friend is the in-class definition.
8430
8431 But, if `f' was called before this point, the
8432 instantiation of `f' will have DECL_TI_ARGS corresponding
8433 to `T' but not to `U', references to which might appear
8434 in the definition of `f'. Previously, the most general
8435 template for an instantiation of `f' was the out-of-class
8436 version; now it is the in-class version. Therefore, we
8437 run through all specialization of `f', adding to their
8438 DECL_TI_ARGS appropriately. In particular, they need a
8439 new set of outer arguments, corresponding to the
8440 arguments for this class instantiation.
8441
8442 The same situation can arise with something like this:
8443
8444 friend void f(int);
8445 template <class T> class C {
8446 friend void f(T) {}
8447 };
8448
8449 when `C<int>' is instantiated. Now, `f(int)' is defined
8450 in the class. */
8451
8452 if (!new_friend_is_defn)
8453 /* On the other hand, if the in-class declaration does
8454 *not* provide a definition, then we don't want to alter
8455 existing definitions. We can just leave everything
8456 alone. */
8457 ;
8458 else
8459 {
8460 tree new_template = TI_TEMPLATE (new_friend_template_info);
8461 tree new_args = TI_ARGS (new_friend_template_info);
8462
8463 /* Overwrite whatever template info was there before, if
8464 any, with the new template information pertaining to
8465 the declaration. */
8466 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8467
8468 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8469 {
8470 /* We should have called reregister_specialization in
8471 duplicate_decls. */
8472 gcc_assert (retrieve_specialization (new_template,
8473 new_args, 0)
8474 == old_decl);
8475
8476 /* Instantiate it if the global has already been used. */
8477 if (DECL_ODR_USED (old_decl))
8478 instantiate_decl (old_decl, /*defer_ok=*/true,
8479 /*expl_inst_class_mem_p=*/false);
8480 }
8481 else
8482 {
8483 tree t;
8484
8485 /* Indicate that the old function template is a partial
8486 instantiation. */
8487 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8488 = new_friend_result_template_info;
8489
8490 gcc_assert (new_template
8491 == most_general_template (new_template));
8492 gcc_assert (new_template != old_decl);
8493
8494 /* Reassign any specializations already in the hash table
8495 to the new more general template, and add the
8496 additional template args. */
8497 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8498 t != NULL_TREE;
8499 t = TREE_CHAIN (t))
8500 {
8501 tree spec = TREE_VALUE (t);
8502 spec_entry elt;
8503
8504 elt.tmpl = old_decl;
8505 elt.args = DECL_TI_ARGS (spec);
8506 elt.spec = NULL_TREE;
8507
8508 htab_remove_elt (decl_specializations, &elt);
8509
8510 DECL_TI_ARGS (spec)
8511 = add_outermost_template_args (new_args,
8512 DECL_TI_ARGS (spec));
8513
8514 register_specialization
8515 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8516
8517 }
8518 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8519 }
8520 }
8521
8522 /* The information from NEW_FRIEND has been merged into OLD_DECL
8523 by duplicate_decls. */
8524 new_friend = old_decl;
8525 }
8526 }
8527 else
8528 {
8529 tree context = DECL_CONTEXT (new_friend);
8530 bool dependent_p;
8531
8532 /* In the code
8533 template <class T> class C {
8534 template <class U> friend void C1<U>::f (); // case 1
8535 friend void C2<T>::f (); // case 2
8536 };
8537 we only need to make sure CONTEXT is a complete type for
8538 case 2. To distinguish between the two cases, we note that
8539 CONTEXT of case 1 remains dependent type after tsubst while
8540 this isn't true for case 2. */
8541 ++processing_template_decl;
8542 dependent_p = dependent_type_p (context);
8543 --processing_template_decl;
8544
8545 if (!dependent_p
8546 && !complete_type_or_else (context, NULL_TREE))
8547 return error_mark_node;
8548
8549 if (COMPLETE_TYPE_P (context))
8550 {
8551 tree fn = new_friend;
8552 /* do_friend adds the TEMPLATE_DECL for any member friend
8553 template even if it isn't a member template, i.e.
8554 template <class T> friend A<T>::f();
8555 Look through it in that case. */
8556 if (TREE_CODE (fn) == TEMPLATE_DECL
8557 && !PRIMARY_TEMPLATE_P (fn))
8558 fn = DECL_TEMPLATE_RESULT (fn);
8559 /* Check to see that the declaration is really present, and,
8560 possibly obtain an improved declaration. */
8561 fn = check_classfn (context, fn, NULL_TREE);
8562
8563 if (fn)
8564 new_friend = fn;
8565 }
8566 }
8567
8568 return new_friend;
8569 }
8570
8571 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8572 template arguments, as for tsubst.
8573
8574 Returns an appropriate tsubst'd friend type or error_mark_node on
8575 failure. */
8576
8577 static tree
8578 tsubst_friend_class (tree friend_tmpl, tree args)
8579 {
8580 tree friend_type;
8581 tree tmpl;
8582 tree context;
8583
8584 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8585 {
8586 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8587 return TREE_TYPE (t);
8588 }
8589
8590 context = CP_DECL_CONTEXT (friend_tmpl);
8591
8592 if (context != global_namespace)
8593 {
8594 if (TREE_CODE (context) == NAMESPACE_DECL)
8595 push_nested_namespace (context);
8596 else
8597 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8598 }
8599
8600 /* Look for a class template declaration. We look for hidden names
8601 because two friend declarations of the same template are the
8602 same. For example, in:
8603
8604 struct A {
8605 template <typename> friend class F;
8606 };
8607 template <typename> struct B {
8608 template <typename> friend class F;
8609 };
8610
8611 both F templates are the same. */
8612 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8613 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8614
8615 /* But, if we don't find one, it might be because we're in a
8616 situation like this:
8617
8618 template <class T>
8619 struct S {
8620 template <class U>
8621 friend struct S;
8622 };
8623
8624 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8625 for `S<int>', not the TEMPLATE_DECL. */
8626 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8627 {
8628 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8629 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8630 }
8631
8632 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8633 {
8634 /* The friend template has already been declared. Just
8635 check to see that the declarations match, and install any new
8636 default parameters. We must tsubst the default parameters,
8637 of course. We only need the innermost template parameters
8638 because that is all that redeclare_class_template will look
8639 at. */
8640 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8641 > TMPL_ARGS_DEPTH (args))
8642 {
8643 tree parms;
8644 location_t saved_input_location;
8645 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8646 args, tf_warning_or_error);
8647
8648 saved_input_location = input_location;
8649 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8650 redeclare_class_template (TREE_TYPE (tmpl), parms);
8651 input_location = saved_input_location;
8652
8653 }
8654
8655 friend_type = TREE_TYPE (tmpl);
8656 }
8657 else
8658 {
8659 /* The friend template has not already been declared. In this
8660 case, the instantiation of the template class will cause the
8661 injection of this template into the global scope. */
8662 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8663 if (tmpl == error_mark_node)
8664 return error_mark_node;
8665
8666 /* The new TMPL is not an instantiation of anything, so we
8667 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8668 the new type because that is supposed to be the corresponding
8669 template decl, i.e., TMPL. */
8670 DECL_USE_TEMPLATE (tmpl) = 0;
8671 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8672 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8673 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8674 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8675
8676 /* Inject this template into the global scope. */
8677 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8678 }
8679
8680 if (context != global_namespace)
8681 {
8682 if (TREE_CODE (context) == NAMESPACE_DECL)
8683 pop_nested_namespace (context);
8684 else
8685 pop_nested_class ();
8686 }
8687
8688 return friend_type;
8689 }
8690
8691 /* Returns zero if TYPE cannot be completed later due to circularity.
8692 Otherwise returns one. */
8693
8694 static int
8695 can_complete_type_without_circularity (tree type)
8696 {
8697 if (type == NULL_TREE || type == error_mark_node)
8698 return 0;
8699 else if (COMPLETE_TYPE_P (type))
8700 return 1;
8701 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8702 return can_complete_type_without_circularity (TREE_TYPE (type));
8703 else if (CLASS_TYPE_P (type)
8704 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8705 return 0;
8706 else
8707 return 1;
8708 }
8709
8710 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8711
8712 /* Apply any attributes which had to be deferred until instantiation
8713 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8714 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8715
8716 static void
8717 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8718 tree args, tsubst_flags_t complain, tree in_decl)
8719 {
8720 tree last_dep = NULL_TREE;
8721 tree t;
8722 tree *p;
8723
8724 for (t = attributes; t; t = TREE_CHAIN (t))
8725 if (ATTR_IS_DEPENDENT (t))
8726 {
8727 last_dep = t;
8728 attributes = copy_list (attributes);
8729 break;
8730 }
8731
8732 if (DECL_P (*decl_p))
8733 {
8734 if (TREE_TYPE (*decl_p) == error_mark_node)
8735 return;
8736 p = &DECL_ATTRIBUTES (*decl_p);
8737 }
8738 else
8739 p = &TYPE_ATTRIBUTES (*decl_p);
8740
8741 if (last_dep)
8742 {
8743 tree late_attrs = NULL_TREE;
8744 tree *q = &late_attrs;
8745
8746 for (*p = attributes; *p; )
8747 {
8748 t = *p;
8749 if (ATTR_IS_DEPENDENT (t))
8750 {
8751 *p = TREE_CHAIN (t);
8752 TREE_CHAIN (t) = NULL_TREE;
8753 if ((flag_openmp || flag_cilkplus)
8754 && is_attribute_p ("omp declare simd",
8755 get_attribute_name (t))
8756 && TREE_VALUE (t))
8757 {
8758 tree clauses = TREE_VALUE (TREE_VALUE (t));
8759 clauses = tsubst_omp_clauses (clauses, true, args,
8760 complain, in_decl);
8761 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8762 clauses = finish_omp_clauses (clauses);
8763 tree parms = DECL_ARGUMENTS (*decl_p);
8764 clauses
8765 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8766 if (clauses)
8767 TREE_VALUE (TREE_VALUE (t)) = clauses;
8768 else
8769 TREE_VALUE (t) = NULL_TREE;
8770 }
8771 /* If the first attribute argument is an identifier, don't
8772 pass it through tsubst. Attributes like mode, format,
8773 cleanup and several target specific attributes expect it
8774 unmodified. */
8775 else if (attribute_takes_identifier_p (get_attribute_name (t))
8776 && TREE_VALUE (t))
8777 {
8778 tree chain
8779 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8780 in_decl,
8781 /*integral_constant_expression_p=*/false);
8782 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8783 TREE_VALUE (t)
8784 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8785 chain);
8786 }
8787 else
8788 TREE_VALUE (t)
8789 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8790 /*integral_constant_expression_p=*/false);
8791 *q = t;
8792 q = &TREE_CHAIN (t);
8793 }
8794 else
8795 p = &TREE_CHAIN (t);
8796 }
8797
8798 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8799 }
8800 }
8801
8802 /* Perform (or defer) access check for typedefs that were referenced
8803 from within the template TMPL code.
8804 This is a subroutine of instantiate_decl and instantiate_class_template.
8805 TMPL is the template to consider and TARGS is the list of arguments of
8806 that template. */
8807
8808 static void
8809 perform_typedefs_access_check (tree tmpl, tree targs)
8810 {
8811 location_t saved_location;
8812 unsigned i;
8813 qualified_typedef_usage_t *iter;
8814
8815 if (!tmpl
8816 || (!CLASS_TYPE_P (tmpl)
8817 && TREE_CODE (tmpl) != FUNCTION_DECL))
8818 return;
8819
8820 saved_location = input_location;
8821 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8822 {
8823 tree type_decl = iter->typedef_decl;
8824 tree type_scope = iter->context;
8825
8826 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8827 continue;
8828
8829 if (uses_template_parms (type_decl))
8830 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8831 if (uses_template_parms (type_scope))
8832 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8833
8834 /* Make access check error messages point to the location
8835 of the use of the typedef. */
8836 input_location = iter->locus;
8837 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8838 type_decl, type_decl,
8839 tf_warning_or_error);
8840 }
8841 input_location = saved_location;
8842 }
8843
8844 static tree
8845 instantiate_class_template_1 (tree type)
8846 {
8847 tree templ, args, pattern, t, member;
8848 tree typedecl;
8849 tree pbinfo;
8850 tree base_list;
8851 unsigned int saved_maximum_field_alignment;
8852 tree fn_context;
8853
8854 if (type == error_mark_node)
8855 return error_mark_node;
8856
8857 if (COMPLETE_OR_OPEN_TYPE_P (type)
8858 || uses_template_parms (type))
8859 return type;
8860
8861 /* Figure out which template is being instantiated. */
8862 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8863 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8864
8865 /* Determine what specialization of the original template to
8866 instantiate. */
8867 t = most_specialized_class (type, tf_warning_or_error);
8868 if (t == error_mark_node)
8869 {
8870 TYPE_BEING_DEFINED (type) = 1;
8871 return error_mark_node;
8872 }
8873 else if (t)
8874 {
8875 /* This TYPE is actually an instantiation of a partial
8876 specialization. We replace the innermost set of ARGS with
8877 the arguments appropriate for substitution. For example,
8878 given:
8879
8880 template <class T> struct S {};
8881 template <class T> struct S<T*> {};
8882
8883 and supposing that we are instantiating S<int*>, ARGS will
8884 presently be {int*} -- but we need {int}. */
8885 pattern = TREE_TYPE (t);
8886 args = TREE_PURPOSE (t);
8887 }
8888 else
8889 {
8890 pattern = TREE_TYPE (templ);
8891 args = CLASSTYPE_TI_ARGS (type);
8892 }
8893
8894 /* If the template we're instantiating is incomplete, then clearly
8895 there's nothing we can do. */
8896 if (!COMPLETE_TYPE_P (pattern))
8897 return type;
8898
8899 /* If we've recursively instantiated too many templates, stop. */
8900 if (! push_tinst_level (type))
8901 return type;
8902
8903 /* Now we're really doing the instantiation. Mark the type as in
8904 the process of being defined. */
8905 TYPE_BEING_DEFINED (type) = 1;
8906
8907 /* We may be in the middle of deferred access check. Disable
8908 it now. */
8909 push_deferring_access_checks (dk_no_deferred);
8910
8911 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8912 if (!fn_context)
8913 push_to_top_level ();
8914 /* Use #pragma pack from the template context. */
8915 saved_maximum_field_alignment = maximum_field_alignment;
8916 maximum_field_alignment = TYPE_PRECISION (pattern);
8917
8918 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8919
8920 /* Set the input location to the most specialized template definition.
8921 This is needed if tsubsting causes an error. */
8922 typedecl = TYPE_MAIN_DECL (pattern);
8923 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8924 DECL_SOURCE_LOCATION (typedecl);
8925
8926 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8927 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8928 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8929 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8930 if (ANON_AGGR_TYPE_P (pattern))
8931 SET_ANON_AGGR_TYPE_P (type);
8932 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8933 {
8934 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8935 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8936 /* Adjust visibility for template arguments. */
8937 determine_visibility (TYPE_MAIN_DECL (type));
8938 }
8939 if (CLASS_TYPE_P (type))
8940 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8941
8942 pbinfo = TYPE_BINFO (pattern);
8943
8944 /* We should never instantiate a nested class before its enclosing
8945 class; we need to look up the nested class by name before we can
8946 instantiate it, and that lookup should instantiate the enclosing
8947 class. */
8948 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8949 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8950
8951 base_list = NULL_TREE;
8952 if (BINFO_N_BASE_BINFOS (pbinfo))
8953 {
8954 tree pbase_binfo;
8955 tree pushed_scope;
8956 int i;
8957
8958 /* We must enter the scope containing the type, as that is where
8959 the accessibility of types named in dependent bases are
8960 looked up from. */
8961 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8962
8963 /* Substitute into each of the bases to determine the actual
8964 basetypes. */
8965 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8966 {
8967 tree base;
8968 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8969 tree expanded_bases = NULL_TREE;
8970 int idx, len = 1;
8971
8972 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8973 {
8974 expanded_bases =
8975 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8976 args, tf_error, NULL_TREE);
8977 if (expanded_bases == error_mark_node)
8978 continue;
8979
8980 len = TREE_VEC_LENGTH (expanded_bases);
8981 }
8982
8983 for (idx = 0; idx < len; idx++)
8984 {
8985 if (expanded_bases)
8986 /* Extract the already-expanded base class. */
8987 base = TREE_VEC_ELT (expanded_bases, idx);
8988 else
8989 /* Substitute to figure out the base class. */
8990 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8991 NULL_TREE);
8992
8993 if (base == error_mark_node)
8994 continue;
8995
8996 base_list = tree_cons (access, base, base_list);
8997 if (BINFO_VIRTUAL_P (pbase_binfo))
8998 TREE_TYPE (base_list) = integer_type_node;
8999 }
9000 }
9001
9002 /* The list is now in reverse order; correct that. */
9003 base_list = nreverse (base_list);
9004
9005 if (pushed_scope)
9006 pop_scope (pushed_scope);
9007 }
9008 /* Now call xref_basetypes to set up all the base-class
9009 information. */
9010 xref_basetypes (type, base_list);
9011
9012 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9013 (int) ATTR_FLAG_TYPE_IN_PLACE,
9014 args, tf_error, NULL_TREE);
9015 fixup_attribute_variants (type);
9016
9017 /* Now that our base classes are set up, enter the scope of the
9018 class, so that name lookups into base classes, etc. will work
9019 correctly. This is precisely analogous to what we do in
9020 begin_class_definition when defining an ordinary non-template
9021 class, except we also need to push the enclosing classes. */
9022 push_nested_class (type);
9023
9024 /* Now members are processed in the order of declaration. */
9025 for (member = CLASSTYPE_DECL_LIST (pattern);
9026 member; member = TREE_CHAIN (member))
9027 {
9028 tree t = TREE_VALUE (member);
9029
9030 if (TREE_PURPOSE (member))
9031 {
9032 if (TYPE_P (t))
9033 {
9034 /* Build new CLASSTYPE_NESTED_UTDS. */
9035
9036 tree newtag;
9037 bool class_template_p;
9038
9039 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9040 && TYPE_LANG_SPECIFIC (t)
9041 && CLASSTYPE_IS_TEMPLATE (t));
9042 /* If the member is a class template, then -- even after
9043 substitution -- there may be dependent types in the
9044 template argument list for the class. We increment
9045 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9046 that function will assume that no types are dependent
9047 when outside of a template. */
9048 if (class_template_p)
9049 ++processing_template_decl;
9050 newtag = tsubst (t, args, tf_error, NULL_TREE);
9051 if (class_template_p)
9052 --processing_template_decl;
9053 if (newtag == error_mark_node)
9054 continue;
9055
9056 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9057 {
9058 tree name = TYPE_IDENTIFIER (t);
9059
9060 if (class_template_p)
9061 /* Unfortunately, lookup_template_class sets
9062 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9063 instantiation (i.e., for the type of a member
9064 template class nested within a template class.)
9065 This behavior is required for
9066 maybe_process_partial_specialization to work
9067 correctly, but is not accurate in this case;
9068 the TAG is not an instantiation of anything.
9069 (The corresponding TEMPLATE_DECL is an
9070 instantiation, but the TYPE is not.) */
9071 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9072
9073 /* Now, we call pushtag to put this NEWTAG into the scope of
9074 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9075 pushtag calling push_template_decl. We don't have to do
9076 this for enums because it will already have been done in
9077 tsubst_enum. */
9078 if (name)
9079 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9080 pushtag (name, newtag, /*tag_scope=*/ts_current);
9081 }
9082 }
9083 else if (DECL_DECLARES_FUNCTION_P (t))
9084 {
9085 /* Build new TYPE_METHODS. */
9086 tree r;
9087
9088 if (TREE_CODE (t) == TEMPLATE_DECL)
9089 ++processing_template_decl;
9090 r = tsubst (t, args, tf_error, NULL_TREE);
9091 if (TREE_CODE (t) == TEMPLATE_DECL)
9092 --processing_template_decl;
9093 set_current_access_from_decl (r);
9094 finish_member_declaration (r);
9095 /* Instantiate members marked with attribute used. */
9096 if (r != error_mark_node && DECL_PRESERVE_P (r))
9097 mark_used (r);
9098 if (TREE_CODE (r) == FUNCTION_DECL
9099 && DECL_OMP_DECLARE_REDUCTION_P (r))
9100 cp_check_omp_declare_reduction (r);
9101 }
9102 else
9103 {
9104 /* Build new TYPE_FIELDS. */
9105 if (TREE_CODE (t) == STATIC_ASSERT)
9106 {
9107 tree condition;
9108
9109 ++c_inhibit_evaluation_warnings;
9110 condition =
9111 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9112 tf_warning_or_error, NULL_TREE,
9113 /*integral_constant_expression_p=*/true);
9114 --c_inhibit_evaluation_warnings;
9115
9116 finish_static_assert (condition,
9117 STATIC_ASSERT_MESSAGE (t),
9118 STATIC_ASSERT_SOURCE_LOCATION (t),
9119 /*member_p=*/true);
9120 }
9121 else if (TREE_CODE (t) != CONST_DECL)
9122 {
9123 tree r;
9124 tree vec = NULL_TREE;
9125 int len = 1;
9126
9127 /* The file and line for this declaration, to
9128 assist in error message reporting. Since we
9129 called push_tinst_level above, we don't need to
9130 restore these. */
9131 input_location = DECL_SOURCE_LOCATION (t);
9132
9133 if (TREE_CODE (t) == TEMPLATE_DECL)
9134 ++processing_template_decl;
9135 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9136 if (TREE_CODE (t) == TEMPLATE_DECL)
9137 --processing_template_decl;
9138
9139 if (TREE_CODE (r) == TREE_VEC)
9140 {
9141 /* A capture pack became multiple fields. */
9142 vec = r;
9143 len = TREE_VEC_LENGTH (vec);
9144 }
9145
9146 for (int i = 0; i < len; ++i)
9147 {
9148 if (vec)
9149 r = TREE_VEC_ELT (vec, i);
9150 if (VAR_P (r))
9151 {
9152 /* In [temp.inst]:
9153
9154 [t]he initialization (and any associated
9155 side-effects) of a static data member does
9156 not occur unless the static data member is
9157 itself used in a way that requires the
9158 definition of the static data member to
9159 exist.
9160
9161 Therefore, we do not substitute into the
9162 initialized for the static data member here. */
9163 finish_static_data_member_decl
9164 (r,
9165 /*init=*/NULL_TREE,
9166 /*init_const_expr_p=*/false,
9167 /*asmspec_tree=*/NULL_TREE,
9168 /*flags=*/0);
9169 /* Instantiate members marked with attribute used. */
9170 if (r != error_mark_node && DECL_PRESERVE_P (r))
9171 mark_used (r);
9172 }
9173 else if (TREE_CODE (r) == FIELD_DECL)
9174 {
9175 /* Determine whether R has a valid type and can be
9176 completed later. If R is invalid, then its type
9177 is replaced by error_mark_node. */
9178 tree rtype = TREE_TYPE (r);
9179 if (can_complete_type_without_circularity (rtype))
9180 complete_type (rtype);
9181
9182 if (!COMPLETE_TYPE_P (rtype))
9183 {
9184 cxx_incomplete_type_error (r, rtype);
9185 TREE_TYPE (r) = error_mark_node;
9186 }
9187 }
9188
9189 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9190 such a thing will already have been added to the field
9191 list by tsubst_enum in finish_member_declaration in the
9192 CLASSTYPE_NESTED_UTDS case above. */
9193 if (!(TREE_CODE (r) == TYPE_DECL
9194 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9195 && DECL_ARTIFICIAL (r)))
9196 {
9197 set_current_access_from_decl (r);
9198 finish_member_declaration (r);
9199 }
9200 }
9201 }
9202 }
9203 }
9204 else
9205 {
9206 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9207 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9208 {
9209 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9210
9211 tree friend_type = t;
9212 bool adjust_processing_template_decl = false;
9213
9214 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9215 {
9216 /* template <class T> friend class C; */
9217 friend_type = tsubst_friend_class (friend_type, args);
9218 adjust_processing_template_decl = true;
9219 }
9220 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9221 {
9222 /* template <class T> friend class C::D; */
9223 friend_type = tsubst (friend_type, args,
9224 tf_warning_or_error, NULL_TREE);
9225 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9226 friend_type = TREE_TYPE (friend_type);
9227 adjust_processing_template_decl = true;
9228 }
9229 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9230 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9231 {
9232 /* This could be either
9233
9234 friend class T::C;
9235
9236 when dependent_type_p is false or
9237
9238 template <class U> friend class T::C;
9239
9240 otherwise. */
9241 friend_type = tsubst (friend_type, args,
9242 tf_warning_or_error, NULL_TREE);
9243 /* Bump processing_template_decl for correct
9244 dependent_type_p calculation. */
9245 ++processing_template_decl;
9246 if (dependent_type_p (friend_type))
9247 adjust_processing_template_decl = true;
9248 --processing_template_decl;
9249 }
9250 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9251 && hidden_name_p (TYPE_NAME (friend_type)))
9252 {
9253 /* friend class C;
9254
9255 where C hasn't been declared yet. Let's lookup name
9256 from namespace scope directly, bypassing any name that
9257 come from dependent base class. */
9258 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9259
9260 /* The call to xref_tag_from_type does injection for friend
9261 classes. */
9262 push_nested_namespace (ns);
9263 friend_type =
9264 xref_tag_from_type (friend_type, NULL_TREE,
9265 /*tag_scope=*/ts_current);
9266 pop_nested_namespace (ns);
9267 }
9268 else if (uses_template_parms (friend_type))
9269 /* friend class C<T>; */
9270 friend_type = tsubst (friend_type, args,
9271 tf_warning_or_error, NULL_TREE);
9272 /* Otherwise it's
9273
9274 friend class C;
9275
9276 where C is already declared or
9277
9278 friend class C<int>;
9279
9280 We don't have to do anything in these cases. */
9281
9282 if (adjust_processing_template_decl)
9283 /* Trick make_friend_class into realizing that the friend
9284 we're adding is a template, not an ordinary class. It's
9285 important that we use make_friend_class since it will
9286 perform some error-checking and output cross-reference
9287 information. */
9288 ++processing_template_decl;
9289
9290 if (friend_type != error_mark_node)
9291 make_friend_class (type, friend_type, /*complain=*/false);
9292
9293 if (adjust_processing_template_decl)
9294 --processing_template_decl;
9295 }
9296 else
9297 {
9298 /* Build new DECL_FRIENDLIST. */
9299 tree r;
9300
9301 /* The file and line for this declaration, to
9302 assist in error message reporting. Since we
9303 called push_tinst_level above, we don't need to
9304 restore these. */
9305 input_location = DECL_SOURCE_LOCATION (t);
9306
9307 if (TREE_CODE (t) == TEMPLATE_DECL)
9308 {
9309 ++processing_template_decl;
9310 push_deferring_access_checks (dk_no_check);
9311 }
9312
9313 r = tsubst_friend_function (t, args);
9314 add_friend (type, r, /*complain=*/false);
9315 if (TREE_CODE (t) == TEMPLATE_DECL)
9316 {
9317 pop_deferring_access_checks ();
9318 --processing_template_decl;
9319 }
9320 }
9321 }
9322 }
9323
9324 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9325 {
9326 tree decl = lambda_function (type);
9327 if (decl)
9328 {
9329 if (!DECL_TEMPLATE_INFO (decl)
9330 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9331 instantiate_decl (decl, false, false);
9332
9333 /* We need to instantiate the capture list from the template
9334 after we've instantiated the closure members, but before we
9335 consider adding the conversion op. Also keep any captures
9336 that may have been added during instantiation of the op(). */
9337 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9338 tree tmpl_cap
9339 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9340 args, tf_warning_or_error, NULL_TREE,
9341 false, false);
9342
9343 LAMBDA_EXPR_CAPTURE_LIST (expr)
9344 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9345
9346 maybe_add_lambda_conv_op (type);
9347 }
9348 else
9349 gcc_assert (errorcount);
9350 }
9351
9352 /* Set the file and line number information to whatever is given for
9353 the class itself. This puts error messages involving generated
9354 implicit functions at a predictable point, and the same point
9355 that would be used for non-template classes. */
9356 input_location = DECL_SOURCE_LOCATION (typedecl);
9357
9358 unreverse_member_declarations (type);
9359 finish_struct_1 (type);
9360 TYPE_BEING_DEFINED (type) = 0;
9361
9362 /* We don't instantiate default arguments for member functions. 14.7.1:
9363
9364 The implicit instantiation of a class template specialization causes
9365 the implicit instantiation of the declarations, but not of the
9366 definitions or default arguments, of the class member functions,
9367 member classes, static data members and member templates.... */
9368
9369 /* Some typedefs referenced from within the template code need to be access
9370 checked at template instantiation time, i.e now. These types were
9371 added to the template at parsing time. Let's get those and perform
9372 the access checks then. */
9373 perform_typedefs_access_check (pattern, args);
9374 perform_deferred_access_checks (tf_warning_or_error);
9375 pop_nested_class ();
9376 maximum_field_alignment = saved_maximum_field_alignment;
9377 if (!fn_context)
9378 pop_from_top_level ();
9379 pop_deferring_access_checks ();
9380 pop_tinst_level ();
9381
9382 /* The vtable for a template class can be emitted in any translation
9383 unit in which the class is instantiated. When there is no key
9384 method, however, finish_struct_1 will already have added TYPE to
9385 the keyed_classes list. */
9386 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9387 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9388
9389 return type;
9390 }
9391
9392 /* Wrapper for instantiate_class_template_1. */
9393
9394 tree
9395 instantiate_class_template (tree type)
9396 {
9397 tree ret;
9398 timevar_push (TV_TEMPLATE_INST);
9399 ret = instantiate_class_template_1 (type);
9400 timevar_pop (TV_TEMPLATE_INST);
9401 return ret;
9402 }
9403
9404 static tree
9405 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9406 {
9407 tree r;
9408
9409 if (!t)
9410 r = t;
9411 else if (TYPE_P (t))
9412 r = tsubst (t, args, complain, in_decl);
9413 else
9414 {
9415 if (!(complain & tf_warning))
9416 ++c_inhibit_evaluation_warnings;
9417 r = tsubst_expr (t, args, complain, in_decl,
9418 /*integral_constant_expression_p=*/true);
9419 if (!(complain & tf_warning))
9420 --c_inhibit_evaluation_warnings;
9421 }
9422 return r;
9423 }
9424
9425 /* Given a function parameter pack TMPL_PARM and some function parameters
9426 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9427 and set *SPEC_P to point at the next point in the list. */
9428
9429 static tree
9430 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9431 {
9432 /* Collect all of the extra "packed" parameters into an
9433 argument pack. */
9434 tree parmvec;
9435 tree parmtypevec;
9436 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9437 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9438 tree spec_parm = *spec_p;
9439 int i, len;
9440
9441 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9442 if (tmpl_parm
9443 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9444 break;
9445
9446 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9447 parmvec = make_tree_vec (len);
9448 parmtypevec = make_tree_vec (len);
9449 spec_parm = *spec_p;
9450 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9451 {
9452 TREE_VEC_ELT (parmvec, i) = spec_parm;
9453 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9454 }
9455
9456 /* Build the argument packs. */
9457 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9458 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9459 TREE_TYPE (argpack) = argtypepack;
9460 *spec_p = spec_parm;
9461
9462 return argpack;
9463 }
9464
9465 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9466 NONTYPE_ARGUMENT_PACK. */
9467
9468 static tree
9469 make_fnparm_pack (tree spec_parm)
9470 {
9471 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9472 }
9473
9474 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9475 pack expansion. */
9476
9477 static bool
9478 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9479 {
9480 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9481 if (i >= TREE_VEC_LENGTH (vec))
9482 return false;
9483 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9484 }
9485
9486
9487 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9488
9489 static tree
9490 make_argument_pack_select (tree arg_pack, unsigned index)
9491 {
9492 tree aps = make_node (ARGUMENT_PACK_SELECT);
9493
9494 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9495 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9496
9497 return aps;
9498 }
9499
9500 /* This is a subroutine of tsubst_pack_expansion.
9501
9502 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9503 mechanism to store the (non complete list of) arguments of the
9504 substitution and return a non substituted pack expansion, in order
9505 to wait for when we have enough arguments to really perform the
9506 substitution. */
9507
9508 static bool
9509 use_pack_expansion_extra_args_p (tree parm_packs,
9510 int arg_pack_len,
9511 bool has_empty_arg)
9512 {
9513 /* If one pack has an expansion and another pack has a normal
9514 argument or if one pack has an empty argument and an another
9515 one hasn't then tsubst_pack_expansion cannot perform the
9516 substitution and need to fall back on the
9517 PACK_EXPANSION_EXTRA mechanism. */
9518 if (parm_packs == NULL_TREE)
9519 return false;
9520 else if (has_empty_arg)
9521 return true;
9522
9523 bool has_expansion_arg = false;
9524 for (int i = 0 ; i < arg_pack_len; ++i)
9525 {
9526 bool has_non_expansion_arg = false;
9527 for (tree parm_pack = parm_packs;
9528 parm_pack;
9529 parm_pack = TREE_CHAIN (parm_pack))
9530 {
9531 tree arg = TREE_VALUE (parm_pack);
9532
9533 if (argument_pack_element_is_expansion_p (arg, i))
9534 has_expansion_arg = true;
9535 else
9536 has_non_expansion_arg = true;
9537 }
9538
9539 if (has_expansion_arg && has_non_expansion_arg)
9540 return true;
9541 }
9542 return false;
9543 }
9544
9545 /* [temp.variadic]/6 says that:
9546
9547 The instantiation of a pack expansion [...]
9548 produces a list E1,E2, ..., En, where N is the number of elements
9549 in the pack expansion parameters.
9550
9551 This subroutine of tsubst_pack_expansion produces one of these Ei.
9552
9553 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9554 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9555 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9556 INDEX is the index 'i' of the element Ei to produce. ARGS,
9557 COMPLAIN, and IN_DECL are the same parameters as for the
9558 tsubst_pack_expansion function.
9559
9560 The function returns the resulting Ei upon successful completion,
9561 or error_mark_node.
9562
9563 Note that this function possibly modifies the ARGS parameter, so
9564 it's the responsibility of the caller to restore it. */
9565
9566 static tree
9567 gen_elem_of_pack_expansion_instantiation (tree pattern,
9568 tree parm_packs,
9569 unsigned index,
9570 tree args /* This parm gets
9571 modified. */,
9572 tsubst_flags_t complain,
9573 tree in_decl)
9574 {
9575 tree t;
9576 bool ith_elem_is_expansion = false;
9577
9578 /* For each parameter pack, change the substitution of the parameter
9579 pack to the ith argument in its argument pack, then expand the
9580 pattern. */
9581 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9582 {
9583 tree parm = TREE_PURPOSE (pack);
9584 tree arg_pack = TREE_VALUE (pack);
9585 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9586
9587 ith_elem_is_expansion |=
9588 argument_pack_element_is_expansion_p (arg_pack, index);
9589
9590 /* Select the Ith argument from the pack. */
9591 if (TREE_CODE (parm) == PARM_DECL
9592 || TREE_CODE (parm) == FIELD_DECL)
9593 {
9594 if (index == 0)
9595 {
9596 aps = make_argument_pack_select (arg_pack, index);
9597 mark_used (parm);
9598 register_local_specialization (aps, parm);
9599 }
9600 else
9601 aps = retrieve_local_specialization (parm);
9602 }
9603 else
9604 {
9605 int idx, level;
9606 template_parm_level_and_index (parm, &level, &idx);
9607
9608 if (index == 0)
9609 {
9610 aps = make_argument_pack_select (arg_pack, index);
9611 /* Update the corresponding argument. */
9612 TMPL_ARG (args, level, idx) = aps;
9613 }
9614 else
9615 /* Re-use the ARGUMENT_PACK_SELECT. */
9616 aps = TMPL_ARG (args, level, idx);
9617 }
9618 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9619 }
9620
9621 /* Substitute into the PATTERN with the (possibly altered)
9622 arguments. */
9623 if (pattern == in_decl)
9624 /* Expanding a fixed parameter pack from
9625 coerce_template_parameter_pack. */
9626 t = tsubst_decl (pattern, args, complain);
9627 else if (!TYPE_P (pattern))
9628 t = tsubst_expr (pattern, args, complain, in_decl,
9629 /*integral_constant_expression_p=*/false);
9630 else
9631 t = tsubst (pattern, args, complain, in_decl);
9632
9633 /* If the Ith argument pack element is a pack expansion, then
9634 the Ith element resulting from the substituting is going to
9635 be a pack expansion as well. */
9636 if (ith_elem_is_expansion)
9637 t = make_pack_expansion (t);
9638
9639 return t;
9640 }
9641
9642 /* Substitute ARGS into T, which is an pack expansion
9643 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9644 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9645 (if only a partial substitution could be performed) or
9646 ERROR_MARK_NODE if there was an error. */
9647 tree
9648 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9649 tree in_decl)
9650 {
9651 tree pattern;
9652 tree pack, packs = NULL_TREE;
9653 bool unsubstituted_packs = false;
9654 int i, len = -1;
9655 tree result;
9656 struct pointer_map_t *saved_local_specializations = NULL;
9657 bool need_local_specializations = false;
9658 int levels;
9659
9660 gcc_assert (PACK_EXPANSION_P (t));
9661 pattern = PACK_EXPANSION_PATTERN (t);
9662
9663 /* Add in any args remembered from an earlier partial instantiation. */
9664 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9665
9666 levels = TMPL_ARGS_DEPTH (args);
9667
9668 /* Determine the argument packs that will instantiate the parameter
9669 packs used in the expansion expression. While we're at it,
9670 compute the number of arguments to be expanded and make sure it
9671 is consistent. */
9672 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9673 pack = TREE_CHAIN (pack))
9674 {
9675 tree parm_pack = TREE_VALUE (pack);
9676 tree arg_pack = NULL_TREE;
9677 tree orig_arg = NULL_TREE;
9678 int level = 0;
9679
9680 if (TREE_CODE (parm_pack) == BASES)
9681 {
9682 if (BASES_DIRECT (parm_pack))
9683 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9684 args, complain, in_decl, false));
9685 else
9686 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9687 args, complain, in_decl, false));
9688 }
9689 if (TREE_CODE (parm_pack) == PARM_DECL)
9690 {
9691 if (PACK_EXPANSION_LOCAL_P (t))
9692 arg_pack = retrieve_local_specialization (parm_pack);
9693 else
9694 {
9695 /* We can't rely on local_specializations for a parameter
9696 name used later in a function declaration (such as in a
9697 late-specified return type). Even if it exists, it might
9698 have the wrong value for a recursive call. Just make a
9699 dummy decl, since it's only used for its type. */
9700 arg_pack = tsubst_decl (parm_pack, args, complain);
9701 if (arg_pack && DECL_PACK_P (arg_pack))
9702 /* Partial instantiation of the parm_pack, we can't build
9703 up an argument pack yet. */
9704 arg_pack = NULL_TREE;
9705 else
9706 arg_pack = make_fnparm_pack (arg_pack);
9707 need_local_specializations = true;
9708 }
9709 }
9710 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9711 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9712 else
9713 {
9714 int idx;
9715 template_parm_level_and_index (parm_pack, &level, &idx);
9716
9717 if (level <= levels)
9718 arg_pack = TMPL_ARG (args, level, idx);
9719 }
9720
9721 orig_arg = arg_pack;
9722 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9723 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9724
9725 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9726 /* This can only happen if we forget to expand an argument
9727 pack somewhere else. Just return an error, silently. */
9728 {
9729 result = make_tree_vec (1);
9730 TREE_VEC_ELT (result, 0) = error_mark_node;
9731 return result;
9732 }
9733
9734 if (arg_pack)
9735 {
9736 int my_len =
9737 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9738
9739 /* Don't bother trying to do a partial substitution with
9740 incomplete packs; we'll try again after deduction. */
9741 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9742 return t;
9743
9744 if (len < 0)
9745 len = my_len;
9746 else if (len != my_len)
9747 {
9748 if (!(complain & tf_error))
9749 /* Fail quietly. */;
9750 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9751 error ("mismatched argument pack lengths while expanding "
9752 "%<%T%>",
9753 pattern);
9754 else
9755 error ("mismatched argument pack lengths while expanding "
9756 "%<%E%>",
9757 pattern);
9758 return error_mark_node;
9759 }
9760
9761 /* Keep track of the parameter packs and their corresponding
9762 argument packs. */
9763 packs = tree_cons (parm_pack, arg_pack, packs);
9764 TREE_TYPE (packs) = orig_arg;
9765 }
9766 else
9767 {
9768 /* We can't substitute for this parameter pack. We use a flag as
9769 well as the missing_level counter because function parameter
9770 packs don't have a level. */
9771 unsubstituted_packs = true;
9772 }
9773 }
9774
9775 /* We cannot expand this expansion expression, because we don't have
9776 all of the argument packs we need. */
9777 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9778 {
9779 /* We got some full packs, but we can't substitute them in until we
9780 have values for all the packs. So remember these until then. */
9781
9782 t = make_pack_expansion (pattern);
9783 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9784 return t;
9785 }
9786 else if (unsubstituted_packs)
9787 {
9788 /* There were no real arguments, we're just replacing a parameter
9789 pack with another version of itself. Substitute into the
9790 pattern and return a PACK_EXPANSION_*. The caller will need to
9791 deal with that. */
9792 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9793 t = tsubst_expr (pattern, args, complain, in_decl,
9794 /*integral_constant_expression_p=*/false);
9795 else
9796 t = tsubst (pattern, args, complain, in_decl);
9797 t = make_pack_expansion (t);
9798 return t;
9799 }
9800
9801 gcc_assert (len >= 0);
9802
9803 if (need_local_specializations)
9804 {
9805 /* We're in a late-specified return type, so create our own local
9806 specializations map; the current map is either NULL or (in the
9807 case of recursive unification) might have bindings that we don't
9808 want to use or alter. */
9809 saved_local_specializations = local_specializations;
9810 local_specializations = pointer_map_create ();
9811 }
9812
9813 /* For each argument in each argument pack, substitute into the
9814 pattern. */
9815 result = make_tree_vec (len);
9816 for (i = 0; i < len; ++i)
9817 {
9818 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9819 i,
9820 args, complain,
9821 in_decl);
9822 TREE_VEC_ELT (result, i) = t;
9823 if (t == error_mark_node)
9824 {
9825 result = error_mark_node;
9826 break;
9827 }
9828 }
9829
9830 /* Update ARGS to restore the substitution from parameter packs to
9831 their argument packs. */
9832 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9833 {
9834 tree parm = TREE_PURPOSE (pack);
9835
9836 if (TREE_CODE (parm) == PARM_DECL
9837 || TREE_CODE (parm) == FIELD_DECL)
9838 register_local_specialization (TREE_TYPE (pack), parm);
9839 else
9840 {
9841 int idx, level;
9842
9843 if (TREE_VALUE (pack) == NULL_TREE)
9844 continue;
9845
9846 template_parm_level_and_index (parm, &level, &idx);
9847
9848 /* Update the corresponding argument. */
9849 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9850 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9851 TREE_TYPE (pack);
9852 else
9853 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9854 }
9855 }
9856
9857 if (need_local_specializations)
9858 {
9859 pointer_map_destroy (local_specializations);
9860 local_specializations = saved_local_specializations;
9861 }
9862
9863 return result;
9864 }
9865
9866 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9867 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9868 parameter packs; all parms generated from a function parameter pack will
9869 have the same DECL_PARM_INDEX. */
9870
9871 tree
9872 get_pattern_parm (tree parm, tree tmpl)
9873 {
9874 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9875 tree patparm;
9876
9877 if (DECL_ARTIFICIAL (parm))
9878 {
9879 for (patparm = DECL_ARGUMENTS (pattern);
9880 patparm; patparm = DECL_CHAIN (patparm))
9881 if (DECL_ARTIFICIAL (patparm)
9882 && DECL_NAME (parm) == DECL_NAME (patparm))
9883 break;
9884 }
9885 else
9886 {
9887 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9888 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9889 gcc_assert (DECL_PARM_INDEX (patparm)
9890 == DECL_PARM_INDEX (parm));
9891 }
9892
9893 return patparm;
9894 }
9895
9896 /* Substitute ARGS into the vector or list of template arguments T. */
9897
9898 static tree
9899 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9900 {
9901 tree orig_t = t;
9902 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9903 tree *elts;
9904
9905 if (t == error_mark_node)
9906 return error_mark_node;
9907
9908 len = TREE_VEC_LENGTH (t);
9909 elts = XALLOCAVEC (tree, len);
9910
9911 for (i = 0; i < len; i++)
9912 {
9913 tree orig_arg = TREE_VEC_ELT (t, i);
9914 tree new_arg;
9915
9916 if (TREE_CODE (orig_arg) == TREE_VEC)
9917 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9918 else if (PACK_EXPANSION_P (orig_arg))
9919 {
9920 /* Substitute into an expansion expression. */
9921 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9922
9923 if (TREE_CODE (new_arg) == TREE_VEC)
9924 /* Add to the expanded length adjustment the number of
9925 expanded arguments. We subtract one from this
9926 measurement, because the argument pack expression
9927 itself is already counted as 1 in
9928 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9929 the argument pack is empty. */
9930 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9931 }
9932 else if (ARGUMENT_PACK_P (orig_arg))
9933 {
9934 /* Substitute into each of the arguments. */
9935 new_arg = TYPE_P (orig_arg)
9936 ? cxx_make_type (TREE_CODE (orig_arg))
9937 : make_node (TREE_CODE (orig_arg));
9938
9939 SET_ARGUMENT_PACK_ARGS (
9940 new_arg,
9941 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9942 args, complain, in_decl));
9943
9944 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9945 new_arg = error_mark_node;
9946
9947 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9948 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9949 complain, in_decl);
9950 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9951
9952 if (TREE_TYPE (new_arg) == error_mark_node)
9953 new_arg = error_mark_node;
9954 }
9955 }
9956 else
9957 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9958
9959 if (new_arg == error_mark_node)
9960 return error_mark_node;
9961
9962 elts[i] = new_arg;
9963 if (new_arg != orig_arg)
9964 need_new = 1;
9965 }
9966
9967 if (!need_new)
9968 return t;
9969
9970 /* Make space for the expanded arguments coming from template
9971 argument packs. */
9972 t = make_tree_vec (len + expanded_len_adjust);
9973 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9974 arguments for a member template.
9975 In that case each TREE_VEC in ORIG_T represents a level of template
9976 arguments, and ORIG_T won't carry any non defaulted argument count.
9977 It will rather be the nested TREE_VECs that will carry one.
9978 In other words, ORIG_T carries a non defaulted argument count only
9979 if it doesn't contain any nested TREE_VEC. */
9980 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9981 {
9982 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9983 count += expanded_len_adjust;
9984 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9985 }
9986 for (i = 0, out = 0; i < len; i++)
9987 {
9988 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9989 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9990 && TREE_CODE (elts[i]) == TREE_VEC)
9991 {
9992 int idx;
9993
9994 /* Now expand the template argument pack "in place". */
9995 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9996 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9997 }
9998 else
9999 {
10000 TREE_VEC_ELT (t, out) = elts[i];
10001 out++;
10002 }
10003 }
10004
10005 return t;
10006 }
10007
10008 /* Return the result of substituting ARGS into the template parameters
10009 given by PARMS. If there are m levels of ARGS and m + n levels of
10010 PARMS, then the result will contain n levels of PARMS. For
10011 example, if PARMS is `template <class T> template <class U>
10012 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10013 result will be `template <int*, double, class V>'. */
10014
10015 static tree
10016 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
10017 {
10018 tree r = NULL_TREE;
10019 tree* new_parms;
10020
10021 /* When substituting into a template, we must set
10022 PROCESSING_TEMPLATE_DECL as the template parameters may be
10023 dependent if they are based on one-another, and the dependency
10024 predicates are short-circuit outside of templates. */
10025 ++processing_template_decl;
10026
10027 for (new_parms = &r;
10028 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
10029 new_parms = &(TREE_CHAIN (*new_parms)),
10030 parms = TREE_CHAIN (parms))
10031 {
10032 tree new_vec =
10033 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
10034 int i;
10035
10036 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
10037 {
10038 tree tuple;
10039
10040 if (parms == error_mark_node)
10041 continue;
10042
10043 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
10044
10045 if (tuple == error_mark_node)
10046 continue;
10047
10048 TREE_VEC_ELT (new_vec, i) =
10049 tsubst_template_parm (tuple, args, complain);
10050 }
10051
10052 *new_parms =
10053 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
10054 - TMPL_ARGS_DEPTH (args)),
10055 new_vec, NULL_TREE);
10056 }
10057
10058 --processing_template_decl;
10059
10060 return r;
10061 }
10062
10063 /* Return the result of substituting ARGS into one template parameter
10064 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10065 parameter and which TREE_PURPOSE is the default argument of the
10066 template parameter. */
10067
10068 static tree
10069 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
10070 {
10071 tree default_value, parm_decl;
10072
10073 if (args == NULL_TREE
10074 || t == NULL_TREE
10075 || t == error_mark_node)
10076 return t;
10077
10078 gcc_assert (TREE_CODE (t) == TREE_LIST);
10079
10080 default_value = TREE_PURPOSE (t);
10081 parm_decl = TREE_VALUE (t);
10082
10083 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
10084 if (TREE_CODE (parm_decl) == PARM_DECL
10085 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
10086 parm_decl = error_mark_node;
10087 default_value = tsubst_template_arg (default_value, args,
10088 complain, NULL_TREE);
10089
10090 return build_tree_list (default_value, parm_decl);
10091 }
10092
10093 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10094 type T. If T is not an aggregate or enumeration type, it is
10095 handled as if by tsubst. IN_DECL is as for tsubst. If
10096 ENTERING_SCOPE is nonzero, T is the context for a template which
10097 we are presently tsubst'ing. Return the substituted value. */
10098
10099 static tree
10100 tsubst_aggr_type (tree t,
10101 tree args,
10102 tsubst_flags_t complain,
10103 tree in_decl,
10104 int entering_scope)
10105 {
10106 if (t == NULL_TREE)
10107 return NULL_TREE;
10108
10109 switch (TREE_CODE (t))
10110 {
10111 case RECORD_TYPE:
10112 if (TYPE_PTRMEMFUNC_P (t))
10113 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
10114
10115 /* Else fall through. */
10116 case ENUMERAL_TYPE:
10117 case UNION_TYPE:
10118 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
10119 {
10120 tree argvec;
10121 tree context;
10122 tree r;
10123 int saved_unevaluated_operand;
10124 int saved_inhibit_evaluation_warnings;
10125
10126 /* In "sizeof(X<I>)" we need to evaluate "I". */
10127 saved_unevaluated_operand = cp_unevaluated_operand;
10128 cp_unevaluated_operand = 0;
10129 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10130 c_inhibit_evaluation_warnings = 0;
10131
10132 /* First, determine the context for the type we are looking
10133 up. */
10134 context = TYPE_CONTEXT (t);
10135 if (context && TYPE_P (context))
10136 {
10137 context = tsubst_aggr_type (context, args, complain,
10138 in_decl, /*entering_scope=*/1);
10139 /* If context is a nested class inside a class template,
10140 it may still need to be instantiated (c++/33959). */
10141 context = complete_type (context);
10142 }
10143
10144 /* Then, figure out what arguments are appropriate for the
10145 type we are trying to find. For example, given:
10146
10147 template <class T> struct S;
10148 template <class T, class U> void f(T, U) { S<U> su; }
10149
10150 and supposing that we are instantiating f<int, double>,
10151 then our ARGS will be {int, double}, but, when looking up
10152 S we only want {double}. */
10153 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10154 complain, in_decl);
10155 if (argvec == error_mark_node)
10156 r = error_mark_node;
10157 else
10158 {
10159 r = lookup_template_class (t, argvec, in_decl, context,
10160 entering_scope, complain);
10161 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10162 }
10163
10164 cp_unevaluated_operand = saved_unevaluated_operand;
10165 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10166
10167 return r;
10168 }
10169 else
10170 /* This is not a template type, so there's nothing to do. */
10171 return t;
10172
10173 default:
10174 return tsubst (t, args, complain, in_decl);
10175 }
10176 }
10177
10178 /* Substitute into the default argument ARG (a default argument for
10179 FN), which has the indicated TYPE. */
10180
10181 tree
10182 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10183 {
10184 tree saved_class_ptr = NULL_TREE;
10185 tree saved_class_ref = NULL_TREE;
10186 int errs = errorcount + sorrycount;
10187
10188 /* This can happen in invalid code. */
10189 if (TREE_CODE (arg) == DEFAULT_ARG)
10190 return arg;
10191
10192 /* This default argument came from a template. Instantiate the
10193 default argument here, not in tsubst. In the case of
10194 something like:
10195
10196 template <class T>
10197 struct S {
10198 static T t();
10199 void f(T = t());
10200 };
10201
10202 we must be careful to do name lookup in the scope of S<T>,
10203 rather than in the current class. */
10204 push_access_scope (fn);
10205 /* The "this" pointer is not valid in a default argument. */
10206 if (cfun)
10207 {
10208 saved_class_ptr = current_class_ptr;
10209 cp_function_chain->x_current_class_ptr = NULL_TREE;
10210 saved_class_ref = current_class_ref;
10211 cp_function_chain->x_current_class_ref = NULL_TREE;
10212 }
10213
10214 push_deferring_access_checks(dk_no_deferred);
10215 /* The default argument expression may cause implicitly defined
10216 member functions to be synthesized, which will result in garbage
10217 collection. We must treat this situation as if we were within
10218 the body of function so as to avoid collecting live data on the
10219 stack. */
10220 ++function_depth;
10221 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10222 complain, NULL_TREE,
10223 /*integral_constant_expression_p=*/false);
10224 --function_depth;
10225 pop_deferring_access_checks();
10226
10227 /* Restore the "this" pointer. */
10228 if (cfun)
10229 {
10230 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10231 cp_function_chain->x_current_class_ref = saved_class_ref;
10232 }
10233
10234 if (errorcount+sorrycount > errs
10235 && (complain & tf_warning_or_error))
10236 inform (input_location,
10237 " when instantiating default argument for call to %D", fn);
10238
10239 /* Make sure the default argument is reasonable. */
10240 arg = check_default_argument (type, arg, complain);
10241
10242 pop_access_scope (fn);
10243
10244 return arg;
10245 }
10246
10247 /* Substitute into all the default arguments for FN. */
10248
10249 static void
10250 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10251 {
10252 tree arg;
10253 tree tmpl_args;
10254
10255 tmpl_args = DECL_TI_ARGS (fn);
10256
10257 /* If this function is not yet instantiated, we certainly don't need
10258 its default arguments. */
10259 if (uses_template_parms (tmpl_args))
10260 return;
10261 /* Don't do this again for clones. */
10262 if (DECL_CLONED_FUNCTION_P (fn))
10263 return;
10264
10265 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10266 arg;
10267 arg = TREE_CHAIN (arg))
10268 if (TREE_PURPOSE (arg))
10269 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10270 TREE_VALUE (arg),
10271 TREE_PURPOSE (arg),
10272 complain);
10273 }
10274
10275 /* Substitute the ARGS into the T, which is a _DECL. Return the
10276 result of the substitution. Issue error and warning messages under
10277 control of COMPLAIN. */
10278
10279 static tree
10280 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10281 {
10282 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10283 location_t saved_loc;
10284 tree r = NULL_TREE;
10285 tree in_decl = t;
10286 hashval_t hash = 0;
10287
10288 /* Set the filename and linenumber to improve error-reporting. */
10289 saved_loc = input_location;
10290 input_location = DECL_SOURCE_LOCATION (t);
10291
10292 switch (TREE_CODE (t))
10293 {
10294 case TEMPLATE_DECL:
10295 {
10296 /* We can get here when processing a member function template,
10297 member class template, or template template parameter. */
10298 tree decl = DECL_TEMPLATE_RESULT (t);
10299 tree spec;
10300 tree tmpl_args;
10301 tree full_args;
10302
10303 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10304 {
10305 /* Template template parameter is treated here. */
10306 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10307 if (new_type == error_mark_node)
10308 RETURN (error_mark_node);
10309 /* If we get a real template back, return it. This can happen in
10310 the context of most_specialized_class. */
10311 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10312 return new_type;
10313
10314 r = copy_decl (t);
10315 DECL_CHAIN (r) = NULL_TREE;
10316 TREE_TYPE (r) = new_type;
10317 DECL_TEMPLATE_RESULT (r)
10318 = build_decl (DECL_SOURCE_LOCATION (decl),
10319 TYPE_DECL, DECL_NAME (decl), new_type);
10320 DECL_TEMPLATE_PARMS (r)
10321 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10322 complain);
10323 TYPE_NAME (new_type) = r;
10324 break;
10325 }
10326
10327 /* We might already have an instance of this template.
10328 The ARGS are for the surrounding class type, so the
10329 full args contain the tsubst'd args for the context,
10330 plus the innermost args from the template decl. */
10331 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10332 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10333 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10334 /* Because this is a template, the arguments will still be
10335 dependent, even after substitution. If
10336 PROCESSING_TEMPLATE_DECL is not set, the dependency
10337 predicates will short-circuit. */
10338 ++processing_template_decl;
10339 full_args = tsubst_template_args (tmpl_args, args,
10340 complain, in_decl);
10341 --processing_template_decl;
10342 if (full_args == error_mark_node)
10343 RETURN (error_mark_node);
10344
10345 /* If this is a default template template argument,
10346 tsubst might not have changed anything. */
10347 if (full_args == tmpl_args)
10348 RETURN (t);
10349
10350 hash = hash_tmpl_and_args (t, full_args);
10351 spec = retrieve_specialization (t, full_args, hash);
10352 if (spec != NULL_TREE)
10353 {
10354 r = spec;
10355 break;
10356 }
10357
10358 /* Make a new template decl. It will be similar to the
10359 original, but will record the current template arguments.
10360 We also create a new function declaration, which is just
10361 like the old one, but points to this new template, rather
10362 than the old one. */
10363 r = copy_decl (t);
10364 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10365 DECL_CHAIN (r) = NULL_TREE;
10366
10367 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10368
10369 if (TREE_CODE (decl) == TYPE_DECL
10370 && !TYPE_DECL_ALIAS_P (decl))
10371 {
10372 tree new_type;
10373 ++processing_template_decl;
10374 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10375 --processing_template_decl;
10376 if (new_type == error_mark_node)
10377 RETURN (error_mark_node);
10378
10379 TREE_TYPE (r) = new_type;
10380 /* For a partial specialization, we need to keep pointing to
10381 the primary template. */
10382 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10383 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10384 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10385 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10386 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10387 }
10388 else
10389 {
10390 tree new_decl;
10391 ++processing_template_decl;
10392 new_decl = tsubst (decl, args, complain, in_decl);
10393 --processing_template_decl;
10394 if (new_decl == error_mark_node)
10395 RETURN (error_mark_node);
10396
10397 DECL_TEMPLATE_RESULT (r) = new_decl;
10398 DECL_TI_TEMPLATE (new_decl) = r;
10399 TREE_TYPE (r) = TREE_TYPE (new_decl);
10400 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10401 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10402 }
10403
10404 SET_DECL_IMPLICIT_INSTANTIATION (r);
10405 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10406 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10407
10408 /* The template parameters for this new template are all the
10409 template parameters for the old template, except the
10410 outermost level of parameters. */
10411 DECL_TEMPLATE_PARMS (r)
10412 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10413 complain);
10414
10415 if (PRIMARY_TEMPLATE_P (t))
10416 DECL_PRIMARY_TEMPLATE (r) = r;
10417
10418 if (TREE_CODE (decl) != TYPE_DECL)
10419 /* Record this non-type partial instantiation. */
10420 register_specialization (r, t,
10421 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10422 false, hash);
10423 }
10424 break;
10425
10426 case FUNCTION_DECL:
10427 {
10428 tree ctx;
10429 tree argvec = NULL_TREE;
10430 tree *friends;
10431 tree gen_tmpl;
10432 tree type;
10433 int member;
10434 int args_depth;
10435 int parms_depth;
10436
10437 /* Nobody should be tsubst'ing into non-template functions. */
10438 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10439
10440 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10441 {
10442 tree spec;
10443 bool dependent_p;
10444
10445 /* If T is not dependent, just return it. We have to
10446 increment PROCESSING_TEMPLATE_DECL because
10447 value_dependent_expression_p assumes that nothing is
10448 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10449 ++processing_template_decl;
10450 dependent_p = value_dependent_expression_p (t);
10451 --processing_template_decl;
10452 if (!dependent_p)
10453 RETURN (t);
10454
10455 /* Calculate the most general template of which R is a
10456 specialization, and the complete set of arguments used to
10457 specialize R. */
10458 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10459 argvec = tsubst_template_args (DECL_TI_ARGS
10460 (DECL_TEMPLATE_RESULT
10461 (DECL_TI_TEMPLATE (t))),
10462 args, complain, in_decl);
10463 if (argvec == error_mark_node)
10464 RETURN (error_mark_node);
10465
10466 /* Check to see if we already have this specialization. */
10467 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10468 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10469
10470 if (spec)
10471 {
10472 r = spec;
10473 break;
10474 }
10475
10476 /* We can see more levels of arguments than parameters if
10477 there was a specialization of a member template, like
10478 this:
10479
10480 template <class T> struct S { template <class U> void f(); }
10481 template <> template <class U> void S<int>::f(U);
10482
10483 Here, we'll be substituting into the specialization,
10484 because that's where we can find the code we actually
10485 want to generate, but we'll have enough arguments for
10486 the most general template.
10487
10488 We also deal with the peculiar case:
10489
10490 template <class T> struct S {
10491 template <class U> friend void f();
10492 };
10493 template <class U> void f() {}
10494 template S<int>;
10495 template void f<double>();
10496
10497 Here, the ARGS for the instantiation of will be {int,
10498 double}. But, we only need as many ARGS as there are
10499 levels of template parameters in CODE_PATTERN. We are
10500 careful not to get fooled into reducing the ARGS in
10501 situations like:
10502
10503 template <class T> struct S { template <class U> void f(U); }
10504 template <class T> template <> void S<T>::f(int) {}
10505
10506 which we can spot because the pattern will be a
10507 specialization in this case. */
10508 args_depth = TMPL_ARGS_DEPTH (args);
10509 parms_depth =
10510 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10511 if (args_depth > parms_depth
10512 && !DECL_TEMPLATE_SPECIALIZATION (t))
10513 args = get_innermost_template_args (args, parms_depth);
10514 }
10515 else
10516 {
10517 /* This special case arises when we have something like this:
10518
10519 template <class T> struct S {
10520 friend void f<int>(int, double);
10521 };
10522
10523 Here, the DECL_TI_TEMPLATE for the friend declaration
10524 will be an IDENTIFIER_NODE. We are being called from
10525 tsubst_friend_function, and we want only to create a
10526 new decl (R) with appropriate types so that we can call
10527 determine_specialization. */
10528 gen_tmpl = NULL_TREE;
10529 }
10530
10531 if (DECL_CLASS_SCOPE_P (t))
10532 {
10533 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10534 member = 2;
10535 else
10536 member = 1;
10537 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10538 complain, t, /*entering_scope=*/1);
10539 }
10540 else
10541 {
10542 member = 0;
10543 ctx = DECL_CONTEXT (t);
10544 }
10545 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10546 if (type == error_mark_node)
10547 RETURN (error_mark_node);
10548
10549 /* If we hit excessive deduction depth, the type is bogus even if
10550 it isn't error_mark_node, so don't build a decl. */
10551 if (excessive_deduction_depth)
10552 RETURN (error_mark_node);
10553
10554 /* We do NOT check for matching decls pushed separately at this
10555 point, as they may not represent instantiations of this
10556 template, and in any case are considered separate under the
10557 discrete model. */
10558 r = copy_decl (t);
10559 DECL_USE_TEMPLATE (r) = 0;
10560 TREE_TYPE (r) = type;
10561 /* Clear out the mangled name and RTL for the instantiation. */
10562 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10563 SET_DECL_RTL (r, NULL);
10564 /* Leave DECL_INITIAL set on deleted instantiations. */
10565 if (!DECL_DELETED_FN (r))
10566 DECL_INITIAL (r) = NULL_TREE;
10567 DECL_CONTEXT (r) = ctx;
10568
10569 /* OpenMP UDRs have the only argument a reference to the declared
10570 type. We want to diagnose if the declared type is a reference,
10571 which is invalid, but as references to references are usually
10572 quietly merged, diagnose it here. */
10573 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10574 {
10575 tree argtype
10576 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10577 argtype = tsubst (argtype, args, complain, in_decl);
10578 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10579 error_at (DECL_SOURCE_LOCATION (t),
10580 "reference type %qT in "
10581 "%<#pragma omp declare reduction%>", argtype);
10582 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10583 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10584 argtype);
10585 }
10586
10587 if (member && DECL_CONV_FN_P (r))
10588 /* Type-conversion operator. Reconstruct the name, in
10589 case it's the name of one of the template's parameters. */
10590 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10591
10592 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10593 complain, t);
10594 DECL_RESULT (r) = NULL_TREE;
10595
10596 TREE_STATIC (r) = 0;
10597 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10598 DECL_EXTERNAL (r) = 1;
10599 /* If this is an instantiation of a function with internal
10600 linkage, we already know what object file linkage will be
10601 assigned to the instantiation. */
10602 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10603 DECL_DEFER_OUTPUT (r) = 0;
10604 DECL_CHAIN (r) = NULL_TREE;
10605 DECL_PENDING_INLINE_INFO (r) = 0;
10606 DECL_PENDING_INLINE_P (r) = 0;
10607 DECL_SAVED_TREE (r) = NULL_TREE;
10608 DECL_STRUCT_FUNCTION (r) = NULL;
10609 TREE_USED (r) = 0;
10610 /* We'll re-clone as appropriate in instantiate_template. */
10611 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10612
10613 /* If we aren't complaining now, return on error before we register
10614 the specialization so that we'll complain eventually. */
10615 if ((complain & tf_error) == 0
10616 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10617 && !grok_op_properties (r, /*complain=*/false))
10618 RETURN (error_mark_node);
10619
10620 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10621 this in the special friend case mentioned above where
10622 GEN_TMPL is NULL. */
10623 if (gen_tmpl)
10624 {
10625 DECL_TEMPLATE_INFO (r)
10626 = build_template_info (gen_tmpl, argvec);
10627 SET_DECL_IMPLICIT_INSTANTIATION (r);
10628
10629 tree new_r
10630 = register_specialization (r, gen_tmpl, argvec, false, hash);
10631 if (new_r != r)
10632 /* We instantiated this while substituting into
10633 the type earlier (template/friend54.C). */
10634 RETURN (new_r);
10635
10636 /* We're not supposed to instantiate default arguments
10637 until they are called, for a template. But, for a
10638 declaration like:
10639
10640 template <class T> void f ()
10641 { extern void g(int i = T()); }
10642
10643 we should do the substitution when the template is
10644 instantiated. We handle the member function case in
10645 instantiate_class_template since the default arguments
10646 might refer to other members of the class. */
10647 if (!member
10648 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10649 && !uses_template_parms (argvec))
10650 tsubst_default_arguments (r, complain);
10651 }
10652 else
10653 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10654
10655 /* Copy the list of befriending classes. */
10656 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10657 *friends;
10658 friends = &TREE_CHAIN (*friends))
10659 {
10660 *friends = copy_node (*friends);
10661 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10662 args, complain,
10663 in_decl);
10664 }
10665
10666 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10667 {
10668 maybe_retrofit_in_chrg (r);
10669 if (DECL_CONSTRUCTOR_P (r))
10670 grok_ctor_properties (ctx, r);
10671 if (DECL_INHERITED_CTOR_BASE (r))
10672 deduce_inheriting_ctor (r);
10673 /* If this is an instantiation of a member template, clone it.
10674 If it isn't, that'll be handled by
10675 clone_constructors_and_destructors. */
10676 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10677 clone_function_decl (r, /*update_method_vec_p=*/0);
10678 }
10679 else if ((complain & tf_error) != 0
10680 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10681 && !grok_op_properties (r, /*complain=*/true))
10682 RETURN (error_mark_node);
10683
10684 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10685 SET_DECL_FRIEND_CONTEXT (r,
10686 tsubst (DECL_FRIEND_CONTEXT (t),
10687 args, complain, in_decl));
10688
10689 /* Possibly limit visibility based on template args. */
10690 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10691 if (DECL_VISIBILITY_SPECIFIED (t))
10692 {
10693 DECL_VISIBILITY_SPECIFIED (r) = 0;
10694 DECL_ATTRIBUTES (r)
10695 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10696 }
10697 determine_visibility (r);
10698 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10699 && !processing_template_decl)
10700 defaulted_late_check (r);
10701
10702 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10703 args, complain, in_decl);
10704 }
10705 break;
10706
10707 case PARM_DECL:
10708 {
10709 tree type = NULL_TREE;
10710 int i, len = 1;
10711 tree expanded_types = NULL_TREE;
10712 tree prev_r = NULL_TREE;
10713 tree first_r = NULL_TREE;
10714
10715 if (DECL_PACK_P (t))
10716 {
10717 /* If there is a local specialization that isn't a
10718 parameter pack, it means that we're doing a "simple"
10719 substitution from inside tsubst_pack_expansion. Just
10720 return the local specialization (which will be a single
10721 parm). */
10722 tree spec = retrieve_local_specialization (t);
10723 if (spec
10724 && TREE_CODE (spec) == PARM_DECL
10725 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10726 RETURN (spec);
10727
10728 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10729 the parameters in this function parameter pack. */
10730 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10731 complain, in_decl);
10732 if (TREE_CODE (expanded_types) == TREE_VEC)
10733 {
10734 len = TREE_VEC_LENGTH (expanded_types);
10735
10736 /* Zero-length parameter packs are boring. Just substitute
10737 into the chain. */
10738 if (len == 0)
10739 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10740 TREE_CHAIN (t)));
10741 }
10742 else
10743 {
10744 /* All we did was update the type. Make a note of that. */
10745 type = expanded_types;
10746 expanded_types = NULL_TREE;
10747 }
10748 }
10749
10750 /* Loop through all of the parameters we'll build. When T is
10751 a function parameter pack, LEN is the number of expanded
10752 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10753 r = NULL_TREE;
10754 for (i = 0; i < len; ++i)
10755 {
10756 prev_r = r;
10757 r = copy_node (t);
10758 if (DECL_TEMPLATE_PARM_P (t))
10759 SET_DECL_TEMPLATE_PARM_P (r);
10760
10761 if (expanded_types)
10762 /* We're on the Ith parameter of the function parameter
10763 pack. */
10764 {
10765 /* Get the Ith type. */
10766 type = TREE_VEC_ELT (expanded_types, i);
10767
10768 /* Rename the parameter to include the index. */
10769 DECL_NAME (r)
10770 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10771 }
10772 else if (!type)
10773 /* We're dealing with a normal parameter. */
10774 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10775
10776 type = type_decays_to (type);
10777 TREE_TYPE (r) = type;
10778 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10779
10780 if (DECL_INITIAL (r))
10781 {
10782 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10783 DECL_INITIAL (r) = TREE_TYPE (r);
10784 else
10785 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10786 complain, in_decl);
10787 }
10788
10789 DECL_CONTEXT (r) = NULL_TREE;
10790
10791 if (!DECL_TEMPLATE_PARM_P (r))
10792 DECL_ARG_TYPE (r) = type_passed_as (type);
10793
10794 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10795 args, complain, in_decl);
10796
10797 /* Keep track of the first new parameter we
10798 generate. That's what will be returned to the
10799 caller. */
10800 if (!first_r)
10801 first_r = r;
10802
10803 /* Build a proper chain of parameters when substituting
10804 into a function parameter pack. */
10805 if (prev_r)
10806 DECL_CHAIN (prev_r) = r;
10807 }
10808
10809 /* If cp_unevaluated_operand is set, we're just looking for a
10810 single dummy parameter, so don't keep going. */
10811 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10812 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10813 complain, DECL_CHAIN (t));
10814
10815 /* FIRST_R contains the start of the chain we've built. */
10816 r = first_r;
10817 }
10818 break;
10819
10820 case FIELD_DECL:
10821 {
10822 tree type = NULL_TREE;
10823 tree vec = NULL_TREE;
10824 tree expanded_types = NULL_TREE;
10825 int len = 1;
10826
10827 if (PACK_EXPANSION_P (TREE_TYPE (t)))
10828 {
10829 /* This field is a lambda capture pack. Return a TREE_VEC of
10830 the expanded fields to instantiate_class_template_1 and
10831 store them in the specializations hash table as a
10832 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10833 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10834 complain, in_decl);
10835 if (TREE_CODE (expanded_types) == TREE_VEC)
10836 {
10837 len = TREE_VEC_LENGTH (expanded_types);
10838 vec = make_tree_vec (len);
10839 }
10840 else
10841 {
10842 /* All we did was update the type. Make a note of that. */
10843 type = expanded_types;
10844 expanded_types = NULL_TREE;
10845 }
10846 }
10847
10848 for (int i = 0; i < len; ++i)
10849 {
10850 r = copy_decl (t);
10851 if (expanded_types)
10852 {
10853 type = TREE_VEC_ELT (expanded_types, i);
10854 DECL_NAME (r)
10855 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10856 }
10857 else if (!type)
10858 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10859
10860 if (type == error_mark_node)
10861 RETURN (error_mark_node);
10862 TREE_TYPE (r) = type;
10863 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10864
10865 if (DECL_C_BIT_FIELD (r))
10866 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10867 non-bit-fields DECL_INITIAL is a non-static data member
10868 initializer, which gets deferred instantiation. */
10869 DECL_INITIAL (r)
10870 = tsubst_expr (DECL_INITIAL (t), args,
10871 complain, in_decl,
10872 /*integral_constant_expression_p=*/true);
10873 else if (DECL_INITIAL (t))
10874 {
10875 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10876 NSDMI in perform_member_init. Still set DECL_INITIAL
10877 so that we know there is one. */
10878 DECL_INITIAL (r) = void_zero_node;
10879 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10880 retrofit_lang_decl (r);
10881 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10882 }
10883 /* We don't have to set DECL_CONTEXT here; it is set by
10884 finish_member_declaration. */
10885 DECL_CHAIN (r) = NULL_TREE;
10886
10887 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10888 args, complain, in_decl);
10889
10890 if (vec)
10891 TREE_VEC_ELT (vec, i) = r;
10892 }
10893
10894 if (vec)
10895 {
10896 r = vec;
10897 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
10898 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
10899 SET_ARGUMENT_PACK_ARGS (pack, vec);
10900 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
10901 TREE_TYPE (pack) = tpack;
10902 register_specialization (pack, t, args, false, 0);
10903 }
10904 }
10905 break;
10906
10907 case USING_DECL:
10908 /* We reach here only for member using decls. We also need to check
10909 uses_template_parms because DECL_DEPENDENT_P is not set for a
10910 using-declaration that designates a member of the current
10911 instantiation (c++/53549). */
10912 if (DECL_DEPENDENT_P (t)
10913 || uses_template_parms (USING_DECL_SCOPE (t)))
10914 {
10915 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10916 complain, in_decl);
10917 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10918 r = do_class_using_decl (inst_scope, name);
10919 if (!r)
10920 r = error_mark_node;
10921 else
10922 {
10923 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10924 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10925 }
10926 }
10927 else
10928 {
10929 r = copy_node (t);
10930 DECL_CHAIN (r) = NULL_TREE;
10931 }
10932 break;
10933
10934 case TYPE_DECL:
10935 case VAR_DECL:
10936 {
10937 tree argvec = NULL_TREE;
10938 tree gen_tmpl = NULL_TREE;
10939 tree spec;
10940 tree tmpl = NULL_TREE;
10941 tree ctx;
10942 tree type = NULL_TREE;
10943 bool local_p;
10944
10945 if (TREE_TYPE (t) == error_mark_node)
10946 RETURN (error_mark_node);
10947
10948 if (TREE_CODE (t) == TYPE_DECL
10949 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10950 {
10951 /* If this is the canonical decl, we don't have to
10952 mess with instantiations, and often we can't (for
10953 typename, template type parms and such). Note that
10954 TYPE_NAME is not correct for the above test if
10955 we've copied the type for a typedef. */
10956 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10957 if (type == error_mark_node)
10958 RETURN (error_mark_node);
10959 r = TYPE_NAME (type);
10960 break;
10961 }
10962
10963 /* Check to see if we already have the specialization we
10964 need. */
10965 spec = NULL_TREE;
10966 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10967 {
10968 /* T is a static data member or namespace-scope entity.
10969 We have to substitute into namespace-scope variables
10970 (even though such entities are never templates) because
10971 of cases like:
10972
10973 template <class T> void f() { extern T t; }
10974
10975 where the entity referenced is not known until
10976 instantiation time. */
10977 local_p = false;
10978 ctx = DECL_CONTEXT (t);
10979 if (DECL_CLASS_SCOPE_P (t))
10980 {
10981 ctx = tsubst_aggr_type (ctx, args,
10982 complain,
10983 in_decl, /*entering_scope=*/1);
10984 /* If CTX is unchanged, then T is in fact the
10985 specialization we want. That situation occurs when
10986 referencing a static data member within in its own
10987 class. We can use pointer equality, rather than
10988 same_type_p, because DECL_CONTEXT is always
10989 canonical... */
10990 if (ctx == DECL_CONTEXT (t)
10991 && (TREE_CODE (t) != TYPE_DECL
10992 /* ... unless T is a member template; in which
10993 case our caller can be willing to create a
10994 specialization of that template represented
10995 by T. */
10996 || !(DECL_TI_TEMPLATE (t)
10997 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10998 spec = t;
10999 }
11000
11001 if (!spec)
11002 {
11003 tmpl = DECL_TI_TEMPLATE (t);
11004 gen_tmpl = most_general_template (tmpl);
11005 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
11006 if (argvec == error_mark_node)
11007 RETURN (error_mark_node);
11008 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11009 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11010 }
11011 }
11012 else
11013 {
11014 /* A local variable. */
11015 local_p = true;
11016 /* Subsequent calls to pushdecl will fill this in. */
11017 ctx = NULL_TREE;
11018 spec = retrieve_local_specialization (t);
11019 }
11020 /* If we already have the specialization we need, there is
11021 nothing more to do. */
11022 if (spec)
11023 {
11024 r = spec;
11025 break;
11026 }
11027
11028 /* Create a new node for the specialization we need. */
11029 r = copy_decl (t);
11030 if (type == NULL_TREE)
11031 {
11032 if (is_typedef_decl (t))
11033 type = DECL_ORIGINAL_TYPE (t);
11034 else
11035 type = TREE_TYPE (t);
11036 if (VAR_P (t)
11037 && VAR_HAD_UNKNOWN_BOUND (t)
11038 && type != error_mark_node)
11039 type = strip_array_domain (type);
11040 type = tsubst (type, args, complain, in_decl);
11041 }
11042 if (VAR_P (r))
11043 {
11044 /* Even if the original location is out of scope, the
11045 newly substituted one is not. */
11046 DECL_DEAD_FOR_LOCAL (r) = 0;
11047 DECL_INITIALIZED_P (r) = 0;
11048 DECL_TEMPLATE_INSTANTIATED (r) = 0;
11049 if (type == error_mark_node)
11050 RETURN (error_mark_node);
11051 if (TREE_CODE (type) == FUNCTION_TYPE)
11052 {
11053 /* It may seem that this case cannot occur, since:
11054
11055 typedef void f();
11056 void g() { f x; }
11057
11058 declares a function, not a variable. However:
11059
11060 typedef void f();
11061 template <typename T> void g() { T t; }
11062 template void g<f>();
11063
11064 is an attempt to declare a variable with function
11065 type. */
11066 error ("variable %qD has function type",
11067 /* R is not yet sufficiently initialized, so we
11068 just use its name. */
11069 DECL_NAME (r));
11070 RETURN (error_mark_node);
11071 }
11072 type = complete_type (type);
11073 /* Wait until cp_finish_decl to set this again, to handle
11074 circular dependency (template/instantiate6.C). */
11075 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
11076 type = check_var_type (DECL_NAME (r), type);
11077
11078 if (DECL_HAS_VALUE_EXPR_P (t))
11079 {
11080 tree ve = DECL_VALUE_EXPR (t);
11081 ve = tsubst_expr (ve, args, complain, in_decl,
11082 /*constant_expression_p=*/false);
11083 if (REFERENCE_REF_P (ve))
11084 {
11085 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
11086 ve = TREE_OPERAND (ve, 0);
11087 }
11088 SET_DECL_VALUE_EXPR (r, ve);
11089 }
11090 }
11091 else if (DECL_SELF_REFERENCE_P (t))
11092 SET_DECL_SELF_REFERENCE_P (r);
11093 TREE_TYPE (r) = type;
11094 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11095 DECL_CONTEXT (r) = ctx;
11096 /* Clear out the mangled name and RTL for the instantiation. */
11097 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11098 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11099 SET_DECL_RTL (r, NULL);
11100 /* The initializer must not be expanded until it is required;
11101 see [temp.inst]. */
11102 DECL_INITIAL (r) = NULL_TREE;
11103 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11104 SET_DECL_RTL (r, NULL);
11105 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
11106 if (VAR_P (r))
11107 {
11108 /* Possibly limit visibility based on template args. */
11109 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11110 if (DECL_VISIBILITY_SPECIFIED (t))
11111 {
11112 DECL_VISIBILITY_SPECIFIED (r) = 0;
11113 DECL_ATTRIBUTES (r)
11114 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11115 }
11116 determine_visibility (r);
11117 }
11118
11119 if (!local_p)
11120 {
11121 /* A static data member declaration is always marked
11122 external when it is declared in-class, even if an
11123 initializer is present. We mimic the non-template
11124 processing here. */
11125 DECL_EXTERNAL (r) = 1;
11126
11127 register_specialization (r, gen_tmpl, argvec, false, hash);
11128 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
11129 SET_DECL_IMPLICIT_INSTANTIATION (r);
11130 }
11131 else if (!cp_unevaluated_operand)
11132 register_local_specialization (r, t);
11133
11134 DECL_CHAIN (r) = NULL_TREE;
11135
11136 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
11137 /*flags=*/0,
11138 args, complain, in_decl);
11139
11140 /* Preserve a typedef that names a type. */
11141 if (is_typedef_decl (r))
11142 {
11143 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11144 set_underlying_type (r);
11145 }
11146
11147 layout_decl (r, 0);
11148 }
11149 break;
11150
11151 default:
11152 gcc_unreachable ();
11153 }
11154 #undef RETURN
11155
11156 out:
11157 /* Restore the file and line information. */
11158 input_location = saved_loc;
11159
11160 return r;
11161 }
11162
11163 /* Substitute into the ARG_TYPES of a function type.
11164 If END is a TREE_CHAIN, leave it and any following types
11165 un-substituted. */
11166
11167 static tree
11168 tsubst_arg_types (tree arg_types,
11169 tree args,
11170 tree end,
11171 tsubst_flags_t complain,
11172 tree in_decl)
11173 {
11174 tree remaining_arg_types;
11175 tree type = NULL_TREE;
11176 int i = 1;
11177 tree expanded_args = NULL_TREE;
11178 tree default_arg;
11179
11180 if (!arg_types || arg_types == void_list_node || arg_types == end)
11181 return arg_types;
11182
11183 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11184 args, end, complain, in_decl);
11185 if (remaining_arg_types == error_mark_node)
11186 return error_mark_node;
11187
11188 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11189 {
11190 /* For a pack expansion, perform substitution on the
11191 entire expression. Later on, we'll handle the arguments
11192 one-by-one. */
11193 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11194 args, complain, in_decl);
11195
11196 if (TREE_CODE (expanded_args) == TREE_VEC)
11197 /* So that we'll spin through the parameters, one by one. */
11198 i = TREE_VEC_LENGTH (expanded_args);
11199 else
11200 {
11201 /* We only partially substituted into the parameter
11202 pack. Our type is TYPE_PACK_EXPANSION. */
11203 type = expanded_args;
11204 expanded_args = NULL_TREE;
11205 }
11206 }
11207
11208 while (i > 0) {
11209 --i;
11210
11211 if (expanded_args)
11212 type = TREE_VEC_ELT (expanded_args, i);
11213 else if (!type)
11214 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11215
11216 if (type == error_mark_node)
11217 return error_mark_node;
11218 if (VOID_TYPE_P (type))
11219 {
11220 if (complain & tf_error)
11221 {
11222 error ("invalid parameter type %qT", type);
11223 if (in_decl)
11224 error ("in declaration %q+D", in_decl);
11225 }
11226 return error_mark_node;
11227 }
11228 /* DR 657. */
11229 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11230 return error_mark_node;
11231
11232 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11233 top-level qualifiers as required. */
11234 type = cv_unqualified (type_decays_to (type));
11235
11236 /* We do not substitute into default arguments here. The standard
11237 mandates that they be instantiated only when needed, which is
11238 done in build_over_call. */
11239 default_arg = TREE_PURPOSE (arg_types);
11240
11241 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11242 {
11243 /* We've instantiated a template before its default arguments
11244 have been parsed. This can happen for a nested template
11245 class, and is not an error unless we require the default
11246 argument in a call of this function. */
11247 remaining_arg_types =
11248 tree_cons (default_arg, type, remaining_arg_types);
11249 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11250 }
11251 else
11252 remaining_arg_types =
11253 hash_tree_cons (default_arg, type, remaining_arg_types);
11254 }
11255
11256 return remaining_arg_types;
11257 }
11258
11259 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11260 *not* handle the exception-specification for FNTYPE, because the
11261 initial substitution of explicitly provided template parameters
11262 during argument deduction forbids substitution into the
11263 exception-specification:
11264
11265 [temp.deduct]
11266
11267 All references in the function type of the function template to the
11268 corresponding template parameters are replaced by the specified tem-
11269 plate argument values. If a substitution in a template parameter or
11270 in the function type of the function template results in an invalid
11271 type, type deduction fails. [Note: The equivalent substitution in
11272 exception specifications is done only when the function is instanti-
11273 ated, at which point a program is ill-formed if the substitution
11274 results in an invalid type.] */
11275
11276 static tree
11277 tsubst_function_type (tree t,
11278 tree args,
11279 tsubst_flags_t complain,
11280 tree in_decl)
11281 {
11282 tree return_type;
11283 tree arg_types;
11284 tree fntype;
11285
11286 /* The TYPE_CONTEXT is not used for function/method types. */
11287 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11288
11289 /* Substitute the return type. */
11290 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11291 if (return_type == error_mark_node)
11292 return error_mark_node;
11293 /* DR 486 clarifies that creation of a function type with an
11294 invalid return type is a deduction failure. */
11295 if (TREE_CODE (return_type) == ARRAY_TYPE
11296 || TREE_CODE (return_type) == FUNCTION_TYPE)
11297 {
11298 if (complain & tf_error)
11299 {
11300 if (TREE_CODE (return_type) == ARRAY_TYPE)
11301 error ("function returning an array");
11302 else
11303 error ("function returning a function");
11304 }
11305 return error_mark_node;
11306 }
11307 /* And DR 657. */
11308 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11309 return error_mark_node;
11310
11311 /* Substitute the argument types. */
11312 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11313 complain, in_decl);
11314 if (arg_types == error_mark_node)
11315 return error_mark_node;
11316
11317 /* Construct a new type node and return it. */
11318 if (TREE_CODE (t) == FUNCTION_TYPE)
11319 {
11320 fntype = build_function_type (return_type, arg_types);
11321 fntype = apply_memfn_quals (fntype,
11322 type_memfn_quals (t),
11323 type_memfn_rqual (t));
11324 }
11325 else
11326 {
11327 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11328 /* Don't pick up extra function qualifiers from the basetype. */
11329 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
11330 if (! MAYBE_CLASS_TYPE_P (r))
11331 {
11332 /* [temp.deduct]
11333
11334 Type deduction may fail for any of the following
11335 reasons:
11336
11337 -- Attempting to create "pointer to member of T" when T
11338 is not a class type. */
11339 if (complain & tf_error)
11340 error ("creating pointer to member function of non-class type %qT",
11341 r);
11342 return error_mark_node;
11343 }
11344
11345 fntype = build_method_type_directly (r, return_type,
11346 TREE_CHAIN (arg_types));
11347 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11348 }
11349 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11350
11351 return fntype;
11352 }
11353
11354 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11355 ARGS into that specification, and return the substituted
11356 specification. If there is no specification, return NULL_TREE. */
11357
11358 static tree
11359 tsubst_exception_specification (tree fntype,
11360 tree args,
11361 tsubst_flags_t complain,
11362 tree in_decl,
11363 bool defer_ok)
11364 {
11365 tree specs;
11366 tree new_specs;
11367
11368 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11369 new_specs = NULL_TREE;
11370 if (specs && TREE_PURPOSE (specs))
11371 {
11372 /* A noexcept-specifier. */
11373 tree expr = TREE_PURPOSE (specs);
11374 if (TREE_CODE (expr) == INTEGER_CST)
11375 new_specs = expr;
11376 else if (defer_ok)
11377 {
11378 /* Defer instantiation of noexcept-specifiers to avoid
11379 excessive instantiations (c++/49107). */
11380 new_specs = make_node (DEFERRED_NOEXCEPT);
11381 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11382 {
11383 /* We already partially instantiated this member template,
11384 so combine the new args with the old. */
11385 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11386 = DEFERRED_NOEXCEPT_PATTERN (expr);
11387 DEFERRED_NOEXCEPT_ARGS (new_specs)
11388 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11389 }
11390 else
11391 {
11392 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11393 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11394 }
11395 }
11396 else
11397 new_specs = tsubst_copy_and_build
11398 (expr, args, complain, in_decl, /*function_p=*/false,
11399 /*integral_constant_expression_p=*/true);
11400 new_specs = build_noexcept_spec (new_specs, complain);
11401 }
11402 else if (specs)
11403 {
11404 if (! TREE_VALUE (specs))
11405 new_specs = specs;
11406 else
11407 while (specs)
11408 {
11409 tree spec;
11410 int i, len = 1;
11411 tree expanded_specs = NULL_TREE;
11412
11413 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11414 {
11415 /* Expand the pack expansion type. */
11416 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11417 args, complain,
11418 in_decl);
11419
11420 if (expanded_specs == error_mark_node)
11421 return error_mark_node;
11422 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11423 len = TREE_VEC_LENGTH (expanded_specs);
11424 else
11425 {
11426 /* We're substituting into a member template, so
11427 we got a TYPE_PACK_EXPANSION back. Add that
11428 expansion and move on. */
11429 gcc_assert (TREE_CODE (expanded_specs)
11430 == TYPE_PACK_EXPANSION);
11431 new_specs = add_exception_specifier (new_specs,
11432 expanded_specs,
11433 complain);
11434 specs = TREE_CHAIN (specs);
11435 continue;
11436 }
11437 }
11438
11439 for (i = 0; i < len; ++i)
11440 {
11441 if (expanded_specs)
11442 spec = TREE_VEC_ELT (expanded_specs, i);
11443 else
11444 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11445 if (spec == error_mark_node)
11446 return spec;
11447 new_specs = add_exception_specifier (new_specs, spec,
11448 complain);
11449 }
11450
11451 specs = TREE_CHAIN (specs);
11452 }
11453 }
11454 return new_specs;
11455 }
11456
11457 /* Take the tree structure T and replace template parameters used
11458 therein with the argument vector ARGS. IN_DECL is an associated
11459 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11460 Issue error and warning messages under control of COMPLAIN. Note
11461 that we must be relatively non-tolerant of extensions here, in
11462 order to preserve conformance; if we allow substitutions that
11463 should not be allowed, we may allow argument deductions that should
11464 not succeed, and therefore report ambiguous overload situations
11465 where there are none. In theory, we could allow the substitution,
11466 but indicate that it should have failed, and allow our caller to
11467 make sure that the right thing happens, but we don't try to do this
11468 yet.
11469
11470 This function is used for dealing with types, decls and the like;
11471 for expressions, use tsubst_expr or tsubst_copy. */
11472
11473 tree
11474 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11475 {
11476 enum tree_code code;
11477 tree type, r = NULL_TREE;
11478
11479 if (t == NULL_TREE || t == error_mark_node
11480 || t == integer_type_node
11481 || t == void_type_node
11482 || t == char_type_node
11483 || t == unknown_type_node
11484 || TREE_CODE (t) == NAMESPACE_DECL
11485 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11486 return t;
11487
11488 if (DECL_P (t))
11489 return tsubst_decl (t, args, complain);
11490
11491 if (args == NULL_TREE)
11492 return t;
11493
11494 code = TREE_CODE (t);
11495
11496 if (code == IDENTIFIER_NODE)
11497 type = IDENTIFIER_TYPE_VALUE (t);
11498 else
11499 type = TREE_TYPE (t);
11500
11501 gcc_assert (type != unknown_type_node);
11502
11503 /* Reuse typedefs. We need to do this to handle dependent attributes,
11504 such as attribute aligned. */
11505 if (TYPE_P (t)
11506 && typedef_variant_p (t))
11507 {
11508 tree decl = TYPE_NAME (t);
11509
11510 if (alias_template_specialization_p (t))
11511 {
11512 /* DECL represents an alias template and we want to
11513 instantiate it. */
11514 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11515 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11516 r = instantiate_alias_template (tmpl, gen_args, complain);
11517 }
11518 else if (DECL_CLASS_SCOPE_P (decl)
11519 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11520 && uses_template_parms (DECL_CONTEXT (decl)))
11521 {
11522 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11523 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11524 r = retrieve_specialization (tmpl, gen_args, 0);
11525 }
11526 else if (DECL_FUNCTION_SCOPE_P (decl)
11527 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11528 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11529 r = retrieve_local_specialization (decl);
11530 else
11531 /* The typedef is from a non-template context. */
11532 return t;
11533
11534 if (r)
11535 {
11536 r = TREE_TYPE (r);
11537 r = cp_build_qualified_type_real
11538 (r, cp_type_quals (t) | cp_type_quals (r),
11539 complain | tf_ignore_bad_quals);
11540 return r;
11541 }
11542 else
11543 {
11544 /* We don't have an instantiation yet, so drop the typedef. */
11545 int quals = cp_type_quals (t);
11546 t = DECL_ORIGINAL_TYPE (decl);
11547 t = cp_build_qualified_type_real (t, quals,
11548 complain | tf_ignore_bad_quals);
11549 }
11550 }
11551
11552 if (type
11553 && code != TYPENAME_TYPE
11554 && code != TEMPLATE_TYPE_PARM
11555 && code != IDENTIFIER_NODE
11556 && code != FUNCTION_TYPE
11557 && code != METHOD_TYPE)
11558 type = tsubst (type, args, complain, in_decl);
11559 if (type == error_mark_node)
11560 return error_mark_node;
11561
11562 switch (code)
11563 {
11564 case RECORD_TYPE:
11565 case UNION_TYPE:
11566 case ENUMERAL_TYPE:
11567 return tsubst_aggr_type (t, args, complain, in_decl,
11568 /*entering_scope=*/0);
11569
11570 case ERROR_MARK:
11571 case IDENTIFIER_NODE:
11572 case VOID_TYPE:
11573 case REAL_TYPE:
11574 case COMPLEX_TYPE:
11575 case VECTOR_TYPE:
11576 case BOOLEAN_TYPE:
11577 case NULLPTR_TYPE:
11578 case LANG_TYPE:
11579 return t;
11580
11581 case INTEGER_TYPE:
11582 if (t == integer_type_node)
11583 return t;
11584
11585 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11586 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11587 return t;
11588
11589 {
11590 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11591
11592 max = tsubst_expr (omax, args, complain, in_decl,
11593 /*integral_constant_expression_p=*/false);
11594
11595 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11596 needed. */
11597 if (TREE_CODE (max) == NOP_EXPR
11598 && TREE_SIDE_EFFECTS (omax)
11599 && !TREE_TYPE (max))
11600 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11601
11602 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11603 with TREE_SIDE_EFFECTS that indicates this is not an integral
11604 constant expression. */
11605 if (processing_template_decl
11606 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11607 {
11608 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11609 TREE_SIDE_EFFECTS (max) = 1;
11610 }
11611
11612 return compute_array_index_type (NULL_TREE, max, complain);
11613 }
11614
11615 case TEMPLATE_TYPE_PARM:
11616 case TEMPLATE_TEMPLATE_PARM:
11617 case BOUND_TEMPLATE_TEMPLATE_PARM:
11618 case TEMPLATE_PARM_INDEX:
11619 {
11620 int idx;
11621 int level;
11622 int levels;
11623 tree arg = NULL_TREE;
11624
11625 r = NULL_TREE;
11626
11627 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11628 template_parm_level_and_index (t, &level, &idx);
11629
11630 levels = TMPL_ARGS_DEPTH (args);
11631 if (level <= levels)
11632 {
11633 arg = TMPL_ARG (args, level, idx);
11634
11635 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11636 {
11637 /* See through ARGUMENT_PACK_SELECT arguments. */
11638 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11639 /* If the selected argument is an expansion E, that most
11640 likely means we were called from
11641 gen_elem_of_pack_expansion_instantiation during the
11642 substituting of pack an argument pack (which Ith
11643 element is a pack expansion, where I is
11644 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11645 In this case, the Ith element resulting from this
11646 substituting is going to be a pack expansion, which
11647 pattern is the pattern of E. Let's return the
11648 pattern of E, and
11649 gen_elem_of_pack_expansion_instantiation will
11650 build the resulting pack expansion from it. */
11651 if (PACK_EXPANSION_P (arg))
11652 arg = PACK_EXPANSION_PATTERN (arg);
11653 }
11654 }
11655
11656 if (arg == error_mark_node)
11657 return error_mark_node;
11658 else if (arg != NULL_TREE)
11659 {
11660 if (ARGUMENT_PACK_P (arg))
11661 /* If ARG is an argument pack, we don't actually want to
11662 perform a substitution here, because substitutions
11663 for argument packs are only done
11664 element-by-element. We can get to this point when
11665 substituting the type of a non-type template
11666 parameter pack, when that type actually contains
11667 template parameter packs from an outer template, e.g.,
11668
11669 template<typename... Types> struct A {
11670 template<Types... Values> struct B { };
11671 }; */
11672 return t;
11673
11674 if (code == TEMPLATE_TYPE_PARM)
11675 {
11676 int quals;
11677 gcc_assert (TYPE_P (arg));
11678
11679 quals = cp_type_quals (arg) | cp_type_quals (t);
11680
11681 return cp_build_qualified_type_real
11682 (arg, quals, complain | tf_ignore_bad_quals);
11683 }
11684 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11685 {
11686 /* We are processing a type constructed from a
11687 template template parameter. */
11688 tree argvec = tsubst (TYPE_TI_ARGS (t),
11689 args, complain, in_decl);
11690 if (argvec == error_mark_node)
11691 return error_mark_node;
11692
11693 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11694 || TREE_CODE (arg) == TEMPLATE_DECL
11695 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11696
11697 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11698 /* Consider this code:
11699
11700 template <template <class> class Template>
11701 struct Internal {
11702 template <class Arg> using Bind = Template<Arg>;
11703 };
11704
11705 template <template <class> class Template, class Arg>
11706 using Instantiate = Template<Arg>; //#0
11707
11708 template <template <class> class Template,
11709 class Argument>
11710 using Bind =
11711 Instantiate<Internal<Template>::template Bind,
11712 Argument>; //#1
11713
11714 When #1 is parsed, the
11715 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11716 parameter `Template' in #0 matches the
11717 UNBOUND_CLASS_TEMPLATE representing the argument
11718 `Internal<Template>::template Bind'; We then want
11719 to assemble the type `Bind<Argument>' that can't
11720 be fully created right now, because
11721 `Internal<Template>' not being complete, the Bind
11722 template cannot be looked up in that context. So
11723 we need to "store" `Bind<Argument>' for later
11724 when the context of Bind becomes complete. Let's
11725 store that in a TYPENAME_TYPE. */
11726 return make_typename_type (TYPE_CONTEXT (arg),
11727 build_nt (TEMPLATE_ID_EXPR,
11728 TYPE_IDENTIFIER (arg),
11729 argvec),
11730 typename_type,
11731 complain);
11732
11733 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11734 are resolving nested-types in the signature of a
11735 member function templates. Otherwise ARG is a
11736 TEMPLATE_DECL and is the real template to be
11737 instantiated. */
11738 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11739 arg = TYPE_NAME (arg);
11740
11741 r = lookup_template_class (arg,
11742 argvec, in_decl,
11743 DECL_CONTEXT (arg),
11744 /*entering_scope=*/0,
11745 complain);
11746 return cp_build_qualified_type_real
11747 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11748 }
11749 else
11750 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11751 return convert_from_reference (unshare_expr (arg));
11752 }
11753
11754 if (level == 1)
11755 /* This can happen during the attempted tsubst'ing in
11756 unify. This means that we don't yet have any information
11757 about the template parameter in question. */
11758 return t;
11759
11760 /* Early in template argument deduction substitution, we don't
11761 want to reduce the level of 'auto', or it will be confused
11762 with a normal template parm in subsequent deduction. */
11763 if (is_auto (t) && (complain & tf_partial))
11764 return t;
11765
11766 /* If we get here, we must have been looking at a parm for a
11767 more deeply nested template. Make a new version of this
11768 template parameter, but with a lower level. */
11769 switch (code)
11770 {
11771 case TEMPLATE_TYPE_PARM:
11772 case TEMPLATE_TEMPLATE_PARM:
11773 case BOUND_TEMPLATE_TEMPLATE_PARM:
11774 if (cp_type_quals (t))
11775 {
11776 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11777 r = cp_build_qualified_type_real
11778 (r, cp_type_quals (t),
11779 complain | (code == TEMPLATE_TYPE_PARM
11780 ? tf_ignore_bad_quals : 0));
11781 }
11782 else
11783 {
11784 r = copy_type (t);
11785 TEMPLATE_TYPE_PARM_INDEX (r)
11786 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11787 r, levels, args, complain);
11788 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11789 TYPE_MAIN_VARIANT (r) = r;
11790 TYPE_POINTER_TO (r) = NULL_TREE;
11791 TYPE_REFERENCE_TO (r) = NULL_TREE;
11792
11793 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11794 /* We have reduced the level of the template
11795 template parameter, but not the levels of its
11796 template parameters, so canonical_type_parameter
11797 will not be able to find the canonical template
11798 template parameter for this level. Thus, we
11799 require structural equality checking to compare
11800 TEMPLATE_TEMPLATE_PARMs. */
11801 SET_TYPE_STRUCTURAL_EQUALITY (r);
11802 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11803 SET_TYPE_STRUCTURAL_EQUALITY (r);
11804 else
11805 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11806
11807 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11808 {
11809 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11810 complain, in_decl);
11811 if (argvec == error_mark_node)
11812 return error_mark_node;
11813
11814 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11815 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11816 }
11817 }
11818 break;
11819
11820 case TEMPLATE_PARM_INDEX:
11821 r = reduce_template_parm_level (t, type, levels, args, complain);
11822 break;
11823
11824 default:
11825 gcc_unreachable ();
11826 }
11827
11828 return r;
11829 }
11830
11831 case TREE_LIST:
11832 {
11833 tree purpose, value, chain;
11834
11835 if (t == void_list_node)
11836 return t;
11837
11838 purpose = TREE_PURPOSE (t);
11839 if (purpose)
11840 {
11841 purpose = tsubst (purpose, args, complain, in_decl);
11842 if (purpose == error_mark_node)
11843 return error_mark_node;
11844 }
11845 value = TREE_VALUE (t);
11846 if (value)
11847 {
11848 value = tsubst (value, args, complain, in_decl);
11849 if (value == error_mark_node)
11850 return error_mark_node;
11851 }
11852 chain = TREE_CHAIN (t);
11853 if (chain && chain != void_type_node)
11854 {
11855 chain = tsubst (chain, args, complain, in_decl);
11856 if (chain == error_mark_node)
11857 return error_mark_node;
11858 }
11859 if (purpose == TREE_PURPOSE (t)
11860 && value == TREE_VALUE (t)
11861 && chain == TREE_CHAIN (t))
11862 return t;
11863 return hash_tree_cons (purpose, value, chain);
11864 }
11865
11866 case TREE_BINFO:
11867 /* We should never be tsubsting a binfo. */
11868 gcc_unreachable ();
11869
11870 case TREE_VEC:
11871 /* A vector of template arguments. */
11872 gcc_assert (!type);
11873 return tsubst_template_args (t, args, complain, in_decl);
11874
11875 case POINTER_TYPE:
11876 case REFERENCE_TYPE:
11877 {
11878 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11879 return t;
11880
11881 /* [temp.deduct]
11882
11883 Type deduction may fail for any of the following
11884 reasons:
11885
11886 -- Attempting to create a pointer to reference type.
11887 -- Attempting to create a reference to a reference type or
11888 a reference to void.
11889
11890 Core issue 106 says that creating a reference to a reference
11891 during instantiation is no longer a cause for failure. We
11892 only enforce this check in strict C++98 mode. */
11893 if ((TREE_CODE (type) == REFERENCE_TYPE
11894 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11895 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
11896 {
11897 static location_t last_loc;
11898
11899 /* We keep track of the last time we issued this error
11900 message to avoid spewing a ton of messages during a
11901 single bad template instantiation. */
11902 if (complain & tf_error
11903 && last_loc != input_location)
11904 {
11905 if (VOID_TYPE_P (type))
11906 error ("forming reference to void");
11907 else if (code == POINTER_TYPE)
11908 error ("forming pointer to reference type %qT", type);
11909 else
11910 error ("forming reference to reference type %qT", type);
11911 last_loc = input_location;
11912 }
11913
11914 return error_mark_node;
11915 }
11916 else if (TREE_CODE (type) == FUNCTION_TYPE
11917 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11918 || type_memfn_rqual (type) != REF_QUAL_NONE))
11919 {
11920 if (complain & tf_error)
11921 {
11922 if (code == POINTER_TYPE)
11923 error ("forming pointer to qualified function type %qT",
11924 type);
11925 else
11926 error ("forming reference to qualified function type %qT",
11927 type);
11928 }
11929 return error_mark_node;
11930 }
11931 else if (code == POINTER_TYPE)
11932 {
11933 r = build_pointer_type (type);
11934 if (TREE_CODE (type) == METHOD_TYPE)
11935 r = build_ptrmemfunc_type (r);
11936 }
11937 else if (TREE_CODE (type) == REFERENCE_TYPE)
11938 /* In C++0x, during template argument substitution, when there is an
11939 attempt to create a reference to a reference type, reference
11940 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11941
11942 "If a template-argument for a template-parameter T names a type
11943 that is a reference to a type A, an attempt to create the type
11944 'lvalue reference to cv T' creates the type 'lvalue reference to
11945 A,' while an attempt to create the type type rvalue reference to
11946 cv T' creates the type T"
11947 */
11948 r = cp_build_reference_type
11949 (TREE_TYPE (type),
11950 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11951 else
11952 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11953 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11954
11955 if (cxx_dialect >= cxx1y
11956 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
11957 && array_of_runtime_bound_p (type))
11958 {
11959 if (complain & tf_warning_or_error)
11960 pedwarn
11961 (input_location, OPT_Wvla,
11962 code == REFERENCE_TYPE
11963 ? G_("cannot declare reference to array of runtime bound")
11964 : G_("cannot declare pointer to array of runtime bound"));
11965 else
11966 r = error_mark_node;
11967 }
11968
11969 if (r != error_mark_node)
11970 /* Will this ever be needed for TYPE_..._TO values? */
11971 layout_type (r);
11972
11973 return r;
11974 }
11975 case OFFSET_TYPE:
11976 {
11977 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11978 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11979 {
11980 /* [temp.deduct]
11981
11982 Type deduction may fail for any of the following
11983 reasons:
11984
11985 -- Attempting to create "pointer to member of T" when T
11986 is not a class type. */
11987 if (complain & tf_error)
11988 error ("creating pointer to member of non-class type %qT", r);
11989 return error_mark_node;
11990 }
11991 if (TREE_CODE (type) == REFERENCE_TYPE)
11992 {
11993 if (complain & tf_error)
11994 error ("creating pointer to member reference type %qT", type);
11995 return error_mark_node;
11996 }
11997 if (VOID_TYPE_P (type))
11998 {
11999 if (complain & tf_error)
12000 error ("creating pointer to member of type void");
12001 return error_mark_node;
12002 }
12003 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12004 if (TREE_CODE (type) == FUNCTION_TYPE)
12005 {
12006 /* The type of the implicit object parameter gets its
12007 cv-qualifiers from the FUNCTION_TYPE. */
12008 tree memptr;
12009 tree method_type
12010 = build_memfn_type (type, r, type_memfn_quals (type),
12011 type_memfn_rqual (type));
12012 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
12013 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
12014 complain);
12015 }
12016 else
12017 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
12018 cp_type_quals (t),
12019 complain);
12020 }
12021 case FUNCTION_TYPE:
12022 case METHOD_TYPE:
12023 {
12024 tree fntype;
12025 tree specs;
12026 fntype = tsubst_function_type (t, args, complain, in_decl);
12027 if (fntype == error_mark_node)
12028 return error_mark_node;
12029
12030 /* Substitute the exception specification. */
12031 specs = tsubst_exception_specification (t, args, complain,
12032 in_decl, /*defer_ok*/true);
12033 if (specs == error_mark_node)
12034 return error_mark_node;
12035 if (specs)
12036 fntype = build_exception_variant (fntype, specs);
12037 return fntype;
12038 }
12039 case ARRAY_TYPE:
12040 {
12041 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
12042 if (domain == error_mark_node)
12043 return error_mark_node;
12044
12045 /* As an optimization, we avoid regenerating the array type if
12046 it will obviously be the same as T. */
12047 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
12048 return t;
12049
12050 /* These checks should match the ones in grokdeclarator.
12051
12052 [temp.deduct]
12053
12054 The deduction may fail for any of the following reasons:
12055
12056 -- Attempting to create an array with an element type that
12057 is void, a function type, or a reference type, or [DR337]
12058 an abstract class type. */
12059 if (VOID_TYPE_P (type)
12060 || TREE_CODE (type) == FUNCTION_TYPE
12061 || TREE_CODE (type) == REFERENCE_TYPE)
12062 {
12063 if (complain & tf_error)
12064 error ("creating array of %qT", type);
12065 return error_mark_node;
12066 }
12067
12068 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
12069 return error_mark_node;
12070
12071 r = build_cplus_array_type (type, domain);
12072
12073 if (TYPE_USER_ALIGN (t))
12074 {
12075 TYPE_ALIGN (r) = TYPE_ALIGN (t);
12076 TYPE_USER_ALIGN (r) = 1;
12077 }
12078
12079 return r;
12080 }
12081
12082 case TYPENAME_TYPE:
12083 {
12084 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12085 in_decl, /*entering_scope=*/1);
12086 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
12087 complain, in_decl);
12088
12089 if (ctx == error_mark_node || f == error_mark_node)
12090 return error_mark_node;
12091
12092 if (!MAYBE_CLASS_TYPE_P (ctx))
12093 {
12094 if (complain & tf_error)
12095 error ("%qT is not a class, struct, or union type", ctx);
12096 return error_mark_node;
12097 }
12098 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
12099 {
12100 /* Normally, make_typename_type does not require that the CTX
12101 have complete type in order to allow things like:
12102
12103 template <class T> struct S { typename S<T>::X Y; };
12104
12105 But, such constructs have already been resolved by this
12106 point, so here CTX really should have complete type, unless
12107 it's a partial instantiation. */
12108 ctx = complete_type (ctx);
12109 if (!COMPLETE_TYPE_P (ctx))
12110 {
12111 if (complain & tf_error)
12112 cxx_incomplete_type_error (NULL_TREE, ctx);
12113 return error_mark_node;
12114 }
12115 }
12116
12117 f = make_typename_type (ctx, f, typename_type,
12118 complain | tf_keep_type_decl);
12119 if (f == error_mark_node)
12120 return f;
12121 if (TREE_CODE (f) == TYPE_DECL)
12122 {
12123 complain |= tf_ignore_bad_quals;
12124 f = TREE_TYPE (f);
12125 }
12126
12127 if (TREE_CODE (f) != TYPENAME_TYPE)
12128 {
12129 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
12130 {
12131 if (complain & tf_error)
12132 error ("%qT resolves to %qT, which is not an enumeration type",
12133 t, f);
12134 else
12135 return error_mark_node;
12136 }
12137 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
12138 {
12139 if (complain & tf_error)
12140 error ("%qT resolves to %qT, which is is not a class type",
12141 t, f);
12142 else
12143 return error_mark_node;
12144 }
12145 }
12146
12147 return cp_build_qualified_type_real
12148 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12149 }
12150
12151 case UNBOUND_CLASS_TEMPLATE:
12152 {
12153 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12154 in_decl, /*entering_scope=*/1);
12155 tree name = TYPE_IDENTIFIER (t);
12156 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12157
12158 if (ctx == error_mark_node || name == error_mark_node)
12159 return error_mark_node;
12160
12161 if (parm_list)
12162 parm_list = tsubst_template_parms (parm_list, args, complain);
12163 return make_unbound_class_template (ctx, name, parm_list, complain);
12164 }
12165
12166 case TYPEOF_TYPE:
12167 {
12168 tree type;
12169
12170 ++cp_unevaluated_operand;
12171 ++c_inhibit_evaluation_warnings;
12172
12173 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12174 complain, in_decl,
12175 /*integral_constant_expression_p=*/false);
12176
12177 --cp_unevaluated_operand;
12178 --c_inhibit_evaluation_warnings;
12179
12180 type = finish_typeof (type);
12181 return cp_build_qualified_type_real (type,
12182 cp_type_quals (t)
12183 | cp_type_quals (type),
12184 complain);
12185 }
12186
12187 case DECLTYPE_TYPE:
12188 {
12189 tree type;
12190
12191 ++cp_unevaluated_operand;
12192 ++c_inhibit_evaluation_warnings;
12193
12194 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12195 complain|tf_decltype, in_decl,
12196 /*function_p*/false,
12197 /*integral_constant_expression*/false);
12198
12199 --cp_unevaluated_operand;
12200 --c_inhibit_evaluation_warnings;
12201
12202 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12203 type = lambda_capture_field_type (type,
12204 DECLTYPE_FOR_INIT_CAPTURE (t));
12205 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12206 type = lambda_proxy_type (type);
12207 else
12208 {
12209 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12210 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12211 && EXPR_P (type))
12212 /* In a template ~id could be either a complement expression
12213 or an unqualified-id naming a destructor; if instantiating
12214 it produces an expression, it's not an id-expression or
12215 member access. */
12216 id = false;
12217 type = finish_decltype_type (type, id, complain);
12218 }
12219 return cp_build_qualified_type_real (type,
12220 cp_type_quals (t)
12221 | cp_type_quals (type),
12222 complain);
12223 }
12224
12225 case UNDERLYING_TYPE:
12226 {
12227 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12228 complain, in_decl);
12229 return finish_underlying_type (type);
12230 }
12231
12232 case TYPE_ARGUMENT_PACK:
12233 case NONTYPE_ARGUMENT_PACK:
12234 {
12235 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12236 tree packed_out =
12237 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12238 args,
12239 complain,
12240 in_decl);
12241 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12242
12243 /* For template nontype argument packs, also substitute into
12244 the type. */
12245 if (code == NONTYPE_ARGUMENT_PACK)
12246 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12247
12248 return r;
12249 }
12250 break;
12251
12252 case INTEGER_CST:
12253 case REAL_CST:
12254 case STRING_CST:
12255 case PLUS_EXPR:
12256 case MINUS_EXPR:
12257 case NEGATE_EXPR:
12258 case NOP_EXPR:
12259 case INDIRECT_REF:
12260 case ADDR_EXPR:
12261 case CALL_EXPR:
12262 case ARRAY_REF:
12263 case SCOPE_REF:
12264 /* We should use one of the expression tsubsts for these codes. */
12265 gcc_unreachable ();
12266
12267 default:
12268 sorry ("use of %qs in template", get_tree_code_name (code));
12269 return error_mark_node;
12270 }
12271 }
12272
12273 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12274 type of the expression on the left-hand side of the "." or "->"
12275 operator. */
12276
12277 static tree
12278 tsubst_baselink (tree baselink, tree object_type,
12279 tree args, tsubst_flags_t complain, tree in_decl)
12280 {
12281 tree name;
12282 tree qualifying_scope;
12283 tree fns;
12284 tree optype;
12285 tree template_args = 0;
12286 bool template_id_p = false;
12287 bool qualified = BASELINK_QUALIFIED_P (baselink);
12288
12289 /* A baselink indicates a function from a base class. Both the
12290 BASELINK_ACCESS_BINFO and the base class referenced may
12291 indicate bases of the template class, rather than the
12292 instantiated class. In addition, lookups that were not
12293 ambiguous before may be ambiguous now. Therefore, we perform
12294 the lookup again. */
12295 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12296 qualifying_scope = tsubst (qualifying_scope, args,
12297 complain, in_decl);
12298 fns = BASELINK_FUNCTIONS (baselink);
12299 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12300 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12301 {
12302 template_id_p = true;
12303 template_args = TREE_OPERAND (fns, 1);
12304 fns = TREE_OPERAND (fns, 0);
12305 if (template_args)
12306 template_args = tsubst_template_args (template_args, args,
12307 complain, in_decl);
12308 }
12309 name = DECL_NAME (get_first_fn (fns));
12310 if (IDENTIFIER_TYPENAME_P (name))
12311 name = mangle_conv_op_name_for_type (optype);
12312 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12313 if (!baselink)
12314 return error_mark_node;
12315
12316 /* If lookup found a single function, mark it as used at this
12317 point. (If it lookup found multiple functions the one selected
12318 later by overload resolution will be marked as used at that
12319 point.) */
12320 if (BASELINK_P (baselink))
12321 fns = BASELINK_FUNCTIONS (baselink);
12322 if (!template_id_p && !really_overloaded_fn (fns))
12323 mark_used (OVL_CURRENT (fns));
12324
12325 /* Add back the template arguments, if present. */
12326 if (BASELINK_P (baselink) && template_id_p)
12327 BASELINK_FUNCTIONS (baselink)
12328 = build_nt (TEMPLATE_ID_EXPR,
12329 BASELINK_FUNCTIONS (baselink),
12330 template_args);
12331 /* Update the conversion operator type. */
12332 BASELINK_OPTYPE (baselink) = optype;
12333
12334 if (!object_type)
12335 object_type = current_class_type;
12336
12337 if (qualified)
12338 baselink = adjust_result_of_qualified_name_lookup (baselink,
12339 qualifying_scope,
12340 object_type);
12341 return baselink;
12342 }
12343
12344 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12345 true if the qualified-id will be a postfix-expression in-and-of
12346 itself; false if more of the postfix-expression follows the
12347 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12348 of "&". */
12349
12350 static tree
12351 tsubst_qualified_id (tree qualified_id, tree args,
12352 tsubst_flags_t complain, tree in_decl,
12353 bool done, bool address_p)
12354 {
12355 tree expr;
12356 tree scope;
12357 tree name;
12358 bool is_template;
12359 tree template_args;
12360 location_t loc = UNKNOWN_LOCATION;
12361
12362 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12363
12364 /* Figure out what name to look up. */
12365 name = TREE_OPERAND (qualified_id, 1);
12366 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12367 {
12368 is_template = true;
12369 loc = EXPR_LOCATION (name);
12370 template_args = TREE_OPERAND (name, 1);
12371 if (template_args)
12372 template_args = tsubst_template_args (template_args, args,
12373 complain, in_decl);
12374 name = TREE_OPERAND (name, 0);
12375 }
12376 else
12377 {
12378 is_template = false;
12379 template_args = NULL_TREE;
12380 }
12381
12382 /* Substitute into the qualifying scope. When there are no ARGS, we
12383 are just trying to simplify a non-dependent expression. In that
12384 case the qualifying scope may be dependent, and, in any case,
12385 substituting will not help. */
12386 scope = TREE_OPERAND (qualified_id, 0);
12387 if (args)
12388 {
12389 scope = tsubst (scope, args, complain, in_decl);
12390 expr = tsubst_copy (name, args, complain, in_decl);
12391 }
12392 else
12393 expr = name;
12394
12395 if (dependent_scope_p (scope))
12396 {
12397 if (is_template)
12398 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12399 return build_qualified_name (NULL_TREE, scope, expr,
12400 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12401 }
12402
12403 if (!BASELINK_P (name) && !DECL_P (expr))
12404 {
12405 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12406 {
12407 /* A BIT_NOT_EXPR is used to represent a destructor. */
12408 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12409 {
12410 error ("qualifying type %qT does not match destructor name ~%qT",
12411 scope, TREE_OPERAND (expr, 0));
12412 expr = error_mark_node;
12413 }
12414 else
12415 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12416 /*is_type_p=*/0, false);
12417 }
12418 else
12419 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12420 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12421 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12422 {
12423 if (complain & tf_error)
12424 {
12425 error ("dependent-name %qE is parsed as a non-type, but "
12426 "instantiation yields a type", qualified_id);
12427 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12428 }
12429 return error_mark_node;
12430 }
12431 }
12432
12433 if (DECL_P (expr))
12434 {
12435 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12436 scope);
12437 /* Remember that there was a reference to this entity. */
12438 mark_used (expr);
12439 }
12440
12441 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12442 {
12443 if (complain & tf_error)
12444 qualified_name_lookup_error (scope,
12445 TREE_OPERAND (qualified_id, 1),
12446 expr, input_location);
12447 return error_mark_node;
12448 }
12449
12450 if (is_template)
12451 expr = lookup_template_function (expr, template_args);
12452
12453 if (expr == error_mark_node && complain & tf_error)
12454 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12455 expr, input_location);
12456 else if (TYPE_P (scope))
12457 {
12458 expr = (adjust_result_of_qualified_name_lookup
12459 (expr, scope, current_nonlambda_class_type ()));
12460 expr = (finish_qualified_id_expr
12461 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12462 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12463 /*template_arg_p=*/false, complain));
12464 }
12465
12466 /* Expressions do not generally have reference type. */
12467 if (TREE_CODE (expr) != SCOPE_REF
12468 /* However, if we're about to form a pointer-to-member, we just
12469 want the referenced member referenced. */
12470 && TREE_CODE (expr) != OFFSET_REF)
12471 expr = convert_from_reference (expr);
12472
12473 return expr;
12474 }
12475
12476 /* Like tsubst, but deals with expressions. This function just replaces
12477 template parms; to finish processing the resultant expression, use
12478 tsubst_copy_and_build or tsubst_expr. */
12479
12480 static tree
12481 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12482 {
12483 enum tree_code code;
12484 tree r;
12485
12486 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12487 return t;
12488
12489 code = TREE_CODE (t);
12490
12491 switch (code)
12492 {
12493 case PARM_DECL:
12494 r = retrieve_local_specialization (t);
12495
12496 if (r == NULL_TREE)
12497 {
12498 /* We get here for a use of 'this' in an NSDMI. */
12499 if (DECL_NAME (t) == this_identifier
12500 && at_function_scope_p ()
12501 && DECL_CONSTRUCTOR_P (current_function_decl))
12502 return current_class_ptr;
12503
12504 /* This can happen for a parameter name used later in a function
12505 declaration (such as in a late-specified return type). Just
12506 make a dummy decl, since it's only used for its type. */
12507 gcc_assert (cp_unevaluated_operand != 0);
12508 r = tsubst_decl (t, args, complain);
12509 /* Give it the template pattern as its context; its true context
12510 hasn't been instantiated yet and this is good enough for
12511 mangling. */
12512 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12513 }
12514
12515 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12516 r = ARGUMENT_PACK_SELECT_ARG (r);
12517 mark_used (r);
12518 return r;
12519
12520 case CONST_DECL:
12521 {
12522 tree enum_type;
12523 tree v;
12524
12525 if (DECL_TEMPLATE_PARM_P (t))
12526 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12527 /* There is no need to substitute into namespace-scope
12528 enumerators. */
12529 if (DECL_NAMESPACE_SCOPE_P (t))
12530 return t;
12531 /* If ARGS is NULL, then T is known to be non-dependent. */
12532 if (args == NULL_TREE)
12533 return integral_constant_value (t);
12534
12535 /* Unfortunately, we cannot just call lookup_name here.
12536 Consider:
12537
12538 template <int I> int f() {
12539 enum E { a = I };
12540 struct S { void g() { E e = a; } };
12541 };
12542
12543 When we instantiate f<7>::S::g(), say, lookup_name is not
12544 clever enough to find f<7>::a. */
12545 enum_type
12546 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12547 /*entering_scope=*/0);
12548
12549 for (v = TYPE_VALUES (enum_type);
12550 v != NULL_TREE;
12551 v = TREE_CHAIN (v))
12552 if (TREE_PURPOSE (v) == DECL_NAME (t))
12553 return TREE_VALUE (v);
12554
12555 /* We didn't find the name. That should never happen; if
12556 name-lookup found it during preliminary parsing, we
12557 should find it again here during instantiation. */
12558 gcc_unreachable ();
12559 }
12560 return t;
12561
12562 case FIELD_DECL:
12563 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12564 {
12565 /* Check for a local specialization set up by
12566 tsubst_pack_expansion. */
12567 tree r = retrieve_local_specialization (t);
12568 if (r)
12569 {
12570 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12571 r = ARGUMENT_PACK_SELECT_ARG (r);
12572 return r;
12573 }
12574
12575 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12576 tsubst_decl put in the hash table. */
12577 return retrieve_specialization (t, args, 0);
12578 }
12579
12580 if (DECL_CONTEXT (t))
12581 {
12582 tree ctx;
12583
12584 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12585 /*entering_scope=*/1);
12586 if (ctx != DECL_CONTEXT (t))
12587 {
12588 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12589 if (!r)
12590 {
12591 if (complain & tf_error)
12592 error ("using invalid field %qD", t);
12593 return error_mark_node;
12594 }
12595 return r;
12596 }
12597 }
12598
12599 return t;
12600
12601 case VAR_DECL:
12602 case FUNCTION_DECL:
12603 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12604 r = tsubst (t, args, complain, in_decl);
12605 else if (local_variable_p (t))
12606 {
12607 r = retrieve_local_specialization (t);
12608 if (r == NULL_TREE)
12609 {
12610 if (DECL_ANON_UNION_VAR_P (t))
12611 {
12612 /* Just use name lookup to find a member alias for an
12613 anonymous union, but then add it to the hash table. */
12614 r = lookup_name (DECL_NAME (t));
12615 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12616 register_local_specialization (r, t);
12617 }
12618 else
12619 {
12620 /* This can happen for a variable used in a late-specified
12621 return type of a local lambda. Just make a dummy decl
12622 since it's only used for its type. */
12623 if (cp_unevaluated_operand)
12624 return tsubst_decl (t, args, complain);
12625 gcc_assert (errorcount || sorrycount);
12626 return error_mark_node;
12627 }
12628 }
12629 }
12630 else
12631 r = t;
12632 mark_used (r);
12633 return r;
12634
12635 case NAMESPACE_DECL:
12636 return t;
12637
12638 case OVERLOAD:
12639 /* An OVERLOAD will always be a non-dependent overload set; an
12640 overload set from function scope will just be represented with an
12641 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12642 gcc_assert (!uses_template_parms (t));
12643 return t;
12644
12645 case BASELINK:
12646 return tsubst_baselink (t, current_nonlambda_class_type (),
12647 args, complain, in_decl);
12648
12649 case TEMPLATE_DECL:
12650 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12651 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12652 args, complain, in_decl);
12653 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12654 return tsubst (t, args, complain, in_decl);
12655 else if (DECL_CLASS_SCOPE_P (t)
12656 && uses_template_parms (DECL_CONTEXT (t)))
12657 {
12658 /* Template template argument like the following example need
12659 special treatment:
12660
12661 template <template <class> class TT> struct C {};
12662 template <class T> struct D {
12663 template <class U> struct E {};
12664 C<E> c; // #1
12665 };
12666 D<int> d; // #2
12667
12668 We are processing the template argument `E' in #1 for
12669 the template instantiation #2. Originally, `E' is a
12670 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12671 have to substitute this with one having context `D<int>'. */
12672
12673 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12674 return lookup_field (context, DECL_NAME(t), 0, false);
12675 }
12676 else
12677 /* Ordinary template template argument. */
12678 return t;
12679
12680 case CAST_EXPR:
12681 case REINTERPRET_CAST_EXPR:
12682 case CONST_CAST_EXPR:
12683 case STATIC_CAST_EXPR:
12684 case DYNAMIC_CAST_EXPR:
12685 case IMPLICIT_CONV_EXPR:
12686 case CONVERT_EXPR:
12687 case NOP_EXPR:
12688 return build1
12689 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12690 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12691
12692 case SIZEOF_EXPR:
12693 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12694 {
12695
12696 tree expanded, op = TREE_OPERAND (t, 0);
12697 int len = 0;
12698
12699 if (SIZEOF_EXPR_TYPE_P (t))
12700 op = TREE_TYPE (op);
12701
12702 ++cp_unevaluated_operand;
12703 ++c_inhibit_evaluation_warnings;
12704 /* We only want to compute the number of arguments. */
12705 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12706 --cp_unevaluated_operand;
12707 --c_inhibit_evaluation_warnings;
12708
12709 if (TREE_CODE (expanded) == TREE_VEC)
12710 len = TREE_VEC_LENGTH (expanded);
12711
12712 if (expanded == error_mark_node)
12713 return error_mark_node;
12714 else if (PACK_EXPANSION_P (expanded)
12715 || (TREE_CODE (expanded) == TREE_VEC
12716 && len > 0
12717 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12718 {
12719 if (TREE_CODE (expanded) == TREE_VEC)
12720 expanded = TREE_VEC_ELT (expanded, len - 1);
12721
12722 if (TYPE_P (expanded))
12723 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12724 complain & tf_error);
12725 else
12726 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12727 complain & tf_error);
12728 }
12729 else
12730 return build_int_cst (size_type_node, len);
12731 }
12732 if (SIZEOF_EXPR_TYPE_P (t))
12733 {
12734 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12735 args, complain, in_decl);
12736 r = build1 (NOP_EXPR, r, error_mark_node);
12737 r = build1 (SIZEOF_EXPR,
12738 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12739 SIZEOF_EXPR_TYPE_P (r) = 1;
12740 return r;
12741 }
12742 /* Fall through */
12743
12744 case INDIRECT_REF:
12745 case NEGATE_EXPR:
12746 case TRUTH_NOT_EXPR:
12747 case BIT_NOT_EXPR:
12748 case ADDR_EXPR:
12749 case UNARY_PLUS_EXPR: /* Unary + */
12750 case ALIGNOF_EXPR:
12751 case AT_ENCODE_EXPR:
12752 case ARROW_EXPR:
12753 case THROW_EXPR:
12754 case TYPEID_EXPR:
12755 case REALPART_EXPR:
12756 case IMAGPART_EXPR:
12757 case PAREN_EXPR:
12758 return build1
12759 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12760 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12761
12762 case COMPONENT_REF:
12763 {
12764 tree object;
12765 tree name;
12766
12767 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12768 name = TREE_OPERAND (t, 1);
12769 if (TREE_CODE (name) == BIT_NOT_EXPR)
12770 {
12771 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12772 complain, in_decl);
12773 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12774 }
12775 else if (TREE_CODE (name) == SCOPE_REF
12776 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12777 {
12778 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12779 complain, in_decl);
12780 name = TREE_OPERAND (name, 1);
12781 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12782 complain, in_decl);
12783 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12784 name = build_qualified_name (/*type=*/NULL_TREE,
12785 base, name,
12786 /*template_p=*/false);
12787 }
12788 else if (BASELINK_P (name))
12789 name = tsubst_baselink (name,
12790 non_reference (TREE_TYPE (object)),
12791 args, complain,
12792 in_decl);
12793 else
12794 name = tsubst_copy (name, args, complain, in_decl);
12795 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12796 }
12797
12798 case PLUS_EXPR:
12799 case MINUS_EXPR:
12800 case MULT_EXPR:
12801 case TRUNC_DIV_EXPR:
12802 case CEIL_DIV_EXPR:
12803 case FLOOR_DIV_EXPR:
12804 case ROUND_DIV_EXPR:
12805 case EXACT_DIV_EXPR:
12806 case BIT_AND_EXPR:
12807 case BIT_IOR_EXPR:
12808 case BIT_XOR_EXPR:
12809 case TRUNC_MOD_EXPR:
12810 case FLOOR_MOD_EXPR:
12811 case TRUTH_ANDIF_EXPR:
12812 case TRUTH_ORIF_EXPR:
12813 case TRUTH_AND_EXPR:
12814 case TRUTH_OR_EXPR:
12815 case RSHIFT_EXPR:
12816 case LSHIFT_EXPR:
12817 case RROTATE_EXPR:
12818 case LROTATE_EXPR:
12819 case EQ_EXPR:
12820 case NE_EXPR:
12821 case MAX_EXPR:
12822 case MIN_EXPR:
12823 case LE_EXPR:
12824 case GE_EXPR:
12825 case LT_EXPR:
12826 case GT_EXPR:
12827 case COMPOUND_EXPR:
12828 case DOTSTAR_EXPR:
12829 case MEMBER_REF:
12830 case PREDECREMENT_EXPR:
12831 case PREINCREMENT_EXPR:
12832 case POSTDECREMENT_EXPR:
12833 case POSTINCREMENT_EXPR:
12834 return build_nt
12835 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12836 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12837
12838 case SCOPE_REF:
12839 return build_qualified_name (/*type=*/NULL_TREE,
12840 tsubst_copy (TREE_OPERAND (t, 0),
12841 args, complain, in_decl),
12842 tsubst_copy (TREE_OPERAND (t, 1),
12843 args, complain, in_decl),
12844 QUALIFIED_NAME_IS_TEMPLATE (t));
12845
12846 case ARRAY_REF:
12847 return build_nt
12848 (ARRAY_REF,
12849 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12850 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12851 NULL_TREE, NULL_TREE);
12852
12853 case CALL_EXPR:
12854 {
12855 int n = VL_EXP_OPERAND_LENGTH (t);
12856 tree result = build_vl_exp (CALL_EXPR, n);
12857 int i;
12858 for (i = 0; i < n; i++)
12859 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12860 complain, in_decl);
12861 return result;
12862 }
12863
12864 case COND_EXPR:
12865 case MODOP_EXPR:
12866 case PSEUDO_DTOR_EXPR:
12867 case VEC_PERM_EXPR:
12868 {
12869 r = build_nt
12870 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12871 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12872 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12873 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12874 return r;
12875 }
12876
12877 case NEW_EXPR:
12878 {
12879 r = build_nt
12880 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12881 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12882 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12883 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12884 return r;
12885 }
12886
12887 case DELETE_EXPR:
12888 {
12889 r = build_nt
12890 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12891 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12892 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12893 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12894 return r;
12895 }
12896
12897 case TEMPLATE_ID_EXPR:
12898 {
12899 /* Substituted template arguments */
12900 tree fn = TREE_OPERAND (t, 0);
12901 tree targs = TREE_OPERAND (t, 1);
12902
12903 fn = tsubst_copy (fn, args, complain, in_decl);
12904 if (targs)
12905 targs = tsubst_template_args (targs, args, complain, in_decl);
12906
12907 return lookup_template_function (fn, targs);
12908 }
12909
12910 case TREE_LIST:
12911 {
12912 tree purpose, value, chain;
12913
12914 if (t == void_list_node)
12915 return t;
12916
12917 purpose = TREE_PURPOSE (t);
12918 if (purpose)
12919 purpose = tsubst_copy (purpose, args, complain, in_decl);
12920 value = TREE_VALUE (t);
12921 if (value)
12922 value = tsubst_copy (value, args, complain, in_decl);
12923 chain = TREE_CHAIN (t);
12924 if (chain && chain != void_type_node)
12925 chain = tsubst_copy (chain, args, complain, in_decl);
12926 if (purpose == TREE_PURPOSE (t)
12927 && value == TREE_VALUE (t)
12928 && chain == TREE_CHAIN (t))
12929 return t;
12930 return tree_cons (purpose, value, chain);
12931 }
12932
12933 case RECORD_TYPE:
12934 case UNION_TYPE:
12935 case ENUMERAL_TYPE:
12936 case INTEGER_TYPE:
12937 case TEMPLATE_TYPE_PARM:
12938 case TEMPLATE_TEMPLATE_PARM:
12939 case BOUND_TEMPLATE_TEMPLATE_PARM:
12940 case TEMPLATE_PARM_INDEX:
12941 case POINTER_TYPE:
12942 case REFERENCE_TYPE:
12943 case OFFSET_TYPE:
12944 case FUNCTION_TYPE:
12945 case METHOD_TYPE:
12946 case ARRAY_TYPE:
12947 case TYPENAME_TYPE:
12948 case UNBOUND_CLASS_TEMPLATE:
12949 case TYPEOF_TYPE:
12950 case DECLTYPE_TYPE:
12951 case TYPE_DECL:
12952 return tsubst (t, args, complain, in_decl);
12953
12954 case USING_DECL:
12955 t = DECL_NAME (t);
12956 /* Fall through. */
12957 case IDENTIFIER_NODE:
12958 if (IDENTIFIER_TYPENAME_P (t))
12959 {
12960 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12961 return mangle_conv_op_name_for_type (new_type);
12962 }
12963 else
12964 return t;
12965
12966 case CONSTRUCTOR:
12967 /* This is handled by tsubst_copy_and_build. */
12968 gcc_unreachable ();
12969
12970 case VA_ARG_EXPR:
12971 return build_x_va_arg (EXPR_LOCATION (t),
12972 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12973 in_decl),
12974 tsubst (TREE_TYPE (t), args, complain, in_decl));
12975
12976 case CLEANUP_POINT_EXPR:
12977 /* We shouldn't have built any of these during initial template
12978 generation. Instead, they should be built during instantiation
12979 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12980 gcc_unreachable ();
12981
12982 case OFFSET_REF:
12983 r = build2
12984 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12985 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12986 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12987 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12988 mark_used (TREE_OPERAND (r, 1));
12989 return r;
12990
12991 case EXPR_PACK_EXPANSION:
12992 error ("invalid use of pack expansion expression");
12993 return error_mark_node;
12994
12995 case NONTYPE_ARGUMENT_PACK:
12996 error ("use %<...%> to expand argument pack");
12997 return error_mark_node;
12998
12999 case INTEGER_CST:
13000 case REAL_CST:
13001 case STRING_CST:
13002 case COMPLEX_CST:
13003 {
13004 /* Instantiate any typedefs in the type. */
13005 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13006 r = fold_convert (type, t);
13007 gcc_assert (TREE_CODE (r) == code);
13008 return r;
13009 }
13010
13011 case PTRMEM_CST:
13012 /* These can sometimes show up in a partial instantiation, but never
13013 involve template parms. */
13014 gcc_assert (!uses_template_parms (t));
13015 return t;
13016
13017 default:
13018 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13019 gcc_checking_assert (false);
13020 return t;
13021 }
13022 }
13023
13024 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13025
13026 static tree
13027 tsubst_omp_clauses (tree clauses, bool declare_simd,
13028 tree args, tsubst_flags_t complain, tree in_decl)
13029 {
13030 tree new_clauses = NULL, nc, oc;
13031
13032 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
13033 {
13034 nc = copy_node (oc);
13035 OMP_CLAUSE_CHAIN (nc) = new_clauses;
13036 new_clauses = nc;
13037
13038 switch (OMP_CLAUSE_CODE (nc))
13039 {
13040 case OMP_CLAUSE_LASTPRIVATE:
13041 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
13042 {
13043 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
13044 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
13045 in_decl, /*integral_constant_expression_p=*/false);
13046 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
13047 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
13048 }
13049 /* FALLTHRU */
13050 case OMP_CLAUSE_PRIVATE:
13051 case OMP_CLAUSE_SHARED:
13052 case OMP_CLAUSE_FIRSTPRIVATE:
13053 case OMP_CLAUSE_COPYIN:
13054 case OMP_CLAUSE_COPYPRIVATE:
13055 case OMP_CLAUSE_IF:
13056 case OMP_CLAUSE_NUM_THREADS:
13057 case OMP_CLAUSE_SCHEDULE:
13058 case OMP_CLAUSE_COLLAPSE:
13059 case OMP_CLAUSE_FINAL:
13060 case OMP_CLAUSE_DEPEND:
13061 case OMP_CLAUSE_FROM:
13062 case OMP_CLAUSE_TO:
13063 case OMP_CLAUSE_UNIFORM:
13064 case OMP_CLAUSE_MAP:
13065 case OMP_CLAUSE_DEVICE:
13066 case OMP_CLAUSE_DIST_SCHEDULE:
13067 case OMP_CLAUSE_NUM_TEAMS:
13068 case OMP_CLAUSE_THREAD_LIMIT:
13069 case OMP_CLAUSE_SAFELEN:
13070 case OMP_CLAUSE_SIMDLEN:
13071 OMP_CLAUSE_OPERAND (nc, 0)
13072 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13073 in_decl, /*integral_constant_expression_p=*/false);
13074 break;
13075 case OMP_CLAUSE_REDUCTION:
13076 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
13077 {
13078 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
13079 if (TREE_CODE (placeholder) == SCOPE_REF)
13080 {
13081 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
13082 complain, in_decl);
13083 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
13084 = build_qualified_name (NULL_TREE, scope,
13085 TREE_OPERAND (placeholder, 1),
13086 false);
13087 }
13088 else
13089 gcc_assert (identifier_p (placeholder));
13090 }
13091 OMP_CLAUSE_OPERAND (nc, 0)
13092 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13093 in_decl, /*integral_constant_expression_p=*/false);
13094 break;
13095 case OMP_CLAUSE_LINEAR:
13096 case OMP_CLAUSE_ALIGNED:
13097 OMP_CLAUSE_OPERAND (nc, 0)
13098 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13099 in_decl, /*integral_constant_expression_p=*/false);
13100 OMP_CLAUSE_OPERAND (nc, 1)
13101 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
13102 in_decl, /*integral_constant_expression_p=*/false);
13103 break;
13104
13105 case OMP_CLAUSE_NOWAIT:
13106 case OMP_CLAUSE_ORDERED:
13107 case OMP_CLAUSE_DEFAULT:
13108 case OMP_CLAUSE_UNTIED:
13109 case OMP_CLAUSE_MERGEABLE:
13110 case OMP_CLAUSE_INBRANCH:
13111 case OMP_CLAUSE_NOTINBRANCH:
13112 case OMP_CLAUSE_PROC_BIND:
13113 case OMP_CLAUSE_FOR:
13114 case OMP_CLAUSE_PARALLEL:
13115 case OMP_CLAUSE_SECTIONS:
13116 case OMP_CLAUSE_TASKGROUP:
13117 break;
13118 default:
13119 gcc_unreachable ();
13120 }
13121 }
13122
13123 new_clauses = nreverse (new_clauses);
13124 if (!declare_simd)
13125 new_clauses = finish_omp_clauses (new_clauses);
13126 return new_clauses;
13127 }
13128
13129 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13130
13131 static tree
13132 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
13133 tree in_decl)
13134 {
13135 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13136
13137 tree purpose, value, chain;
13138
13139 if (t == NULL)
13140 return t;
13141
13142 if (TREE_CODE (t) != TREE_LIST)
13143 return tsubst_copy_and_build (t, args, complain, in_decl,
13144 /*function_p=*/false,
13145 /*integral_constant_expression_p=*/false);
13146
13147 if (t == void_list_node)
13148 return t;
13149
13150 purpose = TREE_PURPOSE (t);
13151 if (purpose)
13152 purpose = RECUR (purpose);
13153 value = TREE_VALUE (t);
13154 if (value)
13155 {
13156 if (TREE_CODE (value) != LABEL_DECL)
13157 value = RECUR (value);
13158 else
13159 {
13160 value = lookup_label (DECL_NAME (value));
13161 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13162 TREE_USED (value) = 1;
13163 }
13164 }
13165 chain = TREE_CHAIN (t);
13166 if (chain && chain != void_type_node)
13167 chain = RECUR (chain);
13168 return tree_cons (purpose, value, chain);
13169 #undef RECUR
13170 }
13171
13172 /* Substitute one OMP_FOR iterator. */
13173
13174 static void
13175 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13176 tree condv, tree incrv, tree *clauses,
13177 tree args, tsubst_flags_t complain, tree in_decl,
13178 bool integral_constant_expression_p)
13179 {
13180 #define RECUR(NODE) \
13181 tsubst_expr ((NODE), args, complain, in_decl, \
13182 integral_constant_expression_p)
13183 tree decl, init, cond, incr;
13184
13185 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13186 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13187 decl = TREE_OPERAND (init, 0);
13188 init = TREE_OPERAND (init, 1);
13189 tree decl_expr = NULL_TREE;
13190 if (init && TREE_CODE (init) == DECL_EXPR)
13191 {
13192 /* We need to jump through some hoops to handle declarations in the
13193 for-init-statement, since we might need to handle auto deduction,
13194 but we need to keep control of initialization. */
13195 decl_expr = init;
13196 init = DECL_INITIAL (DECL_EXPR_DECL (init));
13197 decl = tsubst_decl (decl, args, complain);
13198 }
13199 else
13200 decl = RECUR (decl);
13201 init = RECUR (init);
13202
13203 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13204 if (auto_node && init)
13205 TREE_TYPE (decl)
13206 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
13207
13208 gcc_assert (!type_dependent_expression_p (decl));
13209
13210 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13211 {
13212 if (decl_expr)
13213 {
13214 /* Declare the variable, but don't let that initialize it. */
13215 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
13216 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
13217 RECUR (decl_expr);
13218 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
13219 }
13220
13221 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13222 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13223 if (TREE_CODE (incr) == MODIFY_EXPR)
13224 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13225 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13226 RECUR (TREE_OPERAND (incr, 1)),
13227 complain);
13228 else
13229 incr = RECUR (incr);
13230 TREE_VEC_ELT (declv, i) = decl;
13231 TREE_VEC_ELT (initv, i) = init;
13232 TREE_VEC_ELT (condv, i) = cond;
13233 TREE_VEC_ELT (incrv, i) = incr;
13234 return;
13235 }
13236
13237 if (decl_expr)
13238 {
13239 /* Declare and initialize the variable. */
13240 RECUR (decl_expr);
13241 init = NULL_TREE;
13242 }
13243 else if (init)
13244 {
13245 tree c;
13246 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13247 {
13248 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13249 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13250 && OMP_CLAUSE_DECL (c) == decl)
13251 break;
13252 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13253 && OMP_CLAUSE_DECL (c) == decl)
13254 error ("iteration variable %qD should not be firstprivate", decl);
13255 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13256 && OMP_CLAUSE_DECL (c) == decl)
13257 error ("iteration variable %qD should not be reduction", decl);
13258 }
13259 if (c == NULL)
13260 {
13261 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13262 OMP_CLAUSE_DECL (c) = decl;
13263 c = finish_omp_clauses (c);
13264 if (c)
13265 {
13266 OMP_CLAUSE_CHAIN (c) = *clauses;
13267 *clauses = c;
13268 }
13269 }
13270 }
13271 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13272 if (COMPARISON_CLASS_P (cond))
13273 cond = build2 (TREE_CODE (cond), boolean_type_node,
13274 RECUR (TREE_OPERAND (cond, 0)),
13275 RECUR (TREE_OPERAND (cond, 1)));
13276 else
13277 cond = RECUR (cond);
13278 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13279 switch (TREE_CODE (incr))
13280 {
13281 case PREINCREMENT_EXPR:
13282 case PREDECREMENT_EXPR:
13283 case POSTINCREMENT_EXPR:
13284 case POSTDECREMENT_EXPR:
13285 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13286 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13287 break;
13288 case MODIFY_EXPR:
13289 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13290 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13291 {
13292 tree rhs = TREE_OPERAND (incr, 1);
13293 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13294 RECUR (TREE_OPERAND (incr, 0)),
13295 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13296 RECUR (TREE_OPERAND (rhs, 0)),
13297 RECUR (TREE_OPERAND (rhs, 1))));
13298 }
13299 else
13300 incr = RECUR (incr);
13301 break;
13302 case MODOP_EXPR:
13303 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13304 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13305 {
13306 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13307 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13308 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13309 TREE_TYPE (decl), lhs,
13310 RECUR (TREE_OPERAND (incr, 2))));
13311 }
13312 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13313 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13314 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13315 {
13316 tree rhs = TREE_OPERAND (incr, 2);
13317 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13318 RECUR (TREE_OPERAND (incr, 0)),
13319 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13320 RECUR (TREE_OPERAND (rhs, 0)),
13321 RECUR (TREE_OPERAND (rhs, 1))));
13322 }
13323 else
13324 incr = RECUR (incr);
13325 break;
13326 default:
13327 incr = RECUR (incr);
13328 break;
13329 }
13330
13331 TREE_VEC_ELT (declv, i) = decl;
13332 TREE_VEC_ELT (initv, i) = init;
13333 TREE_VEC_ELT (condv, i) = cond;
13334 TREE_VEC_ELT (incrv, i) = incr;
13335 #undef RECUR
13336 }
13337
13338 /* Like tsubst_copy for expressions, etc. but also does semantic
13339 processing. */
13340
13341 static tree
13342 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13343 bool integral_constant_expression_p)
13344 {
13345 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13346 #define RECUR(NODE) \
13347 tsubst_expr ((NODE), args, complain, in_decl, \
13348 integral_constant_expression_p)
13349
13350 tree stmt, tmp;
13351 tree r;
13352 location_t loc;
13353
13354 if (t == NULL_TREE || t == error_mark_node)
13355 return t;
13356
13357 loc = input_location;
13358 if (EXPR_HAS_LOCATION (t))
13359 input_location = EXPR_LOCATION (t);
13360 if (STATEMENT_CODE_P (TREE_CODE (t)))
13361 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13362
13363 switch (TREE_CODE (t))
13364 {
13365 case STATEMENT_LIST:
13366 {
13367 tree_stmt_iterator i;
13368 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13369 RECUR (tsi_stmt (i));
13370 break;
13371 }
13372
13373 case CTOR_INITIALIZER:
13374 finish_mem_initializers (tsubst_initializer_list
13375 (TREE_OPERAND (t, 0), args));
13376 break;
13377
13378 case RETURN_EXPR:
13379 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13380 break;
13381
13382 case EXPR_STMT:
13383 tmp = RECUR (EXPR_STMT_EXPR (t));
13384 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13385 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13386 else
13387 finish_expr_stmt (tmp);
13388 break;
13389
13390 case USING_STMT:
13391 do_using_directive (USING_STMT_NAMESPACE (t));
13392 break;
13393
13394 case DECL_EXPR:
13395 {
13396 tree decl, pattern_decl;
13397 tree init;
13398
13399 pattern_decl = decl = DECL_EXPR_DECL (t);
13400 if (TREE_CODE (decl) == LABEL_DECL)
13401 finish_label_decl (DECL_NAME (decl));
13402 else if (TREE_CODE (decl) == USING_DECL)
13403 {
13404 tree scope = USING_DECL_SCOPE (decl);
13405 tree name = DECL_NAME (decl);
13406 tree decl;
13407
13408 scope = tsubst (scope, args, complain, in_decl);
13409 decl = lookup_qualified_name (scope, name,
13410 /*is_type_p=*/false,
13411 /*complain=*/false);
13412 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13413 qualified_name_lookup_error (scope, name, decl, input_location);
13414 else
13415 do_local_using_decl (decl, scope, name);
13416 }
13417 else if (DECL_PACK_P (decl))
13418 {
13419 /* Don't build up decls for a variadic capture proxy, we'll
13420 instantiate the elements directly as needed. */
13421 break;
13422 }
13423 else
13424 {
13425 init = DECL_INITIAL (decl);
13426 decl = tsubst (decl, args, complain, in_decl);
13427 if (decl != error_mark_node)
13428 {
13429 /* By marking the declaration as instantiated, we avoid
13430 trying to instantiate it. Since instantiate_decl can't
13431 handle local variables, and since we've already done
13432 all that needs to be done, that's the right thing to
13433 do. */
13434 if (VAR_P (decl))
13435 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13436 if (VAR_P (decl)
13437 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13438 /* Anonymous aggregates are a special case. */
13439 finish_anon_union (decl);
13440 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13441 {
13442 DECL_CONTEXT (decl) = current_function_decl;
13443 if (DECL_NAME (decl) == this_identifier)
13444 {
13445 tree lam = DECL_CONTEXT (current_function_decl);
13446 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13447 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13448 }
13449 insert_capture_proxy (decl);
13450 }
13451 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13452 /* We already did a pushtag. */;
13453 else if (TREE_CODE (decl) == FUNCTION_DECL
13454 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13455 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13456 {
13457 DECL_CONTEXT (decl) = NULL_TREE;
13458 pushdecl (decl);
13459 DECL_CONTEXT (decl) = current_function_decl;
13460 cp_check_omp_declare_reduction (decl);
13461 }
13462 else
13463 {
13464 int const_init = false;
13465 maybe_push_decl (decl);
13466 if (VAR_P (decl)
13467 && DECL_PRETTY_FUNCTION_P (decl))
13468 {
13469 /* For __PRETTY_FUNCTION__ we have to adjust the
13470 initializer. */
13471 const char *const name
13472 = cxx_printable_name (current_function_decl, 2);
13473 init = cp_fname_init (name, &TREE_TYPE (decl));
13474 }
13475 else
13476 {
13477 tree t = RECUR (init);
13478
13479 if (init && !t)
13480 {
13481 /* If we had an initializer but it
13482 instantiated to nothing,
13483 value-initialize the object. This will
13484 only occur when the initializer was a
13485 pack expansion where the parameter packs
13486 used in that expansion were of length
13487 zero. */
13488 init = build_value_init (TREE_TYPE (decl),
13489 complain);
13490 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13491 init = get_target_expr_sfinae (init, complain);
13492 }
13493 else
13494 init = t;
13495 }
13496
13497 if (VAR_P (decl))
13498 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13499 (pattern_decl));
13500 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13501 }
13502 }
13503 }
13504
13505 break;
13506 }
13507
13508 case FOR_STMT:
13509 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13510 RECUR (FOR_INIT_STMT (t));
13511 finish_for_init_stmt (stmt);
13512 tmp = RECUR (FOR_COND (t));
13513 finish_for_cond (tmp, stmt, false);
13514 tmp = RECUR (FOR_EXPR (t));
13515 finish_for_expr (tmp, stmt);
13516 RECUR (FOR_BODY (t));
13517 finish_for_stmt (stmt);
13518 break;
13519
13520 case RANGE_FOR_STMT:
13521 {
13522 tree decl, expr;
13523 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13524 decl = RANGE_FOR_DECL (t);
13525 decl = tsubst (decl, args, complain, in_decl);
13526 maybe_push_decl (decl);
13527 expr = RECUR (RANGE_FOR_EXPR (t));
13528 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13529 RECUR (RANGE_FOR_BODY (t));
13530 finish_for_stmt (stmt);
13531 }
13532 break;
13533
13534 case WHILE_STMT:
13535 stmt = begin_while_stmt ();
13536 tmp = RECUR (WHILE_COND (t));
13537 finish_while_stmt_cond (tmp, stmt, false);
13538 RECUR (WHILE_BODY (t));
13539 finish_while_stmt (stmt);
13540 break;
13541
13542 case DO_STMT:
13543 stmt = begin_do_stmt ();
13544 RECUR (DO_BODY (t));
13545 finish_do_body (stmt);
13546 tmp = RECUR (DO_COND (t));
13547 finish_do_stmt (tmp, stmt, false);
13548 break;
13549
13550 case IF_STMT:
13551 stmt = begin_if_stmt ();
13552 tmp = RECUR (IF_COND (t));
13553 finish_if_stmt_cond (tmp, stmt);
13554 RECUR (THEN_CLAUSE (t));
13555 finish_then_clause (stmt);
13556
13557 if (ELSE_CLAUSE (t))
13558 {
13559 begin_else_clause (stmt);
13560 RECUR (ELSE_CLAUSE (t));
13561 finish_else_clause (stmt);
13562 }
13563
13564 finish_if_stmt (stmt);
13565 break;
13566
13567 case BIND_EXPR:
13568 if (BIND_EXPR_BODY_BLOCK (t))
13569 stmt = begin_function_body ();
13570 else
13571 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13572 ? BCS_TRY_BLOCK : 0);
13573
13574 RECUR (BIND_EXPR_BODY (t));
13575
13576 if (BIND_EXPR_BODY_BLOCK (t))
13577 finish_function_body (stmt);
13578 else
13579 finish_compound_stmt (stmt);
13580 break;
13581
13582 case BREAK_STMT:
13583 finish_break_stmt ();
13584 break;
13585
13586 case CONTINUE_STMT:
13587 finish_continue_stmt ();
13588 break;
13589
13590 case SWITCH_STMT:
13591 stmt = begin_switch_stmt ();
13592 tmp = RECUR (SWITCH_STMT_COND (t));
13593 finish_switch_cond (tmp, stmt);
13594 RECUR (SWITCH_STMT_BODY (t));
13595 finish_switch_stmt (stmt);
13596 break;
13597
13598 case CASE_LABEL_EXPR:
13599 finish_case_label (EXPR_LOCATION (t),
13600 RECUR (CASE_LOW (t)),
13601 RECUR (CASE_HIGH (t)));
13602 break;
13603
13604 case LABEL_EXPR:
13605 {
13606 tree decl = LABEL_EXPR_LABEL (t);
13607 tree label;
13608
13609 label = finish_label_stmt (DECL_NAME (decl));
13610 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13611 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13612 }
13613 break;
13614
13615 case GOTO_EXPR:
13616 tmp = GOTO_DESTINATION (t);
13617 if (TREE_CODE (tmp) != LABEL_DECL)
13618 /* Computed goto's must be tsubst'd into. On the other hand,
13619 non-computed gotos must not be; the identifier in question
13620 will have no binding. */
13621 tmp = RECUR (tmp);
13622 else
13623 tmp = DECL_NAME (tmp);
13624 finish_goto_stmt (tmp);
13625 break;
13626
13627 case ASM_EXPR:
13628 tmp = finish_asm_stmt
13629 (ASM_VOLATILE_P (t),
13630 RECUR (ASM_STRING (t)),
13631 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13632 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13633 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13634 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13635 {
13636 tree asm_expr = tmp;
13637 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13638 asm_expr = TREE_OPERAND (asm_expr, 0);
13639 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13640 }
13641 break;
13642
13643 case TRY_BLOCK:
13644 if (CLEANUP_P (t))
13645 {
13646 stmt = begin_try_block ();
13647 RECUR (TRY_STMTS (t));
13648 finish_cleanup_try_block (stmt);
13649 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13650 }
13651 else
13652 {
13653 tree compound_stmt = NULL_TREE;
13654
13655 if (FN_TRY_BLOCK_P (t))
13656 stmt = begin_function_try_block (&compound_stmt);
13657 else
13658 stmt = begin_try_block ();
13659
13660 RECUR (TRY_STMTS (t));
13661
13662 if (FN_TRY_BLOCK_P (t))
13663 finish_function_try_block (stmt);
13664 else
13665 finish_try_block (stmt);
13666
13667 RECUR (TRY_HANDLERS (t));
13668 if (FN_TRY_BLOCK_P (t))
13669 finish_function_handler_sequence (stmt, compound_stmt);
13670 else
13671 finish_handler_sequence (stmt);
13672 }
13673 break;
13674
13675 case HANDLER:
13676 {
13677 tree decl = HANDLER_PARMS (t);
13678
13679 if (decl)
13680 {
13681 decl = tsubst (decl, args, complain, in_decl);
13682 /* Prevent instantiate_decl from trying to instantiate
13683 this variable. We've already done all that needs to be
13684 done. */
13685 if (decl != error_mark_node)
13686 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13687 }
13688 stmt = begin_handler ();
13689 finish_handler_parms (decl, stmt);
13690 RECUR (HANDLER_BODY (t));
13691 finish_handler (stmt);
13692 }
13693 break;
13694
13695 case TAG_DEFN:
13696 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13697 if (CLASS_TYPE_P (tmp))
13698 {
13699 /* Local classes are not independent templates; they are
13700 instantiated along with their containing function. And this
13701 way we don't have to deal with pushing out of one local class
13702 to instantiate a member of another local class. */
13703 tree fn;
13704 /* Closures are handled by the LAMBDA_EXPR. */
13705 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13706 complete_type (tmp);
13707 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13708 if (!DECL_ARTIFICIAL (fn))
13709 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13710 }
13711 break;
13712
13713 case STATIC_ASSERT:
13714 {
13715 tree condition;
13716
13717 ++c_inhibit_evaluation_warnings;
13718 condition =
13719 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13720 args,
13721 complain, in_decl,
13722 /*integral_constant_expression_p=*/true);
13723 --c_inhibit_evaluation_warnings;
13724
13725 finish_static_assert (condition,
13726 STATIC_ASSERT_MESSAGE (t),
13727 STATIC_ASSERT_SOURCE_LOCATION (t),
13728 /*member_p=*/false);
13729 }
13730 break;
13731
13732 case OMP_PARALLEL:
13733 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13734 args, complain, in_decl);
13735 stmt = begin_omp_parallel ();
13736 RECUR (OMP_PARALLEL_BODY (t));
13737 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13738 = OMP_PARALLEL_COMBINED (t);
13739 break;
13740
13741 case OMP_TASK:
13742 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13743 args, complain, in_decl);
13744 stmt = begin_omp_task ();
13745 RECUR (OMP_TASK_BODY (t));
13746 finish_omp_task (tmp, stmt);
13747 break;
13748
13749 case OMP_FOR:
13750 case OMP_SIMD:
13751 case CILK_SIMD:
13752 case OMP_DISTRIBUTE:
13753 {
13754 tree clauses, body, pre_body;
13755 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13756 tree incrv = NULL_TREE;
13757 int i;
13758
13759 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13760 args, complain, in_decl);
13761 if (OMP_FOR_INIT (t) != NULL_TREE)
13762 {
13763 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13764 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13765 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13766 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13767 }
13768
13769 stmt = begin_omp_structured_block ();
13770
13771 pre_body = push_stmt_list ();
13772 RECUR (OMP_FOR_PRE_BODY (t));
13773 pre_body = pop_stmt_list (pre_body);
13774
13775 if (OMP_FOR_INIT (t) != NULL_TREE)
13776 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13777 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13778 &clauses, args, complain, in_decl,
13779 integral_constant_expression_p);
13780
13781 body = push_stmt_list ();
13782 RECUR (OMP_FOR_BODY (t));
13783 body = pop_stmt_list (body);
13784
13785 if (OMP_FOR_INIT (t) != NULL_TREE)
13786 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13787 condv, incrv, body, pre_body, clauses);
13788 else
13789 {
13790 t = make_node (TREE_CODE (t));
13791 TREE_TYPE (t) = void_type_node;
13792 OMP_FOR_BODY (t) = body;
13793 OMP_FOR_PRE_BODY (t) = pre_body;
13794 OMP_FOR_CLAUSES (t) = clauses;
13795 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13796 add_stmt (t);
13797 }
13798
13799 add_stmt (finish_omp_structured_block (stmt));
13800 }
13801 break;
13802
13803 case OMP_SECTIONS:
13804 case OMP_SINGLE:
13805 case OMP_TEAMS:
13806 case OMP_TARGET_DATA:
13807 case OMP_TARGET:
13808 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13809 args, complain, in_decl);
13810 stmt = push_stmt_list ();
13811 RECUR (OMP_BODY (t));
13812 stmt = pop_stmt_list (stmt);
13813
13814 t = copy_node (t);
13815 OMP_BODY (t) = stmt;
13816 OMP_CLAUSES (t) = tmp;
13817 add_stmt (t);
13818 break;
13819
13820 case OMP_TARGET_UPDATE:
13821 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
13822 args, complain, in_decl);
13823 t = copy_node (t);
13824 OMP_CLAUSES (t) = tmp;
13825 add_stmt (t);
13826 break;
13827
13828 case OMP_SECTION:
13829 case OMP_CRITICAL:
13830 case OMP_MASTER:
13831 case OMP_TASKGROUP:
13832 case OMP_ORDERED:
13833 stmt = push_stmt_list ();
13834 RECUR (OMP_BODY (t));
13835 stmt = pop_stmt_list (stmt);
13836
13837 t = copy_node (t);
13838 OMP_BODY (t) = stmt;
13839 add_stmt (t);
13840 break;
13841
13842 case OMP_ATOMIC:
13843 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13844 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13845 {
13846 tree op1 = TREE_OPERAND (t, 1);
13847 tree rhs1 = NULL_TREE;
13848 tree lhs, rhs;
13849 if (TREE_CODE (op1) == COMPOUND_EXPR)
13850 {
13851 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13852 op1 = TREE_OPERAND (op1, 1);
13853 }
13854 lhs = RECUR (TREE_OPERAND (op1, 0));
13855 rhs = RECUR (TREE_OPERAND (op1, 1));
13856 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13857 NULL_TREE, NULL_TREE, rhs1,
13858 OMP_ATOMIC_SEQ_CST (t));
13859 }
13860 else
13861 {
13862 tree op1 = TREE_OPERAND (t, 1);
13863 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13864 tree rhs1 = NULL_TREE;
13865 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13866 enum tree_code opcode = NOP_EXPR;
13867 if (code == OMP_ATOMIC_READ)
13868 {
13869 v = RECUR (TREE_OPERAND (op1, 0));
13870 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13871 }
13872 else if (code == OMP_ATOMIC_CAPTURE_OLD
13873 || code == OMP_ATOMIC_CAPTURE_NEW)
13874 {
13875 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13876 v = RECUR (TREE_OPERAND (op1, 0));
13877 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13878 if (TREE_CODE (op11) == COMPOUND_EXPR)
13879 {
13880 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13881 op11 = TREE_OPERAND (op11, 1);
13882 }
13883 lhs = RECUR (TREE_OPERAND (op11, 0));
13884 rhs = RECUR (TREE_OPERAND (op11, 1));
13885 opcode = TREE_CODE (op11);
13886 if (opcode == MODIFY_EXPR)
13887 opcode = NOP_EXPR;
13888 }
13889 else
13890 {
13891 code = OMP_ATOMIC;
13892 lhs = RECUR (TREE_OPERAND (op1, 0));
13893 rhs = RECUR (TREE_OPERAND (op1, 1));
13894 }
13895 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
13896 OMP_ATOMIC_SEQ_CST (t));
13897 }
13898 break;
13899
13900 case TRANSACTION_EXPR:
13901 {
13902 int flags = 0;
13903 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13904 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13905
13906 if (TRANSACTION_EXPR_IS_STMT (t))
13907 {
13908 tree body = TRANSACTION_EXPR_BODY (t);
13909 tree noex = NULL_TREE;
13910 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13911 {
13912 noex = MUST_NOT_THROW_COND (body);
13913 if (noex == NULL_TREE)
13914 noex = boolean_true_node;
13915 body = TREE_OPERAND (body, 0);
13916 }
13917 stmt = begin_transaction_stmt (input_location, NULL, flags);
13918 RECUR (body);
13919 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13920 }
13921 else
13922 {
13923 stmt = build_transaction_expr (EXPR_LOCATION (t),
13924 RECUR (TRANSACTION_EXPR_BODY (t)),
13925 flags, NULL_TREE);
13926 RETURN (stmt);
13927 }
13928 }
13929 break;
13930
13931 case MUST_NOT_THROW_EXPR:
13932 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13933 RECUR (MUST_NOT_THROW_COND (t))));
13934
13935 case EXPR_PACK_EXPANSION:
13936 error ("invalid use of pack expansion expression");
13937 RETURN (error_mark_node);
13938
13939 case NONTYPE_ARGUMENT_PACK:
13940 error ("use %<...%> to expand argument pack");
13941 RETURN (error_mark_node);
13942
13943 case CILK_SPAWN_STMT:
13944 cfun->calls_cilk_spawn = 1;
13945 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
13946
13947 case CILK_SYNC_STMT:
13948 RETURN (build_cilk_sync ());
13949
13950 case COMPOUND_EXPR:
13951 tmp = RECUR (TREE_OPERAND (t, 0));
13952 if (tmp == NULL_TREE)
13953 /* If the first operand was a statement, we're done with it. */
13954 RETURN (RECUR (TREE_OPERAND (t, 1)));
13955 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13956 RECUR (TREE_OPERAND (t, 1)),
13957 complain));
13958
13959 case ANNOTATE_EXPR:
13960 tmp = RECUR (TREE_OPERAND (t, 0));
13961 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
13962 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
13963
13964 default:
13965 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13966
13967 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13968 /*function_p=*/false,
13969 integral_constant_expression_p));
13970 }
13971
13972 RETURN (NULL_TREE);
13973 out:
13974 input_location = loc;
13975 return r;
13976 #undef RECUR
13977 #undef RETURN
13978 }
13979
13980 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13981 function. For description of the body see comment above
13982 cp_parser_omp_declare_reduction_exprs. */
13983
13984 static void
13985 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13986 {
13987 if (t == NULL_TREE || t == error_mark_node)
13988 return;
13989
13990 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
13991
13992 tree_stmt_iterator tsi;
13993 int i;
13994 tree stmts[7];
13995 memset (stmts, 0, sizeof stmts);
13996 for (i = 0, tsi = tsi_start (t);
13997 i < 7 && !tsi_end_p (tsi);
13998 i++, tsi_next (&tsi))
13999 stmts[i] = tsi_stmt (tsi);
14000 gcc_assert (tsi_end_p (tsi));
14001
14002 if (i >= 3)
14003 {
14004 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
14005 && TREE_CODE (stmts[1]) == DECL_EXPR);
14006 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
14007 args, complain, in_decl);
14008 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
14009 args, complain, in_decl);
14010 DECL_CONTEXT (omp_out) = current_function_decl;
14011 DECL_CONTEXT (omp_in) = current_function_decl;
14012 keep_next_level (true);
14013 tree block = begin_omp_structured_block ();
14014 tsubst_expr (stmts[2], args, complain, in_decl, false);
14015 block = finish_omp_structured_block (block);
14016 block = maybe_cleanup_point_expr_void (block);
14017 add_decl_expr (omp_out);
14018 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
14019 TREE_NO_WARNING (omp_out) = 1;
14020 add_decl_expr (omp_in);
14021 finish_expr_stmt (block);
14022 }
14023 if (i >= 6)
14024 {
14025 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
14026 && TREE_CODE (stmts[4]) == DECL_EXPR);
14027 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
14028 args, complain, in_decl);
14029 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
14030 args, complain, in_decl);
14031 DECL_CONTEXT (omp_priv) = current_function_decl;
14032 DECL_CONTEXT (omp_orig) = current_function_decl;
14033 keep_next_level (true);
14034 tree block = begin_omp_structured_block ();
14035 tsubst_expr (stmts[5], args, complain, in_decl, false);
14036 block = finish_omp_structured_block (block);
14037 block = maybe_cleanup_point_expr_void (block);
14038 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
14039 add_decl_expr (omp_priv);
14040 add_decl_expr (omp_orig);
14041 finish_expr_stmt (block);
14042 if (i == 7)
14043 add_decl_expr (omp_orig);
14044 }
14045 }
14046
14047 /* T is a postfix-expression that is not being used in a function
14048 call. Return the substituted version of T. */
14049
14050 static tree
14051 tsubst_non_call_postfix_expression (tree t, tree args,
14052 tsubst_flags_t complain,
14053 tree in_decl)
14054 {
14055 if (TREE_CODE (t) == SCOPE_REF)
14056 t = tsubst_qualified_id (t, args, complain, in_decl,
14057 /*done=*/false, /*address_p=*/false);
14058 else
14059 t = tsubst_copy_and_build (t, args, complain, in_decl,
14060 /*function_p=*/false,
14061 /*integral_constant_expression_p=*/false);
14062
14063 return t;
14064 }
14065
14066 /* Like tsubst but deals with expressions and performs semantic
14067 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14068
14069 tree
14070 tsubst_copy_and_build (tree t,
14071 tree args,
14072 tsubst_flags_t complain,
14073 tree in_decl,
14074 bool function_p,
14075 bool integral_constant_expression_p)
14076 {
14077 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14078 #define RECUR(NODE) \
14079 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14080 /*function_p=*/false, \
14081 integral_constant_expression_p)
14082
14083 tree retval, op1;
14084 location_t loc;
14085
14086 if (t == NULL_TREE || t == error_mark_node)
14087 return t;
14088
14089 loc = input_location;
14090 if (EXPR_HAS_LOCATION (t))
14091 input_location = EXPR_LOCATION (t);
14092
14093 /* N3276 decltype magic only applies to calls at the top level or on the
14094 right side of a comma. */
14095 tsubst_flags_t decltype_flag = (complain & tf_decltype);
14096 complain &= ~tf_decltype;
14097
14098 switch (TREE_CODE (t))
14099 {
14100 case USING_DECL:
14101 t = DECL_NAME (t);
14102 /* Fall through. */
14103 case IDENTIFIER_NODE:
14104 {
14105 tree decl;
14106 cp_id_kind idk;
14107 bool non_integral_constant_expression_p;
14108 const char *error_msg;
14109
14110 if (IDENTIFIER_TYPENAME_P (t))
14111 {
14112 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14113 t = mangle_conv_op_name_for_type (new_type);
14114 }
14115
14116 /* Look up the name. */
14117 decl = lookup_name (t);
14118
14119 /* By convention, expressions use ERROR_MARK_NODE to indicate
14120 failure, not NULL_TREE. */
14121 if (decl == NULL_TREE)
14122 decl = error_mark_node;
14123
14124 decl = finish_id_expression (t, decl, NULL_TREE,
14125 &idk,
14126 integral_constant_expression_p,
14127 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
14128 &non_integral_constant_expression_p,
14129 /*template_p=*/false,
14130 /*done=*/true,
14131 /*address_p=*/false,
14132 /*template_arg_p=*/false,
14133 &error_msg,
14134 input_location);
14135 if (error_msg)
14136 error (error_msg);
14137 if (!function_p && identifier_p (decl))
14138 {
14139 if (complain & tf_error)
14140 unqualified_name_lookup_error (decl);
14141 decl = error_mark_node;
14142 }
14143 RETURN (decl);
14144 }
14145
14146 case TEMPLATE_ID_EXPR:
14147 {
14148 tree object;
14149 tree templ = RECUR (TREE_OPERAND (t, 0));
14150 tree targs = TREE_OPERAND (t, 1);
14151
14152 if (targs)
14153 targs = tsubst_template_args (targs, args, complain, in_decl);
14154
14155 if (TREE_CODE (templ) == COMPONENT_REF)
14156 {
14157 object = TREE_OPERAND (templ, 0);
14158 templ = TREE_OPERAND (templ, 1);
14159 }
14160 else
14161 object = NULL_TREE;
14162 templ = lookup_template_function (templ, targs);
14163
14164 if (object)
14165 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
14166 object, templ, NULL_TREE));
14167 else
14168 RETURN (baselink_for_fns (templ));
14169 }
14170
14171 case INDIRECT_REF:
14172 {
14173 tree r = RECUR (TREE_OPERAND (t, 0));
14174
14175 if (REFERENCE_REF_P (t))
14176 {
14177 /* A type conversion to reference type will be enclosed in
14178 such an indirect ref, but the substitution of the cast
14179 will have also added such an indirect ref. */
14180 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14181 r = convert_from_reference (r);
14182 }
14183 else
14184 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14185 complain|decltype_flag);
14186 RETURN (r);
14187 }
14188
14189 case NOP_EXPR:
14190 RETURN (build_nop
14191 (tsubst (TREE_TYPE (t), args, complain, in_decl),
14192 RECUR (TREE_OPERAND (t, 0))));
14193
14194 case IMPLICIT_CONV_EXPR:
14195 {
14196 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14197 tree expr = RECUR (TREE_OPERAND (t, 0));
14198 int flags = LOOKUP_IMPLICIT;
14199 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14200 flags = LOOKUP_NORMAL;
14201 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14202 flags));
14203 }
14204
14205 case CONVERT_EXPR:
14206 RETURN (build1
14207 (CONVERT_EXPR,
14208 tsubst (TREE_TYPE (t), args, complain, in_decl),
14209 RECUR (TREE_OPERAND (t, 0))));
14210
14211 case CAST_EXPR:
14212 case REINTERPRET_CAST_EXPR:
14213 case CONST_CAST_EXPR:
14214 case DYNAMIC_CAST_EXPR:
14215 case STATIC_CAST_EXPR:
14216 {
14217 tree type;
14218 tree op, r = NULL_TREE;
14219
14220 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14221 if (integral_constant_expression_p
14222 && !cast_valid_in_integral_constant_expression_p (type))
14223 {
14224 if (complain & tf_error)
14225 error ("a cast to a type other than an integral or "
14226 "enumeration type cannot appear in a constant-expression");
14227 RETURN (error_mark_node);
14228 }
14229
14230 op = RECUR (TREE_OPERAND (t, 0));
14231
14232 ++c_inhibit_evaluation_warnings;
14233 switch (TREE_CODE (t))
14234 {
14235 case CAST_EXPR:
14236 r = build_functional_cast (type, op, complain);
14237 break;
14238 case REINTERPRET_CAST_EXPR:
14239 r = build_reinterpret_cast (type, op, complain);
14240 break;
14241 case CONST_CAST_EXPR:
14242 r = build_const_cast (type, op, complain);
14243 break;
14244 case DYNAMIC_CAST_EXPR:
14245 r = build_dynamic_cast (type, op, complain);
14246 break;
14247 case STATIC_CAST_EXPR:
14248 r = build_static_cast (type, op, complain);
14249 break;
14250 default:
14251 gcc_unreachable ();
14252 }
14253 --c_inhibit_evaluation_warnings;
14254
14255 RETURN (r);
14256 }
14257
14258 case POSTDECREMENT_EXPR:
14259 case POSTINCREMENT_EXPR:
14260 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14261 args, complain, in_decl);
14262 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14263 complain|decltype_flag));
14264
14265 case PREDECREMENT_EXPR:
14266 case PREINCREMENT_EXPR:
14267 case NEGATE_EXPR:
14268 case BIT_NOT_EXPR:
14269 case ABS_EXPR:
14270 case TRUTH_NOT_EXPR:
14271 case UNARY_PLUS_EXPR: /* Unary + */
14272 case REALPART_EXPR:
14273 case IMAGPART_EXPR:
14274 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14275 RECUR (TREE_OPERAND (t, 0)),
14276 complain|decltype_flag));
14277
14278 case FIX_TRUNC_EXPR:
14279 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14280 0, complain));
14281
14282 case ADDR_EXPR:
14283 op1 = TREE_OPERAND (t, 0);
14284 if (TREE_CODE (op1) == LABEL_DECL)
14285 RETURN (finish_label_address_expr (DECL_NAME (op1),
14286 EXPR_LOCATION (op1)));
14287 if (TREE_CODE (op1) == SCOPE_REF)
14288 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14289 /*done=*/true, /*address_p=*/true);
14290 else
14291 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14292 in_decl);
14293 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14294 complain|decltype_flag));
14295
14296 case PLUS_EXPR:
14297 case MINUS_EXPR:
14298 case MULT_EXPR:
14299 case TRUNC_DIV_EXPR:
14300 case CEIL_DIV_EXPR:
14301 case FLOOR_DIV_EXPR:
14302 case ROUND_DIV_EXPR:
14303 case EXACT_DIV_EXPR:
14304 case BIT_AND_EXPR:
14305 case BIT_IOR_EXPR:
14306 case BIT_XOR_EXPR:
14307 case TRUNC_MOD_EXPR:
14308 case FLOOR_MOD_EXPR:
14309 case TRUTH_ANDIF_EXPR:
14310 case TRUTH_ORIF_EXPR:
14311 case TRUTH_AND_EXPR:
14312 case TRUTH_OR_EXPR:
14313 case RSHIFT_EXPR:
14314 case LSHIFT_EXPR:
14315 case RROTATE_EXPR:
14316 case LROTATE_EXPR:
14317 case EQ_EXPR:
14318 case NE_EXPR:
14319 case MAX_EXPR:
14320 case MIN_EXPR:
14321 case LE_EXPR:
14322 case GE_EXPR:
14323 case LT_EXPR:
14324 case GT_EXPR:
14325 case MEMBER_REF:
14326 case DOTSTAR_EXPR:
14327 {
14328 tree r;
14329
14330 ++c_inhibit_evaluation_warnings;
14331
14332 r = build_x_binary_op
14333 (input_location, TREE_CODE (t),
14334 RECUR (TREE_OPERAND (t, 0)),
14335 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14336 ? ERROR_MARK
14337 : TREE_CODE (TREE_OPERAND (t, 0))),
14338 RECUR (TREE_OPERAND (t, 1)),
14339 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14340 ? ERROR_MARK
14341 : TREE_CODE (TREE_OPERAND (t, 1))),
14342 /*overload=*/NULL,
14343 complain|decltype_flag);
14344 if (EXPR_P (r) && TREE_NO_WARNING (t))
14345 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14346
14347 --c_inhibit_evaluation_warnings;
14348
14349 RETURN (r);
14350 }
14351
14352 case POINTER_PLUS_EXPR:
14353 return fold_build_pointer_plus (RECUR (TREE_OPERAND (t, 0)),
14354 RECUR (TREE_OPERAND (t, 1)));
14355
14356 case SCOPE_REF:
14357 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14358 /*address_p=*/false));
14359 case ARRAY_REF:
14360 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14361 args, complain, in_decl);
14362 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14363 RECUR (TREE_OPERAND (t, 1)),
14364 complain|decltype_flag));
14365
14366 case ARRAY_NOTATION_REF:
14367 {
14368 tree start_index, length, stride;
14369 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14370 args, complain, in_decl);
14371 start_index = RECUR (ARRAY_NOTATION_START (t));
14372 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14373 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14374 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14375 length, stride, TREE_TYPE (op1)));
14376 }
14377 case SIZEOF_EXPR:
14378 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14379 RETURN (tsubst_copy (t, args, complain, in_decl));
14380 /* Fall through */
14381
14382 case ALIGNOF_EXPR:
14383 {
14384 tree r;
14385
14386 op1 = TREE_OPERAND (t, 0);
14387 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14388 op1 = TREE_TYPE (op1);
14389 if (!args)
14390 {
14391 /* When there are no ARGS, we are trying to evaluate a
14392 non-dependent expression from the parser. Trying to do
14393 the substitutions may not work. */
14394 if (!TYPE_P (op1))
14395 op1 = TREE_TYPE (op1);
14396 }
14397 else
14398 {
14399 ++cp_unevaluated_operand;
14400 ++c_inhibit_evaluation_warnings;
14401 if (TYPE_P (op1))
14402 op1 = tsubst (op1, args, complain, in_decl);
14403 else
14404 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14405 /*function_p=*/false,
14406 /*integral_constant_expression_p=*/
14407 false);
14408 --cp_unevaluated_operand;
14409 --c_inhibit_evaluation_warnings;
14410 }
14411 if (TYPE_P (op1))
14412 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14413 complain & tf_error);
14414 else
14415 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14416 complain & tf_error);
14417 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14418 {
14419 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14420 {
14421 if (!processing_template_decl && TYPE_P (op1))
14422 {
14423 r = build_min (SIZEOF_EXPR, size_type_node,
14424 build1 (NOP_EXPR, op1, error_mark_node));
14425 SIZEOF_EXPR_TYPE_P (r) = 1;
14426 }
14427 else
14428 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14429 TREE_SIDE_EFFECTS (r) = 0;
14430 TREE_READONLY (r) = 1;
14431 }
14432 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14433 }
14434 RETURN (r);
14435 }
14436
14437 case AT_ENCODE_EXPR:
14438 {
14439 op1 = TREE_OPERAND (t, 0);
14440 ++cp_unevaluated_operand;
14441 ++c_inhibit_evaluation_warnings;
14442 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14443 /*function_p=*/false,
14444 /*integral_constant_expression_p=*/false);
14445 --cp_unevaluated_operand;
14446 --c_inhibit_evaluation_warnings;
14447 RETURN (objc_build_encode_expr (op1));
14448 }
14449
14450 case NOEXCEPT_EXPR:
14451 op1 = TREE_OPERAND (t, 0);
14452 ++cp_unevaluated_operand;
14453 ++c_inhibit_evaluation_warnings;
14454 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14455 /*function_p=*/false,
14456 /*integral_constant_expression_p=*/false);
14457 --cp_unevaluated_operand;
14458 --c_inhibit_evaluation_warnings;
14459 RETURN (finish_noexcept_expr (op1, complain));
14460
14461 case MODOP_EXPR:
14462 {
14463 tree r;
14464
14465 ++c_inhibit_evaluation_warnings;
14466
14467 r = build_x_modify_expr
14468 (EXPR_LOCATION (t),
14469 RECUR (TREE_OPERAND (t, 0)),
14470 TREE_CODE (TREE_OPERAND (t, 1)),
14471 RECUR (TREE_OPERAND (t, 2)),
14472 complain|decltype_flag);
14473 /* TREE_NO_WARNING must be set if either the expression was
14474 parenthesized or it uses an operator such as >>= rather
14475 than plain assignment. In the former case, it was already
14476 set and must be copied. In the latter case,
14477 build_x_modify_expr sets it and it must not be reset
14478 here. */
14479 if (TREE_NO_WARNING (t))
14480 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14481
14482 --c_inhibit_evaluation_warnings;
14483
14484 RETURN (r);
14485 }
14486
14487 case ARROW_EXPR:
14488 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14489 args, complain, in_decl);
14490 /* Remember that there was a reference to this entity. */
14491 if (DECL_P (op1))
14492 mark_used (op1);
14493 RETURN (build_x_arrow (input_location, op1, complain));
14494
14495 case NEW_EXPR:
14496 {
14497 tree placement = RECUR (TREE_OPERAND (t, 0));
14498 tree init = RECUR (TREE_OPERAND (t, 3));
14499 vec<tree, va_gc> *placement_vec;
14500 vec<tree, va_gc> *init_vec;
14501 tree ret;
14502
14503 if (placement == NULL_TREE)
14504 placement_vec = NULL;
14505 else
14506 {
14507 placement_vec = make_tree_vector ();
14508 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14509 vec_safe_push (placement_vec, TREE_VALUE (placement));
14510 }
14511
14512 /* If there was an initializer in the original tree, but it
14513 instantiated to an empty list, then we should pass a
14514 non-NULL empty vector to tell build_new that it was an
14515 empty initializer() rather than no initializer. This can
14516 only happen when the initializer is a pack expansion whose
14517 parameter packs are of length zero. */
14518 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14519 init_vec = NULL;
14520 else
14521 {
14522 init_vec = make_tree_vector ();
14523 if (init == void_zero_node)
14524 gcc_assert (init_vec != NULL);
14525 else
14526 {
14527 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14528 vec_safe_push (init_vec, TREE_VALUE (init));
14529 }
14530 }
14531
14532 ret = build_new (&placement_vec,
14533 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14534 RECUR (TREE_OPERAND (t, 2)),
14535 &init_vec,
14536 NEW_EXPR_USE_GLOBAL (t),
14537 complain);
14538
14539 if (placement_vec != NULL)
14540 release_tree_vector (placement_vec);
14541 if (init_vec != NULL)
14542 release_tree_vector (init_vec);
14543
14544 RETURN (ret);
14545 }
14546
14547 case DELETE_EXPR:
14548 RETURN (delete_sanity
14549 (RECUR (TREE_OPERAND (t, 0)),
14550 RECUR (TREE_OPERAND (t, 1)),
14551 DELETE_EXPR_USE_VEC (t),
14552 DELETE_EXPR_USE_GLOBAL (t),
14553 complain));
14554
14555 case COMPOUND_EXPR:
14556 {
14557 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14558 complain & ~tf_decltype, in_decl,
14559 /*function_p=*/false,
14560 integral_constant_expression_p);
14561 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14562 op0,
14563 RECUR (TREE_OPERAND (t, 1)),
14564 complain|decltype_flag));
14565 }
14566
14567 case CALL_EXPR:
14568 {
14569 tree function;
14570 vec<tree, va_gc> *call_args;
14571 unsigned int nargs, i;
14572 bool qualified_p;
14573 bool koenig_p;
14574 tree ret;
14575
14576 function = CALL_EXPR_FN (t);
14577 /* When we parsed the expression, we determined whether or
14578 not Koenig lookup should be performed. */
14579 koenig_p = KOENIG_LOOKUP_P (t);
14580 if (TREE_CODE (function) == SCOPE_REF)
14581 {
14582 qualified_p = true;
14583 function = tsubst_qualified_id (function, args, complain, in_decl,
14584 /*done=*/false,
14585 /*address_p=*/false);
14586 }
14587 else if (koenig_p && identifier_p (function))
14588 {
14589 /* Do nothing; calling tsubst_copy_and_build on an identifier
14590 would incorrectly perform unqualified lookup again.
14591
14592 Note that we can also have an IDENTIFIER_NODE if the earlier
14593 unqualified lookup found a member function; in that case
14594 koenig_p will be false and we do want to do the lookup
14595 again to find the instantiated member function.
14596
14597 FIXME but doing that causes c++/15272, so we need to stop
14598 using IDENTIFIER_NODE in that situation. */
14599 qualified_p = false;
14600 }
14601 else
14602 {
14603 if (TREE_CODE (function) == COMPONENT_REF)
14604 {
14605 tree op = TREE_OPERAND (function, 1);
14606
14607 qualified_p = (TREE_CODE (op) == SCOPE_REF
14608 || (BASELINK_P (op)
14609 && BASELINK_QUALIFIED_P (op)));
14610 }
14611 else
14612 qualified_p = false;
14613
14614 if (TREE_CODE (function) == ADDR_EXPR
14615 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14616 /* Avoid error about taking the address of a constructor. */
14617 function = TREE_OPERAND (function, 0);
14618
14619 function = tsubst_copy_and_build (function, args, complain,
14620 in_decl,
14621 !qualified_p,
14622 integral_constant_expression_p);
14623
14624 if (BASELINK_P (function))
14625 qualified_p = true;
14626 }
14627
14628 nargs = call_expr_nargs (t);
14629 call_args = make_tree_vector ();
14630 for (i = 0; i < nargs; ++i)
14631 {
14632 tree arg = CALL_EXPR_ARG (t, i);
14633
14634 if (!PACK_EXPANSION_P (arg))
14635 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14636 else
14637 {
14638 /* Expand the pack expansion and push each entry onto
14639 CALL_ARGS. */
14640 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14641 if (TREE_CODE (arg) == TREE_VEC)
14642 {
14643 unsigned int len, j;
14644
14645 len = TREE_VEC_LENGTH (arg);
14646 for (j = 0; j < len; ++j)
14647 {
14648 tree value = TREE_VEC_ELT (arg, j);
14649 if (value != NULL_TREE)
14650 value = convert_from_reference (value);
14651 vec_safe_push (call_args, value);
14652 }
14653 }
14654 else
14655 {
14656 /* A partial substitution. Add one entry. */
14657 vec_safe_push (call_args, arg);
14658 }
14659 }
14660 }
14661
14662 /* We do not perform argument-dependent lookup if normal
14663 lookup finds a non-function, in accordance with the
14664 expected resolution of DR 218. */
14665 if (koenig_p
14666 && ((is_overloaded_fn (function)
14667 /* If lookup found a member function, the Koenig lookup is
14668 not appropriate, even if an unqualified-name was used
14669 to denote the function. */
14670 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14671 || identifier_p (function))
14672 /* Only do this when substitution turns a dependent call
14673 into a non-dependent call. */
14674 && type_dependent_expression_p_push (t)
14675 && !any_type_dependent_arguments_p (call_args))
14676 function = perform_koenig_lookup (function, call_args, tf_none);
14677
14678 if (identifier_p (function)
14679 && !any_type_dependent_arguments_p (call_args))
14680 {
14681 if (koenig_p && (complain & tf_warning_or_error))
14682 {
14683 /* For backwards compatibility and good diagnostics, try
14684 the unqualified lookup again if we aren't in SFINAE
14685 context. */
14686 tree unq = (tsubst_copy_and_build
14687 (function, args, complain, in_decl, true,
14688 integral_constant_expression_p));
14689 if (unq == error_mark_node)
14690 RETURN (error_mark_node);
14691
14692 if (unq != function)
14693 {
14694 tree fn = unq;
14695 if (INDIRECT_REF_P (fn))
14696 fn = TREE_OPERAND (fn, 0);
14697 if (TREE_CODE (fn) == COMPONENT_REF)
14698 fn = TREE_OPERAND (fn, 1);
14699 if (is_overloaded_fn (fn))
14700 fn = get_first_fn (fn);
14701 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
14702 "%qD was not declared in this scope, "
14703 "and no declarations were found by "
14704 "argument-dependent lookup at the point "
14705 "of instantiation", function))
14706 {
14707 if (!DECL_P (fn))
14708 /* Can't say anything more. */;
14709 else if (DECL_CLASS_SCOPE_P (fn))
14710 {
14711 location_t loc = EXPR_LOC_OR_LOC (t,
14712 input_location);
14713 inform (loc,
14714 "declarations in dependent base %qT are "
14715 "not found by unqualified lookup",
14716 DECL_CLASS_CONTEXT (fn));
14717 if (current_class_ptr)
14718 inform (loc,
14719 "use %<this->%D%> instead", function);
14720 else
14721 inform (loc,
14722 "use %<%T::%D%> instead",
14723 current_class_name, function);
14724 }
14725 else
14726 inform (0, "%q+D declared here, later in the "
14727 "translation unit", fn);
14728 }
14729 function = unq;
14730 }
14731 }
14732 if (identifier_p (function))
14733 {
14734 if (complain & tf_error)
14735 unqualified_name_lookup_error (function);
14736 release_tree_vector (call_args);
14737 RETURN (error_mark_node);
14738 }
14739 }
14740
14741 /* Remember that there was a reference to this entity. */
14742 if (DECL_P (function))
14743 mark_used (function);
14744
14745 /* Put back tf_decltype for the actual call. */
14746 complain |= decltype_flag;
14747
14748 if (TREE_CODE (function) == OFFSET_REF)
14749 ret = build_offset_ref_call_from_tree (function, &call_args,
14750 complain);
14751 else if (TREE_CODE (function) == COMPONENT_REF)
14752 {
14753 tree instance = TREE_OPERAND (function, 0);
14754 tree fn = TREE_OPERAND (function, 1);
14755
14756 if (processing_template_decl
14757 && (type_dependent_expression_p (instance)
14758 || (!BASELINK_P (fn)
14759 && TREE_CODE (fn) != FIELD_DECL)
14760 || type_dependent_expression_p (fn)
14761 || any_type_dependent_arguments_p (call_args)))
14762 ret = build_nt_call_vec (function, call_args);
14763 else if (!BASELINK_P (fn))
14764 ret = finish_call_expr (function, &call_args,
14765 /*disallow_virtual=*/false,
14766 /*koenig_p=*/false,
14767 complain);
14768 else
14769 ret = (build_new_method_call
14770 (instance, fn,
14771 &call_args, NULL_TREE,
14772 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14773 /*fn_p=*/NULL,
14774 complain));
14775 }
14776 else
14777 ret = finish_call_expr (function, &call_args,
14778 /*disallow_virtual=*/qualified_p,
14779 koenig_p,
14780 complain);
14781
14782 release_tree_vector (call_args);
14783
14784 RETURN (ret);
14785 }
14786
14787 case COND_EXPR:
14788 {
14789 tree cond = RECUR (TREE_OPERAND (t, 0));
14790 tree exp1, exp2;
14791
14792 if (TREE_CODE (cond) == INTEGER_CST)
14793 {
14794 if (integer_zerop (cond))
14795 {
14796 ++c_inhibit_evaluation_warnings;
14797 exp1 = RECUR (TREE_OPERAND (t, 1));
14798 --c_inhibit_evaluation_warnings;
14799 exp2 = RECUR (TREE_OPERAND (t, 2));
14800 }
14801 else
14802 {
14803 exp1 = RECUR (TREE_OPERAND (t, 1));
14804 ++c_inhibit_evaluation_warnings;
14805 exp2 = RECUR (TREE_OPERAND (t, 2));
14806 --c_inhibit_evaluation_warnings;
14807 }
14808 }
14809 else
14810 {
14811 exp1 = RECUR (TREE_OPERAND (t, 1));
14812 exp2 = RECUR (TREE_OPERAND (t, 2));
14813 }
14814
14815 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14816 cond, exp1, exp2, complain));
14817 }
14818
14819 case PSEUDO_DTOR_EXPR:
14820 RETURN (finish_pseudo_destructor_expr
14821 (RECUR (TREE_OPERAND (t, 0)),
14822 RECUR (TREE_OPERAND (t, 1)),
14823 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14824 input_location));
14825
14826 case TREE_LIST:
14827 {
14828 tree purpose, value, chain;
14829
14830 if (t == void_list_node)
14831 RETURN (t);
14832
14833 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14834 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14835 {
14836 /* We have pack expansions, so expand those and
14837 create a new list out of it. */
14838 tree purposevec = NULL_TREE;
14839 tree valuevec = NULL_TREE;
14840 tree chain;
14841 int i, len = -1;
14842
14843 /* Expand the argument expressions. */
14844 if (TREE_PURPOSE (t))
14845 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14846 complain, in_decl);
14847 if (TREE_VALUE (t))
14848 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14849 complain, in_decl);
14850
14851 /* Build the rest of the list. */
14852 chain = TREE_CHAIN (t);
14853 if (chain && chain != void_type_node)
14854 chain = RECUR (chain);
14855
14856 /* Determine the number of arguments. */
14857 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14858 {
14859 len = TREE_VEC_LENGTH (purposevec);
14860 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14861 }
14862 else if (TREE_CODE (valuevec) == TREE_VEC)
14863 len = TREE_VEC_LENGTH (valuevec);
14864 else
14865 {
14866 /* Since we only performed a partial substitution into
14867 the argument pack, we only RETURN (a single list
14868 node. */
14869 if (purposevec == TREE_PURPOSE (t)
14870 && valuevec == TREE_VALUE (t)
14871 && chain == TREE_CHAIN (t))
14872 RETURN (t);
14873
14874 RETURN (tree_cons (purposevec, valuevec, chain));
14875 }
14876
14877 /* Convert the argument vectors into a TREE_LIST */
14878 i = len;
14879 while (i > 0)
14880 {
14881 /* Grab the Ith values. */
14882 i--;
14883 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14884 : NULL_TREE;
14885 value
14886 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14887 : NULL_TREE;
14888
14889 /* Build the list (backwards). */
14890 chain = tree_cons (purpose, value, chain);
14891 }
14892
14893 RETURN (chain);
14894 }
14895
14896 purpose = TREE_PURPOSE (t);
14897 if (purpose)
14898 purpose = RECUR (purpose);
14899 value = TREE_VALUE (t);
14900 if (value)
14901 value = RECUR (value);
14902 chain = TREE_CHAIN (t);
14903 if (chain && chain != void_type_node)
14904 chain = RECUR (chain);
14905 if (purpose == TREE_PURPOSE (t)
14906 && value == TREE_VALUE (t)
14907 && chain == TREE_CHAIN (t))
14908 RETURN (t);
14909 RETURN (tree_cons (purpose, value, chain));
14910 }
14911
14912 case COMPONENT_REF:
14913 {
14914 tree object;
14915 tree object_type;
14916 tree member;
14917
14918 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14919 args, complain, in_decl);
14920 /* Remember that there was a reference to this entity. */
14921 if (DECL_P (object))
14922 mark_used (object);
14923 object_type = TREE_TYPE (object);
14924
14925 member = TREE_OPERAND (t, 1);
14926 if (BASELINK_P (member))
14927 member = tsubst_baselink (member,
14928 non_reference (TREE_TYPE (object)),
14929 args, complain, in_decl);
14930 else
14931 member = tsubst_copy (member, args, complain, in_decl);
14932 if (member == error_mark_node)
14933 RETURN (error_mark_node);
14934
14935 if (type_dependent_expression_p (object))
14936 /* We can't do much here. */;
14937 else if (!CLASS_TYPE_P (object_type))
14938 {
14939 if (scalarish_type_p (object_type))
14940 {
14941 tree s = NULL_TREE;
14942 tree dtor = member;
14943
14944 if (TREE_CODE (dtor) == SCOPE_REF)
14945 {
14946 s = TREE_OPERAND (dtor, 0);
14947 dtor = TREE_OPERAND (dtor, 1);
14948 }
14949 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14950 {
14951 dtor = TREE_OPERAND (dtor, 0);
14952 if (TYPE_P (dtor))
14953 RETURN (finish_pseudo_destructor_expr
14954 (object, s, dtor, input_location));
14955 }
14956 }
14957 }
14958 else if (TREE_CODE (member) == SCOPE_REF
14959 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14960 {
14961 /* Lookup the template functions now that we know what the
14962 scope is. */
14963 tree scope = TREE_OPERAND (member, 0);
14964 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14965 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14966 member = lookup_qualified_name (scope, tmpl,
14967 /*is_type_p=*/false,
14968 /*complain=*/false);
14969 if (BASELINK_P (member))
14970 {
14971 BASELINK_FUNCTIONS (member)
14972 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14973 args);
14974 member = (adjust_result_of_qualified_name_lookup
14975 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14976 object_type));
14977 }
14978 else
14979 {
14980 qualified_name_lookup_error (scope, tmpl, member,
14981 input_location);
14982 RETURN (error_mark_node);
14983 }
14984 }
14985 else if (TREE_CODE (member) == SCOPE_REF
14986 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14987 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14988 {
14989 if (complain & tf_error)
14990 {
14991 if (TYPE_P (TREE_OPERAND (member, 0)))
14992 error ("%qT is not a class or namespace",
14993 TREE_OPERAND (member, 0));
14994 else
14995 error ("%qD is not a class or namespace",
14996 TREE_OPERAND (member, 0));
14997 }
14998 RETURN (error_mark_node);
14999 }
15000 else if (TREE_CODE (member) == FIELD_DECL)
15001 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
15002
15003 RETURN (finish_class_member_access_expr (object, member,
15004 /*template_p=*/false,
15005 complain));
15006 }
15007
15008 case THROW_EXPR:
15009 RETURN (build_throw
15010 (RECUR (TREE_OPERAND (t, 0))));
15011
15012 case CONSTRUCTOR:
15013 {
15014 vec<constructor_elt, va_gc> *n;
15015 constructor_elt *ce;
15016 unsigned HOST_WIDE_INT idx;
15017 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15018 bool process_index_p;
15019 int newlen;
15020 bool need_copy_p = false;
15021 tree r;
15022
15023 if (type == error_mark_node)
15024 RETURN (error_mark_node);
15025
15026 /* digest_init will do the wrong thing if we let it. */
15027 if (type && TYPE_PTRMEMFUNC_P (type))
15028 RETURN (t);
15029
15030 /* We do not want to process the index of aggregate
15031 initializers as they are identifier nodes which will be
15032 looked up by digest_init. */
15033 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
15034
15035 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
15036 newlen = vec_safe_length (n);
15037 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
15038 {
15039 if (ce->index && process_index_p
15040 /* An identifier index is looked up in the type
15041 being initialized, not the current scope. */
15042 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
15043 ce->index = RECUR (ce->index);
15044
15045 if (PACK_EXPANSION_P (ce->value))
15046 {
15047 /* Substitute into the pack expansion. */
15048 ce->value = tsubst_pack_expansion (ce->value, args, complain,
15049 in_decl);
15050
15051 if (ce->value == error_mark_node
15052 || PACK_EXPANSION_P (ce->value))
15053 ;
15054 else if (TREE_VEC_LENGTH (ce->value) == 1)
15055 /* Just move the argument into place. */
15056 ce->value = TREE_VEC_ELT (ce->value, 0);
15057 else
15058 {
15059 /* Update the length of the final CONSTRUCTOR
15060 arguments vector, and note that we will need to
15061 copy.*/
15062 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
15063 need_copy_p = true;
15064 }
15065 }
15066 else
15067 ce->value = RECUR (ce->value);
15068 }
15069
15070 if (need_copy_p)
15071 {
15072 vec<constructor_elt, va_gc> *old_n = n;
15073
15074 vec_alloc (n, newlen);
15075 FOR_EACH_VEC_ELT (*old_n, idx, ce)
15076 {
15077 if (TREE_CODE (ce->value) == TREE_VEC)
15078 {
15079 int i, len = TREE_VEC_LENGTH (ce->value);
15080 for (i = 0; i < len; ++i)
15081 CONSTRUCTOR_APPEND_ELT (n, 0,
15082 TREE_VEC_ELT (ce->value, i));
15083 }
15084 else
15085 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
15086 }
15087 }
15088
15089 r = build_constructor (init_list_type_node, n);
15090 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
15091
15092 if (TREE_HAS_CONSTRUCTOR (t))
15093 RETURN (finish_compound_literal (type, r, complain));
15094
15095 TREE_TYPE (r) = type;
15096 RETURN (r);
15097 }
15098
15099 case TYPEID_EXPR:
15100 {
15101 tree operand_0 = TREE_OPERAND (t, 0);
15102 if (TYPE_P (operand_0))
15103 {
15104 operand_0 = tsubst (operand_0, args, complain, in_decl);
15105 RETURN (get_typeid (operand_0, complain));
15106 }
15107 else
15108 {
15109 operand_0 = RECUR (operand_0);
15110 RETURN (build_typeid (operand_0, complain));
15111 }
15112 }
15113
15114 case VAR_DECL:
15115 if (!args)
15116 RETURN (t);
15117 else if (DECL_PACK_P (t))
15118 {
15119 /* We don't build decls for an instantiation of a
15120 variadic capture proxy, we instantiate the elements
15121 when needed. */
15122 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
15123 return RECUR (DECL_VALUE_EXPR (t));
15124 }
15125 /* Fall through */
15126
15127 case PARM_DECL:
15128 {
15129 tree r = tsubst_copy (t, args, complain, in_decl);
15130
15131 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
15132 /* If the original type was a reference, we'll be wrapped in
15133 the appropriate INDIRECT_REF. */
15134 r = convert_from_reference (r);
15135 RETURN (r);
15136 }
15137
15138 case VA_ARG_EXPR:
15139 RETURN (build_x_va_arg (EXPR_LOCATION (t),
15140 RECUR (TREE_OPERAND (t, 0)),
15141 tsubst (TREE_TYPE (t), args, complain, in_decl)));
15142
15143 case OFFSETOF_EXPR:
15144 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
15145
15146 case TRAIT_EXPR:
15147 {
15148 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
15149 complain, in_decl);
15150
15151 tree type2 = TRAIT_EXPR_TYPE2 (t);
15152 if (type2)
15153 type2 = tsubst (type2, args, complain, in_decl);
15154
15155 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
15156 }
15157
15158 case STMT_EXPR:
15159 {
15160 tree old_stmt_expr = cur_stmt_expr;
15161 tree stmt_expr = begin_stmt_expr ();
15162
15163 cur_stmt_expr = stmt_expr;
15164 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
15165 integral_constant_expression_p);
15166 stmt_expr = finish_stmt_expr (stmt_expr, false);
15167 cur_stmt_expr = old_stmt_expr;
15168
15169 /* If the resulting list of expression statement is empty,
15170 fold it further into void_zero_node. */
15171 if (empty_expr_stmt_p (stmt_expr))
15172 stmt_expr = void_zero_node;
15173
15174 RETURN (stmt_expr);
15175 }
15176
15177 case LAMBDA_EXPR:
15178 {
15179 tree r = build_lambda_expr ();
15180
15181 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15182 LAMBDA_EXPR_CLOSURE (r) = type;
15183 CLASSTYPE_LAMBDA_EXPR (type) = r;
15184
15185 LAMBDA_EXPR_LOCATION (r)
15186 = LAMBDA_EXPR_LOCATION (t);
15187 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15188 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15189 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15190 LAMBDA_EXPR_DISCRIMINATOR (r)
15191 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15192 /* For a function scope, we want to use tsubst so that we don't
15193 complain about referring to an auto function before its return
15194 type has been deduced. Otherwise, we want to use tsubst_copy so
15195 that we look up the existing field/parameter/variable rather
15196 than build a new one. */
15197 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15198 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15199 scope = tsubst (scope, args, complain, in_decl);
15200 else if (scope && TREE_CODE (scope) == PARM_DECL)
15201 {
15202 /* Look up the parameter we want directly, as tsubst_copy
15203 doesn't do what we need. */
15204 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15205 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15206 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15207 parm = DECL_CHAIN (parm);
15208 scope = parm;
15209 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15210 if (DECL_CONTEXT (scope) == NULL_TREE)
15211 DECL_CONTEXT (scope) = fn;
15212 }
15213 else
15214 scope = RECUR (scope);
15215 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15216 LAMBDA_EXPR_RETURN_TYPE (r)
15217 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15218
15219 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15220 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15221
15222 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15223 determine_visibility (TYPE_NAME (type));
15224 /* Now that we know visibility, instantiate the type so we have a
15225 declaration of the op() for later calls to lambda_function. */
15226 complete_type (type);
15227
15228 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15229
15230 RETURN (build_lambda_object (r));
15231 }
15232
15233 case TARGET_EXPR:
15234 /* We can get here for a constant initializer of non-dependent type.
15235 FIXME stop folding in cp_parser_initializer_clause. */
15236 {
15237 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15238 complain);
15239 RETURN (r);
15240 }
15241
15242 case TRANSACTION_EXPR:
15243 RETURN (tsubst_expr(t, args, complain, in_decl,
15244 integral_constant_expression_p));
15245
15246 case PAREN_EXPR:
15247 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15248
15249 case VEC_PERM_EXPR:
15250 RETURN (build_x_vec_perm_expr (input_location,
15251 RECUR (TREE_OPERAND (t, 0)),
15252 RECUR (TREE_OPERAND (t, 1)),
15253 RECUR (TREE_OPERAND (t, 2)),
15254 complain));
15255
15256 default:
15257 /* Handle Objective-C++ constructs, if appropriate. */
15258 {
15259 tree subst
15260 = objcp_tsubst_copy_and_build (t, args, complain,
15261 in_decl, /*function_p=*/false);
15262 if (subst)
15263 RETURN (subst);
15264 }
15265 RETURN (tsubst_copy (t, args, complain, in_decl));
15266 }
15267
15268 #undef RECUR
15269 #undef RETURN
15270 out:
15271 input_location = loc;
15272 return retval;
15273 }
15274
15275 /* Verify that the instantiated ARGS are valid. For type arguments,
15276 make sure that the type's linkage is ok. For non-type arguments,
15277 make sure they are constants if they are integral or enumerations.
15278 Emit an error under control of COMPLAIN, and return TRUE on error. */
15279
15280 static bool
15281 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15282 {
15283 if (dependent_template_arg_p (t))
15284 return false;
15285 if (ARGUMENT_PACK_P (t))
15286 {
15287 tree vec = ARGUMENT_PACK_ARGS (t);
15288 int len = TREE_VEC_LENGTH (vec);
15289 bool result = false;
15290 int i;
15291
15292 for (i = 0; i < len; ++i)
15293 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15294 result = true;
15295 return result;
15296 }
15297 else if (TYPE_P (t))
15298 {
15299 /* [basic.link]: A name with no linkage (notably, the name
15300 of a class or enumeration declared in a local scope)
15301 shall not be used to declare an entity with linkage.
15302 This implies that names with no linkage cannot be used as
15303 template arguments
15304
15305 DR 757 relaxes this restriction for C++0x. */
15306 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15307 : no_linkage_check (t, /*relaxed_p=*/false));
15308
15309 if (nt)
15310 {
15311 /* DR 488 makes use of a type with no linkage cause
15312 type deduction to fail. */
15313 if (complain & tf_error)
15314 {
15315 if (TYPE_ANONYMOUS_P (nt))
15316 error ("%qT is/uses anonymous type", t);
15317 else
15318 error ("template argument for %qD uses local type %qT",
15319 tmpl, t);
15320 }
15321 return true;
15322 }
15323 /* In order to avoid all sorts of complications, we do not
15324 allow variably-modified types as template arguments. */
15325 else if (variably_modified_type_p (t, NULL_TREE))
15326 {
15327 if (complain & tf_error)
15328 error ("%qT is a variably modified type", t);
15329 return true;
15330 }
15331 }
15332 /* Class template and alias template arguments should be OK. */
15333 else if (DECL_TYPE_TEMPLATE_P (t))
15334 ;
15335 /* A non-type argument of integral or enumerated type must be a
15336 constant. */
15337 else if (TREE_TYPE (t)
15338 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15339 && !TREE_CONSTANT (t))
15340 {
15341 if (complain & tf_error)
15342 error ("integral expression %qE is not constant", t);
15343 return true;
15344 }
15345 return false;
15346 }
15347
15348 static bool
15349 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15350 {
15351 int ix, len = DECL_NTPARMS (tmpl);
15352 bool result = false;
15353
15354 for (ix = 0; ix != len; ix++)
15355 {
15356 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15357 result = true;
15358 }
15359 if (result && (complain & tf_error))
15360 error (" trying to instantiate %qD", tmpl);
15361 return result;
15362 }
15363
15364 /* We're out of SFINAE context now, so generate diagnostics for the access
15365 errors we saw earlier when instantiating D from TMPL and ARGS. */
15366
15367 static void
15368 recheck_decl_substitution (tree d, tree tmpl, tree args)
15369 {
15370 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15371 tree type = TREE_TYPE (pattern);
15372 location_t loc = input_location;
15373
15374 push_access_scope (d);
15375 push_deferring_access_checks (dk_no_deferred);
15376 input_location = DECL_SOURCE_LOCATION (pattern);
15377 tsubst (type, args, tf_warning_or_error, d);
15378 input_location = loc;
15379 pop_deferring_access_checks ();
15380 pop_access_scope (d);
15381 }
15382
15383 /* Instantiate the indicated variable, function, or alias template TMPL with
15384 the template arguments in TARG_PTR. */
15385
15386 static tree
15387 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15388 {
15389 tree targ_ptr = orig_args;
15390 tree fndecl;
15391 tree gen_tmpl;
15392 tree spec;
15393 bool access_ok = true;
15394
15395 if (tmpl == error_mark_node)
15396 return error_mark_node;
15397
15398 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15399
15400 /* If this function is a clone, handle it specially. */
15401 if (DECL_CLONED_FUNCTION_P (tmpl))
15402 {
15403 tree spec;
15404 tree clone;
15405
15406 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15407 DECL_CLONED_FUNCTION. */
15408 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15409 targ_ptr, complain);
15410 if (spec == error_mark_node)
15411 return error_mark_node;
15412
15413 /* Look for the clone. */
15414 FOR_EACH_CLONE (clone, spec)
15415 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15416 return clone;
15417 /* We should always have found the clone by now. */
15418 gcc_unreachable ();
15419 return NULL_TREE;
15420 }
15421
15422 if (targ_ptr == error_mark_node)
15423 return error_mark_node;
15424
15425 /* Check to see if we already have this specialization. */
15426 gen_tmpl = most_general_template (tmpl);
15427 if (tmpl != gen_tmpl)
15428 /* The TMPL is a partial instantiation. To get a full set of
15429 arguments we must add the arguments used to perform the
15430 partial instantiation. */
15431 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15432 targ_ptr);
15433
15434 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15435 but it doesn't seem to be on the hot path. */
15436 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15437
15438 gcc_assert (tmpl == gen_tmpl
15439 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15440 == spec)
15441 || fndecl == NULL_TREE);
15442
15443 if (spec != NULL_TREE)
15444 {
15445 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15446 {
15447 if (complain & tf_error)
15448 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15449 return error_mark_node;
15450 }
15451 return spec;
15452 }
15453
15454 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15455 complain))
15456 return error_mark_node;
15457
15458 /* We are building a FUNCTION_DECL, during which the access of its
15459 parameters and return types have to be checked. However this
15460 FUNCTION_DECL which is the desired context for access checking
15461 is not built yet. We solve this chicken-and-egg problem by
15462 deferring all checks until we have the FUNCTION_DECL. */
15463 push_deferring_access_checks (dk_deferred);
15464
15465 /* Instantiation of the function happens in the context of the function
15466 template, not the context of the overload resolution we're doing. */
15467 push_to_top_level ();
15468 /* If there are dependent arguments, e.g. because we're doing partial
15469 ordering, make sure processing_template_decl stays set. */
15470 if (uses_template_parms (targ_ptr))
15471 ++processing_template_decl;
15472 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15473 {
15474 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15475 complain, gen_tmpl);
15476 push_nested_class (ctx);
15477 }
15478 /* Substitute template parameters to obtain the specialization. */
15479 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15480 targ_ptr, complain, gen_tmpl);
15481 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15482 pop_nested_class ();
15483 pop_from_top_level ();
15484
15485 if (fndecl == error_mark_node)
15486 {
15487 pop_deferring_access_checks ();
15488 return error_mark_node;
15489 }
15490
15491 /* The DECL_TI_TEMPLATE should always be the immediate parent
15492 template, not the most general template. */
15493 DECL_TI_TEMPLATE (fndecl) = tmpl;
15494
15495 /* Now we know the specialization, compute access previously
15496 deferred. */
15497 push_access_scope (fndecl);
15498 if (!perform_deferred_access_checks (complain))
15499 access_ok = false;
15500 pop_access_scope (fndecl);
15501 pop_deferring_access_checks ();
15502
15503 /* If we've just instantiated the main entry point for a function,
15504 instantiate all the alternate entry points as well. We do this
15505 by cloning the instantiation of the main entry point, not by
15506 instantiating the template clones. */
15507 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15508 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15509
15510 if (!access_ok)
15511 {
15512 if (!(complain & tf_error))
15513 {
15514 /* Remember to reinstantiate when we're out of SFINAE so the user
15515 can see the errors. */
15516 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15517 }
15518 return error_mark_node;
15519 }
15520 return fndecl;
15521 }
15522
15523 /* Wrapper for instantiate_template_1. */
15524
15525 tree
15526 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15527 {
15528 tree ret;
15529 timevar_push (TV_TEMPLATE_INST);
15530 ret = instantiate_template_1 (tmpl, orig_args, complain);
15531 timevar_pop (TV_TEMPLATE_INST);
15532 return ret;
15533 }
15534
15535 /* Instantiate the alias template TMPL with ARGS. Also push a template
15536 instantiation level, which instantiate_template doesn't do because
15537 functions and variables have sufficient context established by the
15538 callers. */
15539
15540 static tree
15541 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15542 {
15543 struct pending_template *old_last_pend = last_pending_template;
15544 struct tinst_level *old_error_tinst = last_error_tinst_level;
15545 if (tmpl == error_mark_node || args == error_mark_node)
15546 return error_mark_node;
15547 tree tinst = build_tree_list (tmpl, args);
15548 if (!push_tinst_level (tinst))
15549 {
15550 ggc_free (tinst);
15551 return error_mark_node;
15552 }
15553
15554 args =
15555 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15556 args, tmpl, complain,
15557 /*require_all_args=*/true,
15558 /*use_default_args=*/true);
15559
15560 tree r = instantiate_template (tmpl, args, complain);
15561 pop_tinst_level ();
15562 /* We can't free this if a pending_template entry or last_error_tinst_level
15563 is pointing at it. */
15564 if (last_pending_template == old_last_pend
15565 && last_error_tinst_level == old_error_tinst)
15566 ggc_free (tinst);
15567
15568 return r;
15569 }
15570
15571 /* PARM is a template parameter pack for FN. Returns true iff
15572 PARM is used in a deducible way in the argument list of FN. */
15573
15574 static bool
15575 pack_deducible_p (tree parm, tree fn)
15576 {
15577 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15578 for (; t; t = TREE_CHAIN (t))
15579 {
15580 tree type = TREE_VALUE (t);
15581 tree packs;
15582 if (!PACK_EXPANSION_P (type))
15583 continue;
15584 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15585 packs; packs = TREE_CHAIN (packs))
15586 if (TREE_VALUE (packs) == parm)
15587 {
15588 /* The template parameter pack is used in a function parameter
15589 pack. If this is the end of the parameter list, the
15590 template parameter pack is deducible. */
15591 if (TREE_CHAIN (t) == void_list_node)
15592 return true;
15593 else
15594 /* Otherwise, not. Well, it could be deduced from
15595 a non-pack parameter, but doing so would end up with
15596 a deduction mismatch, so don't bother. */
15597 return false;
15598 }
15599 }
15600 /* The template parameter pack isn't used in any function parameter
15601 packs, but it might be used deeper, e.g. tuple<Args...>. */
15602 return true;
15603 }
15604
15605 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15606 NARGS elements of the arguments that are being used when calling
15607 it. TARGS is a vector into which the deduced template arguments
15608 are placed.
15609
15610 Returns either a FUNCTION_DECL for the matching specialization of FN or
15611 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
15612 true, diagnostics will be printed to explain why it failed.
15613
15614 If FN is a conversion operator, or we are trying to produce a specific
15615 specialization, RETURN_TYPE is the return type desired.
15616
15617 The EXPLICIT_TARGS are explicit template arguments provided via a
15618 template-id.
15619
15620 The parameter STRICT is one of:
15621
15622 DEDUCE_CALL:
15623 We are deducing arguments for a function call, as in
15624 [temp.deduct.call].
15625
15626 DEDUCE_CONV:
15627 We are deducing arguments for a conversion function, as in
15628 [temp.deduct.conv].
15629
15630 DEDUCE_EXACT:
15631 We are deducing arguments when doing an explicit instantiation
15632 as in [temp.explicit], when determining an explicit specialization
15633 as in [temp.expl.spec], or when taking the address of a function
15634 template, as in [temp.deduct.funcaddr]. */
15635
15636 tree
15637 fn_type_unification (tree fn,
15638 tree explicit_targs,
15639 tree targs,
15640 const tree *args,
15641 unsigned int nargs,
15642 tree return_type,
15643 unification_kind_t strict,
15644 int flags,
15645 bool explain_p,
15646 bool decltype_p)
15647 {
15648 tree parms;
15649 tree fntype;
15650 tree decl = NULL_TREE;
15651 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15652 bool ok;
15653 static int deduction_depth;
15654 struct pending_template *old_last_pend = last_pending_template;
15655 struct tinst_level *old_error_tinst = last_error_tinst_level;
15656 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15657 tree tinst;
15658 tree r = error_mark_node;
15659
15660 if (decltype_p)
15661 complain |= tf_decltype;
15662
15663 /* In C++0x, it's possible to have a function template whose type depends
15664 on itself recursively. This is most obvious with decltype, but can also
15665 occur with enumeration scope (c++/48969). So we need to catch infinite
15666 recursion and reject the substitution at deduction time; this function
15667 will return error_mark_node for any repeated substitution.
15668
15669 This also catches excessive recursion such as when f<N> depends on
15670 f<N-1> across all integers, and returns error_mark_node for all the
15671 substitutions back up to the initial one.
15672
15673 This is, of course, not reentrant. */
15674 if (excessive_deduction_depth)
15675 return error_mark_node;
15676 tinst = build_tree_list (fn, NULL_TREE);
15677 ++deduction_depth;
15678
15679 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15680
15681 fntype = TREE_TYPE (fn);
15682 if (explicit_targs)
15683 {
15684 /* [temp.deduct]
15685
15686 The specified template arguments must match the template
15687 parameters in kind (i.e., type, nontype, template), and there
15688 must not be more arguments than there are parameters;
15689 otherwise type deduction fails.
15690
15691 Nontype arguments must match the types of the corresponding
15692 nontype template parameters, or must be convertible to the
15693 types of the corresponding nontype parameters as specified in
15694 _temp.arg.nontype_, otherwise type deduction fails.
15695
15696 All references in the function type of the function template
15697 to the corresponding template parameters are replaced by the
15698 specified template argument values. If a substitution in a
15699 template parameter or in the function type of the function
15700 template results in an invalid type, type deduction fails. */
15701 int i, len = TREE_VEC_LENGTH (tparms);
15702 location_t loc = input_location;
15703 bool incomplete = false;
15704
15705 /* Adjust any explicit template arguments before entering the
15706 substitution context. */
15707 explicit_targs
15708 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15709 complain,
15710 /*require_all_args=*/false,
15711 /*use_default_args=*/false));
15712 if (explicit_targs == error_mark_node)
15713 goto fail;
15714
15715 /* Substitute the explicit args into the function type. This is
15716 necessary so that, for instance, explicitly declared function
15717 arguments can match null pointed constants. If we were given
15718 an incomplete set of explicit args, we must not do semantic
15719 processing during substitution as we could create partial
15720 instantiations. */
15721 for (i = 0; i < len; i++)
15722 {
15723 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15724 bool parameter_pack = false;
15725 tree targ = TREE_VEC_ELT (explicit_targs, i);
15726
15727 /* Dig out the actual parm. */
15728 if (TREE_CODE (parm) == TYPE_DECL
15729 || TREE_CODE (parm) == TEMPLATE_DECL)
15730 {
15731 parm = TREE_TYPE (parm);
15732 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15733 }
15734 else if (TREE_CODE (parm) == PARM_DECL)
15735 {
15736 parm = DECL_INITIAL (parm);
15737 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15738 }
15739
15740 if (!parameter_pack && targ == NULL_TREE)
15741 /* No explicit argument for this template parameter. */
15742 incomplete = true;
15743
15744 if (parameter_pack && pack_deducible_p (parm, fn))
15745 {
15746 /* Mark the argument pack as "incomplete". We could
15747 still deduce more arguments during unification.
15748 We remove this mark in type_unification_real. */
15749 if (targ)
15750 {
15751 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15752 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15753 = ARGUMENT_PACK_ARGS (targ);
15754 }
15755
15756 /* We have some incomplete argument packs. */
15757 incomplete = true;
15758 }
15759 }
15760
15761 TREE_VALUE (tinst) = explicit_targs;
15762 if (!push_tinst_level (tinst))
15763 {
15764 excessive_deduction_depth = true;
15765 goto fail;
15766 }
15767 processing_template_decl += incomplete;
15768 input_location = DECL_SOURCE_LOCATION (fn);
15769 /* Ignore any access checks; we'll see them again in
15770 instantiate_template and they might have the wrong
15771 access path at this point. */
15772 push_deferring_access_checks (dk_deferred);
15773 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15774 complain | tf_partial, NULL_TREE);
15775 pop_deferring_access_checks ();
15776 input_location = loc;
15777 processing_template_decl -= incomplete;
15778 pop_tinst_level ();
15779
15780 if (fntype == error_mark_node)
15781 goto fail;
15782
15783 /* Place the explicitly specified arguments in TARGS. */
15784 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15785 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15786 }
15787
15788 /* Never do unification on the 'this' parameter. */
15789 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15790
15791 if (return_type)
15792 {
15793 tree *new_args;
15794
15795 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15796 new_args = XALLOCAVEC (tree, nargs + 1);
15797 new_args[0] = return_type;
15798 memcpy (new_args + 1, args, nargs * sizeof (tree));
15799 args = new_args;
15800 ++nargs;
15801 }
15802
15803 /* We allow incomplete unification without an error message here
15804 because the standard doesn't seem to explicitly prohibit it. Our
15805 callers must be ready to deal with unification failures in any
15806 event. */
15807
15808 TREE_VALUE (tinst) = targs;
15809 /* If we aren't explaining yet, push tinst context so we can see where
15810 any errors (e.g. from class instantiations triggered by instantiation
15811 of default template arguments) come from. If we are explaining, this
15812 context is redundant. */
15813 if (!explain_p && !push_tinst_level (tinst))
15814 {
15815 excessive_deduction_depth = true;
15816 goto fail;
15817 }
15818
15819 /* type_unification_real will pass back any access checks from default
15820 template argument substitution. */
15821 vec<deferred_access_check, va_gc> *checks;
15822 checks = NULL;
15823
15824 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15825 targs, parms, args, nargs, /*subr=*/0,
15826 strict, flags, &checks, explain_p);
15827 if (!explain_p)
15828 pop_tinst_level ();
15829 if (!ok)
15830 goto fail;
15831
15832 /* Now that we have bindings for all of the template arguments,
15833 ensure that the arguments deduced for the template template
15834 parameters have compatible template parameter lists. We cannot
15835 check this property before we have deduced all template
15836 arguments, because the template parameter types of a template
15837 template parameter might depend on prior template parameters
15838 deduced after the template template parameter. The following
15839 ill-formed example illustrates this issue:
15840
15841 template<typename T, template<T> class C> void f(C<5>, T);
15842
15843 template<int N> struct X {};
15844
15845 void g() {
15846 f(X<5>(), 5l); // error: template argument deduction fails
15847 }
15848
15849 The template parameter list of 'C' depends on the template type
15850 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15851 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15852 time that we deduce 'C'. */
15853 if (!template_template_parm_bindings_ok_p
15854 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15855 {
15856 unify_inconsistent_template_template_parameters (explain_p);
15857 goto fail;
15858 }
15859
15860 /* All is well so far. Now, check:
15861
15862 [temp.deduct]
15863
15864 When all template arguments have been deduced, all uses of
15865 template parameters in nondeduced contexts are replaced with
15866 the corresponding deduced argument values. If the
15867 substitution results in an invalid type, as described above,
15868 type deduction fails. */
15869 TREE_VALUE (tinst) = targs;
15870 if (!push_tinst_level (tinst))
15871 {
15872 excessive_deduction_depth = true;
15873 goto fail;
15874 }
15875
15876 /* Also collect access checks from the instantiation. */
15877 reopen_deferring_access_checks (checks);
15878
15879 decl = instantiate_template (fn, targs, complain);
15880
15881 checks = get_deferred_access_checks ();
15882 pop_deferring_access_checks ();
15883
15884 pop_tinst_level ();
15885
15886 if (decl == error_mark_node)
15887 goto fail;
15888
15889 /* Now perform any access checks encountered during substitution. */
15890 push_access_scope (decl);
15891 ok = perform_access_checks (checks, complain);
15892 pop_access_scope (decl);
15893 if (!ok)
15894 goto fail;
15895
15896 /* If we're looking for an exact match, check that what we got
15897 is indeed an exact match. It might not be if some template
15898 parameters are used in non-deduced contexts. But don't check
15899 for an exact match if we have dependent template arguments;
15900 in that case we're doing partial ordering, and we already know
15901 that we have two candidates that will provide the actual type. */
15902 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
15903 {
15904 tree substed = TREE_TYPE (decl);
15905 unsigned int i;
15906
15907 tree sarg
15908 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15909 if (return_type)
15910 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15911 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15912 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15913 {
15914 unify_type_mismatch (explain_p, args[i],
15915 TREE_VALUE (sarg));
15916 goto fail;
15917 }
15918 }
15919
15920 r = decl;
15921
15922 fail:
15923 --deduction_depth;
15924 if (excessive_deduction_depth)
15925 {
15926 if (deduction_depth == 0)
15927 /* Reset once we're all the way out. */
15928 excessive_deduction_depth = false;
15929 }
15930
15931 /* We can't free this if a pending_template entry or last_error_tinst_level
15932 is pointing at it. */
15933 if (last_pending_template == old_last_pend
15934 && last_error_tinst_level == old_error_tinst)
15935 ggc_free (tinst);
15936
15937 return r;
15938 }
15939
15940 /* Adjust types before performing type deduction, as described in
15941 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15942 sections are symmetric. PARM is the type of a function parameter
15943 or the return type of the conversion function. ARG is the type of
15944 the argument passed to the call, or the type of the value
15945 initialized with the result of the conversion function.
15946 ARG_EXPR is the original argument expression, which may be null. */
15947
15948 static int
15949 maybe_adjust_types_for_deduction (unification_kind_t strict,
15950 tree* parm,
15951 tree* arg,
15952 tree arg_expr)
15953 {
15954 int result = 0;
15955
15956 switch (strict)
15957 {
15958 case DEDUCE_CALL:
15959 break;
15960
15961 case DEDUCE_CONV:
15962 {
15963 /* Swap PARM and ARG throughout the remainder of this
15964 function; the handling is precisely symmetric since PARM
15965 will initialize ARG rather than vice versa. */
15966 tree* temp = parm;
15967 parm = arg;
15968 arg = temp;
15969 break;
15970 }
15971
15972 case DEDUCE_EXACT:
15973 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15974 too, but here handle it by stripping the reference from PARM
15975 rather than by adding it to ARG. */
15976 if (TREE_CODE (*parm) == REFERENCE_TYPE
15977 && TYPE_REF_IS_RVALUE (*parm)
15978 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15979 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15980 && TREE_CODE (*arg) == REFERENCE_TYPE
15981 && !TYPE_REF_IS_RVALUE (*arg))
15982 *parm = TREE_TYPE (*parm);
15983 /* Nothing else to do in this case. */
15984 return 0;
15985
15986 default:
15987 gcc_unreachable ();
15988 }
15989
15990 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15991 {
15992 /* [temp.deduct.call]
15993
15994 If P is not a reference type:
15995
15996 --If A is an array type, the pointer type produced by the
15997 array-to-pointer standard conversion (_conv.array_) is
15998 used in place of A for type deduction; otherwise,
15999
16000 --If A is a function type, the pointer type produced by
16001 the function-to-pointer standard conversion
16002 (_conv.func_) is used in place of A for type deduction;
16003 otherwise,
16004
16005 --If A is a cv-qualified type, the top level
16006 cv-qualifiers of A's type are ignored for type
16007 deduction. */
16008 if (TREE_CODE (*arg) == ARRAY_TYPE)
16009 *arg = build_pointer_type (TREE_TYPE (*arg));
16010 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
16011 *arg = build_pointer_type (*arg);
16012 else
16013 *arg = TYPE_MAIN_VARIANT (*arg);
16014 }
16015
16016 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16017 of the form T&&, where T is a template parameter, and the argument
16018 is an lvalue, T is deduced as A& */
16019 if (TREE_CODE (*parm) == REFERENCE_TYPE
16020 && TYPE_REF_IS_RVALUE (*parm)
16021 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16022 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16023 && (arg_expr ? real_lvalue_p (arg_expr)
16024 /* try_one_overload doesn't provide an arg_expr, but
16025 functions are always lvalues. */
16026 : TREE_CODE (*arg) == FUNCTION_TYPE))
16027 *arg = build_reference_type (*arg);
16028
16029 /* [temp.deduct.call]
16030
16031 If P is a cv-qualified type, the top level cv-qualifiers
16032 of P's type are ignored for type deduction. If P is a
16033 reference type, the type referred to by P is used for
16034 type deduction. */
16035 *parm = TYPE_MAIN_VARIANT (*parm);
16036 if (TREE_CODE (*parm) == REFERENCE_TYPE)
16037 {
16038 *parm = TREE_TYPE (*parm);
16039 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16040 }
16041
16042 /* DR 322. For conversion deduction, remove a reference type on parm
16043 too (which has been swapped into ARG). */
16044 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
16045 *arg = TREE_TYPE (*arg);
16046
16047 return result;
16048 }
16049
16050 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16051 template which does contain any deducible template parameters; check if
16052 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16053 unify_one_argument. */
16054
16055 static int
16056 check_non_deducible_conversion (tree parm, tree arg, int strict,
16057 int flags, bool explain_p)
16058 {
16059 tree type;
16060
16061 if (!TYPE_P (arg))
16062 type = TREE_TYPE (arg);
16063 else
16064 type = arg;
16065
16066 if (same_type_p (parm, type))
16067 return unify_success (explain_p);
16068
16069 if (strict == DEDUCE_CONV)
16070 {
16071 if (can_convert_arg (type, parm, NULL_TREE, flags,
16072 explain_p ? tf_warning_or_error : tf_none))
16073 return unify_success (explain_p);
16074 }
16075 else if (strict != DEDUCE_EXACT)
16076 {
16077 if (can_convert_arg (parm, type,
16078 TYPE_P (arg) ? NULL_TREE : arg,
16079 flags, explain_p ? tf_warning_or_error : tf_none))
16080 return unify_success (explain_p);
16081 }
16082
16083 if (strict == DEDUCE_EXACT)
16084 return unify_type_mismatch (explain_p, parm, arg);
16085 else
16086 return unify_arg_conversion (explain_p, parm, type, arg);
16087 }
16088
16089 static bool uses_deducible_template_parms (tree type);
16090
16091 /* Returns true iff the expression EXPR is one from which a template
16092 argument can be deduced. In other words, if it's an undecorated
16093 use of a template non-type parameter. */
16094
16095 static bool
16096 deducible_expression (tree expr)
16097 {
16098 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
16099 }
16100
16101 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16102 deducible way; that is, if it has a max value of <PARM> - 1. */
16103
16104 static bool
16105 deducible_array_bound (tree domain)
16106 {
16107 if (domain == NULL_TREE)
16108 return false;
16109
16110 tree max = TYPE_MAX_VALUE (domain);
16111 if (TREE_CODE (max) != MINUS_EXPR)
16112 return false;
16113
16114 return deducible_expression (TREE_OPERAND (max, 0));
16115 }
16116
16117 /* Returns true iff the template arguments ARGS use a template parameter
16118 in a deducible way. */
16119
16120 static bool
16121 deducible_template_args (tree args)
16122 {
16123 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
16124 {
16125 bool deducible;
16126 tree elt = TREE_VEC_ELT (args, i);
16127 if (ARGUMENT_PACK_P (elt))
16128 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
16129 else
16130 {
16131 if (PACK_EXPANSION_P (elt))
16132 elt = PACK_EXPANSION_PATTERN (elt);
16133 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
16134 deducible = true;
16135 else if (TYPE_P (elt))
16136 deducible = uses_deducible_template_parms (elt);
16137 else
16138 deducible = deducible_expression (elt);
16139 }
16140 if (deducible)
16141 return true;
16142 }
16143 return false;
16144 }
16145
16146 /* Returns true iff TYPE contains any deducible references to template
16147 parameters, as per 14.8.2.5. */
16148
16149 static bool
16150 uses_deducible_template_parms (tree type)
16151 {
16152 if (PACK_EXPANSION_P (type))
16153 type = PACK_EXPANSION_PATTERN (type);
16154
16155 /* T
16156 cv-list T
16157 TT<T>
16158 TT<i>
16159 TT<> */
16160 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16161 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16162 return true;
16163
16164 /* T*
16165 T&
16166 T&& */
16167 if (POINTER_TYPE_P (type))
16168 return uses_deducible_template_parms (TREE_TYPE (type));
16169
16170 /* T[integer-constant ]
16171 type [i] */
16172 if (TREE_CODE (type) == ARRAY_TYPE)
16173 return (uses_deducible_template_parms (TREE_TYPE (type))
16174 || deducible_array_bound (TYPE_DOMAIN (type)));
16175
16176 /* T type ::*
16177 type T::*
16178 T T::*
16179 T (type ::*)()
16180 type (T::*)()
16181 type (type ::*)(T)
16182 type (T::*)(T)
16183 T (type ::*)(T)
16184 T (T::*)()
16185 T (T::*)(T) */
16186 if (TYPE_PTRMEM_P (type))
16187 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16188 || (uses_deducible_template_parms
16189 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16190
16191 /* template-name <T> (where template-name refers to a class template)
16192 template-name <i> (where template-name refers to a class template) */
16193 if (CLASS_TYPE_P (type)
16194 && CLASSTYPE_TEMPLATE_INFO (type)
16195 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16196 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16197 (CLASSTYPE_TI_ARGS (type)));
16198
16199 /* type (T)
16200 T()
16201 T(T) */
16202 if (TREE_CODE (type) == FUNCTION_TYPE
16203 || TREE_CODE (type) == METHOD_TYPE)
16204 {
16205 if (uses_deducible_template_parms (TREE_TYPE (type)))
16206 return true;
16207 tree parm = TYPE_ARG_TYPES (type);
16208 if (TREE_CODE (type) == METHOD_TYPE)
16209 parm = TREE_CHAIN (parm);
16210 for (; parm; parm = TREE_CHAIN (parm))
16211 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16212 return true;
16213 }
16214
16215 return false;
16216 }
16217
16218 /* Subroutine of type_unification_real and unify_pack_expansion to
16219 handle unification of a single P/A pair. Parameters are as
16220 for those functions. */
16221
16222 static int
16223 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16224 int subr, unification_kind_t strict, int flags,
16225 bool explain_p)
16226 {
16227 tree arg_expr = NULL_TREE;
16228 int arg_strict;
16229
16230 if (arg == error_mark_node || parm == error_mark_node)
16231 return unify_invalid (explain_p);
16232 if (arg == unknown_type_node)
16233 /* We can't deduce anything from this, but we might get all the
16234 template args from other function args. */
16235 return unify_success (explain_p);
16236
16237 /* Implicit conversions (Clause 4) will be performed on a function
16238 argument to convert it to the type of the corresponding function
16239 parameter if the parameter type contains no template-parameters that
16240 participate in template argument deduction. */
16241 if (TYPE_P (parm) && !uses_template_parms (parm))
16242 /* For function parameters that contain no template-parameters at all,
16243 we have historically checked for convertibility in order to shortcut
16244 consideration of this candidate. */
16245 return check_non_deducible_conversion (parm, arg, strict, flags,
16246 explain_p);
16247 else if (strict == DEDUCE_CALL
16248 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16249 /* For function parameters with only non-deducible template parameters,
16250 just return. */
16251 return unify_success (explain_p);
16252
16253 switch (strict)
16254 {
16255 case DEDUCE_CALL:
16256 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16257 | UNIFY_ALLOW_MORE_CV_QUAL
16258 | UNIFY_ALLOW_DERIVED);
16259 break;
16260
16261 case DEDUCE_CONV:
16262 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16263 break;
16264
16265 case DEDUCE_EXACT:
16266 arg_strict = UNIFY_ALLOW_NONE;
16267 break;
16268
16269 default:
16270 gcc_unreachable ();
16271 }
16272
16273 /* We only do these transformations if this is the top-level
16274 parameter_type_list in a call or declaration matching; in other
16275 situations (nested function declarators, template argument lists) we
16276 won't be comparing a type to an expression, and we don't do any type
16277 adjustments. */
16278 if (!subr)
16279 {
16280 if (!TYPE_P (arg))
16281 {
16282 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16283 if (type_unknown_p (arg))
16284 {
16285 /* [temp.deduct.type] A template-argument can be
16286 deduced from a pointer to function or pointer
16287 to member function argument if the set of
16288 overloaded functions does not contain function
16289 templates and at most one of a set of
16290 overloaded functions provides a unique
16291 match. */
16292
16293 if (resolve_overloaded_unification
16294 (tparms, targs, parm, arg, strict,
16295 arg_strict, explain_p))
16296 return unify_success (explain_p);
16297 return unify_overload_resolution_failure (explain_p, arg);
16298 }
16299
16300 arg_expr = arg;
16301 arg = unlowered_expr_type (arg);
16302 if (arg == error_mark_node)
16303 return unify_invalid (explain_p);
16304 }
16305
16306 arg_strict |=
16307 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16308 }
16309 else
16310 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16311 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16312
16313 /* For deduction from an init-list we need the actual list. */
16314 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16315 arg = arg_expr;
16316 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16317 }
16318
16319 /* Most parms like fn_type_unification.
16320
16321 If SUBR is 1, we're being called recursively (to unify the
16322 arguments of a function or method parameter of a function
16323 template).
16324
16325 CHECKS is a pointer to a vector of access checks encountered while
16326 substituting default template arguments. */
16327
16328 static int
16329 type_unification_real (tree tparms,
16330 tree targs,
16331 tree xparms,
16332 const tree *xargs,
16333 unsigned int xnargs,
16334 int subr,
16335 unification_kind_t strict,
16336 int flags,
16337 vec<deferred_access_check, va_gc> **checks,
16338 bool explain_p)
16339 {
16340 tree parm, arg;
16341 int i;
16342 int ntparms = TREE_VEC_LENGTH (tparms);
16343 int saw_undeduced = 0;
16344 tree parms;
16345 const tree *args;
16346 unsigned int nargs;
16347 unsigned int ia;
16348
16349 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16350 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16351 gcc_assert (ntparms > 0);
16352
16353 /* Reset the number of non-defaulted template arguments contained
16354 in TARGS. */
16355 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16356
16357 again:
16358 parms = xparms;
16359 args = xargs;
16360 nargs = xnargs;
16361
16362 ia = 0;
16363 while (parms && parms != void_list_node
16364 && ia < nargs)
16365 {
16366 parm = TREE_VALUE (parms);
16367
16368 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16369 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16370 /* For a function parameter pack that occurs at the end of the
16371 parameter-declaration-list, the type A of each remaining
16372 argument of the call is compared with the type P of the
16373 declarator-id of the function parameter pack. */
16374 break;
16375
16376 parms = TREE_CHAIN (parms);
16377
16378 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16379 /* For a function parameter pack that does not occur at the
16380 end of the parameter-declaration-list, the type of the
16381 parameter pack is a non-deduced context. */
16382 continue;
16383
16384 arg = args[ia];
16385 ++ia;
16386
16387 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16388 flags, explain_p))
16389 return 1;
16390 }
16391
16392 if (parms
16393 && parms != void_list_node
16394 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16395 {
16396 /* Unify the remaining arguments with the pack expansion type. */
16397 tree argvec;
16398 tree parmvec = make_tree_vec (1);
16399
16400 /* Allocate a TREE_VEC and copy in all of the arguments */
16401 argvec = make_tree_vec (nargs - ia);
16402 for (i = 0; ia < nargs; ++ia, ++i)
16403 TREE_VEC_ELT (argvec, i) = args[ia];
16404
16405 /* Copy the parameter into parmvec. */
16406 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16407 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16408 /*subr=*/subr, explain_p))
16409 return 1;
16410
16411 /* Advance to the end of the list of parameters. */
16412 parms = TREE_CHAIN (parms);
16413 }
16414
16415 /* Fail if we've reached the end of the parm list, and more args
16416 are present, and the parm list isn't variadic. */
16417 if (ia < nargs && parms == void_list_node)
16418 return unify_too_many_arguments (explain_p, nargs, ia);
16419 /* Fail if parms are left and they don't have default values. */
16420 if (parms && parms != void_list_node
16421 && TREE_PURPOSE (parms) == NULL_TREE)
16422 {
16423 unsigned int count = nargs;
16424 tree p = parms;
16425 while (p && p != void_list_node)
16426 {
16427 count++;
16428 p = TREE_CHAIN (p);
16429 }
16430 return unify_too_few_arguments (explain_p, ia, count);
16431 }
16432
16433 if (!subr)
16434 {
16435 tsubst_flags_t complain = (explain_p
16436 ? tf_warning_or_error
16437 : tf_none);
16438
16439 for (i = 0; i < ntparms; i++)
16440 {
16441 tree targ = TREE_VEC_ELT (targs, i);
16442 tree tparm = TREE_VEC_ELT (tparms, i);
16443
16444 /* Clear the "incomplete" flags on all argument packs now so that
16445 substituting them into later default arguments works. */
16446 if (targ && ARGUMENT_PACK_P (targ))
16447 {
16448 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16449 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16450 }
16451
16452 if (targ || tparm == error_mark_node)
16453 continue;
16454 tparm = TREE_VALUE (tparm);
16455
16456 /* If this is an undeduced nontype parameter that depends on
16457 a type parameter, try another pass; its type may have been
16458 deduced from a later argument than the one from which
16459 this parameter can be deduced. */
16460 if (TREE_CODE (tparm) == PARM_DECL
16461 && uses_template_parms (TREE_TYPE (tparm))
16462 && !saw_undeduced++)
16463 goto again;
16464
16465 /* Core issue #226 (C++0x) [temp.deduct]:
16466
16467 If a template argument has not been deduced, its
16468 default template argument, if any, is used.
16469
16470 When we are in C++98 mode, TREE_PURPOSE will either
16471 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16472 to explicitly check cxx_dialect here. */
16473 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16474 {
16475 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16476 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16477 reopen_deferring_access_checks (*checks);
16478 location_t save_loc = input_location;
16479 if (DECL_P (parm))
16480 input_location = DECL_SOURCE_LOCATION (parm);
16481 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16482 arg = convert_template_argument (parm, arg, targs, complain,
16483 i, NULL_TREE);
16484 input_location = save_loc;
16485 *checks = get_deferred_access_checks ();
16486 pop_deferring_access_checks ();
16487 if (arg == error_mark_node)
16488 return 1;
16489 else
16490 {
16491 TREE_VEC_ELT (targs, i) = arg;
16492 /* The position of the first default template argument,
16493 is also the number of non-defaulted arguments in TARGS.
16494 Record that. */
16495 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16496 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16497 continue;
16498 }
16499 }
16500
16501 /* If the type parameter is a parameter pack, then it will
16502 be deduced to an empty parameter pack. */
16503 if (template_parameter_pack_p (tparm))
16504 {
16505 tree arg;
16506
16507 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16508 {
16509 arg = make_node (NONTYPE_ARGUMENT_PACK);
16510 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16511 TREE_CONSTANT (arg) = 1;
16512 }
16513 else
16514 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16515
16516 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16517
16518 TREE_VEC_ELT (targs, i) = arg;
16519 continue;
16520 }
16521
16522 return unify_parameter_deduction_failure (explain_p, tparm);
16523 }
16524 }
16525 #ifdef ENABLE_CHECKING
16526 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16527 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16528 #endif
16529
16530 return unify_success (explain_p);
16531 }
16532
16533 /* Subroutine of type_unification_real. Args are like the variables
16534 at the call site. ARG is an overloaded function (or template-id);
16535 we try deducing template args from each of the overloads, and if
16536 only one succeeds, we go with that. Modifies TARGS and returns
16537 true on success. */
16538
16539 static bool
16540 resolve_overloaded_unification (tree tparms,
16541 tree targs,
16542 tree parm,
16543 tree arg,
16544 unification_kind_t strict,
16545 int sub_strict,
16546 bool explain_p)
16547 {
16548 tree tempargs = copy_node (targs);
16549 int good = 0;
16550 tree goodfn = NULL_TREE;
16551 bool addr_p;
16552
16553 if (TREE_CODE (arg) == ADDR_EXPR)
16554 {
16555 arg = TREE_OPERAND (arg, 0);
16556 addr_p = true;
16557 }
16558 else
16559 addr_p = false;
16560
16561 if (TREE_CODE (arg) == COMPONENT_REF)
16562 /* Handle `&x' where `x' is some static or non-static member
16563 function name. */
16564 arg = TREE_OPERAND (arg, 1);
16565
16566 if (TREE_CODE (arg) == OFFSET_REF)
16567 arg = TREE_OPERAND (arg, 1);
16568
16569 /* Strip baselink information. */
16570 if (BASELINK_P (arg))
16571 arg = BASELINK_FUNCTIONS (arg);
16572
16573 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16574 {
16575 /* If we got some explicit template args, we need to plug them into
16576 the affected templates before we try to unify, in case the
16577 explicit args will completely resolve the templates in question. */
16578
16579 int ok = 0;
16580 tree expl_subargs = TREE_OPERAND (arg, 1);
16581 arg = TREE_OPERAND (arg, 0);
16582
16583 for (; arg; arg = OVL_NEXT (arg))
16584 {
16585 tree fn = OVL_CURRENT (arg);
16586 tree subargs, elem;
16587
16588 if (TREE_CODE (fn) != TEMPLATE_DECL)
16589 continue;
16590
16591 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16592 expl_subargs, NULL_TREE, tf_none,
16593 /*require_all_args=*/true,
16594 /*use_default_args=*/true);
16595 if (subargs != error_mark_node
16596 && !any_dependent_template_arguments_p (subargs))
16597 {
16598 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
16599 if (try_one_overload (tparms, targs, tempargs, parm,
16600 elem, strict, sub_strict, addr_p, explain_p)
16601 && (!goodfn || !same_type_p (goodfn, elem)))
16602 {
16603 goodfn = elem;
16604 ++good;
16605 }
16606 }
16607 else if (subargs)
16608 ++ok;
16609 }
16610 /* If no templates (or more than one) are fully resolved by the
16611 explicit arguments, this template-id is a non-deduced context; it
16612 could still be OK if we deduce all template arguments for the
16613 enclosing call through other arguments. */
16614 if (good != 1)
16615 good = ok;
16616 }
16617 else if (TREE_CODE (arg) != OVERLOAD
16618 && TREE_CODE (arg) != FUNCTION_DECL)
16619 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16620 -- but the deduction does not succeed because the expression is
16621 not just the function on its own. */
16622 return false;
16623 else
16624 for (; arg; arg = OVL_NEXT (arg))
16625 if (try_one_overload (tparms, targs, tempargs, parm,
16626 TREE_TYPE (OVL_CURRENT (arg)),
16627 strict, sub_strict, addr_p, explain_p)
16628 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16629 {
16630 goodfn = OVL_CURRENT (arg);
16631 ++good;
16632 }
16633
16634 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16635 to function or pointer to member function argument if the set of
16636 overloaded functions does not contain function templates and at most
16637 one of a set of overloaded functions provides a unique match.
16638
16639 So if we found multiple possibilities, we return success but don't
16640 deduce anything. */
16641
16642 if (good == 1)
16643 {
16644 int i = TREE_VEC_LENGTH (targs);
16645 for (; i--; )
16646 if (TREE_VEC_ELT (tempargs, i))
16647 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16648 }
16649 if (good)
16650 return true;
16651
16652 return false;
16653 }
16654
16655 /* Core DR 115: In contexts where deduction is done and fails, or in
16656 contexts where deduction is not done, if a template argument list is
16657 specified and it, along with any default template arguments, identifies
16658 a single function template specialization, then the template-id is an
16659 lvalue for the function template specialization. */
16660
16661 tree
16662 resolve_nondeduced_context (tree orig_expr)
16663 {
16664 tree expr, offset, baselink;
16665 bool addr;
16666
16667 if (!type_unknown_p (orig_expr))
16668 return orig_expr;
16669
16670 expr = orig_expr;
16671 addr = false;
16672 offset = NULL_TREE;
16673 baselink = NULL_TREE;
16674
16675 if (TREE_CODE (expr) == ADDR_EXPR)
16676 {
16677 expr = TREE_OPERAND (expr, 0);
16678 addr = true;
16679 }
16680 if (TREE_CODE (expr) == OFFSET_REF)
16681 {
16682 offset = expr;
16683 expr = TREE_OPERAND (expr, 1);
16684 }
16685 if (BASELINK_P (expr))
16686 {
16687 baselink = expr;
16688 expr = BASELINK_FUNCTIONS (expr);
16689 }
16690
16691 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16692 {
16693 int good = 0;
16694 tree goodfn = NULL_TREE;
16695
16696 /* If we got some explicit template args, we need to plug them into
16697 the affected templates before we try to unify, in case the
16698 explicit args will completely resolve the templates in question. */
16699
16700 tree expl_subargs = TREE_OPERAND (expr, 1);
16701 tree arg = TREE_OPERAND (expr, 0);
16702 tree badfn = NULL_TREE;
16703 tree badargs = NULL_TREE;
16704
16705 for (; arg; arg = OVL_NEXT (arg))
16706 {
16707 tree fn = OVL_CURRENT (arg);
16708 tree subargs, elem;
16709
16710 if (TREE_CODE (fn) != TEMPLATE_DECL)
16711 continue;
16712
16713 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16714 expl_subargs, NULL_TREE, tf_none,
16715 /*require_all_args=*/true,
16716 /*use_default_args=*/true);
16717 if (subargs != error_mark_node
16718 && !any_dependent_template_arguments_p (subargs))
16719 {
16720 elem = instantiate_template (fn, subargs, tf_none);
16721 if (elem == error_mark_node)
16722 {
16723 badfn = fn;
16724 badargs = subargs;
16725 }
16726 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16727 {
16728 goodfn = elem;
16729 ++good;
16730 }
16731 }
16732 }
16733 if (good == 1)
16734 {
16735 mark_used (goodfn);
16736 expr = goodfn;
16737 if (baselink)
16738 expr = build_baselink (BASELINK_BINFO (baselink),
16739 BASELINK_ACCESS_BINFO (baselink),
16740 expr, BASELINK_OPTYPE (baselink));
16741 if (offset)
16742 {
16743 tree base
16744 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16745 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16746 }
16747 if (addr)
16748 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16749 return expr;
16750 }
16751 else if (good == 0 && badargs)
16752 /* There were no good options and at least one bad one, so let the
16753 user know what the problem is. */
16754 instantiate_template (badfn, badargs, tf_warning_or_error);
16755 }
16756 return orig_expr;
16757 }
16758
16759 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16760 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16761 different overloads deduce different arguments for a given parm.
16762 ADDR_P is true if the expression for which deduction is being
16763 performed was of the form "& fn" rather than simply "fn".
16764
16765 Returns 1 on success. */
16766
16767 static int
16768 try_one_overload (tree tparms,
16769 tree orig_targs,
16770 tree targs,
16771 tree parm,
16772 tree arg,
16773 unification_kind_t strict,
16774 int sub_strict,
16775 bool addr_p,
16776 bool explain_p)
16777 {
16778 int nargs;
16779 tree tempargs;
16780 int i;
16781
16782 if (arg == error_mark_node)
16783 return 0;
16784
16785 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16786 to function or pointer to member function argument if the set of
16787 overloaded functions does not contain function templates and at most
16788 one of a set of overloaded functions provides a unique match.
16789
16790 So if this is a template, just return success. */
16791
16792 if (uses_template_parms (arg))
16793 return 1;
16794
16795 if (TREE_CODE (arg) == METHOD_TYPE)
16796 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16797 else if (addr_p)
16798 arg = build_pointer_type (arg);
16799
16800 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16801
16802 /* We don't copy orig_targs for this because if we have already deduced
16803 some template args from previous args, unify would complain when we
16804 try to deduce a template parameter for the same argument, even though
16805 there isn't really a conflict. */
16806 nargs = TREE_VEC_LENGTH (targs);
16807 tempargs = make_tree_vec (nargs);
16808
16809 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16810 return 0;
16811
16812 /* First make sure we didn't deduce anything that conflicts with
16813 explicitly specified args. */
16814 for (i = nargs; i--; )
16815 {
16816 tree elt = TREE_VEC_ELT (tempargs, i);
16817 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16818
16819 if (!elt)
16820 /*NOP*/;
16821 else if (uses_template_parms (elt))
16822 /* Since we're unifying against ourselves, we will fill in
16823 template args used in the function parm list with our own
16824 template parms. Discard them. */
16825 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16826 else if (oldelt && !template_args_equal (oldelt, elt))
16827 return 0;
16828 }
16829
16830 for (i = nargs; i--; )
16831 {
16832 tree elt = TREE_VEC_ELT (tempargs, i);
16833
16834 if (elt)
16835 TREE_VEC_ELT (targs, i) = elt;
16836 }
16837
16838 return 1;
16839 }
16840
16841 /* PARM is a template class (perhaps with unbound template
16842 parameters). ARG is a fully instantiated type. If ARG can be
16843 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16844 TARGS are as for unify. */
16845
16846 static tree
16847 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16848 bool explain_p)
16849 {
16850 tree copy_of_targs;
16851
16852 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16853 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16854 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16855 return NULL_TREE;
16856
16857 /* We need to make a new template argument vector for the call to
16858 unify. If we used TARGS, we'd clutter it up with the result of
16859 the attempted unification, even if this class didn't work out.
16860 We also don't want to commit ourselves to all the unifications
16861 we've already done, since unification is supposed to be done on
16862 an argument-by-argument basis. In other words, consider the
16863 following pathological case:
16864
16865 template <int I, int J, int K>
16866 struct S {};
16867
16868 template <int I, int J>
16869 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16870
16871 template <int I, int J, int K>
16872 void f(S<I, J, K>, S<I, I, I>);
16873
16874 void g() {
16875 S<0, 0, 0> s0;
16876 S<0, 1, 2> s2;
16877
16878 f(s0, s2);
16879 }
16880
16881 Now, by the time we consider the unification involving `s2', we
16882 already know that we must have `f<0, 0, 0>'. But, even though
16883 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16884 because there are two ways to unify base classes of S<0, 1, 2>
16885 with S<I, I, I>. If we kept the already deduced knowledge, we
16886 would reject the possibility I=1. */
16887 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16888
16889 /* If unification failed, we're done. */
16890 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16891 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16892 return NULL_TREE;
16893
16894 return arg;
16895 }
16896
16897 /* Given a template type PARM and a class type ARG, find the unique
16898 base type in ARG that is an instance of PARM. We do not examine
16899 ARG itself; only its base-classes. If there is not exactly one
16900 appropriate base class, return NULL_TREE. PARM may be the type of
16901 a partial specialization, as well as a plain template type. Used
16902 by unify. */
16903
16904 static enum template_base_result
16905 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16906 bool explain_p, tree *result)
16907 {
16908 tree rval = NULL_TREE;
16909 tree binfo;
16910
16911 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16912
16913 binfo = TYPE_BINFO (complete_type (arg));
16914 if (!binfo)
16915 {
16916 /* The type could not be completed. */
16917 *result = NULL_TREE;
16918 return tbr_incomplete_type;
16919 }
16920
16921 /* Walk in inheritance graph order. The search order is not
16922 important, and this avoids multiple walks of virtual bases. */
16923 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16924 {
16925 tree r = try_class_unification (tparms, targs, parm,
16926 BINFO_TYPE (binfo), explain_p);
16927
16928 if (r)
16929 {
16930 /* If there is more than one satisfactory baseclass, then:
16931
16932 [temp.deduct.call]
16933
16934 If they yield more than one possible deduced A, the type
16935 deduction fails.
16936
16937 applies. */
16938 if (rval && !same_type_p (r, rval))
16939 {
16940 *result = NULL_TREE;
16941 return tbr_ambiguous_baseclass;
16942 }
16943
16944 rval = r;
16945 }
16946 }
16947
16948 *result = rval;
16949 return tbr_success;
16950 }
16951
16952 /* Returns the level of DECL, which declares a template parameter. */
16953
16954 static int
16955 template_decl_level (tree decl)
16956 {
16957 switch (TREE_CODE (decl))
16958 {
16959 case TYPE_DECL:
16960 case TEMPLATE_DECL:
16961 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16962
16963 case PARM_DECL:
16964 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16965
16966 default:
16967 gcc_unreachable ();
16968 }
16969 return 0;
16970 }
16971
16972 /* Decide whether ARG can be unified with PARM, considering only the
16973 cv-qualifiers of each type, given STRICT as documented for unify.
16974 Returns nonzero iff the unification is OK on that basis. */
16975
16976 static int
16977 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16978 {
16979 int arg_quals = cp_type_quals (arg);
16980 int parm_quals = cp_type_quals (parm);
16981
16982 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16983 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16984 {
16985 /* Although a CVR qualifier is ignored when being applied to a
16986 substituted template parameter ([8.3.2]/1 for example), that
16987 does not allow us to unify "const T" with "int&" because both
16988 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16989 It is ok when we're allowing additional CV qualifiers
16990 at the outer level [14.8.2.1]/3,1st bullet. */
16991 if ((TREE_CODE (arg) == REFERENCE_TYPE
16992 || TREE_CODE (arg) == FUNCTION_TYPE
16993 || TREE_CODE (arg) == METHOD_TYPE)
16994 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16995 return 0;
16996
16997 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16998 && (parm_quals & TYPE_QUAL_RESTRICT))
16999 return 0;
17000 }
17001
17002 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17003 && (arg_quals & parm_quals) != parm_quals)
17004 return 0;
17005
17006 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
17007 && (parm_quals & arg_quals) != arg_quals)
17008 return 0;
17009
17010 return 1;
17011 }
17012
17013 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17014 void
17015 template_parm_level_and_index (tree parm, int* level, int* index)
17016 {
17017 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17018 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17019 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17020 {
17021 *index = TEMPLATE_TYPE_IDX (parm);
17022 *level = TEMPLATE_TYPE_LEVEL (parm);
17023 }
17024 else
17025 {
17026 *index = TEMPLATE_PARM_IDX (parm);
17027 *level = TEMPLATE_PARM_LEVEL (parm);
17028 }
17029 }
17030
17031 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17032 do { \
17033 if (unify (TP, TA, P, A, S, EP)) \
17034 return 1; \
17035 } while (0);
17036
17037 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17038 expansion at the end of PACKED_PARMS. Returns 0 if the type
17039 deduction succeeds, 1 otherwise. STRICT is the same as in
17040 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17041 call argument list. We'll need to adjust the arguments to make them
17042 types. SUBR tells us if this is from a recursive call to
17043 type_unification_real, or for comparing two template argument
17044 lists. */
17045
17046 static int
17047 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
17048 tree packed_args, unification_kind_t strict,
17049 bool subr, bool explain_p)
17050 {
17051 tree parm
17052 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
17053 tree pattern = PACK_EXPANSION_PATTERN (parm);
17054 tree pack, packs = NULL_TREE;
17055 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
17056
17057 packed_args = expand_template_argument_pack (packed_args);
17058
17059 int len = TREE_VEC_LENGTH (packed_args);
17060
17061 /* Determine the parameter packs we will be deducing from the
17062 pattern, and record their current deductions. */
17063 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
17064 pack; pack = TREE_CHAIN (pack))
17065 {
17066 tree parm_pack = TREE_VALUE (pack);
17067 int idx, level;
17068
17069 /* Determine the index and level of this parameter pack. */
17070 template_parm_level_and_index (parm_pack, &level, &idx);
17071
17072 /* Keep track of the parameter packs and their corresponding
17073 argument packs. */
17074 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
17075 TREE_TYPE (packs) = make_tree_vec (len - start);
17076 }
17077
17078 /* Loop through all of the arguments that have not yet been
17079 unified and unify each with the pattern. */
17080 for (i = start; i < len; i++)
17081 {
17082 tree parm;
17083 bool any_explicit = false;
17084 tree arg = TREE_VEC_ELT (packed_args, i);
17085
17086 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17087 or the element of its argument pack at the current index if
17088 this argument was explicitly specified. */
17089 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17090 {
17091 int idx, level;
17092 tree arg, pargs;
17093 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17094
17095 arg = NULL_TREE;
17096 if (TREE_VALUE (pack)
17097 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
17098 && (i - start < TREE_VEC_LENGTH (pargs)))
17099 {
17100 any_explicit = true;
17101 arg = TREE_VEC_ELT (pargs, i - start);
17102 }
17103 TMPL_ARG (targs, level, idx) = arg;
17104 }
17105
17106 /* If we had explicit template arguments, substitute them into the
17107 pattern before deduction. */
17108 if (any_explicit)
17109 {
17110 /* Some arguments might still be unspecified or dependent. */
17111 bool dependent;
17112 ++processing_template_decl;
17113 dependent = any_dependent_template_arguments_p (targs);
17114 if (!dependent)
17115 --processing_template_decl;
17116 parm = tsubst (pattern, targs,
17117 explain_p ? tf_warning_or_error : tf_none,
17118 NULL_TREE);
17119 if (dependent)
17120 --processing_template_decl;
17121 if (parm == error_mark_node)
17122 return 1;
17123 }
17124 else
17125 parm = pattern;
17126
17127 /* Unify the pattern with the current argument. */
17128 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17129 LOOKUP_IMPLICIT, explain_p))
17130 return 1;
17131
17132 /* For each parameter pack, collect the deduced value. */
17133 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17134 {
17135 int idx, level;
17136 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17137
17138 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
17139 TMPL_ARG (targs, level, idx);
17140 }
17141 }
17142
17143 /* Verify that the results of unification with the parameter packs
17144 produce results consistent with what we've seen before, and make
17145 the deduced argument packs available. */
17146 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17147 {
17148 tree old_pack = TREE_VALUE (pack);
17149 tree new_args = TREE_TYPE (pack);
17150 int i, len = TREE_VEC_LENGTH (new_args);
17151 int idx, level;
17152 bool nondeduced_p = false;
17153
17154 /* By default keep the original deduced argument pack.
17155 If necessary, more specific code is going to update the
17156 resulting deduced argument later down in this function. */
17157 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17158 TMPL_ARG (targs, level, idx) = old_pack;
17159
17160 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17161 actually deduce anything. */
17162 for (i = 0; i < len && !nondeduced_p; ++i)
17163 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
17164 nondeduced_p = true;
17165 if (nondeduced_p)
17166 continue;
17167
17168 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
17169 {
17170 /* If we had fewer function args than explicit template args,
17171 just use the explicits. */
17172 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17173 int explicit_len = TREE_VEC_LENGTH (explicit_args);
17174 if (len < explicit_len)
17175 new_args = explicit_args;
17176 }
17177
17178 if (!old_pack)
17179 {
17180 tree result;
17181 /* Build the deduced *_ARGUMENT_PACK. */
17182 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17183 {
17184 result = make_node (NONTYPE_ARGUMENT_PACK);
17185 TREE_TYPE (result) =
17186 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17187 TREE_CONSTANT (result) = 1;
17188 }
17189 else
17190 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17191
17192 SET_ARGUMENT_PACK_ARGS (result, new_args);
17193
17194 /* Note the deduced argument packs for this parameter
17195 pack. */
17196 TMPL_ARG (targs, level, idx) = result;
17197 }
17198 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17199 && (ARGUMENT_PACK_ARGS (old_pack)
17200 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17201 {
17202 /* We only had the explicitly-provided arguments before, but
17203 now we have a complete set of arguments. */
17204 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17205
17206 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17207 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17208 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17209 }
17210 else
17211 {
17212 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17213 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17214
17215 if (!comp_template_args_with_info (old_args, new_args,
17216 &bad_old_arg, &bad_new_arg))
17217 /* Inconsistent unification of this parameter pack. */
17218 return unify_parameter_pack_inconsistent (explain_p,
17219 bad_old_arg,
17220 bad_new_arg);
17221 }
17222 }
17223
17224 return unify_success (explain_p);
17225 }
17226
17227 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17228 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17229 parameters and return value are as for unify. */
17230
17231 static int
17232 unify_array_domain (tree tparms, tree targs,
17233 tree parm_dom, tree arg_dom,
17234 bool explain_p)
17235 {
17236 tree parm_max;
17237 tree arg_max;
17238 bool parm_cst;
17239 bool arg_cst;
17240
17241 /* Our representation of array types uses "N - 1" as the
17242 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17243 not an integer constant. We cannot unify arbitrarily
17244 complex expressions, so we eliminate the MINUS_EXPRs
17245 here. */
17246 parm_max = TYPE_MAX_VALUE (parm_dom);
17247 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17248 if (!parm_cst)
17249 {
17250 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17251 parm_max = TREE_OPERAND (parm_max, 0);
17252 }
17253 arg_max = TYPE_MAX_VALUE (arg_dom);
17254 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17255 if (!arg_cst)
17256 {
17257 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17258 trying to unify the type of a variable with the type
17259 of a template parameter. For example:
17260
17261 template <unsigned int N>
17262 void f (char (&) [N]);
17263 int g();
17264 void h(int i) {
17265 char a[g(i)];
17266 f(a);
17267 }
17268
17269 Here, the type of the ARG will be "int [g(i)]", and
17270 may be a SAVE_EXPR, etc. */
17271 if (TREE_CODE (arg_max) != MINUS_EXPR)
17272 return unify_vla_arg (explain_p, arg_dom);
17273 arg_max = TREE_OPERAND (arg_max, 0);
17274 }
17275
17276 /* If only one of the bounds used a MINUS_EXPR, compensate
17277 by adding one to the other bound. */
17278 if (parm_cst && !arg_cst)
17279 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17280 integer_type_node,
17281 parm_max,
17282 integer_one_node);
17283 else if (arg_cst && !parm_cst)
17284 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17285 integer_type_node,
17286 arg_max,
17287 integer_one_node);
17288
17289 return unify (tparms, targs, parm_max, arg_max,
17290 UNIFY_ALLOW_INTEGER, explain_p);
17291 }
17292
17293 /* Deduce the value of template parameters. TPARMS is the (innermost)
17294 set of template parameters to a template. TARGS is the bindings
17295 for those template parameters, as determined thus far; TARGS may
17296 include template arguments for outer levels of template parameters
17297 as well. PARM is a parameter to a template function, or a
17298 subcomponent of that parameter; ARG is the corresponding argument.
17299 This function attempts to match PARM with ARG in a manner
17300 consistent with the existing assignments in TARGS. If more values
17301 are deduced, then TARGS is updated.
17302
17303 Returns 0 if the type deduction succeeds, 1 otherwise. The
17304 parameter STRICT is a bitwise or of the following flags:
17305
17306 UNIFY_ALLOW_NONE:
17307 Require an exact match between PARM and ARG.
17308 UNIFY_ALLOW_MORE_CV_QUAL:
17309 Allow the deduced ARG to be more cv-qualified (by qualification
17310 conversion) than ARG.
17311 UNIFY_ALLOW_LESS_CV_QUAL:
17312 Allow the deduced ARG to be less cv-qualified than ARG.
17313 UNIFY_ALLOW_DERIVED:
17314 Allow the deduced ARG to be a template base class of ARG,
17315 or a pointer to a template base class of the type pointed to by
17316 ARG.
17317 UNIFY_ALLOW_INTEGER:
17318 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17319 case for more information.
17320 UNIFY_ALLOW_OUTER_LEVEL:
17321 This is the outermost level of a deduction. Used to determine validity
17322 of qualification conversions. A valid qualification conversion must
17323 have const qualified pointers leading up to the inner type which
17324 requires additional CV quals, except at the outer level, where const
17325 is not required [conv.qual]. It would be normal to set this flag in
17326 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17327 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17328 This is the outermost level of a deduction, and PARM can be more CV
17329 qualified at this point.
17330 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17331 This is the outermost level of a deduction, and PARM can be less CV
17332 qualified at this point. */
17333
17334 static int
17335 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17336 bool explain_p)
17337 {
17338 int idx;
17339 tree targ;
17340 tree tparm;
17341 int strict_in = strict;
17342
17343 /* I don't think this will do the right thing with respect to types.
17344 But the only case I've seen it in so far has been array bounds, where
17345 signedness is the only information lost, and I think that will be
17346 okay. */
17347 while (TREE_CODE (parm) == NOP_EXPR)
17348 parm = TREE_OPERAND (parm, 0);
17349
17350 if (arg == error_mark_node)
17351 return unify_invalid (explain_p);
17352 if (arg == unknown_type_node
17353 || arg == init_list_type_node)
17354 /* We can't deduce anything from this, but we might get all the
17355 template args from other function args. */
17356 return unify_success (explain_p);
17357
17358 /* If PARM uses template parameters, then we can't bail out here,
17359 even if ARG == PARM, since we won't record unifications for the
17360 template parameters. We might need them if we're trying to
17361 figure out which of two things is more specialized. */
17362 if (arg == parm && !uses_template_parms (parm))
17363 return unify_success (explain_p);
17364
17365 /* Handle init lists early, so the rest of the function can assume
17366 we're dealing with a type. */
17367 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17368 {
17369 tree elt, elttype;
17370 unsigned i;
17371 tree orig_parm = parm;
17372
17373 /* Replace T with std::initializer_list<T> for deduction. */
17374 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17375 && flag_deduce_init_list)
17376 parm = listify (parm);
17377
17378 if (!is_std_init_list (parm)
17379 && TREE_CODE (parm) != ARRAY_TYPE)
17380 /* We can only deduce from an initializer list argument if the
17381 parameter is std::initializer_list or an array; otherwise this
17382 is a non-deduced context. */
17383 return unify_success (explain_p);
17384
17385 if (TREE_CODE (parm) == ARRAY_TYPE)
17386 elttype = TREE_TYPE (parm);
17387 else
17388 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17389
17390 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17391 {
17392 int elt_strict = strict;
17393
17394 if (elt == error_mark_node)
17395 return unify_invalid (explain_p);
17396
17397 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17398 {
17399 tree type = TREE_TYPE (elt);
17400 /* It should only be possible to get here for a call. */
17401 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17402 elt_strict |= maybe_adjust_types_for_deduction
17403 (DEDUCE_CALL, &elttype, &type, elt);
17404 elt = type;
17405 }
17406
17407 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17408 explain_p);
17409 }
17410
17411 if (TREE_CODE (parm) == ARRAY_TYPE
17412 && deducible_array_bound (TYPE_DOMAIN (parm)))
17413 {
17414 /* Also deduce from the length of the initializer list. */
17415 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17416 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17417 if (idx == error_mark_node)
17418 return unify_invalid (explain_p);
17419 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17420 idx, explain_p);
17421 }
17422
17423 /* If the std::initializer_list<T> deduction worked, replace the
17424 deduced A with std::initializer_list<A>. */
17425 if (orig_parm != parm)
17426 {
17427 idx = TEMPLATE_TYPE_IDX (orig_parm);
17428 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17429 targ = listify (targ);
17430 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17431 }
17432 return unify_success (explain_p);
17433 }
17434
17435 /* Immediately reject some pairs that won't unify because of
17436 cv-qualification mismatches. */
17437 if (TREE_CODE (arg) == TREE_CODE (parm)
17438 && TYPE_P (arg)
17439 /* It is the elements of the array which hold the cv quals of an array
17440 type, and the elements might be template type parms. We'll check
17441 when we recurse. */
17442 && TREE_CODE (arg) != ARRAY_TYPE
17443 /* We check the cv-qualifiers when unifying with template type
17444 parameters below. We want to allow ARG `const T' to unify with
17445 PARM `T' for example, when computing which of two templates
17446 is more specialized, for example. */
17447 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17448 && !check_cv_quals_for_unify (strict_in, arg, parm))
17449 return unify_cv_qual_mismatch (explain_p, parm, arg);
17450
17451 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17452 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17453 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17454 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17455 strict &= ~UNIFY_ALLOW_DERIVED;
17456 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17457 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17458
17459 switch (TREE_CODE (parm))
17460 {
17461 case TYPENAME_TYPE:
17462 case SCOPE_REF:
17463 case UNBOUND_CLASS_TEMPLATE:
17464 /* In a type which contains a nested-name-specifier, template
17465 argument values cannot be deduced for template parameters used
17466 within the nested-name-specifier. */
17467 return unify_success (explain_p);
17468
17469 case TEMPLATE_TYPE_PARM:
17470 case TEMPLATE_TEMPLATE_PARM:
17471 case BOUND_TEMPLATE_TEMPLATE_PARM:
17472 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17473 if (tparm == error_mark_node)
17474 return unify_invalid (explain_p);
17475
17476 if (TEMPLATE_TYPE_LEVEL (parm)
17477 != template_decl_level (tparm))
17478 /* The PARM is not one we're trying to unify. Just check
17479 to see if it matches ARG. */
17480 {
17481 if (TREE_CODE (arg) == TREE_CODE (parm)
17482 && (is_auto (parm) ? is_auto (arg)
17483 : same_type_p (parm, arg)))
17484 return unify_success (explain_p);
17485 else
17486 return unify_type_mismatch (explain_p, parm, arg);
17487 }
17488 idx = TEMPLATE_TYPE_IDX (parm);
17489 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17490 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17491 if (tparm == error_mark_node)
17492 return unify_invalid (explain_p);
17493
17494 /* Check for mixed types and values. */
17495 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17496 && TREE_CODE (tparm) != TYPE_DECL)
17497 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17498 && TREE_CODE (tparm) != TEMPLATE_DECL))
17499 gcc_unreachable ();
17500
17501 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17502 {
17503 /* ARG must be constructed from a template class or a template
17504 template parameter. */
17505 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17506 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17507 return unify_template_deduction_failure (explain_p, parm, arg);
17508 {
17509 tree parmvec = TYPE_TI_ARGS (parm);
17510 /* An alias template name is never deduced. */
17511 if (TYPE_ALIAS_P (arg))
17512 arg = strip_typedefs (arg);
17513 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17514 tree full_argvec = add_to_template_args (targs, argvec);
17515 tree parm_parms
17516 = DECL_INNERMOST_TEMPLATE_PARMS
17517 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17518 int i, len;
17519 int parm_variadic_p = 0;
17520
17521 /* The resolution to DR150 makes clear that default
17522 arguments for an N-argument may not be used to bind T
17523 to a template template parameter with fewer than N
17524 parameters. It is not safe to permit the binding of
17525 default arguments as an extension, as that may change
17526 the meaning of a conforming program. Consider:
17527
17528 struct Dense { static const unsigned int dim = 1; };
17529
17530 template <template <typename> class View,
17531 typename Block>
17532 void operator+(float, View<Block> const&);
17533
17534 template <typename Block,
17535 unsigned int Dim = Block::dim>
17536 struct Lvalue_proxy { operator float() const; };
17537
17538 void
17539 test_1d (void) {
17540 Lvalue_proxy<Dense> p;
17541 float b;
17542 b + p;
17543 }
17544
17545 Here, if Lvalue_proxy is permitted to bind to View, then
17546 the global operator+ will be used; if they are not, the
17547 Lvalue_proxy will be converted to float. */
17548 if (coerce_template_parms (parm_parms,
17549 full_argvec,
17550 TYPE_TI_TEMPLATE (parm),
17551 (explain_p
17552 ? tf_warning_or_error
17553 : tf_none),
17554 /*require_all_args=*/true,
17555 /*use_default_args=*/false)
17556 == error_mark_node)
17557 return 1;
17558
17559 /* Deduce arguments T, i from TT<T> or TT<i>.
17560 We check each element of PARMVEC and ARGVEC individually
17561 rather than the whole TREE_VEC since they can have
17562 different number of elements. */
17563
17564 parmvec = expand_template_argument_pack (parmvec);
17565 argvec = expand_template_argument_pack (argvec);
17566
17567 len = TREE_VEC_LENGTH (parmvec);
17568
17569 /* Check if the parameters end in a pack, making them
17570 variadic. */
17571 if (len > 0
17572 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17573 parm_variadic_p = 1;
17574
17575 for (i = 0; i < len - parm_variadic_p; ++i)
17576 /* If the template argument list of P contains a pack
17577 expansion that is not the last template argument, the
17578 entire template argument list is a non-deduced
17579 context. */
17580 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17581 return unify_success (explain_p);
17582
17583 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17584 return unify_too_few_arguments (explain_p,
17585 TREE_VEC_LENGTH (argvec), len);
17586
17587 for (i = 0; i < len - parm_variadic_p; ++i)
17588 {
17589 RECUR_AND_CHECK_FAILURE (tparms, targs,
17590 TREE_VEC_ELT (parmvec, i),
17591 TREE_VEC_ELT (argvec, i),
17592 UNIFY_ALLOW_NONE, explain_p);
17593 }
17594
17595 if (parm_variadic_p
17596 && unify_pack_expansion (tparms, targs,
17597 parmvec, argvec,
17598 DEDUCE_EXACT,
17599 /*subr=*/true, explain_p))
17600 return 1;
17601 }
17602 arg = TYPE_TI_TEMPLATE (arg);
17603
17604 /* Fall through to deduce template name. */
17605 }
17606
17607 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17608 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17609 {
17610 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17611
17612 /* Simple cases: Value already set, does match or doesn't. */
17613 if (targ != NULL_TREE && template_args_equal (targ, arg))
17614 return unify_success (explain_p);
17615 else if (targ)
17616 return unify_inconsistency (explain_p, parm, targ, arg);
17617 }
17618 else
17619 {
17620 /* If PARM is `const T' and ARG is only `int', we don't have
17621 a match unless we are allowing additional qualification.
17622 If ARG is `const int' and PARM is just `T' that's OK;
17623 that binds `const int' to `T'. */
17624 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17625 arg, parm))
17626 return unify_cv_qual_mismatch (explain_p, parm, arg);
17627
17628 /* Consider the case where ARG is `const volatile int' and
17629 PARM is `const T'. Then, T should be `volatile int'. */
17630 arg = cp_build_qualified_type_real
17631 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17632 if (arg == error_mark_node)
17633 return unify_invalid (explain_p);
17634
17635 /* Simple cases: Value already set, does match or doesn't. */
17636 if (targ != NULL_TREE && same_type_p (targ, arg))
17637 return unify_success (explain_p);
17638 else if (targ)
17639 return unify_inconsistency (explain_p, parm, targ, arg);
17640
17641 /* Make sure that ARG is not a variable-sized array. (Note
17642 that were talking about variable-sized arrays (like
17643 `int[n]'), rather than arrays of unknown size (like
17644 `int[]').) We'll get very confused by such a type since
17645 the bound of the array is not constant, and therefore
17646 not mangleable. Besides, such types are not allowed in
17647 ISO C++, so we can do as we please here. We do allow
17648 them for 'auto' deduction, since that isn't ABI-exposed. */
17649 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17650 return unify_vla_arg (explain_p, arg);
17651
17652 /* Strip typedefs as in convert_template_argument. */
17653 arg = canonicalize_type_argument (arg, tf_none);
17654 }
17655
17656 /* If ARG is a parameter pack or an expansion, we cannot unify
17657 against it unless PARM is also a parameter pack. */
17658 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17659 && !template_parameter_pack_p (parm))
17660 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17661
17662 /* If the argument deduction results is a METHOD_TYPE,
17663 then there is a problem.
17664 METHOD_TYPE doesn't map to any real C++ type the result of
17665 the deduction can not be of that type. */
17666 if (TREE_CODE (arg) == METHOD_TYPE)
17667 return unify_method_type_error (explain_p, arg);
17668
17669 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17670 return unify_success (explain_p);
17671
17672 case TEMPLATE_PARM_INDEX:
17673 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17674 if (tparm == error_mark_node)
17675 return unify_invalid (explain_p);
17676
17677 if (TEMPLATE_PARM_LEVEL (parm)
17678 != template_decl_level (tparm))
17679 {
17680 /* The PARM is not one we're trying to unify. Just check
17681 to see if it matches ARG. */
17682 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17683 && cp_tree_equal (parm, arg));
17684 if (result)
17685 unify_expression_unequal (explain_p, parm, arg);
17686 return result;
17687 }
17688
17689 idx = TEMPLATE_PARM_IDX (parm);
17690 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17691
17692 if (targ)
17693 {
17694 int x = !cp_tree_equal (targ, arg);
17695 if (x)
17696 unify_inconsistency (explain_p, parm, targ, arg);
17697 return x;
17698 }
17699
17700 /* [temp.deduct.type] If, in the declaration of a function template
17701 with a non-type template-parameter, the non-type
17702 template-parameter is used in an expression in the function
17703 parameter-list and, if the corresponding template-argument is
17704 deduced, the template-argument type shall match the type of the
17705 template-parameter exactly, except that a template-argument
17706 deduced from an array bound may be of any integral type.
17707 The non-type parameter might use already deduced type parameters. */
17708 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17709 if (!TREE_TYPE (arg))
17710 /* Template-parameter dependent expression. Just accept it for now.
17711 It will later be processed in convert_template_argument. */
17712 ;
17713 else if (same_type_p (TREE_TYPE (arg), tparm))
17714 /* OK */;
17715 else if ((strict & UNIFY_ALLOW_INTEGER)
17716 && CP_INTEGRAL_TYPE_P (tparm))
17717 /* Convert the ARG to the type of PARM; the deduced non-type
17718 template argument must exactly match the types of the
17719 corresponding parameter. */
17720 arg = fold (build_nop (tparm, arg));
17721 else if (uses_template_parms (tparm))
17722 /* We haven't deduced the type of this parameter yet. Try again
17723 later. */
17724 return unify_success (explain_p);
17725 else
17726 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17727
17728 /* If ARG is a parameter pack or an expansion, we cannot unify
17729 against it unless PARM is also a parameter pack. */
17730 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17731 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17732 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17733
17734 arg = strip_typedefs_expr (arg);
17735 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17736 return unify_success (explain_p);
17737
17738 case PTRMEM_CST:
17739 {
17740 /* A pointer-to-member constant can be unified only with
17741 another constant. */
17742 if (TREE_CODE (arg) != PTRMEM_CST)
17743 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17744
17745 /* Just unify the class member. It would be useless (and possibly
17746 wrong, depending on the strict flags) to unify also
17747 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17748 arg refer to the same variable, even if through different
17749 classes. For instance:
17750
17751 struct A { int x; };
17752 struct B : A { };
17753
17754 Unification of &A::x and &B::x must succeed. */
17755 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17756 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17757 }
17758
17759 case POINTER_TYPE:
17760 {
17761 if (!TYPE_PTR_P (arg))
17762 return unify_type_mismatch (explain_p, parm, arg);
17763
17764 /* [temp.deduct.call]
17765
17766 A can be another pointer or pointer to member type that can
17767 be converted to the deduced A via a qualification
17768 conversion (_conv.qual_).
17769
17770 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17771 This will allow for additional cv-qualification of the
17772 pointed-to types if appropriate. */
17773
17774 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17775 /* The derived-to-base conversion only persists through one
17776 level of pointers. */
17777 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17778
17779 return unify (tparms, targs, TREE_TYPE (parm),
17780 TREE_TYPE (arg), strict, explain_p);
17781 }
17782
17783 case REFERENCE_TYPE:
17784 if (TREE_CODE (arg) != REFERENCE_TYPE)
17785 return unify_type_mismatch (explain_p, parm, arg);
17786 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17787 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17788
17789 case ARRAY_TYPE:
17790 if (TREE_CODE (arg) != ARRAY_TYPE)
17791 return unify_type_mismatch (explain_p, parm, arg);
17792 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17793 != (TYPE_DOMAIN (arg) == NULL_TREE))
17794 return unify_type_mismatch (explain_p, parm, arg);
17795 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17796 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17797 if (TYPE_DOMAIN (parm) != NULL_TREE)
17798 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17799 TYPE_DOMAIN (arg), explain_p);
17800 return unify_success (explain_p);
17801
17802 case REAL_TYPE:
17803 case COMPLEX_TYPE:
17804 case VECTOR_TYPE:
17805 case INTEGER_TYPE:
17806 case BOOLEAN_TYPE:
17807 case ENUMERAL_TYPE:
17808 case VOID_TYPE:
17809 case NULLPTR_TYPE:
17810 if (TREE_CODE (arg) != TREE_CODE (parm))
17811 return unify_type_mismatch (explain_p, parm, arg);
17812
17813 /* We have already checked cv-qualification at the top of the
17814 function. */
17815 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17816 return unify_type_mismatch (explain_p, parm, arg);
17817
17818 /* As far as unification is concerned, this wins. Later checks
17819 will invalidate it if necessary. */
17820 return unify_success (explain_p);
17821
17822 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17823 /* Type INTEGER_CST can come from ordinary constant template args. */
17824 case INTEGER_CST:
17825 while (TREE_CODE (arg) == NOP_EXPR)
17826 arg = TREE_OPERAND (arg, 0);
17827
17828 if (TREE_CODE (arg) != INTEGER_CST)
17829 return unify_template_argument_mismatch (explain_p, parm, arg);
17830 return (tree_int_cst_equal (parm, arg)
17831 ? unify_success (explain_p)
17832 : unify_template_argument_mismatch (explain_p, parm, arg));
17833
17834 case TREE_VEC:
17835 {
17836 int i, len, argslen;
17837 int parm_variadic_p = 0;
17838
17839 if (TREE_CODE (arg) != TREE_VEC)
17840 return unify_template_argument_mismatch (explain_p, parm, arg);
17841
17842 len = TREE_VEC_LENGTH (parm);
17843 argslen = TREE_VEC_LENGTH (arg);
17844
17845 /* Check for pack expansions in the parameters. */
17846 for (i = 0; i < len; ++i)
17847 {
17848 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17849 {
17850 if (i == len - 1)
17851 /* We can unify against something with a trailing
17852 parameter pack. */
17853 parm_variadic_p = 1;
17854 else
17855 /* [temp.deduct.type]/9: If the template argument list of
17856 P contains a pack expansion that is not the last
17857 template argument, the entire template argument list
17858 is a non-deduced context. */
17859 return unify_success (explain_p);
17860 }
17861 }
17862
17863 /* If we don't have enough arguments to satisfy the parameters
17864 (not counting the pack expression at the end), or we have
17865 too many arguments for a parameter list that doesn't end in
17866 a pack expression, we can't unify. */
17867 if (parm_variadic_p
17868 ? argslen < len - parm_variadic_p
17869 : argslen != len)
17870 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17871
17872 /* Unify all of the parameters that precede the (optional)
17873 pack expression. */
17874 for (i = 0; i < len - parm_variadic_p; ++i)
17875 {
17876 RECUR_AND_CHECK_FAILURE (tparms, targs,
17877 TREE_VEC_ELT (parm, i),
17878 TREE_VEC_ELT (arg, i),
17879 UNIFY_ALLOW_NONE, explain_p);
17880 }
17881 if (parm_variadic_p)
17882 return unify_pack_expansion (tparms, targs, parm, arg,
17883 DEDUCE_EXACT,
17884 /*subr=*/true, explain_p);
17885 return unify_success (explain_p);
17886 }
17887
17888 case RECORD_TYPE:
17889 case UNION_TYPE:
17890 if (TREE_CODE (arg) != TREE_CODE (parm))
17891 return unify_type_mismatch (explain_p, parm, arg);
17892
17893 if (TYPE_PTRMEMFUNC_P (parm))
17894 {
17895 if (!TYPE_PTRMEMFUNC_P (arg))
17896 return unify_type_mismatch (explain_p, parm, arg);
17897
17898 return unify (tparms, targs,
17899 TYPE_PTRMEMFUNC_FN_TYPE (parm),
17900 TYPE_PTRMEMFUNC_FN_TYPE (arg),
17901 strict, explain_p);
17902 }
17903
17904 if (CLASSTYPE_TEMPLATE_INFO (parm))
17905 {
17906 tree t = NULL_TREE;
17907
17908 if (strict_in & UNIFY_ALLOW_DERIVED)
17909 {
17910 /* First, we try to unify the PARM and ARG directly. */
17911 t = try_class_unification (tparms, targs,
17912 parm, arg, explain_p);
17913
17914 if (!t)
17915 {
17916 /* Fallback to the special case allowed in
17917 [temp.deduct.call]:
17918
17919 If P is a class, and P has the form
17920 template-id, then A can be a derived class of
17921 the deduced A. Likewise, if P is a pointer to
17922 a class of the form template-id, A can be a
17923 pointer to a derived class pointed to by the
17924 deduced A. */
17925 enum template_base_result r;
17926 r = get_template_base (tparms, targs, parm, arg,
17927 explain_p, &t);
17928
17929 if (!t)
17930 return unify_no_common_base (explain_p, r, parm, arg);
17931 }
17932 }
17933 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17934 && (CLASSTYPE_TI_TEMPLATE (parm)
17935 == CLASSTYPE_TI_TEMPLATE (arg)))
17936 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17937 Then, we should unify `int' and `U'. */
17938 t = arg;
17939 else
17940 /* There's no chance of unification succeeding. */
17941 return unify_type_mismatch (explain_p, parm, arg);
17942
17943 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17944 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17945 }
17946 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17947 return unify_type_mismatch (explain_p, parm, arg);
17948 return unify_success (explain_p);
17949
17950 case METHOD_TYPE:
17951 case FUNCTION_TYPE:
17952 {
17953 unsigned int nargs;
17954 tree *args;
17955 tree a;
17956 unsigned int i;
17957
17958 if (TREE_CODE (arg) != TREE_CODE (parm))
17959 return unify_type_mismatch (explain_p, parm, arg);
17960
17961 /* CV qualifications for methods can never be deduced, they must
17962 match exactly. We need to check them explicitly here,
17963 because type_unification_real treats them as any other
17964 cv-qualified parameter. */
17965 if (TREE_CODE (parm) == METHOD_TYPE
17966 && (!check_cv_quals_for_unify
17967 (UNIFY_ALLOW_NONE,
17968 class_of_this_parm (arg),
17969 class_of_this_parm (parm))))
17970 return unify_cv_qual_mismatch (explain_p, parm, arg);
17971
17972 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17973 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17974
17975 nargs = list_length (TYPE_ARG_TYPES (arg));
17976 args = XALLOCAVEC (tree, nargs);
17977 for (a = TYPE_ARG_TYPES (arg), i = 0;
17978 a != NULL_TREE && a != void_list_node;
17979 a = TREE_CHAIN (a), ++i)
17980 args[i] = TREE_VALUE (a);
17981 nargs = i;
17982
17983 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17984 args, nargs, 1, DEDUCE_EXACT,
17985 LOOKUP_NORMAL, NULL, explain_p);
17986 }
17987
17988 case OFFSET_TYPE:
17989 /* Unify a pointer to member with a pointer to member function, which
17990 deduces the type of the member as a function type. */
17991 if (TYPE_PTRMEMFUNC_P (arg))
17992 {
17993 /* Check top-level cv qualifiers */
17994 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17995 return unify_cv_qual_mismatch (explain_p, parm, arg);
17996
17997 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17998 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17999 UNIFY_ALLOW_NONE, explain_p);
18000
18001 /* Determine the type of the function we are unifying against. */
18002 tree fntype = static_fn_type (arg);
18003
18004 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
18005 }
18006
18007 if (TREE_CODE (arg) != OFFSET_TYPE)
18008 return unify_type_mismatch (explain_p, parm, arg);
18009 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18010 TYPE_OFFSET_BASETYPE (arg),
18011 UNIFY_ALLOW_NONE, explain_p);
18012 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18013 strict, explain_p);
18014
18015 case CONST_DECL:
18016 if (DECL_TEMPLATE_PARM_P (parm))
18017 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
18018 if (arg != integral_constant_value (parm))
18019 return unify_template_argument_mismatch (explain_p, parm, arg);
18020 return unify_success (explain_p);
18021
18022 case FIELD_DECL:
18023 case TEMPLATE_DECL:
18024 /* Matched cases are handled by the ARG == PARM test above. */
18025 return unify_template_argument_mismatch (explain_p, parm, arg);
18026
18027 case VAR_DECL:
18028 /* A non-type template parameter that is a variable should be a
18029 an integral constant, in which case, it whould have been
18030 folded into its (constant) value. So we should not be getting
18031 a variable here. */
18032 gcc_unreachable ();
18033
18034 case TYPE_ARGUMENT_PACK:
18035 case NONTYPE_ARGUMENT_PACK:
18036 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
18037 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
18038
18039 case TYPEOF_TYPE:
18040 case DECLTYPE_TYPE:
18041 case UNDERLYING_TYPE:
18042 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18043 or UNDERLYING_TYPE nodes. */
18044 return unify_success (explain_p);
18045
18046 case ERROR_MARK:
18047 /* Unification fails if we hit an error node. */
18048 return unify_invalid (explain_p);
18049
18050 case INDIRECT_REF:
18051 if (REFERENCE_REF_P (parm))
18052 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
18053 strict, explain_p);
18054 /* FALLTHRU */
18055
18056 default:
18057 /* An unresolved overload is a nondeduced context. */
18058 if (is_overloaded_fn (parm) || type_unknown_p (parm))
18059 return unify_success (explain_p);
18060 gcc_assert (EXPR_P (parm));
18061
18062 /* We must be looking at an expression. This can happen with
18063 something like:
18064
18065 template <int I>
18066 void foo(S<I>, S<I + 2>);
18067
18068 This is a "nondeduced context":
18069
18070 [deduct.type]
18071
18072 The nondeduced contexts are:
18073
18074 --A type that is a template-id in which one or more of
18075 the template-arguments is an expression that references
18076 a template-parameter.
18077
18078 In these cases, we assume deduction succeeded, but don't
18079 actually infer any unifications. */
18080
18081 if (!uses_template_parms (parm)
18082 && !template_args_equal (parm, arg))
18083 return unify_expression_unequal (explain_p, parm, arg);
18084 else
18085 return unify_success (explain_p);
18086 }
18087 }
18088 #undef RECUR_AND_CHECK_FAILURE
18089 \f
18090 /* Note that DECL can be defined in this translation unit, if
18091 required. */
18092
18093 static void
18094 mark_definable (tree decl)
18095 {
18096 tree clone;
18097 DECL_NOT_REALLY_EXTERN (decl) = 1;
18098 FOR_EACH_CLONE (clone, decl)
18099 DECL_NOT_REALLY_EXTERN (clone) = 1;
18100 }
18101
18102 /* Called if RESULT is explicitly instantiated, or is a member of an
18103 explicitly instantiated class. */
18104
18105 void
18106 mark_decl_instantiated (tree result, int extern_p)
18107 {
18108 SET_DECL_EXPLICIT_INSTANTIATION (result);
18109
18110 /* If this entity has already been written out, it's too late to
18111 make any modifications. */
18112 if (TREE_ASM_WRITTEN (result))
18113 return;
18114
18115 /* For anonymous namespace we don't need to do anything. */
18116 if (decl_anon_ns_mem_p (result))
18117 {
18118 gcc_assert (!TREE_PUBLIC (result));
18119 return;
18120 }
18121
18122 if (TREE_CODE (result) != FUNCTION_DECL)
18123 /* The TREE_PUBLIC flag for function declarations will have been
18124 set correctly by tsubst. */
18125 TREE_PUBLIC (result) = 1;
18126
18127 /* This might have been set by an earlier implicit instantiation. */
18128 DECL_COMDAT (result) = 0;
18129
18130 if (extern_p)
18131 DECL_NOT_REALLY_EXTERN (result) = 0;
18132 else
18133 {
18134 mark_definable (result);
18135 mark_needed (result);
18136 /* Always make artificials weak. */
18137 if (DECL_ARTIFICIAL (result) && flag_weak)
18138 comdat_linkage (result);
18139 /* For WIN32 we also want to put explicit instantiations in
18140 linkonce sections. */
18141 else if (TREE_PUBLIC (result))
18142 maybe_make_one_only (result);
18143 }
18144
18145 /* If EXTERN_P, then this function will not be emitted -- unless
18146 followed by an explicit instantiation, at which point its linkage
18147 will be adjusted. If !EXTERN_P, then this function will be
18148 emitted here. In neither circumstance do we want
18149 import_export_decl to adjust the linkage. */
18150 DECL_INTERFACE_KNOWN (result) = 1;
18151 }
18152
18153 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18154 important template arguments. If any are missing, we check whether
18155 they're important by using error_mark_node for substituting into any
18156 args that were used for partial ordering (the ones between ARGS and END)
18157 and seeing if it bubbles up. */
18158
18159 static bool
18160 check_undeduced_parms (tree targs, tree args, tree end)
18161 {
18162 bool found = false;
18163 int i;
18164 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
18165 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
18166 {
18167 found = true;
18168 TREE_VEC_ELT (targs, i) = error_mark_node;
18169 }
18170 if (found)
18171 {
18172 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
18173 if (substed == error_mark_node)
18174 return true;
18175 }
18176 return false;
18177 }
18178
18179 /* Given two function templates PAT1 and PAT2, return:
18180
18181 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18182 -1 if PAT2 is more specialized than PAT1.
18183 0 if neither is more specialized.
18184
18185 LEN indicates the number of parameters we should consider
18186 (defaulted parameters should not be considered).
18187
18188 The 1998 std underspecified function template partial ordering, and
18189 DR214 addresses the issue. We take pairs of arguments, one from
18190 each of the templates, and deduce them against each other. One of
18191 the templates will be more specialized if all the *other*
18192 template's arguments deduce against its arguments and at least one
18193 of its arguments *does* *not* deduce against the other template's
18194 corresponding argument. Deduction is done as for class templates.
18195 The arguments used in deduction have reference and top level cv
18196 qualifiers removed. Iff both arguments were originally reference
18197 types *and* deduction succeeds in both directions, an lvalue reference
18198 wins against an rvalue reference and otherwise the template
18199 with the more cv-qualified argument wins for that pairing (if
18200 neither is more cv-qualified, they both are equal). Unlike regular
18201 deduction, after all the arguments have been deduced in this way,
18202 we do *not* verify the deduced template argument values can be
18203 substituted into non-deduced contexts.
18204
18205 The logic can be a bit confusing here, because we look at deduce1 and
18206 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18207 can find template arguments for pat1 to make arg1 look like arg2, that
18208 means that arg2 is at least as specialized as arg1. */
18209
18210 int
18211 more_specialized_fn (tree pat1, tree pat2, int len)
18212 {
18213 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18214 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18215 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18216 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18217 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18218 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18219 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18220 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18221 tree origs1, origs2;
18222 bool lose1 = false;
18223 bool lose2 = false;
18224
18225 /* Remove the this parameter from non-static member functions. If
18226 one is a non-static member function and the other is not a static
18227 member function, remove the first parameter from that function
18228 also. This situation occurs for operator functions where we
18229 locate both a member function (with this pointer) and non-member
18230 operator (with explicit first operand). */
18231 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18232 {
18233 len--; /* LEN is the number of significant arguments for DECL1 */
18234 args1 = TREE_CHAIN (args1);
18235 if (!DECL_STATIC_FUNCTION_P (decl2))
18236 args2 = TREE_CHAIN (args2);
18237 }
18238 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18239 {
18240 args2 = TREE_CHAIN (args2);
18241 if (!DECL_STATIC_FUNCTION_P (decl1))
18242 {
18243 len--;
18244 args1 = TREE_CHAIN (args1);
18245 }
18246 }
18247
18248 /* If only one is a conversion operator, they are unordered. */
18249 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18250 return 0;
18251
18252 /* Consider the return type for a conversion function */
18253 if (DECL_CONV_FN_P (decl1))
18254 {
18255 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18256 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18257 len++;
18258 }
18259
18260 processing_template_decl++;
18261
18262 origs1 = args1;
18263 origs2 = args2;
18264
18265 while (len--
18266 /* Stop when an ellipsis is seen. */
18267 && args1 != NULL_TREE && args2 != NULL_TREE)
18268 {
18269 tree arg1 = TREE_VALUE (args1);
18270 tree arg2 = TREE_VALUE (args2);
18271 int deduce1, deduce2;
18272 int quals1 = -1;
18273 int quals2 = -1;
18274 int ref1 = 0;
18275 int ref2 = 0;
18276
18277 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18278 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18279 {
18280 /* When both arguments are pack expansions, we need only
18281 unify the patterns themselves. */
18282 arg1 = PACK_EXPANSION_PATTERN (arg1);
18283 arg2 = PACK_EXPANSION_PATTERN (arg2);
18284
18285 /* This is the last comparison we need to do. */
18286 len = 0;
18287 }
18288
18289 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18290 {
18291 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18292 arg1 = TREE_TYPE (arg1);
18293 quals1 = cp_type_quals (arg1);
18294 }
18295
18296 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18297 {
18298 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18299 arg2 = TREE_TYPE (arg2);
18300 quals2 = cp_type_quals (arg2);
18301 }
18302
18303 arg1 = TYPE_MAIN_VARIANT (arg1);
18304 arg2 = TYPE_MAIN_VARIANT (arg2);
18305
18306 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18307 {
18308 int i, len2 = list_length (args2);
18309 tree parmvec = make_tree_vec (1);
18310 tree argvec = make_tree_vec (len2);
18311 tree ta = args2;
18312
18313 /* Setup the parameter vector, which contains only ARG1. */
18314 TREE_VEC_ELT (parmvec, 0) = arg1;
18315
18316 /* Setup the argument vector, which contains the remaining
18317 arguments. */
18318 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18319 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18320
18321 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18322 argvec, DEDUCE_EXACT,
18323 /*subr=*/true, /*explain_p=*/false)
18324 == 0);
18325
18326 /* We cannot deduce in the other direction, because ARG1 is
18327 a pack expansion but ARG2 is not. */
18328 deduce2 = 0;
18329 }
18330 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18331 {
18332 int i, len1 = list_length (args1);
18333 tree parmvec = make_tree_vec (1);
18334 tree argvec = make_tree_vec (len1);
18335 tree ta = args1;
18336
18337 /* Setup the parameter vector, which contains only ARG1. */
18338 TREE_VEC_ELT (parmvec, 0) = arg2;
18339
18340 /* Setup the argument vector, which contains the remaining
18341 arguments. */
18342 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18343 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18344
18345 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18346 argvec, DEDUCE_EXACT,
18347 /*subr=*/true, /*explain_p=*/false)
18348 == 0);
18349
18350 /* We cannot deduce in the other direction, because ARG2 is
18351 a pack expansion but ARG1 is not.*/
18352 deduce1 = 0;
18353 }
18354
18355 else
18356 {
18357 /* The normal case, where neither argument is a pack
18358 expansion. */
18359 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18360 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18361 == 0);
18362 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18363 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18364 == 0);
18365 }
18366
18367 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18368 arg2, then arg2 is not as specialized as arg1. */
18369 if (!deduce1)
18370 lose2 = true;
18371 if (!deduce2)
18372 lose1 = true;
18373
18374 /* "If, for a given type, deduction succeeds in both directions
18375 (i.e., the types are identical after the transformations above)
18376 and both P and A were reference types (before being replaced with
18377 the type referred to above):
18378 - if the type from the argument template was an lvalue reference and
18379 the type from the parameter template was not, the argument type is
18380 considered to be more specialized than the other; otherwise,
18381 - if the type from the argument template is more cv-qualified
18382 than the type from the parameter template (as described above),
18383 the argument type is considered to be more specialized than the other;
18384 otherwise,
18385 - neither type is more specialized than the other." */
18386
18387 if (deduce1 && deduce2)
18388 {
18389 if (ref1 && ref2 && ref1 != ref2)
18390 {
18391 if (ref1 > ref2)
18392 lose1 = true;
18393 else
18394 lose2 = true;
18395 }
18396 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18397 {
18398 if ((quals1 & quals2) == quals2)
18399 lose2 = true;
18400 if ((quals1 & quals2) == quals1)
18401 lose1 = true;
18402 }
18403 }
18404
18405 if (lose1 && lose2)
18406 /* We've failed to deduce something in either direction.
18407 These must be unordered. */
18408 break;
18409
18410 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18411 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18412 /* We have already processed all of the arguments in our
18413 handing of the pack expansion type. */
18414 len = 0;
18415
18416 args1 = TREE_CHAIN (args1);
18417 args2 = TREE_CHAIN (args2);
18418 }
18419
18420 /* "In most cases, all template parameters must have values in order for
18421 deduction to succeed, but for partial ordering purposes a template
18422 parameter may remain without a value provided it is not used in the
18423 types being used for partial ordering."
18424
18425 Thus, if we are missing any of the targs1 we need to substitute into
18426 origs1, then pat2 is not as specialized as pat1. This can happen when
18427 there is a nondeduced context. */
18428 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18429 lose2 = true;
18430 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18431 lose1 = true;
18432
18433 processing_template_decl--;
18434
18435 /* All things being equal, if the next argument is a pack expansion
18436 for one function but not for the other, prefer the
18437 non-variadic function. FIXME this is bogus; see c++/41958. */
18438 if (lose1 == lose2
18439 && args1 && TREE_VALUE (args1)
18440 && args2 && TREE_VALUE (args2))
18441 {
18442 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18443 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18444 }
18445
18446 if (lose1 == lose2)
18447 return 0;
18448 else if (!lose1)
18449 return 1;
18450 else
18451 return -1;
18452 }
18453
18454 /* Determine which of two partial specializations of TMPL is more
18455 specialized.
18456
18457 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18458 to the first partial specialization. The TREE_VALUE is the
18459 innermost set of template parameters for the partial
18460 specialization. PAT2 is similar, but for the second template.
18461
18462 Return 1 if the first partial specialization is more specialized;
18463 -1 if the second is more specialized; 0 if neither is more
18464 specialized.
18465
18466 See [temp.class.order] for information about determining which of
18467 two templates is more specialized. */
18468
18469 static int
18470 more_specialized_class (tree tmpl, tree pat1, tree pat2)
18471 {
18472 tree targs;
18473 tree tmpl1, tmpl2;
18474 int winner = 0;
18475 bool any_deductions = false;
18476
18477 tmpl1 = TREE_TYPE (pat1);
18478 tmpl2 = TREE_TYPE (pat2);
18479
18480 /* Just like what happens for functions, if we are ordering between
18481 different class template specializations, we may encounter dependent
18482 types in the arguments, and we need our dependency check functions
18483 to behave correctly. */
18484 ++processing_template_decl;
18485 targs = get_class_bindings (tmpl, TREE_VALUE (pat1),
18486 CLASSTYPE_TI_ARGS (tmpl1),
18487 CLASSTYPE_TI_ARGS (tmpl2));
18488 if (targs)
18489 {
18490 --winner;
18491 any_deductions = true;
18492 }
18493
18494 targs = get_class_bindings (tmpl, TREE_VALUE (pat2),
18495 CLASSTYPE_TI_ARGS (tmpl2),
18496 CLASSTYPE_TI_ARGS (tmpl1));
18497 if (targs)
18498 {
18499 ++winner;
18500 any_deductions = true;
18501 }
18502 --processing_template_decl;
18503
18504 /* In the case of a tie where at least one of the class templates
18505 has a parameter pack at the end, the template with the most
18506 non-packed parameters wins. */
18507 if (winner == 0
18508 && any_deductions
18509 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18510 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18511 {
18512 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18513 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18514 int len1 = TREE_VEC_LENGTH (args1);
18515 int len2 = TREE_VEC_LENGTH (args2);
18516
18517 /* We don't count the pack expansion at the end. */
18518 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18519 --len1;
18520 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18521 --len2;
18522
18523 if (len1 > len2)
18524 return 1;
18525 else if (len1 < len2)
18526 return -1;
18527 }
18528
18529 return winner;
18530 }
18531
18532 /* Return the template arguments that will produce the function signature
18533 DECL from the function template FN, with the explicit template
18534 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18535 also match. Return NULL_TREE if no satisfactory arguments could be
18536 found. */
18537
18538 static tree
18539 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18540 {
18541 int ntparms = DECL_NTPARMS (fn);
18542 tree targs = make_tree_vec (ntparms);
18543 tree decl_type = TREE_TYPE (decl);
18544 tree decl_arg_types;
18545 tree *args;
18546 unsigned int nargs, ix;
18547 tree arg;
18548
18549 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18550
18551 /* Never do unification on the 'this' parameter. */
18552 decl_arg_types = skip_artificial_parms_for (decl,
18553 TYPE_ARG_TYPES (decl_type));
18554
18555 nargs = list_length (decl_arg_types);
18556 args = XALLOCAVEC (tree, nargs);
18557 for (arg = decl_arg_types, ix = 0;
18558 arg != NULL_TREE && arg != void_list_node;
18559 arg = TREE_CHAIN (arg), ++ix)
18560 args[ix] = TREE_VALUE (arg);
18561
18562 if (fn_type_unification (fn, explicit_args, targs,
18563 args, ix,
18564 (check_rettype || DECL_CONV_FN_P (fn)
18565 ? TREE_TYPE (decl_type) : NULL_TREE),
18566 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18567 /*decltype*/false)
18568 == error_mark_node)
18569 return NULL_TREE;
18570
18571 return targs;
18572 }
18573
18574 /* Return the innermost template arguments that, when applied to a partial
18575 specialization of TMPL whose innermost template parameters are
18576 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18577 ARGS.
18578
18579 For example, suppose we have:
18580
18581 template <class T, class U> struct S {};
18582 template <class T> struct S<T*, int> {};
18583
18584 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18585 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18586 int}. The resulting vector will be {double}, indicating that `T'
18587 is bound to `double'. */
18588
18589 static tree
18590 get_class_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
18591 {
18592 int i, ntparms = TREE_VEC_LENGTH (tparms);
18593 tree deduced_args;
18594 tree innermost_deduced_args;
18595
18596 innermost_deduced_args = make_tree_vec (ntparms);
18597 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18598 {
18599 deduced_args = copy_node (args);
18600 SET_TMPL_ARGS_LEVEL (deduced_args,
18601 TMPL_ARGS_DEPTH (deduced_args),
18602 innermost_deduced_args);
18603 }
18604 else
18605 deduced_args = innermost_deduced_args;
18606
18607 if (unify (tparms, deduced_args,
18608 INNERMOST_TEMPLATE_ARGS (spec_args),
18609 INNERMOST_TEMPLATE_ARGS (args),
18610 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18611 return NULL_TREE;
18612
18613 for (i = 0; i < ntparms; ++i)
18614 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18615 return NULL_TREE;
18616
18617 /* Verify that nondeduced template arguments agree with the type
18618 obtained from argument deduction.
18619
18620 For example:
18621
18622 struct A { typedef int X; };
18623 template <class T, class U> struct C {};
18624 template <class T> struct C<T, typename T::X> {};
18625
18626 Then with the instantiation `C<A, int>', we can deduce that
18627 `T' is `A' but unify () does not check whether `typename T::X'
18628 is `int'. */
18629 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18630 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18631 spec_args, tmpl,
18632 tf_none, false, false);
18633 if (spec_args == error_mark_node
18634 /* We only need to check the innermost arguments; the other
18635 arguments will always agree. */
18636 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18637 INNERMOST_TEMPLATE_ARGS (args)))
18638 return NULL_TREE;
18639
18640 /* Now that we have bindings for all of the template arguments,
18641 ensure that the arguments deduced for the template template
18642 parameters have compatible template parameter lists. See the use
18643 of template_template_parm_bindings_ok_p in fn_type_unification
18644 for more information. */
18645 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18646 return NULL_TREE;
18647
18648 return deduced_args;
18649 }
18650
18651 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18652 Return the TREE_LIST node with the most specialized template, if
18653 any. If there is no most specialized template, the error_mark_node
18654 is returned.
18655
18656 Note that this function does not look at, or modify, the
18657 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18658 returned is one of the elements of INSTANTIATIONS, callers may
18659 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18660 and retrieve it from the value returned. */
18661
18662 tree
18663 most_specialized_instantiation (tree templates)
18664 {
18665 tree fn, champ;
18666
18667 ++processing_template_decl;
18668
18669 champ = templates;
18670 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18671 {
18672 int fate = 0;
18673
18674 if (get_bindings (TREE_VALUE (champ),
18675 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18676 NULL_TREE, /*check_ret=*/true))
18677 fate--;
18678
18679 if (get_bindings (TREE_VALUE (fn),
18680 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18681 NULL_TREE, /*check_ret=*/true))
18682 fate++;
18683
18684 if (fate == -1)
18685 champ = fn;
18686 else if (!fate)
18687 {
18688 /* Equally specialized, move to next function. If there
18689 is no next function, nothing's most specialized. */
18690 fn = TREE_CHAIN (fn);
18691 champ = fn;
18692 if (!fn)
18693 break;
18694 }
18695 }
18696
18697 if (champ)
18698 /* Now verify that champ is better than everything earlier in the
18699 instantiation list. */
18700 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18701 if (get_bindings (TREE_VALUE (champ),
18702 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18703 NULL_TREE, /*check_ret=*/true)
18704 || !get_bindings (TREE_VALUE (fn),
18705 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18706 NULL_TREE, /*check_ret=*/true))
18707 {
18708 champ = NULL_TREE;
18709 break;
18710 }
18711
18712 processing_template_decl--;
18713
18714 if (!champ)
18715 return error_mark_node;
18716
18717 return champ;
18718 }
18719
18720 /* If DECL is a specialization of some template, return the most
18721 general such template. Otherwise, returns NULL_TREE.
18722
18723 For example, given:
18724
18725 template <class T> struct S { template <class U> void f(U); };
18726
18727 if TMPL is `template <class U> void S<int>::f(U)' this will return
18728 the full template. This function will not trace past partial
18729 specializations, however. For example, given in addition:
18730
18731 template <class T> struct S<T*> { template <class U> void f(U); };
18732
18733 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18734 `template <class T> template <class U> S<T*>::f(U)'. */
18735
18736 tree
18737 most_general_template (tree decl)
18738 {
18739 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18740 an immediate specialization. */
18741 if (TREE_CODE (decl) == FUNCTION_DECL)
18742 {
18743 if (DECL_TEMPLATE_INFO (decl)) {
18744 decl = DECL_TI_TEMPLATE (decl);
18745
18746 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18747 template friend. */
18748 if (TREE_CODE (decl) != TEMPLATE_DECL)
18749 return NULL_TREE;
18750 } else
18751 return NULL_TREE;
18752 }
18753
18754 /* Look for more and more general templates. */
18755 while (DECL_TEMPLATE_INFO (decl))
18756 {
18757 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18758 (See cp-tree.h for details.) */
18759 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18760 break;
18761
18762 if (CLASS_TYPE_P (TREE_TYPE (decl))
18763 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18764 break;
18765
18766 /* Stop if we run into an explicitly specialized class template. */
18767 if (!DECL_NAMESPACE_SCOPE_P (decl)
18768 && DECL_CONTEXT (decl)
18769 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18770 break;
18771
18772 decl = DECL_TI_TEMPLATE (decl);
18773 }
18774
18775 return decl;
18776 }
18777
18778 /* Return the most specialized of the class template partial
18779 specializations which can produce TYPE, a specialization of some class
18780 template. The value returned is actually a TREE_LIST; the TREE_TYPE is
18781 a _TYPE node corresponding to the partial specialization, while the
18782 TREE_PURPOSE is the set of template arguments that must be
18783 substituted into the TREE_TYPE in order to generate TYPE.
18784
18785 If the choice of partial specialization is ambiguous, a diagnostic
18786 is issued, and the error_mark_node is returned. If there are no
18787 partial specializations matching TYPE, then NULL_TREE is
18788 returned, indicating that the primary template should be used. */
18789
18790 static tree
18791 most_specialized_class (tree type, tsubst_flags_t complain)
18792 {
18793 tree list = NULL_TREE;
18794 tree t;
18795 tree champ;
18796 int fate;
18797 bool ambiguous_p;
18798 tree outer_args = NULL_TREE;
18799
18800 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
18801 tree main_tmpl = most_general_template (tmpl);
18802 tree args = CLASSTYPE_TI_ARGS (type);
18803
18804 /* For determining which partial specialization to use, only the
18805 innermost args are interesting. */
18806 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18807 {
18808 outer_args = strip_innermost_template_args (args, 1);
18809 args = INNERMOST_TEMPLATE_ARGS (args);
18810 }
18811
18812 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
18813 {
18814 tree partial_spec_args;
18815 tree spec_args;
18816 tree spec_tmpl = TREE_VALUE (t);
18817 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18818
18819 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18820
18821 ++processing_template_decl;
18822
18823 if (outer_args)
18824 {
18825 /* Discard the outer levels of args, and then substitute in the
18826 template args from the enclosing class. */
18827 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18828 partial_spec_args = tsubst_template_args
18829 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18830
18831 /* And the same for the partial specialization TEMPLATE_DECL. */
18832 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18833 }
18834
18835 partial_spec_args =
18836 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18837 partial_spec_args,
18838 tmpl, tf_none,
18839 /*require_all_args=*/true,
18840 /*use_default_args=*/true);
18841
18842 --processing_template_decl;
18843
18844 if (partial_spec_args == error_mark_node)
18845 return error_mark_node;
18846 if (spec_tmpl == error_mark_node)
18847 return error_mark_node;
18848
18849 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18850 spec_args = get_class_bindings (tmpl, parms,
18851 partial_spec_args,
18852 args);
18853 if (spec_args)
18854 {
18855 if (outer_args)
18856 spec_args = add_to_template_args (outer_args, spec_args);
18857 list = tree_cons (spec_args, orig_parms, list);
18858 TREE_TYPE (list) = TREE_TYPE (t);
18859 }
18860 }
18861
18862 if (! list)
18863 return NULL_TREE;
18864
18865 ambiguous_p = false;
18866 t = list;
18867 champ = t;
18868 t = TREE_CHAIN (t);
18869 for (; t; t = TREE_CHAIN (t))
18870 {
18871 fate = more_specialized_class (tmpl, champ, t);
18872 if (fate == 1)
18873 ;
18874 else
18875 {
18876 if (fate == 0)
18877 {
18878 t = TREE_CHAIN (t);
18879 if (! t)
18880 {
18881 ambiguous_p = true;
18882 break;
18883 }
18884 }
18885 champ = t;
18886 }
18887 }
18888
18889 if (!ambiguous_p)
18890 for (t = list; t && t != champ; t = TREE_CHAIN (t))
18891 {
18892 fate = more_specialized_class (tmpl, champ, t);
18893 if (fate != 1)
18894 {
18895 ambiguous_p = true;
18896 break;
18897 }
18898 }
18899
18900 if (ambiguous_p)
18901 {
18902 const char *str;
18903 char *spaces = NULL;
18904 if (!(complain & tf_error))
18905 return error_mark_node;
18906 error ("ambiguous class template instantiation for %q#T", type);
18907 str = ngettext ("candidate is:", "candidates are:", list_length (list));
18908 for (t = list; t; t = TREE_CHAIN (t))
18909 {
18910 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
18911 spaces = spaces ? spaces : get_spaces (str);
18912 }
18913 free (spaces);
18914 return error_mark_node;
18915 }
18916
18917 return champ;
18918 }
18919
18920 /* Explicitly instantiate DECL. */
18921
18922 void
18923 do_decl_instantiation (tree decl, tree storage)
18924 {
18925 tree result = NULL_TREE;
18926 int extern_p = 0;
18927
18928 if (!decl || decl == error_mark_node)
18929 /* An error occurred, for which grokdeclarator has already issued
18930 an appropriate message. */
18931 return;
18932 else if (! DECL_LANG_SPECIFIC (decl))
18933 {
18934 error ("explicit instantiation of non-template %q#D", decl);
18935 return;
18936 }
18937 else if (VAR_P (decl))
18938 {
18939 /* There is an asymmetry here in the way VAR_DECLs and
18940 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18941 the latter, the DECL we get back will be marked as a
18942 template instantiation, and the appropriate
18943 DECL_TEMPLATE_INFO will be set up. This does not happen for
18944 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18945 should handle VAR_DECLs as it currently handles
18946 FUNCTION_DECLs. */
18947 if (!DECL_CLASS_SCOPE_P (decl))
18948 {
18949 error ("%qD is not a static data member of a class template", decl);
18950 return;
18951 }
18952 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18953 if (!result || !VAR_P (result))
18954 {
18955 error ("no matching template for %qD found", decl);
18956 return;
18957 }
18958 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18959 {
18960 error ("type %qT for explicit instantiation %qD does not match "
18961 "declared type %qT", TREE_TYPE (result), decl,
18962 TREE_TYPE (decl));
18963 return;
18964 }
18965 }
18966 else if (TREE_CODE (decl) != FUNCTION_DECL)
18967 {
18968 error ("explicit instantiation of %q#D", decl);
18969 return;
18970 }
18971 else
18972 result = decl;
18973
18974 /* Check for various error cases. Note that if the explicit
18975 instantiation is valid the RESULT will currently be marked as an
18976 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18977 until we get here. */
18978
18979 if (DECL_TEMPLATE_SPECIALIZATION (result))
18980 {
18981 /* DR 259 [temp.spec].
18982
18983 Both an explicit instantiation and a declaration of an explicit
18984 specialization shall not appear in a program unless the explicit
18985 instantiation follows a declaration of the explicit specialization.
18986
18987 For a given set of template parameters, if an explicit
18988 instantiation of a template appears after a declaration of an
18989 explicit specialization for that template, the explicit
18990 instantiation has no effect. */
18991 return;
18992 }
18993 else if (DECL_EXPLICIT_INSTANTIATION (result))
18994 {
18995 /* [temp.spec]
18996
18997 No program shall explicitly instantiate any template more
18998 than once.
18999
19000 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19001 the first instantiation was `extern' and the second is not,
19002 and EXTERN_P for the opposite case. */
19003 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
19004 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
19005 /* If an "extern" explicit instantiation follows an ordinary
19006 explicit instantiation, the template is instantiated. */
19007 if (extern_p)
19008 return;
19009 }
19010 else if (!DECL_IMPLICIT_INSTANTIATION (result))
19011 {
19012 error ("no matching template for %qD found", result);
19013 return;
19014 }
19015 else if (!DECL_TEMPLATE_INFO (result))
19016 {
19017 permerror (input_location, "explicit instantiation of non-template %q#D", result);
19018 return;
19019 }
19020
19021 if (storage == NULL_TREE)
19022 ;
19023 else if (storage == ridpointers[(int) RID_EXTERN])
19024 {
19025 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
19026 pedwarn (input_location, OPT_Wpedantic,
19027 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19028 "instantiations");
19029 extern_p = 1;
19030 }
19031 else
19032 error ("storage class %qD applied to template instantiation", storage);
19033
19034 check_explicit_instantiation_namespace (result);
19035 mark_decl_instantiated (result, extern_p);
19036 if (! extern_p)
19037 instantiate_decl (result, /*defer_ok=*/1,
19038 /*expl_inst_class_mem_p=*/false);
19039 }
19040
19041 static void
19042 mark_class_instantiated (tree t, int extern_p)
19043 {
19044 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
19045 SET_CLASSTYPE_INTERFACE_KNOWN (t);
19046 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
19047 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
19048 if (! extern_p)
19049 {
19050 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
19051 rest_of_type_compilation (t, 1);
19052 }
19053 }
19054
19055 /* Called from do_type_instantiation through binding_table_foreach to
19056 do recursive instantiation for the type bound in ENTRY. */
19057 static void
19058 bt_instantiate_type_proc (binding_entry entry, void *data)
19059 {
19060 tree storage = *(tree *) data;
19061
19062 if (MAYBE_CLASS_TYPE_P (entry->type)
19063 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
19064 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
19065 }
19066
19067 /* Called from do_type_instantiation to instantiate a member
19068 (a member function or a static member variable) of an
19069 explicitly instantiated class template. */
19070 static void
19071 instantiate_class_member (tree decl, int extern_p)
19072 {
19073 mark_decl_instantiated (decl, extern_p);
19074 if (! extern_p)
19075 instantiate_decl (decl, /*defer_ok=*/1,
19076 /*expl_inst_class_mem_p=*/true);
19077 }
19078
19079 /* Perform an explicit instantiation of template class T. STORAGE, if
19080 non-null, is the RID for extern, inline or static. COMPLAIN is
19081 nonzero if this is called from the parser, zero if called recursively,
19082 since the standard is unclear (as detailed below). */
19083
19084 void
19085 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
19086 {
19087 int extern_p = 0;
19088 int nomem_p = 0;
19089 int static_p = 0;
19090 int previous_instantiation_extern_p = 0;
19091
19092 if (TREE_CODE (t) == TYPE_DECL)
19093 t = TREE_TYPE (t);
19094
19095 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
19096 {
19097 tree tmpl =
19098 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
19099 if (tmpl)
19100 error ("explicit instantiation of non-class template %qD", tmpl);
19101 else
19102 error ("explicit instantiation of non-template type %qT", t);
19103 return;
19104 }
19105
19106 complete_type (t);
19107
19108 if (!COMPLETE_TYPE_P (t))
19109 {
19110 if (complain & tf_error)
19111 error ("explicit instantiation of %q#T before definition of template",
19112 t);
19113 return;
19114 }
19115
19116 if (storage != NULL_TREE)
19117 {
19118 if (!in_system_header_at (input_location))
19119 {
19120 if (storage == ridpointers[(int) RID_EXTERN])
19121 {
19122 if (cxx_dialect == cxx98)
19123 pedwarn (input_location, OPT_Wpedantic,
19124 "ISO C++ 1998 forbids the use of %<extern%> on "
19125 "explicit instantiations");
19126 }
19127 else
19128 pedwarn (input_location, OPT_Wpedantic,
19129 "ISO C++ forbids the use of %qE"
19130 " on explicit instantiations", storage);
19131 }
19132
19133 if (storage == ridpointers[(int) RID_INLINE])
19134 nomem_p = 1;
19135 else if (storage == ridpointers[(int) RID_EXTERN])
19136 extern_p = 1;
19137 else if (storage == ridpointers[(int) RID_STATIC])
19138 static_p = 1;
19139 else
19140 {
19141 error ("storage class %qD applied to template instantiation",
19142 storage);
19143 extern_p = 0;
19144 }
19145 }
19146
19147 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
19148 {
19149 /* DR 259 [temp.spec].
19150
19151 Both an explicit instantiation and a declaration of an explicit
19152 specialization shall not appear in a program unless the explicit
19153 instantiation follows a declaration of the explicit specialization.
19154
19155 For a given set of template parameters, if an explicit
19156 instantiation of a template appears after a declaration of an
19157 explicit specialization for that template, the explicit
19158 instantiation has no effect. */
19159 return;
19160 }
19161 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
19162 {
19163 /* [temp.spec]
19164
19165 No program shall explicitly instantiate any template more
19166 than once.
19167
19168 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19169 instantiation was `extern'. If EXTERN_P then the second is.
19170 These cases are OK. */
19171 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
19172
19173 if (!previous_instantiation_extern_p && !extern_p
19174 && (complain & tf_error))
19175 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
19176
19177 /* If we've already instantiated the template, just return now. */
19178 if (!CLASSTYPE_INTERFACE_ONLY (t))
19179 return;
19180 }
19181
19182 check_explicit_instantiation_namespace (TYPE_NAME (t));
19183 mark_class_instantiated (t, extern_p);
19184
19185 if (nomem_p)
19186 return;
19187
19188 {
19189 tree tmp;
19190
19191 /* In contrast to implicit instantiation, where only the
19192 declarations, and not the definitions, of members are
19193 instantiated, we have here:
19194
19195 [temp.explicit]
19196
19197 The explicit instantiation of a class template specialization
19198 implies the instantiation of all of its members not
19199 previously explicitly specialized in the translation unit
19200 containing the explicit instantiation.
19201
19202 Of course, we can't instantiate member template classes, since
19203 we don't have any arguments for them. Note that the standard
19204 is unclear on whether the instantiation of the members are
19205 *explicit* instantiations or not. However, the most natural
19206 interpretation is that it should be an explicit instantiation. */
19207
19208 if (! static_p)
19209 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19210 if (TREE_CODE (tmp) == FUNCTION_DECL
19211 && DECL_TEMPLATE_INSTANTIATION (tmp))
19212 instantiate_class_member (tmp, extern_p);
19213
19214 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19215 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19216 instantiate_class_member (tmp, extern_p);
19217
19218 if (CLASSTYPE_NESTED_UTDS (t))
19219 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19220 bt_instantiate_type_proc, &storage);
19221 }
19222 }
19223
19224 /* Given a function DECL, which is a specialization of TMPL, modify
19225 DECL to be a re-instantiation of TMPL with the same template
19226 arguments. TMPL should be the template into which tsubst'ing
19227 should occur for DECL, not the most general template.
19228
19229 One reason for doing this is a scenario like this:
19230
19231 template <class T>
19232 void f(const T&, int i);
19233
19234 void g() { f(3, 7); }
19235
19236 template <class T>
19237 void f(const T& t, const int i) { }
19238
19239 Note that when the template is first instantiated, with
19240 instantiate_template, the resulting DECL will have no name for the
19241 first parameter, and the wrong type for the second. So, when we go
19242 to instantiate the DECL, we regenerate it. */
19243
19244 static void
19245 regenerate_decl_from_template (tree decl, tree tmpl)
19246 {
19247 /* The arguments used to instantiate DECL, from the most general
19248 template. */
19249 tree args;
19250 tree code_pattern;
19251
19252 args = DECL_TI_ARGS (decl);
19253 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19254
19255 /* Make sure that we can see identifiers, and compute access
19256 correctly. */
19257 push_access_scope (decl);
19258
19259 if (TREE_CODE (decl) == FUNCTION_DECL)
19260 {
19261 tree decl_parm;
19262 tree pattern_parm;
19263 tree specs;
19264 int args_depth;
19265 int parms_depth;
19266
19267 args_depth = TMPL_ARGS_DEPTH (args);
19268 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19269 if (args_depth > parms_depth)
19270 args = get_innermost_template_args (args, parms_depth);
19271
19272 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19273 args, tf_error, NULL_TREE,
19274 /*defer_ok*/false);
19275 if (specs && specs != error_mark_node)
19276 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19277 specs);
19278
19279 /* Merge parameter declarations. */
19280 decl_parm = skip_artificial_parms_for (decl,
19281 DECL_ARGUMENTS (decl));
19282 pattern_parm
19283 = skip_artificial_parms_for (code_pattern,
19284 DECL_ARGUMENTS (code_pattern));
19285 while (decl_parm && !DECL_PACK_P (pattern_parm))
19286 {
19287 tree parm_type;
19288 tree attributes;
19289
19290 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19291 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19292 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19293 NULL_TREE);
19294 parm_type = type_decays_to (parm_type);
19295 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19296 TREE_TYPE (decl_parm) = parm_type;
19297 attributes = DECL_ATTRIBUTES (pattern_parm);
19298 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19299 {
19300 DECL_ATTRIBUTES (decl_parm) = attributes;
19301 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19302 }
19303 decl_parm = DECL_CHAIN (decl_parm);
19304 pattern_parm = DECL_CHAIN (pattern_parm);
19305 }
19306 /* Merge any parameters that match with the function parameter
19307 pack. */
19308 if (pattern_parm && DECL_PACK_P (pattern_parm))
19309 {
19310 int i, len;
19311 tree expanded_types;
19312 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19313 the parameters in this function parameter pack. */
19314 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19315 args, tf_error, NULL_TREE);
19316 len = TREE_VEC_LENGTH (expanded_types);
19317 for (i = 0; i < len; i++)
19318 {
19319 tree parm_type;
19320 tree attributes;
19321
19322 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19323 /* Rename the parameter to include the index. */
19324 DECL_NAME (decl_parm) =
19325 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19326 parm_type = TREE_VEC_ELT (expanded_types, i);
19327 parm_type = type_decays_to (parm_type);
19328 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19329 TREE_TYPE (decl_parm) = parm_type;
19330 attributes = DECL_ATTRIBUTES (pattern_parm);
19331 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19332 {
19333 DECL_ATTRIBUTES (decl_parm) = attributes;
19334 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19335 }
19336 decl_parm = DECL_CHAIN (decl_parm);
19337 }
19338 }
19339 /* Merge additional specifiers from the CODE_PATTERN. */
19340 if (DECL_DECLARED_INLINE_P (code_pattern)
19341 && !DECL_DECLARED_INLINE_P (decl))
19342 DECL_DECLARED_INLINE_P (decl) = 1;
19343 }
19344 else if (VAR_P (decl))
19345 {
19346 DECL_INITIAL (decl) =
19347 tsubst_expr (DECL_INITIAL (code_pattern), args,
19348 tf_error, DECL_TI_TEMPLATE (decl),
19349 /*integral_constant_expression_p=*/false);
19350 if (VAR_HAD_UNKNOWN_BOUND (decl))
19351 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19352 tf_error, DECL_TI_TEMPLATE (decl));
19353 }
19354 else
19355 gcc_unreachable ();
19356
19357 pop_access_scope (decl);
19358 }
19359
19360 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19361 substituted to get DECL. */
19362
19363 tree
19364 template_for_substitution (tree decl)
19365 {
19366 tree tmpl = DECL_TI_TEMPLATE (decl);
19367
19368 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19369 for the instantiation. This is not always the most general
19370 template. Consider, for example:
19371
19372 template <class T>
19373 struct S { template <class U> void f();
19374 template <> void f<int>(); };
19375
19376 and an instantiation of S<double>::f<int>. We want TD to be the
19377 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19378 while (/* An instantiation cannot have a definition, so we need a
19379 more general template. */
19380 DECL_TEMPLATE_INSTANTIATION (tmpl)
19381 /* We must also deal with friend templates. Given:
19382
19383 template <class T> struct S {
19384 template <class U> friend void f() {};
19385 };
19386
19387 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19388 so far as the language is concerned, but that's still
19389 where we get the pattern for the instantiation from. On
19390 other hand, if the definition comes outside the class, say:
19391
19392 template <class T> struct S {
19393 template <class U> friend void f();
19394 };
19395 template <class U> friend void f() {}
19396
19397 we don't need to look any further. That's what the check for
19398 DECL_INITIAL is for. */
19399 || (TREE_CODE (decl) == FUNCTION_DECL
19400 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19401 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19402 {
19403 /* The present template, TD, should not be a definition. If it
19404 were a definition, we should be using it! Note that we
19405 cannot restructure the loop to just keep going until we find
19406 a template with a definition, since that might go too far if
19407 a specialization was declared, but not defined. */
19408 gcc_assert (!VAR_P (decl)
19409 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19410
19411 /* Fetch the more general template. */
19412 tmpl = DECL_TI_TEMPLATE (tmpl);
19413 }
19414
19415 return tmpl;
19416 }
19417
19418 /* Returns true if we need to instantiate this template instance even if we
19419 know we aren't going to emit it.. */
19420
19421 bool
19422 always_instantiate_p (tree decl)
19423 {
19424 /* We always instantiate inline functions so that we can inline them. An
19425 explicit instantiation declaration prohibits implicit instantiation of
19426 non-inline functions. With high levels of optimization, we would
19427 normally inline non-inline functions -- but we're not allowed to do
19428 that for "extern template" functions. Therefore, we check
19429 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19430 return ((TREE_CODE (decl) == FUNCTION_DECL
19431 && (DECL_DECLARED_INLINE_P (decl)
19432 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19433 /* And we need to instantiate static data members so that
19434 their initializers are available in integral constant
19435 expressions. */
19436 || (VAR_P (decl)
19437 && decl_maybe_constant_var_p (decl)));
19438 }
19439
19440 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19441 instantiate it now, modifying TREE_TYPE (fn). */
19442
19443 void
19444 maybe_instantiate_noexcept (tree fn)
19445 {
19446 tree fntype, spec, noex, clone;
19447
19448 /* Don't instantiate a noexcept-specification from template context. */
19449 if (processing_template_decl)
19450 return;
19451
19452 if (DECL_CLONED_FUNCTION_P (fn))
19453 fn = DECL_CLONED_FUNCTION (fn);
19454 fntype = TREE_TYPE (fn);
19455 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19456
19457 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19458 return;
19459
19460 noex = TREE_PURPOSE (spec);
19461
19462 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19463 {
19464 if (push_tinst_level (fn))
19465 {
19466 push_access_scope (fn);
19467 push_deferring_access_checks (dk_no_deferred);
19468 input_location = DECL_SOURCE_LOCATION (fn);
19469 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19470 DEFERRED_NOEXCEPT_ARGS (noex),
19471 tf_warning_or_error, fn,
19472 /*function_p=*/false,
19473 /*integral_constant_expression_p=*/true);
19474 pop_deferring_access_checks ();
19475 pop_access_scope (fn);
19476 pop_tinst_level ();
19477 spec = build_noexcept_spec (noex, tf_warning_or_error);
19478 if (spec == error_mark_node)
19479 spec = noexcept_false_spec;
19480 }
19481 else
19482 spec = noexcept_false_spec;
19483 }
19484 else
19485 {
19486 /* This is an implicitly declared function, so NOEX is a list of
19487 other functions to evaluate and merge. */
19488 tree elt;
19489 spec = noexcept_true_spec;
19490 for (elt = noex; elt; elt = OVL_NEXT (elt))
19491 {
19492 tree fn = OVL_CURRENT (elt);
19493 tree subspec;
19494 maybe_instantiate_noexcept (fn);
19495 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19496 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19497 }
19498 }
19499
19500 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19501
19502 FOR_EACH_CLONE (clone, fn)
19503 {
19504 if (TREE_TYPE (clone) == fntype)
19505 TREE_TYPE (clone) = TREE_TYPE (fn);
19506 else
19507 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19508 }
19509 }
19510
19511 /* Produce the definition of D, a _DECL generated from a template. If
19512 DEFER_OK is nonzero, then we don't have to actually do the
19513 instantiation now; we just have to do it sometime. Normally it is
19514 an error if this is an explicit instantiation but D is undefined.
19515 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19516 explicitly instantiated class template. */
19517
19518 tree
19519 instantiate_decl (tree d, int defer_ok,
19520 bool expl_inst_class_mem_p)
19521 {
19522 tree tmpl = DECL_TI_TEMPLATE (d);
19523 tree gen_args;
19524 tree args;
19525 tree td;
19526 tree code_pattern;
19527 tree spec;
19528 tree gen_tmpl;
19529 bool pattern_defined;
19530 location_t saved_loc = input_location;
19531 int saved_unevaluated_operand = cp_unevaluated_operand;
19532 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19533 bool external_p;
19534 tree fn_context;
19535 bool nested;
19536
19537 /* This function should only be used to instantiate templates for
19538 functions and static member variables. */
19539 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19540
19541 /* Variables are never deferred; if instantiation is required, they
19542 are instantiated right away. That allows for better code in the
19543 case that an expression refers to the value of the variable --
19544 if the variable has a constant value the referring expression can
19545 take advantage of that fact. */
19546 if (VAR_P (d)
19547 || DECL_DECLARED_CONSTEXPR_P (d))
19548 defer_ok = 0;
19549
19550 /* Don't instantiate cloned functions. Instead, instantiate the
19551 functions they cloned. */
19552 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19553 d = DECL_CLONED_FUNCTION (d);
19554
19555 if (DECL_TEMPLATE_INSTANTIATED (d)
19556 || (TREE_CODE (d) == FUNCTION_DECL
19557 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19558 || DECL_TEMPLATE_SPECIALIZATION (d))
19559 /* D has already been instantiated or explicitly specialized, so
19560 there's nothing for us to do here.
19561
19562 It might seem reasonable to check whether or not D is an explicit
19563 instantiation, and, if so, stop here. But when an explicit
19564 instantiation is deferred until the end of the compilation,
19565 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19566 the instantiation. */
19567 return d;
19568
19569 /* Check to see whether we know that this template will be
19570 instantiated in some other file, as with "extern template"
19571 extension. */
19572 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19573
19574 /* In general, we do not instantiate such templates. */
19575 if (external_p && !always_instantiate_p (d))
19576 return d;
19577
19578 gen_tmpl = most_general_template (tmpl);
19579 gen_args = DECL_TI_ARGS (d);
19580
19581 if (tmpl != gen_tmpl)
19582 /* We should already have the extra args. */
19583 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19584 == TMPL_ARGS_DEPTH (gen_args));
19585 /* And what's in the hash table should match D. */
19586 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19587 || spec == NULL_TREE);
19588
19589 /* This needs to happen before any tsubsting. */
19590 if (! push_tinst_level (d))
19591 return d;
19592
19593 timevar_push (TV_TEMPLATE_INST);
19594
19595 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19596 for the instantiation. */
19597 td = template_for_substitution (d);
19598 code_pattern = DECL_TEMPLATE_RESULT (td);
19599
19600 /* We should never be trying to instantiate a member of a class
19601 template or partial specialization. */
19602 gcc_assert (d != code_pattern);
19603
19604 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19605 || DECL_TEMPLATE_SPECIALIZATION (td))
19606 /* In the case of a friend template whose definition is provided
19607 outside the class, we may have too many arguments. Drop the
19608 ones we don't need. The same is true for specializations. */
19609 args = get_innermost_template_args
19610 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19611 else
19612 args = gen_args;
19613
19614 if (TREE_CODE (d) == FUNCTION_DECL)
19615 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19616 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19617 else
19618 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19619
19620 /* We may be in the middle of deferred access check. Disable it now. */
19621 push_deferring_access_checks (dk_no_deferred);
19622
19623 /* Unless an explicit instantiation directive has already determined
19624 the linkage of D, remember that a definition is available for
19625 this entity. */
19626 if (pattern_defined
19627 && !DECL_INTERFACE_KNOWN (d)
19628 && !DECL_NOT_REALLY_EXTERN (d))
19629 mark_definable (d);
19630
19631 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19632 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19633 input_location = DECL_SOURCE_LOCATION (d);
19634
19635 /* If D is a member of an explicitly instantiated class template,
19636 and no definition is available, treat it like an implicit
19637 instantiation. */
19638 if (!pattern_defined && expl_inst_class_mem_p
19639 && DECL_EXPLICIT_INSTANTIATION (d))
19640 {
19641 /* Leave linkage flags alone on instantiations with anonymous
19642 visibility. */
19643 if (TREE_PUBLIC (d))
19644 {
19645 DECL_NOT_REALLY_EXTERN (d) = 0;
19646 DECL_INTERFACE_KNOWN (d) = 0;
19647 }
19648 SET_DECL_IMPLICIT_INSTANTIATION (d);
19649 }
19650
19651 if (TREE_CODE (d) == FUNCTION_DECL)
19652 maybe_instantiate_noexcept (d);
19653
19654 /* Defer all other templates, unless we have been explicitly
19655 forbidden from doing so. */
19656 if (/* If there is no definition, we cannot instantiate the
19657 template. */
19658 ! pattern_defined
19659 /* If it's OK to postpone instantiation, do so. */
19660 || defer_ok
19661 /* If this is a static data member that will be defined
19662 elsewhere, we don't want to instantiate the entire data
19663 member, but we do want to instantiate the initializer so that
19664 we can substitute that elsewhere. */
19665 || (external_p && VAR_P (d)))
19666 {
19667 /* The definition of the static data member is now required so
19668 we must substitute the initializer. */
19669 if (VAR_P (d)
19670 && !DECL_INITIAL (d)
19671 && DECL_INITIAL (code_pattern))
19672 {
19673 tree ns;
19674 tree init;
19675 bool const_init = false;
19676
19677 ns = decl_namespace_context (d);
19678 push_nested_namespace (ns);
19679 push_nested_class (DECL_CONTEXT (d));
19680 init = tsubst_expr (DECL_INITIAL (code_pattern),
19681 args,
19682 tf_warning_or_error, NULL_TREE,
19683 /*integral_constant_expression_p=*/false);
19684 /* Make sure the initializer is still constant, in case of
19685 circular dependency (template/instantiate6.C). */
19686 const_init
19687 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19688 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19689 /*asmspec_tree=*/NULL_TREE,
19690 LOOKUP_ONLYCONVERTING);
19691 pop_nested_class ();
19692 pop_nested_namespace (ns);
19693 }
19694
19695 /* We restore the source position here because it's used by
19696 add_pending_template. */
19697 input_location = saved_loc;
19698
19699 if (at_eof && !pattern_defined
19700 && DECL_EXPLICIT_INSTANTIATION (d)
19701 && DECL_NOT_REALLY_EXTERN (d))
19702 /* [temp.explicit]
19703
19704 The definition of a non-exported function template, a
19705 non-exported member function template, or a non-exported
19706 member function or static data member of a class template
19707 shall be present in every translation unit in which it is
19708 explicitly instantiated. */
19709 permerror (input_location, "explicit instantiation of %qD "
19710 "but no definition available", d);
19711
19712 /* If we're in unevaluated context, we just wanted to get the
19713 constant value; this isn't an odr use, so don't queue
19714 a full instantiation. */
19715 if (cp_unevaluated_operand != 0)
19716 goto out;
19717 /* ??? Historically, we have instantiated inline functions, even
19718 when marked as "extern template". */
19719 if (!(external_p && VAR_P (d)))
19720 add_pending_template (d);
19721 goto out;
19722 }
19723 /* Tell the repository that D is available in this translation unit
19724 -- and see if it is supposed to be instantiated here. */
19725 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19726 {
19727 /* In a PCH file, despite the fact that the repository hasn't
19728 requested instantiation in the PCH it is still possible that
19729 an instantiation will be required in a file that includes the
19730 PCH. */
19731 if (pch_file)
19732 add_pending_template (d);
19733 /* Instantiate inline functions so that the inliner can do its
19734 job, even though we'll not be emitting a copy of this
19735 function. */
19736 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19737 goto out;
19738 }
19739
19740 fn_context = decl_function_context (d);
19741 nested = (current_function_decl != NULL_TREE);
19742 if (!fn_context)
19743 push_to_top_level ();
19744 else
19745 {
19746 if (nested)
19747 push_function_context ();
19748 cp_unevaluated_operand = 0;
19749 c_inhibit_evaluation_warnings = 0;
19750 }
19751
19752 /* Mark D as instantiated so that recursive calls to
19753 instantiate_decl do not try to instantiate it again. */
19754 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19755
19756 /* Regenerate the declaration in case the template has been modified
19757 by a subsequent redeclaration. */
19758 regenerate_decl_from_template (d, td);
19759
19760 /* We already set the file and line above. Reset them now in case
19761 they changed as a result of calling regenerate_decl_from_template. */
19762 input_location = DECL_SOURCE_LOCATION (d);
19763
19764 if (VAR_P (d))
19765 {
19766 tree init;
19767 bool const_init = false;
19768
19769 /* Clear out DECL_RTL; whatever was there before may not be right
19770 since we've reset the type of the declaration. */
19771 SET_DECL_RTL (d, NULL);
19772 DECL_IN_AGGR_P (d) = 0;
19773
19774 /* The initializer is placed in DECL_INITIAL by
19775 regenerate_decl_from_template so we don't need to
19776 push/pop_access_scope again here. Pull it out so that
19777 cp_finish_decl can process it. */
19778 init = DECL_INITIAL (d);
19779 DECL_INITIAL (d) = NULL_TREE;
19780 DECL_INITIALIZED_P (d) = 0;
19781
19782 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19783 initializer. That function will defer actual emission until
19784 we have a chance to determine linkage. */
19785 DECL_EXTERNAL (d) = 0;
19786
19787 /* Enter the scope of D so that access-checking works correctly. */
19788 push_nested_class (DECL_CONTEXT (d));
19789 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19790 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19791 pop_nested_class ();
19792 }
19793 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19794 synthesize_method (d);
19795 else if (TREE_CODE (d) == FUNCTION_DECL)
19796 {
19797 struct pointer_map_t *saved_local_specializations;
19798 tree subst_decl;
19799 tree tmpl_parm;
19800 tree spec_parm;
19801 tree block = NULL_TREE;
19802
19803 /* Save away the current list, in case we are instantiating one
19804 template from within the body of another. */
19805 saved_local_specializations = local_specializations;
19806
19807 /* Set up the list of local specializations. */
19808 local_specializations = pointer_map_create ();
19809
19810 /* Set up context. */
19811 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19812 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19813 block = push_stmt_list ();
19814 else
19815 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19816
19817 /* Some typedefs referenced from within the template code need to be
19818 access checked at template instantiation time, i.e now. These
19819 types were added to the template at parsing time. Let's get those
19820 and perform the access checks then. */
19821 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19822 gen_args);
19823
19824 /* Create substitution entries for the parameters. */
19825 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19826 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19827 spec_parm = DECL_ARGUMENTS (d);
19828 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19829 {
19830 register_local_specialization (spec_parm, tmpl_parm);
19831 spec_parm = skip_artificial_parms_for (d, spec_parm);
19832 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19833 }
19834 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19835 {
19836 if (!DECL_PACK_P (tmpl_parm))
19837 {
19838 register_local_specialization (spec_parm, tmpl_parm);
19839 spec_parm = DECL_CHAIN (spec_parm);
19840 }
19841 else
19842 {
19843 /* Register the (value) argument pack as a specialization of
19844 TMPL_PARM, then move on. */
19845 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19846 register_local_specialization (argpack, tmpl_parm);
19847 }
19848 }
19849 gcc_assert (!spec_parm);
19850
19851 /* Substitute into the body of the function. */
19852 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19853 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19854 tf_warning_or_error, tmpl);
19855 else
19856 {
19857 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19858 tf_warning_or_error, tmpl,
19859 /*integral_constant_expression_p=*/false);
19860
19861 /* Set the current input_location to the end of the function
19862 so that finish_function knows where we are. */
19863 input_location
19864 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19865
19866 /* Remember if we saw an infinite loop in the template. */
19867 current_function_infinite_loop
19868 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
19869 }
19870
19871 /* We don't need the local specializations any more. */
19872 pointer_map_destroy (local_specializations);
19873 local_specializations = saved_local_specializations;
19874
19875 /* Finish the function. */
19876 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19877 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19878 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19879 else
19880 {
19881 d = finish_function (0);
19882 expand_or_defer_fn (d);
19883 }
19884
19885 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19886 cp_check_omp_declare_reduction (d);
19887 }
19888
19889 /* We're not deferring instantiation any more. */
19890 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
19891
19892 if (!fn_context)
19893 pop_from_top_level ();
19894 else if (nested)
19895 pop_function_context ();
19896
19897 out:
19898 input_location = saved_loc;
19899 cp_unevaluated_operand = saved_unevaluated_operand;
19900 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19901 pop_deferring_access_checks ();
19902 pop_tinst_level ();
19903
19904 timevar_pop (TV_TEMPLATE_INST);
19905
19906 return d;
19907 }
19908
19909 /* Run through the list of templates that we wish we could
19910 instantiate, and instantiate any we can. RETRIES is the
19911 number of times we retry pending template instantiation. */
19912
19913 void
19914 instantiate_pending_templates (int retries)
19915 {
19916 int reconsider;
19917 location_t saved_loc = input_location;
19918
19919 /* Instantiating templates may trigger vtable generation. This in turn
19920 may require further template instantiations. We place a limit here
19921 to avoid infinite loop. */
19922 if (pending_templates && retries >= max_tinst_depth)
19923 {
19924 tree decl = pending_templates->tinst->decl;
19925
19926 error ("template instantiation depth exceeds maximum of %d"
19927 " instantiating %q+D, possibly from virtual table generation"
19928 " (use -ftemplate-depth= to increase the maximum)",
19929 max_tinst_depth, decl);
19930 if (TREE_CODE (decl) == FUNCTION_DECL)
19931 /* Pretend that we defined it. */
19932 DECL_INITIAL (decl) = error_mark_node;
19933 return;
19934 }
19935
19936 do
19937 {
19938 struct pending_template **t = &pending_templates;
19939 struct pending_template *last = NULL;
19940 reconsider = 0;
19941 while (*t)
19942 {
19943 tree instantiation = reopen_tinst_level ((*t)->tinst);
19944 bool complete = false;
19945
19946 if (TYPE_P (instantiation))
19947 {
19948 tree fn;
19949
19950 if (!COMPLETE_TYPE_P (instantiation))
19951 {
19952 instantiate_class_template (instantiation);
19953 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
19954 for (fn = TYPE_METHODS (instantiation);
19955 fn;
19956 fn = TREE_CHAIN (fn))
19957 if (! DECL_ARTIFICIAL (fn))
19958 instantiate_decl (fn,
19959 /*defer_ok=*/0,
19960 /*expl_inst_class_mem_p=*/false);
19961 if (COMPLETE_TYPE_P (instantiation))
19962 reconsider = 1;
19963 }
19964
19965 complete = COMPLETE_TYPE_P (instantiation);
19966 }
19967 else
19968 {
19969 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
19970 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
19971 {
19972 instantiation
19973 = instantiate_decl (instantiation,
19974 /*defer_ok=*/0,
19975 /*expl_inst_class_mem_p=*/false);
19976 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
19977 reconsider = 1;
19978 }
19979
19980 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
19981 || DECL_TEMPLATE_INSTANTIATED (instantiation));
19982 }
19983
19984 if (complete)
19985 /* If INSTANTIATION has been instantiated, then we don't
19986 need to consider it again in the future. */
19987 *t = (*t)->next;
19988 else
19989 {
19990 last = *t;
19991 t = &(*t)->next;
19992 }
19993 tinst_depth = 0;
19994 current_tinst_level = NULL;
19995 }
19996 last_pending_template = last;
19997 }
19998 while (reconsider);
19999
20000 input_location = saved_loc;
20001 }
20002
20003 /* Substitute ARGVEC into T, which is a list of initializers for
20004 either base class or a non-static data member. The TREE_PURPOSEs
20005 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20006 instantiate_decl. */
20007
20008 static tree
20009 tsubst_initializer_list (tree t, tree argvec)
20010 {
20011 tree inits = NULL_TREE;
20012
20013 for (; t; t = TREE_CHAIN (t))
20014 {
20015 tree decl;
20016 tree init;
20017 tree expanded_bases = NULL_TREE;
20018 tree expanded_arguments = NULL_TREE;
20019 int i, len = 1;
20020
20021 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
20022 {
20023 tree expr;
20024 tree arg;
20025
20026 /* Expand the base class expansion type into separate base
20027 classes. */
20028 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
20029 tf_warning_or_error,
20030 NULL_TREE);
20031 if (expanded_bases == error_mark_node)
20032 continue;
20033
20034 /* We'll be building separate TREE_LISTs of arguments for
20035 each base. */
20036 len = TREE_VEC_LENGTH (expanded_bases);
20037 expanded_arguments = make_tree_vec (len);
20038 for (i = 0; i < len; i++)
20039 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
20040
20041 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20042 expand each argument in the TREE_VALUE of t. */
20043 expr = make_node (EXPR_PACK_EXPANSION);
20044 PACK_EXPANSION_LOCAL_P (expr) = true;
20045 PACK_EXPANSION_PARAMETER_PACKS (expr) =
20046 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
20047
20048 if (TREE_VALUE (t) == void_type_node)
20049 /* VOID_TYPE_NODE is used to indicate
20050 value-initialization. */
20051 {
20052 for (i = 0; i < len; i++)
20053 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
20054 }
20055 else
20056 {
20057 /* Substitute parameter packs into each argument in the
20058 TREE_LIST. */
20059 in_base_initializer = 1;
20060 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
20061 {
20062 tree expanded_exprs;
20063
20064 /* Expand the argument. */
20065 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
20066 expanded_exprs
20067 = tsubst_pack_expansion (expr, argvec,
20068 tf_warning_or_error,
20069 NULL_TREE);
20070 if (expanded_exprs == error_mark_node)
20071 continue;
20072
20073 /* Prepend each of the expanded expressions to the
20074 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20075 for (i = 0; i < len; i++)
20076 {
20077 TREE_VEC_ELT (expanded_arguments, i) =
20078 tree_cons (NULL_TREE,
20079 TREE_VEC_ELT (expanded_exprs, i),
20080 TREE_VEC_ELT (expanded_arguments, i));
20081 }
20082 }
20083 in_base_initializer = 0;
20084
20085 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20086 since we built them backwards. */
20087 for (i = 0; i < len; i++)
20088 {
20089 TREE_VEC_ELT (expanded_arguments, i) =
20090 nreverse (TREE_VEC_ELT (expanded_arguments, i));
20091 }
20092 }
20093 }
20094
20095 for (i = 0; i < len; ++i)
20096 {
20097 if (expanded_bases)
20098 {
20099 decl = TREE_VEC_ELT (expanded_bases, i);
20100 decl = expand_member_init (decl);
20101 init = TREE_VEC_ELT (expanded_arguments, i);
20102 }
20103 else
20104 {
20105 tree tmp;
20106 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
20107 tf_warning_or_error, NULL_TREE);
20108
20109 decl = expand_member_init (decl);
20110 if (decl && !DECL_P (decl))
20111 in_base_initializer = 1;
20112
20113 init = TREE_VALUE (t);
20114 tmp = init;
20115 if (init != void_type_node)
20116 init = tsubst_expr (init, argvec,
20117 tf_warning_or_error, NULL_TREE,
20118 /*integral_constant_expression_p=*/false);
20119 if (init == NULL_TREE && tmp != NULL_TREE)
20120 /* If we had an initializer but it instantiated to nothing,
20121 value-initialize the object. This will only occur when
20122 the initializer was a pack expansion where the parameter
20123 packs used in that expansion were of length zero. */
20124 init = void_type_node;
20125 in_base_initializer = 0;
20126 }
20127
20128 if (decl)
20129 {
20130 init = build_tree_list (decl, init);
20131 TREE_CHAIN (init) = inits;
20132 inits = init;
20133 }
20134 }
20135 }
20136 return inits;
20137 }
20138
20139 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20140
20141 static void
20142 set_current_access_from_decl (tree decl)
20143 {
20144 if (TREE_PRIVATE (decl))
20145 current_access_specifier = access_private_node;
20146 else if (TREE_PROTECTED (decl))
20147 current_access_specifier = access_protected_node;
20148 else
20149 current_access_specifier = access_public_node;
20150 }
20151
20152 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20153 is the instantiation (which should have been created with
20154 start_enum) and ARGS are the template arguments to use. */
20155
20156 static void
20157 tsubst_enum (tree tag, tree newtag, tree args)
20158 {
20159 tree e;
20160
20161 if (SCOPED_ENUM_P (newtag))
20162 begin_scope (sk_scoped_enum, newtag);
20163
20164 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
20165 {
20166 tree value;
20167 tree decl;
20168
20169 decl = TREE_VALUE (e);
20170 /* Note that in a template enum, the TREE_VALUE is the
20171 CONST_DECL, not the corresponding INTEGER_CST. */
20172 value = tsubst_expr (DECL_INITIAL (decl),
20173 args, tf_warning_or_error, NULL_TREE,
20174 /*integral_constant_expression_p=*/true);
20175
20176 /* Give this enumeration constant the correct access. */
20177 set_current_access_from_decl (decl);
20178
20179 /* Actually build the enumerator itself. */
20180 build_enumerator
20181 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
20182 }
20183
20184 if (SCOPED_ENUM_P (newtag))
20185 finish_scope ();
20186
20187 finish_enum_value_list (newtag);
20188 finish_enum (newtag);
20189
20190 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
20191 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
20192 }
20193
20194 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20195 its type -- but without substituting the innermost set of template
20196 arguments. So, innermost set of template parameters will appear in
20197 the type. */
20198
20199 tree
20200 get_mostly_instantiated_function_type (tree decl)
20201 {
20202 tree fn_type;
20203 tree tmpl;
20204 tree targs;
20205 tree tparms;
20206 int parm_depth;
20207
20208 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
20209 targs = DECL_TI_ARGS (decl);
20210 tparms = DECL_TEMPLATE_PARMS (tmpl);
20211 parm_depth = TMPL_PARMS_DEPTH (tparms);
20212
20213 /* There should be as many levels of arguments as there are levels
20214 of parameters. */
20215 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
20216
20217 fn_type = TREE_TYPE (tmpl);
20218
20219 if (parm_depth == 1)
20220 /* No substitution is necessary. */
20221 ;
20222 else
20223 {
20224 int i;
20225 tree partial_args;
20226
20227 /* Replace the innermost level of the TARGS with NULL_TREEs to
20228 let tsubst know not to substitute for those parameters. */
20229 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20230 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20231 SET_TMPL_ARGS_LEVEL (partial_args, i,
20232 TMPL_ARGS_LEVEL (targs, i));
20233 SET_TMPL_ARGS_LEVEL (partial_args,
20234 TMPL_ARGS_DEPTH (targs),
20235 make_tree_vec (DECL_NTPARMS (tmpl)));
20236
20237 /* Make sure that we can see identifiers, and compute access
20238 correctly. */
20239 push_access_scope (decl);
20240
20241 ++processing_template_decl;
20242 /* Now, do the (partial) substitution to figure out the
20243 appropriate function type. */
20244 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20245 --processing_template_decl;
20246
20247 /* Substitute into the template parameters to obtain the real
20248 innermost set of parameters. This step is important if the
20249 innermost set of template parameters contains value
20250 parameters whose types depend on outer template parameters. */
20251 TREE_VEC_LENGTH (partial_args)--;
20252 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20253
20254 pop_access_scope (decl);
20255 }
20256
20257 return fn_type;
20258 }
20259
20260 /* Return truthvalue if we're processing a template different from
20261 the last one involved in diagnostics. */
20262 int
20263 problematic_instantiation_changed (void)
20264 {
20265 return current_tinst_level != last_error_tinst_level;
20266 }
20267
20268 /* Remember current template involved in diagnostics. */
20269 void
20270 record_last_problematic_instantiation (void)
20271 {
20272 last_error_tinst_level = current_tinst_level;
20273 }
20274
20275 struct tinst_level *
20276 current_instantiation (void)
20277 {
20278 return current_tinst_level;
20279 }
20280
20281 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20282 type. Return zero for ok, nonzero for disallowed. Issue error and
20283 warning messages under control of COMPLAIN. */
20284
20285 static int
20286 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20287 {
20288 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20289 return 0;
20290 else if (POINTER_TYPE_P (type))
20291 return 0;
20292 else if (TYPE_PTRMEM_P (type))
20293 return 0;
20294 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20295 return 0;
20296 else if (TREE_CODE (type) == TYPENAME_TYPE)
20297 return 0;
20298 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20299 return 0;
20300 else if (TREE_CODE (type) == NULLPTR_TYPE)
20301 return 0;
20302
20303 if (complain & tf_error)
20304 {
20305 if (type == error_mark_node)
20306 inform (input_location, "invalid template non-type parameter");
20307 else
20308 error ("%q#T is not a valid type for a template non-type parameter",
20309 type);
20310 }
20311 return 1;
20312 }
20313
20314 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20315 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20316
20317 static bool
20318 dependent_type_p_r (tree type)
20319 {
20320 tree scope;
20321
20322 /* [temp.dep.type]
20323
20324 A type is dependent if it is:
20325
20326 -- a template parameter. Template template parameters are types
20327 for us (since TYPE_P holds true for them) so we handle
20328 them here. */
20329 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20330 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20331 return true;
20332 /* -- a qualified-id with a nested-name-specifier which contains a
20333 class-name that names a dependent type or whose unqualified-id
20334 names a dependent type. */
20335 if (TREE_CODE (type) == TYPENAME_TYPE)
20336 return true;
20337 /* -- a cv-qualified type where the cv-unqualified type is
20338 dependent. */
20339 type = TYPE_MAIN_VARIANT (type);
20340 /* -- a compound type constructed from any dependent type. */
20341 if (TYPE_PTRMEM_P (type))
20342 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20343 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20344 (type)));
20345 else if (TYPE_PTR_P (type)
20346 || TREE_CODE (type) == REFERENCE_TYPE)
20347 return dependent_type_p (TREE_TYPE (type));
20348 else if (TREE_CODE (type) == FUNCTION_TYPE
20349 || TREE_CODE (type) == METHOD_TYPE)
20350 {
20351 tree arg_type;
20352
20353 if (dependent_type_p (TREE_TYPE (type)))
20354 return true;
20355 for (arg_type = TYPE_ARG_TYPES (type);
20356 arg_type;
20357 arg_type = TREE_CHAIN (arg_type))
20358 if (dependent_type_p (TREE_VALUE (arg_type)))
20359 return true;
20360 return false;
20361 }
20362 /* -- an array type constructed from any dependent type or whose
20363 size is specified by a constant expression that is
20364 value-dependent.
20365
20366 We checked for type- and value-dependence of the bounds in
20367 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20368 if (TREE_CODE (type) == ARRAY_TYPE)
20369 {
20370 if (TYPE_DOMAIN (type)
20371 && dependent_type_p (TYPE_DOMAIN (type)))
20372 return true;
20373 return dependent_type_p (TREE_TYPE (type));
20374 }
20375
20376 /* -- a template-id in which either the template name is a template
20377 parameter ... */
20378 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20379 return true;
20380 /* ... or any of the template arguments is a dependent type or
20381 an expression that is type-dependent or value-dependent. */
20382 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20383 && (any_dependent_template_arguments_p
20384 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20385 return true;
20386
20387 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20388 dependent; if the argument of the `typeof' expression is not
20389 type-dependent, then it should already been have resolved. */
20390 if (TREE_CODE (type) == TYPEOF_TYPE
20391 || TREE_CODE (type) == DECLTYPE_TYPE
20392 || TREE_CODE (type) == UNDERLYING_TYPE)
20393 return true;
20394
20395 /* A template argument pack is dependent if any of its packed
20396 arguments are. */
20397 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20398 {
20399 tree args = ARGUMENT_PACK_ARGS (type);
20400 int i, len = TREE_VEC_LENGTH (args);
20401 for (i = 0; i < len; ++i)
20402 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20403 return true;
20404 }
20405
20406 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20407 be template parameters. */
20408 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20409 return true;
20410
20411 /* The standard does not specifically mention types that are local
20412 to template functions or local classes, but they should be
20413 considered dependent too. For example:
20414
20415 template <int I> void f() {
20416 enum E { a = I };
20417 S<sizeof (E)> s;
20418 }
20419
20420 The size of `E' cannot be known until the value of `I' has been
20421 determined. Therefore, `E' must be considered dependent. */
20422 scope = TYPE_CONTEXT (type);
20423 if (scope && TYPE_P (scope))
20424 return dependent_type_p (scope);
20425 /* Don't use type_dependent_expression_p here, as it can lead
20426 to infinite recursion trying to determine whether a lambda
20427 nested in a lambda is dependent (c++/47687). */
20428 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20429 && DECL_LANG_SPECIFIC (scope)
20430 && DECL_TEMPLATE_INFO (scope)
20431 && (any_dependent_template_arguments_p
20432 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20433 return true;
20434
20435 /* Other types are non-dependent. */
20436 return false;
20437 }
20438
20439 /* Returns TRUE if TYPE is dependent, in the sense of
20440 [temp.dep.type]. Note that a NULL type is considered dependent. */
20441
20442 bool
20443 dependent_type_p (tree type)
20444 {
20445 /* If there are no template parameters in scope, then there can't be
20446 any dependent types. */
20447 if (!processing_template_decl)
20448 {
20449 /* If we are not processing a template, then nobody should be
20450 providing us with a dependent type. */
20451 gcc_assert (type);
20452 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20453 return false;
20454 }
20455
20456 /* If the type is NULL, we have not computed a type for the entity
20457 in question; in that case, the type is dependent. */
20458 if (!type)
20459 return true;
20460
20461 /* Erroneous types can be considered non-dependent. */
20462 if (type == error_mark_node)
20463 return false;
20464
20465 /* If we have not already computed the appropriate value for TYPE,
20466 do so now. */
20467 if (!TYPE_DEPENDENT_P_VALID (type))
20468 {
20469 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20470 TYPE_DEPENDENT_P_VALID (type) = 1;
20471 }
20472
20473 return TYPE_DEPENDENT_P (type);
20474 }
20475
20476 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20477 lookup. In other words, a dependent type that is not the current
20478 instantiation. */
20479
20480 bool
20481 dependent_scope_p (tree scope)
20482 {
20483 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20484 && !currently_open_class (scope));
20485 }
20486
20487 /* T is a SCOPE_REF; return whether we need to consider it
20488 instantiation-dependent so that we can check access at instantiation
20489 time even though we know which member it resolves to. */
20490
20491 static bool
20492 instantiation_dependent_scope_ref_p (tree t)
20493 {
20494 if (DECL_P (TREE_OPERAND (t, 1))
20495 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20496 && accessible_in_template_p (TREE_OPERAND (t, 0),
20497 TREE_OPERAND (t, 1)))
20498 return false;
20499 else
20500 return true;
20501 }
20502
20503 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20504 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20505 expression. */
20506
20507 /* Note that this predicate is not appropriate for general expressions;
20508 only constant expressions (that satisfy potential_constant_expression)
20509 can be tested for value dependence. */
20510
20511 bool
20512 value_dependent_expression_p (tree expression)
20513 {
20514 if (!processing_template_decl)
20515 return false;
20516
20517 /* A name declared with a dependent type. */
20518 if (DECL_P (expression) && type_dependent_expression_p (expression))
20519 return true;
20520
20521 switch (TREE_CODE (expression))
20522 {
20523 case IDENTIFIER_NODE:
20524 /* A name that has not been looked up -- must be dependent. */
20525 return true;
20526
20527 case TEMPLATE_PARM_INDEX:
20528 /* A non-type template parm. */
20529 return true;
20530
20531 case CONST_DECL:
20532 /* A non-type template parm. */
20533 if (DECL_TEMPLATE_PARM_P (expression))
20534 return true;
20535 return value_dependent_expression_p (DECL_INITIAL (expression));
20536
20537 case VAR_DECL:
20538 /* A constant with literal type and is initialized
20539 with an expression that is value-dependent.
20540
20541 Note that a non-dependent parenthesized initializer will have
20542 already been replaced with its constant value, so if we see
20543 a TREE_LIST it must be dependent. */
20544 if (DECL_INITIAL (expression)
20545 && decl_constant_var_p (expression)
20546 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20547 || value_dependent_expression_p (DECL_INITIAL (expression))))
20548 return true;
20549 return false;
20550
20551 case DYNAMIC_CAST_EXPR:
20552 case STATIC_CAST_EXPR:
20553 case CONST_CAST_EXPR:
20554 case REINTERPRET_CAST_EXPR:
20555 case CAST_EXPR:
20556 /* These expressions are value-dependent if the type to which
20557 the cast occurs is dependent or the expression being casted
20558 is value-dependent. */
20559 {
20560 tree type = TREE_TYPE (expression);
20561
20562 if (dependent_type_p (type))
20563 return true;
20564
20565 /* A functional cast has a list of operands. */
20566 expression = TREE_OPERAND (expression, 0);
20567 if (!expression)
20568 {
20569 /* If there are no operands, it must be an expression such
20570 as "int()". This should not happen for aggregate types
20571 because it would form non-constant expressions. */
20572 gcc_assert (cxx_dialect >= cxx11
20573 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20574
20575 return false;
20576 }
20577
20578 if (TREE_CODE (expression) == TREE_LIST)
20579 return any_value_dependent_elements_p (expression);
20580
20581 return value_dependent_expression_p (expression);
20582 }
20583
20584 case SIZEOF_EXPR:
20585 if (SIZEOF_EXPR_TYPE_P (expression))
20586 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20587 /* FALLTHRU */
20588 case ALIGNOF_EXPR:
20589 case TYPEID_EXPR:
20590 /* A `sizeof' expression is value-dependent if the operand is
20591 type-dependent or is a pack expansion. */
20592 expression = TREE_OPERAND (expression, 0);
20593 if (PACK_EXPANSION_P (expression))
20594 return true;
20595 else if (TYPE_P (expression))
20596 return dependent_type_p (expression);
20597 return instantiation_dependent_expression_p (expression);
20598
20599 case AT_ENCODE_EXPR:
20600 /* An 'encode' expression is value-dependent if the operand is
20601 type-dependent. */
20602 expression = TREE_OPERAND (expression, 0);
20603 return dependent_type_p (expression);
20604
20605 case NOEXCEPT_EXPR:
20606 expression = TREE_OPERAND (expression, 0);
20607 return instantiation_dependent_expression_p (expression);
20608
20609 case SCOPE_REF:
20610 /* All instantiation-dependent expressions should also be considered
20611 value-dependent. */
20612 return instantiation_dependent_scope_ref_p (expression);
20613
20614 case COMPONENT_REF:
20615 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20616 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20617
20618 case NONTYPE_ARGUMENT_PACK:
20619 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20620 is value-dependent. */
20621 {
20622 tree values = ARGUMENT_PACK_ARGS (expression);
20623 int i, len = TREE_VEC_LENGTH (values);
20624
20625 for (i = 0; i < len; ++i)
20626 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20627 return true;
20628
20629 return false;
20630 }
20631
20632 case TRAIT_EXPR:
20633 {
20634 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20635 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20636 || (type2 ? dependent_type_p (type2) : false));
20637 }
20638
20639 case MODOP_EXPR:
20640 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20641 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20642
20643 case ARRAY_REF:
20644 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20645 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20646
20647 case ADDR_EXPR:
20648 {
20649 tree op = TREE_OPERAND (expression, 0);
20650 return (value_dependent_expression_p (op)
20651 || has_value_dependent_address (op));
20652 }
20653
20654 case CALL_EXPR:
20655 {
20656 tree fn = get_callee_fndecl (expression);
20657 int i, nargs;
20658 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20659 return true;
20660 nargs = call_expr_nargs (expression);
20661 for (i = 0; i < nargs; ++i)
20662 {
20663 tree op = CALL_EXPR_ARG (expression, i);
20664 /* In a call to a constexpr member function, look through the
20665 implicit ADDR_EXPR on the object argument so that it doesn't
20666 cause the call to be considered value-dependent. We also
20667 look through it in potential_constant_expression. */
20668 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20669 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20670 && TREE_CODE (op) == ADDR_EXPR)
20671 op = TREE_OPERAND (op, 0);
20672 if (value_dependent_expression_p (op))
20673 return true;
20674 }
20675 return false;
20676 }
20677
20678 case TEMPLATE_ID_EXPR:
20679 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20680 type-dependent. */
20681 return type_dependent_expression_p (expression);
20682
20683 case CONSTRUCTOR:
20684 {
20685 unsigned ix;
20686 tree val;
20687 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20688 if (value_dependent_expression_p (val))
20689 return true;
20690 return false;
20691 }
20692
20693 case STMT_EXPR:
20694 /* Treat a GNU statement expression as dependent to avoid crashing
20695 under fold_non_dependent_expr; it can't be constant. */
20696 return true;
20697
20698 default:
20699 /* A constant expression is value-dependent if any subexpression is
20700 value-dependent. */
20701 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20702 {
20703 case tcc_reference:
20704 case tcc_unary:
20705 case tcc_comparison:
20706 case tcc_binary:
20707 case tcc_expression:
20708 case tcc_vl_exp:
20709 {
20710 int i, len = cp_tree_operand_length (expression);
20711
20712 for (i = 0; i < len; i++)
20713 {
20714 tree t = TREE_OPERAND (expression, i);
20715
20716 /* In some cases, some of the operands may be missing.l
20717 (For example, in the case of PREDECREMENT_EXPR, the
20718 amount to increment by may be missing.) That doesn't
20719 make the expression dependent. */
20720 if (t && value_dependent_expression_p (t))
20721 return true;
20722 }
20723 }
20724 break;
20725 default:
20726 break;
20727 }
20728 break;
20729 }
20730
20731 /* The expression is not value-dependent. */
20732 return false;
20733 }
20734
20735 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20736 [temp.dep.expr]. Note that an expression with no type is
20737 considered dependent. Other parts of the compiler arrange for an
20738 expression with type-dependent subexpressions to have no type, so
20739 this function doesn't have to be fully recursive. */
20740
20741 bool
20742 type_dependent_expression_p (tree expression)
20743 {
20744 if (!processing_template_decl)
20745 return false;
20746
20747 if (expression == NULL_TREE || expression == error_mark_node)
20748 return false;
20749
20750 /* An unresolved name is always dependent. */
20751 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20752 return true;
20753
20754 /* Some expression forms are never type-dependent. */
20755 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20756 || TREE_CODE (expression) == SIZEOF_EXPR
20757 || TREE_CODE (expression) == ALIGNOF_EXPR
20758 || TREE_CODE (expression) == AT_ENCODE_EXPR
20759 || TREE_CODE (expression) == NOEXCEPT_EXPR
20760 || TREE_CODE (expression) == TRAIT_EXPR
20761 || TREE_CODE (expression) == TYPEID_EXPR
20762 || TREE_CODE (expression) == DELETE_EXPR
20763 || TREE_CODE (expression) == VEC_DELETE_EXPR
20764 || TREE_CODE (expression) == THROW_EXPR)
20765 return false;
20766
20767 /* The types of these expressions depends only on the type to which
20768 the cast occurs. */
20769 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20770 || TREE_CODE (expression) == STATIC_CAST_EXPR
20771 || TREE_CODE (expression) == CONST_CAST_EXPR
20772 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20773 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20774 || TREE_CODE (expression) == CAST_EXPR)
20775 return dependent_type_p (TREE_TYPE (expression));
20776
20777 /* The types of these expressions depends only on the type created
20778 by the expression. */
20779 if (TREE_CODE (expression) == NEW_EXPR
20780 || TREE_CODE (expression) == VEC_NEW_EXPR)
20781 {
20782 /* For NEW_EXPR tree nodes created inside a template, either
20783 the object type itself or a TREE_LIST may appear as the
20784 operand 1. */
20785 tree type = TREE_OPERAND (expression, 1);
20786 if (TREE_CODE (type) == TREE_LIST)
20787 /* This is an array type. We need to check array dimensions
20788 as well. */
20789 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20790 || value_dependent_expression_p
20791 (TREE_OPERAND (TREE_VALUE (type), 1));
20792 else
20793 return dependent_type_p (type);
20794 }
20795
20796 if (TREE_CODE (expression) == SCOPE_REF)
20797 {
20798 tree scope = TREE_OPERAND (expression, 0);
20799 tree name = TREE_OPERAND (expression, 1);
20800
20801 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20802 contains an identifier associated by name lookup with one or more
20803 declarations declared with a dependent type, or...a
20804 nested-name-specifier or qualified-id that names a member of an
20805 unknown specialization. */
20806 return (type_dependent_expression_p (name)
20807 || dependent_scope_p (scope));
20808 }
20809
20810 if (TREE_CODE (expression) == FUNCTION_DECL
20811 && DECL_LANG_SPECIFIC (expression)
20812 && DECL_TEMPLATE_INFO (expression)
20813 && (any_dependent_template_arguments_p
20814 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20815 return true;
20816
20817 if (TREE_CODE (expression) == TEMPLATE_DECL
20818 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20819 return false;
20820
20821 if (TREE_CODE (expression) == STMT_EXPR)
20822 expression = stmt_expr_value_expr (expression);
20823
20824 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20825 {
20826 tree elt;
20827 unsigned i;
20828
20829 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20830 {
20831 if (type_dependent_expression_p (elt))
20832 return true;
20833 }
20834 return false;
20835 }
20836
20837 /* A static data member of the current instantiation with incomplete
20838 array type is type-dependent, as the definition and specializations
20839 can have different bounds. */
20840 if (VAR_P (expression)
20841 && DECL_CLASS_SCOPE_P (expression)
20842 && dependent_type_p (DECL_CONTEXT (expression))
20843 && VAR_HAD_UNKNOWN_BOUND (expression))
20844 return true;
20845
20846 /* An array of unknown bound depending on a variadic parameter, eg:
20847
20848 template<typename... Args>
20849 void foo (Args... args)
20850 {
20851 int arr[] = { args... };
20852 }
20853
20854 template<int... vals>
20855 void bar ()
20856 {
20857 int arr[] = { vals... };
20858 }
20859
20860 If the array has no length and has an initializer, it must be that
20861 we couldn't determine its length in cp_complete_array_type because
20862 it is dependent. */
20863 if (VAR_P (expression)
20864 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20865 && !TYPE_DOMAIN (TREE_TYPE (expression))
20866 && DECL_INITIAL (expression))
20867 return true;
20868
20869 if (TREE_TYPE (expression) == unknown_type_node)
20870 {
20871 if (TREE_CODE (expression) == ADDR_EXPR)
20872 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20873 if (TREE_CODE (expression) == COMPONENT_REF
20874 || TREE_CODE (expression) == OFFSET_REF)
20875 {
20876 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20877 return true;
20878 expression = TREE_OPERAND (expression, 1);
20879 if (identifier_p (expression))
20880 return false;
20881 }
20882 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20883 if (TREE_CODE (expression) == SCOPE_REF)
20884 return false;
20885
20886 /* Always dependent, on the number of arguments if nothing else. */
20887 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
20888 return true;
20889
20890 if (BASELINK_P (expression))
20891 expression = BASELINK_FUNCTIONS (expression);
20892
20893 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
20894 {
20895 if (any_dependent_template_arguments_p
20896 (TREE_OPERAND (expression, 1)))
20897 return true;
20898 expression = TREE_OPERAND (expression, 0);
20899 }
20900 gcc_assert (TREE_CODE (expression) == OVERLOAD
20901 || TREE_CODE (expression) == FUNCTION_DECL);
20902
20903 while (expression)
20904 {
20905 if (type_dependent_expression_p (OVL_CURRENT (expression)))
20906 return true;
20907 expression = OVL_NEXT (expression);
20908 }
20909 return false;
20910 }
20911
20912 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
20913
20914 return (dependent_type_p (TREE_TYPE (expression)));
20915 }
20916
20917 /* walk_tree callback function for instantiation_dependent_expression_p,
20918 below. Returns non-zero if a dependent subexpression is found. */
20919
20920 static tree
20921 instantiation_dependent_r (tree *tp, int *walk_subtrees,
20922 void * /*data*/)
20923 {
20924 if (TYPE_P (*tp))
20925 {
20926 /* We don't have to worry about decltype currently because decltype
20927 of an instantiation-dependent expr is a dependent type. This
20928 might change depending on the resolution of DR 1172. */
20929 *walk_subtrees = false;
20930 return NULL_TREE;
20931 }
20932 enum tree_code code = TREE_CODE (*tp);
20933 switch (code)
20934 {
20935 /* Don't treat an argument list as dependent just because it has no
20936 TREE_TYPE. */
20937 case TREE_LIST:
20938 case TREE_VEC:
20939 return NULL_TREE;
20940
20941 case VAR_DECL:
20942 case CONST_DECL:
20943 /* A constant with a dependent initializer is dependent. */
20944 if (value_dependent_expression_p (*tp))
20945 return *tp;
20946 break;
20947
20948 case TEMPLATE_PARM_INDEX:
20949 return *tp;
20950
20951 /* Handle expressions with type operands. */
20952 case SIZEOF_EXPR:
20953 case ALIGNOF_EXPR:
20954 case TYPEID_EXPR:
20955 case AT_ENCODE_EXPR:
20956 {
20957 tree op = TREE_OPERAND (*tp, 0);
20958 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
20959 op = TREE_TYPE (op);
20960 if (TYPE_P (op))
20961 {
20962 if (dependent_type_p (op))
20963 return *tp;
20964 else
20965 {
20966 *walk_subtrees = false;
20967 return NULL_TREE;
20968 }
20969 }
20970 break;
20971 }
20972
20973 case TRAIT_EXPR:
20974 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
20975 || (TRAIT_EXPR_TYPE2 (*tp)
20976 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
20977 return *tp;
20978 *walk_subtrees = false;
20979 return NULL_TREE;
20980
20981 case COMPONENT_REF:
20982 if (identifier_p (TREE_OPERAND (*tp, 1)))
20983 /* In a template, finish_class_member_access_expr creates a
20984 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20985 type-dependent, so that we can check access control at
20986 instantiation time (PR 42277). See also Core issue 1273. */
20987 return *tp;
20988 break;
20989
20990 case SCOPE_REF:
20991 if (instantiation_dependent_scope_ref_p (*tp))
20992 return *tp;
20993 else
20994 break;
20995
20996 /* Treat statement-expressions as dependent. */
20997 case BIND_EXPR:
20998 return *tp;
20999
21000 default:
21001 break;
21002 }
21003
21004 if (type_dependent_expression_p (*tp))
21005 return *tp;
21006 else
21007 return NULL_TREE;
21008 }
21009
21010 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21011 sense defined by the ABI:
21012
21013 "An expression is instantiation-dependent if it is type-dependent
21014 or value-dependent, or it has a subexpression that is type-dependent
21015 or value-dependent." */
21016
21017 bool
21018 instantiation_dependent_expression_p (tree expression)
21019 {
21020 tree result;
21021
21022 if (!processing_template_decl)
21023 return false;
21024
21025 if (expression == error_mark_node)
21026 return false;
21027
21028 result = cp_walk_tree_without_duplicates (&expression,
21029 instantiation_dependent_r, NULL);
21030 return result != NULL_TREE;
21031 }
21032
21033 /* Like type_dependent_expression_p, but it also works while not processing
21034 a template definition, i.e. during substitution or mangling. */
21035
21036 bool
21037 type_dependent_expression_p_push (tree expr)
21038 {
21039 bool b;
21040 ++processing_template_decl;
21041 b = type_dependent_expression_p (expr);
21042 --processing_template_decl;
21043 return b;
21044 }
21045
21046 /* Returns TRUE if ARGS contains a type-dependent expression. */
21047
21048 bool
21049 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
21050 {
21051 unsigned int i;
21052 tree arg;
21053
21054 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
21055 {
21056 if (type_dependent_expression_p (arg))
21057 return true;
21058 }
21059 return false;
21060 }
21061
21062 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21063 expressions) contains any type-dependent expressions. */
21064
21065 bool
21066 any_type_dependent_elements_p (const_tree list)
21067 {
21068 for (; list; list = TREE_CHAIN (list))
21069 if (type_dependent_expression_p (TREE_VALUE (list)))
21070 return true;
21071
21072 return false;
21073 }
21074
21075 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21076 expressions) contains any value-dependent expressions. */
21077
21078 bool
21079 any_value_dependent_elements_p (const_tree list)
21080 {
21081 for (; list; list = TREE_CHAIN (list))
21082 if (value_dependent_expression_p (TREE_VALUE (list)))
21083 return true;
21084
21085 return false;
21086 }
21087
21088 /* Returns TRUE if the ARG (a template argument) is dependent. */
21089
21090 bool
21091 dependent_template_arg_p (tree arg)
21092 {
21093 if (!processing_template_decl)
21094 return false;
21095
21096 /* Assume a template argument that was wrongly written by the user
21097 is dependent. This is consistent with what
21098 any_dependent_template_arguments_p [that calls this function]
21099 does. */
21100 if (!arg || arg == error_mark_node)
21101 return true;
21102
21103 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
21104 arg = ARGUMENT_PACK_SELECT_ARG (arg);
21105
21106 if (TREE_CODE (arg) == TEMPLATE_DECL
21107 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
21108 return dependent_template_p (arg);
21109 else if (ARGUMENT_PACK_P (arg))
21110 {
21111 tree args = ARGUMENT_PACK_ARGS (arg);
21112 int i, len = TREE_VEC_LENGTH (args);
21113 for (i = 0; i < len; ++i)
21114 {
21115 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21116 return true;
21117 }
21118
21119 return false;
21120 }
21121 else if (TYPE_P (arg))
21122 return dependent_type_p (arg);
21123 else
21124 return (type_dependent_expression_p (arg)
21125 || value_dependent_expression_p (arg));
21126 }
21127
21128 /* Returns true if ARGS (a collection of template arguments) contains
21129 any types that require structural equality testing. */
21130
21131 bool
21132 any_template_arguments_need_structural_equality_p (tree args)
21133 {
21134 int i;
21135 int j;
21136
21137 if (!args)
21138 return false;
21139 if (args == error_mark_node)
21140 return true;
21141
21142 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21143 {
21144 tree level = TMPL_ARGS_LEVEL (args, i + 1);
21145 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21146 {
21147 tree arg = TREE_VEC_ELT (level, j);
21148 tree packed_args = NULL_TREE;
21149 int k, len = 1;
21150
21151 if (ARGUMENT_PACK_P (arg))
21152 {
21153 /* Look inside the argument pack. */
21154 packed_args = ARGUMENT_PACK_ARGS (arg);
21155 len = TREE_VEC_LENGTH (packed_args);
21156 }
21157
21158 for (k = 0; k < len; ++k)
21159 {
21160 if (packed_args)
21161 arg = TREE_VEC_ELT (packed_args, k);
21162
21163 if (error_operand_p (arg))
21164 return true;
21165 else if (TREE_CODE (arg) == TEMPLATE_DECL)
21166 continue;
21167 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
21168 return true;
21169 else if (!TYPE_P (arg) && TREE_TYPE (arg)
21170 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
21171 return true;
21172 }
21173 }
21174 }
21175
21176 return false;
21177 }
21178
21179 /* Returns true if ARGS (a collection of template arguments) contains
21180 any dependent arguments. */
21181
21182 bool
21183 any_dependent_template_arguments_p (const_tree args)
21184 {
21185 int i;
21186 int j;
21187
21188 if (!args)
21189 return false;
21190 if (args == error_mark_node)
21191 return true;
21192
21193 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21194 {
21195 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21196 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21197 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21198 return true;
21199 }
21200
21201 return false;
21202 }
21203
21204 /* Returns TRUE if the template TMPL is dependent. */
21205
21206 bool
21207 dependent_template_p (tree tmpl)
21208 {
21209 if (TREE_CODE (tmpl) == OVERLOAD)
21210 {
21211 while (tmpl)
21212 {
21213 if (dependent_template_p (OVL_CURRENT (tmpl)))
21214 return true;
21215 tmpl = OVL_NEXT (tmpl);
21216 }
21217 return false;
21218 }
21219
21220 /* Template template parameters are dependent. */
21221 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21222 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21223 return true;
21224 /* So are names that have not been looked up. */
21225 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21226 return true;
21227 /* So are member templates of dependent classes. */
21228 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21229 return dependent_type_p (DECL_CONTEXT (tmpl));
21230 return false;
21231 }
21232
21233 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21234
21235 bool
21236 dependent_template_id_p (tree tmpl, tree args)
21237 {
21238 return (dependent_template_p (tmpl)
21239 || any_dependent_template_arguments_p (args));
21240 }
21241
21242 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21243 is dependent. */
21244
21245 bool
21246 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21247 {
21248 int i;
21249
21250 if (!processing_template_decl)
21251 return false;
21252
21253 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21254 {
21255 tree decl = TREE_VEC_ELT (declv, i);
21256 tree init = TREE_VEC_ELT (initv, i);
21257 tree cond = TREE_VEC_ELT (condv, i);
21258 tree incr = TREE_VEC_ELT (incrv, i);
21259
21260 if (type_dependent_expression_p (decl))
21261 return true;
21262
21263 if (init && type_dependent_expression_p (init))
21264 return true;
21265
21266 if (type_dependent_expression_p (cond))
21267 return true;
21268
21269 if (COMPARISON_CLASS_P (cond)
21270 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21271 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21272 return true;
21273
21274 if (TREE_CODE (incr) == MODOP_EXPR)
21275 {
21276 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21277 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21278 return true;
21279 }
21280 else if (type_dependent_expression_p (incr))
21281 return true;
21282 else if (TREE_CODE (incr) == MODIFY_EXPR)
21283 {
21284 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21285 return true;
21286 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21287 {
21288 tree t = TREE_OPERAND (incr, 1);
21289 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21290 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21291 return true;
21292 }
21293 }
21294 }
21295
21296 return false;
21297 }
21298
21299 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21300 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21301 no such TYPE can be found. Note that this function peers inside
21302 uninstantiated templates and therefore should be used only in
21303 extremely limited situations. ONLY_CURRENT_P restricts this
21304 peering to the currently open classes hierarchy (which is required
21305 when comparing types). */
21306
21307 tree
21308 resolve_typename_type (tree type, bool only_current_p)
21309 {
21310 tree scope;
21311 tree name;
21312 tree decl;
21313 int quals;
21314 tree pushed_scope;
21315 tree result;
21316
21317 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21318
21319 scope = TYPE_CONTEXT (type);
21320 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21321 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21322 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21323 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21324 identifier of the TYPENAME_TYPE anymore.
21325 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21326 TYPENAME_TYPE instead, we avoid messing up with a possible
21327 typedef variant case. */
21328 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21329
21330 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21331 it first before we can figure out what NAME refers to. */
21332 if (TREE_CODE (scope) == TYPENAME_TYPE)
21333 {
21334 if (TYPENAME_IS_RESOLVING_P (scope))
21335 /* Given a class template A with a dependent base with nested type C,
21336 typedef typename A::C::C C will land us here, as trying to resolve
21337 the initial A::C leads to the local C typedef, which leads back to
21338 A::C::C. So we break the recursion now. */
21339 return type;
21340 else
21341 scope = resolve_typename_type (scope, only_current_p);
21342 }
21343 /* If we don't know what SCOPE refers to, then we cannot resolve the
21344 TYPENAME_TYPE. */
21345 if (TREE_CODE (scope) == TYPENAME_TYPE)
21346 return type;
21347 /* If the SCOPE is a template type parameter, we have no way of
21348 resolving the name. */
21349 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21350 return type;
21351 /* If the SCOPE is not the current instantiation, there's no reason
21352 to look inside it. */
21353 if (only_current_p && !currently_open_class (scope))
21354 return type;
21355 /* If this is a typedef, we don't want to look inside (c++/11987). */
21356 if (typedef_variant_p (type))
21357 return type;
21358 /* If SCOPE isn't the template itself, it will not have a valid
21359 TYPE_FIELDS list. */
21360 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21361 /* scope is either the template itself or a compatible instantiation
21362 like X<T>, so look up the name in the original template. */
21363 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21364 else
21365 /* scope is a partial instantiation, so we can't do the lookup or we
21366 will lose the template arguments. */
21367 return type;
21368 /* Enter the SCOPE so that name lookup will be resolved as if we
21369 were in the class definition. In particular, SCOPE will no
21370 longer be considered a dependent type. */
21371 pushed_scope = push_scope (scope);
21372 /* Look up the declaration. */
21373 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21374 tf_warning_or_error);
21375
21376 result = NULL_TREE;
21377
21378 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21379 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21380 if (!decl)
21381 /*nop*/;
21382 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21383 && TREE_CODE (decl) == TYPE_DECL)
21384 {
21385 result = TREE_TYPE (decl);
21386 if (result == error_mark_node)
21387 result = NULL_TREE;
21388 }
21389 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21390 && DECL_CLASS_TEMPLATE_P (decl))
21391 {
21392 tree tmpl;
21393 tree args;
21394 /* Obtain the template and the arguments. */
21395 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21396 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21397 /* Instantiate the template. */
21398 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21399 /*entering_scope=*/0,
21400 tf_error | tf_user);
21401 if (result == error_mark_node)
21402 result = NULL_TREE;
21403 }
21404
21405 /* Leave the SCOPE. */
21406 if (pushed_scope)
21407 pop_scope (pushed_scope);
21408
21409 /* If we failed to resolve it, return the original typename. */
21410 if (!result)
21411 return type;
21412
21413 /* If lookup found a typename type, resolve that too. */
21414 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21415 {
21416 /* Ill-formed programs can cause infinite recursion here, so we
21417 must catch that. */
21418 TYPENAME_IS_RESOLVING_P (type) = 1;
21419 result = resolve_typename_type (result, only_current_p);
21420 TYPENAME_IS_RESOLVING_P (type) = 0;
21421 }
21422
21423 /* Qualify the resulting type. */
21424 quals = cp_type_quals (type);
21425 if (quals)
21426 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21427
21428 return result;
21429 }
21430
21431 /* EXPR is an expression which is not type-dependent. Return a proxy
21432 for EXPR that can be used to compute the types of larger
21433 expressions containing EXPR. */
21434
21435 tree
21436 build_non_dependent_expr (tree expr)
21437 {
21438 tree inner_expr;
21439
21440 #ifdef ENABLE_CHECKING
21441 /* Try to get a constant value for all non-dependent expressions in
21442 order to expose bugs in *_dependent_expression_p and constexpr. */
21443 if (cxx_dialect >= cxx11)
21444 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21445 #endif
21446
21447 /* Preserve OVERLOADs; the functions must be available to resolve
21448 types. */
21449 inner_expr = expr;
21450 if (TREE_CODE (inner_expr) == STMT_EXPR)
21451 inner_expr = stmt_expr_value_expr (inner_expr);
21452 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21453 inner_expr = TREE_OPERAND (inner_expr, 0);
21454 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21455 inner_expr = TREE_OPERAND (inner_expr, 1);
21456 if (is_overloaded_fn (inner_expr)
21457 || TREE_CODE (inner_expr) == OFFSET_REF)
21458 return expr;
21459 /* There is no need to return a proxy for a variable. */
21460 if (VAR_P (expr))
21461 return expr;
21462 /* Preserve string constants; conversions from string constants to
21463 "char *" are allowed, even though normally a "const char *"
21464 cannot be used to initialize a "char *". */
21465 if (TREE_CODE (expr) == STRING_CST)
21466 return expr;
21467 /* Preserve arithmetic constants, as an optimization -- there is no
21468 reason to create a new node. */
21469 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21470 return expr;
21471 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21472 There is at least one place where we want to know that a
21473 particular expression is a throw-expression: when checking a ?:
21474 expression, there are special rules if the second or third
21475 argument is a throw-expression. */
21476 if (TREE_CODE (expr) == THROW_EXPR)
21477 return expr;
21478
21479 /* Don't wrap an initializer list, we need to be able to look inside. */
21480 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21481 return expr;
21482
21483 /* Don't wrap a dummy object, we need to be able to test for it. */
21484 if (is_dummy_object (expr))
21485 return expr;
21486
21487 if (TREE_CODE (expr) == COND_EXPR)
21488 return build3 (COND_EXPR,
21489 TREE_TYPE (expr),
21490 TREE_OPERAND (expr, 0),
21491 (TREE_OPERAND (expr, 1)
21492 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21493 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21494 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21495 if (TREE_CODE (expr) == COMPOUND_EXPR
21496 && !COMPOUND_EXPR_OVERLOADED (expr))
21497 return build2 (COMPOUND_EXPR,
21498 TREE_TYPE (expr),
21499 TREE_OPERAND (expr, 0),
21500 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21501
21502 /* If the type is unknown, it can't really be non-dependent */
21503 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21504
21505 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21506 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21507 }
21508
21509 /* ARGS is a vector of expressions as arguments to a function call.
21510 Replace the arguments with equivalent non-dependent expressions.
21511 This modifies ARGS in place. */
21512
21513 void
21514 make_args_non_dependent (vec<tree, va_gc> *args)
21515 {
21516 unsigned int ix;
21517 tree arg;
21518
21519 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21520 {
21521 tree newarg = build_non_dependent_expr (arg);
21522 if (newarg != arg)
21523 (*args)[ix] = newarg;
21524 }
21525 }
21526
21527 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21528 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21529 parms. */
21530
21531 static tree
21532 make_auto_1 (tree name)
21533 {
21534 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21535 TYPE_NAME (au) = build_decl (input_location,
21536 TYPE_DECL, name, au);
21537 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21538 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21539 (0, processing_template_decl + 1, processing_template_decl + 1,
21540 TYPE_NAME (au), NULL_TREE);
21541 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21542 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21543 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21544
21545 return au;
21546 }
21547
21548 tree
21549 make_decltype_auto (void)
21550 {
21551 return make_auto_1 (get_identifier ("decltype(auto)"));
21552 }
21553
21554 tree
21555 make_auto (void)
21556 {
21557 return make_auto_1 (get_identifier ("auto"));
21558 }
21559
21560 /* Given type ARG, return std::initializer_list<ARG>. */
21561
21562 static tree
21563 listify (tree arg)
21564 {
21565 tree std_init_list = namespace_binding
21566 (get_identifier ("initializer_list"), std_node);
21567 tree argvec;
21568 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21569 {
21570 error ("deducing from brace-enclosed initializer list requires "
21571 "#include <initializer_list>");
21572 return error_mark_node;
21573 }
21574 argvec = make_tree_vec (1);
21575 TREE_VEC_ELT (argvec, 0) = arg;
21576 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21577 NULL_TREE, 0, tf_warning_or_error);
21578 }
21579
21580 /* Replace auto in TYPE with std::initializer_list<auto>. */
21581
21582 static tree
21583 listify_autos (tree type, tree auto_node)
21584 {
21585 tree init_auto = listify (auto_node);
21586 tree argvec = make_tree_vec (1);
21587 TREE_VEC_ELT (argvec, 0) = init_auto;
21588 if (processing_template_decl)
21589 argvec = add_to_template_args (current_template_args (), argvec);
21590 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21591 }
21592
21593 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21594 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21595
21596 tree
21597 do_auto_deduction (tree type, tree init, tree auto_node)
21598 {
21599 tree targs;
21600
21601 if (init == error_mark_node)
21602 return error_mark_node;
21603
21604 if (type_dependent_expression_p (init))
21605 /* Defining a subset of type-dependent expressions that we can deduce
21606 from ahead of time isn't worth the trouble. */
21607 return type;
21608
21609 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21610 with either a new invented type template parameter U or, if the
21611 initializer is a braced-init-list (8.5.4), with
21612 std::initializer_list<U>. */
21613 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21614 type = listify_autos (type, auto_node);
21615
21616 init = resolve_nondeduced_context (init);
21617
21618 targs = make_tree_vec (1);
21619 if (AUTO_IS_DECLTYPE (auto_node))
21620 {
21621 bool id = (DECL_P (init) || TREE_CODE (init) == COMPONENT_REF);
21622 TREE_VEC_ELT (targs, 0)
21623 = finish_decltype_type (init, id, tf_warning_or_error);
21624 if (type != auto_node)
21625 {
21626 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21627 return error_mark_node;
21628 }
21629 }
21630 else
21631 {
21632 tree parms = build_tree_list (NULL_TREE, type);
21633 tree tparms = make_tree_vec (1);
21634 int val;
21635
21636 TREE_VEC_ELT (tparms, 0)
21637 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21638 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21639 DEDUCE_CALL, LOOKUP_NORMAL,
21640 NULL, /*explain_p=*/false);
21641 if (val > 0)
21642 {
21643 if (processing_template_decl)
21644 /* Try again at instantiation time. */
21645 return type;
21646 if (type && type != error_mark_node)
21647 /* If type is error_mark_node a diagnostic must have been
21648 emitted by now. Also, having a mention to '<type error>'
21649 in the diagnostic is not really useful to the user. */
21650 {
21651 if (cfun && auto_node == current_function_auto_return_pattern
21652 && LAMBDA_FUNCTION_P (current_function_decl))
21653 error ("unable to deduce lambda return type from %qE", init);
21654 else
21655 error ("unable to deduce %qT from %qE", type, init);
21656 }
21657 return error_mark_node;
21658 }
21659 }
21660
21661 /* If the list of declarators contains more than one declarator, the type
21662 of each declared variable is determined as described above. If the
21663 type deduced for the template parameter U is not the same in each
21664 deduction, the program is ill-formed. */
21665 if (TREE_TYPE (auto_node)
21666 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21667 {
21668 if (cfun && auto_node == current_function_auto_return_pattern
21669 && LAMBDA_FUNCTION_P (current_function_decl))
21670 error ("inconsistent types %qT and %qT deduced for "
21671 "lambda return type", TREE_TYPE (auto_node),
21672 TREE_VEC_ELT (targs, 0));
21673 else
21674 error ("inconsistent deduction for %qT: %qT and then %qT",
21675 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21676 return error_mark_node;
21677 }
21678 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21679
21680 if (processing_template_decl)
21681 targs = add_to_template_args (current_template_args (), targs);
21682 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21683 }
21684
21685 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21686 result. */
21687
21688 tree
21689 splice_late_return_type (tree type, tree late_return_type)
21690 {
21691 tree argvec;
21692
21693 if (late_return_type == NULL_TREE)
21694 return type;
21695 argvec = make_tree_vec (1);
21696 TREE_VEC_ELT (argvec, 0) = late_return_type;
21697 if (processing_template_parmlist)
21698 /* For a late-specified return type in a template type-parameter, we
21699 need to add a dummy argument level for its parmlist. */
21700 argvec = add_to_template_args
21701 (make_tree_vec (processing_template_parmlist), argvec);
21702 if (current_template_parms)
21703 argvec = add_to_template_args (current_template_args (), argvec);
21704 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21705 }
21706
21707 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21708 'decltype(auto)'. */
21709
21710 bool
21711 is_auto (const_tree type)
21712 {
21713 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21714 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21715 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21716 return true;
21717 else
21718 return false;
21719 }
21720
21721 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21722 a use of `auto'. Returns NULL_TREE otherwise. */
21723
21724 tree
21725 type_uses_auto (tree type)
21726 {
21727 return find_type_usage (type, is_auto);
21728 }
21729
21730 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21731 'decltype(auto)' or a concept. */
21732
21733 bool
21734 is_auto_or_concept (const_tree type)
21735 {
21736 return is_auto (type); // or concept
21737 }
21738
21739 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21740 a concept identifier) iff TYPE contains a use of a generic type. Returns
21741 NULL_TREE otherwise. */
21742
21743 tree
21744 type_uses_auto_or_concept (tree type)
21745 {
21746 return find_type_usage (type, is_auto_or_concept);
21747 }
21748
21749
21750 /* For a given template T, return the vector of typedefs referenced
21751 in T for which access check is needed at T instantiation time.
21752 T is either a FUNCTION_DECL or a RECORD_TYPE.
21753 Those typedefs were added to T by the function
21754 append_type_to_template_for_access_check. */
21755
21756 vec<qualified_typedef_usage_t, va_gc> *
21757 get_types_needing_access_check (tree t)
21758 {
21759 tree ti;
21760 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21761
21762 if (!t || t == error_mark_node)
21763 return NULL;
21764
21765 if (!(ti = get_template_info (t)))
21766 return NULL;
21767
21768 if (CLASS_TYPE_P (t)
21769 || TREE_CODE (t) == FUNCTION_DECL)
21770 {
21771 if (!TI_TEMPLATE (ti))
21772 return NULL;
21773
21774 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21775 }
21776
21777 return result;
21778 }
21779
21780 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21781 tied to T. That list of typedefs will be access checked at
21782 T instantiation time.
21783 T is either a FUNCTION_DECL or a RECORD_TYPE.
21784 TYPE_DECL is a TYPE_DECL node representing a typedef.
21785 SCOPE is the scope through which TYPE_DECL is accessed.
21786 LOCATION is the location of the usage point of TYPE_DECL.
21787
21788 This function is a subroutine of
21789 append_type_to_template_for_access_check. */
21790
21791 static void
21792 append_type_to_template_for_access_check_1 (tree t,
21793 tree type_decl,
21794 tree scope,
21795 location_t location)
21796 {
21797 qualified_typedef_usage_t typedef_usage;
21798 tree ti;
21799
21800 if (!t || t == error_mark_node)
21801 return;
21802
21803 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21804 || CLASS_TYPE_P (t))
21805 && type_decl
21806 && TREE_CODE (type_decl) == TYPE_DECL
21807 && scope);
21808
21809 if (!(ti = get_template_info (t)))
21810 return;
21811
21812 gcc_assert (TI_TEMPLATE (ti));
21813
21814 typedef_usage.typedef_decl = type_decl;
21815 typedef_usage.context = scope;
21816 typedef_usage.locus = location;
21817
21818 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21819 }
21820
21821 /* Append TYPE_DECL to the template TEMPL.
21822 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21823 At TEMPL instanciation time, TYPE_DECL will be checked to see
21824 if it can be accessed through SCOPE.
21825 LOCATION is the location of the usage point of TYPE_DECL.
21826
21827 e.g. consider the following code snippet:
21828
21829 class C
21830 {
21831 typedef int myint;
21832 };
21833
21834 template<class U> struct S
21835 {
21836 C::myint mi; // <-- usage point of the typedef C::myint
21837 };
21838
21839 S<char> s;
21840
21841 At S<char> instantiation time, we need to check the access of C::myint
21842 In other words, we need to check the access of the myint typedef through
21843 the C scope. For that purpose, this function will add the myint typedef
21844 and the scope C through which its being accessed to a list of typedefs
21845 tied to the template S. That list will be walked at template instantiation
21846 time and access check performed on each typedefs it contains.
21847 Note that this particular code snippet should yield an error because
21848 myint is private to C. */
21849
21850 void
21851 append_type_to_template_for_access_check (tree templ,
21852 tree type_decl,
21853 tree scope,
21854 location_t location)
21855 {
21856 qualified_typedef_usage_t *iter;
21857 unsigned i;
21858
21859 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21860
21861 /* Make sure we don't append the type to the template twice. */
21862 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21863 if (iter->typedef_decl == type_decl && scope == iter->context)
21864 return;
21865
21866 append_type_to_template_for_access_check_1 (templ, type_decl,
21867 scope, location);
21868 }
21869
21870 /* Convert the generic type parameters in PARM that match the types given in the
21871 range [START_IDX, END_IDX) from the current_template_parms into generic type
21872 packs. */
21873
21874 tree
21875 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
21876 {
21877 tree current = current_template_parms;
21878 int depth = TMPL_PARMS_DEPTH (current);
21879 current = INNERMOST_TEMPLATE_PARMS (current);
21880 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
21881
21882 for (int i = 0; i < start_idx; ++i)
21883 TREE_VEC_ELT (replacement, i)
21884 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21885
21886 for (int i = start_idx; i < end_idx; ++i)
21887 {
21888 /* Create a distinct parameter pack type from the current parm and add it
21889 to the replacement args to tsubst below into the generic function
21890 parameter. */
21891
21892 tree o = TREE_TYPE (TREE_VALUE
21893 (TREE_VEC_ELT (current, i)));
21894 tree t = copy_type (o);
21895 TEMPLATE_TYPE_PARM_INDEX (t)
21896 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
21897 o, 0, 0, tf_none);
21898 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
21899 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
21900 TYPE_MAIN_VARIANT (t) = t;
21901 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
21902 TYPE_CANONICAL (t) = canonical_type_parameter (t);
21903 TREE_VEC_ELT (replacement, i) = t;
21904 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
21905 }
21906
21907 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
21908 TREE_VEC_ELT (replacement, i)
21909 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21910
21911 /* If there are more levels then build up the replacement with the outer
21912 template parms. */
21913 if (depth > 1)
21914 replacement = add_to_template_args (template_parms_to_args
21915 (TREE_CHAIN (current_template_parms)),
21916 replacement);
21917
21918 return tsubst (parm, replacement, tf_none, NULL_TREE);
21919 }
21920
21921
21922 /* Set up the hash tables for template instantiations. */
21923
21924 void
21925 init_template_processing (void)
21926 {
21927 decl_specializations = htab_create_ggc (37,
21928 hash_specialization,
21929 eq_specializations,
21930 ggc_free);
21931 type_specializations = htab_create_ggc (37,
21932 hash_specialization,
21933 eq_specializations,
21934 ggc_free);
21935 }
21936
21937 /* Print stats about the template hash tables for -fstats. */
21938
21939 void
21940 print_template_statistics (void)
21941 {
21942 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
21943 "%f collisions\n", (long) htab_size (decl_specializations),
21944 (long) htab_elements (decl_specializations),
21945 htab_collisions (decl_specializations));
21946 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
21947 "%f collisions\n", (long) htab_size (type_specializations),
21948 (long) htab_elements (type_specializations),
21949 htab_collisions (type_specializations));
21950 }
21951
21952 #include "gt-cp-pt.h"