re PR c++/45329 (When printing a list of candidate functions, explain why each functi...
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
23
24 /* Known bugs or deficiencies include:
25
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "intl.h"
35 #include "pointer-set.h"
36 #include "flags.h"
37 #include "cp-tree.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
42 #include "decl.h"
43 #include "output.h"
44 #include "toplev.h"
45 #include "timevar.h"
46 #include "tree-iterator.h"
47 #include "vecprim.h"
48
49 /* The type of functions taking a tree, and some additional data, and
50 returning an int. */
51 typedef int (*tree_fn_t) (tree, void*);
52
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template {
57 struct pending_template *next;
58 struct tinst_level *tinst;
59 };
60
61 static GTY(()) struct pending_template *pending_templates;
62 static GTY(()) struct pending_template *last_pending_template;
63
64 int processing_template_parmlist;
65 static int template_header_count;
66
67 static GTY(()) tree saved_trees;
68 static VEC(int,heap) *inline_parm_levels;
69
70 static GTY(()) struct tinst_level *current_tinst_level;
71
72 static GTY(()) tree saved_access_scope;
73
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr;
78
79 /* A map from local variable declarations in the body of the template
80 presently being instantiated to the corresponding instantiated
81 local variables. */
82 static htab_t local_specializations;
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,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 void push_deduction_access_scope (tree);
121 static void pop_deduction_access_scope (tree);
122 static bool resolve_overloaded_unification (tree, tree, tree, tree,
123 unification_kind_t, int,
124 bool);
125 static int try_one_overload (tree, tree, tree, tree, tree,
126 unification_kind_t, int, bool, bool);
127 static int unify (tree, tree, tree, tree, int, bool);
128 static void add_pending_template (tree);
129 static tree reopen_tinst_level (struct tinst_level *);
130 static tree tsubst_initializer_list (tree, tree);
131 static tree get_class_bindings (tree, tree, tree);
132 static tree coerce_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 bool);
143 static void note_template_header (int);
144 static tree convert_nontype_argument_function (tree, tree);
145 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
146 static tree convert_template_argument (tree, tree, tree,
147 tsubst_flags_t, int, tree);
148 static int for_each_template_parm (tree, tree_fn_t, void*,
149 struct pointer_set_t*, bool);
150 static tree expand_template_argument_pack (tree);
151 static tree build_template_parm_index (int, int, int, int, tree, tree);
152 static bool inline_needs_template_parms (tree);
153 static void push_inline_template_parms_recursive (tree, int);
154 static tree retrieve_local_specialization (tree);
155 static void register_local_specialization (tree, tree);
156 static hashval_t hash_specialization (const void *p);
157 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
158 static int mark_template_parm (tree, void *);
159 static int template_parm_this_level_p (tree, void *);
160 static tree tsubst_friend_function (tree, tree);
161 static tree tsubst_friend_class (tree, tree);
162 static int can_complete_type_without_circularity (tree);
163 static tree get_bindings (tree, tree, tree, bool);
164 static int template_decl_level (tree);
165 static int check_cv_quals_for_unify (int, tree, tree);
166 static void template_parm_level_and_index (tree, int*, int*);
167 static int unify_pack_expansion (tree, tree, tree,
168 tree, int, bool, bool, bool);
169 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
170 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
171 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
172 static void regenerate_decl_from_template (tree, tree);
173 static tree most_specialized_class (tree, tree, tsubst_flags_t);
174 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
175 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
176 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
177 static bool check_specialization_scope (void);
178 static tree process_partial_specialization (tree);
179 static void set_current_access_from_decl (tree);
180 static enum template_base_result get_template_base (tree, tree, tree, tree,
181 bool , tree *);
182 static tree try_class_unification (tree, tree, tree, tree, bool);
183 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
184 tree, tree);
185 static bool template_template_parm_bindings_ok_p (tree, tree);
186 static int template_args_equal (tree, tree);
187 static void tsubst_default_arguments (tree);
188 static tree for_each_template_parm_r (tree *, int *, void *);
189 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
190 static void copy_default_args_to_explicit_spec (tree);
191 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
192 static int eq_local_specializations (const void *, const void *);
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 fixup_template_type_parm_type (tree, int);
208 static tree fixup_template_parm_index (tree, tree, int);
209 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
210
211 /* Make the current scope suitable for access checking when we are
212 processing T. T can be FUNCTION_DECL for instantiated function
213 template, or VAR_DECL for static member variable (need by
214 instantiate_decl). */
215
216 static void
217 push_access_scope (tree t)
218 {
219 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
220 || TREE_CODE (t) == VAR_DECL);
221
222 if (DECL_FRIEND_CONTEXT (t))
223 push_nested_class (DECL_FRIEND_CONTEXT (t));
224 else if (DECL_CLASS_SCOPE_P (t))
225 push_nested_class (DECL_CONTEXT (t));
226 else
227 push_to_top_level ();
228
229 if (TREE_CODE (t) == FUNCTION_DECL)
230 {
231 saved_access_scope = tree_cons
232 (NULL_TREE, current_function_decl, saved_access_scope);
233 current_function_decl = t;
234 }
235 }
236
237 /* Restore the scope set up by push_access_scope. T is the node we
238 are processing. */
239
240 static void
241 pop_access_scope (tree t)
242 {
243 if (TREE_CODE (t) == FUNCTION_DECL)
244 {
245 current_function_decl = TREE_VALUE (saved_access_scope);
246 saved_access_scope = TREE_CHAIN (saved_access_scope);
247 }
248
249 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
250 pop_nested_class ();
251 else
252 pop_from_top_level ();
253 }
254
255 /* Do any processing required when DECL (a member template
256 declaration) is finished. Returns the TEMPLATE_DECL corresponding
257 to DECL, unless it is a specialization, in which case the DECL
258 itself is returned. */
259
260 tree
261 finish_member_template_decl (tree decl)
262 {
263 if (decl == error_mark_node)
264 return error_mark_node;
265
266 gcc_assert (DECL_P (decl));
267
268 if (TREE_CODE (decl) == TYPE_DECL)
269 {
270 tree type;
271
272 type = TREE_TYPE (decl);
273 if (type == error_mark_node)
274 return error_mark_node;
275 if (MAYBE_CLASS_TYPE_P (type)
276 && CLASSTYPE_TEMPLATE_INFO (type)
277 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
278 {
279 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
280 check_member_template (tmpl);
281 return tmpl;
282 }
283 return NULL_TREE;
284 }
285 else if (TREE_CODE (decl) == FIELD_DECL)
286 error ("data member %qD cannot be a member template", decl);
287 else if (DECL_TEMPLATE_INFO (decl))
288 {
289 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
290 {
291 check_member_template (DECL_TI_TEMPLATE (decl));
292 return DECL_TI_TEMPLATE (decl);
293 }
294 else
295 return decl;
296 }
297 else
298 error ("invalid member template declaration %qD", decl);
299
300 return error_mark_node;
301 }
302
303 /* Create a template info node. */
304
305 tree
306 build_template_info (tree template_decl, tree template_args)
307 {
308 tree result = make_node (TEMPLATE_INFO);
309 TI_TEMPLATE (result) = template_decl;
310 TI_ARGS (result) = template_args;
311 return result;
312 }
313
314 /* Return the template info node corresponding to T, whatever T is. */
315
316 tree
317 get_template_info (const_tree t)
318 {
319 tree tinfo = NULL_TREE;
320
321 if (!t || t == error_mark_node)
322 return NULL;
323
324 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
325 tinfo = DECL_TEMPLATE_INFO (t);
326
327 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
328 t = TREE_TYPE (t);
329
330 if (TAGGED_TYPE_P (t))
331 tinfo = TYPE_TEMPLATE_INFO (t);
332 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
333 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
334
335 return tinfo;
336 }
337
338 /* Returns the template nesting level of the indicated class TYPE.
339
340 For example, in:
341 template <class T>
342 struct A
343 {
344 template <class U>
345 struct B {};
346 };
347
348 A<T>::B<U> has depth two, while A<T> has depth one.
349 Both A<T>::B<int> and A<int>::B<U> have depth one, if
350 they are instantiations, not specializations.
351
352 This function is guaranteed to return 0 if passed NULL_TREE so
353 that, for example, `template_class_depth (current_class_type)' is
354 always safe. */
355
356 int
357 template_class_depth (tree type)
358 {
359 int depth;
360
361 for (depth = 0;
362 type && TREE_CODE (type) != NAMESPACE_DECL;
363 type = (TREE_CODE (type) == FUNCTION_DECL)
364 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
365 {
366 tree tinfo = get_template_info (type);
367
368 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
369 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
370 ++depth;
371 }
372
373 return depth;
374 }
375
376 /* Subroutine of maybe_begin_member_template_processing.
377 Returns true if processing DECL needs us to push template parms. */
378
379 static bool
380 inline_needs_template_parms (tree decl)
381 {
382 if (! DECL_TEMPLATE_INFO (decl))
383 return false;
384
385 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
386 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
387 }
388
389 /* Subroutine of maybe_begin_member_template_processing.
390 Push the template parms in PARMS, starting from LEVELS steps into the
391 chain, and ending at the beginning, since template parms are listed
392 innermost first. */
393
394 static void
395 push_inline_template_parms_recursive (tree parmlist, int levels)
396 {
397 tree parms = TREE_VALUE (parmlist);
398 int i;
399
400 if (levels > 1)
401 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
402
403 ++processing_template_decl;
404 current_template_parms
405 = tree_cons (size_int (processing_template_decl),
406 parms, current_template_parms);
407 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
408
409 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
410 NULL);
411 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
412 {
413 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
414
415 if (parm == error_mark_node)
416 continue;
417
418 gcc_assert (DECL_P (parm));
419
420 switch (TREE_CODE (parm))
421 {
422 case TYPE_DECL:
423 case TEMPLATE_DECL:
424 pushdecl (parm);
425 break;
426
427 case PARM_DECL:
428 {
429 /* Make a CONST_DECL as is done in process_template_parm.
430 It is ugly that we recreate this here; the original
431 version built in process_template_parm is no longer
432 available. */
433 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
434 CONST_DECL, DECL_NAME (parm),
435 TREE_TYPE (parm));
436 DECL_ARTIFICIAL (decl) = 1;
437 TREE_CONSTANT (decl) = 1;
438 TREE_READONLY (decl) = 1;
439 DECL_INITIAL (decl) = DECL_INITIAL (parm);
440 SET_DECL_TEMPLATE_PARM_P (decl);
441 pushdecl (decl);
442 }
443 break;
444
445 default:
446 gcc_unreachable ();
447 }
448 }
449 }
450
451 /* Restore the template parameter context for a member template or
452 a friend template defined in a class definition. */
453
454 void
455 maybe_begin_member_template_processing (tree decl)
456 {
457 tree parms;
458 int levels = 0;
459
460 if (inline_needs_template_parms (decl))
461 {
462 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
463 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
464
465 if (DECL_TEMPLATE_SPECIALIZATION (decl))
466 {
467 --levels;
468 parms = TREE_CHAIN (parms);
469 }
470
471 push_inline_template_parms_recursive (parms, levels);
472 }
473
474 /* Remember how many levels of template parameters we pushed so that
475 we can pop them later. */
476 VEC_safe_push (int, heap, inline_parm_levels, levels);
477 }
478
479 /* Undo the effects of maybe_begin_member_template_processing. */
480
481 void
482 maybe_end_member_template_processing (void)
483 {
484 int i;
485 int last;
486
487 if (VEC_length (int, inline_parm_levels) == 0)
488 return;
489
490 last = VEC_pop (int, inline_parm_levels);
491 for (i = 0; i < last; ++i)
492 {
493 --processing_template_decl;
494 current_template_parms = TREE_CHAIN (current_template_parms);
495 poplevel (0, 0, 0);
496 }
497 }
498
499 /* Return a new template argument vector which contains all of ARGS,
500 but has as its innermost set of arguments the EXTRA_ARGS. */
501
502 static tree
503 add_to_template_args (tree args, tree extra_args)
504 {
505 tree new_args;
506 int extra_depth;
507 int i;
508 int j;
509
510 if (args == NULL_TREE || extra_args == error_mark_node)
511 return extra_args;
512
513 extra_depth = TMPL_ARGS_DEPTH (extra_args);
514 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
515
516 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
517 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
518
519 for (j = 1; j <= extra_depth; ++j, ++i)
520 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
521
522 return new_args;
523 }
524
525 /* Like add_to_template_args, but only the outermost ARGS are added to
526 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
527 (EXTRA_ARGS) levels are added. This function is used to combine
528 the template arguments from a partial instantiation with the
529 template arguments used to attain the full instantiation from the
530 partial instantiation. */
531
532 static tree
533 add_outermost_template_args (tree args, tree extra_args)
534 {
535 tree new_args;
536
537 /* If there are more levels of EXTRA_ARGS than there are ARGS,
538 something very fishy is going on. */
539 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
540
541 /* If *all* the new arguments will be the EXTRA_ARGS, just return
542 them. */
543 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
544 return extra_args;
545
546 /* For the moment, we make ARGS look like it contains fewer levels. */
547 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
548
549 new_args = add_to_template_args (args, extra_args);
550
551 /* Now, we restore ARGS to its full dimensions. */
552 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
553
554 return new_args;
555 }
556
557 /* Return the N levels of innermost template arguments from the ARGS. */
558
559 tree
560 get_innermost_template_args (tree args, int n)
561 {
562 tree new_args;
563 int extra_levels;
564 int i;
565
566 gcc_assert (n >= 0);
567
568 /* If N is 1, just return the innermost set of template arguments. */
569 if (n == 1)
570 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
571
572 /* If we're not removing anything, just return the arguments we were
573 given. */
574 extra_levels = TMPL_ARGS_DEPTH (args) - n;
575 gcc_assert (extra_levels >= 0);
576 if (extra_levels == 0)
577 return args;
578
579 /* Make a new set of arguments, not containing the outer arguments. */
580 new_args = make_tree_vec (n);
581 for (i = 1; i <= n; ++i)
582 SET_TMPL_ARGS_LEVEL (new_args, i,
583 TMPL_ARGS_LEVEL (args, i + extra_levels));
584
585 return new_args;
586 }
587
588 /* The inverse of get_innermost_template_args: Return all but the innermost
589 EXTRA_LEVELS levels of template arguments from the ARGS. */
590
591 static tree
592 strip_innermost_template_args (tree args, int extra_levels)
593 {
594 tree new_args;
595 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
596 int i;
597
598 gcc_assert (n >= 0);
599
600 /* If N is 1, just return the outermost set of template arguments. */
601 if (n == 1)
602 return TMPL_ARGS_LEVEL (args, 1);
603
604 /* If we're not removing anything, just return the arguments we were
605 given. */
606 gcc_assert (extra_levels >= 0);
607 if (extra_levels == 0)
608 return args;
609
610 /* Make a new set of arguments, not containing the inner arguments. */
611 new_args = make_tree_vec (n);
612 for (i = 1; i <= n; ++i)
613 SET_TMPL_ARGS_LEVEL (new_args, i,
614 TMPL_ARGS_LEVEL (args, i));
615
616 return new_args;
617 }
618
619 /* We've got a template header coming up; push to a new level for storing
620 the parms. */
621
622 void
623 begin_template_parm_list (void)
624 {
625 /* We use a non-tag-transparent scope here, which causes pushtag to
626 put tags in this scope, rather than in the enclosing class or
627 namespace scope. This is the right thing, since we want
628 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
629 global template class, push_template_decl handles putting the
630 TEMPLATE_DECL into top-level scope. For a nested template class,
631 e.g.:
632
633 template <class T> struct S1 {
634 template <class T> struct S2 {};
635 };
636
637 pushtag contains special code to call pushdecl_with_scope on the
638 TEMPLATE_DECL for S2. */
639 begin_scope (sk_template_parms, NULL);
640 ++processing_template_decl;
641 ++processing_template_parmlist;
642 note_template_header (0);
643 }
644
645 /* This routine is called when a specialization is declared. If it is
646 invalid to declare a specialization here, an error is reported and
647 false is returned, otherwise this routine will return true. */
648
649 static bool
650 check_specialization_scope (void)
651 {
652 tree scope = current_scope ();
653
654 /* [temp.expl.spec]
655
656 An explicit specialization shall be declared in the namespace of
657 which the template is a member, or, for member templates, in the
658 namespace of which the enclosing class or enclosing class
659 template is a member. An explicit specialization of a member
660 function, member class or static data member of a class template
661 shall be declared in the namespace of which the class template
662 is a member. */
663 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
664 {
665 error ("explicit specialization in non-namespace scope %qD", scope);
666 return false;
667 }
668
669 /* [temp.expl.spec]
670
671 In an explicit specialization declaration for a member of a class
672 template or a member template that appears in namespace scope,
673 the member template and some of its enclosing class templates may
674 remain unspecialized, except that the declaration shall not
675 explicitly specialize a class member template if its enclosing
676 class templates are not explicitly specialized as well. */
677 if (current_template_parms)
678 {
679 error ("enclosing class templates are not explicitly specialized");
680 return false;
681 }
682
683 return true;
684 }
685
686 /* We've just seen template <>. */
687
688 bool
689 begin_specialization (void)
690 {
691 begin_scope (sk_template_spec, NULL);
692 note_template_header (1);
693 return check_specialization_scope ();
694 }
695
696 /* Called at then end of processing a declaration preceded by
697 template<>. */
698
699 void
700 end_specialization (void)
701 {
702 finish_scope ();
703 reset_specialization ();
704 }
705
706 /* Any template <>'s that we have seen thus far are not referring to a
707 function specialization. */
708
709 void
710 reset_specialization (void)
711 {
712 processing_specialization = 0;
713 template_header_count = 0;
714 }
715
716 /* We've just seen a template header. If SPECIALIZATION is nonzero,
717 it was of the form template <>. */
718
719 static void
720 note_template_header (int specialization)
721 {
722 processing_specialization = specialization;
723 template_header_count++;
724 }
725
726 /* We're beginning an explicit instantiation. */
727
728 void
729 begin_explicit_instantiation (void)
730 {
731 gcc_assert (!processing_explicit_instantiation);
732 processing_explicit_instantiation = true;
733 }
734
735
736 void
737 end_explicit_instantiation (void)
738 {
739 gcc_assert (processing_explicit_instantiation);
740 processing_explicit_instantiation = false;
741 }
742
743 /* An explicit specialization or partial specialization TMPL is being
744 declared. Check that the namespace in which the specialization is
745 occurring is permissible. Returns false iff it is invalid to
746 specialize TMPL in the current namespace. */
747
748 static bool
749 check_specialization_namespace (tree tmpl)
750 {
751 tree tpl_ns = decl_namespace_context (tmpl);
752
753 /* [tmpl.expl.spec]
754
755 An explicit specialization shall be declared in the namespace of
756 which the template is a member, or, for member templates, in the
757 namespace of which the enclosing class or enclosing class
758 template is a member. An explicit specialization of a member
759 function, member class or static data member of a class template
760 shall be declared in the namespace of which the class template is
761 a member. */
762 if (current_scope() != DECL_CONTEXT (tmpl)
763 && !at_namespace_scope_p ())
764 {
765 error ("specialization of %qD must appear at namespace scope", tmpl);
766 return false;
767 }
768 if (is_associated_namespace (current_namespace, tpl_ns))
769 /* Same or super-using namespace. */
770 return true;
771 else
772 {
773 permerror (input_location, "specialization of %qD in different namespace", tmpl);
774 permerror (input_location, " from definition of %q+#D", tmpl);
775 return false;
776 }
777 }
778
779 /* SPEC is an explicit instantiation. Check that it is valid to
780 perform this explicit instantiation in the current namespace. */
781
782 static void
783 check_explicit_instantiation_namespace (tree spec)
784 {
785 tree ns;
786
787 /* DR 275: An explicit instantiation shall appear in an enclosing
788 namespace of its template. */
789 ns = decl_namespace_context (spec);
790 if (!is_ancestor (current_namespace, ns))
791 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
792 "(which does not enclose namespace %qD)",
793 spec, current_namespace, ns);
794 }
795
796 /* The TYPE is being declared. If it is a template type, that means it
797 is a partial specialization. Do appropriate error-checking. */
798
799 tree
800 maybe_process_partial_specialization (tree type)
801 {
802 tree context;
803
804 if (type == error_mark_node)
805 return error_mark_node;
806
807 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
808 {
809 error ("name of class shadows template template parameter %qD",
810 TYPE_NAME (type));
811 return error_mark_node;
812 }
813
814 context = TYPE_CONTEXT (type);
815
816 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
817 {
818 /* This is for ordinary explicit specialization and partial
819 specialization of a template class such as:
820
821 template <> class C<int>;
822
823 or:
824
825 template <class T> class C<T*>;
826
827 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
828
829 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
830 && !COMPLETE_TYPE_P (type))
831 {
832 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
833 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
834 if (processing_template_decl)
835 {
836 if (push_template_decl (TYPE_MAIN_DECL (type))
837 == error_mark_node)
838 return error_mark_node;
839 }
840 }
841 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
842 error ("specialization of %qT after instantiation", type);
843 }
844 else if (CLASS_TYPE_P (type)
845 && !CLASSTYPE_USE_TEMPLATE (type)
846 && CLASSTYPE_TEMPLATE_INFO (type)
847 && context && CLASS_TYPE_P (context)
848 && CLASSTYPE_TEMPLATE_INFO (context))
849 {
850 /* This is for an explicit specialization of member class
851 template according to [temp.expl.spec/18]:
852
853 template <> template <class U> class C<int>::D;
854
855 The context `C<int>' must be an implicit instantiation.
856 Otherwise this is just a member class template declared
857 earlier like:
858
859 template <> class C<int> { template <class U> class D; };
860 template <> template <class U> class C<int>::D;
861
862 In the first case, `C<int>::D' is a specialization of `C<T>::D'
863 while in the second case, `C<int>::D' is a primary template
864 and `C<T>::D' may not exist. */
865
866 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
867 && !COMPLETE_TYPE_P (type))
868 {
869 tree t;
870 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
871
872 if (current_namespace
873 != decl_namespace_context (tmpl))
874 {
875 permerror (input_location, "specializing %q#T in different namespace", type);
876 permerror (input_location, " from definition of %q+#D", tmpl);
877 }
878
879 /* Check for invalid specialization after instantiation:
880
881 template <> template <> class C<int>::D<int>;
882 template <> template <class U> class C<int>::D; */
883
884 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
885 t; t = TREE_CHAIN (t))
886 {
887 tree inst = TREE_VALUE (t);
888 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
889 {
890 /* We already have a full specialization of this partial
891 instantiation. Reassign it to the new member
892 specialization template. */
893 spec_entry elt;
894 spec_entry **slot;
895
896 elt.tmpl = most_general_template (tmpl);
897 elt.args = CLASSTYPE_TI_ARGS (inst);
898 elt.spec = inst;
899
900 htab_remove_elt (type_specializations, &elt);
901
902 elt.tmpl = tmpl;
903 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
904
905 slot = (spec_entry **)
906 htab_find_slot (type_specializations, &elt, INSERT);
907 *slot = ggc_alloc_spec_entry ();
908 **slot = elt;
909 }
910 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
911 /* But if we've had an implicit instantiation, that's a
912 problem ([temp.expl.spec]/6). */
913 error ("specialization %qT after instantiation %qT",
914 type, inst);
915 }
916
917 /* Mark TYPE as a specialization. And as a result, we only
918 have one level of template argument for the innermost
919 class template. */
920 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
921 CLASSTYPE_TI_ARGS (type)
922 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
923 }
924 }
925 else if (processing_specialization)
926 {
927 /* Someday C++0x may allow for enum template specialization. */
928 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
929 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
930 pedwarn (input_location, OPT_pedantic, "template specialization "
931 "of %qD not allowed by ISO C++", type);
932 else
933 {
934 error ("explicit specialization of non-template %qT", type);
935 return error_mark_node;
936 }
937 }
938
939 return type;
940 }
941
942 /* Returns nonzero if we can optimize the retrieval of specializations
943 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
944 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
945
946 static inline bool
947 optimize_specialization_lookup_p (tree tmpl)
948 {
949 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
950 && DECL_CLASS_SCOPE_P (tmpl)
951 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
952 parameter. */
953 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
954 /* The optimized lookup depends on the fact that the
955 template arguments for the member function template apply
956 purely to the containing class, which is not true if the
957 containing class is an explicit or partial
958 specialization. */
959 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
960 && !DECL_MEMBER_TEMPLATE_P (tmpl)
961 && !DECL_CONV_FN_P (tmpl)
962 /* It is possible to have a template that is not a member
963 template and is not a member of a template class:
964
965 template <typename T>
966 struct S { friend A::f(); };
967
968 Here, the friend function is a template, but the context does
969 not have template information. The optimized lookup relies
970 on having ARGS be the template arguments for both the class
971 and the function template. */
972 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
973 }
974
975 /* Retrieve the specialization (in the sense of [temp.spec] - a
976 specialization is either an instantiation or an explicit
977 specialization) of TMPL for the given template ARGS. If there is
978 no such specialization, return NULL_TREE. The ARGS are a vector of
979 arguments, or a vector of vectors of arguments, in the case of
980 templates with more than one level of parameters.
981
982 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
983 then we search for a partial specialization matching ARGS. This
984 parameter is ignored if TMPL is not a class template. */
985
986 static tree
987 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
988 {
989 if (args == error_mark_node)
990 return NULL_TREE;
991
992 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
993
994 /* There should be as many levels of arguments as there are
995 levels of parameters. */
996 gcc_assert (TMPL_ARGS_DEPTH (args)
997 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
998
999 if (optimize_specialization_lookup_p (tmpl))
1000 {
1001 tree class_template;
1002 tree class_specialization;
1003 VEC(tree,gc) *methods;
1004 tree fns;
1005 int idx;
1006
1007 /* The template arguments actually apply to the containing
1008 class. Find the class specialization with those
1009 arguments. */
1010 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1011 class_specialization
1012 = retrieve_specialization (class_template, args, 0);
1013 if (!class_specialization)
1014 return NULL_TREE;
1015 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1016 for the specialization. */
1017 idx = class_method_index_for_fn (class_specialization, tmpl);
1018 if (idx == -1)
1019 return NULL_TREE;
1020 /* Iterate through the methods with the indicated name, looking
1021 for the one that has an instance of TMPL. */
1022 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1023 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1024 {
1025 tree fn = OVL_CURRENT (fns);
1026 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1027 /* using-declarations can add base methods to the method vec,
1028 and we don't want those here. */
1029 && DECL_CONTEXT (fn) == class_specialization)
1030 return fn;
1031 }
1032 return NULL_TREE;
1033 }
1034 else
1035 {
1036 spec_entry *found;
1037 spec_entry elt;
1038 htab_t specializations;
1039
1040 elt.tmpl = tmpl;
1041 elt.args = args;
1042 elt.spec = NULL_TREE;
1043
1044 if (DECL_CLASS_TEMPLATE_P (tmpl))
1045 specializations = type_specializations;
1046 else
1047 specializations = decl_specializations;
1048
1049 if (hash == 0)
1050 hash = hash_specialization (&elt);
1051 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1052 if (found)
1053 return found->spec;
1054 }
1055
1056 return NULL_TREE;
1057 }
1058
1059 /* Like retrieve_specialization, but for local declarations. */
1060
1061 static tree
1062 retrieve_local_specialization (tree tmpl)
1063 {
1064 tree spec;
1065
1066 if (local_specializations == NULL)
1067 return NULL_TREE;
1068
1069 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1070 htab_hash_pointer (tmpl));
1071 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1072 }
1073
1074 /* Returns nonzero iff DECL is a specialization of TMPL. */
1075
1076 int
1077 is_specialization_of (tree decl, tree tmpl)
1078 {
1079 tree t;
1080
1081 if (TREE_CODE (decl) == FUNCTION_DECL)
1082 {
1083 for (t = decl;
1084 t != NULL_TREE;
1085 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1086 if (t == tmpl)
1087 return 1;
1088 }
1089 else
1090 {
1091 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1092
1093 for (t = TREE_TYPE (decl);
1094 t != NULL_TREE;
1095 t = CLASSTYPE_USE_TEMPLATE (t)
1096 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1097 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1098 return 1;
1099 }
1100
1101 return 0;
1102 }
1103
1104 /* Returns nonzero iff DECL is a specialization of friend declaration
1105 FRIEND_DECL according to [temp.friend]. */
1106
1107 bool
1108 is_specialization_of_friend (tree decl, tree friend_decl)
1109 {
1110 bool need_template = true;
1111 int template_depth;
1112
1113 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1114 || TREE_CODE (decl) == TYPE_DECL);
1115
1116 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1117 of a template class, we want to check if DECL is a specialization
1118 if this. */
1119 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1120 && DECL_TEMPLATE_INFO (friend_decl)
1121 && !DECL_USE_TEMPLATE (friend_decl))
1122 {
1123 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1124 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1125 need_template = false;
1126 }
1127 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1128 && !PRIMARY_TEMPLATE_P (friend_decl))
1129 need_template = false;
1130
1131 /* There is nothing to do if this is not a template friend. */
1132 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1133 return false;
1134
1135 if (is_specialization_of (decl, friend_decl))
1136 return true;
1137
1138 /* [temp.friend/6]
1139 A member of a class template may be declared to be a friend of a
1140 non-template class. In this case, the corresponding member of
1141 every specialization of the class template is a friend of the
1142 class granting friendship.
1143
1144 For example, given a template friend declaration
1145
1146 template <class T> friend void A<T>::f();
1147
1148 the member function below is considered a friend
1149
1150 template <> struct A<int> {
1151 void f();
1152 };
1153
1154 For this type of template friend, TEMPLATE_DEPTH below will be
1155 nonzero. To determine if DECL is a friend of FRIEND, we first
1156 check if the enclosing class is a specialization of another. */
1157
1158 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1159 if (template_depth
1160 && DECL_CLASS_SCOPE_P (decl)
1161 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1162 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1163 {
1164 /* Next, we check the members themselves. In order to handle
1165 a few tricky cases, such as when FRIEND_DECL's are
1166
1167 template <class T> friend void A<T>::g(T t);
1168 template <class T> template <T t> friend void A<T>::h();
1169
1170 and DECL's are
1171
1172 void A<int>::g(int);
1173 template <int> void A<int>::h();
1174
1175 we need to figure out ARGS, the template arguments from
1176 the context of DECL. This is required for template substitution
1177 of `T' in the function parameter of `g' and template parameter
1178 of `h' in the above examples. Here ARGS corresponds to `int'. */
1179
1180 tree context = DECL_CONTEXT (decl);
1181 tree args = NULL_TREE;
1182 int current_depth = 0;
1183
1184 while (current_depth < template_depth)
1185 {
1186 if (CLASSTYPE_TEMPLATE_INFO (context))
1187 {
1188 if (current_depth == 0)
1189 args = TYPE_TI_ARGS (context);
1190 else
1191 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1192 current_depth++;
1193 }
1194 context = TYPE_CONTEXT (context);
1195 }
1196
1197 if (TREE_CODE (decl) == FUNCTION_DECL)
1198 {
1199 bool is_template;
1200 tree friend_type;
1201 tree decl_type;
1202 tree friend_args_type;
1203 tree decl_args_type;
1204
1205 /* Make sure that both DECL and FRIEND_DECL are templates or
1206 non-templates. */
1207 is_template = DECL_TEMPLATE_INFO (decl)
1208 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1209 if (need_template ^ is_template)
1210 return false;
1211 else if (is_template)
1212 {
1213 /* If both are templates, check template parameter list. */
1214 tree friend_parms
1215 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1216 args, tf_none);
1217 if (!comp_template_parms
1218 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1219 friend_parms))
1220 return false;
1221
1222 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1223 }
1224 else
1225 decl_type = TREE_TYPE (decl);
1226
1227 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1228 tf_none, NULL_TREE);
1229 if (friend_type == error_mark_node)
1230 return false;
1231
1232 /* Check if return types match. */
1233 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1234 return false;
1235
1236 /* Check if function parameter types match, ignoring the
1237 `this' parameter. */
1238 friend_args_type = TYPE_ARG_TYPES (friend_type);
1239 decl_args_type = TYPE_ARG_TYPES (decl_type);
1240 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1241 friend_args_type = TREE_CHAIN (friend_args_type);
1242 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1243 decl_args_type = TREE_CHAIN (decl_args_type);
1244
1245 return compparms (decl_args_type, friend_args_type);
1246 }
1247 else
1248 {
1249 /* DECL is a TYPE_DECL */
1250 bool is_template;
1251 tree decl_type = TREE_TYPE (decl);
1252
1253 /* Make sure that both DECL and FRIEND_DECL are templates or
1254 non-templates. */
1255 is_template
1256 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1257 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1258
1259 if (need_template ^ is_template)
1260 return false;
1261 else if (is_template)
1262 {
1263 tree friend_parms;
1264 /* If both are templates, check the name of the two
1265 TEMPLATE_DECL's first because is_friend didn't. */
1266 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1267 != DECL_NAME (friend_decl))
1268 return false;
1269
1270 /* Now check template parameter list. */
1271 friend_parms
1272 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1273 args, tf_none);
1274 return comp_template_parms
1275 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1276 friend_parms);
1277 }
1278 else
1279 return (DECL_NAME (decl)
1280 == DECL_NAME (friend_decl));
1281 }
1282 }
1283 return false;
1284 }
1285
1286 /* Register the specialization SPEC as a specialization of TMPL with
1287 the indicated ARGS. IS_FRIEND indicates whether the specialization
1288 is actually just a friend declaration. Returns SPEC, or an
1289 equivalent prior declaration, if available. */
1290
1291 static tree
1292 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1293 hashval_t hash)
1294 {
1295 tree fn;
1296 spec_entry **slot = NULL;
1297 spec_entry elt;
1298
1299 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1300
1301 if (TREE_CODE (spec) == FUNCTION_DECL
1302 && uses_template_parms (DECL_TI_ARGS (spec)))
1303 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1304 register it; we want the corresponding TEMPLATE_DECL instead.
1305 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1306 the more obvious `uses_template_parms (spec)' to avoid problems
1307 with default function arguments. In particular, given
1308 something like this:
1309
1310 template <class T> void f(T t1, T t = T())
1311
1312 the default argument expression is not substituted for in an
1313 instantiation unless and until it is actually needed. */
1314 return spec;
1315
1316 if (optimize_specialization_lookup_p (tmpl))
1317 /* We don't put these specializations in the hash table, but we might
1318 want to give an error about a mismatch. */
1319 fn = retrieve_specialization (tmpl, args, 0);
1320 else
1321 {
1322 elt.tmpl = tmpl;
1323 elt.args = args;
1324 elt.spec = spec;
1325
1326 if (hash == 0)
1327 hash = hash_specialization (&elt);
1328
1329 slot = (spec_entry **)
1330 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1331 if (*slot)
1332 fn = (*slot)->spec;
1333 else
1334 fn = NULL_TREE;
1335 }
1336
1337 /* We can sometimes try to re-register a specialization that we've
1338 already got. In particular, regenerate_decl_from_template calls
1339 duplicate_decls which will update the specialization list. But,
1340 we'll still get called again here anyhow. It's more convenient
1341 to simply allow this than to try to prevent it. */
1342 if (fn == spec)
1343 return spec;
1344 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1345 {
1346 if (DECL_TEMPLATE_INSTANTIATION (fn))
1347 {
1348 if (DECL_ODR_USED (fn)
1349 || DECL_EXPLICIT_INSTANTIATION (fn))
1350 {
1351 error ("specialization of %qD after instantiation",
1352 fn);
1353 return error_mark_node;
1354 }
1355 else
1356 {
1357 tree clone;
1358 /* This situation should occur only if the first
1359 specialization is an implicit instantiation, the
1360 second is an explicit specialization, and the
1361 implicit instantiation has not yet been used. That
1362 situation can occur if we have implicitly
1363 instantiated a member function and then specialized
1364 it later.
1365
1366 We can also wind up here if a friend declaration that
1367 looked like an instantiation turns out to be a
1368 specialization:
1369
1370 template <class T> void foo(T);
1371 class S { friend void foo<>(int) };
1372 template <> void foo(int);
1373
1374 We transform the existing DECL in place so that any
1375 pointers to it become pointers to the updated
1376 declaration.
1377
1378 If there was a definition for the template, but not
1379 for the specialization, we want this to look as if
1380 there were no definition, and vice versa. */
1381 DECL_INITIAL (fn) = NULL_TREE;
1382 duplicate_decls (spec, fn, is_friend);
1383 /* The call to duplicate_decls will have applied
1384 [temp.expl.spec]:
1385
1386 An explicit specialization of a function template
1387 is inline only if it is explicitly declared to be,
1388 and independently of whether its function template
1389 is.
1390
1391 to the primary function; now copy the inline bits to
1392 the various clones. */
1393 FOR_EACH_CLONE (clone, fn)
1394 {
1395 DECL_DECLARED_INLINE_P (clone)
1396 = DECL_DECLARED_INLINE_P (fn);
1397 DECL_SOURCE_LOCATION (clone)
1398 = DECL_SOURCE_LOCATION (fn);
1399 }
1400 check_specialization_namespace (fn);
1401
1402 return fn;
1403 }
1404 }
1405 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1406 {
1407 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1408 /* Dup decl failed, but this is a new definition. Set the
1409 line number so any errors match this new
1410 definition. */
1411 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1412
1413 return fn;
1414 }
1415 }
1416 else if (fn)
1417 return duplicate_decls (spec, fn, is_friend);
1418
1419 /* A specialization must be declared in the same namespace as the
1420 template it is specializing. */
1421 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1422 && !check_specialization_namespace (tmpl))
1423 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1424
1425 if (!optimize_specialization_lookup_p (tmpl))
1426 {
1427 gcc_assert (tmpl && args && spec);
1428 *slot = ggc_alloc_spec_entry ();
1429 **slot = elt;
1430 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1431 && PRIMARY_TEMPLATE_P (tmpl)
1432 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1433 /* TMPL is a forward declaration of a template function; keep a list
1434 of all specializations in case we need to reassign them to a friend
1435 template later in tsubst_friend_function. */
1436 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1437 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1438 }
1439
1440 return spec;
1441 }
1442
1443 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1444 TMPL and ARGS members, ignores SPEC. */
1445
1446 static int
1447 eq_specializations (const void *p1, const void *p2)
1448 {
1449 const spec_entry *e1 = (const spec_entry *)p1;
1450 const spec_entry *e2 = (const spec_entry *)p2;
1451
1452 return (e1->tmpl == e2->tmpl
1453 && comp_template_args (e1->args, e2->args));
1454 }
1455
1456 /* Returns a hash for a template TMPL and template arguments ARGS. */
1457
1458 static hashval_t
1459 hash_tmpl_and_args (tree tmpl, tree args)
1460 {
1461 hashval_t val = DECL_UID (tmpl);
1462 return iterative_hash_template_arg (args, val);
1463 }
1464
1465 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1466 ignoring SPEC. */
1467
1468 static hashval_t
1469 hash_specialization (const void *p)
1470 {
1471 const spec_entry *e = (const spec_entry *)p;
1472 return hash_tmpl_and_args (e->tmpl, e->args);
1473 }
1474
1475 /* Recursively calculate a hash value for a template argument ARG, for use
1476 in the hash tables of template specializations. */
1477
1478 hashval_t
1479 iterative_hash_template_arg (tree arg, hashval_t val)
1480 {
1481 unsigned HOST_WIDE_INT i;
1482 enum tree_code code;
1483 char tclass;
1484
1485 if (arg == NULL_TREE)
1486 return iterative_hash_object (arg, val);
1487
1488 if (!TYPE_P (arg))
1489 STRIP_NOPS (arg);
1490
1491 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1492 /* We can get one of these when re-hashing a previous entry in the middle
1493 of substituting into a pack expansion. Just look through it. */
1494 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1495
1496 code = TREE_CODE (arg);
1497 tclass = TREE_CODE_CLASS (code);
1498
1499 val = iterative_hash_object (code, val);
1500
1501 switch (code)
1502 {
1503 case ERROR_MARK:
1504 return val;
1505
1506 case IDENTIFIER_NODE:
1507 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1508
1509 case TREE_VEC:
1510 {
1511 int i, len = TREE_VEC_LENGTH (arg);
1512 for (i = 0; i < len; ++i)
1513 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1514 return val;
1515 }
1516
1517 case TYPE_PACK_EXPANSION:
1518 case EXPR_PACK_EXPANSION:
1519 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1520
1521 case TYPE_ARGUMENT_PACK:
1522 case NONTYPE_ARGUMENT_PACK:
1523 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1524
1525 case TREE_LIST:
1526 for (; arg; arg = TREE_CHAIN (arg))
1527 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1528 return val;
1529
1530 case OVERLOAD:
1531 for (; arg; arg = OVL_NEXT (arg))
1532 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1533 return val;
1534
1535 case CONSTRUCTOR:
1536 {
1537 tree field, value;
1538 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1539 {
1540 val = iterative_hash_template_arg (field, val);
1541 val = iterative_hash_template_arg (value, val);
1542 }
1543 return val;
1544 }
1545
1546 case PARM_DECL:
1547 if (!DECL_ARTIFICIAL (arg))
1548 {
1549 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1550 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1551 }
1552 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1553
1554 case TARGET_EXPR:
1555 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1556
1557 case PTRMEM_CST:
1558 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1559 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1560
1561 case TEMPLATE_PARM_INDEX:
1562 val = iterative_hash_template_arg
1563 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1564 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1565 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1566
1567 case TRAIT_EXPR:
1568 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1569 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1570 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1571
1572 case BASELINK:
1573 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1574 val);
1575 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1576 val);
1577
1578 case MODOP_EXPR:
1579 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1580 code = TREE_CODE (TREE_OPERAND (arg, 1));
1581 val = iterative_hash_object (code, val);
1582 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1583
1584 case LAMBDA_EXPR:
1585 /* A lambda can't appear in a template arg, but don't crash on
1586 erroneous input. */
1587 gcc_assert (seen_error ());
1588 return val;
1589
1590 case CAST_EXPR:
1591 case STATIC_CAST_EXPR:
1592 case REINTERPRET_CAST_EXPR:
1593 case CONST_CAST_EXPR:
1594 case DYNAMIC_CAST_EXPR:
1595 case NEW_EXPR:
1596 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1597 /* Now hash operands as usual. */
1598 break;
1599
1600 default:
1601 break;
1602 }
1603
1604 switch (tclass)
1605 {
1606 case tcc_type:
1607 if (TYPE_CANONICAL (arg))
1608 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1609 val);
1610 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1611 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1612 /* Otherwise just compare the types during lookup. */
1613 return val;
1614
1615 case tcc_declaration:
1616 case tcc_constant:
1617 return iterative_hash_expr (arg, val);
1618
1619 default:
1620 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1621 {
1622 unsigned n = cp_tree_operand_length (arg);
1623 for (i = 0; i < n; ++i)
1624 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1625 return val;
1626 }
1627 }
1628 gcc_unreachable ();
1629 return 0;
1630 }
1631
1632 /* Unregister the specialization SPEC as a specialization of TMPL.
1633 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1634 if the SPEC was listed as a specialization of TMPL.
1635
1636 Note that SPEC has been ggc_freed, so we can't look inside it. */
1637
1638 bool
1639 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1640 {
1641 spec_entry **slot;
1642 spec_entry elt;
1643
1644 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1645 elt.args = TI_ARGS (tinfo);
1646 elt.spec = NULL_TREE;
1647
1648 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1649 if (*slot)
1650 {
1651 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1652 gcc_assert (new_spec != NULL_TREE);
1653 (*slot)->spec = new_spec;
1654 return 1;
1655 }
1656
1657 return 0;
1658 }
1659
1660 /* Compare an entry in the local specializations hash table P1 (which
1661 is really a pointer to a TREE_LIST) with P2 (which is really a
1662 DECL). */
1663
1664 static int
1665 eq_local_specializations (const void *p1, const void *p2)
1666 {
1667 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1668 }
1669
1670 /* Hash P1, an entry in the local specializations table. */
1671
1672 static hashval_t
1673 hash_local_specialization (const void* p1)
1674 {
1675 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1676 }
1677
1678 /* Like register_specialization, but for local declarations. We are
1679 registering SPEC, an instantiation of TMPL. */
1680
1681 static void
1682 register_local_specialization (tree spec, tree tmpl)
1683 {
1684 void **slot;
1685
1686 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1687 htab_hash_pointer (tmpl), INSERT);
1688 *slot = build_tree_list (spec, tmpl);
1689 }
1690
1691 /* TYPE is a class type. Returns true if TYPE is an explicitly
1692 specialized class. */
1693
1694 bool
1695 explicit_class_specialization_p (tree type)
1696 {
1697 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1698 return false;
1699 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1700 }
1701
1702 /* Print the list of functions at FNS, going through all the overloads
1703 for each element of the list. Alternatively, FNS can not be a
1704 TREE_LIST, in which case it will be printed together with all the
1705 overloads.
1706
1707 MORE and *STR should respectively be FALSE and NULL when the function
1708 is called from the outside. They are used internally on recursive
1709 calls. print_candidates manages the two parameters and leaves NULL
1710 in *STR when it ends. */
1711
1712 static void
1713 print_candidates_1 (tree fns, bool more, const char **str)
1714 {
1715 tree fn, fn2;
1716 char *spaces = NULL;
1717
1718 for (fn = fns; fn; fn = OVL_NEXT (fn))
1719 if (TREE_CODE (fn) == TREE_LIST)
1720 {
1721 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1722 print_candidates_1 (TREE_VALUE (fn2),
1723 TREE_CHAIN (fn2) || more, str);
1724 }
1725 else
1726 {
1727 if (!*str)
1728 {
1729 /* Pick the prefix string. */
1730 if (!more && !OVL_NEXT (fns))
1731 {
1732 error ("candidate is: %+#D", OVL_CURRENT (fn));
1733 continue;
1734 }
1735
1736 *str = _("candidates are:");
1737 spaces = get_spaces (*str);
1738 }
1739 error ("%s %+#D", *str, OVL_CURRENT (fn));
1740 *str = spaces ? spaces : *str;
1741 }
1742
1743 if (!more)
1744 {
1745 free (spaces);
1746 *str = NULL;
1747 }
1748 }
1749
1750 /* Print the list of candidate FNS in an error message. FNS can also
1751 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1752
1753 void
1754 print_candidates (tree fns)
1755 {
1756 const char *str = NULL;
1757 print_candidates_1 (fns, false, &str);
1758 gcc_assert (str == NULL);
1759 }
1760
1761 /* Returns the template (one of the functions given by TEMPLATE_ID)
1762 which can be specialized to match the indicated DECL with the
1763 explicit template args given in TEMPLATE_ID. The DECL may be
1764 NULL_TREE if none is available. In that case, the functions in
1765 TEMPLATE_ID are non-members.
1766
1767 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1768 specialization of a member template.
1769
1770 The TEMPLATE_COUNT is the number of references to qualifying
1771 template classes that appeared in the name of the function. See
1772 check_explicit_specialization for a more accurate description.
1773
1774 TSK indicates what kind of template declaration (if any) is being
1775 declared. TSK_TEMPLATE indicates that the declaration given by
1776 DECL, though a FUNCTION_DECL, has template parameters, and is
1777 therefore a template function.
1778
1779 The template args (those explicitly specified and those deduced)
1780 are output in a newly created vector *TARGS_OUT.
1781
1782 If it is impossible to determine the result, an error message is
1783 issued. The error_mark_node is returned to indicate failure. */
1784
1785 static tree
1786 determine_specialization (tree template_id,
1787 tree decl,
1788 tree* targs_out,
1789 int need_member_template,
1790 int template_count,
1791 tmpl_spec_kind tsk)
1792 {
1793 tree fns;
1794 tree targs;
1795 tree explicit_targs;
1796 tree candidates = NULL_TREE;
1797 /* A TREE_LIST of templates of which DECL may be a specialization.
1798 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1799 corresponding TREE_PURPOSE is the set of template arguments that,
1800 when used to instantiate the template, would produce a function
1801 with the signature of DECL. */
1802 tree templates = NULL_TREE;
1803 int header_count;
1804 cp_binding_level *b;
1805
1806 *targs_out = NULL_TREE;
1807
1808 if (template_id == error_mark_node || decl == error_mark_node)
1809 return error_mark_node;
1810
1811 fns = TREE_OPERAND (template_id, 0);
1812 explicit_targs = TREE_OPERAND (template_id, 1);
1813
1814 if (fns == error_mark_node)
1815 return error_mark_node;
1816
1817 /* Check for baselinks. */
1818 if (BASELINK_P (fns))
1819 fns = BASELINK_FUNCTIONS (fns);
1820
1821 if (!is_overloaded_fn (fns))
1822 {
1823 error ("%qD is not a function template", fns);
1824 return error_mark_node;
1825 }
1826
1827 /* Count the number of template headers specified for this
1828 specialization. */
1829 header_count = 0;
1830 for (b = current_binding_level;
1831 b->kind == sk_template_parms;
1832 b = b->level_chain)
1833 ++header_count;
1834
1835 for (; fns; fns = OVL_NEXT (fns))
1836 {
1837 tree fn = OVL_CURRENT (fns);
1838
1839 if (TREE_CODE (fn) == TEMPLATE_DECL)
1840 {
1841 tree decl_arg_types;
1842 tree fn_arg_types;
1843
1844 /* In case of explicit specialization, we need to check if
1845 the number of template headers appearing in the specialization
1846 is correct. This is usually done in check_explicit_specialization,
1847 but the check done there cannot be exhaustive when specializing
1848 member functions. Consider the following code:
1849
1850 template <> void A<int>::f(int);
1851 template <> template <> void A<int>::f(int);
1852
1853 Assuming that A<int> is not itself an explicit specialization
1854 already, the first line specializes "f" which is a non-template
1855 member function, whilst the second line specializes "f" which
1856 is a template member function. So both lines are syntactically
1857 correct, and check_explicit_specialization does not reject
1858 them.
1859
1860 Here, we can do better, as we are matching the specialization
1861 against the declarations. We count the number of template
1862 headers, and we check if they match TEMPLATE_COUNT + 1
1863 (TEMPLATE_COUNT is the number of qualifying template classes,
1864 plus there must be another header for the member template
1865 itself).
1866
1867 Notice that if header_count is zero, this is not a
1868 specialization but rather a template instantiation, so there
1869 is no check we can perform here. */
1870 if (header_count && header_count != template_count + 1)
1871 continue;
1872
1873 /* Check that the number of template arguments at the
1874 innermost level for DECL is the same as for FN. */
1875 if (current_binding_level->kind == sk_template_parms
1876 && !current_binding_level->explicit_spec_p
1877 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1878 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1879 (current_template_parms))))
1880 continue;
1881
1882 /* DECL might be a specialization of FN. */
1883 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1884 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1885
1886 /* For a non-static member function, we need to make sure
1887 that the const qualification is the same. Since
1888 get_bindings does not try to merge the "this" parameter,
1889 we must do the comparison explicitly. */
1890 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1891 && !same_type_p (TREE_VALUE (fn_arg_types),
1892 TREE_VALUE (decl_arg_types)))
1893 continue;
1894
1895 /* Skip the "this" parameter and, for constructors of
1896 classes with virtual bases, the VTT parameter. A
1897 full specialization of a constructor will have a VTT
1898 parameter, but a template never will. */
1899 decl_arg_types
1900 = skip_artificial_parms_for (decl, decl_arg_types);
1901 fn_arg_types
1902 = skip_artificial_parms_for (fn, fn_arg_types);
1903
1904 /* Check that the number of function parameters matches.
1905 For example,
1906 template <class T> void f(int i = 0);
1907 template <> void f<int>();
1908 The specialization f<int> is invalid but is not caught
1909 by get_bindings below. */
1910 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1911 continue;
1912
1913 /* Function templates cannot be specializations; there are
1914 no partial specializations of functions. Therefore, if
1915 the type of DECL does not match FN, there is no
1916 match. */
1917 if (tsk == tsk_template)
1918 {
1919 if (compparms (fn_arg_types, decl_arg_types))
1920 candidates = tree_cons (NULL_TREE, fn, candidates);
1921 continue;
1922 }
1923
1924 /* See whether this function might be a specialization of this
1925 template. */
1926 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1927
1928 if (!targs)
1929 /* We cannot deduce template arguments that when used to
1930 specialize TMPL will produce DECL. */
1931 continue;
1932
1933 /* Save this template, and the arguments deduced. */
1934 templates = tree_cons (targs, fn, templates);
1935 }
1936 else if (need_member_template)
1937 /* FN is an ordinary member function, and we need a
1938 specialization of a member template. */
1939 ;
1940 else if (TREE_CODE (fn) != FUNCTION_DECL)
1941 /* We can get IDENTIFIER_NODEs here in certain erroneous
1942 cases. */
1943 ;
1944 else if (!DECL_FUNCTION_MEMBER_P (fn))
1945 /* This is just an ordinary non-member function. Nothing can
1946 be a specialization of that. */
1947 ;
1948 else if (DECL_ARTIFICIAL (fn))
1949 /* Cannot specialize functions that are created implicitly. */
1950 ;
1951 else
1952 {
1953 tree decl_arg_types;
1954
1955 /* This is an ordinary member function. However, since
1956 we're here, we can assume it's enclosing class is a
1957 template class. For example,
1958
1959 template <typename T> struct S { void f(); };
1960 template <> void S<int>::f() {}
1961
1962 Here, S<int>::f is a non-template, but S<int> is a
1963 template class. If FN has the same type as DECL, we
1964 might be in business. */
1965
1966 if (!DECL_TEMPLATE_INFO (fn))
1967 /* Its enclosing class is an explicit specialization
1968 of a template class. This is not a candidate. */
1969 continue;
1970
1971 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1972 TREE_TYPE (TREE_TYPE (fn))))
1973 /* The return types differ. */
1974 continue;
1975
1976 /* Adjust the type of DECL in case FN is a static member. */
1977 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1978 if (DECL_STATIC_FUNCTION_P (fn)
1979 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1980 decl_arg_types = TREE_CHAIN (decl_arg_types);
1981
1982 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1983 decl_arg_types))
1984 /* They match! */
1985 candidates = tree_cons (NULL_TREE, fn, candidates);
1986 }
1987 }
1988
1989 if (templates && TREE_CHAIN (templates))
1990 {
1991 /* We have:
1992
1993 [temp.expl.spec]
1994
1995 It is possible for a specialization with a given function
1996 signature to be instantiated from more than one function
1997 template. In such cases, explicit specification of the
1998 template arguments must be used to uniquely identify the
1999 function template specialization being specialized.
2000
2001 Note that here, there's no suggestion that we're supposed to
2002 determine which of the candidate templates is most
2003 specialized. However, we, also have:
2004
2005 [temp.func.order]
2006
2007 Partial ordering of overloaded function template
2008 declarations is used in the following contexts to select
2009 the function template to which a function template
2010 specialization refers:
2011
2012 -- when an explicit specialization refers to a function
2013 template.
2014
2015 So, we do use the partial ordering rules, at least for now.
2016 This extension can only serve to make invalid programs valid,
2017 so it's safe. And, there is strong anecdotal evidence that
2018 the committee intended the partial ordering rules to apply;
2019 the EDG front end has that behavior, and John Spicer claims
2020 that the committee simply forgot to delete the wording in
2021 [temp.expl.spec]. */
2022 tree tmpl = most_specialized_instantiation (templates);
2023 if (tmpl != error_mark_node)
2024 {
2025 templates = tmpl;
2026 TREE_CHAIN (templates) = NULL_TREE;
2027 }
2028 }
2029
2030 if (templates == NULL_TREE && candidates == NULL_TREE)
2031 {
2032 error ("template-id %qD for %q+D does not match any template "
2033 "declaration", template_id, decl);
2034 if (header_count && header_count != template_count + 1)
2035 inform (input_location, "saw %d %<template<>%>, need %d for "
2036 "specializing a member function template",
2037 header_count, template_count + 1);
2038 return error_mark_node;
2039 }
2040 else if ((templates && TREE_CHAIN (templates))
2041 || (candidates && TREE_CHAIN (candidates))
2042 || (templates && candidates))
2043 {
2044 error ("ambiguous template specialization %qD for %q+D",
2045 template_id, decl);
2046 candidates = chainon (candidates, templates);
2047 print_candidates (candidates);
2048 return error_mark_node;
2049 }
2050
2051 /* We have one, and exactly one, match. */
2052 if (candidates)
2053 {
2054 tree fn = TREE_VALUE (candidates);
2055 *targs_out = copy_node (DECL_TI_ARGS (fn));
2056 /* DECL is a re-declaration or partial instantiation of a template
2057 function. */
2058 if (TREE_CODE (fn) == TEMPLATE_DECL)
2059 return fn;
2060 /* It was a specialization of an ordinary member function in a
2061 template class. */
2062 return DECL_TI_TEMPLATE (fn);
2063 }
2064
2065 /* It was a specialization of a template. */
2066 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2067 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2068 {
2069 *targs_out = copy_node (targs);
2070 SET_TMPL_ARGS_LEVEL (*targs_out,
2071 TMPL_ARGS_DEPTH (*targs_out),
2072 TREE_PURPOSE (templates));
2073 }
2074 else
2075 *targs_out = TREE_PURPOSE (templates);
2076 return TREE_VALUE (templates);
2077 }
2078
2079 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2080 but with the default argument values filled in from those in the
2081 TMPL_TYPES. */
2082
2083 static tree
2084 copy_default_args_to_explicit_spec_1 (tree spec_types,
2085 tree tmpl_types)
2086 {
2087 tree new_spec_types;
2088
2089 if (!spec_types)
2090 return NULL_TREE;
2091
2092 if (spec_types == void_list_node)
2093 return void_list_node;
2094
2095 /* Substitute into the rest of the list. */
2096 new_spec_types =
2097 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2098 TREE_CHAIN (tmpl_types));
2099
2100 /* Add the default argument for this parameter. */
2101 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2102 TREE_VALUE (spec_types),
2103 new_spec_types);
2104 }
2105
2106 /* DECL is an explicit specialization. Replicate default arguments
2107 from the template it specializes. (That way, code like:
2108
2109 template <class T> void f(T = 3);
2110 template <> void f(double);
2111 void g () { f (); }
2112
2113 works, as required.) An alternative approach would be to look up
2114 the correct default arguments at the call-site, but this approach
2115 is consistent with how implicit instantiations are handled. */
2116
2117 static void
2118 copy_default_args_to_explicit_spec (tree decl)
2119 {
2120 tree tmpl;
2121 tree spec_types;
2122 tree tmpl_types;
2123 tree new_spec_types;
2124 tree old_type;
2125 tree new_type;
2126 tree t;
2127 tree object_type = NULL_TREE;
2128 tree in_charge = NULL_TREE;
2129 tree vtt = NULL_TREE;
2130
2131 /* See if there's anything we need to do. */
2132 tmpl = DECL_TI_TEMPLATE (decl);
2133 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2134 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2135 if (TREE_PURPOSE (t))
2136 break;
2137 if (!t)
2138 return;
2139
2140 old_type = TREE_TYPE (decl);
2141 spec_types = TYPE_ARG_TYPES (old_type);
2142
2143 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2144 {
2145 /* Remove the this pointer, but remember the object's type for
2146 CV quals. */
2147 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2148 spec_types = TREE_CHAIN (spec_types);
2149 tmpl_types = TREE_CHAIN (tmpl_types);
2150
2151 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2152 {
2153 /* DECL may contain more parameters than TMPL due to the extra
2154 in-charge parameter in constructors and destructors. */
2155 in_charge = spec_types;
2156 spec_types = TREE_CHAIN (spec_types);
2157 }
2158 if (DECL_HAS_VTT_PARM_P (decl))
2159 {
2160 vtt = spec_types;
2161 spec_types = TREE_CHAIN (spec_types);
2162 }
2163 }
2164
2165 /* Compute the merged default arguments. */
2166 new_spec_types =
2167 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2168
2169 /* Compute the new FUNCTION_TYPE. */
2170 if (object_type)
2171 {
2172 if (vtt)
2173 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2174 TREE_VALUE (vtt),
2175 new_spec_types);
2176
2177 if (in_charge)
2178 /* Put the in-charge parameter back. */
2179 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2180 TREE_VALUE (in_charge),
2181 new_spec_types);
2182
2183 new_type = build_method_type_directly (object_type,
2184 TREE_TYPE (old_type),
2185 new_spec_types);
2186 }
2187 else
2188 new_type = build_function_type (TREE_TYPE (old_type),
2189 new_spec_types);
2190 new_type = cp_build_type_attribute_variant (new_type,
2191 TYPE_ATTRIBUTES (old_type));
2192 new_type = build_exception_variant (new_type,
2193 TYPE_RAISES_EXCEPTIONS (old_type));
2194 TREE_TYPE (decl) = new_type;
2195 }
2196
2197 /* Check to see if the function just declared, as indicated in
2198 DECLARATOR, and in DECL, is a specialization of a function
2199 template. We may also discover that the declaration is an explicit
2200 instantiation at this point.
2201
2202 Returns DECL, or an equivalent declaration that should be used
2203 instead if all goes well. Issues an error message if something is
2204 amiss. Returns error_mark_node if the error is not easily
2205 recoverable.
2206
2207 FLAGS is a bitmask consisting of the following flags:
2208
2209 2: The function has a definition.
2210 4: The function is a friend.
2211
2212 The TEMPLATE_COUNT is the number of references to qualifying
2213 template classes that appeared in the name of the function. For
2214 example, in
2215
2216 template <class T> struct S { void f(); };
2217 void S<int>::f();
2218
2219 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2220 classes are not counted in the TEMPLATE_COUNT, so that in
2221
2222 template <class T> struct S {};
2223 template <> struct S<int> { void f(); }
2224 template <> void S<int>::f();
2225
2226 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2227 invalid; there should be no template <>.)
2228
2229 If the function is a specialization, it is marked as such via
2230 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2231 is set up correctly, and it is added to the list of specializations
2232 for that template. */
2233
2234 tree
2235 check_explicit_specialization (tree declarator,
2236 tree decl,
2237 int template_count,
2238 int flags)
2239 {
2240 int have_def = flags & 2;
2241 int is_friend = flags & 4;
2242 int specialization = 0;
2243 int explicit_instantiation = 0;
2244 int member_specialization = 0;
2245 tree ctype = DECL_CLASS_CONTEXT (decl);
2246 tree dname = DECL_NAME (decl);
2247 tmpl_spec_kind tsk;
2248
2249 if (is_friend)
2250 {
2251 if (!processing_specialization)
2252 tsk = tsk_none;
2253 else
2254 tsk = tsk_excessive_parms;
2255 }
2256 else
2257 tsk = current_tmpl_spec_kind (template_count);
2258
2259 switch (tsk)
2260 {
2261 case tsk_none:
2262 if (processing_specialization)
2263 {
2264 specialization = 1;
2265 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2266 }
2267 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2268 {
2269 if (is_friend)
2270 /* This could be something like:
2271
2272 template <class T> void f(T);
2273 class S { friend void f<>(int); } */
2274 specialization = 1;
2275 else
2276 {
2277 /* This case handles bogus declarations like template <>
2278 template <class T> void f<int>(); */
2279
2280 error ("template-id %qD in declaration of primary template",
2281 declarator);
2282 return decl;
2283 }
2284 }
2285 break;
2286
2287 case tsk_invalid_member_spec:
2288 /* The error has already been reported in
2289 check_specialization_scope. */
2290 return error_mark_node;
2291
2292 case tsk_invalid_expl_inst:
2293 error ("template parameter list used in explicit instantiation");
2294
2295 /* Fall through. */
2296
2297 case tsk_expl_inst:
2298 if (have_def)
2299 error ("definition provided for explicit instantiation");
2300
2301 explicit_instantiation = 1;
2302 break;
2303
2304 case tsk_excessive_parms:
2305 case tsk_insufficient_parms:
2306 if (tsk == tsk_excessive_parms)
2307 error ("too many template parameter lists in declaration of %qD",
2308 decl);
2309 else if (template_header_count)
2310 error("too few template parameter lists in declaration of %qD", decl);
2311 else
2312 error("explicit specialization of %qD must be introduced by "
2313 "%<template <>%>", decl);
2314
2315 /* Fall through. */
2316 case tsk_expl_spec:
2317 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2318 if (ctype)
2319 member_specialization = 1;
2320 else
2321 specialization = 1;
2322 break;
2323
2324 case tsk_template:
2325 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2326 {
2327 /* This case handles bogus declarations like template <>
2328 template <class T> void f<int>(); */
2329
2330 if (uses_template_parms (declarator))
2331 error ("function template partial specialization %qD "
2332 "is not allowed", declarator);
2333 else
2334 error ("template-id %qD in declaration of primary template",
2335 declarator);
2336 return decl;
2337 }
2338
2339 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2340 /* This is a specialization of a member template, without
2341 specialization the containing class. Something like:
2342
2343 template <class T> struct S {
2344 template <class U> void f (U);
2345 };
2346 template <> template <class U> void S<int>::f(U) {}
2347
2348 That's a specialization -- but of the entire template. */
2349 specialization = 1;
2350 break;
2351
2352 default:
2353 gcc_unreachable ();
2354 }
2355
2356 if (specialization || member_specialization)
2357 {
2358 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2359 for (; t; t = TREE_CHAIN (t))
2360 if (TREE_PURPOSE (t))
2361 {
2362 permerror (input_location,
2363 "default argument specified in explicit specialization");
2364 break;
2365 }
2366 }
2367
2368 if (specialization || member_specialization || explicit_instantiation)
2369 {
2370 tree tmpl = NULL_TREE;
2371 tree targs = NULL_TREE;
2372
2373 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2374 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2375 {
2376 tree fns;
2377
2378 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2379 if (ctype)
2380 fns = dname;
2381 else
2382 {
2383 /* If there is no class context, the explicit instantiation
2384 must be at namespace scope. */
2385 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2386
2387 /* Find the namespace binding, using the declaration
2388 context. */
2389 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2390 false, true);
2391 if (fns == error_mark_node || !is_overloaded_fn (fns))
2392 {
2393 error ("%qD is not a template function", dname);
2394 fns = error_mark_node;
2395 }
2396 else
2397 {
2398 tree fn = OVL_CURRENT (fns);
2399 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2400 CP_DECL_CONTEXT (fn)))
2401 error ("%qD is not declared in %qD",
2402 decl, current_namespace);
2403 }
2404 }
2405
2406 declarator = lookup_template_function (fns, NULL_TREE);
2407 }
2408
2409 if (declarator == error_mark_node)
2410 return error_mark_node;
2411
2412 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2413 {
2414 if (!explicit_instantiation)
2415 /* A specialization in class scope. This is invalid,
2416 but the error will already have been flagged by
2417 check_specialization_scope. */
2418 return error_mark_node;
2419 else
2420 {
2421 /* It's not valid to write an explicit instantiation in
2422 class scope, e.g.:
2423
2424 class C { template void f(); }
2425
2426 This case is caught by the parser. However, on
2427 something like:
2428
2429 template class C { void f(); };
2430
2431 (which is invalid) we can get here. The error will be
2432 issued later. */
2433 ;
2434 }
2435
2436 return decl;
2437 }
2438 else if (ctype != NULL_TREE
2439 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2440 IDENTIFIER_NODE))
2441 {
2442 /* Find the list of functions in ctype that have the same
2443 name as the declared function. */
2444 tree name = TREE_OPERAND (declarator, 0);
2445 tree fns = NULL_TREE;
2446 int idx;
2447
2448 if (constructor_name_p (name, ctype))
2449 {
2450 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2451
2452 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2453 : !CLASSTYPE_DESTRUCTORS (ctype))
2454 {
2455 /* From [temp.expl.spec]:
2456
2457 If such an explicit specialization for the member
2458 of a class template names an implicitly-declared
2459 special member function (clause _special_), the
2460 program is ill-formed.
2461
2462 Similar language is found in [temp.explicit]. */
2463 error ("specialization of implicitly-declared special member function");
2464 return error_mark_node;
2465 }
2466
2467 name = is_constructor ? ctor_identifier : dtor_identifier;
2468 }
2469
2470 if (!DECL_CONV_FN_P (decl))
2471 {
2472 idx = lookup_fnfields_1 (ctype, name);
2473 if (idx >= 0)
2474 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2475 }
2476 else
2477 {
2478 VEC(tree,gc) *methods;
2479 tree ovl;
2480
2481 /* For a type-conversion operator, we cannot do a
2482 name-based lookup. We might be looking for `operator
2483 int' which will be a specialization of `operator T'.
2484 So, we find *all* the conversion operators, and then
2485 select from them. */
2486 fns = NULL_TREE;
2487
2488 methods = CLASSTYPE_METHOD_VEC (ctype);
2489 if (methods)
2490 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2491 VEC_iterate (tree, methods, idx, ovl);
2492 ++idx)
2493 {
2494 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2495 /* There are no more conversion functions. */
2496 break;
2497
2498 /* Glue all these conversion functions together
2499 with those we already have. */
2500 for (; ovl; ovl = OVL_NEXT (ovl))
2501 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2502 }
2503 }
2504
2505 if (fns == NULL_TREE)
2506 {
2507 error ("no member function %qD declared in %qT", name, ctype);
2508 return error_mark_node;
2509 }
2510 else
2511 TREE_OPERAND (declarator, 0) = fns;
2512 }
2513
2514 /* Figure out what exactly is being specialized at this point.
2515 Note that for an explicit instantiation, even one for a
2516 member function, we cannot tell apriori whether the
2517 instantiation is for a member template, or just a member
2518 function of a template class. Even if a member template is
2519 being instantiated, the member template arguments may be
2520 elided if they can be deduced from the rest of the
2521 declaration. */
2522 tmpl = determine_specialization (declarator, decl,
2523 &targs,
2524 member_specialization,
2525 template_count,
2526 tsk);
2527
2528 if (!tmpl || tmpl == error_mark_node)
2529 /* We couldn't figure out what this declaration was
2530 specializing. */
2531 return error_mark_node;
2532 else
2533 {
2534 tree gen_tmpl = most_general_template (tmpl);
2535
2536 if (explicit_instantiation)
2537 {
2538 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2539 is done by do_decl_instantiation later. */
2540
2541 int arg_depth = TMPL_ARGS_DEPTH (targs);
2542 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2543
2544 if (arg_depth > parm_depth)
2545 {
2546 /* If TMPL is not the most general template (for
2547 example, if TMPL is a friend template that is
2548 injected into namespace scope), then there will
2549 be too many levels of TARGS. Remove some of them
2550 here. */
2551 int i;
2552 tree new_targs;
2553
2554 new_targs = make_tree_vec (parm_depth);
2555 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2556 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2557 = TREE_VEC_ELT (targs, i);
2558 targs = new_targs;
2559 }
2560
2561 return instantiate_template (tmpl, targs, tf_error);
2562 }
2563
2564 /* If we thought that the DECL was a member function, but it
2565 turns out to be specializing a static member function,
2566 make DECL a static member function as well. */
2567 if (DECL_STATIC_FUNCTION_P (tmpl)
2568 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2569 revert_static_member_fn (decl);
2570
2571 /* If this is a specialization of a member template of a
2572 template class, we want to return the TEMPLATE_DECL, not
2573 the specialization of it. */
2574 if (tsk == tsk_template)
2575 {
2576 tree result = DECL_TEMPLATE_RESULT (tmpl);
2577 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2578 DECL_INITIAL (result) = NULL_TREE;
2579 if (have_def)
2580 {
2581 tree parm;
2582 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2583 DECL_SOURCE_LOCATION (result)
2584 = DECL_SOURCE_LOCATION (decl);
2585 /* We want to use the argument list specified in the
2586 definition, not in the original declaration. */
2587 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2588 for (parm = DECL_ARGUMENTS (result); parm;
2589 parm = DECL_CHAIN (parm))
2590 DECL_CONTEXT (parm) = result;
2591 }
2592 return register_specialization (tmpl, gen_tmpl, targs,
2593 is_friend, 0);
2594 }
2595
2596 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2597 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2598
2599 /* Inherit default function arguments from the template
2600 DECL is specializing. */
2601 copy_default_args_to_explicit_spec (decl);
2602
2603 /* This specialization has the same protection as the
2604 template it specializes. */
2605 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2606 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2607
2608 /* 7.1.1-1 [dcl.stc]
2609
2610 A storage-class-specifier shall not be specified in an
2611 explicit specialization...
2612
2613 The parser rejects these, so unless action is taken here,
2614 explicit function specializations will always appear with
2615 global linkage.
2616
2617 The action recommended by the C++ CWG in response to C++
2618 defect report 605 is to make the storage class and linkage
2619 of the explicit specialization match the templated function:
2620
2621 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2622 */
2623 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2624 {
2625 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2626 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2627
2628 /* This specialization has the same linkage and visibility as
2629 the function template it specializes. */
2630 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2631 if (! TREE_PUBLIC (decl))
2632 {
2633 DECL_INTERFACE_KNOWN (decl) = 1;
2634 DECL_NOT_REALLY_EXTERN (decl) = 1;
2635 }
2636 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2637 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2638 {
2639 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2640 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2641 }
2642 }
2643
2644 /* If DECL is a friend declaration, declared using an
2645 unqualified name, the namespace associated with DECL may
2646 have been set incorrectly. For example, in:
2647
2648 template <typename T> void f(T);
2649 namespace N {
2650 struct S { friend void f<int>(int); }
2651 }
2652
2653 we will have set the DECL_CONTEXT for the friend
2654 declaration to N, rather than to the global namespace. */
2655 if (DECL_NAMESPACE_SCOPE_P (decl))
2656 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2657
2658 if (is_friend && !have_def)
2659 /* This is not really a declaration of a specialization.
2660 It's just the name of an instantiation. But, it's not
2661 a request for an instantiation, either. */
2662 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2663 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2664 /* This is indeed a specialization. In case of constructors
2665 and destructors, we need in-charge and not-in-charge
2666 versions in V3 ABI. */
2667 clone_function_decl (decl, /*update_method_vec_p=*/0);
2668
2669 /* Register this specialization so that we can find it
2670 again. */
2671 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2672 }
2673 }
2674
2675 return decl;
2676 }
2677
2678 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2679 parameters. These are represented in the same format used for
2680 DECL_TEMPLATE_PARMS. */
2681
2682 int
2683 comp_template_parms (const_tree parms1, const_tree parms2)
2684 {
2685 const_tree p1;
2686 const_tree p2;
2687
2688 if (parms1 == parms2)
2689 return 1;
2690
2691 for (p1 = parms1, p2 = parms2;
2692 p1 != NULL_TREE && p2 != NULL_TREE;
2693 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2694 {
2695 tree t1 = TREE_VALUE (p1);
2696 tree t2 = TREE_VALUE (p2);
2697 int i;
2698
2699 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2700 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2701
2702 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2703 return 0;
2704
2705 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2706 {
2707 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2708 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2709
2710 /* If either of the template parameters are invalid, assume
2711 they match for the sake of error recovery. */
2712 if (parm1 == error_mark_node || parm2 == error_mark_node)
2713 return 1;
2714
2715 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2716 return 0;
2717
2718 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2719 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2720 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2721 continue;
2722 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2723 return 0;
2724 }
2725 }
2726
2727 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2728 /* One set of parameters has more parameters lists than the
2729 other. */
2730 return 0;
2731
2732 return 1;
2733 }
2734
2735 /* Determine whether PARM is a parameter pack. */
2736
2737 bool
2738 template_parameter_pack_p (const_tree parm)
2739 {
2740 /* Determine if we have a non-type template parameter pack. */
2741 if (TREE_CODE (parm) == PARM_DECL)
2742 return (DECL_TEMPLATE_PARM_P (parm)
2743 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2744
2745 /* If this is a list of template parameters, we could get a
2746 TYPE_DECL or a TEMPLATE_DECL. */
2747 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2748 parm = TREE_TYPE (parm);
2749
2750 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2751 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2752 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2753 }
2754
2755 /* Determine if T is a function parameter pack. */
2756
2757 bool
2758 function_parameter_pack_p (const_tree t)
2759 {
2760 if (t && TREE_CODE (t) == PARM_DECL)
2761 return FUNCTION_PARAMETER_PACK_P (t);
2762 return false;
2763 }
2764
2765 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2766 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2767
2768 tree
2769 get_function_template_decl (const_tree primary_func_tmpl_inst)
2770 {
2771 if (! primary_func_tmpl_inst
2772 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2773 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2774 return NULL;
2775
2776 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2777 }
2778
2779 /* Return true iff the function parameter PARAM_DECL was expanded
2780 from the function parameter pack PACK. */
2781
2782 bool
2783 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2784 {
2785 if (DECL_ARTIFICIAL (param_decl)
2786 || !function_parameter_pack_p (pack))
2787 return false;
2788
2789 /* The parameter pack and its pack arguments have the same
2790 DECL_PARM_INDEX. */
2791 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2792 }
2793
2794 /* Determine whether ARGS describes a variadic template args list,
2795 i.e., one that is terminated by a template argument pack. */
2796
2797 static bool
2798 template_args_variadic_p (tree args)
2799 {
2800 int nargs;
2801 tree last_parm;
2802
2803 if (args == NULL_TREE)
2804 return false;
2805
2806 args = INNERMOST_TEMPLATE_ARGS (args);
2807 nargs = TREE_VEC_LENGTH (args);
2808
2809 if (nargs == 0)
2810 return false;
2811
2812 last_parm = TREE_VEC_ELT (args, nargs - 1);
2813
2814 return ARGUMENT_PACK_P (last_parm);
2815 }
2816
2817 /* Generate a new name for the parameter pack name NAME (an
2818 IDENTIFIER_NODE) that incorporates its */
2819
2820 static tree
2821 make_ith_pack_parameter_name (tree name, int i)
2822 {
2823 /* Munge the name to include the parameter index. */
2824 #define NUMBUF_LEN 128
2825 char numbuf[NUMBUF_LEN];
2826 char* newname;
2827 int newname_len;
2828
2829 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2830 newname_len = IDENTIFIER_LENGTH (name)
2831 + strlen (numbuf) + 2;
2832 newname = (char*)alloca (newname_len);
2833 snprintf (newname, newname_len,
2834 "%s#%i", IDENTIFIER_POINTER (name), i);
2835 return get_identifier (newname);
2836 }
2837
2838 /* Return true if T is a primary function
2839 or class template instantiation. */
2840
2841 bool
2842 primary_template_instantiation_p (const_tree t)
2843 {
2844 if (!t)
2845 return false;
2846
2847 if (TREE_CODE (t) == FUNCTION_DECL)
2848 return DECL_LANG_SPECIFIC (t)
2849 && DECL_TEMPLATE_INSTANTIATION (t)
2850 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2851 else if (CLASS_TYPE_P (t))
2852 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2853 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2854 return false;
2855 }
2856
2857 /* Return true if PARM is a template template parameter. */
2858
2859 bool
2860 template_template_parameter_p (const_tree parm)
2861 {
2862 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2863 }
2864
2865 /* Return the template parameters of T if T is a
2866 primary template instantiation, NULL otherwise. */
2867
2868 tree
2869 get_primary_template_innermost_parameters (const_tree t)
2870 {
2871 tree parms = NULL, template_info = NULL;
2872
2873 if ((template_info = get_template_info (t))
2874 && primary_template_instantiation_p (t))
2875 parms = INNERMOST_TEMPLATE_PARMS
2876 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2877
2878 return parms;
2879 }
2880
2881 /* Return the template parameters of the LEVELth level from the full list
2882 of template parameters PARMS. */
2883
2884 tree
2885 get_template_parms_at_level (tree parms, int level)
2886 {
2887 tree p;
2888 if (!parms
2889 || TREE_CODE (parms) != TREE_LIST
2890 || level > TMPL_PARMS_DEPTH (parms))
2891 return NULL_TREE;
2892
2893 for (p = parms; p; p = TREE_CHAIN (p))
2894 if (TMPL_PARMS_DEPTH (p) == level)
2895 return p;
2896
2897 return NULL_TREE;
2898 }
2899
2900 /* Returns the template arguments of T if T is a template instantiation,
2901 NULL otherwise. */
2902
2903 tree
2904 get_template_innermost_arguments (const_tree t)
2905 {
2906 tree args = NULL, template_info = NULL;
2907
2908 if ((template_info = get_template_info (t))
2909 && TI_ARGS (template_info))
2910 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2911
2912 return args;
2913 }
2914
2915 /* Return the argument pack elements of T if T is a template argument pack,
2916 NULL otherwise. */
2917
2918 tree
2919 get_template_argument_pack_elems (const_tree t)
2920 {
2921 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2922 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2923 return NULL;
2924
2925 return ARGUMENT_PACK_ARGS (t);
2926 }
2927
2928 /* Structure used to track the progress of find_parameter_packs_r. */
2929 struct find_parameter_pack_data
2930 {
2931 /* TREE_LIST that will contain all of the parameter packs found by
2932 the traversal. */
2933 tree* parameter_packs;
2934
2935 /* Set of AST nodes that have been visited by the traversal. */
2936 struct pointer_set_t *visited;
2937 };
2938
2939 /* Identifies all of the argument packs that occur in a template
2940 argument and appends them to the TREE_LIST inside DATA, which is a
2941 find_parameter_pack_data structure. This is a subroutine of
2942 make_pack_expansion and uses_parameter_packs. */
2943 static tree
2944 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2945 {
2946 tree t = *tp;
2947 struct find_parameter_pack_data* ppd =
2948 (struct find_parameter_pack_data*)data;
2949 bool parameter_pack_p = false;
2950
2951 /* Identify whether this is a parameter pack or not. */
2952 switch (TREE_CODE (t))
2953 {
2954 case TEMPLATE_PARM_INDEX:
2955 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2956 parameter_pack_p = true;
2957 break;
2958
2959 case TEMPLATE_TYPE_PARM:
2960 case TEMPLATE_TEMPLATE_PARM:
2961 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2962 parameter_pack_p = true;
2963 break;
2964
2965 case PARM_DECL:
2966 if (FUNCTION_PARAMETER_PACK_P (t))
2967 {
2968 /* We don't want to walk into the type of a PARM_DECL,
2969 because we don't want to see the type parameter pack. */
2970 *walk_subtrees = 0;
2971 parameter_pack_p = true;
2972 }
2973 break;
2974
2975 default:
2976 /* Not a parameter pack. */
2977 break;
2978 }
2979
2980 if (parameter_pack_p)
2981 {
2982 /* Add this parameter pack to the list. */
2983 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2984 }
2985
2986 if (TYPE_P (t))
2987 cp_walk_tree (&TYPE_CONTEXT (t),
2988 &find_parameter_packs_r, ppd, ppd->visited);
2989
2990 /* This switch statement will return immediately if we don't find a
2991 parameter pack. */
2992 switch (TREE_CODE (t))
2993 {
2994 case TEMPLATE_PARM_INDEX:
2995 return NULL_TREE;
2996
2997 case BOUND_TEMPLATE_TEMPLATE_PARM:
2998 /* Check the template itself. */
2999 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3000 &find_parameter_packs_r, ppd, ppd->visited);
3001 /* Check the template arguments. */
3002 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3003 ppd->visited);
3004 *walk_subtrees = 0;
3005 return NULL_TREE;
3006
3007 case TEMPLATE_TYPE_PARM:
3008 case TEMPLATE_TEMPLATE_PARM:
3009 return NULL_TREE;
3010
3011 case PARM_DECL:
3012 return NULL_TREE;
3013
3014 case RECORD_TYPE:
3015 if (TYPE_PTRMEMFUNC_P (t))
3016 return NULL_TREE;
3017 /* Fall through. */
3018
3019 case UNION_TYPE:
3020 case ENUMERAL_TYPE:
3021 if (TYPE_TEMPLATE_INFO (t))
3022 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
3023 &find_parameter_packs_r, ppd, ppd->visited);
3024
3025 *walk_subtrees = 0;
3026 return NULL_TREE;
3027
3028 case TEMPLATE_DECL:
3029 cp_walk_tree (&TREE_TYPE (t),
3030 &find_parameter_packs_r, ppd, ppd->visited);
3031 return NULL_TREE;
3032
3033 case TYPENAME_TYPE:
3034 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3035 ppd, ppd->visited);
3036 *walk_subtrees = 0;
3037 return NULL_TREE;
3038
3039 case TYPE_PACK_EXPANSION:
3040 case EXPR_PACK_EXPANSION:
3041 *walk_subtrees = 0;
3042 return NULL_TREE;
3043
3044 case INTEGER_TYPE:
3045 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3046 ppd, ppd->visited);
3047 *walk_subtrees = 0;
3048 return NULL_TREE;
3049
3050 case IDENTIFIER_NODE:
3051 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3052 ppd->visited);
3053 *walk_subtrees = 0;
3054 return NULL_TREE;
3055
3056 default:
3057 return NULL_TREE;
3058 }
3059
3060 return NULL_TREE;
3061 }
3062
3063 /* Determines if the expression or type T uses any parameter packs. */
3064 bool
3065 uses_parameter_packs (tree t)
3066 {
3067 tree parameter_packs = NULL_TREE;
3068 struct find_parameter_pack_data ppd;
3069 ppd.parameter_packs = &parameter_packs;
3070 ppd.visited = pointer_set_create ();
3071 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3072 pointer_set_destroy (ppd.visited);
3073 return parameter_packs != NULL_TREE;
3074 }
3075
3076 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3077 representation a base-class initializer into a parameter pack
3078 expansion. If all goes well, the resulting node will be an
3079 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3080 respectively. */
3081 tree
3082 make_pack_expansion (tree arg)
3083 {
3084 tree result;
3085 tree parameter_packs = NULL_TREE;
3086 bool for_types = false;
3087 struct find_parameter_pack_data ppd;
3088
3089 if (!arg || arg == error_mark_node)
3090 return arg;
3091
3092 if (TREE_CODE (arg) == TREE_LIST)
3093 {
3094 /* The only time we will see a TREE_LIST here is for a base
3095 class initializer. In this case, the TREE_PURPOSE will be a
3096 _TYPE node (representing the base class expansion we're
3097 initializing) and the TREE_VALUE will be a TREE_LIST
3098 containing the initialization arguments.
3099
3100 The resulting expansion looks somewhat different from most
3101 expansions. Rather than returning just one _EXPANSION, we
3102 return a TREE_LIST whose TREE_PURPOSE is a
3103 TYPE_PACK_EXPANSION containing the bases that will be
3104 initialized. The TREE_VALUE will be identical to the
3105 original TREE_VALUE, which is a list of arguments that will
3106 be passed to each base. We do not introduce any new pack
3107 expansion nodes into the TREE_VALUE (although it is possible
3108 that some already exist), because the TREE_PURPOSE and
3109 TREE_VALUE all need to be expanded together with the same
3110 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3111 resulting TREE_PURPOSE will mention the parameter packs in
3112 both the bases and the arguments to the bases. */
3113 tree purpose;
3114 tree value;
3115 tree parameter_packs = NULL_TREE;
3116
3117 /* Determine which parameter packs will be used by the base
3118 class expansion. */
3119 ppd.visited = pointer_set_create ();
3120 ppd.parameter_packs = &parameter_packs;
3121 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3122 &ppd, ppd.visited);
3123
3124 if (parameter_packs == NULL_TREE)
3125 {
3126 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3127 pointer_set_destroy (ppd.visited);
3128 return error_mark_node;
3129 }
3130
3131 if (TREE_VALUE (arg) != void_type_node)
3132 {
3133 /* Collect the sets of parameter packs used in each of the
3134 initialization arguments. */
3135 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3136 {
3137 /* Determine which parameter packs will be expanded in this
3138 argument. */
3139 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3140 &ppd, ppd.visited);
3141 }
3142 }
3143
3144 pointer_set_destroy (ppd.visited);
3145
3146 /* Create the pack expansion type for the base type. */
3147 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3148 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3149 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3150
3151 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3152 they will rarely be compared to anything. */
3153 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3154
3155 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3156 }
3157
3158 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3159 for_types = true;
3160
3161 /* Build the PACK_EXPANSION_* node. */
3162 result = for_types
3163 ? cxx_make_type (TYPE_PACK_EXPANSION)
3164 : make_node (EXPR_PACK_EXPANSION);
3165 SET_PACK_EXPANSION_PATTERN (result, arg);
3166 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3167 {
3168 /* Propagate type and const-expression information. */
3169 TREE_TYPE (result) = TREE_TYPE (arg);
3170 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3171 }
3172 else
3173 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3174 they will rarely be compared to anything. */
3175 SET_TYPE_STRUCTURAL_EQUALITY (result);
3176
3177 /* Determine which parameter packs will be expanded. */
3178 ppd.parameter_packs = &parameter_packs;
3179 ppd.visited = pointer_set_create ();
3180 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3181 pointer_set_destroy (ppd.visited);
3182
3183 /* Make sure we found some parameter packs. */
3184 if (parameter_packs == NULL_TREE)
3185 {
3186 if (TYPE_P (arg))
3187 error ("expansion pattern %<%T%> contains no argument packs", arg);
3188 else
3189 error ("expansion pattern %<%E%> contains no argument packs", arg);
3190 return error_mark_node;
3191 }
3192 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3193
3194 return result;
3195 }
3196
3197 /* Checks T for any "bare" parameter packs, which have not yet been
3198 expanded, and issues an error if any are found. This operation can
3199 only be done on full expressions or types (e.g., an expression
3200 statement, "if" condition, etc.), because we could have expressions like:
3201
3202 foo(f(g(h(args)))...)
3203
3204 where "args" is a parameter pack. check_for_bare_parameter_packs
3205 should not be called for the subexpressions args, h(args),
3206 g(h(args)), or f(g(h(args))), because we would produce erroneous
3207 error messages.
3208
3209 Returns TRUE and emits an error if there were bare parameter packs,
3210 returns FALSE otherwise. */
3211 bool
3212 check_for_bare_parameter_packs (tree t)
3213 {
3214 tree parameter_packs = NULL_TREE;
3215 struct find_parameter_pack_data ppd;
3216
3217 if (!processing_template_decl || !t || t == error_mark_node)
3218 return false;
3219
3220 if (TREE_CODE (t) == TYPE_DECL)
3221 t = TREE_TYPE (t);
3222
3223 ppd.parameter_packs = &parameter_packs;
3224 ppd.visited = pointer_set_create ();
3225 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3226 pointer_set_destroy (ppd.visited);
3227
3228 if (parameter_packs)
3229 {
3230 error ("parameter packs not expanded with %<...%>:");
3231 while (parameter_packs)
3232 {
3233 tree pack = TREE_VALUE (parameter_packs);
3234 tree name = NULL_TREE;
3235
3236 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3237 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3238 name = TYPE_NAME (pack);
3239 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3240 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3241 else
3242 name = DECL_NAME (pack);
3243
3244 if (name)
3245 inform (input_location, " %qD", name);
3246 else
3247 inform (input_location, " <anonymous>");
3248
3249 parameter_packs = TREE_CHAIN (parameter_packs);
3250 }
3251
3252 return true;
3253 }
3254
3255 return false;
3256 }
3257
3258 /* Expand any parameter packs that occur in the template arguments in
3259 ARGS. */
3260 tree
3261 expand_template_argument_pack (tree args)
3262 {
3263 tree result_args = NULL_TREE;
3264 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3265 int num_result_args = -1;
3266 int non_default_args_count = -1;
3267
3268 /* First, determine if we need to expand anything, and the number of
3269 slots we'll need. */
3270 for (in_arg = 0; in_arg < nargs; ++in_arg)
3271 {
3272 tree arg = TREE_VEC_ELT (args, in_arg);
3273 if (arg == NULL_TREE)
3274 return args;
3275 if (ARGUMENT_PACK_P (arg))
3276 {
3277 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3278 if (num_result_args < 0)
3279 num_result_args = in_arg + num_packed;
3280 else
3281 num_result_args += num_packed;
3282 }
3283 else
3284 {
3285 if (num_result_args >= 0)
3286 num_result_args++;
3287 }
3288 }
3289
3290 /* If no expansion is necessary, we're done. */
3291 if (num_result_args < 0)
3292 return args;
3293
3294 /* Expand arguments. */
3295 result_args = make_tree_vec (num_result_args);
3296 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3297 non_default_args_count =
3298 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3299 for (in_arg = 0; in_arg < nargs; ++in_arg)
3300 {
3301 tree arg = TREE_VEC_ELT (args, in_arg);
3302 if (ARGUMENT_PACK_P (arg))
3303 {
3304 tree packed = ARGUMENT_PACK_ARGS (arg);
3305 int i, num_packed = TREE_VEC_LENGTH (packed);
3306 for (i = 0; i < num_packed; ++i, ++out_arg)
3307 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3308 if (non_default_args_count > 0)
3309 non_default_args_count += num_packed;
3310 }
3311 else
3312 {
3313 TREE_VEC_ELT (result_args, out_arg) = arg;
3314 ++out_arg;
3315 }
3316 }
3317 if (non_default_args_count >= 0)
3318 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3319 return result_args;
3320 }
3321
3322 /* Checks if DECL shadows a template parameter.
3323
3324 [temp.local]: A template-parameter shall not be redeclared within its
3325 scope (including nested scopes).
3326
3327 Emits an error and returns TRUE if the DECL shadows a parameter,
3328 returns FALSE otherwise. */
3329
3330 bool
3331 check_template_shadow (tree decl)
3332 {
3333 tree olddecl;
3334
3335 /* If we're not in a template, we can't possibly shadow a template
3336 parameter. */
3337 if (!current_template_parms)
3338 return true;
3339
3340 /* Figure out what we're shadowing. */
3341 if (TREE_CODE (decl) == OVERLOAD)
3342 decl = OVL_CURRENT (decl);
3343 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3344
3345 /* If there's no previous binding for this name, we're not shadowing
3346 anything, let alone a template parameter. */
3347 if (!olddecl)
3348 return true;
3349
3350 /* If we're not shadowing a template parameter, we're done. Note
3351 that OLDDECL might be an OVERLOAD (or perhaps even an
3352 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3353 node. */
3354 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3355 return true;
3356
3357 /* We check for decl != olddecl to avoid bogus errors for using a
3358 name inside a class. We check TPFI to avoid duplicate errors for
3359 inline member templates. */
3360 if (decl == olddecl
3361 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3362 return true;
3363
3364 error ("declaration of %q+#D", decl);
3365 error (" shadows template parm %q+#D", olddecl);
3366 return false;
3367 }
3368
3369 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3370 ORIG_LEVEL, DECL, and TYPE. NUM_SIBLINGS is the total number of
3371 template parameters. */
3372
3373 static tree
3374 build_template_parm_index (int index,
3375 int level,
3376 int orig_level,
3377 int num_siblings,
3378 tree decl,
3379 tree type)
3380 {
3381 tree t = make_node (TEMPLATE_PARM_INDEX);
3382 TEMPLATE_PARM_IDX (t) = index;
3383 TEMPLATE_PARM_LEVEL (t) = level;
3384 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3385 TEMPLATE_PARM_NUM_SIBLINGS (t) = num_siblings;
3386 TEMPLATE_PARM_DECL (t) = decl;
3387 TREE_TYPE (t) = type;
3388 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3389 TREE_READONLY (t) = TREE_READONLY (decl);
3390
3391 return t;
3392 }
3393
3394 /* Find the canonical type parameter for the given template type
3395 parameter. Returns the canonical type parameter, which may be TYPE
3396 if no such parameter existed. */
3397
3398 static tree
3399 canonical_type_parameter (tree type)
3400 {
3401 tree list;
3402 int idx = TEMPLATE_TYPE_IDX (type);
3403 if (!canonical_template_parms)
3404 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3405
3406 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3407 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3408
3409 list = VEC_index (tree, canonical_template_parms, idx);
3410 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3411 list = TREE_CHAIN (list);
3412
3413 if (list)
3414 return TREE_VALUE (list);
3415 else
3416 {
3417 VEC_replace(tree, canonical_template_parms, idx,
3418 tree_cons (NULL_TREE, type,
3419 VEC_index (tree, canonical_template_parms, idx)));
3420 return type;
3421 }
3422 }
3423
3424 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3425 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3426 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3427 new one is created. */
3428
3429 static tree
3430 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3431 tsubst_flags_t complain)
3432 {
3433 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3434 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3435 != TEMPLATE_PARM_LEVEL (index) - levels)
3436 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3437 {
3438 tree orig_decl = TEMPLATE_PARM_DECL (index);
3439 tree decl, t;
3440
3441 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3442 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3443 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3444 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3445 DECL_ARTIFICIAL (decl) = 1;
3446 SET_DECL_TEMPLATE_PARM_P (decl);
3447
3448 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3449 TEMPLATE_PARM_LEVEL (index) - levels,
3450 TEMPLATE_PARM_ORIG_LEVEL (index),
3451 TEMPLATE_PARM_NUM_SIBLINGS (index),
3452 decl, type);
3453 TEMPLATE_PARM_DESCENDANTS (index) = t;
3454 TEMPLATE_PARM_PARAMETER_PACK (t)
3455 = TEMPLATE_PARM_PARAMETER_PACK (index);
3456
3457 /* Template template parameters need this. */
3458 if (TREE_CODE (decl) == TEMPLATE_DECL)
3459 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3460 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3461 args, complain);
3462 }
3463
3464 return TEMPLATE_PARM_DESCENDANTS (index);
3465 }
3466
3467 /* Process information from new template parameter PARM and append it
3468 to the LIST being built. This new parameter is a non-type
3469 parameter iff IS_NON_TYPE is true. This new parameter is a
3470 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3471 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3472 parameter list PARM belongs to. This is used used to create a
3473 proper canonical type for the type of PARM that is to be created,
3474 iff PARM is a type. If the size is not known, this parameter shall
3475 be set to 0. */
3476
3477 tree
3478 process_template_parm (tree list, location_t parm_loc, tree parm,
3479 bool is_non_type, bool is_parameter_pack,
3480 unsigned num_template_parms)
3481 {
3482 tree decl = 0;
3483 tree defval;
3484 tree err_parm_list;
3485 int idx = 0;
3486
3487 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3488 defval = TREE_PURPOSE (parm);
3489
3490 if (list)
3491 {
3492 tree p = tree_last (list);
3493
3494 if (p && TREE_VALUE (p) != error_mark_node)
3495 {
3496 p = TREE_VALUE (p);
3497 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3498 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3499 else
3500 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3501 }
3502
3503 ++idx;
3504 }
3505 else
3506 idx = 0;
3507
3508 if (is_non_type)
3509 {
3510 parm = TREE_VALUE (parm);
3511
3512 SET_DECL_TEMPLATE_PARM_P (parm);
3513
3514 if (TREE_TYPE (parm) == error_mark_node)
3515 {
3516 err_parm_list = build_tree_list (defval, parm);
3517 TREE_VALUE (err_parm_list) = error_mark_node;
3518 return chainon (list, err_parm_list);
3519 }
3520 else
3521 {
3522 /* [temp.param]
3523
3524 The top-level cv-qualifiers on the template-parameter are
3525 ignored when determining its type. */
3526 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3527 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3528 {
3529 err_parm_list = build_tree_list (defval, parm);
3530 TREE_VALUE (err_parm_list) = error_mark_node;
3531 return chainon (list, err_parm_list);
3532 }
3533
3534 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3535 {
3536 /* This template parameter is not a parameter pack, but it
3537 should be. Complain about "bare" parameter packs. */
3538 check_for_bare_parameter_packs (TREE_TYPE (parm));
3539
3540 /* Recover by calling this a parameter pack. */
3541 is_parameter_pack = true;
3542 }
3543 }
3544
3545 /* A template parameter is not modifiable. */
3546 TREE_CONSTANT (parm) = 1;
3547 TREE_READONLY (parm) = 1;
3548 decl = build_decl (parm_loc,
3549 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3550 TREE_CONSTANT (decl) = 1;
3551 TREE_READONLY (decl) = 1;
3552 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3553 = build_template_parm_index (idx, processing_template_decl,
3554 processing_template_decl,
3555 num_template_parms,
3556 decl, TREE_TYPE (parm));
3557
3558 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3559 = is_parameter_pack;
3560 }
3561 else
3562 {
3563 tree t;
3564 parm = TREE_VALUE (TREE_VALUE (parm));
3565
3566 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3567 {
3568 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3569 /* This is for distinguishing between real templates and template
3570 template parameters */
3571 TREE_TYPE (parm) = t;
3572 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3573 decl = parm;
3574 }
3575 else
3576 {
3577 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3578 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3579 decl = build_decl (parm_loc,
3580 TYPE_DECL, parm, t);
3581 }
3582
3583 TYPE_NAME (t) = decl;
3584 TYPE_STUB_DECL (t) = decl;
3585 parm = decl;
3586 TEMPLATE_TYPE_PARM_INDEX (t)
3587 = build_template_parm_index (idx, processing_template_decl,
3588 processing_template_decl,
3589 num_template_parms,
3590 decl, TREE_TYPE (parm));
3591 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3592 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3593 }
3594 DECL_ARTIFICIAL (decl) = 1;
3595 SET_DECL_TEMPLATE_PARM_P (decl);
3596 pushdecl (decl);
3597 parm = build_tree_list (defval, parm);
3598 return chainon (list, parm);
3599 }
3600
3601 /* The end of a template parameter list has been reached. Process the
3602 tree list into a parameter vector, converting each parameter into a more
3603 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3604 as PARM_DECLs. */
3605
3606 tree
3607 end_template_parm_list (tree parms)
3608 {
3609 int nparms;
3610 tree parm, next;
3611 tree saved_parmlist = make_tree_vec (list_length (parms));
3612
3613 current_template_parms
3614 = tree_cons (size_int (processing_template_decl),
3615 saved_parmlist, current_template_parms);
3616
3617 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3618 {
3619 next = TREE_CHAIN (parm);
3620 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3621 TREE_CHAIN (parm) = NULL_TREE;
3622 }
3623
3624 --processing_template_parmlist;
3625
3626 return saved_parmlist;
3627 }
3628
3629 /* Create a new type almost identical to TYPE but which has the
3630 following differences:
3631
3632 1/ T has a new TEMPLATE_PARM_INDEX that carries the new number of
3633 template sibling parameters of T.
3634
3635 2/ T has a new canonical type that matches the new number
3636 of sibling parms.
3637
3638 3/ From now on, T is going to be what lookups referring to the
3639 name of TYPE will return. No lookup should return TYPE anymore.
3640
3641 NUM_PARMS is the new number of sibling parms TYPE belongs to.
3642
3643 This is a subroutine of fixup_template_parms. */
3644
3645 static tree
3646 fixup_template_type_parm_type (tree type, int num_parms)
3647 {
3648 tree orig_idx = TEMPLATE_TYPE_PARM_INDEX (type), idx;
3649 tree t;
3650 /* This is the decl which name is inserted into the symbol table for
3651 the template parm type. So whenever we lookup the type name, this
3652 is the DECL we get. */
3653 tree decl;
3654
3655 /* Do not fix up the type twice. */
3656 if (orig_idx && TEMPLATE_PARM_NUM_SIBLINGS (orig_idx) != 0)
3657 return type;
3658
3659 t = copy_type (type);
3660 decl = TYPE_NAME (t);
3661
3662 TYPE_MAIN_VARIANT (t) = t;
3663 TYPE_NEXT_VARIANT (t)= NULL_TREE;
3664 TYPE_POINTER_TO (t) = 0;
3665 TYPE_REFERENCE_TO (t) = 0;
3666
3667 idx = build_template_parm_index (TEMPLATE_PARM_IDX (orig_idx),
3668 TEMPLATE_PARM_LEVEL (orig_idx),
3669 TEMPLATE_PARM_ORIG_LEVEL (orig_idx),
3670 num_parms,
3671 decl, t);
3672 TEMPLATE_PARM_DESCENDANTS (idx) = TEMPLATE_PARM_DESCENDANTS (orig_idx);
3673 TEMPLATE_PARM_PARAMETER_PACK (idx) = TEMPLATE_PARM_PARAMETER_PACK (orig_idx);
3674 TEMPLATE_TYPE_PARM_INDEX (t) = idx;
3675
3676 TYPE_STUB_DECL (t) = decl;
3677 TEMPLATE_TYPE_DECL (t) = decl;
3678 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
3679 TREE_TYPE (DECL_TEMPLATE_RESULT (decl)) = t;
3680
3681 /* Update the type associated to the type name stored in the symbol
3682 table. Now, whenever the type name is looked up, the resulting
3683 type is properly fixed up. */
3684 TREE_TYPE (decl) = t;
3685
3686 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3687
3688 return t;
3689 }
3690
3691 /* Create and return a new TEMPLATE_PARM_INDEX that is almost
3692 identical to I, but that is fixed up as to:
3693
3694 1/ carry the number of sibling parms (NUM_PARMS) of the template
3695 parm represented by I.
3696
3697 2/ replace all references to template parm types declared before I
3698 (in the same template parm list as I) by references to template
3699 parm types contained in ARGS. ARGS should contain the list of
3700 template parms that have been fixed up so far, in a form suitable
3701 to be passed to tsubst.
3702
3703 This is a subroutine of fixup_template_parms. */
3704
3705 static tree
3706 fixup_template_parm_index (tree i, tree args, int num_parms)
3707 {
3708 tree index, decl, type;
3709
3710 if (i == NULL_TREE
3711 || TREE_CODE (i) != TEMPLATE_PARM_INDEX
3712 /* Do not fix up the index twice. */
3713 || (TEMPLATE_PARM_NUM_SIBLINGS (i) != 0))
3714 return i;
3715
3716 decl = TEMPLATE_PARM_DECL (i);
3717 type = TREE_TYPE (decl);
3718
3719 index = build_template_parm_index (TEMPLATE_PARM_IDX (i),
3720 TEMPLATE_PARM_LEVEL (i),
3721 TEMPLATE_PARM_ORIG_LEVEL (i),
3722 num_parms,
3723 decl, type);
3724
3725 TEMPLATE_PARM_DESCENDANTS (index) = TEMPLATE_PARM_DESCENDANTS (i);
3726 TEMPLATE_PARM_PARAMETER_PACK (index) = TEMPLATE_PARM_PARAMETER_PACK (i);
3727
3728 type = tsubst (type, args, tf_none, NULL_TREE);
3729
3730 TREE_TYPE (decl) = type;
3731 TREE_TYPE (index) = type;
3732
3733 return index;
3734 }
3735
3736 /*
3737 This is a subroutine of fixup_template_parms.
3738
3739 It computes the canonical type of the type of the template
3740 parameter PARM_DESC and update all references to that type so that
3741 they use the newly computed canonical type. No access check is
3742 performed during the fixup. PARM_DESC is a TREE_LIST which
3743 TREE_VALUE is the template parameter and its TREE_PURPOSE is the
3744 default argument of the template parm if any. IDX is the index of
3745 the template parameter, starting at 0. NUM_PARMS is the number of
3746 template parameters in the set PARM_DESC belongs to. ARGLIST is a
3747 TREE_VEC containing the full set of template parameters in a form
3748 suitable to be passed to substs functions as their ARGS
3749 argument. This is what current_template_args returns for a given
3750 template. The innermost vector of args in ARGLIST is the set of
3751 template parms that have been fixed up so far. This function adds
3752 the fixed up parameter into that vector. */
3753
3754 static void
3755 fixup_template_parm (tree parm_desc,
3756 int idx,
3757 int num_parms,
3758 tree arglist)
3759 {
3760 tree parm = TREE_VALUE (parm_desc);
3761 tree fixedup_args = INNERMOST_TEMPLATE_ARGS (arglist);
3762
3763 push_deferring_access_checks (dk_no_check);
3764
3765 if (TREE_CODE (parm) == TYPE_DECL)
3766 {
3767 /* PARM is a template type parameter. Fix up its type, add
3768 the fixed-up template parm to the vector of fixed-up
3769 template parms so far, and substitute the fixed-up
3770 template parms into the default argument of this
3771 parameter. */
3772 tree t =
3773 fixup_template_type_parm_type (TREE_TYPE (parm), num_parms);
3774 TREE_TYPE (parm) = t;
3775
3776 TREE_VEC_ELT (fixedup_args, idx) = template_parm_to_arg (parm_desc);
3777 }
3778 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3779 {
3780 /* PARM is a template template parameter. This is going to
3781 be interesting. */
3782 tree tparms, targs, innermost_args, t;
3783 int j;
3784
3785 /* First, fix up the parms of the template template parm
3786 because the parms are involved in defining the new canonical
3787 type of the template template parm. */
3788
3789 /* So we need to substitute the template parm types that have
3790 been fixed up so far into the template parms of this template
3791 template parm. E.g, consider this:
3792
3793 template<class T, template<T u> class TT> class S;
3794
3795 In this case we want to substitute T into the
3796 template parameters of TT.
3797
3798 So let's walk the template parms of PARM here, and
3799 tsubst ARGLIST into into each of the template
3800 parms. */
3801
3802 /* For this substitution we need to build the full set of
3803 template parameters and use that as arguments for the
3804 tsubsting function. */
3805 tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
3806
3807 /* This will contain the innermost parms of PARM into which
3808 we have substituted so far. */
3809 innermost_args = make_tree_vec (TREE_VEC_LENGTH (tparms));
3810 targs = add_to_template_args (arglist, innermost_args);
3811 for (j = 0; j < TREE_VEC_LENGTH (tparms); ++j)
3812 {
3813 tree parameter;
3814
3815 parameter = TREE_VEC_ELT (tparms, j);
3816
3817 /* INNERMOST_ARGS needs to have at least the same number
3818 of elements as the index PARAMETER, ortherwise
3819 tsubsting into PARAMETER will result in partially
3820 instantiating it, reducing its tempate parm
3821 level. Let's tactically fill INNERMOST_ARGS for that
3822 purpose. */
3823 TREE_VEC_ELT (innermost_args, j) =
3824 template_parm_to_arg (parameter);
3825
3826 fixup_template_parm (parameter, j,
3827 TREE_VEC_LENGTH (tparms),
3828 targs);
3829 }
3830
3831 /* Now fix up the type of the template template parm. */
3832
3833 t = fixup_template_type_parm_type (TREE_TYPE (parm), num_parms);
3834 TREE_TYPE (parm) = t;
3835
3836 TREE_VEC_ELT (fixedup_args, idx) =
3837 template_parm_to_arg (parm_desc);
3838 }
3839 else if (TREE_CODE (parm) == PARM_DECL)
3840 {
3841 /* PARM is a non-type template parameter. We need to:
3842
3843 * Fix up its TEMPLATE_PARM_INDEX to make it carry the
3844 proper number of sibling parameters.
3845
3846 * Make lookups of the template parameter return a reference
3847 to the fixed-up index. No lookup should return references
3848 to the former index anymore.
3849
3850 * Substitute the template parms that got fixed up so far
3851
3852 * into the type of PARM. */
3853
3854 tree index = DECL_INITIAL (parm);
3855
3856 /* PUSHED_DECL is the decl added to the symbol table with
3857 the name of the parameter. E,g:
3858
3859 template<class T, T u> //#0
3860 auto my_function(T t) -> decltype(u); //#1
3861
3862 Here, when looking up u at //#1, we get the decl of u
3863 resulting from the declaration in #0. This is what
3864 PUSHED_DECL is. We need to replace the reference to the
3865 old TEMPLATE_PARM_INDEX carried by PUSHED_DECL by the
3866 fixed-up TEMPLATE_PARM_INDEX. */
3867 tree pushed_decl = TEMPLATE_PARM_DECL (index);
3868
3869 /* Let's fix up the TEMPLATE_PARM_INDEX then. Note that we must
3870 fixup the type of PUSHED_DECL as well and luckily
3871 fixup_template_parm_index does it for us too. */
3872 tree fixed_up_index =
3873 fixup_template_parm_index (index, arglist, num_parms);
3874
3875 DECL_INITIAL (pushed_decl) = DECL_INITIAL (parm) = fixed_up_index;
3876
3877 /* Add this fixed up PARM to the template parms we've fixed
3878 up so far and use that to substitute the fixed-up
3879 template parms into the type of PARM. */
3880 TREE_VEC_ELT (fixedup_args, idx) =
3881 template_parm_to_arg (parm_desc);
3882 TREE_TYPE (parm) = tsubst (TREE_TYPE (parm), arglist,
3883 tf_none, NULL_TREE);
3884 }
3885
3886 TREE_PURPOSE (parm_desc) =
3887 tsubst_template_arg (TREE_PURPOSE (parm_desc),
3888 arglist, tf_none, parm);
3889
3890 pop_deferring_access_checks ();
3891 }
3892
3893 /* Walk the current template parms and properly compute the canonical
3894 types of the dependent types created during
3895 cp_parser_template_parameter_list. */
3896
3897 void
3898 fixup_template_parms (void)
3899 {
3900 tree arglist;
3901 tree parameter_vec;
3902 tree fixedup_args;
3903 int i, num_parms;
3904
3905 parameter_vec = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3906 if (parameter_vec == NULL_TREE)
3907 return;
3908
3909 num_parms = TREE_VEC_LENGTH (parameter_vec);
3910
3911 /* This vector contains the current innermost template parms that
3912 have been fixed up so far. The form of FIXEDUP_ARGS is suitable
3913 to be passed to tsubst* functions as their ARGS argument. */
3914 fixedup_args = make_tree_vec (num_parms);
3915
3916 /* This vector contains the full set of template parms in a form
3917 suitable to be passed to substs functions as their ARGS
3918 argument. */
3919 arglist = current_template_args ();
3920 arglist = add_outermost_template_args (arglist, fixedup_args);
3921
3922 /* Let's do the proper fixup now. */
3923 for (i = 0; i < num_parms; ++i)
3924 fixup_template_parm (TREE_VEC_ELT (parameter_vec, i),
3925 i, num_parms, arglist);
3926 }
3927
3928 /* end_template_decl is called after a template declaration is seen. */
3929
3930 void
3931 end_template_decl (void)
3932 {
3933 reset_specialization ();
3934
3935 if (! processing_template_decl)
3936 return;
3937
3938 /* This matches the pushlevel in begin_template_parm_list. */
3939 finish_scope ();
3940
3941 --processing_template_decl;
3942 current_template_parms = TREE_CHAIN (current_template_parms);
3943 }
3944
3945 /* Takes a TREE_LIST representing a template parameter and convert it
3946 into an argument suitable to be passed to the type substitution
3947 functions. Note that If the TREE_LIST contains an error_mark
3948 node, the returned argument is error_mark_node. */
3949
3950 static tree
3951 template_parm_to_arg (tree t)
3952 {
3953
3954 if (t == NULL_TREE
3955 || TREE_CODE (t) != TREE_LIST)
3956 return t;
3957
3958 if (error_operand_p (TREE_VALUE (t)))
3959 return error_mark_node;
3960
3961 t = TREE_VALUE (t);
3962
3963 if (TREE_CODE (t) == TYPE_DECL
3964 || TREE_CODE (t) == TEMPLATE_DECL)
3965 {
3966 t = TREE_TYPE (t);
3967
3968 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3969 {
3970 /* Turn this argument into a TYPE_ARGUMENT_PACK
3971 with a single element, which expands T. */
3972 tree vec = make_tree_vec (1);
3973 #ifdef ENABLE_CHECKING
3974 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3975 (vec, TREE_VEC_LENGTH (vec));
3976 #endif
3977 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3978
3979 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3980 SET_ARGUMENT_PACK_ARGS (t, vec);
3981 }
3982 }
3983 else
3984 {
3985 t = DECL_INITIAL (t);
3986
3987 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3988 {
3989 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3990 with a single element, which expands T. */
3991 tree vec = make_tree_vec (1);
3992 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3993 #ifdef ENABLE_CHECKING
3994 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3995 (vec, TREE_VEC_LENGTH (vec));
3996 #endif
3997 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3998
3999 t = make_node (NONTYPE_ARGUMENT_PACK);
4000 SET_ARGUMENT_PACK_ARGS (t, vec);
4001 TREE_TYPE (t) = type;
4002 }
4003 }
4004 return t;
4005 }
4006
4007 /* Within the declaration of a template, return all levels of template
4008 parameters that apply. The template parameters are represented as
4009 a TREE_VEC, in the form documented in cp-tree.h for template
4010 arguments. */
4011
4012 static tree
4013 current_template_args (void)
4014 {
4015 tree header;
4016 tree args = NULL_TREE;
4017 int length = TMPL_PARMS_DEPTH (current_template_parms);
4018 int l = length;
4019
4020 /* If there is only one level of template parameters, we do not
4021 create a TREE_VEC of TREE_VECs. Instead, we return a single
4022 TREE_VEC containing the arguments. */
4023 if (length > 1)
4024 args = make_tree_vec (length);
4025
4026 for (header = current_template_parms; header; header = TREE_CHAIN (header))
4027 {
4028 tree a = copy_node (TREE_VALUE (header));
4029 int i;
4030
4031 TREE_TYPE (a) = NULL_TREE;
4032 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4033 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4034
4035 #ifdef ENABLE_CHECKING
4036 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4037 #endif
4038
4039 if (length > 1)
4040 TREE_VEC_ELT (args, --l) = a;
4041 else
4042 args = a;
4043 }
4044
4045 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
4046 /* This can happen for template parms of a template template
4047 parameter, e.g:
4048
4049 template<template<class T, class U> class TT> struct S;
4050
4051 Consider the level of the parms of TT; T and U both have
4052 level 2; TT has no template parm of level 1. So in this case
4053 the first element of full_template_args is NULL_TREE. If we
4054 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
4055 of 2. This will make tsubst wrongly consider that T and U
4056 have level 1. Instead, let's create a dummy vector as the
4057 first element of full_template_args so that TMPL_ARG_DEPTH
4058 returns the correct depth for args. */
4059 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
4060 return args;
4061 }
4062
4063 /* Update the declared TYPE by doing any lookups which were thought to be
4064 dependent, but are not now that we know the SCOPE of the declarator. */
4065
4066 tree
4067 maybe_update_decl_type (tree orig_type, tree scope)
4068 {
4069 tree type = orig_type;
4070
4071 if (type == NULL_TREE)
4072 return type;
4073
4074 if (TREE_CODE (orig_type) == TYPE_DECL)
4075 type = TREE_TYPE (type);
4076
4077 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4078 && dependent_type_p (type)
4079 /* Don't bother building up the args in this case. */
4080 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4081 {
4082 /* tsubst in the args corresponding to the template parameters,
4083 including auto if present. Most things will be unchanged, but
4084 make_typename_type and tsubst_qualified_id will resolve
4085 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4086 tree args = current_template_args ();
4087 tree auto_node = type_uses_auto (type);
4088 tree pushed;
4089 if (auto_node)
4090 {
4091 tree auto_vec = make_tree_vec (1);
4092 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4093 args = add_to_template_args (args, auto_vec);
4094 }
4095 pushed = push_scope (scope);
4096 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4097 if (pushed)
4098 pop_scope (scope);
4099 }
4100
4101 if (type == error_mark_node)
4102 return orig_type;
4103
4104 if (TREE_CODE (orig_type) == TYPE_DECL)
4105 {
4106 if (same_type_p (type, TREE_TYPE (orig_type)))
4107 type = orig_type;
4108 else
4109 type = TYPE_NAME (type);
4110 }
4111 return type;
4112 }
4113
4114 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4115 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4116 a member template. Used by push_template_decl below. */
4117
4118 static tree
4119 build_template_decl (tree decl, tree parms, bool member_template_p)
4120 {
4121 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4122 DECL_TEMPLATE_PARMS (tmpl) = parms;
4123 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4124 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4125
4126 return tmpl;
4127 }
4128
4129 struct template_parm_data
4130 {
4131 /* The level of the template parameters we are currently
4132 processing. */
4133 int level;
4134
4135 /* The index of the specialization argument we are currently
4136 processing. */
4137 int current_arg;
4138
4139 /* An array whose size is the number of template parameters. The
4140 elements are nonzero if the parameter has been used in any one
4141 of the arguments processed so far. */
4142 int* parms;
4143
4144 /* An array whose size is the number of template arguments. The
4145 elements are nonzero if the argument makes use of template
4146 parameters of this level. */
4147 int* arg_uses_template_parms;
4148 };
4149
4150 /* Subroutine of push_template_decl used to see if each template
4151 parameter in a partial specialization is used in the explicit
4152 argument list. If T is of the LEVEL given in DATA (which is
4153 treated as a template_parm_data*), then DATA->PARMS is marked
4154 appropriately. */
4155
4156 static int
4157 mark_template_parm (tree t, void* data)
4158 {
4159 int level;
4160 int idx;
4161 struct template_parm_data* tpd = (struct template_parm_data*) data;
4162
4163 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4164 {
4165 level = TEMPLATE_PARM_LEVEL (t);
4166 idx = TEMPLATE_PARM_IDX (t);
4167 }
4168 else
4169 {
4170 level = TEMPLATE_TYPE_LEVEL (t);
4171 idx = TEMPLATE_TYPE_IDX (t);
4172 }
4173
4174 if (level == tpd->level)
4175 {
4176 tpd->parms[idx] = 1;
4177 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4178 }
4179
4180 /* Return zero so that for_each_template_parm will continue the
4181 traversal of the tree; we want to mark *every* template parm. */
4182 return 0;
4183 }
4184
4185 /* Process the partial specialization DECL. */
4186
4187 static tree
4188 process_partial_specialization (tree decl)
4189 {
4190 tree type = TREE_TYPE (decl);
4191 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4192 tree specargs = CLASSTYPE_TI_ARGS (type);
4193 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4194 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4195 tree inner_parms;
4196 tree inst;
4197 int nargs = TREE_VEC_LENGTH (inner_args);
4198 int ntparms;
4199 int i;
4200 bool did_error_intro = false;
4201 struct template_parm_data tpd;
4202 struct template_parm_data tpd2;
4203
4204 gcc_assert (current_template_parms);
4205
4206 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4207 ntparms = TREE_VEC_LENGTH (inner_parms);
4208
4209 /* We check that each of the template parameters given in the
4210 partial specialization is used in the argument list to the
4211 specialization. For example:
4212
4213 template <class T> struct S;
4214 template <class T> struct S<T*>;
4215
4216 The second declaration is OK because `T*' uses the template
4217 parameter T, whereas
4218
4219 template <class T> struct S<int>;
4220
4221 is no good. Even trickier is:
4222
4223 template <class T>
4224 struct S1
4225 {
4226 template <class U>
4227 struct S2;
4228 template <class U>
4229 struct S2<T>;
4230 };
4231
4232 The S2<T> declaration is actually invalid; it is a
4233 full-specialization. Of course,
4234
4235 template <class U>
4236 struct S2<T (*)(U)>;
4237
4238 or some such would have been OK. */
4239 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4240 tpd.parms = XALLOCAVEC (int, ntparms);
4241 memset (tpd.parms, 0, sizeof (int) * ntparms);
4242
4243 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4244 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4245 for (i = 0; i < nargs; ++i)
4246 {
4247 tpd.current_arg = i;
4248 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4249 &mark_template_parm,
4250 &tpd,
4251 NULL,
4252 /*include_nondeduced_p=*/false);
4253 }
4254 for (i = 0; i < ntparms; ++i)
4255 if (tpd.parms[i] == 0)
4256 {
4257 /* One of the template parms was not used in the
4258 specialization. */
4259 if (!did_error_intro)
4260 {
4261 error ("template parameters not used in partial specialization:");
4262 did_error_intro = true;
4263 }
4264
4265 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4266 }
4267
4268 if (did_error_intro)
4269 return error_mark_node;
4270
4271 /* [temp.class.spec]
4272
4273 The argument list of the specialization shall not be identical to
4274 the implicit argument list of the primary template. */
4275 if (comp_template_args
4276 (inner_args,
4277 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4278 (maintmpl)))))
4279 error ("partial specialization %qT does not specialize any template arguments", type);
4280
4281 /* [temp.class.spec]
4282
4283 A partially specialized non-type argument expression shall not
4284 involve template parameters of the partial specialization except
4285 when the argument expression is a simple identifier.
4286
4287 The type of a template parameter corresponding to a specialized
4288 non-type argument shall not be dependent on a parameter of the
4289 specialization.
4290
4291 Also, we verify that pack expansions only occur at the
4292 end of the argument list. */
4293 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4294 tpd2.parms = 0;
4295 for (i = 0; i < nargs; ++i)
4296 {
4297 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4298 tree arg = TREE_VEC_ELT (inner_args, i);
4299 tree packed_args = NULL_TREE;
4300 int j, len = 1;
4301
4302 if (ARGUMENT_PACK_P (arg))
4303 {
4304 /* Extract the arguments from the argument pack. We'll be
4305 iterating over these in the following loop. */
4306 packed_args = ARGUMENT_PACK_ARGS (arg);
4307 len = TREE_VEC_LENGTH (packed_args);
4308 }
4309
4310 for (j = 0; j < len; j++)
4311 {
4312 if (packed_args)
4313 /* Get the Jth argument in the parameter pack. */
4314 arg = TREE_VEC_ELT (packed_args, j);
4315
4316 if (PACK_EXPANSION_P (arg))
4317 {
4318 /* Pack expansions must come at the end of the
4319 argument list. */
4320 if ((packed_args && j < len - 1)
4321 || (!packed_args && i < nargs - 1))
4322 {
4323 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4324 error ("parameter pack argument %qE must be at the "
4325 "end of the template argument list", arg);
4326 else
4327 error ("parameter pack argument %qT must be at the "
4328 "end of the template argument list", arg);
4329 }
4330 }
4331
4332 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4333 /* We only care about the pattern. */
4334 arg = PACK_EXPANSION_PATTERN (arg);
4335
4336 if (/* These first two lines are the `non-type' bit. */
4337 !TYPE_P (arg)
4338 && TREE_CODE (arg) != TEMPLATE_DECL
4339 /* This next line is the `argument expression is not just a
4340 simple identifier' condition and also the `specialized
4341 non-type argument' bit. */
4342 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4343 {
4344 if ((!packed_args && tpd.arg_uses_template_parms[i])
4345 || (packed_args && uses_template_parms (arg)))
4346 error ("template argument %qE involves template parameter(s)",
4347 arg);
4348 else
4349 {
4350 /* Look at the corresponding template parameter,
4351 marking which template parameters its type depends
4352 upon. */
4353 tree type = TREE_TYPE (parm);
4354
4355 if (!tpd2.parms)
4356 {
4357 /* We haven't yet initialized TPD2. Do so now. */
4358 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4359 /* The number of parameters here is the number in the
4360 main template, which, as checked in the assertion
4361 above, is NARGS. */
4362 tpd2.parms = XALLOCAVEC (int, nargs);
4363 tpd2.level =
4364 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4365 }
4366
4367 /* Mark the template parameters. But this time, we're
4368 looking for the template parameters of the main
4369 template, not in the specialization. */
4370 tpd2.current_arg = i;
4371 tpd2.arg_uses_template_parms[i] = 0;
4372 memset (tpd2.parms, 0, sizeof (int) * nargs);
4373 for_each_template_parm (type,
4374 &mark_template_parm,
4375 &tpd2,
4376 NULL,
4377 /*include_nondeduced_p=*/false);
4378
4379 if (tpd2.arg_uses_template_parms [i])
4380 {
4381 /* The type depended on some template parameters.
4382 If they are fully specialized in the
4383 specialization, that's OK. */
4384 int j;
4385 int count = 0;
4386 for (j = 0; j < nargs; ++j)
4387 if (tpd2.parms[j] != 0
4388 && tpd.arg_uses_template_parms [j])
4389 ++count;
4390 if (count != 0)
4391 error_n (input_location, count,
4392 "type %qT of template argument %qE depends "
4393 "on a template parameter",
4394 "type %qT of template argument %qE depends "
4395 "on template parameters",
4396 type,
4397 arg);
4398 }
4399 }
4400 }
4401 }
4402 }
4403
4404 /* We should only get here once. */
4405 gcc_assert (!COMPLETE_TYPE_P (type));
4406
4407 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4408 = tree_cons (specargs, inner_parms,
4409 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4410 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4411
4412 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4413 inst = TREE_CHAIN (inst))
4414 {
4415 tree inst_type = TREE_VALUE (inst);
4416 if (COMPLETE_TYPE_P (inst_type)
4417 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4418 {
4419 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4420 if (spec && TREE_TYPE (spec) == type)
4421 permerror (input_location,
4422 "partial specialization of %qT after instantiation "
4423 "of %qT", type, inst_type);
4424 }
4425 }
4426
4427 return decl;
4428 }
4429
4430 /* Check that a template declaration's use of default arguments and
4431 parameter packs is not invalid. Here, PARMS are the template
4432 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4433 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4434 specialization.
4435
4436
4437 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4438 declaration (but not a definition); 1 indicates a declaration, 2
4439 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4440 emitted for extraneous default arguments.
4441
4442 Returns TRUE if there were no errors found, FALSE otherwise. */
4443
4444 bool
4445 check_default_tmpl_args (tree decl, tree parms, int is_primary,
4446 int is_partial, int is_friend_decl)
4447 {
4448 const char *msg;
4449 int last_level_to_check;
4450 tree parm_level;
4451 bool no_errors = true;
4452
4453 /* [temp.param]
4454
4455 A default template-argument shall not be specified in a
4456 function template declaration or a function template definition, nor
4457 in the template-parameter-list of the definition of a member of a
4458 class template. */
4459
4460 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4461 /* You can't have a function template declaration in a local
4462 scope, nor you can you define a member of a class template in a
4463 local scope. */
4464 return true;
4465
4466 if (current_class_type
4467 && !TYPE_BEING_DEFINED (current_class_type)
4468 && DECL_LANG_SPECIFIC (decl)
4469 && DECL_DECLARES_FUNCTION_P (decl)
4470 /* If this is either a friend defined in the scope of the class
4471 or a member function. */
4472 && (DECL_FUNCTION_MEMBER_P (decl)
4473 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4474 : DECL_FRIEND_CONTEXT (decl)
4475 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4476 : false)
4477 /* And, if it was a member function, it really was defined in
4478 the scope of the class. */
4479 && (!DECL_FUNCTION_MEMBER_P (decl)
4480 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4481 /* We already checked these parameters when the template was
4482 declared, so there's no need to do it again now. This function
4483 was defined in class scope, but we're processing it's body now
4484 that the class is complete. */
4485 return true;
4486
4487 /* Core issue 226 (C++0x only): the following only applies to class
4488 templates. */
4489 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4490 {
4491 /* [temp.param]
4492
4493 If a template-parameter has a default template-argument, all
4494 subsequent template-parameters shall have a default
4495 template-argument supplied. */
4496 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4497 {
4498 tree inner_parms = TREE_VALUE (parm_level);
4499 int ntparms = TREE_VEC_LENGTH (inner_parms);
4500 int seen_def_arg_p = 0;
4501 int i;
4502
4503 for (i = 0; i < ntparms; ++i)
4504 {
4505 tree parm = TREE_VEC_ELT (inner_parms, i);
4506
4507 if (parm == error_mark_node)
4508 continue;
4509
4510 if (TREE_PURPOSE (parm))
4511 seen_def_arg_p = 1;
4512 else if (seen_def_arg_p
4513 && !template_parameter_pack_p (TREE_VALUE (parm)))
4514 {
4515 error ("no default argument for %qD", TREE_VALUE (parm));
4516 /* For better subsequent error-recovery, we indicate that
4517 there should have been a default argument. */
4518 TREE_PURPOSE (parm) = error_mark_node;
4519 no_errors = false;
4520 }
4521 else if (is_primary
4522 && !is_partial
4523 && !is_friend_decl
4524 /* Don't complain about an enclosing partial
4525 specialization. */
4526 && parm_level == parms
4527 && TREE_CODE (decl) == TYPE_DECL
4528 && i < ntparms - 1
4529 && template_parameter_pack_p (TREE_VALUE (parm)))
4530 {
4531 /* A primary class template can only have one
4532 parameter pack, at the end of the template
4533 parameter list. */
4534
4535 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4536 error ("parameter pack %qE must be at the end of the"
4537 " template parameter list", TREE_VALUE (parm));
4538 else
4539 error ("parameter pack %qT must be at the end of the"
4540 " template parameter list",
4541 TREE_TYPE (TREE_VALUE (parm)));
4542
4543 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4544 = error_mark_node;
4545 no_errors = false;
4546 }
4547 }
4548 }
4549 }
4550
4551 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4552 || is_partial
4553 || !is_primary
4554 || is_friend_decl)
4555 /* For an ordinary class template, default template arguments are
4556 allowed at the innermost level, e.g.:
4557 template <class T = int>
4558 struct S {};
4559 but, in a partial specialization, they're not allowed even
4560 there, as we have in [temp.class.spec]:
4561
4562 The template parameter list of a specialization shall not
4563 contain default template argument values.
4564
4565 So, for a partial specialization, or for a function template
4566 (in C++98/C++03), we look at all of them. */
4567 ;
4568 else
4569 /* But, for a primary class template that is not a partial
4570 specialization we look at all template parameters except the
4571 innermost ones. */
4572 parms = TREE_CHAIN (parms);
4573
4574 /* Figure out what error message to issue. */
4575 if (is_friend_decl == 2)
4576 msg = G_("default template arguments may not be used in function template "
4577 "friend re-declaration");
4578 else if (is_friend_decl)
4579 msg = G_("default template arguments may not be used in function template "
4580 "friend declarations");
4581 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4582 msg = G_("default template arguments may not be used in function templates "
4583 "without -std=c++0x or -std=gnu++0x");
4584 else if (is_partial)
4585 msg = G_("default template arguments may not be used in "
4586 "partial specializations");
4587 else
4588 msg = G_("default argument for template parameter for class enclosing %qD");
4589
4590 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4591 /* If we're inside a class definition, there's no need to
4592 examine the parameters to the class itself. On the one
4593 hand, they will be checked when the class is defined, and,
4594 on the other, default arguments are valid in things like:
4595 template <class T = double>
4596 struct S { template <class U> void f(U); };
4597 Here the default argument for `S' has no bearing on the
4598 declaration of `f'. */
4599 last_level_to_check = template_class_depth (current_class_type) + 1;
4600 else
4601 /* Check everything. */
4602 last_level_to_check = 0;
4603
4604 for (parm_level = parms;
4605 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4606 parm_level = TREE_CHAIN (parm_level))
4607 {
4608 tree inner_parms = TREE_VALUE (parm_level);
4609 int i;
4610 int ntparms;
4611
4612 ntparms = TREE_VEC_LENGTH (inner_parms);
4613 for (i = 0; i < ntparms; ++i)
4614 {
4615 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4616 continue;
4617
4618 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4619 {
4620 if (msg)
4621 {
4622 no_errors = false;
4623 if (is_friend_decl == 2)
4624 return no_errors;
4625
4626 error (msg, decl);
4627 msg = 0;
4628 }
4629
4630 /* Clear out the default argument so that we are not
4631 confused later. */
4632 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4633 }
4634 }
4635
4636 /* At this point, if we're still interested in issuing messages,
4637 they must apply to classes surrounding the object declared. */
4638 if (msg)
4639 msg = G_("default argument for template parameter for class "
4640 "enclosing %qD");
4641 }
4642
4643 return no_errors;
4644 }
4645
4646 /* Worker for push_template_decl_real, called via
4647 for_each_template_parm. DATA is really an int, indicating the
4648 level of the parameters we are interested in. If T is a template
4649 parameter of that level, return nonzero. */
4650
4651 static int
4652 template_parm_this_level_p (tree t, void* data)
4653 {
4654 int this_level = *(int *)data;
4655 int level;
4656
4657 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4658 level = TEMPLATE_PARM_LEVEL (t);
4659 else
4660 level = TEMPLATE_TYPE_LEVEL (t);
4661 return level == this_level;
4662 }
4663
4664 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4665 parameters given by current_template_args, or reuses a
4666 previously existing one, if appropriate. Returns the DECL, or an
4667 equivalent one, if it is replaced via a call to duplicate_decls.
4668
4669 If IS_FRIEND is true, DECL is a friend declaration. */
4670
4671 tree
4672 push_template_decl_real (tree decl, bool is_friend)
4673 {
4674 tree tmpl;
4675 tree args;
4676 tree info;
4677 tree ctx;
4678 int primary;
4679 int is_partial;
4680 int new_template_p = 0;
4681 /* True if the template is a member template, in the sense of
4682 [temp.mem]. */
4683 bool member_template_p = false;
4684
4685 if (decl == error_mark_node || !current_template_parms)
4686 return error_mark_node;
4687
4688 /* See if this is a partial specialization. */
4689 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4690 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4691 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4692
4693 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4694 is_friend = true;
4695
4696 if (is_friend)
4697 /* For a friend, we want the context of the friend function, not
4698 the type of which it is a friend. */
4699 ctx = CP_DECL_CONTEXT (decl);
4700 else if (CP_DECL_CONTEXT (decl)
4701 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4702 /* In the case of a virtual function, we want the class in which
4703 it is defined. */
4704 ctx = CP_DECL_CONTEXT (decl);
4705 else
4706 /* Otherwise, if we're currently defining some class, the DECL
4707 is assumed to be a member of the class. */
4708 ctx = current_scope ();
4709
4710 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4711 ctx = NULL_TREE;
4712
4713 if (!DECL_CONTEXT (decl))
4714 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4715
4716 /* See if this is a primary template. */
4717 if (is_friend && ctx)
4718 /* A friend template that specifies a class context, i.e.
4719 template <typename T> friend void A<T>::f();
4720 is not primary. */
4721 primary = 0;
4722 else
4723 primary = template_parm_scope_p ();
4724
4725 if (primary)
4726 {
4727 if (DECL_CLASS_SCOPE_P (decl))
4728 member_template_p = true;
4729 if (TREE_CODE (decl) == TYPE_DECL
4730 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4731 {
4732 error ("template class without a name");
4733 return error_mark_node;
4734 }
4735 else if (TREE_CODE (decl) == FUNCTION_DECL)
4736 {
4737 if (DECL_DESTRUCTOR_P (decl))
4738 {
4739 /* [temp.mem]
4740
4741 A destructor shall not be a member template. */
4742 error ("destructor %qD declared as member template", decl);
4743 return error_mark_node;
4744 }
4745 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4746 && (!prototype_p (TREE_TYPE (decl))
4747 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4748 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4749 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4750 == void_list_node)))
4751 {
4752 /* [basic.stc.dynamic.allocation]
4753
4754 An allocation function can be a function
4755 template. ... Template allocation functions shall
4756 have two or more parameters. */
4757 error ("invalid template declaration of %qD", decl);
4758 return error_mark_node;
4759 }
4760 }
4761 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4762 && CLASS_TYPE_P (TREE_TYPE (decl)))
4763 /* OK */;
4764 else
4765 {
4766 error ("template declaration of %q#D", decl);
4767 return error_mark_node;
4768 }
4769 }
4770
4771 /* Check to see that the rules regarding the use of default
4772 arguments are not being violated. */
4773 check_default_tmpl_args (decl, current_template_parms,
4774 primary, is_partial, /*is_friend_decl=*/0);
4775
4776 /* Ensure that there are no parameter packs in the type of this
4777 declaration that have not been expanded. */
4778 if (TREE_CODE (decl) == FUNCTION_DECL)
4779 {
4780 /* Check each of the arguments individually to see if there are
4781 any bare parameter packs. */
4782 tree type = TREE_TYPE (decl);
4783 tree arg = DECL_ARGUMENTS (decl);
4784 tree argtype = TYPE_ARG_TYPES (type);
4785
4786 while (arg && argtype)
4787 {
4788 if (!FUNCTION_PARAMETER_PACK_P (arg)
4789 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4790 {
4791 /* This is a PARM_DECL that contains unexpanded parameter
4792 packs. We have already complained about this in the
4793 check_for_bare_parameter_packs call, so just replace
4794 these types with ERROR_MARK_NODE. */
4795 TREE_TYPE (arg) = error_mark_node;
4796 TREE_VALUE (argtype) = error_mark_node;
4797 }
4798
4799 arg = DECL_CHAIN (arg);
4800 argtype = TREE_CHAIN (argtype);
4801 }
4802
4803 /* Check for bare parameter packs in the return type and the
4804 exception specifiers. */
4805 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4806 /* Errors were already issued, set return type to int
4807 as the frontend doesn't expect error_mark_node as
4808 the return type. */
4809 TREE_TYPE (type) = integer_type_node;
4810 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4811 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4812 }
4813 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4814 {
4815 TREE_TYPE (decl) = error_mark_node;
4816 return error_mark_node;
4817 }
4818
4819 if (is_partial)
4820 return process_partial_specialization (decl);
4821
4822 args = current_template_args ();
4823
4824 if (!ctx
4825 || TREE_CODE (ctx) == FUNCTION_DECL
4826 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4827 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4828 {
4829 if (DECL_LANG_SPECIFIC (decl)
4830 && DECL_TEMPLATE_INFO (decl)
4831 && DECL_TI_TEMPLATE (decl))
4832 tmpl = DECL_TI_TEMPLATE (decl);
4833 /* If DECL is a TYPE_DECL for a class-template, then there won't
4834 be DECL_LANG_SPECIFIC. The information equivalent to
4835 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4836 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4837 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4838 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4839 {
4840 /* Since a template declaration already existed for this
4841 class-type, we must be redeclaring it here. Make sure
4842 that the redeclaration is valid. */
4843 redeclare_class_template (TREE_TYPE (decl),
4844 current_template_parms);
4845 /* We don't need to create a new TEMPLATE_DECL; just use the
4846 one we already had. */
4847 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4848 }
4849 else
4850 {
4851 tmpl = build_template_decl (decl, current_template_parms,
4852 member_template_p);
4853 new_template_p = 1;
4854
4855 if (DECL_LANG_SPECIFIC (decl)
4856 && DECL_TEMPLATE_SPECIALIZATION (decl))
4857 {
4858 /* A specialization of a member template of a template
4859 class. */
4860 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4861 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4862 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4863 }
4864 }
4865 }
4866 else
4867 {
4868 tree a, t, current, parms;
4869 int i;
4870 tree tinfo = get_template_info (decl);
4871
4872 if (!tinfo)
4873 {
4874 error ("template definition of non-template %q#D", decl);
4875 return error_mark_node;
4876 }
4877
4878 tmpl = TI_TEMPLATE (tinfo);
4879
4880 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4881 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4882 && DECL_TEMPLATE_SPECIALIZATION (decl)
4883 && DECL_MEMBER_TEMPLATE_P (tmpl))
4884 {
4885 tree new_tmpl;
4886
4887 /* The declaration is a specialization of a member
4888 template, declared outside the class. Therefore, the
4889 innermost template arguments will be NULL, so we
4890 replace them with the arguments determined by the
4891 earlier call to check_explicit_specialization. */
4892 args = DECL_TI_ARGS (decl);
4893
4894 new_tmpl
4895 = build_template_decl (decl, current_template_parms,
4896 member_template_p);
4897 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4898 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4899 DECL_TI_TEMPLATE (decl) = new_tmpl;
4900 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4901 DECL_TEMPLATE_INFO (new_tmpl)
4902 = build_template_info (tmpl, args);
4903
4904 register_specialization (new_tmpl,
4905 most_general_template (tmpl),
4906 args,
4907 is_friend, 0);
4908 return decl;
4909 }
4910
4911 /* Make sure the template headers we got make sense. */
4912
4913 parms = DECL_TEMPLATE_PARMS (tmpl);
4914 i = TMPL_PARMS_DEPTH (parms);
4915 if (TMPL_ARGS_DEPTH (args) != i)
4916 {
4917 error ("expected %d levels of template parms for %q#D, got %d",
4918 i, decl, TMPL_ARGS_DEPTH (args));
4919 }
4920 else
4921 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4922 {
4923 a = TMPL_ARGS_LEVEL (args, i);
4924 t = INNERMOST_TEMPLATE_PARMS (parms);
4925
4926 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4927 {
4928 if (current == decl)
4929 error ("got %d template parameters for %q#D",
4930 TREE_VEC_LENGTH (a), decl);
4931 else
4932 error ("got %d template parameters for %q#T",
4933 TREE_VEC_LENGTH (a), current);
4934 error (" but %d required", TREE_VEC_LENGTH (t));
4935 return error_mark_node;
4936 }
4937
4938 if (current == decl)
4939 current = ctx;
4940 else if (current == NULL_TREE)
4941 /* Can happen in erroneous input. */
4942 break;
4943 else
4944 current = (TYPE_P (current)
4945 ? TYPE_CONTEXT (current)
4946 : DECL_CONTEXT (current));
4947 }
4948
4949 /* Check that the parms are used in the appropriate qualifying scopes
4950 in the declarator. */
4951 if (!comp_template_args
4952 (TI_ARGS (tinfo),
4953 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4954 {
4955 error ("\
4956 template arguments to %qD do not match original template %qD",
4957 decl, DECL_TEMPLATE_RESULT (tmpl));
4958 if (!uses_template_parms (TI_ARGS (tinfo)))
4959 inform (input_location, "use template<> for an explicit specialization");
4960 /* Avoid crash in import_export_decl. */
4961 DECL_INTERFACE_KNOWN (decl) = 1;
4962 return error_mark_node;
4963 }
4964 }
4965
4966 DECL_TEMPLATE_RESULT (tmpl) = decl;
4967 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4968
4969 /* Push template declarations for global functions and types. Note
4970 that we do not try to push a global template friend declared in a
4971 template class; such a thing may well depend on the template
4972 parameters of the class. */
4973 if (new_template_p && !ctx
4974 && !(is_friend && template_class_depth (current_class_type) > 0))
4975 {
4976 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4977 if (tmpl == error_mark_node)
4978 return error_mark_node;
4979
4980 /* Hide template friend classes that haven't been declared yet. */
4981 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4982 {
4983 DECL_ANTICIPATED (tmpl) = 1;
4984 DECL_FRIEND_P (tmpl) = 1;
4985 }
4986 }
4987
4988 if (primary)
4989 {
4990 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4991 int i;
4992
4993 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4994 if (DECL_CONV_FN_P (tmpl))
4995 {
4996 int depth = TMPL_PARMS_DEPTH (parms);
4997
4998 /* It is a conversion operator. See if the type converted to
4999 depends on innermost template operands. */
5000
5001 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5002 depth))
5003 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5004 }
5005
5006 /* Give template template parms a DECL_CONTEXT of the template
5007 for which they are a parameter. */
5008 parms = INNERMOST_TEMPLATE_PARMS (parms);
5009 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5010 {
5011 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5012 if (TREE_CODE (parm) == TEMPLATE_DECL)
5013 DECL_CONTEXT (parm) = tmpl;
5014 }
5015 }
5016
5017 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5018 back to its most general template. If TMPL is a specialization,
5019 ARGS may only have the innermost set of arguments. Add the missing
5020 argument levels if necessary. */
5021 if (DECL_TEMPLATE_INFO (tmpl))
5022 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5023
5024 info = build_template_info (tmpl, args);
5025
5026 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5027 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5028 else if (DECL_LANG_SPECIFIC (decl))
5029 DECL_TEMPLATE_INFO (decl) = info;
5030
5031 return DECL_TEMPLATE_RESULT (tmpl);
5032 }
5033
5034 tree
5035 push_template_decl (tree decl)
5036 {
5037 return push_template_decl_real (decl, false);
5038 }
5039
5040 /* Called when a class template TYPE is redeclared with the indicated
5041 template PARMS, e.g.:
5042
5043 template <class T> struct S;
5044 template <class T> struct S {}; */
5045
5046 bool
5047 redeclare_class_template (tree type, tree parms)
5048 {
5049 tree tmpl;
5050 tree tmpl_parms;
5051 int i;
5052
5053 if (!TYPE_TEMPLATE_INFO (type))
5054 {
5055 error ("%qT is not a template type", type);
5056 return false;
5057 }
5058
5059 tmpl = TYPE_TI_TEMPLATE (type);
5060 if (!PRIMARY_TEMPLATE_P (tmpl))
5061 /* The type is nested in some template class. Nothing to worry
5062 about here; there are no new template parameters for the nested
5063 type. */
5064 return true;
5065
5066 if (!parms)
5067 {
5068 error ("template specifiers not specified in declaration of %qD",
5069 tmpl);
5070 return false;
5071 }
5072
5073 parms = INNERMOST_TEMPLATE_PARMS (parms);
5074 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5075
5076 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5077 {
5078 error_n (input_location, TREE_VEC_LENGTH (parms),
5079 "redeclared with %d template parameter",
5080 "redeclared with %d template parameters",
5081 TREE_VEC_LENGTH (parms));
5082 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5083 "previous declaration %q+D used %d template parameter",
5084 "previous declaration %q+D used %d template parameters",
5085 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5086 return false;
5087 }
5088
5089 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5090 {
5091 tree tmpl_parm;
5092 tree parm;
5093 tree tmpl_default;
5094 tree parm_default;
5095
5096 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5097 || TREE_VEC_ELT (parms, i) == error_mark_node)
5098 continue;
5099
5100 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5101 if (tmpl_parm == error_mark_node)
5102 return false;
5103
5104 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5105 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5106 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5107
5108 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5109 TEMPLATE_DECL. */
5110 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5111 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5112 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5113 || (TREE_CODE (tmpl_parm) != PARM_DECL
5114 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5115 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5116 || (TREE_CODE (tmpl_parm) == PARM_DECL
5117 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5118 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5119 {
5120 error ("template parameter %q+#D", tmpl_parm);
5121 error ("redeclared here as %q#D", parm);
5122 return false;
5123 }
5124
5125 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5126 {
5127 /* We have in [temp.param]:
5128
5129 A template-parameter may not be given default arguments
5130 by two different declarations in the same scope. */
5131 error_at (input_location, "redefinition of default argument for %q#D", parm);
5132 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5133 "original definition appeared here");
5134 return false;
5135 }
5136
5137 if (parm_default != NULL_TREE)
5138 /* Update the previous template parameters (which are the ones
5139 that will really count) with the new default value. */
5140 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5141 else if (tmpl_default != NULL_TREE)
5142 /* Update the new parameters, too; they'll be used as the
5143 parameters for any members. */
5144 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5145 }
5146
5147 return true;
5148 }
5149
5150 /* Simplify EXPR if it is a non-dependent expression. Returns the
5151 (possibly simplified) expression. */
5152
5153 static tree
5154 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5155 {
5156 if (expr == NULL_TREE)
5157 return NULL_TREE;
5158
5159 /* If we're in a template, but EXPR isn't value dependent, simplify
5160 it. We're supposed to treat:
5161
5162 template <typename T> void f(T[1 + 1]);
5163 template <typename T> void f(T[2]);
5164
5165 as two declarations of the same function, for example. */
5166 if (processing_template_decl
5167 && !type_dependent_expression_p (expr)
5168 && potential_constant_expression (expr)
5169 && !value_dependent_expression_p (expr))
5170 {
5171 HOST_WIDE_INT saved_processing_template_decl;
5172
5173 saved_processing_template_decl = processing_template_decl;
5174 processing_template_decl = 0;
5175 expr = tsubst_copy_and_build (expr,
5176 /*args=*/NULL_TREE,
5177 complain,
5178 /*in_decl=*/NULL_TREE,
5179 /*function_p=*/false,
5180 /*integral_constant_expression_p=*/true);
5181 processing_template_decl = saved_processing_template_decl;
5182 }
5183 return expr;
5184 }
5185
5186 tree
5187 fold_non_dependent_expr (tree expr)
5188 {
5189 return fold_non_dependent_expr_sfinae (expr, tf_error);
5190 }
5191
5192 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5193 must be a function or a pointer-to-function type, as specified
5194 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5195 and check that the resulting function has external linkage. */
5196
5197 static tree
5198 convert_nontype_argument_function (tree type, tree expr)
5199 {
5200 tree fns = expr;
5201 tree fn, fn_no_ptr;
5202
5203 fn = instantiate_type (type, fns, tf_none);
5204 if (fn == error_mark_node)
5205 return error_mark_node;
5206
5207 fn_no_ptr = fn;
5208 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5209 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5210 if (TREE_CODE (fn_no_ptr) == BASELINK)
5211 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5212
5213 /* [temp.arg.nontype]/1
5214
5215 A template-argument for a non-type, non-template template-parameter
5216 shall be one of:
5217 [...]
5218 -- the address of an object or function with external linkage. */
5219 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
5220 {
5221 error ("%qE is not a valid template argument for type %qT "
5222 "because function %qD has not external linkage",
5223 expr, type, fn_no_ptr);
5224 return NULL_TREE;
5225 }
5226
5227 return fn;
5228 }
5229
5230 /* Subroutine of convert_nontype_argument.
5231 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5232 Emit an error otherwise. */
5233
5234 static bool
5235 check_valid_ptrmem_cst_expr (tree type, tree expr,
5236 tsubst_flags_t complain)
5237 {
5238 STRIP_NOPS (expr);
5239 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5240 return true;
5241 if (complain & tf_error)
5242 {
5243 error ("%qE is not a valid template argument for type %qT",
5244 expr, type);
5245 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5246 }
5247 return false;
5248 }
5249
5250 /* Returns TRUE iff the address of OP is value-dependent.
5251
5252 14.6.2.4 [temp.dep.temp]:
5253 A non-integral non-type template-argument is dependent if its type is
5254 dependent or it has either of the following forms
5255 qualified-id
5256 & qualified-id
5257 and contains a nested-name-specifier which specifies a class-name that
5258 names a dependent type.
5259
5260 We generalize this to just say that the address of a member of a
5261 dependent class is value-dependent; the above doesn't cover the
5262 address of a static data member named with an unqualified-id. */
5263
5264 static bool
5265 has_value_dependent_address (tree op)
5266 {
5267 /* We could use get_inner_reference here, but there's no need;
5268 this is only relevant for template non-type arguments, which
5269 can only be expressed as &id-expression. */
5270 if (DECL_P (op))
5271 {
5272 tree ctx = CP_DECL_CONTEXT (op);
5273 if (TYPE_P (ctx) && dependent_type_p (ctx))
5274 return true;
5275 }
5276
5277 return false;
5278 }
5279
5280 /* The next set of functions are used for providing helpful explanatory
5281 diagnostics for failed overload resolution. Their messages should be
5282 indented by two spaces for consistency with the messages in
5283 call.c */
5284
5285 static int
5286 unify_success (bool explain_p ATTRIBUTE_UNUSED)
5287 {
5288 return 0;
5289 }
5290
5291 static int
5292 unify_parameter_deduction_failure (bool explain_p, tree parm)
5293 {
5294 if (explain_p)
5295 inform (input_location,
5296 " couldn't deduce template parameter %qD", parm);
5297 return 1;
5298 }
5299
5300 static int
5301 unify_invalid (bool explain_p ATTRIBUTE_UNUSED)
5302 {
5303 return 1;
5304 }
5305
5306 static int
5307 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5308 {
5309 if (explain_p)
5310 inform (input_location,
5311 " types %qT and %qT have incompatible cv-qualifiers",
5312 parm, arg);
5313 return 1;
5314 }
5315
5316 static int
5317 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5318 {
5319 if (explain_p)
5320 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5321 return 1;
5322 }
5323
5324 static int
5325 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5326 {
5327 if (explain_p)
5328 inform (input_location,
5329 " template parameter %qD is not a parameter pack, but "
5330 "argument %qD is",
5331 parm, arg);
5332 return 1;
5333 }
5334
5335 static int
5336 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5337 {
5338 if (explain_p)
5339 inform (input_location,
5340 " template argument %qE does not match "
5341 "pointer-to-member constant %qE",
5342 arg, parm);
5343 return 1;
5344 }
5345
5346 static int
5347 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5348 {
5349 if (explain_p)
5350 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5351 return 1;
5352 }
5353
5354 static int
5355 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5356 {
5357 if (explain_p)
5358 inform (input_location,
5359 " inconsistent parameter pack deduction with %qT and %qT",
5360 old_arg, new_arg);
5361 return 1;
5362 }
5363
5364 static int
5365 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5366 {
5367 if (explain_p)
5368 inform (input_location,
5369 " deduced conflicting types for parameter %qT (%qT and %qT)",
5370 parm, first, second);
5371 return 1;
5372 }
5373
5374 static int
5375 unify_vla_arg (bool explain_p, tree arg)
5376 {
5377 if (explain_p)
5378 inform (input_location,
5379 " variable-sized array type %qT is not "
5380 "a valid template argument",
5381 arg);
5382 return 1;
5383 }
5384
5385 static int
5386 unify_method_type_error (bool explain_p, tree arg)
5387 {
5388 if (explain_p)
5389 inform (input_location,
5390 " member function type %qT is not a valid template argument",
5391 arg);
5392 return 1;
5393 }
5394
5395 static int
5396 unify_arity (bool explain_p, int have, int wanted)
5397 {
5398 if (explain_p)
5399 inform_n (input_location, wanted,
5400 " candidate expects %d argument, %d provided",
5401 " candidate expects %d arguments, %d provided",
5402 wanted, have);
5403 return 1;
5404 }
5405
5406 static int
5407 unify_too_many_arguments (bool explain_p, int have, int wanted)
5408 {
5409 return unify_arity (explain_p, have, wanted);
5410 }
5411
5412 static int
5413 unify_too_few_arguments (bool explain_p, int have, int wanted)
5414 {
5415 return unify_arity (explain_p, have, wanted);
5416 }
5417
5418 static int
5419 unify_arg_conversion (bool explain_p, tree to_type,
5420 tree from_type, tree arg)
5421 {
5422 if (explain_p)
5423 inform (input_location, " cannot convert %qE (type %qT) to type %qT",
5424 arg, from_type, to_type);
5425 return 1;
5426 }
5427
5428 static int
5429 unify_no_common_base (bool explain_p, enum template_base_result r,
5430 tree parm, tree arg)
5431 {
5432 if (explain_p)
5433 switch (r)
5434 {
5435 case tbr_ambiguous_baseclass:
5436 inform (input_location, " %qT is an ambiguous base class of %qT",
5437 arg, parm);
5438 break;
5439 default:
5440 inform (input_location, " %qT is not derived from %qT", arg, parm);
5441 break;
5442 }
5443 return 1;
5444 }
5445
5446 static int
5447 unify_inconsistent_template_template_parameters (bool explain_p)
5448 {
5449 if (explain_p)
5450 inform (input_location,
5451 " template parameters of a template template argument are "
5452 "inconsistent with other deduced template arguments");
5453 return 1;
5454 }
5455
5456 static int
5457 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5458 {
5459 if (explain_p)
5460 inform (input_location,
5461 " can't deduce a template for %qT from non-template type %qT",
5462 parm, arg);
5463 return 1;
5464 }
5465
5466 static int
5467 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5468 {
5469 if (explain_p)
5470 inform (input_location,
5471 " template argument %qE does not match %qD", arg, parm);
5472 return 1;
5473 }
5474
5475 static int
5476 unify_overload_resolution_failure (bool explain_p, tree arg)
5477 {
5478 if (explain_p)
5479 inform (input_location,
5480 " could not resolve address from overloaded function %qE",
5481 arg);
5482 return 1;
5483 }
5484
5485 /* Attempt to convert the non-type template parameter EXPR to the
5486 indicated TYPE. If the conversion is successful, return the
5487 converted value. If the conversion is unsuccessful, return
5488 NULL_TREE if we issued an error message, or error_mark_node if we
5489 did not. We issue error messages for out-and-out bad template
5490 parameters, but not simply because the conversion failed, since we
5491 might be just trying to do argument deduction. Both TYPE and EXPR
5492 must be non-dependent.
5493
5494 The conversion follows the special rules described in
5495 [temp.arg.nontype], and it is much more strict than an implicit
5496 conversion.
5497
5498 This function is called twice for each template argument (see
5499 lookup_template_class for a more accurate description of this
5500 problem). This means that we need to handle expressions which
5501 are not valid in a C++ source, but can be created from the
5502 first call (for instance, casts to perform conversions). These
5503 hacks can go away after we fix the double coercion problem. */
5504
5505 static tree
5506 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5507 {
5508 tree expr_type;
5509
5510 /* Detect immediately string literals as invalid non-type argument.
5511 This special-case is not needed for correctness (we would easily
5512 catch this later), but only to provide better diagnostic for this
5513 common user mistake. As suggested by DR 100, we do not mention
5514 linkage issues in the diagnostic as this is not the point. */
5515 /* FIXME we're making this OK. */
5516 if (TREE_CODE (expr) == STRING_CST)
5517 {
5518 if (complain & tf_error)
5519 error ("%qE is not a valid template argument for type %qT "
5520 "because string literals can never be used in this context",
5521 expr, type);
5522 return NULL_TREE;
5523 }
5524
5525 /* Add the ADDR_EXPR now for the benefit of
5526 value_dependent_expression_p. */
5527 if (TYPE_PTROBV_P (type)
5528 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5529 expr = decay_conversion (expr);
5530
5531 /* If we are in a template, EXPR may be non-dependent, but still
5532 have a syntactic, rather than semantic, form. For example, EXPR
5533 might be a SCOPE_REF, rather than the VAR_DECL to which the
5534 SCOPE_REF refers. Preserving the qualifying scope is necessary
5535 so that access checking can be performed when the template is
5536 instantiated -- but here we need the resolved form so that we can
5537 convert the argument. */
5538 if (TYPE_REF_OBJ_P (type)
5539 && has_value_dependent_address (expr))
5540 /* If we want the address and it's value-dependent, don't fold. */;
5541 else if (!type_unknown_p (expr))
5542 expr = fold_non_dependent_expr_sfinae (expr, complain);
5543 if (error_operand_p (expr))
5544 return error_mark_node;
5545 expr_type = TREE_TYPE (expr);
5546 if (TREE_CODE (type) == REFERENCE_TYPE)
5547 expr = mark_lvalue_use (expr);
5548 else
5549 expr = mark_rvalue_use (expr);
5550
5551 /* HACK: Due to double coercion, we can get a
5552 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5553 which is the tree that we built on the first call (see
5554 below when coercing to reference to object or to reference to
5555 function). We just strip everything and get to the arg.
5556 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5557 for examples. */
5558 if (TREE_CODE (expr) == NOP_EXPR)
5559 {
5560 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5561 {
5562 /* ??? Maybe we could use convert_from_reference here, but we
5563 would need to relax its constraints because the NOP_EXPR
5564 could actually change the type to something more cv-qualified,
5565 and this is not folded by convert_from_reference. */
5566 tree addr = TREE_OPERAND (expr, 0);
5567 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
5568 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
5569 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
5570 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5571 (TREE_TYPE (expr_type),
5572 TREE_TYPE (TREE_TYPE (addr))));
5573
5574 expr = TREE_OPERAND (addr, 0);
5575 expr_type = TREE_TYPE (expr);
5576 }
5577
5578 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5579 parameter is a pointer to object, through decay and
5580 qualification conversion. Let's strip everything. */
5581 else if (TYPE_PTROBV_P (type))
5582 {
5583 STRIP_NOPS (expr);
5584 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
5585 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
5586 /* Skip the ADDR_EXPR only if it is part of the decay for
5587 an array. Otherwise, it is part of the original argument
5588 in the source code. */
5589 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
5590 expr = TREE_OPERAND (expr, 0);
5591 expr_type = TREE_TYPE (expr);
5592 }
5593 }
5594
5595 /* [temp.arg.nontype]/5, bullet 1
5596
5597 For a non-type template-parameter of integral or enumeration type,
5598 integral promotions (_conv.prom_) and integral conversions
5599 (_conv.integral_) are applied. */
5600 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5601 {
5602 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5603 t = maybe_constant_value (t);
5604 if (t != error_mark_node)
5605 expr = t;
5606
5607 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5608 return error_mark_node;
5609
5610 /* Notice that there are constant expressions like '4 % 0' which
5611 do not fold into integer constants. */
5612 if (TREE_CODE (expr) != INTEGER_CST)
5613 {
5614 if (complain & tf_error)
5615 {
5616 int errs = errorcount, warns = warningcount;
5617 expr = cxx_constant_value (expr);
5618 if (errorcount > errs || warningcount > warns)
5619 inform (EXPR_LOC_OR_HERE (expr),
5620 "in template argument for type %qT ", type);
5621 if (expr == error_mark_node)
5622 return NULL_TREE;
5623 /* else cxx_constant_value complained but gave us
5624 a real constant, so go ahead. */
5625 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5626 }
5627 else
5628 return NULL_TREE;
5629 }
5630 }
5631 /* [temp.arg.nontype]/5, bullet 2
5632
5633 For a non-type template-parameter of type pointer to object,
5634 qualification conversions (_conv.qual_) and the array-to-pointer
5635 conversion (_conv.array_) are applied. */
5636 else if (TYPE_PTROBV_P (type))
5637 {
5638 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5639
5640 A template-argument for a non-type, non-template template-parameter
5641 shall be one of: [...]
5642
5643 -- the name of a non-type template-parameter;
5644 -- the address of an object or function with external linkage, [...]
5645 expressed as "& id-expression" where the & is optional if the name
5646 refers to a function or array, or if the corresponding
5647 template-parameter is a reference.
5648
5649 Here, we do not care about functions, as they are invalid anyway
5650 for a parameter of type pointer-to-object. */
5651
5652 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5653 /* Non-type template parameters are OK. */
5654 ;
5655 else if (TREE_CODE (expr) != ADDR_EXPR
5656 && TREE_CODE (expr_type) != ARRAY_TYPE)
5657 {
5658 if (TREE_CODE (expr) == VAR_DECL)
5659 {
5660 error ("%qD is not a valid template argument "
5661 "because %qD is a variable, not the address of "
5662 "a variable",
5663 expr, expr);
5664 return NULL_TREE;
5665 }
5666 /* Other values, like integer constants, might be valid
5667 non-type arguments of some other type. */
5668 return error_mark_node;
5669 }
5670 else
5671 {
5672 tree decl;
5673
5674 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5675 ? TREE_OPERAND (expr, 0) : expr);
5676 if (TREE_CODE (decl) != VAR_DECL)
5677 {
5678 error ("%qE is not a valid template argument of type %qT "
5679 "because %qE is not a variable",
5680 expr, type, decl);
5681 return NULL_TREE;
5682 }
5683 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
5684 {
5685 error ("%qE is not a valid template argument of type %qT "
5686 "because %qD does not have external linkage",
5687 expr, type, decl);
5688 return NULL_TREE;
5689 }
5690 }
5691
5692 expr = decay_conversion (expr);
5693 if (expr == error_mark_node)
5694 return error_mark_node;
5695
5696 expr = perform_qualification_conversions (type, expr);
5697 if (expr == error_mark_node)
5698 return error_mark_node;
5699 }
5700 /* [temp.arg.nontype]/5, bullet 3
5701
5702 For a non-type template-parameter of type reference to object, no
5703 conversions apply. The type referred to by the reference may be more
5704 cv-qualified than the (otherwise identical) type of the
5705 template-argument. The template-parameter is bound directly to the
5706 template-argument, which must be an lvalue. */
5707 else if (TYPE_REF_OBJ_P (type))
5708 {
5709 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5710 expr_type))
5711 return error_mark_node;
5712
5713 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5714 {
5715 error ("%qE is not a valid template argument for type %qT "
5716 "because of conflicts in cv-qualification", expr, type);
5717 return NULL_TREE;
5718 }
5719
5720 if (!real_lvalue_p (expr))
5721 {
5722 error ("%qE is not a valid template argument for type %qT "
5723 "because it is not an lvalue", expr, type);
5724 return NULL_TREE;
5725 }
5726
5727 /* [temp.arg.nontype]/1
5728
5729 A template-argument for a non-type, non-template template-parameter
5730 shall be one of: [...]
5731
5732 -- the address of an object or function with external linkage. */
5733 if (TREE_CODE (expr) == INDIRECT_REF
5734 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5735 {
5736 expr = TREE_OPERAND (expr, 0);
5737 if (DECL_P (expr))
5738 {
5739 error ("%q#D is not a valid template argument for type %qT "
5740 "because a reference variable does not have a constant "
5741 "address", expr, type);
5742 return NULL_TREE;
5743 }
5744 }
5745
5746 if (!DECL_P (expr))
5747 {
5748 error ("%qE is not a valid template argument for type %qT "
5749 "because it is not an object with external linkage",
5750 expr, type);
5751 return NULL_TREE;
5752 }
5753
5754 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5755 {
5756 error ("%qE is not a valid template argument for type %qT "
5757 "because object %qD has not external linkage",
5758 expr, type, expr);
5759 return NULL_TREE;
5760 }
5761
5762 expr = build_nop (type, build_address (expr));
5763 }
5764 /* [temp.arg.nontype]/5, bullet 4
5765
5766 For a non-type template-parameter of type pointer to function, only
5767 the function-to-pointer conversion (_conv.func_) is applied. If the
5768 template-argument represents a set of overloaded functions (or a
5769 pointer to such), the matching function is selected from the set
5770 (_over.over_). */
5771 else if (TYPE_PTRFN_P (type))
5772 {
5773 /* If the argument is a template-id, we might not have enough
5774 context information to decay the pointer. */
5775 if (!type_unknown_p (expr_type))
5776 {
5777 expr = decay_conversion (expr);
5778 if (expr == error_mark_node)
5779 return error_mark_node;
5780 }
5781
5782 expr = convert_nontype_argument_function (type, expr);
5783 if (!expr || expr == error_mark_node)
5784 return expr;
5785
5786 if (TREE_CODE (expr) != ADDR_EXPR)
5787 {
5788 error ("%qE is not a valid template argument for type %qT", expr, type);
5789 error ("it must be the address of a function with external linkage");
5790 return NULL_TREE;
5791 }
5792 }
5793 /* [temp.arg.nontype]/5, bullet 5
5794
5795 For a non-type template-parameter of type reference to function, no
5796 conversions apply. If the template-argument represents a set of
5797 overloaded functions, the matching function is selected from the set
5798 (_over.over_). */
5799 else if (TYPE_REFFN_P (type))
5800 {
5801 if (TREE_CODE (expr) == ADDR_EXPR)
5802 {
5803 error ("%qE is not a valid template argument for type %qT "
5804 "because it is a pointer", expr, type);
5805 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5806 return NULL_TREE;
5807 }
5808
5809 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5810 if (!expr || expr == error_mark_node)
5811 return expr;
5812
5813 expr = build_nop (type, build_address (expr));
5814 }
5815 /* [temp.arg.nontype]/5, bullet 6
5816
5817 For a non-type template-parameter of type pointer to member function,
5818 no conversions apply. If the template-argument represents a set of
5819 overloaded member functions, the matching member function is selected
5820 from the set (_over.over_). */
5821 else if (TYPE_PTRMEMFUNC_P (type))
5822 {
5823 expr = instantiate_type (type, expr, tf_none);
5824 if (expr == error_mark_node)
5825 return error_mark_node;
5826
5827 /* [temp.arg.nontype] bullet 1 says the pointer to member
5828 expression must be a pointer-to-member constant. */
5829 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5830 return error_mark_node;
5831
5832 /* There is no way to disable standard conversions in
5833 resolve_address_of_overloaded_function (called by
5834 instantiate_type). It is possible that the call succeeded by
5835 converting &B::I to &D::I (where B is a base of D), so we need
5836 to reject this conversion here.
5837
5838 Actually, even if there was a way to disable standard conversions,
5839 it would still be better to reject them here so that we can
5840 provide a superior diagnostic. */
5841 if (!same_type_p (TREE_TYPE (expr), type))
5842 {
5843 error ("%qE is not a valid template argument for type %qT "
5844 "because it is of type %qT", expr, type,
5845 TREE_TYPE (expr));
5846 /* If we are just one standard conversion off, explain. */
5847 if (can_convert (type, TREE_TYPE (expr)))
5848 inform (input_location,
5849 "standard conversions are not allowed in this context");
5850 return NULL_TREE;
5851 }
5852 }
5853 /* [temp.arg.nontype]/5, bullet 7
5854
5855 For a non-type template-parameter of type pointer to data member,
5856 qualification conversions (_conv.qual_) are applied. */
5857 else if (TYPE_PTRMEM_P (type))
5858 {
5859 /* [temp.arg.nontype] bullet 1 says the pointer to member
5860 expression must be a pointer-to-member constant. */
5861 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5862 return error_mark_node;
5863
5864 expr = perform_qualification_conversions (type, expr);
5865 if (expr == error_mark_node)
5866 return expr;
5867 }
5868 /* A template non-type parameter must be one of the above. */
5869 else
5870 gcc_unreachable ();
5871
5872 /* Sanity check: did we actually convert the argument to the
5873 right type? */
5874 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5875 (type, TREE_TYPE (expr)));
5876 return expr;
5877 }
5878
5879 /* Subroutine of coerce_template_template_parms, which returns 1 if
5880 PARM_PARM and ARG_PARM match using the rule for the template
5881 parameters of template template parameters. Both PARM and ARG are
5882 template parameters; the rest of the arguments are the same as for
5883 coerce_template_template_parms.
5884 */
5885 static int
5886 coerce_template_template_parm (tree parm,
5887 tree arg,
5888 tsubst_flags_t complain,
5889 tree in_decl,
5890 tree outer_args)
5891 {
5892 if (arg == NULL_TREE || arg == error_mark_node
5893 || parm == NULL_TREE || parm == error_mark_node)
5894 return 0;
5895
5896 if (TREE_CODE (arg) != TREE_CODE (parm))
5897 return 0;
5898
5899 switch (TREE_CODE (parm))
5900 {
5901 case TEMPLATE_DECL:
5902 /* We encounter instantiations of templates like
5903 template <template <template <class> class> class TT>
5904 class C; */
5905 {
5906 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5907 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5908
5909 if (!coerce_template_template_parms
5910 (parmparm, argparm, complain, in_decl, outer_args))
5911 return 0;
5912 }
5913 /* Fall through. */
5914
5915 case TYPE_DECL:
5916 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5917 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5918 /* Argument is a parameter pack but parameter is not. */
5919 return 0;
5920 break;
5921
5922 case PARM_DECL:
5923 /* The tsubst call is used to handle cases such as
5924
5925 template <int> class C {};
5926 template <class T, template <T> class TT> class D {};
5927 D<int, C> d;
5928
5929 i.e. the parameter list of TT depends on earlier parameters. */
5930 if (!uses_template_parms (TREE_TYPE (arg))
5931 && !same_type_p
5932 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5933 TREE_TYPE (arg)))
5934 return 0;
5935
5936 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5937 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5938 /* Argument is a parameter pack but parameter is not. */
5939 return 0;
5940
5941 break;
5942
5943 default:
5944 gcc_unreachable ();
5945 }
5946
5947 return 1;
5948 }
5949
5950
5951 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5952 template template parameters. Both PARM_PARMS and ARG_PARMS are
5953 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5954 or PARM_DECL.
5955
5956 Consider the example:
5957 template <class T> class A;
5958 template<template <class U> class TT> class B;
5959
5960 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5961 the parameters to A, and OUTER_ARGS contains A. */
5962
5963 static int
5964 coerce_template_template_parms (tree parm_parms,
5965 tree arg_parms,
5966 tsubst_flags_t complain,
5967 tree in_decl,
5968 tree outer_args)
5969 {
5970 int nparms, nargs, i;
5971 tree parm, arg;
5972 int variadic_p = 0;
5973
5974 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5975 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5976
5977 nparms = TREE_VEC_LENGTH (parm_parms);
5978 nargs = TREE_VEC_LENGTH (arg_parms);
5979
5980 /* Determine whether we have a parameter pack at the end of the
5981 template template parameter's template parameter list. */
5982 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5983 {
5984 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5985
5986 if (parm == error_mark_node)
5987 return 0;
5988
5989 switch (TREE_CODE (parm))
5990 {
5991 case TEMPLATE_DECL:
5992 case TYPE_DECL:
5993 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5994 variadic_p = 1;
5995 break;
5996
5997 case PARM_DECL:
5998 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5999 variadic_p = 1;
6000 break;
6001
6002 default:
6003 gcc_unreachable ();
6004 }
6005 }
6006
6007 if (nargs != nparms
6008 && !(variadic_p && nargs >= nparms - 1))
6009 return 0;
6010
6011 /* Check all of the template parameters except the parameter pack at
6012 the end (if any). */
6013 for (i = 0; i < nparms - variadic_p; ++i)
6014 {
6015 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6016 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6017 continue;
6018
6019 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6020 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6021
6022 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6023 outer_args))
6024 return 0;
6025
6026 }
6027
6028 if (variadic_p)
6029 {
6030 /* Check each of the template parameters in the template
6031 argument against the template parameter pack at the end of
6032 the template template parameter. */
6033 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6034 return 0;
6035
6036 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6037
6038 for (; i < nargs; ++i)
6039 {
6040 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6041 continue;
6042
6043 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6044
6045 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6046 outer_args))
6047 return 0;
6048 }
6049 }
6050
6051 return 1;
6052 }
6053
6054 /* Verifies that the deduced template arguments (in TARGS) for the
6055 template template parameters (in TPARMS) represent valid bindings,
6056 by comparing the template parameter list of each template argument
6057 to the template parameter list of its corresponding template
6058 template parameter, in accordance with DR150. This
6059 routine can only be called after all template arguments have been
6060 deduced. It will return TRUE if all of the template template
6061 parameter bindings are okay, FALSE otherwise. */
6062 bool
6063 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6064 {
6065 int i, ntparms = TREE_VEC_LENGTH (tparms);
6066 bool ret = true;
6067
6068 /* We're dealing with template parms in this process. */
6069 ++processing_template_decl;
6070
6071 targs = INNERMOST_TEMPLATE_ARGS (targs);
6072
6073 for (i = 0; i < ntparms; ++i)
6074 {
6075 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6076 tree targ = TREE_VEC_ELT (targs, i);
6077
6078 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6079 {
6080 tree packed_args = NULL_TREE;
6081 int idx, len = 1;
6082
6083 if (ARGUMENT_PACK_P (targ))
6084 {
6085 /* Look inside the argument pack. */
6086 packed_args = ARGUMENT_PACK_ARGS (targ);
6087 len = TREE_VEC_LENGTH (packed_args);
6088 }
6089
6090 for (idx = 0; idx < len; ++idx)
6091 {
6092 tree targ_parms = NULL_TREE;
6093
6094 if (packed_args)
6095 /* Extract the next argument from the argument
6096 pack. */
6097 targ = TREE_VEC_ELT (packed_args, idx);
6098
6099 if (PACK_EXPANSION_P (targ))
6100 /* Look at the pattern of the pack expansion. */
6101 targ = PACK_EXPANSION_PATTERN (targ);
6102
6103 /* Extract the template parameters from the template
6104 argument. */
6105 if (TREE_CODE (targ) == TEMPLATE_DECL)
6106 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6107 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6108 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6109
6110 /* Verify that we can coerce the template template
6111 parameters from the template argument to the template
6112 parameter. This requires an exact match. */
6113 if (targ_parms
6114 && !coerce_template_template_parms
6115 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6116 targ_parms,
6117 tf_none,
6118 tparm,
6119 targs))
6120 {
6121 ret = false;
6122 goto out;
6123 }
6124 }
6125 }
6126 }
6127
6128 out:
6129
6130 --processing_template_decl;
6131 return ret;
6132 }
6133
6134 /* Since type attributes aren't mangled, we need to strip them from
6135 template type arguments. */
6136
6137 static tree
6138 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6139 {
6140 tree mv;
6141 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6142 return arg;
6143 mv = TYPE_MAIN_VARIANT (arg);
6144 arg = strip_typedefs (arg);
6145 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6146 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6147 {
6148 if (complain & tf_warning)
6149 warning (0, "ignoring attributes on template argument %qT", arg);
6150 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6151 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6152 }
6153 return arg;
6154 }
6155
6156 /* Convert the indicated template ARG as necessary to match the
6157 indicated template PARM. Returns the converted ARG, or
6158 error_mark_node if the conversion was unsuccessful. Error and
6159 warning messages are issued under control of COMPLAIN. This
6160 conversion is for the Ith parameter in the parameter list. ARGS is
6161 the full set of template arguments deduced so far. */
6162
6163 static tree
6164 convert_template_argument (tree parm,
6165 tree arg,
6166 tree args,
6167 tsubst_flags_t complain,
6168 int i,
6169 tree in_decl)
6170 {
6171 tree orig_arg;
6172 tree val;
6173 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6174
6175 if (TREE_CODE (arg) == TREE_LIST
6176 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6177 {
6178 /* The template argument was the name of some
6179 member function. That's usually
6180 invalid, but static members are OK. In any
6181 case, grab the underlying fields/functions
6182 and issue an error later if required. */
6183 orig_arg = TREE_VALUE (arg);
6184 TREE_TYPE (arg) = unknown_type_node;
6185 }
6186
6187 orig_arg = arg;
6188
6189 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6190 requires_type = (TREE_CODE (parm) == TYPE_DECL
6191 || requires_tmpl_type);
6192
6193 /* When determining whether an argument pack expansion is a template,
6194 look at the pattern. */
6195 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6196 arg = PACK_EXPANSION_PATTERN (arg);
6197
6198 /* Deal with an injected-class-name used as a template template arg. */
6199 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6200 {
6201 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6202 if (TREE_CODE (t) == TEMPLATE_DECL)
6203 {
6204 if (cxx_dialect >= cxx0x)
6205 /* OK under DR 1004. */;
6206 else if (complain & tf_warning_or_error)
6207 pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
6208 " used as template template argument", TYPE_NAME (arg));
6209 else if (flag_pedantic_errors)
6210 t = arg;
6211
6212 arg = t;
6213 }
6214 }
6215
6216 is_tmpl_type =
6217 ((TREE_CODE (arg) == TEMPLATE_DECL
6218 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6219 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6220 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6221
6222 if (is_tmpl_type
6223 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6224 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6225 arg = TYPE_STUB_DECL (arg);
6226
6227 is_type = TYPE_P (arg) || is_tmpl_type;
6228
6229 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6230 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6231 {
6232 permerror (input_location, "to refer to a type member of a template parameter, "
6233 "use %<typename %E%>", orig_arg);
6234
6235 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6236 TREE_OPERAND (arg, 1),
6237 typename_type,
6238 complain & tf_error);
6239 arg = orig_arg;
6240 is_type = 1;
6241 }
6242 if (is_type != requires_type)
6243 {
6244 if (in_decl)
6245 {
6246 if (complain & tf_error)
6247 {
6248 error ("type/value mismatch at argument %d in template "
6249 "parameter list for %qD",
6250 i + 1, in_decl);
6251 if (is_type)
6252 error (" expected a constant of type %qT, got %qT",
6253 TREE_TYPE (parm),
6254 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6255 else if (requires_tmpl_type)
6256 error (" expected a class template, got %qE", orig_arg);
6257 else
6258 error (" expected a type, got %qE", orig_arg);
6259 }
6260 }
6261 return error_mark_node;
6262 }
6263 if (is_tmpl_type ^ requires_tmpl_type)
6264 {
6265 if (in_decl && (complain & tf_error))
6266 {
6267 error ("type/value mismatch at argument %d in template "
6268 "parameter list for %qD",
6269 i + 1, in_decl);
6270 if (is_tmpl_type)
6271 error (" expected a type, got %qT", DECL_NAME (arg));
6272 else
6273 error (" expected a class template, got %qT", orig_arg);
6274 }
6275 return error_mark_node;
6276 }
6277
6278 if (is_type)
6279 {
6280 if (requires_tmpl_type)
6281 {
6282 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6283 /* The number of argument required is not known yet.
6284 Just accept it for now. */
6285 val = TREE_TYPE (arg);
6286 else
6287 {
6288 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6289 tree argparm;
6290
6291 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6292
6293 if (coerce_template_template_parms (parmparm, argparm,
6294 complain, in_decl,
6295 args))
6296 {
6297 val = arg;
6298
6299 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6300 TEMPLATE_DECL. */
6301 if (val != error_mark_node)
6302 {
6303 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6304 val = TREE_TYPE (val);
6305 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6306 val = make_pack_expansion (val);
6307 }
6308 }
6309 else
6310 {
6311 if (in_decl && (complain & tf_error))
6312 {
6313 error ("type/value mismatch at argument %d in "
6314 "template parameter list for %qD",
6315 i + 1, in_decl);
6316 error (" expected a template of type %qD, got %qT",
6317 parm, orig_arg);
6318 }
6319
6320 val = error_mark_node;
6321 }
6322 }
6323 }
6324 else
6325 val = orig_arg;
6326 /* We only form one instance of each template specialization.
6327 Therefore, if we use a non-canonical variant (i.e., a
6328 typedef), any future messages referring to the type will use
6329 the typedef, which is confusing if those future uses do not
6330 themselves also use the typedef. */
6331 if (TYPE_P (val))
6332 val = canonicalize_type_argument (val, complain);
6333 }
6334 else
6335 {
6336 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6337
6338 if (invalid_nontype_parm_type_p (t, complain))
6339 return error_mark_node;
6340
6341 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6342 {
6343 if (same_type_p (t, TREE_TYPE (orig_arg)))
6344 val = orig_arg;
6345 else
6346 {
6347 /* Not sure if this is reachable, but it doesn't hurt
6348 to be robust. */
6349 error ("type mismatch in nontype parameter pack");
6350 val = error_mark_node;
6351 }
6352 }
6353 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
6354 /* We used to call digest_init here. However, digest_init
6355 will report errors, which we don't want when complain
6356 is zero. More importantly, digest_init will try too
6357 hard to convert things: for example, `0' should not be
6358 converted to pointer type at this point according to
6359 the standard. Accepting this is not merely an
6360 extension, since deciding whether or not these
6361 conversions can occur is part of determining which
6362 function template to call, or whether a given explicit
6363 argument specification is valid. */
6364 val = convert_nontype_argument (t, orig_arg, complain);
6365 else
6366 val = orig_arg;
6367
6368 if (val == NULL_TREE)
6369 val = error_mark_node;
6370 else if (val == error_mark_node && (complain & tf_error))
6371 error ("could not convert template argument %qE to %qT", orig_arg, t);
6372
6373 if (TREE_CODE (val) == SCOPE_REF)
6374 {
6375 /* Strip typedefs from the SCOPE_REF. */
6376 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6377 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6378 complain);
6379 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6380 QUALIFIED_NAME_IS_TEMPLATE (val));
6381 }
6382 }
6383
6384 return val;
6385 }
6386
6387 /* Coerces the remaining template arguments in INNER_ARGS (from
6388 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6389 Returns the coerced argument pack. PARM_IDX is the position of this
6390 parameter in the template parameter list. ARGS is the original
6391 template argument list. */
6392 static tree
6393 coerce_template_parameter_pack (tree parms,
6394 int parm_idx,
6395 tree args,
6396 tree inner_args,
6397 int arg_idx,
6398 tree new_args,
6399 int* lost,
6400 tree in_decl,
6401 tsubst_flags_t complain)
6402 {
6403 tree parm = TREE_VEC_ELT (parms, parm_idx);
6404 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6405 tree packed_args;
6406 tree argument_pack;
6407 tree packed_types = NULL_TREE;
6408
6409 if (arg_idx > nargs)
6410 arg_idx = nargs;
6411
6412 packed_args = make_tree_vec (nargs - arg_idx);
6413
6414 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6415 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6416 {
6417 /* When the template parameter is a non-type template
6418 parameter pack whose type uses parameter packs, we need
6419 to look at each of the template arguments
6420 separately. Build a vector of the types for these
6421 non-type template parameters in PACKED_TYPES. */
6422 tree expansion
6423 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6424 packed_types = tsubst_pack_expansion (expansion, args,
6425 complain, in_decl);
6426
6427 if (packed_types == error_mark_node)
6428 return error_mark_node;
6429
6430 /* Check that we have the right number of arguments. */
6431 if (arg_idx < nargs
6432 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6433 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6434 {
6435 int needed_parms
6436 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6437 error ("wrong number of template arguments (%d, should be %d)",
6438 nargs, needed_parms);
6439 return error_mark_node;
6440 }
6441
6442 /* If we aren't able to check the actual arguments now
6443 (because they haven't been expanded yet), we can at least
6444 verify that all of the types used for the non-type
6445 template parameter pack are, in fact, valid for non-type
6446 template parameters. */
6447 if (arg_idx < nargs
6448 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6449 {
6450 int j, len = TREE_VEC_LENGTH (packed_types);
6451 for (j = 0; j < len; ++j)
6452 {
6453 tree t = TREE_VEC_ELT (packed_types, j);
6454 if (invalid_nontype_parm_type_p (t, complain))
6455 return error_mark_node;
6456 }
6457 }
6458 }
6459
6460 /* Convert the remaining arguments, which will be a part of the
6461 parameter pack "parm". */
6462 for (; arg_idx < nargs; ++arg_idx)
6463 {
6464 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6465 tree actual_parm = TREE_VALUE (parm);
6466
6467 if (packed_types && !PACK_EXPANSION_P (arg))
6468 {
6469 /* When we have a vector of types (corresponding to the
6470 non-type template parameter pack that uses parameter
6471 packs in its type, as mention above), and the
6472 argument is not an expansion (which expands to a
6473 currently unknown number of arguments), clone the
6474 parm and give it the next type in PACKED_TYPES. */
6475 actual_parm = copy_node (actual_parm);
6476 TREE_TYPE (actual_parm) =
6477 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6478 }
6479
6480 if (arg != error_mark_node)
6481 arg = convert_template_argument (actual_parm,
6482 arg, new_args, complain, parm_idx,
6483 in_decl);
6484 if (arg == error_mark_node)
6485 (*lost)++;
6486 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6487 }
6488
6489 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6490 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6491 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6492 else
6493 {
6494 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6495 TREE_TYPE (argument_pack)
6496 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6497 TREE_CONSTANT (argument_pack) = 1;
6498 }
6499
6500 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6501 #ifdef ENABLE_CHECKING
6502 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6503 TREE_VEC_LENGTH (packed_args));
6504 #endif
6505 return argument_pack;
6506 }
6507
6508 /* Convert all template arguments to their appropriate types, and
6509 return a vector containing the innermost resulting template
6510 arguments. If any error occurs, return error_mark_node. Error and
6511 warning messages are issued under control of COMPLAIN.
6512
6513 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6514 for arguments not specified in ARGS. Otherwise, if
6515 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6516 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6517 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6518 ARGS. */
6519
6520 static tree
6521 coerce_template_parms (tree parms,
6522 tree args,
6523 tree in_decl,
6524 tsubst_flags_t complain,
6525 bool require_all_args,
6526 bool use_default_args)
6527 {
6528 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6529 tree inner_args;
6530 tree new_args;
6531 tree new_inner_args;
6532 int saved_unevaluated_operand;
6533 int saved_inhibit_evaluation_warnings;
6534
6535 /* When used as a boolean value, indicates whether this is a
6536 variadic template parameter list. Since it's an int, we can also
6537 subtract it from nparms to get the number of non-variadic
6538 parameters. */
6539 int variadic_p = 0;
6540
6541 if (args == error_mark_node)
6542 return error_mark_node;
6543
6544 nparms = TREE_VEC_LENGTH (parms);
6545
6546 /* Determine if there are any parameter packs. */
6547 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6548 {
6549 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6550 if (template_parameter_pack_p (tparm))
6551 ++variadic_p;
6552 }
6553
6554 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6555 /* If there are 0 or 1 parameter packs, we need to expand any argument
6556 packs so that we can deduce a parameter pack from some non-packed args
6557 followed by an argument pack, as in variadic85.C. If there are more
6558 than that, we need to leave argument packs intact so the arguments are
6559 assigned to the right parameter packs. This should only happen when
6560 dealing with a nested class inside a partial specialization of a class
6561 template, as in variadic92.C. */
6562 if (variadic_p <= 1)
6563 inner_args = expand_template_argument_pack (inner_args);
6564
6565 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6566 if ((nargs > nparms && !variadic_p)
6567 || (nargs < nparms - variadic_p
6568 && require_all_args
6569 && (!use_default_args
6570 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6571 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6572 {
6573 if (complain & tf_error)
6574 {
6575 if (variadic_p)
6576 {
6577 --nparms;
6578 error ("wrong number of template arguments "
6579 "(%d, should be %d or more)", nargs, nparms);
6580 }
6581 else
6582 error ("wrong number of template arguments "
6583 "(%d, should be %d)", nargs, nparms);
6584
6585 if (in_decl)
6586 error ("provided for %q+D", in_decl);
6587 }
6588
6589 return error_mark_node;
6590 }
6591
6592 /* We need to evaluate the template arguments, even though this
6593 template-id may be nested within a "sizeof". */
6594 saved_unevaluated_operand = cp_unevaluated_operand;
6595 cp_unevaluated_operand = 0;
6596 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6597 c_inhibit_evaluation_warnings = 0;
6598 new_inner_args = make_tree_vec (nparms);
6599 new_args = add_outermost_template_args (args, new_inner_args);
6600 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6601 {
6602 tree arg;
6603 tree parm;
6604
6605 /* Get the Ith template parameter. */
6606 parm = TREE_VEC_ELT (parms, parm_idx);
6607
6608 if (parm == error_mark_node)
6609 {
6610 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6611 continue;
6612 }
6613
6614 /* Calculate the next argument. */
6615 if (arg_idx < nargs)
6616 arg = TREE_VEC_ELT (inner_args, arg_idx);
6617 else
6618 arg = NULL_TREE;
6619
6620 if (template_parameter_pack_p (TREE_VALUE (parm))
6621 && !(arg && ARGUMENT_PACK_P (arg)))
6622 {
6623 /* All remaining arguments will be placed in the
6624 template parameter pack PARM. */
6625 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6626 inner_args, arg_idx,
6627 new_args, &lost,
6628 in_decl, complain);
6629
6630 /* Store this argument. */
6631 if (arg == error_mark_node)
6632 lost++;
6633 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6634
6635 /* We are done with all of the arguments. */
6636 arg_idx = nargs;
6637
6638 continue;
6639 }
6640 else if (arg)
6641 {
6642 if (PACK_EXPANSION_P (arg))
6643 {
6644 if (complain & tf_error)
6645 {
6646 /* FIXME this restriction was removed by N2555; see
6647 bug 35722. */
6648 /* If ARG is a pack expansion, but PARM is not a
6649 template parameter pack (if it were, we would have
6650 handled it above), we're trying to expand into a
6651 fixed-length argument list. */
6652 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
6653 sorry ("cannot expand %<%E%> into a fixed-length "
6654 "argument list", arg);
6655 else
6656 sorry ("cannot expand %<%T%> into a fixed-length "
6657 "argument list", arg);
6658 }
6659 ++lost;
6660 }
6661 }
6662 else if (require_all_args)
6663 {
6664 /* There must be a default arg in this case. */
6665 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6666 complain, in_decl);
6667 /* The position of the first default template argument,
6668 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6669 Record that. */
6670 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6671 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6672 }
6673 else
6674 break;
6675
6676 if (arg == error_mark_node)
6677 {
6678 if (complain & tf_error)
6679 error ("template argument %d is invalid", arg_idx + 1);
6680 }
6681 else if (!arg)
6682 /* This only occurs if there was an error in the template
6683 parameter list itself (which we would already have
6684 reported) that we are trying to recover from, e.g., a class
6685 template with a parameter list such as
6686 template<typename..., typename>. */
6687 ++lost;
6688 else
6689 arg = convert_template_argument (TREE_VALUE (parm),
6690 arg, new_args, complain,
6691 parm_idx, in_decl);
6692
6693 if (arg == error_mark_node)
6694 lost++;
6695 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6696 }
6697 cp_unevaluated_operand = saved_unevaluated_operand;
6698 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6699
6700 if (lost)
6701 return error_mark_node;
6702
6703 #ifdef ENABLE_CHECKING
6704 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6705 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6706 TREE_VEC_LENGTH (new_inner_args));
6707 #endif
6708
6709 return new_inner_args;
6710 }
6711
6712 /* Returns 1 if template args OT and NT are equivalent. */
6713
6714 static int
6715 template_args_equal (tree ot, tree nt)
6716 {
6717 if (nt == ot)
6718 return 1;
6719 if (nt == NULL_TREE || ot == NULL_TREE)
6720 return false;
6721
6722 if (TREE_CODE (nt) == TREE_VEC)
6723 /* For member templates */
6724 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6725 else if (PACK_EXPANSION_P (ot))
6726 return PACK_EXPANSION_P (nt)
6727 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6728 PACK_EXPANSION_PATTERN (nt));
6729 else if (ARGUMENT_PACK_P (ot))
6730 {
6731 int i, len;
6732 tree opack, npack;
6733
6734 if (!ARGUMENT_PACK_P (nt))
6735 return 0;
6736
6737 opack = ARGUMENT_PACK_ARGS (ot);
6738 npack = ARGUMENT_PACK_ARGS (nt);
6739 len = TREE_VEC_LENGTH (opack);
6740 if (TREE_VEC_LENGTH (npack) != len)
6741 return 0;
6742 for (i = 0; i < len; ++i)
6743 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6744 TREE_VEC_ELT (npack, i)))
6745 return 0;
6746 return 1;
6747 }
6748 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6749 {
6750 /* We get here probably because we are in the middle of substituting
6751 into the pattern of a pack expansion. In that case the
6752 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6753 interested in. So we want to use the initial pack argument for
6754 the comparison. */
6755 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6756 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6757 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6758 return template_args_equal (ot, nt);
6759 }
6760 else if (TYPE_P (nt))
6761 return TYPE_P (ot) && same_type_p (ot, nt);
6762 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6763 return 0;
6764 else
6765 return cp_tree_equal (ot, nt);
6766 }
6767
6768 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6769 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6770 NEWARG_PTR with the offending arguments if they are non-NULL. */
6771
6772 static int
6773 comp_template_args_with_info (tree oldargs, tree newargs,
6774 tree *oldarg_ptr, tree *newarg_ptr)
6775 {
6776 int i;
6777
6778 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6779 return 0;
6780
6781 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6782 {
6783 tree nt = TREE_VEC_ELT (newargs, i);
6784 tree ot = TREE_VEC_ELT (oldargs, i);
6785
6786 if (! template_args_equal (ot, nt))
6787 {
6788 if (oldarg_ptr != NULL)
6789 *oldarg_ptr = ot;
6790 if (newarg_ptr != NULL)
6791 *newarg_ptr = nt;
6792 return 0;
6793 }
6794 }
6795 return 1;
6796 }
6797
6798 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6799 of template arguments. Returns 0 otherwise. */
6800
6801 int
6802 comp_template_args (tree oldargs, tree newargs)
6803 {
6804 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
6805 }
6806
6807 static void
6808 add_pending_template (tree d)
6809 {
6810 tree ti = (TYPE_P (d)
6811 ? CLASSTYPE_TEMPLATE_INFO (d)
6812 : DECL_TEMPLATE_INFO (d));
6813 struct pending_template *pt;
6814 int level;
6815
6816 if (TI_PENDING_TEMPLATE_FLAG (ti))
6817 return;
6818
6819 /* We are called both from instantiate_decl, where we've already had a
6820 tinst_level pushed, and instantiate_template, where we haven't.
6821 Compensate. */
6822 level = !current_tinst_level || current_tinst_level->decl != d;
6823
6824 if (level)
6825 push_tinst_level (d);
6826
6827 pt = ggc_alloc_pending_template ();
6828 pt->next = NULL;
6829 pt->tinst = current_tinst_level;
6830 if (last_pending_template)
6831 last_pending_template->next = pt;
6832 else
6833 pending_templates = pt;
6834
6835 last_pending_template = pt;
6836
6837 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6838
6839 if (level)
6840 pop_tinst_level ();
6841 }
6842
6843
6844 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6845 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6846 documentation for TEMPLATE_ID_EXPR. */
6847
6848 tree
6849 lookup_template_function (tree fns, tree arglist)
6850 {
6851 tree type;
6852
6853 if (fns == error_mark_node || arglist == error_mark_node)
6854 return error_mark_node;
6855
6856 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6857
6858 if (!is_overloaded_fn (fns) && TREE_CODE (fns) != IDENTIFIER_NODE)
6859 {
6860 error ("%q#D is not a function template", fns);
6861 return error_mark_node;
6862 }
6863
6864 if (BASELINK_P (fns))
6865 {
6866 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6867 unknown_type_node,
6868 BASELINK_FUNCTIONS (fns),
6869 arglist);
6870 return fns;
6871 }
6872
6873 type = TREE_TYPE (fns);
6874 if (TREE_CODE (fns) == OVERLOAD || !type)
6875 type = unknown_type_node;
6876
6877 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6878 }
6879
6880 /* Within the scope of a template class S<T>, the name S gets bound
6881 (in build_self_reference) to a TYPE_DECL for the class, not a
6882 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6883 or one of its enclosing classes, and that type is a template,
6884 return the associated TEMPLATE_DECL. Otherwise, the original
6885 DECL is returned.
6886
6887 Also handle the case when DECL is a TREE_LIST of ambiguous
6888 injected-class-names from different bases. */
6889
6890 tree
6891 maybe_get_template_decl_from_type_decl (tree decl)
6892 {
6893 if (decl == NULL_TREE)
6894 return decl;
6895
6896 /* DR 176: A lookup that finds an injected-class-name (10.2
6897 [class.member.lookup]) can result in an ambiguity in certain cases
6898 (for example, if it is found in more than one base class). If all of
6899 the injected-class-names that are found refer to specializations of
6900 the same class template, and if the name is followed by a
6901 template-argument-list, the reference refers to the class template
6902 itself and not a specialization thereof, and is not ambiguous. */
6903 if (TREE_CODE (decl) == TREE_LIST)
6904 {
6905 tree t, tmpl = NULL_TREE;
6906 for (t = decl; t; t = TREE_CHAIN (t))
6907 {
6908 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6909 if (!tmpl)
6910 tmpl = elt;
6911 else if (tmpl != elt)
6912 break;
6913 }
6914 if (tmpl && t == NULL_TREE)
6915 return tmpl;
6916 else
6917 return decl;
6918 }
6919
6920 return (decl != NULL_TREE
6921 && DECL_SELF_REFERENCE_P (decl)
6922 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6923 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6924 }
6925
6926 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6927 parameters, find the desired type.
6928
6929 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6930
6931 IN_DECL, if non-NULL, is the template declaration we are trying to
6932 instantiate.
6933
6934 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6935 the class we are looking up.
6936
6937 Issue error and warning messages under control of COMPLAIN.
6938
6939 If the template class is really a local class in a template
6940 function, then the FUNCTION_CONTEXT is the function in which it is
6941 being instantiated.
6942
6943 ??? Note that this function is currently called *twice* for each
6944 template-id: the first time from the parser, while creating the
6945 incomplete type (finish_template_type), and the second type during the
6946 real instantiation (instantiate_template_class). This is surely something
6947 that we want to avoid. It also causes some problems with argument
6948 coercion (see convert_nontype_argument for more information on this). */
6949
6950 static tree
6951 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
6952 int entering_scope, tsubst_flags_t complain)
6953 {
6954 tree templ = NULL_TREE, parmlist;
6955 tree t;
6956 spec_entry **slot;
6957 spec_entry *entry;
6958 spec_entry elt;
6959 hashval_t hash;
6960
6961 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6962 {
6963 tree value = innermost_non_namespace_value (d1);
6964 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6965 templ = value;
6966 else
6967 {
6968 if (context)
6969 push_decl_namespace (context);
6970 templ = lookup_name (d1);
6971 templ = maybe_get_template_decl_from_type_decl (templ);
6972 if (context)
6973 pop_decl_namespace ();
6974 }
6975 if (templ)
6976 context = DECL_CONTEXT (templ);
6977 }
6978 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6979 {
6980 tree type = TREE_TYPE (d1);
6981
6982 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6983 an implicit typename for the second A. Deal with it. */
6984 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6985 type = TREE_TYPE (type);
6986
6987 if (CLASSTYPE_TEMPLATE_INFO (type))
6988 {
6989 templ = CLASSTYPE_TI_TEMPLATE (type);
6990 d1 = DECL_NAME (templ);
6991 }
6992 }
6993 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6994 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6995 {
6996 templ = TYPE_TI_TEMPLATE (d1);
6997 d1 = DECL_NAME (templ);
6998 }
6999 else if (TREE_CODE (d1) == TEMPLATE_DECL
7000 && DECL_TEMPLATE_RESULT (d1)
7001 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7002 {
7003 templ = d1;
7004 d1 = DECL_NAME (templ);
7005 context = DECL_CONTEXT (templ);
7006 }
7007
7008 /* Issue an error message if we didn't find a template. */
7009 if (! templ)
7010 {
7011 if (complain & tf_error)
7012 error ("%qT is not a template", d1);
7013 return error_mark_node;
7014 }
7015
7016 if (TREE_CODE (templ) != TEMPLATE_DECL
7017 /* Make sure it's a user visible template, if it was named by
7018 the user. */
7019 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7020 && !PRIMARY_TEMPLATE_P (templ)))
7021 {
7022 if (complain & tf_error)
7023 {
7024 error ("non-template type %qT used as a template", d1);
7025 if (in_decl)
7026 error ("for template declaration %q+D", in_decl);
7027 }
7028 return error_mark_node;
7029 }
7030
7031 complain &= ~tf_user;
7032
7033 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7034 {
7035 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7036 template arguments */
7037
7038 tree parm;
7039 tree arglist2;
7040 tree outer;
7041
7042 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7043
7044 /* Consider an example where a template template parameter declared as
7045
7046 template <class T, class U = std::allocator<T> > class TT
7047
7048 The template parameter level of T and U are one level larger than
7049 of TT. To proper process the default argument of U, say when an
7050 instantiation `TT<int>' is seen, we need to build the full
7051 arguments containing {int} as the innermost level. Outer levels,
7052 available when not appearing as default template argument, can be
7053 obtained from the arguments of the enclosing template.
7054
7055 Suppose that TT is later substituted with std::vector. The above
7056 instantiation is `TT<int, std::allocator<T> >' with TT at
7057 level 1, and T at level 2, while the template arguments at level 1
7058 becomes {std::vector} and the inner level 2 is {int}. */
7059
7060 outer = DECL_CONTEXT (templ);
7061 if (outer)
7062 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7063 else if (current_template_parms)
7064 /* This is an argument of the current template, so we haven't set
7065 DECL_CONTEXT yet. */
7066 outer = current_template_args ();
7067
7068 if (outer)
7069 arglist = add_to_template_args (outer, arglist);
7070
7071 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7072 complain,
7073 /*require_all_args=*/true,
7074 /*use_default_args=*/true);
7075 if (arglist2 == error_mark_node
7076 || (!uses_template_parms (arglist2)
7077 && check_instantiated_args (templ, arglist2, complain)))
7078 return error_mark_node;
7079
7080 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7081 return parm;
7082 }
7083 else
7084 {
7085 tree template_type = TREE_TYPE (templ);
7086 tree gen_tmpl;
7087 tree type_decl;
7088 tree found = NULL_TREE;
7089 int arg_depth;
7090 int parm_depth;
7091 int is_dependent_type;
7092 int use_partial_inst_tmpl = false;
7093
7094 gen_tmpl = most_general_template (templ);
7095 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7096 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7097 arg_depth = TMPL_ARGS_DEPTH (arglist);
7098
7099 if (arg_depth == 1 && parm_depth > 1)
7100 {
7101 /* We've been given an incomplete set of template arguments.
7102 For example, given:
7103
7104 template <class T> struct S1 {
7105 template <class U> struct S2 {};
7106 template <class U> struct S2<U*> {};
7107 };
7108
7109 we will be called with an ARGLIST of `U*', but the
7110 TEMPLATE will be `template <class T> template
7111 <class U> struct S1<T>::S2'. We must fill in the missing
7112 arguments. */
7113 arglist
7114 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7115 arglist);
7116 arg_depth = TMPL_ARGS_DEPTH (arglist);
7117 }
7118
7119 /* Now we should have enough arguments. */
7120 gcc_assert (parm_depth == arg_depth);
7121
7122 /* From here on, we're only interested in the most general
7123 template. */
7124
7125 /* Calculate the BOUND_ARGS. These will be the args that are
7126 actually tsubst'd into the definition to create the
7127 instantiation. */
7128 if (parm_depth > 1)
7129 {
7130 /* We have multiple levels of arguments to coerce, at once. */
7131 int i;
7132 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7133
7134 tree bound_args = make_tree_vec (parm_depth);
7135
7136 for (i = saved_depth,
7137 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7138 i > 0 && t != NULL_TREE;
7139 --i, t = TREE_CHAIN (t))
7140 {
7141 tree a;
7142 if (i == saved_depth)
7143 a = coerce_template_parms (TREE_VALUE (t),
7144 arglist, gen_tmpl,
7145 complain,
7146 /*require_all_args=*/true,
7147 /*use_default_args=*/true);
7148 else
7149 /* Outer levels should have already been coerced. */
7150 a = TMPL_ARGS_LEVEL (arglist, i);
7151
7152 /* Don't process further if one of the levels fails. */
7153 if (a == error_mark_node)
7154 {
7155 /* Restore the ARGLIST to its full size. */
7156 TREE_VEC_LENGTH (arglist) = saved_depth;
7157 return error_mark_node;
7158 }
7159
7160 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7161
7162 /* We temporarily reduce the length of the ARGLIST so
7163 that coerce_template_parms will see only the arguments
7164 corresponding to the template parameters it is
7165 examining. */
7166 TREE_VEC_LENGTH (arglist)--;
7167 }
7168
7169 /* Restore the ARGLIST to its full size. */
7170 TREE_VEC_LENGTH (arglist) = saved_depth;
7171
7172 arglist = bound_args;
7173 }
7174 else
7175 arglist
7176 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7177 INNERMOST_TEMPLATE_ARGS (arglist),
7178 gen_tmpl,
7179 complain,
7180 /*require_all_args=*/true,
7181 /*use_default_args=*/true);
7182
7183 if (arglist == error_mark_node)
7184 /* We were unable to bind the arguments. */
7185 return error_mark_node;
7186
7187 /* In the scope of a template class, explicit references to the
7188 template class refer to the type of the template, not any
7189 instantiation of it. For example, in:
7190
7191 template <class T> class C { void f(C<T>); }
7192
7193 the `C<T>' is just the same as `C'. Outside of the
7194 class, however, such a reference is an instantiation. */
7195 if ((entering_scope
7196 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7197 || currently_open_class (template_type))
7198 /* comp_template_args is expensive, check it last. */
7199 && comp_template_args (TYPE_TI_ARGS (template_type),
7200 arglist))
7201 return template_type;
7202
7203 /* If we already have this specialization, return it. */
7204 elt.tmpl = gen_tmpl;
7205 elt.args = arglist;
7206 hash = hash_specialization (&elt);
7207 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7208 &elt, hash);
7209
7210 if (entry)
7211 return entry->spec;
7212
7213 is_dependent_type = uses_template_parms (arglist);
7214
7215 /* If the deduced arguments are invalid, then the binding
7216 failed. */
7217 if (!is_dependent_type
7218 && check_instantiated_args (gen_tmpl,
7219 INNERMOST_TEMPLATE_ARGS (arglist),
7220 complain))
7221 return error_mark_node;
7222
7223 if (!is_dependent_type
7224 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7225 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7226 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7227 {
7228 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7229 DECL_NAME (gen_tmpl),
7230 /*tag_scope=*/ts_global);
7231 return found;
7232 }
7233
7234 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7235 complain, in_decl);
7236 if (!context)
7237 context = global_namespace;
7238
7239 /* Create the type. */
7240 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7241 {
7242 if (!is_dependent_type)
7243 {
7244 set_current_access_from_decl (TYPE_NAME (template_type));
7245 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7246 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7247 arglist, complain, in_decl),
7248 SCOPED_ENUM_P (template_type), NULL);
7249 }
7250 else
7251 {
7252 /* We don't want to call start_enum for this type, since
7253 the values for the enumeration constants may involve
7254 template parameters. And, no one should be interested
7255 in the enumeration constants for such a type. */
7256 t = cxx_make_type (ENUMERAL_TYPE);
7257 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7258 }
7259 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7260 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7261 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7262 }
7263 else
7264 {
7265 t = make_class_type (TREE_CODE (template_type));
7266 CLASSTYPE_DECLARED_CLASS (t)
7267 = CLASSTYPE_DECLARED_CLASS (template_type);
7268 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7269 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7270
7271 /* A local class. Make sure the decl gets registered properly. */
7272 if (context == current_function_decl)
7273 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7274
7275 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7276 /* This instantiation is another name for the primary
7277 template type. Set the TYPE_CANONICAL field
7278 appropriately. */
7279 TYPE_CANONICAL (t) = template_type;
7280 else if (any_template_arguments_need_structural_equality_p (arglist))
7281 /* Some of the template arguments require structural
7282 equality testing, so this template class requires
7283 structural equality testing. */
7284 SET_TYPE_STRUCTURAL_EQUALITY (t);
7285 }
7286
7287 /* If we called start_enum or pushtag above, this information
7288 will already be set up. */
7289 if (!TYPE_NAME (t))
7290 {
7291 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7292
7293 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7294 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7295 DECL_SOURCE_LOCATION (type_decl)
7296 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7297 }
7298 else
7299 type_decl = TYPE_NAME (t);
7300
7301 TREE_PRIVATE (type_decl)
7302 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
7303 TREE_PROTECTED (type_decl)
7304 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
7305 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7306 {
7307 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7308 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7309 }
7310
7311 /* Let's consider the explicit specialization of a member
7312 of a class template specialization that is implicitely instantiated,
7313 e.g.:
7314 template<class T>
7315 struct S
7316 {
7317 template<class U> struct M {}; //#0
7318 };
7319
7320 template<>
7321 template<>
7322 struct S<int>::M<char> //#1
7323 {
7324 int i;
7325 };
7326 [temp.expl.spec]/4 says this is valid.
7327
7328 In this case, when we write:
7329 S<int>::M<char> m;
7330
7331 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7332 the one of #0.
7333
7334 When we encounter #1, we want to store the partial instantiation
7335 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7336
7337 For all cases other than this "explicit specialization of member of a
7338 class template", we just want to store the most general template into
7339 the CLASSTYPE_TI_TEMPLATE of M.
7340
7341 This case of "explicit specialization of member of a class template"
7342 only happens when:
7343 1/ the enclosing class is an instantiation of, and therefore not
7344 the same as, the context of the most general template, and
7345 2/ we aren't looking at the partial instantiation itself, i.e.
7346 the innermost arguments are not the same as the innermost parms of
7347 the most general template.
7348
7349 So it's only when 1/ and 2/ happens that we want to use the partial
7350 instantiation of the member template in lieu of its most general
7351 template. */
7352
7353 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7354 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7355 /* the enclosing class must be an instantiation... */
7356 && CLASS_TYPE_P (context)
7357 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7358 {
7359 tree partial_inst_args;
7360 TREE_VEC_LENGTH (arglist)--;
7361 ++processing_template_decl;
7362 partial_inst_args =
7363 tsubst (INNERMOST_TEMPLATE_ARGS
7364 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7365 arglist, complain, NULL_TREE);
7366 --processing_template_decl;
7367 TREE_VEC_LENGTH (arglist)++;
7368 use_partial_inst_tmpl =
7369 /*...and we must not be looking at the partial instantiation
7370 itself. */
7371 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7372 partial_inst_args);
7373 }
7374
7375 if (!use_partial_inst_tmpl)
7376 /* This case is easy; there are no member templates involved. */
7377 found = gen_tmpl;
7378 else
7379 {
7380 /* This is a full instantiation of a member template. Find
7381 the partial instantiation of which this is an instance. */
7382
7383 /* Temporarily reduce by one the number of levels in the ARGLIST
7384 so as to avoid comparing the last set of arguments. */
7385 TREE_VEC_LENGTH (arglist)--;
7386 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7387 TREE_VEC_LENGTH (arglist)++;
7388 found = CLASSTYPE_TI_TEMPLATE (found);
7389 }
7390
7391 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7392
7393 elt.spec = t;
7394 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
7395 &elt, hash, INSERT);
7396 *slot = ggc_alloc_spec_entry ();
7397 **slot = elt;
7398
7399 /* Note this use of the partial instantiation so we can check it
7400 later in maybe_process_partial_specialization. */
7401 DECL_TEMPLATE_INSTANTIATIONS (templ)
7402 = tree_cons (arglist, t,
7403 DECL_TEMPLATE_INSTANTIATIONS (templ));
7404
7405 if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
7406 /* Now that the type has been registered on the instantiations
7407 list, we set up the enumerators. Because the enumeration
7408 constants may involve the enumeration type itself, we make
7409 sure to register the type first, and then create the
7410 constants. That way, doing tsubst_expr for the enumeration
7411 constants won't result in recursive calls here; we'll find
7412 the instantiation and exit above. */
7413 tsubst_enum (template_type, t, arglist);
7414
7415 if (is_dependent_type)
7416 /* If the type makes use of template parameters, the
7417 code that generates debugging information will crash. */
7418 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
7419
7420 /* Possibly limit visibility based on template args. */
7421 TREE_PUBLIC (type_decl) = 1;
7422 determine_visibility (type_decl);
7423
7424 return t;
7425 }
7426 }
7427
7428 /* Wrapper for lookup_template_class_1. */
7429
7430 tree
7431 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7432 int entering_scope, tsubst_flags_t complain)
7433 {
7434 tree ret;
7435 timevar_push (TV_TEMPLATE_INST);
7436 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7437 entering_scope, complain);
7438 timevar_pop (TV_TEMPLATE_INST);
7439 return ret;
7440 }
7441 \f
7442 struct pair_fn_data
7443 {
7444 tree_fn_t fn;
7445 void *data;
7446 /* True when we should also visit template parameters that occur in
7447 non-deduced contexts. */
7448 bool include_nondeduced_p;
7449 struct pointer_set_t *visited;
7450 };
7451
7452 /* Called from for_each_template_parm via walk_tree. */
7453
7454 static tree
7455 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7456 {
7457 tree t = *tp;
7458 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7459 tree_fn_t fn = pfd->fn;
7460 void *data = pfd->data;
7461
7462 if (TYPE_P (t)
7463 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7464 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7465 pfd->include_nondeduced_p))
7466 return error_mark_node;
7467
7468 switch (TREE_CODE (t))
7469 {
7470 case RECORD_TYPE:
7471 if (TYPE_PTRMEMFUNC_P (t))
7472 break;
7473 /* Fall through. */
7474
7475 case UNION_TYPE:
7476 case ENUMERAL_TYPE:
7477 if (!TYPE_TEMPLATE_INFO (t))
7478 *walk_subtrees = 0;
7479 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
7480 fn, data, pfd->visited,
7481 pfd->include_nondeduced_p))
7482 return error_mark_node;
7483 break;
7484
7485 case INTEGER_TYPE:
7486 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7487 fn, data, pfd->visited,
7488 pfd->include_nondeduced_p)
7489 || for_each_template_parm (TYPE_MAX_VALUE (t),
7490 fn, data, pfd->visited,
7491 pfd->include_nondeduced_p))
7492 return error_mark_node;
7493 break;
7494
7495 case METHOD_TYPE:
7496 /* Since we're not going to walk subtrees, we have to do this
7497 explicitly here. */
7498 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7499 pfd->visited, pfd->include_nondeduced_p))
7500 return error_mark_node;
7501 /* Fall through. */
7502
7503 case FUNCTION_TYPE:
7504 /* Check the return type. */
7505 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7506 pfd->include_nondeduced_p))
7507 return error_mark_node;
7508
7509 /* Check the parameter types. Since default arguments are not
7510 instantiated until they are needed, the TYPE_ARG_TYPES may
7511 contain expressions that involve template parameters. But,
7512 no-one should be looking at them yet. And, once they're
7513 instantiated, they don't contain template parameters, so
7514 there's no point in looking at them then, either. */
7515 {
7516 tree parm;
7517
7518 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7519 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7520 pfd->visited, pfd->include_nondeduced_p))
7521 return error_mark_node;
7522
7523 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7524 want walk_tree walking into them itself. */
7525 *walk_subtrees = 0;
7526 }
7527 break;
7528
7529 case TYPEOF_TYPE:
7530 case UNDERLYING_TYPE:
7531 if (pfd->include_nondeduced_p
7532 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7533 pfd->visited,
7534 pfd->include_nondeduced_p))
7535 return error_mark_node;
7536 break;
7537
7538 case FUNCTION_DECL:
7539 case VAR_DECL:
7540 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7541 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7542 pfd->visited, pfd->include_nondeduced_p))
7543 return error_mark_node;
7544 /* Fall through. */
7545
7546 case PARM_DECL:
7547 case CONST_DECL:
7548 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7549 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7550 pfd->visited, pfd->include_nondeduced_p))
7551 return error_mark_node;
7552 if (DECL_CONTEXT (t)
7553 && pfd->include_nondeduced_p
7554 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7555 pfd->visited, pfd->include_nondeduced_p))
7556 return error_mark_node;
7557 break;
7558
7559 case BOUND_TEMPLATE_TEMPLATE_PARM:
7560 /* Record template parameters such as `T' inside `TT<T>'. */
7561 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7562 pfd->include_nondeduced_p))
7563 return error_mark_node;
7564 /* Fall through. */
7565
7566 case TEMPLATE_TEMPLATE_PARM:
7567 case TEMPLATE_TYPE_PARM:
7568 case TEMPLATE_PARM_INDEX:
7569 if (fn && (*fn)(t, data))
7570 return error_mark_node;
7571 else if (!fn)
7572 return error_mark_node;
7573 break;
7574
7575 case TEMPLATE_DECL:
7576 /* A template template parameter is encountered. */
7577 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7578 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7579 pfd->include_nondeduced_p))
7580 return error_mark_node;
7581
7582 /* Already substituted template template parameter */
7583 *walk_subtrees = 0;
7584 break;
7585
7586 case TYPENAME_TYPE:
7587 if (!fn
7588 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7589 data, pfd->visited,
7590 pfd->include_nondeduced_p))
7591 return error_mark_node;
7592 break;
7593
7594 case CONSTRUCTOR:
7595 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7596 && pfd->include_nondeduced_p
7597 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7598 (TREE_TYPE (t)), fn, data,
7599 pfd->visited, pfd->include_nondeduced_p))
7600 return error_mark_node;
7601 break;
7602
7603 case INDIRECT_REF:
7604 case COMPONENT_REF:
7605 /* If there's no type, then this thing must be some expression
7606 involving template parameters. */
7607 if (!fn && !TREE_TYPE (t))
7608 return error_mark_node;
7609 break;
7610
7611 case MODOP_EXPR:
7612 case CAST_EXPR:
7613 case REINTERPRET_CAST_EXPR:
7614 case CONST_CAST_EXPR:
7615 case STATIC_CAST_EXPR:
7616 case DYNAMIC_CAST_EXPR:
7617 case ARROW_EXPR:
7618 case DOTSTAR_EXPR:
7619 case TYPEID_EXPR:
7620 case PSEUDO_DTOR_EXPR:
7621 if (!fn)
7622 return error_mark_node;
7623 break;
7624
7625 default:
7626 break;
7627 }
7628
7629 /* We didn't find any template parameters we liked. */
7630 return NULL_TREE;
7631 }
7632
7633 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7634 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7635 call FN with the parameter and the DATA.
7636 If FN returns nonzero, the iteration is terminated, and
7637 for_each_template_parm returns 1. Otherwise, the iteration
7638 continues. If FN never returns a nonzero value, the value
7639 returned by for_each_template_parm is 0. If FN is NULL, it is
7640 considered to be the function which always returns 1.
7641
7642 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7643 parameters that occur in non-deduced contexts. When false, only
7644 visits those template parameters that can be deduced. */
7645
7646 static int
7647 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7648 struct pointer_set_t *visited,
7649 bool include_nondeduced_p)
7650 {
7651 struct pair_fn_data pfd;
7652 int result;
7653
7654 /* Set up. */
7655 pfd.fn = fn;
7656 pfd.data = data;
7657 pfd.include_nondeduced_p = include_nondeduced_p;
7658
7659 /* Walk the tree. (Conceptually, we would like to walk without
7660 duplicates, but for_each_template_parm_r recursively calls
7661 for_each_template_parm, so we would need to reorganize a fair
7662 bit to use walk_tree_without_duplicates, so we keep our own
7663 visited list.) */
7664 if (visited)
7665 pfd.visited = visited;
7666 else
7667 pfd.visited = pointer_set_create ();
7668 result = cp_walk_tree (&t,
7669 for_each_template_parm_r,
7670 &pfd,
7671 pfd.visited) != NULL_TREE;
7672
7673 /* Clean up. */
7674 if (!visited)
7675 {
7676 pointer_set_destroy (pfd.visited);
7677 pfd.visited = 0;
7678 }
7679
7680 return result;
7681 }
7682
7683 /* Returns true if T depends on any template parameter. */
7684
7685 int
7686 uses_template_parms (tree t)
7687 {
7688 bool dependent_p;
7689 int saved_processing_template_decl;
7690
7691 saved_processing_template_decl = processing_template_decl;
7692 if (!saved_processing_template_decl)
7693 processing_template_decl = 1;
7694 if (TYPE_P (t))
7695 dependent_p = dependent_type_p (t);
7696 else if (TREE_CODE (t) == TREE_VEC)
7697 dependent_p = any_dependent_template_arguments_p (t);
7698 else if (TREE_CODE (t) == TREE_LIST)
7699 dependent_p = (uses_template_parms (TREE_VALUE (t))
7700 || uses_template_parms (TREE_CHAIN (t)));
7701 else if (TREE_CODE (t) == TYPE_DECL)
7702 dependent_p = dependent_type_p (TREE_TYPE (t));
7703 else if (DECL_P (t)
7704 || EXPR_P (t)
7705 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7706 || TREE_CODE (t) == OVERLOAD
7707 || TREE_CODE (t) == BASELINK
7708 || TREE_CODE (t) == IDENTIFIER_NODE
7709 || TREE_CODE (t) == TRAIT_EXPR
7710 || TREE_CODE (t) == CONSTRUCTOR
7711 || CONSTANT_CLASS_P (t))
7712 dependent_p = (type_dependent_expression_p (t)
7713 || value_dependent_expression_p (t));
7714 else
7715 {
7716 gcc_assert (t == error_mark_node);
7717 dependent_p = false;
7718 }
7719
7720 processing_template_decl = saved_processing_template_decl;
7721
7722 return dependent_p;
7723 }
7724
7725 /* Returns true if T depends on any template parameter with level LEVEL. */
7726
7727 int
7728 uses_template_parms_level (tree t, int level)
7729 {
7730 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7731 /*include_nondeduced_p=*/true);
7732 }
7733
7734 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7735 ill-formed translation unit, i.e. a variable or function that isn't
7736 usable in a constant expression. */
7737
7738 static inline bool
7739 neglectable_inst_p (tree d)
7740 {
7741 return (DECL_P (d)
7742 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
7743 : decl_maybe_constant_var_p (d)));
7744 }
7745
7746 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7747 neglectable and instantiated from within an erroneous instantiation. */
7748
7749 static bool
7750 limit_bad_template_recursion (tree decl)
7751 {
7752 struct tinst_level *lev = current_tinst_level;
7753 int errs = errorcount + sorrycount;
7754 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
7755 return false;
7756
7757 for (; lev; lev = lev->next)
7758 if (neglectable_inst_p (lev->decl))
7759 break;
7760
7761 return (lev && errs > lev->errors);
7762 }
7763
7764 static int tinst_depth;
7765 extern int max_tinst_depth;
7766 #ifdef GATHER_STATISTICS
7767 int depth_reached;
7768 #endif
7769 static int tinst_level_tick;
7770 static int last_template_error_tick;
7771
7772 /* We're starting to instantiate D; record the template instantiation context
7773 for diagnostics and to restore it later. */
7774
7775 int
7776 push_tinst_level (tree d)
7777 {
7778 struct tinst_level *new_level;
7779
7780 if (tinst_depth >= max_tinst_depth)
7781 {
7782 last_template_error_tick = tinst_level_tick;
7783 if (TREE_CODE (d) == TREE_LIST)
7784 error ("template instantiation depth exceeds maximum of %d (use "
7785 "-ftemplate-depth= to increase the maximum) substituting %qS",
7786 max_tinst_depth, d);
7787 else
7788 error ("template instantiation depth exceeds maximum of %d (use "
7789 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7790 max_tinst_depth, d);
7791
7792 print_instantiation_context ();
7793
7794 return 0;
7795 }
7796
7797 /* If the current instantiation caused problems, don't let it instantiate
7798 anything else. Do allow deduction substitution and decls usable in
7799 constant expressions. */
7800 if (limit_bad_template_recursion (d))
7801 return 0;
7802
7803 new_level = ggc_alloc_tinst_level ();
7804 new_level->decl = d;
7805 new_level->locus = input_location;
7806 new_level->errors = errorcount+sorrycount;
7807 new_level->in_system_header_p = in_system_header;
7808 new_level->next = current_tinst_level;
7809 current_tinst_level = new_level;
7810
7811 ++tinst_depth;
7812 #ifdef GATHER_STATISTICS
7813 if (tinst_depth > depth_reached)
7814 depth_reached = tinst_depth;
7815 #endif
7816
7817 ++tinst_level_tick;
7818 return 1;
7819 }
7820
7821 /* We're done instantiating this template; return to the instantiation
7822 context. */
7823
7824 void
7825 pop_tinst_level (void)
7826 {
7827 /* Restore the filename and line number stashed away when we started
7828 this instantiation. */
7829 input_location = current_tinst_level->locus;
7830 current_tinst_level = current_tinst_level->next;
7831 --tinst_depth;
7832 ++tinst_level_tick;
7833 }
7834
7835 /* We're instantiating a deferred template; restore the template
7836 instantiation context in which the instantiation was requested, which
7837 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7838
7839 static tree
7840 reopen_tinst_level (struct tinst_level *level)
7841 {
7842 struct tinst_level *t;
7843
7844 tinst_depth = 0;
7845 for (t = level; t; t = t->next)
7846 ++tinst_depth;
7847
7848 current_tinst_level = level;
7849 pop_tinst_level ();
7850 if (current_tinst_level)
7851 current_tinst_level->errors = errorcount+sorrycount;
7852 return level->decl;
7853 }
7854
7855 /* Returns the TINST_LEVEL which gives the original instantiation
7856 context. */
7857
7858 struct tinst_level *
7859 outermost_tinst_level (void)
7860 {
7861 struct tinst_level *level = current_tinst_level;
7862 if (level)
7863 while (level->next)
7864 level = level->next;
7865 return level;
7866 }
7867
7868 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7869
7870 bool
7871 parameter_of_template_p (tree parm, tree templ)
7872 {
7873 tree parms;
7874 int i;
7875
7876 if (!parm || !templ)
7877 return false;
7878
7879 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
7880 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7881
7882 parms = DECL_TEMPLATE_PARMS (templ);
7883 parms = INNERMOST_TEMPLATE_PARMS (parms);
7884
7885 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
7886 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
7887 return true;
7888
7889 return false;
7890 }
7891
7892 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7893 vector of template arguments, as for tsubst.
7894
7895 Returns an appropriate tsubst'd friend declaration. */
7896
7897 static tree
7898 tsubst_friend_function (tree decl, tree args)
7899 {
7900 tree new_friend;
7901
7902 if (TREE_CODE (decl) == FUNCTION_DECL
7903 && DECL_TEMPLATE_INSTANTIATION (decl)
7904 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
7905 /* This was a friend declared with an explicit template
7906 argument list, e.g.:
7907
7908 friend void f<>(T);
7909
7910 to indicate that f was a template instantiation, not a new
7911 function declaration. Now, we have to figure out what
7912 instantiation of what template. */
7913 {
7914 tree template_id, arglist, fns;
7915 tree new_args;
7916 tree tmpl;
7917 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7918
7919 /* Friend functions are looked up in the containing namespace scope.
7920 We must enter that scope, to avoid finding member functions of the
7921 current class with same name. */
7922 push_nested_namespace (ns);
7923 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7924 tf_warning_or_error, NULL_TREE,
7925 /*integral_constant_expression_p=*/false);
7926 pop_nested_namespace (ns);
7927 arglist = tsubst (DECL_TI_ARGS (decl), args,
7928 tf_warning_or_error, NULL_TREE);
7929 template_id = lookup_template_function (fns, arglist);
7930
7931 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7932 tmpl = determine_specialization (template_id, new_friend,
7933 &new_args,
7934 /*need_member_template=*/0,
7935 TREE_VEC_LENGTH (args),
7936 tsk_none);
7937 return instantiate_template (tmpl, new_args, tf_error);
7938 }
7939
7940 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7941
7942 /* The NEW_FRIEND will look like an instantiation, to the
7943 compiler, but is not an instantiation from the point of view of
7944 the language. For example, we might have had:
7945
7946 template <class T> struct S {
7947 template <class U> friend void f(T, U);
7948 };
7949
7950 Then, in S<int>, template <class U> void f(int, U) is not an
7951 instantiation of anything. */
7952 if (new_friend == error_mark_node)
7953 return error_mark_node;
7954
7955 DECL_USE_TEMPLATE (new_friend) = 0;
7956 if (TREE_CODE (decl) == TEMPLATE_DECL)
7957 {
7958 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7959 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7960 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7961 }
7962
7963 /* The mangled name for the NEW_FRIEND is incorrect. The function
7964 is not a template instantiation and should not be mangled like
7965 one. Therefore, we forget the mangling here; we'll recompute it
7966 later if we need it. */
7967 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7968 {
7969 SET_DECL_RTL (new_friend, NULL);
7970 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7971 }
7972
7973 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7974 {
7975 tree old_decl;
7976 tree new_friend_template_info;
7977 tree new_friend_result_template_info;
7978 tree ns;
7979 int new_friend_is_defn;
7980
7981 /* We must save some information from NEW_FRIEND before calling
7982 duplicate decls since that function will free NEW_FRIEND if
7983 possible. */
7984 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7985 new_friend_is_defn =
7986 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7987 (template_for_substitution (new_friend)))
7988 != NULL_TREE);
7989 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7990 {
7991 /* This declaration is a `primary' template. */
7992 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7993
7994 new_friend_result_template_info
7995 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7996 }
7997 else
7998 new_friend_result_template_info = NULL_TREE;
7999
8000 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8001 if (new_friend_is_defn)
8002 DECL_INITIAL (new_friend) = error_mark_node;
8003
8004 /* Inside pushdecl_namespace_level, we will push into the
8005 current namespace. However, the friend function should go
8006 into the namespace of the template. */
8007 ns = decl_namespace_context (new_friend);
8008 push_nested_namespace (ns);
8009 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8010 pop_nested_namespace (ns);
8011
8012 if (old_decl == error_mark_node)
8013 return error_mark_node;
8014
8015 if (old_decl != new_friend)
8016 {
8017 /* This new friend declaration matched an existing
8018 declaration. For example, given:
8019
8020 template <class T> void f(T);
8021 template <class U> class C {
8022 template <class T> friend void f(T) {}
8023 };
8024
8025 the friend declaration actually provides the definition
8026 of `f', once C has been instantiated for some type. So,
8027 old_decl will be the out-of-class template declaration,
8028 while new_friend is the in-class definition.
8029
8030 But, if `f' was called before this point, the
8031 instantiation of `f' will have DECL_TI_ARGS corresponding
8032 to `T' but not to `U', references to which might appear
8033 in the definition of `f'. Previously, the most general
8034 template for an instantiation of `f' was the out-of-class
8035 version; now it is the in-class version. Therefore, we
8036 run through all specialization of `f', adding to their
8037 DECL_TI_ARGS appropriately. In particular, they need a
8038 new set of outer arguments, corresponding to the
8039 arguments for this class instantiation.
8040
8041 The same situation can arise with something like this:
8042
8043 friend void f(int);
8044 template <class T> class C {
8045 friend void f(T) {}
8046 };
8047
8048 when `C<int>' is instantiated. Now, `f(int)' is defined
8049 in the class. */
8050
8051 if (!new_friend_is_defn)
8052 /* On the other hand, if the in-class declaration does
8053 *not* provide a definition, then we don't want to alter
8054 existing definitions. We can just leave everything
8055 alone. */
8056 ;
8057 else
8058 {
8059 tree new_template = TI_TEMPLATE (new_friend_template_info);
8060 tree new_args = TI_ARGS (new_friend_template_info);
8061
8062 /* Overwrite whatever template info was there before, if
8063 any, with the new template information pertaining to
8064 the declaration. */
8065 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8066
8067 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8068 {
8069 /* We should have called reregister_specialization in
8070 duplicate_decls. */
8071 gcc_assert (retrieve_specialization (new_template,
8072 new_args, 0)
8073 == old_decl);
8074
8075 /* Instantiate it if the global has already been used. */
8076 if (DECL_ODR_USED (old_decl))
8077 instantiate_decl (old_decl, /*defer_ok=*/true,
8078 /*expl_inst_class_mem_p=*/false);
8079 }
8080 else
8081 {
8082 tree t;
8083
8084 /* Indicate that the old function template is a partial
8085 instantiation. */
8086 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8087 = new_friend_result_template_info;
8088
8089 gcc_assert (new_template
8090 == most_general_template (new_template));
8091 gcc_assert (new_template != old_decl);
8092
8093 /* Reassign any specializations already in the hash table
8094 to the new more general template, and add the
8095 additional template args. */
8096 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8097 t != NULL_TREE;
8098 t = TREE_CHAIN (t))
8099 {
8100 tree spec = TREE_VALUE (t);
8101 spec_entry elt;
8102
8103 elt.tmpl = old_decl;
8104 elt.args = DECL_TI_ARGS (spec);
8105 elt.spec = NULL_TREE;
8106
8107 htab_remove_elt (decl_specializations, &elt);
8108
8109 DECL_TI_ARGS (spec)
8110 = add_outermost_template_args (new_args,
8111 DECL_TI_ARGS (spec));
8112
8113 register_specialization
8114 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8115
8116 }
8117 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8118 }
8119 }
8120
8121 /* The information from NEW_FRIEND has been merged into OLD_DECL
8122 by duplicate_decls. */
8123 new_friend = old_decl;
8124 }
8125 }
8126 else
8127 {
8128 tree context = DECL_CONTEXT (new_friend);
8129 bool dependent_p;
8130
8131 /* In the code
8132 template <class T> class C {
8133 template <class U> friend void C1<U>::f (); // case 1
8134 friend void C2<T>::f (); // case 2
8135 };
8136 we only need to make sure CONTEXT is a complete type for
8137 case 2. To distinguish between the two cases, we note that
8138 CONTEXT of case 1 remains dependent type after tsubst while
8139 this isn't true for case 2. */
8140 ++processing_template_decl;
8141 dependent_p = dependent_type_p (context);
8142 --processing_template_decl;
8143
8144 if (!dependent_p
8145 && !complete_type_or_else (context, NULL_TREE))
8146 return error_mark_node;
8147
8148 if (COMPLETE_TYPE_P (context))
8149 {
8150 /* Check to see that the declaration is really present, and,
8151 possibly obtain an improved declaration. */
8152 tree fn = check_classfn (context,
8153 new_friend, NULL_TREE);
8154
8155 if (fn)
8156 new_friend = fn;
8157 }
8158 }
8159
8160 return new_friend;
8161 }
8162
8163 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8164 template arguments, as for tsubst.
8165
8166 Returns an appropriate tsubst'd friend type or error_mark_node on
8167 failure. */
8168
8169 static tree
8170 tsubst_friend_class (tree friend_tmpl, tree args)
8171 {
8172 tree friend_type;
8173 tree tmpl;
8174 tree context;
8175
8176 context = CP_DECL_CONTEXT (friend_tmpl);
8177
8178 if (context != global_namespace)
8179 {
8180 if (TREE_CODE (context) == NAMESPACE_DECL)
8181 push_nested_namespace (context);
8182 else
8183 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8184 }
8185
8186 /* Look for a class template declaration. We look for hidden names
8187 because two friend declarations of the same template are the
8188 same. For example, in:
8189
8190 struct A {
8191 template <typename> friend class F;
8192 };
8193 template <typename> struct B {
8194 template <typename> friend class F;
8195 };
8196
8197 both F templates are the same. */
8198 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8199 /*block_p=*/true, 0,
8200 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
8201
8202 /* But, if we don't find one, it might be because we're in a
8203 situation like this:
8204
8205 template <class T>
8206 struct S {
8207 template <class U>
8208 friend struct S;
8209 };
8210
8211 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8212 for `S<int>', not the TEMPLATE_DECL. */
8213 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8214 {
8215 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8216 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8217 }
8218
8219 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8220 {
8221 /* The friend template has already been declared. Just
8222 check to see that the declarations match, and install any new
8223 default parameters. We must tsubst the default parameters,
8224 of course. We only need the innermost template parameters
8225 because that is all that redeclare_class_template will look
8226 at. */
8227 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8228 > TMPL_ARGS_DEPTH (args))
8229 {
8230 tree parms;
8231 location_t saved_input_location;
8232 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8233 args, tf_warning_or_error);
8234
8235 saved_input_location = input_location;
8236 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8237 redeclare_class_template (TREE_TYPE (tmpl), parms);
8238 input_location = saved_input_location;
8239
8240 }
8241
8242 friend_type = TREE_TYPE (tmpl);
8243 }
8244 else
8245 {
8246 /* The friend template has not already been declared. In this
8247 case, the instantiation of the template class will cause the
8248 injection of this template into the global scope. */
8249 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8250 if (tmpl == error_mark_node)
8251 return error_mark_node;
8252
8253 /* The new TMPL is not an instantiation of anything, so we
8254 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8255 the new type because that is supposed to be the corresponding
8256 template decl, i.e., TMPL. */
8257 DECL_USE_TEMPLATE (tmpl) = 0;
8258 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8259 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8260 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8261 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8262
8263 /* Inject this template into the global scope. */
8264 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8265 }
8266
8267 if (context != global_namespace)
8268 {
8269 if (TREE_CODE (context) == NAMESPACE_DECL)
8270 pop_nested_namespace (context);
8271 else
8272 pop_nested_class ();
8273 }
8274
8275 return friend_type;
8276 }
8277
8278 /* Returns zero if TYPE cannot be completed later due to circularity.
8279 Otherwise returns one. */
8280
8281 static int
8282 can_complete_type_without_circularity (tree type)
8283 {
8284 if (type == NULL_TREE || type == error_mark_node)
8285 return 0;
8286 else if (COMPLETE_TYPE_P (type))
8287 return 1;
8288 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8289 return can_complete_type_without_circularity (TREE_TYPE (type));
8290 else if (CLASS_TYPE_P (type)
8291 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8292 return 0;
8293 else
8294 return 1;
8295 }
8296
8297 /* Apply any attributes which had to be deferred until instantiation
8298 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8299 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8300
8301 static void
8302 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8303 tree args, tsubst_flags_t complain, tree in_decl)
8304 {
8305 tree last_dep = NULL_TREE;
8306 tree t;
8307 tree *p;
8308
8309 for (t = attributes; t; t = TREE_CHAIN (t))
8310 if (ATTR_IS_DEPENDENT (t))
8311 {
8312 last_dep = t;
8313 attributes = copy_list (attributes);
8314 break;
8315 }
8316
8317 if (DECL_P (*decl_p))
8318 {
8319 if (TREE_TYPE (*decl_p) == error_mark_node)
8320 return;
8321 p = &DECL_ATTRIBUTES (*decl_p);
8322 }
8323 else
8324 p = &TYPE_ATTRIBUTES (*decl_p);
8325
8326 if (last_dep)
8327 {
8328 tree late_attrs = NULL_TREE;
8329 tree *q = &late_attrs;
8330
8331 for (*p = attributes; *p; )
8332 {
8333 t = *p;
8334 if (ATTR_IS_DEPENDENT (t))
8335 {
8336 *p = TREE_CHAIN (t);
8337 TREE_CHAIN (t) = NULL_TREE;
8338 /* If the first attribute argument is an identifier, don't
8339 pass it through tsubst. Attributes like mode, format,
8340 cleanup and several target specific attributes expect it
8341 unmodified. */
8342 if (TREE_VALUE (t)
8343 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
8344 && TREE_VALUE (TREE_VALUE (t))
8345 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
8346 == IDENTIFIER_NODE))
8347 {
8348 tree chain
8349 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8350 in_decl,
8351 /*integral_constant_expression_p=*/false);
8352 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8353 TREE_VALUE (t)
8354 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8355 chain);
8356 }
8357 else
8358 TREE_VALUE (t)
8359 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8360 /*integral_constant_expression_p=*/false);
8361 *q = t;
8362 q = &TREE_CHAIN (t);
8363 }
8364 else
8365 p = &TREE_CHAIN (t);
8366 }
8367
8368 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8369 }
8370 }
8371
8372 /* Perform (or defer) access check for typedefs that were referenced
8373 from within the template TMPL code.
8374 This is a subroutine of instantiate_template and instantiate_class_template.
8375 TMPL is the template to consider and TARGS is the list of arguments of
8376 that template. */
8377
8378 static void
8379 perform_typedefs_access_check (tree tmpl, tree targs)
8380 {
8381 location_t saved_location;
8382 int i;
8383 qualified_typedef_usage_t *iter;
8384
8385 if (!tmpl
8386 || (!CLASS_TYPE_P (tmpl)
8387 && TREE_CODE (tmpl) != FUNCTION_DECL))
8388 return;
8389
8390 saved_location = input_location;
8391 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
8392 get_types_needing_access_check (tmpl),
8393 i, iter)
8394 {
8395 tree type_decl = iter->typedef_decl;
8396 tree type_scope = iter->context;
8397
8398 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8399 continue;
8400
8401 if (uses_template_parms (type_decl))
8402 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8403 if (uses_template_parms (type_scope))
8404 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8405
8406 /* Make access check error messages point to the location
8407 of the use of the typedef. */
8408 input_location = iter->locus;
8409 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8410 type_decl, type_decl);
8411 }
8412 input_location = saved_location;
8413 }
8414
8415 static tree
8416 instantiate_class_template_1 (tree type)
8417 {
8418 tree templ, args, pattern, t, member;
8419 tree typedecl;
8420 tree pbinfo;
8421 tree base_list;
8422 unsigned int saved_maximum_field_alignment;
8423
8424 if (type == error_mark_node)
8425 return error_mark_node;
8426
8427 if (COMPLETE_OR_OPEN_TYPE_P (type)
8428 || uses_template_parms (type))
8429 return type;
8430
8431 /* Figure out which template is being instantiated. */
8432 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8433 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8434
8435 /* Determine what specialization of the original template to
8436 instantiate. */
8437 t = most_specialized_class (type, templ, tf_warning_or_error);
8438 if (t == error_mark_node)
8439 {
8440 TYPE_BEING_DEFINED (type) = 1;
8441 return error_mark_node;
8442 }
8443 else if (t)
8444 {
8445 /* This TYPE is actually an instantiation of a partial
8446 specialization. We replace the innermost set of ARGS with
8447 the arguments appropriate for substitution. For example,
8448 given:
8449
8450 template <class T> struct S {};
8451 template <class T> struct S<T*> {};
8452
8453 and supposing that we are instantiating S<int*>, ARGS will
8454 presently be {int*} -- but we need {int}. */
8455 pattern = TREE_TYPE (t);
8456 args = TREE_PURPOSE (t);
8457 }
8458 else
8459 {
8460 pattern = TREE_TYPE (templ);
8461 args = CLASSTYPE_TI_ARGS (type);
8462 }
8463
8464 /* If the template we're instantiating is incomplete, then clearly
8465 there's nothing we can do. */
8466 if (!COMPLETE_TYPE_P (pattern))
8467 return type;
8468
8469 /* If we've recursively instantiated too many templates, stop. */
8470 if (! push_tinst_level (type))
8471 return type;
8472
8473 /* Now we're really doing the instantiation. Mark the type as in
8474 the process of being defined. */
8475 TYPE_BEING_DEFINED (type) = 1;
8476
8477 /* We may be in the middle of deferred access check. Disable
8478 it now. */
8479 push_deferring_access_checks (dk_no_deferred);
8480
8481 push_to_top_level ();
8482 /* Use #pragma pack from the template context. */
8483 saved_maximum_field_alignment = maximum_field_alignment;
8484 maximum_field_alignment = TYPE_PRECISION (pattern);
8485
8486 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8487
8488 /* Set the input location to the most specialized template definition.
8489 This is needed if tsubsting causes an error. */
8490 typedecl = TYPE_MAIN_DECL (pattern);
8491 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8492 DECL_SOURCE_LOCATION (typedecl);
8493
8494 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
8495 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
8496 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
8497 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
8498 TYPE_HAS_COPY_ASSIGN (type) = TYPE_HAS_COPY_ASSIGN (pattern);
8499 TYPE_HAS_CONST_COPY_ASSIGN (type) = TYPE_HAS_CONST_COPY_ASSIGN (pattern);
8500 TYPE_HAS_COPY_CTOR (type) = TYPE_HAS_COPY_CTOR (pattern);
8501 TYPE_HAS_CONST_COPY_CTOR (type) = TYPE_HAS_CONST_COPY_CTOR (pattern);
8502 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
8503 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
8504 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8505 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8506 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8507 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8508 if (ANON_AGGR_TYPE_P (pattern))
8509 SET_ANON_AGGR_TYPE_P (type);
8510 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8511 {
8512 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8513 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8514 }
8515 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8516
8517 pbinfo = TYPE_BINFO (pattern);
8518
8519 /* We should never instantiate a nested class before its enclosing
8520 class; we need to look up the nested class by name before we can
8521 instantiate it, and that lookup should instantiate the enclosing
8522 class. */
8523 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8524 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8525
8526 base_list = NULL_TREE;
8527 if (BINFO_N_BASE_BINFOS (pbinfo))
8528 {
8529 tree pbase_binfo;
8530 tree pushed_scope;
8531 int i;
8532
8533 /* We must enter the scope containing the type, as that is where
8534 the accessibility of types named in dependent bases are
8535 looked up from. */
8536 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8537
8538 /* Substitute into each of the bases to determine the actual
8539 basetypes. */
8540 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8541 {
8542 tree base;
8543 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8544 tree expanded_bases = NULL_TREE;
8545 int idx, len = 1;
8546
8547 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8548 {
8549 expanded_bases =
8550 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8551 args, tf_error, NULL_TREE);
8552 if (expanded_bases == error_mark_node)
8553 continue;
8554
8555 len = TREE_VEC_LENGTH (expanded_bases);
8556 }
8557
8558 for (idx = 0; idx < len; idx++)
8559 {
8560 if (expanded_bases)
8561 /* Extract the already-expanded base class. */
8562 base = TREE_VEC_ELT (expanded_bases, idx);
8563 else
8564 /* Substitute to figure out the base class. */
8565 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8566 NULL_TREE);
8567
8568 if (base == error_mark_node)
8569 continue;
8570
8571 base_list = tree_cons (access, base, base_list);
8572 if (BINFO_VIRTUAL_P (pbase_binfo))
8573 TREE_TYPE (base_list) = integer_type_node;
8574 }
8575 }
8576
8577 /* The list is now in reverse order; correct that. */
8578 base_list = nreverse (base_list);
8579
8580 if (pushed_scope)
8581 pop_scope (pushed_scope);
8582 }
8583 /* Now call xref_basetypes to set up all the base-class
8584 information. */
8585 xref_basetypes (type, base_list);
8586
8587 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8588 (int) ATTR_FLAG_TYPE_IN_PLACE,
8589 args, tf_error, NULL_TREE);
8590 fixup_attribute_variants (type);
8591
8592 /* Now that our base classes are set up, enter the scope of the
8593 class, so that name lookups into base classes, etc. will work
8594 correctly. This is precisely analogous to what we do in
8595 begin_class_definition when defining an ordinary non-template
8596 class, except we also need to push the enclosing classes. */
8597 push_nested_class (type);
8598
8599 /* Now members are processed in the order of declaration. */
8600 for (member = CLASSTYPE_DECL_LIST (pattern);
8601 member; member = TREE_CHAIN (member))
8602 {
8603 tree t = TREE_VALUE (member);
8604
8605 if (TREE_PURPOSE (member))
8606 {
8607 if (TYPE_P (t))
8608 {
8609 /* Build new CLASSTYPE_NESTED_UTDS. */
8610
8611 tree newtag;
8612 bool class_template_p;
8613
8614 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8615 && TYPE_LANG_SPECIFIC (t)
8616 && CLASSTYPE_IS_TEMPLATE (t));
8617 /* If the member is a class template, then -- even after
8618 substitution -- there may be dependent types in the
8619 template argument list for the class. We increment
8620 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8621 that function will assume that no types are dependent
8622 when outside of a template. */
8623 if (class_template_p)
8624 ++processing_template_decl;
8625 newtag = tsubst (t, args, tf_error, NULL_TREE);
8626 if (class_template_p)
8627 --processing_template_decl;
8628 if (newtag == error_mark_node)
8629 continue;
8630
8631 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8632 {
8633 tree name = TYPE_IDENTIFIER (t);
8634
8635 if (class_template_p)
8636 /* Unfortunately, lookup_template_class sets
8637 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8638 instantiation (i.e., for the type of a member
8639 template class nested within a template class.)
8640 This behavior is required for
8641 maybe_process_partial_specialization to work
8642 correctly, but is not accurate in this case;
8643 the TAG is not an instantiation of anything.
8644 (The corresponding TEMPLATE_DECL is an
8645 instantiation, but the TYPE is not.) */
8646 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8647
8648 /* Now, we call pushtag to put this NEWTAG into the scope of
8649 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8650 pushtag calling push_template_decl. We don't have to do
8651 this for enums because it will already have been done in
8652 tsubst_enum. */
8653 if (name)
8654 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8655 pushtag (name, newtag, /*tag_scope=*/ts_current);
8656 }
8657 }
8658 else if (TREE_CODE (t) == FUNCTION_DECL
8659 || DECL_FUNCTION_TEMPLATE_P (t))
8660 {
8661 /* Build new TYPE_METHODS. */
8662 tree r;
8663
8664 if (TREE_CODE (t) == TEMPLATE_DECL)
8665 ++processing_template_decl;
8666 r = tsubst (t, args, tf_error, NULL_TREE);
8667 if (TREE_CODE (t) == TEMPLATE_DECL)
8668 --processing_template_decl;
8669 set_current_access_from_decl (r);
8670 finish_member_declaration (r);
8671 }
8672 else
8673 {
8674 /* Build new TYPE_FIELDS. */
8675 if (TREE_CODE (t) == STATIC_ASSERT)
8676 {
8677 tree condition =
8678 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8679 tf_warning_or_error, NULL_TREE,
8680 /*integral_constant_expression_p=*/true);
8681 finish_static_assert (condition,
8682 STATIC_ASSERT_MESSAGE (t),
8683 STATIC_ASSERT_SOURCE_LOCATION (t),
8684 /*member_p=*/true);
8685 }
8686 else if (TREE_CODE (t) != CONST_DECL)
8687 {
8688 tree r;
8689
8690 /* The file and line for this declaration, to
8691 assist in error message reporting. Since we
8692 called push_tinst_level above, we don't need to
8693 restore these. */
8694 input_location = DECL_SOURCE_LOCATION (t);
8695
8696 if (TREE_CODE (t) == TEMPLATE_DECL)
8697 ++processing_template_decl;
8698 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8699 if (TREE_CODE (t) == TEMPLATE_DECL)
8700 --processing_template_decl;
8701 if (TREE_CODE (r) == VAR_DECL)
8702 {
8703 /* In [temp.inst]:
8704
8705 [t]he initialization (and any associated
8706 side-effects) of a static data member does
8707 not occur unless the static data member is
8708 itself used in a way that requires the
8709 definition of the static data member to
8710 exist.
8711
8712 Therefore, we do not substitute into the
8713 initialized for the static data member here. */
8714 finish_static_data_member_decl
8715 (r,
8716 /*init=*/NULL_TREE,
8717 /*init_const_expr_p=*/false,
8718 /*asmspec_tree=*/NULL_TREE,
8719 /*flags=*/0);
8720 }
8721 else if (TREE_CODE (r) == FIELD_DECL)
8722 {
8723 /* Determine whether R has a valid type and can be
8724 completed later. If R is invalid, then it is
8725 replaced by error_mark_node so that it will not be
8726 added to TYPE_FIELDS. */
8727 tree rtype = TREE_TYPE (r);
8728 if (can_complete_type_without_circularity (rtype))
8729 complete_type (rtype);
8730
8731 if (!COMPLETE_TYPE_P (rtype))
8732 {
8733 cxx_incomplete_type_error (r, rtype);
8734 r = error_mark_node;
8735 }
8736 }
8737
8738 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8739 such a thing will already have been added to the field
8740 list by tsubst_enum in finish_member_declaration in the
8741 CLASSTYPE_NESTED_UTDS case above. */
8742 if (!(TREE_CODE (r) == TYPE_DECL
8743 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8744 && DECL_ARTIFICIAL (r)))
8745 {
8746 set_current_access_from_decl (r);
8747 finish_member_declaration (r);
8748 }
8749 }
8750 }
8751 }
8752 else
8753 {
8754 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
8755 {
8756 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8757
8758 tree friend_type = t;
8759 bool adjust_processing_template_decl = false;
8760
8761 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8762 {
8763 /* template <class T> friend class C; */
8764 friend_type = tsubst_friend_class (friend_type, args);
8765 adjust_processing_template_decl = true;
8766 }
8767 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8768 {
8769 /* template <class T> friend class C::D; */
8770 friend_type = tsubst (friend_type, args,
8771 tf_warning_or_error, NULL_TREE);
8772 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8773 friend_type = TREE_TYPE (friend_type);
8774 adjust_processing_template_decl = true;
8775 }
8776 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
8777 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
8778 {
8779 /* This could be either
8780
8781 friend class T::C;
8782
8783 when dependent_type_p is false or
8784
8785 template <class U> friend class T::C;
8786
8787 otherwise. */
8788 friend_type = tsubst (friend_type, args,
8789 tf_warning_or_error, NULL_TREE);
8790 /* Bump processing_template_decl for correct
8791 dependent_type_p calculation. */
8792 ++processing_template_decl;
8793 if (dependent_type_p (friend_type))
8794 adjust_processing_template_decl = true;
8795 --processing_template_decl;
8796 }
8797 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8798 && hidden_name_p (TYPE_NAME (friend_type)))
8799 {
8800 /* friend class C;
8801
8802 where C hasn't been declared yet. Let's lookup name
8803 from namespace scope directly, bypassing any name that
8804 come from dependent base class. */
8805 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8806
8807 /* The call to xref_tag_from_type does injection for friend
8808 classes. */
8809 push_nested_namespace (ns);
8810 friend_type =
8811 xref_tag_from_type (friend_type, NULL_TREE,
8812 /*tag_scope=*/ts_current);
8813 pop_nested_namespace (ns);
8814 }
8815 else if (uses_template_parms (friend_type))
8816 /* friend class C<T>; */
8817 friend_type = tsubst (friend_type, args,
8818 tf_warning_or_error, NULL_TREE);
8819 /* Otherwise it's
8820
8821 friend class C;
8822
8823 where C is already declared or
8824
8825 friend class C<int>;
8826
8827 We don't have to do anything in these cases. */
8828
8829 if (adjust_processing_template_decl)
8830 /* Trick make_friend_class into realizing that the friend
8831 we're adding is a template, not an ordinary class. It's
8832 important that we use make_friend_class since it will
8833 perform some error-checking and output cross-reference
8834 information. */
8835 ++processing_template_decl;
8836
8837 if (friend_type != error_mark_node)
8838 make_friend_class (type, friend_type, /*complain=*/false);
8839
8840 if (adjust_processing_template_decl)
8841 --processing_template_decl;
8842 }
8843 else
8844 {
8845 /* Build new DECL_FRIENDLIST. */
8846 tree r;
8847
8848 /* The file and line for this declaration, to
8849 assist in error message reporting. Since we
8850 called push_tinst_level above, we don't need to
8851 restore these. */
8852 input_location = DECL_SOURCE_LOCATION (t);
8853
8854 if (TREE_CODE (t) == TEMPLATE_DECL)
8855 {
8856 ++processing_template_decl;
8857 push_deferring_access_checks (dk_no_check);
8858 }
8859
8860 r = tsubst_friend_function (t, args);
8861 add_friend (type, r, /*complain=*/false);
8862 if (TREE_CODE (t) == TEMPLATE_DECL)
8863 {
8864 pop_deferring_access_checks ();
8865 --processing_template_decl;
8866 }
8867 }
8868 }
8869 }
8870
8871 if (CLASSTYPE_LAMBDA_EXPR (type))
8872 maybe_add_lambda_conv_op (type);
8873
8874 /* Set the file and line number information to whatever is given for
8875 the class itself. This puts error messages involving generated
8876 implicit functions at a predictable point, and the same point
8877 that would be used for non-template classes. */
8878 input_location = DECL_SOURCE_LOCATION (typedecl);
8879
8880 unreverse_member_declarations (type);
8881 finish_struct_1 (type);
8882 TYPE_BEING_DEFINED (type) = 0;
8883
8884 /* We don't instantiate default arguments for member functions. 14.7.1:
8885
8886 The implicit instantiation of a class template specialization causes
8887 the implicit instantiation of the declarations, but not of the
8888 definitions or default arguments, of the class member functions,
8889 member classes, static data members and member templates.... */
8890
8891 /* Some typedefs referenced from within the template code need to be access
8892 checked at template instantiation time, i.e now. These types were
8893 added to the template at parsing time. Let's get those and perform
8894 the access checks then. */
8895 perform_typedefs_access_check (pattern, args);
8896 perform_deferred_access_checks ();
8897 pop_nested_class ();
8898 maximum_field_alignment = saved_maximum_field_alignment;
8899 pop_from_top_level ();
8900 pop_deferring_access_checks ();
8901 pop_tinst_level ();
8902
8903 /* The vtable for a template class can be emitted in any translation
8904 unit in which the class is instantiated. When there is no key
8905 method, however, finish_struct_1 will already have added TYPE to
8906 the keyed_classes list. */
8907 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
8908 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
8909
8910 return type;
8911 }
8912
8913 /* Wrapper for instantiate_class_template_1. */
8914
8915 tree
8916 instantiate_class_template (tree type)
8917 {
8918 tree ret;
8919 timevar_push (TV_TEMPLATE_INST);
8920 ret = instantiate_class_template_1 (type);
8921 timevar_pop (TV_TEMPLATE_INST);
8922 return ret;
8923 }
8924
8925 static tree
8926 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8927 {
8928 tree r;
8929
8930 if (!t)
8931 r = t;
8932 else if (TYPE_P (t))
8933 r = tsubst (t, args, complain, in_decl);
8934 else
8935 {
8936 if (!(complain & tf_warning))
8937 ++c_inhibit_evaluation_warnings;
8938 r = tsubst_expr (t, args, complain, in_decl,
8939 /*integral_constant_expression_p=*/true);
8940 if (!(complain & tf_warning))
8941 --c_inhibit_evaluation_warnings;
8942 }
8943 return r;
8944 }
8945
8946 /* Given a function parameter pack TMPL_PARM and some function parameters
8947 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
8948 and set *SPEC_P to point at the next point in the list. */
8949
8950 static tree
8951 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
8952 {
8953 /* Collect all of the extra "packed" parameters into an
8954 argument pack. */
8955 tree parmvec;
8956 tree parmtypevec;
8957 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8958 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8959 tree spec_parm = *spec_p;
8960 int i, len;
8961
8962 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
8963 if (tmpl_parm
8964 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
8965 break;
8966
8967 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8968 parmvec = make_tree_vec (len);
8969 parmtypevec = make_tree_vec (len);
8970 spec_parm = *spec_p;
8971 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
8972 {
8973 TREE_VEC_ELT (parmvec, i) = spec_parm;
8974 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8975 }
8976
8977 /* Build the argument packs. */
8978 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
8979 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
8980 TREE_TYPE (argpack) = argtypepack;
8981 *spec_p = spec_parm;
8982
8983 return argpack;
8984 }
8985
8986 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8987 NONTYPE_ARGUMENT_PACK. */
8988
8989 static tree
8990 make_fnparm_pack (tree spec_parm)
8991 {
8992 return extract_fnparm_pack (NULL_TREE, &spec_parm);
8993 }
8994
8995 /* Substitute ARGS into T, which is an pack expansion
8996 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8997 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8998 (if only a partial substitution could be performed) or
8999 ERROR_MARK_NODE if there was an error. */
9000 tree
9001 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9002 tree in_decl)
9003 {
9004 tree pattern;
9005 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
9006 int i, len = -1;
9007 tree result;
9008 int incomplete = 0;
9009 htab_t saved_local_specializations = NULL;
9010
9011 gcc_assert (PACK_EXPANSION_P (t));
9012 pattern = PACK_EXPANSION_PATTERN (t);
9013
9014 /* Determine the argument packs that will instantiate the parameter
9015 packs used in the expansion expression. While we're at it,
9016 compute the number of arguments to be expanded and make sure it
9017 is consistent. */
9018 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9019 pack = TREE_CHAIN (pack))
9020 {
9021 tree parm_pack = TREE_VALUE (pack);
9022 tree arg_pack = NULL_TREE;
9023 tree orig_arg = NULL_TREE;
9024
9025 if (TREE_CODE (parm_pack) == PARM_DECL)
9026 {
9027 if (!cp_unevaluated_operand)
9028 arg_pack = retrieve_local_specialization (parm_pack);
9029 else
9030 {
9031 /* We can't rely on local_specializations for a parameter
9032 name used later in a function declaration (such as in a
9033 late-specified return type). Even if it exists, it might
9034 have the wrong value for a recursive call. Just make a
9035 dummy decl, since it's only used for its type. */
9036 arg_pack = tsubst_decl (parm_pack, args, complain);
9037 if (arg_pack && FUNCTION_PARAMETER_PACK_P (arg_pack))
9038 /* Partial instantiation of the parm_pack, we can't build
9039 up an argument pack yet. */
9040 arg_pack = NULL_TREE;
9041 else
9042 arg_pack = make_fnparm_pack (arg_pack);
9043 }
9044 }
9045 else
9046 {
9047 int level, idx, levels;
9048 template_parm_level_and_index (parm_pack, &level, &idx);
9049
9050 levels = TMPL_ARGS_DEPTH (args);
9051 if (level <= levels)
9052 arg_pack = TMPL_ARG (args, level, idx);
9053 }
9054
9055 orig_arg = arg_pack;
9056 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9057 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9058
9059 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9060 /* This can only happen if we forget to expand an argument
9061 pack somewhere else. Just return an error, silently. */
9062 {
9063 result = make_tree_vec (1);
9064 TREE_VEC_ELT (result, 0) = error_mark_node;
9065 return result;
9066 }
9067
9068 /* For clarity in the comments below let's use the
9069 representation 'argument_pack<elements>' to denote an
9070 argument pack and its elements.
9071
9072 In the 'if' block below, we want to detect cases where
9073 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
9074 check if ARG_PACK is an argument pack which sole element is
9075 the expansion of PARM_PACK. That argument pack is typically
9076 created by template_parm_to_arg when passed a parameter
9077 pack. */
9078 if (arg_pack
9079 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
9080 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
9081 {
9082 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
9083 tree pattern = PACK_EXPANSION_PATTERN (expansion);
9084 /* So we have an argument_pack<P...>. We want to test if P
9085 is actually PARM_PACK. We will not use cp_tree_equal to
9086 test P and PARM_PACK because during type fixup (by
9087 fixup_template_parm) P can be a pre-fixup version of a
9088 type and PARM_PACK be its post-fixup version.
9089 cp_tree_equal would consider them as different even
9090 though we would want to consider them compatible for our
9091 precise purpose here.
9092
9093 Thus we are going to consider that P and PARM_PACK are
9094 compatible if they have the same DECL. */
9095 if ((/* If ARG_PACK is a type parameter pack named by the
9096 same DECL as parm_pack ... */
9097 (TYPE_P (pattern)
9098 && TYPE_P (parm_pack)
9099 && TYPE_NAME (pattern) == TYPE_NAME (parm_pack))
9100 /* ... or if ARG_PACK is a non-type parameter
9101 named by the same DECL as parm_pack ... */
9102 || (TREE_CODE (pattern) == TEMPLATE_PARM_INDEX
9103 && TREE_CODE (parm_pack) == PARM_DECL
9104 && TEMPLATE_PARM_DECL (pattern)
9105 == TEMPLATE_PARM_DECL (DECL_INITIAL (parm_pack))))
9106 && template_parameter_pack_p (pattern))
9107 /* ... then the argument pack that the parameter maps to
9108 is just an expansion of the parameter itself, such as
9109 one would find in the implicit typedef of a class
9110 inside the class itself. Consider this parameter
9111 "unsubstituted", so that we will maintain the outer
9112 pack expansion. */
9113 arg_pack = NULL_TREE;
9114 }
9115
9116 if (arg_pack)
9117 {
9118 int my_len =
9119 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9120
9121 /* It's all-or-nothing with incomplete argument packs. */
9122 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9123 return error_mark_node;
9124
9125 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9126 incomplete = 1;
9127
9128 if (len < 0)
9129 len = my_len;
9130 else if (len != my_len)
9131 {
9132 if (incomplete)
9133 /* We got explicit args for some packs but not others;
9134 do nothing now and try again after deduction. */
9135 return t;
9136 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9137 error ("mismatched argument pack lengths while expanding "
9138 "%<%T%>",
9139 pattern);
9140 else
9141 error ("mismatched argument pack lengths while expanding "
9142 "%<%E%>",
9143 pattern);
9144 return error_mark_node;
9145 }
9146
9147 /* Keep track of the parameter packs and their corresponding
9148 argument packs. */
9149 packs = tree_cons (parm_pack, arg_pack, packs);
9150 TREE_TYPE (packs) = orig_arg;
9151 }
9152 else
9153 /* We can't substitute for this parameter pack. */
9154 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
9155 TREE_VALUE (pack),
9156 unsubstituted_packs);
9157 }
9158
9159 /* We cannot expand this expansion expression, because we don't have
9160 all of the argument packs we need. Substitute into the pattern
9161 and return a PACK_EXPANSION_*. The caller will need to deal with
9162 that. */
9163 if (unsubstituted_packs)
9164 {
9165 tree new_pat;
9166 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9167 new_pat = tsubst_expr (pattern, args, complain, in_decl,
9168 /*integral_constant_expression_p=*/false);
9169 else
9170 new_pat = tsubst (pattern, args, complain, in_decl);
9171 return make_pack_expansion (new_pat);
9172 }
9173
9174 /* We could not find any argument packs that work. */
9175 if (len < 0)
9176 return error_mark_node;
9177
9178 if (cp_unevaluated_operand)
9179 {
9180 /* We're in a late-specified return type, so create our own local
9181 specializations table; the current table is either NULL or (in the
9182 case of recursive unification) might have bindings that we don't
9183 want to use or alter. */
9184 saved_local_specializations = local_specializations;
9185 local_specializations = htab_create (37,
9186 hash_local_specialization,
9187 eq_local_specializations,
9188 NULL);
9189 }
9190
9191 /* For each argument in each argument pack, substitute into the
9192 pattern. */
9193 result = make_tree_vec (len + incomplete);
9194 for (i = 0; i < len + incomplete; ++i)
9195 {
9196 /* For parameter pack, change the substitution of the parameter
9197 pack to the ith argument in its argument pack, then expand
9198 the pattern. */
9199 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9200 {
9201 tree parm = TREE_PURPOSE (pack);
9202
9203 if (TREE_CODE (parm) == PARM_DECL)
9204 {
9205 /* Select the Ith argument from the pack. */
9206 tree arg = make_node (ARGUMENT_PACK_SELECT);
9207 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
9208 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
9209 mark_used (parm);
9210 register_local_specialization (arg, parm);
9211 }
9212 else
9213 {
9214 tree value = parm;
9215 int idx, level;
9216 template_parm_level_and_index (parm, &level, &idx);
9217
9218 if (i < len)
9219 {
9220 /* Select the Ith argument from the pack. */
9221 value = make_node (ARGUMENT_PACK_SELECT);
9222 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
9223 ARGUMENT_PACK_SELECT_INDEX (value) = i;
9224 }
9225
9226 /* Update the corresponding argument. */
9227 TMPL_ARG (args, level, idx) = value;
9228 }
9229 }
9230
9231 /* Substitute into the PATTERN with the altered arguments. */
9232 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9233 TREE_VEC_ELT (result, i) =
9234 tsubst_expr (pattern, args, complain, in_decl,
9235 /*integral_constant_expression_p=*/false);
9236 else
9237 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
9238
9239 if (i == len)
9240 /* When we have incomplete argument packs, the last "expanded"
9241 result is itself a pack expansion, which allows us
9242 to deduce more arguments. */
9243 TREE_VEC_ELT (result, i) =
9244 make_pack_expansion (TREE_VEC_ELT (result, i));
9245
9246 if (TREE_VEC_ELT (result, i) == error_mark_node)
9247 {
9248 result = error_mark_node;
9249 break;
9250 }
9251 }
9252
9253 /* Update ARGS to restore the substitution from parameter packs to
9254 their argument packs. */
9255 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9256 {
9257 tree parm = TREE_PURPOSE (pack);
9258
9259 if (TREE_CODE (parm) == PARM_DECL)
9260 register_local_specialization (TREE_TYPE (pack), parm);
9261 else
9262 {
9263 int idx, level;
9264 template_parm_level_and_index (parm, &level, &idx);
9265
9266 /* Update the corresponding argument. */
9267 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9268 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9269 TREE_TYPE (pack);
9270 else
9271 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9272 }
9273 }
9274
9275 if (saved_local_specializations)
9276 {
9277 htab_delete (local_specializations);
9278 local_specializations = saved_local_specializations;
9279 }
9280
9281 return result;
9282 }
9283
9284 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9285 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9286 parameter packs; all parms generated from a function parameter pack will
9287 have the same DECL_PARM_INDEX. */
9288
9289 tree
9290 get_pattern_parm (tree parm, tree tmpl)
9291 {
9292 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9293 tree patparm;
9294
9295 if (DECL_ARTIFICIAL (parm))
9296 {
9297 for (patparm = DECL_ARGUMENTS (pattern);
9298 patparm; patparm = DECL_CHAIN (patparm))
9299 if (DECL_ARTIFICIAL (patparm)
9300 && DECL_NAME (parm) == DECL_NAME (patparm))
9301 break;
9302 }
9303 else
9304 {
9305 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9306 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9307 gcc_assert (DECL_PARM_INDEX (patparm)
9308 == DECL_PARM_INDEX (parm));
9309 }
9310
9311 return patparm;
9312 }
9313
9314 /* Substitute ARGS into the vector or list of template arguments T. */
9315
9316 static tree
9317 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9318 {
9319 tree orig_t = t;
9320 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9321 tree *elts;
9322
9323 if (t == error_mark_node)
9324 return error_mark_node;
9325
9326 len = TREE_VEC_LENGTH (t);
9327 elts = XALLOCAVEC (tree, len);
9328
9329 for (i = 0; i < len; i++)
9330 {
9331 tree orig_arg = TREE_VEC_ELT (t, i);
9332 tree new_arg;
9333
9334 if (TREE_CODE (orig_arg) == TREE_VEC)
9335 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9336 else if (PACK_EXPANSION_P (orig_arg))
9337 {
9338 /* Substitute into an expansion expression. */
9339 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9340
9341 if (TREE_CODE (new_arg) == TREE_VEC)
9342 /* Add to the expanded length adjustment the number of
9343 expanded arguments. We subtract one from this
9344 measurement, because the argument pack expression
9345 itself is already counted as 1 in
9346 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9347 the argument pack is empty. */
9348 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9349 }
9350 else if (ARGUMENT_PACK_P (orig_arg))
9351 {
9352 /* Substitute into each of the arguments. */
9353 new_arg = TYPE_P (orig_arg)
9354 ? cxx_make_type (TREE_CODE (orig_arg))
9355 : make_node (TREE_CODE (orig_arg));
9356
9357 SET_ARGUMENT_PACK_ARGS (
9358 new_arg,
9359 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9360 args, complain, in_decl));
9361
9362 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9363 new_arg = error_mark_node;
9364
9365 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9366 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9367 complain, in_decl);
9368 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9369
9370 if (TREE_TYPE (new_arg) == error_mark_node)
9371 new_arg = error_mark_node;
9372 }
9373 }
9374 else
9375 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9376
9377 if (new_arg == error_mark_node)
9378 return error_mark_node;
9379
9380 elts[i] = new_arg;
9381 if (new_arg != orig_arg)
9382 need_new = 1;
9383 }
9384
9385 if (!need_new)
9386 return t;
9387
9388 /* Make space for the expanded arguments coming from template
9389 argument packs. */
9390 t = make_tree_vec (len + expanded_len_adjust);
9391 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9392 arguments for a member template.
9393 In that case each TREE_VEC in ORIG_T represents a level of template
9394 arguments, and ORIG_T won't carry any non defaulted argument count.
9395 It will rather be the nested TREE_VECs that will carry one.
9396 In other words, ORIG_T carries a non defaulted argument count only
9397 if it doesn't contain any nested TREE_VEC. */
9398 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9399 {
9400 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9401 count += expanded_len_adjust;
9402 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9403 }
9404 for (i = 0, out = 0; i < len; i++)
9405 {
9406 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9407 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9408 && TREE_CODE (elts[i]) == TREE_VEC)
9409 {
9410 int idx;
9411
9412 /* Now expand the template argument pack "in place". */
9413 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9414 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9415 }
9416 else
9417 {
9418 TREE_VEC_ELT (t, out) = elts[i];
9419 out++;
9420 }
9421 }
9422
9423 return t;
9424 }
9425
9426 /* Return the result of substituting ARGS into the template parameters
9427 given by PARMS. If there are m levels of ARGS and m + n levels of
9428 PARMS, then the result will contain n levels of PARMS. For
9429 example, if PARMS is `template <class T> template <class U>
9430 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9431 result will be `template <int*, double, class V>'. */
9432
9433 static tree
9434 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9435 {
9436 tree r = NULL_TREE;
9437 tree* new_parms;
9438
9439 /* When substituting into a template, we must set
9440 PROCESSING_TEMPLATE_DECL as the template parameters may be
9441 dependent if they are based on one-another, and the dependency
9442 predicates are short-circuit outside of templates. */
9443 ++processing_template_decl;
9444
9445 for (new_parms = &r;
9446 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9447 new_parms = &(TREE_CHAIN (*new_parms)),
9448 parms = TREE_CHAIN (parms))
9449 {
9450 tree new_vec =
9451 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9452 int i;
9453
9454 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9455 {
9456 tree tuple;
9457
9458 if (parms == error_mark_node)
9459 continue;
9460
9461 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9462
9463 if (tuple == error_mark_node)
9464 continue;
9465
9466 TREE_VEC_ELT (new_vec, i) =
9467 tsubst_template_parm (tuple, args, complain);
9468 }
9469
9470 *new_parms =
9471 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9472 - TMPL_ARGS_DEPTH (args)),
9473 new_vec, NULL_TREE);
9474 }
9475
9476 --processing_template_decl;
9477
9478 return r;
9479 }
9480
9481 /* Return the result of substituting ARGS into one template parameter
9482 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9483 parameter and which TREE_PURPOSE is the default argument of the
9484 template parameter. */
9485
9486 static tree
9487 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9488 {
9489 tree default_value, parm_decl;
9490
9491 if (args == NULL_TREE
9492 || t == NULL_TREE
9493 || t == error_mark_node)
9494 return t;
9495
9496 gcc_assert (TREE_CODE (t) == TREE_LIST);
9497
9498 default_value = TREE_PURPOSE (t);
9499 parm_decl = TREE_VALUE (t);
9500
9501 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9502 if (TREE_CODE (parm_decl) == PARM_DECL
9503 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9504 parm_decl = error_mark_node;
9505 default_value = tsubst_template_arg (default_value, args,
9506 complain, NULL_TREE);
9507
9508 return build_tree_list (default_value, parm_decl);
9509 }
9510
9511 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9512 type T. If T is not an aggregate or enumeration type, it is
9513 handled as if by tsubst. IN_DECL is as for tsubst. If
9514 ENTERING_SCOPE is nonzero, T is the context for a template which
9515 we are presently tsubst'ing. Return the substituted value. */
9516
9517 static tree
9518 tsubst_aggr_type (tree t,
9519 tree args,
9520 tsubst_flags_t complain,
9521 tree in_decl,
9522 int entering_scope)
9523 {
9524 if (t == NULL_TREE)
9525 return NULL_TREE;
9526
9527 switch (TREE_CODE (t))
9528 {
9529 case RECORD_TYPE:
9530 if (TYPE_PTRMEMFUNC_P (t))
9531 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9532
9533 /* Else fall through. */
9534 case ENUMERAL_TYPE:
9535 case UNION_TYPE:
9536 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9537 {
9538 tree argvec;
9539 tree context;
9540 tree r;
9541 int saved_unevaluated_operand;
9542 int saved_inhibit_evaluation_warnings;
9543
9544 /* In "sizeof(X<I>)" we need to evaluate "I". */
9545 saved_unevaluated_operand = cp_unevaluated_operand;
9546 cp_unevaluated_operand = 0;
9547 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9548 c_inhibit_evaluation_warnings = 0;
9549
9550 /* First, determine the context for the type we are looking
9551 up. */
9552 context = TYPE_CONTEXT (t);
9553 if (context)
9554 {
9555 context = tsubst_aggr_type (context, args, complain,
9556 in_decl, /*entering_scope=*/1);
9557 /* If context is a nested class inside a class template,
9558 it may still need to be instantiated (c++/33959). */
9559 if (TYPE_P (context))
9560 context = complete_type (context);
9561 }
9562
9563 /* Then, figure out what arguments are appropriate for the
9564 type we are trying to find. For example, given:
9565
9566 template <class T> struct S;
9567 template <class T, class U> void f(T, U) { S<U> su; }
9568
9569 and supposing that we are instantiating f<int, double>,
9570 then our ARGS will be {int, double}, but, when looking up
9571 S we only want {double}. */
9572 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9573 complain, in_decl);
9574 if (argvec == error_mark_node)
9575 r = error_mark_node;
9576 else
9577 {
9578 r = lookup_template_class (t, argvec, in_decl, context,
9579 entering_scope, complain);
9580 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
9581 }
9582
9583 cp_unevaluated_operand = saved_unevaluated_operand;
9584 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
9585
9586 return r;
9587 }
9588 else
9589 /* This is not a template type, so there's nothing to do. */
9590 return t;
9591
9592 default:
9593 return tsubst (t, args, complain, in_decl);
9594 }
9595 }
9596
9597 /* Substitute into the default argument ARG (a default argument for
9598 FN), which has the indicated TYPE. */
9599
9600 tree
9601 tsubst_default_argument (tree fn, tree type, tree arg)
9602 {
9603 tree saved_class_ptr = NULL_TREE;
9604 tree saved_class_ref = NULL_TREE;
9605
9606 /* This can happen in invalid code. */
9607 if (TREE_CODE (arg) == DEFAULT_ARG)
9608 return arg;
9609
9610 /* This default argument came from a template. Instantiate the
9611 default argument here, not in tsubst. In the case of
9612 something like:
9613
9614 template <class T>
9615 struct S {
9616 static T t();
9617 void f(T = t());
9618 };
9619
9620 we must be careful to do name lookup in the scope of S<T>,
9621 rather than in the current class. */
9622 push_access_scope (fn);
9623 /* The "this" pointer is not valid in a default argument. */
9624 if (cfun)
9625 {
9626 saved_class_ptr = current_class_ptr;
9627 cp_function_chain->x_current_class_ptr = NULL_TREE;
9628 saved_class_ref = current_class_ref;
9629 cp_function_chain->x_current_class_ref = NULL_TREE;
9630 }
9631
9632 push_deferring_access_checks(dk_no_deferred);
9633 /* The default argument expression may cause implicitly defined
9634 member functions to be synthesized, which will result in garbage
9635 collection. We must treat this situation as if we were within
9636 the body of function so as to avoid collecting live data on the
9637 stack. */
9638 ++function_depth;
9639 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
9640 tf_warning_or_error, NULL_TREE,
9641 /*integral_constant_expression_p=*/false);
9642 --function_depth;
9643 pop_deferring_access_checks();
9644
9645 /* Restore the "this" pointer. */
9646 if (cfun)
9647 {
9648 cp_function_chain->x_current_class_ptr = saved_class_ptr;
9649 cp_function_chain->x_current_class_ref = saved_class_ref;
9650 }
9651
9652 /* Make sure the default argument is reasonable. */
9653 arg = check_default_argument (type, arg);
9654
9655 pop_access_scope (fn);
9656
9657 return arg;
9658 }
9659
9660 /* Substitute into all the default arguments for FN. */
9661
9662 static void
9663 tsubst_default_arguments (tree fn)
9664 {
9665 tree arg;
9666 tree tmpl_args;
9667
9668 tmpl_args = DECL_TI_ARGS (fn);
9669
9670 /* If this function is not yet instantiated, we certainly don't need
9671 its default arguments. */
9672 if (uses_template_parms (tmpl_args))
9673 return;
9674 /* Don't do this again for clones. */
9675 if (DECL_CLONED_FUNCTION_P (fn))
9676 return;
9677
9678 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
9679 arg;
9680 arg = TREE_CHAIN (arg))
9681 if (TREE_PURPOSE (arg))
9682 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9683 TREE_VALUE (arg),
9684 TREE_PURPOSE (arg));
9685 }
9686
9687 /* Substitute the ARGS into the T, which is a _DECL. Return the
9688 result of the substitution. Issue error and warning messages under
9689 control of COMPLAIN. */
9690
9691 static tree
9692 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
9693 {
9694 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9695 location_t saved_loc;
9696 tree r = NULL_TREE;
9697 tree in_decl = t;
9698 hashval_t hash = 0;
9699
9700 /* Set the filename and linenumber to improve error-reporting. */
9701 saved_loc = input_location;
9702 input_location = DECL_SOURCE_LOCATION (t);
9703
9704 switch (TREE_CODE (t))
9705 {
9706 case TEMPLATE_DECL:
9707 {
9708 /* We can get here when processing a member function template,
9709 member class template, or template template parameter. */
9710 tree decl = DECL_TEMPLATE_RESULT (t);
9711 tree spec;
9712 tree tmpl_args;
9713 tree full_args;
9714
9715 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9716 {
9717 /* Template template parameter is treated here. */
9718 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9719 if (new_type == error_mark_node)
9720 RETURN (error_mark_node);
9721
9722 r = copy_decl (t);
9723 DECL_CHAIN (r) = NULL_TREE;
9724 TREE_TYPE (r) = new_type;
9725 DECL_TEMPLATE_RESULT (r)
9726 = build_decl (DECL_SOURCE_LOCATION (decl),
9727 TYPE_DECL, DECL_NAME (decl), new_type);
9728 DECL_TEMPLATE_PARMS (r)
9729 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9730 complain);
9731 TYPE_NAME (new_type) = r;
9732 break;
9733 }
9734
9735 /* We might already have an instance of this template.
9736 The ARGS are for the surrounding class type, so the
9737 full args contain the tsubst'd args for the context,
9738 plus the innermost args from the template decl. */
9739 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
9740 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
9741 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
9742 /* Because this is a template, the arguments will still be
9743 dependent, even after substitution. If
9744 PROCESSING_TEMPLATE_DECL is not set, the dependency
9745 predicates will short-circuit. */
9746 ++processing_template_decl;
9747 full_args = tsubst_template_args (tmpl_args, args,
9748 complain, in_decl);
9749 --processing_template_decl;
9750 if (full_args == error_mark_node)
9751 RETURN (error_mark_node);
9752
9753 /* If this is a default template template argument,
9754 tsubst might not have changed anything. */
9755 if (full_args == tmpl_args)
9756 RETURN (t);
9757
9758 hash = hash_tmpl_and_args (t, full_args);
9759 spec = retrieve_specialization (t, full_args, hash);
9760 if (spec != NULL_TREE)
9761 {
9762 r = spec;
9763 break;
9764 }
9765
9766 /* Make a new template decl. It will be similar to the
9767 original, but will record the current template arguments.
9768 We also create a new function declaration, which is just
9769 like the old one, but points to this new template, rather
9770 than the old one. */
9771 r = copy_decl (t);
9772 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
9773 DECL_CHAIN (r) = NULL_TREE;
9774
9775 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
9776
9777 if (TREE_CODE (decl) == TYPE_DECL)
9778 {
9779 tree new_type;
9780 ++processing_template_decl;
9781 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9782 --processing_template_decl;
9783 if (new_type == error_mark_node)
9784 RETURN (error_mark_node);
9785
9786 TREE_TYPE (r) = new_type;
9787 CLASSTYPE_TI_TEMPLATE (new_type) = r;
9788 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
9789 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
9790 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
9791 }
9792 else
9793 {
9794 tree new_decl;
9795 ++processing_template_decl;
9796 new_decl = tsubst (decl, args, complain, in_decl);
9797 --processing_template_decl;
9798 if (new_decl == error_mark_node)
9799 RETURN (error_mark_node);
9800
9801 DECL_TEMPLATE_RESULT (r) = new_decl;
9802 DECL_TI_TEMPLATE (new_decl) = r;
9803 TREE_TYPE (r) = TREE_TYPE (new_decl);
9804 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
9805 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
9806 }
9807
9808 SET_DECL_IMPLICIT_INSTANTIATION (r);
9809 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
9810 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
9811
9812 /* The template parameters for this new template are all the
9813 template parameters for the old template, except the
9814 outermost level of parameters. */
9815 DECL_TEMPLATE_PARMS (r)
9816 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9817 complain);
9818
9819 if (PRIMARY_TEMPLATE_P (t))
9820 DECL_PRIMARY_TEMPLATE (r) = r;
9821
9822 if (TREE_CODE (decl) != TYPE_DECL)
9823 /* Record this non-type partial instantiation. */
9824 register_specialization (r, t,
9825 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
9826 false, hash);
9827 }
9828 break;
9829
9830 case FUNCTION_DECL:
9831 {
9832 tree ctx;
9833 tree argvec = NULL_TREE;
9834 tree *friends;
9835 tree gen_tmpl;
9836 tree type;
9837 int member;
9838 int args_depth;
9839 int parms_depth;
9840
9841 /* Nobody should be tsubst'ing into non-template functions. */
9842 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
9843
9844 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
9845 {
9846 tree spec;
9847 bool dependent_p;
9848
9849 /* If T is not dependent, just return it. We have to
9850 increment PROCESSING_TEMPLATE_DECL because
9851 value_dependent_expression_p assumes that nothing is
9852 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9853 ++processing_template_decl;
9854 dependent_p = value_dependent_expression_p (t);
9855 --processing_template_decl;
9856 if (!dependent_p)
9857 RETURN (t);
9858
9859 /* Calculate the most general template of which R is a
9860 specialization, and the complete set of arguments used to
9861 specialize R. */
9862 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
9863 argvec = tsubst_template_args (DECL_TI_ARGS
9864 (DECL_TEMPLATE_RESULT
9865 (DECL_TI_TEMPLATE (t))),
9866 args, complain, in_decl);
9867 if (argvec == error_mark_node)
9868 RETURN (error_mark_node);
9869
9870 /* Check to see if we already have this specialization. */
9871 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9872 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9873
9874 if (spec)
9875 {
9876 r = spec;
9877 break;
9878 }
9879
9880 /* We can see more levels of arguments than parameters if
9881 there was a specialization of a member template, like
9882 this:
9883
9884 template <class T> struct S { template <class U> void f(); }
9885 template <> template <class U> void S<int>::f(U);
9886
9887 Here, we'll be substituting into the specialization,
9888 because that's where we can find the code we actually
9889 want to generate, but we'll have enough arguments for
9890 the most general template.
9891
9892 We also deal with the peculiar case:
9893
9894 template <class T> struct S {
9895 template <class U> friend void f();
9896 };
9897 template <class U> void f() {}
9898 template S<int>;
9899 template void f<double>();
9900
9901 Here, the ARGS for the instantiation of will be {int,
9902 double}. But, we only need as many ARGS as there are
9903 levels of template parameters in CODE_PATTERN. We are
9904 careful not to get fooled into reducing the ARGS in
9905 situations like:
9906
9907 template <class T> struct S { template <class U> void f(U); }
9908 template <class T> template <> void S<T>::f(int) {}
9909
9910 which we can spot because the pattern will be a
9911 specialization in this case. */
9912 args_depth = TMPL_ARGS_DEPTH (args);
9913 parms_depth =
9914 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
9915 if (args_depth > parms_depth
9916 && !DECL_TEMPLATE_SPECIALIZATION (t))
9917 args = get_innermost_template_args (args, parms_depth);
9918 }
9919 else
9920 {
9921 /* This special case arises when we have something like this:
9922
9923 template <class T> struct S {
9924 friend void f<int>(int, double);
9925 };
9926
9927 Here, the DECL_TI_TEMPLATE for the friend declaration
9928 will be an IDENTIFIER_NODE. We are being called from
9929 tsubst_friend_function, and we want only to create a
9930 new decl (R) with appropriate types so that we can call
9931 determine_specialization. */
9932 gen_tmpl = NULL_TREE;
9933 }
9934
9935 if (DECL_CLASS_SCOPE_P (t))
9936 {
9937 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
9938 member = 2;
9939 else
9940 member = 1;
9941 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
9942 complain, t, /*entering_scope=*/1);
9943 }
9944 else
9945 {
9946 member = 0;
9947 ctx = DECL_CONTEXT (t);
9948 }
9949 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9950 if (type == error_mark_node)
9951 RETURN (error_mark_node);
9952
9953 /* We do NOT check for matching decls pushed separately at this
9954 point, as they may not represent instantiations of this
9955 template, and in any case are considered separate under the
9956 discrete model. */
9957 r = copy_decl (t);
9958 DECL_USE_TEMPLATE (r) = 0;
9959 TREE_TYPE (r) = type;
9960 /* Clear out the mangled name and RTL for the instantiation. */
9961 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9962 SET_DECL_RTL (r, NULL);
9963 /* Leave DECL_INITIAL set on deleted instantiations. */
9964 if (!DECL_DELETED_FN (r))
9965 DECL_INITIAL (r) = NULL_TREE;
9966 DECL_CONTEXT (r) = ctx;
9967
9968 if (member && DECL_CONV_FN_P (r))
9969 /* Type-conversion operator. Reconstruct the name, in
9970 case it's the name of one of the template's parameters. */
9971 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
9972
9973 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
9974 complain, t);
9975 DECL_RESULT (r) = NULL_TREE;
9976
9977 TREE_STATIC (r) = 0;
9978 TREE_PUBLIC (r) = TREE_PUBLIC (t);
9979 DECL_EXTERNAL (r) = 1;
9980 /* If this is an instantiation of a function with internal
9981 linkage, we already know what object file linkage will be
9982 assigned to the instantiation. */
9983 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
9984 DECL_DEFER_OUTPUT (r) = 0;
9985 DECL_CHAIN (r) = NULL_TREE;
9986 DECL_PENDING_INLINE_INFO (r) = 0;
9987 DECL_PENDING_INLINE_P (r) = 0;
9988 DECL_SAVED_TREE (r) = NULL_TREE;
9989 DECL_STRUCT_FUNCTION (r) = NULL;
9990 TREE_USED (r) = 0;
9991 /* We'll re-clone as appropriate in instantiate_template. */
9992 DECL_CLONED_FUNCTION (r) = NULL_TREE;
9993
9994 /* If we aren't complaining now, return on error before we register
9995 the specialization so that we'll complain eventually. */
9996 if ((complain & tf_error) == 0
9997 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9998 && !grok_op_properties (r, /*complain=*/false))
9999 RETURN (error_mark_node);
10000
10001 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10002 this in the special friend case mentioned above where
10003 GEN_TMPL is NULL. */
10004 if (gen_tmpl)
10005 {
10006 DECL_TEMPLATE_INFO (r)
10007 = build_template_info (gen_tmpl, argvec);
10008 SET_DECL_IMPLICIT_INSTANTIATION (r);
10009 register_specialization (r, gen_tmpl, argvec, false, hash);
10010
10011 /* We're not supposed to instantiate default arguments
10012 until they are called, for a template. But, for a
10013 declaration like:
10014
10015 template <class T> void f ()
10016 { extern void g(int i = T()); }
10017
10018 we should do the substitution when the template is
10019 instantiated. We handle the member function case in
10020 instantiate_class_template since the default arguments
10021 might refer to other members of the class. */
10022 if (!member
10023 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10024 && !uses_template_parms (argvec))
10025 tsubst_default_arguments (r);
10026 }
10027 else
10028 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10029
10030 /* Copy the list of befriending classes. */
10031 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10032 *friends;
10033 friends = &TREE_CHAIN (*friends))
10034 {
10035 *friends = copy_node (*friends);
10036 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10037 args, complain,
10038 in_decl);
10039 }
10040
10041 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10042 {
10043 maybe_retrofit_in_chrg (r);
10044 if (DECL_CONSTRUCTOR_P (r))
10045 grok_ctor_properties (ctx, r);
10046 /* If this is an instantiation of a member template, clone it.
10047 If it isn't, that'll be handled by
10048 clone_constructors_and_destructors. */
10049 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10050 clone_function_decl (r, /*update_method_vec_p=*/0);
10051 }
10052 else if ((complain & tf_error) != 0
10053 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10054 && !grok_op_properties (r, /*complain=*/true))
10055 RETURN (error_mark_node);
10056
10057 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10058 SET_DECL_FRIEND_CONTEXT (r,
10059 tsubst (DECL_FRIEND_CONTEXT (t),
10060 args, complain, in_decl));
10061
10062 /* Possibly limit visibility based on template args. */
10063 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10064 if (DECL_VISIBILITY_SPECIFIED (t))
10065 {
10066 DECL_VISIBILITY_SPECIFIED (r) = 0;
10067 DECL_ATTRIBUTES (r)
10068 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10069 }
10070 determine_visibility (r);
10071 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10072 && !processing_template_decl)
10073 defaulted_late_check (r);
10074
10075 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10076 args, complain, in_decl);
10077 }
10078 break;
10079
10080 case PARM_DECL:
10081 {
10082 tree type = NULL_TREE;
10083 int i, len = 1;
10084 tree expanded_types = NULL_TREE;
10085 tree prev_r = NULL_TREE;
10086 tree first_r = NULL_TREE;
10087
10088 if (FUNCTION_PARAMETER_PACK_P (t))
10089 {
10090 /* If there is a local specialization that isn't a
10091 parameter pack, it means that we're doing a "simple"
10092 substitution from inside tsubst_pack_expansion. Just
10093 return the local specialization (which will be a single
10094 parm). */
10095 tree spec = retrieve_local_specialization (t);
10096 if (spec
10097 && TREE_CODE (spec) == PARM_DECL
10098 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10099 RETURN (spec);
10100
10101 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10102 the parameters in this function parameter pack. */
10103 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10104 complain, in_decl);
10105 if (TREE_CODE (expanded_types) == TREE_VEC)
10106 {
10107 len = TREE_VEC_LENGTH (expanded_types);
10108
10109 /* Zero-length parameter packs are boring. Just substitute
10110 into the chain. */
10111 if (len == 0)
10112 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10113 TREE_CHAIN (t)));
10114 }
10115 else
10116 {
10117 /* All we did was update the type. Make a note of that. */
10118 type = expanded_types;
10119 expanded_types = NULL_TREE;
10120 }
10121 }
10122
10123 /* Loop through all of the parameter's we'll build. When T is
10124 a function parameter pack, LEN is the number of expanded
10125 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10126 r = NULL_TREE;
10127 for (i = 0; i < len; ++i)
10128 {
10129 prev_r = r;
10130 r = copy_node (t);
10131 if (DECL_TEMPLATE_PARM_P (t))
10132 SET_DECL_TEMPLATE_PARM_P (r);
10133
10134 if (expanded_types)
10135 /* We're on the Ith parameter of the function parameter
10136 pack. */
10137 {
10138 /* An argument of a function parameter pack is not a parameter
10139 pack. */
10140 FUNCTION_PARAMETER_PACK_P (r) = false;
10141
10142 /* Get the Ith type. */
10143 type = TREE_VEC_ELT (expanded_types, i);
10144
10145 if (DECL_NAME (r))
10146 /* Rename the parameter to include the index. */
10147 DECL_NAME (r) =
10148 make_ith_pack_parameter_name (DECL_NAME (r), i);
10149 }
10150 else if (!type)
10151 /* We're dealing with a normal parameter. */
10152 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10153
10154 type = type_decays_to (type);
10155 TREE_TYPE (r) = type;
10156 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10157
10158 if (DECL_INITIAL (r))
10159 {
10160 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10161 DECL_INITIAL (r) = TREE_TYPE (r);
10162 else
10163 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10164 complain, in_decl);
10165 }
10166
10167 DECL_CONTEXT (r) = NULL_TREE;
10168
10169 if (!DECL_TEMPLATE_PARM_P (r))
10170 DECL_ARG_TYPE (r) = type_passed_as (type);
10171
10172 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10173 args, complain, in_decl);
10174
10175 /* Keep track of the first new parameter we
10176 generate. That's what will be returned to the
10177 caller. */
10178 if (!first_r)
10179 first_r = r;
10180
10181 /* Build a proper chain of parameters when substituting
10182 into a function parameter pack. */
10183 if (prev_r)
10184 DECL_CHAIN (prev_r) = r;
10185 }
10186
10187 if (DECL_CHAIN (t))
10188 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10189 complain, DECL_CHAIN (t));
10190
10191 /* FIRST_R contains the start of the chain we've built. */
10192 r = first_r;
10193 }
10194 break;
10195
10196 case FIELD_DECL:
10197 {
10198 tree type;
10199
10200 r = copy_decl (t);
10201 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10202 if (type == error_mark_node)
10203 RETURN (error_mark_node);
10204 TREE_TYPE (r) = type;
10205 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10206
10207 /* DECL_INITIAL gives the number of bits in a bit-field. */
10208 DECL_INITIAL (r)
10209 = tsubst_expr (DECL_INITIAL (t), args,
10210 complain, in_decl,
10211 /*integral_constant_expression_p=*/true);
10212 /* We don't have to set DECL_CONTEXT here; it is set by
10213 finish_member_declaration. */
10214 DECL_CHAIN (r) = NULL_TREE;
10215 if (VOID_TYPE_P (type))
10216 error ("instantiation of %q+D as type %qT", r, type);
10217
10218 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10219 args, complain, in_decl);
10220 }
10221 break;
10222
10223 case USING_DECL:
10224 /* We reach here only for member using decls. */
10225 if (DECL_DEPENDENT_P (t))
10226 {
10227 r = do_class_using_decl
10228 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
10229 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
10230 if (!r)
10231 r = error_mark_node;
10232 else
10233 {
10234 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10235 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10236 }
10237 }
10238 else
10239 {
10240 r = copy_node (t);
10241 DECL_CHAIN (r) = NULL_TREE;
10242 }
10243 break;
10244
10245 case TYPE_DECL:
10246 case VAR_DECL:
10247 {
10248 tree argvec = NULL_TREE;
10249 tree gen_tmpl = NULL_TREE;
10250 tree spec;
10251 tree tmpl = NULL_TREE;
10252 tree ctx;
10253 tree type = NULL_TREE;
10254 bool local_p;
10255
10256 if (TREE_CODE (t) == TYPE_DECL
10257 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10258 {
10259 /* If this is the canonical decl, we don't have to
10260 mess with instantiations, and often we can't (for
10261 typename, template type parms and such). Note that
10262 TYPE_NAME is not correct for the above test if
10263 we've copied the type for a typedef. */
10264 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10265 if (type == error_mark_node)
10266 RETURN (error_mark_node);
10267 r = TYPE_NAME (type);
10268 break;
10269 }
10270
10271 /* Check to see if we already have the specialization we
10272 need. */
10273 spec = NULL_TREE;
10274 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10275 {
10276 /* T is a static data member or namespace-scope entity.
10277 We have to substitute into namespace-scope variables
10278 (even though such entities are never templates) because
10279 of cases like:
10280
10281 template <class T> void f() { extern T t; }
10282
10283 where the entity referenced is not known until
10284 instantiation time. */
10285 local_p = false;
10286 ctx = DECL_CONTEXT (t);
10287 if (DECL_CLASS_SCOPE_P (t))
10288 {
10289 ctx = tsubst_aggr_type (ctx, args,
10290 complain,
10291 in_decl, /*entering_scope=*/1);
10292 /* If CTX is unchanged, then T is in fact the
10293 specialization we want. That situation occurs when
10294 referencing a static data member within in its own
10295 class. We can use pointer equality, rather than
10296 same_type_p, because DECL_CONTEXT is always
10297 canonical. */
10298 if (ctx == DECL_CONTEXT (t))
10299 spec = t;
10300 }
10301
10302 if (!spec)
10303 {
10304 tmpl = DECL_TI_TEMPLATE (t);
10305 gen_tmpl = most_general_template (tmpl);
10306 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10307 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10308 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10309 }
10310 }
10311 else
10312 {
10313 /* A local variable. */
10314 local_p = true;
10315 /* Subsequent calls to pushdecl will fill this in. */
10316 ctx = NULL_TREE;
10317 spec = retrieve_local_specialization (t);
10318 }
10319 /* If we already have the specialization we need, there is
10320 nothing more to do. */
10321 if (spec)
10322 {
10323 r = spec;
10324 break;
10325 }
10326
10327 /* Create a new node for the specialization we need. */
10328 r = copy_decl (t);
10329 if (type == NULL_TREE)
10330 {
10331 if (is_typedef_decl (t))
10332 type = DECL_ORIGINAL_TYPE (t);
10333 else
10334 type = TREE_TYPE (t);
10335 if (TREE_CODE (t) == VAR_DECL && VAR_HAD_UNKNOWN_BOUND (t))
10336 type = strip_array_domain (type);
10337 type = tsubst (type, args, complain, in_decl);
10338 }
10339 if (TREE_CODE (r) == VAR_DECL)
10340 {
10341 /* Even if the original location is out of scope, the
10342 newly substituted one is not. */
10343 DECL_DEAD_FOR_LOCAL (r) = 0;
10344 DECL_INITIALIZED_P (r) = 0;
10345 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10346 if (type == error_mark_node)
10347 RETURN (error_mark_node);
10348 if (TREE_CODE (type) == FUNCTION_TYPE)
10349 {
10350 /* It may seem that this case cannot occur, since:
10351
10352 typedef void f();
10353 void g() { f x; }
10354
10355 declares a function, not a variable. However:
10356
10357 typedef void f();
10358 template <typename T> void g() { T t; }
10359 template void g<f>();
10360
10361 is an attempt to declare a variable with function
10362 type. */
10363 error ("variable %qD has function type",
10364 /* R is not yet sufficiently initialized, so we
10365 just use its name. */
10366 DECL_NAME (r));
10367 RETURN (error_mark_node);
10368 }
10369 type = complete_type (type);
10370 /* Wait until cp_finish_decl to set this again, to handle
10371 circular dependency (template/instantiate6.C). */
10372 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10373 type = check_var_type (DECL_NAME (r), type);
10374
10375 if (DECL_HAS_VALUE_EXPR_P (t))
10376 {
10377 tree ve = DECL_VALUE_EXPR (t);
10378 ve = tsubst_expr (ve, args, complain, in_decl,
10379 /*constant_expression_p=*/false);
10380 if (REFERENCE_REF_P (ve))
10381 {
10382 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10383 ve = TREE_OPERAND (ve, 0);
10384 }
10385 SET_DECL_VALUE_EXPR (r, ve);
10386 }
10387 }
10388 else if (DECL_SELF_REFERENCE_P (t))
10389 SET_DECL_SELF_REFERENCE_P (r);
10390 TREE_TYPE (r) = type;
10391 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10392 DECL_CONTEXT (r) = ctx;
10393 /* Clear out the mangled name and RTL for the instantiation. */
10394 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10395 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10396 SET_DECL_RTL (r, NULL);
10397 /* The initializer must not be expanded until it is required;
10398 see [temp.inst]. */
10399 DECL_INITIAL (r) = NULL_TREE;
10400 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10401 SET_DECL_RTL (r, NULL);
10402 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10403 if (TREE_CODE (r) == VAR_DECL)
10404 {
10405 /* Possibly limit visibility based on template args. */
10406 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10407 if (DECL_VISIBILITY_SPECIFIED (t))
10408 {
10409 DECL_VISIBILITY_SPECIFIED (r) = 0;
10410 DECL_ATTRIBUTES (r)
10411 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10412 }
10413 determine_visibility (r);
10414 }
10415
10416 if (!local_p)
10417 {
10418 /* A static data member declaration is always marked
10419 external when it is declared in-class, even if an
10420 initializer is present. We mimic the non-template
10421 processing here. */
10422 DECL_EXTERNAL (r) = 1;
10423
10424 register_specialization (r, gen_tmpl, argvec, false, hash);
10425 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10426 SET_DECL_IMPLICIT_INSTANTIATION (r);
10427 }
10428 else if (cp_unevaluated_operand)
10429 {
10430 /* We're substituting this var in a decltype outside of its
10431 scope, such as for a lambda return type. Don't add it to
10432 local_specializations, do perform auto deduction. */
10433 tree auto_node = type_uses_auto (type);
10434 if (auto_node)
10435 {
10436 tree init
10437 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
10438 /*constant_expression_p=*/false);
10439 init = resolve_nondeduced_context (init);
10440 TREE_TYPE (r) = type
10441 = do_auto_deduction (type, init, auto_node);
10442 }
10443 }
10444 else
10445 register_local_specialization (r, t);
10446
10447 DECL_CHAIN (r) = NULL_TREE;
10448
10449 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10450 /*flags=*/0,
10451 args, complain, in_decl);
10452
10453 /* Preserve a typedef that names a type. */
10454 if (is_typedef_decl (r))
10455 {
10456 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10457 set_underlying_type (r);
10458 }
10459
10460 layout_decl (r, 0);
10461 }
10462 break;
10463
10464 default:
10465 gcc_unreachable ();
10466 }
10467 #undef RETURN
10468
10469 out:
10470 /* Restore the file and line information. */
10471 input_location = saved_loc;
10472
10473 return r;
10474 }
10475
10476 /* Substitute into the ARG_TYPES of a function type. */
10477
10478 static tree
10479 tsubst_arg_types (tree arg_types,
10480 tree args,
10481 tsubst_flags_t complain,
10482 tree in_decl)
10483 {
10484 tree remaining_arg_types;
10485 tree type = NULL_TREE;
10486 int i = 1;
10487 tree expanded_args = NULL_TREE;
10488 tree default_arg;
10489
10490 if (!arg_types || arg_types == void_list_node)
10491 return arg_types;
10492
10493 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
10494 args, complain, in_decl);
10495 if (remaining_arg_types == error_mark_node)
10496 return error_mark_node;
10497
10498 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
10499 {
10500 /* For a pack expansion, perform substitution on the
10501 entire expression. Later on, we'll handle the arguments
10502 one-by-one. */
10503 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
10504 args, complain, in_decl);
10505
10506 if (TREE_CODE (expanded_args) == TREE_VEC)
10507 /* So that we'll spin through the parameters, one by one. */
10508 i = TREE_VEC_LENGTH (expanded_args);
10509 else
10510 {
10511 /* We only partially substituted into the parameter
10512 pack. Our type is TYPE_PACK_EXPANSION. */
10513 type = expanded_args;
10514 expanded_args = NULL_TREE;
10515 }
10516 }
10517
10518 while (i > 0) {
10519 --i;
10520
10521 if (expanded_args)
10522 type = TREE_VEC_ELT (expanded_args, i);
10523 else if (!type)
10524 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
10525
10526 if (type == error_mark_node)
10527 return error_mark_node;
10528 if (VOID_TYPE_P (type))
10529 {
10530 if (complain & tf_error)
10531 {
10532 error ("invalid parameter type %qT", type);
10533 if (in_decl)
10534 error ("in declaration %q+D", in_decl);
10535 }
10536 return error_mark_node;
10537 }
10538
10539 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10540 top-level qualifiers as required. */
10541 type = cv_unqualified (type_decays_to (type));
10542
10543 /* We do not substitute into default arguments here. The standard
10544 mandates that they be instantiated only when needed, which is
10545 done in build_over_call. */
10546 default_arg = TREE_PURPOSE (arg_types);
10547
10548 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
10549 {
10550 /* We've instantiated a template before its default arguments
10551 have been parsed. This can happen for a nested template
10552 class, and is not an error unless we require the default
10553 argument in a call of this function. */
10554 remaining_arg_types =
10555 tree_cons (default_arg, type, remaining_arg_types);
10556 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
10557 remaining_arg_types);
10558 }
10559 else
10560 remaining_arg_types =
10561 hash_tree_cons (default_arg, type, remaining_arg_types);
10562 }
10563
10564 return remaining_arg_types;
10565 }
10566
10567 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10568 *not* handle the exception-specification for FNTYPE, because the
10569 initial substitution of explicitly provided template parameters
10570 during argument deduction forbids substitution into the
10571 exception-specification:
10572
10573 [temp.deduct]
10574
10575 All references in the function type of the function template to the
10576 corresponding template parameters are replaced by the specified tem-
10577 plate argument values. If a substitution in a template parameter or
10578 in the function type of the function template results in an invalid
10579 type, type deduction fails. [Note: The equivalent substitution in
10580 exception specifications is done only when the function is instanti-
10581 ated, at which point a program is ill-formed if the substitution
10582 results in an invalid type.] */
10583
10584 static tree
10585 tsubst_function_type (tree t,
10586 tree args,
10587 tsubst_flags_t complain,
10588 tree in_decl)
10589 {
10590 tree return_type;
10591 tree arg_types;
10592 tree fntype;
10593
10594 /* The TYPE_CONTEXT is not used for function/method types. */
10595 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
10596
10597 /* Substitute the return type. */
10598 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10599 if (return_type == error_mark_node)
10600 return error_mark_node;
10601 /* The standard does not presently indicate that creation of a
10602 function type with an invalid return type is a deduction failure.
10603 However, that is clearly analogous to creating an array of "void"
10604 or a reference to a reference. This is core issue #486. */
10605 if (TREE_CODE (return_type) == ARRAY_TYPE
10606 || TREE_CODE (return_type) == FUNCTION_TYPE)
10607 {
10608 if (complain & tf_error)
10609 {
10610 if (TREE_CODE (return_type) == ARRAY_TYPE)
10611 error ("function returning an array");
10612 else
10613 error ("function returning a function");
10614 }
10615 return error_mark_node;
10616 }
10617
10618 /* Substitute the argument types. */
10619 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
10620 complain, in_decl);
10621 if (arg_types == error_mark_node)
10622 return error_mark_node;
10623
10624 /* Construct a new type node and return it. */
10625 if (TREE_CODE (t) == FUNCTION_TYPE)
10626 {
10627 fntype = build_function_type (return_type, arg_types);
10628 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
10629 }
10630 else
10631 {
10632 tree r = TREE_TYPE (TREE_VALUE (arg_types));
10633 if (! MAYBE_CLASS_TYPE_P (r))
10634 {
10635 /* [temp.deduct]
10636
10637 Type deduction may fail for any of the following
10638 reasons:
10639
10640 -- Attempting to create "pointer to member of T" when T
10641 is not a class type. */
10642 if (complain & tf_error)
10643 error ("creating pointer to member function of non-class type %qT",
10644 r);
10645 return error_mark_node;
10646 }
10647
10648 fntype = build_method_type_directly (r, return_type,
10649 TREE_CHAIN (arg_types));
10650 }
10651 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
10652
10653 return fntype;
10654 }
10655
10656 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10657 ARGS into that specification, and return the substituted
10658 specification. If there is no specification, return NULL_TREE. */
10659
10660 static tree
10661 tsubst_exception_specification (tree fntype,
10662 tree args,
10663 tsubst_flags_t complain,
10664 tree in_decl,
10665 bool defer_ok)
10666 {
10667 tree specs;
10668 tree new_specs;
10669
10670 specs = TYPE_RAISES_EXCEPTIONS (fntype);
10671 new_specs = NULL_TREE;
10672 if (specs && TREE_PURPOSE (specs))
10673 {
10674 /* A noexcept-specifier. */
10675 tree expr = TREE_PURPOSE (specs);
10676 if (expr == boolean_true_node || expr == boolean_false_node)
10677 new_specs = expr;
10678 else if (defer_ok)
10679 {
10680 /* Defer instantiation of noexcept-specifiers to avoid
10681 excessive instantiations (c++/49107). */
10682 new_specs = make_node (DEFERRED_NOEXCEPT);
10683 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
10684 {
10685 /* We already partially instantiated this member template,
10686 so combine the new args with the old. */
10687 DEFERRED_NOEXCEPT_PATTERN (new_specs)
10688 = DEFERRED_NOEXCEPT_PATTERN (expr);
10689 DEFERRED_NOEXCEPT_ARGS (new_specs)
10690 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
10691 }
10692 else
10693 {
10694 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
10695 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
10696 }
10697 }
10698 else
10699 new_specs = tsubst_copy_and_build
10700 (expr, args, complain, in_decl, /*function_p=*/false,
10701 /*integral_constant_expression_p=*/true);
10702 new_specs = build_noexcept_spec (new_specs, complain);
10703 }
10704 else if (specs)
10705 {
10706 if (! TREE_VALUE (specs))
10707 new_specs = specs;
10708 else
10709 while (specs)
10710 {
10711 tree spec;
10712 int i, len = 1;
10713 tree expanded_specs = NULL_TREE;
10714
10715 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
10716 {
10717 /* Expand the pack expansion type. */
10718 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
10719 args, complain,
10720 in_decl);
10721
10722 if (expanded_specs == error_mark_node)
10723 return error_mark_node;
10724 else if (TREE_CODE (expanded_specs) == TREE_VEC)
10725 len = TREE_VEC_LENGTH (expanded_specs);
10726 else
10727 {
10728 /* We're substituting into a member template, so
10729 we got a TYPE_PACK_EXPANSION back. Add that
10730 expansion and move on. */
10731 gcc_assert (TREE_CODE (expanded_specs)
10732 == TYPE_PACK_EXPANSION);
10733 new_specs = add_exception_specifier (new_specs,
10734 expanded_specs,
10735 complain);
10736 specs = TREE_CHAIN (specs);
10737 continue;
10738 }
10739 }
10740
10741 for (i = 0; i < len; ++i)
10742 {
10743 if (expanded_specs)
10744 spec = TREE_VEC_ELT (expanded_specs, i);
10745 else
10746 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
10747 if (spec == error_mark_node)
10748 return spec;
10749 new_specs = add_exception_specifier (new_specs, spec,
10750 complain);
10751 }
10752
10753 specs = TREE_CHAIN (specs);
10754 }
10755 }
10756 return new_specs;
10757 }
10758
10759 /* Take the tree structure T and replace template parameters used
10760 therein with the argument vector ARGS. IN_DECL is an associated
10761 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10762 Issue error and warning messages under control of COMPLAIN. Note
10763 that we must be relatively non-tolerant of extensions here, in
10764 order to preserve conformance; if we allow substitutions that
10765 should not be allowed, we may allow argument deductions that should
10766 not succeed, and therefore report ambiguous overload situations
10767 where there are none. In theory, we could allow the substitution,
10768 but indicate that it should have failed, and allow our caller to
10769 make sure that the right thing happens, but we don't try to do this
10770 yet.
10771
10772 This function is used for dealing with types, decls and the like;
10773 for expressions, use tsubst_expr or tsubst_copy. */
10774
10775 tree
10776 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10777 {
10778 enum tree_code code;
10779 tree type, r;
10780
10781 if (t == NULL_TREE || t == error_mark_node
10782 || t == integer_type_node
10783 || t == void_type_node
10784 || t == char_type_node
10785 || t == unknown_type_node
10786 || TREE_CODE (t) == NAMESPACE_DECL
10787 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
10788 return t;
10789
10790 if (DECL_P (t))
10791 return tsubst_decl (t, args, complain);
10792
10793 if (args == NULL_TREE)
10794 return t;
10795
10796 code = TREE_CODE (t);
10797
10798 if (code == IDENTIFIER_NODE)
10799 type = IDENTIFIER_TYPE_VALUE (t);
10800 else
10801 type = TREE_TYPE (t);
10802
10803 gcc_assert (type != unknown_type_node);
10804
10805 /* Reuse typedefs. We need to do this to handle dependent attributes,
10806 such as attribute aligned. */
10807 if (TYPE_P (t)
10808 && typedef_variant_p (t))
10809 {
10810 tree decl = TYPE_NAME (t);
10811
10812 if (DECL_CLASS_SCOPE_P (decl)
10813 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
10814 && uses_template_parms (DECL_CONTEXT (decl)))
10815 {
10816 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10817 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
10818 r = retrieve_specialization (tmpl, gen_args, 0);
10819 }
10820 else if (DECL_FUNCTION_SCOPE_P (decl)
10821 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
10822 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
10823 r = retrieve_local_specialization (decl);
10824 else
10825 /* The typedef is from a non-template context. */
10826 return t;
10827
10828 if (r)
10829 {
10830 r = TREE_TYPE (r);
10831 r = cp_build_qualified_type_real
10832 (r, cp_type_quals (t) | cp_type_quals (r),
10833 complain | tf_ignore_bad_quals);
10834 return r;
10835 }
10836 /* Else we must be instantiating the typedef, so fall through. */
10837 }
10838
10839 if (type
10840 && code != TYPENAME_TYPE
10841 && code != TEMPLATE_TYPE_PARM
10842 && code != IDENTIFIER_NODE
10843 && code != FUNCTION_TYPE
10844 && code != METHOD_TYPE)
10845 type = tsubst (type, args, complain, in_decl);
10846 if (type == error_mark_node)
10847 return error_mark_node;
10848
10849 switch (code)
10850 {
10851 case RECORD_TYPE:
10852 case UNION_TYPE:
10853 case ENUMERAL_TYPE:
10854 return tsubst_aggr_type (t, args, complain, in_decl,
10855 /*entering_scope=*/0);
10856
10857 case ERROR_MARK:
10858 case IDENTIFIER_NODE:
10859 case VOID_TYPE:
10860 case REAL_TYPE:
10861 case COMPLEX_TYPE:
10862 case VECTOR_TYPE:
10863 case BOOLEAN_TYPE:
10864 case NULLPTR_TYPE:
10865 case LANG_TYPE:
10866 return t;
10867
10868 case INTEGER_TYPE:
10869 if (t == integer_type_node)
10870 return t;
10871
10872 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
10873 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
10874 return t;
10875
10876 {
10877 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
10878
10879 max = tsubst_expr (omax, args, complain, in_decl,
10880 /*integral_constant_expression_p=*/false);
10881
10882 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10883 needed. */
10884 if (TREE_CODE (max) == NOP_EXPR
10885 && TREE_SIDE_EFFECTS (omax)
10886 && !TREE_TYPE (max))
10887 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
10888
10889 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10890 with TREE_SIDE_EFFECTS that indicates this is not an integral
10891 constant expression. */
10892 if (processing_template_decl
10893 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
10894 {
10895 gcc_assert (TREE_CODE (max) == NOP_EXPR);
10896 TREE_SIDE_EFFECTS (max) = 1;
10897 }
10898
10899 return compute_array_index_type (NULL_TREE, max, complain);
10900 }
10901
10902 case TEMPLATE_TYPE_PARM:
10903 case TEMPLATE_TEMPLATE_PARM:
10904 case BOUND_TEMPLATE_TEMPLATE_PARM:
10905 case TEMPLATE_PARM_INDEX:
10906 {
10907 int idx;
10908 int level;
10909 int levels;
10910 tree arg = NULL_TREE;
10911
10912 r = NULL_TREE;
10913
10914 gcc_assert (TREE_VEC_LENGTH (args) > 0);
10915 template_parm_level_and_index (t, &level, &idx);
10916
10917 levels = TMPL_ARGS_DEPTH (args);
10918 if (level <= levels)
10919 {
10920 arg = TMPL_ARG (args, level, idx);
10921
10922 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
10923 /* See through ARGUMENT_PACK_SELECT arguments. */
10924 arg = ARGUMENT_PACK_SELECT_ARG (arg);
10925 }
10926
10927 if (arg == error_mark_node)
10928 return error_mark_node;
10929 else if (arg != NULL_TREE)
10930 {
10931 if (ARGUMENT_PACK_P (arg))
10932 /* If ARG is an argument pack, we don't actually want to
10933 perform a substitution here, because substitutions
10934 for argument packs are only done
10935 element-by-element. We can get to this point when
10936 substituting the type of a non-type template
10937 parameter pack, when that type actually contains
10938 template parameter packs from an outer template, e.g.,
10939
10940 template<typename... Types> struct A {
10941 template<Types... Values> struct B { };
10942 }; */
10943 return t;
10944
10945 if (code == TEMPLATE_TYPE_PARM)
10946 {
10947 int quals;
10948 gcc_assert (TYPE_P (arg));
10949
10950 quals = cp_type_quals (arg) | cp_type_quals (t);
10951
10952 return cp_build_qualified_type_real
10953 (arg, quals, complain | tf_ignore_bad_quals);
10954 }
10955 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10956 {
10957 /* We are processing a type constructed from a
10958 template template parameter. */
10959 tree argvec = tsubst (TYPE_TI_ARGS (t),
10960 args, complain, in_decl);
10961 if (argvec == error_mark_node)
10962 return error_mark_node;
10963
10964 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10965 are resolving nested-types in the signature of a
10966 member function templates. Otherwise ARG is a
10967 TEMPLATE_DECL and is the real template to be
10968 instantiated. */
10969 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
10970 arg = TYPE_NAME (arg);
10971
10972 r = lookup_template_class (arg,
10973 argvec, in_decl,
10974 DECL_CONTEXT (arg),
10975 /*entering_scope=*/0,
10976 complain);
10977 return cp_build_qualified_type_real
10978 (r, cp_type_quals (t), complain);
10979 }
10980 else
10981 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
10982 return unshare_expr (arg);
10983 }
10984
10985 if (level == 1)
10986 /* This can happen during the attempted tsubst'ing in
10987 unify. This means that we don't yet have any information
10988 about the template parameter in question. */
10989 return t;
10990
10991 /* If we get here, we must have been looking at a parm for a
10992 more deeply nested template. Make a new version of this
10993 template parameter, but with a lower level. */
10994 switch (code)
10995 {
10996 case TEMPLATE_TYPE_PARM:
10997 case TEMPLATE_TEMPLATE_PARM:
10998 case BOUND_TEMPLATE_TEMPLATE_PARM:
10999 if (cp_type_quals (t))
11000 {
11001 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11002 r = cp_build_qualified_type_real
11003 (r, cp_type_quals (t),
11004 complain | (code == TEMPLATE_TYPE_PARM
11005 ? tf_ignore_bad_quals : 0));
11006 }
11007 else
11008 {
11009 r = copy_type (t);
11010 TEMPLATE_TYPE_PARM_INDEX (r)
11011 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11012 r, levels, args, complain);
11013 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11014 TYPE_MAIN_VARIANT (r) = r;
11015 TYPE_POINTER_TO (r) = NULL_TREE;
11016 TYPE_REFERENCE_TO (r) = NULL_TREE;
11017
11018 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11019 /* We have reduced the level of the template
11020 template parameter, but not the levels of its
11021 template parameters, so canonical_type_parameter
11022 will not be able to find the canonical template
11023 template parameter for this level. Thus, we
11024 require structural equality checking to compare
11025 TEMPLATE_TEMPLATE_PARMs. */
11026 SET_TYPE_STRUCTURAL_EQUALITY (r);
11027 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11028 SET_TYPE_STRUCTURAL_EQUALITY (r);
11029 else
11030 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11031
11032 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11033 {
11034 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11035 complain, in_decl);
11036 if (argvec == error_mark_node)
11037 return error_mark_node;
11038
11039 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11040 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11041 }
11042 }
11043 break;
11044
11045 case TEMPLATE_PARM_INDEX:
11046 r = reduce_template_parm_level (t, type, levels, args, complain);
11047 break;
11048
11049 default:
11050 gcc_unreachable ();
11051 }
11052
11053 return r;
11054 }
11055
11056 case TREE_LIST:
11057 {
11058 tree purpose, value, chain;
11059
11060 if (t == void_list_node)
11061 return t;
11062
11063 purpose = TREE_PURPOSE (t);
11064 if (purpose)
11065 {
11066 purpose = tsubst (purpose, args, complain, in_decl);
11067 if (purpose == error_mark_node)
11068 return error_mark_node;
11069 }
11070 value = TREE_VALUE (t);
11071 if (value)
11072 {
11073 value = tsubst (value, args, complain, in_decl);
11074 if (value == error_mark_node)
11075 return error_mark_node;
11076 }
11077 chain = TREE_CHAIN (t);
11078 if (chain && chain != void_type_node)
11079 {
11080 chain = tsubst (chain, args, complain, in_decl);
11081 if (chain == error_mark_node)
11082 return error_mark_node;
11083 }
11084 if (purpose == TREE_PURPOSE (t)
11085 && value == TREE_VALUE (t)
11086 && chain == TREE_CHAIN (t))
11087 return t;
11088 return hash_tree_cons (purpose, value, chain);
11089 }
11090
11091 case TREE_BINFO:
11092 /* We should never be tsubsting a binfo. */
11093 gcc_unreachable ();
11094
11095 case TREE_VEC:
11096 /* A vector of template arguments. */
11097 gcc_assert (!type);
11098 return tsubst_template_args (t, args, complain, in_decl);
11099
11100 case POINTER_TYPE:
11101 case REFERENCE_TYPE:
11102 {
11103 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11104 return t;
11105
11106 /* [temp.deduct]
11107
11108 Type deduction may fail for any of the following
11109 reasons:
11110
11111 -- Attempting to create a pointer to reference type.
11112 -- Attempting to create a reference to a reference type or
11113 a reference to void.
11114
11115 Core issue 106 says that creating a reference to a reference
11116 during instantiation is no longer a cause for failure. We
11117 only enforce this check in strict C++98 mode. */
11118 if ((TREE_CODE (type) == REFERENCE_TYPE
11119 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11120 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
11121 {
11122 static location_t last_loc;
11123
11124 /* We keep track of the last time we issued this error
11125 message to avoid spewing a ton of messages during a
11126 single bad template instantiation. */
11127 if (complain & tf_error
11128 && last_loc != input_location)
11129 {
11130 if (TREE_CODE (type) == VOID_TYPE)
11131 error ("forming reference to void");
11132 else if (code == POINTER_TYPE)
11133 error ("forming pointer to reference type %qT", type);
11134 else
11135 error ("forming reference to reference type %qT", type);
11136 last_loc = input_location;
11137 }
11138
11139 return error_mark_node;
11140 }
11141 else if (code == POINTER_TYPE)
11142 {
11143 r = build_pointer_type (type);
11144 if (TREE_CODE (type) == METHOD_TYPE)
11145 r = build_ptrmemfunc_type (r);
11146 }
11147 else if (TREE_CODE (type) == REFERENCE_TYPE)
11148 /* In C++0x, during template argument substitution, when there is an
11149 attempt to create a reference to a reference type, reference
11150 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11151
11152 "If a template-argument for a template-parameter T names a type
11153 that is a reference to a type A, an attempt to create the type
11154 'lvalue reference to cv T' creates the type 'lvalue reference to
11155 A,' while an attempt to create the type type rvalue reference to
11156 cv T' creates the type T"
11157 */
11158 r = cp_build_reference_type
11159 (TREE_TYPE (type),
11160 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11161 else
11162 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11163 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11164
11165 if (r != error_mark_node)
11166 /* Will this ever be needed for TYPE_..._TO values? */
11167 layout_type (r);
11168
11169 return r;
11170 }
11171 case OFFSET_TYPE:
11172 {
11173 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11174 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11175 {
11176 /* [temp.deduct]
11177
11178 Type deduction may fail for any of the following
11179 reasons:
11180
11181 -- Attempting to create "pointer to member of T" when T
11182 is not a class type. */
11183 if (complain & tf_error)
11184 error ("creating pointer to member of non-class type %qT", r);
11185 return error_mark_node;
11186 }
11187 if (TREE_CODE (type) == REFERENCE_TYPE)
11188 {
11189 if (complain & tf_error)
11190 error ("creating pointer to member reference type %qT", type);
11191 return error_mark_node;
11192 }
11193 if (TREE_CODE (type) == VOID_TYPE)
11194 {
11195 if (complain & tf_error)
11196 error ("creating pointer to member of type void");
11197 return error_mark_node;
11198 }
11199 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11200 if (TREE_CODE (type) == FUNCTION_TYPE)
11201 {
11202 /* The type of the implicit object parameter gets its
11203 cv-qualifiers from the FUNCTION_TYPE. */
11204 tree memptr;
11205 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
11206 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11207 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11208 complain);
11209 }
11210 else
11211 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11212 cp_type_quals (t),
11213 complain);
11214 }
11215 case FUNCTION_TYPE:
11216 case METHOD_TYPE:
11217 {
11218 tree fntype;
11219 tree specs;
11220 fntype = tsubst_function_type (t, args, complain, in_decl);
11221 if (fntype == error_mark_node)
11222 return error_mark_node;
11223
11224 /* Substitute the exception specification. */
11225 specs = tsubst_exception_specification (t, args, complain,
11226 in_decl, /*defer_ok*/true);
11227 if (specs == error_mark_node)
11228 return error_mark_node;
11229 if (specs)
11230 fntype = build_exception_variant (fntype, specs);
11231 return fntype;
11232 }
11233 case ARRAY_TYPE:
11234 {
11235 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11236 if (domain == error_mark_node)
11237 return error_mark_node;
11238
11239 /* As an optimization, we avoid regenerating the array type if
11240 it will obviously be the same as T. */
11241 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11242 return t;
11243
11244 /* These checks should match the ones in grokdeclarator.
11245
11246 [temp.deduct]
11247
11248 The deduction may fail for any of the following reasons:
11249
11250 -- Attempting to create an array with an element type that
11251 is void, a function type, or a reference type, or [DR337]
11252 an abstract class type. */
11253 if (TREE_CODE (type) == VOID_TYPE
11254 || TREE_CODE (type) == FUNCTION_TYPE
11255 || TREE_CODE (type) == REFERENCE_TYPE)
11256 {
11257 if (complain & tf_error)
11258 error ("creating array of %qT", type);
11259 return error_mark_node;
11260 }
11261 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
11262 {
11263 if (complain & tf_error)
11264 error ("creating array of %qT, which is an abstract class type",
11265 type);
11266 return error_mark_node;
11267 }
11268
11269 r = build_cplus_array_type (type, domain);
11270
11271 if (TYPE_USER_ALIGN (t))
11272 {
11273 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11274 TYPE_USER_ALIGN (r) = 1;
11275 }
11276
11277 return r;
11278 }
11279
11280 case TYPENAME_TYPE:
11281 {
11282 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11283 in_decl, /*entering_scope=*/1);
11284 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11285 complain, in_decl);
11286
11287 if (ctx == error_mark_node || f == error_mark_node)
11288 return error_mark_node;
11289
11290 if (!MAYBE_CLASS_TYPE_P (ctx))
11291 {
11292 if (complain & tf_error)
11293 error ("%qT is not a class, struct, or union type", ctx);
11294 return error_mark_node;
11295 }
11296 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11297 {
11298 /* Normally, make_typename_type does not require that the CTX
11299 have complete type in order to allow things like:
11300
11301 template <class T> struct S { typename S<T>::X Y; };
11302
11303 But, such constructs have already been resolved by this
11304 point, so here CTX really should have complete type, unless
11305 it's a partial instantiation. */
11306 ctx = complete_type (ctx);
11307 if (!COMPLETE_TYPE_P (ctx))
11308 {
11309 if (complain & tf_error)
11310 cxx_incomplete_type_error (NULL_TREE, ctx);
11311 return error_mark_node;
11312 }
11313 }
11314
11315 f = make_typename_type (ctx, f, typename_type,
11316 (complain & tf_error) | tf_keep_type_decl);
11317 if (f == error_mark_node)
11318 return f;
11319 if (TREE_CODE (f) == TYPE_DECL)
11320 {
11321 complain |= tf_ignore_bad_quals;
11322 f = TREE_TYPE (f);
11323 }
11324
11325 if (TREE_CODE (f) != TYPENAME_TYPE)
11326 {
11327 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11328 {
11329 if (complain & tf_error)
11330 error ("%qT resolves to %qT, which is not an enumeration type",
11331 t, f);
11332 else
11333 return error_mark_node;
11334 }
11335 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11336 {
11337 if (complain & tf_error)
11338 error ("%qT resolves to %qT, which is is not a class type",
11339 t, f);
11340 else
11341 return error_mark_node;
11342 }
11343 }
11344
11345 return cp_build_qualified_type_real
11346 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11347 }
11348
11349 case UNBOUND_CLASS_TEMPLATE:
11350 {
11351 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11352 in_decl, /*entering_scope=*/1);
11353 tree name = TYPE_IDENTIFIER (t);
11354 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
11355
11356 if (ctx == error_mark_node || name == error_mark_node)
11357 return error_mark_node;
11358
11359 if (parm_list)
11360 parm_list = tsubst_template_parms (parm_list, args, complain);
11361 return make_unbound_class_template (ctx, name, parm_list, complain);
11362 }
11363
11364 case TYPEOF_TYPE:
11365 {
11366 tree type;
11367
11368 ++cp_unevaluated_operand;
11369 ++c_inhibit_evaluation_warnings;
11370
11371 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
11372 complain, in_decl,
11373 /*integral_constant_expression_p=*/false);
11374
11375 --cp_unevaluated_operand;
11376 --c_inhibit_evaluation_warnings;
11377
11378 type = finish_typeof (type);
11379 return cp_build_qualified_type_real (type,
11380 cp_type_quals (t)
11381 | cp_type_quals (type),
11382 complain);
11383 }
11384
11385 case DECLTYPE_TYPE:
11386 {
11387 tree type;
11388
11389 ++cp_unevaluated_operand;
11390 ++c_inhibit_evaluation_warnings;
11391
11392 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
11393 complain, in_decl,
11394 /*integral_constant_expression_p=*/false);
11395
11396 --cp_unevaluated_operand;
11397 --c_inhibit_evaluation_warnings;
11398
11399 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
11400 type = lambda_capture_field_type (type);
11401 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
11402 type = lambda_return_type (type);
11403 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
11404 type = lambda_proxy_type (type);
11405 else
11406 type = finish_decltype_type
11407 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t), complain);
11408 return cp_build_qualified_type_real (type,
11409 cp_type_quals (t)
11410 | cp_type_quals (type),
11411 complain);
11412 }
11413
11414 case UNDERLYING_TYPE:
11415 {
11416 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
11417 complain, in_decl);
11418 return finish_underlying_type (type);
11419 }
11420
11421 case TYPE_ARGUMENT_PACK:
11422 case NONTYPE_ARGUMENT_PACK:
11423 {
11424 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
11425 tree packed_out =
11426 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
11427 args,
11428 complain,
11429 in_decl);
11430 SET_ARGUMENT_PACK_ARGS (r, packed_out);
11431
11432 /* For template nontype argument packs, also substitute into
11433 the type. */
11434 if (code == NONTYPE_ARGUMENT_PACK)
11435 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
11436
11437 return r;
11438 }
11439 break;
11440
11441 case INTEGER_CST:
11442 case REAL_CST:
11443 case STRING_CST:
11444 case PLUS_EXPR:
11445 case MINUS_EXPR:
11446 case NEGATE_EXPR:
11447 case NOP_EXPR:
11448 case INDIRECT_REF:
11449 case ADDR_EXPR:
11450 case CALL_EXPR:
11451 case ARRAY_REF:
11452 case SCOPE_REF:
11453 /* We should use one of the expression tsubsts for these codes. */
11454 gcc_unreachable ();
11455
11456 default:
11457 sorry ("use of %qs in template", tree_code_name [(int) code]);
11458 return error_mark_node;
11459 }
11460 }
11461
11462 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11463 type of the expression on the left-hand side of the "." or "->"
11464 operator. */
11465
11466 static tree
11467 tsubst_baselink (tree baselink, tree object_type,
11468 tree args, tsubst_flags_t complain, tree in_decl)
11469 {
11470 tree name;
11471 tree qualifying_scope;
11472 tree fns;
11473 tree optype;
11474 tree template_args = 0;
11475 bool template_id_p = false;
11476
11477 /* A baselink indicates a function from a base class. Both the
11478 BASELINK_ACCESS_BINFO and the base class referenced may
11479 indicate bases of the template class, rather than the
11480 instantiated class. In addition, lookups that were not
11481 ambiguous before may be ambiguous now. Therefore, we perform
11482 the lookup again. */
11483 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
11484 qualifying_scope = tsubst (qualifying_scope, args,
11485 complain, in_decl);
11486 fns = BASELINK_FUNCTIONS (baselink);
11487 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
11488 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11489 {
11490 template_id_p = true;
11491 template_args = TREE_OPERAND (fns, 1);
11492 fns = TREE_OPERAND (fns, 0);
11493 if (template_args)
11494 template_args = tsubst_template_args (template_args, args,
11495 complain, in_decl);
11496 }
11497 name = DECL_NAME (get_first_fn (fns));
11498 if (IDENTIFIER_TYPENAME_P (name))
11499 name = mangle_conv_op_name_for_type (optype);
11500 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
11501 if (!baselink)
11502 return error_mark_node;
11503
11504 /* If lookup found a single function, mark it as used at this
11505 point. (If it lookup found multiple functions the one selected
11506 later by overload resolution will be marked as used at that
11507 point.) */
11508 if (BASELINK_P (baselink))
11509 fns = BASELINK_FUNCTIONS (baselink);
11510 if (!template_id_p && !really_overloaded_fn (fns))
11511 mark_used (OVL_CURRENT (fns));
11512
11513 /* Add back the template arguments, if present. */
11514 if (BASELINK_P (baselink) && template_id_p)
11515 BASELINK_FUNCTIONS (baselink)
11516 = build_nt (TEMPLATE_ID_EXPR,
11517 BASELINK_FUNCTIONS (baselink),
11518 template_args);
11519 /* Update the conversion operator type. */
11520 BASELINK_OPTYPE (baselink) = optype;
11521
11522 if (!object_type)
11523 object_type = current_class_type;
11524 return adjust_result_of_qualified_name_lookup (baselink,
11525 qualifying_scope,
11526 object_type);
11527 }
11528
11529 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11530 true if the qualified-id will be a postfix-expression in-and-of
11531 itself; false if more of the postfix-expression follows the
11532 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11533 of "&". */
11534
11535 static tree
11536 tsubst_qualified_id (tree qualified_id, tree args,
11537 tsubst_flags_t complain, tree in_decl,
11538 bool done, bool address_p)
11539 {
11540 tree expr;
11541 tree scope;
11542 tree name;
11543 bool is_template;
11544 tree template_args;
11545
11546 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
11547
11548 /* Figure out what name to look up. */
11549 name = TREE_OPERAND (qualified_id, 1);
11550 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11551 {
11552 is_template = true;
11553 template_args = TREE_OPERAND (name, 1);
11554 if (template_args)
11555 template_args = tsubst_template_args (template_args, args,
11556 complain, in_decl);
11557 name = TREE_OPERAND (name, 0);
11558 }
11559 else
11560 {
11561 is_template = false;
11562 template_args = NULL_TREE;
11563 }
11564
11565 /* Substitute into the qualifying scope. When there are no ARGS, we
11566 are just trying to simplify a non-dependent expression. In that
11567 case the qualifying scope may be dependent, and, in any case,
11568 substituting will not help. */
11569 scope = TREE_OPERAND (qualified_id, 0);
11570 if (args)
11571 {
11572 scope = tsubst (scope, args, complain, in_decl);
11573 expr = tsubst_copy (name, args, complain, in_decl);
11574 }
11575 else
11576 expr = name;
11577
11578 if (dependent_scope_p (scope))
11579 {
11580 if (is_template)
11581 expr = build_min_nt (TEMPLATE_ID_EXPR, expr, template_args);
11582 return build_qualified_name (NULL_TREE, scope, expr,
11583 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
11584 }
11585
11586 if (!BASELINK_P (name) && !DECL_P (expr))
11587 {
11588 if (TREE_CODE (expr) == BIT_NOT_EXPR)
11589 {
11590 /* A BIT_NOT_EXPR is used to represent a destructor. */
11591 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
11592 {
11593 error ("qualifying type %qT does not match destructor name ~%qT",
11594 scope, TREE_OPERAND (expr, 0));
11595 expr = error_mark_node;
11596 }
11597 else
11598 expr = lookup_qualified_name (scope, complete_dtor_identifier,
11599 /*is_type_p=*/0, false);
11600 }
11601 else
11602 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
11603 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
11604 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
11605 {
11606 if (complain & tf_error)
11607 {
11608 error ("dependent-name %qE is parsed as a non-type, but "
11609 "instantiation yields a type", qualified_id);
11610 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
11611 }
11612 return error_mark_node;
11613 }
11614 }
11615
11616 if (DECL_P (expr))
11617 {
11618 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
11619 scope);
11620 /* Remember that there was a reference to this entity. */
11621 mark_used (expr);
11622 }
11623
11624 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
11625 {
11626 if (complain & tf_error)
11627 qualified_name_lookup_error (scope,
11628 TREE_OPERAND (qualified_id, 1),
11629 expr, input_location);
11630 return error_mark_node;
11631 }
11632
11633 if (is_template)
11634 expr = lookup_template_function (expr, template_args);
11635
11636 if (expr == error_mark_node && complain & tf_error)
11637 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
11638 expr, input_location);
11639 else if (TYPE_P (scope))
11640 {
11641 expr = (adjust_result_of_qualified_name_lookup
11642 (expr, scope, current_class_type));
11643 expr = (finish_qualified_id_expr
11644 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
11645 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
11646 /*template_arg_p=*/false));
11647 }
11648
11649 /* Expressions do not generally have reference type. */
11650 if (TREE_CODE (expr) != SCOPE_REF
11651 /* However, if we're about to form a pointer-to-member, we just
11652 want the referenced member referenced. */
11653 && TREE_CODE (expr) != OFFSET_REF)
11654 expr = convert_from_reference (expr);
11655
11656 return expr;
11657 }
11658
11659 /* Like tsubst, but deals with expressions. This function just replaces
11660 template parms; to finish processing the resultant expression, use
11661 tsubst_expr. */
11662
11663 static tree
11664 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11665 {
11666 enum tree_code code;
11667 tree r;
11668
11669 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
11670 return t;
11671
11672 code = TREE_CODE (t);
11673
11674 switch (code)
11675 {
11676 case PARM_DECL:
11677 r = retrieve_local_specialization (t);
11678
11679 if (r == NULL)
11680 {
11681 tree c;
11682 /* This can happen for a parameter name used later in a function
11683 declaration (such as in a late-specified return type). Just
11684 make a dummy decl, since it's only used for its type. */
11685 gcc_assert (cp_unevaluated_operand != 0);
11686 /* We copy T because want to tsubst the PARM_DECL only,
11687 not the following PARM_DECLs that are chained to T. */
11688 c = copy_node (t);
11689 r = tsubst_decl (c, args, complain);
11690 /* Give it the template pattern as its context; its true context
11691 hasn't been instantiated yet and this is good enough for
11692 mangling. */
11693 DECL_CONTEXT (r) = DECL_CONTEXT (t);
11694 }
11695
11696 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
11697 r = ARGUMENT_PACK_SELECT_ARG (r);
11698 mark_used (r);
11699 return r;
11700
11701 case CONST_DECL:
11702 {
11703 tree enum_type;
11704 tree v;
11705
11706 if (DECL_TEMPLATE_PARM_P (t))
11707 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
11708 /* There is no need to substitute into namespace-scope
11709 enumerators. */
11710 if (DECL_NAMESPACE_SCOPE_P (t))
11711 return t;
11712 /* If ARGS is NULL, then T is known to be non-dependent. */
11713 if (args == NULL_TREE)
11714 return integral_constant_value (t);
11715
11716 /* Unfortunately, we cannot just call lookup_name here.
11717 Consider:
11718
11719 template <int I> int f() {
11720 enum E { a = I };
11721 struct S { void g() { E e = a; } };
11722 };
11723
11724 When we instantiate f<7>::S::g(), say, lookup_name is not
11725 clever enough to find f<7>::a. */
11726 enum_type
11727 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
11728 /*entering_scope=*/0);
11729
11730 for (v = TYPE_VALUES (enum_type);
11731 v != NULL_TREE;
11732 v = TREE_CHAIN (v))
11733 if (TREE_PURPOSE (v) == DECL_NAME (t))
11734 return TREE_VALUE (v);
11735
11736 /* We didn't find the name. That should never happen; if
11737 name-lookup found it during preliminary parsing, we
11738 should find it again here during instantiation. */
11739 gcc_unreachable ();
11740 }
11741 return t;
11742
11743 case FIELD_DECL:
11744 if (DECL_CONTEXT (t))
11745 {
11746 tree ctx;
11747
11748 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
11749 /*entering_scope=*/1);
11750 if (ctx != DECL_CONTEXT (t))
11751 {
11752 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
11753 if (!r)
11754 {
11755 if (complain & tf_error)
11756 error ("using invalid field %qD", t);
11757 return error_mark_node;
11758 }
11759 return r;
11760 }
11761 }
11762
11763 return t;
11764
11765 case VAR_DECL:
11766 case FUNCTION_DECL:
11767 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
11768 || local_variable_p (t))
11769 t = tsubst (t, args, complain, in_decl);
11770 mark_used (t);
11771 return t;
11772
11773 case OVERLOAD:
11774 /* An OVERLOAD will always be a non-dependent overload set; an
11775 overload set from function scope will just be represented with an
11776 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11777 gcc_assert (!uses_template_parms (t));
11778 return t;
11779
11780 case BASELINK:
11781 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
11782
11783 case TEMPLATE_DECL:
11784 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11785 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
11786 args, complain, in_decl);
11787 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
11788 return tsubst (t, args, complain, in_decl);
11789 else if (DECL_CLASS_SCOPE_P (t)
11790 && uses_template_parms (DECL_CONTEXT (t)))
11791 {
11792 /* Template template argument like the following example need
11793 special treatment:
11794
11795 template <template <class> class TT> struct C {};
11796 template <class T> struct D {
11797 template <class U> struct E {};
11798 C<E> c; // #1
11799 };
11800 D<int> d; // #2
11801
11802 We are processing the template argument `E' in #1 for
11803 the template instantiation #2. Originally, `E' is a
11804 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11805 have to substitute this with one having context `D<int>'. */
11806
11807 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
11808 return lookup_field (context, DECL_NAME(t), 0, false);
11809 }
11810 else
11811 /* Ordinary template template argument. */
11812 return t;
11813
11814 case CAST_EXPR:
11815 case REINTERPRET_CAST_EXPR:
11816 case CONST_CAST_EXPR:
11817 case STATIC_CAST_EXPR:
11818 case DYNAMIC_CAST_EXPR:
11819 case NOP_EXPR:
11820 return build1
11821 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11822 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11823
11824 case SIZEOF_EXPR:
11825 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11826 {
11827
11828 tree expanded;
11829 int len = 0;
11830
11831 ++cp_unevaluated_operand;
11832 ++c_inhibit_evaluation_warnings;
11833 /* We only want to compute the number of arguments. */
11834 expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
11835 complain, in_decl);
11836 --cp_unevaluated_operand;
11837 --c_inhibit_evaluation_warnings;
11838
11839 if (TREE_CODE (expanded) == TREE_VEC)
11840 len = TREE_VEC_LENGTH (expanded);
11841
11842 if (expanded == error_mark_node)
11843 return error_mark_node;
11844 else if (PACK_EXPANSION_P (expanded)
11845 || (TREE_CODE (expanded) == TREE_VEC
11846 && len > 0
11847 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
11848 {
11849 if (TREE_CODE (expanded) == TREE_VEC)
11850 expanded = TREE_VEC_ELT (expanded, len - 1);
11851
11852 if (TYPE_P (expanded))
11853 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
11854 complain & tf_error);
11855 else
11856 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
11857 complain & tf_error);
11858 }
11859 else
11860 return build_int_cst (size_type_node, len);
11861 }
11862 /* Fall through */
11863
11864 case INDIRECT_REF:
11865 case NEGATE_EXPR:
11866 case TRUTH_NOT_EXPR:
11867 case BIT_NOT_EXPR:
11868 case ADDR_EXPR:
11869 case UNARY_PLUS_EXPR: /* Unary + */
11870 case ALIGNOF_EXPR:
11871 case AT_ENCODE_EXPR:
11872 case ARROW_EXPR:
11873 case THROW_EXPR:
11874 case TYPEID_EXPR:
11875 case REALPART_EXPR:
11876 case IMAGPART_EXPR:
11877 return build1
11878 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11879 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11880
11881 case COMPONENT_REF:
11882 {
11883 tree object;
11884 tree name;
11885
11886 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
11887 name = TREE_OPERAND (t, 1);
11888 if (TREE_CODE (name) == BIT_NOT_EXPR)
11889 {
11890 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11891 complain, in_decl);
11892 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11893 }
11894 else if (TREE_CODE (name) == SCOPE_REF
11895 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
11896 {
11897 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
11898 complain, in_decl);
11899 name = TREE_OPERAND (name, 1);
11900 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11901 complain, in_decl);
11902 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11903 name = build_qualified_name (/*type=*/NULL_TREE,
11904 base, name,
11905 /*template_p=*/false);
11906 }
11907 else if (TREE_CODE (name) == BASELINK)
11908 name = tsubst_baselink (name,
11909 non_reference (TREE_TYPE (object)),
11910 args, complain,
11911 in_decl);
11912 else
11913 name = tsubst_copy (name, args, complain, in_decl);
11914 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
11915 }
11916
11917 case PLUS_EXPR:
11918 case MINUS_EXPR:
11919 case MULT_EXPR:
11920 case TRUNC_DIV_EXPR:
11921 case CEIL_DIV_EXPR:
11922 case FLOOR_DIV_EXPR:
11923 case ROUND_DIV_EXPR:
11924 case EXACT_DIV_EXPR:
11925 case BIT_AND_EXPR:
11926 case BIT_IOR_EXPR:
11927 case BIT_XOR_EXPR:
11928 case TRUNC_MOD_EXPR:
11929 case FLOOR_MOD_EXPR:
11930 case TRUTH_ANDIF_EXPR:
11931 case TRUTH_ORIF_EXPR:
11932 case TRUTH_AND_EXPR:
11933 case TRUTH_OR_EXPR:
11934 case RSHIFT_EXPR:
11935 case LSHIFT_EXPR:
11936 case RROTATE_EXPR:
11937 case LROTATE_EXPR:
11938 case EQ_EXPR:
11939 case NE_EXPR:
11940 case MAX_EXPR:
11941 case MIN_EXPR:
11942 case LE_EXPR:
11943 case GE_EXPR:
11944 case LT_EXPR:
11945 case GT_EXPR:
11946 case COMPOUND_EXPR:
11947 case DOTSTAR_EXPR:
11948 case MEMBER_REF:
11949 case PREDECREMENT_EXPR:
11950 case PREINCREMENT_EXPR:
11951 case POSTDECREMENT_EXPR:
11952 case POSTINCREMENT_EXPR:
11953 return build_nt
11954 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11955 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11956
11957 case SCOPE_REF:
11958 return build_qualified_name (/*type=*/NULL_TREE,
11959 tsubst_copy (TREE_OPERAND (t, 0),
11960 args, complain, in_decl),
11961 tsubst_copy (TREE_OPERAND (t, 1),
11962 args, complain, in_decl),
11963 QUALIFIED_NAME_IS_TEMPLATE (t));
11964
11965 case ARRAY_REF:
11966 return build_nt
11967 (ARRAY_REF,
11968 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11969 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11970 NULL_TREE, NULL_TREE);
11971
11972 case CALL_EXPR:
11973 {
11974 int n = VL_EXP_OPERAND_LENGTH (t);
11975 tree result = build_vl_exp (CALL_EXPR, n);
11976 int i;
11977 for (i = 0; i < n; i++)
11978 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
11979 complain, in_decl);
11980 return result;
11981 }
11982
11983 case COND_EXPR:
11984 case MODOP_EXPR:
11985 case PSEUDO_DTOR_EXPR:
11986 {
11987 r = build_nt
11988 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11989 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11990 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11991 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11992 return r;
11993 }
11994
11995 case NEW_EXPR:
11996 {
11997 r = build_nt
11998 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11999 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12000 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12001 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12002 return r;
12003 }
12004
12005 case DELETE_EXPR:
12006 {
12007 r = build_nt
12008 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12009 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12010 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12011 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12012 return r;
12013 }
12014
12015 case TEMPLATE_ID_EXPR:
12016 {
12017 /* Substituted template arguments */
12018 tree fn = TREE_OPERAND (t, 0);
12019 tree targs = TREE_OPERAND (t, 1);
12020
12021 fn = tsubst_copy (fn, args, complain, in_decl);
12022 if (targs)
12023 targs = tsubst_template_args (targs, args, complain, in_decl);
12024
12025 return lookup_template_function (fn, targs);
12026 }
12027
12028 case TREE_LIST:
12029 {
12030 tree purpose, value, chain;
12031
12032 if (t == void_list_node)
12033 return t;
12034
12035 purpose = TREE_PURPOSE (t);
12036 if (purpose)
12037 purpose = tsubst_copy (purpose, args, complain, in_decl);
12038 value = TREE_VALUE (t);
12039 if (value)
12040 value = tsubst_copy (value, args, complain, in_decl);
12041 chain = TREE_CHAIN (t);
12042 if (chain && chain != void_type_node)
12043 chain = tsubst_copy (chain, args, complain, in_decl);
12044 if (purpose == TREE_PURPOSE (t)
12045 && value == TREE_VALUE (t)
12046 && chain == TREE_CHAIN (t))
12047 return t;
12048 return tree_cons (purpose, value, chain);
12049 }
12050
12051 case RECORD_TYPE:
12052 case UNION_TYPE:
12053 case ENUMERAL_TYPE:
12054 case INTEGER_TYPE:
12055 case TEMPLATE_TYPE_PARM:
12056 case TEMPLATE_TEMPLATE_PARM:
12057 case BOUND_TEMPLATE_TEMPLATE_PARM:
12058 case TEMPLATE_PARM_INDEX:
12059 case POINTER_TYPE:
12060 case REFERENCE_TYPE:
12061 case OFFSET_TYPE:
12062 case FUNCTION_TYPE:
12063 case METHOD_TYPE:
12064 case ARRAY_TYPE:
12065 case TYPENAME_TYPE:
12066 case UNBOUND_CLASS_TEMPLATE:
12067 case TYPEOF_TYPE:
12068 case DECLTYPE_TYPE:
12069 case TYPE_DECL:
12070 return tsubst (t, args, complain, in_decl);
12071
12072 case IDENTIFIER_NODE:
12073 if (IDENTIFIER_TYPENAME_P (t))
12074 {
12075 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12076 return mangle_conv_op_name_for_type (new_type);
12077 }
12078 else
12079 return t;
12080
12081 case CONSTRUCTOR:
12082 /* This is handled by tsubst_copy_and_build. */
12083 gcc_unreachable ();
12084
12085 case VA_ARG_EXPR:
12086 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12087 in_decl),
12088 tsubst (TREE_TYPE (t), args, complain, in_decl));
12089
12090 case CLEANUP_POINT_EXPR:
12091 /* We shouldn't have built any of these during initial template
12092 generation. Instead, they should be built during instantiation
12093 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12094 gcc_unreachable ();
12095
12096 case OFFSET_REF:
12097 r = build2
12098 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12099 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12100 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12101 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12102 mark_used (TREE_OPERAND (r, 1));
12103 return r;
12104
12105 case EXPR_PACK_EXPANSION:
12106 error ("invalid use of pack expansion expression");
12107 return error_mark_node;
12108
12109 case NONTYPE_ARGUMENT_PACK:
12110 error ("use %<...%> to expand argument pack");
12111 return error_mark_node;
12112
12113 case INTEGER_CST:
12114 case REAL_CST:
12115 case STRING_CST:
12116 case COMPLEX_CST:
12117 {
12118 /* Instantiate any typedefs in the type. */
12119 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12120 r = fold_convert (type, t);
12121 gcc_assert (TREE_CODE (r) == code);
12122 return r;
12123 }
12124
12125 case PTRMEM_CST:
12126 /* These can sometimes show up in a partial instantiation, but never
12127 involve template parms. */
12128 gcc_assert (!uses_template_parms (t));
12129 return t;
12130
12131 default:
12132 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12133 gcc_checking_assert (false);
12134 return t;
12135 }
12136 }
12137
12138 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12139
12140 static tree
12141 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
12142 tree in_decl)
12143 {
12144 tree new_clauses = NULL, nc, oc;
12145
12146 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12147 {
12148 nc = copy_node (oc);
12149 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12150 new_clauses = nc;
12151
12152 switch (OMP_CLAUSE_CODE (nc))
12153 {
12154 case OMP_CLAUSE_LASTPRIVATE:
12155 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12156 {
12157 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12158 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12159 in_decl, /*integral_constant_expression_p=*/false);
12160 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12161 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12162 }
12163 /* FALLTHRU */
12164 case OMP_CLAUSE_PRIVATE:
12165 case OMP_CLAUSE_SHARED:
12166 case OMP_CLAUSE_FIRSTPRIVATE:
12167 case OMP_CLAUSE_REDUCTION:
12168 case OMP_CLAUSE_COPYIN:
12169 case OMP_CLAUSE_COPYPRIVATE:
12170 case OMP_CLAUSE_IF:
12171 case OMP_CLAUSE_NUM_THREADS:
12172 case OMP_CLAUSE_SCHEDULE:
12173 case OMP_CLAUSE_COLLAPSE:
12174 OMP_CLAUSE_OPERAND (nc, 0)
12175 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12176 in_decl, /*integral_constant_expression_p=*/false);
12177 break;
12178 case OMP_CLAUSE_NOWAIT:
12179 case OMP_CLAUSE_ORDERED:
12180 case OMP_CLAUSE_DEFAULT:
12181 case OMP_CLAUSE_UNTIED:
12182 break;
12183 default:
12184 gcc_unreachable ();
12185 }
12186 }
12187
12188 return finish_omp_clauses (nreverse (new_clauses));
12189 }
12190
12191 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12192
12193 static tree
12194 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12195 tree in_decl)
12196 {
12197 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12198
12199 tree purpose, value, chain;
12200
12201 if (t == NULL)
12202 return t;
12203
12204 if (TREE_CODE (t) != TREE_LIST)
12205 return tsubst_copy_and_build (t, args, complain, in_decl,
12206 /*function_p=*/false,
12207 /*integral_constant_expression_p=*/false);
12208
12209 if (t == void_list_node)
12210 return t;
12211
12212 purpose = TREE_PURPOSE (t);
12213 if (purpose)
12214 purpose = RECUR (purpose);
12215 value = TREE_VALUE (t);
12216 if (value && TREE_CODE (value) != LABEL_DECL)
12217 value = RECUR (value);
12218 chain = TREE_CHAIN (t);
12219 if (chain && chain != void_type_node)
12220 chain = RECUR (chain);
12221 return tree_cons (purpose, value, chain);
12222 #undef RECUR
12223 }
12224
12225 /* Substitute one OMP_FOR iterator. */
12226
12227 static void
12228 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
12229 tree condv, tree incrv, tree *clauses,
12230 tree args, tsubst_flags_t complain, tree in_decl,
12231 bool integral_constant_expression_p)
12232 {
12233 #define RECUR(NODE) \
12234 tsubst_expr ((NODE), args, complain, in_decl, \
12235 integral_constant_expression_p)
12236 tree decl, init, cond, incr, auto_node;
12237
12238 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
12239 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
12240 decl = RECUR (TREE_OPERAND (init, 0));
12241 init = TREE_OPERAND (init, 1);
12242 auto_node = type_uses_auto (TREE_TYPE (decl));
12243 if (auto_node && init)
12244 {
12245 tree init_expr = init;
12246 if (TREE_CODE (init_expr) == DECL_EXPR)
12247 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
12248 init_expr = RECUR (init_expr);
12249 TREE_TYPE (decl)
12250 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
12251 }
12252 gcc_assert (!type_dependent_expression_p (decl));
12253
12254 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
12255 {
12256 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
12257 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12258 if (TREE_CODE (incr) == MODIFY_EXPR)
12259 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
12260 RECUR (TREE_OPERAND (incr, 1)),
12261 complain);
12262 else
12263 incr = RECUR (incr);
12264 TREE_VEC_ELT (declv, i) = decl;
12265 TREE_VEC_ELT (initv, i) = init;
12266 TREE_VEC_ELT (condv, i) = cond;
12267 TREE_VEC_ELT (incrv, i) = incr;
12268 return;
12269 }
12270
12271 if (init && TREE_CODE (init) != DECL_EXPR)
12272 {
12273 tree c;
12274 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
12275 {
12276 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12277 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
12278 && OMP_CLAUSE_DECL (c) == decl)
12279 break;
12280 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12281 && OMP_CLAUSE_DECL (c) == decl)
12282 error ("iteration variable %qD should not be firstprivate", decl);
12283 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12284 && OMP_CLAUSE_DECL (c) == decl)
12285 error ("iteration variable %qD should not be reduction", decl);
12286 }
12287 if (c == NULL)
12288 {
12289 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
12290 OMP_CLAUSE_DECL (c) = decl;
12291 c = finish_omp_clauses (c);
12292 if (c)
12293 {
12294 OMP_CLAUSE_CHAIN (c) = *clauses;
12295 *clauses = c;
12296 }
12297 }
12298 }
12299 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
12300 if (COMPARISON_CLASS_P (cond))
12301 cond = build2 (TREE_CODE (cond), boolean_type_node,
12302 RECUR (TREE_OPERAND (cond, 0)),
12303 RECUR (TREE_OPERAND (cond, 1)));
12304 else
12305 cond = RECUR (cond);
12306 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12307 switch (TREE_CODE (incr))
12308 {
12309 case PREINCREMENT_EXPR:
12310 case PREDECREMENT_EXPR:
12311 case POSTINCREMENT_EXPR:
12312 case POSTDECREMENT_EXPR:
12313 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
12314 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
12315 break;
12316 case MODIFY_EXPR:
12317 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12318 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12319 {
12320 tree rhs = TREE_OPERAND (incr, 1);
12321 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12322 RECUR (TREE_OPERAND (incr, 0)),
12323 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12324 RECUR (TREE_OPERAND (rhs, 0)),
12325 RECUR (TREE_OPERAND (rhs, 1))));
12326 }
12327 else
12328 incr = RECUR (incr);
12329 break;
12330 case MODOP_EXPR:
12331 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12332 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12333 {
12334 tree lhs = RECUR (TREE_OPERAND (incr, 0));
12335 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
12336 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
12337 TREE_TYPE (decl), lhs,
12338 RECUR (TREE_OPERAND (incr, 2))));
12339 }
12340 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
12341 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
12342 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
12343 {
12344 tree rhs = TREE_OPERAND (incr, 2);
12345 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12346 RECUR (TREE_OPERAND (incr, 0)),
12347 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12348 RECUR (TREE_OPERAND (rhs, 0)),
12349 RECUR (TREE_OPERAND (rhs, 1))));
12350 }
12351 else
12352 incr = RECUR (incr);
12353 break;
12354 default:
12355 incr = RECUR (incr);
12356 break;
12357 }
12358
12359 TREE_VEC_ELT (declv, i) = decl;
12360 TREE_VEC_ELT (initv, i) = init;
12361 TREE_VEC_ELT (condv, i) = cond;
12362 TREE_VEC_ELT (incrv, i) = incr;
12363 #undef RECUR
12364 }
12365
12366 /* Like tsubst_copy for expressions, etc. but also does semantic
12367 processing. */
12368
12369 static tree
12370 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
12371 bool integral_constant_expression_p)
12372 {
12373 #define RECUR(NODE) \
12374 tsubst_expr ((NODE), args, complain, in_decl, \
12375 integral_constant_expression_p)
12376
12377 tree stmt, tmp;
12378
12379 if (t == NULL_TREE || t == error_mark_node)
12380 return t;
12381
12382 if (EXPR_HAS_LOCATION (t))
12383 input_location = EXPR_LOCATION (t);
12384 if (STATEMENT_CODE_P (TREE_CODE (t)))
12385 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
12386
12387 switch (TREE_CODE (t))
12388 {
12389 case STATEMENT_LIST:
12390 {
12391 tree_stmt_iterator i;
12392 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
12393 RECUR (tsi_stmt (i));
12394 break;
12395 }
12396
12397 case CTOR_INITIALIZER:
12398 finish_mem_initializers (tsubst_initializer_list
12399 (TREE_OPERAND (t, 0), args));
12400 break;
12401
12402 case RETURN_EXPR:
12403 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
12404 break;
12405
12406 case EXPR_STMT:
12407 tmp = RECUR (EXPR_STMT_EXPR (t));
12408 if (EXPR_STMT_STMT_EXPR_RESULT (t))
12409 finish_stmt_expr_expr (tmp, cur_stmt_expr);
12410 else
12411 finish_expr_stmt (tmp);
12412 break;
12413
12414 case USING_STMT:
12415 do_using_directive (USING_STMT_NAMESPACE (t));
12416 break;
12417
12418 case DECL_EXPR:
12419 {
12420 tree decl, pattern_decl;
12421 tree init;
12422
12423 pattern_decl = decl = DECL_EXPR_DECL (t);
12424 if (TREE_CODE (decl) == LABEL_DECL)
12425 finish_label_decl (DECL_NAME (decl));
12426 else if (TREE_CODE (decl) == USING_DECL)
12427 {
12428 tree scope = USING_DECL_SCOPE (decl);
12429 tree name = DECL_NAME (decl);
12430 tree decl;
12431
12432 scope = tsubst (scope, args, complain, in_decl);
12433 decl = lookup_qualified_name (scope, name,
12434 /*is_type_p=*/false,
12435 /*complain=*/false);
12436 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
12437 qualified_name_lookup_error (scope, name, decl, input_location);
12438 else
12439 do_local_using_decl (decl, scope, name);
12440 }
12441 else
12442 {
12443 init = DECL_INITIAL (decl);
12444 decl = tsubst (decl, args, complain, in_decl);
12445 if (decl != error_mark_node)
12446 {
12447 /* By marking the declaration as instantiated, we avoid
12448 trying to instantiate it. Since instantiate_decl can't
12449 handle local variables, and since we've already done
12450 all that needs to be done, that's the right thing to
12451 do. */
12452 if (TREE_CODE (decl) == VAR_DECL)
12453 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12454 if (TREE_CODE (decl) == VAR_DECL
12455 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
12456 /* Anonymous aggregates are a special case. */
12457 finish_anon_union (decl);
12458 else
12459 {
12460 int const_init = false;
12461 maybe_push_decl (decl);
12462 if (TREE_CODE (decl) == VAR_DECL
12463 && DECL_PRETTY_FUNCTION_P (decl))
12464 {
12465 /* For __PRETTY_FUNCTION__ we have to adjust the
12466 initializer. */
12467 const char *const name
12468 = cxx_printable_name (current_function_decl, 2);
12469 init = cp_fname_init (name, &TREE_TYPE (decl));
12470 }
12471 else
12472 {
12473 tree t = RECUR (init);
12474
12475 if (init && !t)
12476 {
12477 /* If we had an initializer but it
12478 instantiated to nothing,
12479 value-initialize the object. This will
12480 only occur when the initializer was a
12481 pack expansion where the parameter packs
12482 used in that expansion were of length
12483 zero. */
12484 init = build_value_init (TREE_TYPE (decl),
12485 complain);
12486 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12487 init = get_target_expr_sfinae (init, complain);
12488 }
12489 else
12490 init = t;
12491 }
12492
12493 if (TREE_CODE (decl) == VAR_DECL)
12494 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12495 (pattern_decl));
12496 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
12497 }
12498 }
12499 }
12500
12501 /* A DECL_EXPR can also be used as an expression, in the condition
12502 clause of an if/for/while construct. */
12503 return decl;
12504 }
12505
12506 case FOR_STMT:
12507 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12508 RECUR (FOR_INIT_STMT (t));
12509 finish_for_init_stmt (stmt);
12510 tmp = RECUR (FOR_COND (t));
12511 finish_for_cond (tmp, stmt);
12512 tmp = RECUR (FOR_EXPR (t));
12513 finish_for_expr (tmp, stmt);
12514 RECUR (FOR_BODY (t));
12515 finish_for_stmt (stmt);
12516 break;
12517
12518 case RANGE_FOR_STMT:
12519 {
12520 tree decl, expr;
12521 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12522 decl = RANGE_FOR_DECL (t);
12523 decl = tsubst (decl, args, complain, in_decl);
12524 maybe_push_decl (decl);
12525 expr = RECUR (RANGE_FOR_EXPR (t));
12526 stmt = cp_convert_range_for (stmt, decl, expr);
12527 RECUR (RANGE_FOR_BODY (t));
12528 finish_for_stmt (stmt);
12529 }
12530 break;
12531
12532 case WHILE_STMT:
12533 stmt = begin_while_stmt ();
12534 tmp = RECUR (WHILE_COND (t));
12535 finish_while_stmt_cond (tmp, stmt);
12536 RECUR (WHILE_BODY (t));
12537 finish_while_stmt (stmt);
12538 break;
12539
12540 case DO_STMT:
12541 stmt = begin_do_stmt ();
12542 RECUR (DO_BODY (t));
12543 finish_do_body (stmt);
12544 tmp = RECUR (DO_COND (t));
12545 finish_do_stmt (tmp, stmt);
12546 break;
12547
12548 case IF_STMT:
12549 stmt = begin_if_stmt ();
12550 tmp = RECUR (IF_COND (t));
12551 finish_if_stmt_cond (tmp, stmt);
12552 RECUR (THEN_CLAUSE (t));
12553 finish_then_clause (stmt);
12554
12555 if (ELSE_CLAUSE (t))
12556 {
12557 begin_else_clause (stmt);
12558 RECUR (ELSE_CLAUSE (t));
12559 finish_else_clause (stmt);
12560 }
12561
12562 finish_if_stmt (stmt);
12563 break;
12564
12565 case BIND_EXPR:
12566 if (BIND_EXPR_BODY_BLOCK (t))
12567 stmt = begin_function_body ();
12568 else
12569 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
12570 ? BCS_TRY_BLOCK : 0);
12571
12572 RECUR (BIND_EXPR_BODY (t));
12573
12574 if (BIND_EXPR_BODY_BLOCK (t))
12575 finish_function_body (stmt);
12576 else
12577 finish_compound_stmt (stmt);
12578 break;
12579
12580 case BREAK_STMT:
12581 finish_break_stmt ();
12582 break;
12583
12584 case CONTINUE_STMT:
12585 finish_continue_stmt ();
12586 break;
12587
12588 case SWITCH_STMT:
12589 stmt = begin_switch_stmt ();
12590 tmp = RECUR (SWITCH_STMT_COND (t));
12591 finish_switch_cond (tmp, stmt);
12592 RECUR (SWITCH_STMT_BODY (t));
12593 finish_switch_stmt (stmt);
12594 break;
12595
12596 case CASE_LABEL_EXPR:
12597 finish_case_label (EXPR_LOCATION (t),
12598 RECUR (CASE_LOW (t)),
12599 RECUR (CASE_HIGH (t)));
12600 break;
12601
12602 case LABEL_EXPR:
12603 {
12604 tree decl = LABEL_EXPR_LABEL (t);
12605 tree label;
12606
12607 label = finish_label_stmt (DECL_NAME (decl));
12608 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
12609 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
12610 }
12611 break;
12612
12613 case GOTO_EXPR:
12614 tmp = GOTO_DESTINATION (t);
12615 if (TREE_CODE (tmp) != LABEL_DECL)
12616 /* Computed goto's must be tsubst'd into. On the other hand,
12617 non-computed gotos must not be; the identifier in question
12618 will have no binding. */
12619 tmp = RECUR (tmp);
12620 else
12621 tmp = DECL_NAME (tmp);
12622 finish_goto_stmt (tmp);
12623 break;
12624
12625 case ASM_EXPR:
12626 tmp = finish_asm_stmt
12627 (ASM_VOLATILE_P (t),
12628 RECUR (ASM_STRING (t)),
12629 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
12630 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
12631 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
12632 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
12633 {
12634 tree asm_expr = tmp;
12635 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
12636 asm_expr = TREE_OPERAND (asm_expr, 0);
12637 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
12638 }
12639 break;
12640
12641 case TRY_BLOCK:
12642 if (CLEANUP_P (t))
12643 {
12644 stmt = begin_try_block ();
12645 RECUR (TRY_STMTS (t));
12646 finish_cleanup_try_block (stmt);
12647 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
12648 }
12649 else
12650 {
12651 tree compound_stmt = NULL_TREE;
12652
12653 if (FN_TRY_BLOCK_P (t))
12654 stmt = begin_function_try_block (&compound_stmt);
12655 else
12656 stmt = begin_try_block ();
12657
12658 RECUR (TRY_STMTS (t));
12659
12660 if (FN_TRY_BLOCK_P (t))
12661 finish_function_try_block (stmt);
12662 else
12663 finish_try_block (stmt);
12664
12665 RECUR (TRY_HANDLERS (t));
12666 if (FN_TRY_BLOCK_P (t))
12667 finish_function_handler_sequence (stmt, compound_stmt);
12668 else
12669 finish_handler_sequence (stmt);
12670 }
12671 break;
12672
12673 case HANDLER:
12674 {
12675 tree decl = HANDLER_PARMS (t);
12676
12677 if (decl)
12678 {
12679 decl = tsubst (decl, args, complain, in_decl);
12680 /* Prevent instantiate_decl from trying to instantiate
12681 this variable. We've already done all that needs to be
12682 done. */
12683 if (decl != error_mark_node)
12684 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12685 }
12686 stmt = begin_handler ();
12687 finish_handler_parms (decl, stmt);
12688 RECUR (HANDLER_BODY (t));
12689 finish_handler (stmt);
12690 }
12691 break;
12692
12693 case TAG_DEFN:
12694 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12695 break;
12696
12697 case STATIC_ASSERT:
12698 {
12699 tree condition =
12700 tsubst_expr (STATIC_ASSERT_CONDITION (t),
12701 args,
12702 complain, in_decl,
12703 /*integral_constant_expression_p=*/true);
12704 finish_static_assert (condition,
12705 STATIC_ASSERT_MESSAGE (t),
12706 STATIC_ASSERT_SOURCE_LOCATION (t),
12707 /*member_p=*/false);
12708 }
12709 break;
12710
12711 case OMP_PARALLEL:
12712 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
12713 args, complain, in_decl);
12714 stmt = begin_omp_parallel ();
12715 RECUR (OMP_PARALLEL_BODY (t));
12716 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
12717 = OMP_PARALLEL_COMBINED (t);
12718 break;
12719
12720 case OMP_TASK:
12721 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
12722 args, complain, in_decl);
12723 stmt = begin_omp_task ();
12724 RECUR (OMP_TASK_BODY (t));
12725 finish_omp_task (tmp, stmt);
12726 break;
12727
12728 case OMP_FOR:
12729 {
12730 tree clauses, body, pre_body;
12731 tree declv, initv, condv, incrv;
12732 int i;
12733
12734 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
12735 args, complain, in_decl);
12736 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12737 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12738 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12739 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12740
12741 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
12742 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
12743 &clauses, args, complain, in_decl,
12744 integral_constant_expression_p);
12745
12746 stmt = begin_omp_structured_block ();
12747
12748 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
12749 if (TREE_VEC_ELT (initv, i) == NULL
12750 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
12751 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
12752 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
12753 {
12754 tree init = RECUR (TREE_VEC_ELT (initv, i));
12755 gcc_assert (init == TREE_VEC_ELT (declv, i));
12756 TREE_VEC_ELT (initv, i) = NULL_TREE;
12757 }
12758 else
12759 {
12760 tree decl_expr = TREE_VEC_ELT (initv, i);
12761 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
12762 gcc_assert (init != NULL);
12763 TREE_VEC_ELT (initv, i) = RECUR (init);
12764 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
12765 RECUR (decl_expr);
12766 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
12767 }
12768
12769 pre_body = push_stmt_list ();
12770 RECUR (OMP_FOR_PRE_BODY (t));
12771 pre_body = pop_stmt_list (pre_body);
12772
12773 body = push_stmt_list ();
12774 RECUR (OMP_FOR_BODY (t));
12775 body = pop_stmt_list (body);
12776
12777 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
12778 body, pre_body, clauses);
12779
12780 add_stmt (finish_omp_structured_block (stmt));
12781 }
12782 break;
12783
12784 case OMP_SECTIONS:
12785 case OMP_SINGLE:
12786 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
12787 stmt = push_stmt_list ();
12788 RECUR (OMP_BODY (t));
12789 stmt = pop_stmt_list (stmt);
12790
12791 t = copy_node (t);
12792 OMP_BODY (t) = stmt;
12793 OMP_CLAUSES (t) = tmp;
12794 add_stmt (t);
12795 break;
12796
12797 case OMP_SECTION:
12798 case OMP_CRITICAL:
12799 case OMP_MASTER:
12800 case OMP_ORDERED:
12801 stmt = push_stmt_list ();
12802 RECUR (OMP_BODY (t));
12803 stmt = pop_stmt_list (stmt);
12804
12805 t = copy_node (t);
12806 OMP_BODY (t) = stmt;
12807 add_stmt (t);
12808 break;
12809
12810 case OMP_ATOMIC:
12811 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
12812 {
12813 tree op1 = TREE_OPERAND (t, 1);
12814 tree lhs = RECUR (TREE_OPERAND (op1, 0));
12815 tree rhs = RECUR (TREE_OPERAND (op1, 1));
12816 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
12817 }
12818 break;
12819
12820 case EXPR_PACK_EXPANSION:
12821 error ("invalid use of pack expansion expression");
12822 return error_mark_node;
12823
12824 case NONTYPE_ARGUMENT_PACK:
12825 error ("use %<...%> to expand argument pack");
12826 return error_mark_node;
12827
12828 default:
12829 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
12830
12831 return tsubst_copy_and_build (t, args, complain, in_decl,
12832 /*function_p=*/false,
12833 integral_constant_expression_p);
12834 }
12835
12836 return NULL_TREE;
12837 #undef RECUR
12838 }
12839
12840 /* T is a postfix-expression that is not being used in a function
12841 call. Return the substituted version of T. */
12842
12843 static tree
12844 tsubst_non_call_postfix_expression (tree t, tree args,
12845 tsubst_flags_t complain,
12846 tree in_decl)
12847 {
12848 if (TREE_CODE (t) == SCOPE_REF)
12849 t = tsubst_qualified_id (t, args, complain, in_decl,
12850 /*done=*/false, /*address_p=*/false);
12851 else
12852 t = tsubst_copy_and_build (t, args, complain, in_decl,
12853 /*function_p=*/false,
12854 /*integral_constant_expression_p=*/false);
12855
12856 return t;
12857 }
12858
12859 /* Like tsubst but deals with expressions and performs semantic
12860 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
12861
12862 tree
12863 tsubst_copy_and_build (tree t,
12864 tree args,
12865 tsubst_flags_t complain,
12866 tree in_decl,
12867 bool function_p,
12868 bool integral_constant_expression_p)
12869 {
12870 #define RECUR(NODE) \
12871 tsubst_copy_and_build (NODE, args, complain, in_decl, \
12872 /*function_p=*/false, \
12873 integral_constant_expression_p)
12874
12875 tree op1;
12876
12877 if (t == NULL_TREE || t == error_mark_node)
12878 return t;
12879
12880 switch (TREE_CODE (t))
12881 {
12882 case USING_DECL:
12883 t = DECL_NAME (t);
12884 /* Fall through. */
12885 case IDENTIFIER_NODE:
12886 {
12887 tree decl;
12888 cp_id_kind idk;
12889 bool non_integral_constant_expression_p;
12890 const char *error_msg;
12891
12892 if (IDENTIFIER_TYPENAME_P (t))
12893 {
12894 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12895 t = mangle_conv_op_name_for_type (new_type);
12896 }
12897
12898 /* Look up the name. */
12899 decl = lookup_name (t);
12900
12901 /* By convention, expressions use ERROR_MARK_NODE to indicate
12902 failure, not NULL_TREE. */
12903 if (decl == NULL_TREE)
12904 decl = error_mark_node;
12905
12906 decl = finish_id_expression (t, decl, NULL_TREE,
12907 &idk,
12908 integral_constant_expression_p,
12909 /*allow_non_integral_constant_expression_p=*/false,
12910 &non_integral_constant_expression_p,
12911 /*template_p=*/false,
12912 /*done=*/true,
12913 /*address_p=*/false,
12914 /*template_arg_p=*/false,
12915 &error_msg,
12916 input_location);
12917 if (error_msg)
12918 error (error_msg);
12919 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
12920 decl = unqualified_name_lookup_error (decl);
12921 return decl;
12922 }
12923
12924 case TEMPLATE_ID_EXPR:
12925 {
12926 tree object;
12927 tree templ = RECUR (TREE_OPERAND (t, 0));
12928 tree targs = TREE_OPERAND (t, 1);
12929
12930 if (targs)
12931 targs = tsubst_template_args (targs, args, complain, in_decl);
12932
12933 if (TREE_CODE (templ) == COMPONENT_REF)
12934 {
12935 object = TREE_OPERAND (templ, 0);
12936 templ = TREE_OPERAND (templ, 1);
12937 }
12938 else
12939 object = NULL_TREE;
12940 templ = lookup_template_function (templ, targs);
12941
12942 if (object)
12943 return build3 (COMPONENT_REF, TREE_TYPE (templ),
12944 object, templ, NULL_TREE);
12945 else
12946 return baselink_for_fns (templ);
12947 }
12948
12949 case INDIRECT_REF:
12950 {
12951 tree r = RECUR (TREE_OPERAND (t, 0));
12952
12953 if (REFERENCE_REF_P (t))
12954 {
12955 /* A type conversion to reference type will be enclosed in
12956 such an indirect ref, but the substitution of the cast
12957 will have also added such an indirect ref. */
12958 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
12959 r = convert_from_reference (r);
12960 }
12961 else
12962 r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
12963 return r;
12964 }
12965
12966 case NOP_EXPR:
12967 return build_nop
12968 (tsubst (TREE_TYPE (t), args, complain, in_decl),
12969 RECUR (TREE_OPERAND (t, 0)));
12970
12971 case CAST_EXPR:
12972 case REINTERPRET_CAST_EXPR:
12973 case CONST_CAST_EXPR:
12974 case DYNAMIC_CAST_EXPR:
12975 case STATIC_CAST_EXPR:
12976 {
12977 tree type;
12978 tree op;
12979
12980 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12981 if (integral_constant_expression_p
12982 && !cast_valid_in_integral_constant_expression_p (type))
12983 {
12984 if (complain & tf_error)
12985 error ("a cast to a type other than an integral or "
12986 "enumeration type cannot appear in a constant-expression");
12987 return error_mark_node;
12988 }
12989
12990 op = RECUR (TREE_OPERAND (t, 0));
12991
12992 switch (TREE_CODE (t))
12993 {
12994 case CAST_EXPR:
12995 return build_functional_cast (type, op, complain);
12996 case REINTERPRET_CAST_EXPR:
12997 return build_reinterpret_cast (type, op, complain);
12998 case CONST_CAST_EXPR:
12999 return build_const_cast (type, op, complain);
13000 case DYNAMIC_CAST_EXPR:
13001 return build_dynamic_cast (type, op, complain);
13002 case STATIC_CAST_EXPR:
13003 return build_static_cast (type, op, complain);
13004 default:
13005 gcc_unreachable ();
13006 }
13007 }
13008
13009 case POSTDECREMENT_EXPR:
13010 case POSTINCREMENT_EXPR:
13011 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13012 args, complain, in_decl);
13013 return build_x_unary_op (TREE_CODE (t), op1, complain);
13014
13015 case PREDECREMENT_EXPR:
13016 case PREINCREMENT_EXPR:
13017 case NEGATE_EXPR:
13018 case BIT_NOT_EXPR:
13019 case ABS_EXPR:
13020 case TRUTH_NOT_EXPR:
13021 case UNARY_PLUS_EXPR: /* Unary + */
13022 case REALPART_EXPR:
13023 case IMAGPART_EXPR:
13024 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
13025 complain);
13026
13027 case ADDR_EXPR:
13028 op1 = TREE_OPERAND (t, 0);
13029 if (TREE_CODE (op1) == LABEL_DECL)
13030 return finish_label_address_expr (DECL_NAME (op1),
13031 EXPR_LOCATION (op1));
13032 if (TREE_CODE (op1) == SCOPE_REF)
13033 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
13034 /*done=*/true, /*address_p=*/true);
13035 else
13036 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
13037 in_decl);
13038 return build_x_unary_op (ADDR_EXPR, op1, complain);
13039
13040 case PLUS_EXPR:
13041 case MINUS_EXPR:
13042 case MULT_EXPR:
13043 case TRUNC_DIV_EXPR:
13044 case CEIL_DIV_EXPR:
13045 case FLOOR_DIV_EXPR:
13046 case ROUND_DIV_EXPR:
13047 case EXACT_DIV_EXPR:
13048 case BIT_AND_EXPR:
13049 case BIT_IOR_EXPR:
13050 case BIT_XOR_EXPR:
13051 case TRUNC_MOD_EXPR:
13052 case FLOOR_MOD_EXPR:
13053 case TRUTH_ANDIF_EXPR:
13054 case TRUTH_ORIF_EXPR:
13055 case TRUTH_AND_EXPR:
13056 case TRUTH_OR_EXPR:
13057 case RSHIFT_EXPR:
13058 case LSHIFT_EXPR:
13059 case RROTATE_EXPR:
13060 case LROTATE_EXPR:
13061 case EQ_EXPR:
13062 case NE_EXPR:
13063 case MAX_EXPR:
13064 case MIN_EXPR:
13065 case LE_EXPR:
13066 case GE_EXPR:
13067 case LT_EXPR:
13068 case GT_EXPR:
13069 case MEMBER_REF:
13070 case DOTSTAR_EXPR:
13071 return build_x_binary_op
13072 (TREE_CODE (t),
13073 RECUR (TREE_OPERAND (t, 0)),
13074 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
13075 ? ERROR_MARK
13076 : TREE_CODE (TREE_OPERAND (t, 0))),
13077 RECUR (TREE_OPERAND (t, 1)),
13078 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
13079 ? ERROR_MARK
13080 : TREE_CODE (TREE_OPERAND (t, 1))),
13081 /*overload=*/NULL,
13082 complain);
13083
13084 case SCOPE_REF:
13085 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
13086 /*address_p=*/false);
13087 case ARRAY_REF:
13088 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13089 args, complain, in_decl);
13090 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
13091
13092 case SIZEOF_EXPR:
13093 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13094 return tsubst_copy (t, args, complain, in_decl);
13095 /* Fall through */
13096
13097 case ALIGNOF_EXPR:
13098 op1 = TREE_OPERAND (t, 0);
13099 if (!args)
13100 {
13101 /* When there are no ARGS, we are trying to evaluate a
13102 non-dependent expression from the parser. Trying to do
13103 the substitutions may not work. */
13104 if (!TYPE_P (op1))
13105 op1 = TREE_TYPE (op1);
13106 }
13107 else
13108 {
13109 ++cp_unevaluated_operand;
13110 ++c_inhibit_evaluation_warnings;
13111 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13112 /*function_p=*/false,
13113 /*integral_constant_expression_p=*/false);
13114 --cp_unevaluated_operand;
13115 --c_inhibit_evaluation_warnings;
13116 }
13117 if (TYPE_P (op1))
13118 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
13119 complain & tf_error);
13120 else
13121 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
13122 complain & tf_error);
13123
13124 case AT_ENCODE_EXPR:
13125 {
13126 op1 = TREE_OPERAND (t, 0);
13127 ++cp_unevaluated_operand;
13128 ++c_inhibit_evaluation_warnings;
13129 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13130 /*function_p=*/false,
13131 /*integral_constant_expression_p=*/false);
13132 --cp_unevaluated_operand;
13133 --c_inhibit_evaluation_warnings;
13134 return objc_build_encode_expr (op1);
13135 }
13136
13137 case NOEXCEPT_EXPR:
13138 op1 = TREE_OPERAND (t, 0);
13139 ++cp_unevaluated_operand;
13140 ++c_inhibit_evaluation_warnings;
13141 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13142 /*function_p=*/false,
13143 /*integral_constant_expression_p=*/false);
13144 --cp_unevaluated_operand;
13145 --c_inhibit_evaluation_warnings;
13146 return finish_noexcept_expr (op1, complain);
13147
13148 case MODOP_EXPR:
13149 {
13150 tree r = build_x_modify_expr
13151 (RECUR (TREE_OPERAND (t, 0)),
13152 TREE_CODE (TREE_OPERAND (t, 1)),
13153 RECUR (TREE_OPERAND (t, 2)),
13154 complain);
13155 /* TREE_NO_WARNING must be set if either the expression was
13156 parenthesized or it uses an operator such as >>= rather
13157 than plain assignment. In the former case, it was already
13158 set and must be copied. In the latter case,
13159 build_x_modify_expr sets it and it must not be reset
13160 here. */
13161 if (TREE_NO_WARNING (t))
13162 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13163 return r;
13164 }
13165
13166 case ARROW_EXPR:
13167 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13168 args, complain, in_decl);
13169 /* Remember that there was a reference to this entity. */
13170 if (DECL_P (op1))
13171 mark_used (op1);
13172 return build_x_arrow (op1);
13173
13174 case NEW_EXPR:
13175 {
13176 tree placement = RECUR (TREE_OPERAND (t, 0));
13177 tree init = RECUR (TREE_OPERAND (t, 3));
13178 VEC(tree,gc) *placement_vec;
13179 VEC(tree,gc) *init_vec;
13180 tree ret;
13181
13182 if (placement == NULL_TREE)
13183 placement_vec = NULL;
13184 else
13185 {
13186 placement_vec = make_tree_vector ();
13187 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
13188 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
13189 }
13190
13191 /* If there was an initializer in the original tree, but it
13192 instantiated to an empty list, then we should pass a
13193 non-NULL empty vector to tell build_new that it was an
13194 empty initializer() rather than no initializer. This can
13195 only happen when the initializer is a pack expansion whose
13196 parameter packs are of length zero. */
13197 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
13198 init_vec = NULL;
13199 else
13200 {
13201 init_vec = make_tree_vector ();
13202 if (init == void_zero_node)
13203 gcc_assert (init_vec != NULL);
13204 else
13205 {
13206 for (; init != NULL_TREE; init = TREE_CHAIN (init))
13207 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
13208 }
13209 }
13210
13211 ret = build_new (&placement_vec,
13212 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
13213 RECUR (TREE_OPERAND (t, 2)),
13214 &init_vec,
13215 NEW_EXPR_USE_GLOBAL (t),
13216 complain);
13217
13218 if (placement_vec != NULL)
13219 release_tree_vector (placement_vec);
13220 if (init_vec != NULL)
13221 release_tree_vector (init_vec);
13222
13223 return ret;
13224 }
13225
13226 case DELETE_EXPR:
13227 return delete_sanity
13228 (RECUR (TREE_OPERAND (t, 0)),
13229 RECUR (TREE_OPERAND (t, 1)),
13230 DELETE_EXPR_USE_VEC (t),
13231 DELETE_EXPR_USE_GLOBAL (t),
13232 complain);
13233
13234 case COMPOUND_EXPR:
13235 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
13236 RECUR (TREE_OPERAND (t, 1)),
13237 complain);
13238
13239 case CALL_EXPR:
13240 {
13241 tree function;
13242 VEC(tree,gc) *call_args;
13243 unsigned int nargs, i;
13244 bool qualified_p;
13245 bool koenig_p;
13246 tree ret;
13247
13248 function = CALL_EXPR_FN (t);
13249 /* When we parsed the expression, we determined whether or
13250 not Koenig lookup should be performed. */
13251 koenig_p = KOENIG_LOOKUP_P (t);
13252 if (TREE_CODE (function) == SCOPE_REF)
13253 {
13254 qualified_p = true;
13255 function = tsubst_qualified_id (function, args, complain, in_decl,
13256 /*done=*/false,
13257 /*address_p=*/false);
13258 }
13259 else if (koenig_p && TREE_CODE (function) == IDENTIFIER_NODE)
13260 {
13261 /* Do nothing; calling tsubst_copy_and_build on an identifier
13262 would incorrectly perform unqualified lookup again.
13263
13264 Note that we can also have an IDENTIFIER_NODE if the earlier
13265 unqualified lookup found a member function; in that case
13266 koenig_p will be false and we do want to do the lookup
13267 again to find the instantiated member function.
13268
13269 FIXME but doing that causes c++/15272, so we need to stop
13270 using IDENTIFIER_NODE in that situation. */
13271 qualified_p = false;
13272 }
13273 else
13274 {
13275 if (TREE_CODE (function) == COMPONENT_REF)
13276 {
13277 tree op = TREE_OPERAND (function, 1);
13278
13279 qualified_p = (TREE_CODE (op) == SCOPE_REF
13280 || (BASELINK_P (op)
13281 && BASELINK_QUALIFIED_P (op)));
13282 }
13283 else
13284 qualified_p = false;
13285
13286 function = tsubst_copy_and_build (function, args, complain,
13287 in_decl,
13288 !qualified_p,
13289 integral_constant_expression_p);
13290
13291 if (BASELINK_P (function))
13292 qualified_p = true;
13293 }
13294
13295 nargs = call_expr_nargs (t);
13296 call_args = make_tree_vector ();
13297 for (i = 0; i < nargs; ++i)
13298 {
13299 tree arg = CALL_EXPR_ARG (t, i);
13300
13301 if (!PACK_EXPANSION_P (arg))
13302 VEC_safe_push (tree, gc, call_args,
13303 RECUR (CALL_EXPR_ARG (t, i)));
13304 else
13305 {
13306 /* Expand the pack expansion and push each entry onto
13307 CALL_ARGS. */
13308 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
13309 if (TREE_CODE (arg) == TREE_VEC)
13310 {
13311 unsigned int len, j;
13312
13313 len = TREE_VEC_LENGTH (arg);
13314 for (j = 0; j < len; ++j)
13315 {
13316 tree value = TREE_VEC_ELT (arg, j);
13317 if (value != NULL_TREE)
13318 value = convert_from_reference (value);
13319 VEC_safe_push (tree, gc, call_args, value);
13320 }
13321 }
13322 else
13323 {
13324 /* A partial substitution. Add one entry. */
13325 VEC_safe_push (tree, gc, call_args, arg);
13326 }
13327 }
13328 }
13329
13330 /* We do not perform argument-dependent lookup if normal
13331 lookup finds a non-function, in accordance with the
13332 expected resolution of DR 218. */
13333 if (koenig_p
13334 && ((is_overloaded_fn (function)
13335 /* If lookup found a member function, the Koenig lookup is
13336 not appropriate, even if an unqualified-name was used
13337 to denote the function. */
13338 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
13339 || TREE_CODE (function) == IDENTIFIER_NODE)
13340 /* Only do this when substitution turns a dependent call
13341 into a non-dependent call. */
13342 && type_dependent_expression_p_push (t)
13343 && !any_type_dependent_arguments_p (call_args))
13344 function = perform_koenig_lookup (function, call_args, false,
13345 tf_none);
13346
13347 if (TREE_CODE (function) == IDENTIFIER_NODE
13348 && !any_type_dependent_arguments_p (call_args))
13349 {
13350 if (koenig_p && (complain & tf_warning_or_error))
13351 {
13352 /* For backwards compatibility and good diagnostics, try
13353 the unqualified lookup again if we aren't in SFINAE
13354 context. */
13355 tree unq = (tsubst_copy_and_build
13356 (function, args, complain, in_decl, true,
13357 integral_constant_expression_p));
13358 if (unq != function)
13359 {
13360 tree fn = unq;
13361 if (TREE_CODE (fn) == COMPONENT_REF)
13362 fn = TREE_OPERAND (fn, 1);
13363 if (is_overloaded_fn (fn))
13364 fn = get_first_fn (fn);
13365 permerror (EXPR_LOC_OR_HERE (t),
13366 "%qD was not declared in this scope, "
13367 "and no declarations were found by "
13368 "argument-dependent lookup at the point "
13369 "of instantiation", function);
13370 if (DECL_CLASS_SCOPE_P (fn))
13371 {
13372 inform (EXPR_LOC_OR_HERE (t),
13373 "declarations in dependent base %qT are "
13374 "not found by unqualified lookup",
13375 DECL_CLASS_CONTEXT (fn));
13376 if (current_class_ptr)
13377 inform (EXPR_LOC_OR_HERE (t),
13378 "use %<this->%D%> instead", function);
13379 else
13380 inform (EXPR_LOC_OR_HERE (t),
13381 "use %<%T::%D%> instead",
13382 current_class_name, function);
13383 }
13384 else
13385 inform (0, "%q+D declared here, later in the "
13386 "translation unit", fn);
13387 function = unq;
13388 }
13389 }
13390 if (TREE_CODE (function) == IDENTIFIER_NODE)
13391 {
13392 unqualified_name_lookup_error (function);
13393 release_tree_vector (call_args);
13394 return error_mark_node;
13395 }
13396 }
13397
13398 /* Remember that there was a reference to this entity. */
13399 if (DECL_P (function))
13400 mark_used (function);
13401
13402 if (TREE_CODE (function) == OFFSET_REF)
13403 ret = build_offset_ref_call_from_tree (function, &call_args);
13404 else if (TREE_CODE (function) == COMPONENT_REF)
13405 {
13406 tree instance = TREE_OPERAND (function, 0);
13407 tree fn = TREE_OPERAND (function, 1);
13408
13409 if (processing_template_decl
13410 && (type_dependent_expression_p (instance)
13411 || (!BASELINK_P (fn)
13412 && TREE_CODE (fn) != FIELD_DECL)
13413 || type_dependent_expression_p (fn)
13414 || any_type_dependent_arguments_p (call_args)))
13415 ret = build_nt_call_vec (function, call_args);
13416 else if (!BASELINK_P (fn))
13417 ret = finish_call_expr (function, &call_args,
13418 /*disallow_virtual=*/false,
13419 /*koenig_p=*/false,
13420 complain);
13421 else
13422 ret = (build_new_method_call
13423 (instance, fn,
13424 &call_args, NULL_TREE,
13425 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
13426 /*fn_p=*/NULL,
13427 complain));
13428 }
13429 else
13430 ret = finish_call_expr (function, &call_args,
13431 /*disallow_virtual=*/qualified_p,
13432 koenig_p,
13433 complain);
13434
13435 release_tree_vector (call_args);
13436
13437 return ret;
13438 }
13439
13440 case COND_EXPR:
13441 return build_x_conditional_expr
13442 (RECUR (TREE_OPERAND (t, 0)),
13443 RECUR (TREE_OPERAND (t, 1)),
13444 RECUR (TREE_OPERAND (t, 2)),
13445 complain);
13446
13447 case PSEUDO_DTOR_EXPR:
13448 return finish_pseudo_destructor_expr
13449 (RECUR (TREE_OPERAND (t, 0)),
13450 RECUR (TREE_OPERAND (t, 1)),
13451 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl));
13452
13453 case TREE_LIST:
13454 {
13455 tree purpose, value, chain;
13456
13457 if (t == void_list_node)
13458 return t;
13459
13460 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
13461 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
13462 {
13463 /* We have pack expansions, so expand those and
13464 create a new list out of it. */
13465 tree purposevec = NULL_TREE;
13466 tree valuevec = NULL_TREE;
13467 tree chain;
13468 int i, len = -1;
13469
13470 /* Expand the argument expressions. */
13471 if (TREE_PURPOSE (t))
13472 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
13473 complain, in_decl);
13474 if (TREE_VALUE (t))
13475 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
13476 complain, in_decl);
13477
13478 /* Build the rest of the list. */
13479 chain = TREE_CHAIN (t);
13480 if (chain && chain != void_type_node)
13481 chain = RECUR (chain);
13482
13483 /* Determine the number of arguments. */
13484 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
13485 {
13486 len = TREE_VEC_LENGTH (purposevec);
13487 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
13488 }
13489 else if (TREE_CODE (valuevec) == TREE_VEC)
13490 len = TREE_VEC_LENGTH (valuevec);
13491 else
13492 {
13493 /* Since we only performed a partial substitution into
13494 the argument pack, we only return a single list
13495 node. */
13496 if (purposevec == TREE_PURPOSE (t)
13497 && valuevec == TREE_VALUE (t)
13498 && chain == TREE_CHAIN (t))
13499 return t;
13500
13501 return tree_cons (purposevec, valuevec, chain);
13502 }
13503
13504 /* Convert the argument vectors into a TREE_LIST */
13505 i = len;
13506 while (i > 0)
13507 {
13508 /* Grab the Ith values. */
13509 i--;
13510 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
13511 : NULL_TREE;
13512 value
13513 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
13514 : NULL_TREE;
13515
13516 /* Build the list (backwards). */
13517 chain = tree_cons (purpose, value, chain);
13518 }
13519
13520 return chain;
13521 }
13522
13523 purpose = TREE_PURPOSE (t);
13524 if (purpose)
13525 purpose = RECUR (purpose);
13526 value = TREE_VALUE (t);
13527 if (value)
13528 value = RECUR (value);
13529 chain = TREE_CHAIN (t);
13530 if (chain && chain != void_type_node)
13531 chain = RECUR (chain);
13532 if (purpose == TREE_PURPOSE (t)
13533 && value == TREE_VALUE (t)
13534 && chain == TREE_CHAIN (t))
13535 return t;
13536 return tree_cons (purpose, value, chain);
13537 }
13538
13539 case COMPONENT_REF:
13540 {
13541 tree object;
13542 tree object_type;
13543 tree member;
13544
13545 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13546 args, complain, in_decl);
13547 /* Remember that there was a reference to this entity. */
13548 if (DECL_P (object))
13549 mark_used (object);
13550 object_type = TREE_TYPE (object);
13551
13552 member = TREE_OPERAND (t, 1);
13553 if (BASELINK_P (member))
13554 member = tsubst_baselink (member,
13555 non_reference (TREE_TYPE (object)),
13556 args, complain, in_decl);
13557 else
13558 member = tsubst_copy (member, args, complain, in_decl);
13559 if (member == error_mark_node)
13560 return error_mark_node;
13561
13562 if (object_type && !CLASS_TYPE_P (object_type))
13563 {
13564 if (SCALAR_TYPE_P (object_type))
13565 {
13566 tree s = NULL_TREE;
13567 tree dtor = member;
13568
13569 if (TREE_CODE (dtor) == SCOPE_REF)
13570 {
13571 s = TREE_OPERAND (dtor, 0);
13572 dtor = TREE_OPERAND (dtor, 1);
13573 }
13574 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
13575 {
13576 dtor = TREE_OPERAND (dtor, 0);
13577 if (TYPE_P (dtor))
13578 return finish_pseudo_destructor_expr (object, s, dtor);
13579 }
13580 }
13581 }
13582 else if (TREE_CODE (member) == SCOPE_REF
13583 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
13584 {
13585 tree tmpl;
13586 tree args;
13587
13588 /* Lookup the template functions now that we know what the
13589 scope is. */
13590 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
13591 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
13592 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
13593 /*is_type_p=*/false,
13594 /*complain=*/false);
13595 if (BASELINK_P (member))
13596 {
13597 BASELINK_FUNCTIONS (member)
13598 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
13599 args);
13600 member = (adjust_result_of_qualified_name_lookup
13601 (member, BINFO_TYPE (BASELINK_BINFO (member)),
13602 object_type));
13603 }
13604 else
13605 {
13606 qualified_name_lookup_error (object_type, tmpl, member,
13607 input_location);
13608 return error_mark_node;
13609 }
13610 }
13611 else if (TREE_CODE (member) == SCOPE_REF
13612 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
13613 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
13614 {
13615 if (complain & tf_error)
13616 {
13617 if (TYPE_P (TREE_OPERAND (member, 0)))
13618 error ("%qT is not a class or namespace",
13619 TREE_OPERAND (member, 0));
13620 else
13621 error ("%qD is not a class or namespace",
13622 TREE_OPERAND (member, 0));
13623 }
13624 return error_mark_node;
13625 }
13626 else if (TREE_CODE (member) == FIELD_DECL)
13627 return finish_non_static_data_member (member, object, NULL_TREE);
13628
13629 return finish_class_member_access_expr (object, member,
13630 /*template_p=*/false,
13631 complain);
13632 }
13633
13634 case THROW_EXPR:
13635 return build_throw
13636 (RECUR (TREE_OPERAND (t, 0)));
13637
13638 case CONSTRUCTOR:
13639 {
13640 VEC(constructor_elt,gc) *n;
13641 constructor_elt *ce;
13642 unsigned HOST_WIDE_INT idx;
13643 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13644 bool process_index_p;
13645 int newlen;
13646 bool need_copy_p = false;
13647 tree r;
13648
13649 if (type == error_mark_node)
13650 return error_mark_node;
13651
13652 /* digest_init will do the wrong thing if we let it. */
13653 if (type && TYPE_PTRMEMFUNC_P (type))
13654 return t;
13655
13656 /* We do not want to process the index of aggregate
13657 initializers as they are identifier nodes which will be
13658 looked up by digest_init. */
13659 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
13660
13661 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
13662 newlen = VEC_length (constructor_elt, n);
13663 FOR_EACH_VEC_ELT (constructor_elt, n, idx, ce)
13664 {
13665 if (ce->index && process_index_p)
13666 ce->index = RECUR (ce->index);
13667
13668 if (PACK_EXPANSION_P (ce->value))
13669 {
13670 /* Substitute into the pack expansion. */
13671 ce->value = tsubst_pack_expansion (ce->value, args, complain,
13672 in_decl);
13673
13674 if (ce->value == error_mark_node
13675 || PACK_EXPANSION_P (ce->value))
13676 ;
13677 else if (TREE_VEC_LENGTH (ce->value) == 1)
13678 /* Just move the argument into place. */
13679 ce->value = TREE_VEC_ELT (ce->value, 0);
13680 else
13681 {
13682 /* Update the length of the final CONSTRUCTOR
13683 arguments vector, and note that we will need to
13684 copy.*/
13685 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
13686 need_copy_p = true;
13687 }
13688 }
13689 else
13690 ce->value = RECUR (ce->value);
13691 }
13692
13693 if (need_copy_p)
13694 {
13695 VEC(constructor_elt,gc) *old_n = n;
13696
13697 n = VEC_alloc (constructor_elt, gc, newlen);
13698 FOR_EACH_VEC_ELT (constructor_elt, old_n, idx, ce)
13699 {
13700 if (TREE_CODE (ce->value) == TREE_VEC)
13701 {
13702 int i, len = TREE_VEC_LENGTH (ce->value);
13703 for (i = 0; i < len; ++i)
13704 CONSTRUCTOR_APPEND_ELT (n, 0,
13705 TREE_VEC_ELT (ce->value, i));
13706 }
13707 else
13708 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
13709 }
13710 }
13711
13712 r = build_constructor (init_list_type_node, n);
13713 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
13714
13715 if (TREE_HAS_CONSTRUCTOR (t))
13716 return finish_compound_literal (type, r, complain);
13717
13718 TREE_TYPE (r) = type;
13719 return r;
13720 }
13721
13722 case TYPEID_EXPR:
13723 {
13724 tree operand_0 = TREE_OPERAND (t, 0);
13725 if (TYPE_P (operand_0))
13726 {
13727 operand_0 = tsubst (operand_0, args, complain, in_decl);
13728 return get_typeid (operand_0);
13729 }
13730 else
13731 {
13732 operand_0 = RECUR (operand_0);
13733 return build_typeid (operand_0);
13734 }
13735 }
13736
13737 case VAR_DECL:
13738 if (!args)
13739 return t;
13740 /* Fall through */
13741
13742 case PARM_DECL:
13743 {
13744 tree r = tsubst_copy (t, args, complain, in_decl);
13745
13746 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
13747 /* If the original type was a reference, we'll be wrapped in
13748 the appropriate INDIRECT_REF. */
13749 r = convert_from_reference (r);
13750 return r;
13751 }
13752
13753 case VA_ARG_EXPR:
13754 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
13755 tsubst (TREE_TYPE (t), args, complain, in_decl));
13756
13757 case OFFSETOF_EXPR:
13758 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
13759
13760 case TRAIT_EXPR:
13761 {
13762 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
13763 complain, in_decl);
13764
13765 tree type2 = TRAIT_EXPR_TYPE2 (t);
13766 if (type2)
13767 type2 = tsubst_copy (type2, args, complain, in_decl);
13768
13769 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
13770 }
13771
13772 case STMT_EXPR:
13773 {
13774 tree old_stmt_expr = cur_stmt_expr;
13775 tree stmt_expr = begin_stmt_expr ();
13776
13777 cur_stmt_expr = stmt_expr;
13778 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
13779 integral_constant_expression_p);
13780 stmt_expr = finish_stmt_expr (stmt_expr, false);
13781 cur_stmt_expr = old_stmt_expr;
13782
13783 /* If the resulting list of expression statement is empty,
13784 fold it further into void_zero_node. */
13785 if (empty_expr_stmt_p (stmt_expr))
13786 stmt_expr = void_zero_node;
13787
13788 return stmt_expr;
13789 }
13790
13791 case CONST_DECL:
13792 t = tsubst_copy (t, args, complain, in_decl);
13793 /* As in finish_id_expression, we resolve enumeration constants
13794 to their underlying values. */
13795 if (TREE_CODE (t) == CONST_DECL)
13796 {
13797 used_types_insert (TREE_TYPE (t));
13798 return DECL_INITIAL (t);
13799 }
13800 return t;
13801
13802 case LAMBDA_EXPR:
13803 {
13804 tree r = build_lambda_expr ();
13805
13806 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13807 TREE_TYPE (r) = type;
13808 CLASSTYPE_LAMBDA_EXPR (type) = r;
13809
13810 LAMBDA_EXPR_LOCATION (r)
13811 = LAMBDA_EXPR_LOCATION (t);
13812 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
13813 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
13814 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
13815 LAMBDA_EXPR_DISCRIMINATOR (r)
13816 = (LAMBDA_EXPR_DISCRIMINATOR (t));
13817 LAMBDA_EXPR_CAPTURE_LIST (r)
13818 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
13819 LAMBDA_EXPR_EXTRA_SCOPE (r)
13820 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
13821 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
13822 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
13823
13824 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
13825 determine_visibility (TYPE_NAME (type));
13826 /* Now that we know visibility, instantiate the type so we have a
13827 declaration of the op() for later calls to lambda_function. */
13828 complete_type (type);
13829
13830 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
13831 if (type)
13832 apply_lambda_return_type (r, type);
13833
13834 return build_lambda_object (r);
13835 }
13836
13837 case TARGET_EXPR:
13838 /* We can get here for a constant initializer of non-dependent type.
13839 FIXME stop folding in cp_parser_initializer_clause. */
13840 gcc_assert (TREE_CONSTANT (t));
13841 {
13842 tree r = get_target_expr (RECUR (TARGET_EXPR_INITIAL (t)));
13843 TREE_CONSTANT (r) = true;
13844 return r;
13845 }
13846
13847 default:
13848 /* Handle Objective-C++ constructs, if appropriate. */
13849 {
13850 tree subst
13851 = objcp_tsubst_copy_and_build (t, args, complain,
13852 in_decl, /*function_p=*/false);
13853 if (subst)
13854 return subst;
13855 }
13856 return tsubst_copy (t, args, complain, in_decl);
13857 }
13858
13859 #undef RECUR
13860 }
13861
13862 /* Verify that the instantiated ARGS are valid. For type arguments,
13863 make sure that the type's linkage is ok. For non-type arguments,
13864 make sure they are constants if they are integral or enumerations.
13865 Emit an error under control of COMPLAIN, and return TRUE on error. */
13866
13867 static bool
13868 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
13869 {
13870 if (ARGUMENT_PACK_P (t))
13871 {
13872 tree vec = ARGUMENT_PACK_ARGS (t);
13873 int len = TREE_VEC_LENGTH (vec);
13874 bool result = false;
13875 int i;
13876
13877 for (i = 0; i < len; ++i)
13878 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
13879 result = true;
13880 return result;
13881 }
13882 else if (TYPE_P (t))
13883 {
13884 /* [basic.link]: A name with no linkage (notably, the name
13885 of a class or enumeration declared in a local scope)
13886 shall not be used to declare an entity with linkage.
13887 This implies that names with no linkage cannot be used as
13888 template arguments
13889
13890 DR 757 relaxes this restriction for C++0x. */
13891 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
13892 : no_linkage_check (t, /*relaxed_p=*/false));
13893
13894 if (nt)
13895 {
13896 /* DR 488 makes use of a type with no linkage cause
13897 type deduction to fail. */
13898 if (complain & tf_error)
13899 {
13900 if (TYPE_ANONYMOUS_P (nt))
13901 error ("%qT is/uses anonymous type", t);
13902 else
13903 error ("template argument for %qD uses local type %qT",
13904 tmpl, t);
13905 }
13906 return true;
13907 }
13908 /* In order to avoid all sorts of complications, we do not
13909 allow variably-modified types as template arguments. */
13910 else if (variably_modified_type_p (t, NULL_TREE))
13911 {
13912 if (complain & tf_error)
13913 error ("%qT is a variably modified type", t);
13914 return true;
13915 }
13916 }
13917 /* A non-type argument of integral or enumerated type must be a
13918 constant. */
13919 else if (TREE_TYPE (t)
13920 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
13921 && !TREE_CONSTANT (t))
13922 {
13923 if (complain & tf_error)
13924 error ("integral expression %qE is not constant", t);
13925 return true;
13926 }
13927 return false;
13928 }
13929
13930 static bool
13931 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
13932 {
13933 int ix, len = DECL_NTPARMS (tmpl);
13934 bool result = false;
13935
13936 for (ix = 0; ix != len; ix++)
13937 {
13938 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
13939 result = true;
13940 }
13941 if (result && (complain & tf_error))
13942 error (" trying to instantiate %qD", tmpl);
13943 return result;
13944 }
13945
13946 /* In C++0x, it's possible to have a function template whose type depends
13947 on itself recursively. This is most obvious with decltype, but can also
13948 occur with enumeration scope (c++/48969). So we need to catch infinite
13949 recursion and reject the substitution at deduction time; this function
13950 will return error_mark_node for any repeated substitution.
13951
13952 This also catches excessive recursion such as when f<N> depends on
13953 f<N-1> across all integers, and returns error_mark_node for all the
13954 substitutions back up to the initial one.
13955
13956 This is, of course, not reentrant. */
13957
13958 static tree
13959 deduction_tsubst_fntype (tree fn, tree targs, tsubst_flags_t complain)
13960 {
13961 static bool excessive_deduction_depth;
13962 static int deduction_depth;
13963 struct pending_template *old_last_pend = last_pending_template;
13964
13965 tree fntype = TREE_TYPE (fn);
13966 tree tinst;
13967 tree r;
13968
13969 if (excessive_deduction_depth)
13970 return error_mark_node;
13971
13972 tinst = build_tree_list (fn, targs);
13973 if (!push_tinst_level (tinst))
13974 {
13975 excessive_deduction_depth = true;
13976 ggc_free (tinst);
13977 return error_mark_node;
13978 }
13979
13980 input_location = DECL_SOURCE_LOCATION (fn);
13981 ++deduction_depth;
13982 push_deduction_access_scope (fn);
13983 r = tsubst (fntype, targs, complain, NULL_TREE);
13984 pop_deduction_access_scope (fn);
13985 --deduction_depth;
13986
13987 if (excessive_deduction_depth)
13988 {
13989 r = error_mark_node;
13990 if (deduction_depth == 0)
13991 /* Reset once we're all the way out. */
13992 excessive_deduction_depth = false;
13993 }
13994
13995 pop_tinst_level ();
13996 /* We can't free this if a pending_template entry is pointing at it. */
13997 if (last_pending_template == old_last_pend)
13998 ggc_free (tinst);
13999 return r;
14000 }
14001
14002 /* Instantiate the indicated variable or function template TMPL with
14003 the template arguments in TARG_PTR. */
14004
14005 static tree
14006 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
14007 {
14008 tree targ_ptr = orig_args;
14009 tree fndecl;
14010 tree gen_tmpl;
14011 tree spec;
14012 HOST_WIDE_INT saved_processing_template_decl;
14013
14014 if (tmpl == error_mark_node)
14015 return error_mark_node;
14016
14017 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
14018
14019 /* If this function is a clone, handle it specially. */
14020 if (DECL_CLONED_FUNCTION_P (tmpl))
14021 {
14022 tree spec;
14023 tree clone;
14024
14025 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14026 DECL_CLONED_FUNCTION. */
14027 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
14028 targ_ptr, complain);
14029 if (spec == error_mark_node)
14030 return error_mark_node;
14031
14032 /* Look for the clone. */
14033 FOR_EACH_CLONE (clone, spec)
14034 if (DECL_NAME (clone) == DECL_NAME (tmpl))
14035 return clone;
14036 /* We should always have found the clone by now. */
14037 gcc_unreachable ();
14038 return NULL_TREE;
14039 }
14040
14041 /* Check to see if we already have this specialization. */
14042 gen_tmpl = most_general_template (tmpl);
14043 if (tmpl != gen_tmpl)
14044 /* The TMPL is a partial instantiation. To get a full set of
14045 arguments we must add the arguments used to perform the
14046 partial instantiation. */
14047 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
14048 targ_ptr);
14049
14050 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14051 but it doesn't seem to be on the hot path. */
14052 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
14053
14054 gcc_assert (tmpl == gen_tmpl
14055 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
14056 == spec)
14057 || fndecl == NULL_TREE);
14058
14059 if (spec != NULL_TREE)
14060 return spec;
14061
14062 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
14063 complain))
14064 return error_mark_node;
14065
14066 /* We are building a FUNCTION_DECL, during which the access of its
14067 parameters and return types have to be checked. However this
14068 FUNCTION_DECL which is the desired context for access checking
14069 is not built yet. We solve this chicken-and-egg problem by
14070 deferring all checks until we have the FUNCTION_DECL. */
14071 push_deferring_access_checks (dk_deferred);
14072
14073 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
14074 (because, for example, we have encountered a non-dependent
14075 function call in the body of a template function and must now
14076 determine which of several overloaded functions will be called),
14077 within the instantiation itself we are not processing a
14078 template. */
14079 saved_processing_template_decl = processing_template_decl;
14080 processing_template_decl = 0;
14081 /* Substitute template parameters to obtain the specialization. */
14082 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
14083 targ_ptr, complain, gen_tmpl);
14084 processing_template_decl = saved_processing_template_decl;
14085 if (fndecl == error_mark_node)
14086 return error_mark_node;
14087
14088 /* Now we know the specialization, compute access previously
14089 deferred. */
14090 push_access_scope (fndecl);
14091
14092 /* Some typedefs referenced from within the template code need to be access
14093 checked at template instantiation time, i.e now. These types were
14094 added to the template at parsing time. Let's get those and perfom
14095 the acces checks then. */
14096 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
14097 perform_deferred_access_checks ();
14098 pop_access_scope (fndecl);
14099 pop_deferring_access_checks ();
14100
14101 /* The DECL_TI_TEMPLATE should always be the immediate parent
14102 template, not the most general template. */
14103 DECL_TI_TEMPLATE (fndecl) = tmpl;
14104
14105 /* If we've just instantiated the main entry point for a function,
14106 instantiate all the alternate entry points as well. We do this
14107 by cloning the instantiation of the main entry point, not by
14108 instantiating the template clones. */
14109 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
14110 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
14111
14112 return fndecl;
14113 }
14114
14115 /* Wrapper for instantiate_template_1. */
14116
14117 tree
14118 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
14119 {
14120 tree ret;
14121 timevar_push (TV_TEMPLATE_INST);
14122 ret = instantiate_template_1 (tmpl, orig_args, complain);
14123 timevar_pop (TV_TEMPLATE_INST);
14124 return ret;
14125 }
14126
14127 /* We're going to do deduction substitution on the type of TMPL, a function
14128 template. In C++11 mode, push into that access scope. In C++03 mode,
14129 disable access checking. */
14130
14131 static void
14132 push_deduction_access_scope (tree tmpl)
14133 {
14134 if (cxx_dialect >= cxx0x)
14135 {
14136 int ptd = processing_template_decl;
14137 push_access_scope (DECL_TEMPLATE_RESULT (tmpl));
14138 /* Preserve processing_template_decl across push_to_top_level. */
14139 if (ptd && !processing_template_decl)
14140 ++processing_template_decl;
14141 }
14142 else
14143 push_deferring_access_checks (dk_no_check);
14144 }
14145
14146 /* And pop back out. */
14147
14148 static void
14149 pop_deduction_access_scope (tree tmpl)
14150 {
14151 if (cxx_dialect >= cxx0x)
14152 pop_access_scope (DECL_TEMPLATE_RESULT (tmpl));
14153 else
14154 pop_deferring_access_checks ();
14155 }
14156
14157 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14158 NARGS elements of the arguments that are being used when calling
14159 it. TARGS is a vector into which the deduced template arguments
14160 are placed.
14161
14162 Return zero for success, 2 for an incomplete match that doesn't resolve
14163 all the types, and 1 for complete failure. An error message will be
14164 printed only for an incomplete match.
14165
14166 If FN is a conversion operator, or we are trying to produce a specific
14167 specialization, RETURN_TYPE is the return type desired.
14168
14169 The EXPLICIT_TARGS are explicit template arguments provided via a
14170 template-id.
14171
14172 The parameter STRICT is one of:
14173
14174 DEDUCE_CALL:
14175 We are deducing arguments for a function call, as in
14176 [temp.deduct.call].
14177
14178 DEDUCE_CONV:
14179 We are deducing arguments for a conversion function, as in
14180 [temp.deduct.conv].
14181
14182 DEDUCE_EXACT:
14183 We are deducing arguments when doing an explicit instantiation
14184 as in [temp.explicit], when determining an explicit specialization
14185 as in [temp.expl.spec], or when taking the address of a function
14186 template, as in [temp.deduct.funcaddr]. */
14187
14188 int
14189 fn_type_unification (tree fn,
14190 tree explicit_targs,
14191 tree targs,
14192 const tree *args,
14193 unsigned int nargs,
14194 tree return_type,
14195 unification_kind_t strict,
14196 int flags,
14197 bool explain_p)
14198 {
14199 tree parms;
14200 tree fntype;
14201 int result;
14202 bool incomplete_argument_packs_p = false;
14203
14204 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
14205
14206 fntype = TREE_TYPE (fn);
14207 if (explicit_targs)
14208 {
14209 /* [temp.deduct]
14210
14211 The specified template arguments must match the template
14212 parameters in kind (i.e., type, nontype, template), and there
14213 must not be more arguments than there are parameters;
14214 otherwise type deduction fails.
14215
14216 Nontype arguments must match the types of the corresponding
14217 nontype template parameters, or must be convertible to the
14218 types of the corresponding nontype parameters as specified in
14219 _temp.arg.nontype_, otherwise type deduction fails.
14220
14221 All references in the function type of the function template
14222 to the corresponding template parameters are replaced by the
14223 specified template argument values. If a substitution in a
14224 template parameter or in the function type of the function
14225 template results in an invalid type, type deduction fails. */
14226 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
14227 int i, len = TREE_VEC_LENGTH (tparms);
14228 tree converted_args;
14229 bool incomplete = false;
14230
14231 if (explicit_targs == error_mark_node)
14232 return unify_invalid (explain_p);
14233
14234 converted_args
14235 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
14236 (explain_p
14237 ? tf_warning_or_error
14238 : tf_none),
14239 /*require_all_args=*/false,
14240 /*use_default_args=*/false));
14241 if (converted_args == error_mark_node)
14242 return 1;
14243
14244 /* Substitute the explicit args into the function type. This is
14245 necessary so that, for instance, explicitly declared function
14246 arguments can match null pointed constants. If we were given
14247 an incomplete set of explicit args, we must not do semantic
14248 processing during substitution as we could create partial
14249 instantiations. */
14250 for (i = 0; i < len; i++)
14251 {
14252 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
14253 bool parameter_pack = false;
14254
14255 /* Dig out the actual parm. */
14256 if (TREE_CODE (parm) == TYPE_DECL
14257 || TREE_CODE (parm) == TEMPLATE_DECL)
14258 {
14259 parm = TREE_TYPE (parm);
14260 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
14261 }
14262 else if (TREE_CODE (parm) == PARM_DECL)
14263 {
14264 parm = DECL_INITIAL (parm);
14265 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
14266 }
14267
14268 if (parameter_pack)
14269 {
14270 int level, idx;
14271 tree targ;
14272 template_parm_level_and_index (parm, &level, &idx);
14273
14274 /* Mark the argument pack as "incomplete". We could
14275 still deduce more arguments during unification.
14276 We remove this mark in type_unification_real. */
14277 targ = TMPL_ARG (converted_args, level, idx);
14278 if (targ)
14279 {
14280 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
14281 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
14282 = ARGUMENT_PACK_ARGS (targ);
14283 }
14284
14285 /* We have some incomplete argument packs. */
14286 incomplete_argument_packs_p = true;
14287 }
14288 }
14289
14290 if (incomplete_argument_packs_p)
14291 /* Any substitution is guaranteed to be incomplete if there
14292 are incomplete argument packs, because we can still deduce
14293 more arguments. */
14294 incomplete = 1;
14295 else
14296 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
14297
14298 processing_template_decl += incomplete;
14299 fntype = deduction_tsubst_fntype (fn, converted_args,
14300 (explain_p
14301 ? tf_warning_or_error
14302 : tf_none));
14303 processing_template_decl -= incomplete;
14304
14305 if (fntype == error_mark_node)
14306 return 1;
14307
14308 /* Place the explicitly specified arguments in TARGS. */
14309 for (i = NUM_TMPL_ARGS (converted_args); i--;)
14310 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
14311 }
14312
14313 /* Never do unification on the 'this' parameter. */
14314 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
14315
14316 if (return_type)
14317 {
14318 tree *new_args;
14319
14320 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
14321 new_args = XALLOCAVEC (tree, nargs + 1);
14322 new_args[0] = return_type;
14323 memcpy (new_args + 1, args, nargs * sizeof (tree));
14324 args = new_args;
14325 ++nargs;
14326 }
14327
14328 /* We allow incomplete unification without an error message here
14329 because the standard doesn't seem to explicitly prohibit it. Our
14330 callers must be ready to deal with unification failures in any
14331 event. */
14332 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
14333 targs, parms, args, nargs, /*subr=*/0,
14334 strict, flags, explain_p);
14335
14336 /* Now that we have bindings for all of the template arguments,
14337 ensure that the arguments deduced for the template template
14338 parameters have compatible template parameter lists. We cannot
14339 check this property before we have deduced all template
14340 arguments, because the template parameter types of a template
14341 template parameter might depend on prior template parameters
14342 deduced after the template template parameter. The following
14343 ill-formed example illustrates this issue:
14344
14345 template<typename T, template<T> class C> void f(C<5>, T);
14346
14347 template<int N> struct X {};
14348
14349 void g() {
14350 f(X<5>(), 5l); // error: template argument deduction fails
14351 }
14352
14353 The template parameter list of 'C' depends on the template type
14354 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14355 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14356 time that we deduce 'C'. */
14357 if (result == 0
14358 && !template_template_parm_bindings_ok_p
14359 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
14360 return unify_inconsistent_template_template_parameters (explain_p);
14361
14362 if (result == 0)
14363 /* All is well so far. Now, check:
14364
14365 [temp.deduct]
14366
14367 When all template arguments have been deduced, all uses of
14368 template parameters in nondeduced contexts are replaced with
14369 the corresponding deduced argument values. If the
14370 substitution results in an invalid type, as described above,
14371 type deduction fails. */
14372 {
14373 tree substed = deduction_tsubst_fntype (fn, targs,
14374 (explain_p
14375 ? tf_warning_or_error
14376 : tf_none));
14377 if (substed == error_mark_node)
14378 return 1;
14379
14380 /* If we're looking for an exact match, check that what we got
14381 is indeed an exact match. It might not be if some template
14382 parameters are used in non-deduced contexts. */
14383 if (strict == DEDUCE_EXACT)
14384 {
14385 unsigned int i;
14386
14387 tree sarg
14388 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
14389 if (return_type)
14390 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
14391 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
14392 if (!same_type_p (args[i], TREE_VALUE (sarg)))
14393 return unify_type_mismatch (explain_p, args[i],
14394 TREE_VALUE (sarg));
14395 }
14396 }
14397
14398 return result;
14399 }
14400
14401 /* Adjust types before performing type deduction, as described in
14402 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
14403 sections are symmetric. PARM is the type of a function parameter
14404 or the return type of the conversion function. ARG is the type of
14405 the argument passed to the call, or the type of the value
14406 initialized with the result of the conversion function.
14407 ARG_EXPR is the original argument expression, which may be null. */
14408
14409 static int
14410 maybe_adjust_types_for_deduction (unification_kind_t strict,
14411 tree* parm,
14412 tree* arg,
14413 tree arg_expr)
14414 {
14415 int result = 0;
14416
14417 switch (strict)
14418 {
14419 case DEDUCE_CALL:
14420 break;
14421
14422 case DEDUCE_CONV:
14423 {
14424 /* Swap PARM and ARG throughout the remainder of this
14425 function; the handling is precisely symmetric since PARM
14426 will initialize ARG rather than vice versa. */
14427 tree* temp = parm;
14428 parm = arg;
14429 arg = temp;
14430 break;
14431 }
14432
14433 case DEDUCE_EXACT:
14434 /* Core issue #873: Do the DR606 thing (see below) for these cases,
14435 too, but here handle it by stripping the reference from PARM
14436 rather than by adding it to ARG. */
14437 if (TREE_CODE (*parm) == REFERENCE_TYPE
14438 && TYPE_REF_IS_RVALUE (*parm)
14439 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
14440 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
14441 && TREE_CODE (*arg) == REFERENCE_TYPE
14442 && !TYPE_REF_IS_RVALUE (*arg))
14443 *parm = TREE_TYPE (*parm);
14444 /* Nothing else to do in this case. */
14445 return 0;
14446
14447 default:
14448 gcc_unreachable ();
14449 }
14450
14451 if (TREE_CODE (*parm) != REFERENCE_TYPE)
14452 {
14453 /* [temp.deduct.call]
14454
14455 If P is not a reference type:
14456
14457 --If A is an array type, the pointer type produced by the
14458 array-to-pointer standard conversion (_conv.array_) is
14459 used in place of A for type deduction; otherwise,
14460
14461 --If A is a function type, the pointer type produced by
14462 the function-to-pointer standard conversion
14463 (_conv.func_) is used in place of A for type deduction;
14464 otherwise,
14465
14466 --If A is a cv-qualified type, the top level
14467 cv-qualifiers of A's type are ignored for type
14468 deduction. */
14469 if (TREE_CODE (*arg) == ARRAY_TYPE)
14470 *arg = build_pointer_type (TREE_TYPE (*arg));
14471 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
14472 *arg = build_pointer_type (*arg);
14473 else
14474 *arg = TYPE_MAIN_VARIANT (*arg);
14475 }
14476
14477 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
14478 of the form T&&, where T is a template parameter, and the argument
14479 is an lvalue, T is deduced as A& */
14480 if (TREE_CODE (*parm) == REFERENCE_TYPE
14481 && TYPE_REF_IS_RVALUE (*parm)
14482 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
14483 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
14484 && (arg_expr ? real_lvalue_p (arg_expr)
14485 /* try_one_overload doesn't provide an arg_expr, but
14486 functions are always lvalues. */
14487 : TREE_CODE (*arg) == FUNCTION_TYPE))
14488 *arg = build_reference_type (*arg);
14489
14490 /* [temp.deduct.call]
14491
14492 If P is a cv-qualified type, the top level cv-qualifiers
14493 of P's type are ignored for type deduction. If P is a
14494 reference type, the type referred to by P is used for
14495 type deduction. */
14496 *parm = TYPE_MAIN_VARIANT (*parm);
14497 if (TREE_CODE (*parm) == REFERENCE_TYPE)
14498 {
14499 *parm = TREE_TYPE (*parm);
14500 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14501 }
14502
14503 /* DR 322. For conversion deduction, remove a reference type on parm
14504 too (which has been swapped into ARG). */
14505 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
14506 *arg = TREE_TYPE (*arg);
14507
14508 return result;
14509 }
14510
14511 /* Most parms like fn_type_unification.
14512
14513 If SUBR is 1, we're being called recursively (to unify the
14514 arguments of a function or method parameter of a function
14515 template). */
14516
14517 static int
14518 type_unification_real (tree tparms,
14519 tree targs,
14520 tree xparms,
14521 const tree *xargs,
14522 unsigned int xnargs,
14523 int subr,
14524 unification_kind_t strict,
14525 int flags,
14526 bool explain_p)
14527 {
14528 tree parm, arg, arg_expr;
14529 int i;
14530 int ntparms = TREE_VEC_LENGTH (tparms);
14531 int sub_strict;
14532 int saw_undeduced = 0;
14533 tree parms;
14534 const tree *args;
14535 unsigned int nargs;
14536 unsigned int ia;
14537
14538 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
14539 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
14540 gcc_assert (ntparms > 0);
14541
14542 /* Reset the number of non-defaulted template arguments contained
14543 in TARGS. */
14544 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
14545
14546 switch (strict)
14547 {
14548 case DEDUCE_CALL:
14549 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
14550 | UNIFY_ALLOW_DERIVED);
14551 break;
14552
14553 case DEDUCE_CONV:
14554 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14555 break;
14556
14557 case DEDUCE_EXACT:
14558 sub_strict = UNIFY_ALLOW_NONE;
14559 break;
14560
14561 default:
14562 gcc_unreachable ();
14563 }
14564
14565 again:
14566 parms = xparms;
14567 args = xargs;
14568 nargs = xnargs;
14569
14570 ia = 0;
14571 while (parms && parms != void_list_node
14572 && ia < nargs)
14573 {
14574 parm = TREE_VALUE (parms);
14575
14576 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
14577 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
14578 /* For a function parameter pack that occurs at the end of the
14579 parameter-declaration-list, the type A of each remaining
14580 argument of the call is compared with the type P of the
14581 declarator-id of the function parameter pack. */
14582 break;
14583
14584 parms = TREE_CHAIN (parms);
14585
14586 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
14587 /* For a function parameter pack that does not occur at the
14588 end of the parameter-declaration-list, the type of the
14589 parameter pack is a non-deduced context. */
14590 continue;
14591
14592 arg = args[ia];
14593 ++ia;
14594 arg_expr = NULL;
14595
14596 if (arg == error_mark_node)
14597 return unify_invalid (explain_p);
14598 if (arg == unknown_type_node)
14599 /* We can't deduce anything from this, but we might get all the
14600 template args from other function args. */
14601 continue;
14602
14603 /* Conversions will be performed on a function argument that
14604 corresponds with a function parameter that contains only
14605 non-deducible template parameters and explicitly specified
14606 template parameters. */
14607 if (!uses_template_parms (parm))
14608 {
14609 tree type;
14610
14611 if (!TYPE_P (arg))
14612 type = TREE_TYPE (arg);
14613 else
14614 type = arg;
14615
14616 if (same_type_p (parm, type))
14617 continue;
14618 if (strict != DEDUCE_EXACT
14619 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
14620 flags))
14621 continue;
14622
14623 if (strict == DEDUCE_EXACT)
14624 return unify_type_mismatch (explain_p, parm, arg);
14625 else
14626 return unify_arg_conversion (explain_p, parm, type, arg);
14627 }
14628
14629 if (!TYPE_P (arg))
14630 {
14631 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14632 if (type_unknown_p (arg))
14633 {
14634 /* [temp.deduct.type]
14635
14636 A template-argument can be deduced from a pointer to
14637 function or pointer to member function argument if
14638 the set of overloaded functions does not contain
14639 function templates and at most one of a set of
14640 overloaded functions provides a unique match. */
14641 if (resolve_overloaded_unification
14642 (tparms, targs, parm, arg, strict, sub_strict, explain_p))
14643 continue;
14644
14645 return unify_overload_resolution_failure (explain_p, arg);
14646 }
14647 arg_expr = arg;
14648 arg = unlowered_expr_type (arg);
14649 if (arg == error_mark_node)
14650 return unify_invalid (explain_p);
14651 }
14652
14653 {
14654 int arg_strict = sub_strict;
14655
14656 if (!subr)
14657 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
14658 arg_expr);
14659
14660 if (arg == init_list_type_node && arg_expr)
14661 arg = arg_expr;
14662 if (unify (tparms, targs, parm, arg, arg_strict, explain_p))
14663 /* If unification failed, the recursive call will have updated
14664 UI appropriately. */
14665 return 1;
14666 }
14667 }
14668
14669
14670 if (parms
14671 && parms != void_list_node
14672 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
14673 {
14674 /* Unify the remaining arguments with the pack expansion type. */
14675 tree argvec;
14676 tree parmvec = make_tree_vec (1);
14677
14678 /* Allocate a TREE_VEC and copy in all of the arguments */
14679 argvec = make_tree_vec (nargs - ia);
14680 for (i = 0; ia < nargs; ++ia, ++i)
14681 TREE_VEC_ELT (argvec, i) = args[ia];
14682
14683 /* Copy the parameter into parmvec. */
14684 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
14685 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
14686 /*call_args_p=*/true, /*subr=*/subr, explain_p))
14687 return 1;
14688
14689 /* Advance to the end of the list of parameters. */
14690 parms = TREE_CHAIN (parms);
14691 }
14692
14693 /* Fail if we've reached the end of the parm list, and more args
14694 are present, and the parm list isn't variadic. */
14695 if (ia < nargs && parms == void_list_node)
14696 return unify_too_many_arguments (explain_p, nargs, ia);
14697 /* Fail if parms are left and they don't have default values. */
14698 if (parms && parms != void_list_node
14699 && TREE_PURPOSE (parms) == NULL_TREE)
14700 {
14701 unsigned int count = nargs;
14702 tree p = parms;
14703 while (p && p != void_list_node)
14704 {
14705 count++;
14706 p = TREE_CHAIN (p);
14707 }
14708 return unify_too_few_arguments (explain_p, ia, count);
14709 }
14710
14711 if (!subr)
14712 {
14713 /* Check to see if we need another pass before we start clearing
14714 ARGUMENT_PACK_INCOMPLETE_P. */
14715 for (i = 0; i < ntparms; i++)
14716 {
14717 tree targ = TREE_VEC_ELT (targs, i);
14718 tree tparm = TREE_VEC_ELT (tparms, i);
14719
14720 if (targ || tparm == error_mark_node)
14721 continue;
14722 tparm = TREE_VALUE (tparm);
14723
14724 /* If this is an undeduced nontype parameter that depends on
14725 a type parameter, try another pass; its type may have been
14726 deduced from a later argument than the one from which
14727 this parameter can be deduced. */
14728 if (TREE_CODE (tparm) == PARM_DECL
14729 && uses_template_parms (TREE_TYPE (tparm))
14730 && !saw_undeduced++)
14731 goto again;
14732 }
14733
14734 for (i = 0; i < ntparms; i++)
14735 {
14736 tree targ = TREE_VEC_ELT (targs, i);
14737 tree tparm = TREE_VEC_ELT (tparms, i);
14738
14739 /* Clear the "incomplete" flags on all argument packs now so that
14740 substituting them into later default arguments works. */
14741 if (targ && ARGUMENT_PACK_P (targ))
14742 {
14743 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
14744 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
14745 }
14746
14747 if (targ || tparm == error_mark_node)
14748 continue;
14749 tparm = TREE_VALUE (tparm);
14750
14751 /* Core issue #226 (C++0x) [temp.deduct]:
14752
14753 If a template argument has not been deduced, its
14754 default template argument, if any, is used.
14755
14756 When we are in C++98 mode, TREE_PURPOSE will either
14757 be NULL_TREE or ERROR_MARK_NODE, so we do not need
14758 to explicitly check cxx_dialect here. */
14759 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
14760 {
14761 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
14762 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
14763 arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
14764 arg = convert_template_argument (parm, arg, targs,
14765 (explain_p
14766 ? tf_warning_or_error
14767 : tf_none),
14768 i, NULL_TREE);
14769 if (arg == error_mark_node)
14770 return 1;
14771 else
14772 {
14773 TREE_VEC_ELT (targs, i) = arg;
14774 /* The position of the first default template argument,
14775 is also the number of non-defaulted arguments in TARGS.
14776 Record that. */
14777 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
14778 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
14779 continue;
14780 }
14781 }
14782
14783 /* If the type parameter is a parameter pack, then it will
14784 be deduced to an empty parameter pack. */
14785 if (template_parameter_pack_p (tparm))
14786 {
14787 tree arg;
14788
14789 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
14790 {
14791 arg = make_node (NONTYPE_ARGUMENT_PACK);
14792 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
14793 TREE_CONSTANT (arg) = 1;
14794 }
14795 else
14796 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
14797
14798 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
14799
14800 TREE_VEC_ELT (targs, i) = arg;
14801 continue;
14802 }
14803
14804 return unify_parameter_deduction_failure (explain_p, tparm);
14805 }
14806 }
14807 #ifdef ENABLE_CHECKING
14808 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
14809 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
14810 #endif
14811
14812 return unify_success (explain_p);
14813 }
14814
14815 /* Subroutine of type_unification_real. Args are like the variables
14816 at the call site. ARG is an overloaded function (or template-id);
14817 we try deducing template args from each of the overloads, and if
14818 only one succeeds, we go with that. Modifies TARGS and returns
14819 true on success. */
14820
14821 static bool
14822 resolve_overloaded_unification (tree tparms,
14823 tree targs,
14824 tree parm,
14825 tree arg,
14826 unification_kind_t strict,
14827 int sub_strict,
14828 bool explain_p)
14829 {
14830 tree tempargs = copy_node (targs);
14831 int good = 0;
14832 tree goodfn = NULL_TREE;
14833 bool addr_p;
14834
14835 if (TREE_CODE (arg) == ADDR_EXPR)
14836 {
14837 arg = TREE_OPERAND (arg, 0);
14838 addr_p = true;
14839 }
14840 else
14841 addr_p = false;
14842
14843 if (TREE_CODE (arg) == COMPONENT_REF)
14844 /* Handle `&x' where `x' is some static or non-static member
14845 function name. */
14846 arg = TREE_OPERAND (arg, 1);
14847
14848 if (TREE_CODE (arg) == OFFSET_REF)
14849 arg = TREE_OPERAND (arg, 1);
14850
14851 /* Strip baselink information. */
14852 if (BASELINK_P (arg))
14853 arg = BASELINK_FUNCTIONS (arg);
14854
14855 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
14856 {
14857 /* If we got some explicit template args, we need to plug them into
14858 the affected templates before we try to unify, in case the
14859 explicit args will completely resolve the templates in question. */
14860
14861 int ok = 0;
14862 tree expl_subargs = TREE_OPERAND (arg, 1);
14863 arg = TREE_OPERAND (arg, 0);
14864
14865 for (; arg; arg = OVL_NEXT (arg))
14866 {
14867 tree fn = OVL_CURRENT (arg);
14868 tree subargs, elem;
14869
14870 if (TREE_CODE (fn) != TEMPLATE_DECL)
14871 continue;
14872
14873 ++processing_template_decl;
14874 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
14875 expl_subargs, /*check_ret=*/false);
14876 if (subargs && !any_dependent_template_arguments_p (subargs))
14877 {
14878 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
14879 if (try_one_overload (tparms, targs, tempargs, parm,
14880 elem, strict, sub_strict, addr_p, explain_p)
14881 && (!goodfn || !decls_match (goodfn, elem)))
14882 {
14883 goodfn = elem;
14884 ++good;
14885 }
14886 }
14887 else if (subargs)
14888 ++ok;
14889 --processing_template_decl;
14890 }
14891 /* If no templates (or more than one) are fully resolved by the
14892 explicit arguments, this template-id is a non-deduced context; it
14893 could still be OK if we deduce all template arguments for the
14894 enclosing call through other arguments. */
14895 if (good != 1)
14896 good = ok;
14897 }
14898 else if (TREE_CODE (arg) != OVERLOAD
14899 && TREE_CODE (arg) != FUNCTION_DECL)
14900 /* If ARG is, for example, "(0, &f)" then its type will be unknown
14901 -- but the deduction does not succeed because the expression is
14902 not just the function on its own. */
14903 return false;
14904 else
14905 for (; arg; arg = OVL_NEXT (arg))
14906 if (try_one_overload (tparms, targs, tempargs, parm,
14907 TREE_TYPE (OVL_CURRENT (arg)),
14908 strict, sub_strict, addr_p, explain_p)
14909 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
14910 {
14911 goodfn = OVL_CURRENT (arg);
14912 ++good;
14913 }
14914
14915 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14916 to function or pointer to member function argument if the set of
14917 overloaded functions does not contain function templates and at most
14918 one of a set of overloaded functions provides a unique match.
14919
14920 So if we found multiple possibilities, we return success but don't
14921 deduce anything. */
14922
14923 if (good == 1)
14924 {
14925 int i = TREE_VEC_LENGTH (targs);
14926 for (; i--; )
14927 if (TREE_VEC_ELT (tempargs, i))
14928 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
14929 }
14930 if (good)
14931 return true;
14932
14933 return false;
14934 }
14935
14936 /* Core DR 115: In contexts where deduction is done and fails, or in
14937 contexts where deduction is not done, if a template argument list is
14938 specified and it, along with any default template arguments, identifies
14939 a single function template specialization, then the template-id is an
14940 lvalue for the function template specialization. */
14941
14942 tree
14943 resolve_nondeduced_context (tree orig_expr)
14944 {
14945 tree expr, offset, baselink;
14946 bool addr;
14947
14948 if (!type_unknown_p (orig_expr))
14949 return orig_expr;
14950
14951 expr = orig_expr;
14952 addr = false;
14953 offset = NULL_TREE;
14954 baselink = NULL_TREE;
14955
14956 if (TREE_CODE (expr) == ADDR_EXPR)
14957 {
14958 expr = TREE_OPERAND (expr, 0);
14959 addr = true;
14960 }
14961 if (TREE_CODE (expr) == OFFSET_REF)
14962 {
14963 offset = expr;
14964 expr = TREE_OPERAND (expr, 1);
14965 }
14966 if (TREE_CODE (expr) == BASELINK)
14967 {
14968 baselink = expr;
14969 expr = BASELINK_FUNCTIONS (expr);
14970 }
14971
14972 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
14973 {
14974 int good = 0;
14975 tree goodfn = NULL_TREE;
14976
14977 /* If we got some explicit template args, we need to plug them into
14978 the affected templates before we try to unify, in case the
14979 explicit args will completely resolve the templates in question. */
14980
14981 tree expl_subargs = TREE_OPERAND (expr, 1);
14982 tree arg = TREE_OPERAND (expr, 0);
14983 tree badfn = NULL_TREE;
14984 tree badargs = NULL_TREE;
14985
14986 for (; arg; arg = OVL_NEXT (arg))
14987 {
14988 tree fn = OVL_CURRENT (arg);
14989 tree subargs, elem;
14990
14991 if (TREE_CODE (fn) != TEMPLATE_DECL)
14992 continue;
14993
14994 ++processing_template_decl;
14995 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
14996 expl_subargs, /*check_ret=*/false);
14997 if (subargs && !any_dependent_template_arguments_p (subargs))
14998 {
14999 elem = instantiate_template (fn, subargs, tf_none);
15000 if (elem == error_mark_node)
15001 {
15002 badfn = fn;
15003 badargs = subargs;
15004 }
15005 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
15006 {
15007 goodfn = elem;
15008 ++good;
15009 }
15010 }
15011 --processing_template_decl;
15012 }
15013 if (good == 1)
15014 {
15015 mark_used (goodfn);
15016 expr = goodfn;
15017 if (baselink)
15018 expr = build_baselink (BASELINK_BINFO (baselink),
15019 BASELINK_ACCESS_BINFO (baselink),
15020 expr, BASELINK_OPTYPE (baselink));
15021 if (offset)
15022 {
15023 tree base
15024 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
15025 expr = build_offset_ref (base, expr, addr);
15026 }
15027 if (addr)
15028 expr = cp_build_addr_expr (expr, tf_warning_or_error);
15029 return expr;
15030 }
15031 else if (good == 0 && badargs)
15032 /* There were no good options and at least one bad one, so let the
15033 user know what the problem is. */
15034 instantiate_template (badfn, badargs, tf_warning_or_error);
15035 }
15036 return orig_expr;
15037 }
15038
15039 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15040 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15041 different overloads deduce different arguments for a given parm.
15042 ADDR_P is true if the expression for which deduction is being
15043 performed was of the form "& fn" rather than simply "fn".
15044
15045 Returns 1 on success. */
15046
15047 static int
15048 try_one_overload (tree tparms,
15049 tree orig_targs,
15050 tree targs,
15051 tree parm,
15052 tree arg,
15053 unification_kind_t strict,
15054 int sub_strict,
15055 bool addr_p,
15056 bool explain_p)
15057 {
15058 int nargs;
15059 tree tempargs;
15060 int i;
15061
15062 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15063 to function or pointer to member function argument if the set of
15064 overloaded functions does not contain function templates and at most
15065 one of a set of overloaded functions provides a unique match.
15066
15067 So if this is a template, just return success. */
15068
15069 if (uses_template_parms (arg))
15070 return 1;
15071
15072 if (TREE_CODE (arg) == METHOD_TYPE)
15073 arg = build_ptrmemfunc_type (build_pointer_type (arg));
15074 else if (addr_p)
15075 arg = build_pointer_type (arg);
15076
15077 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
15078
15079 /* We don't copy orig_targs for this because if we have already deduced
15080 some template args from previous args, unify would complain when we
15081 try to deduce a template parameter for the same argument, even though
15082 there isn't really a conflict. */
15083 nargs = TREE_VEC_LENGTH (targs);
15084 tempargs = make_tree_vec (nargs);
15085
15086 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
15087 return 0;
15088
15089 /* First make sure we didn't deduce anything that conflicts with
15090 explicitly specified args. */
15091 for (i = nargs; i--; )
15092 {
15093 tree elt = TREE_VEC_ELT (tempargs, i);
15094 tree oldelt = TREE_VEC_ELT (orig_targs, i);
15095
15096 if (!elt)
15097 /*NOP*/;
15098 else if (uses_template_parms (elt))
15099 /* Since we're unifying against ourselves, we will fill in
15100 template args used in the function parm list with our own
15101 template parms. Discard them. */
15102 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
15103 else if (oldelt && !template_args_equal (oldelt, elt))
15104 return 0;
15105 }
15106
15107 for (i = nargs; i--; )
15108 {
15109 tree elt = TREE_VEC_ELT (tempargs, i);
15110
15111 if (elt)
15112 TREE_VEC_ELT (targs, i) = elt;
15113 }
15114
15115 return 1;
15116 }
15117
15118 /* PARM is a template class (perhaps with unbound template
15119 parameters). ARG is a fully instantiated type. If ARG can be
15120 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15121 TARGS are as for unify. */
15122
15123 static tree
15124 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
15125 bool explain_p)
15126 {
15127 tree copy_of_targs;
15128
15129 if (!CLASSTYPE_TEMPLATE_INFO (arg)
15130 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
15131 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
15132 return NULL_TREE;
15133
15134 /* We need to make a new template argument vector for the call to
15135 unify. If we used TARGS, we'd clutter it up with the result of
15136 the attempted unification, even if this class didn't work out.
15137 We also don't want to commit ourselves to all the unifications
15138 we've already done, since unification is supposed to be done on
15139 an argument-by-argument basis. In other words, consider the
15140 following pathological case:
15141
15142 template <int I, int J, int K>
15143 struct S {};
15144
15145 template <int I, int J>
15146 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15147
15148 template <int I, int J, int K>
15149 void f(S<I, J, K>, S<I, I, I>);
15150
15151 void g() {
15152 S<0, 0, 0> s0;
15153 S<0, 1, 2> s2;
15154
15155 f(s0, s2);
15156 }
15157
15158 Now, by the time we consider the unification involving `s2', we
15159 already know that we must have `f<0, 0, 0>'. But, even though
15160 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15161 because there are two ways to unify base classes of S<0, 1, 2>
15162 with S<I, I, I>. If we kept the already deduced knowledge, we
15163 would reject the possibility I=1. */
15164 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
15165
15166 /* If unification failed, we're done. */
15167 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
15168 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
15169 return NULL_TREE;
15170
15171 return arg;
15172 }
15173
15174 /* Given a template type PARM and a class type ARG, find the unique
15175 base type in ARG that is an instance of PARM. We do not examine
15176 ARG itself; only its base-classes. If there is not exactly one
15177 appropriate base class, return NULL_TREE. PARM may be the type of
15178 a partial specialization, as well as a plain template type. Used
15179 by unify. */
15180
15181 static enum template_base_result
15182 get_template_base (tree tparms, tree targs, tree parm, tree arg,
15183 bool explain_p, tree *result)
15184 {
15185 tree rval = NULL_TREE;
15186 tree binfo;
15187
15188 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
15189
15190 binfo = TYPE_BINFO (complete_type (arg));
15191 if (!binfo)
15192 {
15193 /* The type could not be completed. */
15194 *result = NULL_TREE;
15195 return tbr_incomplete_type;
15196 }
15197
15198 /* Walk in inheritance graph order. The search order is not
15199 important, and this avoids multiple walks of virtual bases. */
15200 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
15201 {
15202 tree r = try_class_unification (tparms, targs, parm,
15203 BINFO_TYPE (binfo), explain_p);
15204
15205 if (r)
15206 {
15207 /* If there is more than one satisfactory baseclass, then:
15208
15209 [temp.deduct.call]
15210
15211 If they yield more than one possible deduced A, the type
15212 deduction fails.
15213
15214 applies. */
15215 if (rval && !same_type_p (r, rval))
15216 {
15217 *result = NULL_TREE;
15218 return tbr_ambiguous_baseclass;
15219 }
15220
15221 rval = r;
15222 }
15223 }
15224
15225 *result = rval;
15226 return tbr_success;
15227 }
15228
15229 /* Returns the level of DECL, which declares a template parameter. */
15230
15231 static int
15232 template_decl_level (tree decl)
15233 {
15234 switch (TREE_CODE (decl))
15235 {
15236 case TYPE_DECL:
15237 case TEMPLATE_DECL:
15238 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
15239
15240 case PARM_DECL:
15241 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
15242
15243 default:
15244 gcc_unreachable ();
15245 }
15246 return 0;
15247 }
15248
15249 /* Decide whether ARG can be unified with PARM, considering only the
15250 cv-qualifiers of each type, given STRICT as documented for unify.
15251 Returns nonzero iff the unification is OK on that basis. */
15252
15253 static int
15254 check_cv_quals_for_unify (int strict, tree arg, tree parm)
15255 {
15256 int arg_quals = cp_type_quals (arg);
15257 int parm_quals = cp_type_quals (parm);
15258
15259 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15260 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
15261 {
15262 /* Although a CVR qualifier is ignored when being applied to a
15263 substituted template parameter ([8.3.2]/1 for example), that
15264 does not allow us to unify "const T" with "int&" because both
15265 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
15266 It is ok when we're allowing additional CV qualifiers
15267 at the outer level [14.8.2.1]/3,1st bullet. */
15268 if ((TREE_CODE (arg) == REFERENCE_TYPE
15269 || TREE_CODE (arg) == FUNCTION_TYPE
15270 || TREE_CODE (arg) == METHOD_TYPE)
15271 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
15272 return 0;
15273
15274 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
15275 && (parm_quals & TYPE_QUAL_RESTRICT))
15276 return 0;
15277 }
15278
15279 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
15280 && (arg_quals & parm_quals) != parm_quals)
15281 return 0;
15282
15283 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
15284 && (parm_quals & arg_quals) != arg_quals)
15285 return 0;
15286
15287 return 1;
15288 }
15289
15290 /* Determines the LEVEL and INDEX for the template parameter PARM. */
15291 void
15292 template_parm_level_and_index (tree parm, int* level, int* index)
15293 {
15294 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15295 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
15296 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
15297 {
15298 *index = TEMPLATE_TYPE_IDX (parm);
15299 *level = TEMPLATE_TYPE_LEVEL (parm);
15300 }
15301 else
15302 {
15303 *index = TEMPLATE_PARM_IDX (parm);
15304 *level = TEMPLATE_PARM_LEVEL (parm);
15305 }
15306 }
15307
15308 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
15309 do { \
15310 if (unify (TP, TA, P, A, S, EP)) \
15311 return 1; \
15312 } while (0);
15313
15314 /* Unifies the remaining arguments in PACKED_ARGS with the pack
15315 expansion at the end of PACKED_PARMS. Returns 0 if the type
15316 deduction succeeds, 1 otherwise. STRICT is the same as in
15317 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
15318 call argument list. We'll need to adjust the arguments to make them
15319 types. SUBR tells us if this is from a recursive call to
15320 type_unification_real. */
15321 static int
15322 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
15323 tree packed_args, int strict, bool call_args_p,
15324 bool subr, bool explain_p)
15325 {
15326 tree parm
15327 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
15328 tree pattern = PACK_EXPANSION_PATTERN (parm);
15329 tree pack, packs = NULL_TREE;
15330 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
15331 int len = TREE_VEC_LENGTH (packed_args);
15332
15333 /* Determine the parameter packs we will be deducing from the
15334 pattern, and record their current deductions. */
15335 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
15336 pack; pack = TREE_CHAIN (pack))
15337 {
15338 tree parm_pack = TREE_VALUE (pack);
15339 int idx, level;
15340
15341 /* Determine the index and level of this parameter pack. */
15342 template_parm_level_and_index (parm_pack, &level, &idx);
15343
15344 /* Keep track of the parameter packs and their corresponding
15345 argument packs. */
15346 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
15347 TREE_TYPE (packs) = make_tree_vec (len - start);
15348 }
15349
15350 /* Loop through all of the arguments that have not yet been
15351 unified and unify each with the pattern. */
15352 for (i = start; i < len; i++)
15353 {
15354 tree parm = pattern;
15355
15356 /* For each parameter pack, clear out the deduced value so that
15357 we can deduce it again. */
15358 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15359 {
15360 int idx, level;
15361 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15362
15363 TMPL_ARG (targs, level, idx) = NULL_TREE;
15364 }
15365
15366 /* Unify the pattern with the current argument. */
15367 {
15368 tree arg = TREE_VEC_ELT (packed_args, i);
15369 tree arg_expr = NULL_TREE;
15370 int arg_strict = strict;
15371 bool skip_arg_p = false;
15372
15373 if (call_args_p)
15374 {
15375 int sub_strict;
15376
15377 /* This mirrors what we do in type_unification_real. */
15378 switch (strict)
15379 {
15380 case DEDUCE_CALL:
15381 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
15382 | UNIFY_ALLOW_MORE_CV_QUAL
15383 | UNIFY_ALLOW_DERIVED);
15384 break;
15385
15386 case DEDUCE_CONV:
15387 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
15388 break;
15389
15390 case DEDUCE_EXACT:
15391 sub_strict = UNIFY_ALLOW_NONE;
15392 break;
15393
15394 default:
15395 gcc_unreachable ();
15396 }
15397
15398 if (!TYPE_P (arg))
15399 {
15400 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
15401 if (type_unknown_p (arg))
15402 {
15403 /* [temp.deduct.type] A template-argument can be
15404 deduced from a pointer to function or pointer
15405 to member function argument if the set of
15406 overloaded functions does not contain function
15407 templates and at most one of a set of
15408 overloaded functions provides a unique
15409 match. */
15410
15411 if (resolve_overloaded_unification
15412 (tparms, targs, parm, arg,
15413 (unification_kind_t) strict,
15414 sub_strict, explain_p)
15415 != 0)
15416 return 1;
15417 skip_arg_p = true;
15418 }
15419
15420 if (!skip_arg_p)
15421 {
15422 arg_expr = arg;
15423 arg = unlowered_expr_type (arg);
15424 if (arg == error_mark_node)
15425 return 1;
15426 }
15427 }
15428
15429 arg_strict = sub_strict;
15430
15431 if (!subr)
15432 arg_strict |=
15433 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
15434 &parm, &arg, arg_expr);
15435 }
15436
15437 if (!skip_arg_p)
15438 {
15439 /* For deduction from an init-list we need the actual list. */
15440 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
15441 arg = arg_expr;
15442 RECUR_AND_CHECK_FAILURE (tparms, targs, parm, arg, arg_strict,
15443 explain_p);
15444 }
15445 }
15446
15447 /* For each parameter pack, collect the deduced value. */
15448 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15449 {
15450 int idx, level;
15451 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15452
15453 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
15454 TMPL_ARG (targs, level, idx);
15455 }
15456 }
15457
15458 /* Verify that the results of unification with the parameter packs
15459 produce results consistent with what we've seen before, and make
15460 the deduced argument packs available. */
15461 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15462 {
15463 tree old_pack = TREE_VALUE (pack);
15464 tree new_args = TREE_TYPE (pack);
15465 int i, len = TREE_VEC_LENGTH (new_args);
15466 int idx, level;
15467 bool nondeduced_p = false;
15468
15469 /* By default keep the original deduced argument pack.
15470 If necessary, more specific code is going to update the
15471 resulting deduced argument later down in this function. */
15472 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15473 TMPL_ARG (targs, level, idx) = old_pack;
15474
15475 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
15476 actually deduce anything. */
15477 for (i = 0; i < len && !nondeduced_p; ++i)
15478 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
15479 nondeduced_p = true;
15480 if (nondeduced_p)
15481 continue;
15482
15483 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
15484 {
15485 /* Prepend the explicit arguments onto NEW_ARGS. */
15486 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
15487 tree old_args = new_args;
15488 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
15489 int len = explicit_len + TREE_VEC_LENGTH (old_args);
15490
15491 /* Copy the explicit arguments. */
15492 new_args = make_tree_vec (len);
15493 for (i = 0; i < explicit_len; i++)
15494 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
15495
15496 /* Copy the deduced arguments. */
15497 for (; i < len; i++)
15498 TREE_VEC_ELT (new_args, i) =
15499 TREE_VEC_ELT (old_args, i - explicit_len);
15500 }
15501
15502 if (!old_pack)
15503 {
15504 tree result;
15505 /* Build the deduced *_ARGUMENT_PACK. */
15506 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
15507 {
15508 result = make_node (NONTYPE_ARGUMENT_PACK);
15509 TREE_TYPE (result) =
15510 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
15511 TREE_CONSTANT (result) = 1;
15512 }
15513 else
15514 result = cxx_make_type (TYPE_ARGUMENT_PACK);
15515
15516 SET_ARGUMENT_PACK_ARGS (result, new_args);
15517
15518 /* Note the deduced argument packs for this parameter
15519 pack. */
15520 TMPL_ARG (targs, level, idx) = result;
15521 }
15522 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
15523 && (ARGUMENT_PACK_ARGS (old_pack)
15524 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
15525 {
15526 /* We only had the explicitly-provided arguments before, but
15527 now we have a complete set of arguments. */
15528 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
15529
15530 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
15531 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
15532 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
15533 }
15534 else
15535 {
15536 tree bad_old_arg, bad_new_arg;
15537 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
15538
15539 if (!comp_template_args_with_info (old_args, new_args,
15540 &bad_old_arg, &bad_new_arg))
15541 /* Inconsistent unification of this parameter pack. */
15542 return unify_parameter_pack_inconsistent (explain_p,
15543 bad_old_arg,
15544 bad_new_arg);
15545 }
15546 }
15547
15548 return unify_success (explain_p);
15549 }
15550
15551 /* Deduce the value of template parameters. TPARMS is the (innermost)
15552 set of template parameters to a template. TARGS is the bindings
15553 for those template parameters, as determined thus far; TARGS may
15554 include template arguments for outer levels of template parameters
15555 as well. PARM is a parameter to a template function, or a
15556 subcomponent of that parameter; ARG is the corresponding argument.
15557 This function attempts to match PARM with ARG in a manner
15558 consistent with the existing assignments in TARGS. If more values
15559 are deduced, then TARGS is updated.
15560
15561 Returns 0 if the type deduction succeeds, 1 otherwise. The
15562 parameter STRICT is a bitwise or of the following flags:
15563
15564 UNIFY_ALLOW_NONE:
15565 Require an exact match between PARM and ARG.
15566 UNIFY_ALLOW_MORE_CV_QUAL:
15567 Allow the deduced ARG to be more cv-qualified (by qualification
15568 conversion) than ARG.
15569 UNIFY_ALLOW_LESS_CV_QUAL:
15570 Allow the deduced ARG to be less cv-qualified than ARG.
15571 UNIFY_ALLOW_DERIVED:
15572 Allow the deduced ARG to be a template base class of ARG,
15573 or a pointer to a template base class of the type pointed to by
15574 ARG.
15575 UNIFY_ALLOW_INTEGER:
15576 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
15577 case for more information.
15578 UNIFY_ALLOW_OUTER_LEVEL:
15579 This is the outermost level of a deduction. Used to determine validity
15580 of qualification conversions. A valid qualification conversion must
15581 have const qualified pointers leading up to the inner type which
15582 requires additional CV quals, except at the outer level, where const
15583 is not required [conv.qual]. It would be normal to set this flag in
15584 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
15585 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
15586 This is the outermost level of a deduction, and PARM can be more CV
15587 qualified at this point.
15588 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
15589 This is the outermost level of a deduction, and PARM can be less CV
15590 qualified at this point. */
15591
15592 static int
15593 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
15594 bool explain_p)
15595 {
15596 int idx;
15597 tree targ;
15598 tree tparm;
15599 int strict_in = strict;
15600
15601 /* I don't think this will do the right thing with respect to types.
15602 But the only case I've seen it in so far has been array bounds, where
15603 signedness is the only information lost, and I think that will be
15604 okay. */
15605 while (TREE_CODE (parm) == NOP_EXPR)
15606 parm = TREE_OPERAND (parm, 0);
15607
15608 if (arg == error_mark_node)
15609 return unify_invalid (explain_p);
15610 if (arg == unknown_type_node
15611 || arg == init_list_type_node)
15612 /* We can't deduce anything from this, but we might get all the
15613 template args from other function args. */
15614 return unify_success (explain_p);
15615
15616 /* If PARM uses template parameters, then we can't bail out here,
15617 even if ARG == PARM, since we won't record unifications for the
15618 template parameters. We might need them if we're trying to
15619 figure out which of two things is more specialized. */
15620 if (arg == parm && !uses_template_parms (parm))
15621 return unify_success (explain_p);
15622
15623 /* Handle init lists early, so the rest of the function can assume
15624 we're dealing with a type. */
15625 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
15626 {
15627 tree elt, elttype;
15628 unsigned i;
15629 tree orig_parm = parm;
15630
15631 /* Replace T with std::initializer_list<T> for deduction. */
15632 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15633 && flag_deduce_init_list)
15634 parm = listify (parm);
15635
15636 if (!is_std_init_list (parm))
15637 /* We can only deduce from an initializer list argument if the
15638 parameter is std::initializer_list; otherwise this is a
15639 non-deduced context. */
15640 return unify_success (explain_p);
15641
15642 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
15643
15644 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
15645 {
15646 int elt_strict = strict;
15647
15648 if (elt == error_mark_node)
15649 return unify_invalid (explain_p);
15650
15651 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
15652 {
15653 tree type = TREE_TYPE (elt);
15654 /* It should only be possible to get here for a call. */
15655 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
15656 elt_strict |= maybe_adjust_types_for_deduction
15657 (DEDUCE_CALL, &elttype, &type, elt);
15658 elt = type;
15659 }
15660
15661 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
15662 explain_p);
15663 }
15664
15665 /* If the std::initializer_list<T> deduction worked, replace the
15666 deduced A with std::initializer_list<A>. */
15667 if (orig_parm != parm)
15668 {
15669 idx = TEMPLATE_TYPE_IDX (orig_parm);
15670 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15671 targ = listify (targ);
15672 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
15673 }
15674 return unify_success (explain_p);
15675 }
15676
15677 /* Immediately reject some pairs that won't unify because of
15678 cv-qualification mismatches. */
15679 if (TREE_CODE (arg) == TREE_CODE (parm)
15680 && TYPE_P (arg)
15681 /* It is the elements of the array which hold the cv quals of an array
15682 type, and the elements might be template type parms. We'll check
15683 when we recurse. */
15684 && TREE_CODE (arg) != ARRAY_TYPE
15685 /* We check the cv-qualifiers when unifying with template type
15686 parameters below. We want to allow ARG `const T' to unify with
15687 PARM `T' for example, when computing which of two templates
15688 is more specialized, for example. */
15689 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
15690 && !check_cv_quals_for_unify (strict_in, arg, parm))
15691 return unify_cv_qual_mismatch (explain_p, parm, arg);
15692
15693 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
15694 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
15695 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
15696 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
15697 strict &= ~UNIFY_ALLOW_DERIVED;
15698 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15699 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
15700
15701 switch (TREE_CODE (parm))
15702 {
15703 case TYPENAME_TYPE:
15704 case SCOPE_REF:
15705 case UNBOUND_CLASS_TEMPLATE:
15706 /* In a type which contains a nested-name-specifier, template
15707 argument values cannot be deduced for template parameters used
15708 within the nested-name-specifier. */
15709 return unify_success (explain_p);
15710
15711 case TEMPLATE_TYPE_PARM:
15712 case TEMPLATE_TEMPLATE_PARM:
15713 case BOUND_TEMPLATE_TEMPLATE_PARM:
15714 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
15715 if (tparm == error_mark_node)
15716 return unify_invalid (explain_p);
15717
15718 if (TEMPLATE_TYPE_LEVEL (parm)
15719 != template_decl_level (tparm))
15720 /* The PARM is not one we're trying to unify. Just check
15721 to see if it matches ARG. */
15722 {
15723 if (TREE_CODE (arg) == TREE_CODE (parm)
15724 && same_type_p (parm, arg))
15725 return unify_success (explain_p);
15726 else
15727 return unify_type_mismatch (explain_p, parm, arg);
15728 }
15729 idx = TEMPLATE_TYPE_IDX (parm);
15730 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15731 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
15732
15733 /* Check for mixed types and values. */
15734 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15735 && TREE_CODE (tparm) != TYPE_DECL)
15736 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
15737 && TREE_CODE (tparm) != TEMPLATE_DECL))
15738 gcc_unreachable ();
15739
15740 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
15741 {
15742 /* ARG must be constructed from a template class or a template
15743 template parameter. */
15744 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
15745 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
15746 return unify_template_deduction_failure (explain_p, parm, arg);
15747
15748 {
15749 tree parmvec = TYPE_TI_ARGS (parm);
15750 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
15751 tree full_argvec = add_to_template_args (targs, argvec);
15752 tree parm_parms
15753 = DECL_INNERMOST_TEMPLATE_PARMS
15754 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
15755 int i, len;
15756 int parm_variadic_p = 0;
15757
15758 /* The resolution to DR150 makes clear that default
15759 arguments for an N-argument may not be used to bind T
15760 to a template template parameter with fewer than N
15761 parameters. It is not safe to permit the binding of
15762 default arguments as an extension, as that may change
15763 the meaning of a conforming program. Consider:
15764
15765 struct Dense { static const unsigned int dim = 1; };
15766
15767 template <template <typename> class View,
15768 typename Block>
15769 void operator+(float, View<Block> const&);
15770
15771 template <typename Block,
15772 unsigned int Dim = Block::dim>
15773 struct Lvalue_proxy { operator float() const; };
15774
15775 void
15776 test_1d (void) {
15777 Lvalue_proxy<Dense> p;
15778 float b;
15779 b + p;
15780 }
15781
15782 Here, if Lvalue_proxy is permitted to bind to View, then
15783 the global operator+ will be used; if they are not, the
15784 Lvalue_proxy will be converted to float. */
15785 if (coerce_template_parms (parm_parms,
15786 full_argvec,
15787 TYPE_TI_TEMPLATE (parm),
15788 (explain_p
15789 ? tf_warning_or_error
15790 : tf_none),
15791 /*require_all_args=*/true,
15792 /*use_default_args=*/false)
15793 == error_mark_node)
15794 return 1;
15795
15796 /* Deduce arguments T, i from TT<T> or TT<i>.
15797 We check each element of PARMVEC and ARGVEC individually
15798 rather than the whole TREE_VEC since they can have
15799 different number of elements. */
15800
15801 parmvec = expand_template_argument_pack (parmvec);
15802 argvec = expand_template_argument_pack (argvec);
15803
15804 len = TREE_VEC_LENGTH (parmvec);
15805
15806 /* Check if the parameters end in a pack, making them
15807 variadic. */
15808 if (len > 0
15809 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
15810 parm_variadic_p = 1;
15811
15812 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
15813 return unify_too_few_arguments (explain_p,
15814 TREE_VEC_LENGTH (argvec), len);
15815
15816 for (i = 0; i < len - parm_variadic_p; ++i)
15817 {
15818 RECUR_AND_CHECK_FAILURE (tparms, targs,
15819 TREE_VEC_ELT (parmvec, i),
15820 TREE_VEC_ELT (argvec, i),
15821 UNIFY_ALLOW_NONE, explain_p);
15822 }
15823
15824 if (parm_variadic_p
15825 && unify_pack_expansion (tparms, targs,
15826 parmvec, argvec,
15827 UNIFY_ALLOW_NONE,
15828 /*call_args_p=*/false,
15829 /*subr=*/false, explain_p))
15830 return 1;
15831 }
15832 arg = TYPE_TI_TEMPLATE (arg);
15833
15834 /* Fall through to deduce template name. */
15835 }
15836
15837 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
15838 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
15839 {
15840 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
15841
15842 /* Simple cases: Value already set, does match or doesn't. */
15843 if (targ != NULL_TREE && template_args_equal (targ, arg))
15844 return unify_success (explain_p);
15845 else if (targ)
15846 return unify_inconsistency (explain_p, parm, targ, arg);
15847 }
15848 else
15849 {
15850 /* If PARM is `const T' and ARG is only `int', we don't have
15851 a match unless we are allowing additional qualification.
15852 If ARG is `const int' and PARM is just `T' that's OK;
15853 that binds `const int' to `T'. */
15854 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
15855 arg, parm))
15856 return unify_cv_qual_mismatch (explain_p, parm, arg);
15857
15858 /* Consider the case where ARG is `const volatile int' and
15859 PARM is `const T'. Then, T should be `volatile int'. */
15860 arg = cp_build_qualified_type_real
15861 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
15862 if (arg == error_mark_node)
15863 return unify_invalid (explain_p);
15864
15865 /* Simple cases: Value already set, does match or doesn't. */
15866 if (targ != NULL_TREE && same_type_p (targ, arg))
15867 return unify_success (explain_p);
15868 else if (targ)
15869 return unify_inconsistency (explain_p, parm, targ, arg);
15870
15871 /* Make sure that ARG is not a variable-sized array. (Note
15872 that were talking about variable-sized arrays (like
15873 `int[n]'), rather than arrays of unknown size (like
15874 `int[]').) We'll get very confused by such a type since
15875 the bound of the array will not be computable in an
15876 instantiation. Besides, such types are not allowed in
15877 ISO C++, so we can do as we please here. */
15878 if (variably_modified_type_p (arg, NULL_TREE))
15879 return unify_vla_arg (explain_p, arg);
15880
15881 /* Strip typedefs as in convert_template_argument. */
15882 arg = canonicalize_type_argument (arg, tf_none);
15883 }
15884
15885 /* If ARG is a parameter pack or an expansion, we cannot unify
15886 against it unless PARM is also a parameter pack. */
15887 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
15888 && !template_parameter_pack_p (parm))
15889 return unify_parameter_pack_mismatch (explain_p, parm, arg);
15890
15891 /* If the argument deduction results is a METHOD_TYPE,
15892 then there is a problem.
15893 METHOD_TYPE doesn't map to any real C++ type the result of
15894 the deduction can not be of that type. */
15895 if (TREE_CODE (arg) == METHOD_TYPE)
15896 return unify_method_type_error (explain_p, arg);
15897
15898 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
15899 return unify_success (explain_p);
15900
15901 case TEMPLATE_PARM_INDEX:
15902 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
15903 if (tparm == error_mark_node)
15904 return unify_invalid (explain_p);
15905
15906 if (TEMPLATE_PARM_LEVEL (parm)
15907 != template_decl_level (tparm))
15908 {
15909 /* The PARM is not one we're trying to unify. Just check
15910 to see if it matches ARG. */
15911 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
15912 && cp_tree_equal (parm, arg));
15913 if (result)
15914 unify_expression_unequal (explain_p, parm, arg);
15915 return result;
15916 }
15917
15918 idx = TEMPLATE_PARM_IDX (parm);
15919 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15920
15921 if (targ)
15922 {
15923 int x = !cp_tree_equal (targ, arg);
15924 if (x)
15925 unify_inconsistency (explain_p, parm, targ, arg);
15926 return x;
15927 }
15928
15929 /* [temp.deduct.type] If, in the declaration of a function template
15930 with a non-type template-parameter, the non-type
15931 template-parameter is used in an expression in the function
15932 parameter-list and, if the corresponding template-argument is
15933 deduced, the template-argument type shall match the type of the
15934 template-parameter exactly, except that a template-argument
15935 deduced from an array bound may be of any integral type.
15936 The non-type parameter might use already deduced type parameters. */
15937 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
15938 if (!TREE_TYPE (arg))
15939 /* Template-parameter dependent expression. Just accept it for now.
15940 It will later be processed in convert_template_argument. */
15941 ;
15942 else if (same_type_p (TREE_TYPE (arg), tparm))
15943 /* OK */;
15944 else if ((strict & UNIFY_ALLOW_INTEGER)
15945 && (TREE_CODE (tparm) == INTEGER_TYPE
15946 || TREE_CODE (tparm) == BOOLEAN_TYPE))
15947 /* Convert the ARG to the type of PARM; the deduced non-type
15948 template argument must exactly match the types of the
15949 corresponding parameter. */
15950 arg = fold (build_nop (tparm, arg));
15951 else if (uses_template_parms (tparm))
15952 /* We haven't deduced the type of this parameter yet. Try again
15953 later. */
15954 return unify_success (explain_p);
15955 else
15956 return unify_type_mismatch (explain_p, tparm, arg);
15957
15958 /* If ARG is a parameter pack or an expansion, we cannot unify
15959 against it unless PARM is also a parameter pack. */
15960 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
15961 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
15962 return unify_parameter_pack_mismatch (explain_p, parm, arg);
15963
15964 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
15965 return unify_success (explain_p);
15966
15967 case PTRMEM_CST:
15968 {
15969 /* A pointer-to-member constant can be unified only with
15970 another constant. */
15971 if (TREE_CODE (arg) != PTRMEM_CST)
15972 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
15973
15974 /* Just unify the class member. It would be useless (and possibly
15975 wrong, depending on the strict flags) to unify also
15976 PTRMEM_CST_CLASS, because we want to be sure that both parm and
15977 arg refer to the same variable, even if through different
15978 classes. For instance:
15979
15980 struct A { int x; };
15981 struct B : A { };
15982
15983 Unification of &A::x and &B::x must succeed. */
15984 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
15985 PTRMEM_CST_MEMBER (arg), strict, explain_p);
15986 }
15987
15988 case POINTER_TYPE:
15989 {
15990 if (TREE_CODE (arg) != POINTER_TYPE)
15991 return unify_type_mismatch (explain_p, parm, arg);
15992
15993 /* [temp.deduct.call]
15994
15995 A can be another pointer or pointer to member type that can
15996 be converted to the deduced A via a qualification
15997 conversion (_conv.qual_).
15998
15999 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16000 This will allow for additional cv-qualification of the
16001 pointed-to types if appropriate. */
16002
16003 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
16004 /* The derived-to-base conversion only persists through one
16005 level of pointers. */
16006 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
16007
16008 return unify (tparms, targs, TREE_TYPE (parm),
16009 TREE_TYPE (arg), strict, explain_p);
16010 }
16011
16012 case REFERENCE_TYPE:
16013 if (TREE_CODE (arg) != REFERENCE_TYPE)
16014 return unify_type_mismatch (explain_p, parm, arg);
16015 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16016 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16017
16018 case ARRAY_TYPE:
16019 if (TREE_CODE (arg) != ARRAY_TYPE)
16020 return unify_type_mismatch (explain_p, parm, arg);
16021 if ((TYPE_DOMAIN (parm) == NULL_TREE)
16022 != (TYPE_DOMAIN (arg) == NULL_TREE))
16023 return unify_type_mismatch (explain_p, parm, arg);
16024 if (TYPE_DOMAIN (parm) != NULL_TREE)
16025 {
16026 tree parm_max;
16027 tree arg_max;
16028 bool parm_cst;
16029 bool arg_cst;
16030
16031 /* Our representation of array types uses "N - 1" as the
16032 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16033 not an integer constant. We cannot unify arbitrarily
16034 complex expressions, so we eliminate the MINUS_EXPRs
16035 here. */
16036 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
16037 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
16038 if (!parm_cst)
16039 {
16040 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
16041 parm_max = TREE_OPERAND (parm_max, 0);
16042 }
16043 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
16044 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
16045 if (!arg_cst)
16046 {
16047 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16048 trying to unify the type of a variable with the type
16049 of a template parameter. For example:
16050
16051 template <unsigned int N>
16052 void f (char (&) [N]);
16053 int g();
16054 void h(int i) {
16055 char a[g(i)];
16056 f(a);
16057 }
16058
16059 Here, the type of the ARG will be "int [g(i)]", and
16060 may be a SAVE_EXPR, etc. */
16061 if (TREE_CODE (arg_max) != MINUS_EXPR)
16062 return unify_vla_arg (explain_p, arg);
16063 arg_max = TREE_OPERAND (arg_max, 0);
16064 }
16065
16066 /* If only one of the bounds used a MINUS_EXPR, compensate
16067 by adding one to the other bound. */
16068 if (parm_cst && !arg_cst)
16069 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
16070 integer_type_node,
16071 parm_max,
16072 integer_one_node);
16073 else if (arg_cst && !parm_cst)
16074 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
16075 integer_type_node,
16076 arg_max,
16077 integer_one_node);
16078
16079 RECUR_AND_CHECK_FAILURE (tparms, targs, parm_max, arg_max,
16080 UNIFY_ALLOW_INTEGER, explain_p);
16081 }
16082 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16083 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16084
16085 case REAL_TYPE:
16086 case COMPLEX_TYPE:
16087 case VECTOR_TYPE:
16088 case INTEGER_TYPE:
16089 case BOOLEAN_TYPE:
16090 case ENUMERAL_TYPE:
16091 case VOID_TYPE:
16092 if (TREE_CODE (arg) != TREE_CODE (parm))
16093 return unify_type_mismatch (explain_p, parm, arg);
16094
16095 /* We have already checked cv-qualification at the top of the
16096 function. */
16097 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
16098 return unify_type_mismatch (explain_p, parm, arg);
16099
16100 /* As far as unification is concerned, this wins. Later checks
16101 will invalidate it if necessary. */
16102 return unify_success (explain_p);
16103
16104 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16105 /* Type INTEGER_CST can come from ordinary constant template args. */
16106 case INTEGER_CST:
16107 while (TREE_CODE (arg) == NOP_EXPR)
16108 arg = TREE_OPERAND (arg, 0);
16109
16110 if (TREE_CODE (arg) != INTEGER_CST)
16111 return unify_template_argument_mismatch (explain_p, parm, arg);
16112 return (tree_int_cst_equal (parm, arg)
16113 ? unify_success (explain_p)
16114 : unify_template_argument_mismatch (explain_p, parm, arg));
16115
16116 case TREE_VEC:
16117 {
16118 int i;
16119 if (TREE_CODE (arg) != TREE_VEC)
16120 return unify_template_argument_mismatch (explain_p, parm, arg);
16121 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
16122 return unify_arity (explain_p, TREE_VEC_LENGTH (arg),
16123 TREE_VEC_LENGTH (parm));
16124 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
16125 RECUR_AND_CHECK_FAILURE (tparms, targs,
16126 TREE_VEC_ELT (parm, i),
16127 TREE_VEC_ELT (arg, i),
16128 UNIFY_ALLOW_NONE, explain_p);
16129 return unify_success (explain_p);
16130 }
16131
16132 case RECORD_TYPE:
16133 case UNION_TYPE:
16134 if (TREE_CODE (arg) != TREE_CODE (parm))
16135 return unify_type_mismatch (explain_p, parm, arg);
16136
16137 if (TYPE_PTRMEMFUNC_P (parm))
16138 {
16139 if (!TYPE_PTRMEMFUNC_P (arg))
16140 return unify_type_mismatch (explain_p, parm, arg);
16141
16142 return unify (tparms, targs,
16143 TYPE_PTRMEMFUNC_FN_TYPE (parm),
16144 TYPE_PTRMEMFUNC_FN_TYPE (arg),
16145 strict, explain_p);
16146 }
16147
16148 if (CLASSTYPE_TEMPLATE_INFO (parm))
16149 {
16150 tree t = NULL_TREE;
16151
16152 if (strict_in & UNIFY_ALLOW_DERIVED)
16153 {
16154 /* First, we try to unify the PARM and ARG directly. */
16155 t = try_class_unification (tparms, targs,
16156 parm, arg, explain_p);
16157
16158 if (!t)
16159 {
16160 /* Fallback to the special case allowed in
16161 [temp.deduct.call]:
16162
16163 If P is a class, and P has the form
16164 template-id, then A can be a derived class of
16165 the deduced A. Likewise, if P is a pointer to
16166 a class of the form template-id, A can be a
16167 pointer to a derived class pointed to by the
16168 deduced A. */
16169 enum template_base_result r;
16170 r = get_template_base (tparms, targs, parm, arg,
16171 explain_p, &t);
16172
16173 if (!t)
16174 return unify_no_common_base (explain_p, r, parm, arg);
16175 }
16176 }
16177 else if (CLASSTYPE_TEMPLATE_INFO (arg)
16178 && (CLASSTYPE_TI_TEMPLATE (parm)
16179 == CLASSTYPE_TI_TEMPLATE (arg)))
16180 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16181 Then, we should unify `int' and `U'. */
16182 t = arg;
16183 else
16184 /* There's no chance of unification succeeding. */
16185 return unify_type_mismatch (explain_p, parm, arg);
16186
16187 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
16188 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
16189 }
16190 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
16191 return unify_type_mismatch (explain_p, parm, arg);
16192 return unify_success (explain_p);
16193
16194 case METHOD_TYPE:
16195 case FUNCTION_TYPE:
16196 {
16197 unsigned int nargs;
16198 tree *args;
16199 tree a;
16200 unsigned int i;
16201
16202 if (TREE_CODE (arg) != TREE_CODE (parm))
16203 return unify_type_mismatch (explain_p, parm, arg);
16204
16205 /* CV qualifications for methods can never be deduced, they must
16206 match exactly. We need to check them explicitly here,
16207 because type_unification_real treats them as any other
16208 cv-qualified parameter. */
16209 if (TREE_CODE (parm) == METHOD_TYPE
16210 && (!check_cv_quals_for_unify
16211 (UNIFY_ALLOW_NONE,
16212 class_of_this_parm (arg),
16213 class_of_this_parm (parm))))
16214 return unify_cv_qual_mismatch (explain_p, parm, arg);
16215
16216 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
16217 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
16218
16219 nargs = list_length (TYPE_ARG_TYPES (arg));
16220 args = XALLOCAVEC (tree, nargs);
16221 for (a = TYPE_ARG_TYPES (arg), i = 0;
16222 a != NULL_TREE && a != void_list_node;
16223 a = TREE_CHAIN (a), ++i)
16224 args[i] = TREE_VALUE (a);
16225 nargs = i;
16226
16227 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
16228 args, nargs, 1, DEDUCE_EXACT,
16229 LOOKUP_NORMAL, explain_p);
16230 }
16231
16232 case OFFSET_TYPE:
16233 /* Unify a pointer to member with a pointer to member function, which
16234 deduces the type of the member as a function type. */
16235 if (TYPE_PTRMEMFUNC_P (arg))
16236 {
16237 tree method_type;
16238 tree fntype;
16239
16240 /* Check top-level cv qualifiers */
16241 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
16242 return unify_cv_qual_mismatch (explain_p, parm, arg);
16243
16244 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
16245 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
16246 UNIFY_ALLOW_NONE, explain_p);
16247
16248 /* Determine the type of the function we are unifying against. */
16249 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
16250 fntype =
16251 build_function_type (TREE_TYPE (method_type),
16252 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
16253
16254 /* Extract the cv-qualifiers of the member function from the
16255 implicit object parameter and place them on the function
16256 type to be restored later. */
16257 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
16258 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
16259 }
16260
16261 if (TREE_CODE (arg) != OFFSET_TYPE)
16262 return unify_type_mismatch (explain_p, parm, arg);
16263 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
16264 TYPE_OFFSET_BASETYPE (arg),
16265 UNIFY_ALLOW_NONE, explain_p);
16266 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16267 strict, explain_p);
16268
16269 case CONST_DECL:
16270 if (DECL_TEMPLATE_PARM_P (parm))
16271 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
16272 if (arg != integral_constant_value (parm))
16273 return unify_template_argument_mismatch (explain_p, parm, arg);
16274 return unify_success (explain_p);
16275
16276 case FIELD_DECL:
16277 case TEMPLATE_DECL:
16278 /* Matched cases are handled by the ARG == PARM test above. */
16279 return unify_template_argument_mismatch (explain_p, parm, arg);
16280
16281 case VAR_DECL:
16282 /* A non-type template parameter that is a variable should be a
16283 an integral constant, in which case, it whould have been
16284 folded into its (constant) value. So we should not be getting
16285 a variable here. */
16286 gcc_unreachable ();
16287
16288 case TYPE_ARGUMENT_PACK:
16289 case NONTYPE_ARGUMENT_PACK:
16290 {
16291 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
16292 tree packed_args = ARGUMENT_PACK_ARGS (arg);
16293 int i, len = TREE_VEC_LENGTH (packed_parms);
16294 int argslen = TREE_VEC_LENGTH (packed_args);
16295 int parm_variadic_p = 0;
16296
16297 for (i = 0; i < len; ++i)
16298 {
16299 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
16300 {
16301 if (i == len - 1)
16302 /* We can unify against something with a trailing
16303 parameter pack. */
16304 parm_variadic_p = 1;
16305 else
16306 /* Since there is something following the pack
16307 expansion, we cannot unify this template argument
16308 list. */
16309 return unify_success (explain_p);
16310 }
16311 }
16312
16313
16314 /* If we don't have enough arguments to satisfy the parameters
16315 (not counting the pack expression at the end), or we have
16316 too many arguments for a parameter list that doesn't end in
16317 a pack expression, we can't unify. */
16318 if (argslen < (len - parm_variadic_p))
16319 return unify_too_few_arguments (explain_p, argslen, len);
16320 if (argslen > len && !parm_variadic_p)
16321 return unify_too_many_arguments (explain_p, argslen, len);
16322
16323 /* Unify all of the parameters that precede the (optional)
16324 pack expression. */
16325 for (i = 0; i < len - parm_variadic_p; ++i)
16326 {
16327 RECUR_AND_CHECK_FAILURE (tparms, targs,
16328 TREE_VEC_ELT (packed_parms, i),
16329 TREE_VEC_ELT (packed_args, i),
16330 strict, explain_p);
16331 }
16332
16333 if (parm_variadic_p)
16334 return unify_pack_expansion (tparms, targs,
16335 packed_parms, packed_args,
16336 strict, /*call_args_p=*/false,
16337 /*subr=*/false, explain_p);
16338 return unify_success (explain_p);
16339 }
16340
16341 break;
16342
16343 case TYPEOF_TYPE:
16344 case DECLTYPE_TYPE:
16345 case UNDERLYING_TYPE:
16346 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
16347 or UNDERLYING_TYPE nodes. */
16348 return unify_success (explain_p);
16349
16350 case ERROR_MARK:
16351 /* Unification fails if we hit an error node. */
16352 return unify_invalid (explain_p);
16353
16354 default:
16355 /* An unresolved overload is a nondeduced context. */
16356 if (type_unknown_p (parm))
16357 return unify_success (explain_p);
16358 gcc_assert (EXPR_P (parm));
16359
16360 /* We must be looking at an expression. This can happen with
16361 something like:
16362
16363 template <int I>
16364 void foo(S<I>, S<I + 2>);
16365
16366 This is a "nondeduced context":
16367
16368 [deduct.type]
16369
16370 The nondeduced contexts are:
16371
16372 --A type that is a template-id in which one or more of
16373 the template-arguments is an expression that references
16374 a template-parameter.
16375
16376 In these cases, we assume deduction succeeded, but don't
16377 actually infer any unifications. */
16378
16379 if (!uses_template_parms (parm)
16380 && !template_args_equal (parm, arg))
16381 return unify_expression_unequal (explain_p, parm, arg);
16382 else
16383 return unify_success (explain_p);
16384 }
16385 }
16386 #undef RECUR_AND_CHECK_FAILURE
16387 \f
16388 /* Note that DECL can be defined in this translation unit, if
16389 required. */
16390
16391 static void
16392 mark_definable (tree decl)
16393 {
16394 tree clone;
16395 DECL_NOT_REALLY_EXTERN (decl) = 1;
16396 FOR_EACH_CLONE (clone, decl)
16397 DECL_NOT_REALLY_EXTERN (clone) = 1;
16398 }
16399
16400 /* Called if RESULT is explicitly instantiated, or is a member of an
16401 explicitly instantiated class. */
16402
16403 void
16404 mark_decl_instantiated (tree result, int extern_p)
16405 {
16406 SET_DECL_EXPLICIT_INSTANTIATION (result);
16407
16408 /* If this entity has already been written out, it's too late to
16409 make any modifications. */
16410 if (TREE_ASM_WRITTEN (result))
16411 return;
16412
16413 if (TREE_CODE (result) != FUNCTION_DECL)
16414 /* The TREE_PUBLIC flag for function declarations will have been
16415 set correctly by tsubst. */
16416 TREE_PUBLIC (result) = 1;
16417
16418 /* This might have been set by an earlier implicit instantiation. */
16419 DECL_COMDAT (result) = 0;
16420
16421 if (extern_p)
16422 DECL_NOT_REALLY_EXTERN (result) = 0;
16423 else
16424 {
16425 mark_definable (result);
16426 /* Always make artificials weak. */
16427 if (DECL_ARTIFICIAL (result) && flag_weak)
16428 comdat_linkage (result);
16429 /* For WIN32 we also want to put explicit instantiations in
16430 linkonce sections. */
16431 else if (TREE_PUBLIC (result))
16432 maybe_make_one_only (result);
16433 }
16434
16435 /* If EXTERN_P, then this function will not be emitted -- unless
16436 followed by an explicit instantiation, at which point its linkage
16437 will be adjusted. If !EXTERN_P, then this function will be
16438 emitted here. In neither circumstance do we want
16439 import_export_decl to adjust the linkage. */
16440 DECL_INTERFACE_KNOWN (result) = 1;
16441 }
16442
16443 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
16444 important template arguments. If any are missing, we check whether
16445 they're important by using error_mark_node for substituting into any
16446 args that were used for partial ordering (the ones between ARGS and END)
16447 and seeing if it bubbles up. */
16448
16449 static bool
16450 check_undeduced_parms (tree targs, tree args, tree end)
16451 {
16452 bool found = false;
16453 int i;
16454 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
16455 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
16456 {
16457 found = true;
16458 TREE_VEC_ELT (targs, i) = error_mark_node;
16459 }
16460 if (found)
16461 {
16462 for (; args != end; args = TREE_CHAIN (args))
16463 {
16464 tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
16465 if (substed == error_mark_node)
16466 return true;
16467 }
16468 }
16469 return false;
16470 }
16471
16472 /* Given two function templates PAT1 and PAT2, return:
16473
16474 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
16475 -1 if PAT2 is more specialized than PAT1.
16476 0 if neither is more specialized.
16477
16478 LEN indicates the number of parameters we should consider
16479 (defaulted parameters should not be considered).
16480
16481 The 1998 std underspecified function template partial ordering, and
16482 DR214 addresses the issue. We take pairs of arguments, one from
16483 each of the templates, and deduce them against each other. One of
16484 the templates will be more specialized if all the *other*
16485 template's arguments deduce against its arguments and at least one
16486 of its arguments *does* *not* deduce against the other template's
16487 corresponding argument. Deduction is done as for class templates.
16488 The arguments used in deduction have reference and top level cv
16489 qualifiers removed. Iff both arguments were originally reference
16490 types *and* deduction succeeds in both directions, the template
16491 with the more cv-qualified argument wins for that pairing (if
16492 neither is more cv-qualified, they both are equal). Unlike regular
16493 deduction, after all the arguments have been deduced in this way,
16494 we do *not* verify the deduced template argument values can be
16495 substituted into non-deduced contexts.
16496
16497 The logic can be a bit confusing here, because we look at deduce1 and
16498 targs1 to see if pat2 is at least as specialized, and vice versa; if we
16499 can find template arguments for pat1 to make arg1 look like arg2, that
16500 means that arg2 is at least as specialized as arg1. */
16501
16502 int
16503 more_specialized_fn (tree pat1, tree pat2, int len)
16504 {
16505 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
16506 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
16507 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
16508 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
16509 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
16510 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
16511 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
16512 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
16513 tree origs1, origs2;
16514 bool lose1 = false;
16515 bool lose2 = false;
16516
16517 /* Remove the this parameter from non-static member functions. If
16518 one is a non-static member function and the other is not a static
16519 member function, remove the first parameter from that function
16520 also. This situation occurs for operator functions where we
16521 locate both a member function (with this pointer) and non-member
16522 operator (with explicit first operand). */
16523 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
16524 {
16525 len--; /* LEN is the number of significant arguments for DECL1 */
16526 args1 = TREE_CHAIN (args1);
16527 if (!DECL_STATIC_FUNCTION_P (decl2))
16528 args2 = TREE_CHAIN (args2);
16529 }
16530 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
16531 {
16532 args2 = TREE_CHAIN (args2);
16533 if (!DECL_STATIC_FUNCTION_P (decl1))
16534 {
16535 len--;
16536 args1 = TREE_CHAIN (args1);
16537 }
16538 }
16539
16540 /* If only one is a conversion operator, they are unordered. */
16541 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
16542 return 0;
16543
16544 /* Consider the return type for a conversion function */
16545 if (DECL_CONV_FN_P (decl1))
16546 {
16547 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
16548 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
16549 len++;
16550 }
16551
16552 processing_template_decl++;
16553
16554 origs1 = args1;
16555 origs2 = args2;
16556
16557 while (len--
16558 /* Stop when an ellipsis is seen. */
16559 && args1 != NULL_TREE && args2 != NULL_TREE)
16560 {
16561 tree arg1 = TREE_VALUE (args1);
16562 tree arg2 = TREE_VALUE (args2);
16563 int deduce1, deduce2;
16564 int quals1 = -1;
16565 int quals2 = -1;
16566
16567 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
16568 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16569 {
16570 /* When both arguments are pack expansions, we need only
16571 unify the patterns themselves. */
16572 arg1 = PACK_EXPANSION_PATTERN (arg1);
16573 arg2 = PACK_EXPANSION_PATTERN (arg2);
16574
16575 /* This is the last comparison we need to do. */
16576 len = 0;
16577 }
16578
16579 if (TREE_CODE (arg1) == REFERENCE_TYPE)
16580 {
16581 arg1 = TREE_TYPE (arg1);
16582 quals1 = cp_type_quals (arg1);
16583 }
16584
16585 if (TREE_CODE (arg2) == REFERENCE_TYPE)
16586 {
16587 arg2 = TREE_TYPE (arg2);
16588 quals2 = cp_type_quals (arg2);
16589 }
16590
16591 if ((quals1 < 0) != (quals2 < 0))
16592 {
16593 /* Only of the args is a reference, see if we should apply
16594 array/function pointer decay to it. This is not part of
16595 DR214, but is, IMHO, consistent with the deduction rules
16596 for the function call itself, and with our earlier
16597 implementation of the underspecified partial ordering
16598 rules. (nathan). */
16599 if (quals1 >= 0)
16600 {
16601 switch (TREE_CODE (arg1))
16602 {
16603 case ARRAY_TYPE:
16604 arg1 = TREE_TYPE (arg1);
16605 /* FALLTHROUGH. */
16606 case FUNCTION_TYPE:
16607 arg1 = build_pointer_type (arg1);
16608 break;
16609
16610 default:
16611 break;
16612 }
16613 }
16614 else
16615 {
16616 switch (TREE_CODE (arg2))
16617 {
16618 case ARRAY_TYPE:
16619 arg2 = TREE_TYPE (arg2);
16620 /* FALLTHROUGH. */
16621 case FUNCTION_TYPE:
16622 arg2 = build_pointer_type (arg2);
16623 break;
16624
16625 default:
16626 break;
16627 }
16628 }
16629 }
16630
16631 arg1 = TYPE_MAIN_VARIANT (arg1);
16632 arg2 = TYPE_MAIN_VARIANT (arg2);
16633
16634 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
16635 {
16636 int i, len2 = list_length (args2);
16637 tree parmvec = make_tree_vec (1);
16638 tree argvec = make_tree_vec (len2);
16639 tree ta = args2;
16640
16641 /* Setup the parameter vector, which contains only ARG1. */
16642 TREE_VEC_ELT (parmvec, 0) = arg1;
16643
16644 /* Setup the argument vector, which contains the remaining
16645 arguments. */
16646 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
16647 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
16648
16649 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
16650 argvec, UNIFY_ALLOW_NONE,
16651 /*call_args_p=*/false,
16652 /*subr=*/0, /*explain_p=*/false)
16653 == 0);
16654
16655 /* We cannot deduce in the other direction, because ARG1 is
16656 a pack expansion but ARG2 is not. */
16657 deduce2 = 0;
16658 }
16659 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16660 {
16661 int i, len1 = list_length (args1);
16662 tree parmvec = make_tree_vec (1);
16663 tree argvec = make_tree_vec (len1);
16664 tree ta = args1;
16665
16666 /* Setup the parameter vector, which contains only ARG1. */
16667 TREE_VEC_ELT (parmvec, 0) = arg2;
16668
16669 /* Setup the argument vector, which contains the remaining
16670 arguments. */
16671 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
16672 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
16673
16674 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
16675 argvec, UNIFY_ALLOW_NONE,
16676 /*call_args_p=*/false,
16677 /*subr=*/0, /*explain_p=*/false)
16678 == 0);
16679
16680 /* We cannot deduce in the other direction, because ARG2 is
16681 a pack expansion but ARG1 is not.*/
16682 deduce1 = 0;
16683 }
16684
16685 else
16686 {
16687 /* The normal case, where neither argument is a pack
16688 expansion. */
16689 deduce1 = (unify (tparms1, targs1, arg1, arg2,
16690 UNIFY_ALLOW_NONE, /*explain_p=*/false)
16691 == 0);
16692 deduce2 = (unify (tparms2, targs2, arg2, arg1,
16693 UNIFY_ALLOW_NONE, /*explain_p=*/false)
16694 == 0);
16695 }
16696
16697 /* If we couldn't deduce arguments for tparms1 to make arg1 match
16698 arg2, then arg2 is not as specialized as arg1. */
16699 if (!deduce1)
16700 lose2 = true;
16701 if (!deduce2)
16702 lose1 = true;
16703
16704 /* "If, for a given type, deduction succeeds in both directions
16705 (i.e., the types are identical after the transformations above)
16706 and if the type from the argument template is more cv-qualified
16707 than the type from the parameter template (as described above)
16708 that type is considered to be more specialized than the other. If
16709 neither type is more cv-qualified than the other then neither type
16710 is more specialized than the other." */
16711
16712 if (deduce1 && deduce2
16713 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
16714 {
16715 if ((quals1 & quals2) == quals2)
16716 lose2 = true;
16717 if ((quals1 & quals2) == quals1)
16718 lose1 = true;
16719 }
16720
16721 if (lose1 && lose2)
16722 /* We've failed to deduce something in either direction.
16723 These must be unordered. */
16724 break;
16725
16726 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
16727 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16728 /* We have already processed all of the arguments in our
16729 handing of the pack expansion type. */
16730 len = 0;
16731
16732 args1 = TREE_CHAIN (args1);
16733 args2 = TREE_CHAIN (args2);
16734 }
16735
16736 /* "In most cases, all template parameters must have values in order for
16737 deduction to succeed, but for partial ordering purposes a template
16738 parameter may remain without a value provided it is not used in the
16739 types being used for partial ordering."
16740
16741 Thus, if we are missing any of the targs1 we need to substitute into
16742 origs1, then pat2 is not as specialized as pat1. This can happen when
16743 there is a nondeduced context. */
16744 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
16745 lose2 = true;
16746 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
16747 lose1 = true;
16748
16749 processing_template_decl--;
16750
16751 /* All things being equal, if the next argument is a pack expansion
16752 for one function but not for the other, prefer the
16753 non-variadic function. FIXME this is bogus; see c++/41958. */
16754 if (lose1 == lose2
16755 && args1 && TREE_VALUE (args1)
16756 && args2 && TREE_VALUE (args2))
16757 {
16758 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
16759 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
16760 }
16761
16762 if (lose1 == lose2)
16763 return 0;
16764 else if (!lose1)
16765 return 1;
16766 else
16767 return -1;
16768 }
16769
16770 /* Determine which of two partial specializations is more specialized.
16771
16772 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
16773 to the first partial specialization. The TREE_VALUE is the
16774 innermost set of template parameters for the partial
16775 specialization. PAT2 is similar, but for the second template.
16776
16777 Return 1 if the first partial specialization is more specialized;
16778 -1 if the second is more specialized; 0 if neither is more
16779 specialized.
16780
16781 See [temp.class.order] for information about determining which of
16782 two templates is more specialized. */
16783
16784 static int
16785 more_specialized_class (tree pat1, tree pat2)
16786 {
16787 tree targs;
16788 tree tmpl1, tmpl2;
16789 int winner = 0;
16790 bool any_deductions = false;
16791
16792 tmpl1 = TREE_TYPE (pat1);
16793 tmpl2 = TREE_TYPE (pat2);
16794
16795 /* Just like what happens for functions, if we are ordering between
16796 different class template specializations, we may encounter dependent
16797 types in the arguments, and we need our dependency check functions
16798 to behave correctly. */
16799 ++processing_template_decl;
16800 targs = get_class_bindings (TREE_VALUE (pat1),
16801 CLASSTYPE_TI_ARGS (tmpl1),
16802 CLASSTYPE_TI_ARGS (tmpl2));
16803 if (targs)
16804 {
16805 --winner;
16806 any_deductions = true;
16807 }
16808
16809 targs = get_class_bindings (TREE_VALUE (pat2),
16810 CLASSTYPE_TI_ARGS (tmpl2),
16811 CLASSTYPE_TI_ARGS (tmpl1));
16812 if (targs)
16813 {
16814 ++winner;
16815 any_deductions = true;
16816 }
16817 --processing_template_decl;
16818
16819 /* In the case of a tie where at least one of the class templates
16820 has a parameter pack at the end, the template with the most
16821 non-packed parameters wins. */
16822 if (winner == 0
16823 && any_deductions
16824 && (template_args_variadic_p (TREE_PURPOSE (pat1))
16825 || template_args_variadic_p (TREE_PURPOSE (pat2))))
16826 {
16827 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
16828 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
16829 int len1 = TREE_VEC_LENGTH (args1);
16830 int len2 = TREE_VEC_LENGTH (args2);
16831
16832 /* We don't count the pack expansion at the end. */
16833 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
16834 --len1;
16835 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
16836 --len2;
16837
16838 if (len1 > len2)
16839 return 1;
16840 else if (len1 < len2)
16841 return -1;
16842 }
16843
16844 return winner;
16845 }
16846
16847 /* Return the template arguments that will produce the function signature
16848 DECL from the function template FN, with the explicit template
16849 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
16850 also match. Return NULL_TREE if no satisfactory arguments could be
16851 found. */
16852
16853 static tree
16854 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
16855 {
16856 int ntparms = DECL_NTPARMS (fn);
16857 tree targs = make_tree_vec (ntparms);
16858 tree decl_type;
16859 tree decl_arg_types;
16860 tree *args;
16861 unsigned int nargs, ix;
16862 tree arg;
16863
16864 /* Substitute the explicit template arguments into the type of DECL.
16865 The call to fn_type_unification will handle substitution into the
16866 FN. */
16867 decl_type = TREE_TYPE (decl);
16868 if (explicit_args && uses_template_parms (decl_type))
16869 {
16870 tree tmpl;
16871 tree converted_args;
16872
16873 if (DECL_TEMPLATE_INFO (decl))
16874 tmpl = DECL_TI_TEMPLATE (decl);
16875 else
16876 /* We can get here for some invalid specializations. */
16877 return NULL_TREE;
16878
16879 converted_args
16880 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
16881 explicit_args, NULL_TREE,
16882 tf_none,
16883 /*require_all_args=*/false,
16884 /*use_default_args=*/false);
16885 if (converted_args == error_mark_node)
16886 return NULL_TREE;
16887
16888 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
16889 if (decl_type == error_mark_node)
16890 return NULL_TREE;
16891 }
16892
16893 /* Never do unification on the 'this' parameter. */
16894 decl_arg_types = skip_artificial_parms_for (decl,
16895 TYPE_ARG_TYPES (decl_type));
16896
16897 nargs = list_length (decl_arg_types);
16898 args = XALLOCAVEC (tree, nargs);
16899 for (arg = decl_arg_types, ix = 0;
16900 arg != NULL_TREE && arg != void_list_node;
16901 arg = TREE_CHAIN (arg), ++ix)
16902 args[ix] = TREE_VALUE (arg);
16903
16904 if (fn_type_unification (fn, explicit_args, targs,
16905 args, ix,
16906 (check_rettype || DECL_CONV_FN_P (fn)
16907 ? TREE_TYPE (decl_type) : NULL_TREE),
16908 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false))
16909 return NULL_TREE;
16910
16911 return targs;
16912 }
16913
16914 /* Return the innermost template arguments that, when applied to a
16915 template specialization whose innermost template parameters are
16916 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
16917 ARGS.
16918
16919 For example, suppose we have:
16920
16921 template <class T, class U> struct S {};
16922 template <class T> struct S<T*, int> {};
16923
16924 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
16925 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
16926 int}. The resulting vector will be {double}, indicating that `T'
16927 is bound to `double'. */
16928
16929 static tree
16930 get_class_bindings (tree tparms, tree spec_args, tree args)
16931 {
16932 int i, ntparms = TREE_VEC_LENGTH (tparms);
16933 tree deduced_args;
16934 tree innermost_deduced_args;
16935
16936 innermost_deduced_args = make_tree_vec (ntparms);
16937 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
16938 {
16939 deduced_args = copy_node (args);
16940 SET_TMPL_ARGS_LEVEL (deduced_args,
16941 TMPL_ARGS_DEPTH (deduced_args),
16942 innermost_deduced_args);
16943 }
16944 else
16945 deduced_args = innermost_deduced_args;
16946
16947 if (unify (tparms, deduced_args,
16948 INNERMOST_TEMPLATE_ARGS (spec_args),
16949 INNERMOST_TEMPLATE_ARGS (args),
16950 UNIFY_ALLOW_NONE, /*explain_p=*/false))
16951 return NULL_TREE;
16952
16953 for (i = 0; i < ntparms; ++i)
16954 if (! TREE_VEC_ELT (innermost_deduced_args, i))
16955 return NULL_TREE;
16956
16957 /* Verify that nondeduced template arguments agree with the type
16958 obtained from argument deduction.
16959
16960 For example:
16961
16962 struct A { typedef int X; };
16963 template <class T, class U> struct C {};
16964 template <class T> struct C<T, typename T::X> {};
16965
16966 Then with the instantiation `C<A, int>', we can deduce that
16967 `T' is `A' but unify () does not check whether `typename T::X'
16968 is `int'. */
16969 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
16970 if (spec_args == error_mark_node
16971 /* We only need to check the innermost arguments; the other
16972 arguments will always agree. */
16973 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
16974 INNERMOST_TEMPLATE_ARGS (args)))
16975 return NULL_TREE;
16976
16977 /* Now that we have bindings for all of the template arguments,
16978 ensure that the arguments deduced for the template template
16979 parameters have compatible template parameter lists. See the use
16980 of template_template_parm_bindings_ok_p in fn_type_unification
16981 for more information. */
16982 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
16983 return NULL_TREE;
16984
16985 return deduced_args;
16986 }
16987
16988 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
16989 Return the TREE_LIST node with the most specialized template, if
16990 any. If there is no most specialized template, the error_mark_node
16991 is returned.
16992
16993 Note that this function does not look at, or modify, the
16994 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
16995 returned is one of the elements of INSTANTIATIONS, callers may
16996 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
16997 and retrieve it from the value returned. */
16998
16999 tree
17000 most_specialized_instantiation (tree templates)
17001 {
17002 tree fn, champ;
17003
17004 ++processing_template_decl;
17005
17006 champ = templates;
17007 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
17008 {
17009 int fate = 0;
17010
17011 if (get_bindings (TREE_VALUE (champ),
17012 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17013 NULL_TREE, /*check_ret=*/true))
17014 fate--;
17015
17016 if (get_bindings (TREE_VALUE (fn),
17017 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17018 NULL_TREE, /*check_ret=*/true))
17019 fate++;
17020
17021 if (fate == -1)
17022 champ = fn;
17023 else if (!fate)
17024 {
17025 /* Equally specialized, move to next function. If there
17026 is no next function, nothing's most specialized. */
17027 fn = TREE_CHAIN (fn);
17028 champ = fn;
17029 if (!fn)
17030 break;
17031 }
17032 }
17033
17034 if (champ)
17035 /* Now verify that champ is better than everything earlier in the
17036 instantiation list. */
17037 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
17038 if (get_bindings (TREE_VALUE (champ),
17039 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17040 NULL_TREE, /*check_ret=*/true)
17041 || !get_bindings (TREE_VALUE (fn),
17042 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17043 NULL_TREE, /*check_ret=*/true))
17044 {
17045 champ = NULL_TREE;
17046 break;
17047 }
17048
17049 processing_template_decl--;
17050
17051 if (!champ)
17052 return error_mark_node;
17053
17054 return champ;
17055 }
17056
17057 /* If DECL is a specialization of some template, return the most
17058 general such template. Otherwise, returns NULL_TREE.
17059
17060 For example, given:
17061
17062 template <class T> struct S { template <class U> void f(U); };
17063
17064 if TMPL is `template <class U> void S<int>::f(U)' this will return
17065 the full template. This function will not trace past partial
17066 specializations, however. For example, given in addition:
17067
17068 template <class T> struct S<T*> { template <class U> void f(U); };
17069
17070 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17071 `template <class T> template <class U> S<T*>::f(U)'. */
17072
17073 tree
17074 most_general_template (tree decl)
17075 {
17076 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17077 an immediate specialization. */
17078 if (TREE_CODE (decl) == FUNCTION_DECL)
17079 {
17080 if (DECL_TEMPLATE_INFO (decl)) {
17081 decl = DECL_TI_TEMPLATE (decl);
17082
17083 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17084 template friend. */
17085 if (TREE_CODE (decl) != TEMPLATE_DECL)
17086 return NULL_TREE;
17087 } else
17088 return NULL_TREE;
17089 }
17090
17091 /* Look for more and more general templates. */
17092 while (DECL_TEMPLATE_INFO (decl))
17093 {
17094 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17095 (See cp-tree.h for details.) */
17096 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
17097 break;
17098
17099 if (CLASS_TYPE_P (TREE_TYPE (decl))
17100 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
17101 break;
17102
17103 /* Stop if we run into an explicitly specialized class template. */
17104 if (!DECL_NAMESPACE_SCOPE_P (decl)
17105 && DECL_CONTEXT (decl)
17106 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
17107 break;
17108
17109 decl = DECL_TI_TEMPLATE (decl);
17110 }
17111
17112 return decl;
17113 }
17114
17115 /* Return the most specialized of the class template partial
17116 specializations of TMPL which can produce TYPE, a specialization of
17117 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17118 a _TYPE node corresponding to the partial specialization, while the
17119 TREE_PURPOSE is the set of template arguments that must be
17120 substituted into the TREE_TYPE in order to generate TYPE.
17121
17122 If the choice of partial specialization is ambiguous, a diagnostic
17123 is issued, and the error_mark_node is returned. If there are no
17124 partial specializations of TMPL matching TYPE, then NULL_TREE is
17125 returned. */
17126
17127 static tree
17128 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
17129 {
17130 tree list = NULL_TREE;
17131 tree t;
17132 tree champ;
17133 int fate;
17134 bool ambiguous_p;
17135 tree args;
17136 tree outer_args = NULL_TREE;
17137
17138 tmpl = most_general_template (tmpl);
17139 args = CLASSTYPE_TI_ARGS (type);
17140
17141 /* For determining which partial specialization to use, only the
17142 innermost args are interesting. */
17143 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17144 {
17145 outer_args = strip_innermost_template_args (args, 1);
17146 args = INNERMOST_TEMPLATE_ARGS (args);
17147 }
17148
17149 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
17150 {
17151 tree partial_spec_args;
17152 tree spec_args;
17153 tree parms = TREE_VALUE (t);
17154
17155 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
17156
17157 ++processing_template_decl;
17158
17159 if (outer_args)
17160 {
17161 int i;
17162
17163 /* Discard the outer levels of args, and then substitute in the
17164 template args from the enclosing class. */
17165 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
17166 partial_spec_args = tsubst_template_args
17167 (partial_spec_args, outer_args, tf_none, NULL_TREE);
17168
17169 /* PARMS already refers to just the innermost parms, but the
17170 template parms in partial_spec_args had their levels lowered
17171 by tsubst, so we need to do the same for the parm list. We
17172 can't just tsubst the TREE_VEC itself, as tsubst wants to
17173 treat a TREE_VEC as an argument vector. */
17174 parms = copy_node (parms);
17175 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
17176 TREE_VEC_ELT (parms, i) =
17177 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
17178
17179 }
17180
17181 partial_spec_args =
17182 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
17183 add_to_template_args (outer_args,
17184 partial_spec_args),
17185 tmpl, tf_none,
17186 /*require_all_args=*/true,
17187 /*use_default_args=*/true);
17188
17189 --processing_template_decl;
17190
17191 if (partial_spec_args == error_mark_node)
17192 return error_mark_node;
17193
17194 spec_args = get_class_bindings (parms,
17195 partial_spec_args,
17196 args);
17197 if (spec_args)
17198 {
17199 if (outer_args)
17200 spec_args = add_to_template_args (outer_args, spec_args);
17201 list = tree_cons (spec_args, TREE_VALUE (t), list);
17202 TREE_TYPE (list) = TREE_TYPE (t);
17203 }
17204 }
17205
17206 if (! list)
17207 return NULL_TREE;
17208
17209 ambiguous_p = false;
17210 t = list;
17211 champ = t;
17212 t = TREE_CHAIN (t);
17213 for (; t; t = TREE_CHAIN (t))
17214 {
17215 fate = more_specialized_class (champ, t);
17216 if (fate == 1)
17217 ;
17218 else
17219 {
17220 if (fate == 0)
17221 {
17222 t = TREE_CHAIN (t);
17223 if (! t)
17224 {
17225 ambiguous_p = true;
17226 break;
17227 }
17228 }
17229 champ = t;
17230 }
17231 }
17232
17233 if (!ambiguous_p)
17234 for (t = list; t && t != champ; t = TREE_CHAIN (t))
17235 {
17236 fate = more_specialized_class (champ, t);
17237 if (fate != 1)
17238 {
17239 ambiguous_p = true;
17240 break;
17241 }
17242 }
17243
17244 if (ambiguous_p)
17245 {
17246 const char *str;
17247 char *spaces = NULL;
17248 if (!(complain & tf_error))
17249 return error_mark_node;
17250 error ("ambiguous class template instantiation for %q#T", type);
17251 str = ngettext ("candidate is:", "candidates are:", list_length (list));
17252 for (t = list; t; t = TREE_CHAIN (t))
17253 {
17254 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
17255 spaces = spaces ? spaces : get_spaces (str);
17256 }
17257 free (spaces);
17258 return error_mark_node;
17259 }
17260
17261 return champ;
17262 }
17263
17264 /* Explicitly instantiate DECL. */
17265
17266 void
17267 do_decl_instantiation (tree decl, tree storage)
17268 {
17269 tree result = NULL_TREE;
17270 int extern_p = 0;
17271
17272 if (!decl || decl == error_mark_node)
17273 /* An error occurred, for which grokdeclarator has already issued
17274 an appropriate message. */
17275 return;
17276 else if (! DECL_LANG_SPECIFIC (decl))
17277 {
17278 error ("explicit instantiation of non-template %q#D", decl);
17279 return;
17280 }
17281 else if (TREE_CODE (decl) == VAR_DECL)
17282 {
17283 /* There is an asymmetry here in the way VAR_DECLs and
17284 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17285 the latter, the DECL we get back will be marked as a
17286 template instantiation, and the appropriate
17287 DECL_TEMPLATE_INFO will be set up. This does not happen for
17288 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17289 should handle VAR_DECLs as it currently handles
17290 FUNCTION_DECLs. */
17291 if (!DECL_CLASS_SCOPE_P (decl))
17292 {
17293 error ("%qD is not a static data member of a class template", decl);
17294 return;
17295 }
17296 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
17297 if (!result || TREE_CODE (result) != VAR_DECL)
17298 {
17299 error ("no matching template for %qD found", decl);
17300 return;
17301 }
17302 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
17303 {
17304 error ("type %qT for explicit instantiation %qD does not match "
17305 "declared type %qT", TREE_TYPE (result), decl,
17306 TREE_TYPE (decl));
17307 return;
17308 }
17309 }
17310 else if (TREE_CODE (decl) != FUNCTION_DECL)
17311 {
17312 error ("explicit instantiation of %q#D", decl);
17313 return;
17314 }
17315 else
17316 result = decl;
17317
17318 /* Check for various error cases. Note that if the explicit
17319 instantiation is valid the RESULT will currently be marked as an
17320 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17321 until we get here. */
17322
17323 if (DECL_TEMPLATE_SPECIALIZATION (result))
17324 {
17325 /* DR 259 [temp.spec].
17326
17327 Both an explicit instantiation and a declaration of an explicit
17328 specialization shall not appear in a program unless the explicit
17329 instantiation follows a declaration of the explicit specialization.
17330
17331 For a given set of template parameters, if an explicit
17332 instantiation of a template appears after a declaration of an
17333 explicit specialization for that template, the explicit
17334 instantiation has no effect. */
17335 return;
17336 }
17337 else if (DECL_EXPLICIT_INSTANTIATION (result))
17338 {
17339 /* [temp.spec]
17340
17341 No program shall explicitly instantiate any template more
17342 than once.
17343
17344 We check DECL_NOT_REALLY_EXTERN so as not to complain when
17345 the first instantiation was `extern' and the second is not,
17346 and EXTERN_P for the opposite case. */
17347 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
17348 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
17349 /* If an "extern" explicit instantiation follows an ordinary
17350 explicit instantiation, the template is instantiated. */
17351 if (extern_p)
17352 return;
17353 }
17354 else if (!DECL_IMPLICIT_INSTANTIATION (result))
17355 {
17356 error ("no matching template for %qD found", result);
17357 return;
17358 }
17359 else if (!DECL_TEMPLATE_INFO (result))
17360 {
17361 permerror (input_location, "explicit instantiation of non-template %q#D", result);
17362 return;
17363 }
17364
17365 if (storage == NULL_TREE)
17366 ;
17367 else if (storage == ridpointers[(int) RID_EXTERN])
17368 {
17369 if (!in_system_header && (cxx_dialect == cxx98))
17370 pedwarn (input_location, OPT_pedantic,
17371 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
17372 "instantiations");
17373 extern_p = 1;
17374 }
17375 else
17376 error ("storage class %qD applied to template instantiation", storage);
17377
17378 check_explicit_instantiation_namespace (result);
17379 mark_decl_instantiated (result, extern_p);
17380 if (! extern_p)
17381 instantiate_decl (result, /*defer_ok=*/1,
17382 /*expl_inst_class_mem_p=*/false);
17383 }
17384
17385 static void
17386 mark_class_instantiated (tree t, int extern_p)
17387 {
17388 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
17389 SET_CLASSTYPE_INTERFACE_KNOWN (t);
17390 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
17391 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
17392 if (! extern_p)
17393 {
17394 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
17395 rest_of_type_compilation (t, 1);
17396 }
17397 }
17398
17399 /* Called from do_type_instantiation through binding_table_foreach to
17400 do recursive instantiation for the type bound in ENTRY. */
17401 static void
17402 bt_instantiate_type_proc (binding_entry entry, void *data)
17403 {
17404 tree storage = *(tree *) data;
17405
17406 if (MAYBE_CLASS_TYPE_P (entry->type)
17407 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
17408 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
17409 }
17410
17411 /* Called from do_type_instantiation to instantiate a member
17412 (a member function or a static member variable) of an
17413 explicitly instantiated class template. */
17414 static void
17415 instantiate_class_member (tree decl, int extern_p)
17416 {
17417 mark_decl_instantiated (decl, extern_p);
17418 if (! extern_p)
17419 instantiate_decl (decl, /*defer_ok=*/1,
17420 /*expl_inst_class_mem_p=*/true);
17421 }
17422
17423 /* Perform an explicit instantiation of template class T. STORAGE, if
17424 non-null, is the RID for extern, inline or static. COMPLAIN is
17425 nonzero if this is called from the parser, zero if called recursively,
17426 since the standard is unclear (as detailed below). */
17427
17428 void
17429 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
17430 {
17431 int extern_p = 0;
17432 int nomem_p = 0;
17433 int static_p = 0;
17434 int previous_instantiation_extern_p = 0;
17435
17436 if (TREE_CODE (t) == TYPE_DECL)
17437 t = TREE_TYPE (t);
17438
17439 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
17440 {
17441 error ("explicit instantiation of non-template type %qT", t);
17442 return;
17443 }
17444
17445 complete_type (t);
17446
17447 if (!COMPLETE_TYPE_P (t))
17448 {
17449 if (complain & tf_error)
17450 error ("explicit instantiation of %q#T before definition of template",
17451 t);
17452 return;
17453 }
17454
17455 if (storage != NULL_TREE)
17456 {
17457 if (!in_system_header)
17458 {
17459 if (storage == ridpointers[(int) RID_EXTERN])
17460 {
17461 if (cxx_dialect == cxx98)
17462 pedwarn (input_location, OPT_pedantic,
17463 "ISO C++ 1998 forbids the use of %<extern%> on "
17464 "explicit instantiations");
17465 }
17466 else
17467 pedwarn (input_location, OPT_pedantic,
17468 "ISO C++ forbids the use of %qE"
17469 " on explicit instantiations", storage);
17470 }
17471
17472 if (storage == ridpointers[(int) RID_INLINE])
17473 nomem_p = 1;
17474 else if (storage == ridpointers[(int) RID_EXTERN])
17475 extern_p = 1;
17476 else if (storage == ridpointers[(int) RID_STATIC])
17477 static_p = 1;
17478 else
17479 {
17480 error ("storage class %qD applied to template instantiation",
17481 storage);
17482 extern_p = 0;
17483 }
17484 }
17485
17486 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
17487 {
17488 /* DR 259 [temp.spec].
17489
17490 Both an explicit instantiation and a declaration of an explicit
17491 specialization shall not appear in a program unless the explicit
17492 instantiation follows a declaration of the explicit specialization.
17493
17494 For a given set of template parameters, if an explicit
17495 instantiation of a template appears after a declaration of an
17496 explicit specialization for that template, the explicit
17497 instantiation has no effect. */
17498 return;
17499 }
17500 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
17501 {
17502 /* [temp.spec]
17503
17504 No program shall explicitly instantiate any template more
17505 than once.
17506
17507 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
17508 instantiation was `extern'. If EXTERN_P then the second is.
17509 These cases are OK. */
17510 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
17511
17512 if (!previous_instantiation_extern_p && !extern_p
17513 && (complain & tf_error))
17514 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
17515
17516 /* If we've already instantiated the template, just return now. */
17517 if (!CLASSTYPE_INTERFACE_ONLY (t))
17518 return;
17519 }
17520
17521 check_explicit_instantiation_namespace (TYPE_NAME (t));
17522 mark_class_instantiated (t, extern_p);
17523
17524 if (nomem_p)
17525 return;
17526
17527 {
17528 tree tmp;
17529
17530 /* In contrast to implicit instantiation, where only the
17531 declarations, and not the definitions, of members are
17532 instantiated, we have here:
17533
17534 [temp.explicit]
17535
17536 The explicit instantiation of a class template specialization
17537 implies the instantiation of all of its members not
17538 previously explicitly specialized in the translation unit
17539 containing the explicit instantiation.
17540
17541 Of course, we can't instantiate member template classes, since
17542 we don't have any arguments for them. Note that the standard
17543 is unclear on whether the instantiation of the members are
17544 *explicit* instantiations or not. However, the most natural
17545 interpretation is that it should be an explicit instantiation. */
17546
17547 if (! static_p)
17548 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
17549 if (TREE_CODE (tmp) == FUNCTION_DECL
17550 && DECL_TEMPLATE_INSTANTIATION (tmp))
17551 instantiate_class_member (tmp, extern_p);
17552
17553 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
17554 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
17555 instantiate_class_member (tmp, extern_p);
17556
17557 if (CLASSTYPE_NESTED_UTDS (t))
17558 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
17559 bt_instantiate_type_proc, &storage);
17560 }
17561 }
17562
17563 /* Given a function DECL, which is a specialization of TMPL, modify
17564 DECL to be a re-instantiation of TMPL with the same template
17565 arguments. TMPL should be the template into which tsubst'ing
17566 should occur for DECL, not the most general template.
17567
17568 One reason for doing this is a scenario like this:
17569
17570 template <class T>
17571 void f(const T&, int i);
17572
17573 void g() { f(3, 7); }
17574
17575 template <class T>
17576 void f(const T& t, const int i) { }
17577
17578 Note that when the template is first instantiated, with
17579 instantiate_template, the resulting DECL will have no name for the
17580 first parameter, and the wrong type for the second. So, when we go
17581 to instantiate the DECL, we regenerate it. */
17582
17583 static void
17584 regenerate_decl_from_template (tree decl, tree tmpl)
17585 {
17586 /* The arguments used to instantiate DECL, from the most general
17587 template. */
17588 tree args;
17589 tree code_pattern;
17590
17591 args = DECL_TI_ARGS (decl);
17592 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
17593
17594 /* Make sure that we can see identifiers, and compute access
17595 correctly. */
17596 push_access_scope (decl);
17597
17598 if (TREE_CODE (decl) == FUNCTION_DECL)
17599 {
17600 tree decl_parm;
17601 tree pattern_parm;
17602 tree specs;
17603 int args_depth;
17604 int parms_depth;
17605
17606 args_depth = TMPL_ARGS_DEPTH (args);
17607 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
17608 if (args_depth > parms_depth)
17609 args = get_innermost_template_args (args, parms_depth);
17610
17611 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
17612 args, tf_error, NULL_TREE,
17613 /*defer_ok*/false);
17614 if (specs)
17615 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
17616 specs);
17617
17618 /* Merge parameter declarations. */
17619 decl_parm = skip_artificial_parms_for (decl,
17620 DECL_ARGUMENTS (decl));
17621 pattern_parm
17622 = skip_artificial_parms_for (code_pattern,
17623 DECL_ARGUMENTS (code_pattern));
17624 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
17625 {
17626 tree parm_type;
17627 tree attributes;
17628
17629 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
17630 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
17631 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
17632 NULL_TREE);
17633 parm_type = type_decays_to (parm_type);
17634 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
17635 TREE_TYPE (decl_parm) = parm_type;
17636 attributes = DECL_ATTRIBUTES (pattern_parm);
17637 if (DECL_ATTRIBUTES (decl_parm) != attributes)
17638 {
17639 DECL_ATTRIBUTES (decl_parm) = attributes;
17640 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
17641 }
17642 decl_parm = DECL_CHAIN (decl_parm);
17643 pattern_parm = DECL_CHAIN (pattern_parm);
17644 }
17645 /* Merge any parameters that match with the function parameter
17646 pack. */
17647 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
17648 {
17649 int i, len;
17650 tree expanded_types;
17651 /* Expand the TYPE_PACK_EXPANSION that provides the types for
17652 the parameters in this function parameter pack. */
17653 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
17654 args, tf_error, NULL_TREE);
17655 len = TREE_VEC_LENGTH (expanded_types);
17656 for (i = 0; i < len; i++)
17657 {
17658 tree parm_type;
17659 tree attributes;
17660
17661 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
17662 /* Rename the parameter to include the index. */
17663 DECL_NAME (decl_parm) =
17664 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
17665 parm_type = TREE_VEC_ELT (expanded_types, i);
17666 parm_type = type_decays_to (parm_type);
17667 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
17668 TREE_TYPE (decl_parm) = parm_type;
17669 attributes = DECL_ATTRIBUTES (pattern_parm);
17670 if (DECL_ATTRIBUTES (decl_parm) != attributes)
17671 {
17672 DECL_ATTRIBUTES (decl_parm) = attributes;
17673 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
17674 }
17675 decl_parm = DECL_CHAIN (decl_parm);
17676 }
17677 }
17678 /* Merge additional specifiers from the CODE_PATTERN. */
17679 if (DECL_DECLARED_INLINE_P (code_pattern)
17680 && !DECL_DECLARED_INLINE_P (decl))
17681 DECL_DECLARED_INLINE_P (decl) = 1;
17682 }
17683 else if (TREE_CODE (decl) == VAR_DECL)
17684 {
17685 DECL_INITIAL (decl) =
17686 tsubst_expr (DECL_INITIAL (code_pattern), args,
17687 tf_error, DECL_TI_TEMPLATE (decl),
17688 /*integral_constant_expression_p=*/false);
17689 if (VAR_HAD_UNKNOWN_BOUND (decl))
17690 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
17691 tf_error, DECL_TI_TEMPLATE (decl));
17692 }
17693 else
17694 gcc_unreachable ();
17695
17696 pop_access_scope (decl);
17697 }
17698
17699 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
17700 substituted to get DECL. */
17701
17702 tree
17703 template_for_substitution (tree decl)
17704 {
17705 tree tmpl = DECL_TI_TEMPLATE (decl);
17706
17707 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
17708 for the instantiation. This is not always the most general
17709 template. Consider, for example:
17710
17711 template <class T>
17712 struct S { template <class U> void f();
17713 template <> void f<int>(); };
17714
17715 and an instantiation of S<double>::f<int>. We want TD to be the
17716 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
17717 while (/* An instantiation cannot have a definition, so we need a
17718 more general template. */
17719 DECL_TEMPLATE_INSTANTIATION (tmpl)
17720 /* We must also deal with friend templates. Given:
17721
17722 template <class T> struct S {
17723 template <class U> friend void f() {};
17724 };
17725
17726 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
17727 so far as the language is concerned, but that's still
17728 where we get the pattern for the instantiation from. On
17729 other hand, if the definition comes outside the class, say:
17730
17731 template <class T> struct S {
17732 template <class U> friend void f();
17733 };
17734 template <class U> friend void f() {}
17735
17736 we don't need to look any further. That's what the check for
17737 DECL_INITIAL is for. */
17738 || (TREE_CODE (decl) == FUNCTION_DECL
17739 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
17740 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
17741 {
17742 /* The present template, TD, should not be a definition. If it
17743 were a definition, we should be using it! Note that we
17744 cannot restructure the loop to just keep going until we find
17745 a template with a definition, since that might go too far if
17746 a specialization was declared, but not defined. */
17747 gcc_assert (TREE_CODE (decl) != VAR_DECL
17748 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
17749
17750 /* Fetch the more general template. */
17751 tmpl = DECL_TI_TEMPLATE (tmpl);
17752 }
17753
17754 return tmpl;
17755 }
17756
17757 /* Returns true if we need to instantiate this template instance even if we
17758 know we aren't going to emit it.. */
17759
17760 bool
17761 always_instantiate_p (tree decl)
17762 {
17763 /* We always instantiate inline functions so that we can inline them. An
17764 explicit instantiation declaration prohibits implicit instantiation of
17765 non-inline functions. With high levels of optimization, we would
17766 normally inline non-inline functions -- but we're not allowed to do
17767 that for "extern template" functions. Therefore, we check
17768 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
17769 return ((TREE_CODE (decl) == FUNCTION_DECL
17770 && DECL_DECLARED_INLINE_P (decl))
17771 /* And we need to instantiate static data members so that
17772 their initializers are available in integral constant
17773 expressions. */
17774 || (TREE_CODE (decl) == VAR_DECL
17775 && decl_maybe_constant_var_p (decl)));
17776 }
17777
17778 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
17779 instantiate it now, modifying TREE_TYPE (fn). */
17780
17781 void
17782 maybe_instantiate_noexcept (tree fn)
17783 {
17784 tree fntype, spec, noex, clone;
17785
17786 if (DECL_CLONED_FUNCTION_P (fn))
17787 fn = DECL_CLONED_FUNCTION (fn);
17788 fntype = TREE_TYPE (fn);
17789 spec = TYPE_RAISES_EXCEPTIONS (fntype);
17790
17791 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
17792 return;
17793
17794 noex = TREE_PURPOSE (spec);
17795
17796 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
17797 {
17798 push_tinst_level (fn);
17799 push_access_scope (fn);
17800 input_location = DECL_SOURCE_LOCATION (fn);
17801 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
17802 DEFERRED_NOEXCEPT_ARGS (noex),
17803 tf_warning_or_error, fn, /*function_p=*/false,
17804 /*integral_constant_expression_p=*/true);
17805 pop_access_scope (fn);
17806 pop_tinst_level ();
17807 spec = build_noexcept_spec (noex, tf_warning_or_error);
17808 if (spec == error_mark_node)
17809 spec = noexcept_false_spec;
17810 }
17811 else
17812 {
17813 /* This is an implicitly declared function, so NOEX is a list of
17814 other functions to evaluate and merge. */
17815 tree elt;
17816 spec = noexcept_true_spec;
17817 for (elt = noex; elt; elt = OVL_NEXT (elt))
17818 {
17819 tree fn = OVL_CURRENT (elt);
17820 tree subspec;
17821 maybe_instantiate_noexcept (fn);
17822 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
17823 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
17824 }
17825 }
17826
17827 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
17828
17829 FOR_EACH_CLONE (clone, fn)
17830 {
17831 if (TREE_TYPE (clone) == fntype)
17832 TREE_TYPE (clone) = TREE_TYPE (fn);
17833 else
17834 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
17835 }
17836 }
17837
17838 /* Produce the definition of D, a _DECL generated from a template. If
17839 DEFER_OK is nonzero, then we don't have to actually do the
17840 instantiation now; we just have to do it sometime. Normally it is
17841 an error if this is an explicit instantiation but D is undefined.
17842 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
17843 explicitly instantiated class template. */
17844
17845 tree
17846 instantiate_decl (tree d, int defer_ok,
17847 bool expl_inst_class_mem_p)
17848 {
17849 tree tmpl = DECL_TI_TEMPLATE (d);
17850 tree gen_args;
17851 tree args;
17852 tree td;
17853 tree code_pattern;
17854 tree spec;
17855 tree gen_tmpl;
17856 bool pattern_defined;
17857 int need_push;
17858 location_t saved_loc = input_location;
17859 bool external_p;
17860
17861 /* This function should only be used to instantiate templates for
17862 functions and static member variables. */
17863 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
17864 || TREE_CODE (d) == VAR_DECL);
17865
17866 /* Variables are never deferred; if instantiation is required, they
17867 are instantiated right away. That allows for better code in the
17868 case that an expression refers to the value of the variable --
17869 if the variable has a constant value the referring expression can
17870 take advantage of that fact. */
17871 if (TREE_CODE (d) == VAR_DECL
17872 || DECL_DECLARED_CONSTEXPR_P (d))
17873 defer_ok = 0;
17874
17875 /* Don't instantiate cloned functions. Instead, instantiate the
17876 functions they cloned. */
17877 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
17878 d = DECL_CLONED_FUNCTION (d);
17879
17880 if (DECL_TEMPLATE_INSTANTIATED (d)
17881 || DECL_TEMPLATE_SPECIALIZATION (d))
17882 /* D has already been instantiated or explicitly specialized, so
17883 there's nothing for us to do here.
17884
17885 It might seem reasonable to check whether or not D is an explicit
17886 instantiation, and, if so, stop here. But when an explicit
17887 instantiation is deferred until the end of the compilation,
17888 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
17889 the instantiation. */
17890 return d;
17891
17892 /* Check to see whether we know that this template will be
17893 instantiated in some other file, as with "extern template"
17894 extension. */
17895 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
17896
17897 /* In general, we do not instantiate such templates. */
17898 if (external_p && !always_instantiate_p (d))
17899 return d;
17900
17901 gen_tmpl = most_general_template (tmpl);
17902 gen_args = DECL_TI_ARGS (d);
17903
17904 if (tmpl != gen_tmpl)
17905 /* We should already have the extra args. */
17906 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
17907 == TMPL_ARGS_DEPTH (gen_args));
17908 /* And what's in the hash table should match D. */
17909 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
17910 || spec == NULL_TREE);
17911
17912 /* This needs to happen before any tsubsting. */
17913 if (! push_tinst_level (d))
17914 return d;
17915
17916 timevar_push (TV_TEMPLATE_INST);
17917
17918 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
17919 for the instantiation. */
17920 td = template_for_substitution (d);
17921 code_pattern = DECL_TEMPLATE_RESULT (td);
17922
17923 /* We should never be trying to instantiate a member of a class
17924 template or partial specialization. */
17925 gcc_assert (d != code_pattern);
17926
17927 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
17928 || DECL_TEMPLATE_SPECIALIZATION (td))
17929 /* In the case of a friend template whose definition is provided
17930 outside the class, we may have too many arguments. Drop the
17931 ones we don't need. The same is true for specializations. */
17932 args = get_innermost_template_args
17933 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
17934 else
17935 args = gen_args;
17936
17937 if (TREE_CODE (d) == FUNCTION_DECL)
17938 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
17939 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
17940 else
17941 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
17942
17943 /* We may be in the middle of deferred access check. Disable it now. */
17944 push_deferring_access_checks (dk_no_deferred);
17945
17946 /* Unless an explicit instantiation directive has already determined
17947 the linkage of D, remember that a definition is available for
17948 this entity. */
17949 if (pattern_defined
17950 && !DECL_INTERFACE_KNOWN (d)
17951 && !DECL_NOT_REALLY_EXTERN (d))
17952 mark_definable (d);
17953
17954 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
17955 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
17956 input_location = DECL_SOURCE_LOCATION (d);
17957
17958 /* If D is a member of an explicitly instantiated class template,
17959 and no definition is available, treat it like an implicit
17960 instantiation. */
17961 if (!pattern_defined && expl_inst_class_mem_p
17962 && DECL_EXPLICIT_INSTANTIATION (d))
17963 {
17964 /* Leave linkage flags alone on instantiations with anonymous
17965 visibility. */
17966 if (TREE_PUBLIC (d))
17967 {
17968 DECL_NOT_REALLY_EXTERN (d) = 0;
17969 DECL_INTERFACE_KNOWN (d) = 0;
17970 }
17971 SET_DECL_IMPLICIT_INSTANTIATION (d);
17972 }
17973
17974 if (TREE_CODE (d) == FUNCTION_DECL)
17975 maybe_instantiate_noexcept (d);
17976
17977 /* Recheck the substitutions to obtain any warning messages
17978 about ignoring cv qualifiers. Don't do this for artificial decls,
17979 as it breaks the context-sensitive substitution for lambda op(). */
17980 if (!defer_ok && !DECL_ARTIFICIAL (d))
17981 {
17982 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
17983 tree type = TREE_TYPE (gen);
17984
17985 /* Make sure that we can see identifiers, and compute access
17986 correctly. D is already the target FUNCTION_DECL with the
17987 right context. */
17988 push_access_scope (d);
17989
17990 if (TREE_CODE (gen) == FUNCTION_DECL)
17991 {
17992 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
17993 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
17994 d, /*defer_ok*/true);
17995 /* Don't simply tsubst the function type, as that will give
17996 duplicate warnings about poor parameter qualifications.
17997 The function arguments are the same as the decl_arguments
17998 without the top level cv qualifiers. */
17999 type = TREE_TYPE (type);
18000 }
18001 tsubst (type, gen_args, tf_warning_or_error, d);
18002
18003 pop_access_scope (d);
18004 }
18005
18006 /* Defer all other templates, unless we have been explicitly
18007 forbidden from doing so. */
18008 if (/* If there is no definition, we cannot instantiate the
18009 template. */
18010 ! pattern_defined
18011 /* If it's OK to postpone instantiation, do so. */
18012 || defer_ok
18013 /* If this is a static data member that will be defined
18014 elsewhere, we don't want to instantiate the entire data
18015 member, but we do want to instantiate the initializer so that
18016 we can substitute that elsewhere. */
18017 || (external_p && TREE_CODE (d) == VAR_DECL))
18018 {
18019 /* The definition of the static data member is now required so
18020 we must substitute the initializer. */
18021 if (TREE_CODE (d) == VAR_DECL
18022 && !DECL_INITIAL (d)
18023 && DECL_INITIAL (code_pattern))
18024 {
18025 tree ns;
18026 tree init;
18027 bool const_init = false;
18028
18029 ns = decl_namespace_context (d);
18030 push_nested_namespace (ns);
18031 push_nested_class (DECL_CONTEXT (d));
18032 init = tsubst_expr (DECL_INITIAL (code_pattern),
18033 args,
18034 tf_warning_or_error, NULL_TREE,
18035 /*integral_constant_expression_p=*/false);
18036 /* Make sure the initializer is still constant, in case of
18037 circular dependency (template/instantiate6.C). */
18038 const_init
18039 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18040 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
18041 /*asmspec_tree=*/NULL_TREE,
18042 LOOKUP_ONLYCONVERTING);
18043 pop_nested_class ();
18044 pop_nested_namespace (ns);
18045 }
18046
18047 /* We restore the source position here because it's used by
18048 add_pending_template. */
18049 input_location = saved_loc;
18050
18051 if (at_eof && !pattern_defined
18052 && DECL_EXPLICIT_INSTANTIATION (d)
18053 && DECL_NOT_REALLY_EXTERN (d))
18054 /* [temp.explicit]
18055
18056 The definition of a non-exported function template, a
18057 non-exported member function template, or a non-exported
18058 member function or static data member of a class template
18059 shall be present in every translation unit in which it is
18060 explicitly instantiated. */
18061 permerror (input_location, "explicit instantiation of %qD "
18062 "but no definition available", d);
18063
18064 /* If we're in unevaluated context, we just wanted to get the
18065 constant value; this isn't an odr use, so don't queue
18066 a full instantiation. */
18067 if (cp_unevaluated_operand != 0)
18068 goto out;
18069 /* ??? Historically, we have instantiated inline functions, even
18070 when marked as "extern template". */
18071 if (!(external_p && TREE_CODE (d) == VAR_DECL))
18072 add_pending_template (d);
18073 goto out;
18074 }
18075 /* Tell the repository that D is available in this translation unit
18076 -- and see if it is supposed to be instantiated here. */
18077 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
18078 {
18079 /* In a PCH file, despite the fact that the repository hasn't
18080 requested instantiation in the PCH it is still possible that
18081 an instantiation will be required in a file that includes the
18082 PCH. */
18083 if (pch_file)
18084 add_pending_template (d);
18085 /* Instantiate inline functions so that the inliner can do its
18086 job, even though we'll not be emitting a copy of this
18087 function. */
18088 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
18089 goto out;
18090 }
18091
18092 need_push = !cfun || !global_bindings_p ();
18093 if (need_push)
18094 push_to_top_level ();
18095
18096 /* Mark D as instantiated so that recursive calls to
18097 instantiate_decl do not try to instantiate it again. */
18098 DECL_TEMPLATE_INSTANTIATED (d) = 1;
18099
18100 /* Regenerate the declaration in case the template has been modified
18101 by a subsequent redeclaration. */
18102 regenerate_decl_from_template (d, td);
18103
18104 /* We already set the file and line above. Reset them now in case
18105 they changed as a result of calling regenerate_decl_from_template. */
18106 input_location = DECL_SOURCE_LOCATION (d);
18107
18108 if (TREE_CODE (d) == VAR_DECL)
18109 {
18110 tree init;
18111 bool const_init = false;
18112
18113 /* Clear out DECL_RTL; whatever was there before may not be right
18114 since we've reset the type of the declaration. */
18115 SET_DECL_RTL (d, NULL);
18116 DECL_IN_AGGR_P (d) = 0;
18117
18118 /* The initializer is placed in DECL_INITIAL by
18119 regenerate_decl_from_template so we don't need to
18120 push/pop_access_scope again here. Pull it out so that
18121 cp_finish_decl can process it. */
18122 init = DECL_INITIAL (d);
18123 DECL_INITIAL (d) = NULL_TREE;
18124 DECL_INITIALIZED_P (d) = 0;
18125
18126 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18127 initializer. That function will defer actual emission until
18128 we have a chance to determine linkage. */
18129 DECL_EXTERNAL (d) = 0;
18130
18131 /* Enter the scope of D so that access-checking works correctly. */
18132 push_nested_class (DECL_CONTEXT (d));
18133 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18134 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
18135 pop_nested_class ();
18136 }
18137 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
18138 synthesize_method (d);
18139 else if (TREE_CODE (d) == FUNCTION_DECL)
18140 {
18141 htab_t saved_local_specializations;
18142 tree subst_decl;
18143 tree tmpl_parm;
18144 tree spec_parm;
18145
18146 /* Save away the current list, in case we are instantiating one
18147 template from within the body of another. */
18148 saved_local_specializations = local_specializations;
18149
18150 /* Set up the list of local specializations. */
18151 local_specializations = htab_create (37,
18152 hash_local_specialization,
18153 eq_local_specializations,
18154 NULL);
18155
18156 /* Set up context. */
18157 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
18158
18159 /* Create substitution entries for the parameters. */
18160 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
18161 tmpl_parm = DECL_ARGUMENTS (subst_decl);
18162 spec_parm = DECL_ARGUMENTS (d);
18163 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
18164 {
18165 register_local_specialization (spec_parm, tmpl_parm);
18166 spec_parm = skip_artificial_parms_for (d, spec_parm);
18167 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
18168 }
18169 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
18170 {
18171 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm))
18172 {
18173 register_local_specialization (spec_parm, tmpl_parm);
18174 spec_parm = DECL_CHAIN (spec_parm);
18175 }
18176 else
18177 {
18178 /* Register the (value) argument pack as a specialization of
18179 TMPL_PARM, then move on. */
18180 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
18181 register_local_specialization (argpack, tmpl_parm);
18182 }
18183 }
18184 gcc_assert (!spec_parm);
18185
18186 /* Substitute into the body of the function. */
18187 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
18188 tf_warning_or_error, tmpl,
18189 /*integral_constant_expression_p=*/false);
18190
18191 /* Set the current input_location to the end of the function
18192 so that finish_function knows where we are. */
18193 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
18194
18195 /* We don't need the local specializations any more. */
18196 htab_delete (local_specializations);
18197 local_specializations = saved_local_specializations;
18198
18199 /* Finish the function. */
18200 d = finish_function (0);
18201 expand_or_defer_fn (d);
18202 }
18203
18204 /* We're not deferring instantiation any more. */
18205 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
18206
18207 if (need_push)
18208 pop_from_top_level ();
18209
18210 out:
18211 input_location = saved_loc;
18212 pop_deferring_access_checks ();
18213 pop_tinst_level ();
18214
18215 timevar_pop (TV_TEMPLATE_INST);
18216
18217 return d;
18218 }
18219
18220 /* Run through the list of templates that we wish we could
18221 instantiate, and instantiate any we can. RETRIES is the
18222 number of times we retry pending template instantiation. */
18223
18224 void
18225 instantiate_pending_templates (int retries)
18226 {
18227 int reconsider;
18228 location_t saved_loc = input_location;
18229
18230 /* Instantiating templates may trigger vtable generation. This in turn
18231 may require further template instantiations. We place a limit here
18232 to avoid infinite loop. */
18233 if (pending_templates && retries >= max_tinst_depth)
18234 {
18235 tree decl = pending_templates->tinst->decl;
18236
18237 error ("template instantiation depth exceeds maximum of %d"
18238 " instantiating %q+D, possibly from virtual table generation"
18239 " (use -ftemplate-depth= to increase the maximum)",
18240 max_tinst_depth, decl);
18241 if (TREE_CODE (decl) == FUNCTION_DECL)
18242 /* Pretend that we defined it. */
18243 DECL_INITIAL (decl) = error_mark_node;
18244 return;
18245 }
18246
18247 do
18248 {
18249 struct pending_template **t = &pending_templates;
18250 struct pending_template *last = NULL;
18251 reconsider = 0;
18252 while (*t)
18253 {
18254 tree instantiation = reopen_tinst_level ((*t)->tinst);
18255 bool complete = false;
18256
18257 if (TYPE_P (instantiation))
18258 {
18259 tree fn;
18260
18261 if (!COMPLETE_TYPE_P (instantiation))
18262 {
18263 instantiate_class_template (instantiation);
18264 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
18265 for (fn = TYPE_METHODS (instantiation);
18266 fn;
18267 fn = TREE_CHAIN (fn))
18268 if (! DECL_ARTIFICIAL (fn))
18269 instantiate_decl (fn,
18270 /*defer_ok=*/0,
18271 /*expl_inst_class_mem_p=*/false);
18272 if (COMPLETE_TYPE_P (instantiation))
18273 reconsider = 1;
18274 }
18275
18276 complete = COMPLETE_TYPE_P (instantiation);
18277 }
18278 else
18279 {
18280 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
18281 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
18282 {
18283 instantiation
18284 = instantiate_decl (instantiation,
18285 /*defer_ok=*/0,
18286 /*expl_inst_class_mem_p=*/false);
18287 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
18288 reconsider = 1;
18289 }
18290
18291 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
18292 || DECL_TEMPLATE_INSTANTIATED (instantiation));
18293 }
18294
18295 if (complete)
18296 /* If INSTANTIATION has been instantiated, then we don't
18297 need to consider it again in the future. */
18298 *t = (*t)->next;
18299 else
18300 {
18301 last = *t;
18302 t = &(*t)->next;
18303 }
18304 tinst_depth = 0;
18305 current_tinst_level = NULL;
18306 }
18307 last_pending_template = last;
18308 }
18309 while (reconsider);
18310
18311 input_location = saved_loc;
18312 }
18313
18314 /* Substitute ARGVEC into T, which is a list of initializers for
18315 either base class or a non-static data member. The TREE_PURPOSEs
18316 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18317 instantiate_decl. */
18318
18319 static tree
18320 tsubst_initializer_list (tree t, tree argvec)
18321 {
18322 tree inits = NULL_TREE;
18323
18324 for (; t; t = TREE_CHAIN (t))
18325 {
18326 tree decl;
18327 tree init;
18328 tree expanded_bases = NULL_TREE;
18329 tree expanded_arguments = NULL_TREE;
18330 int i, len = 1;
18331
18332 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
18333 {
18334 tree expr;
18335 tree arg;
18336
18337 /* Expand the base class expansion type into separate base
18338 classes. */
18339 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
18340 tf_warning_or_error,
18341 NULL_TREE);
18342 if (expanded_bases == error_mark_node)
18343 continue;
18344
18345 /* We'll be building separate TREE_LISTs of arguments for
18346 each base. */
18347 len = TREE_VEC_LENGTH (expanded_bases);
18348 expanded_arguments = make_tree_vec (len);
18349 for (i = 0; i < len; i++)
18350 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
18351
18352 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
18353 expand each argument in the TREE_VALUE of t. */
18354 expr = make_node (EXPR_PACK_EXPANSION);
18355 PACK_EXPANSION_PARAMETER_PACKS (expr) =
18356 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
18357
18358 if (TREE_VALUE (t) == void_type_node)
18359 /* VOID_TYPE_NODE is used to indicate
18360 value-initialization. */
18361 {
18362 for (i = 0; i < len; i++)
18363 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
18364 }
18365 else
18366 {
18367 /* Substitute parameter packs into each argument in the
18368 TREE_LIST. */
18369 in_base_initializer = 1;
18370 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
18371 {
18372 tree expanded_exprs;
18373
18374 /* Expand the argument. */
18375 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
18376 expanded_exprs
18377 = tsubst_pack_expansion (expr, argvec,
18378 tf_warning_or_error,
18379 NULL_TREE);
18380 if (expanded_exprs == error_mark_node)
18381 continue;
18382
18383 /* Prepend each of the expanded expressions to the
18384 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
18385 for (i = 0; i < len; i++)
18386 {
18387 TREE_VEC_ELT (expanded_arguments, i) =
18388 tree_cons (NULL_TREE,
18389 TREE_VEC_ELT (expanded_exprs, i),
18390 TREE_VEC_ELT (expanded_arguments, i));
18391 }
18392 }
18393 in_base_initializer = 0;
18394
18395 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
18396 since we built them backwards. */
18397 for (i = 0; i < len; i++)
18398 {
18399 TREE_VEC_ELT (expanded_arguments, i) =
18400 nreverse (TREE_VEC_ELT (expanded_arguments, i));
18401 }
18402 }
18403 }
18404
18405 for (i = 0; i < len; ++i)
18406 {
18407 if (expanded_bases)
18408 {
18409 decl = TREE_VEC_ELT (expanded_bases, i);
18410 decl = expand_member_init (decl);
18411 init = TREE_VEC_ELT (expanded_arguments, i);
18412 }
18413 else
18414 {
18415 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
18416 tf_warning_or_error, NULL_TREE);
18417
18418 decl = expand_member_init (decl);
18419 if (decl && !DECL_P (decl))
18420 in_base_initializer = 1;
18421
18422 init = TREE_VALUE (t);
18423 if (init != void_type_node)
18424 init = tsubst_expr (init, argvec,
18425 tf_warning_or_error, NULL_TREE,
18426 /*integral_constant_expression_p=*/false);
18427 in_base_initializer = 0;
18428 }
18429
18430 if (decl)
18431 {
18432 init = build_tree_list (decl, init);
18433 TREE_CHAIN (init) = inits;
18434 inits = init;
18435 }
18436 }
18437 }
18438 return inits;
18439 }
18440
18441 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
18442
18443 static void
18444 set_current_access_from_decl (tree decl)
18445 {
18446 if (TREE_PRIVATE (decl))
18447 current_access_specifier = access_private_node;
18448 else if (TREE_PROTECTED (decl))
18449 current_access_specifier = access_protected_node;
18450 else
18451 current_access_specifier = access_public_node;
18452 }
18453
18454 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
18455 is the instantiation (which should have been created with
18456 start_enum) and ARGS are the template arguments to use. */
18457
18458 static void
18459 tsubst_enum (tree tag, tree newtag, tree args)
18460 {
18461 tree e;
18462
18463 if (SCOPED_ENUM_P (newtag))
18464 begin_scope (sk_scoped_enum, newtag);
18465
18466 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
18467 {
18468 tree value;
18469 tree decl;
18470
18471 decl = TREE_VALUE (e);
18472 /* Note that in a template enum, the TREE_VALUE is the
18473 CONST_DECL, not the corresponding INTEGER_CST. */
18474 value = tsubst_expr (DECL_INITIAL (decl),
18475 args, tf_warning_or_error, NULL_TREE,
18476 /*integral_constant_expression_p=*/true);
18477
18478 /* Give this enumeration constant the correct access. */
18479 set_current_access_from_decl (decl);
18480
18481 /* Actually build the enumerator itself. */
18482 build_enumerator
18483 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
18484 }
18485
18486 if (SCOPED_ENUM_P (newtag))
18487 finish_scope ();
18488
18489 finish_enum_value_list (newtag);
18490 finish_enum (newtag);
18491
18492 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
18493 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
18494 }
18495
18496 /* DECL is a FUNCTION_DECL that is a template specialization. Return
18497 its type -- but without substituting the innermost set of template
18498 arguments. So, innermost set of template parameters will appear in
18499 the type. */
18500
18501 tree
18502 get_mostly_instantiated_function_type (tree decl)
18503 {
18504 tree fn_type;
18505 tree tmpl;
18506 tree targs;
18507 tree tparms;
18508 int parm_depth;
18509
18510 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
18511 targs = DECL_TI_ARGS (decl);
18512 tparms = DECL_TEMPLATE_PARMS (tmpl);
18513 parm_depth = TMPL_PARMS_DEPTH (tparms);
18514
18515 /* There should be as many levels of arguments as there are levels
18516 of parameters. */
18517 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
18518
18519 fn_type = TREE_TYPE (tmpl);
18520
18521 if (parm_depth == 1)
18522 /* No substitution is necessary. */
18523 ;
18524 else
18525 {
18526 int i;
18527 tree partial_args;
18528
18529 /* Replace the innermost level of the TARGS with NULL_TREEs to
18530 let tsubst know not to substitute for those parameters. */
18531 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
18532 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
18533 SET_TMPL_ARGS_LEVEL (partial_args, i,
18534 TMPL_ARGS_LEVEL (targs, i));
18535 SET_TMPL_ARGS_LEVEL (partial_args,
18536 TMPL_ARGS_DEPTH (targs),
18537 make_tree_vec (DECL_NTPARMS (tmpl)));
18538
18539 /* Make sure that we can see identifiers, and compute access
18540 correctly. */
18541 push_access_scope (decl);
18542
18543 ++processing_template_decl;
18544 /* Now, do the (partial) substitution to figure out the
18545 appropriate function type. */
18546 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
18547 --processing_template_decl;
18548
18549 /* Substitute into the template parameters to obtain the real
18550 innermost set of parameters. This step is important if the
18551 innermost set of template parameters contains value
18552 parameters whose types depend on outer template parameters. */
18553 TREE_VEC_LENGTH (partial_args)--;
18554 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
18555
18556 pop_access_scope (decl);
18557 }
18558
18559 return fn_type;
18560 }
18561
18562 /* Return truthvalue if we're processing a template different from
18563 the last one involved in diagnostics. */
18564 int
18565 problematic_instantiation_changed (void)
18566 {
18567 return last_template_error_tick != tinst_level_tick;
18568 }
18569
18570 /* Remember current template involved in diagnostics. */
18571 void
18572 record_last_problematic_instantiation (void)
18573 {
18574 last_template_error_tick = tinst_level_tick;
18575 }
18576
18577 struct tinst_level *
18578 current_instantiation (void)
18579 {
18580 return current_tinst_level;
18581 }
18582
18583 /* [temp.param] Check that template non-type parm TYPE is of an allowable
18584 type. Return zero for ok, nonzero for disallowed. Issue error and
18585 warning messages under control of COMPLAIN. */
18586
18587 static int
18588 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
18589 {
18590 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
18591 return 0;
18592 else if (POINTER_TYPE_P (type))
18593 return 0;
18594 else if (TYPE_PTR_TO_MEMBER_P (type))
18595 return 0;
18596 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
18597 return 0;
18598 else if (TREE_CODE (type) == TYPENAME_TYPE)
18599 return 0;
18600 else if (TREE_CODE (type) == DECLTYPE_TYPE)
18601 return 0;
18602
18603 if (complain & tf_error)
18604 error ("%q#T is not a valid type for a template constant parameter", type);
18605 return 1;
18606 }
18607
18608 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
18609 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
18610
18611 static bool
18612 dependent_type_p_r (tree type)
18613 {
18614 tree scope;
18615
18616 /* [temp.dep.type]
18617
18618 A type is dependent if it is:
18619
18620 -- a template parameter. Template template parameters are types
18621 for us (since TYPE_P holds true for them) so we handle
18622 them here. */
18623 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18624 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
18625 return true;
18626 /* -- a qualified-id with a nested-name-specifier which contains a
18627 class-name that names a dependent type or whose unqualified-id
18628 names a dependent type. */
18629 if (TREE_CODE (type) == TYPENAME_TYPE)
18630 return true;
18631 /* -- a cv-qualified type where the cv-unqualified type is
18632 dependent. */
18633 type = TYPE_MAIN_VARIANT (type);
18634 /* -- a compound type constructed from any dependent type. */
18635 if (TYPE_PTR_TO_MEMBER_P (type))
18636 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
18637 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
18638 (type)));
18639 else if (TREE_CODE (type) == POINTER_TYPE
18640 || TREE_CODE (type) == REFERENCE_TYPE)
18641 return dependent_type_p (TREE_TYPE (type));
18642 else if (TREE_CODE (type) == FUNCTION_TYPE
18643 || TREE_CODE (type) == METHOD_TYPE)
18644 {
18645 tree arg_type;
18646
18647 if (dependent_type_p (TREE_TYPE (type)))
18648 return true;
18649 for (arg_type = TYPE_ARG_TYPES (type);
18650 arg_type;
18651 arg_type = TREE_CHAIN (arg_type))
18652 if (dependent_type_p (TREE_VALUE (arg_type)))
18653 return true;
18654 return false;
18655 }
18656 /* -- an array type constructed from any dependent type or whose
18657 size is specified by a constant expression that is
18658 value-dependent.
18659
18660 We checked for type- and value-dependence of the bounds in
18661 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
18662 if (TREE_CODE (type) == ARRAY_TYPE)
18663 {
18664 if (TYPE_DOMAIN (type)
18665 && dependent_type_p (TYPE_DOMAIN (type)))
18666 return true;
18667 return dependent_type_p (TREE_TYPE (type));
18668 }
18669
18670 /* -- a template-id in which either the template name is a template
18671 parameter ... */
18672 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18673 return true;
18674 /* ... or any of the template arguments is a dependent type or
18675 an expression that is type-dependent or value-dependent. */
18676 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
18677 && (any_dependent_template_arguments_p
18678 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
18679 return true;
18680
18681 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
18682 dependent; if the argument of the `typeof' expression is not
18683 type-dependent, then it should already been have resolved. */
18684 if (TREE_CODE (type) == TYPEOF_TYPE
18685 || TREE_CODE (type) == DECLTYPE_TYPE
18686 || TREE_CODE (type) == UNDERLYING_TYPE)
18687 return true;
18688
18689 /* A template argument pack is dependent if any of its packed
18690 arguments are. */
18691 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
18692 {
18693 tree args = ARGUMENT_PACK_ARGS (type);
18694 int i, len = TREE_VEC_LENGTH (args);
18695 for (i = 0; i < len; ++i)
18696 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
18697 return true;
18698 }
18699
18700 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
18701 be template parameters. */
18702 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
18703 return true;
18704
18705 /* The standard does not specifically mention types that are local
18706 to template functions or local classes, but they should be
18707 considered dependent too. For example:
18708
18709 template <int I> void f() {
18710 enum E { a = I };
18711 S<sizeof (E)> s;
18712 }
18713
18714 The size of `E' cannot be known until the value of `I' has been
18715 determined. Therefore, `E' must be considered dependent. */
18716 scope = TYPE_CONTEXT (type);
18717 if (scope && TYPE_P (scope))
18718 return dependent_type_p (scope);
18719 /* Don't use type_dependent_expression_p here, as it can lead
18720 to infinite recursion trying to determine whether a lambda
18721 nested in a lambda is dependent (c++/47687). */
18722 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
18723 && DECL_LANG_SPECIFIC (scope)
18724 && DECL_TEMPLATE_INFO (scope)
18725 && (any_dependent_template_arguments_p
18726 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
18727 return true;
18728
18729 /* Other types are non-dependent. */
18730 return false;
18731 }
18732
18733 /* Returns TRUE if TYPE is dependent, in the sense of
18734 [temp.dep.type]. Note that a NULL type is considered dependent. */
18735
18736 bool
18737 dependent_type_p (tree type)
18738 {
18739 /* If there are no template parameters in scope, then there can't be
18740 any dependent types. */
18741 if (!processing_template_decl)
18742 {
18743 /* If we are not processing a template, then nobody should be
18744 providing us with a dependent type. */
18745 gcc_assert (type);
18746 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
18747 return false;
18748 }
18749
18750 /* If the type is NULL, we have not computed a type for the entity
18751 in question; in that case, the type is dependent. */
18752 if (!type)
18753 return true;
18754
18755 /* Erroneous types can be considered non-dependent. */
18756 if (type == error_mark_node)
18757 return false;
18758
18759 /* If we have not already computed the appropriate value for TYPE,
18760 do so now. */
18761 if (!TYPE_DEPENDENT_P_VALID (type))
18762 {
18763 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
18764 TYPE_DEPENDENT_P_VALID (type) = 1;
18765 }
18766
18767 return TYPE_DEPENDENT_P (type);
18768 }
18769
18770 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
18771 lookup. In other words, a dependent type that is not the current
18772 instantiation. */
18773
18774 bool
18775 dependent_scope_p (tree scope)
18776 {
18777 return (scope && TYPE_P (scope) && dependent_type_p (scope)
18778 && !currently_open_class (scope));
18779 }
18780
18781 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
18782 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
18783 expression. */
18784
18785 /* Note that this predicate is not appropriate for general expressions;
18786 only constant expressions (that satisfy potential_constant_expression)
18787 can be tested for value dependence.
18788
18789 We should really also have a predicate for "instantiation-dependent".
18790
18791 fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent.
18792 (what about instantiation-dependent constant-expressions?)
18793 is_late_template_attribute: defer if instantiation-dependent.
18794 compute_array_index_type: proceed if constant and not t- or v-dependent
18795 if instantiation-dependent, need to remember full expression
18796 uses_template_parms: FIXME - need to audit callers
18797 tsubst_decl [function_decl]: Why is this using value_dependent_expression_p?
18798 dependent_type_p [array_type]: dependent if index type is dependent
18799 (or non-constant?)
18800 static_assert - instantiation-dependent */
18801
18802 bool
18803 value_dependent_expression_p (tree expression)
18804 {
18805 if (!processing_template_decl)
18806 return false;
18807
18808 /* A name declared with a dependent type. */
18809 if (DECL_P (expression) && type_dependent_expression_p (expression))
18810 return true;
18811
18812 switch (TREE_CODE (expression))
18813 {
18814 case IDENTIFIER_NODE:
18815 /* A name that has not been looked up -- must be dependent. */
18816 return true;
18817
18818 case TEMPLATE_PARM_INDEX:
18819 /* A non-type template parm. */
18820 return true;
18821
18822 case CONST_DECL:
18823 /* A non-type template parm. */
18824 if (DECL_TEMPLATE_PARM_P (expression))
18825 return true;
18826 return value_dependent_expression_p (DECL_INITIAL (expression));
18827
18828 case VAR_DECL:
18829 /* A constant with literal type and is initialized
18830 with an expression that is value-dependent. */
18831 if (DECL_INITIAL (expression)
18832 && decl_constant_var_p (expression)
18833 && value_dependent_expression_p (DECL_INITIAL (expression)))
18834 return true;
18835 return false;
18836
18837 case DYNAMIC_CAST_EXPR:
18838 case STATIC_CAST_EXPR:
18839 case CONST_CAST_EXPR:
18840 case REINTERPRET_CAST_EXPR:
18841 case CAST_EXPR:
18842 /* These expressions are value-dependent if the type to which
18843 the cast occurs is dependent or the expression being casted
18844 is value-dependent. */
18845 {
18846 tree type = TREE_TYPE (expression);
18847
18848 if (dependent_type_p (type))
18849 return true;
18850
18851 /* A functional cast has a list of operands. */
18852 expression = TREE_OPERAND (expression, 0);
18853 if (!expression)
18854 {
18855 /* If there are no operands, it must be an expression such
18856 as "int()". This should not happen for aggregate types
18857 because it would form non-constant expressions. */
18858 gcc_assert (cxx_dialect >= cxx0x
18859 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
18860
18861 return false;
18862 }
18863
18864 if (TREE_CODE (expression) == TREE_LIST)
18865 return any_value_dependent_elements_p (expression);
18866
18867 return value_dependent_expression_p (expression);
18868 }
18869
18870 case SIZEOF_EXPR:
18871 case ALIGNOF_EXPR:
18872 case TYPEID_EXPR:
18873 /* A `sizeof' expression is value-dependent if the operand is
18874 type-dependent or is a pack expansion. */
18875 expression = TREE_OPERAND (expression, 0);
18876 if (PACK_EXPANSION_P (expression))
18877 return true;
18878 else if (TYPE_P (expression))
18879 return dependent_type_p (expression);
18880 return type_dependent_expression_p (expression);
18881
18882 case AT_ENCODE_EXPR:
18883 /* An 'encode' expression is value-dependent if the operand is
18884 type-dependent. */
18885 expression = TREE_OPERAND (expression, 0);
18886 return dependent_type_p (expression);
18887
18888 case NOEXCEPT_EXPR:
18889 expression = TREE_OPERAND (expression, 0);
18890 return type_dependent_expression_p (expression);
18891
18892 case SCOPE_REF:
18893 {
18894 tree name = TREE_OPERAND (expression, 1);
18895 return value_dependent_expression_p (name);
18896 }
18897
18898 case COMPONENT_REF:
18899 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
18900 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
18901
18902 case NONTYPE_ARGUMENT_PACK:
18903 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
18904 is value-dependent. */
18905 {
18906 tree values = ARGUMENT_PACK_ARGS (expression);
18907 int i, len = TREE_VEC_LENGTH (values);
18908
18909 for (i = 0; i < len; ++i)
18910 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
18911 return true;
18912
18913 return false;
18914 }
18915
18916 case TRAIT_EXPR:
18917 {
18918 tree type2 = TRAIT_EXPR_TYPE2 (expression);
18919 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
18920 || (type2 ? dependent_type_p (type2) : false));
18921 }
18922
18923 case MODOP_EXPR:
18924 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
18925 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
18926
18927 case ARRAY_REF:
18928 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
18929 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
18930
18931 case ADDR_EXPR:
18932 {
18933 tree op = TREE_OPERAND (expression, 0);
18934 return (value_dependent_expression_p (op)
18935 || has_value_dependent_address (op));
18936 }
18937
18938 case CALL_EXPR:
18939 {
18940 tree fn = get_callee_fndecl (expression);
18941 int i, nargs;
18942 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
18943 return true;
18944 nargs = call_expr_nargs (expression);
18945 for (i = 0; i < nargs; ++i)
18946 {
18947 tree op = CALL_EXPR_ARG (expression, i);
18948 /* In a call to a constexpr member function, look through the
18949 implicit ADDR_EXPR on the object argument so that it doesn't
18950 cause the call to be considered value-dependent. We also
18951 look through it in potential_constant_expression. */
18952 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
18953 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
18954 && TREE_CODE (op) == ADDR_EXPR)
18955 op = TREE_OPERAND (op, 0);
18956 if (value_dependent_expression_p (op))
18957 return true;
18958 }
18959 return false;
18960 }
18961
18962 case TEMPLATE_ID_EXPR:
18963 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
18964 type-dependent. */
18965 return type_dependent_expression_p (expression);
18966
18967 case CONSTRUCTOR:
18968 {
18969 unsigned ix;
18970 tree val;
18971 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
18972 if (value_dependent_expression_p (val))
18973 return true;
18974 return false;
18975 }
18976
18977 default:
18978 /* A constant expression is value-dependent if any subexpression is
18979 value-dependent. */
18980 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
18981 {
18982 case tcc_reference:
18983 case tcc_unary:
18984 case tcc_comparison:
18985 case tcc_binary:
18986 case tcc_expression:
18987 case tcc_vl_exp:
18988 {
18989 int i, len = cp_tree_operand_length (expression);
18990
18991 for (i = 0; i < len; i++)
18992 {
18993 tree t = TREE_OPERAND (expression, i);
18994
18995 /* In some cases, some of the operands may be missing.l
18996 (For example, in the case of PREDECREMENT_EXPR, the
18997 amount to increment by may be missing.) That doesn't
18998 make the expression dependent. */
18999 if (t && value_dependent_expression_p (t))
19000 return true;
19001 }
19002 }
19003 break;
19004 default:
19005 break;
19006 }
19007 break;
19008 }
19009
19010 /* The expression is not value-dependent. */
19011 return false;
19012 }
19013
19014 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19015 [temp.dep.expr]. Note that an expression with no type is
19016 considered dependent. Other parts of the compiler arrange for an
19017 expression with type-dependent subexpressions to have no type, so
19018 this function doesn't have to be fully recursive. */
19019
19020 bool
19021 type_dependent_expression_p (tree expression)
19022 {
19023 if (!processing_template_decl)
19024 return false;
19025
19026 if (expression == error_mark_node)
19027 return false;
19028
19029 /* An unresolved name is always dependent. */
19030 if (TREE_CODE (expression) == IDENTIFIER_NODE
19031 || TREE_CODE (expression) == USING_DECL)
19032 return true;
19033
19034 /* Some expression forms are never type-dependent. */
19035 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
19036 || TREE_CODE (expression) == SIZEOF_EXPR
19037 || TREE_CODE (expression) == ALIGNOF_EXPR
19038 || TREE_CODE (expression) == AT_ENCODE_EXPR
19039 || TREE_CODE (expression) == NOEXCEPT_EXPR
19040 || TREE_CODE (expression) == TRAIT_EXPR
19041 || TREE_CODE (expression) == TYPEID_EXPR
19042 || TREE_CODE (expression) == DELETE_EXPR
19043 || TREE_CODE (expression) == VEC_DELETE_EXPR
19044 || TREE_CODE (expression) == THROW_EXPR)
19045 return false;
19046
19047 /* The types of these expressions depends only on the type to which
19048 the cast occurs. */
19049 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
19050 || TREE_CODE (expression) == STATIC_CAST_EXPR
19051 || TREE_CODE (expression) == CONST_CAST_EXPR
19052 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
19053 || TREE_CODE (expression) == CAST_EXPR)
19054 return dependent_type_p (TREE_TYPE (expression));
19055
19056 /* The types of these expressions depends only on the type created
19057 by the expression. */
19058 if (TREE_CODE (expression) == NEW_EXPR
19059 || TREE_CODE (expression) == VEC_NEW_EXPR)
19060 {
19061 /* For NEW_EXPR tree nodes created inside a template, either
19062 the object type itself or a TREE_LIST may appear as the
19063 operand 1. */
19064 tree type = TREE_OPERAND (expression, 1);
19065 if (TREE_CODE (type) == TREE_LIST)
19066 /* This is an array type. We need to check array dimensions
19067 as well. */
19068 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
19069 || value_dependent_expression_p
19070 (TREE_OPERAND (TREE_VALUE (type), 1));
19071 else
19072 return dependent_type_p (type);
19073 }
19074
19075 if (TREE_CODE (expression) == SCOPE_REF)
19076 {
19077 tree scope = TREE_OPERAND (expression, 0);
19078 tree name = TREE_OPERAND (expression, 1);
19079
19080 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19081 contains an identifier associated by name lookup with one or more
19082 declarations declared with a dependent type, or...a
19083 nested-name-specifier or qualified-id that names a member of an
19084 unknown specialization. */
19085 return (type_dependent_expression_p (name)
19086 || dependent_scope_p (scope));
19087 }
19088
19089 if (TREE_CODE (expression) == FUNCTION_DECL
19090 && DECL_LANG_SPECIFIC (expression)
19091 && DECL_TEMPLATE_INFO (expression)
19092 && (any_dependent_template_arguments_p
19093 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
19094 return true;
19095
19096 if (TREE_CODE (expression) == TEMPLATE_DECL
19097 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
19098 return false;
19099
19100 if (TREE_CODE (expression) == STMT_EXPR)
19101 expression = stmt_expr_value_expr (expression);
19102
19103 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
19104 {
19105 tree elt;
19106 unsigned i;
19107
19108 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
19109 {
19110 if (type_dependent_expression_p (elt))
19111 return true;
19112 }
19113 return false;
19114 }
19115
19116 /* A static data member of the current instantiation with incomplete
19117 array type is type-dependent, as the definition and specializations
19118 can have different bounds. */
19119 if (TREE_CODE (expression) == VAR_DECL
19120 && DECL_CLASS_SCOPE_P (expression)
19121 && dependent_type_p (DECL_CONTEXT (expression))
19122 && VAR_HAD_UNKNOWN_BOUND (expression))
19123 return true;
19124
19125 if (TREE_TYPE (expression) == unknown_type_node)
19126 {
19127 if (TREE_CODE (expression) == ADDR_EXPR)
19128 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
19129 if (TREE_CODE (expression) == COMPONENT_REF
19130 || TREE_CODE (expression) == OFFSET_REF)
19131 {
19132 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
19133 return true;
19134 expression = TREE_OPERAND (expression, 1);
19135 if (TREE_CODE (expression) == IDENTIFIER_NODE)
19136 return false;
19137 }
19138 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19139 if (TREE_CODE (expression) == SCOPE_REF)
19140 return false;
19141
19142 if (TREE_CODE (expression) == BASELINK)
19143 expression = BASELINK_FUNCTIONS (expression);
19144
19145 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
19146 {
19147 if (any_dependent_template_arguments_p
19148 (TREE_OPERAND (expression, 1)))
19149 return true;
19150 expression = TREE_OPERAND (expression, 0);
19151 }
19152 gcc_assert (TREE_CODE (expression) == OVERLOAD
19153 || TREE_CODE (expression) == FUNCTION_DECL);
19154
19155 while (expression)
19156 {
19157 if (type_dependent_expression_p (OVL_CURRENT (expression)))
19158 return true;
19159 expression = OVL_NEXT (expression);
19160 }
19161 return false;
19162 }
19163
19164 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
19165
19166 return (dependent_type_p (TREE_TYPE (expression)));
19167 }
19168
19169 /* Like type_dependent_expression_p, but it also works while not processing
19170 a template definition, i.e. during substitution or mangling. */
19171
19172 bool
19173 type_dependent_expression_p_push (tree expr)
19174 {
19175 bool b;
19176 ++processing_template_decl;
19177 b = type_dependent_expression_p (expr);
19178 --processing_template_decl;
19179 return b;
19180 }
19181
19182 /* Returns TRUE if ARGS contains a type-dependent expression. */
19183
19184 bool
19185 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
19186 {
19187 unsigned int i;
19188 tree arg;
19189
19190 FOR_EACH_VEC_ELT (tree, args, i, arg)
19191 {
19192 if (type_dependent_expression_p (arg))
19193 return true;
19194 }
19195 return false;
19196 }
19197
19198 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19199 expressions) contains any type-dependent expressions. */
19200
19201 bool
19202 any_type_dependent_elements_p (const_tree list)
19203 {
19204 for (; list; list = TREE_CHAIN (list))
19205 if (value_dependent_expression_p (TREE_VALUE (list)))
19206 return true;
19207
19208 return false;
19209 }
19210
19211 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19212 expressions) contains any value-dependent expressions. */
19213
19214 bool
19215 any_value_dependent_elements_p (const_tree list)
19216 {
19217 for (; list; list = TREE_CHAIN (list))
19218 if (value_dependent_expression_p (TREE_VALUE (list)))
19219 return true;
19220
19221 return false;
19222 }
19223
19224 /* Returns TRUE if the ARG (a template argument) is dependent. */
19225
19226 bool
19227 dependent_template_arg_p (tree arg)
19228 {
19229 if (!processing_template_decl)
19230 return false;
19231
19232 /* Assume a template argument that was wrongly written by the user
19233 is dependent. This is consistent with what
19234 any_dependent_template_arguments_p [that calls this function]
19235 does. */
19236 if (!arg || arg == error_mark_node)
19237 return true;
19238
19239 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
19240 arg = ARGUMENT_PACK_SELECT_ARG (arg);
19241
19242 if (TREE_CODE (arg) == TEMPLATE_DECL
19243 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
19244 return dependent_template_p (arg);
19245 else if (ARGUMENT_PACK_P (arg))
19246 {
19247 tree args = ARGUMENT_PACK_ARGS (arg);
19248 int i, len = TREE_VEC_LENGTH (args);
19249 for (i = 0; i < len; ++i)
19250 {
19251 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
19252 return true;
19253 }
19254
19255 return false;
19256 }
19257 else if (TYPE_P (arg))
19258 return dependent_type_p (arg);
19259 else
19260 return (type_dependent_expression_p (arg)
19261 || value_dependent_expression_p (arg));
19262 }
19263
19264 /* Returns true if ARGS (a collection of template arguments) contains
19265 any types that require structural equality testing. */
19266
19267 bool
19268 any_template_arguments_need_structural_equality_p (tree args)
19269 {
19270 int i;
19271 int j;
19272
19273 if (!args)
19274 return false;
19275 if (args == error_mark_node)
19276 return true;
19277
19278 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
19279 {
19280 tree level = TMPL_ARGS_LEVEL (args, i + 1);
19281 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
19282 {
19283 tree arg = TREE_VEC_ELT (level, j);
19284 tree packed_args = NULL_TREE;
19285 int k, len = 1;
19286
19287 if (ARGUMENT_PACK_P (arg))
19288 {
19289 /* Look inside the argument pack. */
19290 packed_args = ARGUMENT_PACK_ARGS (arg);
19291 len = TREE_VEC_LENGTH (packed_args);
19292 }
19293
19294 for (k = 0; k < len; ++k)
19295 {
19296 if (packed_args)
19297 arg = TREE_VEC_ELT (packed_args, k);
19298
19299 if (error_operand_p (arg))
19300 return true;
19301 else if (TREE_CODE (arg) == TEMPLATE_DECL
19302 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
19303 continue;
19304 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
19305 return true;
19306 else if (!TYPE_P (arg) && TREE_TYPE (arg)
19307 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
19308 return true;
19309 }
19310 }
19311 }
19312
19313 return false;
19314 }
19315
19316 /* Returns true if ARGS (a collection of template arguments) contains
19317 any dependent arguments. */
19318
19319 bool
19320 any_dependent_template_arguments_p (const_tree args)
19321 {
19322 int i;
19323 int j;
19324
19325 if (!args)
19326 return false;
19327 if (args == error_mark_node)
19328 return true;
19329
19330 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
19331 {
19332 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
19333 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
19334 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
19335 return true;
19336 }
19337
19338 return false;
19339 }
19340
19341 /* Returns TRUE if the template TMPL is dependent. */
19342
19343 bool
19344 dependent_template_p (tree tmpl)
19345 {
19346 if (TREE_CODE (tmpl) == OVERLOAD)
19347 {
19348 while (tmpl)
19349 {
19350 if (dependent_template_p (OVL_CURRENT (tmpl)))
19351 return true;
19352 tmpl = OVL_NEXT (tmpl);
19353 }
19354 return false;
19355 }
19356
19357 /* Template template parameters are dependent. */
19358 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
19359 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
19360 return true;
19361 /* So are names that have not been looked up. */
19362 if (TREE_CODE (tmpl) == SCOPE_REF
19363 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
19364 return true;
19365 /* So are member templates of dependent classes. */
19366 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
19367 return dependent_type_p (DECL_CONTEXT (tmpl));
19368 return false;
19369 }
19370
19371 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
19372
19373 bool
19374 dependent_template_id_p (tree tmpl, tree args)
19375 {
19376 return (dependent_template_p (tmpl)
19377 || any_dependent_template_arguments_p (args));
19378 }
19379
19380 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
19381 is dependent. */
19382
19383 bool
19384 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
19385 {
19386 int i;
19387
19388 if (!processing_template_decl)
19389 return false;
19390
19391 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
19392 {
19393 tree decl = TREE_VEC_ELT (declv, i);
19394 tree init = TREE_VEC_ELT (initv, i);
19395 tree cond = TREE_VEC_ELT (condv, i);
19396 tree incr = TREE_VEC_ELT (incrv, i);
19397
19398 if (type_dependent_expression_p (decl))
19399 return true;
19400
19401 if (init && type_dependent_expression_p (init))
19402 return true;
19403
19404 if (type_dependent_expression_p (cond))
19405 return true;
19406
19407 if (COMPARISON_CLASS_P (cond)
19408 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
19409 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
19410 return true;
19411
19412 if (TREE_CODE (incr) == MODOP_EXPR)
19413 {
19414 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
19415 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
19416 return true;
19417 }
19418 else if (type_dependent_expression_p (incr))
19419 return true;
19420 else if (TREE_CODE (incr) == MODIFY_EXPR)
19421 {
19422 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
19423 return true;
19424 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
19425 {
19426 tree t = TREE_OPERAND (incr, 1);
19427 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
19428 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
19429 return true;
19430 }
19431 }
19432 }
19433
19434 return false;
19435 }
19436
19437 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
19438 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
19439 no such TYPE can be found. Note that this function peers inside
19440 uninstantiated templates and therefore should be used only in
19441 extremely limited situations. ONLY_CURRENT_P restricts this
19442 peering to the currently open classes hierarchy (which is required
19443 when comparing types). */
19444
19445 tree
19446 resolve_typename_type (tree type, bool only_current_p)
19447 {
19448 tree scope;
19449 tree name;
19450 tree decl;
19451 int quals;
19452 tree pushed_scope;
19453 tree result;
19454
19455 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
19456
19457 scope = TYPE_CONTEXT (type);
19458 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
19459 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
19460 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
19461 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
19462 identifier of the TYPENAME_TYPE anymore.
19463 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
19464 TYPENAME_TYPE instead, we avoid messing up with a possible
19465 typedef variant case. */
19466 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
19467
19468 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
19469 it first before we can figure out what NAME refers to. */
19470 if (TREE_CODE (scope) == TYPENAME_TYPE)
19471 scope = resolve_typename_type (scope, only_current_p);
19472 /* If we don't know what SCOPE refers to, then we cannot resolve the
19473 TYPENAME_TYPE. */
19474 if (TREE_CODE (scope) == TYPENAME_TYPE)
19475 return type;
19476 /* If the SCOPE is a template type parameter, we have no way of
19477 resolving the name. */
19478 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
19479 return type;
19480 /* If the SCOPE is not the current instantiation, there's no reason
19481 to look inside it. */
19482 if (only_current_p && !currently_open_class (scope))
19483 return type;
19484 /* If this is a typedef, we don't want to look inside (c++/11987). */
19485 if (typedef_variant_p (type))
19486 return type;
19487 /* If SCOPE isn't the template itself, it will not have a valid
19488 TYPE_FIELDS list. */
19489 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
19490 /* scope is either the template itself or a compatible instantiation
19491 like X<T>, so look up the name in the original template. */
19492 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
19493 else
19494 /* scope is a partial instantiation, so we can't do the lookup or we
19495 will lose the template arguments. */
19496 return type;
19497 /* Enter the SCOPE so that name lookup will be resolved as if we
19498 were in the class definition. In particular, SCOPE will no
19499 longer be considered a dependent type. */
19500 pushed_scope = push_scope (scope);
19501 /* Look up the declaration. */
19502 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
19503
19504 result = NULL_TREE;
19505
19506 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
19507 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
19508 if (!decl)
19509 /*nop*/;
19510 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
19511 && TREE_CODE (decl) == TYPE_DECL)
19512 {
19513 result = TREE_TYPE (decl);
19514 if (result == error_mark_node)
19515 result = NULL_TREE;
19516 }
19517 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
19518 && DECL_CLASS_TEMPLATE_P (decl))
19519 {
19520 tree tmpl;
19521 tree args;
19522 /* Obtain the template and the arguments. */
19523 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
19524 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
19525 /* Instantiate the template. */
19526 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
19527 /*entering_scope=*/0,
19528 tf_error | tf_user);
19529 if (result == error_mark_node)
19530 result = NULL_TREE;
19531 }
19532
19533 /* Leave the SCOPE. */
19534 if (pushed_scope)
19535 pop_scope (pushed_scope);
19536
19537 /* If we failed to resolve it, return the original typename. */
19538 if (!result)
19539 return type;
19540
19541 /* If lookup found a typename type, resolve that too. */
19542 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
19543 {
19544 /* Ill-formed programs can cause infinite recursion here, so we
19545 must catch that. */
19546 TYPENAME_IS_RESOLVING_P (type) = 1;
19547 result = resolve_typename_type (result, only_current_p);
19548 TYPENAME_IS_RESOLVING_P (type) = 0;
19549 }
19550
19551 /* Qualify the resulting type. */
19552 quals = cp_type_quals (type);
19553 if (quals)
19554 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
19555
19556 return result;
19557 }
19558
19559 /* EXPR is an expression which is not type-dependent. Return a proxy
19560 for EXPR that can be used to compute the types of larger
19561 expressions containing EXPR. */
19562
19563 tree
19564 build_non_dependent_expr (tree expr)
19565 {
19566 tree inner_expr;
19567
19568 #ifdef ENABLE_CHECKING
19569 /* Try to get a constant value for all non-type-dependent expressions in
19570 order to expose bugs in *_dependent_expression_p and constexpr. */
19571 if (cxx_dialect >= cxx0x)
19572 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
19573 #endif
19574
19575 /* Preserve OVERLOADs; the functions must be available to resolve
19576 types. */
19577 inner_expr = expr;
19578 if (TREE_CODE (inner_expr) == STMT_EXPR)
19579 inner_expr = stmt_expr_value_expr (inner_expr);
19580 if (TREE_CODE (inner_expr) == ADDR_EXPR)
19581 inner_expr = TREE_OPERAND (inner_expr, 0);
19582 if (TREE_CODE (inner_expr) == COMPONENT_REF)
19583 inner_expr = TREE_OPERAND (inner_expr, 1);
19584 if (is_overloaded_fn (inner_expr)
19585 || TREE_CODE (inner_expr) == OFFSET_REF)
19586 return expr;
19587 /* There is no need to return a proxy for a variable. */
19588 if (TREE_CODE (expr) == VAR_DECL)
19589 return expr;
19590 /* Preserve string constants; conversions from string constants to
19591 "char *" are allowed, even though normally a "const char *"
19592 cannot be used to initialize a "char *". */
19593 if (TREE_CODE (expr) == STRING_CST)
19594 return expr;
19595 /* Preserve arithmetic constants, as an optimization -- there is no
19596 reason to create a new node. */
19597 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
19598 return expr;
19599 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
19600 There is at least one place where we want to know that a
19601 particular expression is a throw-expression: when checking a ?:
19602 expression, there are special rules if the second or third
19603 argument is a throw-expression. */
19604 if (TREE_CODE (expr) == THROW_EXPR)
19605 return expr;
19606
19607 if (TREE_CODE (expr) == COND_EXPR)
19608 return build3 (COND_EXPR,
19609 TREE_TYPE (expr),
19610 TREE_OPERAND (expr, 0),
19611 (TREE_OPERAND (expr, 1)
19612 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
19613 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
19614 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
19615 if (TREE_CODE (expr) == COMPOUND_EXPR
19616 && !COMPOUND_EXPR_OVERLOADED (expr))
19617 return build2 (COMPOUND_EXPR,
19618 TREE_TYPE (expr),
19619 TREE_OPERAND (expr, 0),
19620 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
19621
19622 /* If the type is unknown, it can't really be non-dependent */
19623 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
19624
19625 /* Otherwise, build a NON_DEPENDENT_EXPR. */
19626 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
19627 }
19628
19629 /* ARGS is a vector of expressions as arguments to a function call.
19630 Replace the arguments with equivalent non-dependent expressions.
19631 This modifies ARGS in place. */
19632
19633 void
19634 make_args_non_dependent (VEC(tree,gc) *args)
19635 {
19636 unsigned int ix;
19637 tree arg;
19638
19639 FOR_EACH_VEC_ELT (tree, args, ix, arg)
19640 {
19641 tree newarg = build_non_dependent_expr (arg);
19642 if (newarg != arg)
19643 VEC_replace (tree, args, ix, newarg);
19644 }
19645 }
19646
19647 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
19648 with a level one deeper than the actual template parms. */
19649
19650 tree
19651 make_auto (void)
19652 {
19653 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
19654 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
19655 TYPE_DECL, get_identifier ("auto"), au);
19656 TYPE_STUB_DECL (au) = TYPE_NAME (au);
19657 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
19658 (0, processing_template_decl + 1, processing_template_decl + 1,
19659 0, TYPE_NAME (au), NULL_TREE);
19660 TYPE_CANONICAL (au) = canonical_type_parameter (au);
19661 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
19662 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
19663
19664 return au;
19665 }
19666
19667 /* Given type ARG, return std::initializer_list<ARG>. */
19668
19669 static tree
19670 listify (tree arg)
19671 {
19672 tree std_init_list = namespace_binding
19673 (get_identifier ("initializer_list"), std_node);
19674 tree argvec;
19675 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
19676 {
19677 error ("deducing from brace-enclosed initializer list requires "
19678 "#include <initializer_list>");
19679 return error_mark_node;
19680 }
19681 argvec = make_tree_vec (1);
19682 TREE_VEC_ELT (argvec, 0) = arg;
19683 return lookup_template_class (std_init_list, argvec, NULL_TREE,
19684 NULL_TREE, 0, tf_warning_or_error);
19685 }
19686
19687 /* Replace auto in TYPE with std::initializer_list<auto>. */
19688
19689 static tree
19690 listify_autos (tree type, tree auto_node)
19691 {
19692 tree init_auto = listify (auto_node);
19693 tree argvec = make_tree_vec (1);
19694 TREE_VEC_ELT (argvec, 0) = init_auto;
19695 if (processing_template_decl)
19696 argvec = add_to_template_args (current_template_args (), argvec);
19697 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
19698 }
19699
19700 /* walk_tree helper for do_auto_deduction. */
19701
19702 static tree
19703 contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
19704 void *type)
19705 {
19706 /* Is this a variable with the type we're looking for? */
19707 if (DECL_P (*tp)
19708 && TREE_TYPE (*tp) == type)
19709 return *tp;
19710 else
19711 return NULL_TREE;
19712 }
19713
19714 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
19715 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
19716
19717 tree
19718 do_auto_deduction (tree type, tree init, tree auto_node)
19719 {
19720 tree parms, tparms, targs;
19721 tree args[1];
19722 tree decl;
19723 int val;
19724
19725 if (processing_template_decl
19726 && (TREE_TYPE (init) == NULL_TREE
19727 || BRACE_ENCLOSED_INITIALIZER_P (init)))
19728 /* Not enough information to try this yet. */
19729 return type;
19730
19731 /* The name of the object being declared shall not appear in the
19732 initializer expression. */
19733 decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
19734 if (decl)
19735 {
19736 error ("variable %q#D with %<auto%> type used in its own "
19737 "initializer", decl);
19738 return error_mark_node;
19739 }
19740
19741 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
19742 with either a new invented type template parameter U or, if the
19743 initializer is a braced-init-list (8.5.4), with
19744 std::initializer_list<U>. */
19745 if (BRACE_ENCLOSED_INITIALIZER_P (init))
19746 type = listify_autos (type, auto_node);
19747
19748 init = resolve_nondeduced_context (init);
19749
19750 parms = build_tree_list (NULL_TREE, type);
19751 args[0] = init;
19752 tparms = make_tree_vec (1);
19753 targs = make_tree_vec (1);
19754 TREE_VEC_ELT (tparms, 0)
19755 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
19756 val = type_unification_real (tparms, targs, parms, args, 1, 0,
19757 DEDUCE_CALL, LOOKUP_NORMAL,
19758 /*explain_p=*/false);
19759 if (val > 0)
19760 {
19761 if (processing_template_decl)
19762 /* Try again at instantiation time. */
19763 return type;
19764 if (type && type != error_mark_node)
19765 /* If type is error_mark_node a diagnostic must have been
19766 emitted by now. Also, having a mention to '<type error>'
19767 in the diagnostic is not really useful to the user. */
19768 error ("unable to deduce %qT from %qE", type, init);
19769 return error_mark_node;
19770 }
19771
19772 /* If the list of declarators contains more than one declarator, the type
19773 of each declared variable is determined as described above. If the
19774 type deduced for the template parameter U is not the same in each
19775 deduction, the program is ill-formed. */
19776 if (TREE_TYPE (auto_node)
19777 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
19778 {
19779 error ("inconsistent deduction for %qT: %qT and then %qT",
19780 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
19781 return error_mark_node;
19782 }
19783 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
19784
19785 if (processing_template_decl)
19786 targs = add_to_template_args (current_template_args (), targs);
19787 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
19788 }
19789
19790 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
19791 result. */
19792
19793 tree
19794 splice_late_return_type (tree type, tree late_return_type)
19795 {
19796 tree argvec;
19797
19798 if (late_return_type == NULL_TREE)
19799 return type;
19800 argvec = make_tree_vec (1);
19801 TREE_VEC_ELT (argvec, 0) = late_return_type;
19802 if (processing_template_parmlist)
19803 /* For a late-specified return type in a template type-parameter, we
19804 need to add a dummy argument level for its parmlist. */
19805 argvec = add_to_template_args
19806 (make_tree_vec (processing_template_parmlist), argvec);
19807 if (current_template_parms)
19808 argvec = add_to_template_args (current_template_args (), argvec);
19809 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
19810 }
19811
19812 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
19813
19814 bool
19815 is_auto (const_tree type)
19816 {
19817 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19818 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
19819 return true;
19820 else
19821 return false;
19822 }
19823
19824 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
19825 appear as a type-specifier for the declaration in question, we don't
19826 have to look through the whole type. */
19827
19828 tree
19829 type_uses_auto (tree type)
19830 {
19831 enum tree_code code;
19832 if (is_auto (type))
19833 return type;
19834
19835 code = TREE_CODE (type);
19836
19837 if (code == POINTER_TYPE || code == REFERENCE_TYPE
19838 || code == OFFSET_TYPE || code == FUNCTION_TYPE
19839 || code == METHOD_TYPE || code == ARRAY_TYPE)
19840 return type_uses_auto (TREE_TYPE (type));
19841
19842 if (TYPE_PTRMEMFUNC_P (type))
19843 return type_uses_auto (TREE_TYPE (TREE_TYPE
19844 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
19845
19846 return NULL_TREE;
19847 }
19848
19849 /* For a given template T, return the vector of typedefs referenced
19850 in T for which access check is needed at T instantiation time.
19851 T is either a FUNCTION_DECL or a RECORD_TYPE.
19852 Those typedefs were added to T by the function
19853 append_type_to_template_for_access_check. */
19854
19855 VEC(qualified_typedef_usage_t,gc)*
19856 get_types_needing_access_check (tree t)
19857 {
19858 tree ti;
19859 VEC(qualified_typedef_usage_t,gc) *result = NULL;
19860
19861 if (!t || t == error_mark_node)
19862 return NULL;
19863
19864 if (!(ti = get_template_info (t)))
19865 return NULL;
19866
19867 if (CLASS_TYPE_P (t)
19868 || TREE_CODE (t) == FUNCTION_DECL)
19869 {
19870 if (!TI_TEMPLATE (ti))
19871 return NULL;
19872
19873 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
19874 }
19875
19876 return result;
19877 }
19878
19879 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
19880 tied to T. That list of typedefs will be access checked at
19881 T instantiation time.
19882 T is either a FUNCTION_DECL or a RECORD_TYPE.
19883 TYPE_DECL is a TYPE_DECL node representing a typedef.
19884 SCOPE is the scope through which TYPE_DECL is accessed.
19885 LOCATION is the location of the usage point of TYPE_DECL.
19886
19887 This function is a subroutine of
19888 append_type_to_template_for_access_check. */
19889
19890 static void
19891 append_type_to_template_for_access_check_1 (tree t,
19892 tree type_decl,
19893 tree scope,
19894 location_t location)
19895 {
19896 qualified_typedef_usage_t typedef_usage;
19897 tree ti;
19898
19899 if (!t || t == error_mark_node)
19900 return;
19901
19902 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
19903 || CLASS_TYPE_P (t))
19904 && type_decl
19905 && TREE_CODE (type_decl) == TYPE_DECL
19906 && scope);
19907
19908 if (!(ti = get_template_info (t)))
19909 return;
19910
19911 gcc_assert (TI_TEMPLATE (ti));
19912
19913 typedef_usage.typedef_decl = type_decl;
19914 typedef_usage.context = scope;
19915 typedef_usage.locus = location;
19916
19917 VEC_safe_push (qualified_typedef_usage_t, gc,
19918 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
19919 &typedef_usage);
19920 }
19921
19922 /* Append TYPE_DECL to the template TEMPL.
19923 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
19924 At TEMPL instanciation time, TYPE_DECL will be checked to see
19925 if it can be accessed through SCOPE.
19926 LOCATION is the location of the usage point of TYPE_DECL.
19927
19928 e.g. consider the following code snippet:
19929
19930 class C
19931 {
19932 typedef int myint;
19933 };
19934
19935 template<class U> struct S
19936 {
19937 C::myint mi; // <-- usage point of the typedef C::myint
19938 };
19939
19940 S<char> s;
19941
19942 At S<char> instantiation time, we need to check the access of C::myint
19943 In other words, we need to check the access of the myint typedef through
19944 the C scope. For that purpose, this function will add the myint typedef
19945 and the scope C through which its being accessed to a list of typedefs
19946 tied to the template S. That list will be walked at template instantiation
19947 time and access check performed on each typedefs it contains.
19948 Note that this particular code snippet should yield an error because
19949 myint is private to C. */
19950
19951 void
19952 append_type_to_template_for_access_check (tree templ,
19953 tree type_decl,
19954 tree scope,
19955 location_t location)
19956 {
19957 qualified_typedef_usage_t *iter;
19958 int i;
19959
19960 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
19961
19962 /* Make sure we don't append the type to the template twice. */
19963 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
19964 get_types_needing_access_check (templ),
19965 i, iter)
19966 if (iter->typedef_decl == type_decl && scope == iter->context)
19967 return;
19968
19969 append_type_to_template_for_access_check_1 (templ, type_decl,
19970 scope, location);
19971 }
19972
19973 /* Set up the hash tables for template instantiations. */
19974
19975 void
19976 init_template_processing (void)
19977 {
19978 decl_specializations = htab_create_ggc (37,
19979 hash_specialization,
19980 eq_specializations,
19981 ggc_free);
19982 type_specializations = htab_create_ggc (37,
19983 hash_specialization,
19984 eq_specializations,
19985 ggc_free);
19986 }
19987
19988 /* Print stats about the template hash tables for -fstats. */
19989
19990 void
19991 print_template_statistics (void)
19992 {
19993 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
19994 "%f collisions\n", (long) htab_size (decl_specializations),
19995 (long) htab_elements (decl_specializations),
19996 htab_collisions (decl_specializations));
19997 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
19998 "%f collisions\n", (long) htab_size (type_specializations),
19999 (long) htab_elements (type_specializations),
20000 htab_collisions (type_specializations));
20001 }
20002
20003 #include "gt-cp-pt.h"