tree.h (tree_decl_common::lang_flag_8): New.
[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
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 "obstack.h"
34 #include "tree.h"
35 #include "pointer-set.h"
36 #include "flags.h"
37 #include "c-common.h"
38 #include "cp-tree.h"
39 #include "cp-objcp-common.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "output.h"
43 #include "except.h"
44 #include "toplev.h"
45 #include "rtl.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
48 #include "vecprim.h"
49
50 /* The type of functions taking a tree, and some additional data, and
51 returning an int. */
52 typedef int (*tree_fn_t) (tree, void*);
53
54 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
56 were not yet available, or because we were putting off doing the work. */
57 struct GTY (()) pending_template {
58 struct pending_template *next;
59 struct tinst_level *tinst;
60 };
61
62 static GTY(()) struct pending_template *pending_templates;
63 static GTY(()) struct pending_template *last_pending_template;
64
65 int processing_template_parmlist;
66 static int template_header_count;
67
68 static GTY(()) tree saved_trees;
69 static VEC(int,heap) *inline_parm_levels;
70
71 static GTY(()) struct tinst_level *current_tinst_level;
72
73 static GTY(()) tree saved_access_scope;
74
75 /* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78 static tree cur_stmt_expr;
79
80 /* A map from local variable declarations in the body of the template
81 presently being instantiated to the corresponding instantiated
82 local variables. */
83 static htab_t local_specializations;
84
85 typedef struct GTY(()) spec_entry
86 {
87 tree tmpl;
88 tree args;
89 tree spec;
90 } spec_entry;
91
92 static GTY ((param_is (spec_entry)))
93 htab_t decl_specializations;
94
95 static GTY ((param_is (spec_entry)))
96 htab_t type_specializations;
97
98 /* Contains canonical template parameter types. The vector is indexed by
99 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
100 TREE_LIST, whose TREE_VALUEs contain the canonical template
101 parameters of various types and levels. */
102 static GTY(()) VEC(tree,gc) *canonical_template_parms;
103
104 #define UNIFY_ALLOW_NONE 0
105 #define UNIFY_ALLOW_MORE_CV_QUAL 1
106 #define UNIFY_ALLOW_LESS_CV_QUAL 2
107 #define UNIFY_ALLOW_DERIVED 4
108 #define UNIFY_ALLOW_INTEGER 8
109 #define UNIFY_ALLOW_OUTER_LEVEL 16
110 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
111 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112
113 static void push_access_scope (tree);
114 static void pop_access_scope (tree);
115 static bool resolve_overloaded_unification (tree, tree, tree, tree,
116 unification_kind_t, int);
117 static int try_one_overload (tree, tree, tree, tree, tree,
118 unification_kind_t, int, bool);
119 static int unify (tree, tree, tree, tree, int);
120 static void add_pending_template (tree);
121 static int push_tinst_level (tree);
122 static void pop_tinst_level (void);
123 static tree reopen_tinst_level (struct tinst_level *);
124 static tree tsubst_initializer_list (tree, tree);
125 static tree get_class_bindings (tree, tree, tree);
126 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
127 bool, bool);
128 static void tsubst_enum (tree, tree, tree);
129 static tree add_to_template_args (tree, tree);
130 static tree add_outermost_template_args (tree, tree);
131 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
132 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
133 tree);
134 static int type_unification_real (tree, tree, tree, const tree *,
135 unsigned int, int, unification_kind_t, int);
136 static void note_template_header (int);
137 static tree convert_nontype_argument_function (tree, tree);
138 static tree convert_nontype_argument (tree, tree);
139 static tree convert_template_argument (tree, tree, tree,
140 tsubst_flags_t, int, tree);
141 static int for_each_template_parm (tree, tree_fn_t, void*,
142 struct pointer_set_t*, bool);
143 static tree expand_template_argument_pack (tree);
144 static tree build_template_parm_index (int, int, int, tree, tree);
145 static bool inline_needs_template_parms (tree);
146 static void push_inline_template_parms_recursive (tree, int);
147 static tree retrieve_local_specialization (tree);
148 static void register_local_specialization (tree, tree);
149 static hashval_t hash_specialization (const void *p);
150 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
151 static int mark_template_parm (tree, void *);
152 static int template_parm_this_level_p (tree, void *);
153 static tree tsubst_friend_function (tree, tree);
154 static tree tsubst_friend_class (tree, tree);
155 static int can_complete_type_without_circularity (tree);
156 static tree get_bindings (tree, tree, tree, bool);
157 static int template_decl_level (tree);
158 static int check_cv_quals_for_unify (int, tree, tree);
159 static void template_parm_level_and_index (tree, int*, int*);
160 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
161 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
162 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
163 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
164 static void regenerate_decl_from_template (tree, tree);
165 static tree most_specialized_class (tree, tree);
166 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
167 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
168 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
169 static bool check_specialization_scope (void);
170 static tree process_partial_specialization (tree);
171 static void set_current_access_from_decl (tree);
172 static tree get_template_base (tree, tree, tree, tree);
173 static tree try_class_unification (tree, tree, tree, tree);
174 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
175 tree, tree);
176 static bool template_template_parm_bindings_ok_p (tree, tree);
177 static int template_args_equal (tree, tree);
178 static void tsubst_default_arguments (tree);
179 static tree for_each_template_parm_r (tree *, int *, void *);
180 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
181 static void copy_default_args_to_explicit_spec (tree);
182 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
183 static int eq_local_specializations (const void *, const void *);
184 static bool dependent_template_arg_p (tree);
185 static bool any_template_arguments_need_structural_equality_p (tree);
186 static bool dependent_type_p_r (tree);
187 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
188 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
190 static tree tsubst_decl (tree, tree, tsubst_flags_t);
191 static void perform_typedefs_access_check (tree tmpl, tree targs);
192 static void append_type_to_template_for_access_check_1 (tree, tree, tree);
193 static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
194 static bool primary_template_instantiation_p (const_tree);
195 static tree listify (tree);
196 static tree listify_autos (tree, tree);
197
198 /* Make the current scope suitable for access checking when we are
199 processing T. T can be FUNCTION_DECL for instantiated function
200 template, or VAR_DECL for static member variable (need by
201 instantiate_decl). */
202
203 static void
204 push_access_scope (tree t)
205 {
206 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
207 || TREE_CODE (t) == VAR_DECL);
208
209 if (DECL_FRIEND_CONTEXT (t))
210 push_nested_class (DECL_FRIEND_CONTEXT (t));
211 else if (DECL_CLASS_SCOPE_P (t))
212 push_nested_class (DECL_CONTEXT (t));
213 else
214 push_to_top_level ();
215
216 if (TREE_CODE (t) == FUNCTION_DECL)
217 {
218 saved_access_scope = tree_cons
219 (NULL_TREE, current_function_decl, saved_access_scope);
220 current_function_decl = t;
221 }
222 }
223
224 /* Restore the scope set up by push_access_scope. T is the node we
225 are processing. */
226
227 static void
228 pop_access_scope (tree t)
229 {
230 if (TREE_CODE (t) == FUNCTION_DECL)
231 {
232 current_function_decl = TREE_VALUE (saved_access_scope);
233 saved_access_scope = TREE_CHAIN (saved_access_scope);
234 }
235
236 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
237 pop_nested_class ();
238 else
239 pop_from_top_level ();
240 }
241
242 /* Do any processing required when DECL (a member template
243 declaration) is finished. Returns the TEMPLATE_DECL corresponding
244 to DECL, unless it is a specialization, in which case the DECL
245 itself is returned. */
246
247 tree
248 finish_member_template_decl (tree decl)
249 {
250 if (decl == error_mark_node)
251 return error_mark_node;
252
253 gcc_assert (DECL_P (decl));
254
255 if (TREE_CODE (decl) == TYPE_DECL)
256 {
257 tree type;
258
259 type = TREE_TYPE (decl);
260 if (type == error_mark_node)
261 return error_mark_node;
262 if (MAYBE_CLASS_TYPE_P (type)
263 && CLASSTYPE_TEMPLATE_INFO (type)
264 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
265 {
266 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
267 check_member_template (tmpl);
268 return tmpl;
269 }
270 return NULL_TREE;
271 }
272 else if (TREE_CODE (decl) == FIELD_DECL)
273 error ("data member %qD cannot be a member template", decl);
274 else if (DECL_TEMPLATE_INFO (decl))
275 {
276 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
277 {
278 check_member_template (DECL_TI_TEMPLATE (decl));
279 return DECL_TI_TEMPLATE (decl);
280 }
281 else
282 return decl;
283 }
284 else
285 error ("invalid member template declaration %qD", decl);
286
287 return error_mark_node;
288 }
289
290 /* Return the template info node corresponding to T, whatever T is. */
291
292 tree
293 get_template_info (const_tree t)
294 {
295 tree tinfo = NULL_TREE;
296
297 if (!t || t == error_mark_node)
298 return NULL;
299
300 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
301 tinfo = DECL_TEMPLATE_INFO (t);
302
303 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
304 t = TREE_TYPE (t);
305
306 if (TAGGED_TYPE_P (t))
307 tinfo = TYPE_TEMPLATE_INFO (t);
308
309 return tinfo;
310 }
311
312 /* Returns the template nesting level of the indicated class TYPE.
313
314 For example, in:
315 template <class T>
316 struct A
317 {
318 template <class U>
319 struct B {};
320 };
321
322 A<T>::B<U> has depth two, while A<T> has depth one.
323 Both A<T>::B<int> and A<int>::B<U> have depth one, if
324 they are instantiations, not specializations.
325
326 This function is guaranteed to return 0 if passed NULL_TREE so
327 that, for example, `template_class_depth (current_class_type)' is
328 always safe. */
329
330 int
331 template_class_depth (tree type)
332 {
333 int depth;
334
335 for (depth = 0;
336 type && TREE_CODE (type) != NAMESPACE_DECL;
337 type = (TREE_CODE (type) == FUNCTION_DECL)
338 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
339 {
340 tree tinfo = get_template_info (type);
341
342 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
343 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
344 ++depth;
345 }
346
347 return depth;
348 }
349
350 /* Subroutine of maybe_begin_member_template_processing.
351 Returns true if processing DECL needs us to push template parms. */
352
353 static bool
354 inline_needs_template_parms (tree decl)
355 {
356 if (! DECL_TEMPLATE_INFO (decl))
357 return false;
358
359 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
360 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
361 }
362
363 /* Subroutine of maybe_begin_member_template_processing.
364 Push the template parms in PARMS, starting from LEVELS steps into the
365 chain, and ending at the beginning, since template parms are listed
366 innermost first. */
367
368 static void
369 push_inline_template_parms_recursive (tree parmlist, int levels)
370 {
371 tree parms = TREE_VALUE (parmlist);
372 int i;
373
374 if (levels > 1)
375 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
376
377 ++processing_template_decl;
378 current_template_parms
379 = tree_cons (size_int (processing_template_decl),
380 parms, current_template_parms);
381 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
382
383 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
384 NULL);
385 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
386 {
387 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
388
389 if (parm == error_mark_node)
390 continue;
391
392 gcc_assert (DECL_P (parm));
393
394 switch (TREE_CODE (parm))
395 {
396 case TYPE_DECL:
397 case TEMPLATE_DECL:
398 pushdecl (parm);
399 break;
400
401 case PARM_DECL:
402 {
403 /* Make a CONST_DECL as is done in process_template_parm.
404 It is ugly that we recreate this here; the original
405 version built in process_template_parm is no longer
406 available. */
407 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
408 CONST_DECL, DECL_NAME (parm),
409 TREE_TYPE (parm));
410 DECL_ARTIFICIAL (decl) = 1;
411 TREE_CONSTANT (decl) = 1;
412 TREE_READONLY (decl) = 1;
413 DECL_INITIAL (decl) = DECL_INITIAL (parm);
414 SET_DECL_TEMPLATE_PARM_P (decl);
415 pushdecl (decl);
416 }
417 break;
418
419 default:
420 gcc_unreachable ();
421 }
422 }
423 }
424
425 /* Restore the template parameter context for a member template or
426 a friend template defined in a class definition. */
427
428 void
429 maybe_begin_member_template_processing (tree decl)
430 {
431 tree parms;
432 int levels = 0;
433
434 if (inline_needs_template_parms (decl))
435 {
436 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
437 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
438
439 if (DECL_TEMPLATE_SPECIALIZATION (decl))
440 {
441 --levels;
442 parms = TREE_CHAIN (parms);
443 }
444
445 push_inline_template_parms_recursive (parms, levels);
446 }
447
448 /* Remember how many levels of template parameters we pushed so that
449 we can pop them later. */
450 VEC_safe_push (int, heap, inline_parm_levels, levels);
451 }
452
453 /* Undo the effects of maybe_begin_member_template_processing. */
454
455 void
456 maybe_end_member_template_processing (void)
457 {
458 int i;
459 int last;
460
461 if (VEC_length (int, inline_parm_levels) == 0)
462 return;
463
464 last = VEC_pop (int, inline_parm_levels);
465 for (i = 0; i < last; ++i)
466 {
467 --processing_template_decl;
468 current_template_parms = TREE_CHAIN (current_template_parms);
469 poplevel (0, 0, 0);
470 }
471 }
472
473 /* Return a new template argument vector which contains all of ARGS,
474 but has as its innermost set of arguments the EXTRA_ARGS. */
475
476 static tree
477 add_to_template_args (tree args, tree extra_args)
478 {
479 tree new_args;
480 int extra_depth;
481 int i;
482 int j;
483
484 extra_depth = TMPL_ARGS_DEPTH (extra_args);
485 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
486
487 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
488 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
489
490 for (j = 1; j <= extra_depth; ++j, ++i)
491 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
492
493 return new_args;
494 }
495
496 /* Like add_to_template_args, but only the outermost ARGS are added to
497 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
498 (EXTRA_ARGS) levels are added. This function is used to combine
499 the template arguments from a partial instantiation with the
500 template arguments used to attain the full instantiation from the
501 partial instantiation. */
502
503 static tree
504 add_outermost_template_args (tree args, tree extra_args)
505 {
506 tree new_args;
507
508 /* If there are more levels of EXTRA_ARGS than there are ARGS,
509 something very fishy is going on. */
510 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
511
512 /* If *all* the new arguments will be the EXTRA_ARGS, just return
513 them. */
514 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
515 return extra_args;
516
517 /* For the moment, we make ARGS look like it contains fewer levels. */
518 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
519
520 new_args = add_to_template_args (args, extra_args);
521
522 /* Now, we restore ARGS to its full dimensions. */
523 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
524
525 return new_args;
526 }
527
528 /* Return the N levels of innermost template arguments from the ARGS. */
529
530 tree
531 get_innermost_template_args (tree args, int n)
532 {
533 tree new_args;
534 int extra_levels;
535 int i;
536
537 gcc_assert (n >= 0);
538
539 /* If N is 1, just return the innermost set of template arguments. */
540 if (n == 1)
541 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
542
543 /* If we're not removing anything, just return the arguments we were
544 given. */
545 extra_levels = TMPL_ARGS_DEPTH (args) - n;
546 gcc_assert (extra_levels >= 0);
547 if (extra_levels == 0)
548 return args;
549
550 /* Make a new set of arguments, not containing the outer arguments. */
551 new_args = make_tree_vec (n);
552 for (i = 1; i <= n; ++i)
553 SET_TMPL_ARGS_LEVEL (new_args, i,
554 TMPL_ARGS_LEVEL (args, i + extra_levels));
555
556 return new_args;
557 }
558
559 /* The inverse of get_innermost_template_args: Return all but the innermost
560 EXTRA_LEVELS levels of template arguments from the ARGS. */
561
562 static tree
563 strip_innermost_template_args (tree args, int extra_levels)
564 {
565 tree new_args;
566 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
567 int i;
568
569 gcc_assert (n >= 0);
570
571 /* If N is 1, just return the outermost set of template arguments. */
572 if (n == 1)
573 return TMPL_ARGS_LEVEL (args, 1);
574
575 /* If we're not removing anything, just return the arguments we were
576 given. */
577 gcc_assert (extra_levels >= 0);
578 if (extra_levels == 0)
579 return args;
580
581 /* Make a new set of arguments, not containing the inner arguments. */
582 new_args = make_tree_vec (n);
583 for (i = 1; i <= n; ++i)
584 SET_TMPL_ARGS_LEVEL (new_args, i,
585 TMPL_ARGS_LEVEL (args, i));
586
587 return new_args;
588 }
589
590 /* We've got a template header coming up; push to a new level for storing
591 the parms. */
592
593 void
594 begin_template_parm_list (void)
595 {
596 /* We use a non-tag-transparent scope here, which causes pushtag to
597 put tags in this scope, rather than in the enclosing class or
598 namespace scope. This is the right thing, since we want
599 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
600 global template class, push_template_decl handles putting the
601 TEMPLATE_DECL into top-level scope. For a nested template class,
602 e.g.:
603
604 template <class T> struct S1 {
605 template <class T> struct S2 {};
606 };
607
608 pushtag contains special code to call pushdecl_with_scope on the
609 TEMPLATE_DECL for S2. */
610 begin_scope (sk_template_parms, NULL);
611 ++processing_template_decl;
612 ++processing_template_parmlist;
613 note_template_header (0);
614 }
615
616 /* This routine is called when a specialization is declared. If it is
617 invalid to declare a specialization here, an error is reported and
618 false is returned, otherwise this routine will return true. */
619
620 static bool
621 check_specialization_scope (void)
622 {
623 tree scope = current_scope ();
624
625 /* [temp.expl.spec]
626
627 An explicit specialization shall be declared in the namespace of
628 which the template is a member, or, for member templates, in the
629 namespace of which the enclosing class or enclosing class
630 template is a member. An explicit specialization of a member
631 function, member class or static data member of a class template
632 shall be declared in the namespace of which the class template
633 is a member. */
634 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
635 {
636 error ("explicit specialization in non-namespace scope %qD", scope);
637 return false;
638 }
639
640 /* [temp.expl.spec]
641
642 In an explicit specialization declaration for a member of a class
643 template or a member template that appears in namespace scope,
644 the member template and some of its enclosing class templates may
645 remain unspecialized, except that the declaration shall not
646 explicitly specialize a class member template if its enclosing
647 class templates are not explicitly specialized as well. */
648 if (current_template_parms)
649 {
650 error ("enclosing class templates are not explicitly specialized");
651 return false;
652 }
653
654 return true;
655 }
656
657 /* We've just seen template <>. */
658
659 bool
660 begin_specialization (void)
661 {
662 begin_scope (sk_template_spec, NULL);
663 note_template_header (1);
664 return check_specialization_scope ();
665 }
666
667 /* Called at then end of processing a declaration preceded by
668 template<>. */
669
670 void
671 end_specialization (void)
672 {
673 finish_scope ();
674 reset_specialization ();
675 }
676
677 /* Any template <>'s that we have seen thus far are not referring to a
678 function specialization. */
679
680 void
681 reset_specialization (void)
682 {
683 processing_specialization = 0;
684 template_header_count = 0;
685 }
686
687 /* We've just seen a template header. If SPECIALIZATION is nonzero,
688 it was of the form template <>. */
689
690 static void
691 note_template_header (int specialization)
692 {
693 processing_specialization = specialization;
694 template_header_count++;
695 }
696
697 /* We're beginning an explicit instantiation. */
698
699 void
700 begin_explicit_instantiation (void)
701 {
702 gcc_assert (!processing_explicit_instantiation);
703 processing_explicit_instantiation = true;
704 }
705
706
707 void
708 end_explicit_instantiation (void)
709 {
710 gcc_assert (processing_explicit_instantiation);
711 processing_explicit_instantiation = false;
712 }
713
714 /* An explicit specialization or partial specialization TMPL is being
715 declared. Check that the namespace in which the specialization is
716 occurring is permissible. Returns false iff it is invalid to
717 specialize TMPL in the current namespace. */
718
719 static bool
720 check_specialization_namespace (tree tmpl)
721 {
722 tree tpl_ns = decl_namespace_context (tmpl);
723
724 /* [tmpl.expl.spec]
725
726 An explicit specialization shall be declared in the namespace of
727 which the template is a member, or, for member templates, in the
728 namespace of which the enclosing class or enclosing class
729 template is a member. An explicit specialization of a member
730 function, member class or static data member of a class template
731 shall be declared in the namespace of which the class template is
732 a member. */
733 if (is_associated_namespace (current_namespace, tpl_ns))
734 /* Same or super-using namespace. */
735 return true;
736 else
737 {
738 permerror (input_location, "specialization of %qD in different namespace", tmpl);
739 permerror (input_location, " from definition of %q+#D", tmpl);
740 return false;
741 }
742 }
743
744 /* SPEC is an explicit instantiation. Check that it is valid to
745 perform this explicit instantiation in the current namespace. */
746
747 static void
748 check_explicit_instantiation_namespace (tree spec)
749 {
750 tree ns;
751
752 /* DR 275: An explicit instantiation shall appear in an enclosing
753 namespace of its template. */
754 ns = decl_namespace_context (spec);
755 if (!is_ancestor (current_namespace, ns))
756 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
757 "(which does not enclose namespace %qD)",
758 spec, current_namespace, ns);
759 }
760
761 /* The TYPE is being declared. If it is a template type, that means it
762 is a partial specialization. Do appropriate error-checking. */
763
764 tree
765 maybe_process_partial_specialization (tree type)
766 {
767 tree context;
768
769 if (type == error_mark_node)
770 return error_mark_node;
771
772 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
773 {
774 error ("name of class shadows template template parameter %qD",
775 TYPE_NAME (type));
776 return error_mark_node;
777 }
778
779 context = TYPE_CONTEXT (type);
780
781 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
782 {
783 /* This is for ordinary explicit specialization and partial
784 specialization of a template class such as:
785
786 template <> class C<int>;
787
788 or:
789
790 template <class T> class C<T*>;
791
792 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
793
794 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
795 && !COMPLETE_TYPE_P (type))
796 {
797 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
798 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
799 if (processing_template_decl)
800 {
801 if (push_template_decl (TYPE_MAIN_DECL (type))
802 == error_mark_node)
803 return error_mark_node;
804 }
805 }
806 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
807 error ("specialization of %qT after instantiation", type);
808 }
809 else if (CLASS_TYPE_P (type)
810 && !CLASSTYPE_USE_TEMPLATE (type)
811 && CLASSTYPE_TEMPLATE_INFO (type)
812 && context && CLASS_TYPE_P (context)
813 && CLASSTYPE_TEMPLATE_INFO (context))
814 {
815 /* This is for an explicit specialization of member class
816 template according to [temp.expl.spec/18]:
817
818 template <> template <class U> class C<int>::D;
819
820 The context `C<int>' must be an implicit instantiation.
821 Otherwise this is just a member class template declared
822 earlier like:
823
824 template <> class C<int> { template <class U> class D; };
825 template <> template <class U> class C<int>::D;
826
827 In the first case, `C<int>::D' is a specialization of `C<T>::D'
828 while in the second case, `C<int>::D' is a primary template
829 and `C<T>::D' may not exist. */
830
831 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
832 && !COMPLETE_TYPE_P (type))
833 {
834 tree t;
835 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
836
837 if (current_namespace
838 != decl_namespace_context (tmpl))
839 {
840 permerror (input_location, "specializing %q#T in different namespace", type);
841 permerror (input_location, " from definition of %q+#D", tmpl);
842 }
843
844 /* Check for invalid specialization after instantiation:
845
846 template <> template <> class C<int>::D<int>;
847 template <> template <class U> class C<int>::D; */
848
849 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
850 t; t = TREE_CHAIN (t))
851 {
852 tree inst = TREE_VALUE (t);
853 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
854 {
855 /* We already have a full specialization of this partial
856 instantiation. Reassign it to the new member
857 specialization template. */
858 spec_entry elt;
859 spec_entry **slot;
860
861 elt.tmpl = most_general_template (tmpl);
862 elt.args = CLASSTYPE_TI_ARGS (inst);
863 elt.spec = inst;
864
865 htab_remove_elt (type_specializations, &elt);
866
867 elt.tmpl = tmpl;
868 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
869
870 slot = (spec_entry **)
871 htab_find_slot (type_specializations, &elt, INSERT);
872 *slot = GGC_NEW (spec_entry);
873 **slot = elt;
874 }
875 else if (COMPLETE_TYPE_P (inst) || TYPE_BEING_DEFINED (inst))
876 /* But if we've had an implicit instantiation, that's a
877 problem ([temp.expl.spec]/6). */
878 error ("specialization %qT after instantiation %qT",
879 type, inst);
880 }
881
882 /* Mark TYPE as a specialization. And as a result, we only
883 have one level of template argument for the innermost
884 class template. */
885 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
886 CLASSTYPE_TI_ARGS (type)
887 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
888 }
889 }
890 else if (processing_specialization)
891 {
892 error ("explicit specialization of non-template %qT", type);
893 return error_mark_node;
894 }
895
896 return type;
897 }
898
899 /* Returns nonzero if we can optimize the retrieval of specializations
900 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
901 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
902
903 static inline bool
904 optimize_specialization_lookup_p (tree tmpl)
905 {
906 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
907 && DECL_CLASS_SCOPE_P (tmpl)
908 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
909 parameter. */
910 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
911 /* The optimized lookup depends on the fact that the
912 template arguments for the member function template apply
913 purely to the containing class, which is not true if the
914 containing class is an explicit or partial
915 specialization. */
916 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
917 && !DECL_MEMBER_TEMPLATE_P (tmpl)
918 && !DECL_CONV_FN_P (tmpl)
919 /* It is possible to have a template that is not a member
920 template and is not a member of a template class:
921
922 template <typename T>
923 struct S { friend A::f(); };
924
925 Here, the friend function is a template, but the context does
926 not have template information. The optimized lookup relies
927 on having ARGS be the template arguments for both the class
928 and the function template. */
929 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
930 }
931
932 /* Retrieve the specialization (in the sense of [temp.spec] - a
933 specialization is either an instantiation or an explicit
934 specialization) of TMPL for the given template ARGS. If there is
935 no such specialization, return NULL_TREE. The ARGS are a vector of
936 arguments, or a vector of vectors of arguments, in the case of
937 templates with more than one level of parameters.
938
939 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
940 then we search for a partial specialization matching ARGS. This
941 parameter is ignored if TMPL is not a class template. */
942
943 static tree
944 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
945 {
946 if (args == error_mark_node)
947 return NULL_TREE;
948
949 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
950
951 /* There should be as many levels of arguments as there are
952 levels of parameters. */
953 gcc_assert (TMPL_ARGS_DEPTH (args)
954 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
955
956 if (optimize_specialization_lookup_p (tmpl))
957 {
958 tree class_template;
959 tree class_specialization;
960 VEC(tree,gc) *methods;
961 tree fns;
962 int idx;
963
964 /* The template arguments actually apply to the containing
965 class. Find the class specialization with those
966 arguments. */
967 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
968 class_specialization
969 = retrieve_specialization (class_template, args, 0);
970 if (!class_specialization)
971 return NULL_TREE;
972 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
973 for the specialization. */
974 idx = class_method_index_for_fn (class_specialization, tmpl);
975 if (idx == -1)
976 return NULL_TREE;
977 /* Iterate through the methods with the indicated name, looking
978 for the one that has an instance of TMPL. */
979 methods = CLASSTYPE_METHOD_VEC (class_specialization);
980 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
981 {
982 tree fn = OVL_CURRENT (fns);
983 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
984 /* using-declarations can add base methods to the method vec,
985 and we don't want those here. */
986 && DECL_CONTEXT (fn) == class_specialization)
987 return fn;
988 }
989 return NULL_TREE;
990 }
991 else
992 {
993 spec_entry *found;
994 spec_entry elt;
995 htab_t specializations;
996
997 elt.tmpl = tmpl;
998 elt.args = args;
999 elt.spec = NULL_TREE;
1000
1001 if (DECL_CLASS_TEMPLATE_P (tmpl))
1002 specializations = type_specializations;
1003 else
1004 specializations = decl_specializations;
1005
1006 if (hash == 0)
1007 hash = hash_specialization (&elt);
1008 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1009 if (found)
1010 return found->spec;
1011 }
1012
1013 return NULL_TREE;
1014 }
1015
1016 /* Like retrieve_specialization, but for local declarations. */
1017
1018 static tree
1019 retrieve_local_specialization (tree tmpl)
1020 {
1021 tree spec;
1022
1023 if (local_specializations == NULL)
1024 return NULL_TREE;
1025
1026 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1027 htab_hash_pointer (tmpl));
1028 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1029 }
1030
1031 /* Returns nonzero iff DECL is a specialization of TMPL. */
1032
1033 int
1034 is_specialization_of (tree decl, tree tmpl)
1035 {
1036 tree t;
1037
1038 if (TREE_CODE (decl) == FUNCTION_DECL)
1039 {
1040 for (t = decl;
1041 t != NULL_TREE;
1042 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1043 if (t == tmpl)
1044 return 1;
1045 }
1046 else
1047 {
1048 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1049
1050 for (t = TREE_TYPE (decl);
1051 t != NULL_TREE;
1052 t = CLASSTYPE_USE_TEMPLATE (t)
1053 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1054 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1055 return 1;
1056 }
1057
1058 return 0;
1059 }
1060
1061 /* Returns nonzero iff DECL is a specialization of friend declaration
1062 FRIEND_DECL according to [temp.friend]. */
1063
1064 bool
1065 is_specialization_of_friend (tree decl, tree friend_decl)
1066 {
1067 bool need_template = true;
1068 int template_depth;
1069
1070 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1071 || TREE_CODE (decl) == TYPE_DECL);
1072
1073 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1074 of a template class, we want to check if DECL is a specialization
1075 if this. */
1076 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1077 && DECL_TEMPLATE_INFO (friend_decl)
1078 && !DECL_USE_TEMPLATE (friend_decl))
1079 {
1080 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1081 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1082 need_template = false;
1083 }
1084 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1085 && !PRIMARY_TEMPLATE_P (friend_decl))
1086 need_template = false;
1087
1088 /* There is nothing to do if this is not a template friend. */
1089 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1090 return false;
1091
1092 if (is_specialization_of (decl, friend_decl))
1093 return true;
1094
1095 /* [temp.friend/6]
1096 A member of a class template may be declared to be a friend of a
1097 non-template class. In this case, the corresponding member of
1098 every specialization of the class template is a friend of the
1099 class granting friendship.
1100
1101 For example, given a template friend declaration
1102
1103 template <class T> friend void A<T>::f();
1104
1105 the member function below is considered a friend
1106
1107 template <> struct A<int> {
1108 void f();
1109 };
1110
1111 For this type of template friend, TEMPLATE_DEPTH below will be
1112 nonzero. To determine if DECL is a friend of FRIEND, we first
1113 check if the enclosing class is a specialization of another. */
1114
1115 template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
1116 if (template_depth
1117 && DECL_CLASS_SCOPE_P (decl)
1118 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1119 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1120 {
1121 /* Next, we check the members themselves. In order to handle
1122 a few tricky cases, such as when FRIEND_DECL's are
1123
1124 template <class T> friend void A<T>::g(T t);
1125 template <class T> template <T t> friend void A<T>::h();
1126
1127 and DECL's are
1128
1129 void A<int>::g(int);
1130 template <int> void A<int>::h();
1131
1132 we need to figure out ARGS, the template arguments from
1133 the context of DECL. This is required for template substitution
1134 of `T' in the function parameter of `g' and template parameter
1135 of `h' in the above examples. Here ARGS corresponds to `int'. */
1136
1137 tree context = DECL_CONTEXT (decl);
1138 tree args = NULL_TREE;
1139 int current_depth = 0;
1140
1141 while (current_depth < template_depth)
1142 {
1143 if (CLASSTYPE_TEMPLATE_INFO (context))
1144 {
1145 if (current_depth == 0)
1146 args = TYPE_TI_ARGS (context);
1147 else
1148 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1149 current_depth++;
1150 }
1151 context = TYPE_CONTEXT (context);
1152 }
1153
1154 if (TREE_CODE (decl) == FUNCTION_DECL)
1155 {
1156 bool is_template;
1157 tree friend_type;
1158 tree decl_type;
1159 tree friend_args_type;
1160 tree decl_args_type;
1161
1162 /* Make sure that both DECL and FRIEND_DECL are templates or
1163 non-templates. */
1164 is_template = DECL_TEMPLATE_INFO (decl)
1165 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1166 if (need_template ^ is_template)
1167 return false;
1168 else if (is_template)
1169 {
1170 /* If both are templates, check template parameter list. */
1171 tree friend_parms
1172 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1173 args, tf_none);
1174 if (!comp_template_parms
1175 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1176 friend_parms))
1177 return false;
1178
1179 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1180 }
1181 else
1182 decl_type = TREE_TYPE (decl);
1183
1184 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1185 tf_none, NULL_TREE);
1186 if (friend_type == error_mark_node)
1187 return false;
1188
1189 /* Check if return types match. */
1190 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1191 return false;
1192
1193 /* Check if function parameter types match, ignoring the
1194 `this' parameter. */
1195 friend_args_type = TYPE_ARG_TYPES (friend_type);
1196 decl_args_type = TYPE_ARG_TYPES (decl_type);
1197 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1198 friend_args_type = TREE_CHAIN (friend_args_type);
1199 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1200 decl_args_type = TREE_CHAIN (decl_args_type);
1201
1202 return compparms (decl_args_type, friend_args_type);
1203 }
1204 else
1205 {
1206 /* DECL is a TYPE_DECL */
1207 bool is_template;
1208 tree decl_type = TREE_TYPE (decl);
1209
1210 /* Make sure that both DECL and FRIEND_DECL are templates or
1211 non-templates. */
1212 is_template
1213 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1214 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1215
1216 if (need_template ^ is_template)
1217 return false;
1218 else if (is_template)
1219 {
1220 tree friend_parms;
1221 /* If both are templates, check the name of the two
1222 TEMPLATE_DECL's first because is_friend didn't. */
1223 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1224 != DECL_NAME (friend_decl))
1225 return false;
1226
1227 /* Now check template parameter list. */
1228 friend_parms
1229 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1230 args, tf_none);
1231 return comp_template_parms
1232 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1233 friend_parms);
1234 }
1235 else
1236 return (DECL_NAME (decl)
1237 == DECL_NAME (friend_decl));
1238 }
1239 }
1240 return false;
1241 }
1242
1243 /* Register the specialization SPEC as a specialization of TMPL with
1244 the indicated ARGS. IS_FRIEND indicates whether the specialization
1245 is actually just a friend declaration. Returns SPEC, or an
1246 equivalent prior declaration, if available. */
1247
1248 static tree
1249 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1250 hashval_t hash)
1251 {
1252 tree fn;
1253 spec_entry **slot = NULL;
1254 spec_entry elt;
1255
1256 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1257
1258 if (TREE_CODE (spec) == FUNCTION_DECL
1259 && uses_template_parms (DECL_TI_ARGS (spec)))
1260 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1261 register it; we want the corresponding TEMPLATE_DECL instead.
1262 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1263 the more obvious `uses_template_parms (spec)' to avoid problems
1264 with default function arguments. In particular, given
1265 something like this:
1266
1267 template <class T> void f(T t1, T t = T())
1268
1269 the default argument expression is not substituted for in an
1270 instantiation unless and until it is actually needed. */
1271 return spec;
1272
1273 if (optimize_specialization_lookup_p (tmpl))
1274 /* We don't put these specializations in the hash table, but we might
1275 want to give an error about a mismatch. */
1276 fn = retrieve_specialization (tmpl, args, 0);
1277 else
1278 {
1279 elt.tmpl = tmpl;
1280 elt.args = args;
1281 elt.spec = spec;
1282
1283 if (hash == 0)
1284 hash = hash_specialization (&elt);
1285
1286 slot = (spec_entry **)
1287 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1288 if (*slot)
1289 fn = (*slot)->spec;
1290 else
1291 fn = NULL_TREE;
1292 }
1293
1294 /* We can sometimes try to re-register a specialization that we've
1295 already got. In particular, regenerate_decl_from_template calls
1296 duplicate_decls which will update the specialization list. But,
1297 we'll still get called again here anyhow. It's more convenient
1298 to simply allow this than to try to prevent it. */
1299 if (fn == spec)
1300 return spec;
1301 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1302 {
1303 if (DECL_TEMPLATE_INSTANTIATION (fn))
1304 {
1305 if (DECL_ODR_USED (fn)
1306 || DECL_EXPLICIT_INSTANTIATION (fn))
1307 {
1308 error ("specialization of %qD after instantiation",
1309 fn);
1310 return error_mark_node;
1311 }
1312 else
1313 {
1314 tree clone;
1315 /* This situation should occur only if the first
1316 specialization is an implicit instantiation, the
1317 second is an explicit specialization, and the
1318 implicit instantiation has not yet been used. That
1319 situation can occur if we have implicitly
1320 instantiated a member function and then specialized
1321 it later.
1322
1323 We can also wind up here if a friend declaration that
1324 looked like an instantiation turns out to be a
1325 specialization:
1326
1327 template <class T> void foo(T);
1328 class S { friend void foo<>(int) };
1329 template <> void foo(int);
1330
1331 We transform the existing DECL in place so that any
1332 pointers to it become pointers to the updated
1333 declaration.
1334
1335 If there was a definition for the template, but not
1336 for the specialization, we want this to look as if
1337 there were no definition, and vice versa. */
1338 DECL_INITIAL (fn) = NULL_TREE;
1339 duplicate_decls (spec, fn, is_friend);
1340 /* The call to duplicate_decls will have applied
1341 [temp.expl.spec]:
1342
1343 An explicit specialization of a function template
1344 is inline only if it is explicitly declared to be,
1345 and independently of whether its function template
1346 is.
1347
1348 to the primary function; now copy the inline bits to
1349 the various clones. */
1350 FOR_EACH_CLONE (clone, fn)
1351 DECL_DECLARED_INLINE_P (clone)
1352 = DECL_DECLARED_INLINE_P (fn);
1353 check_specialization_namespace (fn);
1354
1355 return fn;
1356 }
1357 }
1358 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1359 {
1360 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1361 /* Dup decl failed, but this is a new definition. Set the
1362 line number so any errors match this new
1363 definition. */
1364 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1365
1366 return fn;
1367 }
1368 }
1369 else if (fn)
1370 return duplicate_decls (spec, fn, is_friend);
1371
1372 /* A specialization must be declared in the same namespace as the
1373 template it is specializing. */
1374 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1375 && !check_specialization_namespace (tmpl))
1376 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1377
1378 if (!optimize_specialization_lookup_p (tmpl))
1379 {
1380 gcc_assert (tmpl && args && spec);
1381 *slot = GGC_NEW (spec_entry);
1382 **slot = elt;
1383 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1384 && PRIMARY_TEMPLATE_P (tmpl)
1385 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1386 /* TMPL is a forward declaration of a template function; keep a list
1387 of all specializations in case we need to reassign them to a friend
1388 template later in tsubst_friend_function. */
1389 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1390 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1391 }
1392
1393 return spec;
1394 }
1395
1396 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1397 TMPL and ARGS members, ignores SPEC. */
1398
1399 static int
1400 eq_specializations (const void *p1, const void *p2)
1401 {
1402 const spec_entry *e1 = (const spec_entry *)p1;
1403 const spec_entry *e2 = (const spec_entry *)p2;
1404
1405 return (e1->tmpl == e2->tmpl
1406 && comp_template_args (e1->args, e2->args));
1407 }
1408
1409 /* Returns a hash for a template TMPL and template arguments ARGS. */
1410
1411 static hashval_t
1412 hash_tmpl_and_args (tree tmpl, tree args)
1413 {
1414 hashval_t val = DECL_UID (tmpl);
1415 return iterative_hash_template_arg (args, val);
1416 }
1417
1418 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1419 ignoring SPEC. */
1420
1421 static hashval_t
1422 hash_specialization (const void *p)
1423 {
1424 const spec_entry *e = (const spec_entry *)p;
1425 return hash_tmpl_and_args (e->tmpl, e->args);
1426 }
1427
1428 /* Recursively calculate a hash value for a template argument ARG, for use
1429 in the hash tables of template specializations. */
1430
1431 static hashval_t
1432 iterative_hash_template_arg (tree arg, hashval_t val)
1433 {
1434 unsigned HOST_WIDE_INT i;
1435 enum tree_code code;
1436 char tclass;
1437
1438 if (arg == NULL_TREE)
1439 return iterative_hash_object (arg, val);
1440
1441 if (!TYPE_P (arg))
1442 STRIP_NOPS (arg);
1443
1444 code = TREE_CODE (arg);
1445 tclass = TREE_CODE_CLASS (code);
1446
1447 val = iterative_hash_object (code, val);
1448
1449 switch (code)
1450 {
1451 case ERROR_MARK:
1452 return val;
1453
1454 case IDENTIFIER_NODE:
1455 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1456
1457 case TREE_VEC:
1458 {
1459 int i, len = TREE_VEC_LENGTH (arg);
1460 for (i = 0; i < len; ++i)
1461 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1462 return val;
1463 }
1464
1465 case TYPE_PACK_EXPANSION:
1466 case EXPR_PACK_EXPANSION:
1467 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1468
1469 case ARGUMENT_PACK_SELECT:
1470 /* We can get one of these when re-hashing a previous entry in the middle
1471 of substituting into a pack expansion. Just look through it... */
1472 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1473 /* ...and fall through. */
1474 case TYPE_ARGUMENT_PACK:
1475 case NONTYPE_ARGUMENT_PACK:
1476 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1477
1478 case TREE_LIST:
1479 for (; arg; arg = TREE_CHAIN (arg))
1480 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1481 return val;
1482
1483 case OVERLOAD:
1484 for (; arg; arg = OVL_CHAIN (arg))
1485 val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
1486 return val;
1487
1488 case CONSTRUCTOR:
1489 {
1490 tree field, value;
1491 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1492 {
1493 val = iterative_hash_template_arg (field, val);
1494 val = iterative_hash_template_arg (value, val);
1495 }
1496 return val;
1497 }
1498
1499 case PARM_DECL:
1500 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1501 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1502
1503 case TARGET_EXPR:
1504 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1505
1506 case PTRMEM_CST:
1507 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1508 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1509
1510 case TEMPLATE_PARM_INDEX:
1511 val = iterative_hash_template_arg
1512 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1513 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1514 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1515
1516 case TRAIT_EXPR:
1517 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1518 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1519 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1520
1521 case BASELINK:
1522 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1523 val);
1524 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1525 val);
1526
1527 case MODOP_EXPR:
1528 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1529 code = TREE_CODE (TREE_OPERAND (arg, 1));
1530 val = iterative_hash_object (code, val);
1531 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1532
1533 default:
1534 switch (tclass)
1535 {
1536 case tcc_type:
1537 if (TYPE_CANONICAL (arg))
1538 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1539 val);
1540 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1541 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1542 /* Otherwise just compare the types during lookup. */
1543 return val;
1544
1545 case tcc_declaration:
1546 case tcc_constant:
1547 return iterative_hash_expr (arg, val);
1548
1549 default:
1550 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1551 {
1552 unsigned n = TREE_OPERAND_LENGTH (arg);
1553 for (i = 0; i < n; ++i)
1554 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1555 return val;
1556 }
1557 }
1558 }
1559 gcc_unreachable ();
1560 return 0;
1561 }
1562
1563 /* Unregister the specialization SPEC as a specialization of TMPL.
1564 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1565 if the SPEC was listed as a specialization of TMPL.
1566
1567 Note that SPEC has been ggc_freed, so we can't look inside it. */
1568
1569 bool
1570 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1571 {
1572 spec_entry **slot;
1573 spec_entry elt;
1574
1575 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1576 elt.args = TI_ARGS (tinfo);
1577 elt.spec = NULL_TREE;
1578
1579 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1580 if (*slot)
1581 {
1582 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1583 gcc_assert (new_spec != NULL_TREE);
1584 (*slot)->spec = new_spec;
1585 return 1;
1586 }
1587
1588 return 0;
1589 }
1590
1591 /* Compare an entry in the local specializations hash table P1 (which
1592 is really a pointer to a TREE_LIST) with P2 (which is really a
1593 DECL). */
1594
1595 static int
1596 eq_local_specializations (const void *p1, const void *p2)
1597 {
1598 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1599 }
1600
1601 /* Hash P1, an entry in the local specializations table. */
1602
1603 static hashval_t
1604 hash_local_specialization (const void* p1)
1605 {
1606 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1607 }
1608
1609 /* Like register_specialization, but for local declarations. We are
1610 registering SPEC, an instantiation of TMPL. */
1611
1612 static void
1613 register_local_specialization (tree spec, tree tmpl)
1614 {
1615 void **slot;
1616
1617 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1618 htab_hash_pointer (tmpl), INSERT);
1619 *slot = build_tree_list (spec, tmpl);
1620 }
1621
1622 /* TYPE is a class type. Returns true if TYPE is an explicitly
1623 specialized class. */
1624
1625 bool
1626 explicit_class_specialization_p (tree type)
1627 {
1628 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1629 return false;
1630 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1631 }
1632
1633 /* Print the list of candidate FNS in an error message. */
1634
1635 void
1636 print_candidates (tree fns)
1637 {
1638 tree fn;
1639
1640 const char *str = "candidates are:";
1641
1642 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1643 {
1644 tree f;
1645
1646 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1647 error ("%s %+#D", str, OVL_CURRENT (f));
1648 str = " ";
1649 }
1650 }
1651
1652 /* Returns the template (one of the functions given by TEMPLATE_ID)
1653 which can be specialized to match the indicated DECL with the
1654 explicit template args given in TEMPLATE_ID. The DECL may be
1655 NULL_TREE if none is available. In that case, the functions in
1656 TEMPLATE_ID are non-members.
1657
1658 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1659 specialization of a member template.
1660
1661 The TEMPLATE_COUNT is the number of references to qualifying
1662 template classes that appeared in the name of the function. See
1663 check_explicit_specialization for a more accurate description.
1664
1665 TSK indicates what kind of template declaration (if any) is being
1666 declared. TSK_TEMPLATE indicates that the declaration given by
1667 DECL, though a FUNCTION_DECL, has template parameters, and is
1668 therefore a template function.
1669
1670 The template args (those explicitly specified and those deduced)
1671 are output in a newly created vector *TARGS_OUT.
1672
1673 If it is impossible to determine the result, an error message is
1674 issued. The error_mark_node is returned to indicate failure. */
1675
1676 static tree
1677 determine_specialization (tree template_id,
1678 tree decl,
1679 tree* targs_out,
1680 int need_member_template,
1681 int template_count,
1682 tmpl_spec_kind tsk)
1683 {
1684 tree fns;
1685 tree targs;
1686 tree explicit_targs;
1687 tree candidates = NULL_TREE;
1688 /* A TREE_LIST of templates of which DECL may be a specialization.
1689 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1690 corresponding TREE_PURPOSE is the set of template arguments that,
1691 when used to instantiate the template, would produce a function
1692 with the signature of DECL. */
1693 tree templates = NULL_TREE;
1694 int header_count;
1695 struct cp_binding_level *b;
1696
1697 *targs_out = NULL_TREE;
1698
1699 if (template_id == error_mark_node || decl == error_mark_node)
1700 return error_mark_node;
1701
1702 fns = TREE_OPERAND (template_id, 0);
1703 explicit_targs = TREE_OPERAND (template_id, 1);
1704
1705 if (fns == error_mark_node)
1706 return error_mark_node;
1707
1708 /* Check for baselinks. */
1709 if (BASELINK_P (fns))
1710 fns = BASELINK_FUNCTIONS (fns);
1711
1712 if (!is_overloaded_fn (fns))
1713 {
1714 error ("%qD is not a function template", fns);
1715 return error_mark_node;
1716 }
1717
1718 /* Count the number of template headers specified for this
1719 specialization. */
1720 header_count = 0;
1721 for (b = current_binding_level;
1722 b->kind == sk_template_parms;
1723 b = b->level_chain)
1724 ++header_count;
1725
1726 for (; fns; fns = OVL_NEXT (fns))
1727 {
1728 tree fn = OVL_CURRENT (fns);
1729
1730 if (TREE_CODE (fn) == TEMPLATE_DECL)
1731 {
1732 tree decl_arg_types;
1733 tree fn_arg_types;
1734
1735 /* In case of explicit specialization, we need to check if
1736 the number of template headers appearing in the specialization
1737 is correct. This is usually done in check_explicit_specialization,
1738 but the check done there cannot be exhaustive when specializing
1739 member functions. Consider the following code:
1740
1741 template <> void A<int>::f(int);
1742 template <> template <> void A<int>::f(int);
1743
1744 Assuming that A<int> is not itself an explicit specialization
1745 already, the first line specializes "f" which is a non-template
1746 member function, whilst the second line specializes "f" which
1747 is a template member function. So both lines are syntactically
1748 correct, and check_explicit_specialization does not reject
1749 them.
1750
1751 Here, we can do better, as we are matching the specialization
1752 against the declarations. We count the number of template
1753 headers, and we check if they match TEMPLATE_COUNT + 1
1754 (TEMPLATE_COUNT is the number of qualifying template classes,
1755 plus there must be another header for the member template
1756 itself).
1757
1758 Notice that if header_count is zero, this is not a
1759 specialization but rather a template instantiation, so there
1760 is no check we can perform here. */
1761 if (header_count && header_count != template_count + 1)
1762 continue;
1763
1764 /* Check that the number of template arguments at the
1765 innermost level for DECL is the same as for FN. */
1766 if (current_binding_level->kind == sk_template_parms
1767 && !current_binding_level->explicit_spec_p
1768 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1769 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1770 (current_template_parms))))
1771 continue;
1772
1773 /* DECL might be a specialization of FN. */
1774 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1775 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1776
1777 /* For a non-static member function, we need to make sure
1778 that the const qualification is the same. Since
1779 get_bindings does not try to merge the "this" parameter,
1780 we must do the comparison explicitly. */
1781 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1782 && !same_type_p (TREE_VALUE (fn_arg_types),
1783 TREE_VALUE (decl_arg_types)))
1784 continue;
1785
1786 /* Skip the "this" parameter and, for constructors of
1787 classes with virtual bases, the VTT parameter. A
1788 full specialization of a constructor will have a VTT
1789 parameter, but a template never will. */
1790 decl_arg_types
1791 = skip_artificial_parms_for (decl, decl_arg_types);
1792 fn_arg_types
1793 = skip_artificial_parms_for (fn, fn_arg_types);
1794
1795 /* Check that the number of function parameters matches.
1796 For example,
1797 template <class T> void f(int i = 0);
1798 template <> void f<int>();
1799 The specialization f<int> is invalid but is not caught
1800 by get_bindings below. */
1801 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1802 continue;
1803
1804 /* Function templates cannot be specializations; there are
1805 no partial specializations of functions. Therefore, if
1806 the type of DECL does not match FN, there is no
1807 match. */
1808 if (tsk == tsk_template)
1809 {
1810 if (compparms (fn_arg_types, decl_arg_types))
1811 candidates = tree_cons (NULL_TREE, fn, candidates);
1812 continue;
1813 }
1814
1815 /* See whether this function might be a specialization of this
1816 template. */
1817 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1818
1819 if (!targs)
1820 /* We cannot deduce template arguments that when used to
1821 specialize TMPL will produce DECL. */
1822 continue;
1823
1824 /* Save this template, and the arguments deduced. */
1825 templates = tree_cons (targs, fn, templates);
1826 }
1827 else if (need_member_template)
1828 /* FN is an ordinary member function, and we need a
1829 specialization of a member template. */
1830 ;
1831 else if (TREE_CODE (fn) != FUNCTION_DECL)
1832 /* We can get IDENTIFIER_NODEs here in certain erroneous
1833 cases. */
1834 ;
1835 else if (!DECL_FUNCTION_MEMBER_P (fn))
1836 /* This is just an ordinary non-member function. Nothing can
1837 be a specialization of that. */
1838 ;
1839 else if (DECL_ARTIFICIAL (fn))
1840 /* Cannot specialize functions that are created implicitly. */
1841 ;
1842 else
1843 {
1844 tree decl_arg_types;
1845
1846 /* This is an ordinary member function. However, since
1847 we're here, we can assume it's enclosing class is a
1848 template class. For example,
1849
1850 template <typename T> struct S { void f(); };
1851 template <> void S<int>::f() {}
1852
1853 Here, S<int>::f is a non-template, but S<int> is a
1854 template class. If FN has the same type as DECL, we
1855 might be in business. */
1856
1857 if (!DECL_TEMPLATE_INFO (fn))
1858 /* Its enclosing class is an explicit specialization
1859 of a template class. This is not a candidate. */
1860 continue;
1861
1862 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1863 TREE_TYPE (TREE_TYPE (fn))))
1864 /* The return types differ. */
1865 continue;
1866
1867 /* Adjust the type of DECL in case FN is a static member. */
1868 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1869 if (DECL_STATIC_FUNCTION_P (fn)
1870 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1871 decl_arg_types = TREE_CHAIN (decl_arg_types);
1872
1873 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1874 decl_arg_types))
1875 /* They match! */
1876 candidates = tree_cons (NULL_TREE, fn, candidates);
1877 }
1878 }
1879
1880 if (templates && TREE_CHAIN (templates))
1881 {
1882 /* We have:
1883
1884 [temp.expl.spec]
1885
1886 It is possible for a specialization with a given function
1887 signature to be instantiated from more than one function
1888 template. In such cases, explicit specification of the
1889 template arguments must be used to uniquely identify the
1890 function template specialization being specialized.
1891
1892 Note that here, there's no suggestion that we're supposed to
1893 determine which of the candidate templates is most
1894 specialized. However, we, also have:
1895
1896 [temp.func.order]
1897
1898 Partial ordering of overloaded function template
1899 declarations is used in the following contexts to select
1900 the function template to which a function template
1901 specialization refers:
1902
1903 -- when an explicit specialization refers to a function
1904 template.
1905
1906 So, we do use the partial ordering rules, at least for now.
1907 This extension can only serve to make invalid programs valid,
1908 so it's safe. And, there is strong anecdotal evidence that
1909 the committee intended the partial ordering rules to apply;
1910 the EDG front end has that behavior, and John Spicer claims
1911 that the committee simply forgot to delete the wording in
1912 [temp.expl.spec]. */
1913 tree tmpl = most_specialized_instantiation (templates);
1914 if (tmpl != error_mark_node)
1915 {
1916 templates = tmpl;
1917 TREE_CHAIN (templates) = NULL_TREE;
1918 }
1919 }
1920
1921 if (templates == NULL_TREE && candidates == NULL_TREE)
1922 {
1923 error ("template-id %qD for %q+D does not match any template "
1924 "declaration", template_id, decl);
1925 return error_mark_node;
1926 }
1927 else if ((templates && TREE_CHAIN (templates))
1928 || (candidates && TREE_CHAIN (candidates))
1929 || (templates && candidates))
1930 {
1931 error ("ambiguous template specialization %qD for %q+D",
1932 template_id, decl);
1933 chainon (candidates, templates);
1934 print_candidates (candidates);
1935 return error_mark_node;
1936 }
1937
1938 /* We have one, and exactly one, match. */
1939 if (candidates)
1940 {
1941 tree fn = TREE_VALUE (candidates);
1942 *targs_out = copy_node (DECL_TI_ARGS (fn));
1943 /* DECL is a re-declaration or partial instantiation of a template
1944 function. */
1945 if (TREE_CODE (fn) == TEMPLATE_DECL)
1946 return fn;
1947 /* It was a specialization of an ordinary member function in a
1948 template class. */
1949 return DECL_TI_TEMPLATE (fn);
1950 }
1951
1952 /* It was a specialization of a template. */
1953 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1954 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1955 {
1956 *targs_out = copy_node (targs);
1957 SET_TMPL_ARGS_LEVEL (*targs_out,
1958 TMPL_ARGS_DEPTH (*targs_out),
1959 TREE_PURPOSE (templates));
1960 }
1961 else
1962 *targs_out = TREE_PURPOSE (templates);
1963 return TREE_VALUE (templates);
1964 }
1965
1966 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1967 but with the default argument values filled in from those in the
1968 TMPL_TYPES. */
1969
1970 static tree
1971 copy_default_args_to_explicit_spec_1 (tree spec_types,
1972 tree tmpl_types)
1973 {
1974 tree new_spec_types;
1975
1976 if (!spec_types)
1977 return NULL_TREE;
1978
1979 if (spec_types == void_list_node)
1980 return void_list_node;
1981
1982 /* Substitute into the rest of the list. */
1983 new_spec_types =
1984 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1985 TREE_CHAIN (tmpl_types));
1986
1987 /* Add the default argument for this parameter. */
1988 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1989 TREE_VALUE (spec_types),
1990 new_spec_types);
1991 }
1992
1993 /* DECL is an explicit specialization. Replicate default arguments
1994 from the template it specializes. (That way, code like:
1995
1996 template <class T> void f(T = 3);
1997 template <> void f(double);
1998 void g () { f (); }
1999
2000 works, as required.) An alternative approach would be to look up
2001 the correct default arguments at the call-site, but this approach
2002 is consistent with how implicit instantiations are handled. */
2003
2004 static void
2005 copy_default_args_to_explicit_spec (tree decl)
2006 {
2007 tree tmpl;
2008 tree spec_types;
2009 tree tmpl_types;
2010 tree new_spec_types;
2011 tree old_type;
2012 tree new_type;
2013 tree t;
2014 tree object_type = NULL_TREE;
2015 tree in_charge = NULL_TREE;
2016 tree vtt = NULL_TREE;
2017
2018 /* See if there's anything we need to do. */
2019 tmpl = DECL_TI_TEMPLATE (decl);
2020 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2021 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2022 if (TREE_PURPOSE (t))
2023 break;
2024 if (!t)
2025 return;
2026
2027 old_type = TREE_TYPE (decl);
2028 spec_types = TYPE_ARG_TYPES (old_type);
2029
2030 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2031 {
2032 /* Remove the this pointer, but remember the object's type for
2033 CV quals. */
2034 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2035 spec_types = TREE_CHAIN (spec_types);
2036 tmpl_types = TREE_CHAIN (tmpl_types);
2037
2038 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2039 {
2040 /* DECL may contain more parameters than TMPL due to the extra
2041 in-charge parameter in constructors and destructors. */
2042 in_charge = spec_types;
2043 spec_types = TREE_CHAIN (spec_types);
2044 }
2045 if (DECL_HAS_VTT_PARM_P (decl))
2046 {
2047 vtt = spec_types;
2048 spec_types = TREE_CHAIN (spec_types);
2049 }
2050 }
2051
2052 /* Compute the merged default arguments. */
2053 new_spec_types =
2054 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2055
2056 /* Compute the new FUNCTION_TYPE. */
2057 if (object_type)
2058 {
2059 if (vtt)
2060 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2061 TREE_VALUE (vtt),
2062 new_spec_types);
2063
2064 if (in_charge)
2065 /* Put the in-charge parameter back. */
2066 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2067 TREE_VALUE (in_charge),
2068 new_spec_types);
2069
2070 new_type = build_method_type_directly (object_type,
2071 TREE_TYPE (old_type),
2072 new_spec_types);
2073 }
2074 else
2075 new_type = build_function_type (TREE_TYPE (old_type),
2076 new_spec_types);
2077 new_type = cp_build_type_attribute_variant (new_type,
2078 TYPE_ATTRIBUTES (old_type));
2079 new_type = build_exception_variant (new_type,
2080 TYPE_RAISES_EXCEPTIONS (old_type));
2081 TREE_TYPE (decl) = new_type;
2082 }
2083
2084 /* Check to see if the function just declared, as indicated in
2085 DECLARATOR, and in DECL, is a specialization of a function
2086 template. We may also discover that the declaration is an explicit
2087 instantiation at this point.
2088
2089 Returns DECL, or an equivalent declaration that should be used
2090 instead if all goes well. Issues an error message if something is
2091 amiss. Returns error_mark_node if the error is not easily
2092 recoverable.
2093
2094 FLAGS is a bitmask consisting of the following flags:
2095
2096 2: The function has a definition.
2097 4: The function is a friend.
2098
2099 The TEMPLATE_COUNT is the number of references to qualifying
2100 template classes that appeared in the name of the function. For
2101 example, in
2102
2103 template <class T> struct S { void f(); };
2104 void S<int>::f();
2105
2106 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2107 classes are not counted in the TEMPLATE_COUNT, so that in
2108
2109 template <class T> struct S {};
2110 template <> struct S<int> { void f(); }
2111 template <> void S<int>::f();
2112
2113 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2114 invalid; there should be no template <>.)
2115
2116 If the function is a specialization, it is marked as such via
2117 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2118 is set up correctly, and it is added to the list of specializations
2119 for that template. */
2120
2121 tree
2122 check_explicit_specialization (tree declarator,
2123 tree decl,
2124 int template_count,
2125 int flags)
2126 {
2127 int have_def = flags & 2;
2128 int is_friend = flags & 4;
2129 int specialization = 0;
2130 int explicit_instantiation = 0;
2131 int member_specialization = 0;
2132 tree ctype = DECL_CLASS_CONTEXT (decl);
2133 tree dname = DECL_NAME (decl);
2134 tmpl_spec_kind tsk;
2135
2136 if (is_friend)
2137 {
2138 if (!processing_specialization)
2139 tsk = tsk_none;
2140 else
2141 tsk = tsk_excessive_parms;
2142 }
2143 else
2144 tsk = current_tmpl_spec_kind (template_count);
2145
2146 switch (tsk)
2147 {
2148 case tsk_none:
2149 if (processing_specialization)
2150 {
2151 specialization = 1;
2152 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2153 }
2154 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2155 {
2156 if (is_friend)
2157 /* This could be something like:
2158
2159 template <class T> void f(T);
2160 class S { friend void f<>(int); } */
2161 specialization = 1;
2162 else
2163 {
2164 /* This case handles bogus declarations like template <>
2165 template <class T> void f<int>(); */
2166
2167 error ("template-id %qD in declaration of primary template",
2168 declarator);
2169 return decl;
2170 }
2171 }
2172 break;
2173
2174 case tsk_invalid_member_spec:
2175 /* The error has already been reported in
2176 check_specialization_scope. */
2177 return error_mark_node;
2178
2179 case tsk_invalid_expl_inst:
2180 error ("template parameter list used in explicit instantiation");
2181
2182 /* Fall through. */
2183
2184 case tsk_expl_inst:
2185 if (have_def)
2186 error ("definition provided for explicit instantiation");
2187
2188 explicit_instantiation = 1;
2189 break;
2190
2191 case tsk_excessive_parms:
2192 case tsk_insufficient_parms:
2193 if (tsk == tsk_excessive_parms)
2194 error ("too many template parameter lists in declaration of %qD",
2195 decl);
2196 else if (template_header_count)
2197 error("too few template parameter lists in declaration of %qD", decl);
2198 else
2199 error("explicit specialization of %qD must be introduced by "
2200 "%<template <>%>", decl);
2201
2202 /* Fall through. */
2203 case tsk_expl_spec:
2204 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2205 if (ctype)
2206 member_specialization = 1;
2207 else
2208 specialization = 1;
2209 break;
2210
2211 case tsk_template:
2212 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2213 {
2214 /* This case handles bogus declarations like template <>
2215 template <class T> void f<int>(); */
2216
2217 if (uses_template_parms (declarator))
2218 error ("function template partial specialization %qD "
2219 "is not allowed", declarator);
2220 else
2221 error ("template-id %qD in declaration of primary template",
2222 declarator);
2223 return decl;
2224 }
2225
2226 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2227 /* This is a specialization of a member template, without
2228 specialization the containing class. Something like:
2229
2230 template <class T> struct S {
2231 template <class U> void f (U);
2232 };
2233 template <> template <class U> void S<int>::f(U) {}
2234
2235 That's a specialization -- but of the entire template. */
2236 specialization = 1;
2237 break;
2238
2239 default:
2240 gcc_unreachable ();
2241 }
2242
2243 if (specialization || member_specialization)
2244 {
2245 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2246 for (; t; t = TREE_CHAIN (t))
2247 if (TREE_PURPOSE (t))
2248 {
2249 permerror (input_location,
2250 "default argument specified in explicit specialization");
2251 break;
2252 }
2253 }
2254
2255 if (specialization || member_specialization || explicit_instantiation)
2256 {
2257 tree tmpl = NULL_TREE;
2258 tree targs = NULL_TREE;
2259
2260 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2261 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2262 {
2263 tree fns;
2264
2265 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2266 if (ctype)
2267 fns = dname;
2268 else
2269 {
2270 /* If there is no class context, the explicit instantiation
2271 must be at namespace scope. */
2272 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2273
2274 /* Find the namespace binding, using the declaration
2275 context. */
2276 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2277 false, true);
2278 if (fns == error_mark_node || !is_overloaded_fn (fns))
2279 {
2280 error ("%qD is not a template function", dname);
2281 fns = error_mark_node;
2282 }
2283 else
2284 {
2285 tree fn = OVL_CURRENT (fns);
2286 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2287 CP_DECL_CONTEXT (fn)))
2288 error ("%qD is not declared in %qD",
2289 decl, current_namespace);
2290 }
2291 }
2292
2293 declarator = lookup_template_function (fns, NULL_TREE);
2294 }
2295
2296 if (declarator == error_mark_node)
2297 return error_mark_node;
2298
2299 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2300 {
2301 if (!explicit_instantiation)
2302 /* A specialization in class scope. This is invalid,
2303 but the error will already have been flagged by
2304 check_specialization_scope. */
2305 return error_mark_node;
2306 else
2307 {
2308 /* It's not valid to write an explicit instantiation in
2309 class scope, e.g.:
2310
2311 class C { template void f(); }
2312
2313 This case is caught by the parser. However, on
2314 something like:
2315
2316 template class C { void f(); };
2317
2318 (which is invalid) we can get here. The error will be
2319 issued later. */
2320 ;
2321 }
2322
2323 return decl;
2324 }
2325 else if (ctype != NULL_TREE
2326 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2327 IDENTIFIER_NODE))
2328 {
2329 /* Find the list of functions in ctype that have the same
2330 name as the declared function. */
2331 tree name = TREE_OPERAND (declarator, 0);
2332 tree fns = NULL_TREE;
2333 int idx;
2334
2335 if (constructor_name_p (name, ctype))
2336 {
2337 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2338
2339 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2340 : !CLASSTYPE_DESTRUCTORS (ctype))
2341 {
2342 /* From [temp.expl.spec]:
2343
2344 If such an explicit specialization for the member
2345 of a class template names an implicitly-declared
2346 special member function (clause _special_), the
2347 program is ill-formed.
2348
2349 Similar language is found in [temp.explicit]. */
2350 error ("specialization of implicitly-declared special member function");
2351 return error_mark_node;
2352 }
2353
2354 name = is_constructor ? ctor_identifier : dtor_identifier;
2355 }
2356
2357 if (!DECL_CONV_FN_P (decl))
2358 {
2359 idx = lookup_fnfields_1 (ctype, name);
2360 if (idx >= 0)
2361 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2362 }
2363 else
2364 {
2365 VEC(tree,gc) *methods;
2366 tree ovl;
2367
2368 /* For a type-conversion operator, we cannot do a
2369 name-based lookup. We might be looking for `operator
2370 int' which will be a specialization of `operator T'.
2371 So, we find *all* the conversion operators, and then
2372 select from them. */
2373 fns = NULL_TREE;
2374
2375 methods = CLASSTYPE_METHOD_VEC (ctype);
2376 if (methods)
2377 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2378 VEC_iterate (tree, methods, idx, ovl);
2379 ++idx)
2380 {
2381 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2382 /* There are no more conversion functions. */
2383 break;
2384
2385 /* Glue all these conversion functions together
2386 with those we already have. */
2387 for (; ovl; ovl = OVL_NEXT (ovl))
2388 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2389 }
2390 }
2391
2392 if (fns == NULL_TREE)
2393 {
2394 error ("no member function %qD declared in %qT", name, ctype);
2395 return error_mark_node;
2396 }
2397 else
2398 TREE_OPERAND (declarator, 0) = fns;
2399 }
2400
2401 /* Figure out what exactly is being specialized at this point.
2402 Note that for an explicit instantiation, even one for a
2403 member function, we cannot tell apriori whether the
2404 instantiation is for a member template, or just a member
2405 function of a template class. Even if a member template is
2406 being instantiated, the member template arguments may be
2407 elided if they can be deduced from the rest of the
2408 declaration. */
2409 tmpl = determine_specialization (declarator, decl,
2410 &targs,
2411 member_specialization,
2412 template_count,
2413 tsk);
2414
2415 if (!tmpl || tmpl == error_mark_node)
2416 /* We couldn't figure out what this declaration was
2417 specializing. */
2418 return error_mark_node;
2419 else
2420 {
2421 tree gen_tmpl = most_general_template (tmpl);
2422
2423 if (explicit_instantiation)
2424 {
2425 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2426 is done by do_decl_instantiation later. */
2427
2428 int arg_depth = TMPL_ARGS_DEPTH (targs);
2429 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2430
2431 if (arg_depth > parm_depth)
2432 {
2433 /* If TMPL is not the most general template (for
2434 example, if TMPL is a friend template that is
2435 injected into namespace scope), then there will
2436 be too many levels of TARGS. Remove some of them
2437 here. */
2438 int i;
2439 tree new_targs;
2440
2441 new_targs = make_tree_vec (parm_depth);
2442 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2443 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2444 = TREE_VEC_ELT (targs, i);
2445 targs = new_targs;
2446 }
2447
2448 return instantiate_template (tmpl, targs, tf_error);
2449 }
2450
2451 /* If we thought that the DECL was a member function, but it
2452 turns out to be specializing a static member function,
2453 make DECL a static member function as well. */
2454 if (DECL_STATIC_FUNCTION_P (tmpl)
2455 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2456 revert_static_member_fn (decl);
2457
2458 /* If this is a specialization of a member template of a
2459 template class, we want to return the TEMPLATE_DECL, not
2460 the specialization of it. */
2461 if (tsk == tsk_template)
2462 {
2463 tree result = DECL_TEMPLATE_RESULT (tmpl);
2464 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2465 DECL_INITIAL (result) = NULL_TREE;
2466 if (have_def)
2467 {
2468 tree parm;
2469 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2470 DECL_SOURCE_LOCATION (result)
2471 = DECL_SOURCE_LOCATION (decl);
2472 /* We want to use the argument list specified in the
2473 definition, not in the original declaration. */
2474 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2475 for (parm = DECL_ARGUMENTS (result); parm;
2476 parm = TREE_CHAIN (parm))
2477 DECL_CONTEXT (parm) = result;
2478 }
2479 return register_specialization (tmpl, gen_tmpl, targs,
2480 is_friend, 0);
2481 }
2482
2483 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2484 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2485
2486 /* Inherit default function arguments from the template
2487 DECL is specializing. */
2488 copy_default_args_to_explicit_spec (decl);
2489
2490 /* This specialization has the same protection as the
2491 template it specializes. */
2492 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2493 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2494
2495 /* 7.1.1-1 [dcl.stc]
2496
2497 A storage-class-specifier shall not be specified in an
2498 explicit specialization...
2499
2500 The parser rejects these, so unless action is taken here,
2501 explicit function specializations will always appear with
2502 global linkage.
2503
2504 The action recommended by the C++ CWG in response to C++
2505 defect report 605 is to make the storage class and linkage
2506 of the explicit specialization match the templated function:
2507
2508 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2509 */
2510 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2511 {
2512 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2513 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2514
2515 /* This specialization has the same linkage and visibility as
2516 the function template it specializes. */
2517 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2518 if (! TREE_PUBLIC (decl))
2519 {
2520 DECL_INTERFACE_KNOWN (decl) = 1;
2521 DECL_NOT_REALLY_EXTERN (decl) = 1;
2522 }
2523 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2524 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2525 {
2526 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2527 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2528 }
2529 }
2530
2531 /* If DECL is a friend declaration, declared using an
2532 unqualified name, the namespace associated with DECL may
2533 have been set incorrectly. For example, in:
2534
2535 template <typename T> void f(T);
2536 namespace N {
2537 struct S { friend void f<int>(int); }
2538 }
2539
2540 we will have set the DECL_CONTEXT for the friend
2541 declaration to N, rather than to the global namespace. */
2542 if (DECL_NAMESPACE_SCOPE_P (decl))
2543 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2544
2545 if (is_friend && !have_def)
2546 /* This is not really a declaration of a specialization.
2547 It's just the name of an instantiation. But, it's not
2548 a request for an instantiation, either. */
2549 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2550 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2551 /* This is indeed a specialization. In case of constructors
2552 and destructors, we need in-charge and not-in-charge
2553 versions in V3 ABI. */
2554 clone_function_decl (decl, /*update_method_vec_p=*/0);
2555
2556 /* Register this specialization so that we can find it
2557 again. */
2558 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2559 }
2560 }
2561
2562 return decl;
2563 }
2564
2565 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2566 parameters. These are represented in the same format used for
2567 DECL_TEMPLATE_PARMS. */
2568
2569 int
2570 comp_template_parms (const_tree parms1, const_tree parms2)
2571 {
2572 const_tree p1;
2573 const_tree p2;
2574
2575 if (parms1 == parms2)
2576 return 1;
2577
2578 for (p1 = parms1, p2 = parms2;
2579 p1 != NULL_TREE && p2 != NULL_TREE;
2580 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2581 {
2582 tree t1 = TREE_VALUE (p1);
2583 tree t2 = TREE_VALUE (p2);
2584 int i;
2585
2586 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2587 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2588
2589 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2590 return 0;
2591
2592 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2593 {
2594 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2595 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2596
2597 /* If either of the template parameters are invalid, assume
2598 they match for the sake of error recovery. */
2599 if (parm1 == error_mark_node || parm2 == error_mark_node)
2600 return 1;
2601
2602 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2603 return 0;
2604
2605 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2606 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2607 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2608 continue;
2609 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2610 return 0;
2611 }
2612 }
2613
2614 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2615 /* One set of parameters has more parameters lists than the
2616 other. */
2617 return 0;
2618
2619 return 1;
2620 }
2621
2622 /* Determine whether PARM is a parameter pack. */
2623
2624 bool
2625 template_parameter_pack_p (const_tree parm)
2626 {
2627 /* Determine if we have a non-type template parameter pack. */
2628 if (TREE_CODE (parm) == PARM_DECL)
2629 return (DECL_TEMPLATE_PARM_P (parm)
2630 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2631
2632 /* If this is a list of template parameters, we could get a
2633 TYPE_DECL or a TEMPLATE_DECL. */
2634 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2635 parm = TREE_TYPE (parm);
2636
2637 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2638 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2639 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2640 }
2641
2642 /* Determine if T is a function parameter pack. */
2643
2644 bool
2645 function_parameter_pack_p (const_tree t)
2646 {
2647 if (t && TREE_CODE (t) == PARM_DECL)
2648 return FUNCTION_PARAMETER_PACK_P (t);
2649 return false;
2650 }
2651
2652 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2653 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2654
2655 tree
2656 get_function_template_decl (const_tree primary_func_tmpl_inst)
2657 {
2658 if (! primary_func_tmpl_inst
2659 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2660 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2661 return NULL;
2662
2663 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2664 }
2665
2666 /* Return true iff the function parameter PARAM_DECL was expanded
2667 from the function parameter pack PACK. */
2668
2669 bool
2670 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2671 {
2672 if (! function_parameter_pack_p (pack))
2673 return false;
2674
2675 gcc_assert (DECL_NAME (param_decl) && DECL_NAME (pack));
2676
2677 /* The parameter pack and its pack arguments have the same
2678 DECL_PARM_INDEX. */
2679 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2680 }
2681
2682 /* Determine whether ARGS describes a variadic template args list,
2683 i.e., one that is terminated by a template argument pack. */
2684
2685 static bool
2686 template_args_variadic_p (tree args)
2687 {
2688 int nargs;
2689 tree last_parm;
2690
2691 if (args == NULL_TREE)
2692 return false;
2693
2694 args = INNERMOST_TEMPLATE_ARGS (args);
2695 nargs = TREE_VEC_LENGTH (args);
2696
2697 if (nargs == 0)
2698 return false;
2699
2700 last_parm = TREE_VEC_ELT (args, nargs - 1);
2701
2702 return ARGUMENT_PACK_P (last_parm);
2703 }
2704
2705 /* Generate a new name for the parameter pack name NAME (an
2706 IDENTIFIER_NODE) that incorporates its */
2707
2708 static tree
2709 make_ith_pack_parameter_name (tree name, int i)
2710 {
2711 /* Munge the name to include the parameter index. */
2712 #define NUMBUF_LEN 128
2713 char numbuf[NUMBUF_LEN];
2714 char* newname;
2715 int newname_len;
2716
2717 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2718 newname_len = IDENTIFIER_LENGTH (name)
2719 + strlen (numbuf) + 2;
2720 newname = (char*)alloca (newname_len);
2721 snprintf (newname, newname_len,
2722 "%s#%i", IDENTIFIER_POINTER (name), i);
2723 return get_identifier (newname);
2724 }
2725
2726 /* Return true if T is a primary function
2727 or class template instantiation. */
2728
2729 static bool
2730 primary_template_instantiation_p (const_tree t)
2731 {
2732 if (!t)
2733 return false;
2734
2735 if (TREE_CODE (t) == FUNCTION_DECL)
2736 return DECL_LANG_SPECIFIC (t)
2737 && DECL_TEMPLATE_INSTANTIATION (t)
2738 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2739 else if (CLASS_TYPE_P (t))
2740 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2741 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2742 return false;
2743 }
2744
2745 /* Return true if PARM is a template template parameter. */
2746
2747 bool
2748 template_template_parameter_p (const_tree parm)
2749 {
2750 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2751 }
2752
2753 /* Return the template parameters of T if T is a
2754 primary template instantiation, NULL otherwise. */
2755
2756 tree
2757 get_primary_template_innermost_parameters (const_tree t)
2758 {
2759 tree parms = NULL, template_info = NULL;
2760
2761 if ((template_info = get_template_info (t))
2762 && primary_template_instantiation_p (t))
2763 parms = INNERMOST_TEMPLATE_PARMS
2764 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2765
2766 return parms;
2767 }
2768
2769 /* Returns the template arguments of T if T is a template instantiation,
2770 NULL otherwise. */
2771
2772 tree
2773 get_template_innermost_arguments (const_tree t)
2774 {
2775 tree args = NULL, template_info = NULL;
2776
2777 if ((template_info = get_template_info (t))
2778 && TI_ARGS (template_info))
2779 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2780
2781 return args;
2782 }
2783
2784 /* Return the argument pack elements of T if T is a template argument pack,
2785 NULL otherwise. */
2786
2787 tree
2788 get_template_argument_pack_elems (const_tree t)
2789 {
2790 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2791 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2792 return NULL;
2793
2794 return ARGUMENT_PACK_ARGS (t);
2795 }
2796
2797 /* Structure used to track the progress of find_parameter_packs_r. */
2798 struct find_parameter_pack_data
2799 {
2800 /* TREE_LIST that will contain all of the parameter packs found by
2801 the traversal. */
2802 tree* parameter_packs;
2803
2804 /* Set of AST nodes that have been visited by the traversal. */
2805 struct pointer_set_t *visited;
2806 };
2807
2808 /* Identifies all of the argument packs that occur in a template
2809 argument and appends them to the TREE_LIST inside DATA, which is a
2810 find_parameter_pack_data structure. This is a subroutine of
2811 make_pack_expansion and uses_parameter_packs. */
2812 static tree
2813 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2814 {
2815 tree t = *tp;
2816 struct find_parameter_pack_data* ppd =
2817 (struct find_parameter_pack_data*)data;
2818 bool parameter_pack_p = false;
2819
2820 /* Identify whether this is a parameter pack or not. */
2821 switch (TREE_CODE (t))
2822 {
2823 case TEMPLATE_PARM_INDEX:
2824 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2825 parameter_pack_p = true;
2826 break;
2827
2828 case TEMPLATE_TYPE_PARM:
2829 case TEMPLATE_TEMPLATE_PARM:
2830 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2831 parameter_pack_p = true;
2832 break;
2833
2834 case PARM_DECL:
2835 if (FUNCTION_PARAMETER_PACK_P (t))
2836 {
2837 /* We don't want to walk into the type of a PARM_DECL,
2838 because we don't want to see the type parameter pack. */
2839 *walk_subtrees = 0;
2840 parameter_pack_p = true;
2841 }
2842 break;
2843
2844 default:
2845 /* Not a parameter pack. */
2846 break;
2847 }
2848
2849 if (parameter_pack_p)
2850 {
2851 /* Add this parameter pack to the list. */
2852 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2853 }
2854
2855 if (TYPE_P (t))
2856 cp_walk_tree (&TYPE_CONTEXT (t),
2857 &find_parameter_packs_r, ppd, ppd->visited);
2858
2859 /* This switch statement will return immediately if we don't find a
2860 parameter pack. */
2861 switch (TREE_CODE (t))
2862 {
2863 case TEMPLATE_PARM_INDEX:
2864 return NULL_TREE;
2865
2866 case BOUND_TEMPLATE_TEMPLATE_PARM:
2867 /* Check the template itself. */
2868 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2869 &find_parameter_packs_r, ppd, ppd->visited);
2870 /* Check the template arguments. */
2871 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2872 ppd->visited);
2873 *walk_subtrees = 0;
2874 return NULL_TREE;
2875
2876 case TEMPLATE_TYPE_PARM:
2877 case TEMPLATE_TEMPLATE_PARM:
2878 return NULL_TREE;
2879
2880 case PARM_DECL:
2881 return NULL_TREE;
2882
2883 case RECORD_TYPE:
2884 if (TYPE_PTRMEMFUNC_P (t))
2885 return NULL_TREE;
2886 /* Fall through. */
2887
2888 case UNION_TYPE:
2889 case ENUMERAL_TYPE:
2890 if (TYPE_TEMPLATE_INFO (t))
2891 cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
2892 &find_parameter_packs_r, ppd, ppd->visited);
2893
2894 *walk_subtrees = 0;
2895 return NULL_TREE;
2896
2897 case TEMPLATE_DECL:
2898 cp_walk_tree (&TREE_TYPE (t),
2899 &find_parameter_packs_r, ppd, ppd->visited);
2900 return NULL_TREE;
2901
2902 case TYPENAME_TYPE:
2903 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
2904 ppd, ppd->visited);
2905 *walk_subtrees = 0;
2906 return NULL_TREE;
2907
2908 case TYPE_PACK_EXPANSION:
2909 case EXPR_PACK_EXPANSION:
2910 *walk_subtrees = 0;
2911 return NULL_TREE;
2912
2913 case INTEGER_TYPE:
2914 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
2915 ppd, ppd->visited);
2916 *walk_subtrees = 0;
2917 return NULL_TREE;
2918
2919 case IDENTIFIER_NODE:
2920 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
2921 ppd->visited);
2922 *walk_subtrees = 0;
2923 return NULL_TREE;
2924
2925 default:
2926 return NULL_TREE;
2927 }
2928
2929 return NULL_TREE;
2930 }
2931
2932 /* Determines if the expression or type T uses any parameter packs. */
2933 bool
2934 uses_parameter_packs (tree t)
2935 {
2936 tree parameter_packs = NULL_TREE;
2937 struct find_parameter_pack_data ppd;
2938 ppd.parameter_packs = &parameter_packs;
2939 ppd.visited = pointer_set_create ();
2940 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2941 pointer_set_destroy (ppd.visited);
2942 return parameter_packs != NULL_TREE;
2943 }
2944
2945 /* Turn ARG, which may be an expression, type, or a TREE_LIST
2946 representation a base-class initializer into a parameter pack
2947 expansion. If all goes well, the resulting node will be an
2948 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2949 respectively. */
2950 tree
2951 make_pack_expansion (tree arg)
2952 {
2953 tree result;
2954 tree parameter_packs = NULL_TREE;
2955 bool for_types = false;
2956 struct find_parameter_pack_data ppd;
2957
2958 if (!arg || arg == error_mark_node)
2959 return arg;
2960
2961 if (TREE_CODE (arg) == TREE_LIST)
2962 {
2963 /* The only time we will see a TREE_LIST here is for a base
2964 class initializer. In this case, the TREE_PURPOSE will be a
2965 _TYPE node (representing the base class expansion we're
2966 initializing) and the TREE_VALUE will be a TREE_LIST
2967 containing the initialization arguments.
2968
2969 The resulting expansion looks somewhat different from most
2970 expansions. Rather than returning just one _EXPANSION, we
2971 return a TREE_LIST whose TREE_PURPOSE is a
2972 TYPE_PACK_EXPANSION containing the bases that will be
2973 initialized. The TREE_VALUE will be identical to the
2974 original TREE_VALUE, which is a list of arguments that will
2975 be passed to each base. We do not introduce any new pack
2976 expansion nodes into the TREE_VALUE (although it is possible
2977 that some already exist), because the TREE_PURPOSE and
2978 TREE_VALUE all need to be expanded together with the same
2979 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2980 resulting TREE_PURPOSE will mention the parameter packs in
2981 both the bases and the arguments to the bases. */
2982 tree purpose;
2983 tree value;
2984 tree parameter_packs = NULL_TREE;
2985
2986 /* Determine which parameter packs will be used by the base
2987 class expansion. */
2988 ppd.visited = pointer_set_create ();
2989 ppd.parameter_packs = &parameter_packs;
2990 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
2991 &ppd, ppd.visited);
2992
2993 if (parameter_packs == NULL_TREE)
2994 {
2995 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
2996 pointer_set_destroy (ppd.visited);
2997 return error_mark_node;
2998 }
2999
3000 if (TREE_VALUE (arg) != void_type_node)
3001 {
3002 /* Collect the sets of parameter packs used in each of the
3003 initialization arguments. */
3004 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3005 {
3006 /* Determine which parameter packs will be expanded in this
3007 argument. */
3008 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3009 &ppd, ppd.visited);
3010 }
3011 }
3012
3013 pointer_set_destroy (ppd.visited);
3014
3015 /* Create the pack expansion type for the base type. */
3016 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3017 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3018 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3019
3020 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3021 they will rarely be compared to anything. */
3022 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3023
3024 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3025 }
3026
3027 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3028 for_types = true;
3029
3030 /* Build the PACK_EXPANSION_* node. */
3031 result = for_types
3032 ? cxx_make_type (TYPE_PACK_EXPANSION)
3033 : make_node (EXPR_PACK_EXPANSION);
3034 SET_PACK_EXPANSION_PATTERN (result, arg);
3035 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3036 {
3037 /* Propagate type and const-expression information. */
3038 TREE_TYPE (result) = TREE_TYPE (arg);
3039 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3040 }
3041 else
3042 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3043 they will rarely be compared to anything. */
3044 SET_TYPE_STRUCTURAL_EQUALITY (result);
3045
3046 /* Determine which parameter packs will be expanded. */
3047 ppd.parameter_packs = &parameter_packs;
3048 ppd.visited = pointer_set_create ();
3049 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3050 pointer_set_destroy (ppd.visited);
3051
3052 /* Make sure we found some parameter packs. */
3053 if (parameter_packs == NULL_TREE)
3054 {
3055 if (TYPE_P (arg))
3056 error ("expansion pattern %<%T%> contains no argument packs", arg);
3057 else
3058 error ("expansion pattern %<%E%> contains no argument packs", arg);
3059 return error_mark_node;
3060 }
3061 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3062
3063 return result;
3064 }
3065
3066 /* Checks T for any "bare" parameter packs, which have not yet been
3067 expanded, and issues an error if any are found. This operation can
3068 only be done on full expressions or types (e.g., an expression
3069 statement, "if" condition, etc.), because we could have expressions like:
3070
3071 foo(f(g(h(args)))...)
3072
3073 where "args" is a parameter pack. check_for_bare_parameter_packs
3074 should not be called for the subexpressions args, h(args),
3075 g(h(args)), or f(g(h(args))), because we would produce erroneous
3076 error messages.
3077
3078 Returns TRUE and emits an error if there were bare parameter packs,
3079 returns FALSE otherwise. */
3080 bool
3081 check_for_bare_parameter_packs (tree t)
3082 {
3083 tree parameter_packs = NULL_TREE;
3084 struct find_parameter_pack_data ppd;
3085
3086 if (!processing_template_decl || !t || t == error_mark_node)
3087 return false;
3088
3089 if (TREE_CODE (t) == TYPE_DECL)
3090 t = TREE_TYPE (t);
3091
3092 ppd.parameter_packs = &parameter_packs;
3093 ppd.visited = pointer_set_create ();
3094 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3095 pointer_set_destroy (ppd.visited);
3096
3097 if (parameter_packs)
3098 {
3099 error ("parameter packs not expanded with %<...%>:");
3100 while (parameter_packs)
3101 {
3102 tree pack = TREE_VALUE (parameter_packs);
3103 tree name = NULL_TREE;
3104
3105 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3106 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3107 name = TYPE_NAME (pack);
3108 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3109 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3110 else
3111 name = DECL_NAME (pack);
3112
3113 if (name)
3114 inform (input_location, " %qD", name);
3115 else
3116 inform (input_location, " <anonymous>");
3117
3118 parameter_packs = TREE_CHAIN (parameter_packs);
3119 }
3120
3121 return true;
3122 }
3123
3124 return false;
3125 }
3126
3127 /* Expand any parameter packs that occur in the template arguments in
3128 ARGS. */
3129 tree
3130 expand_template_argument_pack (tree args)
3131 {
3132 tree result_args = NULL_TREE;
3133 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3134 int num_result_args = -1;
3135
3136 /* First, determine if we need to expand anything, and the number of
3137 slots we'll need. */
3138 for (in_arg = 0; in_arg < nargs; ++in_arg)
3139 {
3140 tree arg = TREE_VEC_ELT (args, in_arg);
3141 if (ARGUMENT_PACK_P (arg))
3142 {
3143 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3144 if (num_result_args < 0)
3145 num_result_args = in_arg + num_packed;
3146 else
3147 num_result_args += num_packed;
3148 }
3149 else
3150 {
3151 if (num_result_args >= 0)
3152 num_result_args++;
3153 }
3154 }
3155
3156 /* If no expansion is necessary, we're done. */
3157 if (num_result_args < 0)
3158 return args;
3159
3160 /* Expand arguments. */
3161 result_args = make_tree_vec (num_result_args);
3162 for (in_arg = 0; in_arg < nargs; ++in_arg)
3163 {
3164 tree arg = TREE_VEC_ELT (args, in_arg);
3165 if (ARGUMENT_PACK_P (arg))
3166 {
3167 tree packed = ARGUMENT_PACK_ARGS (arg);
3168 int i, num_packed = TREE_VEC_LENGTH (packed);
3169 for (i = 0; i < num_packed; ++i, ++out_arg)
3170 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3171 }
3172 else
3173 {
3174 TREE_VEC_ELT (result_args, out_arg) = arg;
3175 ++out_arg;
3176 }
3177 }
3178
3179 return result_args;
3180 }
3181
3182 /* Checks if DECL shadows a template parameter.
3183
3184 [temp.local]: A template-parameter shall not be redeclared within its
3185 scope (including nested scopes).
3186
3187 Emits an error and returns TRUE if the DECL shadows a parameter,
3188 returns FALSE otherwise. */
3189
3190 bool
3191 check_template_shadow (tree decl)
3192 {
3193 tree olddecl;
3194
3195 /* If we're not in a template, we can't possibly shadow a template
3196 parameter. */
3197 if (!current_template_parms)
3198 return true;
3199
3200 /* Figure out what we're shadowing. */
3201 if (TREE_CODE (decl) == OVERLOAD)
3202 decl = OVL_CURRENT (decl);
3203 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3204
3205 /* If there's no previous binding for this name, we're not shadowing
3206 anything, let alone a template parameter. */
3207 if (!olddecl)
3208 return true;
3209
3210 /* If we're not shadowing a template parameter, we're done. Note
3211 that OLDDECL might be an OVERLOAD (or perhaps even an
3212 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3213 node. */
3214 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3215 return true;
3216
3217 /* We check for decl != olddecl to avoid bogus errors for using a
3218 name inside a class. We check TPFI to avoid duplicate errors for
3219 inline member templates. */
3220 if (decl == olddecl
3221 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3222 return true;
3223
3224 error ("declaration of %q+#D", decl);
3225 error (" shadows template parm %q+#D", olddecl);
3226 return false;
3227 }
3228
3229 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3230 ORIG_LEVEL, DECL, and TYPE. */
3231
3232 static tree
3233 build_template_parm_index (int index,
3234 int level,
3235 int orig_level,
3236 tree decl,
3237 tree type)
3238 {
3239 tree t = make_node (TEMPLATE_PARM_INDEX);
3240 TEMPLATE_PARM_IDX (t) = index;
3241 TEMPLATE_PARM_LEVEL (t) = level;
3242 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3243 TEMPLATE_PARM_DECL (t) = decl;
3244 TREE_TYPE (t) = type;
3245 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3246 TREE_READONLY (t) = TREE_READONLY (decl);
3247
3248 return t;
3249 }
3250
3251 /* Find the canonical type parameter for the given template type
3252 parameter. Returns the canonical type parameter, which may be TYPE
3253 if no such parameter existed. */
3254 static tree
3255 canonical_type_parameter (tree type)
3256 {
3257 tree list;
3258 int idx = TEMPLATE_TYPE_IDX (type);
3259 if (!canonical_template_parms)
3260 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3261
3262 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3263 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3264
3265 list = VEC_index (tree, canonical_template_parms, idx);
3266 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3267 list = TREE_CHAIN (list);
3268
3269 if (list)
3270 return TREE_VALUE (list);
3271 else
3272 {
3273 VEC_replace(tree, canonical_template_parms, idx,
3274 tree_cons (NULL_TREE, type,
3275 VEC_index (tree, canonical_template_parms, idx)));
3276 return type;
3277 }
3278 }
3279
3280 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3281 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3282 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3283 new one is created. */
3284
3285 static tree
3286 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3287 tsubst_flags_t complain)
3288 {
3289 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3290 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3291 != TEMPLATE_PARM_LEVEL (index) - levels))
3292 {
3293 tree orig_decl = TEMPLATE_PARM_DECL (index);
3294 tree decl, t;
3295
3296 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3297 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3298 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3299 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3300 DECL_ARTIFICIAL (decl) = 1;
3301 SET_DECL_TEMPLATE_PARM_P (decl);
3302
3303 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3304 TEMPLATE_PARM_LEVEL (index) - levels,
3305 TEMPLATE_PARM_ORIG_LEVEL (index),
3306 decl, type);
3307 TEMPLATE_PARM_DESCENDANTS (index) = t;
3308 TEMPLATE_PARM_PARAMETER_PACK (t)
3309 = TEMPLATE_PARM_PARAMETER_PACK (index);
3310
3311 /* Template template parameters need this. */
3312 if (TREE_CODE (decl) == TEMPLATE_DECL)
3313 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3314 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3315 args, complain);
3316 }
3317
3318 return TEMPLATE_PARM_DESCENDANTS (index);
3319 }
3320
3321 /* Process information from new template parameter PARM and append it to the
3322 LIST being built. This new parameter is a non-type parameter iff
3323 IS_NON_TYPE is true. This new parameter is a parameter
3324 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3325 PARM_LOC. */
3326
3327 tree
3328 process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
3329 bool is_parameter_pack)
3330 {
3331 tree decl = 0;
3332 tree defval;
3333 tree err_parm_list;
3334 int idx = 0;
3335
3336 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3337 defval = TREE_PURPOSE (parm);
3338
3339 if (list)
3340 {
3341 tree p = tree_last (list);
3342
3343 if (p && TREE_VALUE (p) != error_mark_node)
3344 {
3345 p = TREE_VALUE (p);
3346 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3347 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3348 else
3349 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3350 }
3351
3352 ++idx;
3353 }
3354 else
3355 idx = 0;
3356
3357 if (is_non_type)
3358 {
3359 parm = TREE_VALUE (parm);
3360
3361 SET_DECL_TEMPLATE_PARM_P (parm);
3362
3363 if (TREE_TYPE (parm) == error_mark_node)
3364 {
3365 err_parm_list = build_tree_list (defval, parm);
3366 TREE_VALUE (err_parm_list) = error_mark_node;
3367 return chainon (list, err_parm_list);
3368 }
3369 else
3370 {
3371 /* [temp.param]
3372
3373 The top-level cv-qualifiers on the template-parameter are
3374 ignored when determining its type. */
3375 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3376 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3377 {
3378 err_parm_list = build_tree_list (defval, parm);
3379 TREE_VALUE (err_parm_list) = error_mark_node;
3380 return chainon (list, err_parm_list);
3381 }
3382
3383 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3384 {
3385 /* This template parameter is not a parameter pack, but it
3386 should be. Complain about "bare" parameter packs. */
3387 check_for_bare_parameter_packs (TREE_TYPE (parm));
3388
3389 /* Recover by calling this a parameter pack. */
3390 is_parameter_pack = true;
3391 }
3392 }
3393
3394 /* A template parameter is not modifiable. */
3395 TREE_CONSTANT (parm) = 1;
3396 TREE_READONLY (parm) = 1;
3397 decl = build_decl (parm_loc,
3398 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3399 TREE_CONSTANT (decl) = 1;
3400 TREE_READONLY (decl) = 1;
3401 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3402 = build_template_parm_index (idx, processing_template_decl,
3403 processing_template_decl,
3404 decl, TREE_TYPE (parm));
3405
3406 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3407 = is_parameter_pack;
3408 }
3409 else
3410 {
3411 tree t;
3412 parm = TREE_VALUE (TREE_VALUE (parm));
3413
3414 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3415 {
3416 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3417 /* This is for distinguishing between real templates and template
3418 template parameters */
3419 TREE_TYPE (parm) = t;
3420 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3421 decl = parm;
3422 }
3423 else
3424 {
3425 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3426 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3427 decl = build_decl (parm_loc,
3428 TYPE_DECL, parm, t);
3429 }
3430
3431 TYPE_NAME (t) = decl;
3432 TYPE_STUB_DECL (t) = decl;
3433 parm = decl;
3434 TEMPLATE_TYPE_PARM_INDEX (t)
3435 = build_template_parm_index (idx, processing_template_decl,
3436 processing_template_decl,
3437 decl, TREE_TYPE (parm));
3438 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3439 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3440 }
3441 DECL_ARTIFICIAL (decl) = 1;
3442 SET_DECL_TEMPLATE_PARM_P (decl);
3443 pushdecl (decl);
3444 parm = build_tree_list (defval, parm);
3445 return chainon (list, parm);
3446 }
3447
3448 /* The end of a template parameter list has been reached. Process the
3449 tree list into a parameter vector, converting each parameter into a more
3450 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3451 as PARM_DECLs. */
3452
3453 tree
3454 end_template_parm_list (tree parms)
3455 {
3456 int nparms;
3457 tree parm, next;
3458 tree saved_parmlist = make_tree_vec (list_length (parms));
3459
3460 current_template_parms
3461 = tree_cons (size_int (processing_template_decl),
3462 saved_parmlist, current_template_parms);
3463
3464 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3465 {
3466 next = TREE_CHAIN (parm);
3467 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3468 TREE_CHAIN (parm) = NULL_TREE;
3469 }
3470
3471 --processing_template_parmlist;
3472
3473 return saved_parmlist;
3474 }
3475
3476 /* end_template_decl is called after a template declaration is seen. */
3477
3478 void
3479 end_template_decl (void)
3480 {
3481 reset_specialization ();
3482
3483 if (! processing_template_decl)
3484 return;
3485
3486 /* This matches the pushlevel in begin_template_parm_list. */
3487 finish_scope ();
3488
3489 --processing_template_decl;
3490 current_template_parms = TREE_CHAIN (current_template_parms);
3491 }
3492
3493 /* Within the declaration of a template, return all levels of template
3494 parameters that apply. The template parameters are represented as
3495 a TREE_VEC, in the form documented in cp-tree.h for template
3496 arguments. */
3497
3498 static tree
3499 current_template_args (void)
3500 {
3501 tree header;
3502 tree args = NULL_TREE;
3503 int length = TMPL_PARMS_DEPTH (current_template_parms);
3504 int l = length;
3505
3506 /* If there is only one level of template parameters, we do not
3507 create a TREE_VEC of TREE_VECs. Instead, we return a single
3508 TREE_VEC containing the arguments. */
3509 if (length > 1)
3510 args = make_tree_vec (length);
3511
3512 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3513 {
3514 tree a = copy_node (TREE_VALUE (header));
3515 int i;
3516
3517 TREE_TYPE (a) = NULL_TREE;
3518 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3519 {
3520 tree t = TREE_VEC_ELT (a, i);
3521
3522 /* T will be a list if we are called from within a
3523 begin/end_template_parm_list pair, but a vector directly
3524 if within a begin/end_member_template_processing pair. */
3525 if (TREE_CODE (t) == TREE_LIST)
3526 {
3527 t = TREE_VALUE (t);
3528
3529 if (!error_operand_p (t))
3530 {
3531 if (TREE_CODE (t) == TYPE_DECL
3532 || TREE_CODE (t) == TEMPLATE_DECL)
3533 {
3534 t = TREE_TYPE (t);
3535
3536 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3537 {
3538 /* Turn this argument into a TYPE_ARGUMENT_PACK
3539 with a single element, which expands T. */
3540 tree vec = make_tree_vec (1);
3541 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3542
3543 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3544 SET_ARGUMENT_PACK_ARGS (t, vec);
3545 }
3546 }
3547 else
3548 {
3549 t = DECL_INITIAL (t);
3550
3551 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3552 {
3553 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3554 with a single element, which expands T. */
3555 tree vec = make_tree_vec (1);
3556 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3557 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3558
3559 t = make_node (NONTYPE_ARGUMENT_PACK);
3560 SET_ARGUMENT_PACK_ARGS (t, vec);
3561 TREE_TYPE (t) = type;
3562 }
3563 }
3564 TREE_VEC_ELT (a, i) = t;
3565 }
3566 }
3567 }
3568
3569 if (length > 1)
3570 TREE_VEC_ELT (args, --l) = a;
3571 else
3572 args = a;
3573 }
3574
3575 return args;
3576 }
3577
3578 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3579 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3580 a member template. Used by push_template_decl below. */
3581
3582 static tree
3583 build_template_decl (tree decl, tree parms, bool member_template_p)
3584 {
3585 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3586 DECL_TEMPLATE_PARMS (tmpl) = parms;
3587 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3588 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3589
3590 return tmpl;
3591 }
3592
3593 struct template_parm_data
3594 {
3595 /* The level of the template parameters we are currently
3596 processing. */
3597 int level;
3598
3599 /* The index of the specialization argument we are currently
3600 processing. */
3601 int current_arg;
3602
3603 /* An array whose size is the number of template parameters. The
3604 elements are nonzero if the parameter has been used in any one
3605 of the arguments processed so far. */
3606 int* parms;
3607
3608 /* An array whose size is the number of template arguments. The
3609 elements are nonzero if the argument makes use of template
3610 parameters of this level. */
3611 int* arg_uses_template_parms;
3612 };
3613
3614 /* Subroutine of push_template_decl used to see if each template
3615 parameter in a partial specialization is used in the explicit
3616 argument list. If T is of the LEVEL given in DATA (which is
3617 treated as a template_parm_data*), then DATA->PARMS is marked
3618 appropriately. */
3619
3620 static int
3621 mark_template_parm (tree t, void* data)
3622 {
3623 int level;
3624 int idx;
3625 struct template_parm_data* tpd = (struct template_parm_data*) data;
3626
3627 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3628 {
3629 level = TEMPLATE_PARM_LEVEL (t);
3630 idx = TEMPLATE_PARM_IDX (t);
3631 }
3632 else
3633 {
3634 level = TEMPLATE_TYPE_LEVEL (t);
3635 idx = TEMPLATE_TYPE_IDX (t);
3636 }
3637
3638 if (level == tpd->level)
3639 {
3640 tpd->parms[idx] = 1;
3641 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3642 }
3643
3644 /* Return zero so that for_each_template_parm will continue the
3645 traversal of the tree; we want to mark *every* template parm. */
3646 return 0;
3647 }
3648
3649 /* Process the partial specialization DECL. */
3650
3651 static tree
3652 process_partial_specialization (tree decl)
3653 {
3654 tree type = TREE_TYPE (decl);
3655 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3656 tree specargs = CLASSTYPE_TI_ARGS (type);
3657 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3658 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3659 tree inner_parms;
3660 int nargs = TREE_VEC_LENGTH (inner_args);
3661 int ntparms;
3662 int i;
3663 int did_error_intro = 0;
3664 struct template_parm_data tpd;
3665 struct template_parm_data tpd2;
3666
3667 gcc_assert (current_template_parms);
3668
3669 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3670 ntparms = TREE_VEC_LENGTH (inner_parms);
3671
3672 /* We check that each of the template parameters given in the
3673 partial specialization is used in the argument list to the
3674 specialization. For example:
3675
3676 template <class T> struct S;
3677 template <class T> struct S<T*>;
3678
3679 The second declaration is OK because `T*' uses the template
3680 parameter T, whereas
3681
3682 template <class T> struct S<int>;
3683
3684 is no good. Even trickier is:
3685
3686 template <class T>
3687 struct S1
3688 {
3689 template <class U>
3690 struct S2;
3691 template <class U>
3692 struct S2<T>;
3693 };
3694
3695 The S2<T> declaration is actually invalid; it is a
3696 full-specialization. Of course,
3697
3698 template <class U>
3699 struct S2<T (*)(U)>;
3700
3701 or some such would have been OK. */
3702 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3703 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3704 memset (tpd.parms, 0, sizeof (int) * ntparms);
3705
3706 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3707 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3708 for (i = 0; i < nargs; ++i)
3709 {
3710 tpd.current_arg = i;
3711 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3712 &mark_template_parm,
3713 &tpd,
3714 NULL,
3715 /*include_nondeduced_p=*/false);
3716 }
3717 for (i = 0; i < ntparms; ++i)
3718 if (tpd.parms[i] == 0)
3719 {
3720 /* One of the template parms was not used in the
3721 specialization. */
3722 if (!did_error_intro)
3723 {
3724 error ("template parameters not used in partial specialization:");
3725 did_error_intro = 1;
3726 }
3727
3728 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3729 }
3730
3731 /* [temp.class.spec]
3732
3733 The argument list of the specialization shall not be identical to
3734 the implicit argument list of the primary template. */
3735 if (comp_template_args
3736 (inner_args,
3737 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3738 (maintmpl)))))
3739 error ("partial specialization %qT does not specialize any template arguments", type);
3740
3741 /* [temp.class.spec]
3742
3743 A partially specialized non-type argument expression shall not
3744 involve template parameters of the partial specialization except
3745 when the argument expression is a simple identifier.
3746
3747 The type of a template parameter corresponding to a specialized
3748 non-type argument shall not be dependent on a parameter of the
3749 specialization.
3750
3751 Also, we verify that pack expansions only occur at the
3752 end of the argument list. */
3753 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3754 tpd2.parms = 0;
3755 for (i = 0; i < nargs; ++i)
3756 {
3757 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3758 tree arg = TREE_VEC_ELT (inner_args, i);
3759 tree packed_args = NULL_TREE;
3760 int j, len = 1;
3761
3762 if (ARGUMENT_PACK_P (arg))
3763 {
3764 /* Extract the arguments from the argument pack. We'll be
3765 iterating over these in the following loop. */
3766 packed_args = ARGUMENT_PACK_ARGS (arg);
3767 len = TREE_VEC_LENGTH (packed_args);
3768 }
3769
3770 for (j = 0; j < len; j++)
3771 {
3772 if (packed_args)
3773 /* Get the Jth argument in the parameter pack. */
3774 arg = TREE_VEC_ELT (packed_args, j);
3775
3776 if (PACK_EXPANSION_P (arg))
3777 {
3778 /* Pack expansions must come at the end of the
3779 argument list. */
3780 if ((packed_args && j < len - 1)
3781 || (!packed_args && i < nargs - 1))
3782 {
3783 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3784 error ("parameter pack argument %qE must be at the end of the template argument list", arg);
3785 else
3786 error ("parameter pack argument %qT must be at the end of the template argument list", arg);
3787
3788 if (packed_args)
3789 TREE_VEC_ELT (packed_args, j) = error_mark_node;
3790 }
3791 }
3792
3793 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3794 /* We only care about the pattern. */
3795 arg = PACK_EXPANSION_PATTERN (arg);
3796
3797 if (/* These first two lines are the `non-type' bit. */
3798 !TYPE_P (arg)
3799 && TREE_CODE (arg) != TEMPLATE_DECL
3800 /* This next line is the `argument expression is not just a
3801 simple identifier' condition and also the `specialized
3802 non-type argument' bit. */
3803 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3804 {
3805 if ((!packed_args && tpd.arg_uses_template_parms[i])
3806 || (packed_args && uses_template_parms (arg)))
3807 error ("template argument %qE involves template parameter(s)",
3808 arg);
3809 else
3810 {
3811 /* Look at the corresponding template parameter,
3812 marking which template parameters its type depends
3813 upon. */
3814 tree type = TREE_TYPE (parm);
3815
3816 if (!tpd2.parms)
3817 {
3818 /* We haven't yet initialized TPD2. Do so now. */
3819 tpd2.arg_uses_template_parms
3820 = (int *) alloca (sizeof (int) * nargs);
3821 /* The number of parameters here is the number in the
3822 main template, which, as checked in the assertion
3823 above, is NARGS. */
3824 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3825 tpd2.level =
3826 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3827 }
3828
3829 /* Mark the template parameters. But this time, we're
3830 looking for the template parameters of the main
3831 template, not in the specialization. */
3832 tpd2.current_arg = i;
3833 tpd2.arg_uses_template_parms[i] = 0;
3834 memset (tpd2.parms, 0, sizeof (int) * nargs);
3835 for_each_template_parm (type,
3836 &mark_template_parm,
3837 &tpd2,
3838 NULL,
3839 /*include_nondeduced_p=*/false);
3840
3841 if (tpd2.arg_uses_template_parms [i])
3842 {
3843 /* The type depended on some template parameters.
3844 If they are fully specialized in the
3845 specialization, that's OK. */
3846 int j;
3847 for (j = 0; j < nargs; ++j)
3848 if (tpd2.parms[j] != 0
3849 && tpd.arg_uses_template_parms [j])
3850 {
3851 error ("type %qT of template argument %qE depends "
3852 "on template parameter(s)",
3853 type,
3854 arg);
3855 break;
3856 }
3857 }
3858 }
3859 }
3860 }
3861 }
3862
3863 /* We should only get here once. */
3864 gcc_assert (!COMPLETE_TYPE_P (type));
3865
3866 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
3867 = tree_cons (specargs, inner_parms,
3868 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
3869 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3870 return decl;
3871 }
3872
3873 /* Check that a template declaration's use of default arguments and
3874 parameter packs is not invalid. Here, PARMS are the template
3875 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
3876 a primary template. IS_PARTIAL is nonzero if DECL is a partial
3877 specialization.
3878
3879
3880 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3881 declaration (but not a definition); 1 indicates a declaration, 2
3882 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3883 emitted for extraneous default arguments.
3884
3885 Returns TRUE if there were no errors found, FALSE otherwise. */
3886
3887 bool
3888 check_default_tmpl_args (tree decl, tree parms, int is_primary,
3889 int is_partial, int is_friend_decl)
3890 {
3891 const char *msg;
3892 int last_level_to_check;
3893 tree parm_level;
3894 bool no_errors = true;
3895
3896 /* [temp.param]
3897
3898 A default template-argument shall not be specified in a
3899 function template declaration or a function template definition, nor
3900 in the template-parameter-list of the definition of a member of a
3901 class template. */
3902
3903 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
3904 /* You can't have a function template declaration in a local
3905 scope, nor you can you define a member of a class template in a
3906 local scope. */
3907 return true;
3908
3909 if (current_class_type
3910 && !TYPE_BEING_DEFINED (current_class_type)
3911 && DECL_LANG_SPECIFIC (decl)
3912 && DECL_DECLARES_FUNCTION_P (decl)
3913 /* If this is either a friend defined in the scope of the class
3914 or a member function. */
3915 && (DECL_FUNCTION_MEMBER_P (decl)
3916 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3917 : DECL_FRIEND_CONTEXT (decl)
3918 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3919 : false)
3920 /* And, if it was a member function, it really was defined in
3921 the scope of the class. */
3922 && (!DECL_FUNCTION_MEMBER_P (decl)
3923 || DECL_INITIALIZED_IN_CLASS_P (decl)))
3924 /* We already checked these parameters when the template was
3925 declared, so there's no need to do it again now. This function
3926 was defined in class scope, but we're processing it's body now
3927 that the class is complete. */
3928 return true;
3929
3930 /* Core issue 226 (C++0x only): the following only applies to class
3931 templates. */
3932 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
3933 {
3934 /* [temp.param]
3935
3936 If a template-parameter has a default template-argument, all
3937 subsequent template-parameters shall have a default
3938 template-argument supplied. */
3939 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
3940 {
3941 tree inner_parms = TREE_VALUE (parm_level);
3942 int ntparms = TREE_VEC_LENGTH (inner_parms);
3943 int seen_def_arg_p = 0;
3944 int i;
3945
3946 for (i = 0; i < ntparms; ++i)
3947 {
3948 tree parm = TREE_VEC_ELT (inner_parms, i);
3949
3950 if (parm == error_mark_node)
3951 continue;
3952
3953 if (TREE_PURPOSE (parm))
3954 seen_def_arg_p = 1;
3955 else if (seen_def_arg_p
3956 && !template_parameter_pack_p (TREE_VALUE (parm)))
3957 {
3958 error ("no default argument for %qD", TREE_VALUE (parm));
3959 /* For better subsequent error-recovery, we indicate that
3960 there should have been a default argument. */
3961 TREE_PURPOSE (parm) = error_mark_node;
3962 no_errors = false;
3963 }
3964 else if (is_primary
3965 && !is_partial
3966 && !is_friend_decl
3967 /* Don't complain about an enclosing partial
3968 specialization. */
3969 && parm_level == parms
3970 && TREE_CODE (decl) == TYPE_DECL
3971 && i < ntparms - 1
3972 && template_parameter_pack_p (TREE_VALUE (parm)))
3973 {
3974 /* A primary class template can only have one
3975 parameter pack, at the end of the template
3976 parameter list. */
3977
3978 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
3979 error ("parameter pack %qE must be at the end of the"
3980 " template parameter list", TREE_VALUE (parm));
3981 else
3982 error ("parameter pack %qT must be at the end of the"
3983 " template parameter list",
3984 TREE_TYPE (TREE_VALUE (parm)));
3985
3986 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
3987 = error_mark_node;
3988 no_errors = false;
3989 }
3990 }
3991 }
3992 }
3993
3994 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
3995 || is_partial
3996 || !is_primary
3997 || is_friend_decl)
3998 /* For an ordinary class template, default template arguments are
3999 allowed at the innermost level, e.g.:
4000 template <class T = int>
4001 struct S {};
4002 but, in a partial specialization, they're not allowed even
4003 there, as we have in [temp.class.spec]:
4004
4005 The template parameter list of a specialization shall not
4006 contain default template argument values.
4007
4008 So, for a partial specialization, or for a function template
4009 (in C++98/C++03), we look at all of them. */
4010 ;
4011 else
4012 /* But, for a primary class template that is not a partial
4013 specialization we look at all template parameters except the
4014 innermost ones. */
4015 parms = TREE_CHAIN (parms);
4016
4017 /* Figure out what error message to issue. */
4018 if (is_friend_decl == 2)
4019 msg = "default template arguments may not be used in function template friend re-declaration";
4020 else if (is_friend_decl)
4021 msg = "default template arguments may not be used in function template friend declarations";
4022 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4023 msg = "default template arguments may not be used in function templates";
4024 else if (is_partial)
4025 msg = "default template arguments may not be used in partial specializations";
4026 else
4027 msg = "default argument for template parameter for class enclosing %qD";
4028
4029 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4030 /* If we're inside a class definition, there's no need to
4031 examine the parameters to the class itself. On the one
4032 hand, they will be checked when the class is defined, and,
4033 on the other, default arguments are valid in things like:
4034 template <class T = double>
4035 struct S { template <class U> void f(U); };
4036 Here the default argument for `S' has no bearing on the
4037 declaration of `f'. */
4038 last_level_to_check = template_class_depth (current_class_type) + 1;
4039 else
4040 /* Check everything. */
4041 last_level_to_check = 0;
4042
4043 for (parm_level = parms;
4044 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4045 parm_level = TREE_CHAIN (parm_level))
4046 {
4047 tree inner_parms = TREE_VALUE (parm_level);
4048 int i;
4049 int ntparms;
4050
4051 ntparms = TREE_VEC_LENGTH (inner_parms);
4052 for (i = 0; i < ntparms; ++i)
4053 {
4054 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4055 continue;
4056
4057 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4058 {
4059 if (msg)
4060 {
4061 no_errors = false;
4062 if (is_friend_decl == 2)
4063 return no_errors;
4064
4065 error (msg, decl);
4066 msg = 0;
4067 }
4068
4069 /* Clear out the default argument so that we are not
4070 confused later. */
4071 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4072 }
4073 }
4074
4075 /* At this point, if we're still interested in issuing messages,
4076 they must apply to classes surrounding the object declared. */
4077 if (msg)
4078 msg = "default argument for template parameter for class enclosing %qD";
4079 }
4080
4081 return no_errors;
4082 }
4083
4084 /* Worker for push_template_decl_real, called via
4085 for_each_template_parm. DATA is really an int, indicating the
4086 level of the parameters we are interested in. If T is a template
4087 parameter of that level, return nonzero. */
4088
4089 static int
4090 template_parm_this_level_p (tree t, void* data)
4091 {
4092 int this_level = *(int *)data;
4093 int level;
4094
4095 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4096 level = TEMPLATE_PARM_LEVEL (t);
4097 else
4098 level = TEMPLATE_TYPE_LEVEL (t);
4099 return level == this_level;
4100 }
4101
4102 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4103 parameters given by current_template_args, or reuses a
4104 previously existing one, if appropriate. Returns the DECL, or an
4105 equivalent one, if it is replaced via a call to duplicate_decls.
4106
4107 If IS_FRIEND is true, DECL is a friend declaration. */
4108
4109 tree
4110 push_template_decl_real (tree decl, bool is_friend)
4111 {
4112 tree tmpl;
4113 tree args;
4114 tree info;
4115 tree ctx;
4116 int primary;
4117 int is_partial;
4118 int new_template_p = 0;
4119 /* True if the template is a member template, in the sense of
4120 [temp.mem]. */
4121 bool member_template_p = false;
4122
4123 if (decl == error_mark_node || !current_template_parms)
4124 return error_mark_node;
4125
4126 /* See if this is a partial specialization. */
4127 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4128 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4129 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4130
4131 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4132 is_friend = true;
4133
4134 if (is_friend)
4135 /* For a friend, we want the context of the friend function, not
4136 the type of which it is a friend. */
4137 ctx = DECL_CONTEXT (decl);
4138 else if (CP_DECL_CONTEXT (decl)
4139 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4140 /* In the case of a virtual function, we want the class in which
4141 it is defined. */
4142 ctx = CP_DECL_CONTEXT (decl);
4143 else
4144 /* Otherwise, if we're currently defining some class, the DECL
4145 is assumed to be a member of the class. */
4146 ctx = current_scope ();
4147
4148 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4149 ctx = NULL_TREE;
4150
4151 if (!DECL_CONTEXT (decl))
4152 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4153
4154 /* See if this is a primary template. */
4155 if (is_friend && ctx)
4156 /* A friend template that specifies a class context, i.e.
4157 template <typename T> friend void A<T>::f();
4158 is not primary. */
4159 primary = 0;
4160 else
4161 primary = template_parm_scope_p ();
4162
4163 if (primary)
4164 {
4165 if (DECL_CLASS_SCOPE_P (decl))
4166 member_template_p = true;
4167 if (TREE_CODE (decl) == TYPE_DECL
4168 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4169 {
4170 error ("template class without a name");
4171 return error_mark_node;
4172 }
4173 else if (TREE_CODE (decl) == FUNCTION_DECL)
4174 {
4175 if (DECL_DESTRUCTOR_P (decl))
4176 {
4177 /* [temp.mem]
4178
4179 A destructor shall not be a member template. */
4180 error ("destructor %qD declared as member template", decl);
4181 return error_mark_node;
4182 }
4183 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4184 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
4185 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4186 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4187 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4188 == void_list_node)))
4189 {
4190 /* [basic.stc.dynamic.allocation]
4191
4192 An allocation function can be a function
4193 template. ... Template allocation functions shall
4194 have two or more parameters. */
4195 error ("invalid template declaration of %qD", decl);
4196 return error_mark_node;
4197 }
4198 }
4199 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4200 && CLASS_TYPE_P (TREE_TYPE (decl)))
4201 /* OK */;
4202 else
4203 {
4204 error ("template declaration of %q#D", decl);
4205 return error_mark_node;
4206 }
4207 }
4208
4209 /* Check to see that the rules regarding the use of default
4210 arguments are not being violated. */
4211 check_default_tmpl_args (decl, current_template_parms,
4212 primary, is_partial, /*is_friend_decl=*/0);
4213
4214 /* Ensure that there are no parameter packs in the type of this
4215 declaration that have not been expanded. */
4216 if (TREE_CODE (decl) == FUNCTION_DECL)
4217 {
4218 /* Check each of the arguments individually to see if there are
4219 any bare parameter packs. */
4220 tree type = TREE_TYPE (decl);
4221 tree arg = DECL_ARGUMENTS (decl);
4222 tree argtype = TYPE_ARG_TYPES (type);
4223
4224 while (arg && argtype)
4225 {
4226 if (!FUNCTION_PARAMETER_PACK_P (arg)
4227 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4228 {
4229 /* This is a PARM_DECL that contains unexpanded parameter
4230 packs. We have already complained about this in the
4231 check_for_bare_parameter_packs call, so just replace
4232 these types with ERROR_MARK_NODE. */
4233 TREE_TYPE (arg) = error_mark_node;
4234 TREE_VALUE (argtype) = error_mark_node;
4235 }
4236
4237 arg = TREE_CHAIN (arg);
4238 argtype = TREE_CHAIN (argtype);
4239 }
4240
4241 /* Check for bare parameter packs in the return type and the
4242 exception specifiers. */
4243 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4244 /* Errors were already issued, set return type to int
4245 as the frontend doesn't expect error_mark_node as
4246 the return type. */
4247 TREE_TYPE (type) = integer_type_node;
4248 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4249 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4250 }
4251 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4252 {
4253 TREE_TYPE (decl) = error_mark_node;
4254 return error_mark_node;
4255 }
4256
4257 if (is_partial)
4258 return process_partial_specialization (decl);
4259
4260 args = current_template_args ();
4261
4262 if (!ctx
4263 || TREE_CODE (ctx) == FUNCTION_DECL
4264 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4265 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4266 {
4267 if (DECL_LANG_SPECIFIC (decl)
4268 && DECL_TEMPLATE_INFO (decl)
4269 && DECL_TI_TEMPLATE (decl))
4270 tmpl = DECL_TI_TEMPLATE (decl);
4271 /* If DECL is a TYPE_DECL for a class-template, then there won't
4272 be DECL_LANG_SPECIFIC. The information equivalent to
4273 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4274 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4275 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4276 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4277 {
4278 /* Since a template declaration already existed for this
4279 class-type, we must be redeclaring it here. Make sure
4280 that the redeclaration is valid. */
4281 redeclare_class_template (TREE_TYPE (decl),
4282 current_template_parms);
4283 /* We don't need to create a new TEMPLATE_DECL; just use the
4284 one we already had. */
4285 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4286 }
4287 else
4288 {
4289 tmpl = build_template_decl (decl, current_template_parms,
4290 member_template_p);
4291 new_template_p = 1;
4292
4293 if (DECL_LANG_SPECIFIC (decl)
4294 && DECL_TEMPLATE_SPECIALIZATION (decl))
4295 {
4296 /* A specialization of a member template of a template
4297 class. */
4298 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4299 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4300 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4301 }
4302 }
4303 }
4304 else
4305 {
4306 tree a, t, current, parms;
4307 int i;
4308 tree tinfo = get_template_info (decl);
4309
4310 if (!tinfo)
4311 {
4312 error ("template definition of non-template %q#D", decl);
4313 return error_mark_node;
4314 }
4315
4316 tmpl = TI_TEMPLATE (tinfo);
4317
4318 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4319 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4320 && DECL_TEMPLATE_SPECIALIZATION (decl)
4321 && DECL_MEMBER_TEMPLATE_P (tmpl))
4322 {
4323 tree new_tmpl;
4324
4325 /* The declaration is a specialization of a member
4326 template, declared outside the class. Therefore, the
4327 innermost template arguments will be NULL, so we
4328 replace them with the arguments determined by the
4329 earlier call to check_explicit_specialization. */
4330 args = DECL_TI_ARGS (decl);
4331
4332 new_tmpl
4333 = build_template_decl (decl, current_template_parms,
4334 member_template_p);
4335 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4336 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4337 DECL_TI_TEMPLATE (decl) = new_tmpl;
4338 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4339 DECL_TEMPLATE_INFO (new_tmpl)
4340 = tree_cons (tmpl, args, NULL_TREE);
4341
4342 register_specialization (new_tmpl,
4343 most_general_template (tmpl),
4344 args,
4345 is_friend, 0);
4346 return decl;
4347 }
4348
4349 /* Make sure the template headers we got make sense. */
4350
4351 parms = DECL_TEMPLATE_PARMS (tmpl);
4352 i = TMPL_PARMS_DEPTH (parms);
4353 if (TMPL_ARGS_DEPTH (args) != i)
4354 {
4355 error ("expected %d levels of template parms for %q#D, got %d",
4356 i, decl, TMPL_ARGS_DEPTH (args));
4357 }
4358 else
4359 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4360 {
4361 a = TMPL_ARGS_LEVEL (args, i);
4362 t = INNERMOST_TEMPLATE_PARMS (parms);
4363
4364 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4365 {
4366 if (current == decl)
4367 error ("got %d template parameters for %q#D",
4368 TREE_VEC_LENGTH (a), decl);
4369 else
4370 error ("got %d template parameters for %q#T",
4371 TREE_VEC_LENGTH (a), current);
4372 error (" but %d required", TREE_VEC_LENGTH (t));
4373 return error_mark_node;
4374 }
4375
4376 if (current == decl)
4377 current = ctx;
4378 else
4379 current = (TYPE_P (current)
4380 ? TYPE_CONTEXT (current)
4381 : DECL_CONTEXT (current));
4382 }
4383
4384 /* Check that the parms are used in the appropriate qualifying scopes
4385 in the declarator. */
4386 if (!comp_template_args
4387 (TI_ARGS (tinfo),
4388 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4389 {
4390 error ("\
4391 template arguments to %qD do not match original template %qD",
4392 decl, DECL_TEMPLATE_RESULT (tmpl));
4393 if (!uses_template_parms (TI_ARGS (tinfo)))
4394 inform (input_location, "use template<> for an explicit specialization");
4395 /* Avoid crash in import_export_decl. */
4396 DECL_INTERFACE_KNOWN (decl) = 1;
4397 return error_mark_node;
4398 }
4399 }
4400
4401 DECL_TEMPLATE_RESULT (tmpl) = decl;
4402 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4403
4404 /* Push template declarations for global functions and types. Note
4405 that we do not try to push a global template friend declared in a
4406 template class; such a thing may well depend on the template
4407 parameters of the class. */
4408 if (new_template_p && !ctx
4409 && !(is_friend && template_class_depth (current_class_type) > 0))
4410 {
4411 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4412 if (tmpl == error_mark_node)
4413 return error_mark_node;
4414
4415 /* Hide template friend classes that haven't been declared yet. */
4416 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4417 {
4418 DECL_ANTICIPATED (tmpl) = 1;
4419 DECL_FRIEND_P (tmpl) = 1;
4420 }
4421 }
4422
4423 if (primary)
4424 {
4425 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4426 int i;
4427
4428 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4429 if (DECL_CONV_FN_P (tmpl))
4430 {
4431 int depth = TMPL_PARMS_DEPTH (parms);
4432
4433 /* It is a conversion operator. See if the type converted to
4434 depends on innermost template operands. */
4435
4436 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4437 depth))
4438 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4439 }
4440
4441 /* Give template template parms a DECL_CONTEXT of the template
4442 for which they are a parameter. */
4443 parms = INNERMOST_TEMPLATE_PARMS (parms);
4444 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4445 {
4446 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4447 if (TREE_CODE (parm) == TEMPLATE_DECL)
4448 DECL_CONTEXT (parm) = tmpl;
4449 }
4450 }
4451
4452 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4453 back to its most general template. If TMPL is a specialization,
4454 ARGS may only have the innermost set of arguments. Add the missing
4455 argument levels if necessary. */
4456 if (DECL_TEMPLATE_INFO (tmpl))
4457 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4458
4459 info = tree_cons (tmpl, args, NULL_TREE);
4460
4461 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4462 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4463 else if (DECL_LANG_SPECIFIC (decl))
4464 DECL_TEMPLATE_INFO (decl) = info;
4465
4466 return DECL_TEMPLATE_RESULT (tmpl);
4467 }
4468
4469 tree
4470 push_template_decl (tree decl)
4471 {
4472 return push_template_decl_real (decl, false);
4473 }
4474
4475 /* Called when a class template TYPE is redeclared with the indicated
4476 template PARMS, e.g.:
4477
4478 template <class T> struct S;
4479 template <class T> struct S {}; */
4480
4481 bool
4482 redeclare_class_template (tree type, tree parms)
4483 {
4484 tree tmpl;
4485 tree tmpl_parms;
4486 int i;
4487
4488 if (!TYPE_TEMPLATE_INFO (type))
4489 {
4490 error ("%qT is not a template type", type);
4491 return false;
4492 }
4493
4494 tmpl = TYPE_TI_TEMPLATE (type);
4495 if (!PRIMARY_TEMPLATE_P (tmpl))
4496 /* The type is nested in some template class. Nothing to worry
4497 about here; there are no new template parameters for the nested
4498 type. */
4499 return true;
4500
4501 if (!parms)
4502 {
4503 error ("template specifiers not specified in declaration of %qD",
4504 tmpl);
4505 return false;
4506 }
4507
4508 parms = INNERMOST_TEMPLATE_PARMS (parms);
4509 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4510
4511 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4512 {
4513 error ("redeclared with %d template parameter(s)",
4514 TREE_VEC_LENGTH (parms));
4515 inform (input_location, "previous declaration %q+D used %d template parameter(s)",
4516 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4517 return false;
4518 }
4519
4520 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4521 {
4522 tree tmpl_parm;
4523 tree parm;
4524 tree tmpl_default;
4525 tree parm_default;
4526
4527 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4528 || TREE_VEC_ELT (parms, i) == error_mark_node)
4529 continue;
4530
4531 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4532 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4533 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4534 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4535
4536 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4537 TEMPLATE_DECL. */
4538 if (tmpl_parm != error_mark_node
4539 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4540 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4541 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4542 || (TREE_CODE (tmpl_parm) != PARM_DECL
4543 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4544 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4545 || (TREE_CODE (tmpl_parm) == PARM_DECL
4546 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4547 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))))
4548 {
4549 error ("template parameter %q+#D", tmpl_parm);
4550 error ("redeclared here as %q#D", parm);
4551 return false;
4552 }
4553
4554 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4555 {
4556 /* We have in [temp.param]:
4557
4558 A template-parameter may not be given default arguments
4559 by two different declarations in the same scope. */
4560 error_at (input_location, "redefinition of default argument for %q#D", parm);
4561 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4562 "original definition appeared here");
4563 return false;
4564 }
4565
4566 if (parm_default != NULL_TREE)
4567 /* Update the previous template parameters (which are the ones
4568 that will really count) with the new default value. */
4569 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4570 else if (tmpl_default != NULL_TREE)
4571 /* Update the new parameters, too; they'll be used as the
4572 parameters for any members. */
4573 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4574 }
4575
4576 return true;
4577 }
4578
4579 /* Simplify EXPR if it is a non-dependent expression. Returns the
4580 (possibly simplified) expression. */
4581
4582 tree
4583 fold_non_dependent_expr (tree expr)
4584 {
4585 if (expr == NULL_TREE)
4586 return NULL_TREE;
4587
4588 /* If we're in a template, but EXPR isn't value dependent, simplify
4589 it. We're supposed to treat:
4590
4591 template <typename T> void f(T[1 + 1]);
4592 template <typename T> void f(T[2]);
4593
4594 as two declarations of the same function, for example. */
4595 if (processing_template_decl
4596 && !type_dependent_expression_p (expr)
4597 && !value_dependent_expression_p (expr))
4598 {
4599 HOST_WIDE_INT saved_processing_template_decl;
4600
4601 saved_processing_template_decl = processing_template_decl;
4602 processing_template_decl = 0;
4603 expr = tsubst_copy_and_build (expr,
4604 /*args=*/NULL_TREE,
4605 tf_error,
4606 /*in_decl=*/NULL_TREE,
4607 /*function_p=*/false,
4608 /*integral_constant_expression_p=*/true);
4609 processing_template_decl = saved_processing_template_decl;
4610 }
4611 return expr;
4612 }
4613
4614 /* EXPR is an expression which is used in a constant-expression context.
4615 For instance, it could be a VAR_DECL with a constant initializer.
4616 Extract the innermost constant expression.
4617
4618 This is basically a more powerful version of
4619 integral_constant_value, which can be used also in templates where
4620 initializers can maintain a syntactic rather than semantic form
4621 (even if they are non-dependent, for access-checking purposes). */
4622
4623 static tree
4624 fold_decl_constant_value (tree expr)
4625 {
4626 tree const_expr = expr;
4627 do
4628 {
4629 expr = fold_non_dependent_expr (const_expr);
4630 const_expr = integral_constant_value (expr);
4631 }
4632 while (expr != const_expr);
4633
4634 return expr;
4635 }
4636
4637 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4638 must be a function or a pointer-to-function type, as specified
4639 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4640 and check that the resulting function has external linkage. */
4641
4642 static tree
4643 convert_nontype_argument_function (tree type, tree expr)
4644 {
4645 tree fns = expr;
4646 tree fn, fn_no_ptr;
4647
4648 fn = instantiate_type (type, fns, tf_none);
4649 if (fn == error_mark_node)
4650 return error_mark_node;
4651
4652 fn_no_ptr = fn;
4653 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4654 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4655 if (TREE_CODE (fn_no_ptr) == BASELINK)
4656 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4657
4658 /* [temp.arg.nontype]/1
4659
4660 A template-argument for a non-type, non-template template-parameter
4661 shall be one of:
4662 [...]
4663 -- the address of an object or function with external linkage. */
4664 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4665 {
4666 error ("%qE is not a valid template argument for type %qT "
4667 "because function %qD has not external linkage",
4668 expr, type, fn_no_ptr);
4669 return NULL_TREE;
4670 }
4671
4672 return fn;
4673 }
4674
4675 /* Attempt to convert the non-type template parameter EXPR to the
4676 indicated TYPE. If the conversion is successful, return the
4677 converted value. If the conversion is unsuccessful, return
4678 NULL_TREE if we issued an error message, or error_mark_node if we
4679 did not. We issue error messages for out-and-out bad template
4680 parameters, but not simply because the conversion failed, since we
4681 might be just trying to do argument deduction. Both TYPE and EXPR
4682 must be non-dependent.
4683
4684 The conversion follows the special rules described in
4685 [temp.arg.nontype], and it is much more strict than an implicit
4686 conversion.
4687
4688 This function is called twice for each template argument (see
4689 lookup_template_class for a more accurate description of this
4690 problem). This means that we need to handle expressions which
4691 are not valid in a C++ source, but can be created from the
4692 first call (for instance, casts to perform conversions). These
4693 hacks can go away after we fix the double coercion problem. */
4694
4695 static tree
4696 convert_nontype_argument (tree type, tree expr)
4697 {
4698 tree expr_type;
4699
4700 /* Detect immediately string literals as invalid non-type argument.
4701 This special-case is not needed for correctness (we would easily
4702 catch this later), but only to provide better diagnostic for this
4703 common user mistake. As suggested by DR 100, we do not mention
4704 linkage issues in the diagnostic as this is not the point. */
4705 if (TREE_CODE (expr) == STRING_CST)
4706 {
4707 error ("%qE is not a valid template argument for type %qT "
4708 "because string literals can never be used in this context",
4709 expr, type);
4710 return NULL_TREE;
4711 }
4712
4713 /* If we are in a template, EXPR may be non-dependent, but still
4714 have a syntactic, rather than semantic, form. For example, EXPR
4715 might be a SCOPE_REF, rather than the VAR_DECL to which the
4716 SCOPE_REF refers. Preserving the qualifying scope is necessary
4717 so that access checking can be performed when the template is
4718 instantiated -- but here we need the resolved form so that we can
4719 convert the argument. */
4720 expr = fold_non_dependent_expr (expr);
4721 if (error_operand_p (expr))
4722 return error_mark_node;
4723 expr_type = TREE_TYPE (expr);
4724
4725 /* HACK: Due to double coercion, we can get a
4726 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4727 which is the tree that we built on the first call (see
4728 below when coercing to reference to object or to reference to
4729 function). We just strip everything and get to the arg.
4730 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4731 for examples. */
4732 if (TREE_CODE (expr) == NOP_EXPR)
4733 {
4734 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4735 {
4736 /* ??? Maybe we could use convert_from_reference here, but we
4737 would need to relax its constraints because the NOP_EXPR
4738 could actually change the type to something more cv-qualified,
4739 and this is not folded by convert_from_reference. */
4740 tree addr = TREE_OPERAND (expr, 0);
4741 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4742 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4743 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4744 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4745 (TREE_TYPE (expr_type),
4746 TREE_TYPE (TREE_TYPE (addr))));
4747
4748 expr = TREE_OPERAND (addr, 0);
4749 expr_type = TREE_TYPE (expr);
4750 }
4751
4752 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4753 parameter is a pointer to object, through decay and
4754 qualification conversion. Let's strip everything. */
4755 else if (TYPE_PTROBV_P (type))
4756 {
4757 STRIP_NOPS (expr);
4758 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4759 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4760 /* Skip the ADDR_EXPR only if it is part of the decay for
4761 an array. Otherwise, it is part of the original argument
4762 in the source code. */
4763 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4764 expr = TREE_OPERAND (expr, 0);
4765 expr_type = TREE_TYPE (expr);
4766 }
4767 }
4768
4769 /* [temp.arg.nontype]/5, bullet 1
4770
4771 For a non-type template-parameter of integral or enumeration type,
4772 integral promotions (_conv.prom_) and integral conversions
4773 (_conv.integral_) are applied. */
4774 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4775 {
4776 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
4777 return error_mark_node;
4778
4779 expr = fold_decl_constant_value (expr);
4780 /* Notice that there are constant expressions like '4 % 0' which
4781 do not fold into integer constants. */
4782 if (TREE_CODE (expr) != INTEGER_CST)
4783 {
4784 error ("%qE is not a valid template argument for type %qT "
4785 "because it is a non-constant expression", expr, type);
4786 return NULL_TREE;
4787 }
4788
4789 /* At this point, an implicit conversion does what we want,
4790 because we already know that the expression is of integral
4791 type. */
4792 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4793 if (expr == error_mark_node)
4794 return error_mark_node;
4795
4796 /* Conversion was allowed: fold it to a bare integer constant. */
4797 expr = fold (expr);
4798 }
4799 /* [temp.arg.nontype]/5, bullet 2
4800
4801 For a non-type template-parameter of type pointer to object,
4802 qualification conversions (_conv.qual_) and the array-to-pointer
4803 conversion (_conv.array_) are applied. */
4804 else if (TYPE_PTROBV_P (type))
4805 {
4806 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4807
4808 A template-argument for a non-type, non-template template-parameter
4809 shall be one of: [...]
4810
4811 -- the name of a non-type template-parameter;
4812 -- the address of an object or function with external linkage, [...]
4813 expressed as "& id-expression" where the & is optional if the name
4814 refers to a function or array, or if the corresponding
4815 template-parameter is a reference.
4816
4817 Here, we do not care about functions, as they are invalid anyway
4818 for a parameter of type pointer-to-object. */
4819
4820 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4821 /* Non-type template parameters are OK. */
4822 ;
4823 else if (TREE_CODE (expr) != ADDR_EXPR
4824 && TREE_CODE (expr_type) != ARRAY_TYPE)
4825 {
4826 if (TREE_CODE (expr) == VAR_DECL)
4827 {
4828 error ("%qD is not a valid template argument "
4829 "because %qD is a variable, not the address of "
4830 "a variable",
4831 expr, expr);
4832 return NULL_TREE;
4833 }
4834 /* Other values, like integer constants, might be valid
4835 non-type arguments of some other type. */
4836 return error_mark_node;
4837 }
4838 else
4839 {
4840 tree decl;
4841
4842 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4843 ? TREE_OPERAND (expr, 0) : expr);
4844 if (TREE_CODE (decl) != VAR_DECL)
4845 {
4846 error ("%qE is not a valid template argument of type %qT "
4847 "because %qE is not a variable",
4848 expr, type, decl);
4849 return NULL_TREE;
4850 }
4851 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4852 {
4853 error ("%qE is not a valid template argument of type %qT "
4854 "because %qD does not have external linkage",
4855 expr, type, decl);
4856 return NULL_TREE;
4857 }
4858 }
4859
4860 expr = decay_conversion (expr);
4861 if (expr == error_mark_node)
4862 return error_mark_node;
4863
4864 expr = perform_qualification_conversions (type, expr);
4865 if (expr == error_mark_node)
4866 return error_mark_node;
4867 }
4868 /* [temp.arg.nontype]/5, bullet 3
4869
4870 For a non-type template-parameter of type reference to object, no
4871 conversions apply. The type referred to by the reference may be more
4872 cv-qualified than the (otherwise identical) type of the
4873 template-argument. The template-parameter is bound directly to the
4874 template-argument, which must be an lvalue. */
4875 else if (TYPE_REF_OBJ_P (type))
4876 {
4877 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4878 expr_type))
4879 return error_mark_node;
4880
4881 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4882 {
4883 error ("%qE is not a valid template argument for type %qT "
4884 "because of conflicts in cv-qualification", expr, type);
4885 return NULL_TREE;
4886 }
4887
4888 if (!real_lvalue_p (expr))
4889 {
4890 error ("%qE is not a valid template argument for type %qT "
4891 "because it is not an lvalue", expr, type);
4892 return NULL_TREE;
4893 }
4894
4895 /* [temp.arg.nontype]/1
4896
4897 A template-argument for a non-type, non-template template-parameter
4898 shall be one of: [...]
4899
4900 -- the address of an object or function with external linkage. */
4901 if (!DECL_EXTERNAL_LINKAGE_P (expr))
4902 {
4903 error ("%qE is not a valid template argument for type %qT "
4904 "because object %qD has not external linkage",
4905 expr, type, expr);
4906 return NULL_TREE;
4907 }
4908
4909 expr = build_nop (type, build_address (expr));
4910 }
4911 /* [temp.arg.nontype]/5, bullet 4
4912
4913 For a non-type template-parameter of type pointer to function, only
4914 the function-to-pointer conversion (_conv.func_) is applied. If the
4915 template-argument represents a set of overloaded functions (or a
4916 pointer to such), the matching function is selected from the set
4917 (_over.over_). */
4918 else if (TYPE_PTRFN_P (type))
4919 {
4920 /* If the argument is a template-id, we might not have enough
4921 context information to decay the pointer. */
4922 if (!type_unknown_p (expr_type))
4923 {
4924 expr = decay_conversion (expr);
4925 if (expr == error_mark_node)
4926 return error_mark_node;
4927 }
4928
4929 expr = convert_nontype_argument_function (type, expr);
4930 if (!expr || expr == error_mark_node)
4931 return expr;
4932
4933 if (TREE_CODE (expr) != ADDR_EXPR)
4934 {
4935 error ("%qE is not a valid template argument for type %qT", expr, type);
4936 error ("it must be the address of a function with external linkage");
4937 return NULL_TREE;
4938 }
4939 }
4940 /* [temp.arg.nontype]/5, bullet 5
4941
4942 For a non-type template-parameter of type reference to function, no
4943 conversions apply. If the template-argument represents a set of
4944 overloaded functions, the matching function is selected from the set
4945 (_over.over_). */
4946 else if (TYPE_REFFN_P (type))
4947 {
4948 if (TREE_CODE (expr) == ADDR_EXPR)
4949 {
4950 error ("%qE is not a valid template argument for type %qT "
4951 "because it is a pointer", expr, type);
4952 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
4953 return NULL_TREE;
4954 }
4955
4956 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
4957 if (!expr || expr == error_mark_node)
4958 return expr;
4959
4960 expr = build_nop (type, build_address (expr));
4961 }
4962 /* [temp.arg.nontype]/5, bullet 6
4963
4964 For a non-type template-parameter of type pointer to member function,
4965 no conversions apply. If the template-argument represents a set of
4966 overloaded member functions, the matching member function is selected
4967 from the set (_over.over_). */
4968 else if (TYPE_PTRMEMFUNC_P (type))
4969 {
4970 expr = instantiate_type (type, expr, tf_none);
4971 if (expr == error_mark_node)
4972 return error_mark_node;
4973
4974 /* There is no way to disable standard conversions in
4975 resolve_address_of_overloaded_function (called by
4976 instantiate_type). It is possible that the call succeeded by
4977 converting &B::I to &D::I (where B is a base of D), so we need
4978 to reject this conversion here.
4979
4980 Actually, even if there was a way to disable standard conversions,
4981 it would still be better to reject them here so that we can
4982 provide a superior diagnostic. */
4983 if (!same_type_p (TREE_TYPE (expr), type))
4984 {
4985 /* Make sure we are just one standard conversion off. */
4986 gcc_assert (can_convert (type, TREE_TYPE (expr)));
4987 error ("%qE is not a valid template argument for type %qT "
4988 "because it is of type %qT", expr, type,
4989 TREE_TYPE (expr));
4990 inform (input_location, "standard conversions are not allowed in this context");
4991 return NULL_TREE;
4992 }
4993 }
4994 /* [temp.arg.nontype]/5, bullet 7
4995
4996 For a non-type template-parameter of type pointer to data member,
4997 qualification conversions (_conv.qual_) are applied. */
4998 else if (TYPE_PTRMEM_P (type))
4999 {
5000 expr = perform_qualification_conversions (type, expr);
5001 if (expr == error_mark_node)
5002 return expr;
5003 }
5004 /* A template non-type parameter must be one of the above. */
5005 else
5006 gcc_unreachable ();
5007
5008 /* Sanity check: did we actually convert the argument to the
5009 right type? */
5010 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
5011 return expr;
5012 }
5013
5014 /* Subroutine of coerce_template_template_parms, which returns 1 if
5015 PARM_PARM and ARG_PARM match using the rule for the template
5016 parameters of template template parameters. Both PARM and ARG are
5017 template parameters; the rest of the arguments are the same as for
5018 coerce_template_template_parms.
5019 */
5020 static int
5021 coerce_template_template_parm (tree parm,
5022 tree arg,
5023 tsubst_flags_t complain,
5024 tree in_decl,
5025 tree outer_args)
5026 {
5027 if (arg == NULL_TREE || arg == error_mark_node
5028 || parm == NULL_TREE || parm == error_mark_node)
5029 return 0;
5030
5031 if (TREE_CODE (arg) != TREE_CODE (parm))
5032 return 0;
5033
5034 switch (TREE_CODE (parm))
5035 {
5036 case TEMPLATE_DECL:
5037 /* We encounter instantiations of templates like
5038 template <template <template <class> class> class TT>
5039 class C; */
5040 {
5041 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5042 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5043
5044 if (!coerce_template_template_parms
5045 (parmparm, argparm, complain, in_decl, outer_args))
5046 return 0;
5047 }
5048 /* Fall through. */
5049
5050 case TYPE_DECL:
5051 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5052 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5053 /* Argument is a parameter pack but parameter is not. */
5054 return 0;
5055 break;
5056
5057 case PARM_DECL:
5058 /* The tsubst call is used to handle cases such as
5059
5060 template <int> class C {};
5061 template <class T, template <T> class TT> class D {};
5062 D<int, C> d;
5063
5064 i.e. the parameter list of TT depends on earlier parameters. */
5065 if (!uses_template_parms (TREE_TYPE (arg))
5066 && !same_type_p
5067 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5068 TREE_TYPE (arg)))
5069 return 0;
5070
5071 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5072 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5073 /* Argument is a parameter pack but parameter is not. */
5074 return 0;
5075
5076 break;
5077
5078 default:
5079 gcc_unreachable ();
5080 }
5081
5082 return 1;
5083 }
5084
5085
5086 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5087 template template parameters. Both PARM_PARMS and ARG_PARMS are
5088 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5089 or PARM_DECL.
5090
5091 Consider the example:
5092 template <class T> class A;
5093 template<template <class U> class TT> class B;
5094
5095 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5096 the parameters to A, and OUTER_ARGS contains A. */
5097
5098 static int
5099 coerce_template_template_parms (tree parm_parms,
5100 tree arg_parms,
5101 tsubst_flags_t complain,
5102 tree in_decl,
5103 tree outer_args)
5104 {
5105 int nparms, nargs, i;
5106 tree parm, arg;
5107 int variadic_p = 0;
5108
5109 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5110 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5111
5112 nparms = TREE_VEC_LENGTH (parm_parms);
5113 nargs = TREE_VEC_LENGTH (arg_parms);
5114
5115 /* Determine whether we have a parameter pack at the end of the
5116 template template parameter's template parameter list. */
5117 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5118 {
5119 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5120
5121 if (parm == error_mark_node)
5122 return 0;
5123
5124 switch (TREE_CODE (parm))
5125 {
5126 case TEMPLATE_DECL:
5127 case TYPE_DECL:
5128 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5129 variadic_p = 1;
5130 break;
5131
5132 case PARM_DECL:
5133 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5134 variadic_p = 1;
5135 break;
5136
5137 default:
5138 gcc_unreachable ();
5139 }
5140 }
5141
5142 if (nargs != nparms
5143 && !(variadic_p && nargs >= nparms - 1))
5144 return 0;
5145
5146 /* Check all of the template parameters except the parameter pack at
5147 the end (if any). */
5148 for (i = 0; i < nparms - variadic_p; ++i)
5149 {
5150 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5151 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5152 continue;
5153
5154 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5155 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5156
5157 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5158 outer_args))
5159 return 0;
5160
5161 }
5162
5163 if (variadic_p)
5164 {
5165 /* Check each of the template parameters in the template
5166 argument against the template parameter pack at the end of
5167 the template template parameter. */
5168 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5169 return 0;
5170
5171 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5172
5173 for (; i < nargs; ++i)
5174 {
5175 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5176 continue;
5177
5178 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5179
5180 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5181 outer_args))
5182 return 0;
5183 }
5184 }
5185
5186 return 1;
5187 }
5188
5189 /* Verifies that the deduced template arguments (in TARGS) for the
5190 template template parameters (in TPARMS) represent valid bindings,
5191 by comparing the template parameter list of each template argument
5192 to the template parameter list of its corresponding template
5193 template parameter, in accordance with DR150. This
5194 routine can only be called after all template arguments have been
5195 deduced. It will return TRUE if all of the template template
5196 parameter bindings are okay, FALSE otherwise. */
5197 bool
5198 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5199 {
5200 int i, ntparms = TREE_VEC_LENGTH (tparms);
5201 bool ret = true;
5202
5203 /* We're dealing with template parms in this process. */
5204 ++processing_template_decl;
5205
5206 targs = INNERMOST_TEMPLATE_ARGS (targs);
5207
5208 for (i = 0; i < ntparms; ++i)
5209 {
5210 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5211 tree targ = TREE_VEC_ELT (targs, i);
5212
5213 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5214 {
5215 tree packed_args = NULL_TREE;
5216 int idx, len = 1;
5217
5218 if (ARGUMENT_PACK_P (targ))
5219 {
5220 /* Look inside the argument pack. */
5221 packed_args = ARGUMENT_PACK_ARGS (targ);
5222 len = TREE_VEC_LENGTH (packed_args);
5223 }
5224
5225 for (idx = 0; idx < len; ++idx)
5226 {
5227 tree targ_parms = NULL_TREE;
5228
5229 if (packed_args)
5230 /* Extract the next argument from the argument
5231 pack. */
5232 targ = TREE_VEC_ELT (packed_args, idx);
5233
5234 if (PACK_EXPANSION_P (targ))
5235 /* Look at the pattern of the pack expansion. */
5236 targ = PACK_EXPANSION_PATTERN (targ);
5237
5238 /* Extract the template parameters from the template
5239 argument. */
5240 if (TREE_CODE (targ) == TEMPLATE_DECL)
5241 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5242 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5243 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5244
5245 /* Verify that we can coerce the template template
5246 parameters from the template argument to the template
5247 parameter. This requires an exact match. */
5248 if (targ_parms
5249 && !coerce_template_template_parms
5250 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5251 targ_parms,
5252 tf_none,
5253 tparm,
5254 targs))
5255 {
5256 ret = false;
5257 goto out;
5258 }
5259 }
5260 }
5261 }
5262
5263 out:
5264
5265 --processing_template_decl;
5266 return ret;
5267 }
5268
5269 /* Convert the indicated template ARG as necessary to match the
5270 indicated template PARM. Returns the converted ARG, or
5271 error_mark_node if the conversion was unsuccessful. Error and
5272 warning messages are issued under control of COMPLAIN. This
5273 conversion is for the Ith parameter in the parameter list. ARGS is
5274 the full set of template arguments deduced so far. */
5275
5276 static tree
5277 convert_template_argument (tree parm,
5278 tree arg,
5279 tree args,
5280 tsubst_flags_t complain,
5281 int i,
5282 tree in_decl)
5283 {
5284 tree orig_arg;
5285 tree val;
5286 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5287
5288 if (TREE_CODE (arg) == TREE_LIST
5289 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5290 {
5291 /* The template argument was the name of some
5292 member function. That's usually
5293 invalid, but static members are OK. In any
5294 case, grab the underlying fields/functions
5295 and issue an error later if required. */
5296 orig_arg = TREE_VALUE (arg);
5297 TREE_TYPE (arg) = unknown_type_node;
5298 }
5299
5300 orig_arg = arg;
5301
5302 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5303 requires_type = (TREE_CODE (parm) == TYPE_DECL
5304 || requires_tmpl_type);
5305
5306 /* When determining whether an argument pack expansion is a template,
5307 look at the pattern. */
5308 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5309 arg = PACK_EXPANSION_PATTERN (arg);
5310
5311 is_tmpl_type =
5312 ((TREE_CODE (arg) == TEMPLATE_DECL
5313 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5314 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5315 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
5316
5317 if (is_tmpl_type
5318 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5319 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
5320 arg = TYPE_STUB_DECL (arg);
5321
5322 is_type = TYPE_P (arg) || is_tmpl_type;
5323
5324 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5325 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5326 {
5327 permerror (input_location, "to refer to a type member of a template parameter, "
5328 "use %<typename %E%>", orig_arg);
5329
5330 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
5331 TREE_OPERAND (arg, 1),
5332 typename_type,
5333 complain & tf_error);
5334 arg = orig_arg;
5335 is_type = 1;
5336 }
5337 if (is_type != requires_type)
5338 {
5339 if (in_decl)
5340 {
5341 if (complain & tf_error)
5342 {
5343 error ("type/value mismatch at argument %d in template "
5344 "parameter list for %qD",
5345 i + 1, in_decl);
5346 if (is_type)
5347 error (" expected a constant of type %qT, got %qT",
5348 TREE_TYPE (parm),
5349 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
5350 else if (requires_tmpl_type)
5351 error (" expected a class template, got %qE", orig_arg);
5352 else
5353 error (" expected a type, got %qE", orig_arg);
5354 }
5355 }
5356 return error_mark_node;
5357 }
5358 if (is_tmpl_type ^ requires_tmpl_type)
5359 {
5360 if (in_decl && (complain & tf_error))
5361 {
5362 error ("type/value mismatch at argument %d in template "
5363 "parameter list for %qD",
5364 i + 1, in_decl);
5365 if (is_tmpl_type)
5366 error (" expected a type, got %qT", DECL_NAME (arg));
5367 else
5368 error (" expected a class template, got %qT", orig_arg);
5369 }
5370 return error_mark_node;
5371 }
5372
5373 if (is_type)
5374 {
5375 if (requires_tmpl_type)
5376 {
5377 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5378 /* The number of argument required is not known yet.
5379 Just accept it for now. */
5380 val = TREE_TYPE (arg);
5381 else
5382 {
5383 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5384 tree argparm;
5385
5386 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5387
5388 if (coerce_template_template_parms (parmparm, argparm,
5389 complain, in_decl,
5390 args))
5391 {
5392 val = orig_arg;
5393
5394 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5395 TEMPLATE_DECL. */
5396 if (val != error_mark_node)
5397 {
5398 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5399 val = TREE_TYPE (val);
5400 else if (TREE_CODE (val) == TYPE_PACK_EXPANSION
5401 && DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
5402 {
5403 val = TREE_TYPE (arg);
5404 val = make_pack_expansion (val);
5405 }
5406 }
5407 }
5408 else
5409 {
5410 if (in_decl && (complain & tf_error))
5411 {
5412 error ("type/value mismatch at argument %d in "
5413 "template parameter list for %qD",
5414 i + 1, in_decl);
5415 error (" expected a template of type %qD, got %qD",
5416 parm, orig_arg);
5417 }
5418
5419 val = error_mark_node;
5420 }
5421 }
5422 }
5423 else
5424 val = orig_arg;
5425 /* We only form one instance of each template specialization.
5426 Therefore, if we use a non-canonical variant (i.e., a
5427 typedef), any future messages referring to the type will use
5428 the typedef, which is confusing if those future uses do not
5429 themselves also use the typedef. */
5430 if (TYPE_P (val))
5431 val = strip_typedefs (val);
5432 }
5433 else
5434 {
5435 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5436
5437 if (invalid_nontype_parm_type_p (t, complain))
5438 return error_mark_node;
5439
5440 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5441 {
5442 if (same_type_p (t, TREE_TYPE (orig_arg)))
5443 val = orig_arg;
5444 else
5445 {
5446 /* Not sure if this is reachable, but it doesn't hurt
5447 to be robust. */
5448 error ("type mismatch in nontype parameter pack");
5449 val = error_mark_node;
5450 }
5451 }
5452 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5453 /* We used to call digest_init here. However, digest_init
5454 will report errors, which we don't want when complain
5455 is zero. More importantly, digest_init will try too
5456 hard to convert things: for example, `0' should not be
5457 converted to pointer type at this point according to
5458 the standard. Accepting this is not merely an
5459 extension, since deciding whether or not these
5460 conversions can occur is part of determining which
5461 function template to call, or whether a given explicit
5462 argument specification is valid. */
5463 val = convert_nontype_argument (t, orig_arg);
5464 else
5465 val = orig_arg;
5466
5467 if (val == NULL_TREE)
5468 val = error_mark_node;
5469 else if (val == error_mark_node && (complain & tf_error))
5470 error ("could not convert template argument %qE to %qT", orig_arg, t);
5471 }
5472
5473 return val;
5474 }
5475
5476 /* Coerces the remaining template arguments in INNER_ARGS (from
5477 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5478 Returns the coerced argument pack. PARM_IDX is the position of this
5479 parameter in the template parameter list. ARGS is the original
5480 template argument list. */
5481 static tree
5482 coerce_template_parameter_pack (tree parms,
5483 int parm_idx,
5484 tree args,
5485 tree inner_args,
5486 int arg_idx,
5487 tree new_args,
5488 int* lost,
5489 tree in_decl,
5490 tsubst_flags_t complain)
5491 {
5492 tree parm = TREE_VEC_ELT (parms, parm_idx);
5493 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5494 tree packed_args;
5495 tree argument_pack;
5496 tree packed_types = NULL_TREE;
5497
5498 if (arg_idx > nargs)
5499 arg_idx = nargs;
5500
5501 packed_args = make_tree_vec (nargs - arg_idx);
5502
5503 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5504 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5505 {
5506 /* When the template parameter is a non-type template
5507 parameter pack whose type uses parameter packs, we need
5508 to look at each of the template arguments
5509 separately. Build a vector of the types for these
5510 non-type template parameters in PACKED_TYPES. */
5511 tree expansion
5512 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5513 packed_types = tsubst_pack_expansion (expansion, args,
5514 complain, in_decl);
5515
5516 if (packed_types == error_mark_node)
5517 return error_mark_node;
5518
5519 /* Check that we have the right number of arguments. */
5520 if (arg_idx < nargs
5521 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5522 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5523 {
5524 int needed_parms
5525 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5526 error ("wrong number of template arguments (%d, should be %d)",
5527 nargs, needed_parms);
5528 return error_mark_node;
5529 }
5530
5531 /* If we aren't able to check the actual arguments now
5532 (because they haven't been expanded yet), we can at least
5533 verify that all of the types used for the non-type
5534 template parameter pack are, in fact, valid for non-type
5535 template parameters. */
5536 if (arg_idx < nargs
5537 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5538 {
5539 int j, len = TREE_VEC_LENGTH (packed_types);
5540 for (j = 0; j < len; ++j)
5541 {
5542 tree t = TREE_VEC_ELT (packed_types, j);
5543 if (invalid_nontype_parm_type_p (t, complain))
5544 return error_mark_node;
5545 }
5546 }
5547 }
5548
5549 /* Convert the remaining arguments, which will be a part of the
5550 parameter pack "parm". */
5551 for (; arg_idx < nargs; ++arg_idx)
5552 {
5553 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5554 tree actual_parm = TREE_VALUE (parm);
5555
5556 if (packed_types && !PACK_EXPANSION_P (arg))
5557 {
5558 /* When we have a vector of types (corresponding to the
5559 non-type template parameter pack that uses parameter
5560 packs in its type, as mention above), and the
5561 argument is not an expansion (which expands to a
5562 currently unknown number of arguments), clone the
5563 parm and give it the next type in PACKED_TYPES. */
5564 actual_parm = copy_node (actual_parm);
5565 TREE_TYPE (actual_parm) =
5566 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5567 }
5568
5569 if (arg != error_mark_node)
5570 arg = convert_template_argument (actual_parm,
5571 arg, new_args, complain, parm_idx,
5572 in_decl);
5573 if (arg == error_mark_node)
5574 (*lost)++;
5575 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5576 }
5577
5578 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5579 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5580 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
5581 else
5582 {
5583 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5584 TREE_TYPE (argument_pack)
5585 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5586 TREE_CONSTANT (argument_pack) = 1;
5587 }
5588
5589 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5590 return argument_pack;
5591 }
5592
5593 /* Convert all template arguments to their appropriate types, and
5594 return a vector containing the innermost resulting template
5595 arguments. If any error occurs, return error_mark_node. Error and
5596 warning messages are issued under control of COMPLAIN.
5597
5598 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5599 for arguments not specified in ARGS. Otherwise, if
5600 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5601 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5602 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5603 ARGS. */
5604
5605 static tree
5606 coerce_template_parms (tree parms,
5607 tree args,
5608 tree in_decl,
5609 tsubst_flags_t complain,
5610 bool require_all_args,
5611 bool use_default_args)
5612 {
5613 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5614 tree inner_args;
5615 tree new_args;
5616 tree new_inner_args;
5617 int saved_unevaluated_operand;
5618 int saved_inhibit_evaluation_warnings;
5619
5620 /* When used as a boolean value, indicates whether this is a
5621 variadic template parameter list. Since it's an int, we can also
5622 subtract it from nparms to get the number of non-variadic
5623 parameters. */
5624 int variadic_p = 0;
5625
5626 nparms = TREE_VEC_LENGTH (parms);
5627
5628 /* Determine if there are any parameter packs. */
5629 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5630 {
5631 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5632 if (template_parameter_pack_p (tparm))
5633 ++variadic_p;
5634 }
5635
5636 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5637 /* If there are 0 or 1 parameter packs, we need to expand any argument
5638 packs so that we can deduce a parameter pack from some non-packed args
5639 followed by an argument pack, as in variadic85.C. If there are more
5640 than that, we need to leave argument packs intact so the arguments are
5641 assigned to the right parameter packs. This should only happen when
5642 dealing with a nested class inside a partial specialization of a class
5643 template, as in variadic92.C. */
5644 if (variadic_p <= 1)
5645 inner_args = expand_template_argument_pack (inner_args);
5646
5647 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5648 if ((nargs > nparms && !variadic_p)
5649 || (nargs < nparms - variadic_p
5650 && require_all_args
5651 && (!use_default_args
5652 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5653 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
5654 {
5655 if (complain & tf_error)
5656 {
5657 const char *or_more = "";
5658 if (variadic_p)
5659 {
5660 or_more = " or more";
5661 --nparms;
5662 }
5663
5664 error ("wrong number of template arguments (%d, should be %d%s)",
5665 nargs, nparms, or_more);
5666
5667 if (in_decl)
5668 error ("provided for %q+D", in_decl);
5669 }
5670
5671 return error_mark_node;
5672 }
5673
5674 /* We need to evaluate the template arguments, even though this
5675 template-id may be nested within a "sizeof". */
5676 saved_unevaluated_operand = cp_unevaluated_operand;
5677 cp_unevaluated_operand = 0;
5678 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5679 c_inhibit_evaluation_warnings = 0;
5680 new_inner_args = make_tree_vec (nparms);
5681 new_args = add_outermost_template_args (args, new_inner_args);
5682 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5683 {
5684 tree arg;
5685 tree parm;
5686
5687 /* Get the Ith template parameter. */
5688 parm = TREE_VEC_ELT (parms, parm_idx);
5689
5690 if (parm == error_mark_node)
5691 {
5692 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5693 continue;
5694 }
5695
5696 /* Calculate the next argument. */
5697 if (arg_idx < nargs)
5698 arg = TREE_VEC_ELT (inner_args, arg_idx);
5699 else
5700 arg = NULL_TREE;
5701
5702 if (template_parameter_pack_p (TREE_VALUE (parm))
5703 && !(arg && ARGUMENT_PACK_P (arg)))
5704 {
5705 /* All remaining arguments will be placed in the
5706 template parameter pack PARM. */
5707 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5708 inner_args, arg_idx,
5709 new_args, &lost,
5710 in_decl, complain);
5711
5712 /* Store this argument. */
5713 if (arg == error_mark_node)
5714 lost++;
5715 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5716
5717 /* We are done with all of the arguments. */
5718 arg_idx = nargs;
5719
5720 continue;
5721 }
5722 else if (arg)
5723 {
5724 if (PACK_EXPANSION_P (arg))
5725 {
5726 if (complain & tf_error)
5727 {
5728 /* FIXME this restriction was removed by N2555; see
5729 bug 35722. */
5730 /* If ARG is a pack expansion, but PARM is not a
5731 template parameter pack (if it were, we would have
5732 handled it above), we're trying to expand into a
5733 fixed-length argument list. */
5734 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5735 sorry ("cannot expand %<%E%> into a fixed-length "
5736 "argument list", arg);
5737 else
5738 sorry ("cannot expand %<%T%> into a fixed-length "
5739 "argument list", arg);
5740 }
5741 return error_mark_node;
5742 }
5743 }
5744 else if (require_all_args)
5745 /* There must be a default arg in this case. */
5746 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
5747 complain, in_decl);
5748 else
5749 break;
5750
5751 if (arg == error_mark_node)
5752 {
5753 if (complain & tf_error)
5754 error ("template argument %d is invalid", arg_idx + 1);
5755 }
5756 else if (!arg)
5757 /* This only occurs if there was an error in the template
5758 parameter list itself (which we would already have
5759 reported) that we are trying to recover from, e.g., a class
5760 template with a parameter list such as
5761 template<typename..., typename>. */
5762 return error_mark_node;
5763 else
5764 arg = convert_template_argument (TREE_VALUE (parm),
5765 arg, new_args, complain,
5766 parm_idx, in_decl);
5767
5768 if (arg == error_mark_node)
5769 lost++;
5770 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
5771 }
5772 cp_unevaluated_operand = saved_unevaluated_operand;
5773 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
5774
5775 if (lost)
5776 return error_mark_node;
5777
5778 return new_inner_args;
5779 }
5780
5781 /* Returns 1 if template args OT and NT are equivalent. */
5782
5783 static int
5784 template_args_equal (tree ot, tree nt)
5785 {
5786 if (nt == ot)
5787 return 1;
5788
5789 if (TREE_CODE (nt) == TREE_VEC)
5790 /* For member templates */
5791 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
5792 else if (PACK_EXPANSION_P (ot))
5793 return PACK_EXPANSION_P (nt)
5794 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
5795 PACK_EXPANSION_PATTERN (nt));
5796 else if (ARGUMENT_PACK_P (ot))
5797 {
5798 int i, len;
5799 tree opack, npack;
5800
5801 if (!ARGUMENT_PACK_P (nt))
5802 return 0;
5803
5804 opack = ARGUMENT_PACK_ARGS (ot);
5805 npack = ARGUMENT_PACK_ARGS (nt);
5806 len = TREE_VEC_LENGTH (opack);
5807 if (TREE_VEC_LENGTH (npack) != len)
5808 return 0;
5809 for (i = 0; i < len; ++i)
5810 if (!template_args_equal (TREE_VEC_ELT (opack, i),
5811 TREE_VEC_ELT (npack, i)))
5812 return 0;
5813 return 1;
5814 }
5815 else if (TYPE_P (nt))
5816 return TYPE_P (ot) && same_type_p (ot, nt);
5817 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
5818 return 0;
5819 else
5820 return cp_tree_equal (ot, nt);
5821 }
5822
5823 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5824 of template arguments. Returns 0 otherwise. */
5825
5826 int
5827 comp_template_args (tree oldargs, tree newargs)
5828 {
5829 int i;
5830
5831 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5832 return 0;
5833
5834 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5835 {
5836 tree nt = TREE_VEC_ELT (newargs, i);
5837 tree ot = TREE_VEC_ELT (oldargs, i);
5838
5839 if (! template_args_equal (ot, nt))
5840 return 0;
5841 }
5842 return 1;
5843 }
5844
5845 static void
5846 add_pending_template (tree d)
5847 {
5848 tree ti = (TYPE_P (d)
5849 ? CLASSTYPE_TEMPLATE_INFO (d)
5850 : DECL_TEMPLATE_INFO (d));
5851 struct pending_template *pt;
5852 int level;
5853
5854 if (TI_PENDING_TEMPLATE_FLAG (ti))
5855 return;
5856
5857 /* We are called both from instantiate_decl, where we've already had a
5858 tinst_level pushed, and instantiate_template, where we haven't.
5859 Compensate. */
5860 level = !current_tinst_level || current_tinst_level->decl != d;
5861
5862 if (level)
5863 push_tinst_level (d);
5864
5865 pt = GGC_NEW (struct pending_template);
5866 pt->next = NULL;
5867 pt->tinst = current_tinst_level;
5868 if (last_pending_template)
5869 last_pending_template->next = pt;
5870 else
5871 pending_templates = pt;
5872
5873 last_pending_template = pt;
5874
5875 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5876
5877 if (level)
5878 pop_tinst_level ();
5879 }
5880
5881
5882 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5883 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5884 documentation for TEMPLATE_ID_EXPR. */
5885
5886 tree
5887 lookup_template_function (tree fns, tree arglist)
5888 {
5889 tree type;
5890
5891 if (fns == error_mark_node || arglist == error_mark_node)
5892 return error_mark_node;
5893
5894 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
5895 gcc_assert (fns && (is_overloaded_fn (fns)
5896 || TREE_CODE (fns) == IDENTIFIER_NODE));
5897
5898 if (BASELINK_P (fns))
5899 {
5900 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
5901 unknown_type_node,
5902 BASELINK_FUNCTIONS (fns),
5903 arglist);
5904 return fns;
5905 }
5906
5907 type = TREE_TYPE (fns);
5908 if (TREE_CODE (fns) == OVERLOAD || !type)
5909 type = unknown_type_node;
5910
5911 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
5912 }
5913
5914 /* Within the scope of a template class S<T>, the name S gets bound
5915 (in build_self_reference) to a TYPE_DECL for the class, not a
5916 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5917 or one of its enclosing classes, and that type is a template,
5918 return the associated TEMPLATE_DECL. Otherwise, the original
5919 DECL is returned. */
5920
5921 tree
5922 maybe_get_template_decl_from_type_decl (tree decl)
5923 {
5924 return (decl != NULL_TREE
5925 && TREE_CODE (decl) == TYPE_DECL
5926 && DECL_ARTIFICIAL (decl)
5927 && CLASS_TYPE_P (TREE_TYPE (decl))
5928 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
5929 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
5930 }
5931
5932 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
5933 parameters, find the desired type.
5934
5935 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
5936
5937 IN_DECL, if non-NULL, is the template declaration we are trying to
5938 instantiate.
5939
5940 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
5941 the class we are looking up.
5942
5943 Issue error and warning messages under control of COMPLAIN.
5944
5945 If the template class is really a local class in a template
5946 function, then the FUNCTION_CONTEXT is the function in which it is
5947 being instantiated.
5948
5949 ??? Note that this function is currently called *twice* for each
5950 template-id: the first time from the parser, while creating the
5951 incomplete type (finish_template_type), and the second type during the
5952 real instantiation (instantiate_template_class). This is surely something
5953 that we want to avoid. It also causes some problems with argument
5954 coercion (see convert_nontype_argument for more information on this). */
5955
5956 tree
5957 lookup_template_class (tree d1,
5958 tree arglist,
5959 tree in_decl,
5960 tree context,
5961 int entering_scope,
5962 tsubst_flags_t complain)
5963 {
5964 tree templ = NULL_TREE, parmlist;
5965 tree t;
5966 spec_entry **slot;
5967 spec_entry *entry;
5968 spec_entry elt;
5969 hashval_t hash;
5970
5971 timevar_push (TV_NAME_LOOKUP);
5972
5973 if (TREE_CODE (d1) == IDENTIFIER_NODE)
5974 {
5975 tree value = innermost_non_namespace_value (d1);
5976 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
5977 templ = value;
5978 else
5979 {
5980 if (context)
5981 push_decl_namespace (context);
5982 templ = lookup_name (d1);
5983 templ = maybe_get_template_decl_from_type_decl (templ);
5984 if (context)
5985 pop_decl_namespace ();
5986 }
5987 if (templ)
5988 context = DECL_CONTEXT (templ);
5989 }
5990 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
5991 {
5992 tree type = TREE_TYPE (d1);
5993
5994 /* If we are declaring a constructor, say A<T>::A<T>, we will get
5995 an implicit typename for the second A. Deal with it. */
5996 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5997 type = TREE_TYPE (type);
5998
5999 if (CLASSTYPE_TEMPLATE_INFO (type))
6000 {
6001 templ = CLASSTYPE_TI_TEMPLATE (type);
6002 d1 = DECL_NAME (templ);
6003 }
6004 }
6005 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6006 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6007 {
6008 templ = TYPE_TI_TEMPLATE (d1);
6009 d1 = DECL_NAME (templ);
6010 }
6011 else if (TREE_CODE (d1) == TEMPLATE_DECL
6012 && DECL_TEMPLATE_RESULT (d1)
6013 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6014 {
6015 templ = d1;
6016 d1 = DECL_NAME (templ);
6017 context = DECL_CONTEXT (templ);
6018 }
6019
6020 /* Issue an error message if we didn't find a template. */
6021 if (! templ)
6022 {
6023 if (complain & tf_error)
6024 error ("%qT is not a template", d1);
6025 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6026 }
6027
6028 if (TREE_CODE (templ) != TEMPLATE_DECL
6029 /* Make sure it's a user visible template, if it was named by
6030 the user. */
6031 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6032 && !PRIMARY_TEMPLATE_P (templ)))
6033 {
6034 if (complain & tf_error)
6035 {
6036 error ("non-template type %qT used as a template", d1);
6037 if (in_decl)
6038 error ("for template declaration %q+D", in_decl);
6039 }
6040 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6041 }
6042
6043 complain &= ~tf_user;
6044
6045 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6046 {
6047 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6048 template arguments */
6049
6050 tree parm;
6051 tree arglist2;
6052 tree outer;
6053
6054 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6055
6056 /* Consider an example where a template template parameter declared as
6057
6058 template <class T, class U = std::allocator<T> > class TT
6059
6060 The template parameter level of T and U are one level larger than
6061 of TT. To proper process the default argument of U, say when an
6062 instantiation `TT<int>' is seen, we need to build the full
6063 arguments containing {int} as the innermost level. Outer levels,
6064 available when not appearing as default template argument, can be
6065 obtained from the arguments of the enclosing template.
6066
6067 Suppose that TT is later substituted with std::vector. The above
6068 instantiation is `TT<int, std::allocator<T> >' with TT at
6069 level 1, and T at level 2, while the template arguments at level 1
6070 becomes {std::vector} and the inner level 2 is {int}. */
6071
6072 outer = DECL_CONTEXT (templ);
6073 if (outer)
6074 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6075 else if (current_template_parms)
6076 /* This is an argument of the current template, so we haven't set
6077 DECL_CONTEXT yet. */
6078 outer = current_template_args ();
6079
6080 if (outer)
6081 arglist = add_to_template_args (outer, arglist);
6082
6083 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6084 complain,
6085 /*require_all_args=*/true,
6086 /*use_default_args=*/true);
6087 if (arglist2 == error_mark_node
6088 || (!uses_template_parms (arglist2)
6089 && check_instantiated_args (templ, arglist2, complain)))
6090 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6091
6092 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6093 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
6094 }
6095 else
6096 {
6097 tree template_type = TREE_TYPE (templ);
6098 tree gen_tmpl;
6099 tree type_decl;
6100 tree found = NULL_TREE;
6101 int arg_depth;
6102 int parm_depth;
6103 int is_partial_instantiation;
6104
6105 gen_tmpl = most_general_template (templ);
6106 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6107 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6108 arg_depth = TMPL_ARGS_DEPTH (arglist);
6109
6110 if (arg_depth == 1 && parm_depth > 1)
6111 {
6112 /* We've been given an incomplete set of template arguments.
6113 For example, given:
6114
6115 template <class T> struct S1 {
6116 template <class U> struct S2 {};
6117 template <class U> struct S2<U*> {};
6118 };
6119
6120 we will be called with an ARGLIST of `U*', but the
6121 TEMPLATE will be `template <class T> template
6122 <class U> struct S1<T>::S2'. We must fill in the missing
6123 arguments. */
6124 arglist
6125 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6126 arglist);
6127 arg_depth = TMPL_ARGS_DEPTH (arglist);
6128 }
6129
6130 /* Now we should have enough arguments. */
6131 gcc_assert (parm_depth == arg_depth);
6132
6133 /* From here on, we're only interested in the most general
6134 template. */
6135
6136 /* Calculate the BOUND_ARGS. These will be the args that are
6137 actually tsubst'd into the definition to create the
6138 instantiation. */
6139 if (parm_depth > 1)
6140 {
6141 /* We have multiple levels of arguments to coerce, at once. */
6142 int i;
6143 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6144
6145 tree bound_args = make_tree_vec (parm_depth);
6146
6147 for (i = saved_depth,
6148 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6149 i > 0 && t != NULL_TREE;
6150 --i, t = TREE_CHAIN (t))
6151 {
6152 tree a = coerce_template_parms (TREE_VALUE (t),
6153 arglist, gen_tmpl,
6154 complain,
6155 /*require_all_args=*/true,
6156 /*use_default_args=*/true);
6157
6158 /* Don't process further if one of the levels fails. */
6159 if (a == error_mark_node)
6160 {
6161 /* Restore the ARGLIST to its full size. */
6162 TREE_VEC_LENGTH (arglist) = saved_depth;
6163 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6164 }
6165
6166 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6167
6168 /* We temporarily reduce the length of the ARGLIST so
6169 that coerce_template_parms will see only the arguments
6170 corresponding to the template parameters it is
6171 examining. */
6172 TREE_VEC_LENGTH (arglist)--;
6173 }
6174
6175 /* Restore the ARGLIST to its full size. */
6176 TREE_VEC_LENGTH (arglist) = saved_depth;
6177
6178 arglist = bound_args;
6179 }
6180 else
6181 arglist
6182 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6183 INNERMOST_TEMPLATE_ARGS (arglist),
6184 gen_tmpl,
6185 complain,
6186 /*require_all_args=*/true,
6187 /*use_default_args=*/true);
6188
6189 if (arglist == error_mark_node)
6190 /* We were unable to bind the arguments. */
6191 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6192
6193 /* In the scope of a template class, explicit references to the
6194 template class refer to the type of the template, not any
6195 instantiation of it. For example, in:
6196
6197 template <class T> class C { void f(C<T>); }
6198
6199 the `C<T>' is just the same as `C'. Outside of the
6200 class, however, such a reference is an instantiation. */
6201 if ((entering_scope
6202 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6203 || currently_open_class (template_type))
6204 /* comp_template_args is expensive, check it last. */
6205 && comp_template_args (TYPE_TI_ARGS (template_type),
6206 arglist))
6207 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
6208
6209 /* If we already have this specialization, return it. */
6210 elt.tmpl = gen_tmpl;
6211 elt.args = arglist;
6212 hash = hash_specialization (&elt);
6213 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6214 &elt, hash);
6215
6216 if (entry)
6217 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
6218
6219 /* This type is a "partial instantiation" if any of the template
6220 arguments still involve template parameters. Note that we set
6221 IS_PARTIAL_INSTANTIATION for partial specializations as
6222 well. */
6223 is_partial_instantiation = uses_template_parms (arglist);
6224
6225 /* If the deduced arguments are invalid, then the binding
6226 failed. */
6227 if (!is_partial_instantiation
6228 && check_instantiated_args (gen_tmpl,
6229 INNERMOST_TEMPLATE_ARGS (arglist),
6230 complain))
6231 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6232
6233 if (!is_partial_instantiation
6234 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6235 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6236 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6237 {
6238 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6239 DECL_NAME (gen_tmpl),
6240 /*tag_scope=*/ts_global);
6241 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
6242 }
6243
6244 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
6245 complain, in_decl);
6246 if (!context)
6247 context = global_namespace;
6248
6249 /* Create the type. */
6250 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6251 {
6252 if (!is_partial_instantiation)
6253 {
6254 set_current_access_from_decl (TYPE_NAME (template_type));
6255 t = start_enum (TYPE_IDENTIFIER (template_type),
6256 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6257 arglist, complain, in_decl),
6258 SCOPED_ENUM_P (template_type));
6259 }
6260 else
6261 {
6262 /* We don't want to call start_enum for this type, since
6263 the values for the enumeration constants may involve
6264 template parameters. And, no one should be interested
6265 in the enumeration constants for such a type. */
6266 t = cxx_make_type (ENUMERAL_TYPE);
6267 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
6268 }
6269 }
6270 else
6271 {
6272 t = make_class_type (TREE_CODE (template_type));
6273 CLASSTYPE_DECLARED_CLASS (t)
6274 = CLASSTYPE_DECLARED_CLASS (template_type);
6275 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
6276 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
6277
6278 /* A local class. Make sure the decl gets registered properly. */
6279 if (context == current_function_decl)
6280 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
6281
6282 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
6283 /* This instantiation is another name for the primary
6284 template type. Set the TYPE_CANONICAL field
6285 appropriately. */
6286 TYPE_CANONICAL (t) = template_type;
6287 else if (any_template_arguments_need_structural_equality_p (arglist))
6288 /* Some of the template arguments require structural
6289 equality testing, so this template class requires
6290 structural equality testing. */
6291 SET_TYPE_STRUCTURAL_EQUALITY (t);
6292 }
6293
6294 /* If we called start_enum or pushtag above, this information
6295 will already be set up. */
6296 if (!TYPE_NAME (t))
6297 {
6298 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
6299
6300 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
6301 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
6302 TYPE_STUB_DECL (t) = type_decl;
6303 DECL_SOURCE_LOCATION (type_decl)
6304 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
6305 }
6306 else
6307 type_decl = TYPE_NAME (t);
6308
6309 TREE_PRIVATE (type_decl)
6310 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
6311 TREE_PROTECTED (type_decl)
6312 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
6313 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
6314 {
6315 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
6316 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
6317 }
6318
6319 /* Set up the template information. We have to figure out which
6320 template is the immediate parent if this is a full
6321 instantiation. */
6322 if (parm_depth == 1 || is_partial_instantiation
6323 || !PRIMARY_TEMPLATE_P (gen_tmpl))
6324 /* This case is easy; there are no member templates involved. */
6325 found = gen_tmpl;
6326 else
6327 {
6328 /* This is a full instantiation of a member template. Find
6329 the partial instantiation of which this is an instance. */
6330
6331 /* Temporarily reduce by one the number of levels in the ARGLIST
6332 so as to avoid comparing the last set of arguments. */
6333 TREE_VEC_LENGTH (arglist)--;
6334 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
6335 TREE_VEC_LENGTH (arglist)++;
6336 found = CLASSTYPE_TI_TEMPLATE (found);
6337 }
6338
6339 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
6340
6341 elt.spec = t;
6342 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
6343 &elt, hash, INSERT);
6344 *slot = GGC_NEW (spec_entry);
6345 **slot = elt;
6346
6347 /* Note this use of the partial instantiation so we can check it
6348 later in maybe_process_partial_specialization. */
6349 DECL_TEMPLATE_INSTANTIATIONS (templ)
6350 = tree_cons (arglist, t,
6351 DECL_TEMPLATE_INSTANTIATIONS (templ));
6352
6353 if (TREE_CODE (t) == ENUMERAL_TYPE
6354 && !is_partial_instantiation)
6355 /* Now that the type has been registered on the instantiations
6356 list, we set up the enumerators. Because the enumeration
6357 constants may involve the enumeration type itself, we make
6358 sure to register the type first, and then create the
6359 constants. That way, doing tsubst_expr for the enumeration
6360 constants won't result in recursive calls here; we'll find
6361 the instantiation and exit above. */
6362 tsubst_enum (template_type, t, arglist);
6363
6364 if (is_partial_instantiation)
6365 /* If the type makes use of template parameters, the
6366 code that generates debugging information will crash. */
6367 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6368
6369 /* Possibly limit visibility based on template args. */
6370 TREE_PUBLIC (type_decl) = 1;
6371 determine_visibility (type_decl);
6372
6373 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6374 }
6375 timevar_pop (TV_NAME_LOOKUP);
6376 }
6377 \f
6378 struct pair_fn_data
6379 {
6380 tree_fn_t fn;
6381 void *data;
6382 /* True when we should also visit template parameters that occur in
6383 non-deduced contexts. */
6384 bool include_nondeduced_p;
6385 struct pointer_set_t *visited;
6386 };
6387
6388 /* Called from for_each_template_parm via walk_tree. */
6389
6390 static tree
6391 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6392 {
6393 tree t = *tp;
6394 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6395 tree_fn_t fn = pfd->fn;
6396 void *data = pfd->data;
6397
6398 if (TYPE_P (t)
6399 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6400 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6401 pfd->include_nondeduced_p))
6402 return error_mark_node;
6403
6404 switch (TREE_CODE (t))
6405 {
6406 case RECORD_TYPE:
6407 if (TYPE_PTRMEMFUNC_P (t))
6408 break;
6409 /* Fall through. */
6410
6411 case UNION_TYPE:
6412 case ENUMERAL_TYPE:
6413 if (!TYPE_TEMPLATE_INFO (t))
6414 *walk_subtrees = 0;
6415 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
6416 fn, data, pfd->visited,
6417 pfd->include_nondeduced_p))
6418 return error_mark_node;
6419 break;
6420
6421 case INTEGER_TYPE:
6422 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6423 fn, data, pfd->visited,
6424 pfd->include_nondeduced_p)
6425 || for_each_template_parm (TYPE_MAX_VALUE (t),
6426 fn, data, pfd->visited,
6427 pfd->include_nondeduced_p))
6428 return error_mark_node;
6429 break;
6430
6431 case METHOD_TYPE:
6432 /* Since we're not going to walk subtrees, we have to do this
6433 explicitly here. */
6434 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6435 pfd->visited, pfd->include_nondeduced_p))
6436 return error_mark_node;
6437 /* Fall through. */
6438
6439 case FUNCTION_TYPE:
6440 /* Check the return type. */
6441 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6442 pfd->include_nondeduced_p))
6443 return error_mark_node;
6444
6445 /* Check the parameter types. Since default arguments are not
6446 instantiated until they are needed, the TYPE_ARG_TYPES may
6447 contain expressions that involve template parameters. But,
6448 no-one should be looking at them yet. And, once they're
6449 instantiated, they don't contain template parameters, so
6450 there's no point in looking at them then, either. */
6451 {
6452 tree parm;
6453
6454 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6455 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6456 pfd->visited, pfd->include_nondeduced_p))
6457 return error_mark_node;
6458
6459 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6460 want walk_tree walking into them itself. */
6461 *walk_subtrees = 0;
6462 }
6463 break;
6464
6465 case TYPEOF_TYPE:
6466 if (pfd->include_nondeduced_p
6467 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6468 pfd->visited,
6469 pfd->include_nondeduced_p))
6470 return error_mark_node;
6471 break;
6472
6473 case FUNCTION_DECL:
6474 case VAR_DECL:
6475 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6476 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6477 pfd->visited, pfd->include_nondeduced_p))
6478 return error_mark_node;
6479 /* Fall through. */
6480
6481 case PARM_DECL:
6482 case CONST_DECL:
6483 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6484 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6485 pfd->visited, pfd->include_nondeduced_p))
6486 return error_mark_node;
6487 if (DECL_CONTEXT (t)
6488 && pfd->include_nondeduced_p
6489 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6490 pfd->visited, pfd->include_nondeduced_p))
6491 return error_mark_node;
6492 break;
6493
6494 case BOUND_TEMPLATE_TEMPLATE_PARM:
6495 /* Record template parameters such as `T' inside `TT<T>'. */
6496 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6497 pfd->include_nondeduced_p))
6498 return error_mark_node;
6499 /* Fall through. */
6500
6501 case TEMPLATE_TEMPLATE_PARM:
6502 case TEMPLATE_TYPE_PARM:
6503 case TEMPLATE_PARM_INDEX:
6504 if (fn && (*fn)(t, data))
6505 return error_mark_node;
6506 else if (!fn)
6507 return error_mark_node;
6508 break;
6509
6510 case TEMPLATE_DECL:
6511 /* A template template parameter is encountered. */
6512 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6513 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6514 pfd->include_nondeduced_p))
6515 return error_mark_node;
6516
6517 /* Already substituted template template parameter */
6518 *walk_subtrees = 0;
6519 break;
6520
6521 case TYPENAME_TYPE:
6522 if (!fn
6523 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6524 data, pfd->visited,
6525 pfd->include_nondeduced_p))
6526 return error_mark_node;
6527 break;
6528
6529 case CONSTRUCTOR:
6530 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
6531 && pfd->include_nondeduced_p
6532 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6533 (TREE_TYPE (t)), fn, data,
6534 pfd->visited, pfd->include_nondeduced_p))
6535 return error_mark_node;
6536 break;
6537
6538 case INDIRECT_REF:
6539 case COMPONENT_REF:
6540 /* If there's no type, then this thing must be some expression
6541 involving template parameters. */
6542 if (!fn && !TREE_TYPE (t))
6543 return error_mark_node;
6544 break;
6545
6546 case MODOP_EXPR:
6547 case CAST_EXPR:
6548 case REINTERPRET_CAST_EXPR:
6549 case CONST_CAST_EXPR:
6550 case STATIC_CAST_EXPR:
6551 case DYNAMIC_CAST_EXPR:
6552 case ARROW_EXPR:
6553 case DOTSTAR_EXPR:
6554 case TYPEID_EXPR:
6555 case PSEUDO_DTOR_EXPR:
6556 if (!fn)
6557 return error_mark_node;
6558 break;
6559
6560 default:
6561 break;
6562 }
6563
6564 /* We didn't find any template parameters we liked. */
6565 return NULL_TREE;
6566 }
6567
6568 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6569 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6570 call FN with the parameter and the DATA.
6571 If FN returns nonzero, the iteration is terminated, and
6572 for_each_template_parm returns 1. Otherwise, the iteration
6573 continues. If FN never returns a nonzero value, the value
6574 returned by for_each_template_parm is 0. If FN is NULL, it is
6575 considered to be the function which always returns 1.
6576
6577 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6578 parameters that occur in non-deduced contexts. When false, only
6579 visits those template parameters that can be deduced. */
6580
6581 static int
6582 for_each_template_parm (tree t, tree_fn_t fn, void* data,
6583 struct pointer_set_t *visited,
6584 bool include_nondeduced_p)
6585 {
6586 struct pair_fn_data pfd;
6587 int result;
6588
6589 /* Set up. */
6590 pfd.fn = fn;
6591 pfd.data = data;
6592 pfd.include_nondeduced_p = include_nondeduced_p;
6593
6594 /* Walk the tree. (Conceptually, we would like to walk without
6595 duplicates, but for_each_template_parm_r recursively calls
6596 for_each_template_parm, so we would need to reorganize a fair
6597 bit to use walk_tree_without_duplicates, so we keep our own
6598 visited list.) */
6599 if (visited)
6600 pfd.visited = visited;
6601 else
6602 pfd.visited = pointer_set_create ();
6603 result = cp_walk_tree (&t,
6604 for_each_template_parm_r,
6605 &pfd,
6606 pfd.visited) != NULL_TREE;
6607
6608 /* Clean up. */
6609 if (!visited)
6610 {
6611 pointer_set_destroy (pfd.visited);
6612 pfd.visited = 0;
6613 }
6614
6615 return result;
6616 }
6617
6618 /* Returns true if T depends on any template parameter. */
6619
6620 int
6621 uses_template_parms (tree t)
6622 {
6623 bool dependent_p;
6624 int saved_processing_template_decl;
6625
6626 saved_processing_template_decl = processing_template_decl;
6627 if (!saved_processing_template_decl)
6628 processing_template_decl = 1;
6629 if (TYPE_P (t))
6630 dependent_p = dependent_type_p (t);
6631 else if (TREE_CODE (t) == TREE_VEC)
6632 dependent_p = any_dependent_template_arguments_p (t);
6633 else if (TREE_CODE (t) == TREE_LIST)
6634 dependent_p = (uses_template_parms (TREE_VALUE (t))
6635 || uses_template_parms (TREE_CHAIN (t)));
6636 else if (TREE_CODE (t) == TYPE_DECL)
6637 dependent_p = dependent_type_p (TREE_TYPE (t));
6638 else if (DECL_P (t)
6639 || EXPR_P (t)
6640 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
6641 || TREE_CODE (t) == OVERLOAD
6642 || TREE_CODE (t) == BASELINK
6643 || TREE_CODE (t) == IDENTIFIER_NODE
6644 || TREE_CODE (t) == TRAIT_EXPR
6645 || TREE_CODE (t) == CONSTRUCTOR
6646 || CONSTANT_CLASS_P (t))
6647 dependent_p = (type_dependent_expression_p (t)
6648 || value_dependent_expression_p (t));
6649 else
6650 {
6651 gcc_assert (t == error_mark_node);
6652 dependent_p = false;
6653 }
6654
6655 processing_template_decl = saved_processing_template_decl;
6656
6657 return dependent_p;
6658 }
6659
6660 /* Returns true if T depends on any template parameter with level LEVEL. */
6661
6662 int
6663 uses_template_parms_level (tree t, int level)
6664 {
6665 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
6666 /*include_nondeduced_p=*/true);
6667 }
6668
6669 static int tinst_depth;
6670 extern int max_tinst_depth;
6671 #ifdef GATHER_STATISTICS
6672 int depth_reached;
6673 #endif
6674 static int tinst_level_tick;
6675 static int last_template_error_tick;
6676
6677 /* We're starting to instantiate D; record the template instantiation context
6678 for diagnostics and to restore it later. */
6679
6680 static int
6681 push_tinst_level (tree d)
6682 {
6683 struct tinst_level *new_level;
6684
6685 if (tinst_depth >= max_tinst_depth)
6686 {
6687 /* If the instantiation in question still has unbound template parms,
6688 we don't really care if we can't instantiate it, so just return.
6689 This happens with base instantiation for implicit `typename'. */
6690 if (uses_template_parms (d))
6691 return 0;
6692
6693 last_template_error_tick = tinst_level_tick;
6694 error ("template instantiation depth exceeds maximum of %d (use "
6695 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6696 max_tinst_depth, d);
6697
6698 print_instantiation_context ();
6699
6700 return 0;
6701 }
6702
6703 new_level = GGC_NEW (struct tinst_level);
6704 new_level->decl = d;
6705 new_level->locus = input_location;
6706 new_level->in_system_header_p = in_system_header;
6707 new_level->next = current_tinst_level;
6708 current_tinst_level = new_level;
6709
6710 ++tinst_depth;
6711 #ifdef GATHER_STATISTICS
6712 if (tinst_depth > depth_reached)
6713 depth_reached = tinst_depth;
6714 #endif
6715
6716 ++tinst_level_tick;
6717 return 1;
6718 }
6719
6720 /* We're done instantiating this template; return to the instantiation
6721 context. */
6722
6723 static void
6724 pop_tinst_level (void)
6725 {
6726 /* Restore the filename and line number stashed away when we started
6727 this instantiation. */
6728 input_location = current_tinst_level->locus;
6729 current_tinst_level = current_tinst_level->next;
6730 --tinst_depth;
6731 ++tinst_level_tick;
6732 }
6733
6734 /* We're instantiating a deferred template; restore the template
6735 instantiation context in which the instantiation was requested, which
6736 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6737
6738 static tree
6739 reopen_tinst_level (struct tinst_level *level)
6740 {
6741 struct tinst_level *t;
6742
6743 tinst_depth = 0;
6744 for (t = level; t; t = t->next)
6745 ++tinst_depth;
6746
6747 current_tinst_level = level;
6748 pop_tinst_level ();
6749 return level->decl;
6750 }
6751
6752 /* Returns the TINST_LEVEL which gives the original instantiation
6753 context. */
6754
6755 struct tinst_level *
6756 outermost_tinst_level (void)
6757 {
6758 struct tinst_level *level = current_tinst_level;
6759 if (level)
6760 while (level->next)
6761 level = level->next;
6762 return level;
6763 }
6764
6765 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
6766
6767 bool
6768 parameter_of_template_p (tree parm, tree templ)
6769 {
6770 tree parms;
6771 int i;
6772
6773 if (!parm || !templ)
6774 return false;
6775
6776 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
6777 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
6778
6779 parms = DECL_TEMPLATE_PARMS (templ);
6780 parms = INNERMOST_TEMPLATE_PARMS (parms);
6781
6782 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
6783 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
6784 return true;
6785
6786 return false;
6787 }
6788
6789 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6790 vector of template arguments, as for tsubst.
6791
6792 Returns an appropriate tsubst'd friend declaration. */
6793
6794 static tree
6795 tsubst_friend_function (tree decl, tree args)
6796 {
6797 tree new_friend;
6798
6799 if (TREE_CODE (decl) == FUNCTION_DECL
6800 && DECL_TEMPLATE_INSTANTIATION (decl)
6801 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
6802 /* This was a friend declared with an explicit template
6803 argument list, e.g.:
6804
6805 friend void f<>(T);
6806
6807 to indicate that f was a template instantiation, not a new
6808 function declaration. Now, we have to figure out what
6809 instantiation of what template. */
6810 {
6811 tree template_id, arglist, fns;
6812 tree new_args;
6813 tree tmpl;
6814 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
6815
6816 /* Friend functions are looked up in the containing namespace scope.
6817 We must enter that scope, to avoid finding member functions of the
6818 current class with same name. */
6819 push_nested_namespace (ns);
6820 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
6821 tf_warning_or_error, NULL_TREE,
6822 /*integral_constant_expression_p=*/false);
6823 pop_nested_namespace (ns);
6824 arglist = tsubst (DECL_TI_ARGS (decl), args,
6825 tf_warning_or_error, NULL_TREE);
6826 template_id = lookup_template_function (fns, arglist);
6827
6828 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6829 tmpl = determine_specialization (template_id, new_friend,
6830 &new_args,
6831 /*need_member_template=*/0,
6832 TREE_VEC_LENGTH (args),
6833 tsk_none);
6834 return instantiate_template (tmpl, new_args, tf_error);
6835 }
6836
6837 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6838
6839 /* The NEW_FRIEND will look like an instantiation, to the
6840 compiler, but is not an instantiation from the point of view of
6841 the language. For example, we might have had:
6842
6843 template <class T> struct S {
6844 template <class U> friend void f(T, U);
6845 };
6846
6847 Then, in S<int>, template <class U> void f(int, U) is not an
6848 instantiation of anything. */
6849 if (new_friend == error_mark_node)
6850 return error_mark_node;
6851
6852 DECL_USE_TEMPLATE (new_friend) = 0;
6853 if (TREE_CODE (decl) == TEMPLATE_DECL)
6854 {
6855 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
6856 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
6857 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
6858 }
6859
6860 /* The mangled name for the NEW_FRIEND is incorrect. The function
6861 is not a template instantiation and should not be mangled like
6862 one. Therefore, we forget the mangling here; we'll recompute it
6863 later if we need it. */
6864 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
6865 {
6866 SET_DECL_RTL (new_friend, NULL_RTX);
6867 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
6868 }
6869
6870 if (DECL_NAMESPACE_SCOPE_P (new_friend))
6871 {
6872 tree old_decl;
6873 tree new_friend_template_info;
6874 tree new_friend_result_template_info;
6875 tree ns;
6876 int new_friend_is_defn;
6877
6878 /* We must save some information from NEW_FRIEND before calling
6879 duplicate decls since that function will free NEW_FRIEND if
6880 possible. */
6881 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
6882 new_friend_is_defn =
6883 (DECL_INITIAL (DECL_TEMPLATE_RESULT
6884 (template_for_substitution (new_friend)))
6885 != NULL_TREE);
6886 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
6887 {
6888 /* This declaration is a `primary' template. */
6889 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
6890
6891 new_friend_result_template_info
6892 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
6893 }
6894 else
6895 new_friend_result_template_info = NULL_TREE;
6896
6897 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6898 if (new_friend_is_defn)
6899 DECL_INITIAL (new_friend) = error_mark_node;
6900
6901 /* Inside pushdecl_namespace_level, we will push into the
6902 current namespace. However, the friend function should go
6903 into the namespace of the template. */
6904 ns = decl_namespace_context (new_friend);
6905 push_nested_namespace (ns);
6906 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
6907 pop_nested_namespace (ns);
6908
6909 if (old_decl == error_mark_node)
6910 return error_mark_node;
6911
6912 if (old_decl != new_friend)
6913 {
6914 /* This new friend declaration matched an existing
6915 declaration. For example, given:
6916
6917 template <class T> void f(T);
6918 template <class U> class C {
6919 template <class T> friend void f(T) {}
6920 };
6921
6922 the friend declaration actually provides the definition
6923 of `f', once C has been instantiated for some type. So,
6924 old_decl will be the out-of-class template declaration,
6925 while new_friend is the in-class definition.
6926
6927 But, if `f' was called before this point, the
6928 instantiation of `f' will have DECL_TI_ARGS corresponding
6929 to `T' but not to `U', references to which might appear
6930 in the definition of `f'. Previously, the most general
6931 template for an instantiation of `f' was the out-of-class
6932 version; now it is the in-class version. Therefore, we
6933 run through all specialization of `f', adding to their
6934 DECL_TI_ARGS appropriately. In particular, they need a
6935 new set of outer arguments, corresponding to the
6936 arguments for this class instantiation.
6937
6938 The same situation can arise with something like this:
6939
6940 friend void f(int);
6941 template <class T> class C {
6942 friend void f(T) {}
6943 };
6944
6945 when `C<int>' is instantiated. Now, `f(int)' is defined
6946 in the class. */
6947
6948 if (!new_friend_is_defn)
6949 /* On the other hand, if the in-class declaration does
6950 *not* provide a definition, then we don't want to alter
6951 existing definitions. We can just leave everything
6952 alone. */
6953 ;
6954 else
6955 {
6956 tree new_template = TI_TEMPLATE (new_friend_template_info);
6957 tree new_args = TI_ARGS (new_friend_template_info);
6958
6959 /* Overwrite whatever template info was there before, if
6960 any, with the new template information pertaining to
6961 the declaration. */
6962 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
6963
6964 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
6965 /* We should have called reregister_specialization in
6966 duplicate_decls. */
6967 gcc_assert (retrieve_specialization (new_template,
6968 new_args, 0)
6969 == old_decl);
6970 else
6971 {
6972 tree t;
6973
6974 /* Indicate that the old function template is a partial
6975 instantiation. */
6976 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
6977 = new_friend_result_template_info;
6978
6979 gcc_assert (new_template
6980 == most_general_template (new_template));
6981 gcc_assert (new_template != old_decl);
6982
6983 /* Reassign any specializations already in the hash table
6984 to the new more general template, and add the
6985 additional template args. */
6986 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
6987 t != NULL_TREE;
6988 t = TREE_CHAIN (t))
6989 {
6990 tree spec = TREE_VALUE (t);
6991 spec_entry elt;
6992
6993 elt.tmpl = old_decl;
6994 elt.args = DECL_TI_ARGS (spec);
6995 elt.spec = NULL_TREE;
6996
6997 htab_remove_elt (decl_specializations, &elt);
6998
6999 DECL_TI_ARGS (spec)
7000 = add_outermost_template_args (new_args,
7001 DECL_TI_ARGS (spec));
7002
7003 register_specialization
7004 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7005
7006 }
7007 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7008 }
7009 }
7010
7011 /* The information from NEW_FRIEND has been merged into OLD_DECL
7012 by duplicate_decls. */
7013 new_friend = old_decl;
7014 }
7015 }
7016 else
7017 {
7018 tree context = DECL_CONTEXT (new_friend);
7019 bool dependent_p;
7020
7021 /* In the code
7022 template <class T> class C {
7023 template <class U> friend void C1<U>::f (); // case 1
7024 friend void C2<T>::f (); // case 2
7025 };
7026 we only need to make sure CONTEXT is a complete type for
7027 case 2. To distinguish between the two cases, we note that
7028 CONTEXT of case 1 remains dependent type after tsubst while
7029 this isn't true for case 2. */
7030 ++processing_template_decl;
7031 dependent_p = dependent_type_p (context);
7032 --processing_template_decl;
7033
7034 if (!dependent_p
7035 && !complete_type_or_else (context, NULL_TREE))
7036 return error_mark_node;
7037
7038 if (COMPLETE_TYPE_P (context))
7039 {
7040 /* Check to see that the declaration is really present, and,
7041 possibly obtain an improved declaration. */
7042 tree fn = check_classfn (context,
7043 new_friend, NULL_TREE);
7044
7045 if (fn)
7046 new_friend = fn;
7047 }
7048 }
7049
7050 return new_friend;
7051 }
7052
7053 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7054 template arguments, as for tsubst.
7055
7056 Returns an appropriate tsubst'd friend type or error_mark_node on
7057 failure. */
7058
7059 static tree
7060 tsubst_friend_class (tree friend_tmpl, tree args)
7061 {
7062 tree friend_type;
7063 tree tmpl;
7064 tree context;
7065
7066 context = DECL_CONTEXT (friend_tmpl);
7067
7068 if (context)
7069 {
7070 if (TREE_CODE (context) == NAMESPACE_DECL)
7071 push_nested_namespace (context);
7072 else
7073 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7074 }
7075
7076 /* Look for a class template declaration. We look for hidden names
7077 because two friend declarations of the same template are the
7078 same. For example, in:
7079
7080 struct A {
7081 template <typename> friend class F;
7082 };
7083 template <typename> struct B {
7084 template <typename> friend class F;
7085 };
7086
7087 both F templates are the same. */
7088 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7089 /*block_p=*/true, 0,
7090 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7091
7092 /* But, if we don't find one, it might be because we're in a
7093 situation like this:
7094
7095 template <class T>
7096 struct S {
7097 template <class U>
7098 friend struct S;
7099 };
7100
7101 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7102 for `S<int>', not the TEMPLATE_DECL. */
7103 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7104 {
7105 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7106 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7107 }
7108
7109 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7110 {
7111 /* The friend template has already been declared. Just
7112 check to see that the declarations match, and install any new
7113 default parameters. We must tsubst the default parameters,
7114 of course. We only need the innermost template parameters
7115 because that is all that redeclare_class_template will look
7116 at. */
7117 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7118 > TMPL_ARGS_DEPTH (args))
7119 {
7120 tree parms;
7121 location_t saved_input_location;
7122 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7123 args, tf_warning_or_error);
7124
7125 saved_input_location = input_location;
7126 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7127 redeclare_class_template (TREE_TYPE (tmpl), parms);
7128 input_location = saved_input_location;
7129
7130 }
7131
7132 friend_type = TREE_TYPE (tmpl);
7133 }
7134 else
7135 {
7136 /* The friend template has not already been declared. In this
7137 case, the instantiation of the template class will cause the
7138 injection of this template into the global scope. */
7139 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7140 if (tmpl == error_mark_node)
7141 return error_mark_node;
7142
7143 /* The new TMPL is not an instantiation of anything, so we
7144 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7145 the new type because that is supposed to be the corresponding
7146 template decl, i.e., TMPL. */
7147 DECL_USE_TEMPLATE (tmpl) = 0;
7148 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7149 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7150 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7151 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7152
7153 /* Inject this template into the global scope. */
7154 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7155 }
7156
7157 if (context)
7158 {
7159 if (TREE_CODE (context) == NAMESPACE_DECL)
7160 pop_nested_namespace (context);
7161 else
7162 pop_nested_class ();
7163 }
7164
7165 return friend_type;
7166 }
7167
7168 /* Returns zero if TYPE cannot be completed later due to circularity.
7169 Otherwise returns one. */
7170
7171 static int
7172 can_complete_type_without_circularity (tree type)
7173 {
7174 if (type == NULL_TREE || type == error_mark_node)
7175 return 0;
7176 else if (COMPLETE_TYPE_P (type))
7177 return 1;
7178 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7179 return can_complete_type_without_circularity (TREE_TYPE (type));
7180 else if (CLASS_TYPE_P (type)
7181 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
7182 return 0;
7183 else
7184 return 1;
7185 }
7186
7187 /* Apply any attributes which had to be deferred until instantiation
7188 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7189 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7190
7191 static void
7192 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7193 tree args, tsubst_flags_t complain, tree in_decl)
7194 {
7195 tree last_dep = NULL_TREE;
7196 tree t;
7197 tree *p;
7198
7199 for (t = attributes; t; t = TREE_CHAIN (t))
7200 if (ATTR_IS_DEPENDENT (t))
7201 {
7202 last_dep = t;
7203 attributes = copy_list (attributes);
7204 break;
7205 }
7206
7207 if (DECL_P (*decl_p))
7208 {
7209 if (TREE_TYPE (*decl_p) == error_mark_node)
7210 return;
7211 p = &DECL_ATTRIBUTES (*decl_p);
7212 }
7213 else
7214 p = &TYPE_ATTRIBUTES (*decl_p);
7215
7216 if (last_dep)
7217 {
7218 tree late_attrs = NULL_TREE;
7219 tree *q = &late_attrs;
7220
7221 for (*p = attributes; *p; )
7222 {
7223 t = *p;
7224 if (ATTR_IS_DEPENDENT (t))
7225 {
7226 *p = TREE_CHAIN (t);
7227 TREE_CHAIN (t) = NULL_TREE;
7228 /* If the first attribute argument is an identifier, don't
7229 pass it through tsubst. Attributes like mode, format,
7230 cleanup and several target specific attributes expect it
7231 unmodified. */
7232 if (TREE_VALUE (t)
7233 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
7234 && TREE_VALUE (TREE_VALUE (t))
7235 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
7236 == IDENTIFIER_NODE))
7237 {
7238 tree chain
7239 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
7240 in_decl,
7241 /*integral_constant_expression_p=*/false);
7242 if (chain != TREE_CHAIN (TREE_VALUE (t)))
7243 TREE_VALUE (t)
7244 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
7245 chain);
7246 }
7247 else
7248 TREE_VALUE (t)
7249 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
7250 /*integral_constant_expression_p=*/false);
7251 *q = t;
7252 q = &TREE_CHAIN (t);
7253 }
7254 else
7255 p = &TREE_CHAIN (t);
7256 }
7257
7258 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
7259 }
7260 }
7261
7262 /* Perform (or defer) access check for typedefs that were referenced
7263 from within the template TMPL code.
7264 This is a subroutine of instantiate_template and instantiate_class_template.
7265 TMPL is the template to consider and TARGS is the list of arguments of
7266 that template. */
7267
7268 static void
7269 perform_typedefs_access_check (tree tmpl, tree targs)
7270 {
7271 tree t;
7272
7273 if (!tmpl
7274 || (!CLASS_TYPE_P (tmpl)
7275 && TREE_CODE (tmpl) != FUNCTION_DECL))
7276 return;
7277
7278 for (t = get_types_needing_access_check (tmpl); t; t = TREE_CHAIN (t))
7279 {
7280 tree type_decl = TREE_PURPOSE (t);
7281 tree type_scope = TREE_VALUE (t);
7282
7283 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
7284 continue;
7285
7286 if (uses_template_parms (type_decl))
7287 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
7288 if (uses_template_parms (type_scope))
7289 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
7290
7291 perform_or_defer_access_check (TYPE_BINFO (type_scope),
7292 type_decl, type_decl);
7293 }
7294 }
7295
7296 tree
7297 instantiate_class_template (tree type)
7298 {
7299 tree templ, args, pattern, t, member;
7300 tree typedecl;
7301 tree pbinfo;
7302 tree base_list;
7303
7304 if (type == error_mark_node)
7305 return error_mark_node;
7306
7307 if (TYPE_BEING_DEFINED (type)
7308 || COMPLETE_TYPE_P (type)
7309 || dependent_type_p (type))
7310 return type;
7311
7312 /* Figure out which template is being instantiated. */
7313 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
7314 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7315
7316 /* Determine what specialization of the original template to
7317 instantiate. */
7318 t = most_specialized_class (type, templ);
7319 if (t == error_mark_node)
7320 {
7321 TYPE_BEING_DEFINED (type) = 1;
7322 return error_mark_node;
7323 }
7324 else if (t)
7325 {
7326 /* This TYPE is actually an instantiation of a partial
7327 specialization. We replace the innermost set of ARGS with
7328 the arguments appropriate for substitution. For example,
7329 given:
7330
7331 template <class T> struct S {};
7332 template <class T> struct S<T*> {};
7333
7334 and supposing that we are instantiating S<int*>, ARGS will
7335 presently be {int*} -- but we need {int}. */
7336 pattern = TREE_TYPE (t);
7337 args = TREE_PURPOSE (t);
7338 }
7339 else
7340 {
7341 pattern = TREE_TYPE (templ);
7342 args = CLASSTYPE_TI_ARGS (type);
7343 }
7344
7345 /* If the template we're instantiating is incomplete, then clearly
7346 there's nothing we can do. */
7347 if (!COMPLETE_TYPE_P (pattern))
7348 return type;
7349
7350 /* If we've recursively instantiated too many templates, stop. */
7351 if (! push_tinst_level (type))
7352 return type;
7353
7354 /* Now we're really doing the instantiation. Mark the type as in
7355 the process of being defined. */
7356 TYPE_BEING_DEFINED (type) = 1;
7357
7358 /* We may be in the middle of deferred access check. Disable
7359 it now. */
7360 push_deferring_access_checks (dk_no_deferred);
7361
7362 push_to_top_level ();
7363
7364 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
7365
7366 /* Set the input location to the most specialized template definition.
7367 This is needed if tsubsting causes an error. */
7368 typedecl = TYPE_MAIN_DECL (pattern);
7369 input_location = DECL_SOURCE_LOCATION (typedecl);
7370
7371 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7372 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7373 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7374 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7375 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
7376 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
7377 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
7378 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
7379 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7380 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7381 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7382 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7383 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7384 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7385 if (ANON_AGGR_TYPE_P (pattern))
7386 SET_ANON_AGGR_TYPE_P (type);
7387 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7388 {
7389 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7390 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7391 }
7392
7393 pbinfo = TYPE_BINFO (pattern);
7394
7395 /* We should never instantiate a nested class before its enclosing
7396 class; we need to look up the nested class by name before we can
7397 instantiate it, and that lookup should instantiate the enclosing
7398 class. */
7399 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7400 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
7401 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
7402
7403 base_list = NULL_TREE;
7404 if (BINFO_N_BASE_BINFOS (pbinfo))
7405 {
7406 tree pbase_binfo;
7407 tree context = TYPE_CONTEXT (type);
7408 tree pushed_scope;
7409 int i;
7410
7411 /* We must enter the scope containing the type, as that is where
7412 the accessibility of types named in dependent bases are
7413 looked up from. */
7414 pushed_scope = push_scope (context ? context : global_namespace);
7415
7416 /* Substitute into each of the bases to determine the actual
7417 basetypes. */
7418 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7419 {
7420 tree base;
7421 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7422 tree expanded_bases = NULL_TREE;
7423 int idx, len = 1;
7424
7425 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7426 {
7427 expanded_bases =
7428 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7429 args, tf_error, NULL_TREE);
7430 if (expanded_bases == error_mark_node)
7431 continue;
7432
7433 len = TREE_VEC_LENGTH (expanded_bases);
7434 }
7435
7436 for (idx = 0; idx < len; idx++)
7437 {
7438 if (expanded_bases)
7439 /* Extract the already-expanded base class. */
7440 base = TREE_VEC_ELT (expanded_bases, idx);
7441 else
7442 /* Substitute to figure out the base class. */
7443 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7444 NULL_TREE);
7445
7446 if (base == error_mark_node)
7447 continue;
7448
7449 base_list = tree_cons (access, base, base_list);
7450 if (BINFO_VIRTUAL_P (pbase_binfo))
7451 TREE_TYPE (base_list) = integer_type_node;
7452 }
7453 }
7454
7455 /* The list is now in reverse order; correct that. */
7456 base_list = nreverse (base_list);
7457
7458 if (pushed_scope)
7459 pop_scope (pushed_scope);
7460 }
7461 /* Now call xref_basetypes to set up all the base-class
7462 information. */
7463 xref_basetypes (type, base_list);
7464
7465 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7466 (int) ATTR_FLAG_TYPE_IN_PLACE,
7467 args, tf_error, NULL_TREE);
7468
7469 /* Now that our base classes are set up, enter the scope of the
7470 class, so that name lookups into base classes, etc. will work
7471 correctly. This is precisely analogous to what we do in
7472 begin_class_definition when defining an ordinary non-template
7473 class, except we also need to push the enclosing classes. */
7474 push_nested_class (type);
7475
7476 /* Now members are processed in the order of declaration. */
7477 for (member = CLASSTYPE_DECL_LIST (pattern);
7478 member; member = TREE_CHAIN (member))
7479 {
7480 tree t = TREE_VALUE (member);
7481
7482 if (TREE_PURPOSE (member))
7483 {
7484 if (TYPE_P (t))
7485 {
7486 /* Build new CLASSTYPE_NESTED_UTDS. */
7487
7488 tree newtag;
7489 bool class_template_p;
7490
7491 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7492 && TYPE_LANG_SPECIFIC (t)
7493 && CLASSTYPE_IS_TEMPLATE (t));
7494 /* If the member is a class template, then -- even after
7495 substitution -- there may be dependent types in the
7496 template argument list for the class. We increment
7497 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7498 that function will assume that no types are dependent
7499 when outside of a template. */
7500 if (class_template_p)
7501 ++processing_template_decl;
7502 newtag = tsubst (t, args, tf_error, NULL_TREE);
7503 if (class_template_p)
7504 --processing_template_decl;
7505 if (newtag == error_mark_node)
7506 continue;
7507
7508 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7509 {
7510 tree name = TYPE_IDENTIFIER (t);
7511
7512 if (class_template_p)
7513 /* Unfortunately, lookup_template_class sets
7514 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7515 instantiation (i.e., for the type of a member
7516 template class nested within a template class.)
7517 This behavior is required for
7518 maybe_process_partial_specialization to work
7519 correctly, but is not accurate in this case;
7520 the TAG is not an instantiation of anything.
7521 (The corresponding TEMPLATE_DECL is an
7522 instantiation, but the TYPE is not.) */
7523 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
7524
7525 /* Now, we call pushtag to put this NEWTAG into the scope of
7526 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7527 pushtag calling push_template_decl. We don't have to do
7528 this for enums because it will already have been done in
7529 tsubst_enum. */
7530 if (name)
7531 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
7532 pushtag (name, newtag, /*tag_scope=*/ts_current);
7533 }
7534 }
7535 else if (TREE_CODE (t) == FUNCTION_DECL
7536 || DECL_FUNCTION_TEMPLATE_P (t))
7537 {
7538 /* Build new TYPE_METHODS. */
7539 tree r;
7540
7541 if (TREE_CODE (t) == TEMPLATE_DECL)
7542 ++processing_template_decl;
7543 r = tsubst (t, args, tf_error, NULL_TREE);
7544 if (TREE_CODE (t) == TEMPLATE_DECL)
7545 --processing_template_decl;
7546 set_current_access_from_decl (r);
7547 finish_member_declaration (r);
7548 }
7549 else
7550 {
7551 /* Build new TYPE_FIELDS. */
7552 if (TREE_CODE (t) == STATIC_ASSERT)
7553 {
7554 tree condition =
7555 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
7556 tf_warning_or_error, NULL_TREE,
7557 /*integral_constant_expression_p=*/true);
7558 finish_static_assert (condition,
7559 STATIC_ASSERT_MESSAGE (t),
7560 STATIC_ASSERT_SOURCE_LOCATION (t),
7561 /*member_p=*/true);
7562 }
7563 else if (TREE_CODE (t) != CONST_DECL)
7564 {
7565 tree r;
7566
7567 /* The file and line for this declaration, to
7568 assist in error message reporting. Since we
7569 called push_tinst_level above, we don't need to
7570 restore these. */
7571 input_location = DECL_SOURCE_LOCATION (t);
7572
7573 if (TREE_CODE (t) == TEMPLATE_DECL)
7574 ++processing_template_decl;
7575 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
7576 if (TREE_CODE (t) == TEMPLATE_DECL)
7577 --processing_template_decl;
7578 if (TREE_CODE (r) == VAR_DECL)
7579 {
7580 /* In [temp.inst]:
7581
7582 [t]he initialization (and any associated
7583 side-effects) of a static data member does
7584 not occur unless the static data member is
7585 itself used in a way that requires the
7586 definition of the static data member to
7587 exist.
7588
7589 Therefore, we do not substitute into the
7590 initialized for the static data member here. */
7591 finish_static_data_member_decl
7592 (r,
7593 /*init=*/NULL_TREE,
7594 /*init_const_expr_p=*/false,
7595 /*asmspec_tree=*/NULL_TREE,
7596 /*flags=*/0);
7597 if (DECL_INITIALIZED_IN_CLASS_P (r))
7598 check_static_variable_definition (r, TREE_TYPE (r));
7599 }
7600 else if (TREE_CODE (r) == FIELD_DECL)
7601 {
7602 /* Determine whether R has a valid type and can be
7603 completed later. If R is invalid, then it is
7604 replaced by error_mark_node so that it will not be
7605 added to TYPE_FIELDS. */
7606 tree rtype = TREE_TYPE (r);
7607 if (can_complete_type_without_circularity (rtype))
7608 complete_type (rtype);
7609
7610 if (!COMPLETE_TYPE_P (rtype))
7611 {
7612 cxx_incomplete_type_error (r, rtype);
7613 r = error_mark_node;
7614 }
7615 }
7616
7617 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
7618 such a thing will already have been added to the field
7619 list by tsubst_enum in finish_member_declaration in the
7620 CLASSTYPE_NESTED_UTDS case above. */
7621 if (!(TREE_CODE (r) == TYPE_DECL
7622 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
7623 && DECL_ARTIFICIAL (r)))
7624 {
7625 set_current_access_from_decl (r);
7626 finish_member_declaration (r);
7627 }
7628 }
7629 }
7630 }
7631 else
7632 {
7633 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
7634 {
7635 /* Build new CLASSTYPE_FRIEND_CLASSES. */
7636
7637 tree friend_type = t;
7638 bool adjust_processing_template_decl = false;
7639
7640 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7641 {
7642 /* template <class T> friend class C; */
7643 friend_type = tsubst_friend_class (friend_type, args);
7644 adjust_processing_template_decl = true;
7645 }
7646 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
7647 {
7648 /* template <class T> friend class C::D; */
7649 friend_type = tsubst (friend_type, args,
7650 tf_warning_or_error, NULL_TREE);
7651 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7652 friend_type = TREE_TYPE (friend_type);
7653 adjust_processing_template_decl = true;
7654 }
7655 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
7656 {
7657 /* This could be either
7658
7659 friend class T::C;
7660
7661 when dependent_type_p is false or
7662
7663 template <class U> friend class T::C;
7664
7665 otherwise. */
7666 friend_type = tsubst (friend_type, args,
7667 tf_warning_or_error, NULL_TREE);
7668 /* Bump processing_template_decl for correct
7669 dependent_type_p calculation. */
7670 ++processing_template_decl;
7671 if (dependent_type_p (friend_type))
7672 adjust_processing_template_decl = true;
7673 --processing_template_decl;
7674 }
7675 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
7676 && hidden_name_p (TYPE_NAME (friend_type)))
7677 {
7678 /* friend class C;
7679
7680 where C hasn't been declared yet. Let's lookup name
7681 from namespace scope directly, bypassing any name that
7682 come from dependent base class. */
7683 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
7684
7685 /* The call to xref_tag_from_type does injection for friend
7686 classes. */
7687 push_nested_namespace (ns);
7688 friend_type =
7689 xref_tag_from_type (friend_type, NULL_TREE,
7690 /*tag_scope=*/ts_current);
7691 pop_nested_namespace (ns);
7692 }
7693 else if (uses_template_parms (friend_type))
7694 /* friend class C<T>; */
7695 friend_type = tsubst (friend_type, args,
7696 tf_warning_or_error, NULL_TREE);
7697 /* Otherwise it's
7698
7699 friend class C;
7700
7701 where C is already declared or
7702
7703 friend class C<int>;
7704
7705 We don't have to do anything in these cases. */
7706
7707 if (adjust_processing_template_decl)
7708 /* Trick make_friend_class into realizing that the friend
7709 we're adding is a template, not an ordinary class. It's
7710 important that we use make_friend_class since it will
7711 perform some error-checking and output cross-reference
7712 information. */
7713 ++processing_template_decl;
7714
7715 if (friend_type != error_mark_node)
7716 make_friend_class (type, friend_type, /*complain=*/false);
7717
7718 if (adjust_processing_template_decl)
7719 --processing_template_decl;
7720 }
7721 else
7722 {
7723 /* Build new DECL_FRIENDLIST. */
7724 tree r;
7725
7726 /* The file and line for this declaration, to
7727 assist in error message reporting. Since we
7728 called push_tinst_level above, we don't need to
7729 restore these. */
7730 input_location = DECL_SOURCE_LOCATION (t);
7731
7732 if (TREE_CODE (t) == TEMPLATE_DECL)
7733 {
7734 ++processing_template_decl;
7735 push_deferring_access_checks (dk_no_check);
7736 }
7737
7738 r = tsubst_friend_function (t, args);
7739 add_friend (type, r, /*complain=*/false);
7740 if (TREE_CODE (t) == TEMPLATE_DECL)
7741 {
7742 pop_deferring_access_checks ();
7743 --processing_template_decl;
7744 }
7745 }
7746 }
7747 }
7748
7749 /* Set the file and line number information to whatever is given for
7750 the class itself. This puts error messages involving generated
7751 implicit functions at a predictable point, and the same point
7752 that would be used for non-template classes. */
7753 input_location = DECL_SOURCE_LOCATION (typedecl);
7754
7755 unreverse_member_declarations (type);
7756 finish_struct_1 (type);
7757 TYPE_BEING_DEFINED (type) = 0;
7758
7759 /* Now that the class is complete, instantiate default arguments for
7760 any member functions. We don't do this earlier because the
7761 default arguments may reference members of the class. */
7762 if (!PRIMARY_TEMPLATE_P (templ))
7763 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
7764 if (TREE_CODE (t) == FUNCTION_DECL
7765 /* Implicitly generated member functions will not have template
7766 information; they are not instantiations, but instead are
7767 created "fresh" for each instantiation. */
7768 && DECL_TEMPLATE_INFO (t))
7769 tsubst_default_arguments (t);
7770
7771 /* Some typedefs referenced from within the template code need to be access
7772 checked at template instantiation time, i.e now. These types were
7773 added to the template at parsing time. Let's get those and perform
7774 the access checks then. */
7775 perform_typedefs_access_check (pattern, args);
7776 perform_deferred_access_checks ();
7777 pop_nested_class ();
7778 pop_from_top_level ();
7779 pop_deferring_access_checks ();
7780 pop_tinst_level ();
7781
7782 /* The vtable for a template class can be emitted in any translation
7783 unit in which the class is instantiated. When there is no key
7784 method, however, finish_struct_1 will already have added TYPE to
7785 the keyed_classes list. */
7786 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
7787 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
7788
7789 return type;
7790 }
7791
7792 static tree
7793 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7794 {
7795 tree r;
7796
7797 if (!t)
7798 r = t;
7799 else if (TYPE_P (t))
7800 r = tsubst (t, args, complain, in_decl);
7801 else
7802 {
7803 r = tsubst_expr (t, args, complain, in_decl,
7804 /*integral_constant_expression_p=*/true);
7805 r = fold_non_dependent_expr (r);
7806 }
7807 return r;
7808 }
7809
7810 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
7811 NONTYPE_ARGUMENT_PACK. */
7812
7813 static tree
7814 make_fnparm_pack (tree spec_parm)
7815 {
7816 /* Collect all of the extra "packed" parameters into an
7817 argument pack. */
7818 tree parmvec;
7819 tree parmtypevec;
7820 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
7821 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
7822 int i, len = list_length (spec_parm);
7823
7824 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
7825 parmvec = make_tree_vec (len);
7826 parmtypevec = make_tree_vec (len);
7827 for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
7828 {
7829 TREE_VEC_ELT (parmvec, i) = spec_parm;
7830 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
7831 }
7832
7833 /* Build the argument packs. */
7834 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
7835 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
7836 TREE_TYPE (argpack) = argtypepack;
7837
7838 return argpack;
7839 }
7840
7841 /* Substitute ARGS into T, which is an pack expansion
7842 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
7843 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
7844 (if only a partial substitution could be performed) or
7845 ERROR_MARK_NODE if there was an error. */
7846 tree
7847 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
7848 tree in_decl)
7849 {
7850 tree pattern;
7851 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
7852 tree first_arg_pack; int i, len = -1;
7853 tree result;
7854 int incomplete = 0;
7855 bool very_local_specializations = false;
7856
7857 gcc_assert (PACK_EXPANSION_P (t));
7858 pattern = PACK_EXPANSION_PATTERN (t);
7859
7860 /* Determine the argument packs that will instantiate the parameter
7861 packs used in the expansion expression. While we're at it,
7862 compute the number of arguments to be expanded and make sure it
7863 is consistent. */
7864 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
7865 pack = TREE_CHAIN (pack))
7866 {
7867 tree parm_pack = TREE_VALUE (pack);
7868 tree arg_pack = NULL_TREE;
7869 tree orig_arg = NULL_TREE;
7870
7871 if (TREE_CODE (parm_pack) == PARM_DECL)
7872 {
7873 arg_pack = retrieve_local_specialization (parm_pack);
7874 if (arg_pack == NULL_TREE)
7875 {
7876 /* This can happen for a parameter name used later in a function
7877 declaration (such as in a late-specified return type). Just
7878 make a dummy decl, since it's only used for its type. */
7879 gcc_assert (cp_unevaluated_operand != 0);
7880 arg_pack = tsubst_decl (parm_pack, args, complain);
7881 arg_pack = make_fnparm_pack (arg_pack);
7882 }
7883 }
7884 else
7885 {
7886 int level, idx, levels;
7887 template_parm_level_and_index (parm_pack, &level, &idx);
7888
7889 levels = TMPL_ARGS_DEPTH (args);
7890 if (level <= levels)
7891 arg_pack = TMPL_ARG (args, level, idx);
7892 }
7893
7894 orig_arg = arg_pack;
7895 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
7896 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
7897
7898 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
7899 /* This can only happen if we forget to expand an argument
7900 pack somewhere else. Just return an error, silently. */
7901 {
7902 result = make_tree_vec (1);
7903 TREE_VEC_ELT (result, 0) = error_mark_node;
7904 return result;
7905 }
7906
7907 if (arg_pack
7908 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
7909 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
7910 {
7911 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
7912 tree pattern = PACK_EXPANSION_PATTERN (expansion);
7913 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
7914 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
7915 /* The argument pack that the parameter maps to is just an
7916 expansion of the parameter itself, such as one would
7917 find in the implicit typedef of a class inside the
7918 class itself. Consider this parameter "unsubstituted",
7919 so that we will maintain the outer pack expansion. */
7920 arg_pack = NULL_TREE;
7921 }
7922
7923 if (arg_pack)
7924 {
7925 int my_len =
7926 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
7927
7928 /* It's all-or-nothing with incomplete argument packs. */
7929 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7930 return error_mark_node;
7931
7932 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7933 incomplete = 1;
7934
7935 if (len < 0)
7936 {
7937 len = my_len;
7938 first_arg_pack = arg_pack;
7939 }
7940 else if (len != my_len)
7941 {
7942 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
7943 error ("mismatched argument pack lengths while expanding "
7944 "%<%T%>",
7945 pattern);
7946 else
7947 error ("mismatched argument pack lengths while expanding "
7948 "%<%E%>",
7949 pattern);
7950 return error_mark_node;
7951 }
7952
7953 /* Keep track of the parameter packs and their corresponding
7954 argument packs. */
7955 packs = tree_cons (parm_pack, arg_pack, packs);
7956 TREE_TYPE (packs) = orig_arg;
7957 }
7958 else
7959 /* We can't substitute for this parameter pack. */
7960 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
7961 TREE_VALUE (pack),
7962 unsubstituted_packs);
7963 }
7964
7965 /* We cannot expand this expansion expression, because we don't have
7966 all of the argument packs we need. Substitute into the pattern
7967 and return a PACK_EXPANSION_*. The caller will need to deal with
7968 that. */
7969 if (unsubstituted_packs)
7970 {
7971 tree new_pat;
7972 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
7973 new_pat = tsubst_expr (pattern, args, complain, in_decl,
7974 /*integral_constant_expression_p=*/false);
7975 else
7976 new_pat = tsubst (pattern, args, complain, in_decl);
7977 return make_pack_expansion (new_pat);
7978 }
7979
7980 /* We could not find any argument packs that work. */
7981 if (len < 0)
7982 return error_mark_node;
7983
7984 if (!local_specializations)
7985 {
7986 /* We're in a late-specified return type, so we don't have a local
7987 specializations table. Create one for doing this expansion. */
7988 very_local_specializations = true;
7989 local_specializations = htab_create (37,
7990 hash_local_specialization,
7991 eq_local_specializations,
7992 NULL);
7993 }
7994
7995 /* For each argument in each argument pack, substitute into the
7996 pattern. */
7997 result = make_tree_vec (len + incomplete);
7998 for (i = 0; i < len + incomplete; ++i)
7999 {
8000 /* For parameter pack, change the substitution of the parameter
8001 pack to the ith argument in its argument pack, then expand
8002 the pattern. */
8003 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8004 {
8005 tree parm = TREE_PURPOSE (pack);
8006
8007 if (TREE_CODE (parm) == PARM_DECL)
8008 {
8009 /* Select the Ith argument from the pack. */
8010 tree arg = make_node (ARGUMENT_PACK_SELECT);
8011 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8012 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8013 mark_used (parm);
8014 register_local_specialization (arg, parm);
8015 }
8016 else
8017 {
8018 tree value = parm;
8019 int idx, level;
8020 template_parm_level_and_index (parm, &level, &idx);
8021
8022 if (i < len)
8023 {
8024 /* Select the Ith argument from the pack. */
8025 value = make_node (ARGUMENT_PACK_SELECT);
8026 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8027 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8028 }
8029
8030 /* Update the corresponding argument. */
8031 TMPL_ARG (args, level, idx) = value;
8032 }
8033 }
8034
8035 /* Substitute into the PATTERN with the altered arguments. */
8036 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8037 TREE_VEC_ELT (result, i) =
8038 tsubst_expr (pattern, args, complain, in_decl,
8039 /*integral_constant_expression_p=*/false);
8040 else
8041 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8042
8043 if (i == len)
8044 /* When we have incomplete argument packs, the last "expanded"
8045 result is itself a pack expansion, which allows us
8046 to deduce more arguments. */
8047 TREE_VEC_ELT (result, i) =
8048 make_pack_expansion (TREE_VEC_ELT (result, i));
8049
8050 if (TREE_VEC_ELT (result, i) == error_mark_node)
8051 {
8052 result = error_mark_node;
8053 break;
8054 }
8055 }
8056
8057 /* Update ARGS to restore the substitution from parameter packs to
8058 their argument packs. */
8059 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8060 {
8061 tree parm = TREE_PURPOSE (pack);
8062
8063 if (TREE_CODE (parm) == PARM_DECL)
8064 register_local_specialization (TREE_TYPE (pack), parm);
8065 else
8066 {
8067 int idx, level;
8068 template_parm_level_and_index (parm, &level, &idx);
8069
8070 /* Update the corresponding argument. */
8071 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8072 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8073 TREE_TYPE (pack);
8074 else
8075 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8076 }
8077 }
8078
8079 if (very_local_specializations)
8080 {
8081 htab_delete (local_specializations);
8082 local_specializations = NULL;
8083 }
8084
8085 return result;
8086 }
8087
8088 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8089 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8090 parameter packs; all parms generated from a function parameter pack will
8091 have the same DECL_PARM_INDEX. */
8092
8093 tree
8094 get_pattern_parm (tree parm, tree tmpl)
8095 {
8096 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
8097 tree patparm;
8098
8099 if (DECL_ARTIFICIAL (parm))
8100 {
8101 for (patparm = DECL_ARGUMENTS (pattern);
8102 patparm; patparm = TREE_CHAIN (patparm))
8103 if (DECL_ARTIFICIAL (patparm)
8104 && DECL_NAME (parm) == DECL_NAME (patparm))
8105 break;
8106 }
8107 else
8108 {
8109 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
8110 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
8111 gcc_assert (DECL_PARM_INDEX (patparm)
8112 == DECL_PARM_INDEX (parm));
8113 }
8114
8115 return patparm;
8116 }
8117
8118 /* Substitute ARGS into the vector or list of template arguments T. */
8119
8120 static tree
8121 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8122 {
8123 tree orig_t = t;
8124 int len = TREE_VEC_LENGTH (t);
8125 int need_new = 0, i, expanded_len_adjust = 0, out;
8126 tree *elts = (tree *) alloca (len * sizeof (tree));
8127
8128 for (i = 0; i < len; i++)
8129 {
8130 tree orig_arg = TREE_VEC_ELT (t, i);
8131 tree new_arg;
8132
8133 if (TREE_CODE (orig_arg) == TREE_VEC)
8134 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
8135 else if (PACK_EXPANSION_P (orig_arg))
8136 {
8137 /* Substitute into an expansion expression. */
8138 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8139
8140 if (TREE_CODE (new_arg) == TREE_VEC)
8141 /* Add to the expanded length adjustment the number of
8142 expanded arguments. We subtract one from this
8143 measurement, because the argument pack expression
8144 itself is already counted as 1 in
8145 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8146 the argument pack is empty. */
8147 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
8148 }
8149 else if (ARGUMENT_PACK_P (orig_arg))
8150 {
8151 /* Substitute into each of the arguments. */
8152 new_arg = TYPE_P (orig_arg)
8153 ? cxx_make_type (TREE_CODE (orig_arg))
8154 : make_node (TREE_CODE (orig_arg));
8155
8156 SET_ARGUMENT_PACK_ARGS (
8157 new_arg,
8158 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
8159 args, complain, in_decl));
8160
8161 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
8162 new_arg = error_mark_node;
8163
8164 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
8165 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
8166 complain, in_decl);
8167 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
8168
8169 if (TREE_TYPE (new_arg) == error_mark_node)
8170 new_arg = error_mark_node;
8171 }
8172 }
8173 else
8174 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
8175
8176 if (new_arg == error_mark_node)
8177 return error_mark_node;
8178
8179 elts[i] = new_arg;
8180 if (new_arg != orig_arg)
8181 need_new = 1;
8182 }
8183
8184 if (!need_new)
8185 return t;
8186
8187 /* Make space for the expanded arguments coming from template
8188 argument packs. */
8189 t = make_tree_vec (len + expanded_len_adjust);
8190 for (i = 0, out = 0; i < len; i++)
8191 {
8192 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
8193 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
8194 && TREE_CODE (elts[i]) == TREE_VEC)
8195 {
8196 int idx;
8197
8198 /* Now expand the template argument pack "in place". */
8199 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
8200 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
8201 }
8202 else
8203 {
8204 TREE_VEC_ELT (t, out) = elts[i];
8205 out++;
8206 }
8207 }
8208
8209 return t;
8210 }
8211
8212 /* Return the result of substituting ARGS into the template parameters
8213 given by PARMS. If there are m levels of ARGS and m + n levels of
8214 PARMS, then the result will contain n levels of PARMS. For
8215 example, if PARMS is `template <class T> template <class U>
8216 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8217 result will be `template <int*, double, class V>'. */
8218
8219 static tree
8220 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
8221 {
8222 tree r = NULL_TREE;
8223 tree* new_parms;
8224
8225 /* When substituting into a template, we must set
8226 PROCESSING_TEMPLATE_DECL as the template parameters may be
8227 dependent if they are based on one-another, and the dependency
8228 predicates are short-circuit outside of templates. */
8229 ++processing_template_decl;
8230
8231 for (new_parms = &r;
8232 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
8233 new_parms = &(TREE_CHAIN (*new_parms)),
8234 parms = TREE_CHAIN (parms))
8235 {
8236 tree new_vec =
8237 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
8238 int i;
8239
8240 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
8241 {
8242 tree tuple;
8243 tree default_value;
8244 tree parm_decl;
8245
8246 if (parms == error_mark_node)
8247 continue;
8248
8249 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
8250
8251 if (tuple == error_mark_node)
8252 continue;
8253
8254 default_value = TREE_PURPOSE (tuple);
8255 parm_decl = TREE_VALUE (tuple);
8256
8257 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
8258 if (TREE_CODE (parm_decl) == PARM_DECL
8259 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
8260 parm_decl = error_mark_node;
8261 default_value = tsubst_template_arg (default_value, args,
8262 complain, NULL_TREE);
8263
8264 tuple = build_tree_list (default_value, parm_decl);
8265 TREE_VEC_ELT (new_vec, i) = tuple;
8266 }
8267
8268 *new_parms =
8269 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
8270 - TMPL_ARGS_DEPTH (args)),
8271 new_vec, NULL_TREE);
8272 }
8273
8274 --processing_template_decl;
8275
8276 return r;
8277 }
8278
8279 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8280 type T. If T is not an aggregate or enumeration type, it is
8281 handled as if by tsubst. IN_DECL is as for tsubst. If
8282 ENTERING_SCOPE is nonzero, T is the context for a template which
8283 we are presently tsubst'ing. Return the substituted value. */
8284
8285 static tree
8286 tsubst_aggr_type (tree t,
8287 tree args,
8288 tsubst_flags_t complain,
8289 tree in_decl,
8290 int entering_scope)
8291 {
8292 if (t == NULL_TREE)
8293 return NULL_TREE;
8294
8295 switch (TREE_CODE (t))
8296 {
8297 case RECORD_TYPE:
8298 if (TYPE_PTRMEMFUNC_P (t))
8299 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
8300
8301 /* Else fall through. */
8302 case ENUMERAL_TYPE:
8303 case UNION_TYPE:
8304 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
8305 {
8306 tree argvec;
8307 tree context;
8308 tree r;
8309 int saved_unevaluated_operand;
8310 int saved_inhibit_evaluation_warnings;
8311
8312 /* In "sizeof(X<I>)" we need to evaluate "I". */
8313 saved_unevaluated_operand = cp_unevaluated_operand;
8314 cp_unevaluated_operand = 0;
8315 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8316 c_inhibit_evaluation_warnings = 0;
8317
8318 /* First, determine the context for the type we are looking
8319 up. */
8320 context = TYPE_CONTEXT (t);
8321 if (context)
8322 {
8323 context = tsubst_aggr_type (context, args, complain,
8324 in_decl, /*entering_scope=*/1);
8325 /* If context is a nested class inside a class template,
8326 it may still need to be instantiated (c++/33959). */
8327 if (TYPE_P (context))
8328 context = complete_type (context);
8329 }
8330
8331 /* Then, figure out what arguments are appropriate for the
8332 type we are trying to find. For example, given:
8333
8334 template <class T> struct S;
8335 template <class T, class U> void f(T, U) { S<U> su; }
8336
8337 and supposing that we are instantiating f<int, double>,
8338 then our ARGS will be {int, double}, but, when looking up
8339 S we only want {double}. */
8340 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
8341 complain, in_decl);
8342 if (argvec == error_mark_node)
8343 r = error_mark_node;
8344 else
8345 {
8346 r = lookup_template_class (t, argvec, in_decl, context,
8347 entering_scope, complain);
8348 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
8349 }
8350
8351 cp_unevaluated_operand = saved_unevaluated_operand;
8352 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8353
8354 return r;
8355 }
8356 else
8357 /* This is not a template type, so there's nothing to do. */
8358 return t;
8359
8360 default:
8361 return tsubst (t, args, complain, in_decl);
8362 }
8363 }
8364
8365 /* Substitute into the default argument ARG (a default argument for
8366 FN), which has the indicated TYPE. */
8367
8368 tree
8369 tsubst_default_argument (tree fn, tree type, tree arg)
8370 {
8371 tree saved_class_ptr = NULL_TREE;
8372 tree saved_class_ref = NULL_TREE;
8373
8374 /* This default argument came from a template. Instantiate the
8375 default argument here, not in tsubst. In the case of
8376 something like:
8377
8378 template <class T>
8379 struct S {
8380 static T t();
8381 void f(T = t());
8382 };
8383
8384 we must be careful to do name lookup in the scope of S<T>,
8385 rather than in the current class. */
8386 push_access_scope (fn);
8387 /* The "this" pointer is not valid in a default argument. */
8388 if (cfun)
8389 {
8390 saved_class_ptr = current_class_ptr;
8391 cp_function_chain->x_current_class_ptr = NULL_TREE;
8392 saved_class_ref = current_class_ref;
8393 cp_function_chain->x_current_class_ref = NULL_TREE;
8394 }
8395
8396 push_deferring_access_checks(dk_no_deferred);
8397 /* The default argument expression may cause implicitly defined
8398 member functions to be synthesized, which will result in garbage
8399 collection. We must treat this situation as if we were within
8400 the body of function so as to avoid collecting live data on the
8401 stack. */
8402 ++function_depth;
8403 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
8404 tf_warning_or_error, NULL_TREE,
8405 /*integral_constant_expression_p=*/false);
8406 --function_depth;
8407 pop_deferring_access_checks();
8408
8409 /* Restore the "this" pointer. */
8410 if (cfun)
8411 {
8412 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8413 cp_function_chain->x_current_class_ref = saved_class_ref;
8414 }
8415
8416 /* Make sure the default argument is reasonable. */
8417 arg = check_default_argument (type, arg);
8418
8419 pop_access_scope (fn);
8420
8421 return arg;
8422 }
8423
8424 /* Substitute into all the default arguments for FN. */
8425
8426 static void
8427 tsubst_default_arguments (tree fn)
8428 {
8429 tree arg;
8430 tree tmpl_args;
8431
8432 tmpl_args = DECL_TI_ARGS (fn);
8433
8434 /* If this function is not yet instantiated, we certainly don't need
8435 its default arguments. */
8436 if (uses_template_parms (tmpl_args))
8437 return;
8438
8439 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8440 arg;
8441 arg = TREE_CHAIN (arg))
8442 if (TREE_PURPOSE (arg))
8443 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
8444 TREE_VALUE (arg),
8445 TREE_PURPOSE (arg));
8446 }
8447
8448 /* Substitute the ARGS into the T, which is a _DECL. Return the
8449 result of the substitution. Issue error and warning messages under
8450 control of COMPLAIN. */
8451
8452 static tree
8453 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8454 {
8455 location_t saved_loc;
8456 tree r = NULL_TREE;
8457 tree in_decl = t;
8458 hashval_t hash = 0;
8459
8460 /* Set the filename and linenumber to improve error-reporting. */
8461 saved_loc = input_location;
8462 input_location = DECL_SOURCE_LOCATION (t);
8463
8464 switch (TREE_CODE (t))
8465 {
8466 case TEMPLATE_DECL:
8467 {
8468 /* We can get here when processing a member function template,
8469 member class template, or template template parameter. */
8470 tree decl = DECL_TEMPLATE_RESULT (t);
8471 tree spec;
8472 tree tmpl_args;
8473 tree full_args;
8474
8475 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8476 {
8477 /* Template template parameter is treated here. */
8478 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8479 if (new_type == error_mark_node)
8480 return error_mark_node;
8481
8482 r = copy_decl (t);
8483 TREE_CHAIN (r) = NULL_TREE;
8484 TREE_TYPE (r) = new_type;
8485 DECL_TEMPLATE_RESULT (r)
8486 = build_decl (DECL_SOURCE_LOCATION (decl),
8487 TYPE_DECL, DECL_NAME (decl), new_type);
8488 DECL_TEMPLATE_PARMS (r)
8489 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8490 complain);
8491 TYPE_NAME (new_type) = r;
8492 break;
8493 }
8494
8495 /* We might already have an instance of this template.
8496 The ARGS are for the surrounding class type, so the
8497 full args contain the tsubst'd args for the context,
8498 plus the innermost args from the template decl. */
8499 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
8500 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
8501 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8502 /* Because this is a template, the arguments will still be
8503 dependent, even after substitution. If
8504 PROCESSING_TEMPLATE_DECL is not set, the dependency
8505 predicates will short-circuit. */
8506 ++processing_template_decl;
8507 full_args = tsubst_template_args (tmpl_args, args,
8508 complain, in_decl);
8509 --processing_template_decl;
8510 if (full_args == error_mark_node)
8511 return error_mark_node;
8512
8513 /* If this is a default template template argument,
8514 tsubst might not have changed anything. */
8515 if (full_args == tmpl_args)
8516 return t;
8517
8518 hash = hash_tmpl_and_args (t, full_args);
8519 spec = retrieve_specialization (t, full_args, hash);
8520 if (spec != NULL_TREE)
8521 {
8522 r = spec;
8523 break;
8524 }
8525
8526 /* Make a new template decl. It will be similar to the
8527 original, but will record the current template arguments.
8528 We also create a new function declaration, which is just
8529 like the old one, but points to this new template, rather
8530 than the old one. */
8531 r = copy_decl (t);
8532 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
8533 TREE_CHAIN (r) = NULL_TREE;
8534
8535 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
8536
8537 if (TREE_CODE (decl) == TYPE_DECL)
8538 {
8539 tree new_type;
8540 ++processing_template_decl;
8541 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8542 --processing_template_decl;
8543 if (new_type == error_mark_node)
8544 return error_mark_node;
8545
8546 TREE_TYPE (r) = new_type;
8547 CLASSTYPE_TI_TEMPLATE (new_type) = r;
8548 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
8549 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8550 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
8551 }
8552 else
8553 {
8554 tree new_decl;
8555 ++processing_template_decl;
8556 new_decl = tsubst (decl, args, complain, in_decl);
8557 --processing_template_decl;
8558 if (new_decl == error_mark_node)
8559 return error_mark_node;
8560
8561 DECL_TEMPLATE_RESULT (r) = new_decl;
8562 DECL_TI_TEMPLATE (new_decl) = r;
8563 TREE_TYPE (r) = TREE_TYPE (new_decl);
8564 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
8565 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
8566 }
8567
8568 SET_DECL_IMPLICIT_INSTANTIATION (r);
8569 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
8570 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
8571
8572 /* The template parameters for this new template are all the
8573 template parameters for the old template, except the
8574 outermost level of parameters. */
8575 DECL_TEMPLATE_PARMS (r)
8576 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8577 complain);
8578
8579 if (PRIMARY_TEMPLATE_P (t))
8580 DECL_PRIMARY_TEMPLATE (r) = r;
8581
8582 if (TREE_CODE (decl) != TYPE_DECL)
8583 /* Record this non-type partial instantiation. */
8584 register_specialization (r, t,
8585 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
8586 false, hash);
8587 }
8588 break;
8589
8590 case FUNCTION_DECL:
8591 {
8592 tree ctx;
8593 tree argvec = NULL_TREE;
8594 tree *friends;
8595 tree gen_tmpl;
8596 tree type;
8597 int member;
8598 int args_depth;
8599 int parms_depth;
8600
8601 /* Nobody should be tsubst'ing into non-template functions. */
8602 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
8603
8604 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
8605 {
8606 tree spec;
8607 bool dependent_p;
8608
8609 /* If T is not dependent, just return it. We have to
8610 increment PROCESSING_TEMPLATE_DECL because
8611 value_dependent_expression_p assumes that nothing is
8612 dependent when PROCESSING_TEMPLATE_DECL is zero. */
8613 ++processing_template_decl;
8614 dependent_p = value_dependent_expression_p (t);
8615 --processing_template_decl;
8616 if (!dependent_p)
8617 return t;
8618
8619 /* Calculate the most general template of which R is a
8620 specialization, and the complete set of arguments used to
8621 specialize R. */
8622 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
8623 argvec = tsubst_template_args (DECL_TI_ARGS
8624 (DECL_TEMPLATE_RESULT (gen_tmpl)),
8625 args, complain, in_decl);
8626
8627 /* Check to see if we already have this specialization. */
8628 hash = hash_tmpl_and_args (gen_tmpl, argvec);
8629 spec = retrieve_specialization (gen_tmpl, argvec, hash);
8630
8631 if (spec)
8632 {
8633 r = spec;
8634 break;
8635 }
8636
8637 /* We can see more levels of arguments than parameters if
8638 there was a specialization of a member template, like
8639 this:
8640
8641 template <class T> struct S { template <class U> void f(); }
8642 template <> template <class U> void S<int>::f(U);
8643
8644 Here, we'll be substituting into the specialization,
8645 because that's where we can find the code we actually
8646 want to generate, but we'll have enough arguments for
8647 the most general template.
8648
8649 We also deal with the peculiar case:
8650
8651 template <class T> struct S {
8652 template <class U> friend void f();
8653 };
8654 template <class U> void f() {}
8655 template S<int>;
8656 template void f<double>();
8657
8658 Here, the ARGS for the instantiation of will be {int,
8659 double}. But, we only need as many ARGS as there are
8660 levels of template parameters in CODE_PATTERN. We are
8661 careful not to get fooled into reducing the ARGS in
8662 situations like:
8663
8664 template <class T> struct S { template <class U> void f(U); }
8665 template <class T> template <> void S<T>::f(int) {}
8666
8667 which we can spot because the pattern will be a
8668 specialization in this case. */
8669 args_depth = TMPL_ARGS_DEPTH (args);
8670 parms_depth =
8671 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
8672 if (args_depth > parms_depth
8673 && !DECL_TEMPLATE_SPECIALIZATION (t))
8674 args = get_innermost_template_args (args, parms_depth);
8675 }
8676 else
8677 {
8678 /* This special case arises when we have something like this:
8679
8680 template <class T> struct S {
8681 friend void f<int>(int, double);
8682 };
8683
8684 Here, the DECL_TI_TEMPLATE for the friend declaration
8685 will be an IDENTIFIER_NODE. We are being called from
8686 tsubst_friend_function, and we want only to create a
8687 new decl (R) with appropriate types so that we can call
8688 determine_specialization. */
8689 gen_tmpl = NULL_TREE;
8690 }
8691
8692 if (DECL_CLASS_SCOPE_P (t))
8693 {
8694 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
8695 member = 2;
8696 else
8697 member = 1;
8698 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
8699 complain, t, /*entering_scope=*/1);
8700 }
8701 else
8702 {
8703 member = 0;
8704 ctx = DECL_CONTEXT (t);
8705 }
8706 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8707 if (type == error_mark_node)
8708 return error_mark_node;
8709
8710 /* We do NOT check for matching decls pushed separately at this
8711 point, as they may not represent instantiations of this
8712 template, and in any case are considered separate under the
8713 discrete model. */
8714 r = copy_decl (t);
8715 DECL_USE_TEMPLATE (r) = 0;
8716 TREE_TYPE (r) = type;
8717 /* Clear out the mangled name and RTL for the instantiation. */
8718 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8719 SET_DECL_RTL (r, NULL_RTX);
8720 /* Leave DECL_INITIAL set on deleted instantiations. */
8721 if (!DECL_DELETED_FN (r))
8722 DECL_INITIAL (r) = NULL_TREE;
8723 DECL_CONTEXT (r) = ctx;
8724
8725 if (member && DECL_CONV_FN_P (r))
8726 /* Type-conversion operator. Reconstruct the name, in
8727 case it's the name of one of the template's parameters. */
8728 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
8729
8730 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
8731 complain, t);
8732 DECL_RESULT (r) = NULL_TREE;
8733
8734 TREE_STATIC (r) = 0;
8735 TREE_PUBLIC (r) = TREE_PUBLIC (t);
8736 DECL_EXTERNAL (r) = 1;
8737 /* If this is an instantiation of a function with internal
8738 linkage, we already know what object file linkage will be
8739 assigned to the instantiation. */
8740 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
8741 DECL_DEFER_OUTPUT (r) = 0;
8742 TREE_CHAIN (r) = NULL_TREE;
8743 DECL_PENDING_INLINE_INFO (r) = 0;
8744 DECL_PENDING_INLINE_P (r) = 0;
8745 DECL_SAVED_TREE (r) = NULL_TREE;
8746 DECL_STRUCT_FUNCTION (r) = NULL;
8747 TREE_USED (r) = 0;
8748 /* We'll re-clone as appropriate in instantiate_template. */
8749 DECL_CLONED_FUNCTION (r) = NULL_TREE;
8750
8751 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
8752 this in the special friend case mentioned above where
8753 GEN_TMPL is NULL. */
8754 if (gen_tmpl)
8755 {
8756 DECL_TEMPLATE_INFO (r)
8757 = tree_cons (gen_tmpl, argvec, NULL_TREE);
8758 SET_DECL_IMPLICIT_INSTANTIATION (r);
8759 register_specialization (r, gen_tmpl, argvec, false, hash);
8760
8761 /* We're not supposed to instantiate default arguments
8762 until they are called, for a template. But, for a
8763 declaration like:
8764
8765 template <class T> void f ()
8766 { extern void g(int i = T()); }
8767
8768 we should do the substitution when the template is
8769 instantiated. We handle the member function case in
8770 instantiate_class_template since the default arguments
8771 might refer to other members of the class. */
8772 if (!member
8773 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8774 && !uses_template_parms (argvec))
8775 tsubst_default_arguments (r);
8776 }
8777 else
8778 DECL_TEMPLATE_INFO (r) = NULL_TREE;
8779
8780 /* Copy the list of befriending classes. */
8781 for (friends = &DECL_BEFRIENDING_CLASSES (r);
8782 *friends;
8783 friends = &TREE_CHAIN (*friends))
8784 {
8785 *friends = copy_node (*friends);
8786 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
8787 args, complain,
8788 in_decl);
8789 }
8790
8791 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
8792 {
8793 maybe_retrofit_in_chrg (r);
8794 if (DECL_CONSTRUCTOR_P (r))
8795 grok_ctor_properties (ctx, r);
8796 /* If this is an instantiation of a member template, clone it.
8797 If it isn't, that'll be handled by
8798 clone_constructors_and_destructors. */
8799 if (PRIMARY_TEMPLATE_P (gen_tmpl))
8800 clone_function_decl (r, /*update_method_vec_p=*/0);
8801 }
8802 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
8803 && !grok_op_properties (r, (complain & tf_error) != 0))
8804 return error_mark_node;
8805
8806 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
8807 SET_DECL_FRIEND_CONTEXT (r,
8808 tsubst (DECL_FRIEND_CONTEXT (t),
8809 args, complain, in_decl));
8810
8811 /* Possibly limit visibility based on template args. */
8812 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
8813 if (DECL_VISIBILITY_SPECIFIED (t))
8814 {
8815 DECL_VISIBILITY_SPECIFIED (r) = 0;
8816 DECL_ATTRIBUTES (r)
8817 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8818 }
8819 determine_visibility (r);
8820
8821 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8822 args, complain, in_decl);
8823 }
8824 break;
8825
8826 case PARM_DECL:
8827 {
8828 tree type = NULL_TREE;
8829 int i, len = 1;
8830 tree expanded_types = NULL_TREE;
8831 tree prev_r = NULL_TREE;
8832 tree first_r = NULL_TREE;
8833
8834 if (FUNCTION_PARAMETER_PACK_P (t))
8835 {
8836 /* If there is a local specialization that isn't a
8837 parameter pack, it means that we're doing a "simple"
8838 substitution from inside tsubst_pack_expansion. Just
8839 return the local specialization (which will be a single
8840 parm). */
8841 tree spec = retrieve_local_specialization (t);
8842 if (spec
8843 && TREE_CODE (spec) == PARM_DECL
8844 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
8845 return spec;
8846
8847 /* Expand the TYPE_PACK_EXPANSION that provides the types for
8848 the parameters in this function parameter pack. */
8849 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
8850 complain, in_decl);
8851 if (TREE_CODE (expanded_types) == TREE_VEC)
8852 {
8853 len = TREE_VEC_LENGTH (expanded_types);
8854
8855 /* Zero-length parameter packs are boring. Just substitute
8856 into the chain. */
8857 if (len == 0)
8858 return tsubst (TREE_CHAIN (t), args, complain,
8859 TREE_CHAIN (t));
8860 }
8861 else
8862 {
8863 /* All we did was update the type. Make a note of that. */
8864 type = expanded_types;
8865 expanded_types = NULL_TREE;
8866 }
8867 }
8868
8869 /* Loop through all of the parameter's we'll build. When T is
8870 a function parameter pack, LEN is the number of expanded
8871 types in EXPANDED_TYPES; otherwise, LEN is 1. */
8872 r = NULL_TREE;
8873 for (i = 0; i < len; ++i)
8874 {
8875 prev_r = r;
8876 r = copy_node (t);
8877 if (DECL_TEMPLATE_PARM_P (t))
8878 SET_DECL_TEMPLATE_PARM_P (r);
8879
8880 /* An argument of a function parameter pack is not a parameter
8881 pack. */
8882 FUNCTION_PARAMETER_PACK_P (r) = false;
8883
8884 if (expanded_types)
8885 /* We're on the Ith parameter of the function parameter
8886 pack. */
8887 {
8888 /* Get the Ith type. */
8889 type = TREE_VEC_ELT (expanded_types, i);
8890
8891 if (DECL_NAME (r))
8892 /* Rename the parameter to include the index. */
8893 DECL_NAME (r) =
8894 make_ith_pack_parameter_name (DECL_NAME (r), i);
8895 }
8896 else if (!type)
8897 /* We're dealing with a normal parameter. */
8898 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8899
8900 type = type_decays_to (type);
8901 TREE_TYPE (r) = type;
8902 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8903
8904 if (DECL_INITIAL (r))
8905 {
8906 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
8907 DECL_INITIAL (r) = TREE_TYPE (r);
8908 else
8909 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
8910 complain, in_decl);
8911 }
8912
8913 DECL_CONTEXT (r) = NULL_TREE;
8914
8915 if (!DECL_TEMPLATE_PARM_P (r))
8916 DECL_ARG_TYPE (r) = type_passed_as (type);
8917
8918 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8919 args, complain, in_decl);
8920
8921 /* Keep track of the first new parameter we
8922 generate. That's what will be returned to the
8923 caller. */
8924 if (!first_r)
8925 first_r = r;
8926
8927 /* Build a proper chain of parameters when substituting
8928 into a function parameter pack. */
8929 if (prev_r)
8930 TREE_CHAIN (prev_r) = r;
8931 }
8932
8933 if (TREE_CHAIN (t))
8934 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
8935 complain, TREE_CHAIN (t));
8936
8937 /* FIRST_R contains the start of the chain we've built. */
8938 r = first_r;
8939 }
8940 break;
8941
8942 case FIELD_DECL:
8943 {
8944 tree type;
8945
8946 r = copy_decl (t);
8947 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8948 if (type == error_mark_node)
8949 return error_mark_node;
8950 TREE_TYPE (r) = type;
8951 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8952
8953 /* DECL_INITIAL gives the number of bits in a bit-field. */
8954 DECL_INITIAL (r)
8955 = tsubst_expr (DECL_INITIAL (t), args,
8956 complain, in_decl,
8957 /*integral_constant_expression_p=*/true);
8958 /* We don't have to set DECL_CONTEXT here; it is set by
8959 finish_member_declaration. */
8960 TREE_CHAIN (r) = NULL_TREE;
8961 if (VOID_TYPE_P (type))
8962 error ("instantiation of %q+D as type %qT", r, type);
8963
8964 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8965 args, complain, in_decl);
8966 }
8967 break;
8968
8969 case USING_DECL:
8970 /* We reach here only for member using decls. */
8971 if (DECL_DEPENDENT_P (t))
8972 {
8973 r = do_class_using_decl
8974 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
8975 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
8976 if (!r)
8977 r = error_mark_node;
8978 else
8979 {
8980 TREE_PROTECTED (r) = TREE_PROTECTED (t);
8981 TREE_PRIVATE (r) = TREE_PRIVATE (t);
8982 }
8983 }
8984 else
8985 {
8986 r = copy_node (t);
8987 TREE_CHAIN (r) = NULL_TREE;
8988 }
8989 break;
8990
8991 case TYPE_DECL:
8992 case VAR_DECL:
8993 {
8994 tree argvec = NULL_TREE;
8995 tree gen_tmpl = NULL_TREE;
8996 tree spec;
8997 tree tmpl = NULL_TREE;
8998 tree ctx;
8999 tree type = NULL_TREE;
9000 bool local_p;
9001
9002 if (TREE_CODE (t) == TYPE_DECL
9003 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9004 {
9005 /* If this is the canonical decl, we don't have to
9006 mess with instantiations, and often we can't (for
9007 typename, template type parms and such). Note that
9008 TYPE_NAME is not correct for the above test if
9009 we've copied the type for a typedef. */
9010 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9011 if (type == error_mark_node)
9012 return error_mark_node;
9013 r = TYPE_NAME (type);
9014 break;
9015 }
9016
9017 /* Check to see if we already have the specialization we
9018 need. */
9019 spec = NULL_TREE;
9020 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9021 {
9022 /* T is a static data member or namespace-scope entity.
9023 We have to substitute into namespace-scope variables
9024 (even though such entities are never templates) because
9025 of cases like:
9026
9027 template <class T> void f() { extern T t; }
9028
9029 where the entity referenced is not known until
9030 instantiation time. */
9031 local_p = false;
9032 ctx = DECL_CONTEXT (t);
9033 if (DECL_CLASS_SCOPE_P (t))
9034 {
9035 ctx = tsubst_aggr_type (ctx, args,
9036 complain,
9037 in_decl, /*entering_scope=*/1);
9038 /* If CTX is unchanged, then T is in fact the
9039 specialization we want. That situation occurs when
9040 referencing a static data member within in its own
9041 class. We can use pointer equality, rather than
9042 same_type_p, because DECL_CONTEXT is always
9043 canonical. */
9044 if (ctx == DECL_CONTEXT (t))
9045 spec = t;
9046 }
9047
9048 if (!spec)
9049 {
9050 tmpl = DECL_TI_TEMPLATE (t);
9051 gen_tmpl = most_general_template (tmpl);
9052 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
9053 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9054 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9055 }
9056 }
9057 else
9058 {
9059 /* A local variable. */
9060 local_p = true;
9061 /* Subsequent calls to pushdecl will fill this in. */
9062 ctx = NULL_TREE;
9063 spec = retrieve_local_specialization (t);
9064 }
9065 /* If we already have the specialization we need, there is
9066 nothing more to do. */
9067 if (spec)
9068 {
9069 r = spec;
9070 break;
9071 }
9072
9073 /* Create a new node for the specialization we need. */
9074 r = copy_decl (t);
9075 if (type == NULL_TREE)
9076 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9077 if (TREE_CODE (r) == VAR_DECL)
9078 {
9079 /* Even if the original location is out of scope, the
9080 newly substituted one is not. */
9081 DECL_DEAD_FOR_LOCAL (r) = 0;
9082 DECL_INITIALIZED_P (r) = 0;
9083 DECL_TEMPLATE_INSTANTIATED (r) = 0;
9084 if (type == error_mark_node)
9085 return error_mark_node;
9086 if (TREE_CODE (type) == FUNCTION_TYPE)
9087 {
9088 /* It may seem that this case cannot occur, since:
9089
9090 typedef void f();
9091 void g() { f x; }
9092
9093 declares a function, not a variable. However:
9094
9095 typedef void f();
9096 template <typename T> void g() { T t; }
9097 template void g<f>();
9098
9099 is an attempt to declare a variable with function
9100 type. */
9101 error ("variable %qD has function type",
9102 /* R is not yet sufficiently initialized, so we
9103 just use its name. */
9104 DECL_NAME (r));
9105 return error_mark_node;
9106 }
9107 type = complete_type (type);
9108 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
9109 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
9110 type = check_var_type (DECL_NAME (r), type);
9111
9112 if (DECL_HAS_VALUE_EXPR_P (t))
9113 {
9114 tree ve = DECL_VALUE_EXPR (t);
9115 ve = tsubst_expr (ve, args, complain, in_decl,
9116 /*constant_expression_p=*/false);
9117 SET_DECL_VALUE_EXPR (r, ve);
9118 }
9119 }
9120 else if (DECL_SELF_REFERENCE_P (t))
9121 SET_DECL_SELF_REFERENCE_P (r);
9122 TREE_TYPE (r) = type;
9123 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9124 DECL_CONTEXT (r) = ctx;
9125 /* Clear out the mangled name and RTL for the instantiation. */
9126 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9127 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9128 SET_DECL_RTL (r, NULL_RTX);
9129 /* The initializer must not be expanded until it is required;
9130 see [temp.inst]. */
9131 DECL_INITIAL (r) = NULL_TREE;
9132 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9133 SET_DECL_RTL (r, NULL_RTX);
9134 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
9135 if (TREE_CODE (r) == VAR_DECL)
9136 {
9137 /* Possibly limit visibility based on template args. */
9138 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9139 if (DECL_VISIBILITY_SPECIFIED (t))
9140 {
9141 DECL_VISIBILITY_SPECIFIED (r) = 0;
9142 DECL_ATTRIBUTES (r)
9143 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9144 }
9145 determine_visibility (r);
9146 }
9147 /* Preserve a typedef that names a type. */
9148 else if (TREE_CODE (r) == TYPE_DECL
9149 && DECL_ORIGINAL_TYPE (t)
9150 && type != error_mark_node)
9151 {
9152 DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t),
9153 args, complain, in_decl);
9154 TREE_TYPE (r) = type = build_variant_type_copy (type);
9155 TYPE_NAME (type) = r;
9156 }
9157
9158 if (!local_p)
9159 {
9160 /* A static data member declaration is always marked
9161 external when it is declared in-class, even if an
9162 initializer is present. We mimic the non-template
9163 processing here. */
9164 DECL_EXTERNAL (r) = 1;
9165
9166 register_specialization (r, gen_tmpl, argvec, false, hash);
9167 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
9168 SET_DECL_IMPLICIT_INSTANTIATION (r);
9169 }
9170 else if (cp_unevaluated_operand)
9171 {
9172 /* We're substituting this var in a decltype outside of its
9173 scope, such as for a lambda return type. Don't add it to
9174 local_specializations, do perform auto deduction. */
9175 tree auto_node = type_uses_auto (type);
9176 tree init
9177 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
9178 /*constant_expression_p=*/false);
9179
9180 if (auto_node && init && describable_type (init))
9181 {
9182 type = do_auto_deduction (type, init, auto_node);
9183 TREE_TYPE (r) = type;
9184 }
9185 }
9186 else
9187 register_local_specialization (r, t);
9188
9189 TREE_CHAIN (r) = NULL_TREE;
9190
9191 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
9192 (int) ATTR_FLAG_TYPE_IN_PLACE,
9193 args, complain, in_decl);
9194 layout_decl (r, 0);
9195 }
9196 break;
9197
9198 default:
9199 gcc_unreachable ();
9200 }
9201
9202 /* Restore the file and line information. */
9203 input_location = saved_loc;
9204
9205 return r;
9206 }
9207
9208 /* Substitute into the ARG_TYPES of a function type. */
9209
9210 static tree
9211 tsubst_arg_types (tree arg_types,
9212 tree args,
9213 tsubst_flags_t complain,
9214 tree in_decl)
9215 {
9216 tree remaining_arg_types;
9217 tree type = NULL_TREE;
9218 int i = 1;
9219 tree expanded_args = NULL_TREE;
9220 tree default_arg;
9221
9222 if (!arg_types || arg_types == void_list_node)
9223 return arg_types;
9224
9225 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
9226 args, complain, in_decl);
9227 if (remaining_arg_types == error_mark_node)
9228 return error_mark_node;
9229
9230 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
9231 {
9232 /* For a pack expansion, perform substitution on the
9233 entire expression. Later on, we'll handle the arguments
9234 one-by-one. */
9235 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
9236 args, complain, in_decl);
9237
9238 if (TREE_CODE (expanded_args) == TREE_VEC)
9239 /* So that we'll spin through the parameters, one by one. */
9240 i = TREE_VEC_LENGTH (expanded_args);
9241 else
9242 {
9243 /* We only partially substituted into the parameter
9244 pack. Our type is TYPE_PACK_EXPANSION. */
9245 type = expanded_args;
9246 expanded_args = NULL_TREE;
9247 }
9248 }
9249
9250 while (i > 0) {
9251 --i;
9252
9253 if (expanded_args)
9254 type = TREE_VEC_ELT (expanded_args, i);
9255 else if (!type)
9256 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
9257
9258 if (type == error_mark_node)
9259 return error_mark_node;
9260 if (VOID_TYPE_P (type))
9261 {
9262 if (complain & tf_error)
9263 {
9264 error ("invalid parameter type %qT", type);
9265 if (in_decl)
9266 error ("in declaration %q+D", in_decl);
9267 }
9268 return error_mark_node;
9269 }
9270
9271 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9272 top-level qualifiers as required. */
9273 type = TYPE_MAIN_VARIANT (type_decays_to (type));
9274
9275 /* We do not substitute into default arguments here. The standard
9276 mandates that they be instantiated only when needed, which is
9277 done in build_over_call. */
9278 default_arg = TREE_PURPOSE (arg_types);
9279
9280 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
9281 {
9282 /* We've instantiated a template before its default arguments
9283 have been parsed. This can happen for a nested template
9284 class, and is not an error unless we require the default
9285 argument in a call of this function. */
9286 remaining_arg_types =
9287 tree_cons (default_arg, type, remaining_arg_types);
9288 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
9289 remaining_arg_types);
9290 }
9291 else
9292 remaining_arg_types =
9293 hash_tree_cons (default_arg, type, remaining_arg_types);
9294 }
9295
9296 return remaining_arg_types;
9297 }
9298
9299 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9300 *not* handle the exception-specification for FNTYPE, because the
9301 initial substitution of explicitly provided template parameters
9302 during argument deduction forbids substitution into the
9303 exception-specification:
9304
9305 [temp.deduct]
9306
9307 All references in the function type of the function template to the
9308 corresponding template parameters are replaced by the specified tem-
9309 plate argument values. If a substitution in a template parameter or
9310 in the function type of the function template results in an invalid
9311 type, type deduction fails. [Note: The equivalent substitution in
9312 exception specifications is done only when the function is instanti-
9313 ated, at which point a program is ill-formed if the substitution
9314 results in an invalid type.] */
9315
9316 static tree
9317 tsubst_function_type (tree t,
9318 tree args,
9319 tsubst_flags_t complain,
9320 tree in_decl)
9321 {
9322 tree return_type;
9323 tree arg_types;
9324 tree fntype;
9325
9326 /* The TYPE_CONTEXT is not used for function/method types. */
9327 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
9328
9329 /* Substitute the return type. */
9330 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9331 if (return_type == error_mark_node)
9332 return error_mark_node;
9333 /* The standard does not presently indicate that creation of a
9334 function type with an invalid return type is a deduction failure.
9335 However, that is clearly analogous to creating an array of "void"
9336 or a reference to a reference. This is core issue #486. */
9337 if (TREE_CODE (return_type) == ARRAY_TYPE
9338 || TREE_CODE (return_type) == FUNCTION_TYPE)
9339 {
9340 if (complain & tf_error)
9341 {
9342 if (TREE_CODE (return_type) == ARRAY_TYPE)
9343 error ("function returning an array");
9344 else
9345 error ("function returning a function");
9346 }
9347 return error_mark_node;
9348 }
9349
9350 /* Substitute the argument types. */
9351 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
9352 complain, in_decl);
9353 if (arg_types == error_mark_node)
9354 return error_mark_node;
9355
9356 /* Construct a new type node and return it. */
9357 if (TREE_CODE (t) == FUNCTION_TYPE)
9358 fntype = build_function_type (return_type, arg_types);
9359 else
9360 {
9361 tree r = TREE_TYPE (TREE_VALUE (arg_types));
9362 if (! MAYBE_CLASS_TYPE_P (r))
9363 {
9364 /* [temp.deduct]
9365
9366 Type deduction may fail for any of the following
9367 reasons:
9368
9369 -- Attempting to create "pointer to member of T" when T
9370 is not a class type. */
9371 if (complain & tf_error)
9372 error ("creating pointer to member function of non-class type %qT",
9373 r);
9374 return error_mark_node;
9375 }
9376
9377 fntype = build_method_type_directly (r, return_type,
9378 TREE_CHAIN (arg_types));
9379 }
9380 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
9381 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
9382
9383 return fntype;
9384 }
9385
9386 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9387 ARGS into that specification, and return the substituted
9388 specification. If there is no specification, return NULL_TREE. */
9389
9390 static tree
9391 tsubst_exception_specification (tree fntype,
9392 tree args,
9393 tsubst_flags_t complain,
9394 tree in_decl)
9395 {
9396 tree specs;
9397 tree new_specs;
9398
9399 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9400 new_specs = NULL_TREE;
9401 if (specs)
9402 {
9403 if (! TREE_VALUE (specs))
9404 new_specs = specs;
9405 else
9406 while (specs)
9407 {
9408 tree spec;
9409 int i, len = 1;
9410 tree expanded_specs = NULL_TREE;
9411
9412 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9413 {
9414 /* Expand the pack expansion type. */
9415 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9416 args, complain,
9417 in_decl);
9418
9419 if (expanded_specs == error_mark_node)
9420 return error_mark_node;
9421 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9422 len = TREE_VEC_LENGTH (expanded_specs);
9423 else
9424 {
9425 /* We're substituting into a member template, so
9426 we got a TYPE_PACK_EXPANSION back. Add that
9427 expansion and move on. */
9428 gcc_assert (TREE_CODE (expanded_specs)
9429 == TYPE_PACK_EXPANSION);
9430 new_specs = add_exception_specifier (new_specs,
9431 expanded_specs,
9432 complain);
9433 specs = TREE_CHAIN (specs);
9434 continue;
9435 }
9436 }
9437
9438 for (i = 0; i < len; ++i)
9439 {
9440 if (expanded_specs)
9441 spec = TREE_VEC_ELT (expanded_specs, i);
9442 else
9443 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9444 if (spec == error_mark_node)
9445 return spec;
9446 new_specs = add_exception_specifier (new_specs, spec,
9447 complain);
9448 }
9449
9450 specs = TREE_CHAIN (specs);
9451 }
9452 }
9453 return new_specs;
9454 }
9455
9456 /* Take the tree structure T and replace template parameters used
9457 therein with the argument vector ARGS. IN_DECL is an associated
9458 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9459 Issue error and warning messages under control of COMPLAIN. Note
9460 that we must be relatively non-tolerant of extensions here, in
9461 order to preserve conformance; if we allow substitutions that
9462 should not be allowed, we may allow argument deductions that should
9463 not succeed, and therefore report ambiguous overload situations
9464 where there are none. In theory, we could allow the substitution,
9465 but indicate that it should have failed, and allow our caller to
9466 make sure that the right thing happens, but we don't try to do this
9467 yet.
9468
9469 This function is used for dealing with types, decls and the like;
9470 for expressions, use tsubst_expr or tsubst_copy. */
9471
9472 tree
9473 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9474 {
9475 tree type, r;
9476
9477 if (t == NULL_TREE || t == error_mark_node
9478 || t == integer_type_node
9479 || t == void_type_node
9480 || t == char_type_node
9481 || t == unknown_type_node
9482 || TREE_CODE (t) == NAMESPACE_DECL)
9483 return t;
9484
9485 if (DECL_P (t))
9486 return tsubst_decl (t, args, complain);
9487
9488 if (args == NULL_TREE)
9489 return t;
9490
9491 if (TREE_CODE (t) == IDENTIFIER_NODE)
9492 type = IDENTIFIER_TYPE_VALUE (t);
9493 else
9494 type = TREE_TYPE (t);
9495
9496 gcc_assert (type != unknown_type_node);
9497
9498 /* Reuse typedefs. We need to do this to handle dependent attributes,
9499 such as attribute aligned. */
9500 if (TYPE_P (t)
9501 && TYPE_NAME (t)
9502 && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
9503 {
9504 tree decl = TYPE_NAME (t);
9505
9506 if (DECL_CLASS_SCOPE_P (decl)
9507 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
9508 && uses_template_parms (DECL_CONTEXT (decl)))
9509 {
9510 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
9511 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
9512 r = retrieve_specialization (tmpl, gen_args, 0);
9513 }
9514 else if (DECL_FUNCTION_SCOPE_P (decl)
9515 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
9516 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
9517 r = retrieve_local_specialization (decl);
9518 else
9519 /* The typedef is from a non-template context. */
9520 return t;
9521
9522 if (r)
9523 {
9524 r = TREE_TYPE (r);
9525 r = cp_build_qualified_type_real
9526 (r, cp_type_quals (t) | cp_type_quals (r),
9527 complain | tf_ignore_bad_quals);
9528 return r;
9529 }
9530 /* Else we must be instantiating the typedef, so fall through. */
9531 }
9532
9533 if (type
9534 && TREE_CODE (t) != TYPENAME_TYPE
9535 && TREE_CODE (t) != IDENTIFIER_NODE
9536 && TREE_CODE (t) != FUNCTION_TYPE
9537 && TREE_CODE (t) != METHOD_TYPE)
9538 type = tsubst (type, args, complain, in_decl);
9539 if (type == error_mark_node)
9540 return error_mark_node;
9541
9542 switch (TREE_CODE (t))
9543 {
9544 case RECORD_TYPE:
9545 case UNION_TYPE:
9546 case ENUMERAL_TYPE:
9547 return tsubst_aggr_type (t, args, complain, in_decl,
9548 /*entering_scope=*/0);
9549
9550 case ERROR_MARK:
9551 case IDENTIFIER_NODE:
9552 case VOID_TYPE:
9553 case REAL_TYPE:
9554 case COMPLEX_TYPE:
9555 case VECTOR_TYPE:
9556 case BOOLEAN_TYPE:
9557 case INTEGER_CST:
9558 case REAL_CST:
9559 case STRING_CST:
9560 return t;
9561
9562 case INTEGER_TYPE:
9563 if (t == integer_type_node)
9564 return t;
9565
9566 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
9567 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
9568 return t;
9569
9570 {
9571 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
9572
9573 max = tsubst_expr (omax, args, complain, in_decl,
9574 /*integral_constant_expression_p=*/false);
9575
9576 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
9577 needed. */
9578 if (TREE_CODE (max) == NOP_EXPR
9579 && TREE_SIDE_EFFECTS (omax)
9580 && !TREE_TYPE (max))
9581 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
9582
9583 max = fold_decl_constant_value (max);
9584
9585 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
9586 with TREE_SIDE_EFFECTS that indicates this is not an integral
9587 constant expression. */
9588 if (processing_template_decl
9589 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
9590 {
9591 gcc_assert (TREE_CODE (max) == NOP_EXPR);
9592 TREE_SIDE_EFFECTS (max) = 1;
9593 }
9594
9595 if (TREE_CODE (max) != INTEGER_CST
9596 && !at_function_scope_p ()
9597 && !TREE_SIDE_EFFECTS (max)
9598 && !value_dependent_expression_p (max))
9599 {
9600 if (complain & tf_error)
9601 error ("array bound is not an integer constant");
9602 return error_mark_node;
9603 }
9604
9605 /* [temp.deduct]
9606
9607 Type deduction may fail for any of the following
9608 reasons:
9609
9610 Attempting to create an array with a size that is
9611 zero or negative. */
9612 if (integer_zerop (max) && !(complain & tf_error))
9613 /* We must fail if performing argument deduction (as
9614 indicated by the state of complain), so that
9615 another substitution can be found. */
9616 return error_mark_node;
9617 else if (TREE_CODE (max) == INTEGER_CST
9618 && INT_CST_LT (max, integer_zero_node))
9619 {
9620 if (complain & tf_error)
9621 error ("creating array with negative size (%qE)", max);
9622
9623 return error_mark_node;
9624 }
9625
9626 return compute_array_index_type (NULL_TREE, max);
9627 }
9628
9629 case TEMPLATE_TYPE_PARM:
9630 case TEMPLATE_TEMPLATE_PARM:
9631 case BOUND_TEMPLATE_TEMPLATE_PARM:
9632 case TEMPLATE_PARM_INDEX:
9633 {
9634 int idx;
9635 int level;
9636 int levels;
9637 tree arg = NULL_TREE;
9638
9639 r = NULL_TREE;
9640
9641 gcc_assert (TREE_VEC_LENGTH (args) > 0);
9642 template_parm_level_and_index (t, &level, &idx);
9643
9644 levels = TMPL_ARGS_DEPTH (args);
9645 if (level <= levels)
9646 {
9647 arg = TMPL_ARG (args, level, idx);
9648
9649 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
9650 /* See through ARGUMENT_PACK_SELECT arguments. */
9651 arg = ARGUMENT_PACK_SELECT_ARG (arg);
9652 }
9653
9654 if (arg == error_mark_node)
9655 return error_mark_node;
9656 else if (arg != NULL_TREE)
9657 {
9658 if (ARGUMENT_PACK_P (arg))
9659 /* If ARG is an argument pack, we don't actually want to
9660 perform a substitution here, because substitutions
9661 for argument packs are only done
9662 element-by-element. We can get to this point when
9663 substituting the type of a non-type template
9664 parameter pack, when that type actually contains
9665 template parameter packs from an outer template, e.g.,
9666
9667 template<typename... Types> struct A {
9668 template<Types... Values> struct B { };
9669 }; */
9670 return t;
9671
9672 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
9673 {
9674 int quals;
9675 gcc_assert (TYPE_P (arg));
9676
9677 /* cv-quals from the template are discarded when
9678 substituting in a function or reference type. */
9679 if (TREE_CODE (arg) == FUNCTION_TYPE
9680 || TREE_CODE (arg) == METHOD_TYPE
9681 || TREE_CODE (arg) == REFERENCE_TYPE)
9682 quals = cp_type_quals (arg);
9683 else
9684 quals = cp_type_quals (arg) | cp_type_quals (t);
9685
9686 return cp_build_qualified_type_real
9687 (arg, quals, complain | tf_ignore_bad_quals);
9688 }
9689 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9690 {
9691 /* We are processing a type constructed from a
9692 template template parameter. */
9693 tree argvec = tsubst (TYPE_TI_ARGS (t),
9694 args, complain, in_decl);
9695 if (argvec == error_mark_node)
9696 return error_mark_node;
9697
9698 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
9699 are resolving nested-types in the signature of a
9700 member function templates. Otherwise ARG is a
9701 TEMPLATE_DECL and is the real template to be
9702 instantiated. */
9703 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
9704 arg = TYPE_NAME (arg);
9705
9706 r = lookup_template_class (arg,
9707 argvec, in_decl,
9708 DECL_CONTEXT (arg),
9709 /*entering_scope=*/0,
9710 complain);
9711 return cp_build_qualified_type_real
9712 (r, TYPE_QUALS (t), complain);
9713 }
9714 else
9715 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
9716 return arg;
9717 }
9718
9719 if (level == 1)
9720 /* This can happen during the attempted tsubst'ing in
9721 unify. This means that we don't yet have any information
9722 about the template parameter in question. */
9723 return t;
9724
9725 /* If we get here, we must have been looking at a parm for a
9726 more deeply nested template. Make a new version of this
9727 template parameter, but with a lower level. */
9728 switch (TREE_CODE (t))
9729 {
9730 case TEMPLATE_TYPE_PARM:
9731 case TEMPLATE_TEMPLATE_PARM:
9732 case BOUND_TEMPLATE_TEMPLATE_PARM:
9733 if (cp_type_quals (t))
9734 {
9735 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
9736 r = cp_build_qualified_type_real
9737 (r, cp_type_quals (t),
9738 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
9739 ? tf_ignore_bad_quals : 0));
9740 }
9741 else
9742 {
9743 r = copy_type (t);
9744 TEMPLATE_TYPE_PARM_INDEX (r)
9745 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
9746 r, levels, args, complain);
9747 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
9748 TYPE_MAIN_VARIANT (r) = r;
9749 TYPE_POINTER_TO (r) = NULL_TREE;
9750 TYPE_REFERENCE_TO (r) = NULL_TREE;
9751
9752 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
9753 /* We have reduced the level of the template
9754 template parameter, but not the levels of its
9755 template parameters, so canonical_type_parameter
9756 will not be able to find the canonical template
9757 template parameter for this level. Thus, we
9758 require structural equality checking to compare
9759 TEMPLATE_TEMPLATE_PARMs. */
9760 SET_TYPE_STRUCTURAL_EQUALITY (r);
9761 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
9762 SET_TYPE_STRUCTURAL_EQUALITY (r);
9763 else
9764 TYPE_CANONICAL (r) = canonical_type_parameter (r);
9765
9766 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9767 {
9768 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
9769 complain, in_decl);
9770 if (argvec == error_mark_node)
9771 return error_mark_node;
9772
9773 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
9774 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
9775 }
9776 }
9777 break;
9778
9779 case TEMPLATE_PARM_INDEX:
9780 r = reduce_template_parm_level (t, type, levels, args, complain);
9781 break;
9782
9783 default:
9784 gcc_unreachable ();
9785 }
9786
9787 return r;
9788 }
9789
9790 case TREE_LIST:
9791 {
9792 tree purpose, value, chain;
9793
9794 if (t == void_list_node)
9795 return t;
9796
9797 purpose = TREE_PURPOSE (t);
9798 if (purpose)
9799 {
9800 purpose = tsubst (purpose, args, complain, in_decl);
9801 if (purpose == error_mark_node)
9802 return error_mark_node;
9803 }
9804 value = TREE_VALUE (t);
9805 if (value)
9806 {
9807 value = tsubst (value, args, complain, in_decl);
9808 if (value == error_mark_node)
9809 return error_mark_node;
9810 }
9811 chain = TREE_CHAIN (t);
9812 if (chain && chain != void_type_node)
9813 {
9814 chain = tsubst (chain, args, complain, in_decl);
9815 if (chain == error_mark_node)
9816 return error_mark_node;
9817 }
9818 if (purpose == TREE_PURPOSE (t)
9819 && value == TREE_VALUE (t)
9820 && chain == TREE_CHAIN (t))
9821 return t;
9822 return hash_tree_cons (purpose, value, chain);
9823 }
9824
9825 case TREE_BINFO:
9826 /* We should never be tsubsting a binfo. */
9827 gcc_unreachable ();
9828
9829 case TREE_VEC:
9830 /* A vector of template arguments. */
9831 gcc_assert (!type);
9832 return tsubst_template_args (t, args, complain, in_decl);
9833
9834 case POINTER_TYPE:
9835 case REFERENCE_TYPE:
9836 {
9837 enum tree_code code;
9838
9839 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
9840 return t;
9841
9842 code = TREE_CODE (t);
9843
9844
9845 /* [temp.deduct]
9846
9847 Type deduction may fail for any of the following
9848 reasons:
9849
9850 -- Attempting to create a pointer to reference type.
9851 -- Attempting to create a reference to a reference type or
9852 a reference to void.
9853
9854 Core issue 106 says that creating a reference to a reference
9855 during instantiation is no longer a cause for failure. We
9856 only enforce this check in strict C++98 mode. */
9857 if ((TREE_CODE (type) == REFERENCE_TYPE
9858 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
9859 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
9860 {
9861 static location_t last_loc;
9862
9863 /* We keep track of the last time we issued this error
9864 message to avoid spewing a ton of messages during a
9865 single bad template instantiation. */
9866 if (complain & tf_error
9867 && last_loc != input_location)
9868 {
9869 if (TREE_CODE (type) == VOID_TYPE)
9870 error ("forming reference to void");
9871 else
9872 error ("forming %s to reference type %qT",
9873 (code == POINTER_TYPE) ? "pointer" : "reference",
9874 type);
9875 last_loc = input_location;
9876 }
9877
9878 return error_mark_node;
9879 }
9880 else if (code == POINTER_TYPE)
9881 {
9882 r = build_pointer_type (type);
9883 if (TREE_CODE (type) == METHOD_TYPE)
9884 r = build_ptrmemfunc_type (r);
9885 }
9886 else if (TREE_CODE (type) == REFERENCE_TYPE)
9887 /* In C++0x, during template argument substitution, when there is an
9888 attempt to create a reference to a reference type, reference
9889 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
9890
9891 "If a template-argument for a template-parameter T names a type
9892 that is a reference to a type A, an attempt to create the type
9893 'lvalue reference to cv T' creates the type 'lvalue reference to
9894 A,' while an attempt to create the type type rvalue reference to
9895 cv T' creates the type T"
9896 */
9897 r = cp_build_reference_type
9898 (TREE_TYPE (type),
9899 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
9900 else
9901 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
9902 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
9903
9904 if (r != error_mark_node)
9905 /* Will this ever be needed for TYPE_..._TO values? */
9906 layout_type (r);
9907
9908 return r;
9909 }
9910 case OFFSET_TYPE:
9911 {
9912 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
9913 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
9914 {
9915 /* [temp.deduct]
9916
9917 Type deduction may fail for any of the following
9918 reasons:
9919
9920 -- Attempting to create "pointer to member of T" when T
9921 is not a class type. */
9922 if (complain & tf_error)
9923 error ("creating pointer to member of non-class type %qT", r);
9924 return error_mark_node;
9925 }
9926 if (TREE_CODE (type) == REFERENCE_TYPE)
9927 {
9928 if (complain & tf_error)
9929 error ("creating pointer to member reference type %qT", type);
9930 return error_mark_node;
9931 }
9932 if (TREE_CODE (type) == VOID_TYPE)
9933 {
9934 if (complain & tf_error)
9935 error ("creating pointer to member of type void");
9936 return error_mark_node;
9937 }
9938 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
9939 if (TREE_CODE (type) == FUNCTION_TYPE)
9940 {
9941 /* The type of the implicit object parameter gets its
9942 cv-qualifiers from the FUNCTION_TYPE. */
9943 tree method_type;
9944 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
9945 cp_type_quals (type));
9946 tree memptr;
9947 method_type = build_method_type_directly (this_type,
9948 TREE_TYPE (type),
9949 TYPE_ARG_TYPES (type));
9950 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
9951 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
9952 complain);
9953 }
9954 else
9955 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
9956 TYPE_QUALS (t),
9957 complain);
9958 }
9959 case FUNCTION_TYPE:
9960 case METHOD_TYPE:
9961 {
9962 tree fntype;
9963 tree specs;
9964 fntype = tsubst_function_type (t, args, complain, in_decl);
9965 if (fntype == error_mark_node)
9966 return error_mark_node;
9967
9968 /* Substitute the exception specification. */
9969 specs = tsubst_exception_specification (t, args, complain,
9970 in_decl);
9971 if (specs == error_mark_node)
9972 return error_mark_node;
9973 if (specs)
9974 fntype = build_exception_variant (fntype, specs);
9975 return fntype;
9976 }
9977 case ARRAY_TYPE:
9978 {
9979 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
9980 if (domain == error_mark_node)
9981 return error_mark_node;
9982
9983 /* As an optimization, we avoid regenerating the array type if
9984 it will obviously be the same as T. */
9985 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9986 return t;
9987
9988 /* These checks should match the ones in grokdeclarator.
9989
9990 [temp.deduct]
9991
9992 The deduction may fail for any of the following reasons:
9993
9994 -- Attempting to create an array with an element type that
9995 is void, a function type, or a reference type, or [DR337]
9996 an abstract class type. */
9997 if (TREE_CODE (type) == VOID_TYPE
9998 || TREE_CODE (type) == FUNCTION_TYPE
9999 || TREE_CODE (type) == REFERENCE_TYPE)
10000 {
10001 if (complain & tf_error)
10002 error ("creating array of %qT", type);
10003 return error_mark_node;
10004 }
10005 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10006 {
10007 if (complain & tf_error)
10008 error ("creating array of %qT, which is an abstract class type",
10009 type);
10010 return error_mark_node;
10011 }
10012
10013 r = build_cplus_array_type (type, domain);
10014
10015 if (TYPE_USER_ALIGN (t))
10016 {
10017 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10018 TYPE_USER_ALIGN (r) = 1;
10019 }
10020
10021 return r;
10022 }
10023
10024 case PLUS_EXPR:
10025 case MINUS_EXPR:
10026 {
10027 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10028 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10029
10030 if (e1 == error_mark_node || e2 == error_mark_node)
10031 return error_mark_node;
10032
10033 return fold_build2_loc (input_location,
10034 TREE_CODE (t), TREE_TYPE (t), e1, e2);
10035 }
10036
10037 case NEGATE_EXPR:
10038 case NOP_EXPR:
10039 {
10040 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10041 if (e == error_mark_node)
10042 return error_mark_node;
10043
10044 return fold_build1_loc (input_location, TREE_CODE (t), TREE_TYPE (t), e);
10045 }
10046
10047 case TYPENAME_TYPE:
10048 {
10049 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10050 in_decl, /*entering_scope=*/1);
10051 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10052 complain, in_decl);
10053
10054 if (ctx == error_mark_node || f == error_mark_node)
10055 return error_mark_node;
10056
10057 if (!MAYBE_CLASS_TYPE_P (ctx))
10058 {
10059 if (complain & tf_error)
10060 error ("%qT is not a class, struct, or union type", ctx);
10061 return error_mark_node;
10062 }
10063 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
10064 {
10065 /* Normally, make_typename_type does not require that the CTX
10066 have complete type in order to allow things like:
10067
10068 template <class T> struct S { typename S<T>::X Y; };
10069
10070 But, such constructs have already been resolved by this
10071 point, so here CTX really should have complete type, unless
10072 it's a partial instantiation. */
10073 if (!(complain & tf_no_class_instantiations))
10074 ctx = complete_type (ctx);
10075 if (!COMPLETE_TYPE_P (ctx))
10076 {
10077 if (complain & tf_error)
10078 cxx_incomplete_type_error (NULL_TREE, ctx);
10079 return error_mark_node;
10080 }
10081 }
10082
10083 f = make_typename_type (ctx, f, typename_type,
10084 (complain & tf_error) | tf_keep_type_decl);
10085 if (f == error_mark_node)
10086 return f;
10087 if (TREE_CODE (f) == TYPE_DECL)
10088 {
10089 complain |= tf_ignore_bad_quals;
10090 f = TREE_TYPE (f);
10091 }
10092
10093 if (TREE_CODE (f) != TYPENAME_TYPE)
10094 {
10095 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
10096 error ("%qT resolves to %qT, which is not an enumeration type",
10097 t, f);
10098 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
10099 error ("%qT resolves to %qT, which is is not a class type",
10100 t, f);
10101 }
10102
10103 return cp_build_qualified_type_real
10104 (f, cp_type_quals (f) | cp_type_quals (t), complain);
10105 }
10106
10107 case UNBOUND_CLASS_TEMPLATE:
10108 {
10109 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10110 in_decl, /*entering_scope=*/1);
10111 tree name = TYPE_IDENTIFIER (t);
10112 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
10113
10114 if (ctx == error_mark_node || name == error_mark_node)
10115 return error_mark_node;
10116
10117 if (parm_list)
10118 parm_list = tsubst_template_parms (parm_list, args, complain);
10119 return make_unbound_class_template (ctx, name, parm_list, complain);
10120 }
10121
10122 case INDIRECT_REF:
10123 case ADDR_EXPR:
10124 case CALL_EXPR:
10125 gcc_unreachable ();
10126
10127 case ARRAY_REF:
10128 {
10129 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10130 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
10131 /*integral_constant_expression_p=*/false);
10132 if (e1 == error_mark_node || e2 == error_mark_node)
10133 return error_mark_node;
10134
10135 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
10136 }
10137
10138 case SCOPE_REF:
10139 {
10140 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10141 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10142 if (e1 == error_mark_node || e2 == error_mark_node)
10143 return error_mark_node;
10144
10145 return build_qualified_name (/*type=*/NULL_TREE,
10146 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
10147 }
10148
10149 case TYPEOF_TYPE:
10150 {
10151 tree type;
10152
10153 type = finish_typeof (tsubst_expr
10154 (TYPEOF_TYPE_EXPR (t), args,
10155 complain, in_decl,
10156 /*integral_constant_expression_p=*/false));
10157 return cp_build_qualified_type_real (type,
10158 cp_type_quals (t)
10159 | cp_type_quals (type),
10160 complain);
10161 }
10162
10163 case DECLTYPE_TYPE:
10164 {
10165 tree type;
10166
10167 ++cp_unevaluated_operand;
10168 ++c_inhibit_evaluation_warnings;
10169
10170 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
10171 complain, in_decl,
10172 /*integral_constant_expression_p=*/false);
10173
10174 --cp_unevaluated_operand;
10175 --c_inhibit_evaluation_warnings;
10176
10177 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
10178 type = lambda_capture_field_type (type);
10179 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
10180 type = lambda_return_type (type);
10181 else
10182 type = finish_decltype_type
10183 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
10184 return cp_build_qualified_type_real (type,
10185 cp_type_quals (t)
10186 | cp_type_quals (type),
10187 complain);
10188 }
10189
10190 case TYPE_ARGUMENT_PACK:
10191 case NONTYPE_ARGUMENT_PACK:
10192 {
10193 tree r = TYPE_P (t)
10194 ? cxx_make_type (TREE_CODE (t))
10195 : make_node (TREE_CODE (t));
10196 tree packed_out =
10197 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
10198 args,
10199 complain,
10200 in_decl);
10201 SET_ARGUMENT_PACK_ARGS (r, packed_out);
10202
10203 /* For template nontype argument packs, also substitute into
10204 the type. */
10205 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
10206 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
10207
10208 return r;
10209 }
10210 break;
10211
10212 default:
10213 sorry ("use of %qs in template",
10214 tree_code_name [(int) TREE_CODE (t)]);
10215 return error_mark_node;
10216 }
10217 }
10218
10219 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10220 type of the expression on the left-hand side of the "." or "->"
10221 operator. */
10222
10223 static tree
10224 tsubst_baselink (tree baselink, tree object_type,
10225 tree args, tsubst_flags_t complain, tree in_decl)
10226 {
10227 tree name;
10228 tree qualifying_scope;
10229 tree fns;
10230 tree optype;
10231 tree template_args = 0;
10232 bool template_id_p = false;
10233
10234 /* A baselink indicates a function from a base class. Both the
10235 BASELINK_ACCESS_BINFO and the base class referenced may
10236 indicate bases of the template class, rather than the
10237 instantiated class. In addition, lookups that were not
10238 ambiguous before may be ambiguous now. Therefore, we perform
10239 the lookup again. */
10240 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
10241 qualifying_scope = tsubst (qualifying_scope, args,
10242 complain, in_decl);
10243 fns = BASELINK_FUNCTIONS (baselink);
10244 optype = BASELINK_OPTYPE (baselink);
10245 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
10246 {
10247 template_id_p = true;
10248 template_args = TREE_OPERAND (fns, 1);
10249 fns = TREE_OPERAND (fns, 0);
10250 if (template_args)
10251 template_args = tsubst_template_args (template_args, args,
10252 complain, in_decl);
10253 }
10254 name = DECL_NAME (get_first_fn (fns));
10255 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
10256
10257 /* If lookup found a single function, mark it as used at this
10258 point. (If it lookup found multiple functions the one selected
10259 later by overload resolution will be marked as used at that
10260 point.) */
10261 if (BASELINK_P (baselink))
10262 fns = BASELINK_FUNCTIONS (baselink);
10263 if (!template_id_p && !really_overloaded_fn (fns))
10264 mark_used (OVL_CURRENT (fns));
10265
10266 /* Add back the template arguments, if present. */
10267 if (BASELINK_P (baselink) && template_id_p)
10268 BASELINK_FUNCTIONS (baselink)
10269 = build_nt (TEMPLATE_ID_EXPR,
10270 BASELINK_FUNCTIONS (baselink),
10271 template_args);
10272 /* Update the conversion operator type. */
10273 BASELINK_OPTYPE (baselink)
10274 = tsubst (optype, args, complain, in_decl);
10275
10276 if (!object_type)
10277 object_type = current_class_type;
10278 return adjust_result_of_qualified_name_lookup (baselink,
10279 qualifying_scope,
10280 object_type);
10281 }
10282
10283 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10284 true if the qualified-id will be a postfix-expression in-and-of
10285 itself; false if more of the postfix-expression follows the
10286 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10287 of "&". */
10288
10289 static tree
10290 tsubst_qualified_id (tree qualified_id, tree args,
10291 tsubst_flags_t complain, tree in_decl,
10292 bool done, bool address_p)
10293 {
10294 tree expr;
10295 tree scope;
10296 tree name;
10297 bool is_template;
10298 tree template_args;
10299
10300 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
10301
10302 /* Figure out what name to look up. */
10303 name = TREE_OPERAND (qualified_id, 1);
10304 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
10305 {
10306 is_template = true;
10307 template_args = TREE_OPERAND (name, 1);
10308 if (template_args)
10309 template_args = tsubst_template_args (template_args, args,
10310 complain, in_decl);
10311 name = TREE_OPERAND (name, 0);
10312 }
10313 else
10314 {
10315 is_template = false;
10316 template_args = NULL_TREE;
10317 }
10318
10319 /* Substitute into the qualifying scope. When there are no ARGS, we
10320 are just trying to simplify a non-dependent expression. In that
10321 case the qualifying scope may be dependent, and, in any case,
10322 substituting will not help. */
10323 scope = TREE_OPERAND (qualified_id, 0);
10324 if (args)
10325 {
10326 scope = tsubst (scope, args, complain, in_decl);
10327 expr = tsubst_copy (name, args, complain, in_decl);
10328 }
10329 else
10330 expr = name;
10331
10332 if (dependent_type_p (scope))
10333 {
10334 tree type = NULL_TREE;
10335 if (DECL_P (expr) && !dependent_scope_p (scope))
10336 type = TREE_TYPE (expr);
10337 return build_qualified_name (type, scope, expr,
10338 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
10339 }
10340
10341 if (!BASELINK_P (name) && !DECL_P (expr))
10342 {
10343 if (TREE_CODE (expr) == BIT_NOT_EXPR)
10344 {
10345 /* A BIT_NOT_EXPR is used to represent a destructor. */
10346 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
10347 {
10348 error ("qualifying type %qT does not match destructor name ~%qT",
10349 scope, TREE_OPERAND (expr, 0));
10350 expr = error_mark_node;
10351 }
10352 else
10353 expr = lookup_qualified_name (scope, complete_dtor_identifier,
10354 /*is_type_p=*/0, false);
10355 }
10356 else
10357 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
10358 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
10359 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
10360 {
10361 if (complain & tf_error)
10362 {
10363 error ("dependent-name %qE is parsed as a non-type, but "
10364 "instantiation yields a type", qualified_id);
10365 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
10366 }
10367 return error_mark_node;
10368 }
10369 }
10370
10371 if (DECL_P (expr))
10372 {
10373 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
10374 scope);
10375 /* Remember that there was a reference to this entity. */
10376 mark_used (expr);
10377 }
10378
10379 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
10380 {
10381 if (complain & tf_error)
10382 qualified_name_lookup_error (scope,
10383 TREE_OPERAND (qualified_id, 1),
10384 expr, input_location);
10385 return error_mark_node;
10386 }
10387
10388 if (is_template)
10389 expr = lookup_template_function (expr, template_args);
10390
10391 if (expr == error_mark_node && complain & tf_error)
10392 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
10393 expr, input_location);
10394 else if (TYPE_P (scope))
10395 {
10396 expr = (adjust_result_of_qualified_name_lookup
10397 (expr, scope, current_class_type));
10398 expr = (finish_qualified_id_expr
10399 (scope, expr, done, address_p,
10400 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
10401 /*template_arg_p=*/false));
10402 }
10403
10404 /* Expressions do not generally have reference type. */
10405 if (TREE_CODE (expr) != SCOPE_REF
10406 /* However, if we're about to form a pointer-to-member, we just
10407 want the referenced member referenced. */
10408 && TREE_CODE (expr) != OFFSET_REF)
10409 expr = convert_from_reference (expr);
10410
10411 return expr;
10412 }
10413
10414 /* Like tsubst, but deals with expressions. This function just replaces
10415 template parms; to finish processing the resultant expression, use
10416 tsubst_expr. */
10417
10418 static tree
10419 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10420 {
10421 enum tree_code code;
10422 tree r;
10423
10424 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
10425 return t;
10426
10427 code = TREE_CODE (t);
10428
10429 switch (code)
10430 {
10431 case PARM_DECL:
10432 r = retrieve_local_specialization (t);
10433
10434 if (r == NULL)
10435 {
10436 tree c;
10437 /* This can happen for a parameter name used later in a function
10438 declaration (such as in a late-specified return type). Just
10439 make a dummy decl, since it's only used for its type. */
10440 gcc_assert (cp_unevaluated_operand != 0);
10441 /* We copy T because want to tsubst the PARM_DECL only,
10442 not the following PARM_DECLs that are chained to T. */
10443 c = copy_node (t);
10444 r = tsubst_decl (c, args, complain);
10445 /* Give it the template pattern as its context; its true context
10446 hasn't been instantiated yet and this is good enough for
10447 mangling. */
10448 DECL_CONTEXT (r) = DECL_CONTEXT (t);
10449 }
10450
10451 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10452 r = ARGUMENT_PACK_SELECT_ARG (r);
10453 mark_used (r);
10454 return r;
10455
10456 case CONST_DECL:
10457 {
10458 tree enum_type;
10459 tree v;
10460
10461 if (DECL_TEMPLATE_PARM_P (t))
10462 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
10463 /* There is no need to substitute into namespace-scope
10464 enumerators. */
10465 if (DECL_NAMESPACE_SCOPE_P (t))
10466 return t;
10467 /* If ARGS is NULL, then T is known to be non-dependent. */
10468 if (args == NULL_TREE)
10469 return integral_constant_value (t);
10470
10471 /* Unfortunately, we cannot just call lookup_name here.
10472 Consider:
10473
10474 template <int I> int f() {
10475 enum E { a = I };
10476 struct S { void g() { E e = a; } };
10477 };
10478
10479 When we instantiate f<7>::S::g(), say, lookup_name is not
10480 clever enough to find f<7>::a. */
10481 enum_type
10482 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
10483 /*entering_scope=*/0);
10484
10485 for (v = TYPE_VALUES (enum_type);
10486 v != NULL_TREE;
10487 v = TREE_CHAIN (v))
10488 if (TREE_PURPOSE (v) == DECL_NAME (t))
10489 return TREE_VALUE (v);
10490
10491 /* We didn't find the name. That should never happen; if
10492 name-lookup found it during preliminary parsing, we
10493 should find it again here during instantiation. */
10494 gcc_unreachable ();
10495 }
10496 return t;
10497
10498 case FIELD_DECL:
10499 if (DECL_CONTEXT (t))
10500 {
10501 tree ctx;
10502
10503 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
10504 /*entering_scope=*/1);
10505 if (ctx != DECL_CONTEXT (t))
10506 {
10507 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
10508 if (!r)
10509 {
10510 if (complain & tf_error)
10511 error ("using invalid field %qD", t);
10512 return error_mark_node;
10513 }
10514 return r;
10515 }
10516 }
10517
10518 return t;
10519
10520 case VAR_DECL:
10521 case FUNCTION_DECL:
10522 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10523 || local_variable_p (t))
10524 t = tsubst (t, args, complain, in_decl);
10525 mark_used (t);
10526 return t;
10527
10528 case BASELINK:
10529 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
10530
10531 case TEMPLATE_DECL:
10532 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10533 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
10534 args, complain, in_decl);
10535 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
10536 return tsubst (t, args, complain, in_decl);
10537 else if (DECL_CLASS_SCOPE_P (t)
10538 && uses_template_parms (DECL_CONTEXT (t)))
10539 {
10540 /* Template template argument like the following example need
10541 special treatment:
10542
10543 template <template <class> class TT> struct C {};
10544 template <class T> struct D {
10545 template <class U> struct E {};
10546 C<E> c; // #1
10547 };
10548 D<int> d; // #2
10549
10550 We are processing the template argument `E' in #1 for
10551 the template instantiation #2. Originally, `E' is a
10552 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10553 have to substitute this with one having context `D<int>'. */
10554
10555 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
10556 return lookup_field (context, DECL_NAME(t), 0, false);
10557 }
10558 else
10559 /* Ordinary template template argument. */
10560 return t;
10561
10562 case CAST_EXPR:
10563 case REINTERPRET_CAST_EXPR:
10564 case CONST_CAST_EXPR:
10565 case STATIC_CAST_EXPR:
10566 case DYNAMIC_CAST_EXPR:
10567 case NOP_EXPR:
10568 return build1
10569 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10570 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10571
10572 case SIZEOF_EXPR:
10573 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10574 {
10575 /* We only want to compute the number of arguments. */
10576 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10577 complain, in_decl);
10578 int len = 0;
10579
10580 if (TREE_CODE (expanded) == TREE_VEC)
10581 len = TREE_VEC_LENGTH (expanded);
10582
10583 if (expanded == error_mark_node)
10584 return error_mark_node;
10585 else if (PACK_EXPANSION_P (expanded)
10586 || (TREE_CODE (expanded) == TREE_VEC
10587 && len > 0
10588 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
10589 {
10590 if (TREE_CODE (expanded) == TREE_VEC)
10591 expanded = TREE_VEC_ELT (expanded, len - 1);
10592
10593 if (TYPE_P (expanded))
10594 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
10595 complain & tf_error);
10596 else
10597 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
10598 complain & tf_error);
10599 }
10600 else
10601 return build_int_cst (size_type_node, len);
10602 }
10603 /* Fall through */
10604
10605 case INDIRECT_REF:
10606 case NEGATE_EXPR:
10607 case TRUTH_NOT_EXPR:
10608 case BIT_NOT_EXPR:
10609 case ADDR_EXPR:
10610 case UNARY_PLUS_EXPR: /* Unary + */
10611 case ALIGNOF_EXPR:
10612 case ARROW_EXPR:
10613 case THROW_EXPR:
10614 case TYPEID_EXPR:
10615 case REALPART_EXPR:
10616 case IMAGPART_EXPR:
10617 return build1
10618 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10619 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10620
10621 case COMPONENT_REF:
10622 {
10623 tree object;
10624 tree name;
10625
10626 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
10627 name = TREE_OPERAND (t, 1);
10628 if (TREE_CODE (name) == BIT_NOT_EXPR)
10629 {
10630 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10631 complain, in_decl);
10632 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10633 }
10634 else if (TREE_CODE (name) == SCOPE_REF
10635 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
10636 {
10637 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
10638 complain, in_decl);
10639 name = TREE_OPERAND (name, 1);
10640 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10641 complain, in_decl);
10642 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10643 name = build_qualified_name (/*type=*/NULL_TREE,
10644 base, name,
10645 /*template_p=*/false);
10646 }
10647 else if (TREE_CODE (name) == BASELINK)
10648 name = tsubst_baselink (name,
10649 non_reference (TREE_TYPE (object)),
10650 args, complain,
10651 in_decl);
10652 else
10653 name = tsubst_copy (name, args, complain, in_decl);
10654 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
10655 }
10656
10657 case PLUS_EXPR:
10658 case MINUS_EXPR:
10659 case MULT_EXPR:
10660 case TRUNC_DIV_EXPR:
10661 case CEIL_DIV_EXPR:
10662 case FLOOR_DIV_EXPR:
10663 case ROUND_DIV_EXPR:
10664 case EXACT_DIV_EXPR:
10665 case BIT_AND_EXPR:
10666 case BIT_IOR_EXPR:
10667 case BIT_XOR_EXPR:
10668 case TRUNC_MOD_EXPR:
10669 case FLOOR_MOD_EXPR:
10670 case TRUTH_ANDIF_EXPR:
10671 case TRUTH_ORIF_EXPR:
10672 case TRUTH_AND_EXPR:
10673 case TRUTH_OR_EXPR:
10674 case RSHIFT_EXPR:
10675 case LSHIFT_EXPR:
10676 case RROTATE_EXPR:
10677 case LROTATE_EXPR:
10678 case EQ_EXPR:
10679 case NE_EXPR:
10680 case MAX_EXPR:
10681 case MIN_EXPR:
10682 case LE_EXPR:
10683 case GE_EXPR:
10684 case LT_EXPR:
10685 case GT_EXPR:
10686 case COMPOUND_EXPR:
10687 case DOTSTAR_EXPR:
10688 case MEMBER_REF:
10689 case PREDECREMENT_EXPR:
10690 case PREINCREMENT_EXPR:
10691 case POSTDECREMENT_EXPR:
10692 case POSTINCREMENT_EXPR:
10693 return build_nt
10694 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10695 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10696
10697 case SCOPE_REF:
10698 return build_qualified_name (/*type=*/NULL_TREE,
10699 tsubst_copy (TREE_OPERAND (t, 0),
10700 args, complain, in_decl),
10701 tsubst_copy (TREE_OPERAND (t, 1),
10702 args, complain, in_decl),
10703 QUALIFIED_NAME_IS_TEMPLATE (t));
10704
10705 case ARRAY_REF:
10706 return build_nt
10707 (ARRAY_REF,
10708 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10709 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10710 NULL_TREE, NULL_TREE);
10711
10712 case CALL_EXPR:
10713 {
10714 int n = VL_EXP_OPERAND_LENGTH (t);
10715 tree result = build_vl_exp (CALL_EXPR, n);
10716 int i;
10717 for (i = 0; i < n; i++)
10718 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
10719 complain, in_decl);
10720 return result;
10721 }
10722
10723 case COND_EXPR:
10724 case MODOP_EXPR:
10725 case PSEUDO_DTOR_EXPR:
10726 {
10727 r = build_nt
10728 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10729 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10730 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10731 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
10732 return r;
10733 }
10734
10735 case NEW_EXPR:
10736 {
10737 r = build_nt
10738 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10739 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10740 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10741 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
10742 return r;
10743 }
10744
10745 case DELETE_EXPR:
10746 {
10747 r = build_nt
10748 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10749 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10750 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
10751 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
10752 return r;
10753 }
10754
10755 case TEMPLATE_ID_EXPR:
10756 {
10757 /* Substituted template arguments */
10758 tree fn = TREE_OPERAND (t, 0);
10759 tree targs = TREE_OPERAND (t, 1);
10760
10761 fn = tsubst_copy (fn, args, complain, in_decl);
10762 if (targs)
10763 targs = tsubst_template_args (targs, args, complain, in_decl);
10764
10765 return lookup_template_function (fn, targs);
10766 }
10767
10768 case TREE_LIST:
10769 {
10770 tree purpose, value, chain;
10771
10772 if (t == void_list_node)
10773 return t;
10774
10775 purpose = TREE_PURPOSE (t);
10776 if (purpose)
10777 purpose = tsubst_copy (purpose, args, complain, in_decl);
10778 value = TREE_VALUE (t);
10779 if (value)
10780 value = tsubst_copy (value, args, complain, in_decl);
10781 chain = TREE_CHAIN (t);
10782 if (chain && chain != void_type_node)
10783 chain = tsubst_copy (chain, args, complain, in_decl);
10784 if (purpose == TREE_PURPOSE (t)
10785 && value == TREE_VALUE (t)
10786 && chain == TREE_CHAIN (t))
10787 return t;
10788 return tree_cons (purpose, value, chain);
10789 }
10790
10791 case RECORD_TYPE:
10792 case UNION_TYPE:
10793 case ENUMERAL_TYPE:
10794 case INTEGER_TYPE:
10795 case TEMPLATE_TYPE_PARM:
10796 case TEMPLATE_TEMPLATE_PARM:
10797 case BOUND_TEMPLATE_TEMPLATE_PARM:
10798 case TEMPLATE_PARM_INDEX:
10799 case POINTER_TYPE:
10800 case REFERENCE_TYPE:
10801 case OFFSET_TYPE:
10802 case FUNCTION_TYPE:
10803 case METHOD_TYPE:
10804 case ARRAY_TYPE:
10805 case TYPENAME_TYPE:
10806 case UNBOUND_CLASS_TEMPLATE:
10807 case TYPEOF_TYPE:
10808 case DECLTYPE_TYPE:
10809 case TYPE_DECL:
10810 return tsubst (t, args, complain, in_decl);
10811
10812 case IDENTIFIER_NODE:
10813 if (IDENTIFIER_TYPENAME_P (t))
10814 {
10815 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10816 return mangle_conv_op_name_for_type (new_type);
10817 }
10818 else
10819 return t;
10820
10821 case CONSTRUCTOR:
10822 /* This is handled by tsubst_copy_and_build. */
10823 gcc_unreachable ();
10824
10825 case VA_ARG_EXPR:
10826 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
10827 in_decl),
10828 tsubst (TREE_TYPE (t), args, complain, in_decl));
10829
10830 case CLEANUP_POINT_EXPR:
10831 /* We shouldn't have built any of these during initial template
10832 generation. Instead, they should be built during instantiation
10833 in response to the saved STMT_IS_FULL_EXPR_P setting. */
10834 gcc_unreachable ();
10835
10836 case OFFSET_REF:
10837 mark_used (TREE_OPERAND (t, 1));
10838 return t;
10839
10840 case EXPR_PACK_EXPANSION:
10841 error ("invalid use of pack expansion expression");
10842 return error_mark_node;
10843
10844 case NONTYPE_ARGUMENT_PACK:
10845 error ("use %<...%> to expand argument pack");
10846 return error_mark_node;
10847
10848 default:
10849 return t;
10850 }
10851 }
10852
10853 /* Like tsubst_copy, but specifically for OpenMP clauses. */
10854
10855 static tree
10856 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
10857 tree in_decl)
10858 {
10859 tree new_clauses = NULL, nc, oc;
10860
10861 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
10862 {
10863 nc = copy_node (oc);
10864 OMP_CLAUSE_CHAIN (nc) = new_clauses;
10865 new_clauses = nc;
10866
10867 switch (OMP_CLAUSE_CODE (nc))
10868 {
10869 case OMP_CLAUSE_LASTPRIVATE:
10870 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
10871 {
10872 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
10873 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
10874 in_decl, /*integral_constant_expression_p=*/false);
10875 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
10876 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
10877 }
10878 /* FALLTHRU */
10879 case OMP_CLAUSE_PRIVATE:
10880 case OMP_CLAUSE_SHARED:
10881 case OMP_CLAUSE_FIRSTPRIVATE:
10882 case OMP_CLAUSE_REDUCTION:
10883 case OMP_CLAUSE_COPYIN:
10884 case OMP_CLAUSE_COPYPRIVATE:
10885 case OMP_CLAUSE_IF:
10886 case OMP_CLAUSE_NUM_THREADS:
10887 case OMP_CLAUSE_SCHEDULE:
10888 case OMP_CLAUSE_COLLAPSE:
10889 OMP_CLAUSE_OPERAND (nc, 0)
10890 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
10891 in_decl, /*integral_constant_expression_p=*/false);
10892 break;
10893 case OMP_CLAUSE_NOWAIT:
10894 case OMP_CLAUSE_ORDERED:
10895 case OMP_CLAUSE_DEFAULT:
10896 case OMP_CLAUSE_UNTIED:
10897 break;
10898 default:
10899 gcc_unreachable ();
10900 }
10901 }
10902
10903 return finish_omp_clauses (nreverse (new_clauses));
10904 }
10905
10906 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
10907
10908 static tree
10909 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
10910 tree in_decl)
10911 {
10912 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
10913
10914 tree purpose, value, chain;
10915
10916 if (t == NULL)
10917 return t;
10918
10919 if (TREE_CODE (t) != TREE_LIST)
10920 return tsubst_copy_and_build (t, args, complain, in_decl,
10921 /*function_p=*/false,
10922 /*integral_constant_expression_p=*/false);
10923
10924 if (t == void_list_node)
10925 return t;
10926
10927 purpose = TREE_PURPOSE (t);
10928 if (purpose)
10929 purpose = RECUR (purpose);
10930 value = TREE_VALUE (t);
10931 if (value && TREE_CODE (value) != LABEL_DECL)
10932 value = RECUR (value);
10933 chain = TREE_CHAIN (t);
10934 if (chain && chain != void_type_node)
10935 chain = RECUR (chain);
10936 return tree_cons (purpose, value, chain);
10937 #undef RECUR
10938 }
10939
10940 /* Substitute one OMP_FOR iterator. */
10941
10942 static void
10943 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
10944 tree condv, tree incrv, tree *clauses,
10945 tree args, tsubst_flags_t complain, tree in_decl,
10946 bool integral_constant_expression_p)
10947 {
10948 #define RECUR(NODE) \
10949 tsubst_expr ((NODE), args, complain, in_decl, \
10950 integral_constant_expression_p)
10951 tree decl, init, cond, incr, auto_node;
10952
10953 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
10954 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
10955 decl = RECUR (TREE_OPERAND (init, 0));
10956 init = TREE_OPERAND (init, 1);
10957 auto_node = type_uses_auto (TREE_TYPE (decl));
10958 if (auto_node && init)
10959 {
10960 tree init_expr = init;
10961 if (TREE_CODE (init_expr) == DECL_EXPR)
10962 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
10963 init_expr = RECUR (init_expr);
10964 TREE_TYPE (decl)
10965 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
10966 }
10967 gcc_assert (!type_dependent_expression_p (decl));
10968
10969 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
10970 {
10971 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
10972 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
10973 if (TREE_CODE (incr) == MODIFY_EXPR)
10974 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
10975 RECUR (TREE_OPERAND (incr, 1)),
10976 complain);
10977 else
10978 incr = RECUR (incr);
10979 TREE_VEC_ELT (declv, i) = decl;
10980 TREE_VEC_ELT (initv, i) = init;
10981 TREE_VEC_ELT (condv, i) = cond;
10982 TREE_VEC_ELT (incrv, i) = incr;
10983 return;
10984 }
10985
10986 if (init && TREE_CODE (init) != DECL_EXPR)
10987 {
10988 tree c;
10989 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10990 {
10991 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
10992 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10993 && OMP_CLAUSE_DECL (c) == decl)
10994 break;
10995 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10996 && OMP_CLAUSE_DECL (c) == decl)
10997 error ("iteration variable %qD should not be firstprivate", decl);
10998 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
10999 && OMP_CLAUSE_DECL (c) == decl)
11000 error ("iteration variable %qD should not be reduction", decl);
11001 }
11002 if (c == NULL)
11003 {
11004 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11005 OMP_CLAUSE_DECL (c) = decl;
11006 c = finish_omp_clauses (c);
11007 if (c)
11008 {
11009 OMP_CLAUSE_CHAIN (c) = *clauses;
11010 *clauses = c;
11011 }
11012 }
11013 }
11014 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
11015 if (COMPARISON_CLASS_P (cond))
11016 cond = build2 (TREE_CODE (cond), boolean_type_node,
11017 RECUR (TREE_OPERAND (cond, 0)),
11018 RECUR (TREE_OPERAND (cond, 1)));
11019 else
11020 cond = RECUR (cond);
11021 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11022 switch (TREE_CODE (incr))
11023 {
11024 case PREINCREMENT_EXPR:
11025 case PREDECREMENT_EXPR:
11026 case POSTINCREMENT_EXPR:
11027 case POSTDECREMENT_EXPR:
11028 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
11029 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
11030 break;
11031 case MODIFY_EXPR:
11032 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11033 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11034 {
11035 tree rhs = TREE_OPERAND (incr, 1);
11036 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11037 RECUR (TREE_OPERAND (incr, 0)),
11038 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11039 RECUR (TREE_OPERAND (rhs, 0)),
11040 RECUR (TREE_OPERAND (rhs, 1))));
11041 }
11042 else
11043 incr = RECUR (incr);
11044 break;
11045 case MODOP_EXPR:
11046 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11047 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11048 {
11049 tree lhs = RECUR (TREE_OPERAND (incr, 0));
11050 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
11051 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
11052 TREE_TYPE (decl), lhs,
11053 RECUR (TREE_OPERAND (incr, 2))));
11054 }
11055 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
11056 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
11057 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
11058 {
11059 tree rhs = TREE_OPERAND (incr, 2);
11060 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11061 RECUR (TREE_OPERAND (incr, 0)),
11062 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11063 RECUR (TREE_OPERAND (rhs, 0)),
11064 RECUR (TREE_OPERAND (rhs, 1))));
11065 }
11066 else
11067 incr = RECUR (incr);
11068 break;
11069 default:
11070 incr = RECUR (incr);
11071 break;
11072 }
11073
11074 TREE_VEC_ELT (declv, i) = decl;
11075 TREE_VEC_ELT (initv, i) = init;
11076 TREE_VEC_ELT (condv, i) = cond;
11077 TREE_VEC_ELT (incrv, i) = incr;
11078 #undef RECUR
11079 }
11080
11081 /* Like tsubst_copy for expressions, etc. but also does semantic
11082 processing. */
11083
11084 static tree
11085 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
11086 bool integral_constant_expression_p)
11087 {
11088 #define RECUR(NODE) \
11089 tsubst_expr ((NODE), args, complain, in_decl, \
11090 integral_constant_expression_p)
11091
11092 tree stmt, tmp;
11093
11094 if (t == NULL_TREE || t == error_mark_node)
11095 return t;
11096
11097 if (EXPR_HAS_LOCATION (t))
11098 input_location = EXPR_LOCATION (t);
11099 if (STATEMENT_CODE_P (TREE_CODE (t)))
11100 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
11101
11102 switch (TREE_CODE (t))
11103 {
11104 case STATEMENT_LIST:
11105 {
11106 tree_stmt_iterator i;
11107 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
11108 RECUR (tsi_stmt (i));
11109 break;
11110 }
11111
11112 case CTOR_INITIALIZER:
11113 finish_mem_initializers (tsubst_initializer_list
11114 (TREE_OPERAND (t, 0), args));
11115 break;
11116
11117 case RETURN_EXPR:
11118 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
11119 break;
11120
11121 case EXPR_STMT:
11122 tmp = RECUR (EXPR_STMT_EXPR (t));
11123 if (EXPR_STMT_STMT_EXPR_RESULT (t))
11124 finish_stmt_expr_expr (tmp, cur_stmt_expr);
11125 else
11126 finish_expr_stmt (tmp);
11127 break;
11128
11129 case USING_STMT:
11130 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
11131 break;
11132
11133 case DECL_EXPR:
11134 {
11135 tree decl;
11136 tree init;
11137
11138 decl = DECL_EXPR_DECL (t);
11139 if (TREE_CODE (decl) == LABEL_DECL)
11140 finish_label_decl (DECL_NAME (decl));
11141 else if (TREE_CODE (decl) == USING_DECL)
11142 {
11143 tree scope = USING_DECL_SCOPE (decl);
11144 tree name = DECL_NAME (decl);
11145 tree decl;
11146
11147 scope = RECUR (scope);
11148 decl = lookup_qualified_name (scope, name,
11149 /*is_type_p=*/false,
11150 /*complain=*/false);
11151 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
11152 qualified_name_lookup_error (scope, name, decl, input_location);
11153 else
11154 do_local_using_decl (decl, scope, name);
11155 }
11156 else
11157 {
11158 init = DECL_INITIAL (decl);
11159 decl = tsubst (decl, args, complain, in_decl);
11160 if (decl != error_mark_node)
11161 {
11162 /* By marking the declaration as instantiated, we avoid
11163 trying to instantiate it. Since instantiate_decl can't
11164 handle local variables, and since we've already done
11165 all that needs to be done, that's the right thing to
11166 do. */
11167 if (TREE_CODE (decl) == VAR_DECL)
11168 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11169 if (TREE_CODE (decl) == VAR_DECL
11170 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
11171 /* Anonymous aggregates are a special case. */
11172 finish_anon_union (decl);
11173 else
11174 {
11175 maybe_push_decl (decl);
11176 if (TREE_CODE (decl) == VAR_DECL
11177 && DECL_PRETTY_FUNCTION_P (decl))
11178 {
11179 /* For __PRETTY_FUNCTION__ we have to adjust the
11180 initializer. */
11181 const char *const name
11182 = cxx_printable_name (current_function_decl, 2);
11183 init = cp_fname_init (name, &TREE_TYPE (decl));
11184 }
11185 else
11186 {
11187 tree t = RECUR (init);
11188
11189 if (init && !t)
11190 /* If we had an initializer but it
11191 instantiated to nothing,
11192 value-initialize the object. This will
11193 only occur when the initializer was a
11194 pack expansion where the parameter packs
11195 used in that expansion were of length
11196 zero. */
11197 init = build_value_init (TREE_TYPE (decl));
11198 else
11199 init = t;
11200 }
11201
11202 cp_finish_decl (decl, init, false, NULL_TREE, 0);
11203 }
11204 }
11205 }
11206
11207 /* A DECL_EXPR can also be used as an expression, in the condition
11208 clause of an if/for/while construct. */
11209 return decl;
11210 }
11211
11212 case FOR_STMT:
11213 stmt = begin_for_stmt ();
11214 RECUR (FOR_INIT_STMT (t));
11215 finish_for_init_stmt (stmt);
11216 tmp = RECUR (FOR_COND (t));
11217 finish_for_cond (tmp, stmt);
11218 tmp = RECUR (FOR_EXPR (t));
11219 finish_for_expr (tmp, stmt);
11220 RECUR (FOR_BODY (t));
11221 finish_for_stmt (stmt);
11222 break;
11223
11224 case WHILE_STMT:
11225 stmt = begin_while_stmt ();
11226 tmp = RECUR (WHILE_COND (t));
11227 finish_while_stmt_cond (tmp, stmt);
11228 RECUR (WHILE_BODY (t));
11229 finish_while_stmt (stmt);
11230 break;
11231
11232 case DO_STMT:
11233 stmt = begin_do_stmt ();
11234 RECUR (DO_BODY (t));
11235 finish_do_body (stmt);
11236 tmp = RECUR (DO_COND (t));
11237 finish_do_stmt (tmp, stmt);
11238 break;
11239
11240 case IF_STMT:
11241 stmt = begin_if_stmt ();
11242 tmp = RECUR (IF_COND (t));
11243 finish_if_stmt_cond (tmp, stmt);
11244 RECUR (THEN_CLAUSE (t));
11245 finish_then_clause (stmt);
11246
11247 if (ELSE_CLAUSE (t))
11248 {
11249 begin_else_clause (stmt);
11250 RECUR (ELSE_CLAUSE (t));
11251 finish_else_clause (stmt);
11252 }
11253
11254 finish_if_stmt (stmt);
11255 break;
11256
11257 case BIND_EXPR:
11258 if (BIND_EXPR_BODY_BLOCK (t))
11259 stmt = begin_function_body ();
11260 else
11261 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
11262 ? BCS_TRY_BLOCK : 0);
11263
11264 RECUR (BIND_EXPR_BODY (t));
11265
11266 if (BIND_EXPR_BODY_BLOCK (t))
11267 finish_function_body (stmt);
11268 else
11269 finish_compound_stmt (stmt);
11270 break;
11271
11272 case BREAK_STMT:
11273 finish_break_stmt ();
11274 break;
11275
11276 case CONTINUE_STMT:
11277 finish_continue_stmt ();
11278 break;
11279
11280 case SWITCH_STMT:
11281 stmt = begin_switch_stmt ();
11282 tmp = RECUR (SWITCH_STMT_COND (t));
11283 finish_switch_cond (tmp, stmt);
11284 RECUR (SWITCH_STMT_BODY (t));
11285 finish_switch_stmt (stmt);
11286 break;
11287
11288 case CASE_LABEL_EXPR:
11289 finish_case_label (EXPR_LOCATION (t),
11290 RECUR (CASE_LOW (t)),
11291 RECUR (CASE_HIGH (t)));
11292 break;
11293
11294 case LABEL_EXPR:
11295 {
11296 tree decl = LABEL_EXPR_LABEL (t);
11297 tree label;
11298
11299 label = finish_label_stmt (DECL_NAME (decl));
11300 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
11301 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
11302 }
11303 break;
11304
11305 case GOTO_EXPR:
11306 tmp = GOTO_DESTINATION (t);
11307 if (TREE_CODE (tmp) != LABEL_DECL)
11308 /* Computed goto's must be tsubst'd into. On the other hand,
11309 non-computed gotos must not be; the identifier in question
11310 will have no binding. */
11311 tmp = RECUR (tmp);
11312 else
11313 tmp = DECL_NAME (tmp);
11314 finish_goto_stmt (tmp);
11315 break;
11316
11317 case ASM_EXPR:
11318 tmp = finish_asm_stmt
11319 (ASM_VOLATILE_P (t),
11320 RECUR (ASM_STRING (t)),
11321 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
11322 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
11323 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
11324 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
11325 {
11326 tree asm_expr = tmp;
11327 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
11328 asm_expr = TREE_OPERAND (asm_expr, 0);
11329 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
11330 }
11331 break;
11332
11333 case TRY_BLOCK:
11334 if (CLEANUP_P (t))
11335 {
11336 stmt = begin_try_block ();
11337 RECUR (TRY_STMTS (t));
11338 finish_cleanup_try_block (stmt);
11339 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
11340 }
11341 else
11342 {
11343 tree compound_stmt = NULL_TREE;
11344
11345 if (FN_TRY_BLOCK_P (t))
11346 stmt = begin_function_try_block (&compound_stmt);
11347 else
11348 stmt = begin_try_block ();
11349
11350 RECUR (TRY_STMTS (t));
11351
11352 if (FN_TRY_BLOCK_P (t))
11353 finish_function_try_block (stmt);
11354 else
11355 finish_try_block (stmt);
11356
11357 RECUR (TRY_HANDLERS (t));
11358 if (FN_TRY_BLOCK_P (t))
11359 finish_function_handler_sequence (stmt, compound_stmt);
11360 else
11361 finish_handler_sequence (stmt);
11362 }
11363 break;
11364
11365 case HANDLER:
11366 {
11367 tree decl = HANDLER_PARMS (t);
11368
11369 if (decl)
11370 {
11371 decl = tsubst (decl, args, complain, in_decl);
11372 /* Prevent instantiate_decl from trying to instantiate
11373 this variable. We've already done all that needs to be
11374 done. */
11375 if (decl != error_mark_node)
11376 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11377 }
11378 stmt = begin_handler ();
11379 finish_handler_parms (decl, stmt);
11380 RECUR (HANDLER_BODY (t));
11381 finish_handler (stmt);
11382 }
11383 break;
11384
11385 case TAG_DEFN:
11386 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
11387 break;
11388
11389 case STATIC_ASSERT:
11390 {
11391 tree condition =
11392 tsubst_expr (STATIC_ASSERT_CONDITION (t),
11393 args,
11394 complain, in_decl,
11395 /*integral_constant_expression_p=*/true);
11396 finish_static_assert (condition,
11397 STATIC_ASSERT_MESSAGE (t),
11398 STATIC_ASSERT_SOURCE_LOCATION (t),
11399 /*member_p=*/false);
11400 }
11401 break;
11402
11403 case OMP_PARALLEL:
11404 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
11405 args, complain, in_decl);
11406 stmt = begin_omp_parallel ();
11407 RECUR (OMP_PARALLEL_BODY (t));
11408 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
11409 = OMP_PARALLEL_COMBINED (t);
11410 break;
11411
11412 case OMP_TASK:
11413 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
11414 args, complain, in_decl);
11415 stmt = begin_omp_task ();
11416 RECUR (OMP_TASK_BODY (t));
11417 finish_omp_task (tmp, stmt);
11418 break;
11419
11420 case OMP_FOR:
11421 {
11422 tree clauses, body, pre_body;
11423 tree declv, initv, condv, incrv;
11424 int i;
11425
11426 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
11427 args, complain, in_decl);
11428 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11429 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11430 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11431 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11432
11433 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
11434 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
11435 &clauses, args, complain, in_decl,
11436 integral_constant_expression_p);
11437
11438 stmt = begin_omp_structured_block ();
11439
11440 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
11441 if (TREE_VEC_ELT (initv, i) == NULL
11442 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
11443 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
11444 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
11445 {
11446 tree init = RECUR (TREE_VEC_ELT (initv, i));
11447 gcc_assert (init == TREE_VEC_ELT (declv, i));
11448 TREE_VEC_ELT (initv, i) = NULL_TREE;
11449 }
11450 else
11451 {
11452 tree decl_expr = TREE_VEC_ELT (initv, i);
11453 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
11454 gcc_assert (init != NULL);
11455 TREE_VEC_ELT (initv, i) = RECUR (init);
11456 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
11457 RECUR (decl_expr);
11458 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
11459 }
11460
11461 pre_body = push_stmt_list ();
11462 RECUR (OMP_FOR_PRE_BODY (t));
11463 pre_body = pop_stmt_list (pre_body);
11464
11465 body = push_stmt_list ();
11466 RECUR (OMP_FOR_BODY (t));
11467 body = pop_stmt_list (body);
11468
11469 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
11470 body, pre_body, clauses);
11471
11472 add_stmt (finish_omp_structured_block (stmt));
11473 }
11474 break;
11475
11476 case OMP_SECTIONS:
11477 case OMP_SINGLE:
11478 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
11479 stmt = push_stmt_list ();
11480 RECUR (OMP_BODY (t));
11481 stmt = pop_stmt_list (stmt);
11482
11483 t = copy_node (t);
11484 OMP_BODY (t) = stmt;
11485 OMP_CLAUSES (t) = tmp;
11486 add_stmt (t);
11487 break;
11488
11489 case OMP_SECTION:
11490 case OMP_CRITICAL:
11491 case OMP_MASTER:
11492 case OMP_ORDERED:
11493 stmt = push_stmt_list ();
11494 RECUR (OMP_BODY (t));
11495 stmt = pop_stmt_list (stmt);
11496
11497 t = copy_node (t);
11498 OMP_BODY (t) = stmt;
11499 add_stmt (t);
11500 break;
11501
11502 case OMP_ATOMIC:
11503 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
11504 {
11505 tree op1 = TREE_OPERAND (t, 1);
11506 tree lhs = RECUR (TREE_OPERAND (op1, 0));
11507 tree rhs = RECUR (TREE_OPERAND (op1, 1));
11508 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
11509 }
11510 break;
11511
11512 case EXPR_PACK_EXPANSION:
11513 error ("invalid use of pack expansion expression");
11514 return error_mark_node;
11515
11516 case NONTYPE_ARGUMENT_PACK:
11517 error ("use %<...%> to expand argument pack");
11518 return error_mark_node;
11519
11520 default:
11521 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
11522
11523 return tsubst_copy_and_build (t, args, complain, in_decl,
11524 /*function_p=*/false,
11525 integral_constant_expression_p);
11526 }
11527
11528 return NULL_TREE;
11529 #undef RECUR
11530 }
11531
11532 /* T is a postfix-expression that is not being used in a function
11533 call. Return the substituted version of T. */
11534
11535 static tree
11536 tsubst_non_call_postfix_expression (tree t, tree args,
11537 tsubst_flags_t complain,
11538 tree in_decl)
11539 {
11540 if (TREE_CODE (t) == SCOPE_REF)
11541 t = tsubst_qualified_id (t, args, complain, in_decl,
11542 /*done=*/false, /*address_p=*/false);
11543 else
11544 t = tsubst_copy_and_build (t, args, complain, in_decl,
11545 /*function_p=*/false,
11546 /*integral_constant_expression_p=*/false);
11547
11548 return t;
11549 }
11550
11551 /* Like tsubst but deals with expressions and performs semantic
11552 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11553
11554 tree
11555 tsubst_copy_and_build (tree t,
11556 tree args,
11557 tsubst_flags_t complain,
11558 tree in_decl,
11559 bool function_p,
11560 bool integral_constant_expression_p)
11561 {
11562 #define RECUR(NODE) \
11563 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11564 /*function_p=*/false, \
11565 integral_constant_expression_p)
11566
11567 tree op1;
11568
11569 if (t == NULL_TREE || t == error_mark_node)
11570 return t;
11571
11572 switch (TREE_CODE (t))
11573 {
11574 case USING_DECL:
11575 t = DECL_NAME (t);
11576 /* Fall through. */
11577 case IDENTIFIER_NODE:
11578 {
11579 tree decl;
11580 cp_id_kind idk;
11581 bool non_integral_constant_expression_p;
11582 const char *error_msg;
11583
11584 if (IDENTIFIER_TYPENAME_P (t))
11585 {
11586 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11587 t = mangle_conv_op_name_for_type (new_type);
11588 }
11589
11590 /* Look up the name. */
11591 decl = lookup_name (t);
11592
11593 /* By convention, expressions use ERROR_MARK_NODE to indicate
11594 failure, not NULL_TREE. */
11595 if (decl == NULL_TREE)
11596 decl = error_mark_node;
11597
11598 decl = finish_id_expression (t, decl, NULL_TREE,
11599 &idk,
11600 integral_constant_expression_p,
11601 /*allow_non_integral_constant_expression_p=*/false,
11602 &non_integral_constant_expression_p,
11603 /*template_p=*/false,
11604 /*done=*/true,
11605 /*address_p=*/false,
11606 /*template_arg_p=*/false,
11607 &error_msg,
11608 input_location);
11609 if (error_msg)
11610 error (error_msg);
11611 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
11612 decl = unqualified_name_lookup_error (decl);
11613 return decl;
11614 }
11615
11616 case TEMPLATE_ID_EXPR:
11617 {
11618 tree object;
11619 tree templ = RECUR (TREE_OPERAND (t, 0));
11620 tree targs = TREE_OPERAND (t, 1);
11621
11622 if (targs)
11623 targs = tsubst_template_args (targs, args, complain, in_decl);
11624
11625 if (TREE_CODE (templ) == COMPONENT_REF)
11626 {
11627 object = TREE_OPERAND (templ, 0);
11628 templ = TREE_OPERAND (templ, 1);
11629 }
11630 else
11631 object = NULL_TREE;
11632 templ = lookup_template_function (templ, targs);
11633
11634 if (object)
11635 return build3 (COMPONENT_REF, TREE_TYPE (templ),
11636 object, templ, NULL_TREE);
11637 else
11638 return baselink_for_fns (templ);
11639 }
11640
11641 case INDIRECT_REF:
11642 {
11643 tree r = RECUR (TREE_OPERAND (t, 0));
11644
11645 if (REFERENCE_REF_P (t))
11646 {
11647 /* A type conversion to reference type will be enclosed in
11648 such an indirect ref, but the substitution of the cast
11649 will have also added such an indirect ref. */
11650 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
11651 r = convert_from_reference (r);
11652 }
11653 else
11654 r = build_x_indirect_ref (r, "unary *", complain);
11655 return r;
11656 }
11657
11658 case NOP_EXPR:
11659 return build_nop
11660 (tsubst (TREE_TYPE (t), args, complain, in_decl),
11661 RECUR (TREE_OPERAND (t, 0)));
11662
11663 case CAST_EXPR:
11664 case REINTERPRET_CAST_EXPR:
11665 case CONST_CAST_EXPR:
11666 case DYNAMIC_CAST_EXPR:
11667 case STATIC_CAST_EXPR:
11668 {
11669 tree type;
11670 tree op;
11671
11672 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11673 if (integral_constant_expression_p
11674 && !cast_valid_in_integral_constant_expression_p (type))
11675 {
11676 if (complain & tf_error)
11677 error ("a cast to a type other than an integral or "
11678 "enumeration type cannot appear in a constant-expression");
11679 return error_mark_node;
11680 }
11681
11682 op = RECUR (TREE_OPERAND (t, 0));
11683
11684 switch (TREE_CODE (t))
11685 {
11686 case CAST_EXPR:
11687 return build_functional_cast (type, op, complain);
11688 case REINTERPRET_CAST_EXPR:
11689 return build_reinterpret_cast (type, op, complain);
11690 case CONST_CAST_EXPR:
11691 return build_const_cast (type, op, complain);
11692 case DYNAMIC_CAST_EXPR:
11693 return build_dynamic_cast (type, op, complain);
11694 case STATIC_CAST_EXPR:
11695 return build_static_cast (type, op, complain);
11696 default:
11697 gcc_unreachable ();
11698 }
11699 }
11700
11701 case POSTDECREMENT_EXPR:
11702 case POSTINCREMENT_EXPR:
11703 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11704 args, complain, in_decl);
11705 return build_x_unary_op (TREE_CODE (t), op1, complain);
11706
11707 case PREDECREMENT_EXPR:
11708 case PREINCREMENT_EXPR:
11709 case NEGATE_EXPR:
11710 case BIT_NOT_EXPR:
11711 case ABS_EXPR:
11712 case TRUTH_NOT_EXPR:
11713 case UNARY_PLUS_EXPR: /* Unary + */
11714 case REALPART_EXPR:
11715 case IMAGPART_EXPR:
11716 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
11717 complain);
11718
11719 case ADDR_EXPR:
11720 op1 = TREE_OPERAND (t, 0);
11721 if (TREE_CODE (op1) == SCOPE_REF)
11722 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
11723 /*done=*/true, /*address_p=*/true);
11724 else
11725 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
11726 in_decl);
11727 if (TREE_CODE (op1) == LABEL_DECL)
11728 return finish_label_address_expr (DECL_NAME (op1),
11729 EXPR_LOCATION (op1));
11730 return build_x_unary_op (ADDR_EXPR, op1, complain);
11731
11732 case PLUS_EXPR:
11733 case MINUS_EXPR:
11734 case MULT_EXPR:
11735 case TRUNC_DIV_EXPR:
11736 case CEIL_DIV_EXPR:
11737 case FLOOR_DIV_EXPR:
11738 case ROUND_DIV_EXPR:
11739 case EXACT_DIV_EXPR:
11740 case BIT_AND_EXPR:
11741 case BIT_IOR_EXPR:
11742 case BIT_XOR_EXPR:
11743 case TRUNC_MOD_EXPR:
11744 case FLOOR_MOD_EXPR:
11745 case TRUTH_ANDIF_EXPR:
11746 case TRUTH_ORIF_EXPR:
11747 case TRUTH_AND_EXPR:
11748 case TRUTH_OR_EXPR:
11749 case RSHIFT_EXPR:
11750 case LSHIFT_EXPR:
11751 case RROTATE_EXPR:
11752 case LROTATE_EXPR:
11753 case EQ_EXPR:
11754 case NE_EXPR:
11755 case MAX_EXPR:
11756 case MIN_EXPR:
11757 case LE_EXPR:
11758 case GE_EXPR:
11759 case LT_EXPR:
11760 case GT_EXPR:
11761 case MEMBER_REF:
11762 case DOTSTAR_EXPR:
11763 return build_x_binary_op
11764 (TREE_CODE (t),
11765 RECUR (TREE_OPERAND (t, 0)),
11766 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
11767 ? ERROR_MARK
11768 : TREE_CODE (TREE_OPERAND (t, 0))),
11769 RECUR (TREE_OPERAND (t, 1)),
11770 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
11771 ? ERROR_MARK
11772 : TREE_CODE (TREE_OPERAND (t, 1))),
11773 /*overloaded_p=*/NULL,
11774 complain);
11775
11776 case SCOPE_REF:
11777 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
11778 /*address_p=*/false);
11779 case ARRAY_REF:
11780 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11781 args, complain, in_decl);
11782 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
11783
11784 case SIZEOF_EXPR:
11785 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11786 return tsubst_copy (t, args, complain, in_decl);
11787 /* Fall through */
11788
11789 case ALIGNOF_EXPR:
11790 op1 = TREE_OPERAND (t, 0);
11791 if (!args)
11792 {
11793 /* When there are no ARGS, we are trying to evaluate a
11794 non-dependent expression from the parser. Trying to do
11795 the substitutions may not work. */
11796 if (!TYPE_P (op1))
11797 op1 = TREE_TYPE (op1);
11798 }
11799 else
11800 {
11801 ++cp_unevaluated_operand;
11802 ++c_inhibit_evaluation_warnings;
11803 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
11804 /*function_p=*/false,
11805 /*integral_constant_expression_p=*/false);
11806 --cp_unevaluated_operand;
11807 --c_inhibit_evaluation_warnings;
11808 }
11809 if (TYPE_P (op1))
11810 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
11811 complain & tf_error);
11812 else
11813 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
11814 complain & tf_error);
11815
11816 case MODOP_EXPR:
11817 {
11818 tree r = build_x_modify_expr
11819 (RECUR (TREE_OPERAND (t, 0)),
11820 TREE_CODE (TREE_OPERAND (t, 1)),
11821 RECUR (TREE_OPERAND (t, 2)),
11822 complain);
11823 /* TREE_NO_WARNING must be set if either the expression was
11824 parenthesized or it uses an operator such as >>= rather
11825 than plain assignment. In the former case, it was already
11826 set and must be copied. In the latter case,
11827 build_x_modify_expr sets it and it must not be reset
11828 here. */
11829 if (TREE_NO_WARNING (t))
11830 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11831 return r;
11832 }
11833
11834 case ARROW_EXPR:
11835 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11836 args, complain, in_decl);
11837 /* Remember that there was a reference to this entity. */
11838 if (DECL_P (op1))
11839 mark_used (op1);
11840 return build_x_arrow (op1);
11841
11842 case NEW_EXPR:
11843 {
11844 tree placement = RECUR (TREE_OPERAND (t, 0));
11845 tree init = RECUR (TREE_OPERAND (t, 3));
11846 VEC(tree,gc) *placement_vec;
11847 VEC(tree,gc) *init_vec;
11848 tree ret;
11849
11850 if (placement == NULL_TREE)
11851 placement_vec = NULL;
11852 else
11853 {
11854 placement_vec = make_tree_vector ();
11855 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
11856 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
11857 }
11858
11859 /* If there was an initializer in the original tree, but it
11860 instantiated to an empty list, then we should pass a
11861 non-NULL empty vector to tell build_new that it was an
11862 empty initializer() rather than no initializer. This can
11863 only happen when the initializer is a pack expansion whose
11864 parameter packs are of length zero. */
11865 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
11866 init_vec = NULL;
11867 else
11868 {
11869 init_vec = make_tree_vector ();
11870 if (init == void_zero_node)
11871 gcc_assert (init_vec != NULL);
11872 else
11873 {
11874 for (; init != NULL_TREE; init = TREE_CHAIN (init))
11875 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
11876 }
11877 }
11878
11879 ret = build_new (&placement_vec,
11880 RECUR (TREE_OPERAND (t, 1)),
11881 RECUR (TREE_OPERAND (t, 2)),
11882 &init_vec,
11883 NEW_EXPR_USE_GLOBAL (t),
11884 complain);
11885
11886 if (placement_vec != NULL)
11887 release_tree_vector (placement_vec);
11888 if (init_vec != NULL)
11889 release_tree_vector (init_vec);
11890
11891 return ret;
11892 }
11893
11894 case DELETE_EXPR:
11895 return delete_sanity
11896 (RECUR (TREE_OPERAND (t, 0)),
11897 RECUR (TREE_OPERAND (t, 1)),
11898 DELETE_EXPR_USE_VEC (t),
11899 DELETE_EXPR_USE_GLOBAL (t));
11900
11901 case COMPOUND_EXPR:
11902 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
11903 RECUR (TREE_OPERAND (t, 1)),
11904 complain);
11905
11906 case CALL_EXPR:
11907 {
11908 tree function;
11909 VEC(tree,gc) *call_args;
11910 unsigned int nargs, i;
11911 bool qualified_p;
11912 bool koenig_p;
11913 tree ret;
11914
11915 function = CALL_EXPR_FN (t);
11916 /* When we parsed the expression, we determined whether or
11917 not Koenig lookup should be performed. */
11918 koenig_p = KOENIG_LOOKUP_P (t);
11919 if (TREE_CODE (function) == SCOPE_REF)
11920 {
11921 qualified_p = true;
11922 function = tsubst_qualified_id (function, args, complain, in_decl,
11923 /*done=*/false,
11924 /*address_p=*/false);
11925 }
11926 else
11927 {
11928 if (TREE_CODE (function) == COMPONENT_REF)
11929 {
11930 tree op = TREE_OPERAND (function, 1);
11931
11932 qualified_p = (TREE_CODE (op) == SCOPE_REF
11933 || (BASELINK_P (op)
11934 && BASELINK_QUALIFIED_P (op)));
11935 }
11936 else
11937 qualified_p = false;
11938
11939 function = tsubst_copy_and_build (function, args, complain,
11940 in_decl,
11941 !qualified_p,
11942 integral_constant_expression_p);
11943
11944 if (BASELINK_P (function))
11945 qualified_p = true;
11946 }
11947
11948 nargs = call_expr_nargs (t);
11949 call_args = make_tree_vector ();
11950 for (i = 0; i < nargs; ++i)
11951 {
11952 tree arg = CALL_EXPR_ARG (t, i);
11953
11954 if (!PACK_EXPANSION_P (arg))
11955 VEC_safe_push (tree, gc, call_args,
11956 RECUR (CALL_EXPR_ARG (t, i)));
11957 else
11958 {
11959 /* Expand the pack expansion and push each entry onto
11960 CALL_ARGS. */
11961 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
11962 if (TREE_CODE (arg) == TREE_VEC)
11963 {
11964 unsigned int len, j;
11965
11966 len = TREE_VEC_LENGTH (arg);
11967 for (j = 0; j < len; ++j)
11968 {
11969 tree value = TREE_VEC_ELT (arg, j);
11970 if (value != NULL_TREE)
11971 value = convert_from_reference (value);
11972 VEC_safe_push (tree, gc, call_args, value);
11973 }
11974 }
11975 else
11976 {
11977 /* A partial substitution. Add one entry. */
11978 VEC_safe_push (tree, gc, call_args, arg);
11979 }
11980 }
11981 }
11982
11983 /* We do not perform argument-dependent lookup if normal
11984 lookup finds a non-function, in accordance with the
11985 expected resolution of DR 218. */
11986 if (koenig_p
11987 && ((is_overloaded_fn (function)
11988 /* If lookup found a member function, the Koenig lookup is
11989 not appropriate, even if an unqualified-name was used
11990 to denote the function. */
11991 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
11992 || TREE_CODE (function) == IDENTIFIER_NODE)
11993 /* Only do this when substitution turns a dependent call
11994 into a non-dependent call. */
11995 && type_dependent_expression_p_push (t)
11996 && !any_type_dependent_arguments_p (call_args))
11997 function = perform_koenig_lookup (function, call_args);
11998
11999 if (TREE_CODE (function) == IDENTIFIER_NODE)
12000 {
12001 unqualified_name_lookup_error (function);
12002 release_tree_vector (call_args);
12003 return error_mark_node;
12004 }
12005
12006 /* Remember that there was a reference to this entity. */
12007 if (DECL_P (function))
12008 mark_used (function);
12009
12010 if (TREE_CODE (function) == OFFSET_REF)
12011 ret = build_offset_ref_call_from_tree (function, &call_args);
12012 else if (TREE_CODE (function) == COMPONENT_REF)
12013 {
12014 if (!BASELINK_P (TREE_OPERAND (function, 1)))
12015 ret = finish_call_expr (function, &call_args,
12016 /*disallow_virtual=*/false,
12017 /*koenig_p=*/false,
12018 complain);
12019 else
12020 ret = (build_new_method_call
12021 (TREE_OPERAND (function, 0),
12022 TREE_OPERAND (function, 1),
12023 &call_args, NULL_TREE,
12024 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
12025 /*fn_p=*/NULL,
12026 complain));
12027 }
12028 else
12029 ret = finish_call_expr (function, &call_args,
12030 /*disallow_virtual=*/qualified_p,
12031 koenig_p,
12032 complain);
12033
12034 release_tree_vector (call_args);
12035
12036 return ret;
12037 }
12038
12039 case COND_EXPR:
12040 return build_x_conditional_expr
12041 (RECUR (TREE_OPERAND (t, 0)),
12042 RECUR (TREE_OPERAND (t, 1)),
12043 RECUR (TREE_OPERAND (t, 2)),
12044 complain);
12045
12046 case PSEUDO_DTOR_EXPR:
12047 return finish_pseudo_destructor_expr
12048 (RECUR (TREE_OPERAND (t, 0)),
12049 RECUR (TREE_OPERAND (t, 1)),
12050 RECUR (TREE_OPERAND (t, 2)));
12051
12052 case TREE_LIST:
12053 {
12054 tree purpose, value, chain;
12055
12056 if (t == void_list_node)
12057 return t;
12058
12059 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
12060 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
12061 {
12062 /* We have pack expansions, so expand those and
12063 create a new list out of it. */
12064 tree purposevec = NULL_TREE;
12065 tree valuevec = NULL_TREE;
12066 tree chain;
12067 int i, len = -1;
12068
12069 /* Expand the argument expressions. */
12070 if (TREE_PURPOSE (t))
12071 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
12072 complain, in_decl);
12073 if (TREE_VALUE (t))
12074 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
12075 complain, in_decl);
12076
12077 /* Build the rest of the list. */
12078 chain = TREE_CHAIN (t);
12079 if (chain && chain != void_type_node)
12080 chain = RECUR (chain);
12081
12082 /* Determine the number of arguments. */
12083 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
12084 {
12085 len = TREE_VEC_LENGTH (purposevec);
12086 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
12087 }
12088 else if (TREE_CODE (valuevec) == TREE_VEC)
12089 len = TREE_VEC_LENGTH (valuevec);
12090 else
12091 {
12092 /* Since we only performed a partial substitution into
12093 the argument pack, we only return a single list
12094 node. */
12095 if (purposevec == TREE_PURPOSE (t)
12096 && valuevec == TREE_VALUE (t)
12097 && chain == TREE_CHAIN (t))
12098 return t;
12099
12100 return tree_cons (purposevec, valuevec, chain);
12101 }
12102
12103 /* Convert the argument vectors into a TREE_LIST */
12104 i = len;
12105 while (i > 0)
12106 {
12107 /* Grab the Ith values. */
12108 i--;
12109 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
12110 : NULL_TREE;
12111 value
12112 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
12113 : NULL_TREE;
12114
12115 /* Build the list (backwards). */
12116 chain = tree_cons (purpose, value, chain);
12117 }
12118
12119 return chain;
12120 }
12121
12122 purpose = TREE_PURPOSE (t);
12123 if (purpose)
12124 purpose = RECUR (purpose);
12125 value = TREE_VALUE (t);
12126 if (value)
12127 value = RECUR (value);
12128 chain = TREE_CHAIN (t);
12129 if (chain && chain != void_type_node)
12130 chain = RECUR (chain);
12131 if (purpose == TREE_PURPOSE (t)
12132 && value == TREE_VALUE (t)
12133 && chain == TREE_CHAIN (t))
12134 return t;
12135 return tree_cons (purpose, value, chain);
12136 }
12137
12138 case COMPONENT_REF:
12139 {
12140 tree object;
12141 tree object_type;
12142 tree member;
12143
12144 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12145 args, complain, in_decl);
12146 /* Remember that there was a reference to this entity. */
12147 if (DECL_P (object))
12148 mark_used (object);
12149 object_type = TREE_TYPE (object);
12150
12151 member = TREE_OPERAND (t, 1);
12152 if (BASELINK_P (member))
12153 member = tsubst_baselink (member,
12154 non_reference (TREE_TYPE (object)),
12155 args, complain, in_decl);
12156 else
12157 member = tsubst_copy (member, args, complain, in_decl);
12158 if (member == error_mark_node)
12159 return error_mark_node;
12160
12161 if (object_type && !CLASS_TYPE_P (object_type))
12162 {
12163 if (SCALAR_TYPE_P (object_type))
12164 {
12165 tree s = NULL_TREE;
12166 tree dtor = member;
12167
12168 if (TREE_CODE (dtor) == SCOPE_REF)
12169 {
12170 s = TREE_OPERAND (dtor, 0);
12171 dtor = TREE_OPERAND (dtor, 1);
12172 }
12173 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
12174 {
12175 dtor = TREE_OPERAND (dtor, 0);
12176 if (TYPE_P (dtor))
12177 return finish_pseudo_destructor_expr (object, s, dtor);
12178 }
12179 }
12180 }
12181 else if (TREE_CODE (member) == SCOPE_REF
12182 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
12183 {
12184 tree tmpl;
12185 tree args;
12186
12187 /* Lookup the template functions now that we know what the
12188 scope is. */
12189 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
12190 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
12191 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12192 /*is_type_p=*/false,
12193 /*complain=*/false);
12194 if (BASELINK_P (member))
12195 {
12196 BASELINK_FUNCTIONS (member)
12197 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
12198 args);
12199 member = (adjust_result_of_qualified_name_lookup
12200 (member, BINFO_TYPE (BASELINK_BINFO (member)),
12201 object_type));
12202 }
12203 else
12204 {
12205 qualified_name_lookup_error (object_type, tmpl, member,
12206 input_location);
12207 return error_mark_node;
12208 }
12209 }
12210 else if (TREE_CODE (member) == SCOPE_REF
12211 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
12212 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
12213 {
12214 if (complain & tf_error)
12215 {
12216 if (TYPE_P (TREE_OPERAND (member, 0)))
12217 error ("%qT is not a class or namespace",
12218 TREE_OPERAND (member, 0));
12219 else
12220 error ("%qD is not a class or namespace",
12221 TREE_OPERAND (member, 0));
12222 }
12223 return error_mark_node;
12224 }
12225 else if (TREE_CODE (member) == FIELD_DECL)
12226 return finish_non_static_data_member (member, object, NULL_TREE);
12227
12228 return finish_class_member_access_expr (object, member,
12229 /*template_p=*/false,
12230 complain);
12231 }
12232
12233 case THROW_EXPR:
12234 return build_throw
12235 (RECUR (TREE_OPERAND (t, 0)));
12236
12237 case CONSTRUCTOR:
12238 {
12239 VEC(constructor_elt,gc) *n;
12240 constructor_elt *ce;
12241 unsigned HOST_WIDE_INT idx;
12242 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12243 bool process_index_p;
12244 int newlen;
12245 bool need_copy_p = false;
12246 tree r;
12247
12248 if (type == error_mark_node)
12249 return error_mark_node;
12250
12251 /* digest_init will do the wrong thing if we let it. */
12252 if (type && TYPE_PTRMEMFUNC_P (type))
12253 return t;
12254
12255 /* We do not want to process the index of aggregate
12256 initializers as they are identifier nodes which will be
12257 looked up by digest_init. */
12258 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
12259
12260 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
12261 newlen = VEC_length (constructor_elt, n);
12262 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
12263 {
12264 if (ce->index && process_index_p)
12265 ce->index = RECUR (ce->index);
12266
12267 if (PACK_EXPANSION_P (ce->value))
12268 {
12269 /* Substitute into the pack expansion. */
12270 ce->value = tsubst_pack_expansion (ce->value, args, complain,
12271 in_decl);
12272
12273 if (ce->value == error_mark_node)
12274 ;
12275 else if (TREE_VEC_LENGTH (ce->value) == 1)
12276 /* Just move the argument into place. */
12277 ce->value = TREE_VEC_ELT (ce->value, 0);
12278 else
12279 {
12280 /* Update the length of the final CONSTRUCTOR
12281 arguments vector, and note that we will need to
12282 copy.*/
12283 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
12284 need_copy_p = true;
12285 }
12286 }
12287 else
12288 ce->value = RECUR (ce->value);
12289 }
12290
12291 if (need_copy_p)
12292 {
12293 VEC(constructor_elt,gc) *old_n = n;
12294
12295 n = VEC_alloc (constructor_elt, gc, newlen);
12296 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
12297 idx++)
12298 {
12299 if (TREE_CODE (ce->value) == TREE_VEC)
12300 {
12301 int i, len = TREE_VEC_LENGTH (ce->value);
12302 for (i = 0; i < len; ++i)
12303 CONSTRUCTOR_APPEND_ELT (n, 0,
12304 TREE_VEC_ELT (ce->value, i));
12305 }
12306 else
12307 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
12308 }
12309 }
12310
12311 r = build_constructor (init_list_type_node, n);
12312 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
12313
12314 if (TREE_HAS_CONSTRUCTOR (t))
12315 return finish_compound_literal (type, r);
12316
12317 return r;
12318 }
12319
12320 case TYPEID_EXPR:
12321 {
12322 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
12323 if (TYPE_P (operand_0))
12324 return get_typeid (operand_0);
12325 return build_typeid (operand_0);
12326 }
12327
12328 case VAR_DECL:
12329 if (!args)
12330 return t;
12331 /* Fall through */
12332
12333 case PARM_DECL:
12334 {
12335 tree r = tsubst_copy (t, args, complain, in_decl);
12336
12337 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
12338 /* If the original type was a reference, we'll be wrapped in
12339 the appropriate INDIRECT_REF. */
12340 r = convert_from_reference (r);
12341 return r;
12342 }
12343
12344 case VA_ARG_EXPR:
12345 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
12346 tsubst_copy (TREE_TYPE (t), args, complain,
12347 in_decl));
12348
12349 case OFFSETOF_EXPR:
12350 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
12351
12352 case TRAIT_EXPR:
12353 {
12354 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
12355 complain, in_decl);
12356
12357 tree type2 = TRAIT_EXPR_TYPE2 (t);
12358 if (type2)
12359 type2 = tsubst_copy (type2, args, complain, in_decl);
12360
12361 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
12362 }
12363
12364 case STMT_EXPR:
12365 {
12366 tree old_stmt_expr = cur_stmt_expr;
12367 tree stmt_expr = begin_stmt_expr ();
12368
12369 cur_stmt_expr = stmt_expr;
12370 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
12371 integral_constant_expression_p);
12372 stmt_expr = finish_stmt_expr (stmt_expr, false);
12373 cur_stmt_expr = old_stmt_expr;
12374
12375 return stmt_expr;
12376 }
12377
12378 case CONST_DECL:
12379 t = tsubst_copy (t, args, complain, in_decl);
12380 /* As in finish_id_expression, we resolve enumeration constants
12381 to their underlying values. */
12382 if (TREE_CODE (t) == CONST_DECL)
12383 {
12384 used_types_insert (TREE_TYPE (t));
12385 return DECL_INITIAL (t);
12386 }
12387 return t;
12388
12389 case LAMBDA_EXPR:
12390 {
12391 tree r = build_lambda_expr ();
12392
12393 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12394 TREE_TYPE (r) = type;
12395 CLASSTYPE_LAMBDA_EXPR (type) = r;
12396
12397 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
12398 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
12399 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
12400 LAMBDA_EXPR_DISCRIMINATOR (r)
12401 = (LAMBDA_EXPR_DISCRIMINATOR (t));
12402 LAMBDA_EXPR_CAPTURE_LIST (r)
12403 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
12404 LAMBDA_EXPR_THIS_CAPTURE (r)
12405 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
12406 LAMBDA_EXPR_EXTRA_SCOPE (r)
12407 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
12408
12409 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
12410 determine_visibility (TYPE_NAME (type));
12411 /* Now that we know visibility, instantiate the type so we have a
12412 declaration of the op() for later calls to lambda_function. */
12413 complete_type (type);
12414
12415 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
12416 if (type)
12417 apply_lambda_return_type (r, type);
12418
12419 return build_lambda_object (r);
12420 }
12421
12422 default:
12423 /* Handle Objective-C++ constructs, if appropriate. */
12424 {
12425 tree subst
12426 = objcp_tsubst_copy_and_build (t, args, complain,
12427 in_decl, /*function_p=*/false);
12428 if (subst)
12429 return subst;
12430 }
12431 return tsubst_copy (t, args, complain, in_decl);
12432 }
12433
12434 #undef RECUR
12435 }
12436
12437 /* Verify that the instantiated ARGS are valid. For type arguments,
12438 make sure that the type is not variably modified. For non-type arguments,
12439 make sure they are constants if they are integral or enumerations.
12440 Emit an error under control of COMPLAIN, and return TRUE on error. */
12441
12442 static bool
12443 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
12444 {
12445 if (ARGUMENT_PACK_P (t))
12446 {
12447 tree vec = ARGUMENT_PACK_ARGS (t);
12448 int len = TREE_VEC_LENGTH (vec);
12449 bool result = false;
12450 int i;
12451
12452 for (i = 0; i < len; ++i)
12453 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
12454 result = true;
12455 return result;
12456 }
12457 else if (TYPE_P (t))
12458 {
12459 if (variably_modified_type_p (t, NULL_TREE))
12460 {
12461 if (complain & tf_error)
12462 error ("%qT is a variably modified type", t);
12463 return true;
12464 }
12465 }
12466 /* A non-type argument of integral or enumerated type must be a
12467 constant. */
12468 else if (TREE_TYPE (t)
12469 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
12470 && !TREE_CONSTANT (t))
12471 {
12472 if (complain & tf_error)
12473 error ("integral expression %qE is not constant", t);
12474 return true;
12475 }
12476 return false;
12477 }
12478
12479 static bool
12480 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
12481 {
12482 int ix, len = DECL_NTPARMS (tmpl);
12483 bool result = false;
12484
12485 for (ix = 0; ix != len; ix++)
12486 {
12487 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
12488 result = true;
12489 }
12490 if (result && (complain & tf_error))
12491 error (" trying to instantiate %qD", tmpl);
12492 return result;
12493 }
12494
12495 /* Instantiate the indicated variable or function template TMPL with
12496 the template arguments in TARG_PTR. */
12497
12498 tree
12499 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
12500 {
12501 tree targ_ptr = orig_args;
12502 tree fndecl;
12503 tree gen_tmpl;
12504 tree spec;
12505 HOST_WIDE_INT saved_processing_template_decl;
12506
12507 if (tmpl == error_mark_node)
12508 return error_mark_node;
12509
12510 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
12511
12512 /* If this function is a clone, handle it specially. */
12513 if (DECL_CLONED_FUNCTION_P (tmpl))
12514 {
12515 tree spec;
12516 tree clone;
12517
12518 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
12519 DECL_CLONED_FUNCTION. */
12520 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
12521 targ_ptr, complain);
12522 if (spec == error_mark_node)
12523 return error_mark_node;
12524
12525 /* Look for the clone. */
12526 FOR_EACH_CLONE (clone, spec)
12527 if (DECL_NAME (clone) == DECL_NAME (tmpl))
12528 return clone;
12529 /* We should always have found the clone by now. */
12530 gcc_unreachable ();
12531 return NULL_TREE;
12532 }
12533
12534 /* Check to see if we already have this specialization. */
12535 gen_tmpl = most_general_template (tmpl);
12536 if (tmpl != gen_tmpl)
12537 /* The TMPL is a partial instantiation. To get a full set of
12538 arguments we must add the arguments used to perform the
12539 partial instantiation. */
12540 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
12541 targ_ptr);
12542
12543 /* It would be nice to avoid hashing here and then again in tsubst_decl,
12544 but it doesn't seem to be on the hot path. */
12545 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
12546
12547 gcc_assert (tmpl == gen_tmpl
12548 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
12549 == spec)
12550 || fndecl == NULL_TREE);
12551
12552 if (spec != NULL_TREE)
12553 return spec;
12554
12555 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
12556 complain))
12557 return error_mark_node;
12558
12559 /* We are building a FUNCTION_DECL, during which the access of its
12560 parameters and return types have to be checked. However this
12561 FUNCTION_DECL which is the desired context for access checking
12562 is not built yet. We solve this chicken-and-egg problem by
12563 deferring all checks until we have the FUNCTION_DECL. */
12564 push_deferring_access_checks (dk_deferred);
12565
12566 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
12567 (because, for example, we have encountered a non-dependent
12568 function call in the body of a template function and must now
12569 determine which of several overloaded functions will be called),
12570 within the instantiation itself we are not processing a
12571 template. */
12572 saved_processing_template_decl = processing_template_decl;
12573 processing_template_decl = 0;
12574 /* Substitute template parameters to obtain the specialization. */
12575 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
12576 targ_ptr, complain, gen_tmpl);
12577 processing_template_decl = saved_processing_template_decl;
12578 if (fndecl == error_mark_node)
12579 return error_mark_node;
12580
12581 /* Now we know the specialization, compute access previously
12582 deferred. */
12583 push_access_scope (fndecl);
12584
12585 /* Some typedefs referenced from within the template code need to be access
12586 checked at template instantiation time, i.e now. These types were
12587 added to the template at parsing time. Let's get those and perfom
12588 the acces checks then. */
12589 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
12590 perform_deferred_access_checks ();
12591 pop_access_scope (fndecl);
12592 pop_deferring_access_checks ();
12593
12594 /* The DECL_TI_TEMPLATE should always be the immediate parent
12595 template, not the most general template. */
12596 DECL_TI_TEMPLATE (fndecl) = tmpl;
12597
12598 /* If we've just instantiated the main entry point for a function,
12599 instantiate all the alternate entry points as well. We do this
12600 by cloning the instantiation of the main entry point, not by
12601 instantiating the template clones. */
12602 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
12603 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
12604
12605 return fndecl;
12606 }
12607
12608 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
12609 NARGS elements of the arguments that are being used when calling
12610 it. TARGS is a vector into which the deduced template arguments
12611 are placed.
12612
12613 Return zero for success, 2 for an incomplete match that doesn't resolve
12614 all the types, and 1 for complete failure. An error message will be
12615 printed only for an incomplete match.
12616
12617 If FN is a conversion operator, or we are trying to produce a specific
12618 specialization, RETURN_TYPE is the return type desired.
12619
12620 The EXPLICIT_TARGS are explicit template arguments provided via a
12621 template-id.
12622
12623 The parameter STRICT is one of:
12624
12625 DEDUCE_CALL:
12626 We are deducing arguments for a function call, as in
12627 [temp.deduct.call].
12628
12629 DEDUCE_CONV:
12630 We are deducing arguments for a conversion function, as in
12631 [temp.deduct.conv].
12632
12633 DEDUCE_EXACT:
12634 We are deducing arguments when doing an explicit instantiation
12635 as in [temp.explicit], when determining an explicit specialization
12636 as in [temp.expl.spec], or when taking the address of a function
12637 template, as in [temp.deduct.funcaddr]. */
12638
12639 int
12640 fn_type_unification (tree fn,
12641 tree explicit_targs,
12642 tree targs,
12643 const tree *args,
12644 unsigned int nargs,
12645 tree return_type,
12646 unification_kind_t strict,
12647 int flags)
12648 {
12649 tree parms;
12650 tree fntype;
12651 int result;
12652 bool incomplete_argument_packs_p = false;
12653
12654 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
12655
12656 fntype = TREE_TYPE (fn);
12657 if (explicit_targs)
12658 {
12659 /* [temp.deduct]
12660
12661 The specified template arguments must match the template
12662 parameters in kind (i.e., type, nontype, template), and there
12663 must not be more arguments than there are parameters;
12664 otherwise type deduction fails.
12665
12666 Nontype arguments must match the types of the corresponding
12667 nontype template parameters, or must be convertible to the
12668 types of the corresponding nontype parameters as specified in
12669 _temp.arg.nontype_, otherwise type deduction fails.
12670
12671 All references in the function type of the function template
12672 to the corresponding template parameters are replaced by the
12673 specified template argument values. If a substitution in a
12674 template parameter or in the function type of the function
12675 template results in an invalid type, type deduction fails. */
12676 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
12677 int i, len = TREE_VEC_LENGTH (tparms);
12678 tree converted_args;
12679 bool incomplete = false;
12680
12681 if (explicit_targs == error_mark_node)
12682 return 1;
12683
12684 converted_args
12685 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
12686 /*require_all_args=*/false,
12687 /*use_default_args=*/false));
12688 if (converted_args == error_mark_node)
12689 return 1;
12690
12691 /* Substitute the explicit args into the function type. This is
12692 necessary so that, for instance, explicitly declared function
12693 arguments can match null pointed constants. If we were given
12694 an incomplete set of explicit args, we must not do semantic
12695 processing during substitution as we could create partial
12696 instantiations. */
12697 for (i = 0; i < len; i++)
12698 {
12699 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
12700 bool parameter_pack = false;
12701
12702 /* Dig out the actual parm. */
12703 if (TREE_CODE (parm) == TYPE_DECL
12704 || TREE_CODE (parm) == TEMPLATE_DECL)
12705 {
12706 parm = TREE_TYPE (parm);
12707 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
12708 }
12709 else if (TREE_CODE (parm) == PARM_DECL)
12710 {
12711 parm = DECL_INITIAL (parm);
12712 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
12713 }
12714
12715 if (parameter_pack)
12716 {
12717 int level, idx;
12718 tree targ;
12719 template_parm_level_and_index (parm, &level, &idx);
12720
12721 /* Mark the argument pack as "incomplete". We could
12722 still deduce more arguments during unification. */
12723 targ = TMPL_ARG (converted_args, level, idx);
12724 if (targ)
12725 {
12726 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
12727 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
12728 = ARGUMENT_PACK_ARGS (targ);
12729 }
12730
12731 /* We have some incomplete argument packs. */
12732 incomplete_argument_packs_p = true;
12733 }
12734 }
12735
12736 if (incomplete_argument_packs_p)
12737 /* Any substitution is guaranteed to be incomplete if there
12738 are incomplete argument packs, because we can still deduce
12739 more arguments. */
12740 incomplete = 1;
12741 else
12742 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
12743
12744 processing_template_decl += incomplete;
12745 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
12746 processing_template_decl -= incomplete;
12747
12748 if (fntype == error_mark_node)
12749 return 1;
12750
12751 /* Place the explicitly specified arguments in TARGS. */
12752 for (i = NUM_TMPL_ARGS (converted_args); i--;)
12753 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
12754 }
12755
12756 /* Never do unification on the 'this' parameter. */
12757 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
12758
12759 if (return_type)
12760 {
12761 tree *new_args;
12762
12763 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
12764 new_args = XALLOCAVEC (tree, nargs + 1);
12765 new_args[0] = return_type;
12766 memcpy (new_args + 1, args, nargs * sizeof (tree));
12767 args = new_args;
12768 ++nargs;
12769 }
12770
12771 /* We allow incomplete unification without an error message here
12772 because the standard doesn't seem to explicitly prohibit it. Our
12773 callers must be ready to deal with unification failures in any
12774 event. */
12775 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
12776 targs, parms, args, nargs, /*subr=*/0,
12777 strict, flags);
12778
12779 if (result == 0 && incomplete_argument_packs_p)
12780 {
12781 int i, len = NUM_TMPL_ARGS (targs);
12782
12783 /* Clear the "incomplete" flags on all argument packs. */
12784 for (i = 0; i < len; i++)
12785 {
12786 tree arg = TREE_VEC_ELT (targs, i);
12787 if (ARGUMENT_PACK_P (arg))
12788 {
12789 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
12790 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
12791 }
12792 }
12793 }
12794
12795 /* Now that we have bindings for all of the template arguments,
12796 ensure that the arguments deduced for the template template
12797 parameters have compatible template parameter lists. We cannot
12798 check this property before we have deduced all template
12799 arguments, because the template parameter types of a template
12800 template parameter might depend on prior template parameters
12801 deduced after the template template parameter. The following
12802 ill-formed example illustrates this issue:
12803
12804 template<typename T, template<T> class C> void f(C<5>, T);
12805
12806 template<int N> struct X {};
12807
12808 void g() {
12809 f(X<5>(), 5l); // error: template argument deduction fails
12810 }
12811
12812 The template parameter list of 'C' depends on the template type
12813 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
12814 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
12815 time that we deduce 'C'. */
12816 if (result == 0
12817 && !template_template_parm_bindings_ok_p
12818 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
12819 return 1;
12820
12821 if (result == 0)
12822 /* All is well so far. Now, check:
12823
12824 [temp.deduct]
12825
12826 When all template arguments have been deduced, all uses of
12827 template parameters in nondeduced contexts are replaced with
12828 the corresponding deduced argument values. If the
12829 substitution results in an invalid type, as described above,
12830 type deduction fails. */
12831 {
12832 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
12833 if (substed == error_mark_node)
12834 return 1;
12835
12836 /* If we're looking for an exact match, check that what we got
12837 is indeed an exact match. It might not be if some template
12838 parameters are used in non-deduced contexts. */
12839 if (strict == DEDUCE_EXACT)
12840 {
12841 unsigned int i;
12842
12843 tree sarg
12844 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
12845 if (return_type)
12846 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
12847 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
12848 if (!same_type_p (args[i], TREE_VALUE (sarg)))
12849 return 1;
12850 }
12851 }
12852
12853 return result;
12854 }
12855
12856 /* Adjust types before performing type deduction, as described in
12857 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
12858 sections are symmetric. PARM is the type of a function parameter
12859 or the return type of the conversion function. ARG is the type of
12860 the argument passed to the call, or the type of the value
12861 initialized with the result of the conversion function.
12862 ARG_EXPR is the original argument expression, which may be null. */
12863
12864 static int
12865 maybe_adjust_types_for_deduction (unification_kind_t strict,
12866 tree* parm,
12867 tree* arg,
12868 tree arg_expr)
12869 {
12870 int result = 0;
12871
12872 switch (strict)
12873 {
12874 case DEDUCE_CALL:
12875 break;
12876
12877 case DEDUCE_CONV:
12878 {
12879 /* Swap PARM and ARG throughout the remainder of this
12880 function; the handling is precisely symmetric since PARM
12881 will initialize ARG rather than vice versa. */
12882 tree* temp = parm;
12883 parm = arg;
12884 arg = temp;
12885 break;
12886 }
12887
12888 case DEDUCE_EXACT:
12889 /* There is nothing to do in this case. */
12890 return 0;
12891
12892 default:
12893 gcc_unreachable ();
12894 }
12895
12896 if (TREE_CODE (*parm) != REFERENCE_TYPE)
12897 {
12898 /* [temp.deduct.call]
12899
12900 If P is not a reference type:
12901
12902 --If A is an array type, the pointer type produced by the
12903 array-to-pointer standard conversion (_conv.array_) is
12904 used in place of A for type deduction; otherwise,
12905
12906 --If A is a function type, the pointer type produced by
12907 the function-to-pointer standard conversion
12908 (_conv.func_) is used in place of A for type deduction;
12909 otherwise,
12910
12911 --If A is a cv-qualified type, the top level
12912 cv-qualifiers of A's type are ignored for type
12913 deduction. */
12914 if (TREE_CODE (*arg) == ARRAY_TYPE)
12915 *arg = build_pointer_type (TREE_TYPE (*arg));
12916 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
12917 *arg = build_pointer_type (*arg);
12918 else
12919 *arg = TYPE_MAIN_VARIANT (*arg);
12920 }
12921
12922 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
12923 of the form T&&, where T is a template parameter, and the argument
12924 is an lvalue, T is deduced as A& */
12925 if (TREE_CODE (*parm) == REFERENCE_TYPE
12926 && TYPE_REF_IS_RVALUE (*parm)
12927 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
12928 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
12929 && arg_expr && real_lvalue_p (arg_expr))
12930 *arg = build_reference_type (*arg);
12931
12932 /* [temp.deduct.call]
12933
12934 If P is a cv-qualified type, the top level cv-qualifiers
12935 of P's type are ignored for type deduction. If P is a
12936 reference type, the type referred to by P is used for
12937 type deduction. */
12938 *parm = TYPE_MAIN_VARIANT (*parm);
12939 if (TREE_CODE (*parm) == REFERENCE_TYPE)
12940 {
12941 *parm = TREE_TYPE (*parm);
12942 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
12943 }
12944
12945 /* DR 322. For conversion deduction, remove a reference type on parm
12946 too (which has been swapped into ARG). */
12947 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
12948 *arg = TREE_TYPE (*arg);
12949
12950 return result;
12951 }
12952
12953 /* Most parms like fn_type_unification.
12954
12955 If SUBR is 1, we're being called recursively (to unify the
12956 arguments of a function or method parameter of a function
12957 template). */
12958
12959 static int
12960 type_unification_real (tree tparms,
12961 tree targs,
12962 tree xparms,
12963 const tree *xargs,
12964 unsigned int xnargs,
12965 int subr,
12966 unification_kind_t strict,
12967 int flags)
12968 {
12969 tree parm, arg, arg_expr;
12970 int i;
12971 int ntparms = TREE_VEC_LENGTH (tparms);
12972 int sub_strict;
12973 int saw_undeduced = 0;
12974 tree parms;
12975 const tree *args;
12976 unsigned int nargs;
12977 unsigned int ia;
12978
12979 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
12980 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
12981 gcc_assert (ntparms > 0);
12982
12983 switch (strict)
12984 {
12985 case DEDUCE_CALL:
12986 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
12987 | UNIFY_ALLOW_DERIVED);
12988 break;
12989
12990 case DEDUCE_CONV:
12991 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
12992 break;
12993
12994 case DEDUCE_EXACT:
12995 sub_strict = UNIFY_ALLOW_NONE;
12996 break;
12997
12998 default:
12999 gcc_unreachable ();
13000 }
13001
13002 again:
13003 parms = xparms;
13004 args = xargs;
13005 nargs = xnargs;
13006
13007 ia = 0;
13008 while (parms && parms != void_list_node
13009 && ia < nargs)
13010 {
13011 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13012 break;
13013
13014 parm = TREE_VALUE (parms);
13015 parms = TREE_CHAIN (parms);
13016 arg = args[ia];
13017 ++ia;
13018 arg_expr = NULL;
13019
13020 if (arg == error_mark_node)
13021 return 1;
13022 if (arg == unknown_type_node)
13023 /* We can't deduce anything from this, but we might get all the
13024 template args from other function args. */
13025 continue;
13026
13027 /* Conversions will be performed on a function argument that
13028 corresponds with a function parameter that contains only
13029 non-deducible template parameters and explicitly specified
13030 template parameters. */
13031 if (!uses_template_parms (parm))
13032 {
13033 tree type;
13034
13035 if (!TYPE_P (arg))
13036 type = TREE_TYPE (arg);
13037 else
13038 type = arg;
13039
13040 if (same_type_p (parm, type))
13041 continue;
13042 if (strict != DEDUCE_EXACT
13043 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
13044 flags))
13045 continue;
13046
13047 return 1;
13048 }
13049
13050 if (!TYPE_P (arg))
13051 {
13052 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13053 if (type_unknown_p (arg))
13054 {
13055 /* [temp.deduct.type]
13056
13057 A template-argument can be deduced from a pointer to
13058 function or pointer to member function argument if
13059 the set of overloaded functions does not contain
13060 function templates and at most one of a set of
13061 overloaded functions provides a unique match. */
13062 if (resolve_overloaded_unification
13063 (tparms, targs, parm, arg, strict, sub_strict))
13064 continue;
13065
13066 return 1;
13067 }
13068 arg_expr = arg;
13069 arg = unlowered_expr_type (arg);
13070 if (arg == error_mark_node)
13071 return 1;
13072 }
13073
13074 {
13075 int arg_strict = sub_strict;
13076
13077 if (!subr)
13078 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
13079 arg_expr);
13080
13081 if (arg == init_list_type_node && arg_expr)
13082 arg = arg_expr;
13083 if (unify (tparms, targs, parm, arg, arg_strict))
13084 return 1;
13085 }
13086 }
13087
13088
13089 if (parms
13090 && parms != void_list_node
13091 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13092 {
13093 /* Unify the remaining arguments with the pack expansion type. */
13094 tree argvec;
13095 tree parmvec = make_tree_vec (1);
13096
13097 /* Allocate a TREE_VEC and copy in all of the arguments */
13098 argvec = make_tree_vec (nargs - ia);
13099 for (i = 0; ia < nargs; ++ia, ++i)
13100 TREE_VEC_ELT (argvec, i) = args[ia];
13101
13102 /* Copy the parameter into parmvec. */
13103 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
13104 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
13105 /*call_args_p=*/true, /*subr=*/subr))
13106 return 1;
13107
13108 /* Advance to the end of the list of parameters. */
13109 parms = TREE_CHAIN (parms);
13110 }
13111
13112 /* Fail if we've reached the end of the parm list, and more args
13113 are present, and the parm list isn't variadic. */
13114 if (ia < nargs && parms == void_list_node)
13115 return 1;
13116 /* Fail if parms are left and they don't have default values. */
13117 if (parms && parms != void_list_node
13118 && TREE_PURPOSE (parms) == NULL_TREE)
13119 return 1;
13120
13121 if (!subr)
13122 for (i = 0; i < ntparms; i++)
13123 if (!TREE_VEC_ELT (targs, i))
13124 {
13125 tree tparm;
13126
13127 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
13128 continue;
13129
13130 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13131
13132 /* If this is an undeduced nontype parameter that depends on
13133 a type parameter, try another pass; its type may have been
13134 deduced from a later argument than the one from which
13135 this parameter can be deduced. */
13136 if (TREE_CODE (tparm) == PARM_DECL
13137 && uses_template_parms (TREE_TYPE (tparm))
13138 && !saw_undeduced++)
13139 goto again;
13140
13141 /* Core issue #226 (C++0x) [temp.deduct]:
13142
13143 If a template argument has not been deduced, its
13144 default template argument, if any, is used.
13145
13146 When we are in C++98 mode, TREE_PURPOSE will either
13147 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13148 to explicitly check cxx_dialect here. */
13149 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
13150 {
13151 tree arg = tsubst_template_arg
13152 (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)),
13153 targs, tf_none, NULL_TREE);
13154 if (arg == error_mark_node)
13155 return 1;
13156 else
13157 {
13158 TREE_VEC_ELT (targs, i) = arg;
13159 continue;
13160 }
13161 }
13162
13163 /* If the type parameter is a parameter pack, then it will
13164 be deduced to an empty parameter pack. */
13165 if (template_parameter_pack_p (tparm))
13166 {
13167 tree arg;
13168
13169 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
13170 {
13171 arg = make_node (NONTYPE_ARGUMENT_PACK);
13172 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
13173 TREE_CONSTANT (arg) = 1;
13174 }
13175 else
13176 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
13177
13178 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
13179
13180 TREE_VEC_ELT (targs, i) = arg;
13181 continue;
13182 }
13183
13184 return 2;
13185 }
13186
13187 return 0;
13188 }
13189
13190 /* Subroutine of type_unification_real. Args are like the variables
13191 at the call site. ARG is an overloaded function (or template-id);
13192 we try deducing template args from each of the overloads, and if
13193 only one succeeds, we go with that. Modifies TARGS and returns
13194 true on success. */
13195
13196 static bool
13197 resolve_overloaded_unification (tree tparms,
13198 tree targs,
13199 tree parm,
13200 tree arg,
13201 unification_kind_t strict,
13202 int sub_strict)
13203 {
13204 tree tempargs = copy_node (targs);
13205 int good = 0;
13206 tree goodfn = NULL_TREE;
13207 bool addr_p;
13208
13209 if (TREE_CODE (arg) == ADDR_EXPR)
13210 {
13211 arg = TREE_OPERAND (arg, 0);
13212 addr_p = true;
13213 }
13214 else
13215 addr_p = false;
13216
13217 if (TREE_CODE (arg) == COMPONENT_REF)
13218 /* Handle `&x' where `x' is some static or non-static member
13219 function name. */
13220 arg = TREE_OPERAND (arg, 1);
13221
13222 if (TREE_CODE (arg) == OFFSET_REF)
13223 arg = TREE_OPERAND (arg, 1);
13224
13225 /* Strip baselink information. */
13226 if (BASELINK_P (arg))
13227 arg = BASELINK_FUNCTIONS (arg);
13228
13229 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
13230 {
13231 /* If we got some explicit template args, we need to plug them into
13232 the affected templates before we try to unify, in case the
13233 explicit args will completely resolve the templates in question. */
13234
13235 tree expl_subargs = TREE_OPERAND (arg, 1);
13236 arg = TREE_OPERAND (arg, 0);
13237
13238 for (; arg; arg = OVL_NEXT (arg))
13239 {
13240 tree fn = OVL_CURRENT (arg);
13241 tree subargs, elem;
13242
13243 if (TREE_CODE (fn) != TEMPLATE_DECL)
13244 continue;
13245
13246 ++processing_template_decl;
13247 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13248 expl_subargs, /*check_ret=*/false);
13249 if (subargs)
13250 {
13251 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
13252 if (try_one_overload (tparms, targs, tempargs, parm,
13253 elem, strict, sub_strict, addr_p)
13254 && (!goodfn || !decls_match (goodfn, elem)))
13255 {
13256 goodfn = elem;
13257 ++good;
13258 }
13259 }
13260 --processing_template_decl;
13261 }
13262 }
13263 else if (TREE_CODE (arg) != OVERLOAD
13264 && TREE_CODE (arg) != FUNCTION_DECL)
13265 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13266 -- but the deduction does not succeed because the expression is
13267 not just the function on its own. */
13268 return false;
13269 else
13270 for (; arg; arg = OVL_NEXT (arg))
13271 if (try_one_overload (tparms, targs, tempargs, parm,
13272 TREE_TYPE (OVL_CURRENT (arg)),
13273 strict, sub_strict, addr_p)
13274 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
13275 {
13276 goodfn = OVL_CURRENT (arg);
13277 ++good;
13278 }
13279
13280 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13281 to function or pointer to member function argument if the set of
13282 overloaded functions does not contain function templates and at most
13283 one of a set of overloaded functions provides a unique match.
13284
13285 So if we found multiple possibilities, we return success but don't
13286 deduce anything. */
13287
13288 if (good == 1)
13289 {
13290 int i = TREE_VEC_LENGTH (targs);
13291 for (; i--; )
13292 if (TREE_VEC_ELT (tempargs, i))
13293 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
13294 }
13295 if (good)
13296 return true;
13297
13298 return false;
13299 }
13300
13301 /* Subroutine of resolve_overloaded_unification; does deduction for a single
13302 overload. Fills TARGS with any deduced arguments, or error_mark_node if
13303 different overloads deduce different arguments for a given parm.
13304 ADDR_P is true if the expression for which deduction is being
13305 performed was of the form "& fn" rather than simply "fn".
13306
13307 Returns 1 on success. */
13308
13309 static int
13310 try_one_overload (tree tparms,
13311 tree orig_targs,
13312 tree targs,
13313 tree parm,
13314 tree arg,
13315 unification_kind_t strict,
13316 int sub_strict,
13317 bool addr_p)
13318 {
13319 int nargs;
13320 tree tempargs;
13321 int i;
13322
13323 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13324 to function or pointer to member function argument if the set of
13325 overloaded functions does not contain function templates and at most
13326 one of a set of overloaded functions provides a unique match.
13327
13328 So if this is a template, just return success. */
13329
13330 if (uses_template_parms (arg))
13331 return 1;
13332
13333 if (TREE_CODE (arg) == METHOD_TYPE)
13334 arg = build_ptrmemfunc_type (build_pointer_type (arg));
13335 else if (addr_p)
13336 arg = build_pointer_type (arg);
13337
13338 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
13339
13340 /* We don't copy orig_targs for this because if we have already deduced
13341 some template args from previous args, unify would complain when we
13342 try to deduce a template parameter for the same argument, even though
13343 there isn't really a conflict. */
13344 nargs = TREE_VEC_LENGTH (targs);
13345 tempargs = make_tree_vec (nargs);
13346
13347 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
13348 return 0;
13349
13350 /* First make sure we didn't deduce anything that conflicts with
13351 explicitly specified args. */
13352 for (i = nargs; i--; )
13353 {
13354 tree elt = TREE_VEC_ELT (tempargs, i);
13355 tree oldelt = TREE_VEC_ELT (orig_targs, i);
13356
13357 if (!elt)
13358 /*NOP*/;
13359 else if (uses_template_parms (elt))
13360 /* Since we're unifying against ourselves, we will fill in
13361 template args used in the function parm list with our own
13362 template parms. Discard them. */
13363 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
13364 else if (oldelt && !template_args_equal (oldelt, elt))
13365 return 0;
13366 }
13367
13368 for (i = nargs; i--; )
13369 {
13370 tree elt = TREE_VEC_ELT (tempargs, i);
13371
13372 if (elt)
13373 TREE_VEC_ELT (targs, i) = elt;
13374 }
13375
13376 return 1;
13377 }
13378
13379 /* PARM is a template class (perhaps with unbound template
13380 parameters). ARG is a fully instantiated type. If ARG can be
13381 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
13382 TARGS are as for unify. */
13383
13384 static tree
13385 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
13386 {
13387 tree copy_of_targs;
13388
13389 if (!CLASSTYPE_TEMPLATE_INFO (arg)
13390 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
13391 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
13392 return NULL_TREE;
13393
13394 /* We need to make a new template argument vector for the call to
13395 unify. If we used TARGS, we'd clutter it up with the result of
13396 the attempted unification, even if this class didn't work out.
13397 We also don't want to commit ourselves to all the unifications
13398 we've already done, since unification is supposed to be done on
13399 an argument-by-argument basis. In other words, consider the
13400 following pathological case:
13401
13402 template <int I, int J, int K>
13403 struct S {};
13404
13405 template <int I, int J>
13406 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
13407
13408 template <int I, int J, int K>
13409 void f(S<I, J, K>, S<I, I, I>);
13410
13411 void g() {
13412 S<0, 0, 0> s0;
13413 S<0, 1, 2> s2;
13414
13415 f(s0, s2);
13416 }
13417
13418 Now, by the time we consider the unification involving `s2', we
13419 already know that we must have `f<0, 0, 0>'. But, even though
13420 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
13421 because there are two ways to unify base classes of S<0, 1, 2>
13422 with S<I, I, I>. If we kept the already deduced knowledge, we
13423 would reject the possibility I=1. */
13424 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
13425
13426 /* If unification failed, we're done. */
13427 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
13428 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
13429 return NULL_TREE;
13430
13431 return arg;
13432 }
13433
13434 /* Given a template type PARM and a class type ARG, find the unique
13435 base type in ARG that is an instance of PARM. We do not examine
13436 ARG itself; only its base-classes. If there is not exactly one
13437 appropriate base class, return NULL_TREE. PARM may be the type of
13438 a partial specialization, as well as a plain template type. Used
13439 by unify. */
13440
13441 static tree
13442 get_template_base (tree tparms, tree targs, tree parm, tree arg)
13443 {
13444 tree rval = NULL_TREE;
13445 tree binfo;
13446
13447 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
13448
13449 binfo = TYPE_BINFO (complete_type (arg));
13450 if (!binfo)
13451 /* The type could not be completed. */
13452 return NULL_TREE;
13453
13454 /* Walk in inheritance graph order. The search order is not
13455 important, and this avoids multiple walks of virtual bases. */
13456 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
13457 {
13458 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
13459
13460 if (r)
13461 {
13462 /* If there is more than one satisfactory baseclass, then:
13463
13464 [temp.deduct.call]
13465
13466 If they yield more than one possible deduced A, the type
13467 deduction fails.
13468
13469 applies. */
13470 if (rval && !same_type_p (r, rval))
13471 return NULL_TREE;
13472
13473 rval = r;
13474 }
13475 }
13476
13477 return rval;
13478 }
13479
13480 /* Returns the level of DECL, which declares a template parameter. */
13481
13482 static int
13483 template_decl_level (tree decl)
13484 {
13485 switch (TREE_CODE (decl))
13486 {
13487 case TYPE_DECL:
13488 case TEMPLATE_DECL:
13489 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
13490
13491 case PARM_DECL:
13492 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
13493
13494 default:
13495 gcc_unreachable ();
13496 }
13497 return 0;
13498 }
13499
13500 /* Decide whether ARG can be unified with PARM, considering only the
13501 cv-qualifiers of each type, given STRICT as documented for unify.
13502 Returns nonzero iff the unification is OK on that basis. */
13503
13504 static int
13505 check_cv_quals_for_unify (int strict, tree arg, tree parm)
13506 {
13507 int arg_quals = cp_type_quals (arg);
13508 int parm_quals = cp_type_quals (parm);
13509
13510 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13511 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13512 {
13513 /* Although a CVR qualifier is ignored when being applied to a
13514 substituted template parameter ([8.3.2]/1 for example), that
13515 does not apply during deduction [14.8.2.4]/1, (even though
13516 that is not explicitly mentioned, [14.8.2.4]/9 indicates
13517 this). Except when we're allowing additional CV qualifiers
13518 at the outer level [14.8.2.1]/3,1st bullet. */
13519 if ((TREE_CODE (arg) == REFERENCE_TYPE
13520 || TREE_CODE (arg) == FUNCTION_TYPE
13521 || TREE_CODE (arg) == METHOD_TYPE)
13522 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
13523 return 0;
13524
13525 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
13526 && (parm_quals & TYPE_QUAL_RESTRICT))
13527 return 0;
13528 }
13529
13530 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13531 && (arg_quals & parm_quals) != parm_quals)
13532 return 0;
13533
13534 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
13535 && (parm_quals & arg_quals) != arg_quals)
13536 return 0;
13537
13538 return 1;
13539 }
13540
13541 /* Determines the LEVEL and INDEX for the template parameter PARM. */
13542 void
13543 template_parm_level_and_index (tree parm, int* level, int* index)
13544 {
13545 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13546 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13547 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13548 {
13549 *index = TEMPLATE_TYPE_IDX (parm);
13550 *level = TEMPLATE_TYPE_LEVEL (parm);
13551 }
13552 else
13553 {
13554 *index = TEMPLATE_PARM_IDX (parm);
13555 *level = TEMPLATE_PARM_LEVEL (parm);
13556 }
13557 }
13558
13559 /* Unifies the remaining arguments in PACKED_ARGS with the pack
13560 expansion at the end of PACKED_PARMS. Returns 0 if the type
13561 deduction succeeds, 1 otherwise. STRICT is the same as in
13562 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
13563 call argument list. We'll need to adjust the arguments to make them
13564 types. SUBR tells us if this is from a recursive call to
13565 type_unification_real. */
13566 int
13567 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
13568 tree packed_args, int strict, bool call_args_p,
13569 bool subr)
13570 {
13571 tree parm
13572 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
13573 tree pattern = PACK_EXPANSION_PATTERN (parm);
13574 tree pack, packs = NULL_TREE;
13575 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
13576 int len = TREE_VEC_LENGTH (packed_args);
13577
13578 /* Determine the parameter packs we will be deducing from the
13579 pattern, and record their current deductions. */
13580 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
13581 pack; pack = TREE_CHAIN (pack))
13582 {
13583 tree parm_pack = TREE_VALUE (pack);
13584 int idx, level;
13585
13586 /* Determine the index and level of this parameter pack. */
13587 template_parm_level_and_index (parm_pack, &level, &idx);
13588
13589 /* Keep track of the parameter packs and their corresponding
13590 argument packs. */
13591 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
13592 TREE_TYPE (packs) = make_tree_vec (len - start);
13593 }
13594
13595 /* Loop through all of the arguments that have not yet been
13596 unified and unify each with the pattern. */
13597 for (i = start; i < len; i++)
13598 {
13599 tree parm = pattern;
13600
13601 /* For each parameter pack, clear out the deduced value so that
13602 we can deduce it again. */
13603 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13604 {
13605 int idx, level;
13606 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13607
13608 TMPL_ARG (targs, level, idx) = NULL_TREE;
13609 }
13610
13611 /* Unify the pattern with the current argument. */
13612 {
13613 tree arg = TREE_VEC_ELT (packed_args, i);
13614 tree arg_expr = NULL_TREE;
13615 int arg_strict = strict;
13616 bool skip_arg_p = false;
13617
13618 if (call_args_p)
13619 {
13620 int sub_strict;
13621
13622 /* This mirrors what we do in type_unification_real. */
13623 switch (strict)
13624 {
13625 case DEDUCE_CALL:
13626 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
13627 | UNIFY_ALLOW_MORE_CV_QUAL
13628 | UNIFY_ALLOW_DERIVED);
13629 break;
13630
13631 case DEDUCE_CONV:
13632 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13633 break;
13634
13635 case DEDUCE_EXACT:
13636 sub_strict = UNIFY_ALLOW_NONE;
13637 break;
13638
13639 default:
13640 gcc_unreachable ();
13641 }
13642
13643 if (!TYPE_P (arg))
13644 {
13645 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13646 if (type_unknown_p (arg))
13647 {
13648 /* [temp.deduct.type] A template-argument can be
13649 deduced from a pointer to function or pointer
13650 to member function argument if the set of
13651 overloaded functions does not contain function
13652 templates and at most one of a set of
13653 overloaded functions provides a unique
13654 match. */
13655
13656 if (resolve_overloaded_unification
13657 (tparms, targs, parm, arg,
13658 (unification_kind_t) strict,
13659 sub_strict)
13660 != 0)
13661 return 1;
13662 skip_arg_p = true;
13663 }
13664
13665 if (!skip_arg_p)
13666 {
13667 arg_expr = arg;
13668 arg = unlowered_expr_type (arg);
13669 if (arg == error_mark_node)
13670 return 1;
13671 }
13672 }
13673
13674 arg_strict = sub_strict;
13675
13676 if (!subr)
13677 arg_strict |=
13678 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
13679 &parm, &arg, arg_expr);
13680 }
13681
13682 if (!skip_arg_p)
13683 {
13684 if (unify (tparms, targs, parm, arg, arg_strict))
13685 return 1;
13686 }
13687 }
13688
13689 /* For each parameter pack, collect the deduced value. */
13690 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13691 {
13692 int idx, level;
13693 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13694
13695 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
13696 TMPL_ARG (targs, level, idx);
13697 }
13698 }
13699
13700 /* Verify that the results of unification with the parameter packs
13701 produce results consistent with what we've seen before, and make
13702 the deduced argument packs available. */
13703 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13704 {
13705 tree old_pack = TREE_VALUE (pack);
13706 tree new_args = TREE_TYPE (pack);
13707 int i, len = TREE_VEC_LENGTH (new_args);
13708 bool nondeduced_p = false;
13709
13710 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
13711 actually deduce anything. */
13712 for (i = 0; i < len && !nondeduced_p; ++i)
13713 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
13714 nondeduced_p = true;
13715 if (nondeduced_p)
13716 continue;
13717
13718 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
13719 {
13720 /* Prepend the explicit arguments onto NEW_ARGS. */
13721 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
13722 tree old_args = new_args;
13723 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
13724 int len = explicit_len + TREE_VEC_LENGTH (old_args);
13725
13726 /* Copy the explicit arguments. */
13727 new_args = make_tree_vec (len);
13728 for (i = 0; i < explicit_len; i++)
13729 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
13730
13731 /* Copy the deduced arguments. */
13732 for (; i < len; i++)
13733 TREE_VEC_ELT (new_args, i) =
13734 TREE_VEC_ELT (old_args, i - explicit_len);
13735 }
13736
13737 if (!old_pack)
13738 {
13739 tree result;
13740 int idx, level;
13741
13742 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13743
13744 /* Build the deduced *_ARGUMENT_PACK. */
13745 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
13746 {
13747 result = make_node (NONTYPE_ARGUMENT_PACK);
13748 TREE_TYPE (result) =
13749 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
13750 TREE_CONSTANT (result) = 1;
13751 }
13752 else
13753 result = cxx_make_type (TYPE_ARGUMENT_PACK);
13754
13755 SET_ARGUMENT_PACK_ARGS (result, new_args);
13756
13757 /* Note the deduced argument packs for this parameter
13758 pack. */
13759 TMPL_ARG (targs, level, idx) = result;
13760 }
13761 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
13762 && (ARGUMENT_PACK_ARGS (old_pack)
13763 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
13764 {
13765 /* We only had the explicitly-provided arguments before, but
13766 now we have a complete set of arguments. */
13767 int idx, level;
13768 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
13769 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13770
13771 /* Keep the original deduced argument pack. */
13772 TMPL_ARG (targs, level, idx) = old_pack;
13773
13774 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
13775 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
13776 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
13777 }
13778 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
13779 new_args))
13780 /* Inconsistent unification of this parameter pack. */
13781 return 1;
13782 else
13783 {
13784 int idx, level;
13785
13786 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13787
13788 /* Keep the original deduced argument pack. */
13789 TMPL_ARG (targs, level, idx) = old_pack;
13790 }
13791 }
13792
13793 return 0;
13794 }
13795
13796 /* Deduce the value of template parameters. TPARMS is the (innermost)
13797 set of template parameters to a template. TARGS is the bindings
13798 for those template parameters, as determined thus far; TARGS may
13799 include template arguments for outer levels of template parameters
13800 as well. PARM is a parameter to a template function, or a
13801 subcomponent of that parameter; ARG is the corresponding argument.
13802 This function attempts to match PARM with ARG in a manner
13803 consistent with the existing assignments in TARGS. If more values
13804 are deduced, then TARGS is updated.
13805
13806 Returns 0 if the type deduction succeeds, 1 otherwise. The
13807 parameter STRICT is a bitwise or of the following flags:
13808
13809 UNIFY_ALLOW_NONE:
13810 Require an exact match between PARM and ARG.
13811 UNIFY_ALLOW_MORE_CV_QUAL:
13812 Allow the deduced ARG to be more cv-qualified (by qualification
13813 conversion) than ARG.
13814 UNIFY_ALLOW_LESS_CV_QUAL:
13815 Allow the deduced ARG to be less cv-qualified than ARG.
13816 UNIFY_ALLOW_DERIVED:
13817 Allow the deduced ARG to be a template base class of ARG,
13818 or a pointer to a template base class of the type pointed to by
13819 ARG.
13820 UNIFY_ALLOW_INTEGER:
13821 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
13822 case for more information.
13823 UNIFY_ALLOW_OUTER_LEVEL:
13824 This is the outermost level of a deduction. Used to determine validity
13825 of qualification conversions. A valid qualification conversion must
13826 have const qualified pointers leading up to the inner type which
13827 requires additional CV quals, except at the outer level, where const
13828 is not required [conv.qual]. It would be normal to set this flag in
13829 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
13830 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
13831 This is the outermost level of a deduction, and PARM can be more CV
13832 qualified at this point.
13833 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
13834 This is the outermost level of a deduction, and PARM can be less CV
13835 qualified at this point. */
13836
13837 static int
13838 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
13839 {
13840 int idx;
13841 tree targ;
13842 tree tparm;
13843 int strict_in = strict;
13844
13845 /* I don't think this will do the right thing with respect to types.
13846 But the only case I've seen it in so far has been array bounds, where
13847 signedness is the only information lost, and I think that will be
13848 okay. */
13849 while (TREE_CODE (parm) == NOP_EXPR)
13850 parm = TREE_OPERAND (parm, 0);
13851
13852 if (arg == error_mark_node)
13853 return 1;
13854 if (arg == unknown_type_node
13855 || arg == init_list_type_node)
13856 /* We can't deduce anything from this, but we might get all the
13857 template args from other function args. */
13858 return 0;
13859
13860 /* If PARM uses template parameters, then we can't bail out here,
13861 even if ARG == PARM, since we won't record unifications for the
13862 template parameters. We might need them if we're trying to
13863 figure out which of two things is more specialized. */
13864 if (arg == parm && !uses_template_parms (parm))
13865 return 0;
13866
13867 /* Handle init lists early, so the rest of the function can assume
13868 we're dealing with a type. */
13869 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
13870 {
13871 tree elt, elttype;
13872 unsigned i;
13873 tree orig_parm = parm;
13874
13875 /* Replace T with std::initializer_list<T> for deduction. */
13876 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13877 && flag_deduce_init_list)
13878 parm = listify (parm);
13879
13880 if (!is_std_init_list (parm))
13881 /* We can only deduce from an initializer list argument if the
13882 parameter is std::initializer_list; otherwise this is a
13883 non-deduced context. */
13884 return 0;
13885
13886 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
13887
13888 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
13889 {
13890 int elt_strict = strict;
13891 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
13892 {
13893 tree type = TREE_TYPE (elt);
13894 /* It should only be possible to get here for a call. */
13895 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
13896 elt_strict |= maybe_adjust_types_for_deduction
13897 (DEDUCE_CALL, &elttype, &type, elt);
13898 elt = type;
13899 }
13900
13901 if (unify (tparms, targs, elttype, elt, elt_strict))
13902 return 1;
13903 }
13904
13905 /* If the std::initializer_list<T> deduction worked, replace the
13906 deduced A with std::initializer_list<A>. */
13907 if (orig_parm != parm)
13908 {
13909 idx = TEMPLATE_TYPE_IDX (orig_parm);
13910 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
13911 targ = listify (targ);
13912 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
13913 }
13914 return 0;
13915 }
13916
13917 /* Immediately reject some pairs that won't unify because of
13918 cv-qualification mismatches. */
13919 if (TREE_CODE (arg) == TREE_CODE (parm)
13920 && TYPE_P (arg)
13921 /* It is the elements of the array which hold the cv quals of an array
13922 type, and the elements might be template type parms. We'll check
13923 when we recurse. */
13924 && TREE_CODE (arg) != ARRAY_TYPE
13925 /* We check the cv-qualifiers when unifying with template type
13926 parameters below. We want to allow ARG `const T' to unify with
13927 PARM `T' for example, when computing which of two templates
13928 is more specialized, for example. */
13929 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
13930 && !check_cv_quals_for_unify (strict_in, arg, parm))
13931 return 1;
13932
13933 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
13934 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
13935 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
13936 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
13937 strict &= ~UNIFY_ALLOW_DERIVED;
13938 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13939 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
13940
13941 switch (TREE_CODE (parm))
13942 {
13943 case TYPENAME_TYPE:
13944 case SCOPE_REF:
13945 case UNBOUND_CLASS_TEMPLATE:
13946 /* In a type which contains a nested-name-specifier, template
13947 argument values cannot be deduced for template parameters used
13948 within the nested-name-specifier. */
13949 return 0;
13950
13951 case TEMPLATE_TYPE_PARM:
13952 case TEMPLATE_TEMPLATE_PARM:
13953 case BOUND_TEMPLATE_TEMPLATE_PARM:
13954 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
13955 if (tparm == error_mark_node)
13956 return 1;
13957
13958 if (TEMPLATE_TYPE_LEVEL (parm)
13959 != template_decl_level (tparm))
13960 /* The PARM is not one we're trying to unify. Just check
13961 to see if it matches ARG. */
13962 return (TREE_CODE (arg) == TREE_CODE (parm)
13963 && same_type_p (parm, arg)) ? 0 : 1;
13964 idx = TEMPLATE_TYPE_IDX (parm);
13965 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
13966 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
13967
13968 /* Check for mixed types and values. */
13969 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13970 && TREE_CODE (tparm) != TYPE_DECL)
13971 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13972 && TREE_CODE (tparm) != TEMPLATE_DECL))
13973 return 1;
13974
13975 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13976 {
13977 /* ARG must be constructed from a template class or a template
13978 template parameter. */
13979 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
13980 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
13981 return 1;
13982
13983 {
13984 tree parmvec = TYPE_TI_ARGS (parm);
13985 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
13986 tree parm_parms
13987 = DECL_INNERMOST_TEMPLATE_PARMS
13988 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
13989 int i, len;
13990 int parm_variadic_p = 0;
13991
13992 /* The resolution to DR150 makes clear that default
13993 arguments for an N-argument may not be used to bind T
13994 to a template template parameter with fewer than N
13995 parameters. It is not safe to permit the binding of
13996 default arguments as an extension, as that may change
13997 the meaning of a conforming program. Consider:
13998
13999 struct Dense { static const unsigned int dim = 1; };
14000
14001 template <template <typename> class View,
14002 typename Block>
14003 void operator+(float, View<Block> const&);
14004
14005 template <typename Block,
14006 unsigned int Dim = Block::dim>
14007 struct Lvalue_proxy { operator float() const; };
14008
14009 void
14010 test_1d (void) {
14011 Lvalue_proxy<Dense> p;
14012 float b;
14013 b + p;
14014 }
14015
14016 Here, if Lvalue_proxy is permitted to bind to View, then
14017 the global operator+ will be used; if they are not, the
14018 Lvalue_proxy will be converted to float. */
14019 if (coerce_template_parms (parm_parms,
14020 argvec,
14021 TYPE_TI_TEMPLATE (parm),
14022 tf_none,
14023 /*require_all_args=*/true,
14024 /*use_default_args=*/false)
14025 == error_mark_node)
14026 return 1;
14027
14028 /* Deduce arguments T, i from TT<T> or TT<i>.
14029 We check each element of PARMVEC and ARGVEC individually
14030 rather than the whole TREE_VEC since they can have
14031 different number of elements. */
14032
14033 parmvec = expand_template_argument_pack (parmvec);
14034 argvec = expand_template_argument_pack (argvec);
14035
14036 len = TREE_VEC_LENGTH (parmvec);
14037
14038 /* Check if the parameters end in a pack, making them
14039 variadic. */
14040 if (len > 0
14041 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
14042 parm_variadic_p = 1;
14043
14044 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
14045 return 1;
14046
14047 for (i = 0; i < len - parm_variadic_p; ++i)
14048 {
14049 if (unify (tparms, targs,
14050 TREE_VEC_ELT (parmvec, i),
14051 TREE_VEC_ELT (argvec, i),
14052 UNIFY_ALLOW_NONE))
14053 return 1;
14054 }
14055
14056 if (parm_variadic_p
14057 && unify_pack_expansion (tparms, targs,
14058 parmvec, argvec,
14059 UNIFY_ALLOW_NONE,
14060 /*call_args_p=*/false,
14061 /*subr=*/false))
14062 return 1;
14063 }
14064 arg = TYPE_TI_TEMPLATE (arg);
14065
14066 /* Fall through to deduce template name. */
14067 }
14068
14069 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14070 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14071 {
14072 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14073
14074 /* Simple cases: Value already set, does match or doesn't. */
14075 if (targ != NULL_TREE && template_args_equal (targ, arg))
14076 return 0;
14077 else if (targ)
14078 return 1;
14079 }
14080 else
14081 {
14082 /* If PARM is `const T' and ARG is only `int', we don't have
14083 a match unless we are allowing additional qualification.
14084 If ARG is `const int' and PARM is just `T' that's OK;
14085 that binds `const int' to `T'. */
14086 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
14087 arg, parm))
14088 return 1;
14089
14090 /* Consider the case where ARG is `const volatile int' and
14091 PARM is `const T'. Then, T should be `volatile int'. */
14092 arg = cp_build_qualified_type_real
14093 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
14094 if (arg == error_mark_node)
14095 return 1;
14096
14097 /* Simple cases: Value already set, does match or doesn't. */
14098 if (targ != NULL_TREE && same_type_p (targ, arg))
14099 return 0;
14100 else if (targ)
14101 return 1;
14102
14103 /* Make sure that ARG is not a variable-sized array. (Note
14104 that were talking about variable-sized arrays (like
14105 `int[n]'), rather than arrays of unknown size (like
14106 `int[]').) We'll get very confused by such a type since
14107 the bound of the array will not be computable in an
14108 instantiation. Besides, such types are not allowed in
14109 ISO C++, so we can do as we please here. */
14110 if (variably_modified_type_p (arg, NULL_TREE))
14111 return 1;
14112
14113 /* Strip typedefs as in convert_template_argument. */
14114 arg = strip_typedefs (arg);
14115 }
14116
14117 /* If ARG is a parameter pack or an expansion, we cannot unify
14118 against it unless PARM is also a parameter pack. */
14119 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14120 && !template_parameter_pack_p (parm))
14121 return 1;
14122
14123 /* If the argument deduction results is a METHOD_TYPE,
14124 then there is a problem.
14125 METHOD_TYPE doesn't map to any real C++ type the result of
14126 the deduction can not be of that type. */
14127 if (TREE_CODE (arg) == METHOD_TYPE)
14128 return 1;
14129
14130 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14131 return 0;
14132
14133 case TEMPLATE_PARM_INDEX:
14134 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14135 if (tparm == error_mark_node)
14136 return 1;
14137
14138 if (TEMPLATE_PARM_LEVEL (parm)
14139 != template_decl_level (tparm))
14140 /* The PARM is not one we're trying to unify. Just check
14141 to see if it matches ARG. */
14142 return !(TREE_CODE (arg) == TREE_CODE (parm)
14143 && cp_tree_equal (parm, arg));
14144
14145 idx = TEMPLATE_PARM_IDX (parm);
14146 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14147
14148 if (targ)
14149 return !cp_tree_equal (targ, arg);
14150
14151 /* [temp.deduct.type] If, in the declaration of a function template
14152 with a non-type template-parameter, the non-type
14153 template-parameter is used in an expression in the function
14154 parameter-list and, if the corresponding template-argument is
14155 deduced, the template-argument type shall match the type of the
14156 template-parameter exactly, except that a template-argument
14157 deduced from an array bound may be of any integral type.
14158 The non-type parameter might use already deduced type parameters. */
14159 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
14160 if (!TREE_TYPE (arg))
14161 /* Template-parameter dependent expression. Just accept it for now.
14162 It will later be processed in convert_template_argument. */
14163 ;
14164 else if (same_type_p (TREE_TYPE (arg), tparm))
14165 /* OK */;
14166 else if ((strict & UNIFY_ALLOW_INTEGER)
14167 && (TREE_CODE (tparm) == INTEGER_TYPE
14168 || TREE_CODE (tparm) == BOOLEAN_TYPE))
14169 /* Convert the ARG to the type of PARM; the deduced non-type
14170 template argument must exactly match the types of the
14171 corresponding parameter. */
14172 arg = fold (build_nop (tparm, arg));
14173 else if (uses_template_parms (tparm))
14174 /* We haven't deduced the type of this parameter yet. Try again
14175 later. */
14176 return 0;
14177 else
14178 return 1;
14179
14180 /* If ARG is a parameter pack or an expansion, we cannot unify
14181 against it unless PARM is also a parameter pack. */
14182 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14183 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
14184 return 1;
14185
14186 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14187 return 0;
14188
14189 case PTRMEM_CST:
14190 {
14191 /* A pointer-to-member constant can be unified only with
14192 another constant. */
14193 if (TREE_CODE (arg) != PTRMEM_CST)
14194 return 1;
14195
14196 /* Just unify the class member. It would be useless (and possibly
14197 wrong, depending on the strict flags) to unify also
14198 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14199 arg refer to the same variable, even if through different
14200 classes. For instance:
14201
14202 struct A { int x; };
14203 struct B : A { };
14204
14205 Unification of &A::x and &B::x must succeed. */
14206 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
14207 PTRMEM_CST_MEMBER (arg), strict);
14208 }
14209
14210 case POINTER_TYPE:
14211 {
14212 if (TREE_CODE (arg) != POINTER_TYPE)
14213 return 1;
14214
14215 /* [temp.deduct.call]
14216
14217 A can be another pointer or pointer to member type that can
14218 be converted to the deduced A via a qualification
14219 conversion (_conv.qual_).
14220
14221 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14222 This will allow for additional cv-qualification of the
14223 pointed-to types if appropriate. */
14224
14225 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
14226 /* The derived-to-base conversion only persists through one
14227 level of pointers. */
14228 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
14229
14230 return unify (tparms, targs, TREE_TYPE (parm),
14231 TREE_TYPE (arg), strict);
14232 }
14233
14234 case REFERENCE_TYPE:
14235 if (TREE_CODE (arg) != REFERENCE_TYPE)
14236 return 1;
14237 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14238 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14239
14240 case ARRAY_TYPE:
14241 if (TREE_CODE (arg) != ARRAY_TYPE)
14242 return 1;
14243 if ((TYPE_DOMAIN (parm) == NULL_TREE)
14244 != (TYPE_DOMAIN (arg) == NULL_TREE))
14245 return 1;
14246 if (TYPE_DOMAIN (parm) != NULL_TREE)
14247 {
14248 tree parm_max;
14249 tree arg_max;
14250 bool parm_cst;
14251 bool arg_cst;
14252
14253 /* Our representation of array types uses "N - 1" as the
14254 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14255 not an integer constant. We cannot unify arbitrarily
14256 complex expressions, so we eliminate the MINUS_EXPRs
14257 here. */
14258 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
14259 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
14260 if (!parm_cst)
14261 {
14262 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
14263 parm_max = TREE_OPERAND (parm_max, 0);
14264 }
14265 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
14266 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
14267 if (!arg_cst)
14268 {
14269 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
14270 trying to unify the type of a variable with the type
14271 of a template parameter. For example:
14272
14273 template <unsigned int N>
14274 void f (char (&) [N]);
14275 int g();
14276 void h(int i) {
14277 char a[g(i)];
14278 f(a);
14279 }
14280
14281 Here, the type of the ARG will be "int [g(i)]", and
14282 may be a SAVE_EXPR, etc. */
14283 if (TREE_CODE (arg_max) != MINUS_EXPR)
14284 return 1;
14285 arg_max = TREE_OPERAND (arg_max, 0);
14286 }
14287
14288 /* If only one of the bounds used a MINUS_EXPR, compensate
14289 by adding one to the other bound. */
14290 if (parm_cst && !arg_cst)
14291 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
14292 integer_type_node,
14293 parm_max,
14294 integer_one_node);
14295 else if (arg_cst && !parm_cst)
14296 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
14297 integer_type_node,
14298 arg_max,
14299 integer_one_node);
14300
14301 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
14302 return 1;
14303 }
14304 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14305 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14306
14307 case REAL_TYPE:
14308 case COMPLEX_TYPE:
14309 case VECTOR_TYPE:
14310 case INTEGER_TYPE:
14311 case BOOLEAN_TYPE:
14312 case ENUMERAL_TYPE:
14313 case VOID_TYPE:
14314 if (TREE_CODE (arg) != TREE_CODE (parm))
14315 return 1;
14316
14317 /* We have already checked cv-qualification at the top of the
14318 function. */
14319 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
14320 return 1;
14321
14322 /* As far as unification is concerned, this wins. Later checks
14323 will invalidate it if necessary. */
14324 return 0;
14325
14326 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
14327 /* Type INTEGER_CST can come from ordinary constant template args. */
14328 case INTEGER_CST:
14329 while (TREE_CODE (arg) == NOP_EXPR)
14330 arg = TREE_OPERAND (arg, 0);
14331
14332 if (TREE_CODE (arg) != INTEGER_CST)
14333 return 1;
14334 return !tree_int_cst_equal (parm, arg);
14335
14336 case TREE_VEC:
14337 {
14338 int i;
14339 if (TREE_CODE (arg) != TREE_VEC)
14340 return 1;
14341 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
14342 return 1;
14343 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
14344 if (unify (tparms, targs,
14345 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
14346 UNIFY_ALLOW_NONE))
14347 return 1;
14348 return 0;
14349 }
14350
14351 case RECORD_TYPE:
14352 case UNION_TYPE:
14353 if (TREE_CODE (arg) != TREE_CODE (parm))
14354 return 1;
14355
14356 if (TYPE_PTRMEMFUNC_P (parm))
14357 {
14358 if (!TYPE_PTRMEMFUNC_P (arg))
14359 return 1;
14360
14361 return unify (tparms, targs,
14362 TYPE_PTRMEMFUNC_FN_TYPE (parm),
14363 TYPE_PTRMEMFUNC_FN_TYPE (arg),
14364 strict);
14365 }
14366
14367 if (CLASSTYPE_TEMPLATE_INFO (parm))
14368 {
14369 tree t = NULL_TREE;
14370
14371 if (strict_in & UNIFY_ALLOW_DERIVED)
14372 {
14373 /* First, we try to unify the PARM and ARG directly. */
14374 t = try_class_unification (tparms, targs,
14375 parm, arg);
14376
14377 if (!t)
14378 {
14379 /* Fallback to the special case allowed in
14380 [temp.deduct.call]:
14381
14382 If P is a class, and P has the form
14383 template-id, then A can be a derived class of
14384 the deduced A. Likewise, if P is a pointer to
14385 a class of the form template-id, A can be a
14386 pointer to a derived class pointed to by the
14387 deduced A. */
14388 t = get_template_base (tparms, targs, parm, arg);
14389
14390 if (!t)
14391 return 1;
14392 }
14393 }
14394 else if (CLASSTYPE_TEMPLATE_INFO (arg)
14395 && (CLASSTYPE_TI_TEMPLATE (parm)
14396 == CLASSTYPE_TI_TEMPLATE (arg)))
14397 /* Perhaps PARM is something like S<U> and ARG is S<int>.
14398 Then, we should unify `int' and `U'. */
14399 t = arg;
14400 else
14401 /* There's no chance of unification succeeding. */
14402 return 1;
14403
14404 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
14405 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
14406 }
14407 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
14408 return 1;
14409 return 0;
14410
14411 case METHOD_TYPE:
14412 case FUNCTION_TYPE:
14413 {
14414 unsigned int nargs;
14415 tree *args;
14416 tree a;
14417 unsigned int i;
14418
14419 if (TREE_CODE (arg) != TREE_CODE (parm))
14420 return 1;
14421
14422 /* CV qualifications for methods can never be deduced, they must
14423 match exactly. We need to check them explicitly here,
14424 because type_unification_real treats them as any other
14425 cv-qualified parameter. */
14426 if (TREE_CODE (parm) == METHOD_TYPE
14427 && (!check_cv_quals_for_unify
14428 (UNIFY_ALLOW_NONE,
14429 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
14430 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
14431 return 1;
14432
14433 if (unify (tparms, targs, TREE_TYPE (parm),
14434 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
14435 return 1;
14436
14437 nargs = list_length (TYPE_ARG_TYPES (arg));
14438 args = XALLOCAVEC (tree, nargs);
14439 for (a = TYPE_ARG_TYPES (arg), i = 0;
14440 a != NULL_TREE && a != void_list_node;
14441 a = TREE_CHAIN (a), ++i)
14442 args[i] = TREE_VALUE (a);
14443 nargs = i;
14444
14445 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
14446 args, nargs, 1, DEDUCE_EXACT,
14447 LOOKUP_NORMAL);
14448 }
14449
14450 case OFFSET_TYPE:
14451 /* Unify a pointer to member with a pointer to member function, which
14452 deduces the type of the member as a function type. */
14453 if (TYPE_PTRMEMFUNC_P (arg))
14454 {
14455 tree method_type;
14456 tree fntype;
14457 cp_cv_quals cv_quals;
14458
14459 /* Check top-level cv qualifiers */
14460 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
14461 return 1;
14462
14463 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14464 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
14465 return 1;
14466
14467 /* Determine the type of the function we are unifying against. */
14468 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
14469 fntype =
14470 build_function_type (TREE_TYPE (method_type),
14471 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
14472
14473 /* Extract the cv-qualifiers of the member function from the
14474 implicit object parameter and place them on the function
14475 type to be restored later. */
14476 cv_quals =
14477 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
14478 fntype = build_qualified_type (fntype, cv_quals);
14479 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
14480 }
14481
14482 if (TREE_CODE (arg) != OFFSET_TYPE)
14483 return 1;
14484 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14485 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
14486 return 1;
14487 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14488 strict);
14489
14490 case CONST_DECL:
14491 if (DECL_TEMPLATE_PARM_P (parm))
14492 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
14493 if (arg != integral_constant_value (parm))
14494 return 1;
14495 return 0;
14496
14497 case FIELD_DECL:
14498 case TEMPLATE_DECL:
14499 /* Matched cases are handled by the ARG == PARM test above. */
14500 return 1;
14501
14502 case TYPE_ARGUMENT_PACK:
14503 case NONTYPE_ARGUMENT_PACK:
14504 {
14505 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
14506 tree packed_args = ARGUMENT_PACK_ARGS (arg);
14507 int i, len = TREE_VEC_LENGTH (packed_parms);
14508 int argslen = TREE_VEC_LENGTH (packed_args);
14509 int parm_variadic_p = 0;
14510
14511 for (i = 0; i < len; ++i)
14512 {
14513 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
14514 {
14515 if (i == len - 1)
14516 /* We can unify against something with a trailing
14517 parameter pack. */
14518 parm_variadic_p = 1;
14519 else
14520 /* Since there is something following the pack
14521 expansion, we cannot unify this template argument
14522 list. */
14523 return 0;
14524 }
14525 }
14526
14527
14528 /* If we don't have enough arguments to satisfy the parameters
14529 (not counting the pack expression at the end), or we have
14530 too many arguments for a parameter list that doesn't end in
14531 a pack expression, we can't unify. */
14532 if (argslen < (len - parm_variadic_p)
14533 || (argslen > len && !parm_variadic_p))
14534 return 1;
14535
14536 /* Unify all of the parameters that precede the (optional)
14537 pack expression. */
14538 for (i = 0; i < len - parm_variadic_p; ++i)
14539 {
14540 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
14541 TREE_VEC_ELT (packed_args, i), strict))
14542 return 1;
14543 }
14544
14545 if (parm_variadic_p)
14546 return unify_pack_expansion (tparms, targs,
14547 packed_parms, packed_args,
14548 strict, /*call_args_p=*/false,
14549 /*subr=*/false);
14550 return 0;
14551 }
14552
14553 break;
14554
14555 case TYPEOF_TYPE:
14556 case DECLTYPE_TYPE:
14557 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
14558 nodes. */
14559 return 0;
14560
14561 case ERROR_MARK:
14562 /* Unification fails if we hit an error node. */
14563 return 1;
14564
14565 default:
14566 gcc_assert (EXPR_P (parm));
14567
14568 /* We must be looking at an expression. This can happen with
14569 something like:
14570
14571 template <int I>
14572 void foo(S<I>, S<I + 2>);
14573
14574 This is a "nondeduced context":
14575
14576 [deduct.type]
14577
14578 The nondeduced contexts are:
14579
14580 --A type that is a template-id in which one or more of
14581 the template-arguments is an expression that references
14582 a template-parameter.
14583
14584 In these cases, we assume deduction succeeded, but don't
14585 actually infer any unifications. */
14586
14587 if (!uses_template_parms (parm)
14588 && !template_args_equal (parm, arg))
14589 return 1;
14590 else
14591 return 0;
14592 }
14593 }
14594 \f
14595 /* Note that DECL can be defined in this translation unit, if
14596 required. */
14597
14598 static void
14599 mark_definable (tree decl)
14600 {
14601 tree clone;
14602 DECL_NOT_REALLY_EXTERN (decl) = 1;
14603 FOR_EACH_CLONE (clone, decl)
14604 DECL_NOT_REALLY_EXTERN (clone) = 1;
14605 }
14606
14607 /* Called if RESULT is explicitly instantiated, or is a member of an
14608 explicitly instantiated class. */
14609
14610 void
14611 mark_decl_instantiated (tree result, int extern_p)
14612 {
14613 SET_DECL_EXPLICIT_INSTANTIATION (result);
14614
14615 /* If this entity has already been written out, it's too late to
14616 make any modifications. */
14617 if (TREE_ASM_WRITTEN (result))
14618 return;
14619
14620 if (TREE_CODE (result) != FUNCTION_DECL)
14621 /* The TREE_PUBLIC flag for function declarations will have been
14622 set correctly by tsubst. */
14623 TREE_PUBLIC (result) = 1;
14624
14625 /* This might have been set by an earlier implicit instantiation. */
14626 DECL_COMDAT (result) = 0;
14627
14628 if (extern_p)
14629 DECL_NOT_REALLY_EXTERN (result) = 0;
14630 else
14631 {
14632 mark_definable (result);
14633 /* Always make artificials weak. */
14634 if (DECL_ARTIFICIAL (result) && flag_weak)
14635 comdat_linkage (result);
14636 /* For WIN32 we also want to put explicit instantiations in
14637 linkonce sections. */
14638 else if (TREE_PUBLIC (result))
14639 maybe_make_one_only (result);
14640 }
14641
14642 /* If EXTERN_P, then this function will not be emitted -- unless
14643 followed by an explicit instantiation, at which point its linkage
14644 will be adjusted. If !EXTERN_P, then this function will be
14645 emitted here. In neither circumstance do we want
14646 import_export_decl to adjust the linkage. */
14647 DECL_INTERFACE_KNOWN (result) = 1;
14648 }
14649
14650 /* Given two function templates PAT1 and PAT2, return:
14651
14652 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
14653 -1 if PAT2 is more specialized than PAT1.
14654 0 if neither is more specialized.
14655
14656 LEN indicates the number of parameters we should consider
14657 (defaulted parameters should not be considered).
14658
14659 The 1998 std underspecified function template partial ordering, and
14660 DR214 addresses the issue. We take pairs of arguments, one from
14661 each of the templates, and deduce them against each other. One of
14662 the templates will be more specialized if all the *other*
14663 template's arguments deduce against its arguments and at least one
14664 of its arguments *does* *not* deduce against the other template's
14665 corresponding argument. Deduction is done as for class templates.
14666 The arguments used in deduction have reference and top level cv
14667 qualifiers removed. Iff both arguments were originally reference
14668 types *and* deduction succeeds in both directions, the template
14669 with the more cv-qualified argument wins for that pairing (if
14670 neither is more cv-qualified, they both are equal). Unlike regular
14671 deduction, after all the arguments have been deduced in this way,
14672 we do *not* verify the deduced template argument values can be
14673 substituted into non-deduced contexts, nor do we have to verify
14674 that all template arguments have been deduced. */
14675
14676 int
14677 more_specialized_fn (tree pat1, tree pat2, int len)
14678 {
14679 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
14680 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
14681 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
14682 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
14683 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
14684 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
14685 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
14686 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
14687 int better1 = 0;
14688 int better2 = 0;
14689
14690 /* Remove the this parameter from non-static member functions. If
14691 one is a non-static member function and the other is not a static
14692 member function, remove the first parameter from that function
14693 also. This situation occurs for operator functions where we
14694 locate both a member function (with this pointer) and non-member
14695 operator (with explicit first operand). */
14696 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
14697 {
14698 len--; /* LEN is the number of significant arguments for DECL1 */
14699 args1 = TREE_CHAIN (args1);
14700 if (!DECL_STATIC_FUNCTION_P (decl2))
14701 args2 = TREE_CHAIN (args2);
14702 }
14703 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
14704 {
14705 args2 = TREE_CHAIN (args2);
14706 if (!DECL_STATIC_FUNCTION_P (decl1))
14707 {
14708 len--;
14709 args1 = TREE_CHAIN (args1);
14710 }
14711 }
14712
14713 /* If only one is a conversion operator, they are unordered. */
14714 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
14715 return 0;
14716
14717 /* Consider the return type for a conversion function */
14718 if (DECL_CONV_FN_P (decl1))
14719 {
14720 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
14721 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
14722 len++;
14723 }
14724
14725 processing_template_decl++;
14726
14727 while (len--
14728 /* Stop when an ellipsis is seen. */
14729 && args1 != NULL_TREE && args2 != NULL_TREE)
14730 {
14731 tree arg1 = TREE_VALUE (args1);
14732 tree arg2 = TREE_VALUE (args2);
14733 int deduce1, deduce2;
14734 int quals1 = -1;
14735 int quals2 = -1;
14736
14737 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
14738 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14739 {
14740 /* When both arguments are pack expansions, we need only
14741 unify the patterns themselves. */
14742 arg1 = PACK_EXPANSION_PATTERN (arg1);
14743 arg2 = PACK_EXPANSION_PATTERN (arg2);
14744
14745 /* This is the last comparison we need to do. */
14746 len = 0;
14747 }
14748
14749 if (TREE_CODE (arg1) == REFERENCE_TYPE)
14750 {
14751 arg1 = TREE_TYPE (arg1);
14752 quals1 = cp_type_quals (arg1);
14753 }
14754
14755 if (TREE_CODE (arg2) == REFERENCE_TYPE)
14756 {
14757 arg2 = TREE_TYPE (arg2);
14758 quals2 = cp_type_quals (arg2);
14759 }
14760
14761 if ((quals1 < 0) != (quals2 < 0))
14762 {
14763 /* Only of the args is a reference, see if we should apply
14764 array/function pointer decay to it. This is not part of
14765 DR214, but is, IMHO, consistent with the deduction rules
14766 for the function call itself, and with our earlier
14767 implementation of the underspecified partial ordering
14768 rules. (nathan). */
14769 if (quals1 >= 0)
14770 {
14771 switch (TREE_CODE (arg1))
14772 {
14773 case ARRAY_TYPE:
14774 arg1 = TREE_TYPE (arg1);
14775 /* FALLTHROUGH. */
14776 case FUNCTION_TYPE:
14777 arg1 = build_pointer_type (arg1);
14778 break;
14779
14780 default:
14781 break;
14782 }
14783 }
14784 else
14785 {
14786 switch (TREE_CODE (arg2))
14787 {
14788 case ARRAY_TYPE:
14789 arg2 = TREE_TYPE (arg2);
14790 /* FALLTHROUGH. */
14791 case FUNCTION_TYPE:
14792 arg2 = build_pointer_type (arg2);
14793 break;
14794
14795 default:
14796 break;
14797 }
14798 }
14799 }
14800
14801 arg1 = TYPE_MAIN_VARIANT (arg1);
14802 arg2 = TYPE_MAIN_VARIANT (arg2);
14803
14804 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
14805 {
14806 int i, len2 = list_length (args2);
14807 tree parmvec = make_tree_vec (1);
14808 tree argvec = make_tree_vec (len2);
14809 tree ta = args2;
14810
14811 /* Setup the parameter vector, which contains only ARG1. */
14812 TREE_VEC_ELT (parmvec, 0) = arg1;
14813
14814 /* Setup the argument vector, which contains the remaining
14815 arguments. */
14816 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
14817 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
14818
14819 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
14820 argvec, UNIFY_ALLOW_NONE,
14821 /*call_args_p=*/false,
14822 /*subr=*/0);
14823
14824 /* We cannot deduce in the other direction, because ARG1 is
14825 a pack expansion but ARG2 is not. */
14826 deduce2 = 0;
14827 }
14828 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14829 {
14830 int i, len1 = list_length (args1);
14831 tree parmvec = make_tree_vec (1);
14832 tree argvec = make_tree_vec (len1);
14833 tree ta = args1;
14834
14835 /* Setup the parameter vector, which contains only ARG1. */
14836 TREE_VEC_ELT (parmvec, 0) = arg2;
14837
14838 /* Setup the argument vector, which contains the remaining
14839 arguments. */
14840 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
14841 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
14842
14843 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
14844 argvec, UNIFY_ALLOW_NONE,
14845 /*call_args_p=*/false,
14846 /*subr=*/0);
14847
14848 /* We cannot deduce in the other direction, because ARG2 is
14849 a pack expansion but ARG1 is not.*/
14850 deduce1 = 0;
14851 }
14852
14853 else
14854 {
14855 /* The normal case, where neither argument is a pack
14856 expansion. */
14857 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
14858 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
14859 }
14860
14861 if (!deduce1)
14862 better2 = -1;
14863 if (!deduce2)
14864 better1 = -1;
14865 if (better1 < 0 && better2 < 0)
14866 /* We've failed to deduce something in either direction.
14867 These must be unordered. */
14868 break;
14869
14870 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
14871 {
14872 /* Deduces in both directions, see if quals can
14873 disambiguate. Pretend the worse one failed to deduce. */
14874 if ((quals1 & quals2) == quals2)
14875 deduce1 = 0;
14876 if ((quals1 & quals2) == quals1)
14877 deduce2 = 0;
14878 }
14879 if (deduce1 && !deduce2 && !better2)
14880 better2 = 1;
14881 if (deduce2 && !deduce1 && !better1)
14882 better1 = 1;
14883
14884 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
14885 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14886 /* We have already processed all of the arguments in our
14887 handing of the pack expansion type. */
14888 len = 0;
14889
14890 args1 = TREE_CHAIN (args1);
14891 args2 = TREE_CHAIN (args2);
14892 }
14893
14894 processing_template_decl--;
14895
14896 /* All things being equal, if the next argument is a pack expansion
14897 for one function but not for the other, prefer the
14898 non-variadic function. */
14899 if ((better1 > 0) - (better2 > 0) == 0
14900 && args1 && TREE_VALUE (args1)
14901 && args2 && TREE_VALUE (args2))
14902 {
14903 if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION)
14904 return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1;
14905 else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION)
14906 return 1;
14907 }
14908
14909 return (better1 > 0) - (better2 > 0);
14910 }
14911
14912 /* Determine which of two partial specializations is more specialized.
14913
14914 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
14915 to the first partial specialization. The TREE_VALUE is the
14916 innermost set of template parameters for the partial
14917 specialization. PAT2 is similar, but for the second template.
14918
14919 Return 1 if the first partial specialization is more specialized;
14920 -1 if the second is more specialized; 0 if neither is more
14921 specialized.
14922
14923 See [temp.class.order] for information about determining which of
14924 two templates is more specialized. */
14925
14926 static int
14927 more_specialized_class (tree pat1, tree pat2)
14928 {
14929 tree targs;
14930 tree tmpl1, tmpl2;
14931 int winner = 0;
14932 bool any_deductions = false;
14933
14934 tmpl1 = TREE_TYPE (pat1);
14935 tmpl2 = TREE_TYPE (pat2);
14936
14937 /* Just like what happens for functions, if we are ordering between
14938 different class template specializations, we may encounter dependent
14939 types in the arguments, and we need our dependency check functions
14940 to behave correctly. */
14941 ++processing_template_decl;
14942 targs = get_class_bindings (TREE_VALUE (pat1),
14943 CLASSTYPE_TI_ARGS (tmpl1),
14944 CLASSTYPE_TI_ARGS (tmpl2));
14945 if (targs)
14946 {
14947 --winner;
14948 any_deductions = true;
14949 }
14950
14951 targs = get_class_bindings (TREE_VALUE (pat2),
14952 CLASSTYPE_TI_ARGS (tmpl2),
14953 CLASSTYPE_TI_ARGS (tmpl1));
14954 if (targs)
14955 {
14956 ++winner;
14957 any_deductions = true;
14958 }
14959 --processing_template_decl;
14960
14961 /* In the case of a tie where at least one of the class templates
14962 has a parameter pack at the end, the template with the most
14963 non-packed parameters wins. */
14964 if (winner == 0
14965 && any_deductions
14966 && (template_args_variadic_p (TREE_PURPOSE (pat1))
14967 || template_args_variadic_p (TREE_PURPOSE (pat2))))
14968 {
14969 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
14970 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
14971 int len1 = TREE_VEC_LENGTH (args1);
14972 int len2 = TREE_VEC_LENGTH (args2);
14973
14974 /* We don't count the pack expansion at the end. */
14975 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
14976 --len1;
14977 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
14978 --len2;
14979
14980 if (len1 > len2)
14981 return 1;
14982 else if (len1 < len2)
14983 return -1;
14984 }
14985
14986 return winner;
14987 }
14988
14989 /* Return the template arguments that will produce the function signature
14990 DECL from the function template FN, with the explicit template
14991 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
14992 also match. Return NULL_TREE if no satisfactory arguments could be
14993 found. */
14994
14995 static tree
14996 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
14997 {
14998 int ntparms = DECL_NTPARMS (fn);
14999 tree targs = make_tree_vec (ntparms);
15000 tree decl_type;
15001 tree decl_arg_types;
15002 tree *args;
15003 unsigned int nargs, ix;
15004 tree arg;
15005
15006 /* Substitute the explicit template arguments into the type of DECL.
15007 The call to fn_type_unification will handle substitution into the
15008 FN. */
15009 decl_type = TREE_TYPE (decl);
15010 if (explicit_args && uses_template_parms (decl_type))
15011 {
15012 tree tmpl;
15013 tree converted_args;
15014
15015 if (DECL_TEMPLATE_INFO (decl))
15016 tmpl = DECL_TI_TEMPLATE (decl);
15017 else
15018 /* We can get here for some invalid specializations. */
15019 return NULL_TREE;
15020
15021 converted_args
15022 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
15023 explicit_args, NULL_TREE,
15024 tf_none,
15025 /*require_all_args=*/false,
15026 /*use_default_args=*/false);
15027 if (converted_args == error_mark_node)
15028 return NULL_TREE;
15029
15030 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
15031 if (decl_type == error_mark_node)
15032 return NULL_TREE;
15033 }
15034
15035 /* Never do unification on the 'this' parameter. */
15036 decl_arg_types = skip_artificial_parms_for (decl,
15037 TYPE_ARG_TYPES (decl_type));
15038
15039 nargs = list_length (decl_arg_types);
15040 args = XALLOCAVEC (tree, nargs);
15041 for (arg = decl_arg_types, ix = 0;
15042 arg != NULL_TREE && arg != void_list_node;
15043 arg = TREE_CHAIN (arg), ++ix)
15044 args[ix] = TREE_VALUE (arg);
15045
15046 if (fn_type_unification (fn, explicit_args, targs,
15047 args, ix,
15048 (check_rettype || DECL_CONV_FN_P (fn)
15049 ? TREE_TYPE (decl_type) : NULL_TREE),
15050 DEDUCE_EXACT, LOOKUP_NORMAL))
15051 return NULL_TREE;
15052
15053 return targs;
15054 }
15055
15056 /* Return the innermost template arguments that, when applied to a
15057 template specialization whose innermost template parameters are
15058 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15059 ARGS.
15060
15061 For example, suppose we have:
15062
15063 template <class T, class U> struct S {};
15064 template <class T> struct S<T*, int> {};
15065
15066 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15067 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15068 int}. The resulting vector will be {double}, indicating that `T'
15069 is bound to `double'. */
15070
15071 static tree
15072 get_class_bindings (tree tparms, tree spec_args, tree args)
15073 {
15074 int i, ntparms = TREE_VEC_LENGTH (tparms);
15075 tree deduced_args;
15076 tree innermost_deduced_args;
15077
15078 innermost_deduced_args = make_tree_vec (ntparms);
15079 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15080 {
15081 deduced_args = copy_node (args);
15082 SET_TMPL_ARGS_LEVEL (deduced_args,
15083 TMPL_ARGS_DEPTH (deduced_args),
15084 innermost_deduced_args);
15085 }
15086 else
15087 deduced_args = innermost_deduced_args;
15088
15089 if (unify (tparms, deduced_args,
15090 INNERMOST_TEMPLATE_ARGS (spec_args),
15091 INNERMOST_TEMPLATE_ARGS (args),
15092 UNIFY_ALLOW_NONE))
15093 return NULL_TREE;
15094
15095 for (i = 0; i < ntparms; ++i)
15096 if (! TREE_VEC_ELT (innermost_deduced_args, i))
15097 return NULL_TREE;
15098
15099 /* Verify that nondeduced template arguments agree with the type
15100 obtained from argument deduction.
15101
15102 For example:
15103
15104 struct A { typedef int X; };
15105 template <class T, class U> struct C {};
15106 template <class T> struct C<T, typename T::X> {};
15107
15108 Then with the instantiation `C<A, int>', we can deduce that
15109 `T' is `A' but unify () does not check whether `typename T::X'
15110 is `int'. */
15111 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
15112 if (spec_args == error_mark_node
15113 /* We only need to check the innermost arguments; the other
15114 arguments will always agree. */
15115 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
15116 INNERMOST_TEMPLATE_ARGS (args)))
15117 return NULL_TREE;
15118
15119 /* Now that we have bindings for all of the template arguments,
15120 ensure that the arguments deduced for the template template
15121 parameters have compatible template parameter lists. See the use
15122 of template_template_parm_bindings_ok_p in fn_type_unification
15123 for more information. */
15124 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
15125 return NULL_TREE;
15126
15127 return deduced_args;
15128 }
15129
15130 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15131 Return the TREE_LIST node with the most specialized template, if
15132 any. If there is no most specialized template, the error_mark_node
15133 is returned.
15134
15135 Note that this function does not look at, or modify, the
15136 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15137 returned is one of the elements of INSTANTIATIONS, callers may
15138 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15139 and retrieve it from the value returned. */
15140
15141 tree
15142 most_specialized_instantiation (tree templates)
15143 {
15144 tree fn, champ;
15145
15146 ++processing_template_decl;
15147
15148 champ = templates;
15149 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
15150 {
15151 int fate = 0;
15152
15153 if (get_bindings (TREE_VALUE (champ),
15154 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15155 NULL_TREE, /*check_ret=*/false))
15156 fate--;
15157
15158 if (get_bindings (TREE_VALUE (fn),
15159 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15160 NULL_TREE, /*check_ret=*/false))
15161 fate++;
15162
15163 if (fate == -1)
15164 champ = fn;
15165 else if (!fate)
15166 {
15167 /* Equally specialized, move to next function. If there
15168 is no next function, nothing's most specialized. */
15169 fn = TREE_CHAIN (fn);
15170 champ = fn;
15171 if (!fn)
15172 break;
15173 }
15174 }
15175
15176 if (champ)
15177 /* Now verify that champ is better than everything earlier in the
15178 instantiation list. */
15179 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
15180 if (get_bindings (TREE_VALUE (champ),
15181 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15182 NULL_TREE, /*check_ret=*/false)
15183 || !get_bindings (TREE_VALUE (fn),
15184 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15185 NULL_TREE, /*check_ret=*/false))
15186 {
15187 champ = NULL_TREE;
15188 break;
15189 }
15190
15191 processing_template_decl--;
15192
15193 if (!champ)
15194 return error_mark_node;
15195
15196 return champ;
15197 }
15198
15199 /* If DECL is a specialization of some template, return the most
15200 general such template. Otherwise, returns NULL_TREE.
15201
15202 For example, given:
15203
15204 template <class T> struct S { template <class U> void f(U); };
15205
15206 if TMPL is `template <class U> void S<int>::f(U)' this will return
15207 the full template. This function will not trace past partial
15208 specializations, however. For example, given in addition:
15209
15210 template <class T> struct S<T*> { template <class U> void f(U); };
15211
15212 if TMPL is `template <class U> void S<int*>::f(U)' this will return
15213 `template <class T> template <class U> S<T*>::f(U)'. */
15214
15215 tree
15216 most_general_template (tree decl)
15217 {
15218 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
15219 an immediate specialization. */
15220 if (TREE_CODE (decl) == FUNCTION_DECL)
15221 {
15222 if (DECL_TEMPLATE_INFO (decl)) {
15223 decl = DECL_TI_TEMPLATE (decl);
15224
15225 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
15226 template friend. */
15227 if (TREE_CODE (decl) != TEMPLATE_DECL)
15228 return NULL_TREE;
15229 } else
15230 return NULL_TREE;
15231 }
15232
15233 /* Look for more and more general templates. */
15234 while (DECL_TEMPLATE_INFO (decl))
15235 {
15236 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
15237 (See cp-tree.h for details.) */
15238 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
15239 break;
15240
15241 if (CLASS_TYPE_P (TREE_TYPE (decl))
15242 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
15243 break;
15244
15245 /* Stop if we run into an explicitly specialized class template. */
15246 if (!DECL_NAMESPACE_SCOPE_P (decl)
15247 && DECL_CONTEXT (decl)
15248 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
15249 break;
15250
15251 decl = DECL_TI_TEMPLATE (decl);
15252 }
15253
15254 return decl;
15255 }
15256
15257 /* Return the most specialized of the class template partial
15258 specializations of TMPL which can produce TYPE, a specialization of
15259 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
15260 a _TYPE node corresponding to the partial specialization, while the
15261 TREE_PURPOSE is the set of template arguments that must be
15262 substituted into the TREE_TYPE in order to generate TYPE.
15263
15264 If the choice of partial specialization is ambiguous, a diagnostic
15265 is issued, and the error_mark_node is returned. If there are no
15266 partial specializations of TMPL matching TYPE, then NULL_TREE is
15267 returned. */
15268
15269 static tree
15270 most_specialized_class (tree type, tree tmpl)
15271 {
15272 tree list = NULL_TREE;
15273 tree t;
15274 tree champ;
15275 int fate;
15276 bool ambiguous_p;
15277 tree args;
15278 tree outer_args = NULL_TREE;
15279
15280 tmpl = most_general_template (tmpl);
15281 args = CLASSTYPE_TI_ARGS (type);
15282
15283 /* For determining which partial specialization to use, only the
15284 innermost args are interesting. */
15285 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15286 {
15287 outer_args = strip_innermost_template_args (args, 1);
15288 args = INNERMOST_TEMPLATE_ARGS (args);
15289 }
15290
15291 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
15292 {
15293 tree partial_spec_args;
15294 tree spec_args;
15295 tree parms = TREE_VALUE (t);
15296
15297 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
15298 if (outer_args)
15299 {
15300 int i;
15301
15302 ++processing_template_decl;
15303
15304 /* Discard the outer levels of args, and then substitute in the
15305 template args from the enclosing class. */
15306 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
15307 partial_spec_args = tsubst_template_args
15308 (partial_spec_args, outer_args, tf_none, NULL_TREE);
15309
15310 /* PARMS already refers to just the innermost parms, but the
15311 template parms in partial_spec_args had their levels lowered
15312 by tsubst, so we need to do the same for the parm list. We
15313 can't just tsubst the TREE_VEC itself, as tsubst wants to
15314 treat a TREE_VEC as an argument vector. */
15315 parms = copy_node (parms);
15316 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
15317 TREE_VEC_ELT (parms, i) =
15318 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
15319
15320 --processing_template_decl;
15321 }
15322 spec_args = get_class_bindings (parms,
15323 partial_spec_args,
15324 args);
15325 if (spec_args)
15326 {
15327 if (outer_args)
15328 spec_args = add_to_template_args (outer_args, spec_args);
15329 list = tree_cons (spec_args, TREE_VALUE (t), list);
15330 TREE_TYPE (list) = TREE_TYPE (t);
15331 }
15332 }
15333
15334 if (! list)
15335 return NULL_TREE;
15336
15337 ambiguous_p = false;
15338 t = list;
15339 champ = t;
15340 t = TREE_CHAIN (t);
15341 for (; t; t = TREE_CHAIN (t))
15342 {
15343 fate = more_specialized_class (champ, t);
15344 if (fate == 1)
15345 ;
15346 else
15347 {
15348 if (fate == 0)
15349 {
15350 t = TREE_CHAIN (t);
15351 if (! t)
15352 {
15353 ambiguous_p = true;
15354 break;
15355 }
15356 }
15357 champ = t;
15358 }
15359 }
15360
15361 if (!ambiguous_p)
15362 for (t = list; t && t != champ; t = TREE_CHAIN (t))
15363 {
15364 fate = more_specialized_class (champ, t);
15365 if (fate != 1)
15366 {
15367 ambiguous_p = true;
15368 break;
15369 }
15370 }
15371
15372 if (ambiguous_p)
15373 {
15374 const char *str = "candidates are:";
15375 error ("ambiguous class template instantiation for %q#T", type);
15376 for (t = list; t; t = TREE_CHAIN (t))
15377 {
15378 error ("%s %+#T", str, TREE_TYPE (t));
15379 str = " ";
15380 }
15381 return error_mark_node;
15382 }
15383
15384 return champ;
15385 }
15386
15387 /* Explicitly instantiate DECL. */
15388
15389 void
15390 do_decl_instantiation (tree decl, tree storage)
15391 {
15392 tree result = NULL_TREE;
15393 int extern_p = 0;
15394
15395 if (!decl || decl == error_mark_node)
15396 /* An error occurred, for which grokdeclarator has already issued
15397 an appropriate message. */
15398 return;
15399 else if (! DECL_LANG_SPECIFIC (decl))
15400 {
15401 error ("explicit instantiation of non-template %q#D", decl);
15402 return;
15403 }
15404 else if (TREE_CODE (decl) == VAR_DECL)
15405 {
15406 /* There is an asymmetry here in the way VAR_DECLs and
15407 FUNCTION_DECLs are handled by grokdeclarator. In the case of
15408 the latter, the DECL we get back will be marked as a
15409 template instantiation, and the appropriate
15410 DECL_TEMPLATE_INFO will be set up. This does not happen for
15411 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
15412 should handle VAR_DECLs as it currently handles
15413 FUNCTION_DECLs. */
15414 if (!DECL_CLASS_SCOPE_P (decl))
15415 {
15416 error ("%qD is not a static data member of a class template", decl);
15417 return;
15418 }
15419 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
15420 if (!result || TREE_CODE (result) != VAR_DECL)
15421 {
15422 error ("no matching template for %qD found", decl);
15423 return;
15424 }
15425 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
15426 {
15427 error ("type %qT for explicit instantiation %qD does not match "
15428 "declared type %qT", TREE_TYPE (result), decl,
15429 TREE_TYPE (decl));
15430 return;
15431 }
15432 }
15433 else if (TREE_CODE (decl) != FUNCTION_DECL)
15434 {
15435 error ("explicit instantiation of %q#D", decl);
15436 return;
15437 }
15438 else
15439 result = decl;
15440
15441 /* Check for various error cases. Note that if the explicit
15442 instantiation is valid the RESULT will currently be marked as an
15443 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
15444 until we get here. */
15445
15446 if (DECL_TEMPLATE_SPECIALIZATION (result))
15447 {
15448 /* DR 259 [temp.spec].
15449
15450 Both an explicit instantiation and a declaration of an explicit
15451 specialization shall not appear in a program unless the explicit
15452 instantiation follows a declaration of the explicit specialization.
15453
15454 For a given set of template parameters, if an explicit
15455 instantiation of a template appears after a declaration of an
15456 explicit specialization for that template, the explicit
15457 instantiation has no effect. */
15458 return;
15459 }
15460 else if (DECL_EXPLICIT_INSTANTIATION (result))
15461 {
15462 /* [temp.spec]
15463
15464 No program shall explicitly instantiate any template more
15465 than once.
15466
15467 We check DECL_NOT_REALLY_EXTERN so as not to complain when
15468 the first instantiation was `extern' and the second is not,
15469 and EXTERN_P for the opposite case. */
15470 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
15471 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
15472 /* If an "extern" explicit instantiation follows an ordinary
15473 explicit instantiation, the template is instantiated. */
15474 if (extern_p)
15475 return;
15476 }
15477 else if (!DECL_IMPLICIT_INSTANTIATION (result))
15478 {
15479 error ("no matching template for %qD found", result);
15480 return;
15481 }
15482 else if (!DECL_TEMPLATE_INFO (result))
15483 {
15484 permerror (input_location, "explicit instantiation of non-template %q#D", result);
15485 return;
15486 }
15487
15488 if (storage == NULL_TREE)
15489 ;
15490 else if (storage == ridpointers[(int) RID_EXTERN])
15491 {
15492 if (!in_system_header && (cxx_dialect == cxx98))
15493 pedwarn (input_location, OPT_pedantic,
15494 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
15495 "instantiations");
15496 extern_p = 1;
15497 }
15498 else
15499 error ("storage class %qD applied to template instantiation", storage);
15500
15501 check_explicit_instantiation_namespace (result);
15502 mark_decl_instantiated (result, extern_p);
15503 if (! extern_p)
15504 instantiate_decl (result, /*defer_ok=*/1,
15505 /*expl_inst_class_mem_p=*/false);
15506 }
15507
15508 static void
15509 mark_class_instantiated (tree t, int extern_p)
15510 {
15511 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
15512 SET_CLASSTYPE_INTERFACE_KNOWN (t);
15513 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
15514 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
15515 if (! extern_p)
15516 {
15517 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
15518 rest_of_type_compilation (t, 1);
15519 }
15520 }
15521
15522 /* Called from do_type_instantiation through binding_table_foreach to
15523 do recursive instantiation for the type bound in ENTRY. */
15524 static void
15525 bt_instantiate_type_proc (binding_entry entry, void *data)
15526 {
15527 tree storage = *(tree *) data;
15528
15529 if (MAYBE_CLASS_TYPE_P (entry->type)
15530 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
15531 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
15532 }
15533
15534 /* Called from do_type_instantiation to instantiate a member
15535 (a member function or a static member variable) of an
15536 explicitly instantiated class template. */
15537 static void
15538 instantiate_class_member (tree decl, int extern_p)
15539 {
15540 mark_decl_instantiated (decl, extern_p);
15541 if (! extern_p)
15542 instantiate_decl (decl, /*defer_ok=*/1,
15543 /*expl_inst_class_mem_p=*/true);
15544 }
15545
15546 /* Perform an explicit instantiation of template class T. STORAGE, if
15547 non-null, is the RID for extern, inline or static. COMPLAIN is
15548 nonzero if this is called from the parser, zero if called recursively,
15549 since the standard is unclear (as detailed below). */
15550
15551 void
15552 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
15553 {
15554 int extern_p = 0;
15555 int nomem_p = 0;
15556 int static_p = 0;
15557 int previous_instantiation_extern_p = 0;
15558
15559 if (TREE_CODE (t) == TYPE_DECL)
15560 t = TREE_TYPE (t);
15561
15562 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
15563 {
15564 error ("explicit instantiation of non-template type %qT", t);
15565 return;
15566 }
15567
15568 complete_type (t);
15569
15570 if (!COMPLETE_TYPE_P (t))
15571 {
15572 if (complain & tf_error)
15573 error ("explicit instantiation of %q#T before definition of template",
15574 t);
15575 return;
15576 }
15577
15578 if (storage != NULL_TREE)
15579 {
15580 if (!in_system_header)
15581 {
15582 if (storage == ridpointers[(int) RID_EXTERN])
15583 {
15584 if (cxx_dialect == cxx98)
15585 pedwarn (input_location, OPT_pedantic,
15586 "ISO C++ 1998 forbids the use of %<extern%> on "
15587 "explicit instantiations");
15588 }
15589 else
15590 pedwarn (input_location, OPT_pedantic,
15591 "ISO C++ forbids the use of %qE"
15592 " on explicit instantiations", storage);
15593 }
15594
15595 if (storage == ridpointers[(int) RID_INLINE])
15596 nomem_p = 1;
15597 else if (storage == ridpointers[(int) RID_EXTERN])
15598 extern_p = 1;
15599 else if (storage == ridpointers[(int) RID_STATIC])
15600 static_p = 1;
15601 else
15602 {
15603 error ("storage class %qD applied to template instantiation",
15604 storage);
15605 extern_p = 0;
15606 }
15607 }
15608
15609 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
15610 {
15611 /* DR 259 [temp.spec].
15612
15613 Both an explicit instantiation and a declaration of an explicit
15614 specialization shall not appear in a program unless the explicit
15615 instantiation follows a declaration of the explicit specialization.
15616
15617 For a given set of template parameters, if an explicit
15618 instantiation of a template appears after a declaration of an
15619 explicit specialization for that template, the explicit
15620 instantiation has no effect. */
15621 return;
15622 }
15623 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
15624 {
15625 /* [temp.spec]
15626
15627 No program shall explicitly instantiate any template more
15628 than once.
15629
15630 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
15631 instantiation was `extern'. If EXTERN_P then the second is.
15632 These cases are OK. */
15633 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
15634
15635 if (!previous_instantiation_extern_p && !extern_p
15636 && (complain & tf_error))
15637 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
15638
15639 /* If we've already instantiated the template, just return now. */
15640 if (!CLASSTYPE_INTERFACE_ONLY (t))
15641 return;
15642 }
15643
15644 check_explicit_instantiation_namespace (TYPE_NAME (t));
15645 mark_class_instantiated (t, extern_p);
15646
15647 if (nomem_p)
15648 return;
15649
15650 {
15651 tree tmp;
15652
15653 /* In contrast to implicit instantiation, where only the
15654 declarations, and not the definitions, of members are
15655 instantiated, we have here:
15656
15657 [temp.explicit]
15658
15659 The explicit instantiation of a class template specialization
15660 implies the instantiation of all of its members not
15661 previously explicitly specialized in the translation unit
15662 containing the explicit instantiation.
15663
15664 Of course, we can't instantiate member template classes, since
15665 we don't have any arguments for them. Note that the standard
15666 is unclear on whether the instantiation of the members are
15667 *explicit* instantiations or not. However, the most natural
15668 interpretation is that it should be an explicit instantiation. */
15669
15670 if (! static_p)
15671 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
15672 if (TREE_CODE (tmp) == FUNCTION_DECL
15673 && DECL_TEMPLATE_INSTANTIATION (tmp))
15674 instantiate_class_member (tmp, extern_p);
15675
15676 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
15677 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
15678 instantiate_class_member (tmp, extern_p);
15679
15680 if (CLASSTYPE_NESTED_UTDS (t))
15681 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
15682 bt_instantiate_type_proc, &storage);
15683 }
15684 }
15685
15686 /* Given a function DECL, which is a specialization of TMPL, modify
15687 DECL to be a re-instantiation of TMPL with the same template
15688 arguments. TMPL should be the template into which tsubst'ing
15689 should occur for DECL, not the most general template.
15690
15691 One reason for doing this is a scenario like this:
15692
15693 template <class T>
15694 void f(const T&, int i);
15695
15696 void g() { f(3, 7); }
15697
15698 template <class T>
15699 void f(const T& t, const int i) { }
15700
15701 Note that when the template is first instantiated, with
15702 instantiate_template, the resulting DECL will have no name for the
15703 first parameter, and the wrong type for the second. So, when we go
15704 to instantiate the DECL, we regenerate it. */
15705
15706 static void
15707 regenerate_decl_from_template (tree decl, tree tmpl)
15708 {
15709 /* The arguments used to instantiate DECL, from the most general
15710 template. */
15711 tree args;
15712 tree code_pattern;
15713
15714 args = DECL_TI_ARGS (decl);
15715 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
15716
15717 /* Make sure that we can see identifiers, and compute access
15718 correctly. */
15719 push_access_scope (decl);
15720
15721 if (TREE_CODE (decl) == FUNCTION_DECL)
15722 {
15723 tree decl_parm;
15724 tree pattern_parm;
15725 tree specs;
15726 int args_depth;
15727 int parms_depth;
15728
15729 args_depth = TMPL_ARGS_DEPTH (args);
15730 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
15731 if (args_depth > parms_depth)
15732 args = get_innermost_template_args (args, parms_depth);
15733
15734 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
15735 args, tf_error, NULL_TREE);
15736 if (specs)
15737 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
15738 specs);
15739
15740 /* Merge parameter declarations. */
15741 decl_parm = skip_artificial_parms_for (decl,
15742 DECL_ARGUMENTS (decl));
15743 pattern_parm
15744 = skip_artificial_parms_for (code_pattern,
15745 DECL_ARGUMENTS (code_pattern));
15746 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
15747 {
15748 tree parm_type;
15749 tree attributes;
15750
15751 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
15752 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
15753 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
15754 NULL_TREE);
15755 parm_type = type_decays_to (parm_type);
15756 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
15757 TREE_TYPE (decl_parm) = parm_type;
15758 attributes = DECL_ATTRIBUTES (pattern_parm);
15759 if (DECL_ATTRIBUTES (decl_parm) != attributes)
15760 {
15761 DECL_ATTRIBUTES (decl_parm) = attributes;
15762 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
15763 }
15764 decl_parm = TREE_CHAIN (decl_parm);
15765 pattern_parm = TREE_CHAIN (pattern_parm);
15766 }
15767 /* Merge any parameters that match with the function parameter
15768 pack. */
15769 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
15770 {
15771 int i, len;
15772 tree expanded_types;
15773 /* Expand the TYPE_PACK_EXPANSION that provides the types for
15774 the parameters in this function parameter pack. */
15775 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
15776 args, tf_error, NULL_TREE);
15777 len = TREE_VEC_LENGTH (expanded_types);
15778 for (i = 0; i < len; i++)
15779 {
15780 tree parm_type;
15781 tree attributes;
15782
15783 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
15784 /* Rename the parameter to include the index. */
15785 DECL_NAME (decl_parm) =
15786 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
15787 parm_type = TREE_VEC_ELT (expanded_types, i);
15788 parm_type = type_decays_to (parm_type);
15789 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
15790 TREE_TYPE (decl_parm) = parm_type;
15791 attributes = DECL_ATTRIBUTES (pattern_parm);
15792 if (DECL_ATTRIBUTES (decl_parm) != attributes)
15793 {
15794 DECL_ATTRIBUTES (decl_parm) = attributes;
15795 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
15796 }
15797 decl_parm = TREE_CHAIN (decl_parm);
15798 }
15799 }
15800 /* Merge additional specifiers from the CODE_PATTERN. */
15801 if (DECL_DECLARED_INLINE_P (code_pattern)
15802 && !DECL_DECLARED_INLINE_P (decl))
15803 DECL_DECLARED_INLINE_P (decl) = 1;
15804 }
15805 else if (TREE_CODE (decl) == VAR_DECL)
15806 DECL_INITIAL (decl) =
15807 tsubst_expr (DECL_INITIAL (code_pattern), args,
15808 tf_error, DECL_TI_TEMPLATE (decl),
15809 /*integral_constant_expression_p=*/false);
15810 else
15811 gcc_unreachable ();
15812
15813 pop_access_scope (decl);
15814 }
15815
15816 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
15817 substituted to get DECL. */
15818
15819 tree
15820 template_for_substitution (tree decl)
15821 {
15822 tree tmpl = DECL_TI_TEMPLATE (decl);
15823
15824 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
15825 for the instantiation. This is not always the most general
15826 template. Consider, for example:
15827
15828 template <class T>
15829 struct S { template <class U> void f();
15830 template <> void f<int>(); };
15831
15832 and an instantiation of S<double>::f<int>. We want TD to be the
15833 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
15834 while (/* An instantiation cannot have a definition, so we need a
15835 more general template. */
15836 DECL_TEMPLATE_INSTANTIATION (tmpl)
15837 /* We must also deal with friend templates. Given:
15838
15839 template <class T> struct S {
15840 template <class U> friend void f() {};
15841 };
15842
15843 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
15844 so far as the language is concerned, but that's still
15845 where we get the pattern for the instantiation from. On
15846 other hand, if the definition comes outside the class, say:
15847
15848 template <class T> struct S {
15849 template <class U> friend void f();
15850 };
15851 template <class U> friend void f() {}
15852
15853 we don't need to look any further. That's what the check for
15854 DECL_INITIAL is for. */
15855 || (TREE_CODE (decl) == FUNCTION_DECL
15856 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
15857 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
15858 {
15859 /* The present template, TD, should not be a definition. If it
15860 were a definition, we should be using it! Note that we
15861 cannot restructure the loop to just keep going until we find
15862 a template with a definition, since that might go too far if
15863 a specialization was declared, but not defined. */
15864 gcc_assert (TREE_CODE (decl) != VAR_DECL
15865 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
15866
15867 /* Fetch the more general template. */
15868 tmpl = DECL_TI_TEMPLATE (tmpl);
15869 }
15870
15871 return tmpl;
15872 }
15873
15874 /* Returns true if we need to instantiate this template instance even if we
15875 know we aren't going to emit it.. */
15876
15877 bool
15878 always_instantiate_p (tree decl)
15879 {
15880 /* We always instantiate inline functions so that we can inline them. An
15881 explicit instantiation declaration prohibits implicit instantiation of
15882 non-inline functions. With high levels of optimization, we would
15883 normally inline non-inline functions -- but we're not allowed to do
15884 that for "extern template" functions. Therefore, we check
15885 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
15886 return ((TREE_CODE (decl) == FUNCTION_DECL
15887 && DECL_DECLARED_INLINE_P (decl))
15888 /* And we need to instantiate static data members so that
15889 their initializers are available in integral constant
15890 expressions. */
15891 || (TREE_CODE (decl) == VAR_DECL
15892 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
15893 }
15894
15895 /* Produce the definition of D, a _DECL generated from a template. If
15896 DEFER_OK is nonzero, then we don't have to actually do the
15897 instantiation now; we just have to do it sometime. Normally it is
15898 an error if this is an explicit instantiation but D is undefined.
15899 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
15900 explicitly instantiated class template. */
15901
15902 tree
15903 instantiate_decl (tree d, int defer_ok,
15904 bool expl_inst_class_mem_p)
15905 {
15906 tree tmpl = DECL_TI_TEMPLATE (d);
15907 tree gen_args;
15908 tree args;
15909 tree td;
15910 tree code_pattern;
15911 tree spec;
15912 tree gen_tmpl;
15913 bool pattern_defined;
15914 int need_push;
15915 location_t saved_loc = input_location;
15916 bool external_p;
15917
15918 /* This function should only be used to instantiate templates for
15919 functions and static member variables. */
15920 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
15921 || TREE_CODE (d) == VAR_DECL);
15922
15923 /* Variables are never deferred; if instantiation is required, they
15924 are instantiated right away. That allows for better code in the
15925 case that an expression refers to the value of the variable --
15926 if the variable has a constant value the referring expression can
15927 take advantage of that fact. */
15928 if (TREE_CODE (d) == VAR_DECL)
15929 defer_ok = 0;
15930
15931 /* Don't instantiate cloned functions. Instead, instantiate the
15932 functions they cloned. */
15933 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
15934 d = DECL_CLONED_FUNCTION (d);
15935
15936 if (DECL_TEMPLATE_INSTANTIATED (d)
15937 || DECL_TEMPLATE_SPECIALIZATION (d))
15938 /* D has already been instantiated or explicitly specialized, so
15939 there's nothing for us to do here.
15940
15941 It might seem reasonable to check whether or not D is an explicit
15942 instantiation, and, if so, stop here. But when an explicit
15943 instantiation is deferred until the end of the compilation,
15944 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
15945 the instantiation. */
15946 return d;
15947
15948 /* Check to see whether we know that this template will be
15949 instantiated in some other file, as with "extern template"
15950 extension. */
15951 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
15952
15953 /* In general, we do not instantiate such templates. */
15954 if (external_p && !always_instantiate_p (d))
15955 return d;
15956
15957 gen_tmpl = most_general_template (tmpl);
15958 gen_args = DECL_TI_ARGS (d);
15959
15960 if (tmpl != gen_tmpl)
15961 /* We should already have the extra args. */
15962 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
15963 == TMPL_ARGS_DEPTH (gen_args));
15964 /* And what's in the hash table should match D. */
15965 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
15966 || spec == NULL_TREE);
15967
15968 /* This needs to happen before any tsubsting. */
15969 if (! push_tinst_level (d))
15970 return d;
15971
15972 timevar_push (TV_PARSE);
15973
15974 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
15975 for the instantiation. */
15976 td = template_for_substitution (d);
15977 code_pattern = DECL_TEMPLATE_RESULT (td);
15978
15979 /* We should never be trying to instantiate a member of a class
15980 template or partial specialization. */
15981 gcc_assert (d != code_pattern);
15982
15983 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
15984 || DECL_TEMPLATE_SPECIALIZATION (td))
15985 /* In the case of a friend template whose definition is provided
15986 outside the class, we may have too many arguments. Drop the
15987 ones we don't need. The same is true for specializations. */
15988 args = get_innermost_template_args
15989 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
15990 else
15991 args = gen_args;
15992
15993 if (TREE_CODE (d) == FUNCTION_DECL)
15994 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
15995 else
15996 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
15997
15998 /* We may be in the middle of deferred access check. Disable it now. */
15999 push_deferring_access_checks (dk_no_deferred);
16000
16001 /* Unless an explicit instantiation directive has already determined
16002 the linkage of D, remember that a definition is available for
16003 this entity. */
16004 if (pattern_defined
16005 && !DECL_INTERFACE_KNOWN (d)
16006 && !DECL_NOT_REALLY_EXTERN (d))
16007 mark_definable (d);
16008
16009 input_location = DECL_SOURCE_LOCATION (d);
16010
16011 /* If D is a member of an explicitly instantiated class template,
16012 and no definition is available, treat it like an implicit
16013 instantiation. */
16014 if (!pattern_defined && expl_inst_class_mem_p
16015 && DECL_EXPLICIT_INSTANTIATION (d))
16016 {
16017 DECL_NOT_REALLY_EXTERN (d) = 0;
16018 DECL_INTERFACE_KNOWN (d) = 0;
16019 SET_DECL_IMPLICIT_INSTANTIATION (d);
16020 }
16021
16022 /* Recheck the substitutions to obtain any warning messages
16023 about ignoring cv qualifiers. Don't do this for artificial decls,
16024 as it breaks the context-sensitive substitution for lambda op(). */
16025 if (!defer_ok && !DECL_ARTIFICIAL (d))
16026 {
16027 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
16028 tree type = TREE_TYPE (gen);
16029
16030 /* Make sure that we can see identifiers, and compute access
16031 correctly. D is already the target FUNCTION_DECL with the
16032 right context. */
16033 push_access_scope (d);
16034
16035 if (TREE_CODE (gen) == FUNCTION_DECL)
16036 {
16037 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
16038 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
16039 d);
16040 /* Don't simply tsubst the function type, as that will give
16041 duplicate warnings about poor parameter qualifications.
16042 The function arguments are the same as the decl_arguments
16043 without the top level cv qualifiers. */
16044 type = TREE_TYPE (type);
16045 }
16046 tsubst (type, gen_args, tf_warning_or_error, d);
16047
16048 pop_access_scope (d);
16049 }
16050
16051 /* Defer all other templates, unless we have been explicitly
16052 forbidden from doing so. */
16053 if (/* If there is no definition, we cannot instantiate the
16054 template. */
16055 ! pattern_defined
16056 /* If it's OK to postpone instantiation, do so. */
16057 || defer_ok
16058 /* If this is a static data member that will be defined
16059 elsewhere, we don't want to instantiate the entire data
16060 member, but we do want to instantiate the initializer so that
16061 we can substitute that elsewhere. */
16062 || (external_p && TREE_CODE (d) == VAR_DECL))
16063 {
16064 /* The definition of the static data member is now required so
16065 we must substitute the initializer. */
16066 if (TREE_CODE (d) == VAR_DECL
16067 && !DECL_INITIAL (d)
16068 && DECL_INITIAL (code_pattern))
16069 {
16070 tree ns;
16071 tree init;
16072
16073 ns = decl_namespace_context (d);
16074 push_nested_namespace (ns);
16075 push_nested_class (DECL_CONTEXT (d));
16076 init = tsubst_expr (DECL_INITIAL (code_pattern),
16077 args,
16078 tf_warning_or_error, NULL_TREE,
16079 /*integral_constant_expression_p=*/false);
16080 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
16081 /*asmspec_tree=*/NULL_TREE,
16082 LOOKUP_ONLYCONVERTING);
16083 pop_nested_class ();
16084 pop_nested_namespace (ns);
16085 }
16086
16087 /* We restore the source position here because it's used by
16088 add_pending_template. */
16089 input_location = saved_loc;
16090
16091 if (at_eof && !pattern_defined
16092 && DECL_EXPLICIT_INSTANTIATION (d)
16093 && DECL_NOT_REALLY_EXTERN (d))
16094 /* [temp.explicit]
16095
16096 The definition of a non-exported function template, a
16097 non-exported member function template, or a non-exported
16098 member function or static data member of a class template
16099 shall be present in every translation unit in which it is
16100 explicitly instantiated. */
16101 permerror (input_location, "explicit instantiation of %qD "
16102 "but no definition available", d);
16103
16104 /* ??? Historically, we have instantiated inline functions, even
16105 when marked as "extern template". */
16106 if (!(external_p && TREE_CODE (d) == VAR_DECL))
16107 add_pending_template (d);
16108 goto out;
16109 }
16110 /* Tell the repository that D is available in this translation unit
16111 -- and see if it is supposed to be instantiated here. */
16112 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
16113 {
16114 /* In a PCH file, despite the fact that the repository hasn't
16115 requested instantiation in the PCH it is still possible that
16116 an instantiation will be required in a file that includes the
16117 PCH. */
16118 if (pch_file)
16119 add_pending_template (d);
16120 /* Instantiate inline functions so that the inliner can do its
16121 job, even though we'll not be emitting a copy of this
16122 function. */
16123 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
16124 goto out;
16125 }
16126
16127 need_push = !cfun || !global_bindings_p ();
16128 if (need_push)
16129 push_to_top_level ();
16130
16131 /* Mark D as instantiated so that recursive calls to
16132 instantiate_decl do not try to instantiate it again. */
16133 DECL_TEMPLATE_INSTANTIATED (d) = 1;
16134
16135 /* Regenerate the declaration in case the template has been modified
16136 by a subsequent redeclaration. */
16137 regenerate_decl_from_template (d, td);
16138
16139 /* We already set the file and line above. Reset them now in case
16140 they changed as a result of calling regenerate_decl_from_template. */
16141 input_location = DECL_SOURCE_LOCATION (d);
16142
16143 if (TREE_CODE (d) == VAR_DECL)
16144 {
16145 tree init;
16146
16147 /* Clear out DECL_RTL; whatever was there before may not be right
16148 since we've reset the type of the declaration. */
16149 SET_DECL_RTL (d, NULL_RTX);
16150 DECL_IN_AGGR_P (d) = 0;
16151
16152 /* The initializer is placed in DECL_INITIAL by
16153 regenerate_decl_from_template. Pull it out so that
16154 cp_finish_decl can process it. */
16155 init = DECL_INITIAL (d);
16156 DECL_INITIAL (d) = NULL_TREE;
16157 DECL_INITIALIZED_P (d) = 0;
16158
16159 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16160 initializer. That function will defer actual emission until
16161 we have a chance to determine linkage. */
16162 DECL_EXTERNAL (d) = 0;
16163
16164 /* Enter the scope of D so that access-checking works correctly. */
16165 push_nested_class (DECL_CONTEXT (d));
16166 cp_finish_decl (d, init, false, NULL_TREE, 0);
16167 pop_nested_class ();
16168 }
16169 else if (TREE_CODE (d) == FUNCTION_DECL)
16170 {
16171 htab_t saved_local_specializations;
16172 tree subst_decl;
16173 tree tmpl_parm;
16174 tree spec_parm;
16175
16176 /* Save away the current list, in case we are instantiating one
16177 template from within the body of another. */
16178 saved_local_specializations = local_specializations;
16179
16180 /* Set up the list of local specializations. */
16181 local_specializations = htab_create (37,
16182 hash_local_specialization,
16183 eq_local_specializations,
16184 NULL);
16185
16186 /* Set up context. */
16187 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
16188
16189 /* Create substitution entries for the parameters. */
16190 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
16191 tmpl_parm = DECL_ARGUMENTS (subst_decl);
16192 spec_parm = DECL_ARGUMENTS (d);
16193 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
16194 {
16195 register_local_specialization (spec_parm, tmpl_parm);
16196 spec_parm = skip_artificial_parms_for (d, spec_parm);
16197 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
16198 }
16199 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16200 {
16201 register_local_specialization (spec_parm, tmpl_parm);
16202 tmpl_parm = TREE_CHAIN (tmpl_parm);
16203 spec_parm = TREE_CHAIN (spec_parm);
16204 }
16205 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16206 {
16207 /* Register the (value) argument pack as a specialization of
16208 TMPL_PARM, then move on. */
16209 tree argpack = make_fnparm_pack (spec_parm);
16210 register_local_specialization (argpack, tmpl_parm);
16211 tmpl_parm = TREE_CHAIN (tmpl_parm);
16212 spec_parm = NULL_TREE;
16213 }
16214 gcc_assert (!spec_parm);
16215
16216 /* Substitute into the body of the function. */
16217 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
16218 tf_warning_or_error, tmpl,
16219 /*integral_constant_expression_p=*/false);
16220
16221 /* Set the current input_location to the end of the function
16222 so that finish_function knows where we are. */
16223 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
16224
16225 /* We don't need the local specializations any more. */
16226 htab_delete (local_specializations);
16227 local_specializations = saved_local_specializations;
16228
16229 /* Finish the function. */
16230 d = finish_function (0);
16231 expand_or_defer_fn (d);
16232 }
16233
16234 /* We're not deferring instantiation any more. */
16235 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
16236
16237 if (need_push)
16238 pop_from_top_level ();
16239
16240 out:
16241 input_location = saved_loc;
16242 pop_deferring_access_checks ();
16243 pop_tinst_level ();
16244
16245 timevar_pop (TV_PARSE);
16246
16247 return d;
16248 }
16249
16250 /* Run through the list of templates that we wish we could
16251 instantiate, and instantiate any we can. RETRIES is the
16252 number of times we retry pending template instantiation. */
16253
16254 void
16255 instantiate_pending_templates (int retries)
16256 {
16257 int reconsider;
16258 location_t saved_loc = input_location;
16259
16260 /* Instantiating templates may trigger vtable generation. This in turn
16261 may require further template instantiations. We place a limit here
16262 to avoid infinite loop. */
16263 if (pending_templates && retries >= max_tinst_depth)
16264 {
16265 tree decl = pending_templates->tinst->decl;
16266
16267 error ("template instantiation depth exceeds maximum of %d"
16268 " instantiating %q+D, possibly from virtual table generation"
16269 " (use -ftemplate-depth-NN to increase the maximum)",
16270 max_tinst_depth, decl);
16271 if (TREE_CODE (decl) == FUNCTION_DECL)
16272 /* Pretend that we defined it. */
16273 DECL_INITIAL (decl) = error_mark_node;
16274 return;
16275 }
16276
16277 do
16278 {
16279 struct pending_template **t = &pending_templates;
16280 struct pending_template *last = NULL;
16281 reconsider = 0;
16282 while (*t)
16283 {
16284 tree instantiation = reopen_tinst_level ((*t)->tinst);
16285 bool complete = false;
16286
16287 if (TYPE_P (instantiation))
16288 {
16289 tree fn;
16290
16291 if (!COMPLETE_TYPE_P (instantiation))
16292 {
16293 instantiate_class_template (instantiation);
16294 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
16295 for (fn = TYPE_METHODS (instantiation);
16296 fn;
16297 fn = TREE_CHAIN (fn))
16298 if (! DECL_ARTIFICIAL (fn))
16299 instantiate_decl (fn,
16300 /*defer_ok=*/0,
16301 /*expl_inst_class_mem_p=*/false);
16302 if (COMPLETE_TYPE_P (instantiation))
16303 reconsider = 1;
16304 }
16305
16306 complete = COMPLETE_TYPE_P (instantiation);
16307 }
16308 else
16309 {
16310 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
16311 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
16312 {
16313 instantiation
16314 = instantiate_decl (instantiation,
16315 /*defer_ok=*/0,
16316 /*expl_inst_class_mem_p=*/false);
16317 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
16318 reconsider = 1;
16319 }
16320
16321 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
16322 || DECL_TEMPLATE_INSTANTIATED (instantiation));
16323 }
16324
16325 if (complete)
16326 /* If INSTANTIATION has been instantiated, then we don't
16327 need to consider it again in the future. */
16328 *t = (*t)->next;
16329 else
16330 {
16331 last = *t;
16332 t = &(*t)->next;
16333 }
16334 tinst_depth = 0;
16335 current_tinst_level = NULL;
16336 }
16337 last_pending_template = last;
16338 }
16339 while (reconsider);
16340
16341 input_location = saved_loc;
16342 }
16343
16344 /* Substitute ARGVEC into T, which is a list of initializers for
16345 either base class or a non-static data member. The TREE_PURPOSEs
16346 are DECLs, and the TREE_VALUEs are the initializer values. Used by
16347 instantiate_decl. */
16348
16349 static tree
16350 tsubst_initializer_list (tree t, tree argvec)
16351 {
16352 tree inits = NULL_TREE;
16353
16354 for (; t; t = TREE_CHAIN (t))
16355 {
16356 tree decl;
16357 tree init;
16358 tree expanded_bases = NULL_TREE;
16359 tree expanded_arguments = NULL_TREE;
16360 int i, len = 1;
16361
16362 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
16363 {
16364 tree expr;
16365 tree arg;
16366
16367 /* Expand the base class expansion type into separate base
16368 classes. */
16369 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
16370 tf_warning_or_error,
16371 NULL_TREE);
16372 if (expanded_bases == error_mark_node)
16373 continue;
16374
16375 /* We'll be building separate TREE_LISTs of arguments for
16376 each base. */
16377 len = TREE_VEC_LENGTH (expanded_bases);
16378 expanded_arguments = make_tree_vec (len);
16379 for (i = 0; i < len; i++)
16380 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
16381
16382 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
16383 expand each argument in the TREE_VALUE of t. */
16384 expr = make_node (EXPR_PACK_EXPANSION);
16385 PACK_EXPANSION_PARAMETER_PACKS (expr) =
16386 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
16387
16388 if (TREE_VALUE (t) == void_type_node)
16389 /* VOID_TYPE_NODE is used to indicate
16390 value-initialization. */
16391 {
16392 for (i = 0; i < len; i++)
16393 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
16394 }
16395 else
16396 {
16397 /* Substitute parameter packs into each argument in the
16398 TREE_LIST. */
16399 in_base_initializer = 1;
16400 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
16401 {
16402 tree expanded_exprs;
16403
16404 /* Expand the argument. */
16405 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
16406 expanded_exprs
16407 = tsubst_pack_expansion (expr, argvec,
16408 tf_warning_or_error,
16409 NULL_TREE);
16410 if (expanded_exprs == error_mark_node)
16411 continue;
16412
16413 /* Prepend each of the expanded expressions to the
16414 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
16415 for (i = 0; i < len; i++)
16416 {
16417 TREE_VEC_ELT (expanded_arguments, i) =
16418 tree_cons (NULL_TREE,
16419 TREE_VEC_ELT (expanded_exprs, i),
16420 TREE_VEC_ELT (expanded_arguments, i));
16421 }
16422 }
16423 in_base_initializer = 0;
16424
16425 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
16426 since we built them backwards. */
16427 for (i = 0; i < len; i++)
16428 {
16429 TREE_VEC_ELT (expanded_arguments, i) =
16430 nreverse (TREE_VEC_ELT (expanded_arguments, i));
16431 }
16432 }
16433 }
16434
16435 for (i = 0; i < len; ++i)
16436 {
16437 if (expanded_bases)
16438 {
16439 decl = TREE_VEC_ELT (expanded_bases, i);
16440 decl = expand_member_init (decl);
16441 init = TREE_VEC_ELT (expanded_arguments, i);
16442 }
16443 else
16444 {
16445 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
16446 tf_warning_or_error, NULL_TREE);
16447
16448 decl = expand_member_init (decl);
16449 if (decl && !DECL_P (decl))
16450 in_base_initializer = 1;
16451
16452 init = tsubst_expr (TREE_VALUE (t), argvec,
16453 tf_warning_or_error, NULL_TREE,
16454 /*integral_constant_expression_p=*/false);
16455 in_base_initializer = 0;
16456 }
16457
16458 if (decl)
16459 {
16460 init = build_tree_list (decl, init);
16461 TREE_CHAIN (init) = inits;
16462 inits = init;
16463 }
16464 }
16465 }
16466 return inits;
16467 }
16468
16469 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
16470
16471 static void
16472 set_current_access_from_decl (tree decl)
16473 {
16474 if (TREE_PRIVATE (decl))
16475 current_access_specifier = access_private_node;
16476 else if (TREE_PROTECTED (decl))
16477 current_access_specifier = access_protected_node;
16478 else
16479 current_access_specifier = access_public_node;
16480 }
16481
16482 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
16483 is the instantiation (which should have been created with
16484 start_enum) and ARGS are the template arguments to use. */
16485
16486 static void
16487 tsubst_enum (tree tag, tree newtag, tree args)
16488 {
16489 tree e;
16490
16491 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
16492 {
16493 tree value;
16494 tree decl;
16495
16496 decl = TREE_VALUE (e);
16497 /* Note that in a template enum, the TREE_VALUE is the
16498 CONST_DECL, not the corresponding INTEGER_CST. */
16499 value = tsubst_expr (DECL_INITIAL (decl),
16500 args, tf_warning_or_error, NULL_TREE,
16501 /*integral_constant_expression_p=*/true);
16502
16503 /* Give this enumeration constant the correct access. */
16504 set_current_access_from_decl (decl);
16505
16506 /* Actually build the enumerator itself. */
16507 build_enumerator (DECL_NAME (decl), value, newtag);
16508 }
16509
16510 finish_enum (newtag);
16511 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
16512 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
16513 }
16514
16515 /* DECL is a FUNCTION_DECL that is a template specialization. Return
16516 its type -- but without substituting the innermost set of template
16517 arguments. So, innermost set of template parameters will appear in
16518 the type. */
16519
16520 tree
16521 get_mostly_instantiated_function_type (tree decl)
16522 {
16523 tree fn_type;
16524 tree tmpl;
16525 tree targs;
16526 tree tparms;
16527 int parm_depth;
16528
16529 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16530 targs = DECL_TI_ARGS (decl);
16531 tparms = DECL_TEMPLATE_PARMS (tmpl);
16532 parm_depth = TMPL_PARMS_DEPTH (tparms);
16533
16534 /* There should be as many levels of arguments as there are levels
16535 of parameters. */
16536 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
16537
16538 fn_type = TREE_TYPE (tmpl);
16539
16540 if (parm_depth == 1)
16541 /* No substitution is necessary. */
16542 ;
16543 else
16544 {
16545 int i, save_access_control;
16546 tree partial_args;
16547
16548 /* Replace the innermost level of the TARGS with NULL_TREEs to
16549 let tsubst know not to substitute for those parameters. */
16550 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
16551 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
16552 SET_TMPL_ARGS_LEVEL (partial_args, i,
16553 TMPL_ARGS_LEVEL (targs, i));
16554 SET_TMPL_ARGS_LEVEL (partial_args,
16555 TMPL_ARGS_DEPTH (targs),
16556 make_tree_vec (DECL_NTPARMS (tmpl)));
16557
16558 /* Disable access control as this function is used only during
16559 name-mangling. */
16560 save_access_control = flag_access_control;
16561 flag_access_control = 0;
16562
16563 ++processing_template_decl;
16564 /* Now, do the (partial) substitution to figure out the
16565 appropriate function type. */
16566 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
16567 --processing_template_decl;
16568
16569 /* Substitute into the template parameters to obtain the real
16570 innermost set of parameters. This step is important if the
16571 innermost set of template parameters contains value
16572 parameters whose types depend on outer template parameters. */
16573 TREE_VEC_LENGTH (partial_args)--;
16574 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
16575
16576 flag_access_control = save_access_control;
16577 }
16578
16579 return fn_type;
16580 }
16581
16582 /* Return truthvalue if we're processing a template different from
16583 the last one involved in diagnostics. */
16584 int
16585 problematic_instantiation_changed (void)
16586 {
16587 return last_template_error_tick != tinst_level_tick;
16588 }
16589
16590 /* Remember current template involved in diagnostics. */
16591 void
16592 record_last_problematic_instantiation (void)
16593 {
16594 last_template_error_tick = tinst_level_tick;
16595 }
16596
16597 struct tinst_level *
16598 current_instantiation (void)
16599 {
16600 return current_tinst_level;
16601 }
16602
16603 /* [temp.param] Check that template non-type parm TYPE is of an allowable
16604 type. Return zero for ok, nonzero for disallowed. Issue error and
16605 warning messages under control of COMPLAIN. */
16606
16607 static int
16608 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
16609 {
16610 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
16611 return 0;
16612 else if (POINTER_TYPE_P (type))
16613 return 0;
16614 else if (TYPE_PTR_TO_MEMBER_P (type))
16615 return 0;
16616 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16617 return 0;
16618 else if (TREE_CODE (type) == TYPENAME_TYPE)
16619 return 0;
16620
16621 if (complain & tf_error)
16622 error ("%q#T is not a valid type for a template constant parameter", type);
16623 return 1;
16624 }
16625
16626 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
16627 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
16628
16629 static bool
16630 dependent_type_p_r (tree type)
16631 {
16632 tree scope;
16633
16634 /* [temp.dep.type]
16635
16636 A type is dependent if it is:
16637
16638 -- a template parameter. Template template parameters are types
16639 for us (since TYPE_P holds true for them) so we handle
16640 them here. */
16641 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16642 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
16643 return true;
16644 /* -- a qualified-id with a nested-name-specifier which contains a
16645 class-name that names a dependent type or whose unqualified-id
16646 names a dependent type. */
16647 if (TREE_CODE (type) == TYPENAME_TYPE)
16648 return true;
16649 /* -- a cv-qualified type where the cv-unqualified type is
16650 dependent. */
16651 type = TYPE_MAIN_VARIANT (type);
16652 /* -- a compound type constructed from any dependent type. */
16653 if (TYPE_PTR_TO_MEMBER_P (type))
16654 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
16655 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
16656 (type)));
16657 else if (TREE_CODE (type) == POINTER_TYPE
16658 || TREE_CODE (type) == REFERENCE_TYPE)
16659 return dependent_type_p (TREE_TYPE (type));
16660 else if (TREE_CODE (type) == FUNCTION_TYPE
16661 || TREE_CODE (type) == METHOD_TYPE)
16662 {
16663 tree arg_type;
16664
16665 if (dependent_type_p (TREE_TYPE (type)))
16666 return true;
16667 for (arg_type = TYPE_ARG_TYPES (type);
16668 arg_type;
16669 arg_type = TREE_CHAIN (arg_type))
16670 if (dependent_type_p (TREE_VALUE (arg_type)))
16671 return true;
16672 return false;
16673 }
16674 /* -- an array type constructed from any dependent type or whose
16675 size is specified by a constant expression that is
16676 value-dependent. */
16677 if (TREE_CODE (type) == ARRAY_TYPE)
16678 {
16679 if (TYPE_DOMAIN (type)
16680 && dependent_type_p (TYPE_DOMAIN (type)))
16681 return true;
16682 return dependent_type_p (TREE_TYPE (type));
16683 }
16684 else if (TREE_CODE (type) == INTEGER_TYPE
16685 && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
16686 {
16687 /* If this is the TYPE_DOMAIN of an array type, consider it
16688 dependent. We already checked for value-dependence in
16689 compute_array_index_type. */
16690 return type_dependent_expression_p (TYPE_MAX_VALUE (type));
16691 }
16692
16693 /* -- a template-id in which either the template name is a template
16694 parameter ... */
16695 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16696 return true;
16697 /* ... or any of the template arguments is a dependent type or
16698 an expression that is type-dependent or value-dependent. */
16699 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
16700 && (any_dependent_template_arguments_p
16701 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
16702 return true;
16703
16704 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
16705 argument of the `typeof' expression is not type-dependent, then
16706 it should already been have resolved. */
16707 if (TREE_CODE (type) == TYPEOF_TYPE
16708 || TREE_CODE (type) == DECLTYPE_TYPE)
16709 return true;
16710
16711 /* A template argument pack is dependent if any of its packed
16712 arguments are. */
16713 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
16714 {
16715 tree args = ARGUMENT_PACK_ARGS (type);
16716 int i, len = TREE_VEC_LENGTH (args);
16717 for (i = 0; i < len; ++i)
16718 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
16719 return true;
16720 }
16721
16722 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
16723 be template parameters. */
16724 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
16725 return true;
16726
16727 /* The standard does not specifically mention types that are local
16728 to template functions or local classes, but they should be
16729 considered dependent too. For example:
16730
16731 template <int I> void f() {
16732 enum E { a = I };
16733 S<sizeof (E)> s;
16734 }
16735
16736 The size of `E' cannot be known until the value of `I' has been
16737 determined. Therefore, `E' must be considered dependent. */
16738 scope = TYPE_CONTEXT (type);
16739 if (scope && TYPE_P (scope))
16740 return dependent_type_p (scope);
16741 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
16742 return type_dependent_expression_p (scope);
16743
16744 /* Other types are non-dependent. */
16745 return false;
16746 }
16747
16748 /* Returns TRUE if TYPE is dependent, in the sense of
16749 [temp.dep.type]. */
16750
16751 bool
16752 dependent_type_p (tree type)
16753 {
16754 /* If there are no template parameters in scope, then there can't be
16755 any dependent types. */
16756 if (!processing_template_decl)
16757 {
16758 /* If we are not processing a template, then nobody should be
16759 providing us with a dependent type. */
16760 gcc_assert (type);
16761 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
16762 return false;
16763 }
16764
16765 /* If the type is NULL, we have not computed a type for the entity
16766 in question; in that case, the type is dependent. */
16767 if (!type)
16768 return true;
16769
16770 /* Erroneous types can be considered non-dependent. */
16771 if (type == error_mark_node)
16772 return false;
16773
16774 /* If we have not already computed the appropriate value for TYPE,
16775 do so now. */
16776 if (!TYPE_DEPENDENT_P_VALID (type))
16777 {
16778 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
16779 TYPE_DEPENDENT_P_VALID (type) = 1;
16780 }
16781
16782 return TYPE_DEPENDENT_P (type);
16783 }
16784
16785 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
16786 lookup. In other words, a dependent type that is not the current
16787 instantiation. */
16788
16789 bool
16790 dependent_scope_p (tree scope)
16791 {
16792 return (scope && TYPE_P (scope) && dependent_type_p (scope)
16793 && !currently_open_class (scope));
16794 }
16795
16796 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
16797
16798 static bool
16799 dependent_scope_ref_p (tree expression, bool criterion (tree))
16800 {
16801 tree scope;
16802 tree name;
16803
16804 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
16805
16806 if (!TYPE_P (TREE_OPERAND (expression, 0)))
16807 return true;
16808
16809 scope = TREE_OPERAND (expression, 0);
16810 name = TREE_OPERAND (expression, 1);
16811
16812 /* [temp.dep.expr]
16813
16814 An id-expression is type-dependent if it contains a
16815 nested-name-specifier that contains a class-name that names a
16816 dependent type. */
16817 /* The suggested resolution to Core Issue 224 implies that if the
16818 qualifying type is the current class, then we must peek
16819 inside it. */
16820 if (DECL_P (name)
16821 && currently_open_class (scope)
16822 && !criterion (name))
16823 return false;
16824 if (dependent_type_p (scope))
16825 return true;
16826
16827 return false;
16828 }
16829
16830 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
16831 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
16832 expression. */
16833
16834 bool
16835 value_dependent_expression_p (tree expression)
16836 {
16837 if (!processing_template_decl)
16838 return false;
16839
16840 /* A name declared with a dependent type. */
16841 if (DECL_P (expression) && type_dependent_expression_p (expression))
16842 return true;
16843
16844 switch (TREE_CODE (expression))
16845 {
16846 case IDENTIFIER_NODE:
16847 /* A name that has not been looked up -- must be dependent. */
16848 return true;
16849
16850 case TEMPLATE_PARM_INDEX:
16851 /* A non-type template parm. */
16852 return true;
16853
16854 case CONST_DECL:
16855 /* A non-type template parm. */
16856 if (DECL_TEMPLATE_PARM_P (expression))
16857 return true;
16858 return value_dependent_expression_p (DECL_INITIAL (expression));
16859
16860 case VAR_DECL:
16861 /* A constant with integral or enumeration type and is initialized
16862 with an expression that is value-dependent. */
16863 if (DECL_INITIAL (expression)
16864 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
16865 && value_dependent_expression_p (DECL_INITIAL (expression)))
16866 return true;
16867 return false;
16868
16869 case DYNAMIC_CAST_EXPR:
16870 case STATIC_CAST_EXPR:
16871 case CONST_CAST_EXPR:
16872 case REINTERPRET_CAST_EXPR:
16873 case CAST_EXPR:
16874 /* These expressions are value-dependent if the type to which
16875 the cast occurs is dependent or the expression being casted
16876 is value-dependent. */
16877 {
16878 tree type = TREE_TYPE (expression);
16879
16880 if (dependent_type_p (type))
16881 return true;
16882
16883 /* A functional cast has a list of operands. */
16884 expression = TREE_OPERAND (expression, 0);
16885 if (!expression)
16886 {
16887 /* If there are no operands, it must be an expression such
16888 as "int()". This should not happen for aggregate types
16889 because it would form non-constant expressions. */
16890 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
16891
16892 return false;
16893 }
16894
16895 if (TREE_CODE (expression) == TREE_LIST)
16896 return any_value_dependent_elements_p (expression);
16897
16898 return value_dependent_expression_p (expression);
16899 }
16900
16901 case SIZEOF_EXPR:
16902 case ALIGNOF_EXPR:
16903 /* A `sizeof' expression is value-dependent if the operand is
16904 type-dependent or is a pack expansion. */
16905 expression = TREE_OPERAND (expression, 0);
16906 if (PACK_EXPANSION_P (expression))
16907 return true;
16908 else if (TYPE_P (expression))
16909 return dependent_type_p (expression);
16910 return type_dependent_expression_p (expression);
16911
16912 case SCOPE_REF:
16913 return dependent_scope_ref_p (expression, value_dependent_expression_p);
16914
16915 case COMPONENT_REF:
16916 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
16917 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
16918
16919 case CALL_EXPR:
16920 /* A CALL_EXPR may appear in a constant expression if it is a
16921 call to a builtin function, e.g., __builtin_constant_p. All
16922 such calls are value-dependent. */
16923 return true;
16924
16925 case NONTYPE_ARGUMENT_PACK:
16926 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
16927 is value-dependent. */
16928 {
16929 tree values = ARGUMENT_PACK_ARGS (expression);
16930 int i, len = TREE_VEC_LENGTH (values);
16931
16932 for (i = 0; i < len; ++i)
16933 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
16934 return true;
16935
16936 return false;
16937 }
16938
16939 case TRAIT_EXPR:
16940 {
16941 tree type2 = TRAIT_EXPR_TYPE2 (expression);
16942 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
16943 || (type2 ? dependent_type_p (type2) : false));
16944 }
16945
16946 case MODOP_EXPR:
16947 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
16948 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
16949
16950 default:
16951 /* A constant expression is value-dependent if any subexpression is
16952 value-dependent. */
16953 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
16954 {
16955 case tcc_reference:
16956 case tcc_unary:
16957 return (value_dependent_expression_p
16958 (TREE_OPERAND (expression, 0)));
16959
16960 case tcc_comparison:
16961 case tcc_binary:
16962 return ((value_dependent_expression_p
16963 (TREE_OPERAND (expression, 0)))
16964 || (value_dependent_expression_p
16965 (TREE_OPERAND (expression, 1))));
16966
16967 case tcc_expression:
16968 case tcc_vl_exp:
16969 {
16970 int i;
16971 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
16972 /* In some cases, some of the operands may be missing.
16973 (For example, in the case of PREDECREMENT_EXPR, the
16974 amount to increment by may be missing.) That doesn't
16975 make the expression dependent. */
16976 if (TREE_OPERAND (expression, i)
16977 && (value_dependent_expression_p
16978 (TREE_OPERAND (expression, i))))
16979 return true;
16980 return false;
16981 }
16982
16983 default:
16984 break;
16985 }
16986 }
16987
16988 /* The expression is not value-dependent. */
16989 return false;
16990 }
16991
16992 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
16993 [temp.dep.expr]. */
16994
16995 bool
16996 type_dependent_expression_p (tree expression)
16997 {
16998 if (!processing_template_decl)
16999 return false;
17000
17001 if (expression == error_mark_node)
17002 return false;
17003
17004 /* An unresolved name is always dependent. */
17005 if (TREE_CODE (expression) == IDENTIFIER_NODE
17006 || TREE_CODE (expression) == USING_DECL)
17007 return true;
17008
17009 /* Some expression forms are never type-dependent. */
17010 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
17011 || TREE_CODE (expression) == SIZEOF_EXPR
17012 || TREE_CODE (expression) == ALIGNOF_EXPR
17013 || TREE_CODE (expression) == TRAIT_EXPR
17014 || TREE_CODE (expression) == TYPEID_EXPR
17015 || TREE_CODE (expression) == DELETE_EXPR
17016 || TREE_CODE (expression) == VEC_DELETE_EXPR
17017 || TREE_CODE (expression) == THROW_EXPR)
17018 return false;
17019
17020 /* The types of these expressions depends only on the type to which
17021 the cast occurs. */
17022 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
17023 || TREE_CODE (expression) == STATIC_CAST_EXPR
17024 || TREE_CODE (expression) == CONST_CAST_EXPR
17025 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
17026 || TREE_CODE (expression) == CAST_EXPR)
17027 return dependent_type_p (TREE_TYPE (expression));
17028
17029 /* The types of these expressions depends only on the type created
17030 by the expression. */
17031 if (TREE_CODE (expression) == NEW_EXPR
17032 || TREE_CODE (expression) == VEC_NEW_EXPR)
17033 {
17034 /* For NEW_EXPR tree nodes created inside a template, either
17035 the object type itself or a TREE_LIST may appear as the
17036 operand 1. */
17037 tree type = TREE_OPERAND (expression, 1);
17038 if (TREE_CODE (type) == TREE_LIST)
17039 /* This is an array type. We need to check array dimensions
17040 as well. */
17041 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
17042 || value_dependent_expression_p
17043 (TREE_OPERAND (TREE_VALUE (type), 1));
17044 else
17045 return dependent_type_p (type);
17046 }
17047
17048 if (TREE_CODE (expression) == SCOPE_REF
17049 && dependent_scope_ref_p (expression,
17050 type_dependent_expression_p))
17051 return true;
17052
17053 if (TREE_CODE (expression) == FUNCTION_DECL
17054 && DECL_LANG_SPECIFIC (expression)
17055 && DECL_TEMPLATE_INFO (expression)
17056 && (any_dependent_template_arguments_p
17057 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
17058 return true;
17059
17060 if (TREE_CODE (expression) == TEMPLATE_DECL
17061 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
17062 return false;
17063
17064 if (TREE_CODE (expression) == STMT_EXPR)
17065 expression = stmt_expr_value_expr (expression);
17066
17067 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
17068 {
17069 tree elt;
17070 unsigned i;
17071
17072 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
17073 {
17074 if (type_dependent_expression_p (elt))
17075 return true;
17076 }
17077 return false;
17078 }
17079
17080 if (TREE_TYPE (expression) == unknown_type_node)
17081 {
17082 if (TREE_CODE (expression) == ADDR_EXPR)
17083 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
17084 if (TREE_CODE (expression) == COMPONENT_REF
17085 || TREE_CODE (expression) == OFFSET_REF)
17086 {
17087 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
17088 return true;
17089 expression = TREE_OPERAND (expression, 1);
17090 if (TREE_CODE (expression) == IDENTIFIER_NODE)
17091 return false;
17092 }
17093 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17094 if (TREE_CODE (expression) == SCOPE_REF)
17095 return false;
17096
17097 if (TREE_CODE (expression) == BASELINK)
17098 expression = BASELINK_FUNCTIONS (expression);
17099
17100 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
17101 {
17102 if (any_dependent_template_arguments_p
17103 (TREE_OPERAND (expression, 1)))
17104 return true;
17105 expression = TREE_OPERAND (expression, 0);
17106 }
17107 gcc_assert (TREE_CODE (expression) == OVERLOAD
17108 || TREE_CODE (expression) == FUNCTION_DECL);
17109
17110 while (expression)
17111 {
17112 if (type_dependent_expression_p (OVL_CURRENT (expression)))
17113 return true;
17114 expression = OVL_NEXT (expression);
17115 }
17116 return false;
17117 }
17118
17119 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
17120
17121 return (dependent_type_p (TREE_TYPE (expression)));
17122 }
17123
17124 /* Like type_dependent_expression_p, but it also works while not processing
17125 a template definition, i.e. during substitution or mangling. */
17126
17127 bool
17128 type_dependent_expression_p_push (tree expr)
17129 {
17130 bool b;
17131 ++processing_template_decl;
17132 b = type_dependent_expression_p (expr);
17133 --processing_template_decl;
17134 return b;
17135 }
17136
17137 /* Returns TRUE if ARGS contains a type-dependent expression. */
17138
17139 bool
17140 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
17141 {
17142 unsigned int i;
17143 tree arg;
17144
17145 for (i = 0; VEC_iterate (tree, args, i, arg); ++i)
17146 {
17147 if (type_dependent_expression_p (arg))
17148 return true;
17149 }
17150 return false;
17151 }
17152
17153 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17154 expressions) contains any value-dependent expressions. */
17155
17156 bool
17157 any_value_dependent_elements_p (const_tree list)
17158 {
17159 for (; list; list = TREE_CHAIN (list))
17160 if (value_dependent_expression_p (TREE_VALUE (list)))
17161 return true;
17162
17163 return false;
17164 }
17165
17166 /* Returns TRUE if the ARG (a template argument) is dependent. */
17167
17168 bool
17169 dependent_template_arg_p (tree arg)
17170 {
17171 if (!processing_template_decl)
17172 return false;
17173
17174 if (TREE_CODE (arg) == TEMPLATE_DECL
17175 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17176 return dependent_template_p (arg);
17177 else if (ARGUMENT_PACK_P (arg))
17178 {
17179 tree args = ARGUMENT_PACK_ARGS (arg);
17180 int i, len = TREE_VEC_LENGTH (args);
17181 for (i = 0; i < len; ++i)
17182 {
17183 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17184 return true;
17185 }
17186
17187 return false;
17188 }
17189 else if (TYPE_P (arg))
17190 return dependent_type_p (arg);
17191 else
17192 return (type_dependent_expression_p (arg)
17193 || value_dependent_expression_p (arg));
17194 }
17195
17196 /* Returns true if ARGS (a collection of template arguments) contains
17197 any types that require structural equality testing. */
17198
17199 bool
17200 any_template_arguments_need_structural_equality_p (tree args)
17201 {
17202 int i;
17203 int j;
17204
17205 if (!args)
17206 return false;
17207 if (args == error_mark_node)
17208 return true;
17209
17210 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17211 {
17212 tree level = TMPL_ARGS_LEVEL (args, i + 1);
17213 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17214 {
17215 tree arg = TREE_VEC_ELT (level, j);
17216 tree packed_args = NULL_TREE;
17217 int k, len = 1;
17218
17219 if (ARGUMENT_PACK_P (arg))
17220 {
17221 /* Look inside the argument pack. */
17222 packed_args = ARGUMENT_PACK_ARGS (arg);
17223 len = TREE_VEC_LENGTH (packed_args);
17224 }
17225
17226 for (k = 0; k < len; ++k)
17227 {
17228 if (packed_args)
17229 arg = TREE_VEC_ELT (packed_args, k);
17230
17231 if (error_operand_p (arg))
17232 return true;
17233 else if (TREE_CODE (arg) == TEMPLATE_DECL
17234 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17235 continue;
17236 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
17237 return true;
17238 else if (!TYPE_P (arg) && TREE_TYPE (arg)
17239 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
17240 return true;
17241 }
17242 }
17243 }
17244
17245 return false;
17246 }
17247
17248 /* Returns true if ARGS (a collection of template arguments) contains
17249 any dependent arguments. */
17250
17251 bool
17252 any_dependent_template_arguments_p (const_tree args)
17253 {
17254 int i;
17255 int j;
17256
17257 if (!args)
17258 return false;
17259 if (args == error_mark_node)
17260 return true;
17261
17262 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17263 {
17264 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
17265 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17266 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
17267 return true;
17268 }
17269
17270 return false;
17271 }
17272
17273 /* Returns TRUE if the template TMPL is dependent. */
17274
17275 bool
17276 dependent_template_p (tree tmpl)
17277 {
17278 if (TREE_CODE (tmpl) == OVERLOAD)
17279 {
17280 while (tmpl)
17281 {
17282 if (dependent_template_p (OVL_FUNCTION (tmpl)))
17283 return true;
17284 tmpl = OVL_CHAIN (tmpl);
17285 }
17286 return false;
17287 }
17288
17289 /* Template template parameters are dependent. */
17290 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
17291 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
17292 return true;
17293 /* So are names that have not been looked up. */
17294 if (TREE_CODE (tmpl) == SCOPE_REF
17295 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
17296 return true;
17297 /* So are member templates of dependent classes. */
17298 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
17299 return dependent_type_p (DECL_CONTEXT (tmpl));
17300 return false;
17301 }
17302
17303 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
17304
17305 bool
17306 dependent_template_id_p (tree tmpl, tree args)
17307 {
17308 return (dependent_template_p (tmpl)
17309 || any_dependent_template_arguments_p (args));
17310 }
17311
17312 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
17313 is dependent. */
17314
17315 bool
17316 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
17317 {
17318 int i;
17319
17320 if (!processing_template_decl)
17321 return false;
17322
17323 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
17324 {
17325 tree decl = TREE_VEC_ELT (declv, i);
17326 tree init = TREE_VEC_ELT (initv, i);
17327 tree cond = TREE_VEC_ELT (condv, i);
17328 tree incr = TREE_VEC_ELT (incrv, i);
17329
17330 if (type_dependent_expression_p (decl))
17331 return true;
17332
17333 if (init && type_dependent_expression_p (init))
17334 return true;
17335
17336 if (type_dependent_expression_p (cond))
17337 return true;
17338
17339 if (COMPARISON_CLASS_P (cond)
17340 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
17341 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
17342 return true;
17343
17344 if (TREE_CODE (incr) == MODOP_EXPR)
17345 {
17346 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
17347 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
17348 return true;
17349 }
17350 else if (type_dependent_expression_p (incr))
17351 return true;
17352 else if (TREE_CODE (incr) == MODIFY_EXPR)
17353 {
17354 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
17355 return true;
17356 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
17357 {
17358 tree t = TREE_OPERAND (incr, 1);
17359 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
17360 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
17361 return true;
17362 }
17363 }
17364 }
17365
17366 return false;
17367 }
17368
17369 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
17370 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
17371 no such TYPE can be found. Note that this function peers inside
17372 uninstantiated templates and therefore should be used only in
17373 extremely limited situations. ONLY_CURRENT_P restricts this
17374 peering to the currently open classes hierarchy (which is required
17375 when comparing types). */
17376
17377 tree
17378 resolve_typename_type (tree type, bool only_current_p)
17379 {
17380 tree scope;
17381 tree name;
17382 tree decl;
17383 int quals;
17384 tree pushed_scope;
17385 tree result;
17386
17387 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
17388
17389 scope = TYPE_CONTEXT (type);
17390 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
17391 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
17392 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
17393 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
17394 identifier of the TYPENAME_TYPE anymore.
17395 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
17396 TYPENAME_TYPE instead, we avoid messing up with a possible
17397 typedef variant case. */
17398 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
17399
17400 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
17401 it first before we can figure out what NAME refers to. */
17402 if (TREE_CODE (scope) == TYPENAME_TYPE)
17403 scope = resolve_typename_type (scope, only_current_p);
17404 /* If we don't know what SCOPE refers to, then we cannot resolve the
17405 TYPENAME_TYPE. */
17406 if (TREE_CODE (scope) == TYPENAME_TYPE)
17407 return type;
17408 /* If the SCOPE is a template type parameter, we have no way of
17409 resolving the name. */
17410 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
17411 return type;
17412 /* If the SCOPE is not the current instantiation, there's no reason
17413 to look inside it. */
17414 if (only_current_p && !currently_open_class (scope))
17415 return type;
17416 /* If SCOPE isn't the template itself, it will not have a valid
17417 TYPE_FIELDS list. */
17418 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
17419 /* scope is either the template itself or a compatible instantiation
17420 like X<T>, so look up the name in the original template. */
17421 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
17422 else
17423 /* scope is a partial instantiation, so we can't do the lookup or we
17424 will lose the template arguments. */
17425 return type;
17426 /* Enter the SCOPE so that name lookup will be resolved as if we
17427 were in the class definition. In particular, SCOPE will no
17428 longer be considered a dependent type. */
17429 pushed_scope = push_scope (scope);
17430 /* Look up the declaration. */
17431 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
17432
17433 result = NULL_TREE;
17434
17435 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
17436 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
17437 if (!decl)
17438 /*nop*/;
17439 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
17440 && TREE_CODE (decl) == TYPE_DECL)
17441 {
17442 result = TREE_TYPE (decl);
17443 if (result == error_mark_node)
17444 result = NULL_TREE;
17445 }
17446 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
17447 && DECL_CLASS_TEMPLATE_P (decl))
17448 {
17449 tree tmpl;
17450 tree args;
17451 /* Obtain the template and the arguments. */
17452 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
17453 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
17454 /* Instantiate the template. */
17455 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
17456 /*entering_scope=*/0,
17457 tf_error | tf_user);
17458 if (result == error_mark_node)
17459 result = NULL_TREE;
17460 }
17461
17462 /* Leave the SCOPE. */
17463 if (pushed_scope)
17464 pop_scope (pushed_scope);
17465
17466 /* If we failed to resolve it, return the original typename. */
17467 if (!result)
17468 return type;
17469
17470 /* If lookup found a typename type, resolve that too. */
17471 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
17472 {
17473 /* Ill-formed programs can cause infinite recursion here, so we
17474 must catch that. */
17475 TYPENAME_IS_RESOLVING_P (type) = 1;
17476 result = resolve_typename_type (result, only_current_p);
17477 TYPENAME_IS_RESOLVING_P (type) = 0;
17478 }
17479
17480 /* Qualify the resulting type. */
17481 quals = cp_type_quals (type);
17482 if (quals)
17483 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
17484
17485 return result;
17486 }
17487
17488 /* EXPR is an expression which is not type-dependent. Return a proxy
17489 for EXPR that can be used to compute the types of larger
17490 expressions containing EXPR. */
17491
17492 tree
17493 build_non_dependent_expr (tree expr)
17494 {
17495 tree inner_expr;
17496
17497 /* Preserve null pointer constants so that the type of things like
17498 "p == 0" where "p" is a pointer can be determined. */
17499 if (null_ptr_cst_p (expr))
17500 return expr;
17501 /* Preserve OVERLOADs; the functions must be available to resolve
17502 types. */
17503 inner_expr = expr;
17504 if (TREE_CODE (inner_expr) == STMT_EXPR)
17505 inner_expr = stmt_expr_value_expr (inner_expr);
17506 if (TREE_CODE (inner_expr) == ADDR_EXPR)
17507 inner_expr = TREE_OPERAND (inner_expr, 0);
17508 if (TREE_CODE (inner_expr) == COMPONENT_REF)
17509 inner_expr = TREE_OPERAND (inner_expr, 1);
17510 if (is_overloaded_fn (inner_expr)
17511 || TREE_CODE (inner_expr) == OFFSET_REF)
17512 return expr;
17513 /* There is no need to return a proxy for a variable. */
17514 if (TREE_CODE (expr) == VAR_DECL)
17515 return expr;
17516 /* Preserve string constants; conversions from string constants to
17517 "char *" are allowed, even though normally a "const char *"
17518 cannot be used to initialize a "char *". */
17519 if (TREE_CODE (expr) == STRING_CST)
17520 return expr;
17521 /* Preserve arithmetic constants, as an optimization -- there is no
17522 reason to create a new node. */
17523 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
17524 return expr;
17525 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
17526 There is at least one place where we want to know that a
17527 particular expression is a throw-expression: when checking a ?:
17528 expression, there are special rules if the second or third
17529 argument is a throw-expression. */
17530 if (TREE_CODE (expr) == THROW_EXPR)
17531 return expr;
17532
17533 if (TREE_CODE (expr) == COND_EXPR)
17534 return build3 (COND_EXPR,
17535 TREE_TYPE (expr),
17536 TREE_OPERAND (expr, 0),
17537 (TREE_OPERAND (expr, 1)
17538 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
17539 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
17540 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
17541 if (TREE_CODE (expr) == COMPOUND_EXPR
17542 && !COMPOUND_EXPR_OVERLOADED (expr))
17543 return build2 (COMPOUND_EXPR,
17544 TREE_TYPE (expr),
17545 TREE_OPERAND (expr, 0),
17546 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
17547
17548 /* If the type is unknown, it can't really be non-dependent */
17549 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
17550
17551 /* Otherwise, build a NON_DEPENDENT_EXPR.
17552
17553 REFERENCE_TYPEs are not stripped for expressions in templates
17554 because doing so would play havoc with mangling. Consider, for
17555 example:
17556
17557 template <typename T> void f<T& g>() { g(); }
17558
17559 In the body of "f", the expression for "g" will have
17560 REFERENCE_TYPE, even though the standard says that it should
17561 not. The reason is that we must preserve the syntactic form of
17562 the expression so that mangling (say) "f<g>" inside the body of
17563 "f" works out correctly. Therefore, the REFERENCE_TYPE is
17564 stripped here. */
17565 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
17566 }
17567
17568 /* ARGS is a vector of expressions as arguments to a function call.
17569 Replace the arguments with equivalent non-dependent expressions.
17570 This modifies ARGS in place. */
17571
17572 void
17573 make_args_non_dependent (VEC(tree,gc) *args)
17574 {
17575 unsigned int ix;
17576 tree arg;
17577
17578 for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
17579 {
17580 tree newarg = build_non_dependent_expr (arg);
17581 if (newarg != arg)
17582 VEC_replace (tree, args, ix, newarg);
17583 }
17584 }
17585
17586 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
17587 with a level one deeper than the actual template parms. */
17588
17589 tree
17590 make_auto (void)
17591 {
17592 tree au;
17593
17594 /* ??? Is it worth caching this for multiple autos at the same level? */
17595 au = cxx_make_type (TEMPLATE_TYPE_PARM);
17596 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
17597 TYPE_DECL, get_identifier ("auto"), au);
17598 TYPE_STUB_DECL (au) = TYPE_NAME (au);
17599 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
17600 (0, processing_template_decl + 1, processing_template_decl + 1,
17601 TYPE_NAME (au), NULL_TREE);
17602 TYPE_CANONICAL (au) = canonical_type_parameter (au);
17603 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
17604 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
17605
17606 return au;
17607 }
17608
17609 /* Given type ARG, return std::initializer_list<ARG>. */
17610
17611 static tree
17612 listify (tree arg)
17613 {
17614 tree std_init_list = namespace_binding
17615 (get_identifier ("initializer_list"), std_node);
17616 tree argvec;
17617 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
17618 {
17619 error ("deducing from brace-enclosed initializer list requires "
17620 "#include <initializer_list>");
17621 return error_mark_node;
17622 }
17623 argvec = make_tree_vec (1);
17624 TREE_VEC_ELT (argvec, 0) = arg;
17625 return lookup_template_class (std_init_list, argvec, NULL_TREE,
17626 NULL_TREE, 0, tf_warning_or_error);
17627 }
17628
17629 /* Replace auto in TYPE with std::initializer_list<auto>. */
17630
17631 static tree
17632 listify_autos (tree type, tree auto_node)
17633 {
17634 tree init_auto = listify (auto_node);
17635 tree argvec = make_tree_vec (1);
17636 TREE_VEC_ELT (argvec, 0) = init_auto;
17637 if (processing_template_decl)
17638 argvec = add_to_template_args (current_template_args (), argvec);
17639 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
17640 }
17641
17642 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
17643 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
17644
17645 tree
17646 do_auto_deduction (tree type, tree init, tree auto_node)
17647 {
17648 tree parms, tparms, targs;
17649 tree args[1];
17650 int val;
17651
17652 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
17653 with either a new invented type template parameter U or, if the
17654 initializer is a braced-init-list (8.5.4), with
17655 std::initializer_list<U>. */
17656 if (BRACE_ENCLOSED_INITIALIZER_P (init))
17657 type = listify_autos (type, auto_node);
17658
17659 parms = build_tree_list (NULL_TREE, type);
17660 args[0] = init;
17661 tparms = make_tree_vec (1);
17662 targs = make_tree_vec (1);
17663 TREE_VEC_ELT (tparms, 0)
17664 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
17665 val = type_unification_real (tparms, targs, parms, args, 1, 0,
17666 DEDUCE_CALL, LOOKUP_NORMAL);
17667 if (val > 0)
17668 {
17669 error ("unable to deduce %qT from %qE", type, init);
17670 return error_mark_node;
17671 }
17672
17673 if (processing_template_decl)
17674 targs = add_to_template_args (current_template_args (), targs);
17675 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
17676 }
17677
17678 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
17679 result. */
17680
17681 tree
17682 splice_late_return_type (tree type, tree late_return_type)
17683 {
17684 tree argvec;
17685
17686 if (late_return_type == NULL_TREE)
17687 return type;
17688 argvec = make_tree_vec (1);
17689 TREE_VEC_ELT (argvec, 0) = late_return_type;
17690 if (processing_template_decl)
17691 argvec = add_to_template_args (current_template_args (), argvec);
17692 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
17693 }
17694
17695 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
17696
17697 bool
17698 is_auto (const_tree type)
17699 {
17700 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17701 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
17702 return true;
17703 else
17704 return false;
17705 }
17706
17707 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
17708 appear as a type-specifier for the declaration in question, we don't
17709 have to look through the whole type. */
17710
17711 tree
17712 type_uses_auto (tree type)
17713 {
17714 enum tree_code code;
17715 if (is_auto (type))
17716 return type;
17717
17718 code = TREE_CODE (type);
17719
17720 if (code == POINTER_TYPE || code == REFERENCE_TYPE
17721 || code == OFFSET_TYPE || code == FUNCTION_TYPE
17722 || code == METHOD_TYPE || code == ARRAY_TYPE)
17723 return type_uses_auto (TREE_TYPE (type));
17724
17725 if (TYPE_PTRMEMFUNC_P (type))
17726 return type_uses_auto (TREE_TYPE (TREE_TYPE
17727 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17728
17729 return NULL_TREE;
17730 }
17731
17732 /* For a given template T, return the list of typedefs referenced
17733 in T for which access check is needed at T instantiation time.
17734 T is either a FUNCTION_DECL or a RECORD_TYPE.
17735 Those typedefs were added to T by the function
17736 append_type_to_template_for_access_check. */
17737
17738 tree
17739 get_types_needing_access_check (tree t)
17740 {
17741 tree ti, result = NULL_TREE;
17742
17743 if (!t || t == error_mark_node)
17744 return t;
17745
17746 if (!(ti = get_template_info (t)))
17747 return NULL_TREE;
17748
17749 if (CLASS_TYPE_P (t)
17750 || TREE_CODE (t) == FUNCTION_DECL)
17751 {
17752 if (!TI_TEMPLATE (ti))
17753 return NULL_TREE;
17754
17755 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
17756 }
17757
17758 return result;
17759 }
17760
17761 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
17762 tied to T. That list of typedefs will be access checked at
17763 T instantiation time.
17764 T is either a FUNCTION_DECL or a RECORD_TYPE.
17765 TYPE_DECL is a TYPE_DECL node representing a typedef.
17766 SCOPE is the scope through which TYPE_DECL is accessed.
17767
17768 This function is a subroutine of
17769 append_type_to_template_for_access_check. */
17770
17771 static void
17772 append_type_to_template_for_access_check_1 (tree t,
17773 tree type_decl,
17774 tree scope)
17775 {
17776 tree ti;
17777
17778 if (!t || t == error_mark_node)
17779 return;
17780
17781 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
17782 || CLASS_TYPE_P (t))
17783 && type_decl
17784 && TREE_CODE (type_decl) == TYPE_DECL
17785 && scope);
17786
17787 if (!(ti = get_template_info (t)))
17788 return;
17789
17790 gcc_assert (TI_TEMPLATE (ti));
17791
17792 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti) =
17793 tree_cons (type_decl, scope, TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti));
17794 }
17795
17796 /* Append TYPE_DECL to the template TEMPL.
17797 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
17798 At TEMPL instanciation time, TYPE_DECL will be checked to see
17799 if it can be accessed through SCOPE.
17800
17801 e.g. consider the following code snippet:
17802
17803 class C
17804 {
17805 typedef int myint;
17806 };
17807
17808 template<class U> struct S
17809 {
17810 C::myint mi;
17811 };
17812
17813 S<char> s;
17814
17815 At S<char> instantiation time, we need to check the access of C::myint
17816 In other words, we need to check the access of the myint typedef through
17817 the C scope. For that purpose, this function will add the myint typedef
17818 and the scope C through which its being accessed to a list of typedefs
17819 tied to the template S. That list will be walked at template instantiation
17820 time and access check performed on each typedefs it contains.
17821 Note that this particular code snippet should yield an error because
17822 myint is private to C. */
17823
17824 void
17825 append_type_to_template_for_access_check (tree templ,
17826 tree type_decl,
17827 tree scope)
17828 {
17829 tree node;
17830
17831 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
17832
17833 /* Make sure we don't append the type to the template twice. */
17834 for (node = get_types_needing_access_check (templ);
17835 node;
17836 node = TREE_CHAIN (node))
17837 {
17838 tree decl = TREE_PURPOSE (node);
17839 tree type_scope = TREE_VALUE (node);
17840
17841 if (decl == type_decl && type_scope == scope)
17842 return;
17843 }
17844
17845 append_type_to_template_for_access_check_1 (templ, type_decl, scope);
17846 }
17847
17848 /* Set up the hash tables for template instantiations. */
17849
17850 void
17851 init_template_processing (void)
17852 {
17853 decl_specializations = htab_create_ggc (37,
17854 hash_specialization,
17855 eq_specializations,
17856 ggc_free);
17857 type_specializations = htab_create_ggc (37,
17858 hash_specialization,
17859 eq_specializations,
17860 ggc_free);
17861 }
17862
17863 #include "gt-cp-pt.h"