]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/pt.c
re PR c++/40155 ([c++0x] variadic template pack problem)
[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 "intl.h"
36 #include "pointer-set.h"
37 #include "flags.h"
38 #include "c-common.h"
39 #include "cp-tree.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
42 #include "decl.h"
43 #include "output.h"
44 #include "except.h"
45 #include "toplev.h"
46 #include "rtl.h"
47 #include "timevar.h"
48 #include "tree-iterator.h"
49 #include "vecprim.h"
50
51 /* The type of functions taking a tree, and some additional data, and
52 returning an int. */
53 typedef int (*tree_fn_t) (tree, void*);
54
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY (()) pending_template {
59 struct pending_template *next;
60 struct tinst_level *tinst;
61 };
62
63 static GTY(()) struct pending_template *pending_templates;
64 static GTY(()) struct pending_template *last_pending_template;
65
66 int processing_template_parmlist;
67 static int template_header_count;
68
69 static GTY(()) tree saved_trees;
70 static VEC(int,heap) *inline_parm_levels;
71
72 static GTY(()) struct tinst_level *current_tinst_level;
73
74 static GTY(()) tree saved_access_scope;
75
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr;
80
81 /* A map from local variable declarations in the body of the template
82 presently being instantiated to the corresponding instantiated
83 local variables. */
84 static htab_t local_specializations;
85
86 typedef struct GTY(()) spec_entry
87 {
88 tree tmpl;
89 tree args;
90 tree spec;
91 } spec_entry;
92
93 static GTY ((param_is (spec_entry)))
94 htab_t decl_specializations;
95
96 static GTY ((param_is (spec_entry)))
97 htab_t type_specializations;
98
99 /* Contains canonical template parameter types. The vector is indexed by
100 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
101 TREE_LIST, whose TREE_VALUEs contain the canonical template
102 parameters of various types and levels. */
103 static GTY(()) VEC(tree,gc) *canonical_template_parms;
104
105 #define UNIFY_ALLOW_NONE 0
106 #define UNIFY_ALLOW_MORE_CV_QUAL 1
107 #define UNIFY_ALLOW_LESS_CV_QUAL 2
108 #define UNIFY_ALLOW_DERIVED 4
109 #define UNIFY_ALLOW_INTEGER 8
110 #define UNIFY_ALLOW_OUTER_LEVEL 16
111 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
112 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
113
114 static void push_access_scope (tree);
115 static void pop_access_scope (tree);
116 static bool resolve_overloaded_unification (tree, tree, tree, tree,
117 unification_kind_t, int);
118 static int try_one_overload (tree, tree, tree, tree, tree,
119 unification_kind_t, int, bool);
120 static int unify (tree, tree, tree, tree, int);
121 static void add_pending_template (tree);
122 static int push_tinst_level (tree);
123 static void pop_tinst_level (void);
124 static tree reopen_tinst_level (struct tinst_level *);
125 static tree tsubst_initializer_list (tree, tree);
126 static tree get_class_bindings (tree, tree, tree);
127 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
128 bool, bool);
129 static void tsubst_enum (tree, tree, tree);
130 static tree add_to_template_args (tree, tree);
131 static tree add_outermost_template_args (tree, tree);
132 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
133 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
134 tree);
135 static int type_unification_real (tree, tree, tree, const tree *,
136 unsigned int, int, unification_kind_t, int);
137 static void note_template_header (int);
138 static tree convert_nontype_argument_function (tree, tree);
139 static tree convert_nontype_argument (tree, tree);
140 static tree convert_template_argument (tree, tree, tree,
141 tsubst_flags_t, int, tree);
142 static int for_each_template_parm (tree, tree_fn_t, void*,
143 struct pointer_set_t*, bool);
144 static tree expand_template_argument_pack (tree);
145 static tree build_template_parm_index (int, int, int, tree, tree);
146 static bool inline_needs_template_parms (tree);
147 static void push_inline_template_parms_recursive (tree, int);
148 static tree retrieve_local_specialization (tree);
149 static void register_local_specialization (tree, tree);
150 static hashval_t hash_specialization (const void *p);
151 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
152 static int mark_template_parm (tree, void *);
153 static int template_parm_this_level_p (tree, void *);
154 static tree tsubst_friend_function (tree, tree);
155 static tree tsubst_friend_class (tree, tree);
156 static int can_complete_type_without_circularity (tree);
157 static tree get_bindings (tree, tree, tree, bool);
158 static int template_decl_level (tree);
159 static int check_cv_quals_for_unify (int, tree, tree);
160 static void template_parm_level_and_index (tree, int*, int*);
161 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
162 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
163 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
164 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
165 static void regenerate_decl_from_template (tree, tree);
166 static tree most_specialized_class (tree, tree);
167 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
168 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
169 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
170 static bool check_specialization_scope (void);
171 static tree process_partial_specialization (tree);
172 static void set_current_access_from_decl (tree);
173 static tree get_template_base (tree, tree, tree, tree);
174 static tree try_class_unification (tree, tree, tree, tree);
175 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
176 tree, tree);
177 static bool template_template_parm_bindings_ok_p (tree, tree);
178 static int template_args_equal (tree, tree);
179 static void tsubst_default_arguments (tree);
180 static tree for_each_template_parm_r (tree *, int *, void *);
181 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
182 static void copy_default_args_to_explicit_spec (tree);
183 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
184 static int eq_local_specializations (const void *, const void *);
185 static bool dependent_template_arg_p (tree);
186 static bool any_template_arguments_need_structural_equality_p (tree);
187 static bool dependent_type_p_r (tree);
188 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
189 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
190 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
191 static tree tsubst_decl (tree, tree, tsubst_flags_t);
192 static void perform_typedefs_access_check (tree tmpl, tree targs);
193 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
194 location_t);
195 static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
196 static tree listify (tree);
197 static tree listify_autos (tree, tree);
198
199 /* Make the current scope suitable for access checking when we are
200 processing T. T can be FUNCTION_DECL for instantiated function
201 template, or VAR_DECL for static member variable (need by
202 instantiate_decl). */
203
204 static void
205 push_access_scope (tree t)
206 {
207 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
208 || TREE_CODE (t) == VAR_DECL);
209
210 if (DECL_FRIEND_CONTEXT (t))
211 push_nested_class (DECL_FRIEND_CONTEXT (t));
212 else if (DECL_CLASS_SCOPE_P (t))
213 push_nested_class (DECL_CONTEXT (t));
214 else
215 push_to_top_level ();
216
217 if (TREE_CODE (t) == FUNCTION_DECL)
218 {
219 saved_access_scope = tree_cons
220 (NULL_TREE, current_function_decl, saved_access_scope);
221 current_function_decl = t;
222 }
223 }
224
225 /* Restore the scope set up by push_access_scope. T is the node we
226 are processing. */
227
228 static void
229 pop_access_scope (tree t)
230 {
231 if (TREE_CODE (t) == FUNCTION_DECL)
232 {
233 current_function_decl = TREE_VALUE (saved_access_scope);
234 saved_access_scope = TREE_CHAIN (saved_access_scope);
235 }
236
237 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
238 pop_nested_class ();
239 else
240 pop_from_top_level ();
241 }
242
243 /* Do any processing required when DECL (a member template
244 declaration) is finished. Returns the TEMPLATE_DECL corresponding
245 to DECL, unless it is a specialization, in which case the DECL
246 itself is returned. */
247
248 tree
249 finish_member_template_decl (tree decl)
250 {
251 if (decl == error_mark_node)
252 return error_mark_node;
253
254 gcc_assert (DECL_P (decl));
255
256 if (TREE_CODE (decl) == TYPE_DECL)
257 {
258 tree type;
259
260 type = TREE_TYPE (decl);
261 if (type == error_mark_node)
262 return error_mark_node;
263 if (MAYBE_CLASS_TYPE_P (type)
264 && CLASSTYPE_TEMPLATE_INFO (type)
265 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
266 {
267 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
268 check_member_template (tmpl);
269 return tmpl;
270 }
271 return NULL_TREE;
272 }
273 else if (TREE_CODE (decl) == FIELD_DECL)
274 error ("data member %qD cannot be a member template", decl);
275 else if (DECL_TEMPLATE_INFO (decl))
276 {
277 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
278 {
279 check_member_template (DECL_TI_TEMPLATE (decl));
280 return DECL_TI_TEMPLATE (decl);
281 }
282 else
283 return decl;
284 }
285 else
286 error ("invalid member template declaration %qD", decl);
287
288 return error_mark_node;
289 }
290
291 /* Create a template info node. */
292
293 tree
294 build_template_info (tree template_decl, tree template_args)
295 {
296 tree result = make_node (TEMPLATE_INFO);
297 TI_TEMPLATE (result) = template_decl;
298 TI_ARGS (result) = template_args;
299 return result;
300 }
301
302 /* Return the template info node corresponding to T, whatever T is. */
303
304 tree
305 get_template_info (const_tree t)
306 {
307 tree tinfo = NULL_TREE;
308
309 if (!t || t == error_mark_node)
310 return NULL;
311
312 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
313 tinfo = DECL_TEMPLATE_INFO (t);
314
315 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
316 t = TREE_TYPE (t);
317
318 if (TAGGED_TYPE_P (t))
319 tinfo = TYPE_TEMPLATE_INFO (t);
320
321 return tinfo;
322 }
323
324 /* Returns the template nesting level of the indicated class TYPE.
325
326 For example, in:
327 template <class T>
328 struct A
329 {
330 template <class U>
331 struct B {};
332 };
333
334 A<T>::B<U> has depth two, while A<T> has depth one.
335 Both A<T>::B<int> and A<int>::B<U> have depth one, if
336 they are instantiations, not specializations.
337
338 This function is guaranteed to return 0 if passed NULL_TREE so
339 that, for example, `template_class_depth (current_class_type)' is
340 always safe. */
341
342 int
343 template_class_depth (tree type)
344 {
345 int depth;
346
347 for (depth = 0;
348 type && TREE_CODE (type) != NAMESPACE_DECL;
349 type = (TREE_CODE (type) == FUNCTION_DECL)
350 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
351 {
352 tree tinfo = get_template_info (type);
353
354 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
355 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
356 ++depth;
357 }
358
359 return depth;
360 }
361
362 /* Subroutine of maybe_begin_member_template_processing.
363 Returns true if processing DECL needs us to push template parms. */
364
365 static bool
366 inline_needs_template_parms (tree decl)
367 {
368 if (! DECL_TEMPLATE_INFO (decl))
369 return false;
370
371 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
372 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
373 }
374
375 /* Subroutine of maybe_begin_member_template_processing.
376 Push the template parms in PARMS, starting from LEVELS steps into the
377 chain, and ending at the beginning, since template parms are listed
378 innermost first. */
379
380 static void
381 push_inline_template_parms_recursive (tree parmlist, int levels)
382 {
383 tree parms = TREE_VALUE (parmlist);
384 int i;
385
386 if (levels > 1)
387 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
388
389 ++processing_template_decl;
390 current_template_parms
391 = tree_cons (size_int (processing_template_decl),
392 parms, current_template_parms);
393 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
394
395 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
396 NULL);
397 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
398 {
399 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
400
401 if (parm == error_mark_node)
402 continue;
403
404 gcc_assert (DECL_P (parm));
405
406 switch (TREE_CODE (parm))
407 {
408 case TYPE_DECL:
409 case TEMPLATE_DECL:
410 pushdecl (parm);
411 break;
412
413 case PARM_DECL:
414 {
415 /* Make a CONST_DECL as is done in process_template_parm.
416 It is ugly that we recreate this here; the original
417 version built in process_template_parm is no longer
418 available. */
419 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
420 CONST_DECL, DECL_NAME (parm),
421 TREE_TYPE (parm));
422 DECL_ARTIFICIAL (decl) = 1;
423 TREE_CONSTANT (decl) = 1;
424 TREE_READONLY (decl) = 1;
425 DECL_INITIAL (decl) = DECL_INITIAL (parm);
426 SET_DECL_TEMPLATE_PARM_P (decl);
427 pushdecl (decl);
428 }
429 break;
430
431 default:
432 gcc_unreachable ();
433 }
434 }
435 }
436
437 /* Restore the template parameter context for a member template or
438 a friend template defined in a class definition. */
439
440 void
441 maybe_begin_member_template_processing (tree decl)
442 {
443 tree parms;
444 int levels = 0;
445
446 if (inline_needs_template_parms (decl))
447 {
448 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
449 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
450
451 if (DECL_TEMPLATE_SPECIALIZATION (decl))
452 {
453 --levels;
454 parms = TREE_CHAIN (parms);
455 }
456
457 push_inline_template_parms_recursive (parms, levels);
458 }
459
460 /* Remember how many levels of template parameters we pushed so that
461 we can pop them later. */
462 VEC_safe_push (int, heap, inline_parm_levels, levels);
463 }
464
465 /* Undo the effects of maybe_begin_member_template_processing. */
466
467 void
468 maybe_end_member_template_processing (void)
469 {
470 int i;
471 int last;
472
473 if (VEC_length (int, inline_parm_levels) == 0)
474 return;
475
476 last = VEC_pop (int, inline_parm_levels);
477 for (i = 0; i < last; ++i)
478 {
479 --processing_template_decl;
480 current_template_parms = TREE_CHAIN (current_template_parms);
481 poplevel (0, 0, 0);
482 }
483 }
484
485 /* Return a new template argument vector which contains all of ARGS,
486 but has as its innermost set of arguments the EXTRA_ARGS. */
487
488 static tree
489 add_to_template_args (tree args, tree extra_args)
490 {
491 tree new_args;
492 int extra_depth;
493 int i;
494 int j;
495
496 extra_depth = TMPL_ARGS_DEPTH (extra_args);
497 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
498
499 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
500 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
501
502 for (j = 1; j <= extra_depth; ++j, ++i)
503 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
504
505 return new_args;
506 }
507
508 /* Like add_to_template_args, but only the outermost ARGS are added to
509 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
510 (EXTRA_ARGS) levels are added. This function is used to combine
511 the template arguments from a partial instantiation with the
512 template arguments used to attain the full instantiation from the
513 partial instantiation. */
514
515 static tree
516 add_outermost_template_args (tree args, tree extra_args)
517 {
518 tree new_args;
519
520 /* If there are more levels of EXTRA_ARGS than there are ARGS,
521 something very fishy is going on. */
522 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
523
524 /* If *all* the new arguments will be the EXTRA_ARGS, just return
525 them. */
526 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
527 return extra_args;
528
529 /* For the moment, we make ARGS look like it contains fewer levels. */
530 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
531
532 new_args = add_to_template_args (args, extra_args);
533
534 /* Now, we restore ARGS to its full dimensions. */
535 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
536
537 return new_args;
538 }
539
540 /* Return the N levels of innermost template arguments from the ARGS. */
541
542 tree
543 get_innermost_template_args (tree args, int n)
544 {
545 tree new_args;
546 int extra_levels;
547 int i;
548
549 gcc_assert (n >= 0);
550
551 /* If N is 1, just return the innermost set of template arguments. */
552 if (n == 1)
553 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
554
555 /* If we're not removing anything, just return the arguments we were
556 given. */
557 extra_levels = TMPL_ARGS_DEPTH (args) - n;
558 gcc_assert (extra_levels >= 0);
559 if (extra_levels == 0)
560 return args;
561
562 /* Make a new set of arguments, not containing the outer arguments. */
563 new_args = make_tree_vec (n);
564 for (i = 1; i <= n; ++i)
565 SET_TMPL_ARGS_LEVEL (new_args, i,
566 TMPL_ARGS_LEVEL (args, i + extra_levels));
567
568 return new_args;
569 }
570
571 /* The inverse of get_innermost_template_args: Return all but the innermost
572 EXTRA_LEVELS levels of template arguments from the ARGS. */
573
574 static tree
575 strip_innermost_template_args (tree args, int extra_levels)
576 {
577 tree new_args;
578 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
579 int i;
580
581 gcc_assert (n >= 0);
582
583 /* If N is 1, just return the outermost set of template arguments. */
584 if (n == 1)
585 return TMPL_ARGS_LEVEL (args, 1);
586
587 /* If we're not removing anything, just return the arguments we were
588 given. */
589 gcc_assert (extra_levels >= 0);
590 if (extra_levels == 0)
591 return args;
592
593 /* Make a new set of arguments, not containing the inner arguments. */
594 new_args = make_tree_vec (n);
595 for (i = 1; i <= n; ++i)
596 SET_TMPL_ARGS_LEVEL (new_args, i,
597 TMPL_ARGS_LEVEL (args, i));
598
599 return new_args;
600 }
601
602 /* We've got a template header coming up; push to a new level for storing
603 the parms. */
604
605 void
606 begin_template_parm_list (void)
607 {
608 /* We use a non-tag-transparent scope here, which causes pushtag to
609 put tags in this scope, rather than in the enclosing class or
610 namespace scope. This is the right thing, since we want
611 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
612 global template class, push_template_decl handles putting the
613 TEMPLATE_DECL into top-level scope. For a nested template class,
614 e.g.:
615
616 template <class T> struct S1 {
617 template <class T> struct S2 {};
618 };
619
620 pushtag contains special code to call pushdecl_with_scope on the
621 TEMPLATE_DECL for S2. */
622 begin_scope (sk_template_parms, NULL);
623 ++processing_template_decl;
624 ++processing_template_parmlist;
625 note_template_header (0);
626 }
627
628 /* This routine is called when a specialization is declared. If it is
629 invalid to declare a specialization here, an error is reported and
630 false is returned, otherwise this routine will return true. */
631
632 static bool
633 check_specialization_scope (void)
634 {
635 tree scope = current_scope ();
636
637 /* [temp.expl.spec]
638
639 An explicit specialization shall be declared in the namespace of
640 which the template is a member, or, for member templates, in the
641 namespace of which the enclosing class or enclosing class
642 template is a member. An explicit specialization of a member
643 function, member class or static data member of a class template
644 shall be declared in the namespace of which the class template
645 is a member. */
646 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
647 {
648 error ("explicit specialization in non-namespace scope %qD", scope);
649 return false;
650 }
651
652 /* [temp.expl.spec]
653
654 In an explicit specialization declaration for a member of a class
655 template or a member template that appears in namespace scope,
656 the member template and some of its enclosing class templates may
657 remain unspecialized, except that the declaration shall not
658 explicitly specialize a class member template if its enclosing
659 class templates are not explicitly specialized as well. */
660 if (current_template_parms)
661 {
662 error ("enclosing class templates are not explicitly specialized");
663 return false;
664 }
665
666 return true;
667 }
668
669 /* We've just seen template <>. */
670
671 bool
672 begin_specialization (void)
673 {
674 begin_scope (sk_template_spec, NULL);
675 note_template_header (1);
676 return check_specialization_scope ();
677 }
678
679 /* Called at then end of processing a declaration preceded by
680 template<>. */
681
682 void
683 end_specialization (void)
684 {
685 finish_scope ();
686 reset_specialization ();
687 }
688
689 /* Any template <>'s that we have seen thus far are not referring to a
690 function specialization. */
691
692 void
693 reset_specialization (void)
694 {
695 processing_specialization = 0;
696 template_header_count = 0;
697 }
698
699 /* We've just seen a template header. If SPECIALIZATION is nonzero,
700 it was of the form template <>. */
701
702 static void
703 note_template_header (int specialization)
704 {
705 processing_specialization = specialization;
706 template_header_count++;
707 }
708
709 /* We're beginning an explicit instantiation. */
710
711 void
712 begin_explicit_instantiation (void)
713 {
714 gcc_assert (!processing_explicit_instantiation);
715 processing_explicit_instantiation = true;
716 }
717
718
719 void
720 end_explicit_instantiation (void)
721 {
722 gcc_assert (processing_explicit_instantiation);
723 processing_explicit_instantiation = false;
724 }
725
726 /* An explicit specialization or partial specialization TMPL is being
727 declared. Check that the namespace in which the specialization is
728 occurring is permissible. Returns false iff it is invalid to
729 specialize TMPL in the current namespace. */
730
731 static bool
732 check_specialization_namespace (tree tmpl)
733 {
734 tree tpl_ns = decl_namespace_context (tmpl);
735
736 /* [tmpl.expl.spec]
737
738 An explicit specialization shall be declared in the namespace of
739 which the template is a member, or, for member templates, in the
740 namespace of which the enclosing class or enclosing class
741 template is a member. An explicit specialization of a member
742 function, member class or static data member of a class template
743 shall be declared in the namespace of which the class template is
744 a member. */
745 if (current_scope() != DECL_CONTEXT (tmpl)
746 && !at_namespace_scope_p ())
747 {
748 error ("specialization of %qD must appear at namespace scope", tmpl);
749 return false;
750 }
751 if (is_associated_namespace (current_namespace, tpl_ns))
752 /* Same or super-using namespace. */
753 return true;
754 else
755 {
756 permerror (input_location, "specialization of %qD in different namespace", tmpl);
757 permerror (input_location, " from definition of %q+#D", tmpl);
758 return false;
759 }
760 }
761
762 /* SPEC is an explicit instantiation. Check that it is valid to
763 perform this explicit instantiation in the current namespace. */
764
765 static void
766 check_explicit_instantiation_namespace (tree spec)
767 {
768 tree ns;
769
770 /* DR 275: An explicit instantiation shall appear in an enclosing
771 namespace of its template. */
772 ns = decl_namespace_context (spec);
773 if (!is_ancestor (current_namespace, ns))
774 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
775 "(which does not enclose namespace %qD)",
776 spec, current_namespace, ns);
777 }
778
779 /* The TYPE is being declared. If it is a template type, that means it
780 is a partial specialization. Do appropriate error-checking. */
781
782 tree
783 maybe_process_partial_specialization (tree type)
784 {
785 tree context;
786
787 if (type == error_mark_node)
788 return error_mark_node;
789
790 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
791 {
792 error ("name of class shadows template template parameter %qD",
793 TYPE_NAME (type));
794 return error_mark_node;
795 }
796
797 context = TYPE_CONTEXT (type);
798
799 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
800 {
801 /* This is for ordinary explicit specialization and partial
802 specialization of a template class such as:
803
804 template <> class C<int>;
805
806 or:
807
808 template <class T> class C<T*>;
809
810 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
811
812 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
813 && !COMPLETE_TYPE_P (type))
814 {
815 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
816 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
817 if (processing_template_decl)
818 {
819 if (push_template_decl (TYPE_MAIN_DECL (type))
820 == error_mark_node)
821 return error_mark_node;
822 }
823 }
824 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
825 error ("specialization of %qT after instantiation", type);
826 }
827 else if (CLASS_TYPE_P (type)
828 && !CLASSTYPE_USE_TEMPLATE (type)
829 && CLASSTYPE_TEMPLATE_INFO (type)
830 && context && CLASS_TYPE_P (context)
831 && CLASSTYPE_TEMPLATE_INFO (context))
832 {
833 /* This is for an explicit specialization of member class
834 template according to [temp.expl.spec/18]:
835
836 template <> template <class U> class C<int>::D;
837
838 The context `C<int>' must be an implicit instantiation.
839 Otherwise this is just a member class template declared
840 earlier like:
841
842 template <> class C<int> { template <class U> class D; };
843 template <> template <class U> class C<int>::D;
844
845 In the first case, `C<int>::D' is a specialization of `C<T>::D'
846 while in the second case, `C<int>::D' is a primary template
847 and `C<T>::D' may not exist. */
848
849 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
850 && !COMPLETE_TYPE_P (type))
851 {
852 tree t;
853 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
854
855 if (current_namespace
856 != decl_namespace_context (tmpl))
857 {
858 permerror (input_location, "specializing %q#T in different namespace", type);
859 permerror (input_location, " from definition of %q+#D", tmpl);
860 }
861
862 /* Check for invalid specialization after instantiation:
863
864 template <> template <> class C<int>::D<int>;
865 template <> template <class U> class C<int>::D; */
866
867 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
868 t; t = TREE_CHAIN (t))
869 {
870 tree inst = TREE_VALUE (t);
871 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
872 {
873 /* We already have a full specialization of this partial
874 instantiation. Reassign it to the new member
875 specialization template. */
876 spec_entry elt;
877 spec_entry **slot;
878
879 elt.tmpl = most_general_template (tmpl);
880 elt.args = CLASSTYPE_TI_ARGS (inst);
881 elt.spec = inst;
882
883 htab_remove_elt (type_specializations, &elt);
884
885 elt.tmpl = tmpl;
886 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
887
888 slot = (spec_entry **)
889 htab_find_slot (type_specializations, &elt, INSERT);
890 *slot = GGC_NEW (spec_entry);
891 **slot = elt;
892 }
893 else if (COMPLETE_TYPE_P (inst) || TYPE_BEING_DEFINED (inst))
894 /* But if we've had an implicit instantiation, that's a
895 problem ([temp.expl.spec]/6). */
896 error ("specialization %qT after instantiation %qT",
897 type, inst);
898 }
899
900 /* Mark TYPE as a specialization. And as a result, we only
901 have one level of template argument for the innermost
902 class template. */
903 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
904 CLASSTYPE_TI_ARGS (type)
905 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
906 }
907 }
908 else if (processing_specialization)
909 {
910 error ("explicit specialization of non-template %qT", type);
911 return error_mark_node;
912 }
913
914 return type;
915 }
916
917 /* Returns nonzero if we can optimize the retrieval of specializations
918 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
919 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
920
921 static inline bool
922 optimize_specialization_lookup_p (tree tmpl)
923 {
924 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
925 && DECL_CLASS_SCOPE_P (tmpl)
926 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
927 parameter. */
928 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
929 /* The optimized lookup depends on the fact that the
930 template arguments for the member function template apply
931 purely to the containing class, which is not true if the
932 containing class is an explicit or partial
933 specialization. */
934 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
935 && !DECL_MEMBER_TEMPLATE_P (tmpl)
936 && !DECL_CONV_FN_P (tmpl)
937 /* It is possible to have a template that is not a member
938 template and is not a member of a template class:
939
940 template <typename T>
941 struct S { friend A::f(); };
942
943 Here, the friend function is a template, but the context does
944 not have template information. The optimized lookup relies
945 on having ARGS be the template arguments for both the class
946 and the function template. */
947 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
948 }
949
950 /* Retrieve the specialization (in the sense of [temp.spec] - a
951 specialization is either an instantiation or an explicit
952 specialization) of TMPL for the given template ARGS. If there is
953 no such specialization, return NULL_TREE. The ARGS are a vector of
954 arguments, or a vector of vectors of arguments, in the case of
955 templates with more than one level of parameters.
956
957 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
958 then we search for a partial specialization matching ARGS. This
959 parameter is ignored if TMPL is not a class template. */
960
961 static tree
962 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
963 {
964 if (args == error_mark_node)
965 return NULL_TREE;
966
967 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
968
969 /* There should be as many levels of arguments as there are
970 levels of parameters. */
971 gcc_assert (TMPL_ARGS_DEPTH (args)
972 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
973
974 if (optimize_specialization_lookup_p (tmpl))
975 {
976 tree class_template;
977 tree class_specialization;
978 VEC(tree,gc) *methods;
979 tree fns;
980 int idx;
981
982 /* The template arguments actually apply to the containing
983 class. Find the class specialization with those
984 arguments. */
985 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
986 class_specialization
987 = retrieve_specialization (class_template, args, 0);
988 if (!class_specialization)
989 return NULL_TREE;
990 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
991 for the specialization. */
992 idx = class_method_index_for_fn (class_specialization, tmpl);
993 if (idx == -1)
994 return NULL_TREE;
995 /* Iterate through the methods with the indicated name, looking
996 for the one that has an instance of TMPL. */
997 methods = CLASSTYPE_METHOD_VEC (class_specialization);
998 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
999 {
1000 tree fn = OVL_CURRENT (fns);
1001 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1002 /* using-declarations can add base methods to the method vec,
1003 and we don't want those here. */
1004 && DECL_CONTEXT (fn) == class_specialization)
1005 return fn;
1006 }
1007 return NULL_TREE;
1008 }
1009 else
1010 {
1011 spec_entry *found;
1012 spec_entry elt;
1013 htab_t specializations;
1014
1015 elt.tmpl = tmpl;
1016 elt.args = args;
1017 elt.spec = NULL_TREE;
1018
1019 if (DECL_CLASS_TEMPLATE_P (tmpl))
1020 specializations = type_specializations;
1021 else
1022 specializations = decl_specializations;
1023
1024 if (hash == 0)
1025 hash = hash_specialization (&elt);
1026 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1027 if (found)
1028 return found->spec;
1029 }
1030
1031 return NULL_TREE;
1032 }
1033
1034 /* Like retrieve_specialization, but for local declarations. */
1035
1036 static tree
1037 retrieve_local_specialization (tree tmpl)
1038 {
1039 tree spec;
1040
1041 if (local_specializations == NULL)
1042 return NULL_TREE;
1043
1044 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1045 htab_hash_pointer (tmpl));
1046 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1047 }
1048
1049 /* Returns nonzero iff DECL is a specialization of TMPL. */
1050
1051 int
1052 is_specialization_of (tree decl, tree tmpl)
1053 {
1054 tree t;
1055
1056 if (TREE_CODE (decl) == FUNCTION_DECL)
1057 {
1058 for (t = decl;
1059 t != NULL_TREE;
1060 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1061 if (t == tmpl)
1062 return 1;
1063 }
1064 else
1065 {
1066 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1067
1068 for (t = TREE_TYPE (decl);
1069 t != NULL_TREE;
1070 t = CLASSTYPE_USE_TEMPLATE (t)
1071 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1072 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1073 return 1;
1074 }
1075
1076 return 0;
1077 }
1078
1079 /* Returns nonzero iff DECL is a specialization of friend declaration
1080 FRIEND_DECL according to [temp.friend]. */
1081
1082 bool
1083 is_specialization_of_friend (tree decl, tree friend_decl)
1084 {
1085 bool need_template = true;
1086 int template_depth;
1087
1088 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1089 || TREE_CODE (decl) == TYPE_DECL);
1090
1091 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1092 of a template class, we want to check if DECL is a specialization
1093 if this. */
1094 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1095 && DECL_TEMPLATE_INFO (friend_decl)
1096 && !DECL_USE_TEMPLATE (friend_decl))
1097 {
1098 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1099 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1100 need_template = false;
1101 }
1102 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1103 && !PRIMARY_TEMPLATE_P (friend_decl))
1104 need_template = false;
1105
1106 /* There is nothing to do if this is not a template friend. */
1107 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1108 return false;
1109
1110 if (is_specialization_of (decl, friend_decl))
1111 return true;
1112
1113 /* [temp.friend/6]
1114 A member of a class template may be declared to be a friend of a
1115 non-template class. In this case, the corresponding member of
1116 every specialization of the class template is a friend of the
1117 class granting friendship.
1118
1119 For example, given a template friend declaration
1120
1121 template <class T> friend void A<T>::f();
1122
1123 the member function below is considered a friend
1124
1125 template <> struct A<int> {
1126 void f();
1127 };
1128
1129 For this type of template friend, TEMPLATE_DEPTH below will be
1130 nonzero. To determine if DECL is a friend of FRIEND, we first
1131 check if the enclosing class is a specialization of another. */
1132
1133 template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
1134 if (template_depth
1135 && DECL_CLASS_SCOPE_P (decl)
1136 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1137 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1138 {
1139 /* Next, we check the members themselves. In order to handle
1140 a few tricky cases, such as when FRIEND_DECL's are
1141
1142 template <class T> friend void A<T>::g(T t);
1143 template <class T> template <T t> friend void A<T>::h();
1144
1145 and DECL's are
1146
1147 void A<int>::g(int);
1148 template <int> void A<int>::h();
1149
1150 we need to figure out ARGS, the template arguments from
1151 the context of DECL. This is required for template substitution
1152 of `T' in the function parameter of `g' and template parameter
1153 of `h' in the above examples. Here ARGS corresponds to `int'. */
1154
1155 tree context = DECL_CONTEXT (decl);
1156 tree args = NULL_TREE;
1157 int current_depth = 0;
1158
1159 while (current_depth < template_depth)
1160 {
1161 if (CLASSTYPE_TEMPLATE_INFO (context))
1162 {
1163 if (current_depth == 0)
1164 args = TYPE_TI_ARGS (context);
1165 else
1166 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1167 current_depth++;
1168 }
1169 context = TYPE_CONTEXT (context);
1170 }
1171
1172 if (TREE_CODE (decl) == FUNCTION_DECL)
1173 {
1174 bool is_template;
1175 tree friend_type;
1176 tree decl_type;
1177 tree friend_args_type;
1178 tree decl_args_type;
1179
1180 /* Make sure that both DECL and FRIEND_DECL are templates or
1181 non-templates. */
1182 is_template = DECL_TEMPLATE_INFO (decl)
1183 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1184 if (need_template ^ is_template)
1185 return false;
1186 else if (is_template)
1187 {
1188 /* If both are templates, check template parameter list. */
1189 tree friend_parms
1190 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1191 args, tf_none);
1192 if (!comp_template_parms
1193 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1194 friend_parms))
1195 return false;
1196
1197 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1198 }
1199 else
1200 decl_type = TREE_TYPE (decl);
1201
1202 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1203 tf_none, NULL_TREE);
1204 if (friend_type == error_mark_node)
1205 return false;
1206
1207 /* Check if return types match. */
1208 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1209 return false;
1210
1211 /* Check if function parameter types match, ignoring the
1212 `this' parameter. */
1213 friend_args_type = TYPE_ARG_TYPES (friend_type);
1214 decl_args_type = TYPE_ARG_TYPES (decl_type);
1215 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1216 friend_args_type = TREE_CHAIN (friend_args_type);
1217 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1218 decl_args_type = TREE_CHAIN (decl_args_type);
1219
1220 return compparms (decl_args_type, friend_args_type);
1221 }
1222 else
1223 {
1224 /* DECL is a TYPE_DECL */
1225 bool is_template;
1226 tree decl_type = TREE_TYPE (decl);
1227
1228 /* Make sure that both DECL and FRIEND_DECL are templates or
1229 non-templates. */
1230 is_template
1231 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1232 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1233
1234 if (need_template ^ is_template)
1235 return false;
1236 else if (is_template)
1237 {
1238 tree friend_parms;
1239 /* If both are templates, check the name of the two
1240 TEMPLATE_DECL's first because is_friend didn't. */
1241 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1242 != DECL_NAME (friend_decl))
1243 return false;
1244
1245 /* Now check template parameter list. */
1246 friend_parms
1247 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1248 args, tf_none);
1249 return comp_template_parms
1250 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1251 friend_parms);
1252 }
1253 else
1254 return (DECL_NAME (decl)
1255 == DECL_NAME (friend_decl));
1256 }
1257 }
1258 return false;
1259 }
1260
1261 /* Register the specialization SPEC as a specialization of TMPL with
1262 the indicated ARGS. IS_FRIEND indicates whether the specialization
1263 is actually just a friend declaration. Returns SPEC, or an
1264 equivalent prior declaration, if available. */
1265
1266 static tree
1267 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1268 hashval_t hash)
1269 {
1270 tree fn;
1271 spec_entry **slot = NULL;
1272 spec_entry elt;
1273
1274 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1275
1276 if (TREE_CODE (spec) == FUNCTION_DECL
1277 && uses_template_parms (DECL_TI_ARGS (spec)))
1278 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1279 register it; we want the corresponding TEMPLATE_DECL instead.
1280 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1281 the more obvious `uses_template_parms (spec)' to avoid problems
1282 with default function arguments. In particular, given
1283 something like this:
1284
1285 template <class T> void f(T t1, T t = T())
1286
1287 the default argument expression is not substituted for in an
1288 instantiation unless and until it is actually needed. */
1289 return spec;
1290
1291 if (optimize_specialization_lookup_p (tmpl))
1292 /* We don't put these specializations in the hash table, but we might
1293 want to give an error about a mismatch. */
1294 fn = retrieve_specialization (tmpl, args, 0);
1295 else
1296 {
1297 elt.tmpl = tmpl;
1298 elt.args = args;
1299 elt.spec = spec;
1300
1301 if (hash == 0)
1302 hash = hash_specialization (&elt);
1303
1304 slot = (spec_entry **)
1305 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1306 if (*slot)
1307 fn = (*slot)->spec;
1308 else
1309 fn = NULL_TREE;
1310 }
1311
1312 /* We can sometimes try to re-register a specialization that we've
1313 already got. In particular, regenerate_decl_from_template calls
1314 duplicate_decls which will update the specialization list. But,
1315 we'll still get called again here anyhow. It's more convenient
1316 to simply allow this than to try to prevent it. */
1317 if (fn == spec)
1318 return spec;
1319 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1320 {
1321 if (DECL_TEMPLATE_INSTANTIATION (fn))
1322 {
1323 if (DECL_ODR_USED (fn)
1324 || DECL_EXPLICIT_INSTANTIATION (fn))
1325 {
1326 error ("specialization of %qD after instantiation",
1327 fn);
1328 return error_mark_node;
1329 }
1330 else
1331 {
1332 tree clone;
1333 /* This situation should occur only if the first
1334 specialization is an implicit instantiation, the
1335 second is an explicit specialization, and the
1336 implicit instantiation has not yet been used. That
1337 situation can occur if we have implicitly
1338 instantiated a member function and then specialized
1339 it later.
1340
1341 We can also wind up here if a friend declaration that
1342 looked like an instantiation turns out to be a
1343 specialization:
1344
1345 template <class T> void foo(T);
1346 class S { friend void foo<>(int) };
1347 template <> void foo(int);
1348
1349 We transform the existing DECL in place so that any
1350 pointers to it become pointers to the updated
1351 declaration.
1352
1353 If there was a definition for the template, but not
1354 for the specialization, we want this to look as if
1355 there were no definition, and vice versa. */
1356 DECL_INITIAL (fn) = NULL_TREE;
1357 duplicate_decls (spec, fn, is_friend);
1358 /* The call to duplicate_decls will have applied
1359 [temp.expl.spec]:
1360
1361 An explicit specialization of a function template
1362 is inline only if it is explicitly declared to be,
1363 and independently of whether its function template
1364 is.
1365
1366 to the primary function; now copy the inline bits to
1367 the various clones. */
1368 FOR_EACH_CLONE (clone, fn)
1369 {
1370 DECL_DECLARED_INLINE_P (clone)
1371 = DECL_DECLARED_INLINE_P (fn);
1372 DECL_SOURCE_LOCATION (clone)
1373 = DECL_SOURCE_LOCATION (fn);
1374 }
1375 check_specialization_namespace (fn);
1376
1377 return fn;
1378 }
1379 }
1380 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1381 {
1382 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1383 /* Dup decl failed, but this is a new definition. Set the
1384 line number so any errors match this new
1385 definition. */
1386 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1387
1388 return fn;
1389 }
1390 }
1391 else if (fn)
1392 return duplicate_decls (spec, fn, is_friend);
1393
1394 /* A specialization must be declared in the same namespace as the
1395 template it is specializing. */
1396 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1397 && !check_specialization_namespace (tmpl))
1398 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1399
1400 if (!optimize_specialization_lookup_p (tmpl))
1401 {
1402 gcc_assert (tmpl && args && spec);
1403 *slot = GGC_NEW (spec_entry);
1404 **slot = elt;
1405 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1406 && PRIMARY_TEMPLATE_P (tmpl)
1407 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1408 /* TMPL is a forward declaration of a template function; keep a list
1409 of all specializations in case we need to reassign them to a friend
1410 template later in tsubst_friend_function. */
1411 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1412 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1413 }
1414
1415 return spec;
1416 }
1417
1418 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1419 TMPL and ARGS members, ignores SPEC. */
1420
1421 static int
1422 eq_specializations (const void *p1, const void *p2)
1423 {
1424 const spec_entry *e1 = (const spec_entry *)p1;
1425 const spec_entry *e2 = (const spec_entry *)p2;
1426
1427 return (e1->tmpl == e2->tmpl
1428 && comp_template_args (e1->args, e2->args));
1429 }
1430
1431 /* Returns a hash for a template TMPL and template arguments ARGS. */
1432
1433 static hashval_t
1434 hash_tmpl_and_args (tree tmpl, tree args)
1435 {
1436 hashval_t val = DECL_UID (tmpl);
1437 return iterative_hash_template_arg (args, val);
1438 }
1439
1440 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1441 ignoring SPEC. */
1442
1443 static hashval_t
1444 hash_specialization (const void *p)
1445 {
1446 const spec_entry *e = (const spec_entry *)p;
1447 return hash_tmpl_and_args (e->tmpl, e->args);
1448 }
1449
1450 /* Recursively calculate a hash value for a template argument ARG, for use
1451 in the hash tables of template specializations. */
1452
1453 static hashval_t
1454 iterative_hash_template_arg (tree arg, hashval_t val)
1455 {
1456 unsigned HOST_WIDE_INT i;
1457 enum tree_code code;
1458 char tclass;
1459
1460 if (arg == NULL_TREE)
1461 return iterative_hash_object (arg, val);
1462
1463 if (!TYPE_P (arg))
1464 STRIP_NOPS (arg);
1465
1466 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1467 /* We can get one of these when re-hashing a previous entry in the middle
1468 of substituting into a pack expansion. Just look through it. */
1469 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1470
1471 code = TREE_CODE (arg);
1472 tclass = TREE_CODE_CLASS (code);
1473
1474 val = iterative_hash_object (code, val);
1475
1476 switch (code)
1477 {
1478 case ERROR_MARK:
1479 return val;
1480
1481 case IDENTIFIER_NODE:
1482 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1483
1484 case TREE_VEC:
1485 {
1486 int i, len = TREE_VEC_LENGTH (arg);
1487 for (i = 0; i < len; ++i)
1488 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1489 return val;
1490 }
1491
1492 case TYPE_PACK_EXPANSION:
1493 case EXPR_PACK_EXPANSION:
1494 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1495
1496 case TYPE_ARGUMENT_PACK:
1497 case NONTYPE_ARGUMENT_PACK:
1498 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1499
1500 case TREE_LIST:
1501 for (; arg; arg = TREE_CHAIN (arg))
1502 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1503 return val;
1504
1505 case OVERLOAD:
1506 for (; arg; arg = OVL_CHAIN (arg))
1507 val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
1508 return val;
1509
1510 case CONSTRUCTOR:
1511 {
1512 tree field, value;
1513 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1514 {
1515 val = iterative_hash_template_arg (field, val);
1516 val = iterative_hash_template_arg (value, val);
1517 }
1518 return val;
1519 }
1520
1521 case PARM_DECL:
1522 if (!DECL_ARTIFICIAL (arg))
1523 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1524 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1525
1526 case TARGET_EXPR:
1527 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1528
1529 case PTRMEM_CST:
1530 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1531 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1532
1533 case TEMPLATE_PARM_INDEX:
1534 val = iterative_hash_template_arg
1535 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1536 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1537 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1538
1539 case TRAIT_EXPR:
1540 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1541 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1542 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1543
1544 case BASELINK:
1545 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1546 val);
1547 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1548 val);
1549
1550 case MODOP_EXPR:
1551 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1552 code = TREE_CODE (TREE_OPERAND (arg, 1));
1553 val = iterative_hash_object (code, val);
1554 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1555
1556 case ARRAY_TYPE:
1557 /* layout_type sets structural equality for arrays of
1558 incomplete type, so we can't rely on the canonical type
1559 for hashing. */
1560 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1561 return iterative_hash_template_arg (TYPE_DOMAIN (arg), val);
1562
1563 default:
1564 switch (tclass)
1565 {
1566 case tcc_type:
1567 if (TYPE_CANONICAL (arg))
1568 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1569 val);
1570 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1571 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1572 /* Otherwise just compare the types during lookup. */
1573 return val;
1574
1575 case tcc_declaration:
1576 case tcc_constant:
1577 return iterative_hash_expr (arg, val);
1578
1579 default:
1580 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1581 {
1582 unsigned n = TREE_OPERAND_LENGTH (arg);
1583 for (i = 0; i < n; ++i)
1584 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1585 return val;
1586 }
1587 }
1588 }
1589 gcc_unreachable ();
1590 return 0;
1591 }
1592
1593 /* Unregister the specialization SPEC as a specialization of TMPL.
1594 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1595 if the SPEC was listed as a specialization of TMPL.
1596
1597 Note that SPEC has been ggc_freed, so we can't look inside it. */
1598
1599 bool
1600 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1601 {
1602 spec_entry **slot;
1603 spec_entry elt;
1604
1605 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1606 elt.args = TI_ARGS (tinfo);
1607 elt.spec = NULL_TREE;
1608
1609 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1610 if (*slot)
1611 {
1612 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1613 gcc_assert (new_spec != NULL_TREE);
1614 (*slot)->spec = new_spec;
1615 return 1;
1616 }
1617
1618 return 0;
1619 }
1620
1621 /* Compare an entry in the local specializations hash table P1 (which
1622 is really a pointer to a TREE_LIST) with P2 (which is really a
1623 DECL). */
1624
1625 static int
1626 eq_local_specializations (const void *p1, const void *p2)
1627 {
1628 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1629 }
1630
1631 /* Hash P1, an entry in the local specializations table. */
1632
1633 static hashval_t
1634 hash_local_specialization (const void* p1)
1635 {
1636 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1637 }
1638
1639 /* Like register_specialization, but for local declarations. We are
1640 registering SPEC, an instantiation of TMPL. */
1641
1642 static void
1643 register_local_specialization (tree spec, tree tmpl)
1644 {
1645 void **slot;
1646
1647 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1648 htab_hash_pointer (tmpl), INSERT);
1649 *slot = build_tree_list (spec, tmpl);
1650 }
1651
1652 /* TYPE is a class type. Returns true if TYPE is an explicitly
1653 specialized class. */
1654
1655 bool
1656 explicit_class_specialization_p (tree type)
1657 {
1658 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1659 return false;
1660 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1661 }
1662
1663 /* Print the list of functions at FNS, going through all the overloads
1664 for each element of the list. Alternatively, FNS can not be a
1665 TREE_LIST, in which case it will be printed together with all the
1666 overloads.
1667
1668 MORE and *STR should respectively be FALSE and NULL when the function
1669 is called from the outside. They are used internally on recursive
1670 calls. print_candidates manages the two parameters and leaves NULL
1671 in *STR when it ends. */
1672
1673 static void
1674 print_candidates_1 (tree fns, bool more, const char **str)
1675 {
1676 tree fn, fn2;
1677 char *spaces = NULL;
1678
1679 for (fn = fns; fn; fn = OVL_NEXT (fn))
1680 if (TREE_CODE (fn) == TREE_LIST)
1681 {
1682 gcc_assert (!OVL_NEXT (fn) && !is_overloaded_fn (fn));
1683 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1684 print_candidates_1 (TREE_VALUE (fn2),
1685 TREE_CHAIN (fn2) || more, str);
1686 }
1687 else
1688 {
1689 if (!*str)
1690 {
1691 /* Pick the prefix string. */
1692 if (!more && !OVL_NEXT (fns))
1693 {
1694 error ("candidate is: %+#D", OVL_CURRENT (fn));
1695 continue;
1696 }
1697
1698 *str = _("candidates are:");
1699 spaces = get_spaces (*str);
1700 }
1701 error ("%s %+#D", *str, OVL_CURRENT (fn));
1702 *str = spaces ? spaces : *str;
1703 }
1704
1705 if (!more)
1706 {
1707 free (spaces);
1708 *str = NULL;
1709 }
1710 }
1711
1712 /* Print the list of candidate FNS in an error message. */
1713
1714 void
1715 print_candidates (tree fns)
1716 {
1717 const char *str = NULL;
1718 print_candidates_1 (fns, false, &str);
1719 gcc_assert (str == NULL);
1720 }
1721
1722 /* Returns the template (one of the functions given by TEMPLATE_ID)
1723 which can be specialized to match the indicated DECL with the
1724 explicit template args given in TEMPLATE_ID. The DECL may be
1725 NULL_TREE if none is available. In that case, the functions in
1726 TEMPLATE_ID are non-members.
1727
1728 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1729 specialization of a member template.
1730
1731 The TEMPLATE_COUNT is the number of references to qualifying
1732 template classes that appeared in the name of the function. See
1733 check_explicit_specialization for a more accurate description.
1734
1735 TSK indicates what kind of template declaration (if any) is being
1736 declared. TSK_TEMPLATE indicates that the declaration given by
1737 DECL, though a FUNCTION_DECL, has template parameters, and is
1738 therefore a template function.
1739
1740 The template args (those explicitly specified and those deduced)
1741 are output in a newly created vector *TARGS_OUT.
1742
1743 If it is impossible to determine the result, an error message is
1744 issued. The error_mark_node is returned to indicate failure. */
1745
1746 static tree
1747 determine_specialization (tree template_id,
1748 tree decl,
1749 tree* targs_out,
1750 int need_member_template,
1751 int template_count,
1752 tmpl_spec_kind tsk)
1753 {
1754 tree fns;
1755 tree targs;
1756 tree explicit_targs;
1757 tree candidates = NULL_TREE;
1758 /* A TREE_LIST of templates of which DECL may be a specialization.
1759 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1760 corresponding TREE_PURPOSE is the set of template arguments that,
1761 when used to instantiate the template, would produce a function
1762 with the signature of DECL. */
1763 tree templates = NULL_TREE;
1764 int header_count;
1765 struct cp_binding_level *b;
1766
1767 *targs_out = NULL_TREE;
1768
1769 if (template_id == error_mark_node || decl == error_mark_node)
1770 return error_mark_node;
1771
1772 fns = TREE_OPERAND (template_id, 0);
1773 explicit_targs = TREE_OPERAND (template_id, 1);
1774
1775 if (fns == error_mark_node)
1776 return error_mark_node;
1777
1778 /* Check for baselinks. */
1779 if (BASELINK_P (fns))
1780 fns = BASELINK_FUNCTIONS (fns);
1781
1782 if (!is_overloaded_fn (fns))
1783 {
1784 error ("%qD is not a function template", fns);
1785 return error_mark_node;
1786 }
1787
1788 /* Count the number of template headers specified for this
1789 specialization. */
1790 header_count = 0;
1791 for (b = current_binding_level;
1792 b->kind == sk_template_parms;
1793 b = b->level_chain)
1794 ++header_count;
1795
1796 for (; fns; fns = OVL_NEXT (fns))
1797 {
1798 tree fn = OVL_CURRENT (fns);
1799
1800 if (TREE_CODE (fn) == TEMPLATE_DECL)
1801 {
1802 tree decl_arg_types;
1803 tree fn_arg_types;
1804
1805 /* In case of explicit specialization, we need to check if
1806 the number of template headers appearing in the specialization
1807 is correct. This is usually done in check_explicit_specialization,
1808 but the check done there cannot be exhaustive when specializing
1809 member functions. Consider the following code:
1810
1811 template <> void A<int>::f(int);
1812 template <> template <> void A<int>::f(int);
1813
1814 Assuming that A<int> is not itself an explicit specialization
1815 already, the first line specializes "f" which is a non-template
1816 member function, whilst the second line specializes "f" which
1817 is a template member function. So both lines are syntactically
1818 correct, and check_explicit_specialization does not reject
1819 them.
1820
1821 Here, we can do better, as we are matching the specialization
1822 against the declarations. We count the number of template
1823 headers, and we check if they match TEMPLATE_COUNT + 1
1824 (TEMPLATE_COUNT is the number of qualifying template classes,
1825 plus there must be another header for the member template
1826 itself).
1827
1828 Notice that if header_count is zero, this is not a
1829 specialization but rather a template instantiation, so there
1830 is no check we can perform here. */
1831 if (header_count && header_count != template_count + 1)
1832 continue;
1833
1834 /* Check that the number of template arguments at the
1835 innermost level for DECL is the same as for FN. */
1836 if (current_binding_level->kind == sk_template_parms
1837 && !current_binding_level->explicit_spec_p
1838 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1839 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1840 (current_template_parms))))
1841 continue;
1842
1843 /* DECL might be a specialization of FN. */
1844 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1845 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1846
1847 /* For a non-static member function, we need to make sure
1848 that the const qualification is the same. Since
1849 get_bindings does not try to merge the "this" parameter,
1850 we must do the comparison explicitly. */
1851 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1852 && !same_type_p (TREE_VALUE (fn_arg_types),
1853 TREE_VALUE (decl_arg_types)))
1854 continue;
1855
1856 /* Skip the "this" parameter and, for constructors of
1857 classes with virtual bases, the VTT parameter. A
1858 full specialization of a constructor will have a VTT
1859 parameter, but a template never will. */
1860 decl_arg_types
1861 = skip_artificial_parms_for (decl, decl_arg_types);
1862 fn_arg_types
1863 = skip_artificial_parms_for (fn, fn_arg_types);
1864
1865 /* Check that the number of function parameters matches.
1866 For example,
1867 template <class T> void f(int i = 0);
1868 template <> void f<int>();
1869 The specialization f<int> is invalid but is not caught
1870 by get_bindings below. */
1871 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1872 continue;
1873
1874 /* Function templates cannot be specializations; there are
1875 no partial specializations of functions. Therefore, if
1876 the type of DECL does not match FN, there is no
1877 match. */
1878 if (tsk == tsk_template)
1879 {
1880 if (compparms (fn_arg_types, decl_arg_types))
1881 candidates = tree_cons (NULL_TREE, fn, candidates);
1882 continue;
1883 }
1884
1885 /* See whether this function might be a specialization of this
1886 template. */
1887 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1888
1889 if (!targs)
1890 /* We cannot deduce template arguments that when used to
1891 specialize TMPL will produce DECL. */
1892 continue;
1893
1894 /* Save this template, and the arguments deduced. */
1895 templates = tree_cons (targs, fn, templates);
1896 }
1897 else if (need_member_template)
1898 /* FN is an ordinary member function, and we need a
1899 specialization of a member template. */
1900 ;
1901 else if (TREE_CODE (fn) != FUNCTION_DECL)
1902 /* We can get IDENTIFIER_NODEs here in certain erroneous
1903 cases. */
1904 ;
1905 else if (!DECL_FUNCTION_MEMBER_P (fn))
1906 /* This is just an ordinary non-member function. Nothing can
1907 be a specialization of that. */
1908 ;
1909 else if (DECL_ARTIFICIAL (fn))
1910 /* Cannot specialize functions that are created implicitly. */
1911 ;
1912 else
1913 {
1914 tree decl_arg_types;
1915
1916 /* This is an ordinary member function. However, since
1917 we're here, we can assume it's enclosing class is a
1918 template class. For example,
1919
1920 template <typename T> struct S { void f(); };
1921 template <> void S<int>::f() {}
1922
1923 Here, S<int>::f is a non-template, but S<int> is a
1924 template class. If FN has the same type as DECL, we
1925 might be in business. */
1926
1927 if (!DECL_TEMPLATE_INFO (fn))
1928 /* Its enclosing class is an explicit specialization
1929 of a template class. This is not a candidate. */
1930 continue;
1931
1932 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1933 TREE_TYPE (TREE_TYPE (fn))))
1934 /* The return types differ. */
1935 continue;
1936
1937 /* Adjust the type of DECL in case FN is a static member. */
1938 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1939 if (DECL_STATIC_FUNCTION_P (fn)
1940 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1941 decl_arg_types = TREE_CHAIN (decl_arg_types);
1942
1943 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1944 decl_arg_types))
1945 /* They match! */
1946 candidates = tree_cons (NULL_TREE, fn, candidates);
1947 }
1948 }
1949
1950 if (templates && TREE_CHAIN (templates))
1951 {
1952 /* We have:
1953
1954 [temp.expl.spec]
1955
1956 It is possible for a specialization with a given function
1957 signature to be instantiated from more than one function
1958 template. In such cases, explicit specification of the
1959 template arguments must be used to uniquely identify the
1960 function template specialization being specialized.
1961
1962 Note that here, there's no suggestion that we're supposed to
1963 determine which of the candidate templates is most
1964 specialized. However, we, also have:
1965
1966 [temp.func.order]
1967
1968 Partial ordering of overloaded function template
1969 declarations is used in the following contexts to select
1970 the function template to which a function template
1971 specialization refers:
1972
1973 -- when an explicit specialization refers to a function
1974 template.
1975
1976 So, we do use the partial ordering rules, at least for now.
1977 This extension can only serve to make invalid programs valid,
1978 so it's safe. And, there is strong anecdotal evidence that
1979 the committee intended the partial ordering rules to apply;
1980 the EDG front end has that behavior, and John Spicer claims
1981 that the committee simply forgot to delete the wording in
1982 [temp.expl.spec]. */
1983 tree tmpl = most_specialized_instantiation (templates);
1984 if (tmpl != error_mark_node)
1985 {
1986 templates = tmpl;
1987 TREE_CHAIN (templates) = NULL_TREE;
1988 }
1989 }
1990
1991 if (templates == NULL_TREE && candidates == NULL_TREE)
1992 {
1993 error ("template-id %qD for %q+D does not match any template "
1994 "declaration", template_id, decl);
1995 if (header_count && header_count != template_count + 1)
1996 inform (input_location, "saw %d %<template<>%>, need %d for "
1997 "specializing a member function template",
1998 header_count, template_count + 1);
1999 return error_mark_node;
2000 }
2001 else if ((templates && TREE_CHAIN (templates))
2002 || (candidates && TREE_CHAIN (candidates))
2003 || (templates && candidates))
2004 {
2005 error ("ambiguous template specialization %qD for %q+D",
2006 template_id, decl);
2007 candidates = chainon (candidates, templates);
2008 print_candidates (candidates);
2009 return error_mark_node;
2010 }
2011
2012 /* We have one, and exactly one, match. */
2013 if (candidates)
2014 {
2015 tree fn = TREE_VALUE (candidates);
2016 *targs_out = copy_node (DECL_TI_ARGS (fn));
2017 /* DECL is a re-declaration or partial instantiation of a template
2018 function. */
2019 if (TREE_CODE (fn) == TEMPLATE_DECL)
2020 return fn;
2021 /* It was a specialization of an ordinary member function in a
2022 template class. */
2023 return DECL_TI_TEMPLATE (fn);
2024 }
2025
2026 /* It was a specialization of a template. */
2027 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2028 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2029 {
2030 *targs_out = copy_node (targs);
2031 SET_TMPL_ARGS_LEVEL (*targs_out,
2032 TMPL_ARGS_DEPTH (*targs_out),
2033 TREE_PURPOSE (templates));
2034 }
2035 else
2036 *targs_out = TREE_PURPOSE (templates);
2037 return TREE_VALUE (templates);
2038 }
2039
2040 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2041 but with the default argument values filled in from those in the
2042 TMPL_TYPES. */
2043
2044 static tree
2045 copy_default_args_to_explicit_spec_1 (tree spec_types,
2046 tree tmpl_types)
2047 {
2048 tree new_spec_types;
2049
2050 if (!spec_types)
2051 return NULL_TREE;
2052
2053 if (spec_types == void_list_node)
2054 return void_list_node;
2055
2056 /* Substitute into the rest of the list. */
2057 new_spec_types =
2058 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2059 TREE_CHAIN (tmpl_types));
2060
2061 /* Add the default argument for this parameter. */
2062 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2063 TREE_VALUE (spec_types),
2064 new_spec_types);
2065 }
2066
2067 /* DECL is an explicit specialization. Replicate default arguments
2068 from the template it specializes. (That way, code like:
2069
2070 template <class T> void f(T = 3);
2071 template <> void f(double);
2072 void g () { f (); }
2073
2074 works, as required.) An alternative approach would be to look up
2075 the correct default arguments at the call-site, but this approach
2076 is consistent with how implicit instantiations are handled. */
2077
2078 static void
2079 copy_default_args_to_explicit_spec (tree decl)
2080 {
2081 tree tmpl;
2082 tree spec_types;
2083 tree tmpl_types;
2084 tree new_spec_types;
2085 tree old_type;
2086 tree new_type;
2087 tree t;
2088 tree object_type = NULL_TREE;
2089 tree in_charge = NULL_TREE;
2090 tree vtt = NULL_TREE;
2091
2092 /* See if there's anything we need to do. */
2093 tmpl = DECL_TI_TEMPLATE (decl);
2094 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2095 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2096 if (TREE_PURPOSE (t))
2097 break;
2098 if (!t)
2099 return;
2100
2101 old_type = TREE_TYPE (decl);
2102 spec_types = TYPE_ARG_TYPES (old_type);
2103
2104 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2105 {
2106 /* Remove the this pointer, but remember the object's type for
2107 CV quals. */
2108 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2109 spec_types = TREE_CHAIN (spec_types);
2110 tmpl_types = TREE_CHAIN (tmpl_types);
2111
2112 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2113 {
2114 /* DECL may contain more parameters than TMPL due to the extra
2115 in-charge parameter in constructors and destructors. */
2116 in_charge = spec_types;
2117 spec_types = TREE_CHAIN (spec_types);
2118 }
2119 if (DECL_HAS_VTT_PARM_P (decl))
2120 {
2121 vtt = spec_types;
2122 spec_types = TREE_CHAIN (spec_types);
2123 }
2124 }
2125
2126 /* Compute the merged default arguments. */
2127 new_spec_types =
2128 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2129
2130 /* Compute the new FUNCTION_TYPE. */
2131 if (object_type)
2132 {
2133 if (vtt)
2134 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2135 TREE_VALUE (vtt),
2136 new_spec_types);
2137
2138 if (in_charge)
2139 /* Put the in-charge parameter back. */
2140 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2141 TREE_VALUE (in_charge),
2142 new_spec_types);
2143
2144 new_type = build_method_type_directly (object_type,
2145 TREE_TYPE (old_type),
2146 new_spec_types);
2147 }
2148 else
2149 new_type = build_function_type (TREE_TYPE (old_type),
2150 new_spec_types);
2151 new_type = cp_build_type_attribute_variant (new_type,
2152 TYPE_ATTRIBUTES (old_type));
2153 new_type = build_exception_variant (new_type,
2154 TYPE_RAISES_EXCEPTIONS (old_type));
2155 TREE_TYPE (decl) = new_type;
2156 }
2157
2158 /* Check to see if the function just declared, as indicated in
2159 DECLARATOR, and in DECL, is a specialization of a function
2160 template. We may also discover that the declaration is an explicit
2161 instantiation at this point.
2162
2163 Returns DECL, or an equivalent declaration that should be used
2164 instead if all goes well. Issues an error message if something is
2165 amiss. Returns error_mark_node if the error is not easily
2166 recoverable.
2167
2168 FLAGS is a bitmask consisting of the following flags:
2169
2170 2: The function has a definition.
2171 4: The function is a friend.
2172
2173 The TEMPLATE_COUNT is the number of references to qualifying
2174 template classes that appeared in the name of the function. For
2175 example, in
2176
2177 template <class T> struct S { void f(); };
2178 void S<int>::f();
2179
2180 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2181 classes are not counted in the TEMPLATE_COUNT, so that in
2182
2183 template <class T> struct S {};
2184 template <> struct S<int> { void f(); }
2185 template <> void S<int>::f();
2186
2187 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2188 invalid; there should be no template <>.)
2189
2190 If the function is a specialization, it is marked as such via
2191 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2192 is set up correctly, and it is added to the list of specializations
2193 for that template. */
2194
2195 tree
2196 check_explicit_specialization (tree declarator,
2197 tree decl,
2198 int template_count,
2199 int flags)
2200 {
2201 int have_def = flags & 2;
2202 int is_friend = flags & 4;
2203 int specialization = 0;
2204 int explicit_instantiation = 0;
2205 int member_specialization = 0;
2206 tree ctype = DECL_CLASS_CONTEXT (decl);
2207 tree dname = DECL_NAME (decl);
2208 tmpl_spec_kind tsk;
2209
2210 if (is_friend)
2211 {
2212 if (!processing_specialization)
2213 tsk = tsk_none;
2214 else
2215 tsk = tsk_excessive_parms;
2216 }
2217 else
2218 tsk = current_tmpl_spec_kind (template_count);
2219
2220 switch (tsk)
2221 {
2222 case tsk_none:
2223 if (processing_specialization)
2224 {
2225 specialization = 1;
2226 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2227 }
2228 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2229 {
2230 if (is_friend)
2231 /* This could be something like:
2232
2233 template <class T> void f(T);
2234 class S { friend void f<>(int); } */
2235 specialization = 1;
2236 else
2237 {
2238 /* This case handles bogus declarations like template <>
2239 template <class T> void f<int>(); */
2240
2241 error ("template-id %qD in declaration of primary template",
2242 declarator);
2243 return decl;
2244 }
2245 }
2246 break;
2247
2248 case tsk_invalid_member_spec:
2249 /* The error has already been reported in
2250 check_specialization_scope. */
2251 return error_mark_node;
2252
2253 case tsk_invalid_expl_inst:
2254 error ("template parameter list used in explicit instantiation");
2255
2256 /* Fall through. */
2257
2258 case tsk_expl_inst:
2259 if (have_def)
2260 error ("definition provided for explicit instantiation");
2261
2262 explicit_instantiation = 1;
2263 break;
2264
2265 case tsk_excessive_parms:
2266 case tsk_insufficient_parms:
2267 if (tsk == tsk_excessive_parms)
2268 error ("too many template parameter lists in declaration of %qD",
2269 decl);
2270 else if (template_header_count)
2271 error("too few template parameter lists in declaration of %qD", decl);
2272 else
2273 error("explicit specialization of %qD must be introduced by "
2274 "%<template <>%>", decl);
2275
2276 /* Fall through. */
2277 case tsk_expl_spec:
2278 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2279 if (ctype)
2280 member_specialization = 1;
2281 else
2282 specialization = 1;
2283 break;
2284
2285 case tsk_template:
2286 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2287 {
2288 /* This case handles bogus declarations like template <>
2289 template <class T> void f<int>(); */
2290
2291 if (uses_template_parms (declarator))
2292 error ("function template partial specialization %qD "
2293 "is not allowed", declarator);
2294 else
2295 error ("template-id %qD in declaration of primary template",
2296 declarator);
2297 return decl;
2298 }
2299
2300 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2301 /* This is a specialization of a member template, without
2302 specialization the containing class. Something like:
2303
2304 template <class T> struct S {
2305 template <class U> void f (U);
2306 };
2307 template <> template <class U> void S<int>::f(U) {}
2308
2309 That's a specialization -- but of the entire template. */
2310 specialization = 1;
2311 break;
2312
2313 default:
2314 gcc_unreachable ();
2315 }
2316
2317 if (specialization || member_specialization)
2318 {
2319 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2320 for (; t; t = TREE_CHAIN (t))
2321 if (TREE_PURPOSE (t))
2322 {
2323 permerror (input_location,
2324 "default argument specified in explicit specialization");
2325 break;
2326 }
2327 }
2328
2329 if (specialization || member_specialization || explicit_instantiation)
2330 {
2331 tree tmpl = NULL_TREE;
2332 tree targs = NULL_TREE;
2333
2334 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2335 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2336 {
2337 tree fns;
2338
2339 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2340 if (ctype)
2341 fns = dname;
2342 else
2343 {
2344 /* If there is no class context, the explicit instantiation
2345 must be at namespace scope. */
2346 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2347
2348 /* Find the namespace binding, using the declaration
2349 context. */
2350 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2351 false, true);
2352 if (fns == error_mark_node || !is_overloaded_fn (fns))
2353 {
2354 error ("%qD is not a template function", dname);
2355 fns = error_mark_node;
2356 }
2357 else
2358 {
2359 tree fn = OVL_CURRENT (fns);
2360 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2361 CP_DECL_CONTEXT (fn)))
2362 error ("%qD is not declared in %qD",
2363 decl, current_namespace);
2364 }
2365 }
2366
2367 declarator = lookup_template_function (fns, NULL_TREE);
2368 }
2369
2370 if (declarator == error_mark_node)
2371 return error_mark_node;
2372
2373 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2374 {
2375 if (!explicit_instantiation)
2376 /* A specialization in class scope. This is invalid,
2377 but the error will already have been flagged by
2378 check_specialization_scope. */
2379 return error_mark_node;
2380 else
2381 {
2382 /* It's not valid to write an explicit instantiation in
2383 class scope, e.g.:
2384
2385 class C { template void f(); }
2386
2387 This case is caught by the parser. However, on
2388 something like:
2389
2390 template class C { void f(); };
2391
2392 (which is invalid) we can get here. The error will be
2393 issued later. */
2394 ;
2395 }
2396
2397 return decl;
2398 }
2399 else if (ctype != NULL_TREE
2400 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2401 IDENTIFIER_NODE))
2402 {
2403 /* Find the list of functions in ctype that have the same
2404 name as the declared function. */
2405 tree name = TREE_OPERAND (declarator, 0);
2406 tree fns = NULL_TREE;
2407 int idx;
2408
2409 if (constructor_name_p (name, ctype))
2410 {
2411 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2412
2413 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2414 : !CLASSTYPE_DESTRUCTORS (ctype))
2415 {
2416 /* From [temp.expl.spec]:
2417
2418 If such an explicit specialization for the member
2419 of a class template names an implicitly-declared
2420 special member function (clause _special_), the
2421 program is ill-formed.
2422
2423 Similar language is found in [temp.explicit]. */
2424 error ("specialization of implicitly-declared special member function");
2425 return error_mark_node;
2426 }
2427
2428 name = is_constructor ? ctor_identifier : dtor_identifier;
2429 }
2430
2431 if (!DECL_CONV_FN_P (decl))
2432 {
2433 idx = lookup_fnfields_1 (ctype, name);
2434 if (idx >= 0)
2435 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2436 }
2437 else
2438 {
2439 VEC(tree,gc) *methods;
2440 tree ovl;
2441
2442 /* For a type-conversion operator, we cannot do a
2443 name-based lookup. We might be looking for `operator
2444 int' which will be a specialization of `operator T'.
2445 So, we find *all* the conversion operators, and then
2446 select from them. */
2447 fns = NULL_TREE;
2448
2449 methods = CLASSTYPE_METHOD_VEC (ctype);
2450 if (methods)
2451 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2452 VEC_iterate (tree, methods, idx, ovl);
2453 ++idx)
2454 {
2455 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2456 /* There are no more conversion functions. */
2457 break;
2458
2459 /* Glue all these conversion functions together
2460 with those we already have. */
2461 for (; ovl; ovl = OVL_NEXT (ovl))
2462 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2463 }
2464 }
2465
2466 if (fns == NULL_TREE)
2467 {
2468 error ("no member function %qD declared in %qT", name, ctype);
2469 return error_mark_node;
2470 }
2471 else
2472 TREE_OPERAND (declarator, 0) = fns;
2473 }
2474
2475 /* Figure out what exactly is being specialized at this point.
2476 Note that for an explicit instantiation, even one for a
2477 member function, we cannot tell apriori whether the
2478 instantiation is for a member template, or just a member
2479 function of a template class. Even if a member template is
2480 being instantiated, the member template arguments may be
2481 elided if they can be deduced from the rest of the
2482 declaration. */
2483 tmpl = determine_specialization (declarator, decl,
2484 &targs,
2485 member_specialization,
2486 template_count,
2487 tsk);
2488
2489 if (!tmpl || tmpl == error_mark_node)
2490 /* We couldn't figure out what this declaration was
2491 specializing. */
2492 return error_mark_node;
2493 else
2494 {
2495 tree gen_tmpl = most_general_template (tmpl);
2496
2497 if (explicit_instantiation)
2498 {
2499 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2500 is done by do_decl_instantiation later. */
2501
2502 int arg_depth = TMPL_ARGS_DEPTH (targs);
2503 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2504
2505 if (arg_depth > parm_depth)
2506 {
2507 /* If TMPL is not the most general template (for
2508 example, if TMPL is a friend template that is
2509 injected into namespace scope), then there will
2510 be too many levels of TARGS. Remove some of them
2511 here. */
2512 int i;
2513 tree new_targs;
2514
2515 new_targs = make_tree_vec (parm_depth);
2516 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2517 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2518 = TREE_VEC_ELT (targs, i);
2519 targs = new_targs;
2520 }
2521
2522 return instantiate_template (tmpl, targs, tf_error);
2523 }
2524
2525 /* If we thought that the DECL was a member function, but it
2526 turns out to be specializing a static member function,
2527 make DECL a static member function as well. */
2528 if (DECL_STATIC_FUNCTION_P (tmpl)
2529 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2530 revert_static_member_fn (decl);
2531
2532 /* If this is a specialization of a member template of a
2533 template class, we want to return the TEMPLATE_DECL, not
2534 the specialization of it. */
2535 if (tsk == tsk_template)
2536 {
2537 tree result = DECL_TEMPLATE_RESULT (tmpl);
2538 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2539 DECL_INITIAL (result) = NULL_TREE;
2540 if (have_def)
2541 {
2542 tree parm;
2543 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2544 DECL_SOURCE_LOCATION (result)
2545 = DECL_SOURCE_LOCATION (decl);
2546 /* We want to use the argument list specified in the
2547 definition, not in the original declaration. */
2548 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2549 for (parm = DECL_ARGUMENTS (result); parm;
2550 parm = TREE_CHAIN (parm))
2551 DECL_CONTEXT (parm) = result;
2552 }
2553 return register_specialization (tmpl, gen_tmpl, targs,
2554 is_friend, 0);
2555 }
2556
2557 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2558 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2559
2560 /* Inherit default function arguments from the template
2561 DECL is specializing. */
2562 copy_default_args_to_explicit_spec (decl);
2563
2564 /* This specialization has the same protection as the
2565 template it specializes. */
2566 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2567 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2568
2569 /* 7.1.1-1 [dcl.stc]
2570
2571 A storage-class-specifier shall not be specified in an
2572 explicit specialization...
2573
2574 The parser rejects these, so unless action is taken here,
2575 explicit function specializations will always appear with
2576 global linkage.
2577
2578 The action recommended by the C++ CWG in response to C++
2579 defect report 605 is to make the storage class and linkage
2580 of the explicit specialization match the templated function:
2581
2582 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2583 */
2584 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2585 {
2586 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2587 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2588
2589 /* This specialization has the same linkage and visibility as
2590 the function template it specializes. */
2591 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2592 if (! TREE_PUBLIC (decl))
2593 {
2594 DECL_INTERFACE_KNOWN (decl) = 1;
2595 DECL_NOT_REALLY_EXTERN (decl) = 1;
2596 }
2597 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2598 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2599 {
2600 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2601 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2602 }
2603 }
2604
2605 /* If DECL is a friend declaration, declared using an
2606 unqualified name, the namespace associated with DECL may
2607 have been set incorrectly. For example, in:
2608
2609 template <typename T> void f(T);
2610 namespace N {
2611 struct S { friend void f<int>(int); }
2612 }
2613
2614 we will have set the DECL_CONTEXT for the friend
2615 declaration to N, rather than to the global namespace. */
2616 if (DECL_NAMESPACE_SCOPE_P (decl))
2617 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2618
2619 if (is_friend && !have_def)
2620 /* This is not really a declaration of a specialization.
2621 It's just the name of an instantiation. But, it's not
2622 a request for an instantiation, either. */
2623 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2624 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2625 /* This is indeed a specialization. In case of constructors
2626 and destructors, we need in-charge and not-in-charge
2627 versions in V3 ABI. */
2628 clone_function_decl (decl, /*update_method_vec_p=*/0);
2629
2630 /* Register this specialization so that we can find it
2631 again. */
2632 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2633 }
2634 }
2635
2636 return decl;
2637 }
2638
2639 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2640 parameters. These are represented in the same format used for
2641 DECL_TEMPLATE_PARMS. */
2642
2643 int
2644 comp_template_parms (const_tree parms1, const_tree parms2)
2645 {
2646 const_tree p1;
2647 const_tree p2;
2648
2649 if (parms1 == parms2)
2650 return 1;
2651
2652 for (p1 = parms1, p2 = parms2;
2653 p1 != NULL_TREE && p2 != NULL_TREE;
2654 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2655 {
2656 tree t1 = TREE_VALUE (p1);
2657 tree t2 = TREE_VALUE (p2);
2658 int i;
2659
2660 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2661 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2662
2663 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2664 return 0;
2665
2666 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2667 {
2668 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2669 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2670
2671 /* If either of the template parameters are invalid, assume
2672 they match for the sake of error recovery. */
2673 if (parm1 == error_mark_node || parm2 == error_mark_node)
2674 return 1;
2675
2676 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2677 return 0;
2678
2679 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2680 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2681 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2682 continue;
2683 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2684 return 0;
2685 }
2686 }
2687
2688 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2689 /* One set of parameters has more parameters lists than the
2690 other. */
2691 return 0;
2692
2693 return 1;
2694 }
2695
2696 /* Determine whether PARM is a parameter pack. */
2697
2698 bool
2699 template_parameter_pack_p (const_tree parm)
2700 {
2701 /* Determine if we have a non-type template parameter pack. */
2702 if (TREE_CODE (parm) == PARM_DECL)
2703 return (DECL_TEMPLATE_PARM_P (parm)
2704 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2705
2706 /* If this is a list of template parameters, we could get a
2707 TYPE_DECL or a TEMPLATE_DECL. */
2708 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2709 parm = TREE_TYPE (parm);
2710
2711 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2712 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2713 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2714 }
2715
2716 /* Determine if T is a function parameter pack. */
2717
2718 bool
2719 function_parameter_pack_p (const_tree t)
2720 {
2721 if (t && TREE_CODE (t) == PARM_DECL)
2722 return FUNCTION_PARAMETER_PACK_P (t);
2723 return false;
2724 }
2725
2726 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2727 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2728
2729 tree
2730 get_function_template_decl (const_tree primary_func_tmpl_inst)
2731 {
2732 if (! primary_func_tmpl_inst
2733 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2734 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2735 return NULL;
2736
2737 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2738 }
2739
2740 /* Return true iff the function parameter PARAM_DECL was expanded
2741 from the function parameter pack PACK. */
2742
2743 bool
2744 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2745 {
2746 if (DECL_ARTIFICIAL (param_decl)
2747 || !function_parameter_pack_p (pack))
2748 return false;
2749
2750 /* The parameter pack and its pack arguments have the same
2751 DECL_PARM_INDEX. */
2752 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2753 }
2754
2755 /* Determine whether ARGS describes a variadic template args list,
2756 i.e., one that is terminated by a template argument pack. */
2757
2758 static bool
2759 template_args_variadic_p (tree args)
2760 {
2761 int nargs;
2762 tree last_parm;
2763
2764 if (args == NULL_TREE)
2765 return false;
2766
2767 args = INNERMOST_TEMPLATE_ARGS (args);
2768 nargs = TREE_VEC_LENGTH (args);
2769
2770 if (nargs == 0)
2771 return false;
2772
2773 last_parm = TREE_VEC_ELT (args, nargs - 1);
2774
2775 return ARGUMENT_PACK_P (last_parm);
2776 }
2777
2778 /* Generate a new name for the parameter pack name NAME (an
2779 IDENTIFIER_NODE) that incorporates its */
2780
2781 static tree
2782 make_ith_pack_parameter_name (tree name, int i)
2783 {
2784 /* Munge the name to include the parameter index. */
2785 #define NUMBUF_LEN 128
2786 char numbuf[NUMBUF_LEN];
2787 char* newname;
2788 int newname_len;
2789
2790 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2791 newname_len = IDENTIFIER_LENGTH (name)
2792 + strlen (numbuf) + 2;
2793 newname = (char*)alloca (newname_len);
2794 snprintf (newname, newname_len,
2795 "%s#%i", IDENTIFIER_POINTER (name), i);
2796 return get_identifier (newname);
2797 }
2798
2799 /* Return true if T is a primary function
2800 or class template instantiation. */
2801
2802 bool
2803 primary_template_instantiation_p (const_tree t)
2804 {
2805 if (!t)
2806 return false;
2807
2808 if (TREE_CODE (t) == FUNCTION_DECL)
2809 return DECL_LANG_SPECIFIC (t)
2810 && DECL_TEMPLATE_INSTANTIATION (t)
2811 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2812 else if (CLASS_TYPE_P (t))
2813 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2814 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2815 return false;
2816 }
2817
2818 /* Return true if PARM is a template template parameter. */
2819
2820 bool
2821 template_template_parameter_p (const_tree parm)
2822 {
2823 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2824 }
2825
2826 /* Return the template parameters of T if T is a
2827 primary template instantiation, NULL otherwise. */
2828
2829 tree
2830 get_primary_template_innermost_parameters (const_tree t)
2831 {
2832 tree parms = NULL, template_info = NULL;
2833
2834 if ((template_info = get_template_info (t))
2835 && primary_template_instantiation_p (t))
2836 parms = INNERMOST_TEMPLATE_PARMS
2837 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2838
2839 return parms;
2840 }
2841
2842 /* Returns the template arguments of T if T is a template instantiation,
2843 NULL otherwise. */
2844
2845 tree
2846 get_template_innermost_arguments (const_tree t)
2847 {
2848 tree args = NULL, template_info = NULL;
2849
2850 if ((template_info = get_template_info (t))
2851 && TI_ARGS (template_info))
2852 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2853
2854 return args;
2855 }
2856
2857 /* Return the argument pack elements of T if T is a template argument pack,
2858 NULL otherwise. */
2859
2860 tree
2861 get_template_argument_pack_elems (const_tree t)
2862 {
2863 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2864 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2865 return NULL;
2866
2867 return ARGUMENT_PACK_ARGS (t);
2868 }
2869
2870 /* Structure used to track the progress of find_parameter_packs_r. */
2871 struct find_parameter_pack_data
2872 {
2873 /* TREE_LIST that will contain all of the parameter packs found by
2874 the traversal. */
2875 tree* parameter_packs;
2876
2877 /* Set of AST nodes that have been visited by the traversal. */
2878 struct pointer_set_t *visited;
2879 };
2880
2881 /* Identifies all of the argument packs that occur in a template
2882 argument and appends them to the TREE_LIST inside DATA, which is a
2883 find_parameter_pack_data structure. This is a subroutine of
2884 make_pack_expansion and uses_parameter_packs. */
2885 static tree
2886 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2887 {
2888 tree t = *tp;
2889 struct find_parameter_pack_data* ppd =
2890 (struct find_parameter_pack_data*)data;
2891 bool parameter_pack_p = false;
2892
2893 /* Identify whether this is a parameter pack or not. */
2894 switch (TREE_CODE (t))
2895 {
2896 case TEMPLATE_PARM_INDEX:
2897 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2898 parameter_pack_p = true;
2899 break;
2900
2901 case TEMPLATE_TYPE_PARM:
2902 case TEMPLATE_TEMPLATE_PARM:
2903 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2904 parameter_pack_p = true;
2905 break;
2906
2907 case PARM_DECL:
2908 if (FUNCTION_PARAMETER_PACK_P (t))
2909 {
2910 /* We don't want to walk into the type of a PARM_DECL,
2911 because we don't want to see the type parameter pack. */
2912 *walk_subtrees = 0;
2913 parameter_pack_p = true;
2914 }
2915 break;
2916
2917 default:
2918 /* Not a parameter pack. */
2919 break;
2920 }
2921
2922 if (parameter_pack_p)
2923 {
2924 /* Add this parameter pack to the list. */
2925 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2926 }
2927
2928 if (TYPE_P (t))
2929 cp_walk_tree (&TYPE_CONTEXT (t),
2930 &find_parameter_packs_r, ppd, ppd->visited);
2931
2932 /* This switch statement will return immediately if we don't find a
2933 parameter pack. */
2934 switch (TREE_CODE (t))
2935 {
2936 case TEMPLATE_PARM_INDEX:
2937 return NULL_TREE;
2938
2939 case BOUND_TEMPLATE_TEMPLATE_PARM:
2940 /* Check the template itself. */
2941 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2942 &find_parameter_packs_r, ppd, ppd->visited);
2943 /* Check the template arguments. */
2944 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2945 ppd->visited);
2946 *walk_subtrees = 0;
2947 return NULL_TREE;
2948
2949 case TEMPLATE_TYPE_PARM:
2950 case TEMPLATE_TEMPLATE_PARM:
2951 return NULL_TREE;
2952
2953 case PARM_DECL:
2954 return NULL_TREE;
2955
2956 case RECORD_TYPE:
2957 if (TYPE_PTRMEMFUNC_P (t))
2958 return NULL_TREE;
2959 /* Fall through. */
2960
2961 case UNION_TYPE:
2962 case ENUMERAL_TYPE:
2963 if (TYPE_TEMPLATE_INFO (t))
2964 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
2965 &find_parameter_packs_r, ppd, ppd->visited);
2966
2967 *walk_subtrees = 0;
2968 return NULL_TREE;
2969
2970 case TEMPLATE_DECL:
2971 cp_walk_tree (&TREE_TYPE (t),
2972 &find_parameter_packs_r, ppd, ppd->visited);
2973 return NULL_TREE;
2974
2975 case TYPENAME_TYPE:
2976 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
2977 ppd, ppd->visited);
2978 *walk_subtrees = 0;
2979 return NULL_TREE;
2980
2981 case TYPE_PACK_EXPANSION:
2982 case EXPR_PACK_EXPANSION:
2983 *walk_subtrees = 0;
2984 return NULL_TREE;
2985
2986 case INTEGER_TYPE:
2987 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
2988 ppd, ppd->visited);
2989 *walk_subtrees = 0;
2990 return NULL_TREE;
2991
2992 case IDENTIFIER_NODE:
2993 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
2994 ppd->visited);
2995 *walk_subtrees = 0;
2996 return NULL_TREE;
2997
2998 default:
2999 return NULL_TREE;
3000 }
3001
3002 return NULL_TREE;
3003 }
3004
3005 /* Determines if the expression or type T uses any parameter packs. */
3006 bool
3007 uses_parameter_packs (tree t)
3008 {
3009 tree parameter_packs = NULL_TREE;
3010 struct find_parameter_pack_data ppd;
3011 ppd.parameter_packs = &parameter_packs;
3012 ppd.visited = pointer_set_create ();
3013 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3014 pointer_set_destroy (ppd.visited);
3015 return parameter_packs != NULL_TREE;
3016 }
3017
3018 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3019 representation a base-class initializer into a parameter pack
3020 expansion. If all goes well, the resulting node will be an
3021 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3022 respectively. */
3023 tree
3024 make_pack_expansion (tree arg)
3025 {
3026 tree result;
3027 tree parameter_packs = NULL_TREE;
3028 bool for_types = false;
3029 struct find_parameter_pack_data ppd;
3030
3031 if (!arg || arg == error_mark_node)
3032 return arg;
3033
3034 if (TREE_CODE (arg) == TREE_LIST)
3035 {
3036 /* The only time we will see a TREE_LIST here is for a base
3037 class initializer. In this case, the TREE_PURPOSE will be a
3038 _TYPE node (representing the base class expansion we're
3039 initializing) and the TREE_VALUE will be a TREE_LIST
3040 containing the initialization arguments.
3041
3042 The resulting expansion looks somewhat different from most
3043 expansions. Rather than returning just one _EXPANSION, we
3044 return a TREE_LIST whose TREE_PURPOSE is a
3045 TYPE_PACK_EXPANSION containing the bases that will be
3046 initialized. The TREE_VALUE will be identical to the
3047 original TREE_VALUE, which is a list of arguments that will
3048 be passed to each base. We do not introduce any new pack
3049 expansion nodes into the TREE_VALUE (although it is possible
3050 that some already exist), because the TREE_PURPOSE and
3051 TREE_VALUE all need to be expanded together with the same
3052 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3053 resulting TREE_PURPOSE will mention the parameter packs in
3054 both the bases and the arguments to the bases. */
3055 tree purpose;
3056 tree value;
3057 tree parameter_packs = NULL_TREE;
3058
3059 /* Determine which parameter packs will be used by the base
3060 class expansion. */
3061 ppd.visited = pointer_set_create ();
3062 ppd.parameter_packs = &parameter_packs;
3063 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3064 &ppd, ppd.visited);
3065
3066 if (parameter_packs == NULL_TREE)
3067 {
3068 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3069 pointer_set_destroy (ppd.visited);
3070 return error_mark_node;
3071 }
3072
3073 if (TREE_VALUE (arg) != void_type_node)
3074 {
3075 /* Collect the sets of parameter packs used in each of the
3076 initialization arguments. */
3077 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3078 {
3079 /* Determine which parameter packs will be expanded in this
3080 argument. */
3081 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3082 &ppd, ppd.visited);
3083 }
3084 }
3085
3086 pointer_set_destroy (ppd.visited);
3087
3088 /* Create the pack expansion type for the base type. */
3089 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3090 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3091 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3092
3093 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3094 they will rarely be compared to anything. */
3095 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3096
3097 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3098 }
3099
3100 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3101 for_types = true;
3102
3103 /* Build the PACK_EXPANSION_* node. */
3104 result = for_types
3105 ? cxx_make_type (TYPE_PACK_EXPANSION)
3106 : make_node (EXPR_PACK_EXPANSION);
3107 SET_PACK_EXPANSION_PATTERN (result, arg);
3108 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3109 {
3110 /* Propagate type and const-expression information. */
3111 TREE_TYPE (result) = TREE_TYPE (arg);
3112 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3113 }
3114 else
3115 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3116 they will rarely be compared to anything. */
3117 SET_TYPE_STRUCTURAL_EQUALITY (result);
3118
3119 /* Determine which parameter packs will be expanded. */
3120 ppd.parameter_packs = &parameter_packs;
3121 ppd.visited = pointer_set_create ();
3122 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3123 pointer_set_destroy (ppd.visited);
3124
3125 /* Make sure we found some parameter packs. */
3126 if (parameter_packs == NULL_TREE)
3127 {
3128 if (TYPE_P (arg))
3129 error ("expansion pattern %<%T%> contains no argument packs", arg);
3130 else
3131 error ("expansion pattern %<%E%> contains no argument packs", arg);
3132 return error_mark_node;
3133 }
3134 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3135
3136 return result;
3137 }
3138
3139 /* Checks T for any "bare" parameter packs, which have not yet been
3140 expanded, and issues an error if any are found. This operation can
3141 only be done on full expressions or types (e.g., an expression
3142 statement, "if" condition, etc.), because we could have expressions like:
3143
3144 foo(f(g(h(args)))...)
3145
3146 where "args" is a parameter pack. check_for_bare_parameter_packs
3147 should not be called for the subexpressions args, h(args),
3148 g(h(args)), or f(g(h(args))), because we would produce erroneous
3149 error messages.
3150
3151 Returns TRUE and emits an error if there were bare parameter packs,
3152 returns FALSE otherwise. */
3153 bool
3154 check_for_bare_parameter_packs (tree t)
3155 {
3156 tree parameter_packs = NULL_TREE;
3157 struct find_parameter_pack_data ppd;
3158
3159 if (!processing_template_decl || !t || t == error_mark_node)
3160 return false;
3161
3162 if (TREE_CODE (t) == TYPE_DECL)
3163 t = TREE_TYPE (t);
3164
3165 ppd.parameter_packs = &parameter_packs;
3166 ppd.visited = pointer_set_create ();
3167 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3168 pointer_set_destroy (ppd.visited);
3169
3170 if (parameter_packs)
3171 {
3172 error ("parameter packs not expanded with %<...%>:");
3173 while (parameter_packs)
3174 {
3175 tree pack = TREE_VALUE (parameter_packs);
3176 tree name = NULL_TREE;
3177
3178 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3179 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3180 name = TYPE_NAME (pack);
3181 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3182 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3183 else
3184 name = DECL_NAME (pack);
3185
3186 if (name)
3187 inform (input_location, " %qD", name);
3188 else
3189 inform (input_location, " <anonymous>");
3190
3191 parameter_packs = TREE_CHAIN (parameter_packs);
3192 }
3193
3194 return true;
3195 }
3196
3197 return false;
3198 }
3199
3200 /* Expand any parameter packs that occur in the template arguments in
3201 ARGS. */
3202 tree
3203 expand_template_argument_pack (tree args)
3204 {
3205 tree result_args = NULL_TREE;
3206 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3207 int num_result_args = -1;
3208
3209 /* First, determine if we need to expand anything, and the number of
3210 slots we'll need. */
3211 for (in_arg = 0; in_arg < nargs; ++in_arg)
3212 {
3213 tree arg = TREE_VEC_ELT (args, in_arg);
3214 if (arg == NULL_TREE)
3215 return args;
3216 if (ARGUMENT_PACK_P (arg))
3217 {
3218 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3219 if (num_result_args < 0)
3220 num_result_args = in_arg + num_packed;
3221 else
3222 num_result_args += num_packed;
3223 }
3224 else
3225 {
3226 if (num_result_args >= 0)
3227 num_result_args++;
3228 }
3229 }
3230
3231 /* If no expansion is necessary, we're done. */
3232 if (num_result_args < 0)
3233 return args;
3234
3235 /* Expand arguments. */
3236 result_args = make_tree_vec (num_result_args);
3237 for (in_arg = 0; in_arg < nargs; ++in_arg)
3238 {
3239 tree arg = TREE_VEC_ELT (args, in_arg);
3240 if (ARGUMENT_PACK_P (arg))
3241 {
3242 tree packed = ARGUMENT_PACK_ARGS (arg);
3243 int i, num_packed = TREE_VEC_LENGTH (packed);
3244 for (i = 0; i < num_packed; ++i, ++out_arg)
3245 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3246 }
3247 else
3248 {
3249 TREE_VEC_ELT (result_args, out_arg) = arg;
3250 ++out_arg;
3251 }
3252 }
3253
3254 return result_args;
3255 }
3256
3257 /* Checks if DECL shadows a template parameter.
3258
3259 [temp.local]: A template-parameter shall not be redeclared within its
3260 scope (including nested scopes).
3261
3262 Emits an error and returns TRUE if the DECL shadows a parameter,
3263 returns FALSE otherwise. */
3264
3265 bool
3266 check_template_shadow (tree decl)
3267 {
3268 tree olddecl;
3269
3270 /* If we're not in a template, we can't possibly shadow a template
3271 parameter. */
3272 if (!current_template_parms)
3273 return true;
3274
3275 /* Figure out what we're shadowing. */
3276 if (TREE_CODE (decl) == OVERLOAD)
3277 decl = OVL_CURRENT (decl);
3278 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3279
3280 /* If there's no previous binding for this name, we're not shadowing
3281 anything, let alone a template parameter. */
3282 if (!olddecl)
3283 return true;
3284
3285 /* If we're not shadowing a template parameter, we're done. Note
3286 that OLDDECL might be an OVERLOAD (or perhaps even an
3287 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3288 node. */
3289 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3290 return true;
3291
3292 /* We check for decl != olddecl to avoid bogus errors for using a
3293 name inside a class. We check TPFI to avoid duplicate errors for
3294 inline member templates. */
3295 if (decl == olddecl
3296 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3297 return true;
3298
3299 error ("declaration of %q+#D", decl);
3300 error (" shadows template parm %q+#D", olddecl);
3301 return false;
3302 }
3303
3304 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3305 ORIG_LEVEL, DECL, and TYPE. */
3306
3307 static tree
3308 build_template_parm_index (int index,
3309 int level,
3310 int orig_level,
3311 tree decl,
3312 tree type)
3313 {
3314 tree t = make_node (TEMPLATE_PARM_INDEX);
3315 TEMPLATE_PARM_IDX (t) = index;
3316 TEMPLATE_PARM_LEVEL (t) = level;
3317 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3318 TEMPLATE_PARM_DECL (t) = decl;
3319 TREE_TYPE (t) = type;
3320 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3321 TREE_READONLY (t) = TREE_READONLY (decl);
3322
3323 return t;
3324 }
3325
3326 /* Find the canonical type parameter for the given template type
3327 parameter. Returns the canonical type parameter, which may be TYPE
3328 if no such parameter existed. */
3329 static tree
3330 canonical_type_parameter (tree type)
3331 {
3332 tree list;
3333 int idx = TEMPLATE_TYPE_IDX (type);
3334 if (!canonical_template_parms)
3335 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3336
3337 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3338 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3339
3340 list = VEC_index (tree, canonical_template_parms, idx);
3341 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3342 list = TREE_CHAIN (list);
3343
3344 if (list)
3345 return TREE_VALUE (list);
3346 else
3347 {
3348 VEC_replace(tree, canonical_template_parms, idx,
3349 tree_cons (NULL_TREE, type,
3350 VEC_index (tree, canonical_template_parms, idx)));
3351 return type;
3352 }
3353 }
3354
3355 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3356 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3357 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3358 new one is created. */
3359
3360 static tree
3361 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3362 tsubst_flags_t complain)
3363 {
3364 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3365 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3366 != TEMPLATE_PARM_LEVEL (index) - levels)
3367 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3368 {
3369 tree orig_decl = TEMPLATE_PARM_DECL (index);
3370 tree decl, t;
3371
3372 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3373 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3374 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3375 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3376 DECL_ARTIFICIAL (decl) = 1;
3377 SET_DECL_TEMPLATE_PARM_P (decl);
3378
3379 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3380 TEMPLATE_PARM_LEVEL (index) - levels,
3381 TEMPLATE_PARM_ORIG_LEVEL (index),
3382 decl, type);
3383 TEMPLATE_PARM_DESCENDANTS (index) = t;
3384 TEMPLATE_PARM_PARAMETER_PACK (t)
3385 = TEMPLATE_PARM_PARAMETER_PACK (index);
3386
3387 /* Template template parameters need this. */
3388 if (TREE_CODE (decl) == TEMPLATE_DECL)
3389 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3390 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3391 args, complain);
3392 }
3393
3394 return TEMPLATE_PARM_DESCENDANTS (index);
3395 }
3396
3397 /* Process information from new template parameter PARM and append it to the
3398 LIST being built. This new parameter is a non-type parameter iff
3399 IS_NON_TYPE is true. This new parameter is a parameter
3400 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3401 PARM_LOC. */
3402
3403 tree
3404 process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
3405 bool is_parameter_pack)
3406 {
3407 tree decl = 0;
3408 tree defval;
3409 tree err_parm_list;
3410 int idx = 0;
3411
3412 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3413 defval = TREE_PURPOSE (parm);
3414
3415 if (list)
3416 {
3417 tree p = tree_last (list);
3418
3419 if (p && TREE_VALUE (p) != error_mark_node)
3420 {
3421 p = TREE_VALUE (p);
3422 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3423 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3424 else
3425 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3426 }
3427
3428 ++idx;
3429 }
3430 else
3431 idx = 0;
3432
3433 if (is_non_type)
3434 {
3435 parm = TREE_VALUE (parm);
3436
3437 SET_DECL_TEMPLATE_PARM_P (parm);
3438
3439 if (TREE_TYPE (parm) == error_mark_node)
3440 {
3441 err_parm_list = build_tree_list (defval, parm);
3442 TREE_VALUE (err_parm_list) = error_mark_node;
3443 return chainon (list, err_parm_list);
3444 }
3445 else
3446 {
3447 /* [temp.param]
3448
3449 The top-level cv-qualifiers on the template-parameter are
3450 ignored when determining its type. */
3451 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3452 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3453 {
3454 err_parm_list = build_tree_list (defval, parm);
3455 TREE_VALUE (err_parm_list) = error_mark_node;
3456 return chainon (list, err_parm_list);
3457 }
3458
3459 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3460 {
3461 /* This template parameter is not a parameter pack, but it
3462 should be. Complain about "bare" parameter packs. */
3463 check_for_bare_parameter_packs (TREE_TYPE (parm));
3464
3465 /* Recover by calling this a parameter pack. */
3466 is_parameter_pack = true;
3467 }
3468 }
3469
3470 /* A template parameter is not modifiable. */
3471 TREE_CONSTANT (parm) = 1;
3472 TREE_READONLY (parm) = 1;
3473 decl = build_decl (parm_loc,
3474 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3475 TREE_CONSTANT (decl) = 1;
3476 TREE_READONLY (decl) = 1;
3477 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3478 = build_template_parm_index (idx, processing_template_decl,
3479 processing_template_decl,
3480 decl, TREE_TYPE (parm));
3481
3482 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3483 = is_parameter_pack;
3484 }
3485 else
3486 {
3487 tree t;
3488 parm = TREE_VALUE (TREE_VALUE (parm));
3489
3490 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3491 {
3492 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3493 /* This is for distinguishing between real templates and template
3494 template parameters */
3495 TREE_TYPE (parm) = t;
3496 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3497 decl = parm;
3498 }
3499 else
3500 {
3501 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3502 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3503 decl = build_decl (parm_loc,
3504 TYPE_DECL, parm, t);
3505 }
3506
3507 TYPE_NAME (t) = decl;
3508 TYPE_STUB_DECL (t) = decl;
3509 parm = decl;
3510 TEMPLATE_TYPE_PARM_INDEX (t)
3511 = build_template_parm_index (idx, processing_template_decl,
3512 processing_template_decl,
3513 decl, TREE_TYPE (parm));
3514 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3515 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3516 }
3517 DECL_ARTIFICIAL (decl) = 1;
3518 SET_DECL_TEMPLATE_PARM_P (decl);
3519 pushdecl (decl);
3520 parm = build_tree_list (defval, parm);
3521 return chainon (list, parm);
3522 }
3523
3524 /* The end of a template parameter list has been reached. Process the
3525 tree list into a parameter vector, converting each parameter into a more
3526 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3527 as PARM_DECLs. */
3528
3529 tree
3530 end_template_parm_list (tree parms)
3531 {
3532 int nparms;
3533 tree parm, next;
3534 tree saved_parmlist = make_tree_vec (list_length (parms));
3535
3536 current_template_parms
3537 = tree_cons (size_int (processing_template_decl),
3538 saved_parmlist, current_template_parms);
3539
3540 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3541 {
3542 next = TREE_CHAIN (parm);
3543 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3544 TREE_CHAIN (parm) = NULL_TREE;
3545 }
3546
3547 --processing_template_parmlist;
3548
3549 return saved_parmlist;
3550 }
3551
3552 /* end_template_decl is called after a template declaration is seen. */
3553
3554 void
3555 end_template_decl (void)
3556 {
3557 reset_specialization ();
3558
3559 if (! processing_template_decl)
3560 return;
3561
3562 /* This matches the pushlevel in begin_template_parm_list. */
3563 finish_scope ();
3564
3565 --processing_template_decl;
3566 current_template_parms = TREE_CHAIN (current_template_parms);
3567 }
3568
3569 /* Within the declaration of a template, return all levels of template
3570 parameters that apply. The template parameters are represented as
3571 a TREE_VEC, in the form documented in cp-tree.h for template
3572 arguments. */
3573
3574 static tree
3575 current_template_args (void)
3576 {
3577 tree header;
3578 tree args = NULL_TREE;
3579 int length = TMPL_PARMS_DEPTH (current_template_parms);
3580 int l = length;
3581
3582 /* If there is only one level of template parameters, we do not
3583 create a TREE_VEC of TREE_VECs. Instead, we return a single
3584 TREE_VEC containing the arguments. */
3585 if (length > 1)
3586 args = make_tree_vec (length);
3587
3588 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3589 {
3590 tree a = copy_node (TREE_VALUE (header));
3591 int i;
3592
3593 TREE_TYPE (a) = NULL_TREE;
3594 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3595 {
3596 tree t = TREE_VEC_ELT (a, i);
3597
3598 /* T will be a list if we are called from within a
3599 begin/end_template_parm_list pair, but a vector directly
3600 if within a begin/end_member_template_processing pair. */
3601 if (TREE_CODE (t) == TREE_LIST)
3602 {
3603 t = TREE_VALUE (t);
3604
3605 if (!error_operand_p (t))
3606 {
3607 if (TREE_CODE (t) == TYPE_DECL
3608 || TREE_CODE (t) == TEMPLATE_DECL)
3609 {
3610 t = TREE_TYPE (t);
3611
3612 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3613 {
3614 /* Turn this argument into a TYPE_ARGUMENT_PACK
3615 with a single element, which expands T. */
3616 tree vec = make_tree_vec (1);
3617 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3618
3619 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3620 SET_ARGUMENT_PACK_ARGS (t, vec);
3621 }
3622 }
3623 else
3624 {
3625 t = DECL_INITIAL (t);
3626
3627 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3628 {
3629 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3630 with a single element, which expands T. */
3631 tree vec = make_tree_vec (1);
3632 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3633 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3634
3635 t = make_node (NONTYPE_ARGUMENT_PACK);
3636 SET_ARGUMENT_PACK_ARGS (t, vec);
3637 TREE_TYPE (t) = type;
3638 }
3639 }
3640 TREE_VEC_ELT (a, i) = t;
3641 }
3642 }
3643 }
3644
3645 if (length > 1)
3646 TREE_VEC_ELT (args, --l) = a;
3647 else
3648 args = a;
3649 }
3650
3651 return args;
3652 }
3653
3654 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3655 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3656 a member template. Used by push_template_decl below. */
3657
3658 static tree
3659 build_template_decl (tree decl, tree parms, bool member_template_p)
3660 {
3661 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3662 DECL_TEMPLATE_PARMS (tmpl) = parms;
3663 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3664 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3665
3666 return tmpl;
3667 }
3668
3669 struct template_parm_data
3670 {
3671 /* The level of the template parameters we are currently
3672 processing. */
3673 int level;
3674
3675 /* The index of the specialization argument we are currently
3676 processing. */
3677 int current_arg;
3678
3679 /* An array whose size is the number of template parameters. The
3680 elements are nonzero if the parameter has been used in any one
3681 of the arguments processed so far. */
3682 int* parms;
3683
3684 /* An array whose size is the number of template arguments. The
3685 elements are nonzero if the argument makes use of template
3686 parameters of this level. */
3687 int* arg_uses_template_parms;
3688 };
3689
3690 /* Subroutine of push_template_decl used to see if each template
3691 parameter in a partial specialization is used in the explicit
3692 argument list. If T is of the LEVEL given in DATA (which is
3693 treated as a template_parm_data*), then DATA->PARMS is marked
3694 appropriately. */
3695
3696 static int
3697 mark_template_parm (tree t, void* data)
3698 {
3699 int level;
3700 int idx;
3701 struct template_parm_data* tpd = (struct template_parm_data*) data;
3702
3703 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3704 {
3705 level = TEMPLATE_PARM_LEVEL (t);
3706 idx = TEMPLATE_PARM_IDX (t);
3707 }
3708 else
3709 {
3710 level = TEMPLATE_TYPE_LEVEL (t);
3711 idx = TEMPLATE_TYPE_IDX (t);
3712 }
3713
3714 if (level == tpd->level)
3715 {
3716 tpd->parms[idx] = 1;
3717 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3718 }
3719
3720 /* Return zero so that for_each_template_parm will continue the
3721 traversal of the tree; we want to mark *every* template parm. */
3722 return 0;
3723 }
3724
3725 /* Process the partial specialization DECL. */
3726
3727 static tree
3728 process_partial_specialization (tree decl)
3729 {
3730 tree type = TREE_TYPE (decl);
3731 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3732 tree specargs = CLASSTYPE_TI_ARGS (type);
3733 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3734 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3735 tree inner_parms;
3736 int nargs = TREE_VEC_LENGTH (inner_args);
3737 int ntparms;
3738 int i;
3739 int did_error_intro = 0;
3740 struct template_parm_data tpd;
3741 struct template_parm_data tpd2;
3742
3743 gcc_assert (current_template_parms);
3744
3745 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3746 ntparms = TREE_VEC_LENGTH (inner_parms);
3747
3748 /* We check that each of the template parameters given in the
3749 partial specialization is used in the argument list to the
3750 specialization. For example:
3751
3752 template <class T> struct S;
3753 template <class T> struct S<T*>;
3754
3755 The second declaration is OK because `T*' uses the template
3756 parameter T, whereas
3757
3758 template <class T> struct S<int>;
3759
3760 is no good. Even trickier is:
3761
3762 template <class T>
3763 struct S1
3764 {
3765 template <class U>
3766 struct S2;
3767 template <class U>
3768 struct S2<T>;
3769 };
3770
3771 The S2<T> declaration is actually invalid; it is a
3772 full-specialization. Of course,
3773
3774 template <class U>
3775 struct S2<T (*)(U)>;
3776
3777 or some such would have been OK. */
3778 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3779 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3780 memset (tpd.parms, 0, sizeof (int) * ntparms);
3781
3782 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3783 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3784 for (i = 0; i < nargs; ++i)
3785 {
3786 tpd.current_arg = i;
3787 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3788 &mark_template_parm,
3789 &tpd,
3790 NULL,
3791 /*include_nondeduced_p=*/false);
3792 }
3793 for (i = 0; i < ntparms; ++i)
3794 if (tpd.parms[i] == 0)
3795 {
3796 /* One of the template parms was not used in the
3797 specialization. */
3798 if (!did_error_intro)
3799 {
3800 error ("template parameters not used in partial specialization:");
3801 did_error_intro = 1;
3802 }
3803
3804 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3805 }
3806
3807 /* [temp.class.spec]
3808
3809 The argument list of the specialization shall not be identical to
3810 the implicit argument list of the primary template. */
3811 if (comp_template_args
3812 (inner_args,
3813 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3814 (maintmpl)))))
3815 error ("partial specialization %qT does not specialize any template arguments", type);
3816
3817 /* [temp.class.spec]
3818
3819 A partially specialized non-type argument expression shall not
3820 involve template parameters of the partial specialization except
3821 when the argument expression is a simple identifier.
3822
3823 The type of a template parameter corresponding to a specialized
3824 non-type argument shall not be dependent on a parameter of the
3825 specialization.
3826
3827 Also, we verify that pack expansions only occur at the
3828 end of the argument list. */
3829 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3830 tpd2.parms = 0;
3831 for (i = 0; i < nargs; ++i)
3832 {
3833 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3834 tree arg = TREE_VEC_ELT (inner_args, i);
3835 tree packed_args = NULL_TREE;
3836 int j, len = 1;
3837
3838 if (ARGUMENT_PACK_P (arg))
3839 {
3840 /* Extract the arguments from the argument pack. We'll be
3841 iterating over these in the following loop. */
3842 packed_args = ARGUMENT_PACK_ARGS (arg);
3843 len = TREE_VEC_LENGTH (packed_args);
3844 }
3845
3846 for (j = 0; j < len; j++)
3847 {
3848 if (packed_args)
3849 /* Get the Jth argument in the parameter pack. */
3850 arg = TREE_VEC_ELT (packed_args, j);
3851
3852 if (PACK_EXPANSION_P (arg))
3853 {
3854 /* Pack expansions must come at the end of the
3855 argument list. */
3856 if ((packed_args && j < len - 1)
3857 || (!packed_args && i < nargs - 1))
3858 {
3859 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3860 error ("parameter pack argument %qE must be at the "
3861 "end of the template argument list", arg);
3862 else
3863 error ("parameter pack argument %qT must be at the "
3864 "end of the template argument list", arg);
3865 }
3866 }
3867
3868 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3869 /* We only care about the pattern. */
3870 arg = PACK_EXPANSION_PATTERN (arg);
3871
3872 if (/* These first two lines are the `non-type' bit. */
3873 !TYPE_P (arg)
3874 && TREE_CODE (arg) != TEMPLATE_DECL
3875 /* This next line is the `argument expression is not just a
3876 simple identifier' condition and also the `specialized
3877 non-type argument' bit. */
3878 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3879 {
3880 if ((!packed_args && tpd.arg_uses_template_parms[i])
3881 || (packed_args && uses_template_parms (arg)))
3882 error ("template argument %qE involves template parameter(s)",
3883 arg);
3884 else
3885 {
3886 /* Look at the corresponding template parameter,
3887 marking which template parameters its type depends
3888 upon. */
3889 tree type = TREE_TYPE (parm);
3890
3891 if (!tpd2.parms)
3892 {
3893 /* We haven't yet initialized TPD2. Do so now. */
3894 tpd2.arg_uses_template_parms
3895 = (int *) alloca (sizeof (int) * nargs);
3896 /* The number of parameters here is the number in the
3897 main template, which, as checked in the assertion
3898 above, is NARGS. */
3899 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3900 tpd2.level =
3901 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3902 }
3903
3904 /* Mark the template parameters. But this time, we're
3905 looking for the template parameters of the main
3906 template, not in the specialization. */
3907 tpd2.current_arg = i;
3908 tpd2.arg_uses_template_parms[i] = 0;
3909 memset (tpd2.parms, 0, sizeof (int) * nargs);
3910 for_each_template_parm (type,
3911 &mark_template_parm,
3912 &tpd2,
3913 NULL,
3914 /*include_nondeduced_p=*/false);
3915
3916 if (tpd2.arg_uses_template_parms [i])
3917 {
3918 /* The type depended on some template parameters.
3919 If they are fully specialized in the
3920 specialization, that's OK. */
3921 int j;
3922 for (j = 0; j < nargs; ++j)
3923 if (tpd2.parms[j] != 0
3924 && tpd.arg_uses_template_parms [j])
3925 {
3926 error ("type %qT of template argument %qE depends "
3927 "on template parameter(s)",
3928 type,
3929 arg);
3930 break;
3931 }
3932 }
3933 }
3934 }
3935 }
3936 }
3937
3938 /* We should only get here once. */
3939 gcc_assert (!COMPLETE_TYPE_P (type));
3940
3941 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
3942 = tree_cons (specargs, inner_parms,
3943 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
3944 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3945 return decl;
3946 }
3947
3948 /* Check that a template declaration's use of default arguments and
3949 parameter packs is not invalid. Here, PARMS are the template
3950 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
3951 a primary template. IS_PARTIAL is nonzero if DECL is a partial
3952 specialization.
3953
3954
3955 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3956 declaration (but not a definition); 1 indicates a declaration, 2
3957 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3958 emitted for extraneous default arguments.
3959
3960 Returns TRUE if there were no errors found, FALSE otherwise. */
3961
3962 bool
3963 check_default_tmpl_args (tree decl, tree parms, int is_primary,
3964 int is_partial, int is_friend_decl)
3965 {
3966 const char *msg;
3967 int last_level_to_check;
3968 tree parm_level;
3969 bool no_errors = true;
3970
3971 /* [temp.param]
3972
3973 A default template-argument shall not be specified in a
3974 function template declaration or a function template definition, nor
3975 in the template-parameter-list of the definition of a member of a
3976 class template. */
3977
3978 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
3979 /* You can't have a function template declaration in a local
3980 scope, nor you can you define a member of a class template in a
3981 local scope. */
3982 return true;
3983
3984 if (current_class_type
3985 && !TYPE_BEING_DEFINED (current_class_type)
3986 && DECL_LANG_SPECIFIC (decl)
3987 && DECL_DECLARES_FUNCTION_P (decl)
3988 /* If this is either a friend defined in the scope of the class
3989 or a member function. */
3990 && (DECL_FUNCTION_MEMBER_P (decl)
3991 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3992 : DECL_FRIEND_CONTEXT (decl)
3993 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3994 : false)
3995 /* And, if it was a member function, it really was defined in
3996 the scope of the class. */
3997 && (!DECL_FUNCTION_MEMBER_P (decl)
3998 || DECL_INITIALIZED_IN_CLASS_P (decl)))
3999 /* We already checked these parameters when the template was
4000 declared, so there's no need to do it again now. This function
4001 was defined in class scope, but we're processing it's body now
4002 that the class is complete. */
4003 return true;
4004
4005 /* Core issue 226 (C++0x only): the following only applies to class
4006 templates. */
4007 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4008 {
4009 /* [temp.param]
4010
4011 If a template-parameter has a default template-argument, all
4012 subsequent template-parameters shall have a default
4013 template-argument supplied. */
4014 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4015 {
4016 tree inner_parms = TREE_VALUE (parm_level);
4017 int ntparms = TREE_VEC_LENGTH (inner_parms);
4018 int seen_def_arg_p = 0;
4019 int i;
4020
4021 for (i = 0; i < ntparms; ++i)
4022 {
4023 tree parm = TREE_VEC_ELT (inner_parms, i);
4024
4025 if (parm == error_mark_node)
4026 continue;
4027
4028 if (TREE_PURPOSE (parm))
4029 seen_def_arg_p = 1;
4030 else if (seen_def_arg_p
4031 && !template_parameter_pack_p (TREE_VALUE (parm)))
4032 {
4033 error ("no default argument for %qD", TREE_VALUE (parm));
4034 /* For better subsequent error-recovery, we indicate that
4035 there should have been a default argument. */
4036 TREE_PURPOSE (parm) = error_mark_node;
4037 no_errors = false;
4038 }
4039 else if (is_primary
4040 && !is_partial
4041 && !is_friend_decl
4042 /* Don't complain about an enclosing partial
4043 specialization. */
4044 && parm_level == parms
4045 && TREE_CODE (decl) == TYPE_DECL
4046 && i < ntparms - 1
4047 && template_parameter_pack_p (TREE_VALUE (parm)))
4048 {
4049 /* A primary class template can only have one
4050 parameter pack, at the end of the template
4051 parameter list. */
4052
4053 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4054 error ("parameter pack %qE must be at the end of the"
4055 " template parameter list", TREE_VALUE (parm));
4056 else
4057 error ("parameter pack %qT must be at the end of the"
4058 " template parameter list",
4059 TREE_TYPE (TREE_VALUE (parm)));
4060
4061 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4062 = error_mark_node;
4063 no_errors = false;
4064 }
4065 }
4066 }
4067 }
4068
4069 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4070 || is_partial
4071 || !is_primary
4072 || is_friend_decl)
4073 /* For an ordinary class template, default template arguments are
4074 allowed at the innermost level, e.g.:
4075 template <class T = int>
4076 struct S {};
4077 but, in a partial specialization, they're not allowed even
4078 there, as we have in [temp.class.spec]:
4079
4080 The template parameter list of a specialization shall not
4081 contain default template argument values.
4082
4083 So, for a partial specialization, or for a function template
4084 (in C++98/C++03), we look at all of them. */
4085 ;
4086 else
4087 /* But, for a primary class template that is not a partial
4088 specialization we look at all template parameters except the
4089 innermost ones. */
4090 parms = TREE_CHAIN (parms);
4091
4092 /* Figure out what error message to issue. */
4093 if (is_friend_decl == 2)
4094 msg = "default template arguments may not be used in function template friend re-declaration";
4095 else if (is_friend_decl)
4096 msg = "default template arguments may not be used in function template friend declarations";
4097 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4098 msg = ("default template arguments may not be used in function templates "
4099 "without -std=c++0x or -std=gnu++0x");
4100 else if (is_partial)
4101 msg = "default template arguments may not be used in partial specializations";
4102 else
4103 msg = "default argument for template parameter for class enclosing %qD";
4104
4105 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4106 /* If we're inside a class definition, there's no need to
4107 examine the parameters to the class itself. On the one
4108 hand, they will be checked when the class is defined, and,
4109 on the other, default arguments are valid in things like:
4110 template <class T = double>
4111 struct S { template <class U> void f(U); };
4112 Here the default argument for `S' has no bearing on the
4113 declaration of `f'. */
4114 last_level_to_check = template_class_depth (current_class_type) + 1;
4115 else
4116 /* Check everything. */
4117 last_level_to_check = 0;
4118
4119 for (parm_level = parms;
4120 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4121 parm_level = TREE_CHAIN (parm_level))
4122 {
4123 tree inner_parms = TREE_VALUE (parm_level);
4124 int i;
4125 int ntparms;
4126
4127 ntparms = TREE_VEC_LENGTH (inner_parms);
4128 for (i = 0; i < ntparms; ++i)
4129 {
4130 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4131 continue;
4132
4133 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4134 {
4135 if (msg)
4136 {
4137 no_errors = false;
4138 if (is_friend_decl == 2)
4139 return no_errors;
4140
4141 error (msg, decl);
4142 msg = 0;
4143 }
4144
4145 /* Clear out the default argument so that we are not
4146 confused later. */
4147 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4148 }
4149 }
4150
4151 /* At this point, if we're still interested in issuing messages,
4152 they must apply to classes surrounding the object declared. */
4153 if (msg)
4154 msg = "default argument for template parameter for class enclosing %qD";
4155 }
4156
4157 return no_errors;
4158 }
4159
4160 /* Worker for push_template_decl_real, called via
4161 for_each_template_parm. DATA is really an int, indicating the
4162 level of the parameters we are interested in. If T is a template
4163 parameter of that level, return nonzero. */
4164
4165 static int
4166 template_parm_this_level_p (tree t, void* data)
4167 {
4168 int this_level = *(int *)data;
4169 int level;
4170
4171 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4172 level = TEMPLATE_PARM_LEVEL (t);
4173 else
4174 level = TEMPLATE_TYPE_LEVEL (t);
4175 return level == this_level;
4176 }
4177
4178 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4179 parameters given by current_template_args, or reuses a
4180 previously existing one, if appropriate. Returns the DECL, or an
4181 equivalent one, if it is replaced via a call to duplicate_decls.
4182
4183 If IS_FRIEND is true, DECL is a friend declaration. */
4184
4185 tree
4186 push_template_decl_real (tree decl, bool is_friend)
4187 {
4188 tree tmpl;
4189 tree args;
4190 tree info;
4191 tree ctx;
4192 int primary;
4193 int is_partial;
4194 int new_template_p = 0;
4195 /* True if the template is a member template, in the sense of
4196 [temp.mem]. */
4197 bool member_template_p = false;
4198
4199 if (decl == error_mark_node || !current_template_parms)
4200 return error_mark_node;
4201
4202 /* See if this is a partial specialization. */
4203 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4204 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4205 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4206
4207 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4208 is_friend = true;
4209
4210 if (is_friend)
4211 /* For a friend, we want the context of the friend function, not
4212 the type of which it is a friend. */
4213 ctx = DECL_CONTEXT (decl);
4214 else if (CP_DECL_CONTEXT (decl)
4215 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4216 /* In the case of a virtual function, we want the class in which
4217 it is defined. */
4218 ctx = CP_DECL_CONTEXT (decl);
4219 else
4220 /* Otherwise, if we're currently defining some class, the DECL
4221 is assumed to be a member of the class. */
4222 ctx = current_scope ();
4223
4224 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4225 ctx = NULL_TREE;
4226
4227 if (!DECL_CONTEXT (decl))
4228 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4229
4230 /* See if this is a primary template. */
4231 if (is_friend && ctx)
4232 /* A friend template that specifies a class context, i.e.
4233 template <typename T> friend void A<T>::f();
4234 is not primary. */
4235 primary = 0;
4236 else
4237 primary = template_parm_scope_p ();
4238
4239 if (primary)
4240 {
4241 if (DECL_CLASS_SCOPE_P (decl))
4242 member_template_p = true;
4243 if (TREE_CODE (decl) == TYPE_DECL
4244 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4245 {
4246 error ("template class without a name");
4247 return error_mark_node;
4248 }
4249 else if (TREE_CODE (decl) == FUNCTION_DECL)
4250 {
4251 if (DECL_DESTRUCTOR_P (decl))
4252 {
4253 /* [temp.mem]
4254
4255 A destructor shall not be a member template. */
4256 error ("destructor %qD declared as member template", decl);
4257 return error_mark_node;
4258 }
4259 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4260 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
4261 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4262 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4263 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4264 == void_list_node)))
4265 {
4266 /* [basic.stc.dynamic.allocation]
4267
4268 An allocation function can be a function
4269 template. ... Template allocation functions shall
4270 have two or more parameters. */
4271 error ("invalid template declaration of %qD", decl);
4272 return error_mark_node;
4273 }
4274 }
4275 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4276 && CLASS_TYPE_P (TREE_TYPE (decl)))
4277 /* OK */;
4278 else
4279 {
4280 error ("template declaration of %q#D", decl);
4281 return error_mark_node;
4282 }
4283 }
4284
4285 /* Check to see that the rules regarding the use of default
4286 arguments are not being violated. */
4287 check_default_tmpl_args (decl, current_template_parms,
4288 primary, is_partial, /*is_friend_decl=*/0);
4289
4290 /* Ensure that there are no parameter packs in the type of this
4291 declaration that have not been expanded. */
4292 if (TREE_CODE (decl) == FUNCTION_DECL)
4293 {
4294 /* Check each of the arguments individually to see if there are
4295 any bare parameter packs. */
4296 tree type = TREE_TYPE (decl);
4297 tree arg = DECL_ARGUMENTS (decl);
4298 tree argtype = TYPE_ARG_TYPES (type);
4299
4300 while (arg && argtype)
4301 {
4302 if (!FUNCTION_PARAMETER_PACK_P (arg)
4303 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4304 {
4305 /* This is a PARM_DECL that contains unexpanded parameter
4306 packs. We have already complained about this in the
4307 check_for_bare_parameter_packs call, so just replace
4308 these types with ERROR_MARK_NODE. */
4309 TREE_TYPE (arg) = error_mark_node;
4310 TREE_VALUE (argtype) = error_mark_node;
4311 }
4312
4313 arg = TREE_CHAIN (arg);
4314 argtype = TREE_CHAIN (argtype);
4315 }
4316
4317 /* Check for bare parameter packs in the return type and the
4318 exception specifiers. */
4319 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4320 /* Errors were already issued, set return type to int
4321 as the frontend doesn't expect error_mark_node as
4322 the return type. */
4323 TREE_TYPE (type) = integer_type_node;
4324 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4325 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4326 }
4327 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4328 {
4329 TREE_TYPE (decl) = error_mark_node;
4330 return error_mark_node;
4331 }
4332
4333 if (is_partial)
4334 return process_partial_specialization (decl);
4335
4336 args = current_template_args ();
4337
4338 if (!ctx
4339 || TREE_CODE (ctx) == FUNCTION_DECL
4340 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4341 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4342 {
4343 if (DECL_LANG_SPECIFIC (decl)
4344 && DECL_TEMPLATE_INFO (decl)
4345 && DECL_TI_TEMPLATE (decl))
4346 tmpl = DECL_TI_TEMPLATE (decl);
4347 /* If DECL is a TYPE_DECL for a class-template, then there won't
4348 be DECL_LANG_SPECIFIC. The information equivalent to
4349 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4350 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4351 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4352 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4353 {
4354 /* Since a template declaration already existed for this
4355 class-type, we must be redeclaring it here. Make sure
4356 that the redeclaration is valid. */
4357 redeclare_class_template (TREE_TYPE (decl),
4358 current_template_parms);
4359 /* We don't need to create a new TEMPLATE_DECL; just use the
4360 one we already had. */
4361 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4362 }
4363 else
4364 {
4365 tmpl = build_template_decl (decl, current_template_parms,
4366 member_template_p);
4367 new_template_p = 1;
4368
4369 if (DECL_LANG_SPECIFIC (decl)
4370 && DECL_TEMPLATE_SPECIALIZATION (decl))
4371 {
4372 /* A specialization of a member template of a template
4373 class. */
4374 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4375 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4376 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4377 }
4378 }
4379 }
4380 else
4381 {
4382 tree a, t, current, parms;
4383 int i;
4384 tree tinfo = get_template_info (decl);
4385
4386 if (!tinfo)
4387 {
4388 error ("template definition of non-template %q#D", decl);
4389 return error_mark_node;
4390 }
4391
4392 tmpl = TI_TEMPLATE (tinfo);
4393
4394 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4395 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4396 && DECL_TEMPLATE_SPECIALIZATION (decl)
4397 && DECL_MEMBER_TEMPLATE_P (tmpl))
4398 {
4399 tree new_tmpl;
4400
4401 /* The declaration is a specialization of a member
4402 template, declared outside the class. Therefore, the
4403 innermost template arguments will be NULL, so we
4404 replace them with the arguments determined by the
4405 earlier call to check_explicit_specialization. */
4406 args = DECL_TI_ARGS (decl);
4407
4408 new_tmpl
4409 = build_template_decl (decl, current_template_parms,
4410 member_template_p);
4411 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4412 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4413 DECL_TI_TEMPLATE (decl) = new_tmpl;
4414 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4415 DECL_TEMPLATE_INFO (new_tmpl)
4416 = build_template_info (tmpl, args);
4417
4418 register_specialization (new_tmpl,
4419 most_general_template (tmpl),
4420 args,
4421 is_friend, 0);
4422 return decl;
4423 }
4424
4425 /* Make sure the template headers we got make sense. */
4426
4427 parms = DECL_TEMPLATE_PARMS (tmpl);
4428 i = TMPL_PARMS_DEPTH (parms);
4429 if (TMPL_ARGS_DEPTH (args) != i)
4430 {
4431 error ("expected %d levels of template parms for %q#D, got %d",
4432 i, decl, TMPL_ARGS_DEPTH (args));
4433 }
4434 else
4435 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4436 {
4437 a = TMPL_ARGS_LEVEL (args, i);
4438 t = INNERMOST_TEMPLATE_PARMS (parms);
4439
4440 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4441 {
4442 if (current == decl)
4443 error ("got %d template parameters for %q#D",
4444 TREE_VEC_LENGTH (a), decl);
4445 else
4446 error ("got %d template parameters for %q#T",
4447 TREE_VEC_LENGTH (a), current);
4448 error (" but %d required", TREE_VEC_LENGTH (t));
4449 return error_mark_node;
4450 }
4451
4452 if (current == decl)
4453 current = ctx;
4454 else
4455 current = (TYPE_P (current)
4456 ? TYPE_CONTEXT (current)
4457 : DECL_CONTEXT (current));
4458 }
4459
4460 /* Check that the parms are used in the appropriate qualifying scopes
4461 in the declarator. */
4462 if (!comp_template_args
4463 (TI_ARGS (tinfo),
4464 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4465 {
4466 error ("\
4467 template arguments to %qD do not match original template %qD",
4468 decl, DECL_TEMPLATE_RESULT (tmpl));
4469 if (!uses_template_parms (TI_ARGS (tinfo)))
4470 inform (input_location, "use template<> for an explicit specialization");
4471 /* Avoid crash in import_export_decl. */
4472 DECL_INTERFACE_KNOWN (decl) = 1;
4473 return error_mark_node;
4474 }
4475 }
4476
4477 DECL_TEMPLATE_RESULT (tmpl) = decl;
4478 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4479
4480 /* Push template declarations for global functions and types. Note
4481 that we do not try to push a global template friend declared in a
4482 template class; such a thing may well depend on the template
4483 parameters of the class. */
4484 if (new_template_p && !ctx
4485 && !(is_friend && template_class_depth (current_class_type) > 0))
4486 {
4487 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4488 if (tmpl == error_mark_node)
4489 return error_mark_node;
4490
4491 /* Hide template friend classes that haven't been declared yet. */
4492 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4493 {
4494 DECL_ANTICIPATED (tmpl) = 1;
4495 DECL_FRIEND_P (tmpl) = 1;
4496 }
4497 }
4498
4499 if (primary)
4500 {
4501 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4502 int i;
4503
4504 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4505 if (DECL_CONV_FN_P (tmpl))
4506 {
4507 int depth = TMPL_PARMS_DEPTH (parms);
4508
4509 /* It is a conversion operator. See if the type converted to
4510 depends on innermost template operands. */
4511
4512 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4513 depth))
4514 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4515 }
4516
4517 /* Give template template parms a DECL_CONTEXT of the template
4518 for which they are a parameter. */
4519 parms = INNERMOST_TEMPLATE_PARMS (parms);
4520 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4521 {
4522 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4523 if (TREE_CODE (parm) == TEMPLATE_DECL)
4524 DECL_CONTEXT (parm) = tmpl;
4525
4526 if (TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM)
4527 DECL_CONTEXT (TYPE_NAME (TREE_TYPE (parm))) = tmpl;
4528 }
4529 }
4530
4531 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4532 back to its most general template. If TMPL is a specialization,
4533 ARGS may only have the innermost set of arguments. Add the missing
4534 argument levels if necessary. */
4535 if (DECL_TEMPLATE_INFO (tmpl))
4536 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4537
4538 info = build_template_info (tmpl, args);
4539
4540 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4541 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4542 else if (DECL_LANG_SPECIFIC (decl))
4543 DECL_TEMPLATE_INFO (decl) = info;
4544
4545 return DECL_TEMPLATE_RESULT (tmpl);
4546 }
4547
4548 tree
4549 push_template_decl (tree decl)
4550 {
4551 return push_template_decl_real (decl, false);
4552 }
4553
4554 /* Called when a class template TYPE is redeclared with the indicated
4555 template PARMS, e.g.:
4556
4557 template <class T> struct S;
4558 template <class T> struct S {}; */
4559
4560 bool
4561 redeclare_class_template (tree type, tree parms)
4562 {
4563 tree tmpl;
4564 tree tmpl_parms;
4565 int i;
4566
4567 if (!TYPE_TEMPLATE_INFO (type))
4568 {
4569 error ("%qT is not a template type", type);
4570 return false;
4571 }
4572
4573 tmpl = TYPE_TI_TEMPLATE (type);
4574 if (!PRIMARY_TEMPLATE_P (tmpl))
4575 /* The type is nested in some template class. Nothing to worry
4576 about here; there are no new template parameters for the nested
4577 type. */
4578 return true;
4579
4580 if (!parms)
4581 {
4582 error ("template specifiers not specified in declaration of %qD",
4583 tmpl);
4584 return false;
4585 }
4586
4587 parms = INNERMOST_TEMPLATE_PARMS (parms);
4588 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4589
4590 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4591 {
4592 error ("redeclared with %d template parameter(s)",
4593 TREE_VEC_LENGTH (parms));
4594 inform (input_location, "previous declaration %q+D used %d template parameter(s)",
4595 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4596 return false;
4597 }
4598
4599 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4600 {
4601 tree tmpl_parm;
4602 tree parm;
4603 tree tmpl_default;
4604 tree parm_default;
4605
4606 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4607 || TREE_VEC_ELT (parms, i) == error_mark_node)
4608 continue;
4609
4610 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4611 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4612 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4613 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4614
4615 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4616 TEMPLATE_DECL. */
4617 if (tmpl_parm != error_mark_node
4618 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4619 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4620 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4621 || (TREE_CODE (tmpl_parm) != PARM_DECL
4622 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4623 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4624 || (TREE_CODE (tmpl_parm) == PARM_DECL
4625 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4626 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))))
4627 {
4628 error ("template parameter %q+#D", tmpl_parm);
4629 error ("redeclared here as %q#D", parm);
4630 return false;
4631 }
4632
4633 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4634 {
4635 /* We have in [temp.param]:
4636
4637 A template-parameter may not be given default arguments
4638 by two different declarations in the same scope. */
4639 error_at (input_location, "redefinition of default argument for %q#D", parm);
4640 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4641 "original definition appeared here");
4642 return false;
4643 }
4644
4645 if (parm_default != NULL_TREE)
4646 /* Update the previous template parameters (which are the ones
4647 that will really count) with the new default value. */
4648 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4649 else if (tmpl_default != NULL_TREE)
4650 /* Update the new parameters, too; they'll be used as the
4651 parameters for any members. */
4652 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4653 }
4654
4655 return true;
4656 }
4657
4658 /* Simplify EXPR if it is a non-dependent expression. Returns the
4659 (possibly simplified) expression. */
4660
4661 tree
4662 fold_non_dependent_expr (tree expr)
4663 {
4664 if (expr == NULL_TREE)
4665 return NULL_TREE;
4666
4667 /* If we're in a template, but EXPR isn't value dependent, simplify
4668 it. We're supposed to treat:
4669
4670 template <typename T> void f(T[1 + 1]);
4671 template <typename T> void f(T[2]);
4672
4673 as two declarations of the same function, for example. */
4674 if (processing_template_decl
4675 && !type_dependent_expression_p (expr)
4676 && !value_dependent_expression_p (expr))
4677 {
4678 HOST_WIDE_INT saved_processing_template_decl;
4679
4680 saved_processing_template_decl = processing_template_decl;
4681 processing_template_decl = 0;
4682 expr = tsubst_copy_and_build (expr,
4683 /*args=*/NULL_TREE,
4684 tf_error,
4685 /*in_decl=*/NULL_TREE,
4686 /*function_p=*/false,
4687 /*integral_constant_expression_p=*/true);
4688 processing_template_decl = saved_processing_template_decl;
4689 }
4690 return expr;
4691 }
4692
4693 /* EXPR is an expression which is used in a constant-expression context.
4694 For instance, it could be a VAR_DECL with a constant initializer.
4695 Extract the innermost constant expression.
4696
4697 This is basically a more powerful version of
4698 integral_constant_value, which can be used also in templates where
4699 initializers can maintain a syntactic rather than semantic form
4700 (even if they are non-dependent, for access-checking purposes). */
4701
4702 static tree
4703 fold_decl_constant_value (tree expr)
4704 {
4705 tree const_expr = expr;
4706 do
4707 {
4708 expr = fold_non_dependent_expr (const_expr);
4709 const_expr = integral_constant_value (expr);
4710 }
4711 while (expr != const_expr);
4712
4713 return expr;
4714 }
4715
4716 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4717 must be a function or a pointer-to-function type, as specified
4718 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4719 and check that the resulting function has external linkage. */
4720
4721 static tree
4722 convert_nontype_argument_function (tree type, tree expr)
4723 {
4724 tree fns = expr;
4725 tree fn, fn_no_ptr;
4726
4727 fn = instantiate_type (type, fns, tf_none);
4728 if (fn == error_mark_node)
4729 return error_mark_node;
4730
4731 fn_no_ptr = fn;
4732 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4733 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4734 if (TREE_CODE (fn_no_ptr) == BASELINK)
4735 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4736
4737 /* [temp.arg.nontype]/1
4738
4739 A template-argument for a non-type, non-template template-parameter
4740 shall be one of:
4741 [...]
4742 -- the address of an object or function with external linkage. */
4743 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4744 {
4745 error ("%qE is not a valid template argument for type %qT "
4746 "because function %qD has not external linkage",
4747 expr, type, fn_no_ptr);
4748 return NULL_TREE;
4749 }
4750
4751 return fn;
4752 }
4753
4754 /* Subroutine of convert_nontype_argument.
4755 Check if EXPR of type TYPE is a valid pointer-to-member constant.
4756 Emit an error otherwise. */
4757
4758 static bool
4759 check_valid_ptrmem_cst_expr (tree type, tree expr)
4760 {
4761 STRIP_NOPS (expr);
4762 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
4763 return true;
4764 error ("%qE is not a valid template argument for type %qT",
4765 expr, type);
4766 error ("it must be a pointer-to-member of the form `&X::Y'");
4767 return false;
4768 }
4769
4770 /* Attempt to convert the non-type template parameter EXPR to the
4771 indicated TYPE. If the conversion is successful, return the
4772 converted value. If the conversion is unsuccessful, return
4773 NULL_TREE if we issued an error message, or error_mark_node if we
4774 did not. We issue error messages for out-and-out bad template
4775 parameters, but not simply because the conversion failed, since we
4776 might be just trying to do argument deduction. Both TYPE and EXPR
4777 must be non-dependent.
4778
4779 The conversion follows the special rules described in
4780 [temp.arg.nontype], and it is much more strict than an implicit
4781 conversion.
4782
4783 This function is called twice for each template argument (see
4784 lookup_template_class for a more accurate description of this
4785 problem). This means that we need to handle expressions which
4786 are not valid in a C++ source, but can be created from the
4787 first call (for instance, casts to perform conversions). These
4788 hacks can go away after we fix the double coercion problem. */
4789
4790 static tree
4791 convert_nontype_argument (tree type, tree expr)
4792 {
4793 tree expr_type;
4794
4795 /* Detect immediately string literals as invalid non-type argument.
4796 This special-case is not needed for correctness (we would easily
4797 catch this later), but only to provide better diagnostic for this
4798 common user mistake. As suggested by DR 100, we do not mention
4799 linkage issues in the diagnostic as this is not the point. */
4800 if (TREE_CODE (expr) == STRING_CST)
4801 {
4802 error ("%qE is not a valid template argument for type %qT "
4803 "because string literals can never be used in this context",
4804 expr, type);
4805 return NULL_TREE;
4806 }
4807
4808 /* If we are in a template, EXPR may be non-dependent, but still
4809 have a syntactic, rather than semantic, form. For example, EXPR
4810 might be a SCOPE_REF, rather than the VAR_DECL to which the
4811 SCOPE_REF refers. Preserving the qualifying scope is necessary
4812 so that access checking can be performed when the template is
4813 instantiated -- but here we need the resolved form so that we can
4814 convert the argument. */
4815 expr = fold_non_dependent_expr (expr);
4816 if (error_operand_p (expr))
4817 return error_mark_node;
4818 expr_type = TREE_TYPE (expr);
4819
4820 /* HACK: Due to double coercion, we can get a
4821 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4822 which is the tree that we built on the first call (see
4823 below when coercing to reference to object or to reference to
4824 function). We just strip everything and get to the arg.
4825 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4826 for examples. */
4827 if (TREE_CODE (expr) == NOP_EXPR)
4828 {
4829 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4830 {
4831 /* ??? Maybe we could use convert_from_reference here, but we
4832 would need to relax its constraints because the NOP_EXPR
4833 could actually change the type to something more cv-qualified,
4834 and this is not folded by convert_from_reference. */
4835 tree addr = TREE_OPERAND (expr, 0);
4836 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4837 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4838 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4839 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4840 (TREE_TYPE (expr_type),
4841 TREE_TYPE (TREE_TYPE (addr))));
4842
4843 expr = TREE_OPERAND (addr, 0);
4844 expr_type = TREE_TYPE (expr);
4845 }
4846
4847 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4848 parameter is a pointer to object, through decay and
4849 qualification conversion. Let's strip everything. */
4850 else if (TYPE_PTROBV_P (type))
4851 {
4852 STRIP_NOPS (expr);
4853 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4854 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4855 /* Skip the ADDR_EXPR only if it is part of the decay for
4856 an array. Otherwise, it is part of the original argument
4857 in the source code. */
4858 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4859 expr = TREE_OPERAND (expr, 0);
4860 expr_type = TREE_TYPE (expr);
4861 }
4862 }
4863
4864 /* [temp.arg.nontype]/5, bullet 1
4865
4866 For a non-type template-parameter of integral or enumeration type,
4867 integral promotions (_conv.prom_) and integral conversions
4868 (_conv.integral_) are applied. */
4869 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4870 {
4871 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
4872 return error_mark_node;
4873
4874 expr = fold_decl_constant_value (expr);
4875 /* Notice that there are constant expressions like '4 % 0' which
4876 do not fold into integer constants. */
4877 if (TREE_CODE (expr) != INTEGER_CST)
4878 {
4879 error ("%qE is not a valid template argument for type %qT "
4880 "because it is a non-constant expression", expr, type);
4881 return NULL_TREE;
4882 }
4883
4884 /* At this point, an implicit conversion does what we want,
4885 because we already know that the expression is of integral
4886 type. */
4887 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4888 if (expr == error_mark_node)
4889 return error_mark_node;
4890
4891 /* Conversion was allowed: fold it to a bare integer constant. */
4892 expr = fold (expr);
4893 }
4894 /* [temp.arg.nontype]/5, bullet 2
4895
4896 For a non-type template-parameter of type pointer to object,
4897 qualification conversions (_conv.qual_) and the array-to-pointer
4898 conversion (_conv.array_) are applied. */
4899 else if (TYPE_PTROBV_P (type))
4900 {
4901 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4902
4903 A template-argument for a non-type, non-template template-parameter
4904 shall be one of: [...]
4905
4906 -- the name of a non-type template-parameter;
4907 -- the address of an object or function with external linkage, [...]
4908 expressed as "& id-expression" where the & is optional if the name
4909 refers to a function or array, or if the corresponding
4910 template-parameter is a reference.
4911
4912 Here, we do not care about functions, as they are invalid anyway
4913 for a parameter of type pointer-to-object. */
4914
4915 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4916 /* Non-type template parameters are OK. */
4917 ;
4918 else if (TREE_CODE (expr) != ADDR_EXPR
4919 && TREE_CODE (expr_type) != ARRAY_TYPE)
4920 {
4921 if (TREE_CODE (expr) == VAR_DECL)
4922 {
4923 error ("%qD is not a valid template argument "
4924 "because %qD is a variable, not the address of "
4925 "a variable",
4926 expr, expr);
4927 return NULL_TREE;
4928 }
4929 /* Other values, like integer constants, might be valid
4930 non-type arguments of some other type. */
4931 return error_mark_node;
4932 }
4933 else
4934 {
4935 tree decl;
4936
4937 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4938 ? TREE_OPERAND (expr, 0) : expr);
4939 if (TREE_CODE (decl) != VAR_DECL)
4940 {
4941 error ("%qE is not a valid template argument of type %qT "
4942 "because %qE is not a variable",
4943 expr, type, decl);
4944 return NULL_TREE;
4945 }
4946 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4947 {
4948 error ("%qE is not a valid template argument of type %qT "
4949 "because %qD does not have external linkage",
4950 expr, type, decl);
4951 return NULL_TREE;
4952 }
4953 }
4954
4955 expr = decay_conversion (expr);
4956 if (expr == error_mark_node)
4957 return error_mark_node;
4958
4959 expr = perform_qualification_conversions (type, expr);
4960 if (expr == error_mark_node)
4961 return error_mark_node;
4962 }
4963 /* [temp.arg.nontype]/5, bullet 3
4964
4965 For a non-type template-parameter of type reference to object, no
4966 conversions apply. The type referred to by the reference may be more
4967 cv-qualified than the (otherwise identical) type of the
4968 template-argument. The template-parameter is bound directly to the
4969 template-argument, which must be an lvalue. */
4970 else if (TYPE_REF_OBJ_P (type))
4971 {
4972 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4973 expr_type))
4974 return error_mark_node;
4975
4976 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4977 {
4978 error ("%qE is not a valid template argument for type %qT "
4979 "because of conflicts in cv-qualification", expr, type);
4980 return NULL_TREE;
4981 }
4982
4983 if (!real_lvalue_p (expr))
4984 {
4985 error ("%qE is not a valid template argument for type %qT "
4986 "because it is not an lvalue", expr, type);
4987 return NULL_TREE;
4988 }
4989
4990 /* [temp.arg.nontype]/1
4991
4992 A template-argument for a non-type, non-template template-parameter
4993 shall be one of: [...]
4994
4995 -- the address of an object or function with external linkage. */
4996 if (TREE_CODE (expr) == INDIRECT_REF
4997 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
4998 {
4999 expr = TREE_OPERAND (expr, 0);
5000 if (DECL_P (expr))
5001 {
5002 error ("%q#D is not a valid template argument for type %qT "
5003 "because a reference variable does not have a constant "
5004 "address", expr, type);
5005 return NULL_TREE;
5006 }
5007 }
5008
5009 if (!DECL_P (expr))
5010 {
5011 error ("%qE is not a valid template argument for type %qT "
5012 "because it is not an object with external linkage",
5013 expr, type);
5014 return NULL_TREE;
5015 }
5016
5017 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5018 {
5019 error ("%qE is not a valid template argument for type %qT "
5020 "because object %qD has not external linkage",
5021 expr, type, expr);
5022 return NULL_TREE;
5023 }
5024
5025 expr = build_nop (type, build_address (expr));
5026 }
5027 /* [temp.arg.nontype]/5, bullet 4
5028
5029 For a non-type template-parameter of type pointer to function, only
5030 the function-to-pointer conversion (_conv.func_) is applied. If the
5031 template-argument represents a set of overloaded functions (or a
5032 pointer to such), the matching function is selected from the set
5033 (_over.over_). */
5034 else if (TYPE_PTRFN_P (type))
5035 {
5036 /* If the argument is a template-id, we might not have enough
5037 context information to decay the pointer. */
5038 if (!type_unknown_p (expr_type))
5039 {
5040 expr = decay_conversion (expr);
5041 if (expr == error_mark_node)
5042 return error_mark_node;
5043 }
5044
5045 expr = convert_nontype_argument_function (type, expr);
5046 if (!expr || expr == error_mark_node)
5047 return expr;
5048
5049 if (TREE_CODE (expr) != ADDR_EXPR)
5050 {
5051 error ("%qE is not a valid template argument for type %qT", expr, type);
5052 error ("it must be the address of a function with external linkage");
5053 return NULL_TREE;
5054 }
5055 }
5056 /* [temp.arg.nontype]/5, bullet 5
5057
5058 For a non-type template-parameter of type reference to function, no
5059 conversions apply. If the template-argument represents a set of
5060 overloaded functions, the matching function is selected from the set
5061 (_over.over_). */
5062 else if (TYPE_REFFN_P (type))
5063 {
5064 if (TREE_CODE (expr) == ADDR_EXPR)
5065 {
5066 error ("%qE is not a valid template argument for type %qT "
5067 "because it is a pointer", expr, type);
5068 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5069 return NULL_TREE;
5070 }
5071
5072 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5073 if (!expr || expr == error_mark_node)
5074 return expr;
5075
5076 expr = build_nop (type, build_address (expr));
5077 }
5078 /* [temp.arg.nontype]/5, bullet 6
5079
5080 For a non-type template-parameter of type pointer to member function,
5081 no conversions apply. If the template-argument represents a set of
5082 overloaded member functions, the matching member function is selected
5083 from the set (_over.over_). */
5084 else if (TYPE_PTRMEMFUNC_P (type))
5085 {
5086 expr = instantiate_type (type, expr, tf_none);
5087 if (expr == error_mark_node)
5088 return error_mark_node;
5089
5090 /* [temp.arg.nontype] bullet 1 says the pointer to member
5091 expression must be a pointer-to-member constant. */
5092 if (!check_valid_ptrmem_cst_expr (type, expr))
5093 return error_mark_node;
5094
5095 /* There is no way to disable standard conversions in
5096 resolve_address_of_overloaded_function (called by
5097 instantiate_type). It is possible that the call succeeded by
5098 converting &B::I to &D::I (where B is a base of D), so we need
5099 to reject this conversion here.
5100
5101 Actually, even if there was a way to disable standard conversions,
5102 it would still be better to reject them here so that we can
5103 provide a superior diagnostic. */
5104 if (!same_type_p (TREE_TYPE (expr), type))
5105 {
5106 /* Make sure we are just one standard conversion off. */
5107 gcc_assert (can_convert (type, TREE_TYPE (expr)));
5108 error ("%qE is not a valid template argument for type %qT "
5109 "because it is of type %qT", expr, type,
5110 TREE_TYPE (expr));
5111 inform (input_location, "standard conversions are not allowed in this context");
5112 return NULL_TREE;
5113 }
5114 }
5115 /* [temp.arg.nontype]/5, bullet 7
5116
5117 For a non-type template-parameter of type pointer to data member,
5118 qualification conversions (_conv.qual_) are applied. */
5119 else if (TYPE_PTRMEM_P (type))
5120 {
5121 /* [temp.arg.nontype] bullet 1 says the pointer to member
5122 expression must be a pointer-to-member constant. */
5123 if (!check_valid_ptrmem_cst_expr (type, expr))
5124 return error_mark_node;
5125
5126 expr = perform_qualification_conversions (type, expr);
5127 if (expr == error_mark_node)
5128 return expr;
5129 }
5130 /* A template non-type parameter must be one of the above. */
5131 else
5132 gcc_unreachable ();
5133
5134 /* Sanity check: did we actually convert the argument to the
5135 right type? */
5136 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
5137 return expr;
5138 }
5139
5140 /* Subroutine of coerce_template_template_parms, which returns 1 if
5141 PARM_PARM and ARG_PARM match using the rule for the template
5142 parameters of template template parameters. Both PARM and ARG are
5143 template parameters; the rest of the arguments are the same as for
5144 coerce_template_template_parms.
5145 */
5146 static int
5147 coerce_template_template_parm (tree parm,
5148 tree arg,
5149 tsubst_flags_t complain,
5150 tree in_decl,
5151 tree outer_args)
5152 {
5153 if (arg == NULL_TREE || arg == error_mark_node
5154 || parm == NULL_TREE || parm == error_mark_node)
5155 return 0;
5156
5157 if (TREE_CODE (arg) != TREE_CODE (parm))
5158 return 0;
5159
5160 switch (TREE_CODE (parm))
5161 {
5162 case TEMPLATE_DECL:
5163 /* We encounter instantiations of templates like
5164 template <template <template <class> class> class TT>
5165 class C; */
5166 {
5167 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5168 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5169
5170 if (!coerce_template_template_parms
5171 (parmparm, argparm, complain, in_decl, outer_args))
5172 return 0;
5173 }
5174 /* Fall through. */
5175
5176 case TYPE_DECL:
5177 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5178 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5179 /* Argument is a parameter pack but parameter is not. */
5180 return 0;
5181 break;
5182
5183 case PARM_DECL:
5184 /* The tsubst call is used to handle cases such as
5185
5186 template <int> class C {};
5187 template <class T, template <T> class TT> class D {};
5188 D<int, C> d;
5189
5190 i.e. the parameter list of TT depends on earlier parameters. */
5191 if (!uses_template_parms (TREE_TYPE (arg))
5192 && !same_type_p
5193 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5194 TREE_TYPE (arg)))
5195 return 0;
5196
5197 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5198 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5199 /* Argument is a parameter pack but parameter is not. */
5200 return 0;
5201
5202 break;
5203
5204 default:
5205 gcc_unreachable ();
5206 }
5207
5208 return 1;
5209 }
5210
5211
5212 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5213 template template parameters. Both PARM_PARMS and ARG_PARMS are
5214 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5215 or PARM_DECL.
5216
5217 Consider the example:
5218 template <class T> class A;
5219 template<template <class U> class TT> class B;
5220
5221 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5222 the parameters to A, and OUTER_ARGS contains A. */
5223
5224 static int
5225 coerce_template_template_parms (tree parm_parms,
5226 tree arg_parms,
5227 tsubst_flags_t complain,
5228 tree in_decl,
5229 tree outer_args)
5230 {
5231 int nparms, nargs, i;
5232 tree parm, arg;
5233 int variadic_p = 0;
5234
5235 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5236 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5237
5238 nparms = TREE_VEC_LENGTH (parm_parms);
5239 nargs = TREE_VEC_LENGTH (arg_parms);
5240
5241 /* Determine whether we have a parameter pack at the end of the
5242 template template parameter's template parameter list. */
5243 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5244 {
5245 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5246
5247 if (parm == error_mark_node)
5248 return 0;
5249
5250 switch (TREE_CODE (parm))
5251 {
5252 case TEMPLATE_DECL:
5253 case TYPE_DECL:
5254 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5255 variadic_p = 1;
5256 break;
5257
5258 case PARM_DECL:
5259 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5260 variadic_p = 1;
5261 break;
5262
5263 default:
5264 gcc_unreachable ();
5265 }
5266 }
5267
5268 if (nargs != nparms
5269 && !(variadic_p && nargs >= nparms - 1))
5270 return 0;
5271
5272 /* Check all of the template parameters except the parameter pack at
5273 the end (if any). */
5274 for (i = 0; i < nparms - variadic_p; ++i)
5275 {
5276 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5277 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5278 continue;
5279
5280 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5281 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5282
5283 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5284 outer_args))
5285 return 0;
5286
5287 }
5288
5289 if (variadic_p)
5290 {
5291 /* Check each of the template parameters in the template
5292 argument against the template parameter pack at the end of
5293 the template template parameter. */
5294 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5295 return 0;
5296
5297 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5298
5299 for (; i < nargs; ++i)
5300 {
5301 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5302 continue;
5303
5304 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5305
5306 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5307 outer_args))
5308 return 0;
5309 }
5310 }
5311
5312 return 1;
5313 }
5314
5315 /* Verifies that the deduced template arguments (in TARGS) for the
5316 template template parameters (in TPARMS) represent valid bindings,
5317 by comparing the template parameter list of each template argument
5318 to the template parameter list of its corresponding template
5319 template parameter, in accordance with DR150. This
5320 routine can only be called after all template arguments have been
5321 deduced. It will return TRUE if all of the template template
5322 parameter bindings are okay, FALSE otherwise. */
5323 bool
5324 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5325 {
5326 int i, ntparms = TREE_VEC_LENGTH (tparms);
5327 bool ret = true;
5328
5329 /* We're dealing with template parms in this process. */
5330 ++processing_template_decl;
5331
5332 targs = INNERMOST_TEMPLATE_ARGS (targs);
5333
5334 for (i = 0; i < ntparms; ++i)
5335 {
5336 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5337 tree targ = TREE_VEC_ELT (targs, i);
5338
5339 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5340 {
5341 tree packed_args = NULL_TREE;
5342 int idx, len = 1;
5343
5344 if (ARGUMENT_PACK_P (targ))
5345 {
5346 /* Look inside the argument pack. */
5347 packed_args = ARGUMENT_PACK_ARGS (targ);
5348 len = TREE_VEC_LENGTH (packed_args);
5349 }
5350
5351 for (idx = 0; idx < len; ++idx)
5352 {
5353 tree targ_parms = NULL_TREE;
5354
5355 if (packed_args)
5356 /* Extract the next argument from the argument
5357 pack. */
5358 targ = TREE_VEC_ELT (packed_args, idx);
5359
5360 if (PACK_EXPANSION_P (targ))
5361 /* Look at the pattern of the pack expansion. */
5362 targ = PACK_EXPANSION_PATTERN (targ);
5363
5364 /* Extract the template parameters from the template
5365 argument. */
5366 if (TREE_CODE (targ) == TEMPLATE_DECL)
5367 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5368 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5369 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5370
5371 /* Verify that we can coerce the template template
5372 parameters from the template argument to the template
5373 parameter. This requires an exact match. */
5374 if (targ_parms
5375 && !coerce_template_template_parms
5376 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5377 targ_parms,
5378 tf_none,
5379 tparm,
5380 targs))
5381 {
5382 ret = false;
5383 goto out;
5384 }
5385 }
5386 }
5387 }
5388
5389 out:
5390
5391 --processing_template_decl;
5392 return ret;
5393 }
5394
5395 /* Convert the indicated template ARG as necessary to match the
5396 indicated template PARM. Returns the converted ARG, or
5397 error_mark_node if the conversion was unsuccessful. Error and
5398 warning messages are issued under control of COMPLAIN. This
5399 conversion is for the Ith parameter in the parameter list. ARGS is
5400 the full set of template arguments deduced so far. */
5401
5402 static tree
5403 convert_template_argument (tree parm,
5404 tree arg,
5405 tree args,
5406 tsubst_flags_t complain,
5407 int i,
5408 tree in_decl)
5409 {
5410 tree orig_arg;
5411 tree val;
5412 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5413
5414 if (TREE_CODE (arg) == TREE_LIST
5415 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5416 {
5417 /* The template argument was the name of some
5418 member function. That's usually
5419 invalid, but static members are OK. In any
5420 case, grab the underlying fields/functions
5421 and issue an error later if required. */
5422 orig_arg = TREE_VALUE (arg);
5423 TREE_TYPE (arg) = unknown_type_node;
5424 }
5425
5426 orig_arg = arg;
5427
5428 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5429 requires_type = (TREE_CODE (parm) == TYPE_DECL
5430 || requires_tmpl_type);
5431
5432 /* When determining whether an argument pack expansion is a template,
5433 look at the pattern. */
5434 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5435 arg = PACK_EXPANSION_PATTERN (arg);
5436
5437 /* Deal with an injected-class-name used as a template template arg. */
5438 if (requires_tmpl_type && CLASS_TYPE_P (arg))
5439 {
5440 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
5441 if (TREE_CODE (t) == TEMPLATE_DECL)
5442 {
5443 if (complain & tf_warning_or_error)
5444 pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
5445 " used as template template argument", TYPE_NAME (arg));
5446 else if (flag_pedantic_errors)
5447 t = arg;
5448
5449 arg = t;
5450 }
5451 }
5452
5453 is_tmpl_type =
5454 ((TREE_CODE (arg) == TEMPLATE_DECL
5455 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5456 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5457 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
5458
5459 if (is_tmpl_type
5460 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5461 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
5462 arg = TYPE_STUB_DECL (arg);
5463
5464 is_type = TYPE_P (arg) || is_tmpl_type;
5465
5466 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5467 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5468 {
5469 permerror (input_location, "to refer to a type member of a template parameter, "
5470 "use %<typename %E%>", orig_arg);
5471
5472 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
5473 TREE_OPERAND (arg, 1),
5474 typename_type,
5475 complain & tf_error);
5476 arg = orig_arg;
5477 is_type = 1;
5478 }
5479 if (is_type != requires_type)
5480 {
5481 if (in_decl)
5482 {
5483 if (complain & tf_error)
5484 {
5485 error ("type/value mismatch at argument %d in template "
5486 "parameter list for %qD",
5487 i + 1, in_decl);
5488 if (is_type)
5489 error (" expected a constant of type %qT, got %qT",
5490 TREE_TYPE (parm),
5491 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
5492 else if (requires_tmpl_type)
5493 error (" expected a class template, got %qE", orig_arg);
5494 else
5495 error (" expected a type, got %qE", orig_arg);
5496 }
5497 }
5498 return error_mark_node;
5499 }
5500 if (is_tmpl_type ^ requires_tmpl_type)
5501 {
5502 if (in_decl && (complain & tf_error))
5503 {
5504 error ("type/value mismatch at argument %d in template "
5505 "parameter list for %qD",
5506 i + 1, in_decl);
5507 if (is_tmpl_type)
5508 error (" expected a type, got %qT", DECL_NAME (arg));
5509 else
5510 error (" expected a class template, got %qT", orig_arg);
5511 }
5512 return error_mark_node;
5513 }
5514
5515 if (is_type)
5516 {
5517 if (requires_tmpl_type)
5518 {
5519 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5520 /* The number of argument required is not known yet.
5521 Just accept it for now. */
5522 val = TREE_TYPE (arg);
5523 else
5524 {
5525 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5526 tree argparm;
5527
5528 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5529
5530 if (coerce_template_template_parms (parmparm, argparm,
5531 complain, in_decl,
5532 args))
5533 {
5534 val = arg;
5535
5536 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5537 TEMPLATE_DECL. */
5538 if (val != error_mark_node)
5539 {
5540 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5541 val = TREE_TYPE (val);
5542 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
5543 val = make_pack_expansion (val);
5544 }
5545 }
5546 else
5547 {
5548 if (in_decl && (complain & tf_error))
5549 {
5550 error ("type/value mismatch at argument %d in "
5551 "template parameter list for %qD",
5552 i + 1, in_decl);
5553 error (" expected a template of type %qD, got %qT",
5554 parm, orig_arg);
5555 }
5556
5557 val = error_mark_node;
5558 }
5559 }
5560 }
5561 else
5562 val = orig_arg;
5563 /* We only form one instance of each template specialization.
5564 Therefore, if we use a non-canonical variant (i.e., a
5565 typedef), any future messages referring to the type will use
5566 the typedef, which is confusing if those future uses do not
5567 themselves also use the typedef. */
5568 if (TYPE_P (val))
5569 val = strip_typedefs (val);
5570 }
5571 else
5572 {
5573 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5574
5575 if (invalid_nontype_parm_type_p (t, complain))
5576 return error_mark_node;
5577
5578 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5579 {
5580 if (same_type_p (t, TREE_TYPE (orig_arg)))
5581 val = orig_arg;
5582 else
5583 {
5584 /* Not sure if this is reachable, but it doesn't hurt
5585 to be robust. */
5586 error ("type mismatch in nontype parameter pack");
5587 val = error_mark_node;
5588 }
5589 }
5590 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5591 /* We used to call digest_init here. However, digest_init
5592 will report errors, which we don't want when complain
5593 is zero. More importantly, digest_init will try too
5594 hard to convert things: for example, `0' should not be
5595 converted to pointer type at this point according to
5596 the standard. Accepting this is not merely an
5597 extension, since deciding whether or not these
5598 conversions can occur is part of determining which
5599 function template to call, or whether a given explicit
5600 argument specification is valid. */
5601 val = convert_nontype_argument (t, orig_arg);
5602 else
5603 val = orig_arg;
5604
5605 if (val == NULL_TREE)
5606 val = error_mark_node;
5607 else if (val == error_mark_node && (complain & tf_error))
5608 error ("could not convert template argument %qE to %qT", orig_arg, t);
5609
5610 if (TREE_CODE (val) == SCOPE_REF)
5611 {
5612 /* Strip typedefs from the SCOPE_REF. */
5613 tree type = strip_typedefs (TREE_TYPE (val));
5614 tree scope = strip_typedefs (TREE_OPERAND (val, 0));
5615 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
5616 QUALIFIED_NAME_IS_TEMPLATE (val));
5617 }
5618 }
5619
5620 return val;
5621 }
5622
5623 /* Coerces the remaining template arguments in INNER_ARGS (from
5624 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5625 Returns the coerced argument pack. PARM_IDX is the position of this
5626 parameter in the template parameter list. ARGS is the original
5627 template argument list. */
5628 static tree
5629 coerce_template_parameter_pack (tree parms,
5630 int parm_idx,
5631 tree args,
5632 tree inner_args,
5633 int arg_idx,
5634 tree new_args,
5635 int* lost,
5636 tree in_decl,
5637 tsubst_flags_t complain)
5638 {
5639 tree parm = TREE_VEC_ELT (parms, parm_idx);
5640 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5641 tree packed_args;
5642 tree argument_pack;
5643 tree packed_types = NULL_TREE;
5644
5645 if (arg_idx > nargs)
5646 arg_idx = nargs;
5647
5648 packed_args = make_tree_vec (nargs - arg_idx);
5649
5650 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5651 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5652 {
5653 /* When the template parameter is a non-type template
5654 parameter pack whose type uses parameter packs, we need
5655 to look at each of the template arguments
5656 separately. Build a vector of the types for these
5657 non-type template parameters in PACKED_TYPES. */
5658 tree expansion
5659 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5660 packed_types = tsubst_pack_expansion (expansion, args,
5661 complain, in_decl);
5662
5663 if (packed_types == error_mark_node)
5664 return error_mark_node;
5665
5666 /* Check that we have the right number of arguments. */
5667 if (arg_idx < nargs
5668 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5669 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5670 {
5671 int needed_parms
5672 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5673 error ("wrong number of template arguments (%d, should be %d)",
5674 nargs, needed_parms);
5675 return error_mark_node;
5676 }
5677
5678 /* If we aren't able to check the actual arguments now
5679 (because they haven't been expanded yet), we can at least
5680 verify that all of the types used for the non-type
5681 template parameter pack are, in fact, valid for non-type
5682 template parameters. */
5683 if (arg_idx < nargs
5684 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5685 {
5686 int j, len = TREE_VEC_LENGTH (packed_types);
5687 for (j = 0; j < len; ++j)
5688 {
5689 tree t = TREE_VEC_ELT (packed_types, j);
5690 if (invalid_nontype_parm_type_p (t, complain))
5691 return error_mark_node;
5692 }
5693 }
5694 }
5695
5696 /* Convert the remaining arguments, which will be a part of the
5697 parameter pack "parm". */
5698 for (; arg_idx < nargs; ++arg_idx)
5699 {
5700 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5701 tree actual_parm = TREE_VALUE (parm);
5702
5703 if (packed_types && !PACK_EXPANSION_P (arg))
5704 {
5705 /* When we have a vector of types (corresponding to the
5706 non-type template parameter pack that uses parameter
5707 packs in its type, as mention above), and the
5708 argument is not an expansion (which expands to a
5709 currently unknown number of arguments), clone the
5710 parm and give it the next type in PACKED_TYPES. */
5711 actual_parm = copy_node (actual_parm);
5712 TREE_TYPE (actual_parm) =
5713 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5714 }
5715
5716 if (arg != error_mark_node)
5717 arg = convert_template_argument (actual_parm,
5718 arg, new_args, complain, parm_idx,
5719 in_decl);
5720 if (arg == error_mark_node)
5721 (*lost)++;
5722 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5723 }
5724
5725 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5726 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5727 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
5728 else
5729 {
5730 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5731 TREE_TYPE (argument_pack)
5732 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5733 TREE_CONSTANT (argument_pack) = 1;
5734 }
5735
5736 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5737 return argument_pack;
5738 }
5739
5740 /* Convert all template arguments to their appropriate types, and
5741 return a vector containing the innermost resulting template
5742 arguments. If any error occurs, return error_mark_node. Error and
5743 warning messages are issued under control of COMPLAIN.
5744
5745 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5746 for arguments not specified in ARGS. Otherwise, if
5747 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5748 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5749 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5750 ARGS. */
5751
5752 static tree
5753 coerce_template_parms (tree parms,
5754 tree args,
5755 tree in_decl,
5756 tsubst_flags_t complain,
5757 bool require_all_args,
5758 bool use_default_args)
5759 {
5760 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5761 tree inner_args;
5762 tree new_args;
5763 tree new_inner_args;
5764 int saved_unevaluated_operand;
5765 int saved_inhibit_evaluation_warnings;
5766
5767 /* When used as a boolean value, indicates whether this is a
5768 variadic template parameter list. Since it's an int, we can also
5769 subtract it from nparms to get the number of non-variadic
5770 parameters. */
5771 int variadic_p = 0;
5772
5773 nparms = TREE_VEC_LENGTH (parms);
5774
5775 /* Determine if there are any parameter packs. */
5776 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5777 {
5778 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5779 if (template_parameter_pack_p (tparm))
5780 ++variadic_p;
5781 }
5782
5783 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5784 /* If there are 0 or 1 parameter packs, we need to expand any argument
5785 packs so that we can deduce a parameter pack from some non-packed args
5786 followed by an argument pack, as in variadic85.C. If there are more
5787 than that, we need to leave argument packs intact so the arguments are
5788 assigned to the right parameter packs. This should only happen when
5789 dealing with a nested class inside a partial specialization of a class
5790 template, as in variadic92.C. */
5791 if (variadic_p <= 1)
5792 inner_args = expand_template_argument_pack (inner_args);
5793
5794 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5795 if ((nargs > nparms && !variadic_p)
5796 || (nargs < nparms - variadic_p
5797 && require_all_args
5798 && (!use_default_args
5799 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5800 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
5801 {
5802 if (complain & tf_error)
5803 {
5804 const char *or_more = "";
5805 if (variadic_p)
5806 {
5807 or_more = " or more";
5808 --nparms;
5809 }
5810
5811 error ("wrong number of template arguments (%d, should be %d%s)",
5812 nargs, nparms, or_more);
5813
5814 if (in_decl)
5815 error ("provided for %q+D", in_decl);
5816 }
5817
5818 return error_mark_node;
5819 }
5820
5821 /* We need to evaluate the template arguments, even though this
5822 template-id may be nested within a "sizeof". */
5823 saved_unevaluated_operand = cp_unevaluated_operand;
5824 cp_unevaluated_operand = 0;
5825 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5826 c_inhibit_evaluation_warnings = 0;
5827 new_inner_args = make_tree_vec (nparms);
5828 new_args = add_outermost_template_args (args, new_inner_args);
5829 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5830 {
5831 tree arg;
5832 tree parm;
5833
5834 /* Get the Ith template parameter. */
5835 parm = TREE_VEC_ELT (parms, parm_idx);
5836
5837 if (parm == error_mark_node)
5838 {
5839 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5840 continue;
5841 }
5842
5843 /* Calculate the next argument. */
5844 if (arg_idx < nargs)
5845 arg = TREE_VEC_ELT (inner_args, arg_idx);
5846 else
5847 arg = NULL_TREE;
5848
5849 if (template_parameter_pack_p (TREE_VALUE (parm))
5850 && !(arg && ARGUMENT_PACK_P (arg)))
5851 {
5852 /* All remaining arguments will be placed in the
5853 template parameter pack PARM. */
5854 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5855 inner_args, arg_idx,
5856 new_args, &lost,
5857 in_decl, complain);
5858
5859 /* Store this argument. */
5860 if (arg == error_mark_node)
5861 lost++;
5862 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5863
5864 /* We are done with all of the arguments. */
5865 arg_idx = nargs;
5866
5867 continue;
5868 }
5869 else if (arg)
5870 {
5871 if (PACK_EXPANSION_P (arg))
5872 {
5873 if (complain & tf_error)
5874 {
5875 /* FIXME this restriction was removed by N2555; see
5876 bug 35722. */
5877 /* If ARG is a pack expansion, but PARM is not a
5878 template parameter pack (if it were, we would have
5879 handled it above), we're trying to expand into a
5880 fixed-length argument list. */
5881 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5882 sorry ("cannot expand %<%E%> into a fixed-length "
5883 "argument list", arg);
5884 else
5885 sorry ("cannot expand %<%T%> into a fixed-length "
5886 "argument list", arg);
5887 }
5888 return error_mark_node;
5889 }
5890 }
5891 else if (require_all_args)
5892 /* There must be a default arg in this case. */
5893 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
5894 complain, in_decl);
5895 else
5896 break;
5897
5898 if (arg == error_mark_node)
5899 {
5900 if (complain & tf_error)
5901 error ("template argument %d is invalid", arg_idx + 1);
5902 }
5903 else if (!arg)
5904 /* This only occurs if there was an error in the template
5905 parameter list itself (which we would already have
5906 reported) that we are trying to recover from, e.g., a class
5907 template with a parameter list such as
5908 template<typename..., typename>. */
5909 return error_mark_node;
5910 else
5911 arg = convert_template_argument (TREE_VALUE (parm),
5912 arg, new_args, complain,
5913 parm_idx, in_decl);
5914
5915 if (arg == error_mark_node)
5916 lost++;
5917 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
5918 }
5919 cp_unevaluated_operand = saved_unevaluated_operand;
5920 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
5921
5922 if (lost)
5923 return error_mark_node;
5924
5925 return new_inner_args;
5926 }
5927
5928 /* Returns 1 if template args OT and NT are equivalent. */
5929
5930 static int
5931 template_args_equal (tree ot, tree nt)
5932 {
5933 if (nt == ot)
5934 return 1;
5935
5936 if (TREE_CODE (nt) == TREE_VEC)
5937 /* For member templates */
5938 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
5939 else if (PACK_EXPANSION_P (ot))
5940 return PACK_EXPANSION_P (nt)
5941 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
5942 PACK_EXPANSION_PATTERN (nt));
5943 else if (ARGUMENT_PACK_P (ot))
5944 {
5945 int i, len;
5946 tree opack, npack;
5947
5948 if (!ARGUMENT_PACK_P (nt))
5949 return 0;
5950
5951 opack = ARGUMENT_PACK_ARGS (ot);
5952 npack = ARGUMENT_PACK_ARGS (nt);
5953 len = TREE_VEC_LENGTH (opack);
5954 if (TREE_VEC_LENGTH (npack) != len)
5955 return 0;
5956 for (i = 0; i < len; ++i)
5957 if (!template_args_equal (TREE_VEC_ELT (opack, i),
5958 TREE_VEC_ELT (npack, i)))
5959 return 0;
5960 return 1;
5961 }
5962 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
5963 {
5964 /* We get here probably because we are in the middle of substituting
5965 into the pattern of a pack expansion. In that case the
5966 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
5967 interested in. So we want to use the initial pack argument for
5968 the comparison. */
5969 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
5970 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
5971 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
5972 return template_args_equal (ot, nt);
5973 }
5974 else if (TYPE_P (nt))
5975 return TYPE_P (ot) && same_type_p (ot, nt);
5976 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
5977 return 0;
5978 else
5979 return cp_tree_equal (ot, nt);
5980 }
5981
5982 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5983 of template arguments. Returns 0 otherwise. */
5984
5985 int
5986 comp_template_args (tree oldargs, tree newargs)
5987 {
5988 int i;
5989
5990 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5991 return 0;
5992
5993 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5994 {
5995 tree nt = TREE_VEC_ELT (newargs, i);
5996 tree ot = TREE_VEC_ELT (oldargs, i);
5997
5998 if (! template_args_equal (ot, nt))
5999 return 0;
6000 }
6001 return 1;
6002 }
6003
6004 static void
6005 add_pending_template (tree d)
6006 {
6007 tree ti = (TYPE_P (d)
6008 ? CLASSTYPE_TEMPLATE_INFO (d)
6009 : DECL_TEMPLATE_INFO (d));
6010 struct pending_template *pt;
6011 int level;
6012
6013 if (TI_PENDING_TEMPLATE_FLAG (ti))
6014 return;
6015
6016 /* We are called both from instantiate_decl, where we've already had a
6017 tinst_level pushed, and instantiate_template, where we haven't.
6018 Compensate. */
6019 level = !current_tinst_level || current_tinst_level->decl != d;
6020
6021 if (level)
6022 push_tinst_level (d);
6023
6024 pt = GGC_NEW (struct pending_template);
6025 pt->next = NULL;
6026 pt->tinst = current_tinst_level;
6027 if (last_pending_template)
6028 last_pending_template->next = pt;
6029 else
6030 pending_templates = pt;
6031
6032 last_pending_template = pt;
6033
6034 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6035
6036 if (level)
6037 pop_tinst_level ();
6038 }
6039
6040
6041 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6042 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6043 documentation for TEMPLATE_ID_EXPR. */
6044
6045 tree
6046 lookup_template_function (tree fns, tree arglist)
6047 {
6048 tree type;
6049
6050 if (fns == error_mark_node || arglist == error_mark_node)
6051 return error_mark_node;
6052
6053 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6054 gcc_assert (fns && (is_overloaded_fn (fns)
6055 || TREE_CODE (fns) == IDENTIFIER_NODE));
6056
6057 if (BASELINK_P (fns))
6058 {
6059 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6060 unknown_type_node,
6061 BASELINK_FUNCTIONS (fns),
6062 arglist);
6063 return fns;
6064 }
6065
6066 type = TREE_TYPE (fns);
6067 if (TREE_CODE (fns) == OVERLOAD || !type)
6068 type = unknown_type_node;
6069
6070 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6071 }
6072
6073 /* Within the scope of a template class S<T>, the name S gets bound
6074 (in build_self_reference) to a TYPE_DECL for the class, not a
6075 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6076 or one of its enclosing classes, and that type is a template,
6077 return the associated TEMPLATE_DECL. Otherwise, the original
6078 DECL is returned.
6079
6080 Also handle the case when DECL is a TREE_LIST of ambiguous
6081 injected-class-names from different bases. */
6082
6083 tree
6084 maybe_get_template_decl_from_type_decl (tree decl)
6085 {
6086 if (decl == NULL_TREE)
6087 return decl;
6088
6089 /* DR 176: A lookup that finds an injected-class-name (10.2
6090 [class.member.lookup]) can result in an ambiguity in certain cases
6091 (for example, if it is found in more than one base class). If all of
6092 the injected-class-names that are found refer to specializations of
6093 the same class template, and if the name is followed by a
6094 template-argument-list, the reference refers to the class template
6095 itself and not a specialization thereof, and is not ambiguous. */
6096 if (TREE_CODE (decl) == TREE_LIST)
6097 {
6098 tree t, tmpl = NULL_TREE;
6099 for (t = decl; t; t = TREE_CHAIN (t))
6100 {
6101 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6102 if (!tmpl)
6103 tmpl = elt;
6104 else if (tmpl != elt)
6105 break;
6106 }
6107 if (tmpl && t == NULL_TREE)
6108 return tmpl;
6109 else
6110 return decl;
6111 }
6112
6113 return (decl != NULL_TREE
6114 && DECL_SELF_REFERENCE_P (decl)
6115 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6116 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6117 }
6118
6119 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6120 parameters, find the desired type.
6121
6122 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6123
6124 IN_DECL, if non-NULL, is the template declaration we are trying to
6125 instantiate.
6126
6127 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6128 the class we are looking up.
6129
6130 Issue error and warning messages under control of COMPLAIN.
6131
6132 If the template class is really a local class in a template
6133 function, then the FUNCTION_CONTEXT is the function in which it is
6134 being instantiated.
6135
6136 ??? Note that this function is currently called *twice* for each
6137 template-id: the first time from the parser, while creating the
6138 incomplete type (finish_template_type), and the second type during the
6139 real instantiation (instantiate_template_class). This is surely something
6140 that we want to avoid. It also causes some problems with argument
6141 coercion (see convert_nontype_argument for more information on this). */
6142
6143 tree
6144 lookup_template_class (tree d1,
6145 tree arglist,
6146 tree in_decl,
6147 tree context,
6148 int entering_scope,
6149 tsubst_flags_t complain)
6150 {
6151 tree templ = NULL_TREE, parmlist;
6152 tree t;
6153 spec_entry **slot;
6154 spec_entry *entry;
6155 spec_entry elt;
6156 hashval_t hash;
6157
6158 timevar_push (TV_NAME_LOOKUP);
6159
6160 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6161 {
6162 tree value = innermost_non_namespace_value (d1);
6163 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6164 templ = value;
6165 else
6166 {
6167 if (context)
6168 push_decl_namespace (context);
6169 templ = lookup_name (d1);
6170 templ = maybe_get_template_decl_from_type_decl (templ);
6171 if (context)
6172 pop_decl_namespace ();
6173 }
6174 if (templ)
6175 context = DECL_CONTEXT (templ);
6176 }
6177 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6178 {
6179 tree type = TREE_TYPE (d1);
6180
6181 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6182 an implicit typename for the second A. Deal with it. */
6183 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6184 type = TREE_TYPE (type);
6185
6186 if (CLASSTYPE_TEMPLATE_INFO (type))
6187 {
6188 templ = CLASSTYPE_TI_TEMPLATE (type);
6189 d1 = DECL_NAME (templ);
6190 }
6191 }
6192 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6193 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6194 {
6195 templ = TYPE_TI_TEMPLATE (d1);
6196 d1 = DECL_NAME (templ);
6197 }
6198 else if (TREE_CODE (d1) == TEMPLATE_DECL
6199 && DECL_TEMPLATE_RESULT (d1)
6200 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6201 {
6202 templ = d1;
6203 d1 = DECL_NAME (templ);
6204 context = DECL_CONTEXT (templ);
6205 }
6206
6207 /* Issue an error message if we didn't find a template. */
6208 if (! templ)
6209 {
6210 if (complain & tf_error)
6211 error ("%qT is not a template", d1);
6212 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6213 }
6214
6215 if (TREE_CODE (templ) != TEMPLATE_DECL
6216 /* Make sure it's a user visible template, if it was named by
6217 the user. */
6218 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6219 && !PRIMARY_TEMPLATE_P (templ)))
6220 {
6221 if (complain & tf_error)
6222 {
6223 error ("non-template type %qT used as a template", d1);
6224 if (in_decl)
6225 error ("for template declaration %q+D", in_decl);
6226 }
6227 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6228 }
6229
6230 complain &= ~tf_user;
6231
6232 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6233 {
6234 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6235 template arguments */
6236
6237 tree parm;
6238 tree arglist2;
6239 tree outer;
6240
6241 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6242
6243 /* Consider an example where a template template parameter declared as
6244
6245 template <class T, class U = std::allocator<T> > class TT
6246
6247 The template parameter level of T and U are one level larger than
6248 of TT. To proper process the default argument of U, say when an
6249 instantiation `TT<int>' is seen, we need to build the full
6250 arguments containing {int} as the innermost level. Outer levels,
6251 available when not appearing as default template argument, can be
6252 obtained from the arguments of the enclosing template.
6253
6254 Suppose that TT is later substituted with std::vector. The above
6255 instantiation is `TT<int, std::allocator<T> >' with TT at
6256 level 1, and T at level 2, while the template arguments at level 1
6257 becomes {std::vector} and the inner level 2 is {int}. */
6258
6259 outer = DECL_CONTEXT (templ);
6260 if (outer)
6261 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6262 else if (current_template_parms)
6263 /* This is an argument of the current template, so we haven't set
6264 DECL_CONTEXT yet. */
6265 outer = current_template_args ();
6266
6267 if (outer)
6268 arglist = add_to_template_args (outer, arglist);
6269
6270 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6271 complain,
6272 /*require_all_args=*/true,
6273 /*use_default_args=*/true);
6274 if (arglist2 == error_mark_node
6275 || (!uses_template_parms (arglist2)
6276 && check_instantiated_args (templ, arglist2, complain)))
6277 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6278
6279 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6280 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
6281 }
6282 else
6283 {
6284 tree template_type = TREE_TYPE (templ);
6285 tree gen_tmpl;
6286 tree type_decl;
6287 tree found = NULL_TREE;
6288 int arg_depth;
6289 int parm_depth;
6290 int is_partial_instantiation;
6291
6292 gen_tmpl = most_general_template (templ);
6293 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6294 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6295 arg_depth = TMPL_ARGS_DEPTH (arglist);
6296
6297 if (arg_depth == 1 && parm_depth > 1)
6298 {
6299 /* We've been given an incomplete set of template arguments.
6300 For example, given:
6301
6302 template <class T> struct S1 {
6303 template <class U> struct S2 {};
6304 template <class U> struct S2<U*> {};
6305 };
6306
6307 we will be called with an ARGLIST of `U*', but the
6308 TEMPLATE will be `template <class T> template
6309 <class U> struct S1<T>::S2'. We must fill in the missing
6310 arguments. */
6311 arglist
6312 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6313 arglist);
6314 arg_depth = TMPL_ARGS_DEPTH (arglist);
6315 }
6316
6317 /* Now we should have enough arguments. */
6318 gcc_assert (parm_depth == arg_depth);
6319
6320 /* From here on, we're only interested in the most general
6321 template. */
6322
6323 /* Calculate the BOUND_ARGS. These will be the args that are
6324 actually tsubst'd into the definition to create the
6325 instantiation. */
6326 if (parm_depth > 1)
6327 {
6328 /* We have multiple levels of arguments to coerce, at once. */
6329 int i;
6330 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6331
6332 tree bound_args = make_tree_vec (parm_depth);
6333
6334 for (i = saved_depth,
6335 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6336 i > 0 && t != NULL_TREE;
6337 --i, t = TREE_CHAIN (t))
6338 {
6339 tree a = coerce_template_parms (TREE_VALUE (t),
6340 arglist, gen_tmpl,
6341 complain,
6342 /*require_all_args=*/true,
6343 /*use_default_args=*/true);
6344
6345 /* Don't process further if one of the levels fails. */
6346 if (a == error_mark_node)
6347 {
6348 /* Restore the ARGLIST to its full size. */
6349 TREE_VEC_LENGTH (arglist) = saved_depth;
6350 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6351 }
6352
6353 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6354
6355 /* We temporarily reduce the length of the ARGLIST so
6356 that coerce_template_parms will see only the arguments
6357 corresponding to the template parameters it is
6358 examining. */
6359 TREE_VEC_LENGTH (arglist)--;
6360 }
6361
6362 /* Restore the ARGLIST to its full size. */
6363 TREE_VEC_LENGTH (arglist) = saved_depth;
6364
6365 arglist = bound_args;
6366 }
6367 else
6368 arglist
6369 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6370 INNERMOST_TEMPLATE_ARGS (arglist),
6371 gen_tmpl,
6372 complain,
6373 /*require_all_args=*/true,
6374 /*use_default_args=*/true);
6375
6376 if (arglist == error_mark_node)
6377 /* We were unable to bind the arguments. */
6378 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6379
6380 /* In the scope of a template class, explicit references to the
6381 template class refer to the type of the template, not any
6382 instantiation of it. For example, in:
6383
6384 template <class T> class C { void f(C<T>); }
6385
6386 the `C<T>' is just the same as `C'. Outside of the
6387 class, however, such a reference is an instantiation. */
6388 if ((entering_scope
6389 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6390 || currently_open_class (template_type))
6391 /* comp_template_args is expensive, check it last. */
6392 && comp_template_args (TYPE_TI_ARGS (template_type),
6393 arglist))
6394 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
6395
6396 /* If we already have this specialization, return it. */
6397 elt.tmpl = gen_tmpl;
6398 elt.args = arglist;
6399 hash = hash_specialization (&elt);
6400 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6401 &elt, hash);
6402
6403 if (entry)
6404 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
6405
6406 /* This type is a "partial instantiation" if any of the template
6407 arguments still involve template parameters. Note that we set
6408 IS_PARTIAL_INSTANTIATION for partial specializations as
6409 well. */
6410 is_partial_instantiation = uses_template_parms (arglist);
6411
6412 /* If the deduced arguments are invalid, then the binding
6413 failed. */
6414 if (!is_partial_instantiation
6415 && check_instantiated_args (gen_tmpl,
6416 INNERMOST_TEMPLATE_ARGS (arglist),
6417 complain))
6418 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6419
6420 if (!is_partial_instantiation
6421 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6422 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6423 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6424 {
6425 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6426 DECL_NAME (gen_tmpl),
6427 /*tag_scope=*/ts_global);
6428 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
6429 }
6430
6431 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
6432 complain, in_decl);
6433 if (!context)
6434 context = global_namespace;
6435
6436 /* Create the type. */
6437 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6438 {
6439 if (!is_partial_instantiation)
6440 {
6441 set_current_access_from_decl (TYPE_NAME (template_type));
6442 t = start_enum (TYPE_IDENTIFIER (template_type),
6443 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6444 arglist, complain, in_decl),
6445 SCOPED_ENUM_P (template_type));
6446 }
6447 else
6448 {
6449 /* We don't want to call start_enum for this type, since
6450 the values for the enumeration constants may involve
6451 template parameters. And, no one should be interested
6452 in the enumeration constants for such a type. */
6453 t = cxx_make_type (ENUMERAL_TYPE);
6454 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
6455 }
6456 }
6457 else
6458 {
6459 t = make_class_type (TREE_CODE (template_type));
6460 CLASSTYPE_DECLARED_CLASS (t)
6461 = CLASSTYPE_DECLARED_CLASS (template_type);
6462 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
6463 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
6464
6465 /* A local class. Make sure the decl gets registered properly. */
6466 if (context == current_function_decl)
6467 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
6468
6469 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
6470 /* This instantiation is another name for the primary
6471 template type. Set the TYPE_CANONICAL field
6472 appropriately. */
6473 TYPE_CANONICAL (t) = template_type;
6474 else if (any_template_arguments_need_structural_equality_p (arglist))
6475 /* Some of the template arguments require structural
6476 equality testing, so this template class requires
6477 structural equality testing. */
6478 SET_TYPE_STRUCTURAL_EQUALITY (t);
6479 }
6480
6481 /* If we called start_enum or pushtag above, this information
6482 will already be set up. */
6483 if (!TYPE_NAME (t))
6484 {
6485 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
6486
6487 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
6488 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
6489 DECL_SOURCE_LOCATION (type_decl)
6490 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
6491 }
6492 else
6493 type_decl = TYPE_NAME (t);
6494
6495 TREE_PRIVATE (type_decl)
6496 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
6497 TREE_PROTECTED (type_decl)
6498 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
6499 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
6500 {
6501 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
6502 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
6503 }
6504
6505 /* Set up the template information. We have to figure out which
6506 template is the immediate parent if this is a full
6507 instantiation. */
6508 if (parm_depth == 1 || is_partial_instantiation
6509 || !PRIMARY_TEMPLATE_P (gen_tmpl))
6510 /* This case is easy; there are no member templates involved. */
6511 found = gen_tmpl;
6512 else
6513 {
6514 /* This is a full instantiation of a member template. Find
6515 the partial instantiation of which this is an instance. */
6516
6517 /* Temporarily reduce by one the number of levels in the ARGLIST
6518 so as to avoid comparing the last set of arguments. */
6519 TREE_VEC_LENGTH (arglist)--;
6520 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
6521 TREE_VEC_LENGTH (arglist)++;
6522 found = CLASSTYPE_TI_TEMPLATE (found);
6523 }
6524
6525 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
6526
6527 elt.spec = t;
6528 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
6529 &elt, hash, INSERT);
6530 *slot = GGC_NEW (spec_entry);
6531 **slot = elt;
6532
6533 /* Note this use of the partial instantiation so we can check it
6534 later in maybe_process_partial_specialization. */
6535 DECL_TEMPLATE_INSTANTIATIONS (templ)
6536 = tree_cons (arglist, t,
6537 DECL_TEMPLATE_INSTANTIATIONS (templ));
6538
6539 if (TREE_CODE (t) == ENUMERAL_TYPE
6540 && !is_partial_instantiation)
6541 /* Now that the type has been registered on the instantiations
6542 list, we set up the enumerators. Because the enumeration
6543 constants may involve the enumeration type itself, we make
6544 sure to register the type first, and then create the
6545 constants. That way, doing tsubst_expr for the enumeration
6546 constants won't result in recursive calls here; we'll find
6547 the instantiation and exit above. */
6548 tsubst_enum (template_type, t, arglist);
6549
6550 if (is_partial_instantiation)
6551 /* If the type makes use of template parameters, the
6552 code that generates debugging information will crash. */
6553 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6554
6555 /* Possibly limit visibility based on template args. */
6556 TREE_PUBLIC (type_decl) = 1;
6557 determine_visibility (type_decl);
6558
6559 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6560 }
6561 timevar_pop (TV_NAME_LOOKUP);
6562 }
6563 \f
6564 struct pair_fn_data
6565 {
6566 tree_fn_t fn;
6567 void *data;
6568 /* True when we should also visit template parameters that occur in
6569 non-deduced contexts. */
6570 bool include_nondeduced_p;
6571 struct pointer_set_t *visited;
6572 };
6573
6574 /* Called from for_each_template_parm via walk_tree. */
6575
6576 static tree
6577 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6578 {
6579 tree t = *tp;
6580 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6581 tree_fn_t fn = pfd->fn;
6582 void *data = pfd->data;
6583
6584 if (TYPE_P (t)
6585 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6586 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6587 pfd->include_nondeduced_p))
6588 return error_mark_node;
6589
6590 switch (TREE_CODE (t))
6591 {
6592 case RECORD_TYPE:
6593 if (TYPE_PTRMEMFUNC_P (t))
6594 break;
6595 /* Fall through. */
6596
6597 case UNION_TYPE:
6598 case ENUMERAL_TYPE:
6599 if (!TYPE_TEMPLATE_INFO (t))
6600 *walk_subtrees = 0;
6601 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
6602 fn, data, pfd->visited,
6603 pfd->include_nondeduced_p))
6604 return error_mark_node;
6605 break;
6606
6607 case INTEGER_TYPE:
6608 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6609 fn, data, pfd->visited,
6610 pfd->include_nondeduced_p)
6611 || for_each_template_parm (TYPE_MAX_VALUE (t),
6612 fn, data, pfd->visited,
6613 pfd->include_nondeduced_p))
6614 return error_mark_node;
6615 break;
6616
6617 case METHOD_TYPE:
6618 /* Since we're not going to walk subtrees, we have to do this
6619 explicitly here. */
6620 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6621 pfd->visited, pfd->include_nondeduced_p))
6622 return error_mark_node;
6623 /* Fall through. */
6624
6625 case FUNCTION_TYPE:
6626 /* Check the return type. */
6627 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6628 pfd->include_nondeduced_p))
6629 return error_mark_node;
6630
6631 /* Check the parameter types. Since default arguments are not
6632 instantiated until they are needed, the TYPE_ARG_TYPES may
6633 contain expressions that involve template parameters. But,
6634 no-one should be looking at them yet. And, once they're
6635 instantiated, they don't contain template parameters, so
6636 there's no point in looking at them then, either. */
6637 {
6638 tree parm;
6639
6640 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6641 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6642 pfd->visited, pfd->include_nondeduced_p))
6643 return error_mark_node;
6644
6645 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6646 want walk_tree walking into them itself. */
6647 *walk_subtrees = 0;
6648 }
6649 break;
6650
6651 case TYPEOF_TYPE:
6652 if (pfd->include_nondeduced_p
6653 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6654 pfd->visited,
6655 pfd->include_nondeduced_p))
6656 return error_mark_node;
6657 break;
6658
6659 case FUNCTION_DECL:
6660 case VAR_DECL:
6661 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6662 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6663 pfd->visited, pfd->include_nondeduced_p))
6664 return error_mark_node;
6665 /* Fall through. */
6666
6667 case PARM_DECL:
6668 case CONST_DECL:
6669 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6670 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6671 pfd->visited, pfd->include_nondeduced_p))
6672 return error_mark_node;
6673 if (DECL_CONTEXT (t)
6674 && pfd->include_nondeduced_p
6675 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6676 pfd->visited, pfd->include_nondeduced_p))
6677 return error_mark_node;
6678 break;
6679
6680 case BOUND_TEMPLATE_TEMPLATE_PARM:
6681 /* Record template parameters such as `T' inside `TT<T>'. */
6682 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6683 pfd->include_nondeduced_p))
6684 return error_mark_node;
6685 /* Fall through. */
6686
6687 case TEMPLATE_TEMPLATE_PARM:
6688 case TEMPLATE_TYPE_PARM:
6689 case TEMPLATE_PARM_INDEX:
6690 if (fn && (*fn)(t, data))
6691 return error_mark_node;
6692 else if (!fn)
6693 return error_mark_node;
6694 break;
6695
6696 case TEMPLATE_DECL:
6697 /* A template template parameter is encountered. */
6698 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6699 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6700 pfd->include_nondeduced_p))
6701 return error_mark_node;
6702
6703 /* Already substituted template template parameter */
6704 *walk_subtrees = 0;
6705 break;
6706
6707 case TYPENAME_TYPE:
6708 if (!fn
6709 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6710 data, pfd->visited,
6711 pfd->include_nondeduced_p))
6712 return error_mark_node;
6713 break;
6714
6715 case CONSTRUCTOR:
6716 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
6717 && pfd->include_nondeduced_p
6718 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6719 (TREE_TYPE (t)), fn, data,
6720 pfd->visited, pfd->include_nondeduced_p))
6721 return error_mark_node;
6722 break;
6723
6724 case INDIRECT_REF:
6725 case COMPONENT_REF:
6726 /* If there's no type, then this thing must be some expression
6727 involving template parameters. */
6728 if (!fn && !TREE_TYPE (t))
6729 return error_mark_node;
6730 break;
6731
6732 case MODOP_EXPR:
6733 case CAST_EXPR:
6734 case REINTERPRET_CAST_EXPR:
6735 case CONST_CAST_EXPR:
6736 case STATIC_CAST_EXPR:
6737 case DYNAMIC_CAST_EXPR:
6738 case ARROW_EXPR:
6739 case DOTSTAR_EXPR:
6740 case TYPEID_EXPR:
6741 case PSEUDO_DTOR_EXPR:
6742 if (!fn)
6743 return error_mark_node;
6744 break;
6745
6746 default:
6747 break;
6748 }
6749
6750 /* We didn't find any template parameters we liked. */
6751 return NULL_TREE;
6752 }
6753
6754 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6755 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6756 call FN with the parameter and the DATA.
6757 If FN returns nonzero, the iteration is terminated, and
6758 for_each_template_parm returns 1. Otherwise, the iteration
6759 continues. If FN never returns a nonzero value, the value
6760 returned by for_each_template_parm is 0. If FN is NULL, it is
6761 considered to be the function which always returns 1.
6762
6763 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6764 parameters that occur in non-deduced contexts. When false, only
6765 visits those template parameters that can be deduced. */
6766
6767 static int
6768 for_each_template_parm (tree t, tree_fn_t fn, void* data,
6769 struct pointer_set_t *visited,
6770 bool include_nondeduced_p)
6771 {
6772 struct pair_fn_data pfd;
6773 int result;
6774
6775 /* Set up. */
6776 pfd.fn = fn;
6777 pfd.data = data;
6778 pfd.include_nondeduced_p = include_nondeduced_p;
6779
6780 /* Walk the tree. (Conceptually, we would like to walk without
6781 duplicates, but for_each_template_parm_r recursively calls
6782 for_each_template_parm, so we would need to reorganize a fair
6783 bit to use walk_tree_without_duplicates, so we keep our own
6784 visited list.) */
6785 if (visited)
6786 pfd.visited = visited;
6787 else
6788 pfd.visited = pointer_set_create ();
6789 result = cp_walk_tree (&t,
6790 for_each_template_parm_r,
6791 &pfd,
6792 pfd.visited) != NULL_TREE;
6793
6794 /* Clean up. */
6795 if (!visited)
6796 {
6797 pointer_set_destroy (pfd.visited);
6798 pfd.visited = 0;
6799 }
6800
6801 return result;
6802 }
6803
6804 /* Returns true if T depends on any template parameter. */
6805
6806 int
6807 uses_template_parms (tree t)
6808 {
6809 bool dependent_p;
6810 int saved_processing_template_decl;
6811
6812 saved_processing_template_decl = processing_template_decl;
6813 if (!saved_processing_template_decl)
6814 processing_template_decl = 1;
6815 if (TYPE_P (t))
6816 dependent_p = dependent_type_p (t);
6817 else if (TREE_CODE (t) == TREE_VEC)
6818 dependent_p = any_dependent_template_arguments_p (t);
6819 else if (TREE_CODE (t) == TREE_LIST)
6820 dependent_p = (uses_template_parms (TREE_VALUE (t))
6821 || uses_template_parms (TREE_CHAIN (t)));
6822 else if (TREE_CODE (t) == TYPE_DECL)
6823 dependent_p = dependent_type_p (TREE_TYPE (t));
6824 else if (DECL_P (t)
6825 || EXPR_P (t)
6826 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
6827 || TREE_CODE (t) == OVERLOAD
6828 || TREE_CODE (t) == BASELINK
6829 || TREE_CODE (t) == IDENTIFIER_NODE
6830 || TREE_CODE (t) == TRAIT_EXPR
6831 || TREE_CODE (t) == CONSTRUCTOR
6832 || CONSTANT_CLASS_P (t))
6833 dependent_p = (type_dependent_expression_p (t)
6834 || value_dependent_expression_p (t));
6835 else
6836 {
6837 gcc_assert (t == error_mark_node);
6838 dependent_p = false;
6839 }
6840
6841 processing_template_decl = saved_processing_template_decl;
6842
6843 return dependent_p;
6844 }
6845
6846 /* Returns true if T depends on any template parameter with level LEVEL. */
6847
6848 int
6849 uses_template_parms_level (tree t, int level)
6850 {
6851 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
6852 /*include_nondeduced_p=*/true);
6853 }
6854
6855 static int tinst_depth;
6856 extern int max_tinst_depth;
6857 #ifdef GATHER_STATISTICS
6858 int depth_reached;
6859 #endif
6860 static int tinst_level_tick;
6861 static int last_template_error_tick;
6862
6863 /* We're starting to instantiate D; record the template instantiation context
6864 for diagnostics and to restore it later. */
6865
6866 static int
6867 push_tinst_level (tree d)
6868 {
6869 struct tinst_level *new_level;
6870
6871 if (tinst_depth >= max_tinst_depth)
6872 {
6873 /* If the instantiation in question still has unbound template parms,
6874 we don't really care if we can't instantiate it, so just return.
6875 This happens with base instantiation for implicit `typename'. */
6876 if (uses_template_parms (d))
6877 return 0;
6878
6879 last_template_error_tick = tinst_level_tick;
6880 error ("template instantiation depth exceeds maximum of %d (use "
6881 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6882 max_tinst_depth, d);
6883
6884 print_instantiation_context ();
6885
6886 return 0;
6887 }
6888
6889 new_level = GGC_NEW (struct tinst_level);
6890 new_level->decl = d;
6891 new_level->locus = input_location;
6892 new_level->in_system_header_p = in_system_header;
6893 new_level->next = current_tinst_level;
6894 current_tinst_level = new_level;
6895
6896 ++tinst_depth;
6897 #ifdef GATHER_STATISTICS
6898 if (tinst_depth > depth_reached)
6899 depth_reached = tinst_depth;
6900 #endif
6901
6902 ++tinst_level_tick;
6903 return 1;
6904 }
6905
6906 /* We're done instantiating this template; return to the instantiation
6907 context. */
6908
6909 static void
6910 pop_tinst_level (void)
6911 {
6912 /* Restore the filename and line number stashed away when we started
6913 this instantiation. */
6914 input_location = current_tinst_level->locus;
6915 current_tinst_level = current_tinst_level->next;
6916 --tinst_depth;
6917 ++tinst_level_tick;
6918 }
6919
6920 /* We're instantiating a deferred template; restore the template
6921 instantiation context in which the instantiation was requested, which
6922 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6923
6924 static tree
6925 reopen_tinst_level (struct tinst_level *level)
6926 {
6927 struct tinst_level *t;
6928
6929 tinst_depth = 0;
6930 for (t = level; t; t = t->next)
6931 ++tinst_depth;
6932
6933 current_tinst_level = level;
6934 pop_tinst_level ();
6935 return level->decl;
6936 }
6937
6938 /* Returns the TINST_LEVEL which gives the original instantiation
6939 context. */
6940
6941 struct tinst_level *
6942 outermost_tinst_level (void)
6943 {
6944 struct tinst_level *level = current_tinst_level;
6945 if (level)
6946 while (level->next)
6947 level = level->next;
6948 return level;
6949 }
6950
6951 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
6952
6953 bool
6954 parameter_of_template_p (tree parm, tree templ)
6955 {
6956 tree parms;
6957 int i;
6958
6959 if (!parm || !templ)
6960 return false;
6961
6962 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
6963 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
6964
6965 parms = DECL_TEMPLATE_PARMS (templ);
6966 parms = INNERMOST_TEMPLATE_PARMS (parms);
6967
6968 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
6969 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
6970 return true;
6971
6972 return false;
6973 }
6974
6975 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6976 vector of template arguments, as for tsubst.
6977
6978 Returns an appropriate tsubst'd friend declaration. */
6979
6980 static tree
6981 tsubst_friend_function (tree decl, tree args)
6982 {
6983 tree new_friend;
6984
6985 if (TREE_CODE (decl) == FUNCTION_DECL
6986 && DECL_TEMPLATE_INSTANTIATION (decl)
6987 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
6988 /* This was a friend declared with an explicit template
6989 argument list, e.g.:
6990
6991 friend void f<>(T);
6992
6993 to indicate that f was a template instantiation, not a new
6994 function declaration. Now, we have to figure out what
6995 instantiation of what template. */
6996 {
6997 tree template_id, arglist, fns;
6998 tree new_args;
6999 tree tmpl;
7000 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7001
7002 /* Friend functions are looked up in the containing namespace scope.
7003 We must enter that scope, to avoid finding member functions of the
7004 current class with same name. */
7005 push_nested_namespace (ns);
7006 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7007 tf_warning_or_error, NULL_TREE,
7008 /*integral_constant_expression_p=*/false);
7009 pop_nested_namespace (ns);
7010 arglist = tsubst (DECL_TI_ARGS (decl), args,
7011 tf_warning_or_error, NULL_TREE);
7012 template_id = lookup_template_function (fns, arglist);
7013
7014 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7015 tmpl = determine_specialization (template_id, new_friend,
7016 &new_args,
7017 /*need_member_template=*/0,
7018 TREE_VEC_LENGTH (args),
7019 tsk_none);
7020 return instantiate_template (tmpl, new_args, tf_error);
7021 }
7022
7023 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7024
7025 /* The NEW_FRIEND will look like an instantiation, to the
7026 compiler, but is not an instantiation from the point of view of
7027 the language. For example, we might have had:
7028
7029 template <class T> struct S {
7030 template <class U> friend void f(T, U);
7031 };
7032
7033 Then, in S<int>, template <class U> void f(int, U) is not an
7034 instantiation of anything. */
7035 if (new_friend == error_mark_node)
7036 return error_mark_node;
7037
7038 DECL_USE_TEMPLATE (new_friend) = 0;
7039 if (TREE_CODE (decl) == TEMPLATE_DECL)
7040 {
7041 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7042 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7043 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7044 }
7045
7046 /* The mangled name for the NEW_FRIEND is incorrect. The function
7047 is not a template instantiation and should not be mangled like
7048 one. Therefore, we forget the mangling here; we'll recompute it
7049 later if we need it. */
7050 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7051 {
7052 SET_DECL_RTL (new_friend, NULL_RTX);
7053 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7054 }
7055
7056 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7057 {
7058 tree old_decl;
7059 tree new_friend_template_info;
7060 tree new_friend_result_template_info;
7061 tree ns;
7062 int new_friend_is_defn;
7063
7064 /* We must save some information from NEW_FRIEND before calling
7065 duplicate decls since that function will free NEW_FRIEND if
7066 possible. */
7067 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7068 new_friend_is_defn =
7069 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7070 (template_for_substitution (new_friend)))
7071 != NULL_TREE);
7072 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7073 {
7074 /* This declaration is a `primary' template. */
7075 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7076
7077 new_friend_result_template_info
7078 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7079 }
7080 else
7081 new_friend_result_template_info = NULL_TREE;
7082
7083 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7084 if (new_friend_is_defn)
7085 DECL_INITIAL (new_friend) = error_mark_node;
7086
7087 /* Inside pushdecl_namespace_level, we will push into the
7088 current namespace. However, the friend function should go
7089 into the namespace of the template. */
7090 ns = decl_namespace_context (new_friend);
7091 push_nested_namespace (ns);
7092 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
7093 pop_nested_namespace (ns);
7094
7095 if (old_decl == error_mark_node)
7096 return error_mark_node;
7097
7098 if (old_decl != new_friend)
7099 {
7100 /* This new friend declaration matched an existing
7101 declaration. For example, given:
7102
7103 template <class T> void f(T);
7104 template <class U> class C {
7105 template <class T> friend void f(T) {}
7106 };
7107
7108 the friend declaration actually provides the definition
7109 of `f', once C has been instantiated for some type. So,
7110 old_decl will be the out-of-class template declaration,
7111 while new_friend is the in-class definition.
7112
7113 But, if `f' was called before this point, the
7114 instantiation of `f' will have DECL_TI_ARGS corresponding
7115 to `T' but not to `U', references to which might appear
7116 in the definition of `f'. Previously, the most general
7117 template for an instantiation of `f' was the out-of-class
7118 version; now it is the in-class version. Therefore, we
7119 run through all specialization of `f', adding to their
7120 DECL_TI_ARGS appropriately. In particular, they need a
7121 new set of outer arguments, corresponding to the
7122 arguments for this class instantiation.
7123
7124 The same situation can arise with something like this:
7125
7126 friend void f(int);
7127 template <class T> class C {
7128 friend void f(T) {}
7129 };
7130
7131 when `C<int>' is instantiated. Now, `f(int)' is defined
7132 in the class. */
7133
7134 if (!new_friend_is_defn)
7135 /* On the other hand, if the in-class declaration does
7136 *not* provide a definition, then we don't want to alter
7137 existing definitions. We can just leave everything
7138 alone. */
7139 ;
7140 else
7141 {
7142 tree new_template = TI_TEMPLATE (new_friend_template_info);
7143 tree new_args = TI_ARGS (new_friend_template_info);
7144
7145 /* Overwrite whatever template info was there before, if
7146 any, with the new template information pertaining to
7147 the declaration. */
7148 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
7149
7150 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
7151 /* We should have called reregister_specialization in
7152 duplicate_decls. */
7153 gcc_assert (retrieve_specialization (new_template,
7154 new_args, 0)
7155 == old_decl);
7156 else
7157 {
7158 tree t;
7159
7160 /* Indicate that the old function template is a partial
7161 instantiation. */
7162 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
7163 = new_friend_result_template_info;
7164
7165 gcc_assert (new_template
7166 == most_general_template (new_template));
7167 gcc_assert (new_template != old_decl);
7168
7169 /* Reassign any specializations already in the hash table
7170 to the new more general template, and add the
7171 additional template args. */
7172 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
7173 t != NULL_TREE;
7174 t = TREE_CHAIN (t))
7175 {
7176 tree spec = TREE_VALUE (t);
7177 spec_entry elt;
7178
7179 elt.tmpl = old_decl;
7180 elt.args = DECL_TI_ARGS (spec);
7181 elt.spec = NULL_TREE;
7182
7183 htab_remove_elt (decl_specializations, &elt);
7184
7185 DECL_TI_ARGS (spec)
7186 = add_outermost_template_args (new_args,
7187 DECL_TI_ARGS (spec));
7188
7189 register_specialization
7190 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7191
7192 }
7193 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7194 }
7195 }
7196
7197 /* The information from NEW_FRIEND has been merged into OLD_DECL
7198 by duplicate_decls. */
7199 new_friend = old_decl;
7200 }
7201 }
7202 else
7203 {
7204 tree context = DECL_CONTEXT (new_friend);
7205 bool dependent_p;
7206
7207 /* In the code
7208 template <class T> class C {
7209 template <class U> friend void C1<U>::f (); // case 1
7210 friend void C2<T>::f (); // case 2
7211 };
7212 we only need to make sure CONTEXT is a complete type for
7213 case 2. To distinguish between the two cases, we note that
7214 CONTEXT of case 1 remains dependent type after tsubst while
7215 this isn't true for case 2. */
7216 ++processing_template_decl;
7217 dependent_p = dependent_type_p (context);
7218 --processing_template_decl;
7219
7220 if (!dependent_p
7221 && !complete_type_or_else (context, NULL_TREE))
7222 return error_mark_node;
7223
7224 if (COMPLETE_TYPE_P (context))
7225 {
7226 /* Check to see that the declaration is really present, and,
7227 possibly obtain an improved declaration. */
7228 tree fn = check_classfn (context,
7229 new_friend, NULL_TREE);
7230
7231 if (fn)
7232 new_friend = fn;
7233 }
7234 }
7235
7236 return new_friend;
7237 }
7238
7239 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7240 template arguments, as for tsubst.
7241
7242 Returns an appropriate tsubst'd friend type or error_mark_node on
7243 failure. */
7244
7245 static tree
7246 tsubst_friend_class (tree friend_tmpl, tree args)
7247 {
7248 tree friend_type;
7249 tree tmpl;
7250 tree context;
7251
7252 context = DECL_CONTEXT (friend_tmpl);
7253
7254 if (context)
7255 {
7256 if (TREE_CODE (context) == NAMESPACE_DECL)
7257 push_nested_namespace (context);
7258 else
7259 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7260 }
7261
7262 /* Look for a class template declaration. We look for hidden names
7263 because two friend declarations of the same template are the
7264 same. For example, in:
7265
7266 struct A {
7267 template <typename> friend class F;
7268 };
7269 template <typename> struct B {
7270 template <typename> friend class F;
7271 };
7272
7273 both F templates are the same. */
7274 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7275 /*block_p=*/true, 0,
7276 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7277
7278 /* But, if we don't find one, it might be because we're in a
7279 situation like this:
7280
7281 template <class T>
7282 struct S {
7283 template <class U>
7284 friend struct S;
7285 };
7286
7287 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7288 for `S<int>', not the TEMPLATE_DECL. */
7289 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7290 {
7291 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7292 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7293 }
7294
7295 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7296 {
7297 /* The friend template has already been declared. Just
7298 check to see that the declarations match, and install any new
7299 default parameters. We must tsubst the default parameters,
7300 of course. We only need the innermost template parameters
7301 because that is all that redeclare_class_template will look
7302 at. */
7303 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7304 > TMPL_ARGS_DEPTH (args))
7305 {
7306 tree parms;
7307 location_t saved_input_location;
7308 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7309 args, tf_warning_or_error);
7310
7311 saved_input_location = input_location;
7312 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7313 redeclare_class_template (TREE_TYPE (tmpl), parms);
7314 input_location = saved_input_location;
7315
7316 }
7317
7318 friend_type = TREE_TYPE (tmpl);
7319 }
7320 else
7321 {
7322 /* The friend template has not already been declared. In this
7323 case, the instantiation of the template class will cause the
7324 injection of this template into the global scope. */
7325 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7326 if (tmpl == error_mark_node)
7327 return error_mark_node;
7328
7329 /* The new TMPL is not an instantiation of anything, so we
7330 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7331 the new type because that is supposed to be the corresponding
7332 template decl, i.e., TMPL. */
7333 DECL_USE_TEMPLATE (tmpl) = 0;
7334 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7335 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7336 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7337 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7338
7339 /* Inject this template into the global scope. */
7340 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7341 }
7342
7343 if (context)
7344 {
7345 if (TREE_CODE (context) == NAMESPACE_DECL)
7346 pop_nested_namespace (context);
7347 else
7348 pop_nested_class ();
7349 }
7350
7351 return friend_type;
7352 }
7353
7354 /* Returns zero if TYPE cannot be completed later due to circularity.
7355 Otherwise returns one. */
7356
7357 static int
7358 can_complete_type_without_circularity (tree type)
7359 {
7360 if (type == NULL_TREE || type == error_mark_node)
7361 return 0;
7362 else if (COMPLETE_TYPE_P (type))
7363 return 1;
7364 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7365 return can_complete_type_without_circularity (TREE_TYPE (type));
7366 else if (CLASS_TYPE_P (type)
7367 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
7368 return 0;
7369 else
7370 return 1;
7371 }
7372
7373 /* Apply any attributes which had to be deferred until instantiation
7374 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7375 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7376
7377 static void
7378 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7379 tree args, tsubst_flags_t complain, tree in_decl)
7380 {
7381 tree last_dep = NULL_TREE;
7382 tree t;
7383 tree *p;
7384
7385 for (t = attributes; t; t = TREE_CHAIN (t))
7386 if (ATTR_IS_DEPENDENT (t))
7387 {
7388 last_dep = t;
7389 attributes = copy_list (attributes);
7390 break;
7391 }
7392
7393 if (DECL_P (*decl_p))
7394 {
7395 if (TREE_TYPE (*decl_p) == error_mark_node)
7396 return;
7397 p = &DECL_ATTRIBUTES (*decl_p);
7398 }
7399 else
7400 p = &TYPE_ATTRIBUTES (*decl_p);
7401
7402 if (last_dep)
7403 {
7404 tree late_attrs = NULL_TREE;
7405 tree *q = &late_attrs;
7406
7407 for (*p = attributes; *p; )
7408 {
7409 t = *p;
7410 if (ATTR_IS_DEPENDENT (t))
7411 {
7412 *p = TREE_CHAIN (t);
7413 TREE_CHAIN (t) = NULL_TREE;
7414 /* If the first attribute argument is an identifier, don't
7415 pass it through tsubst. Attributes like mode, format,
7416 cleanup and several target specific attributes expect it
7417 unmodified. */
7418 if (TREE_VALUE (t)
7419 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
7420 && TREE_VALUE (TREE_VALUE (t))
7421 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
7422 == IDENTIFIER_NODE))
7423 {
7424 tree chain
7425 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
7426 in_decl,
7427 /*integral_constant_expression_p=*/false);
7428 if (chain != TREE_CHAIN (TREE_VALUE (t)))
7429 TREE_VALUE (t)
7430 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
7431 chain);
7432 }
7433 else
7434 TREE_VALUE (t)
7435 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
7436 /*integral_constant_expression_p=*/false);
7437 *q = t;
7438 q = &TREE_CHAIN (t);
7439 }
7440 else
7441 p = &TREE_CHAIN (t);
7442 }
7443
7444 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
7445 }
7446 }
7447
7448 /* Perform (or defer) access check for typedefs that were referenced
7449 from within the template TMPL code.
7450 This is a subroutine of instantiate_template and instantiate_class_template.
7451 TMPL is the template to consider and TARGS is the list of arguments of
7452 that template. */
7453
7454 static void
7455 perform_typedefs_access_check (tree tmpl, tree targs)
7456 {
7457 location_t saved_location;
7458 int i;
7459 qualified_typedef_usage_t *iter;
7460
7461 if (!tmpl
7462 || (!CLASS_TYPE_P (tmpl)
7463 && TREE_CODE (tmpl) != FUNCTION_DECL))
7464 return;
7465
7466 saved_location = input_location;
7467 for (i = 0;
7468 VEC_iterate (qualified_typedef_usage_t,
7469 get_types_needing_access_check (tmpl),
7470 i, iter);
7471 ++i)
7472 {
7473 tree type_decl = iter->typedef_decl;
7474 tree type_scope = iter->context;
7475
7476 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
7477 continue;
7478
7479 if (uses_template_parms (type_decl))
7480 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
7481 if (uses_template_parms (type_scope))
7482 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
7483
7484 /* Make access check error messages point to the location
7485 of the use of the typedef. */
7486 input_location = iter->locus;
7487 perform_or_defer_access_check (TYPE_BINFO (type_scope),
7488 type_decl, type_decl);
7489 }
7490 input_location = saved_location;
7491 }
7492
7493 tree
7494 instantiate_class_template (tree type)
7495 {
7496 tree templ, args, pattern, t, member;
7497 tree typedecl;
7498 tree pbinfo;
7499 tree base_list;
7500 unsigned int saved_maximum_field_alignment;
7501
7502 if (type == error_mark_node)
7503 return error_mark_node;
7504
7505 if (TYPE_BEING_DEFINED (type)
7506 || COMPLETE_TYPE_P (type)
7507 || uses_template_parms (type))
7508 return type;
7509
7510 /* Figure out which template is being instantiated. */
7511 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
7512 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7513
7514 /* Determine what specialization of the original template to
7515 instantiate. */
7516 t = most_specialized_class (type, templ);
7517 if (t == error_mark_node)
7518 {
7519 TYPE_BEING_DEFINED (type) = 1;
7520 return error_mark_node;
7521 }
7522 else if (t)
7523 {
7524 /* This TYPE is actually an instantiation of a partial
7525 specialization. We replace the innermost set of ARGS with
7526 the arguments appropriate for substitution. For example,
7527 given:
7528
7529 template <class T> struct S {};
7530 template <class T> struct S<T*> {};
7531
7532 and supposing that we are instantiating S<int*>, ARGS will
7533 presently be {int*} -- but we need {int}. */
7534 pattern = TREE_TYPE (t);
7535 args = TREE_PURPOSE (t);
7536 }
7537 else
7538 {
7539 pattern = TREE_TYPE (templ);
7540 args = CLASSTYPE_TI_ARGS (type);
7541 }
7542
7543 /* If the template we're instantiating is incomplete, then clearly
7544 there's nothing we can do. */
7545 if (!COMPLETE_TYPE_P (pattern))
7546 return type;
7547
7548 /* If we've recursively instantiated too many templates, stop. */
7549 if (! push_tinst_level (type))
7550 return type;
7551
7552 /* Now we're really doing the instantiation. Mark the type as in
7553 the process of being defined. */
7554 TYPE_BEING_DEFINED (type) = 1;
7555
7556 /* We may be in the middle of deferred access check. Disable
7557 it now. */
7558 push_deferring_access_checks (dk_no_deferred);
7559
7560 push_to_top_level ();
7561 /* Use #pragma pack from the template context. */
7562 saved_maximum_field_alignment = maximum_field_alignment;
7563 maximum_field_alignment = TYPE_PRECISION (pattern);
7564
7565 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
7566
7567 /* Set the input location to the most specialized template definition.
7568 This is needed if tsubsting causes an error. */
7569 typedecl = TYPE_MAIN_DECL (pattern);
7570 input_location = DECL_SOURCE_LOCATION (typedecl);
7571
7572 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7573 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7574 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7575 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7576 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
7577 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
7578 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
7579 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
7580 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7581 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7582 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7583 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7584 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7585 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7586 if (ANON_AGGR_TYPE_P (pattern))
7587 SET_ANON_AGGR_TYPE_P (type);
7588 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7589 {
7590 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7591 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7592 }
7593
7594 pbinfo = TYPE_BINFO (pattern);
7595
7596 /* We should never instantiate a nested class before its enclosing
7597 class; we need to look up the nested class by name before we can
7598 instantiate it, and that lookup should instantiate the enclosing
7599 class. */
7600 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7601 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
7602 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
7603
7604 base_list = NULL_TREE;
7605 if (BINFO_N_BASE_BINFOS (pbinfo))
7606 {
7607 tree pbase_binfo;
7608 tree context = TYPE_CONTEXT (type);
7609 tree pushed_scope;
7610 int i;
7611
7612 /* We must enter the scope containing the type, as that is where
7613 the accessibility of types named in dependent bases are
7614 looked up from. */
7615 pushed_scope = push_scope (context ? context : global_namespace);
7616
7617 /* Substitute into each of the bases to determine the actual
7618 basetypes. */
7619 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7620 {
7621 tree base;
7622 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7623 tree expanded_bases = NULL_TREE;
7624 int idx, len = 1;
7625
7626 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7627 {
7628 expanded_bases =
7629 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7630 args, tf_error, NULL_TREE);
7631 if (expanded_bases == error_mark_node)
7632 continue;
7633
7634 len = TREE_VEC_LENGTH (expanded_bases);
7635 }
7636
7637 for (idx = 0; idx < len; idx++)
7638 {
7639 if (expanded_bases)
7640 /* Extract the already-expanded base class. */
7641 base = TREE_VEC_ELT (expanded_bases, idx);
7642 else
7643 /* Substitute to figure out the base class. */
7644 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7645 NULL_TREE);
7646
7647 if (base == error_mark_node)
7648 continue;
7649
7650 base_list = tree_cons (access, base, base_list);
7651 if (BINFO_VIRTUAL_P (pbase_binfo))
7652 TREE_TYPE (base_list) = integer_type_node;
7653 }
7654 }
7655
7656 /* The list is now in reverse order; correct that. */
7657 base_list = nreverse (base_list);
7658
7659 if (pushed_scope)
7660 pop_scope (pushed_scope);
7661 }
7662 /* Now call xref_basetypes to set up all the base-class
7663 information. */
7664 xref_basetypes (type, base_list);
7665
7666 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7667 (int) ATTR_FLAG_TYPE_IN_PLACE,
7668 args, tf_error, NULL_TREE);
7669
7670 /* Now that our base classes are set up, enter the scope of the
7671 class, so that name lookups into base classes, etc. will work
7672 correctly. This is precisely analogous to what we do in
7673 begin_class_definition when defining an ordinary non-template
7674 class, except we also need to push the enclosing classes. */
7675 push_nested_class (type);
7676
7677 /* Now members are processed in the order of declaration. */
7678 for (member = CLASSTYPE_DECL_LIST (pattern);
7679 member; member = TREE_CHAIN (member))
7680 {
7681 tree t = TREE_VALUE (member);
7682
7683 if (TREE_PURPOSE (member))
7684 {
7685 if (TYPE_P (t))
7686 {
7687 /* Build new CLASSTYPE_NESTED_UTDS. */
7688
7689 tree newtag;
7690 bool class_template_p;
7691
7692 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7693 && TYPE_LANG_SPECIFIC (t)
7694 && CLASSTYPE_IS_TEMPLATE (t));
7695 /* If the member is a class template, then -- even after
7696 substitution -- there may be dependent types in the
7697 template argument list for the class. We increment
7698 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7699 that function will assume that no types are dependent
7700 when outside of a template. */
7701 if (class_template_p)
7702 ++processing_template_decl;
7703 newtag = tsubst (t, args, tf_error, NULL_TREE);
7704 if (class_template_p)
7705 --processing_template_decl;
7706 if (newtag == error_mark_node)
7707 continue;
7708
7709 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7710 {
7711 tree name = TYPE_IDENTIFIER (t);
7712
7713 if (class_template_p)
7714 /* Unfortunately, lookup_template_class sets
7715 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7716 instantiation (i.e., for the type of a member
7717 template class nested within a template class.)
7718 This behavior is required for
7719 maybe_process_partial_specialization to work
7720 correctly, but is not accurate in this case;
7721 the TAG is not an instantiation of anything.
7722 (The corresponding TEMPLATE_DECL is an
7723 instantiation, but the TYPE is not.) */
7724 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
7725
7726 /* Now, we call pushtag to put this NEWTAG into the scope of
7727 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7728 pushtag calling push_template_decl. We don't have to do
7729 this for enums because it will already have been done in
7730 tsubst_enum. */
7731 if (name)
7732 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
7733 pushtag (name, newtag, /*tag_scope=*/ts_current);
7734 }
7735 }
7736 else if (TREE_CODE (t) == FUNCTION_DECL
7737 || DECL_FUNCTION_TEMPLATE_P (t))
7738 {
7739 /* Build new TYPE_METHODS. */
7740 tree r;
7741
7742 if (TREE_CODE (t) == TEMPLATE_DECL)
7743 ++processing_template_decl;
7744 r = tsubst (t, args, tf_error, NULL_TREE);
7745 if (TREE_CODE (t) == TEMPLATE_DECL)
7746 --processing_template_decl;
7747 set_current_access_from_decl (r);
7748 finish_member_declaration (r);
7749 }
7750 else
7751 {
7752 /* Build new TYPE_FIELDS. */
7753 if (TREE_CODE (t) == STATIC_ASSERT)
7754 {
7755 tree condition =
7756 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
7757 tf_warning_or_error, NULL_TREE,
7758 /*integral_constant_expression_p=*/true);
7759 finish_static_assert (condition,
7760 STATIC_ASSERT_MESSAGE (t),
7761 STATIC_ASSERT_SOURCE_LOCATION (t),
7762 /*member_p=*/true);
7763 }
7764 else if (TREE_CODE (t) != CONST_DECL)
7765 {
7766 tree r;
7767
7768 /* The file and line for this declaration, to
7769 assist in error message reporting. Since we
7770 called push_tinst_level above, we don't need to
7771 restore these. */
7772 input_location = DECL_SOURCE_LOCATION (t);
7773
7774 if (TREE_CODE (t) == TEMPLATE_DECL)
7775 ++processing_template_decl;
7776 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
7777 if (TREE_CODE (t) == TEMPLATE_DECL)
7778 --processing_template_decl;
7779 if (TREE_CODE (r) == VAR_DECL)
7780 {
7781 /* In [temp.inst]:
7782
7783 [t]he initialization (and any associated
7784 side-effects) of a static data member does
7785 not occur unless the static data member is
7786 itself used in a way that requires the
7787 definition of the static data member to
7788 exist.
7789
7790 Therefore, we do not substitute into the
7791 initialized for the static data member here. */
7792 finish_static_data_member_decl
7793 (r,
7794 /*init=*/NULL_TREE,
7795 /*init_const_expr_p=*/false,
7796 /*asmspec_tree=*/NULL_TREE,
7797 /*flags=*/0);
7798 if (DECL_INITIALIZED_IN_CLASS_P (r))
7799 check_static_variable_definition (r, TREE_TYPE (r));
7800 }
7801 else if (TREE_CODE (r) == FIELD_DECL)
7802 {
7803 /* Determine whether R has a valid type and can be
7804 completed later. If R is invalid, then it is
7805 replaced by error_mark_node so that it will not be
7806 added to TYPE_FIELDS. */
7807 tree rtype = TREE_TYPE (r);
7808 if (can_complete_type_without_circularity (rtype))
7809 complete_type (rtype);
7810
7811 if (!COMPLETE_TYPE_P (rtype))
7812 {
7813 cxx_incomplete_type_error (r, rtype);
7814 r = error_mark_node;
7815 }
7816 }
7817
7818 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
7819 such a thing will already have been added to the field
7820 list by tsubst_enum in finish_member_declaration in the
7821 CLASSTYPE_NESTED_UTDS case above. */
7822 if (!(TREE_CODE (r) == TYPE_DECL
7823 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
7824 && DECL_ARTIFICIAL (r)))
7825 {
7826 set_current_access_from_decl (r);
7827 finish_member_declaration (r);
7828 }
7829 }
7830 }
7831 }
7832 else
7833 {
7834 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
7835 {
7836 /* Build new CLASSTYPE_FRIEND_CLASSES. */
7837
7838 tree friend_type = t;
7839 bool adjust_processing_template_decl = false;
7840
7841 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7842 {
7843 /* template <class T> friend class C; */
7844 friend_type = tsubst_friend_class (friend_type, args);
7845 adjust_processing_template_decl = true;
7846 }
7847 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
7848 {
7849 /* template <class T> friend class C::D; */
7850 friend_type = tsubst (friend_type, args,
7851 tf_warning_or_error, NULL_TREE);
7852 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7853 friend_type = TREE_TYPE (friend_type);
7854 adjust_processing_template_decl = true;
7855 }
7856 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
7857 {
7858 /* This could be either
7859
7860 friend class T::C;
7861
7862 when dependent_type_p is false or
7863
7864 template <class U> friend class T::C;
7865
7866 otherwise. */
7867 friend_type = tsubst (friend_type, args,
7868 tf_warning_or_error, NULL_TREE);
7869 /* Bump processing_template_decl for correct
7870 dependent_type_p calculation. */
7871 ++processing_template_decl;
7872 if (dependent_type_p (friend_type))
7873 adjust_processing_template_decl = true;
7874 --processing_template_decl;
7875 }
7876 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
7877 && hidden_name_p (TYPE_NAME (friend_type)))
7878 {
7879 /* friend class C;
7880
7881 where C hasn't been declared yet. Let's lookup name
7882 from namespace scope directly, bypassing any name that
7883 come from dependent base class. */
7884 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
7885
7886 /* The call to xref_tag_from_type does injection for friend
7887 classes. */
7888 push_nested_namespace (ns);
7889 friend_type =
7890 xref_tag_from_type (friend_type, NULL_TREE,
7891 /*tag_scope=*/ts_current);
7892 pop_nested_namespace (ns);
7893 }
7894 else if (uses_template_parms (friend_type))
7895 /* friend class C<T>; */
7896 friend_type = tsubst (friend_type, args,
7897 tf_warning_or_error, NULL_TREE);
7898 /* Otherwise it's
7899
7900 friend class C;
7901
7902 where C is already declared or
7903
7904 friend class C<int>;
7905
7906 We don't have to do anything in these cases. */
7907
7908 if (adjust_processing_template_decl)
7909 /* Trick make_friend_class into realizing that the friend
7910 we're adding is a template, not an ordinary class. It's
7911 important that we use make_friend_class since it will
7912 perform some error-checking and output cross-reference
7913 information. */
7914 ++processing_template_decl;
7915
7916 if (friend_type != error_mark_node)
7917 make_friend_class (type, friend_type, /*complain=*/false);
7918
7919 if (adjust_processing_template_decl)
7920 --processing_template_decl;
7921 }
7922 else
7923 {
7924 /* Build new DECL_FRIENDLIST. */
7925 tree r;
7926
7927 /* The file and line for this declaration, to
7928 assist in error message reporting. Since we
7929 called push_tinst_level above, we don't need to
7930 restore these. */
7931 input_location = DECL_SOURCE_LOCATION (t);
7932
7933 if (TREE_CODE (t) == TEMPLATE_DECL)
7934 {
7935 ++processing_template_decl;
7936 push_deferring_access_checks (dk_no_check);
7937 }
7938
7939 r = tsubst_friend_function (t, args);
7940 add_friend (type, r, /*complain=*/false);
7941 if (TREE_CODE (t) == TEMPLATE_DECL)
7942 {
7943 pop_deferring_access_checks ();
7944 --processing_template_decl;
7945 }
7946 }
7947 }
7948 }
7949
7950 /* Set the file and line number information to whatever is given for
7951 the class itself. This puts error messages involving generated
7952 implicit functions at a predictable point, and the same point
7953 that would be used for non-template classes. */
7954 input_location = DECL_SOURCE_LOCATION (typedecl);
7955
7956 unreverse_member_declarations (type);
7957 finish_struct_1 (type);
7958 TYPE_BEING_DEFINED (type) = 0;
7959
7960 /* Now that the class is complete, instantiate default arguments for
7961 any member functions. We don't do this earlier because the
7962 default arguments may reference members of the class. */
7963 if (!PRIMARY_TEMPLATE_P (templ))
7964 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
7965 if (TREE_CODE (t) == FUNCTION_DECL
7966 /* Implicitly generated member functions will not have template
7967 information; they are not instantiations, but instead are
7968 created "fresh" for each instantiation. */
7969 && DECL_TEMPLATE_INFO (t))
7970 tsubst_default_arguments (t);
7971
7972 /* Some typedefs referenced from within the template code need to be access
7973 checked at template instantiation time, i.e now. These types were
7974 added to the template at parsing time. Let's get those and perform
7975 the access checks then. */
7976 perform_typedefs_access_check (pattern, args);
7977 perform_deferred_access_checks ();
7978 pop_nested_class ();
7979 maximum_field_alignment = saved_maximum_field_alignment;
7980 pop_from_top_level ();
7981 pop_deferring_access_checks ();
7982 pop_tinst_level ();
7983
7984 /* The vtable for a template class can be emitted in any translation
7985 unit in which the class is instantiated. When there is no key
7986 method, however, finish_struct_1 will already have added TYPE to
7987 the keyed_classes list. */
7988 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
7989 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
7990
7991 return type;
7992 }
7993
7994 static tree
7995 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7996 {
7997 tree r;
7998
7999 if (!t)
8000 r = t;
8001 else if (TYPE_P (t))
8002 r = tsubst (t, args, complain, in_decl);
8003 else
8004 {
8005 r = tsubst_expr (t, args, complain, in_decl,
8006 /*integral_constant_expression_p=*/true);
8007 r = fold_non_dependent_expr (r);
8008 }
8009 return r;
8010 }
8011
8012 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8013 NONTYPE_ARGUMENT_PACK. */
8014
8015 static tree
8016 make_fnparm_pack (tree spec_parm)
8017 {
8018 /* Collect all of the extra "packed" parameters into an
8019 argument pack. */
8020 tree parmvec;
8021 tree parmtypevec;
8022 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8023 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8024 int i, len = list_length (spec_parm);
8025
8026 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8027 parmvec = make_tree_vec (len);
8028 parmtypevec = make_tree_vec (len);
8029 for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
8030 {
8031 TREE_VEC_ELT (parmvec, i) = spec_parm;
8032 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8033 }
8034
8035 /* Build the argument packs. */
8036 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
8037 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
8038 TREE_TYPE (argpack) = argtypepack;
8039
8040 return argpack;
8041 }
8042
8043 /* Substitute ARGS into T, which is an pack expansion
8044 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8045 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8046 (if only a partial substitution could be performed) or
8047 ERROR_MARK_NODE if there was an error. */
8048 tree
8049 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
8050 tree in_decl)
8051 {
8052 tree pattern;
8053 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
8054 int i, len = -1;
8055 tree result;
8056 int incomplete = 0;
8057 bool very_local_specializations = false;
8058
8059 gcc_assert (PACK_EXPANSION_P (t));
8060 pattern = PACK_EXPANSION_PATTERN (t);
8061
8062 /* Determine the argument packs that will instantiate the parameter
8063 packs used in the expansion expression. While we're at it,
8064 compute the number of arguments to be expanded and make sure it
8065 is consistent. */
8066 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
8067 pack = TREE_CHAIN (pack))
8068 {
8069 tree parm_pack = TREE_VALUE (pack);
8070 tree arg_pack = NULL_TREE;
8071 tree orig_arg = NULL_TREE;
8072
8073 if (TREE_CODE (parm_pack) == PARM_DECL)
8074 {
8075 arg_pack = retrieve_local_specialization (parm_pack);
8076 if (arg_pack == NULL_TREE)
8077 {
8078 /* This can happen for a parameter name used later in a function
8079 declaration (such as in a late-specified return type). Just
8080 make a dummy decl, since it's only used for its type. */
8081 gcc_assert (cp_unevaluated_operand != 0);
8082 arg_pack = tsubst_decl (parm_pack, args, complain);
8083 arg_pack = make_fnparm_pack (arg_pack);
8084 }
8085 }
8086 else
8087 {
8088 int level, idx, levels;
8089 template_parm_level_and_index (parm_pack, &level, &idx);
8090
8091 levels = TMPL_ARGS_DEPTH (args);
8092 if (level <= levels)
8093 arg_pack = TMPL_ARG (args, level, idx);
8094 }
8095
8096 orig_arg = arg_pack;
8097 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
8098 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
8099
8100 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
8101 /* This can only happen if we forget to expand an argument
8102 pack somewhere else. Just return an error, silently. */
8103 {
8104 result = make_tree_vec (1);
8105 TREE_VEC_ELT (result, 0) = error_mark_node;
8106 return result;
8107 }
8108
8109 if (arg_pack
8110 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
8111 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
8112 {
8113 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
8114 tree pattern = PACK_EXPANSION_PATTERN (expansion);
8115 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
8116 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
8117 /* The argument pack that the parameter maps to is just an
8118 expansion of the parameter itself, such as one would
8119 find in the implicit typedef of a class inside the
8120 class itself. Consider this parameter "unsubstituted",
8121 so that we will maintain the outer pack expansion. */
8122 arg_pack = NULL_TREE;
8123 }
8124
8125 if (arg_pack)
8126 {
8127 int my_len =
8128 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
8129
8130 /* It's all-or-nothing with incomplete argument packs. */
8131 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8132 return error_mark_node;
8133
8134 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8135 incomplete = 1;
8136
8137 if (len < 0)
8138 len = my_len;
8139 else if (len != my_len)
8140 {
8141 if (incomplete)
8142 /* We got explicit args for some packs but not others;
8143 do nothing now and try again after deduction. */
8144 return t;
8145 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
8146 error ("mismatched argument pack lengths while expanding "
8147 "%<%T%>",
8148 pattern);
8149 else
8150 error ("mismatched argument pack lengths while expanding "
8151 "%<%E%>",
8152 pattern);
8153 return error_mark_node;
8154 }
8155
8156 /* Keep track of the parameter packs and their corresponding
8157 argument packs. */
8158 packs = tree_cons (parm_pack, arg_pack, packs);
8159 TREE_TYPE (packs) = orig_arg;
8160 }
8161 else
8162 /* We can't substitute for this parameter pack. */
8163 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
8164 TREE_VALUE (pack),
8165 unsubstituted_packs);
8166 }
8167
8168 /* We cannot expand this expansion expression, because we don't have
8169 all of the argument packs we need. Substitute into the pattern
8170 and return a PACK_EXPANSION_*. The caller will need to deal with
8171 that. */
8172 if (unsubstituted_packs)
8173 {
8174 tree new_pat;
8175 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8176 new_pat = tsubst_expr (pattern, args, complain, in_decl,
8177 /*integral_constant_expression_p=*/false);
8178 else
8179 new_pat = tsubst (pattern, args, complain, in_decl);
8180 return make_pack_expansion (new_pat);
8181 }
8182
8183 /* We could not find any argument packs that work. */
8184 if (len < 0)
8185 return error_mark_node;
8186
8187 if (!local_specializations)
8188 {
8189 /* We're in a late-specified return type, so we don't have a local
8190 specializations table. Create one for doing this expansion. */
8191 very_local_specializations = true;
8192 local_specializations = htab_create (37,
8193 hash_local_specialization,
8194 eq_local_specializations,
8195 NULL);
8196 }
8197
8198 /* For each argument in each argument pack, substitute into the
8199 pattern. */
8200 result = make_tree_vec (len + incomplete);
8201 for (i = 0; i < len + incomplete; ++i)
8202 {
8203 /* For parameter pack, change the substitution of the parameter
8204 pack to the ith argument in its argument pack, then expand
8205 the pattern. */
8206 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8207 {
8208 tree parm = TREE_PURPOSE (pack);
8209
8210 if (TREE_CODE (parm) == PARM_DECL)
8211 {
8212 /* Select the Ith argument from the pack. */
8213 tree arg = make_node (ARGUMENT_PACK_SELECT);
8214 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8215 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8216 mark_used (parm);
8217 register_local_specialization (arg, parm);
8218 }
8219 else
8220 {
8221 tree value = parm;
8222 int idx, level;
8223 template_parm_level_and_index (parm, &level, &idx);
8224
8225 if (i < len)
8226 {
8227 /* Select the Ith argument from the pack. */
8228 value = make_node (ARGUMENT_PACK_SELECT);
8229 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8230 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8231 }
8232
8233 /* Update the corresponding argument. */
8234 TMPL_ARG (args, level, idx) = value;
8235 }
8236 }
8237
8238 /* Substitute into the PATTERN with the altered arguments. */
8239 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8240 TREE_VEC_ELT (result, i) =
8241 tsubst_expr (pattern, args, complain, in_decl,
8242 /*integral_constant_expression_p=*/false);
8243 else
8244 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8245
8246 if (i == len)
8247 /* When we have incomplete argument packs, the last "expanded"
8248 result is itself a pack expansion, which allows us
8249 to deduce more arguments. */
8250 TREE_VEC_ELT (result, i) =
8251 make_pack_expansion (TREE_VEC_ELT (result, i));
8252
8253 if (TREE_VEC_ELT (result, i) == error_mark_node)
8254 {
8255 result = error_mark_node;
8256 break;
8257 }
8258 }
8259
8260 /* Update ARGS to restore the substitution from parameter packs to
8261 their argument packs. */
8262 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8263 {
8264 tree parm = TREE_PURPOSE (pack);
8265
8266 if (TREE_CODE (parm) == PARM_DECL)
8267 register_local_specialization (TREE_TYPE (pack), parm);
8268 else
8269 {
8270 int idx, level;
8271 template_parm_level_and_index (parm, &level, &idx);
8272
8273 /* Update the corresponding argument. */
8274 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8275 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8276 TREE_TYPE (pack);
8277 else
8278 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8279 }
8280 }
8281
8282 if (very_local_specializations)
8283 {
8284 htab_delete (local_specializations);
8285 local_specializations = NULL;
8286 }
8287
8288 return result;
8289 }
8290
8291 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8292 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8293 parameter packs; all parms generated from a function parameter pack will
8294 have the same DECL_PARM_INDEX. */
8295
8296 tree
8297 get_pattern_parm (tree parm, tree tmpl)
8298 {
8299 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
8300 tree patparm;
8301
8302 if (DECL_ARTIFICIAL (parm))
8303 {
8304 for (patparm = DECL_ARGUMENTS (pattern);
8305 patparm; patparm = TREE_CHAIN (patparm))
8306 if (DECL_ARTIFICIAL (patparm)
8307 && DECL_NAME (parm) == DECL_NAME (patparm))
8308 break;
8309 }
8310 else
8311 {
8312 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
8313 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
8314 gcc_assert (DECL_PARM_INDEX (patparm)
8315 == DECL_PARM_INDEX (parm));
8316 }
8317
8318 return patparm;
8319 }
8320
8321 /* Substitute ARGS into the vector or list of template arguments T. */
8322
8323 static tree
8324 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8325 {
8326 tree orig_t = t;
8327 int len = TREE_VEC_LENGTH (t);
8328 int need_new = 0, i, expanded_len_adjust = 0, out;
8329 tree *elts = (tree *) alloca (len * sizeof (tree));
8330
8331 for (i = 0; i < len; i++)
8332 {
8333 tree orig_arg = TREE_VEC_ELT (t, i);
8334 tree new_arg;
8335
8336 if (TREE_CODE (orig_arg) == TREE_VEC)
8337 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
8338 else if (PACK_EXPANSION_P (orig_arg))
8339 {
8340 /* Substitute into an expansion expression. */
8341 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8342
8343 if (TREE_CODE (new_arg) == TREE_VEC)
8344 /* Add to the expanded length adjustment the number of
8345 expanded arguments. We subtract one from this
8346 measurement, because the argument pack expression
8347 itself is already counted as 1 in
8348 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8349 the argument pack is empty. */
8350 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
8351 }
8352 else if (ARGUMENT_PACK_P (orig_arg))
8353 {
8354 /* Substitute into each of the arguments. */
8355 new_arg = TYPE_P (orig_arg)
8356 ? cxx_make_type (TREE_CODE (orig_arg))
8357 : make_node (TREE_CODE (orig_arg));
8358
8359 SET_ARGUMENT_PACK_ARGS (
8360 new_arg,
8361 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
8362 args, complain, in_decl));
8363
8364 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
8365 new_arg = error_mark_node;
8366
8367 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
8368 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
8369 complain, in_decl);
8370 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
8371
8372 if (TREE_TYPE (new_arg) == error_mark_node)
8373 new_arg = error_mark_node;
8374 }
8375 }
8376 else
8377 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
8378
8379 if (new_arg == error_mark_node)
8380 return error_mark_node;
8381
8382 elts[i] = new_arg;
8383 if (new_arg != orig_arg)
8384 need_new = 1;
8385 }
8386
8387 if (!need_new)
8388 return t;
8389
8390 /* Make space for the expanded arguments coming from template
8391 argument packs. */
8392 t = make_tree_vec (len + expanded_len_adjust);
8393 for (i = 0, out = 0; i < len; i++)
8394 {
8395 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
8396 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
8397 && TREE_CODE (elts[i]) == TREE_VEC)
8398 {
8399 int idx;
8400
8401 /* Now expand the template argument pack "in place". */
8402 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
8403 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
8404 }
8405 else
8406 {
8407 TREE_VEC_ELT (t, out) = elts[i];
8408 out++;
8409 }
8410 }
8411
8412 return t;
8413 }
8414
8415 /* Return the result of substituting ARGS into the template parameters
8416 given by PARMS. If there are m levels of ARGS and m + n levels of
8417 PARMS, then the result will contain n levels of PARMS. For
8418 example, if PARMS is `template <class T> template <class U>
8419 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8420 result will be `template <int*, double, class V>'. */
8421
8422 static tree
8423 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
8424 {
8425 tree r = NULL_TREE;
8426 tree* new_parms;
8427
8428 /* When substituting into a template, we must set
8429 PROCESSING_TEMPLATE_DECL as the template parameters may be
8430 dependent if they are based on one-another, and the dependency
8431 predicates are short-circuit outside of templates. */
8432 ++processing_template_decl;
8433
8434 for (new_parms = &r;
8435 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
8436 new_parms = &(TREE_CHAIN (*new_parms)),
8437 parms = TREE_CHAIN (parms))
8438 {
8439 tree new_vec =
8440 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
8441 int i;
8442
8443 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
8444 {
8445 tree tuple;
8446 tree default_value;
8447 tree parm_decl;
8448
8449 if (parms == error_mark_node)
8450 continue;
8451
8452 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
8453
8454 if (tuple == error_mark_node)
8455 continue;
8456
8457 default_value = TREE_PURPOSE (tuple);
8458 parm_decl = TREE_VALUE (tuple);
8459
8460 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
8461 if (TREE_CODE (parm_decl) == PARM_DECL
8462 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
8463 parm_decl = error_mark_node;
8464 default_value = tsubst_template_arg (default_value, args,
8465 complain, NULL_TREE);
8466
8467 tuple = build_tree_list (default_value, parm_decl);
8468 TREE_VEC_ELT (new_vec, i) = tuple;
8469 }
8470
8471 *new_parms =
8472 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
8473 - TMPL_ARGS_DEPTH (args)),
8474 new_vec, NULL_TREE);
8475 }
8476
8477 --processing_template_decl;
8478
8479 return r;
8480 }
8481
8482 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8483 type T. If T is not an aggregate or enumeration type, it is
8484 handled as if by tsubst. IN_DECL is as for tsubst. If
8485 ENTERING_SCOPE is nonzero, T is the context for a template which
8486 we are presently tsubst'ing. Return the substituted value. */
8487
8488 static tree
8489 tsubst_aggr_type (tree t,
8490 tree args,
8491 tsubst_flags_t complain,
8492 tree in_decl,
8493 int entering_scope)
8494 {
8495 if (t == NULL_TREE)
8496 return NULL_TREE;
8497
8498 switch (TREE_CODE (t))
8499 {
8500 case RECORD_TYPE:
8501 if (TYPE_PTRMEMFUNC_P (t))
8502 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
8503
8504 /* Else fall through. */
8505 case ENUMERAL_TYPE:
8506 case UNION_TYPE:
8507 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
8508 {
8509 tree argvec;
8510 tree context;
8511 tree r;
8512 int saved_unevaluated_operand;
8513 int saved_inhibit_evaluation_warnings;
8514
8515 /* In "sizeof(X<I>)" we need to evaluate "I". */
8516 saved_unevaluated_operand = cp_unevaluated_operand;
8517 cp_unevaluated_operand = 0;
8518 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8519 c_inhibit_evaluation_warnings = 0;
8520
8521 /* First, determine the context for the type we are looking
8522 up. */
8523 context = TYPE_CONTEXT (t);
8524 if (context)
8525 {
8526 context = tsubst_aggr_type (context, args, complain,
8527 in_decl, /*entering_scope=*/1);
8528 /* If context is a nested class inside a class template,
8529 it may still need to be instantiated (c++/33959). */
8530 if (TYPE_P (context))
8531 context = complete_type (context);
8532 }
8533
8534 /* Then, figure out what arguments are appropriate for the
8535 type we are trying to find. For example, given:
8536
8537 template <class T> struct S;
8538 template <class T, class U> void f(T, U) { S<U> su; }
8539
8540 and supposing that we are instantiating f<int, double>,
8541 then our ARGS will be {int, double}, but, when looking up
8542 S we only want {double}. */
8543 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
8544 complain, in_decl);
8545 if (argvec == error_mark_node)
8546 r = error_mark_node;
8547 else
8548 {
8549 r = lookup_template_class (t, argvec, in_decl, context,
8550 entering_scope, complain);
8551 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
8552 }
8553
8554 cp_unevaluated_operand = saved_unevaluated_operand;
8555 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8556
8557 return r;
8558 }
8559 else
8560 /* This is not a template type, so there's nothing to do. */
8561 return t;
8562
8563 default:
8564 return tsubst (t, args, complain, in_decl);
8565 }
8566 }
8567
8568 /* Substitute into the default argument ARG (a default argument for
8569 FN), which has the indicated TYPE. */
8570
8571 tree
8572 tsubst_default_argument (tree fn, tree type, tree arg)
8573 {
8574 tree saved_class_ptr = NULL_TREE;
8575 tree saved_class_ref = NULL_TREE;
8576
8577 /* This default argument came from a template. Instantiate the
8578 default argument here, not in tsubst. In the case of
8579 something like:
8580
8581 template <class T>
8582 struct S {
8583 static T t();
8584 void f(T = t());
8585 };
8586
8587 we must be careful to do name lookup in the scope of S<T>,
8588 rather than in the current class. */
8589 push_access_scope (fn);
8590 /* The "this" pointer is not valid in a default argument. */
8591 if (cfun)
8592 {
8593 saved_class_ptr = current_class_ptr;
8594 cp_function_chain->x_current_class_ptr = NULL_TREE;
8595 saved_class_ref = current_class_ref;
8596 cp_function_chain->x_current_class_ref = NULL_TREE;
8597 }
8598
8599 push_deferring_access_checks(dk_no_deferred);
8600 /* The default argument expression may cause implicitly defined
8601 member functions to be synthesized, which will result in garbage
8602 collection. We must treat this situation as if we were within
8603 the body of function so as to avoid collecting live data on the
8604 stack. */
8605 ++function_depth;
8606 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
8607 tf_warning_or_error, NULL_TREE,
8608 /*integral_constant_expression_p=*/false);
8609 --function_depth;
8610 pop_deferring_access_checks();
8611
8612 /* Restore the "this" pointer. */
8613 if (cfun)
8614 {
8615 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8616 cp_function_chain->x_current_class_ref = saved_class_ref;
8617 }
8618
8619 /* Make sure the default argument is reasonable. */
8620 arg = check_default_argument (type, arg);
8621
8622 pop_access_scope (fn);
8623
8624 return arg;
8625 }
8626
8627 /* Substitute into all the default arguments for FN. */
8628
8629 static void
8630 tsubst_default_arguments (tree fn)
8631 {
8632 tree arg;
8633 tree tmpl_args;
8634
8635 tmpl_args = DECL_TI_ARGS (fn);
8636
8637 /* If this function is not yet instantiated, we certainly don't need
8638 its default arguments. */
8639 if (uses_template_parms (tmpl_args))
8640 return;
8641
8642 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8643 arg;
8644 arg = TREE_CHAIN (arg))
8645 if (TREE_PURPOSE (arg))
8646 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
8647 TREE_VALUE (arg),
8648 TREE_PURPOSE (arg));
8649 }
8650
8651 /* Substitute the ARGS into the T, which is a _DECL. Return the
8652 result of the substitution. Issue error and warning messages under
8653 control of COMPLAIN. */
8654
8655 static tree
8656 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8657 {
8658 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
8659 location_t saved_loc;
8660 tree r = NULL_TREE;
8661 tree in_decl = t;
8662 hashval_t hash = 0;
8663
8664 /* Set the filename and linenumber to improve error-reporting. */
8665 saved_loc = input_location;
8666 input_location = DECL_SOURCE_LOCATION (t);
8667
8668 switch (TREE_CODE (t))
8669 {
8670 case TEMPLATE_DECL:
8671 {
8672 /* We can get here when processing a member function template,
8673 member class template, or template template parameter. */
8674 tree decl = DECL_TEMPLATE_RESULT (t);
8675 tree spec;
8676 tree tmpl_args;
8677 tree full_args;
8678
8679 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8680 {
8681 /* Template template parameter is treated here. */
8682 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8683 if (new_type == error_mark_node)
8684 RETURN (error_mark_node);
8685
8686 r = copy_decl (t);
8687 TREE_CHAIN (r) = NULL_TREE;
8688 TREE_TYPE (r) = new_type;
8689 DECL_TEMPLATE_RESULT (r)
8690 = build_decl (DECL_SOURCE_LOCATION (decl),
8691 TYPE_DECL, DECL_NAME (decl), new_type);
8692 DECL_TEMPLATE_PARMS (r)
8693 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8694 complain);
8695 TYPE_NAME (new_type) = r;
8696 break;
8697 }
8698
8699 /* We might already have an instance of this template.
8700 The ARGS are for the surrounding class type, so the
8701 full args contain the tsubst'd args for the context,
8702 plus the innermost args from the template decl. */
8703 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
8704 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
8705 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8706 /* Because this is a template, the arguments will still be
8707 dependent, even after substitution. If
8708 PROCESSING_TEMPLATE_DECL is not set, the dependency
8709 predicates will short-circuit. */
8710 ++processing_template_decl;
8711 full_args = tsubst_template_args (tmpl_args, args,
8712 complain, in_decl);
8713 --processing_template_decl;
8714 if (full_args == error_mark_node)
8715 RETURN (error_mark_node);
8716
8717 /* If this is a default template template argument,
8718 tsubst might not have changed anything. */
8719 if (full_args == tmpl_args)
8720 RETURN (t);
8721
8722 hash = hash_tmpl_and_args (t, full_args);
8723 spec = retrieve_specialization (t, full_args, hash);
8724 if (spec != NULL_TREE)
8725 {
8726 r = spec;
8727 break;
8728 }
8729
8730 /* Make a new template decl. It will be similar to the
8731 original, but will record the current template arguments.
8732 We also create a new function declaration, which is just
8733 like the old one, but points to this new template, rather
8734 than the old one. */
8735 r = copy_decl (t);
8736 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
8737 TREE_CHAIN (r) = NULL_TREE;
8738
8739 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
8740
8741 if (TREE_CODE (decl) == TYPE_DECL)
8742 {
8743 tree new_type;
8744 ++processing_template_decl;
8745 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8746 --processing_template_decl;
8747 if (new_type == error_mark_node)
8748 RETURN (error_mark_node);
8749
8750 TREE_TYPE (r) = new_type;
8751 CLASSTYPE_TI_TEMPLATE (new_type) = r;
8752 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
8753 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8754 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
8755 }
8756 else
8757 {
8758 tree new_decl;
8759 ++processing_template_decl;
8760 new_decl = tsubst (decl, args, complain, in_decl);
8761 --processing_template_decl;
8762 if (new_decl == error_mark_node)
8763 RETURN (error_mark_node);
8764
8765 DECL_TEMPLATE_RESULT (r) = new_decl;
8766 DECL_TI_TEMPLATE (new_decl) = r;
8767 TREE_TYPE (r) = TREE_TYPE (new_decl);
8768 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
8769 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
8770 }
8771
8772 SET_DECL_IMPLICIT_INSTANTIATION (r);
8773 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
8774 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
8775
8776 /* The template parameters for this new template are all the
8777 template parameters for the old template, except the
8778 outermost level of parameters. */
8779 DECL_TEMPLATE_PARMS (r)
8780 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8781 complain);
8782
8783 if (PRIMARY_TEMPLATE_P (t))
8784 DECL_PRIMARY_TEMPLATE (r) = r;
8785
8786 if (TREE_CODE (decl) != TYPE_DECL)
8787 /* Record this non-type partial instantiation. */
8788 register_specialization (r, t,
8789 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
8790 false, hash);
8791 }
8792 break;
8793
8794 case FUNCTION_DECL:
8795 {
8796 tree ctx;
8797 tree argvec = NULL_TREE;
8798 tree *friends;
8799 tree gen_tmpl;
8800 tree type;
8801 int member;
8802 int args_depth;
8803 int parms_depth;
8804
8805 /* Nobody should be tsubst'ing into non-template functions. */
8806 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
8807
8808 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
8809 {
8810 tree spec;
8811 bool dependent_p;
8812
8813 /* If T is not dependent, just return it. We have to
8814 increment PROCESSING_TEMPLATE_DECL because
8815 value_dependent_expression_p assumes that nothing is
8816 dependent when PROCESSING_TEMPLATE_DECL is zero. */
8817 ++processing_template_decl;
8818 dependent_p = value_dependent_expression_p (t);
8819 --processing_template_decl;
8820 if (!dependent_p)
8821 RETURN (t);
8822
8823 /* Calculate the most general template of which R is a
8824 specialization, and the complete set of arguments used to
8825 specialize R. */
8826 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
8827 argvec = tsubst_template_args (DECL_TI_ARGS
8828 (DECL_TEMPLATE_RESULT (gen_tmpl)),
8829 args, complain, in_decl);
8830
8831 /* Check to see if we already have this specialization. */
8832 hash = hash_tmpl_and_args (gen_tmpl, argvec);
8833 spec = retrieve_specialization (gen_tmpl, argvec, hash);
8834
8835 if (spec)
8836 {
8837 r = spec;
8838 break;
8839 }
8840
8841 /* We can see more levels of arguments than parameters if
8842 there was a specialization of a member template, like
8843 this:
8844
8845 template <class T> struct S { template <class U> void f(); }
8846 template <> template <class U> void S<int>::f(U);
8847
8848 Here, we'll be substituting into the specialization,
8849 because that's where we can find the code we actually
8850 want to generate, but we'll have enough arguments for
8851 the most general template.
8852
8853 We also deal with the peculiar case:
8854
8855 template <class T> struct S {
8856 template <class U> friend void f();
8857 };
8858 template <class U> void f() {}
8859 template S<int>;
8860 template void f<double>();
8861
8862 Here, the ARGS for the instantiation of will be {int,
8863 double}. But, we only need as many ARGS as there are
8864 levels of template parameters in CODE_PATTERN. We are
8865 careful not to get fooled into reducing the ARGS in
8866 situations like:
8867
8868 template <class T> struct S { template <class U> void f(U); }
8869 template <class T> template <> void S<T>::f(int) {}
8870
8871 which we can spot because the pattern will be a
8872 specialization in this case. */
8873 args_depth = TMPL_ARGS_DEPTH (args);
8874 parms_depth =
8875 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
8876 if (args_depth > parms_depth
8877 && !DECL_TEMPLATE_SPECIALIZATION (t))
8878 args = get_innermost_template_args (args, parms_depth);
8879 }
8880 else
8881 {
8882 /* This special case arises when we have something like this:
8883
8884 template <class T> struct S {
8885 friend void f<int>(int, double);
8886 };
8887
8888 Here, the DECL_TI_TEMPLATE for the friend declaration
8889 will be an IDENTIFIER_NODE. We are being called from
8890 tsubst_friend_function, and we want only to create a
8891 new decl (R) with appropriate types so that we can call
8892 determine_specialization. */
8893 gen_tmpl = NULL_TREE;
8894 }
8895
8896 if (DECL_CLASS_SCOPE_P (t))
8897 {
8898 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
8899 member = 2;
8900 else
8901 member = 1;
8902 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
8903 complain, t, /*entering_scope=*/1);
8904 }
8905 else
8906 {
8907 member = 0;
8908 ctx = DECL_CONTEXT (t);
8909 }
8910 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8911 if (type == error_mark_node)
8912 RETURN (error_mark_node);
8913
8914 /* We do NOT check for matching decls pushed separately at this
8915 point, as they may not represent instantiations of this
8916 template, and in any case are considered separate under the
8917 discrete model. */
8918 r = copy_decl (t);
8919 DECL_USE_TEMPLATE (r) = 0;
8920 TREE_TYPE (r) = type;
8921 /* Clear out the mangled name and RTL for the instantiation. */
8922 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8923 SET_DECL_RTL (r, NULL_RTX);
8924 /* Leave DECL_INITIAL set on deleted instantiations. */
8925 if (!DECL_DELETED_FN (r))
8926 DECL_INITIAL (r) = NULL_TREE;
8927 DECL_CONTEXT (r) = ctx;
8928
8929 if (member && DECL_CONV_FN_P (r))
8930 /* Type-conversion operator. Reconstruct the name, in
8931 case it's the name of one of the template's parameters. */
8932 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
8933
8934 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
8935 complain, t);
8936 DECL_RESULT (r) = NULL_TREE;
8937
8938 TREE_STATIC (r) = 0;
8939 TREE_PUBLIC (r) = TREE_PUBLIC (t);
8940 DECL_EXTERNAL (r) = 1;
8941 /* If this is an instantiation of a function with internal
8942 linkage, we already know what object file linkage will be
8943 assigned to the instantiation. */
8944 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
8945 DECL_DEFER_OUTPUT (r) = 0;
8946 TREE_CHAIN (r) = NULL_TREE;
8947 DECL_PENDING_INLINE_INFO (r) = 0;
8948 DECL_PENDING_INLINE_P (r) = 0;
8949 DECL_SAVED_TREE (r) = NULL_TREE;
8950 DECL_STRUCT_FUNCTION (r) = NULL;
8951 TREE_USED (r) = 0;
8952 /* We'll re-clone as appropriate in instantiate_template. */
8953 DECL_CLONED_FUNCTION (r) = NULL_TREE;
8954
8955 /* If we aren't complaining now, return on error before we register
8956 the specialization so that we'll complain eventually. */
8957 if ((complain & tf_error) == 0
8958 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
8959 && !grok_op_properties (r, /*complain=*/false))
8960 RETURN (error_mark_node);
8961
8962 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
8963 this in the special friend case mentioned above where
8964 GEN_TMPL is NULL. */
8965 if (gen_tmpl)
8966 {
8967 DECL_TEMPLATE_INFO (r)
8968 = build_template_info (gen_tmpl, argvec);
8969 SET_DECL_IMPLICIT_INSTANTIATION (r);
8970 register_specialization (r, gen_tmpl, argvec, false, hash);
8971
8972 /* We're not supposed to instantiate default arguments
8973 until they are called, for a template. But, for a
8974 declaration like:
8975
8976 template <class T> void f ()
8977 { extern void g(int i = T()); }
8978
8979 we should do the substitution when the template is
8980 instantiated. We handle the member function case in
8981 instantiate_class_template since the default arguments
8982 might refer to other members of the class. */
8983 if (!member
8984 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8985 && !uses_template_parms (argvec))
8986 tsubst_default_arguments (r);
8987 }
8988 else
8989 DECL_TEMPLATE_INFO (r) = NULL_TREE;
8990
8991 /* Copy the list of befriending classes. */
8992 for (friends = &DECL_BEFRIENDING_CLASSES (r);
8993 *friends;
8994 friends = &TREE_CHAIN (*friends))
8995 {
8996 *friends = copy_node (*friends);
8997 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
8998 args, complain,
8999 in_decl);
9000 }
9001
9002 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
9003 {
9004 maybe_retrofit_in_chrg (r);
9005 if (DECL_CONSTRUCTOR_P (r))
9006 grok_ctor_properties (ctx, r);
9007 /* If this is an instantiation of a member template, clone it.
9008 If it isn't, that'll be handled by
9009 clone_constructors_and_destructors. */
9010 if (PRIMARY_TEMPLATE_P (gen_tmpl))
9011 clone_function_decl (r, /*update_method_vec_p=*/0);
9012 }
9013 else if ((complain & tf_error) != 0
9014 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9015 && !grok_op_properties (r, /*complain=*/true))
9016 RETURN (error_mark_node);
9017
9018 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
9019 SET_DECL_FRIEND_CONTEXT (r,
9020 tsubst (DECL_FRIEND_CONTEXT (t),
9021 args, complain, in_decl));
9022
9023 /* Possibly limit visibility based on template args. */
9024 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9025 if (DECL_VISIBILITY_SPECIFIED (t))
9026 {
9027 DECL_VISIBILITY_SPECIFIED (r) = 0;
9028 DECL_ATTRIBUTES (r)
9029 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9030 }
9031 determine_visibility (r);
9032 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
9033 && !processing_template_decl)
9034 defaulted_late_check (r);
9035
9036 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9037 args, complain, in_decl);
9038 }
9039 break;
9040
9041 case PARM_DECL:
9042 {
9043 tree type = NULL_TREE;
9044 int i, len = 1;
9045 tree expanded_types = NULL_TREE;
9046 tree prev_r = NULL_TREE;
9047 tree first_r = NULL_TREE;
9048
9049 if (FUNCTION_PARAMETER_PACK_P (t))
9050 {
9051 /* If there is a local specialization that isn't a
9052 parameter pack, it means that we're doing a "simple"
9053 substitution from inside tsubst_pack_expansion. Just
9054 return the local specialization (which will be a single
9055 parm). */
9056 tree spec = retrieve_local_specialization (t);
9057 if (spec
9058 && TREE_CODE (spec) == PARM_DECL
9059 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
9060 RETURN (spec);
9061
9062 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9063 the parameters in this function parameter pack. */
9064 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
9065 complain, in_decl);
9066 if (TREE_CODE (expanded_types) == TREE_VEC)
9067 {
9068 len = TREE_VEC_LENGTH (expanded_types);
9069
9070 /* Zero-length parameter packs are boring. Just substitute
9071 into the chain. */
9072 if (len == 0)
9073 RETURN (tsubst (TREE_CHAIN (t), args, complain,
9074 TREE_CHAIN (t)));
9075 }
9076 else
9077 {
9078 /* All we did was update the type. Make a note of that. */
9079 type = expanded_types;
9080 expanded_types = NULL_TREE;
9081 }
9082 }
9083
9084 /* Loop through all of the parameter's we'll build. When T is
9085 a function parameter pack, LEN is the number of expanded
9086 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9087 r = NULL_TREE;
9088 for (i = 0; i < len; ++i)
9089 {
9090 prev_r = r;
9091 r = copy_node (t);
9092 if (DECL_TEMPLATE_PARM_P (t))
9093 SET_DECL_TEMPLATE_PARM_P (r);
9094
9095 /* An argument of a function parameter pack is not a parameter
9096 pack. */
9097 FUNCTION_PARAMETER_PACK_P (r) = false;
9098
9099 if (expanded_types)
9100 /* We're on the Ith parameter of the function parameter
9101 pack. */
9102 {
9103 /* Get the Ith type. */
9104 type = TREE_VEC_ELT (expanded_types, i);
9105
9106 if (DECL_NAME (r))
9107 /* Rename the parameter to include the index. */
9108 DECL_NAME (r) =
9109 make_ith_pack_parameter_name (DECL_NAME (r), i);
9110 }
9111 else if (!type)
9112 /* We're dealing with a normal parameter. */
9113 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9114
9115 type = type_decays_to (type);
9116 TREE_TYPE (r) = type;
9117 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9118
9119 if (DECL_INITIAL (r))
9120 {
9121 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
9122 DECL_INITIAL (r) = TREE_TYPE (r);
9123 else
9124 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
9125 complain, in_decl);
9126 }
9127
9128 DECL_CONTEXT (r) = NULL_TREE;
9129
9130 if (!DECL_TEMPLATE_PARM_P (r))
9131 DECL_ARG_TYPE (r) = type_passed_as (type);
9132
9133 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9134 args, complain, in_decl);
9135
9136 /* Keep track of the first new parameter we
9137 generate. That's what will be returned to the
9138 caller. */
9139 if (!first_r)
9140 first_r = r;
9141
9142 /* Build a proper chain of parameters when substituting
9143 into a function parameter pack. */
9144 if (prev_r)
9145 TREE_CHAIN (prev_r) = r;
9146 }
9147
9148 if (TREE_CHAIN (t))
9149 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
9150 complain, TREE_CHAIN (t));
9151
9152 /* FIRST_R contains the start of the chain we've built. */
9153 r = first_r;
9154 }
9155 break;
9156
9157 case FIELD_DECL:
9158 {
9159 tree type;
9160
9161 r = copy_decl (t);
9162 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9163 if (type == error_mark_node)
9164 RETURN (error_mark_node);
9165 TREE_TYPE (r) = type;
9166 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9167
9168 /* DECL_INITIAL gives the number of bits in a bit-field. */
9169 DECL_INITIAL (r)
9170 = tsubst_expr (DECL_INITIAL (t), args,
9171 complain, in_decl,
9172 /*integral_constant_expression_p=*/true);
9173 /* We don't have to set DECL_CONTEXT here; it is set by
9174 finish_member_declaration. */
9175 TREE_CHAIN (r) = NULL_TREE;
9176 if (VOID_TYPE_P (type))
9177 error ("instantiation of %q+D as type %qT", r, type);
9178
9179 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9180 args, complain, in_decl);
9181 }
9182 break;
9183
9184 case USING_DECL:
9185 /* We reach here only for member using decls. */
9186 if (DECL_DEPENDENT_P (t))
9187 {
9188 r = do_class_using_decl
9189 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
9190 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
9191 if (!r)
9192 r = error_mark_node;
9193 else
9194 {
9195 TREE_PROTECTED (r) = TREE_PROTECTED (t);
9196 TREE_PRIVATE (r) = TREE_PRIVATE (t);
9197 }
9198 }
9199 else
9200 {
9201 r = copy_node (t);
9202 TREE_CHAIN (r) = NULL_TREE;
9203 }
9204 break;
9205
9206 case TYPE_DECL:
9207 case VAR_DECL:
9208 {
9209 tree argvec = NULL_TREE;
9210 tree gen_tmpl = NULL_TREE;
9211 tree spec;
9212 tree tmpl = NULL_TREE;
9213 tree ctx;
9214 tree type = NULL_TREE;
9215 bool local_p;
9216
9217 if (TREE_CODE (t) == TYPE_DECL
9218 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9219 {
9220 /* If this is the canonical decl, we don't have to
9221 mess with instantiations, and often we can't (for
9222 typename, template type parms and such). Note that
9223 TYPE_NAME is not correct for the above test if
9224 we've copied the type for a typedef. */
9225 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9226 if (type == error_mark_node)
9227 RETURN (error_mark_node);
9228 r = TYPE_NAME (type);
9229 break;
9230 }
9231
9232 /* Check to see if we already have the specialization we
9233 need. */
9234 spec = NULL_TREE;
9235 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9236 {
9237 /* T is a static data member or namespace-scope entity.
9238 We have to substitute into namespace-scope variables
9239 (even though such entities are never templates) because
9240 of cases like:
9241
9242 template <class T> void f() { extern T t; }
9243
9244 where the entity referenced is not known until
9245 instantiation time. */
9246 local_p = false;
9247 ctx = DECL_CONTEXT (t);
9248 if (DECL_CLASS_SCOPE_P (t))
9249 {
9250 ctx = tsubst_aggr_type (ctx, args,
9251 complain,
9252 in_decl, /*entering_scope=*/1);
9253 /* If CTX is unchanged, then T is in fact the
9254 specialization we want. That situation occurs when
9255 referencing a static data member within in its own
9256 class. We can use pointer equality, rather than
9257 same_type_p, because DECL_CONTEXT is always
9258 canonical. */
9259 if (ctx == DECL_CONTEXT (t))
9260 spec = t;
9261 }
9262
9263 if (!spec)
9264 {
9265 tmpl = DECL_TI_TEMPLATE (t);
9266 gen_tmpl = most_general_template (tmpl);
9267 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
9268 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9269 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9270 }
9271 }
9272 else
9273 {
9274 /* A local variable. */
9275 local_p = true;
9276 /* Subsequent calls to pushdecl will fill this in. */
9277 ctx = NULL_TREE;
9278 spec = retrieve_local_specialization (t);
9279 }
9280 /* If we already have the specialization we need, there is
9281 nothing more to do. */
9282 if (spec)
9283 {
9284 r = spec;
9285 break;
9286 }
9287
9288 /* Create a new node for the specialization we need. */
9289 r = copy_decl (t);
9290 if (type == NULL_TREE)
9291 {
9292 if (is_typedef_decl (t))
9293 type = DECL_ORIGINAL_TYPE (t);
9294 else
9295 type = TREE_TYPE (t);
9296 type = tsubst (type, args, complain, in_decl);
9297 }
9298 if (TREE_CODE (r) == VAR_DECL)
9299 {
9300 /* Even if the original location is out of scope, the
9301 newly substituted one is not. */
9302 DECL_DEAD_FOR_LOCAL (r) = 0;
9303 DECL_INITIALIZED_P (r) = 0;
9304 DECL_TEMPLATE_INSTANTIATED (r) = 0;
9305 if (type == error_mark_node)
9306 RETURN (error_mark_node);
9307 if (TREE_CODE (type) == FUNCTION_TYPE)
9308 {
9309 /* It may seem that this case cannot occur, since:
9310
9311 typedef void f();
9312 void g() { f x; }
9313
9314 declares a function, not a variable. However:
9315
9316 typedef void f();
9317 template <typename T> void g() { T t; }
9318 template void g<f>();
9319
9320 is an attempt to declare a variable with function
9321 type. */
9322 error ("variable %qD has function type",
9323 /* R is not yet sufficiently initialized, so we
9324 just use its name. */
9325 DECL_NAME (r));
9326 RETURN (error_mark_node);
9327 }
9328 type = complete_type (type);
9329 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
9330 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
9331 type = check_var_type (DECL_NAME (r), type);
9332
9333 if (DECL_HAS_VALUE_EXPR_P (t))
9334 {
9335 tree ve = DECL_VALUE_EXPR (t);
9336 ve = tsubst_expr (ve, args, complain, in_decl,
9337 /*constant_expression_p=*/false);
9338 SET_DECL_VALUE_EXPR (r, ve);
9339 }
9340 }
9341 else if (DECL_SELF_REFERENCE_P (t))
9342 SET_DECL_SELF_REFERENCE_P (r);
9343 TREE_TYPE (r) = type;
9344 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9345 DECL_CONTEXT (r) = ctx;
9346 /* Clear out the mangled name and RTL for the instantiation. */
9347 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9348 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9349 SET_DECL_RTL (r, NULL_RTX);
9350 /* The initializer must not be expanded until it is required;
9351 see [temp.inst]. */
9352 DECL_INITIAL (r) = NULL_TREE;
9353 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9354 SET_DECL_RTL (r, NULL_RTX);
9355 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
9356 if (TREE_CODE (r) == VAR_DECL)
9357 {
9358 /* Possibly limit visibility based on template args. */
9359 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9360 if (DECL_VISIBILITY_SPECIFIED (t))
9361 {
9362 DECL_VISIBILITY_SPECIFIED (r) = 0;
9363 DECL_ATTRIBUTES (r)
9364 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9365 }
9366 determine_visibility (r);
9367 }
9368
9369 if (!local_p)
9370 {
9371 /* A static data member declaration is always marked
9372 external when it is declared in-class, even if an
9373 initializer is present. We mimic the non-template
9374 processing here. */
9375 DECL_EXTERNAL (r) = 1;
9376
9377 register_specialization (r, gen_tmpl, argvec, false, hash);
9378 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
9379 SET_DECL_IMPLICIT_INSTANTIATION (r);
9380 }
9381 else if (cp_unevaluated_operand)
9382 {
9383 /* We're substituting this var in a decltype outside of its
9384 scope, such as for a lambda return type. Don't add it to
9385 local_specializations, do perform auto deduction. */
9386 tree auto_node = type_uses_auto (type);
9387 tree init
9388 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
9389 /*constant_expression_p=*/false);
9390
9391 if (auto_node && init && describable_type (init))
9392 {
9393 type = do_auto_deduction (type, init, auto_node);
9394 TREE_TYPE (r) = type;
9395 }
9396 }
9397 else
9398 register_local_specialization (r, t);
9399
9400 TREE_CHAIN (r) = NULL_TREE;
9401
9402 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
9403 /*flags=*/0,
9404 args, complain, in_decl);
9405
9406 /* Preserve a typedef that names a type. */
9407 if (is_typedef_decl (r))
9408 {
9409 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
9410 set_underlying_type (r);
9411 }
9412
9413 layout_decl (r, 0);
9414 }
9415 break;
9416
9417 default:
9418 gcc_unreachable ();
9419 }
9420 #undef RETURN
9421
9422 out:
9423 /* Restore the file and line information. */
9424 input_location = saved_loc;
9425
9426 return r;
9427 }
9428
9429 /* Substitute into the ARG_TYPES of a function type. */
9430
9431 static tree
9432 tsubst_arg_types (tree arg_types,
9433 tree args,
9434 tsubst_flags_t complain,
9435 tree in_decl)
9436 {
9437 tree remaining_arg_types;
9438 tree type = NULL_TREE;
9439 int i = 1;
9440 tree expanded_args = NULL_TREE;
9441 tree default_arg;
9442
9443 if (!arg_types || arg_types == void_list_node)
9444 return arg_types;
9445
9446 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
9447 args, complain, in_decl);
9448 if (remaining_arg_types == error_mark_node)
9449 return error_mark_node;
9450
9451 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
9452 {
9453 /* For a pack expansion, perform substitution on the
9454 entire expression. Later on, we'll handle the arguments
9455 one-by-one. */
9456 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
9457 args, complain, in_decl);
9458
9459 if (TREE_CODE (expanded_args) == TREE_VEC)
9460 /* So that we'll spin through the parameters, one by one. */
9461 i = TREE_VEC_LENGTH (expanded_args);
9462 else
9463 {
9464 /* We only partially substituted into the parameter
9465 pack. Our type is TYPE_PACK_EXPANSION. */
9466 type = expanded_args;
9467 expanded_args = NULL_TREE;
9468 }
9469 }
9470
9471 while (i > 0) {
9472 --i;
9473
9474 if (expanded_args)
9475 type = TREE_VEC_ELT (expanded_args, i);
9476 else if (!type)
9477 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
9478
9479 if (type == error_mark_node)
9480 return error_mark_node;
9481 if (VOID_TYPE_P (type))
9482 {
9483 if (complain & tf_error)
9484 {
9485 error ("invalid parameter type %qT", type);
9486 if (in_decl)
9487 error ("in declaration %q+D", in_decl);
9488 }
9489 return error_mark_node;
9490 }
9491
9492 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9493 top-level qualifiers as required. */
9494 type = TYPE_MAIN_VARIANT (type_decays_to (type));
9495
9496 /* We do not substitute into default arguments here. The standard
9497 mandates that they be instantiated only when needed, which is
9498 done in build_over_call. */
9499 default_arg = TREE_PURPOSE (arg_types);
9500
9501 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
9502 {
9503 /* We've instantiated a template before its default arguments
9504 have been parsed. This can happen for a nested template
9505 class, and is not an error unless we require the default
9506 argument in a call of this function. */
9507 remaining_arg_types =
9508 tree_cons (default_arg, type, remaining_arg_types);
9509 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
9510 remaining_arg_types);
9511 }
9512 else
9513 remaining_arg_types =
9514 hash_tree_cons (default_arg, type, remaining_arg_types);
9515 }
9516
9517 return remaining_arg_types;
9518 }
9519
9520 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9521 *not* handle the exception-specification for FNTYPE, because the
9522 initial substitution of explicitly provided template parameters
9523 during argument deduction forbids substitution into the
9524 exception-specification:
9525
9526 [temp.deduct]
9527
9528 All references in the function type of the function template to the
9529 corresponding template parameters are replaced by the specified tem-
9530 plate argument values. If a substitution in a template parameter or
9531 in the function type of the function template results in an invalid
9532 type, type deduction fails. [Note: The equivalent substitution in
9533 exception specifications is done only when the function is instanti-
9534 ated, at which point a program is ill-formed if the substitution
9535 results in an invalid type.] */
9536
9537 static tree
9538 tsubst_function_type (tree t,
9539 tree args,
9540 tsubst_flags_t complain,
9541 tree in_decl)
9542 {
9543 tree return_type;
9544 tree arg_types;
9545 tree fntype;
9546
9547 /* The TYPE_CONTEXT is not used for function/method types. */
9548 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
9549
9550 /* Substitute the return type. */
9551 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9552 if (return_type == error_mark_node)
9553 return error_mark_node;
9554 /* The standard does not presently indicate that creation of a
9555 function type with an invalid return type is a deduction failure.
9556 However, that is clearly analogous to creating an array of "void"
9557 or a reference to a reference. This is core issue #486. */
9558 if (TREE_CODE (return_type) == ARRAY_TYPE
9559 || TREE_CODE (return_type) == FUNCTION_TYPE)
9560 {
9561 if (complain & tf_error)
9562 {
9563 if (TREE_CODE (return_type) == ARRAY_TYPE)
9564 error ("function returning an array");
9565 else
9566 error ("function returning a function");
9567 }
9568 return error_mark_node;
9569 }
9570
9571 /* Substitute the argument types. */
9572 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
9573 complain, in_decl);
9574 if (arg_types == error_mark_node)
9575 return error_mark_node;
9576
9577 /* Construct a new type node and return it. */
9578 if (TREE_CODE (t) == FUNCTION_TYPE)
9579 fntype = build_function_type (return_type, arg_types);
9580 else
9581 {
9582 tree r = TREE_TYPE (TREE_VALUE (arg_types));
9583 if (! MAYBE_CLASS_TYPE_P (r))
9584 {
9585 /* [temp.deduct]
9586
9587 Type deduction may fail for any of the following
9588 reasons:
9589
9590 -- Attempting to create "pointer to member of T" when T
9591 is not a class type. */
9592 if (complain & tf_error)
9593 error ("creating pointer to member function of non-class type %qT",
9594 r);
9595 return error_mark_node;
9596 }
9597
9598 fntype = build_method_type_directly (r, return_type,
9599 TREE_CHAIN (arg_types));
9600 }
9601 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
9602 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
9603
9604 return fntype;
9605 }
9606
9607 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9608 ARGS into that specification, and return the substituted
9609 specification. If there is no specification, return NULL_TREE. */
9610
9611 static tree
9612 tsubst_exception_specification (tree fntype,
9613 tree args,
9614 tsubst_flags_t complain,
9615 tree in_decl)
9616 {
9617 tree specs;
9618 tree new_specs;
9619
9620 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9621 new_specs = NULL_TREE;
9622 if (specs)
9623 {
9624 if (! TREE_VALUE (specs))
9625 new_specs = specs;
9626 else
9627 while (specs)
9628 {
9629 tree spec;
9630 int i, len = 1;
9631 tree expanded_specs = NULL_TREE;
9632
9633 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9634 {
9635 /* Expand the pack expansion type. */
9636 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9637 args, complain,
9638 in_decl);
9639
9640 if (expanded_specs == error_mark_node)
9641 return error_mark_node;
9642 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9643 len = TREE_VEC_LENGTH (expanded_specs);
9644 else
9645 {
9646 /* We're substituting into a member template, so
9647 we got a TYPE_PACK_EXPANSION back. Add that
9648 expansion and move on. */
9649 gcc_assert (TREE_CODE (expanded_specs)
9650 == TYPE_PACK_EXPANSION);
9651 new_specs = add_exception_specifier (new_specs,
9652 expanded_specs,
9653 complain);
9654 specs = TREE_CHAIN (specs);
9655 continue;
9656 }
9657 }
9658
9659 for (i = 0; i < len; ++i)
9660 {
9661 if (expanded_specs)
9662 spec = TREE_VEC_ELT (expanded_specs, i);
9663 else
9664 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9665 if (spec == error_mark_node)
9666 return spec;
9667 new_specs = add_exception_specifier (new_specs, spec,
9668 complain);
9669 }
9670
9671 specs = TREE_CHAIN (specs);
9672 }
9673 }
9674 return new_specs;
9675 }
9676
9677 /* Take the tree structure T and replace template parameters used
9678 therein with the argument vector ARGS. IN_DECL is an associated
9679 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9680 Issue error and warning messages under control of COMPLAIN. Note
9681 that we must be relatively non-tolerant of extensions here, in
9682 order to preserve conformance; if we allow substitutions that
9683 should not be allowed, we may allow argument deductions that should
9684 not succeed, and therefore report ambiguous overload situations
9685 where there are none. In theory, we could allow the substitution,
9686 but indicate that it should have failed, and allow our caller to
9687 make sure that the right thing happens, but we don't try to do this
9688 yet.
9689
9690 This function is used for dealing with types, decls and the like;
9691 for expressions, use tsubst_expr or tsubst_copy. */
9692
9693 tree
9694 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9695 {
9696 tree type, r;
9697
9698 if (t == NULL_TREE || t == error_mark_node
9699 || t == integer_type_node
9700 || t == void_type_node
9701 || t == char_type_node
9702 || t == unknown_type_node
9703 || TREE_CODE (t) == NAMESPACE_DECL)
9704 return t;
9705
9706 if (DECL_P (t))
9707 return tsubst_decl (t, args, complain);
9708
9709 if (args == NULL_TREE)
9710 return t;
9711
9712 if (TREE_CODE (t) == IDENTIFIER_NODE)
9713 type = IDENTIFIER_TYPE_VALUE (t);
9714 else
9715 type = TREE_TYPE (t);
9716
9717 gcc_assert (type != unknown_type_node);
9718
9719 /* Reuse typedefs. We need to do this to handle dependent attributes,
9720 such as attribute aligned. */
9721 if (TYPE_P (t)
9722 && TYPE_NAME (t)
9723 && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
9724 {
9725 tree decl = TYPE_NAME (t);
9726
9727 if (DECL_CLASS_SCOPE_P (decl)
9728 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
9729 && uses_template_parms (DECL_CONTEXT (decl)))
9730 {
9731 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
9732 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
9733 r = retrieve_specialization (tmpl, gen_args, 0);
9734 }
9735 else if (DECL_FUNCTION_SCOPE_P (decl)
9736 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
9737 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
9738 r = retrieve_local_specialization (decl);
9739 else
9740 /* The typedef is from a non-template context. */
9741 return t;
9742
9743 if (r)
9744 {
9745 r = TREE_TYPE (r);
9746 r = cp_build_qualified_type_real
9747 (r, cp_type_quals (t) | cp_type_quals (r),
9748 complain | tf_ignore_bad_quals);
9749 return r;
9750 }
9751 /* Else we must be instantiating the typedef, so fall through. */
9752 }
9753
9754 if (type
9755 && TREE_CODE (t) != TYPENAME_TYPE
9756 && TREE_CODE (t) != IDENTIFIER_NODE
9757 && TREE_CODE (t) != FUNCTION_TYPE
9758 && TREE_CODE (t) != METHOD_TYPE)
9759 type = tsubst (type, args, complain, in_decl);
9760 if (type == error_mark_node)
9761 return error_mark_node;
9762
9763 switch (TREE_CODE (t))
9764 {
9765 case RECORD_TYPE:
9766 case UNION_TYPE:
9767 case ENUMERAL_TYPE:
9768 return tsubst_aggr_type (t, args, complain, in_decl,
9769 /*entering_scope=*/0);
9770
9771 case ERROR_MARK:
9772 case IDENTIFIER_NODE:
9773 case VOID_TYPE:
9774 case REAL_TYPE:
9775 case COMPLEX_TYPE:
9776 case VECTOR_TYPE:
9777 case BOOLEAN_TYPE:
9778 case INTEGER_CST:
9779 case REAL_CST:
9780 case STRING_CST:
9781 return t;
9782
9783 case INTEGER_TYPE:
9784 if (t == integer_type_node)
9785 return t;
9786
9787 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
9788 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
9789 return t;
9790
9791 {
9792 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
9793
9794 max = tsubst_expr (omax, args, complain, in_decl,
9795 /*integral_constant_expression_p=*/false);
9796
9797 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
9798 needed. */
9799 if (TREE_CODE (max) == NOP_EXPR
9800 && TREE_SIDE_EFFECTS (omax)
9801 && !TREE_TYPE (max))
9802 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
9803
9804 max = fold_decl_constant_value (max);
9805
9806 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
9807 with TREE_SIDE_EFFECTS that indicates this is not an integral
9808 constant expression. */
9809 if (processing_template_decl
9810 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
9811 {
9812 gcc_assert (TREE_CODE (max) == NOP_EXPR);
9813 TREE_SIDE_EFFECTS (max) = 1;
9814 }
9815
9816 if (TREE_CODE (max) != INTEGER_CST
9817 && !at_function_scope_p ()
9818 && !TREE_SIDE_EFFECTS (max)
9819 && !value_dependent_expression_p (max))
9820 {
9821 if (complain & tf_error)
9822 error ("array bound is not an integer constant");
9823 return error_mark_node;
9824 }
9825
9826 /* [temp.deduct]
9827
9828 Type deduction may fail for any of the following
9829 reasons:
9830
9831 Attempting to create an array with a size that is
9832 zero or negative. */
9833 if (integer_zerop (max) && !(complain & tf_error))
9834 /* We must fail if performing argument deduction (as
9835 indicated by the state of complain), so that
9836 another substitution can be found. */
9837 return error_mark_node;
9838 else if (TREE_CODE (max) == INTEGER_CST
9839 && INT_CST_LT (max, integer_zero_node))
9840 {
9841 if (complain & tf_error)
9842 error ("creating array with negative size (%qE)", max);
9843
9844 return error_mark_node;
9845 }
9846
9847 return compute_array_index_type (NULL_TREE, max);
9848 }
9849
9850 case TEMPLATE_TYPE_PARM:
9851 case TEMPLATE_TEMPLATE_PARM:
9852 case BOUND_TEMPLATE_TEMPLATE_PARM:
9853 case TEMPLATE_PARM_INDEX:
9854 {
9855 int idx;
9856 int level;
9857 int levels;
9858 tree arg = NULL_TREE;
9859
9860 r = NULL_TREE;
9861
9862 gcc_assert (TREE_VEC_LENGTH (args) > 0);
9863 template_parm_level_and_index (t, &level, &idx);
9864
9865 levels = TMPL_ARGS_DEPTH (args);
9866 if (level <= levels)
9867 {
9868 arg = TMPL_ARG (args, level, idx);
9869
9870 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
9871 /* See through ARGUMENT_PACK_SELECT arguments. */
9872 arg = ARGUMENT_PACK_SELECT_ARG (arg);
9873 }
9874
9875 if (arg == error_mark_node)
9876 return error_mark_node;
9877 else if (arg != NULL_TREE)
9878 {
9879 if (ARGUMENT_PACK_P (arg))
9880 /* If ARG is an argument pack, we don't actually want to
9881 perform a substitution here, because substitutions
9882 for argument packs are only done
9883 element-by-element. We can get to this point when
9884 substituting the type of a non-type template
9885 parameter pack, when that type actually contains
9886 template parameter packs from an outer template, e.g.,
9887
9888 template<typename... Types> struct A {
9889 template<Types... Values> struct B { };
9890 }; */
9891 return t;
9892
9893 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
9894 {
9895 int quals;
9896 gcc_assert (TYPE_P (arg));
9897
9898 /* cv-quals from the template are discarded when
9899 substituting in a function or reference type. */
9900 if (TREE_CODE (arg) == FUNCTION_TYPE
9901 || TREE_CODE (arg) == METHOD_TYPE
9902 || TREE_CODE (arg) == REFERENCE_TYPE)
9903 quals = cp_type_quals (arg);
9904 else
9905 quals = cp_type_quals (arg) | cp_type_quals (t);
9906
9907 return cp_build_qualified_type_real
9908 (arg, quals, complain | tf_ignore_bad_quals);
9909 }
9910 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9911 {
9912 /* We are processing a type constructed from a
9913 template template parameter. */
9914 tree argvec = tsubst (TYPE_TI_ARGS (t),
9915 args, complain, in_decl);
9916 if (argvec == error_mark_node)
9917 return error_mark_node;
9918
9919 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
9920 are resolving nested-types in the signature of a
9921 member function templates. Otherwise ARG is a
9922 TEMPLATE_DECL and is the real template to be
9923 instantiated. */
9924 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
9925 arg = TYPE_NAME (arg);
9926
9927 r = lookup_template_class (arg,
9928 argvec, in_decl,
9929 DECL_CONTEXT (arg),
9930 /*entering_scope=*/0,
9931 complain);
9932 return cp_build_qualified_type_real
9933 (r, TYPE_QUALS (t), complain);
9934 }
9935 else
9936 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
9937 return arg;
9938 }
9939
9940 if (level == 1)
9941 /* This can happen during the attempted tsubst'ing in
9942 unify. This means that we don't yet have any information
9943 about the template parameter in question. */
9944 return t;
9945
9946 /* If we get here, we must have been looking at a parm for a
9947 more deeply nested template. Make a new version of this
9948 template parameter, but with a lower level. */
9949 switch (TREE_CODE (t))
9950 {
9951 case TEMPLATE_TYPE_PARM:
9952 case TEMPLATE_TEMPLATE_PARM:
9953 case BOUND_TEMPLATE_TEMPLATE_PARM:
9954 if (cp_type_quals (t))
9955 {
9956 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
9957 r = cp_build_qualified_type_real
9958 (r, cp_type_quals (t),
9959 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
9960 ? tf_ignore_bad_quals : 0));
9961 }
9962 else
9963 {
9964 r = copy_type (t);
9965 TEMPLATE_TYPE_PARM_INDEX (r)
9966 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
9967 r, levels, args, complain);
9968 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
9969 TYPE_MAIN_VARIANT (r) = r;
9970 TYPE_POINTER_TO (r) = NULL_TREE;
9971 TYPE_REFERENCE_TO (r) = NULL_TREE;
9972
9973 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
9974 /* We have reduced the level of the template
9975 template parameter, but not the levels of its
9976 template parameters, so canonical_type_parameter
9977 will not be able to find the canonical template
9978 template parameter for this level. Thus, we
9979 require structural equality checking to compare
9980 TEMPLATE_TEMPLATE_PARMs. */
9981 SET_TYPE_STRUCTURAL_EQUALITY (r);
9982 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
9983 SET_TYPE_STRUCTURAL_EQUALITY (r);
9984 else
9985 TYPE_CANONICAL (r) = canonical_type_parameter (r);
9986
9987 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9988 {
9989 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
9990 complain, in_decl);
9991 if (argvec == error_mark_node)
9992 return error_mark_node;
9993
9994 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
9995 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
9996 }
9997 }
9998 break;
9999
10000 case TEMPLATE_PARM_INDEX:
10001 r = reduce_template_parm_level (t, type, levels, args, complain);
10002 break;
10003
10004 default:
10005 gcc_unreachable ();
10006 }
10007
10008 return r;
10009 }
10010
10011 case TREE_LIST:
10012 {
10013 tree purpose, value, chain;
10014
10015 if (t == void_list_node)
10016 return t;
10017
10018 purpose = TREE_PURPOSE (t);
10019 if (purpose)
10020 {
10021 purpose = tsubst (purpose, args, complain, in_decl);
10022 if (purpose == error_mark_node)
10023 return error_mark_node;
10024 }
10025 value = TREE_VALUE (t);
10026 if (value)
10027 {
10028 value = tsubst (value, args, complain, in_decl);
10029 if (value == error_mark_node)
10030 return error_mark_node;
10031 }
10032 chain = TREE_CHAIN (t);
10033 if (chain && chain != void_type_node)
10034 {
10035 chain = tsubst (chain, args, complain, in_decl);
10036 if (chain == error_mark_node)
10037 return error_mark_node;
10038 }
10039 if (purpose == TREE_PURPOSE (t)
10040 && value == TREE_VALUE (t)
10041 && chain == TREE_CHAIN (t))
10042 return t;
10043 return hash_tree_cons (purpose, value, chain);
10044 }
10045
10046 case TREE_BINFO:
10047 /* We should never be tsubsting a binfo. */
10048 gcc_unreachable ();
10049
10050 case TREE_VEC:
10051 /* A vector of template arguments. */
10052 gcc_assert (!type);
10053 return tsubst_template_args (t, args, complain, in_decl);
10054
10055 case POINTER_TYPE:
10056 case REFERENCE_TYPE:
10057 {
10058 enum tree_code code;
10059
10060 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
10061 return t;
10062
10063 code = TREE_CODE (t);
10064
10065
10066 /* [temp.deduct]
10067
10068 Type deduction may fail for any of the following
10069 reasons:
10070
10071 -- Attempting to create a pointer to reference type.
10072 -- Attempting to create a reference to a reference type or
10073 a reference to void.
10074
10075 Core issue 106 says that creating a reference to a reference
10076 during instantiation is no longer a cause for failure. We
10077 only enforce this check in strict C++98 mode. */
10078 if ((TREE_CODE (type) == REFERENCE_TYPE
10079 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
10080 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
10081 {
10082 static location_t last_loc;
10083
10084 /* We keep track of the last time we issued this error
10085 message to avoid spewing a ton of messages during a
10086 single bad template instantiation. */
10087 if (complain & tf_error
10088 && last_loc != input_location)
10089 {
10090 if (TREE_CODE (type) == VOID_TYPE)
10091 error ("forming reference to void");
10092 else if (code == POINTER_TYPE)
10093 error ("forming pointer to reference type %qT", type);
10094 else
10095 error ("forming reference to reference type %qT", type);
10096 last_loc = input_location;
10097 }
10098
10099 return error_mark_node;
10100 }
10101 else if (code == POINTER_TYPE)
10102 {
10103 r = build_pointer_type (type);
10104 if (TREE_CODE (type) == METHOD_TYPE)
10105 r = build_ptrmemfunc_type (r);
10106 }
10107 else if (TREE_CODE (type) == REFERENCE_TYPE)
10108 /* In C++0x, during template argument substitution, when there is an
10109 attempt to create a reference to a reference type, reference
10110 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10111
10112 "If a template-argument for a template-parameter T names a type
10113 that is a reference to a type A, an attempt to create the type
10114 'lvalue reference to cv T' creates the type 'lvalue reference to
10115 A,' while an attempt to create the type type rvalue reference to
10116 cv T' creates the type T"
10117 */
10118 r = cp_build_reference_type
10119 (TREE_TYPE (type),
10120 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
10121 else
10122 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
10123 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
10124
10125 if (r != error_mark_node)
10126 /* Will this ever be needed for TYPE_..._TO values? */
10127 layout_type (r);
10128
10129 return r;
10130 }
10131 case OFFSET_TYPE:
10132 {
10133 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
10134 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
10135 {
10136 /* [temp.deduct]
10137
10138 Type deduction may fail for any of the following
10139 reasons:
10140
10141 -- Attempting to create "pointer to member of T" when T
10142 is not a class type. */
10143 if (complain & tf_error)
10144 error ("creating pointer to member of non-class type %qT", r);
10145 return error_mark_node;
10146 }
10147 if (TREE_CODE (type) == REFERENCE_TYPE)
10148 {
10149 if (complain & tf_error)
10150 error ("creating pointer to member reference type %qT", type);
10151 return error_mark_node;
10152 }
10153 if (TREE_CODE (type) == VOID_TYPE)
10154 {
10155 if (complain & tf_error)
10156 error ("creating pointer to member of type void");
10157 return error_mark_node;
10158 }
10159 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10160 if (TREE_CODE (type) == FUNCTION_TYPE)
10161 {
10162 /* The type of the implicit object parameter gets its
10163 cv-qualifiers from the FUNCTION_TYPE. */
10164 tree memptr;
10165 tree method_type = build_memfn_type (type, r, cp_type_quals (type));
10166 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
10167 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
10168 complain);
10169 }
10170 else
10171 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
10172 TYPE_QUALS (t),
10173 complain);
10174 }
10175 case FUNCTION_TYPE:
10176 case METHOD_TYPE:
10177 {
10178 tree fntype;
10179 tree specs;
10180 fntype = tsubst_function_type (t, args, complain, in_decl);
10181 if (fntype == error_mark_node)
10182 return error_mark_node;
10183
10184 /* Substitute the exception specification. */
10185 specs = tsubst_exception_specification (t, args, complain,
10186 in_decl);
10187 if (specs == error_mark_node)
10188 return error_mark_node;
10189 if (specs)
10190 fntype = build_exception_variant (fntype, specs);
10191 return fntype;
10192 }
10193 case ARRAY_TYPE:
10194 {
10195 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
10196 if (domain == error_mark_node)
10197 return error_mark_node;
10198
10199 /* As an optimization, we avoid regenerating the array type if
10200 it will obviously be the same as T. */
10201 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
10202 return t;
10203
10204 /* These checks should match the ones in grokdeclarator.
10205
10206 [temp.deduct]
10207
10208 The deduction may fail for any of the following reasons:
10209
10210 -- Attempting to create an array with an element type that
10211 is void, a function type, or a reference type, or [DR337]
10212 an abstract class type. */
10213 if (TREE_CODE (type) == VOID_TYPE
10214 || TREE_CODE (type) == FUNCTION_TYPE
10215 || TREE_CODE (type) == REFERENCE_TYPE)
10216 {
10217 if (complain & tf_error)
10218 error ("creating array of %qT", type);
10219 return error_mark_node;
10220 }
10221 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10222 {
10223 if (complain & tf_error)
10224 error ("creating array of %qT, which is an abstract class type",
10225 type);
10226 return error_mark_node;
10227 }
10228
10229 r = build_cplus_array_type (type, domain);
10230
10231 if (TYPE_USER_ALIGN (t))
10232 {
10233 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10234 TYPE_USER_ALIGN (r) = 1;
10235 }
10236
10237 return r;
10238 }
10239
10240 case PLUS_EXPR:
10241 case MINUS_EXPR:
10242 {
10243 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10244 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10245
10246 if (e1 == error_mark_node || e2 == error_mark_node)
10247 return error_mark_node;
10248
10249 return fold_build2_loc (input_location,
10250 TREE_CODE (t), TREE_TYPE (t), e1, e2);
10251 }
10252
10253 case NEGATE_EXPR:
10254 case NOP_EXPR:
10255 {
10256 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10257 if (e == error_mark_node)
10258 return error_mark_node;
10259
10260 return fold_build1_loc (input_location, TREE_CODE (t), TREE_TYPE (t), e);
10261 }
10262
10263 case TYPENAME_TYPE:
10264 {
10265 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10266 in_decl, /*entering_scope=*/1);
10267 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10268 complain, in_decl);
10269
10270 if (ctx == error_mark_node || f == error_mark_node)
10271 return error_mark_node;
10272
10273 if (!MAYBE_CLASS_TYPE_P (ctx))
10274 {
10275 if (complain & tf_error)
10276 error ("%qT is not a class, struct, or union type", ctx);
10277 return error_mark_node;
10278 }
10279 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
10280 {
10281 /* Normally, make_typename_type does not require that the CTX
10282 have complete type in order to allow things like:
10283
10284 template <class T> struct S { typename S<T>::X Y; };
10285
10286 But, such constructs have already been resolved by this
10287 point, so here CTX really should have complete type, unless
10288 it's a partial instantiation. */
10289 if (!(complain & tf_no_class_instantiations))
10290 ctx = complete_type (ctx);
10291 if (!COMPLETE_TYPE_P (ctx))
10292 {
10293 if (complain & tf_error)
10294 cxx_incomplete_type_error (NULL_TREE, ctx);
10295 return error_mark_node;
10296 }
10297 }
10298
10299 f = make_typename_type (ctx, f, typename_type,
10300 (complain & tf_error) | tf_keep_type_decl);
10301 if (f == error_mark_node)
10302 return f;
10303 if (TREE_CODE (f) == TYPE_DECL)
10304 {
10305 complain |= tf_ignore_bad_quals;
10306 f = TREE_TYPE (f);
10307 }
10308
10309 if (TREE_CODE (f) != TYPENAME_TYPE)
10310 {
10311 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
10312 error ("%qT resolves to %qT, which is not an enumeration type",
10313 t, f);
10314 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
10315 error ("%qT resolves to %qT, which is is not a class type",
10316 t, f);
10317 }
10318
10319 return cp_build_qualified_type_real
10320 (f, cp_type_quals (f) | cp_type_quals (t), complain);
10321 }
10322
10323 case UNBOUND_CLASS_TEMPLATE:
10324 {
10325 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10326 in_decl, /*entering_scope=*/1);
10327 tree name = TYPE_IDENTIFIER (t);
10328 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
10329
10330 if (ctx == error_mark_node || name == error_mark_node)
10331 return error_mark_node;
10332
10333 if (parm_list)
10334 parm_list = tsubst_template_parms (parm_list, args, complain);
10335 return make_unbound_class_template (ctx, name, parm_list, complain);
10336 }
10337
10338 case INDIRECT_REF:
10339 case ADDR_EXPR:
10340 case CALL_EXPR:
10341 gcc_unreachable ();
10342
10343 case ARRAY_REF:
10344 {
10345 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10346 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
10347 /*integral_constant_expression_p=*/false);
10348 if (e1 == error_mark_node || e2 == error_mark_node)
10349 return error_mark_node;
10350
10351 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
10352 }
10353
10354 case SCOPE_REF:
10355 {
10356 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10357 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10358 if (e1 == error_mark_node || e2 == error_mark_node)
10359 return error_mark_node;
10360
10361 return build_qualified_name (/*type=*/NULL_TREE,
10362 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
10363 }
10364
10365 case TYPEOF_TYPE:
10366 {
10367 tree type;
10368
10369 ++cp_unevaluated_operand;
10370 ++c_inhibit_evaluation_warnings;
10371
10372 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
10373 complain, in_decl,
10374 /*integral_constant_expression_p=*/false);
10375
10376 --cp_unevaluated_operand;
10377 --c_inhibit_evaluation_warnings;
10378
10379 type = finish_typeof (type);
10380 return cp_build_qualified_type_real (type,
10381 cp_type_quals (t)
10382 | cp_type_quals (type),
10383 complain);
10384 }
10385
10386 case DECLTYPE_TYPE:
10387 {
10388 tree type;
10389
10390 ++cp_unevaluated_operand;
10391 ++c_inhibit_evaluation_warnings;
10392
10393 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
10394 complain, in_decl,
10395 /*integral_constant_expression_p=*/false);
10396
10397 --cp_unevaluated_operand;
10398 --c_inhibit_evaluation_warnings;
10399
10400 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
10401 type = lambda_capture_field_type (type);
10402 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
10403 type = lambda_return_type (type);
10404 else
10405 type = finish_decltype_type
10406 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
10407 return cp_build_qualified_type_real (type,
10408 cp_type_quals (t)
10409 | cp_type_quals (type),
10410 complain);
10411 }
10412
10413 case TYPE_ARGUMENT_PACK:
10414 case NONTYPE_ARGUMENT_PACK:
10415 {
10416 tree r = TYPE_P (t)
10417 ? cxx_make_type (TREE_CODE (t))
10418 : make_node (TREE_CODE (t));
10419 tree packed_out =
10420 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
10421 args,
10422 complain,
10423 in_decl);
10424 SET_ARGUMENT_PACK_ARGS (r, packed_out);
10425
10426 /* For template nontype argument packs, also substitute into
10427 the type. */
10428 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
10429 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
10430
10431 return r;
10432 }
10433 break;
10434
10435 default:
10436 sorry ("use of %qs in template",
10437 tree_code_name [(int) TREE_CODE (t)]);
10438 return error_mark_node;
10439 }
10440 }
10441
10442 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10443 type of the expression on the left-hand side of the "." or "->"
10444 operator. */
10445
10446 static tree
10447 tsubst_baselink (tree baselink, tree object_type,
10448 tree args, tsubst_flags_t complain, tree in_decl)
10449 {
10450 tree name;
10451 tree qualifying_scope;
10452 tree fns;
10453 tree optype;
10454 tree template_args = 0;
10455 bool template_id_p = false;
10456
10457 /* A baselink indicates a function from a base class. Both the
10458 BASELINK_ACCESS_BINFO and the base class referenced may
10459 indicate bases of the template class, rather than the
10460 instantiated class. In addition, lookups that were not
10461 ambiguous before may be ambiguous now. Therefore, we perform
10462 the lookup again. */
10463 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
10464 qualifying_scope = tsubst (qualifying_scope, args,
10465 complain, in_decl);
10466 fns = BASELINK_FUNCTIONS (baselink);
10467 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
10468 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
10469 {
10470 template_id_p = true;
10471 template_args = TREE_OPERAND (fns, 1);
10472 fns = TREE_OPERAND (fns, 0);
10473 if (template_args)
10474 template_args = tsubst_template_args (template_args, args,
10475 complain, in_decl);
10476 }
10477 name = DECL_NAME (get_first_fn (fns));
10478 if (IDENTIFIER_TYPENAME_P (name))
10479 name = mangle_conv_op_name_for_type (optype);
10480 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
10481
10482 /* If lookup found a single function, mark it as used at this
10483 point. (If it lookup found multiple functions the one selected
10484 later by overload resolution will be marked as used at that
10485 point.) */
10486 if (BASELINK_P (baselink))
10487 fns = BASELINK_FUNCTIONS (baselink);
10488 if (!template_id_p && !really_overloaded_fn (fns))
10489 mark_used (OVL_CURRENT (fns));
10490
10491 /* Add back the template arguments, if present. */
10492 if (BASELINK_P (baselink) && template_id_p)
10493 BASELINK_FUNCTIONS (baselink)
10494 = build_nt (TEMPLATE_ID_EXPR,
10495 BASELINK_FUNCTIONS (baselink),
10496 template_args);
10497 /* Update the conversion operator type. */
10498 BASELINK_OPTYPE (baselink) = optype;
10499
10500 if (!object_type)
10501 object_type = current_class_type;
10502 return adjust_result_of_qualified_name_lookup (baselink,
10503 qualifying_scope,
10504 object_type);
10505 }
10506
10507 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10508 true if the qualified-id will be a postfix-expression in-and-of
10509 itself; false if more of the postfix-expression follows the
10510 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10511 of "&". */
10512
10513 static tree
10514 tsubst_qualified_id (tree qualified_id, tree args,
10515 tsubst_flags_t complain, tree in_decl,
10516 bool done, bool address_p)
10517 {
10518 tree expr;
10519 tree scope;
10520 tree name;
10521 bool is_template;
10522 tree template_args;
10523
10524 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
10525
10526 /* Figure out what name to look up. */
10527 name = TREE_OPERAND (qualified_id, 1);
10528 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
10529 {
10530 is_template = true;
10531 template_args = TREE_OPERAND (name, 1);
10532 if (template_args)
10533 template_args = tsubst_template_args (template_args, args,
10534 complain, in_decl);
10535 name = TREE_OPERAND (name, 0);
10536 }
10537 else
10538 {
10539 is_template = false;
10540 template_args = NULL_TREE;
10541 }
10542
10543 /* Substitute into the qualifying scope. When there are no ARGS, we
10544 are just trying to simplify a non-dependent expression. In that
10545 case the qualifying scope may be dependent, and, in any case,
10546 substituting will not help. */
10547 scope = TREE_OPERAND (qualified_id, 0);
10548 if (args)
10549 {
10550 scope = tsubst (scope, args, complain, in_decl);
10551 expr = tsubst_copy (name, args, complain, in_decl);
10552 }
10553 else
10554 expr = name;
10555
10556 if (dependent_type_p (scope))
10557 {
10558 tree type = NULL_TREE;
10559 if (DECL_P (expr) && !dependent_scope_p (scope))
10560 type = TREE_TYPE (expr);
10561 return build_qualified_name (type, scope, expr,
10562 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
10563 }
10564
10565 if (!BASELINK_P (name) && !DECL_P (expr))
10566 {
10567 if (TREE_CODE (expr) == BIT_NOT_EXPR)
10568 {
10569 /* A BIT_NOT_EXPR is used to represent a destructor. */
10570 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
10571 {
10572 error ("qualifying type %qT does not match destructor name ~%qT",
10573 scope, TREE_OPERAND (expr, 0));
10574 expr = error_mark_node;
10575 }
10576 else
10577 expr = lookup_qualified_name (scope, complete_dtor_identifier,
10578 /*is_type_p=*/0, false);
10579 }
10580 else
10581 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
10582 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
10583 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
10584 {
10585 if (complain & tf_error)
10586 {
10587 error ("dependent-name %qE is parsed as a non-type, but "
10588 "instantiation yields a type", qualified_id);
10589 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
10590 }
10591 return error_mark_node;
10592 }
10593 }
10594
10595 if (DECL_P (expr))
10596 {
10597 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
10598 scope);
10599 /* Remember that there was a reference to this entity. */
10600 mark_used (expr);
10601 }
10602
10603 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
10604 {
10605 if (complain & tf_error)
10606 qualified_name_lookup_error (scope,
10607 TREE_OPERAND (qualified_id, 1),
10608 expr, input_location);
10609 return error_mark_node;
10610 }
10611
10612 if (is_template)
10613 expr = lookup_template_function (expr, template_args);
10614
10615 if (expr == error_mark_node && complain & tf_error)
10616 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
10617 expr, input_location);
10618 else if (TYPE_P (scope))
10619 {
10620 expr = (adjust_result_of_qualified_name_lookup
10621 (expr, scope, current_class_type));
10622 expr = (finish_qualified_id_expr
10623 (scope, expr, done, address_p,
10624 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
10625 /*template_arg_p=*/false));
10626 }
10627
10628 /* Expressions do not generally have reference type. */
10629 if (TREE_CODE (expr) != SCOPE_REF
10630 /* However, if we're about to form a pointer-to-member, we just
10631 want the referenced member referenced. */
10632 && TREE_CODE (expr) != OFFSET_REF)
10633 expr = convert_from_reference (expr);
10634
10635 return expr;
10636 }
10637
10638 /* Like tsubst, but deals with expressions. This function just replaces
10639 template parms; to finish processing the resultant expression, use
10640 tsubst_expr. */
10641
10642 static tree
10643 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10644 {
10645 enum tree_code code;
10646 tree r;
10647
10648 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
10649 return t;
10650
10651 code = TREE_CODE (t);
10652
10653 switch (code)
10654 {
10655 case PARM_DECL:
10656 r = retrieve_local_specialization (t);
10657
10658 if (r == NULL)
10659 {
10660 tree c;
10661 /* This can happen for a parameter name used later in a function
10662 declaration (such as in a late-specified return type). Just
10663 make a dummy decl, since it's only used for its type. */
10664 gcc_assert (cp_unevaluated_operand != 0);
10665 /* We copy T because want to tsubst the PARM_DECL only,
10666 not the following PARM_DECLs that are chained to T. */
10667 c = copy_node (t);
10668 r = tsubst_decl (c, args, complain);
10669 /* Give it the template pattern as its context; its true context
10670 hasn't been instantiated yet and this is good enough for
10671 mangling. */
10672 DECL_CONTEXT (r) = DECL_CONTEXT (t);
10673 }
10674
10675 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10676 r = ARGUMENT_PACK_SELECT_ARG (r);
10677 mark_used (r);
10678 return r;
10679
10680 case CONST_DECL:
10681 {
10682 tree enum_type;
10683 tree v;
10684
10685 if (DECL_TEMPLATE_PARM_P (t))
10686 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
10687 /* There is no need to substitute into namespace-scope
10688 enumerators. */
10689 if (DECL_NAMESPACE_SCOPE_P (t))
10690 return t;
10691 /* If ARGS is NULL, then T is known to be non-dependent. */
10692 if (args == NULL_TREE)
10693 return integral_constant_value (t);
10694
10695 /* Unfortunately, we cannot just call lookup_name here.
10696 Consider:
10697
10698 template <int I> int f() {
10699 enum E { a = I };
10700 struct S { void g() { E e = a; } };
10701 };
10702
10703 When we instantiate f<7>::S::g(), say, lookup_name is not
10704 clever enough to find f<7>::a. */
10705 enum_type
10706 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
10707 /*entering_scope=*/0);
10708
10709 for (v = TYPE_VALUES (enum_type);
10710 v != NULL_TREE;
10711 v = TREE_CHAIN (v))
10712 if (TREE_PURPOSE (v) == DECL_NAME (t))
10713 return TREE_VALUE (v);
10714
10715 /* We didn't find the name. That should never happen; if
10716 name-lookup found it during preliminary parsing, we
10717 should find it again here during instantiation. */
10718 gcc_unreachable ();
10719 }
10720 return t;
10721
10722 case FIELD_DECL:
10723 if (DECL_CONTEXT (t))
10724 {
10725 tree ctx;
10726
10727 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
10728 /*entering_scope=*/1);
10729 if (ctx != DECL_CONTEXT (t))
10730 {
10731 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
10732 if (!r)
10733 {
10734 if (complain & tf_error)
10735 error ("using invalid field %qD", t);
10736 return error_mark_node;
10737 }
10738 return r;
10739 }
10740 }
10741
10742 return t;
10743
10744 case VAR_DECL:
10745 case FUNCTION_DECL:
10746 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10747 || local_variable_p (t))
10748 t = tsubst (t, args, complain, in_decl);
10749 mark_used (t);
10750 return t;
10751
10752 case BASELINK:
10753 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
10754
10755 case TEMPLATE_DECL:
10756 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10757 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
10758 args, complain, in_decl);
10759 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
10760 return tsubst (t, args, complain, in_decl);
10761 else if (DECL_CLASS_SCOPE_P (t)
10762 && uses_template_parms (DECL_CONTEXT (t)))
10763 {
10764 /* Template template argument like the following example need
10765 special treatment:
10766
10767 template <template <class> class TT> struct C {};
10768 template <class T> struct D {
10769 template <class U> struct E {};
10770 C<E> c; // #1
10771 };
10772 D<int> d; // #2
10773
10774 We are processing the template argument `E' in #1 for
10775 the template instantiation #2. Originally, `E' is a
10776 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10777 have to substitute this with one having context `D<int>'. */
10778
10779 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
10780 return lookup_field (context, DECL_NAME(t), 0, false);
10781 }
10782 else
10783 /* Ordinary template template argument. */
10784 return t;
10785
10786 case CAST_EXPR:
10787 case REINTERPRET_CAST_EXPR:
10788 case CONST_CAST_EXPR:
10789 case STATIC_CAST_EXPR:
10790 case DYNAMIC_CAST_EXPR:
10791 case NOP_EXPR:
10792 return build1
10793 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10794 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10795
10796 case SIZEOF_EXPR:
10797 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10798 {
10799 /* We only want to compute the number of arguments. */
10800 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10801 complain, in_decl);
10802 int len = 0;
10803
10804 if (TREE_CODE (expanded) == TREE_VEC)
10805 len = TREE_VEC_LENGTH (expanded);
10806
10807 if (expanded == error_mark_node)
10808 return error_mark_node;
10809 else if (PACK_EXPANSION_P (expanded)
10810 || (TREE_CODE (expanded) == TREE_VEC
10811 && len > 0
10812 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
10813 {
10814 if (TREE_CODE (expanded) == TREE_VEC)
10815 expanded = TREE_VEC_ELT (expanded, len - 1);
10816
10817 if (TYPE_P (expanded))
10818 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
10819 complain & tf_error);
10820 else
10821 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
10822 complain & tf_error);
10823 }
10824 else
10825 return build_int_cst (size_type_node, len);
10826 }
10827 /* Fall through */
10828
10829 case INDIRECT_REF:
10830 case NEGATE_EXPR:
10831 case TRUTH_NOT_EXPR:
10832 case BIT_NOT_EXPR:
10833 case ADDR_EXPR:
10834 case UNARY_PLUS_EXPR: /* Unary + */
10835 case ALIGNOF_EXPR:
10836 case ARROW_EXPR:
10837 case THROW_EXPR:
10838 case TYPEID_EXPR:
10839 case REALPART_EXPR:
10840 case IMAGPART_EXPR:
10841 return build1
10842 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10843 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10844
10845 case COMPONENT_REF:
10846 {
10847 tree object;
10848 tree name;
10849
10850 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
10851 name = TREE_OPERAND (t, 1);
10852 if (TREE_CODE (name) == BIT_NOT_EXPR)
10853 {
10854 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10855 complain, in_decl);
10856 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10857 }
10858 else if (TREE_CODE (name) == SCOPE_REF
10859 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
10860 {
10861 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
10862 complain, in_decl);
10863 name = TREE_OPERAND (name, 1);
10864 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10865 complain, in_decl);
10866 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10867 name = build_qualified_name (/*type=*/NULL_TREE,
10868 base, name,
10869 /*template_p=*/false);
10870 }
10871 else if (TREE_CODE (name) == BASELINK)
10872 name = tsubst_baselink (name,
10873 non_reference (TREE_TYPE (object)),
10874 args, complain,
10875 in_decl);
10876 else
10877 name = tsubst_copy (name, args, complain, in_decl);
10878 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
10879 }
10880
10881 case PLUS_EXPR:
10882 case MINUS_EXPR:
10883 case MULT_EXPR:
10884 case TRUNC_DIV_EXPR:
10885 case CEIL_DIV_EXPR:
10886 case FLOOR_DIV_EXPR:
10887 case ROUND_DIV_EXPR:
10888 case EXACT_DIV_EXPR:
10889 case BIT_AND_EXPR:
10890 case BIT_IOR_EXPR:
10891 case BIT_XOR_EXPR:
10892 case TRUNC_MOD_EXPR:
10893 case FLOOR_MOD_EXPR:
10894 case TRUTH_ANDIF_EXPR:
10895 case TRUTH_ORIF_EXPR:
10896 case TRUTH_AND_EXPR:
10897 case TRUTH_OR_EXPR:
10898 case RSHIFT_EXPR:
10899 case LSHIFT_EXPR:
10900 case RROTATE_EXPR:
10901 case LROTATE_EXPR:
10902 case EQ_EXPR:
10903 case NE_EXPR:
10904 case MAX_EXPR:
10905 case MIN_EXPR:
10906 case LE_EXPR:
10907 case GE_EXPR:
10908 case LT_EXPR:
10909 case GT_EXPR:
10910 case COMPOUND_EXPR:
10911 case DOTSTAR_EXPR:
10912 case MEMBER_REF:
10913 case PREDECREMENT_EXPR:
10914 case PREINCREMENT_EXPR:
10915 case POSTDECREMENT_EXPR:
10916 case POSTINCREMENT_EXPR:
10917 return build_nt
10918 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10919 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10920
10921 case SCOPE_REF:
10922 return build_qualified_name (/*type=*/NULL_TREE,
10923 tsubst_copy (TREE_OPERAND (t, 0),
10924 args, complain, in_decl),
10925 tsubst_copy (TREE_OPERAND (t, 1),
10926 args, complain, in_decl),
10927 QUALIFIED_NAME_IS_TEMPLATE (t));
10928
10929 case ARRAY_REF:
10930 return build_nt
10931 (ARRAY_REF,
10932 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10933 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10934 NULL_TREE, NULL_TREE);
10935
10936 case CALL_EXPR:
10937 {
10938 int n = VL_EXP_OPERAND_LENGTH (t);
10939 tree result = build_vl_exp (CALL_EXPR, n);
10940 int i;
10941 for (i = 0; i < n; i++)
10942 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
10943 complain, in_decl);
10944 return result;
10945 }
10946
10947 case COND_EXPR:
10948 case MODOP_EXPR:
10949 case PSEUDO_DTOR_EXPR:
10950 {
10951 r = build_nt
10952 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10953 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10954 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10955 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
10956 return r;
10957 }
10958
10959 case NEW_EXPR:
10960 {
10961 r = build_nt
10962 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10963 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10964 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10965 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
10966 return r;
10967 }
10968
10969 case DELETE_EXPR:
10970 {
10971 r = build_nt
10972 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10973 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10974 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
10975 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
10976 return r;
10977 }
10978
10979 case TEMPLATE_ID_EXPR:
10980 {
10981 /* Substituted template arguments */
10982 tree fn = TREE_OPERAND (t, 0);
10983 tree targs = TREE_OPERAND (t, 1);
10984
10985 fn = tsubst_copy (fn, args, complain, in_decl);
10986 if (targs)
10987 targs = tsubst_template_args (targs, args, complain, in_decl);
10988
10989 return lookup_template_function (fn, targs);
10990 }
10991
10992 case TREE_LIST:
10993 {
10994 tree purpose, value, chain;
10995
10996 if (t == void_list_node)
10997 return t;
10998
10999 purpose = TREE_PURPOSE (t);
11000 if (purpose)
11001 purpose = tsubst_copy (purpose, args, complain, in_decl);
11002 value = TREE_VALUE (t);
11003 if (value)
11004 value = tsubst_copy (value, args, complain, in_decl);
11005 chain = TREE_CHAIN (t);
11006 if (chain && chain != void_type_node)
11007 chain = tsubst_copy (chain, args, complain, in_decl);
11008 if (purpose == TREE_PURPOSE (t)
11009 && value == TREE_VALUE (t)
11010 && chain == TREE_CHAIN (t))
11011 return t;
11012 return tree_cons (purpose, value, chain);
11013 }
11014
11015 case RECORD_TYPE:
11016 case UNION_TYPE:
11017 case ENUMERAL_TYPE:
11018 case INTEGER_TYPE:
11019 case TEMPLATE_TYPE_PARM:
11020 case TEMPLATE_TEMPLATE_PARM:
11021 case BOUND_TEMPLATE_TEMPLATE_PARM:
11022 case TEMPLATE_PARM_INDEX:
11023 case POINTER_TYPE:
11024 case REFERENCE_TYPE:
11025 case OFFSET_TYPE:
11026 case FUNCTION_TYPE:
11027 case METHOD_TYPE:
11028 case ARRAY_TYPE:
11029 case TYPENAME_TYPE:
11030 case UNBOUND_CLASS_TEMPLATE:
11031 case TYPEOF_TYPE:
11032 case DECLTYPE_TYPE:
11033 case TYPE_DECL:
11034 return tsubst (t, args, complain, in_decl);
11035
11036 case IDENTIFIER_NODE:
11037 if (IDENTIFIER_TYPENAME_P (t))
11038 {
11039 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11040 return mangle_conv_op_name_for_type (new_type);
11041 }
11042 else
11043 return t;
11044
11045 case CONSTRUCTOR:
11046 /* This is handled by tsubst_copy_and_build. */
11047 gcc_unreachable ();
11048
11049 case VA_ARG_EXPR:
11050 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
11051 in_decl),
11052 tsubst (TREE_TYPE (t), args, complain, in_decl));
11053
11054 case CLEANUP_POINT_EXPR:
11055 /* We shouldn't have built any of these during initial template
11056 generation. Instead, they should be built during instantiation
11057 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11058 gcc_unreachable ();
11059
11060 case OFFSET_REF:
11061 mark_used (TREE_OPERAND (t, 1));
11062 return t;
11063
11064 case EXPR_PACK_EXPANSION:
11065 error ("invalid use of pack expansion expression");
11066 return error_mark_node;
11067
11068 case NONTYPE_ARGUMENT_PACK:
11069 error ("use %<...%> to expand argument pack");
11070 return error_mark_node;
11071
11072 default:
11073 return t;
11074 }
11075 }
11076
11077 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11078
11079 static tree
11080 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
11081 tree in_decl)
11082 {
11083 tree new_clauses = NULL, nc, oc;
11084
11085 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
11086 {
11087 nc = copy_node (oc);
11088 OMP_CLAUSE_CHAIN (nc) = new_clauses;
11089 new_clauses = nc;
11090
11091 switch (OMP_CLAUSE_CODE (nc))
11092 {
11093 case OMP_CLAUSE_LASTPRIVATE:
11094 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
11095 {
11096 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
11097 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
11098 in_decl, /*integral_constant_expression_p=*/false);
11099 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
11100 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
11101 }
11102 /* FALLTHRU */
11103 case OMP_CLAUSE_PRIVATE:
11104 case OMP_CLAUSE_SHARED:
11105 case OMP_CLAUSE_FIRSTPRIVATE:
11106 case OMP_CLAUSE_REDUCTION:
11107 case OMP_CLAUSE_COPYIN:
11108 case OMP_CLAUSE_COPYPRIVATE:
11109 case OMP_CLAUSE_IF:
11110 case OMP_CLAUSE_NUM_THREADS:
11111 case OMP_CLAUSE_SCHEDULE:
11112 case OMP_CLAUSE_COLLAPSE:
11113 OMP_CLAUSE_OPERAND (nc, 0)
11114 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
11115 in_decl, /*integral_constant_expression_p=*/false);
11116 break;
11117 case OMP_CLAUSE_NOWAIT:
11118 case OMP_CLAUSE_ORDERED:
11119 case OMP_CLAUSE_DEFAULT:
11120 case OMP_CLAUSE_UNTIED:
11121 break;
11122 default:
11123 gcc_unreachable ();
11124 }
11125 }
11126
11127 return finish_omp_clauses (nreverse (new_clauses));
11128 }
11129
11130 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11131
11132 static tree
11133 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
11134 tree in_decl)
11135 {
11136 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11137
11138 tree purpose, value, chain;
11139
11140 if (t == NULL)
11141 return t;
11142
11143 if (TREE_CODE (t) != TREE_LIST)
11144 return tsubst_copy_and_build (t, args, complain, in_decl,
11145 /*function_p=*/false,
11146 /*integral_constant_expression_p=*/false);
11147
11148 if (t == void_list_node)
11149 return t;
11150
11151 purpose = TREE_PURPOSE (t);
11152 if (purpose)
11153 purpose = RECUR (purpose);
11154 value = TREE_VALUE (t);
11155 if (value && TREE_CODE (value) != LABEL_DECL)
11156 value = RECUR (value);
11157 chain = TREE_CHAIN (t);
11158 if (chain && chain != void_type_node)
11159 chain = RECUR (chain);
11160 return tree_cons (purpose, value, chain);
11161 #undef RECUR
11162 }
11163
11164 /* Substitute one OMP_FOR iterator. */
11165
11166 static void
11167 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
11168 tree condv, tree incrv, tree *clauses,
11169 tree args, tsubst_flags_t complain, tree in_decl,
11170 bool integral_constant_expression_p)
11171 {
11172 #define RECUR(NODE) \
11173 tsubst_expr ((NODE), args, complain, in_decl, \
11174 integral_constant_expression_p)
11175 tree decl, init, cond, incr, auto_node;
11176
11177 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
11178 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
11179 decl = RECUR (TREE_OPERAND (init, 0));
11180 init = TREE_OPERAND (init, 1);
11181 auto_node = type_uses_auto (TREE_TYPE (decl));
11182 if (auto_node && init)
11183 {
11184 tree init_expr = init;
11185 if (TREE_CODE (init_expr) == DECL_EXPR)
11186 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
11187 init_expr = RECUR (init_expr);
11188 TREE_TYPE (decl)
11189 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
11190 }
11191 gcc_assert (!type_dependent_expression_p (decl));
11192
11193 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
11194 {
11195 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
11196 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11197 if (TREE_CODE (incr) == MODIFY_EXPR)
11198 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
11199 RECUR (TREE_OPERAND (incr, 1)),
11200 complain);
11201 else
11202 incr = RECUR (incr);
11203 TREE_VEC_ELT (declv, i) = decl;
11204 TREE_VEC_ELT (initv, i) = init;
11205 TREE_VEC_ELT (condv, i) = cond;
11206 TREE_VEC_ELT (incrv, i) = incr;
11207 return;
11208 }
11209
11210 if (init && TREE_CODE (init) != DECL_EXPR)
11211 {
11212 tree c;
11213 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11214 {
11215 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
11216 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11217 && OMP_CLAUSE_DECL (c) == decl)
11218 break;
11219 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11220 && OMP_CLAUSE_DECL (c) == decl)
11221 error ("iteration variable %qD should not be firstprivate", decl);
11222 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11223 && OMP_CLAUSE_DECL (c) == decl)
11224 error ("iteration variable %qD should not be reduction", decl);
11225 }
11226 if (c == NULL)
11227 {
11228 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11229 OMP_CLAUSE_DECL (c) = decl;
11230 c = finish_omp_clauses (c);
11231 if (c)
11232 {
11233 OMP_CLAUSE_CHAIN (c) = *clauses;
11234 *clauses = c;
11235 }
11236 }
11237 }
11238 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
11239 if (COMPARISON_CLASS_P (cond))
11240 cond = build2 (TREE_CODE (cond), boolean_type_node,
11241 RECUR (TREE_OPERAND (cond, 0)),
11242 RECUR (TREE_OPERAND (cond, 1)));
11243 else
11244 cond = RECUR (cond);
11245 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11246 switch (TREE_CODE (incr))
11247 {
11248 case PREINCREMENT_EXPR:
11249 case PREDECREMENT_EXPR:
11250 case POSTINCREMENT_EXPR:
11251 case POSTDECREMENT_EXPR:
11252 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
11253 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
11254 break;
11255 case MODIFY_EXPR:
11256 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11257 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11258 {
11259 tree rhs = TREE_OPERAND (incr, 1);
11260 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11261 RECUR (TREE_OPERAND (incr, 0)),
11262 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11263 RECUR (TREE_OPERAND (rhs, 0)),
11264 RECUR (TREE_OPERAND (rhs, 1))));
11265 }
11266 else
11267 incr = RECUR (incr);
11268 break;
11269 case MODOP_EXPR:
11270 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11271 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11272 {
11273 tree lhs = RECUR (TREE_OPERAND (incr, 0));
11274 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
11275 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
11276 TREE_TYPE (decl), lhs,
11277 RECUR (TREE_OPERAND (incr, 2))));
11278 }
11279 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
11280 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
11281 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
11282 {
11283 tree rhs = TREE_OPERAND (incr, 2);
11284 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11285 RECUR (TREE_OPERAND (incr, 0)),
11286 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11287 RECUR (TREE_OPERAND (rhs, 0)),
11288 RECUR (TREE_OPERAND (rhs, 1))));
11289 }
11290 else
11291 incr = RECUR (incr);
11292 break;
11293 default:
11294 incr = RECUR (incr);
11295 break;
11296 }
11297
11298 TREE_VEC_ELT (declv, i) = decl;
11299 TREE_VEC_ELT (initv, i) = init;
11300 TREE_VEC_ELT (condv, i) = cond;
11301 TREE_VEC_ELT (incrv, i) = incr;
11302 #undef RECUR
11303 }
11304
11305 /* Like tsubst_copy for expressions, etc. but also does semantic
11306 processing. */
11307
11308 static tree
11309 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
11310 bool integral_constant_expression_p)
11311 {
11312 #define RECUR(NODE) \
11313 tsubst_expr ((NODE), args, complain, in_decl, \
11314 integral_constant_expression_p)
11315
11316 tree stmt, tmp;
11317
11318 if (t == NULL_TREE || t == error_mark_node)
11319 return t;
11320
11321 if (EXPR_HAS_LOCATION (t))
11322 input_location = EXPR_LOCATION (t);
11323 if (STATEMENT_CODE_P (TREE_CODE (t)))
11324 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
11325
11326 switch (TREE_CODE (t))
11327 {
11328 case STATEMENT_LIST:
11329 {
11330 tree_stmt_iterator i;
11331 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
11332 RECUR (tsi_stmt (i));
11333 break;
11334 }
11335
11336 case CTOR_INITIALIZER:
11337 finish_mem_initializers (tsubst_initializer_list
11338 (TREE_OPERAND (t, 0), args));
11339 break;
11340
11341 case RETURN_EXPR:
11342 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
11343 break;
11344
11345 case EXPR_STMT:
11346 tmp = RECUR (EXPR_STMT_EXPR (t));
11347 if (EXPR_STMT_STMT_EXPR_RESULT (t))
11348 finish_stmt_expr_expr (tmp, cur_stmt_expr);
11349 else
11350 finish_expr_stmt (tmp);
11351 break;
11352
11353 case USING_STMT:
11354 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
11355 break;
11356
11357 case DECL_EXPR:
11358 {
11359 tree decl;
11360 tree init;
11361
11362 decl = DECL_EXPR_DECL (t);
11363 if (TREE_CODE (decl) == LABEL_DECL)
11364 finish_label_decl (DECL_NAME (decl));
11365 else if (TREE_CODE (decl) == USING_DECL)
11366 {
11367 tree scope = USING_DECL_SCOPE (decl);
11368 tree name = DECL_NAME (decl);
11369 tree decl;
11370
11371 scope = RECUR (scope);
11372 decl = lookup_qualified_name (scope, name,
11373 /*is_type_p=*/false,
11374 /*complain=*/false);
11375 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
11376 qualified_name_lookup_error (scope, name, decl, input_location);
11377 else
11378 do_local_using_decl (decl, scope, name);
11379 }
11380 else
11381 {
11382 init = DECL_INITIAL (decl);
11383 decl = tsubst (decl, args, complain, in_decl);
11384 if (decl != error_mark_node)
11385 {
11386 /* By marking the declaration as instantiated, we avoid
11387 trying to instantiate it. Since instantiate_decl can't
11388 handle local variables, and since we've already done
11389 all that needs to be done, that's the right thing to
11390 do. */
11391 if (TREE_CODE (decl) == VAR_DECL)
11392 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11393 if (TREE_CODE (decl) == VAR_DECL
11394 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
11395 /* Anonymous aggregates are a special case. */
11396 finish_anon_union (decl);
11397 else
11398 {
11399 maybe_push_decl (decl);
11400 if (TREE_CODE (decl) == VAR_DECL
11401 && DECL_PRETTY_FUNCTION_P (decl))
11402 {
11403 /* For __PRETTY_FUNCTION__ we have to adjust the
11404 initializer. */
11405 const char *const name
11406 = cxx_printable_name (current_function_decl, 2);
11407 init = cp_fname_init (name, &TREE_TYPE (decl));
11408 }
11409 else
11410 {
11411 tree t = RECUR (init);
11412
11413 if (init && !t)
11414 /* If we had an initializer but it
11415 instantiated to nothing,
11416 value-initialize the object. This will
11417 only occur when the initializer was a
11418 pack expansion where the parameter packs
11419 used in that expansion were of length
11420 zero. */
11421 init = build_value_init (TREE_TYPE (decl));
11422 else
11423 init = t;
11424 }
11425
11426 cp_finish_decl (decl, init, false, NULL_TREE, 0);
11427 }
11428 }
11429 }
11430
11431 /* A DECL_EXPR can also be used as an expression, in the condition
11432 clause of an if/for/while construct. */
11433 return decl;
11434 }
11435
11436 case FOR_STMT:
11437 stmt = begin_for_stmt ();
11438 RECUR (FOR_INIT_STMT (t));
11439 finish_for_init_stmt (stmt);
11440 tmp = RECUR (FOR_COND (t));
11441 finish_for_cond (tmp, stmt);
11442 tmp = RECUR (FOR_EXPR (t));
11443 finish_for_expr (tmp, stmt);
11444 RECUR (FOR_BODY (t));
11445 finish_for_stmt (stmt);
11446 break;
11447
11448 case WHILE_STMT:
11449 stmt = begin_while_stmt ();
11450 tmp = RECUR (WHILE_COND (t));
11451 finish_while_stmt_cond (tmp, stmt);
11452 RECUR (WHILE_BODY (t));
11453 finish_while_stmt (stmt);
11454 break;
11455
11456 case DO_STMT:
11457 stmt = begin_do_stmt ();
11458 RECUR (DO_BODY (t));
11459 finish_do_body (stmt);
11460 tmp = RECUR (DO_COND (t));
11461 finish_do_stmt (tmp, stmt);
11462 break;
11463
11464 case IF_STMT:
11465 stmt = begin_if_stmt ();
11466 tmp = RECUR (IF_COND (t));
11467 finish_if_stmt_cond (tmp, stmt);
11468 RECUR (THEN_CLAUSE (t));
11469 finish_then_clause (stmt);
11470
11471 if (ELSE_CLAUSE (t))
11472 {
11473 begin_else_clause (stmt);
11474 RECUR (ELSE_CLAUSE (t));
11475 finish_else_clause (stmt);
11476 }
11477
11478 finish_if_stmt (stmt);
11479 break;
11480
11481 case BIND_EXPR:
11482 if (BIND_EXPR_BODY_BLOCK (t))
11483 stmt = begin_function_body ();
11484 else
11485 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
11486 ? BCS_TRY_BLOCK : 0);
11487
11488 RECUR (BIND_EXPR_BODY (t));
11489
11490 if (BIND_EXPR_BODY_BLOCK (t))
11491 finish_function_body (stmt);
11492 else
11493 finish_compound_stmt (stmt);
11494 break;
11495
11496 case BREAK_STMT:
11497 finish_break_stmt ();
11498 break;
11499
11500 case CONTINUE_STMT:
11501 finish_continue_stmt ();
11502 break;
11503
11504 case SWITCH_STMT:
11505 stmt = begin_switch_stmt ();
11506 tmp = RECUR (SWITCH_STMT_COND (t));
11507 finish_switch_cond (tmp, stmt);
11508 RECUR (SWITCH_STMT_BODY (t));
11509 finish_switch_stmt (stmt);
11510 break;
11511
11512 case CASE_LABEL_EXPR:
11513 finish_case_label (EXPR_LOCATION (t),
11514 RECUR (CASE_LOW (t)),
11515 RECUR (CASE_HIGH (t)));
11516 break;
11517
11518 case LABEL_EXPR:
11519 {
11520 tree decl = LABEL_EXPR_LABEL (t);
11521 tree label;
11522
11523 label = finish_label_stmt (DECL_NAME (decl));
11524 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
11525 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
11526 }
11527 break;
11528
11529 case GOTO_EXPR:
11530 tmp = GOTO_DESTINATION (t);
11531 if (TREE_CODE (tmp) != LABEL_DECL)
11532 /* Computed goto's must be tsubst'd into. On the other hand,
11533 non-computed gotos must not be; the identifier in question
11534 will have no binding. */
11535 tmp = RECUR (tmp);
11536 else
11537 tmp = DECL_NAME (tmp);
11538 finish_goto_stmt (tmp);
11539 break;
11540
11541 case ASM_EXPR:
11542 tmp = finish_asm_stmt
11543 (ASM_VOLATILE_P (t),
11544 RECUR (ASM_STRING (t)),
11545 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
11546 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
11547 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
11548 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
11549 {
11550 tree asm_expr = tmp;
11551 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
11552 asm_expr = TREE_OPERAND (asm_expr, 0);
11553 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
11554 }
11555 break;
11556
11557 case TRY_BLOCK:
11558 if (CLEANUP_P (t))
11559 {
11560 stmt = begin_try_block ();
11561 RECUR (TRY_STMTS (t));
11562 finish_cleanup_try_block (stmt);
11563 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
11564 }
11565 else
11566 {
11567 tree compound_stmt = NULL_TREE;
11568
11569 if (FN_TRY_BLOCK_P (t))
11570 stmt = begin_function_try_block (&compound_stmt);
11571 else
11572 stmt = begin_try_block ();
11573
11574 RECUR (TRY_STMTS (t));
11575
11576 if (FN_TRY_BLOCK_P (t))
11577 finish_function_try_block (stmt);
11578 else
11579 finish_try_block (stmt);
11580
11581 RECUR (TRY_HANDLERS (t));
11582 if (FN_TRY_BLOCK_P (t))
11583 finish_function_handler_sequence (stmt, compound_stmt);
11584 else
11585 finish_handler_sequence (stmt);
11586 }
11587 break;
11588
11589 case HANDLER:
11590 {
11591 tree decl = HANDLER_PARMS (t);
11592
11593 if (decl)
11594 {
11595 decl = tsubst (decl, args, complain, in_decl);
11596 /* Prevent instantiate_decl from trying to instantiate
11597 this variable. We've already done all that needs to be
11598 done. */
11599 if (decl != error_mark_node)
11600 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11601 }
11602 stmt = begin_handler ();
11603 finish_handler_parms (decl, stmt);
11604 RECUR (HANDLER_BODY (t));
11605 finish_handler (stmt);
11606 }
11607 break;
11608
11609 case TAG_DEFN:
11610 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
11611 break;
11612
11613 case STATIC_ASSERT:
11614 {
11615 tree condition =
11616 tsubst_expr (STATIC_ASSERT_CONDITION (t),
11617 args,
11618 complain, in_decl,
11619 /*integral_constant_expression_p=*/true);
11620 finish_static_assert (condition,
11621 STATIC_ASSERT_MESSAGE (t),
11622 STATIC_ASSERT_SOURCE_LOCATION (t),
11623 /*member_p=*/false);
11624 }
11625 break;
11626
11627 case OMP_PARALLEL:
11628 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
11629 args, complain, in_decl);
11630 stmt = begin_omp_parallel ();
11631 RECUR (OMP_PARALLEL_BODY (t));
11632 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
11633 = OMP_PARALLEL_COMBINED (t);
11634 break;
11635
11636 case OMP_TASK:
11637 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
11638 args, complain, in_decl);
11639 stmt = begin_omp_task ();
11640 RECUR (OMP_TASK_BODY (t));
11641 finish_omp_task (tmp, stmt);
11642 break;
11643
11644 case OMP_FOR:
11645 {
11646 tree clauses, body, pre_body;
11647 tree declv, initv, condv, incrv;
11648 int i;
11649
11650 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
11651 args, complain, in_decl);
11652 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11653 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11654 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11655 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11656
11657 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
11658 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
11659 &clauses, args, complain, in_decl,
11660 integral_constant_expression_p);
11661
11662 stmt = begin_omp_structured_block ();
11663
11664 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
11665 if (TREE_VEC_ELT (initv, i) == NULL
11666 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
11667 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
11668 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
11669 {
11670 tree init = RECUR (TREE_VEC_ELT (initv, i));
11671 gcc_assert (init == TREE_VEC_ELT (declv, i));
11672 TREE_VEC_ELT (initv, i) = NULL_TREE;
11673 }
11674 else
11675 {
11676 tree decl_expr = TREE_VEC_ELT (initv, i);
11677 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
11678 gcc_assert (init != NULL);
11679 TREE_VEC_ELT (initv, i) = RECUR (init);
11680 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
11681 RECUR (decl_expr);
11682 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
11683 }
11684
11685 pre_body = push_stmt_list ();
11686 RECUR (OMP_FOR_PRE_BODY (t));
11687 pre_body = pop_stmt_list (pre_body);
11688
11689 body = push_stmt_list ();
11690 RECUR (OMP_FOR_BODY (t));
11691 body = pop_stmt_list (body);
11692
11693 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
11694 body, pre_body, clauses);
11695
11696 add_stmt (finish_omp_structured_block (stmt));
11697 }
11698 break;
11699
11700 case OMP_SECTIONS:
11701 case OMP_SINGLE:
11702 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
11703 stmt = push_stmt_list ();
11704 RECUR (OMP_BODY (t));
11705 stmt = pop_stmt_list (stmt);
11706
11707 t = copy_node (t);
11708 OMP_BODY (t) = stmt;
11709 OMP_CLAUSES (t) = tmp;
11710 add_stmt (t);
11711 break;
11712
11713 case OMP_SECTION:
11714 case OMP_CRITICAL:
11715 case OMP_MASTER:
11716 case OMP_ORDERED:
11717 stmt = push_stmt_list ();
11718 RECUR (OMP_BODY (t));
11719 stmt = pop_stmt_list (stmt);
11720
11721 t = copy_node (t);
11722 OMP_BODY (t) = stmt;
11723 add_stmt (t);
11724 break;
11725
11726 case OMP_ATOMIC:
11727 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
11728 {
11729 tree op1 = TREE_OPERAND (t, 1);
11730 tree lhs = RECUR (TREE_OPERAND (op1, 0));
11731 tree rhs = RECUR (TREE_OPERAND (op1, 1));
11732 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
11733 }
11734 break;
11735
11736 case EXPR_PACK_EXPANSION:
11737 error ("invalid use of pack expansion expression");
11738 return error_mark_node;
11739
11740 case NONTYPE_ARGUMENT_PACK:
11741 error ("use %<...%> to expand argument pack");
11742 return error_mark_node;
11743
11744 default:
11745 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
11746
11747 return tsubst_copy_and_build (t, args, complain, in_decl,
11748 /*function_p=*/false,
11749 integral_constant_expression_p);
11750 }
11751
11752 return NULL_TREE;
11753 #undef RECUR
11754 }
11755
11756 /* T is a postfix-expression that is not being used in a function
11757 call. Return the substituted version of T. */
11758
11759 static tree
11760 tsubst_non_call_postfix_expression (tree t, tree args,
11761 tsubst_flags_t complain,
11762 tree in_decl)
11763 {
11764 if (TREE_CODE (t) == SCOPE_REF)
11765 t = tsubst_qualified_id (t, args, complain, in_decl,
11766 /*done=*/false, /*address_p=*/false);
11767 else
11768 t = tsubst_copy_and_build (t, args, complain, in_decl,
11769 /*function_p=*/false,
11770 /*integral_constant_expression_p=*/false);
11771
11772 return t;
11773 }
11774
11775 /* Like tsubst but deals with expressions and performs semantic
11776 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11777
11778 tree
11779 tsubst_copy_and_build (tree t,
11780 tree args,
11781 tsubst_flags_t complain,
11782 tree in_decl,
11783 bool function_p,
11784 bool integral_constant_expression_p)
11785 {
11786 #define RECUR(NODE) \
11787 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11788 /*function_p=*/false, \
11789 integral_constant_expression_p)
11790
11791 tree op1;
11792
11793 if (t == NULL_TREE || t == error_mark_node)
11794 return t;
11795
11796 switch (TREE_CODE (t))
11797 {
11798 case USING_DECL:
11799 t = DECL_NAME (t);
11800 /* Fall through. */
11801 case IDENTIFIER_NODE:
11802 {
11803 tree decl;
11804 cp_id_kind idk;
11805 bool non_integral_constant_expression_p;
11806 const char *error_msg;
11807
11808 if (IDENTIFIER_TYPENAME_P (t))
11809 {
11810 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11811 t = mangle_conv_op_name_for_type (new_type);
11812 }
11813
11814 /* Look up the name. */
11815 decl = lookup_name (t);
11816
11817 /* By convention, expressions use ERROR_MARK_NODE to indicate
11818 failure, not NULL_TREE. */
11819 if (decl == NULL_TREE)
11820 decl = error_mark_node;
11821
11822 decl = finish_id_expression (t, decl, NULL_TREE,
11823 &idk,
11824 integral_constant_expression_p,
11825 /*allow_non_integral_constant_expression_p=*/false,
11826 &non_integral_constant_expression_p,
11827 /*template_p=*/false,
11828 /*done=*/true,
11829 /*address_p=*/false,
11830 /*template_arg_p=*/false,
11831 &error_msg,
11832 input_location);
11833 if (error_msg)
11834 error (error_msg);
11835 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
11836 decl = unqualified_name_lookup_error (decl);
11837 return decl;
11838 }
11839
11840 case TEMPLATE_ID_EXPR:
11841 {
11842 tree object;
11843 tree templ = RECUR (TREE_OPERAND (t, 0));
11844 tree targs = TREE_OPERAND (t, 1);
11845
11846 if (targs)
11847 targs = tsubst_template_args (targs, args, complain, in_decl);
11848
11849 if (TREE_CODE (templ) == COMPONENT_REF)
11850 {
11851 object = TREE_OPERAND (templ, 0);
11852 templ = TREE_OPERAND (templ, 1);
11853 }
11854 else
11855 object = NULL_TREE;
11856 templ = lookup_template_function (templ, targs);
11857
11858 if (object)
11859 return build3 (COMPONENT_REF, TREE_TYPE (templ),
11860 object, templ, NULL_TREE);
11861 else
11862 return baselink_for_fns (templ);
11863 }
11864
11865 case INDIRECT_REF:
11866 {
11867 tree r = RECUR (TREE_OPERAND (t, 0));
11868
11869 if (REFERENCE_REF_P (t))
11870 {
11871 /* A type conversion to reference type will be enclosed in
11872 such an indirect ref, but the substitution of the cast
11873 will have also added such an indirect ref. */
11874 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
11875 r = convert_from_reference (r);
11876 }
11877 else
11878 r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
11879 return r;
11880 }
11881
11882 case NOP_EXPR:
11883 return build_nop
11884 (tsubst (TREE_TYPE (t), args, complain, in_decl),
11885 RECUR (TREE_OPERAND (t, 0)));
11886
11887 case CAST_EXPR:
11888 case REINTERPRET_CAST_EXPR:
11889 case CONST_CAST_EXPR:
11890 case DYNAMIC_CAST_EXPR:
11891 case STATIC_CAST_EXPR:
11892 {
11893 tree type;
11894 tree op;
11895
11896 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11897 if (integral_constant_expression_p
11898 && !cast_valid_in_integral_constant_expression_p (type))
11899 {
11900 if (complain & tf_error)
11901 error ("a cast to a type other than an integral or "
11902 "enumeration type cannot appear in a constant-expression");
11903 return error_mark_node;
11904 }
11905
11906 op = RECUR (TREE_OPERAND (t, 0));
11907
11908 switch (TREE_CODE (t))
11909 {
11910 case CAST_EXPR:
11911 return build_functional_cast (type, op, complain);
11912 case REINTERPRET_CAST_EXPR:
11913 return build_reinterpret_cast (type, op, complain);
11914 case CONST_CAST_EXPR:
11915 return build_const_cast (type, op, complain);
11916 case DYNAMIC_CAST_EXPR:
11917 return build_dynamic_cast (type, op, complain);
11918 case STATIC_CAST_EXPR:
11919 return build_static_cast (type, op, complain);
11920 default:
11921 gcc_unreachable ();
11922 }
11923 }
11924
11925 case POSTDECREMENT_EXPR:
11926 case POSTINCREMENT_EXPR:
11927 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11928 args, complain, in_decl);
11929 return build_x_unary_op (TREE_CODE (t), op1, complain);
11930
11931 case PREDECREMENT_EXPR:
11932 case PREINCREMENT_EXPR:
11933 case NEGATE_EXPR:
11934 case BIT_NOT_EXPR:
11935 case ABS_EXPR:
11936 case TRUTH_NOT_EXPR:
11937 case UNARY_PLUS_EXPR: /* Unary + */
11938 case REALPART_EXPR:
11939 case IMAGPART_EXPR:
11940 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
11941 complain);
11942
11943 case ADDR_EXPR:
11944 op1 = TREE_OPERAND (t, 0);
11945 if (TREE_CODE (op1) == SCOPE_REF)
11946 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
11947 /*done=*/true, /*address_p=*/true);
11948 else
11949 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
11950 in_decl);
11951 if (TREE_CODE (op1) == LABEL_DECL)
11952 return finish_label_address_expr (DECL_NAME (op1),
11953 EXPR_LOCATION (op1));
11954 return build_x_unary_op (ADDR_EXPR, op1, complain);
11955
11956 case PLUS_EXPR:
11957 case MINUS_EXPR:
11958 case MULT_EXPR:
11959 case TRUNC_DIV_EXPR:
11960 case CEIL_DIV_EXPR:
11961 case FLOOR_DIV_EXPR:
11962 case ROUND_DIV_EXPR:
11963 case EXACT_DIV_EXPR:
11964 case BIT_AND_EXPR:
11965 case BIT_IOR_EXPR:
11966 case BIT_XOR_EXPR:
11967 case TRUNC_MOD_EXPR:
11968 case FLOOR_MOD_EXPR:
11969 case TRUTH_ANDIF_EXPR:
11970 case TRUTH_ORIF_EXPR:
11971 case TRUTH_AND_EXPR:
11972 case TRUTH_OR_EXPR:
11973 case RSHIFT_EXPR:
11974 case LSHIFT_EXPR:
11975 case RROTATE_EXPR:
11976 case LROTATE_EXPR:
11977 case EQ_EXPR:
11978 case NE_EXPR:
11979 case MAX_EXPR:
11980 case MIN_EXPR:
11981 case LE_EXPR:
11982 case GE_EXPR:
11983 case LT_EXPR:
11984 case GT_EXPR:
11985 case MEMBER_REF:
11986 case DOTSTAR_EXPR:
11987 return build_x_binary_op
11988 (TREE_CODE (t),
11989 RECUR (TREE_OPERAND (t, 0)),
11990 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
11991 ? ERROR_MARK
11992 : TREE_CODE (TREE_OPERAND (t, 0))),
11993 RECUR (TREE_OPERAND (t, 1)),
11994 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
11995 ? ERROR_MARK
11996 : TREE_CODE (TREE_OPERAND (t, 1))),
11997 /*overloaded_p=*/NULL,
11998 complain);
11999
12000 case SCOPE_REF:
12001 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
12002 /*address_p=*/false);
12003 case ARRAY_REF:
12004 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12005 args, complain, in_decl);
12006 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
12007
12008 case SIZEOF_EXPR:
12009 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12010 return tsubst_copy (t, args, complain, in_decl);
12011 /* Fall through */
12012
12013 case ALIGNOF_EXPR:
12014 op1 = TREE_OPERAND (t, 0);
12015 if (!args)
12016 {
12017 /* When there are no ARGS, we are trying to evaluate a
12018 non-dependent expression from the parser. Trying to do
12019 the substitutions may not work. */
12020 if (!TYPE_P (op1))
12021 op1 = TREE_TYPE (op1);
12022 }
12023 else
12024 {
12025 ++cp_unevaluated_operand;
12026 ++c_inhibit_evaluation_warnings;
12027 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12028 /*function_p=*/false,
12029 /*integral_constant_expression_p=*/false);
12030 --cp_unevaluated_operand;
12031 --c_inhibit_evaluation_warnings;
12032 }
12033 if (TYPE_P (op1))
12034 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
12035 complain & tf_error);
12036 else
12037 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
12038 complain & tf_error);
12039
12040 case MODOP_EXPR:
12041 {
12042 tree r = build_x_modify_expr
12043 (RECUR (TREE_OPERAND (t, 0)),
12044 TREE_CODE (TREE_OPERAND (t, 1)),
12045 RECUR (TREE_OPERAND (t, 2)),
12046 complain);
12047 /* TREE_NO_WARNING must be set if either the expression was
12048 parenthesized or it uses an operator such as >>= rather
12049 than plain assignment. In the former case, it was already
12050 set and must be copied. In the latter case,
12051 build_x_modify_expr sets it and it must not be reset
12052 here. */
12053 if (TREE_NO_WARNING (t))
12054 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12055 return r;
12056 }
12057
12058 case ARROW_EXPR:
12059 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12060 args, complain, in_decl);
12061 /* Remember that there was a reference to this entity. */
12062 if (DECL_P (op1))
12063 mark_used (op1);
12064 return build_x_arrow (op1);
12065
12066 case NEW_EXPR:
12067 {
12068 tree placement = RECUR (TREE_OPERAND (t, 0));
12069 tree init = RECUR (TREE_OPERAND (t, 3));
12070 VEC(tree,gc) *placement_vec;
12071 VEC(tree,gc) *init_vec;
12072 tree ret;
12073
12074 if (placement == NULL_TREE)
12075 placement_vec = NULL;
12076 else
12077 {
12078 placement_vec = make_tree_vector ();
12079 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
12080 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
12081 }
12082
12083 /* If there was an initializer in the original tree, but it
12084 instantiated to an empty list, then we should pass a
12085 non-NULL empty vector to tell build_new that it was an
12086 empty initializer() rather than no initializer. This can
12087 only happen when the initializer is a pack expansion whose
12088 parameter packs are of length zero. */
12089 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
12090 init_vec = NULL;
12091 else
12092 {
12093 init_vec = make_tree_vector ();
12094 if (init == void_zero_node)
12095 gcc_assert (init_vec != NULL);
12096 else
12097 {
12098 for (; init != NULL_TREE; init = TREE_CHAIN (init))
12099 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
12100 }
12101 }
12102
12103 ret = build_new (&placement_vec,
12104 RECUR (TREE_OPERAND (t, 1)),
12105 RECUR (TREE_OPERAND (t, 2)),
12106 &init_vec,
12107 NEW_EXPR_USE_GLOBAL (t),
12108 complain);
12109
12110 if (placement_vec != NULL)
12111 release_tree_vector (placement_vec);
12112 if (init_vec != NULL)
12113 release_tree_vector (init_vec);
12114
12115 return ret;
12116 }
12117
12118 case DELETE_EXPR:
12119 return delete_sanity
12120 (RECUR (TREE_OPERAND (t, 0)),
12121 RECUR (TREE_OPERAND (t, 1)),
12122 DELETE_EXPR_USE_VEC (t),
12123 DELETE_EXPR_USE_GLOBAL (t));
12124
12125 case COMPOUND_EXPR:
12126 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
12127 RECUR (TREE_OPERAND (t, 1)),
12128 complain);
12129
12130 case CALL_EXPR:
12131 {
12132 tree function;
12133 VEC(tree,gc) *call_args;
12134 unsigned int nargs, i;
12135 bool qualified_p;
12136 bool koenig_p;
12137 tree ret;
12138
12139 function = CALL_EXPR_FN (t);
12140 /* When we parsed the expression, we determined whether or
12141 not Koenig lookup should be performed. */
12142 koenig_p = KOENIG_LOOKUP_P (t);
12143 if (TREE_CODE (function) == SCOPE_REF)
12144 {
12145 qualified_p = true;
12146 function = tsubst_qualified_id (function, args, complain, in_decl,
12147 /*done=*/false,
12148 /*address_p=*/false);
12149 }
12150 else
12151 {
12152 if (TREE_CODE (function) == COMPONENT_REF)
12153 {
12154 tree op = TREE_OPERAND (function, 1);
12155
12156 qualified_p = (TREE_CODE (op) == SCOPE_REF
12157 || (BASELINK_P (op)
12158 && BASELINK_QUALIFIED_P (op)));
12159 }
12160 else
12161 qualified_p = false;
12162
12163 function = tsubst_copy_and_build (function, args, complain,
12164 in_decl,
12165 !qualified_p,
12166 integral_constant_expression_p);
12167
12168 if (BASELINK_P (function))
12169 qualified_p = true;
12170 }
12171
12172 nargs = call_expr_nargs (t);
12173 call_args = make_tree_vector ();
12174 for (i = 0; i < nargs; ++i)
12175 {
12176 tree arg = CALL_EXPR_ARG (t, i);
12177
12178 if (!PACK_EXPANSION_P (arg))
12179 VEC_safe_push (tree, gc, call_args,
12180 RECUR (CALL_EXPR_ARG (t, i)));
12181 else
12182 {
12183 /* Expand the pack expansion and push each entry onto
12184 CALL_ARGS. */
12185 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
12186 if (TREE_CODE (arg) == TREE_VEC)
12187 {
12188 unsigned int len, j;
12189
12190 len = TREE_VEC_LENGTH (arg);
12191 for (j = 0; j < len; ++j)
12192 {
12193 tree value = TREE_VEC_ELT (arg, j);
12194 if (value != NULL_TREE)
12195 value = convert_from_reference (value);
12196 VEC_safe_push (tree, gc, call_args, value);
12197 }
12198 }
12199 else
12200 {
12201 /* A partial substitution. Add one entry. */
12202 VEC_safe_push (tree, gc, call_args, arg);
12203 }
12204 }
12205 }
12206
12207 /* We do not perform argument-dependent lookup if normal
12208 lookup finds a non-function, in accordance with the
12209 expected resolution of DR 218. */
12210 if (koenig_p
12211 && ((is_overloaded_fn (function)
12212 /* If lookup found a member function, the Koenig lookup is
12213 not appropriate, even if an unqualified-name was used
12214 to denote the function. */
12215 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
12216 || TREE_CODE (function) == IDENTIFIER_NODE)
12217 /* Only do this when substitution turns a dependent call
12218 into a non-dependent call. */
12219 && type_dependent_expression_p_push (t)
12220 && !any_type_dependent_arguments_p (call_args))
12221 function = perform_koenig_lookup (function, call_args);
12222
12223 if (TREE_CODE (function) == IDENTIFIER_NODE)
12224 {
12225 unqualified_name_lookup_error (function);
12226 release_tree_vector (call_args);
12227 return error_mark_node;
12228 }
12229
12230 /* Remember that there was a reference to this entity. */
12231 if (DECL_P (function))
12232 mark_used (function);
12233
12234 if (TREE_CODE (function) == OFFSET_REF)
12235 ret = build_offset_ref_call_from_tree (function, &call_args);
12236 else if (TREE_CODE (function) == COMPONENT_REF)
12237 {
12238 if (!BASELINK_P (TREE_OPERAND (function, 1)))
12239 ret = finish_call_expr (function, &call_args,
12240 /*disallow_virtual=*/false,
12241 /*koenig_p=*/false,
12242 complain);
12243 else
12244 ret = (build_new_method_call
12245 (TREE_OPERAND (function, 0),
12246 TREE_OPERAND (function, 1),
12247 &call_args, NULL_TREE,
12248 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
12249 /*fn_p=*/NULL,
12250 complain));
12251 }
12252 else
12253 ret = finish_call_expr (function, &call_args,
12254 /*disallow_virtual=*/qualified_p,
12255 koenig_p,
12256 complain);
12257
12258 release_tree_vector (call_args);
12259
12260 return ret;
12261 }
12262
12263 case COND_EXPR:
12264 return build_x_conditional_expr
12265 (RECUR (TREE_OPERAND (t, 0)),
12266 RECUR (TREE_OPERAND (t, 1)),
12267 RECUR (TREE_OPERAND (t, 2)),
12268 complain);
12269
12270 case PSEUDO_DTOR_EXPR:
12271 return finish_pseudo_destructor_expr
12272 (RECUR (TREE_OPERAND (t, 0)),
12273 RECUR (TREE_OPERAND (t, 1)),
12274 RECUR (TREE_OPERAND (t, 2)));
12275
12276 case TREE_LIST:
12277 {
12278 tree purpose, value, chain;
12279
12280 if (t == void_list_node)
12281 return t;
12282
12283 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
12284 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
12285 {
12286 /* We have pack expansions, so expand those and
12287 create a new list out of it. */
12288 tree purposevec = NULL_TREE;
12289 tree valuevec = NULL_TREE;
12290 tree chain;
12291 int i, len = -1;
12292
12293 /* Expand the argument expressions. */
12294 if (TREE_PURPOSE (t))
12295 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
12296 complain, in_decl);
12297 if (TREE_VALUE (t))
12298 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
12299 complain, in_decl);
12300
12301 /* Build the rest of the list. */
12302 chain = TREE_CHAIN (t);
12303 if (chain && chain != void_type_node)
12304 chain = RECUR (chain);
12305
12306 /* Determine the number of arguments. */
12307 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
12308 {
12309 len = TREE_VEC_LENGTH (purposevec);
12310 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
12311 }
12312 else if (TREE_CODE (valuevec) == TREE_VEC)
12313 len = TREE_VEC_LENGTH (valuevec);
12314 else
12315 {
12316 /* Since we only performed a partial substitution into
12317 the argument pack, we only return a single list
12318 node. */
12319 if (purposevec == TREE_PURPOSE (t)
12320 && valuevec == TREE_VALUE (t)
12321 && chain == TREE_CHAIN (t))
12322 return t;
12323
12324 return tree_cons (purposevec, valuevec, chain);
12325 }
12326
12327 /* Convert the argument vectors into a TREE_LIST */
12328 i = len;
12329 while (i > 0)
12330 {
12331 /* Grab the Ith values. */
12332 i--;
12333 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
12334 : NULL_TREE;
12335 value
12336 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
12337 : NULL_TREE;
12338
12339 /* Build the list (backwards). */
12340 chain = tree_cons (purpose, value, chain);
12341 }
12342
12343 return chain;
12344 }
12345
12346 purpose = TREE_PURPOSE (t);
12347 if (purpose)
12348 purpose = RECUR (purpose);
12349 value = TREE_VALUE (t);
12350 if (value)
12351 value = RECUR (value);
12352 chain = TREE_CHAIN (t);
12353 if (chain && chain != void_type_node)
12354 chain = RECUR (chain);
12355 if (purpose == TREE_PURPOSE (t)
12356 && value == TREE_VALUE (t)
12357 && chain == TREE_CHAIN (t))
12358 return t;
12359 return tree_cons (purpose, value, chain);
12360 }
12361
12362 case COMPONENT_REF:
12363 {
12364 tree object;
12365 tree object_type;
12366 tree member;
12367
12368 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12369 args, complain, in_decl);
12370 /* Remember that there was a reference to this entity. */
12371 if (DECL_P (object))
12372 mark_used (object);
12373 object_type = TREE_TYPE (object);
12374
12375 member = TREE_OPERAND (t, 1);
12376 if (BASELINK_P (member))
12377 member = tsubst_baselink (member,
12378 non_reference (TREE_TYPE (object)),
12379 args, complain, in_decl);
12380 else
12381 member = tsubst_copy (member, args, complain, in_decl);
12382 if (member == error_mark_node)
12383 return error_mark_node;
12384
12385 if (object_type && !CLASS_TYPE_P (object_type))
12386 {
12387 if (SCALAR_TYPE_P (object_type))
12388 {
12389 tree s = NULL_TREE;
12390 tree dtor = member;
12391
12392 if (TREE_CODE (dtor) == SCOPE_REF)
12393 {
12394 s = TREE_OPERAND (dtor, 0);
12395 dtor = TREE_OPERAND (dtor, 1);
12396 }
12397 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
12398 {
12399 dtor = TREE_OPERAND (dtor, 0);
12400 if (TYPE_P (dtor))
12401 return finish_pseudo_destructor_expr (object, s, dtor);
12402 }
12403 }
12404 }
12405 else if (TREE_CODE (member) == SCOPE_REF
12406 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
12407 {
12408 tree tmpl;
12409 tree args;
12410
12411 /* Lookup the template functions now that we know what the
12412 scope is. */
12413 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
12414 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
12415 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12416 /*is_type_p=*/false,
12417 /*complain=*/false);
12418 if (BASELINK_P (member))
12419 {
12420 BASELINK_FUNCTIONS (member)
12421 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
12422 args);
12423 member = (adjust_result_of_qualified_name_lookup
12424 (member, BINFO_TYPE (BASELINK_BINFO (member)),
12425 object_type));
12426 }
12427 else
12428 {
12429 qualified_name_lookup_error (object_type, tmpl, member,
12430 input_location);
12431 return error_mark_node;
12432 }
12433 }
12434 else if (TREE_CODE (member) == SCOPE_REF
12435 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
12436 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
12437 {
12438 if (complain & tf_error)
12439 {
12440 if (TYPE_P (TREE_OPERAND (member, 0)))
12441 error ("%qT is not a class or namespace",
12442 TREE_OPERAND (member, 0));
12443 else
12444 error ("%qD is not a class or namespace",
12445 TREE_OPERAND (member, 0));
12446 }
12447 return error_mark_node;
12448 }
12449 else if (TREE_CODE (member) == FIELD_DECL)
12450 return finish_non_static_data_member (member, object, NULL_TREE);
12451
12452 return finish_class_member_access_expr (object, member,
12453 /*template_p=*/false,
12454 complain);
12455 }
12456
12457 case THROW_EXPR:
12458 return build_throw
12459 (RECUR (TREE_OPERAND (t, 0)));
12460
12461 case CONSTRUCTOR:
12462 {
12463 VEC(constructor_elt,gc) *n;
12464 constructor_elt *ce;
12465 unsigned HOST_WIDE_INT idx;
12466 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12467 bool process_index_p;
12468 int newlen;
12469 bool need_copy_p = false;
12470 tree r;
12471
12472 if (type == error_mark_node)
12473 return error_mark_node;
12474
12475 /* digest_init will do the wrong thing if we let it. */
12476 if (type && TYPE_PTRMEMFUNC_P (type))
12477 return t;
12478
12479 /* We do not want to process the index of aggregate
12480 initializers as they are identifier nodes which will be
12481 looked up by digest_init. */
12482 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
12483
12484 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
12485 newlen = VEC_length (constructor_elt, n);
12486 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
12487 {
12488 if (ce->index && process_index_p)
12489 ce->index = RECUR (ce->index);
12490
12491 if (PACK_EXPANSION_P (ce->value))
12492 {
12493 /* Substitute into the pack expansion. */
12494 ce->value = tsubst_pack_expansion (ce->value, args, complain,
12495 in_decl);
12496
12497 if (ce->value == error_mark_node)
12498 ;
12499 else if (TREE_VEC_LENGTH (ce->value) == 1)
12500 /* Just move the argument into place. */
12501 ce->value = TREE_VEC_ELT (ce->value, 0);
12502 else
12503 {
12504 /* Update the length of the final CONSTRUCTOR
12505 arguments vector, and note that we will need to
12506 copy.*/
12507 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
12508 need_copy_p = true;
12509 }
12510 }
12511 else
12512 ce->value = RECUR (ce->value);
12513 }
12514
12515 if (need_copy_p)
12516 {
12517 VEC(constructor_elt,gc) *old_n = n;
12518
12519 n = VEC_alloc (constructor_elt, gc, newlen);
12520 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
12521 idx++)
12522 {
12523 if (TREE_CODE (ce->value) == TREE_VEC)
12524 {
12525 int i, len = TREE_VEC_LENGTH (ce->value);
12526 for (i = 0; i < len; ++i)
12527 CONSTRUCTOR_APPEND_ELT (n, 0,
12528 TREE_VEC_ELT (ce->value, i));
12529 }
12530 else
12531 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
12532 }
12533 }
12534
12535 r = build_constructor (init_list_type_node, n);
12536 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
12537
12538 if (TREE_HAS_CONSTRUCTOR (t))
12539 return finish_compound_literal (type, r);
12540
12541 return r;
12542 }
12543
12544 case TYPEID_EXPR:
12545 {
12546 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
12547 if (TYPE_P (operand_0))
12548 return get_typeid (operand_0);
12549 return build_typeid (operand_0);
12550 }
12551
12552 case VAR_DECL:
12553 if (!args)
12554 return t;
12555 /* Fall through */
12556
12557 case PARM_DECL:
12558 {
12559 tree r = tsubst_copy (t, args, complain, in_decl);
12560
12561 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
12562 /* If the original type was a reference, we'll be wrapped in
12563 the appropriate INDIRECT_REF. */
12564 r = convert_from_reference (r);
12565 return r;
12566 }
12567
12568 case VA_ARG_EXPR:
12569 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
12570 tsubst_copy (TREE_TYPE (t), args, complain,
12571 in_decl));
12572
12573 case OFFSETOF_EXPR:
12574 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
12575
12576 case TRAIT_EXPR:
12577 {
12578 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
12579 complain, in_decl);
12580
12581 tree type2 = TRAIT_EXPR_TYPE2 (t);
12582 if (type2)
12583 type2 = tsubst_copy (type2, args, complain, in_decl);
12584
12585 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
12586 }
12587
12588 case STMT_EXPR:
12589 {
12590 tree old_stmt_expr = cur_stmt_expr;
12591 tree stmt_expr = begin_stmt_expr ();
12592
12593 cur_stmt_expr = stmt_expr;
12594 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
12595 integral_constant_expression_p);
12596 stmt_expr = finish_stmt_expr (stmt_expr, false);
12597 cur_stmt_expr = old_stmt_expr;
12598
12599 /* If the resulting list of expression statement is empty,
12600 fold it further into void_zero_node. */
12601 if (empty_expr_stmt_p (stmt_expr))
12602 stmt_expr = void_zero_node;
12603
12604 return stmt_expr;
12605 }
12606
12607 case CONST_DECL:
12608 t = tsubst_copy (t, args, complain, in_decl);
12609 /* As in finish_id_expression, we resolve enumeration constants
12610 to their underlying values. */
12611 if (TREE_CODE (t) == CONST_DECL)
12612 {
12613 used_types_insert (TREE_TYPE (t));
12614 return DECL_INITIAL (t);
12615 }
12616 return t;
12617
12618 case LAMBDA_EXPR:
12619 {
12620 tree r = build_lambda_expr ();
12621
12622 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12623 TREE_TYPE (r) = type;
12624 CLASSTYPE_LAMBDA_EXPR (type) = r;
12625
12626 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
12627 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
12628 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
12629 LAMBDA_EXPR_DISCRIMINATOR (r)
12630 = (LAMBDA_EXPR_DISCRIMINATOR (t));
12631 LAMBDA_EXPR_CAPTURE_LIST (r)
12632 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
12633 LAMBDA_EXPR_THIS_CAPTURE (r)
12634 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
12635 LAMBDA_EXPR_EXTRA_SCOPE (r)
12636 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
12637
12638 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
12639 determine_visibility (TYPE_NAME (type));
12640 /* Now that we know visibility, instantiate the type so we have a
12641 declaration of the op() for later calls to lambda_function. */
12642 complete_type (type);
12643
12644 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
12645 if (type)
12646 apply_lambda_return_type (r, type);
12647
12648 return build_lambda_object (r);
12649 }
12650
12651 default:
12652 /* Handle Objective-C++ constructs, if appropriate. */
12653 {
12654 tree subst
12655 = objcp_tsubst_copy_and_build (t, args, complain,
12656 in_decl, /*function_p=*/false);
12657 if (subst)
12658 return subst;
12659 }
12660 return tsubst_copy (t, args, complain, in_decl);
12661 }
12662
12663 #undef RECUR
12664 }
12665
12666 /* Verify that the instantiated ARGS are valid. For type arguments,
12667 make sure that the type's linkage is ok. For non-type arguments,
12668 make sure they are constants if they are integral or enumerations.
12669 Emit an error under control of COMPLAIN, and return TRUE on error. */
12670
12671 static bool
12672 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
12673 {
12674 if (ARGUMENT_PACK_P (t))
12675 {
12676 tree vec = ARGUMENT_PACK_ARGS (t);
12677 int len = TREE_VEC_LENGTH (vec);
12678 bool result = false;
12679 int i;
12680
12681 for (i = 0; i < len; ++i)
12682 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
12683 result = true;
12684 return result;
12685 }
12686 else if (TYPE_P (t))
12687 {
12688 /* [basic.link]: A name with no linkage (notably, the name
12689 of a class or enumeration declared in a local scope)
12690 shall not be used to declare an entity with linkage.
12691 This implies that names with no linkage cannot be used as
12692 template arguments
12693
12694 DR 757 relaxes this restriction for C++0x. */
12695 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
12696 : no_linkage_check (t, /*relaxed_p=*/false));
12697
12698 if (nt)
12699 {
12700 /* DR 488 makes use of a type with no linkage cause
12701 type deduction to fail. */
12702 if (complain & tf_error)
12703 {
12704 if (TYPE_ANONYMOUS_P (nt))
12705 error ("%qT is/uses anonymous type", t);
12706 else
12707 error ("template argument for %qD uses local type %qT",
12708 tmpl, t);
12709 }
12710 return true;
12711 }
12712 /* In order to avoid all sorts of complications, we do not
12713 allow variably-modified types as template arguments. */
12714 else if (variably_modified_type_p (t, NULL_TREE))
12715 {
12716 if (complain & tf_error)
12717 error ("%qT is a variably modified type", t);
12718 return true;
12719 }
12720 }
12721 /* A non-type argument of integral or enumerated type must be a
12722 constant. */
12723 else if (TREE_TYPE (t)
12724 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
12725 && !TREE_CONSTANT (t))
12726 {
12727 if (complain & tf_error)
12728 error ("integral expression %qE is not constant", t);
12729 return true;
12730 }
12731 return false;
12732 }
12733
12734 static bool
12735 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
12736 {
12737 int ix, len = DECL_NTPARMS (tmpl);
12738 bool result = false;
12739
12740 for (ix = 0; ix != len; ix++)
12741 {
12742 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
12743 result = true;
12744 }
12745 if (result && (complain & tf_error))
12746 error (" trying to instantiate %qD", tmpl);
12747 return result;
12748 }
12749
12750 /* Instantiate the indicated variable or function template TMPL with
12751 the template arguments in TARG_PTR. */
12752
12753 tree
12754 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
12755 {
12756 tree targ_ptr = orig_args;
12757 tree fndecl;
12758 tree gen_tmpl;
12759 tree spec;
12760 HOST_WIDE_INT saved_processing_template_decl;
12761
12762 if (tmpl == error_mark_node)
12763 return error_mark_node;
12764
12765 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
12766
12767 /* If this function is a clone, handle it specially. */
12768 if (DECL_CLONED_FUNCTION_P (tmpl))
12769 {
12770 tree spec;
12771 tree clone;
12772
12773 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
12774 DECL_CLONED_FUNCTION. */
12775 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
12776 targ_ptr, complain);
12777 if (spec == error_mark_node)
12778 return error_mark_node;
12779
12780 /* Look for the clone. */
12781 FOR_EACH_CLONE (clone, spec)
12782 if (DECL_NAME (clone) == DECL_NAME (tmpl))
12783 return clone;
12784 /* We should always have found the clone by now. */
12785 gcc_unreachable ();
12786 return NULL_TREE;
12787 }
12788
12789 /* Check to see if we already have this specialization. */
12790 gen_tmpl = most_general_template (tmpl);
12791 if (tmpl != gen_tmpl)
12792 /* The TMPL is a partial instantiation. To get a full set of
12793 arguments we must add the arguments used to perform the
12794 partial instantiation. */
12795 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
12796 targ_ptr);
12797
12798 /* It would be nice to avoid hashing here and then again in tsubst_decl,
12799 but it doesn't seem to be on the hot path. */
12800 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
12801
12802 gcc_assert (tmpl == gen_tmpl
12803 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
12804 == spec)
12805 || fndecl == NULL_TREE);
12806
12807 if (spec != NULL_TREE)
12808 return spec;
12809
12810 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
12811 complain))
12812 return error_mark_node;
12813
12814 /* We are building a FUNCTION_DECL, during which the access of its
12815 parameters and return types have to be checked. However this
12816 FUNCTION_DECL which is the desired context for access checking
12817 is not built yet. We solve this chicken-and-egg problem by
12818 deferring all checks until we have the FUNCTION_DECL. */
12819 push_deferring_access_checks (dk_deferred);
12820
12821 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
12822 (because, for example, we have encountered a non-dependent
12823 function call in the body of a template function and must now
12824 determine which of several overloaded functions will be called),
12825 within the instantiation itself we are not processing a
12826 template. */
12827 saved_processing_template_decl = processing_template_decl;
12828 processing_template_decl = 0;
12829 /* Substitute template parameters to obtain the specialization. */
12830 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
12831 targ_ptr, complain, gen_tmpl);
12832 processing_template_decl = saved_processing_template_decl;
12833 if (fndecl == error_mark_node)
12834 return error_mark_node;
12835
12836 /* Now we know the specialization, compute access previously
12837 deferred. */
12838 push_access_scope (fndecl);
12839
12840 /* Some typedefs referenced from within the template code need to be access
12841 checked at template instantiation time, i.e now. These types were
12842 added to the template at parsing time. Let's get those and perfom
12843 the acces checks then. */
12844 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
12845 perform_deferred_access_checks ();
12846 pop_access_scope (fndecl);
12847 pop_deferring_access_checks ();
12848
12849 /* The DECL_TI_TEMPLATE should always be the immediate parent
12850 template, not the most general template. */
12851 DECL_TI_TEMPLATE (fndecl) = tmpl;
12852
12853 /* If we've just instantiated the main entry point for a function,
12854 instantiate all the alternate entry points as well. We do this
12855 by cloning the instantiation of the main entry point, not by
12856 instantiating the template clones. */
12857 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
12858 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
12859
12860 return fndecl;
12861 }
12862
12863 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
12864 NARGS elements of the arguments that are being used when calling
12865 it. TARGS is a vector into which the deduced template arguments
12866 are placed.
12867
12868 Return zero for success, 2 for an incomplete match that doesn't resolve
12869 all the types, and 1 for complete failure. An error message will be
12870 printed only for an incomplete match.
12871
12872 If FN is a conversion operator, or we are trying to produce a specific
12873 specialization, RETURN_TYPE is the return type desired.
12874
12875 The EXPLICIT_TARGS are explicit template arguments provided via a
12876 template-id.
12877
12878 The parameter STRICT is one of:
12879
12880 DEDUCE_CALL:
12881 We are deducing arguments for a function call, as in
12882 [temp.deduct.call].
12883
12884 DEDUCE_CONV:
12885 We are deducing arguments for a conversion function, as in
12886 [temp.deduct.conv].
12887
12888 DEDUCE_EXACT:
12889 We are deducing arguments when doing an explicit instantiation
12890 as in [temp.explicit], when determining an explicit specialization
12891 as in [temp.expl.spec], or when taking the address of a function
12892 template, as in [temp.deduct.funcaddr]. */
12893
12894 int
12895 fn_type_unification (tree fn,
12896 tree explicit_targs,
12897 tree targs,
12898 const tree *args,
12899 unsigned int nargs,
12900 tree return_type,
12901 unification_kind_t strict,
12902 int flags)
12903 {
12904 tree parms;
12905 tree fntype;
12906 int result;
12907 bool incomplete_argument_packs_p = false;
12908
12909 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
12910
12911 fntype = TREE_TYPE (fn);
12912 if (explicit_targs)
12913 {
12914 /* [temp.deduct]
12915
12916 The specified template arguments must match the template
12917 parameters in kind (i.e., type, nontype, template), and there
12918 must not be more arguments than there are parameters;
12919 otherwise type deduction fails.
12920
12921 Nontype arguments must match the types of the corresponding
12922 nontype template parameters, or must be convertible to the
12923 types of the corresponding nontype parameters as specified in
12924 _temp.arg.nontype_, otherwise type deduction fails.
12925
12926 All references in the function type of the function template
12927 to the corresponding template parameters are replaced by the
12928 specified template argument values. If a substitution in a
12929 template parameter or in the function type of the function
12930 template results in an invalid type, type deduction fails. */
12931 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
12932 int i, len = TREE_VEC_LENGTH (tparms);
12933 tree converted_args;
12934 bool incomplete = false;
12935
12936 if (explicit_targs == error_mark_node)
12937 return 1;
12938
12939 converted_args
12940 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
12941 /*require_all_args=*/false,
12942 /*use_default_args=*/false));
12943 if (converted_args == error_mark_node)
12944 return 1;
12945
12946 /* Substitute the explicit args into the function type. This is
12947 necessary so that, for instance, explicitly declared function
12948 arguments can match null pointed constants. If we were given
12949 an incomplete set of explicit args, we must not do semantic
12950 processing during substitution as we could create partial
12951 instantiations. */
12952 for (i = 0; i < len; i++)
12953 {
12954 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
12955 bool parameter_pack = false;
12956
12957 /* Dig out the actual parm. */
12958 if (TREE_CODE (parm) == TYPE_DECL
12959 || TREE_CODE (parm) == TEMPLATE_DECL)
12960 {
12961 parm = TREE_TYPE (parm);
12962 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
12963 }
12964 else if (TREE_CODE (parm) == PARM_DECL)
12965 {
12966 parm = DECL_INITIAL (parm);
12967 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
12968 }
12969
12970 if (parameter_pack)
12971 {
12972 int level, idx;
12973 tree targ;
12974 template_parm_level_and_index (parm, &level, &idx);
12975
12976 /* Mark the argument pack as "incomplete". We could
12977 still deduce more arguments during unification. */
12978 targ = TMPL_ARG (converted_args, level, idx);
12979 if (targ)
12980 {
12981 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
12982 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
12983 = ARGUMENT_PACK_ARGS (targ);
12984 }
12985
12986 /* We have some incomplete argument packs. */
12987 incomplete_argument_packs_p = true;
12988 }
12989 }
12990
12991 if (incomplete_argument_packs_p)
12992 /* Any substitution is guaranteed to be incomplete if there
12993 are incomplete argument packs, because we can still deduce
12994 more arguments. */
12995 incomplete = 1;
12996 else
12997 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
12998
12999 processing_template_decl += incomplete;
13000 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
13001 processing_template_decl -= incomplete;
13002
13003 if (fntype == error_mark_node)
13004 return 1;
13005
13006 /* Place the explicitly specified arguments in TARGS. */
13007 for (i = NUM_TMPL_ARGS (converted_args); i--;)
13008 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
13009 }
13010
13011 /* Never do unification on the 'this' parameter. */
13012 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
13013
13014 if (return_type)
13015 {
13016 tree *new_args;
13017
13018 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
13019 new_args = XALLOCAVEC (tree, nargs + 1);
13020 new_args[0] = return_type;
13021 memcpy (new_args + 1, args, nargs * sizeof (tree));
13022 args = new_args;
13023 ++nargs;
13024 }
13025
13026 /* We allow incomplete unification without an error message here
13027 because the standard doesn't seem to explicitly prohibit it. Our
13028 callers must be ready to deal with unification failures in any
13029 event. */
13030 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
13031 targs, parms, args, nargs, /*subr=*/0,
13032 strict, flags);
13033
13034 if (result == 0 && incomplete_argument_packs_p)
13035 {
13036 int i, len = NUM_TMPL_ARGS (targs);
13037
13038 /* Clear the "incomplete" flags on all argument packs. */
13039 for (i = 0; i < len; i++)
13040 {
13041 tree arg = TREE_VEC_ELT (targs, i);
13042 if (ARGUMENT_PACK_P (arg))
13043 {
13044 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
13045 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
13046 }
13047 }
13048 }
13049
13050 /* Now that we have bindings for all of the template arguments,
13051 ensure that the arguments deduced for the template template
13052 parameters have compatible template parameter lists. We cannot
13053 check this property before we have deduced all template
13054 arguments, because the template parameter types of a template
13055 template parameter might depend on prior template parameters
13056 deduced after the template template parameter. The following
13057 ill-formed example illustrates this issue:
13058
13059 template<typename T, template<T> class C> void f(C<5>, T);
13060
13061 template<int N> struct X {};
13062
13063 void g() {
13064 f(X<5>(), 5l); // error: template argument deduction fails
13065 }
13066
13067 The template parameter list of 'C' depends on the template type
13068 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13069 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13070 time that we deduce 'C'. */
13071 if (result == 0
13072 && !template_template_parm_bindings_ok_p
13073 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
13074 return 1;
13075
13076 if (result == 0)
13077 /* All is well so far. Now, check:
13078
13079 [temp.deduct]
13080
13081 When all template arguments have been deduced, all uses of
13082 template parameters in nondeduced contexts are replaced with
13083 the corresponding deduced argument values. If the
13084 substitution results in an invalid type, as described above,
13085 type deduction fails. */
13086 {
13087 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
13088 if (substed == error_mark_node)
13089 return 1;
13090
13091 /* If we're looking for an exact match, check that what we got
13092 is indeed an exact match. It might not be if some template
13093 parameters are used in non-deduced contexts. */
13094 if (strict == DEDUCE_EXACT)
13095 {
13096 unsigned int i;
13097
13098 tree sarg
13099 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
13100 if (return_type)
13101 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
13102 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
13103 if (!same_type_p (args[i], TREE_VALUE (sarg)))
13104 return 1;
13105 }
13106 }
13107
13108 return result;
13109 }
13110
13111 /* Adjust types before performing type deduction, as described in
13112 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13113 sections are symmetric. PARM is the type of a function parameter
13114 or the return type of the conversion function. ARG is the type of
13115 the argument passed to the call, or the type of the value
13116 initialized with the result of the conversion function.
13117 ARG_EXPR is the original argument expression, which may be null. */
13118
13119 static int
13120 maybe_adjust_types_for_deduction (unification_kind_t strict,
13121 tree* parm,
13122 tree* arg,
13123 tree arg_expr)
13124 {
13125 int result = 0;
13126
13127 switch (strict)
13128 {
13129 case DEDUCE_CALL:
13130 break;
13131
13132 case DEDUCE_CONV:
13133 {
13134 /* Swap PARM and ARG throughout the remainder of this
13135 function; the handling is precisely symmetric since PARM
13136 will initialize ARG rather than vice versa. */
13137 tree* temp = parm;
13138 parm = arg;
13139 arg = temp;
13140 break;
13141 }
13142
13143 case DEDUCE_EXACT:
13144 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13145 too, but here handle it by stripping the reference from PARM
13146 rather than by adding it to ARG. */
13147 if (TREE_CODE (*parm) == REFERENCE_TYPE
13148 && TYPE_REF_IS_RVALUE (*parm)
13149 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13150 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13151 && TREE_CODE (*arg) == REFERENCE_TYPE
13152 && !TYPE_REF_IS_RVALUE (*arg))
13153 *parm = TREE_TYPE (*parm);
13154 /* Nothing else to do in this case. */
13155 return 0;
13156
13157 default:
13158 gcc_unreachable ();
13159 }
13160
13161 if (TREE_CODE (*parm) != REFERENCE_TYPE)
13162 {
13163 /* [temp.deduct.call]
13164
13165 If P is not a reference type:
13166
13167 --If A is an array type, the pointer type produced by the
13168 array-to-pointer standard conversion (_conv.array_) is
13169 used in place of A for type deduction; otherwise,
13170
13171 --If A is a function type, the pointer type produced by
13172 the function-to-pointer standard conversion
13173 (_conv.func_) is used in place of A for type deduction;
13174 otherwise,
13175
13176 --If A is a cv-qualified type, the top level
13177 cv-qualifiers of A's type are ignored for type
13178 deduction. */
13179 if (TREE_CODE (*arg) == ARRAY_TYPE)
13180 *arg = build_pointer_type (TREE_TYPE (*arg));
13181 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
13182 *arg = build_pointer_type (*arg);
13183 else
13184 *arg = TYPE_MAIN_VARIANT (*arg);
13185 }
13186
13187 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13188 of the form T&&, where T is a template parameter, and the argument
13189 is an lvalue, T is deduced as A& */
13190 if (TREE_CODE (*parm) == REFERENCE_TYPE
13191 && TYPE_REF_IS_RVALUE (*parm)
13192 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13193 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13194 && arg_expr && real_lvalue_p (arg_expr))
13195 *arg = build_reference_type (*arg);
13196
13197 /* [temp.deduct.call]
13198
13199 If P is a cv-qualified type, the top level cv-qualifiers
13200 of P's type are ignored for type deduction. If P is a
13201 reference type, the type referred to by P is used for
13202 type deduction. */
13203 *parm = TYPE_MAIN_VARIANT (*parm);
13204 if (TREE_CODE (*parm) == REFERENCE_TYPE)
13205 {
13206 *parm = TREE_TYPE (*parm);
13207 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13208 }
13209
13210 /* DR 322. For conversion deduction, remove a reference type on parm
13211 too (which has been swapped into ARG). */
13212 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
13213 *arg = TREE_TYPE (*arg);
13214
13215 return result;
13216 }
13217
13218 /* Most parms like fn_type_unification.
13219
13220 If SUBR is 1, we're being called recursively (to unify the
13221 arguments of a function or method parameter of a function
13222 template). */
13223
13224 static int
13225 type_unification_real (tree tparms,
13226 tree targs,
13227 tree xparms,
13228 const tree *xargs,
13229 unsigned int xnargs,
13230 int subr,
13231 unification_kind_t strict,
13232 int flags)
13233 {
13234 tree parm, arg, arg_expr;
13235 int i;
13236 int ntparms = TREE_VEC_LENGTH (tparms);
13237 int sub_strict;
13238 int saw_undeduced = 0;
13239 tree parms;
13240 const tree *args;
13241 unsigned int nargs;
13242 unsigned int ia;
13243
13244 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
13245 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
13246 gcc_assert (ntparms > 0);
13247
13248 switch (strict)
13249 {
13250 case DEDUCE_CALL:
13251 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
13252 | UNIFY_ALLOW_DERIVED);
13253 break;
13254
13255 case DEDUCE_CONV:
13256 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13257 break;
13258
13259 case DEDUCE_EXACT:
13260 sub_strict = UNIFY_ALLOW_NONE;
13261 break;
13262
13263 default:
13264 gcc_unreachable ();
13265 }
13266
13267 again:
13268 parms = xparms;
13269 args = xargs;
13270 nargs = xnargs;
13271
13272 ia = 0;
13273 while (parms && parms != void_list_node
13274 && ia < nargs)
13275 {
13276 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13277 break;
13278
13279 parm = TREE_VALUE (parms);
13280 parms = TREE_CHAIN (parms);
13281 arg = args[ia];
13282 ++ia;
13283 arg_expr = NULL;
13284
13285 if (arg == error_mark_node)
13286 return 1;
13287 if (arg == unknown_type_node)
13288 /* We can't deduce anything from this, but we might get all the
13289 template args from other function args. */
13290 continue;
13291
13292 /* Conversions will be performed on a function argument that
13293 corresponds with a function parameter that contains only
13294 non-deducible template parameters and explicitly specified
13295 template parameters. */
13296 if (!uses_template_parms (parm))
13297 {
13298 tree type;
13299
13300 if (!TYPE_P (arg))
13301 type = TREE_TYPE (arg);
13302 else
13303 type = arg;
13304
13305 if (same_type_p (parm, type))
13306 continue;
13307 if (strict != DEDUCE_EXACT
13308 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
13309 flags))
13310 continue;
13311
13312 return 1;
13313 }
13314
13315 if (!TYPE_P (arg))
13316 {
13317 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13318 if (type_unknown_p (arg))
13319 {
13320 /* [temp.deduct.type]
13321
13322 A template-argument can be deduced from a pointer to
13323 function or pointer to member function argument if
13324 the set of overloaded functions does not contain
13325 function templates and at most one of a set of
13326 overloaded functions provides a unique match. */
13327 if (resolve_overloaded_unification
13328 (tparms, targs, parm, arg, strict, sub_strict))
13329 continue;
13330
13331 return 1;
13332 }
13333 arg_expr = arg;
13334 arg = unlowered_expr_type (arg);
13335 if (arg == error_mark_node)
13336 return 1;
13337 }
13338
13339 {
13340 int arg_strict = sub_strict;
13341
13342 if (!subr)
13343 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
13344 arg_expr);
13345
13346 if (arg == init_list_type_node && arg_expr)
13347 arg = arg_expr;
13348 if (unify (tparms, targs, parm, arg, arg_strict))
13349 return 1;
13350 }
13351 }
13352
13353
13354 if (parms
13355 && parms != void_list_node
13356 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13357 {
13358 /* Unify the remaining arguments with the pack expansion type. */
13359 tree argvec;
13360 tree parmvec = make_tree_vec (1);
13361
13362 /* Allocate a TREE_VEC and copy in all of the arguments */
13363 argvec = make_tree_vec (nargs - ia);
13364 for (i = 0; ia < nargs; ++ia, ++i)
13365 TREE_VEC_ELT (argvec, i) = args[ia];
13366
13367 /* Copy the parameter into parmvec. */
13368 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
13369 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
13370 /*call_args_p=*/true, /*subr=*/subr))
13371 return 1;
13372
13373 /* Advance to the end of the list of parameters. */
13374 parms = TREE_CHAIN (parms);
13375 }
13376
13377 /* Fail if we've reached the end of the parm list, and more args
13378 are present, and the parm list isn't variadic. */
13379 if (ia < nargs && parms == void_list_node)
13380 return 1;
13381 /* Fail if parms are left and they don't have default values. */
13382 if (parms && parms != void_list_node
13383 && TREE_PURPOSE (parms) == NULL_TREE)
13384 return 1;
13385
13386 if (!subr)
13387 for (i = 0; i < ntparms; i++)
13388 if (!TREE_VEC_ELT (targs, i))
13389 {
13390 tree tparm;
13391
13392 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
13393 continue;
13394
13395 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13396
13397 /* If this is an undeduced nontype parameter that depends on
13398 a type parameter, try another pass; its type may have been
13399 deduced from a later argument than the one from which
13400 this parameter can be deduced. */
13401 if (TREE_CODE (tparm) == PARM_DECL
13402 && uses_template_parms (TREE_TYPE (tparm))
13403 && !saw_undeduced++)
13404 goto again;
13405
13406 /* Core issue #226 (C++0x) [temp.deduct]:
13407
13408 If a template argument has not been deduced, its
13409 default template argument, if any, is used.
13410
13411 When we are in C++98 mode, TREE_PURPOSE will either
13412 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13413 to explicitly check cxx_dialect here. */
13414 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
13415 {
13416 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13417 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
13418 arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
13419 arg = convert_template_argument (parm, arg, targs, tf_none,
13420 i, NULL_TREE);
13421 if (arg == error_mark_node)
13422 return 1;
13423 else
13424 {
13425 TREE_VEC_ELT (targs, i) = arg;
13426 continue;
13427 }
13428 }
13429
13430 /* If the type parameter is a parameter pack, then it will
13431 be deduced to an empty parameter pack. */
13432 if (template_parameter_pack_p (tparm))
13433 {
13434 tree arg;
13435
13436 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
13437 {
13438 arg = make_node (NONTYPE_ARGUMENT_PACK);
13439 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
13440 TREE_CONSTANT (arg) = 1;
13441 }
13442 else
13443 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
13444
13445 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
13446
13447 TREE_VEC_ELT (targs, i) = arg;
13448 continue;
13449 }
13450
13451 return 2;
13452 }
13453
13454 return 0;
13455 }
13456
13457 /* Subroutine of type_unification_real. Args are like the variables
13458 at the call site. ARG is an overloaded function (or template-id);
13459 we try deducing template args from each of the overloads, and if
13460 only one succeeds, we go with that. Modifies TARGS and returns
13461 true on success. */
13462
13463 static bool
13464 resolve_overloaded_unification (tree tparms,
13465 tree targs,
13466 tree parm,
13467 tree arg,
13468 unification_kind_t strict,
13469 int sub_strict)
13470 {
13471 tree tempargs = copy_node (targs);
13472 int good = 0;
13473 tree goodfn = NULL_TREE;
13474 bool addr_p;
13475
13476 if (TREE_CODE (arg) == ADDR_EXPR)
13477 {
13478 arg = TREE_OPERAND (arg, 0);
13479 addr_p = true;
13480 }
13481 else
13482 addr_p = false;
13483
13484 if (TREE_CODE (arg) == COMPONENT_REF)
13485 /* Handle `&x' where `x' is some static or non-static member
13486 function name. */
13487 arg = TREE_OPERAND (arg, 1);
13488
13489 if (TREE_CODE (arg) == OFFSET_REF)
13490 arg = TREE_OPERAND (arg, 1);
13491
13492 /* Strip baselink information. */
13493 if (BASELINK_P (arg))
13494 arg = BASELINK_FUNCTIONS (arg);
13495
13496 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
13497 {
13498 /* If we got some explicit template args, we need to plug them into
13499 the affected templates before we try to unify, in case the
13500 explicit args will completely resolve the templates in question. */
13501
13502 tree expl_subargs = TREE_OPERAND (arg, 1);
13503 arg = TREE_OPERAND (arg, 0);
13504
13505 for (; arg; arg = OVL_NEXT (arg))
13506 {
13507 tree fn = OVL_CURRENT (arg);
13508 tree subargs, elem;
13509
13510 if (TREE_CODE (fn) != TEMPLATE_DECL)
13511 continue;
13512
13513 ++processing_template_decl;
13514 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13515 expl_subargs, /*check_ret=*/false);
13516 if (subargs)
13517 {
13518 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
13519 if (try_one_overload (tparms, targs, tempargs, parm,
13520 elem, strict, sub_strict, addr_p)
13521 && (!goodfn || !decls_match (goodfn, elem)))
13522 {
13523 goodfn = elem;
13524 ++good;
13525 }
13526 }
13527 --processing_template_decl;
13528 }
13529 }
13530 else if (TREE_CODE (arg) != OVERLOAD
13531 && TREE_CODE (arg) != FUNCTION_DECL)
13532 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13533 -- but the deduction does not succeed because the expression is
13534 not just the function on its own. */
13535 return false;
13536 else
13537 for (; arg; arg = OVL_NEXT (arg))
13538 if (try_one_overload (tparms, targs, tempargs, parm,
13539 TREE_TYPE (OVL_CURRENT (arg)),
13540 strict, sub_strict, addr_p)
13541 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
13542 {
13543 goodfn = OVL_CURRENT (arg);
13544 ++good;
13545 }
13546
13547 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13548 to function or pointer to member function argument if the set of
13549 overloaded functions does not contain function templates and at most
13550 one of a set of overloaded functions provides a unique match.
13551
13552 So if we found multiple possibilities, we return success but don't
13553 deduce anything. */
13554
13555 if (good == 1)
13556 {
13557 int i = TREE_VEC_LENGTH (targs);
13558 for (; i--; )
13559 if (TREE_VEC_ELT (tempargs, i))
13560 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
13561 }
13562 if (good)
13563 return true;
13564
13565 return false;
13566 }
13567
13568 /* Core DR 115: In contexts where deduction is done and fails, or in
13569 contexts where deduction is not done, if a template argument list is
13570 specified and it, along with any default template arguments, identifies
13571 a single function template specialization, then the template-id is an
13572 lvalue for the function template specialization. */
13573
13574 tree
13575 resolve_nondeduced_context (tree orig_expr)
13576 {
13577 tree expr, offset, baselink;
13578 bool addr;
13579
13580 if (!type_unknown_p (orig_expr))
13581 return orig_expr;
13582
13583 expr = orig_expr;
13584 addr = false;
13585 offset = NULL_TREE;
13586 baselink = NULL_TREE;
13587
13588 if (TREE_CODE (expr) == ADDR_EXPR)
13589 {
13590 expr = TREE_OPERAND (expr, 0);
13591 addr = true;
13592 }
13593 if (TREE_CODE (expr) == OFFSET_REF)
13594 {
13595 offset = expr;
13596 expr = TREE_OPERAND (expr, 1);
13597 }
13598 if (TREE_CODE (expr) == BASELINK)
13599 {
13600 baselink = expr;
13601 expr = BASELINK_FUNCTIONS (expr);
13602 }
13603
13604 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
13605 {
13606 int good = 0;
13607 tree goodfn = NULL_TREE;
13608
13609 /* If we got some explicit template args, we need to plug them into
13610 the affected templates before we try to unify, in case the
13611 explicit args will completely resolve the templates in question. */
13612
13613 tree expl_subargs = TREE_OPERAND (expr, 1);
13614 tree arg = TREE_OPERAND (expr, 0);
13615 tree badfn = NULL_TREE;
13616 tree badargs = NULL_TREE;
13617
13618 for (; arg; arg = OVL_NEXT (arg))
13619 {
13620 tree fn = OVL_CURRENT (arg);
13621 tree subargs, elem;
13622
13623 if (TREE_CODE (fn) != TEMPLATE_DECL)
13624 continue;
13625
13626 ++processing_template_decl;
13627 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13628 expl_subargs, /*check_ret=*/false);
13629 if (subargs && !any_dependent_template_arguments_p (subargs))
13630 {
13631 elem = instantiate_template (fn, subargs, tf_none);
13632 if (elem == error_mark_node)
13633 {
13634 badfn = fn;
13635 badargs = subargs;
13636 }
13637 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
13638 {
13639 goodfn = elem;
13640 ++good;
13641 }
13642 }
13643 --processing_template_decl;
13644 }
13645 if (good == 1)
13646 {
13647 expr = goodfn;
13648 if (baselink)
13649 expr = build_baselink (BASELINK_BINFO (baselink),
13650 BASELINK_ACCESS_BINFO (baselink),
13651 expr, BASELINK_OPTYPE (baselink));
13652 if (offset)
13653 expr = build2 (OFFSET_REF, TREE_TYPE (expr),
13654 TREE_OPERAND (offset, 0), expr);
13655 if (addr)
13656 expr = build_address (expr);
13657 return expr;
13658 }
13659 else if (good == 0 && badargs)
13660 /* There were no good options and at least one bad one, so let the
13661 user know what the problem is. */
13662 instantiate_template (badfn, badargs, tf_warning_or_error);
13663 }
13664 return orig_expr;
13665 }
13666
13667 /* Subroutine of resolve_overloaded_unification; does deduction for a single
13668 overload. Fills TARGS with any deduced arguments, or error_mark_node if
13669 different overloads deduce different arguments for a given parm.
13670 ADDR_P is true if the expression for which deduction is being
13671 performed was of the form "& fn" rather than simply "fn".
13672
13673 Returns 1 on success. */
13674
13675 static int
13676 try_one_overload (tree tparms,
13677 tree orig_targs,
13678 tree targs,
13679 tree parm,
13680 tree arg,
13681 unification_kind_t strict,
13682 int sub_strict,
13683 bool addr_p)
13684 {
13685 int nargs;
13686 tree tempargs;
13687 int i;
13688
13689 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13690 to function or pointer to member function argument if the set of
13691 overloaded functions does not contain function templates and at most
13692 one of a set of overloaded functions provides a unique match.
13693
13694 So if this is a template, just return success. */
13695
13696 if (uses_template_parms (arg))
13697 return 1;
13698
13699 if (TREE_CODE (arg) == METHOD_TYPE)
13700 arg = build_ptrmemfunc_type (build_pointer_type (arg));
13701 else if (addr_p)
13702 arg = build_pointer_type (arg);
13703
13704 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
13705
13706 /* We don't copy orig_targs for this because if we have already deduced
13707 some template args from previous args, unify would complain when we
13708 try to deduce a template parameter for the same argument, even though
13709 there isn't really a conflict. */
13710 nargs = TREE_VEC_LENGTH (targs);
13711 tempargs = make_tree_vec (nargs);
13712
13713 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
13714 return 0;
13715
13716 /* First make sure we didn't deduce anything that conflicts with
13717 explicitly specified args. */
13718 for (i = nargs; i--; )
13719 {
13720 tree elt = TREE_VEC_ELT (tempargs, i);
13721 tree oldelt = TREE_VEC_ELT (orig_targs, i);
13722
13723 if (!elt)
13724 /*NOP*/;
13725 else if (uses_template_parms (elt))
13726 /* Since we're unifying against ourselves, we will fill in
13727 template args used in the function parm list with our own
13728 template parms. Discard them. */
13729 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
13730 else if (oldelt && !template_args_equal (oldelt, elt))
13731 return 0;
13732 }
13733
13734 for (i = nargs; i--; )
13735 {
13736 tree elt = TREE_VEC_ELT (tempargs, i);
13737
13738 if (elt)
13739 TREE_VEC_ELT (targs, i) = elt;
13740 }
13741
13742 return 1;
13743 }
13744
13745 /* PARM is a template class (perhaps with unbound template
13746 parameters). ARG is a fully instantiated type. If ARG can be
13747 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
13748 TARGS are as for unify. */
13749
13750 static tree
13751 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
13752 {
13753 tree copy_of_targs;
13754
13755 if (!CLASSTYPE_TEMPLATE_INFO (arg)
13756 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
13757 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
13758 return NULL_TREE;
13759
13760 /* We need to make a new template argument vector for the call to
13761 unify. If we used TARGS, we'd clutter it up with the result of
13762 the attempted unification, even if this class didn't work out.
13763 We also don't want to commit ourselves to all the unifications
13764 we've already done, since unification is supposed to be done on
13765 an argument-by-argument basis. In other words, consider the
13766 following pathological case:
13767
13768 template <int I, int J, int K>
13769 struct S {};
13770
13771 template <int I, int J>
13772 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
13773
13774 template <int I, int J, int K>
13775 void f(S<I, J, K>, S<I, I, I>);
13776
13777 void g() {
13778 S<0, 0, 0> s0;
13779 S<0, 1, 2> s2;
13780
13781 f(s0, s2);
13782 }
13783
13784 Now, by the time we consider the unification involving `s2', we
13785 already know that we must have `f<0, 0, 0>'. But, even though
13786 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
13787 because there are two ways to unify base classes of S<0, 1, 2>
13788 with S<I, I, I>. If we kept the already deduced knowledge, we
13789 would reject the possibility I=1. */
13790 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
13791
13792 /* If unification failed, we're done. */
13793 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
13794 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
13795 return NULL_TREE;
13796
13797 return arg;
13798 }
13799
13800 /* Given a template type PARM and a class type ARG, find the unique
13801 base type in ARG that is an instance of PARM. We do not examine
13802 ARG itself; only its base-classes. If there is not exactly one
13803 appropriate base class, return NULL_TREE. PARM may be the type of
13804 a partial specialization, as well as a plain template type. Used
13805 by unify. */
13806
13807 static tree
13808 get_template_base (tree tparms, tree targs, tree parm, tree arg)
13809 {
13810 tree rval = NULL_TREE;
13811 tree binfo;
13812
13813 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
13814
13815 binfo = TYPE_BINFO (complete_type (arg));
13816 if (!binfo)
13817 /* The type could not be completed. */
13818 return NULL_TREE;
13819
13820 /* Walk in inheritance graph order. The search order is not
13821 important, and this avoids multiple walks of virtual bases. */
13822 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
13823 {
13824 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
13825
13826 if (r)
13827 {
13828 /* If there is more than one satisfactory baseclass, then:
13829
13830 [temp.deduct.call]
13831
13832 If they yield more than one possible deduced A, the type
13833 deduction fails.
13834
13835 applies. */
13836 if (rval && !same_type_p (r, rval))
13837 return NULL_TREE;
13838
13839 rval = r;
13840 }
13841 }
13842
13843 return rval;
13844 }
13845
13846 /* Returns the level of DECL, which declares a template parameter. */
13847
13848 static int
13849 template_decl_level (tree decl)
13850 {
13851 switch (TREE_CODE (decl))
13852 {
13853 case TYPE_DECL:
13854 case TEMPLATE_DECL:
13855 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
13856
13857 case PARM_DECL:
13858 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
13859
13860 default:
13861 gcc_unreachable ();
13862 }
13863 return 0;
13864 }
13865
13866 /* Decide whether ARG can be unified with PARM, considering only the
13867 cv-qualifiers of each type, given STRICT as documented for unify.
13868 Returns nonzero iff the unification is OK on that basis. */
13869
13870 static int
13871 check_cv_quals_for_unify (int strict, tree arg, tree parm)
13872 {
13873 int arg_quals = cp_type_quals (arg);
13874 int parm_quals = cp_type_quals (parm);
13875
13876 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13877 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13878 {
13879 /* Although a CVR qualifier is ignored when being applied to a
13880 substituted template parameter ([8.3.2]/1 for example), that
13881 does not apply during deduction [14.8.2.4]/1, (even though
13882 that is not explicitly mentioned, [14.8.2.4]/9 indicates
13883 this). Except when we're allowing additional CV qualifiers
13884 at the outer level [14.8.2.1]/3,1st bullet. */
13885 if ((TREE_CODE (arg) == REFERENCE_TYPE
13886 || TREE_CODE (arg) == FUNCTION_TYPE
13887 || TREE_CODE (arg) == METHOD_TYPE)
13888 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
13889 return 0;
13890
13891 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
13892 && (parm_quals & TYPE_QUAL_RESTRICT))
13893 return 0;
13894 }
13895
13896 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13897 && (arg_quals & parm_quals) != parm_quals)
13898 return 0;
13899
13900 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
13901 && (parm_quals & arg_quals) != arg_quals)
13902 return 0;
13903
13904 return 1;
13905 }
13906
13907 /* Determines the LEVEL and INDEX for the template parameter PARM. */
13908 void
13909 template_parm_level_and_index (tree parm, int* level, int* index)
13910 {
13911 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13912 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13913 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13914 {
13915 *index = TEMPLATE_TYPE_IDX (parm);
13916 *level = TEMPLATE_TYPE_LEVEL (parm);
13917 }
13918 else
13919 {
13920 *index = TEMPLATE_PARM_IDX (parm);
13921 *level = TEMPLATE_PARM_LEVEL (parm);
13922 }
13923 }
13924
13925 /* Unifies the remaining arguments in PACKED_ARGS with the pack
13926 expansion at the end of PACKED_PARMS. Returns 0 if the type
13927 deduction succeeds, 1 otherwise. STRICT is the same as in
13928 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
13929 call argument list. We'll need to adjust the arguments to make them
13930 types. SUBR tells us if this is from a recursive call to
13931 type_unification_real. */
13932 int
13933 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
13934 tree packed_args, int strict, bool call_args_p,
13935 bool subr)
13936 {
13937 tree parm
13938 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
13939 tree pattern = PACK_EXPANSION_PATTERN (parm);
13940 tree pack, packs = NULL_TREE;
13941 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
13942 int len = TREE_VEC_LENGTH (packed_args);
13943
13944 /* Determine the parameter packs we will be deducing from the
13945 pattern, and record their current deductions. */
13946 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
13947 pack; pack = TREE_CHAIN (pack))
13948 {
13949 tree parm_pack = TREE_VALUE (pack);
13950 int idx, level;
13951
13952 /* Determine the index and level of this parameter pack. */
13953 template_parm_level_and_index (parm_pack, &level, &idx);
13954
13955 /* Keep track of the parameter packs and their corresponding
13956 argument packs. */
13957 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
13958 TREE_TYPE (packs) = make_tree_vec (len - start);
13959 }
13960
13961 /* Loop through all of the arguments that have not yet been
13962 unified and unify each with the pattern. */
13963 for (i = start; i < len; i++)
13964 {
13965 tree parm = pattern;
13966
13967 /* For each parameter pack, clear out the deduced value so that
13968 we can deduce it again. */
13969 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13970 {
13971 int idx, level;
13972 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13973
13974 TMPL_ARG (targs, level, idx) = NULL_TREE;
13975 }
13976
13977 /* Unify the pattern with the current argument. */
13978 {
13979 tree arg = TREE_VEC_ELT (packed_args, i);
13980 tree arg_expr = NULL_TREE;
13981 int arg_strict = strict;
13982 bool skip_arg_p = false;
13983
13984 if (call_args_p)
13985 {
13986 int sub_strict;
13987
13988 /* This mirrors what we do in type_unification_real. */
13989 switch (strict)
13990 {
13991 case DEDUCE_CALL:
13992 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
13993 | UNIFY_ALLOW_MORE_CV_QUAL
13994 | UNIFY_ALLOW_DERIVED);
13995 break;
13996
13997 case DEDUCE_CONV:
13998 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13999 break;
14000
14001 case DEDUCE_EXACT:
14002 sub_strict = UNIFY_ALLOW_NONE;
14003 break;
14004
14005 default:
14006 gcc_unreachable ();
14007 }
14008
14009 if (!TYPE_P (arg))
14010 {
14011 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14012 if (type_unknown_p (arg))
14013 {
14014 /* [temp.deduct.type] A template-argument can be
14015 deduced from a pointer to function or pointer
14016 to member function argument if the set of
14017 overloaded functions does not contain function
14018 templates and at most one of a set of
14019 overloaded functions provides a unique
14020 match. */
14021
14022 if (resolve_overloaded_unification
14023 (tparms, targs, parm, arg,
14024 (unification_kind_t) strict,
14025 sub_strict)
14026 != 0)
14027 return 1;
14028 skip_arg_p = true;
14029 }
14030
14031 if (!skip_arg_p)
14032 {
14033 arg_expr = arg;
14034 arg = unlowered_expr_type (arg);
14035 if (arg == error_mark_node)
14036 return 1;
14037 }
14038 }
14039
14040 arg_strict = sub_strict;
14041
14042 if (!subr)
14043 arg_strict |=
14044 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
14045 &parm, &arg, arg_expr);
14046 }
14047
14048 if (!skip_arg_p)
14049 {
14050 /* For deduction from an init-list we need the actual list. */
14051 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
14052 arg = arg_expr;
14053 if (unify (tparms, targs, parm, arg, arg_strict))
14054 return 1;
14055 }
14056 }
14057
14058 /* For each parameter pack, collect the deduced value. */
14059 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14060 {
14061 int idx, level;
14062 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14063
14064 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
14065 TMPL_ARG (targs, level, idx);
14066 }
14067 }
14068
14069 /* Verify that the results of unification with the parameter packs
14070 produce results consistent with what we've seen before, and make
14071 the deduced argument packs available. */
14072 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14073 {
14074 tree old_pack = TREE_VALUE (pack);
14075 tree new_args = TREE_TYPE (pack);
14076 int i, len = TREE_VEC_LENGTH (new_args);
14077 int idx, level;
14078 bool nondeduced_p = false;
14079
14080 /* By default keep the original deduced argument pack.
14081 If necessary, more specific code is going to update the
14082 resulting deduced argument later down in this function. */
14083 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14084 TMPL_ARG (targs, level, idx) = old_pack;
14085
14086 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14087 actually deduce anything. */
14088 for (i = 0; i < len && !nondeduced_p; ++i)
14089 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
14090 nondeduced_p = true;
14091 if (nondeduced_p)
14092 continue;
14093
14094 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
14095 {
14096 /* Prepend the explicit arguments onto NEW_ARGS. */
14097 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14098 tree old_args = new_args;
14099 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
14100 int len = explicit_len + TREE_VEC_LENGTH (old_args);
14101
14102 /* Copy the explicit arguments. */
14103 new_args = make_tree_vec (len);
14104 for (i = 0; i < explicit_len; i++)
14105 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
14106
14107 /* Copy the deduced arguments. */
14108 for (; i < len; i++)
14109 TREE_VEC_ELT (new_args, i) =
14110 TREE_VEC_ELT (old_args, i - explicit_len);
14111 }
14112
14113 if (!old_pack)
14114 {
14115 tree result;
14116 /* Build the deduced *_ARGUMENT_PACK. */
14117 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
14118 {
14119 result = make_node (NONTYPE_ARGUMENT_PACK);
14120 TREE_TYPE (result) =
14121 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
14122 TREE_CONSTANT (result) = 1;
14123 }
14124 else
14125 result = cxx_make_type (TYPE_ARGUMENT_PACK);
14126
14127 SET_ARGUMENT_PACK_ARGS (result, new_args);
14128
14129 /* Note the deduced argument packs for this parameter
14130 pack. */
14131 TMPL_ARG (targs, level, idx) = result;
14132 }
14133 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
14134 && (ARGUMENT_PACK_ARGS (old_pack)
14135 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
14136 {
14137 /* We only had the explicitly-provided arguments before, but
14138 now we have a complete set of arguments. */
14139 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14140
14141 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
14142 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
14143 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
14144 }
14145 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
14146 new_args))
14147 /* Inconsistent unification of this parameter pack. */
14148 return 1;
14149 }
14150
14151 return 0;
14152 }
14153
14154 /* Deduce the value of template parameters. TPARMS is the (innermost)
14155 set of template parameters to a template. TARGS is the bindings
14156 for those template parameters, as determined thus far; TARGS may
14157 include template arguments for outer levels of template parameters
14158 as well. PARM is a parameter to a template function, or a
14159 subcomponent of that parameter; ARG is the corresponding argument.
14160 This function attempts to match PARM with ARG in a manner
14161 consistent with the existing assignments in TARGS. If more values
14162 are deduced, then TARGS is updated.
14163
14164 Returns 0 if the type deduction succeeds, 1 otherwise. The
14165 parameter STRICT is a bitwise or of the following flags:
14166
14167 UNIFY_ALLOW_NONE:
14168 Require an exact match between PARM and ARG.
14169 UNIFY_ALLOW_MORE_CV_QUAL:
14170 Allow the deduced ARG to be more cv-qualified (by qualification
14171 conversion) than ARG.
14172 UNIFY_ALLOW_LESS_CV_QUAL:
14173 Allow the deduced ARG to be less cv-qualified than ARG.
14174 UNIFY_ALLOW_DERIVED:
14175 Allow the deduced ARG to be a template base class of ARG,
14176 or a pointer to a template base class of the type pointed to by
14177 ARG.
14178 UNIFY_ALLOW_INTEGER:
14179 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14180 case for more information.
14181 UNIFY_ALLOW_OUTER_LEVEL:
14182 This is the outermost level of a deduction. Used to determine validity
14183 of qualification conversions. A valid qualification conversion must
14184 have const qualified pointers leading up to the inner type which
14185 requires additional CV quals, except at the outer level, where const
14186 is not required [conv.qual]. It would be normal to set this flag in
14187 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14188 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14189 This is the outermost level of a deduction, and PARM can be more CV
14190 qualified at this point.
14191 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14192 This is the outermost level of a deduction, and PARM can be less CV
14193 qualified at this point. */
14194
14195 static int
14196 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
14197 {
14198 int idx;
14199 tree targ;
14200 tree tparm;
14201 int strict_in = strict;
14202
14203 /* I don't think this will do the right thing with respect to types.
14204 But the only case I've seen it in so far has been array bounds, where
14205 signedness is the only information lost, and I think that will be
14206 okay. */
14207 while (TREE_CODE (parm) == NOP_EXPR)
14208 parm = TREE_OPERAND (parm, 0);
14209
14210 if (arg == error_mark_node)
14211 return 1;
14212 if (arg == unknown_type_node
14213 || arg == init_list_type_node)
14214 /* We can't deduce anything from this, but we might get all the
14215 template args from other function args. */
14216 return 0;
14217
14218 /* If PARM uses template parameters, then we can't bail out here,
14219 even if ARG == PARM, since we won't record unifications for the
14220 template parameters. We might need them if we're trying to
14221 figure out which of two things is more specialized. */
14222 if (arg == parm && !uses_template_parms (parm))
14223 return 0;
14224
14225 /* Handle init lists early, so the rest of the function can assume
14226 we're dealing with a type. */
14227 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
14228 {
14229 tree elt, elttype;
14230 unsigned i;
14231 tree orig_parm = parm;
14232
14233 /* Replace T with std::initializer_list<T> for deduction. */
14234 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14235 && flag_deduce_init_list)
14236 parm = listify (parm);
14237
14238 if (!is_std_init_list (parm))
14239 /* We can only deduce from an initializer list argument if the
14240 parameter is std::initializer_list; otherwise this is a
14241 non-deduced context. */
14242 return 0;
14243
14244 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
14245
14246 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
14247 {
14248 int elt_strict = strict;
14249 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
14250 {
14251 tree type = TREE_TYPE (elt);
14252 /* It should only be possible to get here for a call. */
14253 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
14254 elt_strict |= maybe_adjust_types_for_deduction
14255 (DEDUCE_CALL, &elttype, &type, elt);
14256 elt = type;
14257 }
14258
14259 if (unify (tparms, targs, elttype, elt, elt_strict))
14260 return 1;
14261 }
14262
14263 /* If the std::initializer_list<T> deduction worked, replace the
14264 deduced A with std::initializer_list<A>. */
14265 if (orig_parm != parm)
14266 {
14267 idx = TEMPLATE_TYPE_IDX (orig_parm);
14268 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14269 targ = listify (targ);
14270 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
14271 }
14272 return 0;
14273 }
14274
14275 /* Immediately reject some pairs that won't unify because of
14276 cv-qualification mismatches. */
14277 if (TREE_CODE (arg) == TREE_CODE (parm)
14278 && TYPE_P (arg)
14279 /* It is the elements of the array which hold the cv quals of an array
14280 type, and the elements might be template type parms. We'll check
14281 when we recurse. */
14282 && TREE_CODE (arg) != ARRAY_TYPE
14283 /* We check the cv-qualifiers when unifying with template type
14284 parameters below. We want to allow ARG `const T' to unify with
14285 PARM `T' for example, when computing which of two templates
14286 is more specialized, for example. */
14287 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
14288 && !check_cv_quals_for_unify (strict_in, arg, parm))
14289 return 1;
14290
14291 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
14292 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
14293 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
14294 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
14295 strict &= ~UNIFY_ALLOW_DERIVED;
14296 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14297 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
14298
14299 switch (TREE_CODE (parm))
14300 {
14301 case TYPENAME_TYPE:
14302 case SCOPE_REF:
14303 case UNBOUND_CLASS_TEMPLATE:
14304 /* In a type which contains a nested-name-specifier, template
14305 argument values cannot be deduced for template parameters used
14306 within the nested-name-specifier. */
14307 return 0;
14308
14309 case TEMPLATE_TYPE_PARM:
14310 case TEMPLATE_TEMPLATE_PARM:
14311 case BOUND_TEMPLATE_TEMPLATE_PARM:
14312 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14313 if (tparm == error_mark_node)
14314 return 1;
14315
14316 if (TEMPLATE_TYPE_LEVEL (parm)
14317 != template_decl_level (tparm))
14318 /* The PARM is not one we're trying to unify. Just check
14319 to see if it matches ARG. */
14320 return (TREE_CODE (arg) == TREE_CODE (parm)
14321 && same_type_p (parm, arg)) ? 0 : 1;
14322 idx = TEMPLATE_TYPE_IDX (parm);
14323 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14324 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
14325
14326 /* Check for mixed types and values. */
14327 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14328 && TREE_CODE (tparm) != TYPE_DECL)
14329 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14330 && TREE_CODE (tparm) != TEMPLATE_DECL))
14331 return 1;
14332
14333 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14334 {
14335 /* ARG must be constructed from a template class or a template
14336 template parameter. */
14337 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
14338 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
14339 return 1;
14340
14341 {
14342 tree parmvec = TYPE_TI_ARGS (parm);
14343 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
14344 tree parm_parms
14345 = DECL_INNERMOST_TEMPLATE_PARMS
14346 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
14347 int i, len;
14348 int parm_variadic_p = 0;
14349
14350 /* The resolution to DR150 makes clear that default
14351 arguments for an N-argument may not be used to bind T
14352 to a template template parameter with fewer than N
14353 parameters. It is not safe to permit the binding of
14354 default arguments as an extension, as that may change
14355 the meaning of a conforming program. Consider:
14356
14357 struct Dense { static const unsigned int dim = 1; };
14358
14359 template <template <typename> class View,
14360 typename Block>
14361 void operator+(float, View<Block> const&);
14362
14363 template <typename Block,
14364 unsigned int Dim = Block::dim>
14365 struct Lvalue_proxy { operator float() const; };
14366
14367 void
14368 test_1d (void) {
14369 Lvalue_proxy<Dense> p;
14370 float b;
14371 b + p;
14372 }
14373
14374 Here, if Lvalue_proxy is permitted to bind to View, then
14375 the global operator+ will be used; if they are not, the
14376 Lvalue_proxy will be converted to float. */
14377 if (coerce_template_parms (parm_parms,
14378 argvec,
14379 TYPE_TI_TEMPLATE (parm),
14380 tf_none,
14381 /*require_all_args=*/true,
14382 /*use_default_args=*/false)
14383 == error_mark_node)
14384 return 1;
14385
14386 /* Deduce arguments T, i from TT<T> or TT<i>.
14387 We check each element of PARMVEC and ARGVEC individually
14388 rather than the whole TREE_VEC since they can have
14389 different number of elements. */
14390
14391 parmvec = expand_template_argument_pack (parmvec);
14392 argvec = expand_template_argument_pack (argvec);
14393
14394 len = TREE_VEC_LENGTH (parmvec);
14395
14396 /* Check if the parameters end in a pack, making them
14397 variadic. */
14398 if (len > 0
14399 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
14400 parm_variadic_p = 1;
14401
14402 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
14403 return 1;
14404
14405 for (i = 0; i < len - parm_variadic_p; ++i)
14406 {
14407 if (unify (tparms, targs,
14408 TREE_VEC_ELT (parmvec, i),
14409 TREE_VEC_ELT (argvec, i),
14410 UNIFY_ALLOW_NONE))
14411 return 1;
14412 }
14413
14414 if (parm_variadic_p
14415 && unify_pack_expansion (tparms, targs,
14416 parmvec, argvec,
14417 UNIFY_ALLOW_NONE,
14418 /*call_args_p=*/false,
14419 /*subr=*/false))
14420 return 1;
14421 }
14422 arg = TYPE_TI_TEMPLATE (arg);
14423
14424 /* Fall through to deduce template name. */
14425 }
14426
14427 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14428 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14429 {
14430 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14431
14432 /* Simple cases: Value already set, does match or doesn't. */
14433 if (targ != NULL_TREE && template_args_equal (targ, arg))
14434 return 0;
14435 else if (targ)
14436 return 1;
14437 }
14438 else
14439 {
14440 /* If PARM is `const T' and ARG is only `int', we don't have
14441 a match unless we are allowing additional qualification.
14442 If ARG is `const int' and PARM is just `T' that's OK;
14443 that binds `const int' to `T'. */
14444 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
14445 arg, parm))
14446 return 1;
14447
14448 /* Consider the case where ARG is `const volatile int' and
14449 PARM is `const T'. Then, T should be `volatile int'. */
14450 arg = cp_build_qualified_type_real
14451 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
14452 if (arg == error_mark_node)
14453 return 1;
14454
14455 /* Simple cases: Value already set, does match or doesn't. */
14456 if (targ != NULL_TREE && same_type_p (targ, arg))
14457 return 0;
14458 else if (targ)
14459 return 1;
14460
14461 /* Make sure that ARG is not a variable-sized array. (Note
14462 that were talking about variable-sized arrays (like
14463 `int[n]'), rather than arrays of unknown size (like
14464 `int[]').) We'll get very confused by such a type since
14465 the bound of the array will not be computable in an
14466 instantiation. Besides, such types are not allowed in
14467 ISO C++, so we can do as we please here. */
14468 if (variably_modified_type_p (arg, NULL_TREE))
14469 return 1;
14470
14471 /* Strip typedefs as in convert_template_argument. */
14472 arg = strip_typedefs (arg);
14473 }
14474
14475 /* If ARG is a parameter pack or an expansion, we cannot unify
14476 against it unless PARM is also a parameter pack. */
14477 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14478 && !template_parameter_pack_p (parm))
14479 return 1;
14480
14481 /* If the argument deduction results is a METHOD_TYPE,
14482 then there is a problem.
14483 METHOD_TYPE doesn't map to any real C++ type the result of
14484 the deduction can not be of that type. */
14485 if (TREE_CODE (arg) == METHOD_TYPE)
14486 return 1;
14487
14488 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14489 return 0;
14490
14491 case TEMPLATE_PARM_INDEX:
14492 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14493 if (tparm == error_mark_node)
14494 return 1;
14495
14496 if (TEMPLATE_PARM_LEVEL (parm)
14497 != template_decl_level (tparm))
14498 /* The PARM is not one we're trying to unify. Just check
14499 to see if it matches ARG. */
14500 return !(TREE_CODE (arg) == TREE_CODE (parm)
14501 && cp_tree_equal (parm, arg));
14502
14503 idx = TEMPLATE_PARM_IDX (parm);
14504 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14505
14506 if (targ)
14507 return !cp_tree_equal (targ, arg);
14508
14509 /* [temp.deduct.type] If, in the declaration of a function template
14510 with a non-type template-parameter, the non-type
14511 template-parameter is used in an expression in the function
14512 parameter-list and, if the corresponding template-argument is
14513 deduced, the template-argument type shall match the type of the
14514 template-parameter exactly, except that a template-argument
14515 deduced from an array bound may be of any integral type.
14516 The non-type parameter might use already deduced type parameters. */
14517 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
14518 if (!TREE_TYPE (arg))
14519 /* Template-parameter dependent expression. Just accept it for now.
14520 It will later be processed in convert_template_argument. */
14521 ;
14522 else if (same_type_p (TREE_TYPE (arg), tparm))
14523 /* OK */;
14524 else if ((strict & UNIFY_ALLOW_INTEGER)
14525 && (TREE_CODE (tparm) == INTEGER_TYPE
14526 || TREE_CODE (tparm) == BOOLEAN_TYPE))
14527 /* Convert the ARG to the type of PARM; the deduced non-type
14528 template argument must exactly match the types of the
14529 corresponding parameter. */
14530 arg = fold (build_nop (tparm, arg));
14531 else if (uses_template_parms (tparm))
14532 /* We haven't deduced the type of this parameter yet. Try again
14533 later. */
14534 return 0;
14535 else
14536 return 1;
14537
14538 /* If ARG is a parameter pack or an expansion, we cannot unify
14539 against it unless PARM is also a parameter pack. */
14540 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14541 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
14542 return 1;
14543
14544 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14545 return 0;
14546
14547 case PTRMEM_CST:
14548 {
14549 /* A pointer-to-member constant can be unified only with
14550 another constant. */
14551 if (TREE_CODE (arg) != PTRMEM_CST)
14552 return 1;
14553
14554 /* Just unify the class member. It would be useless (and possibly
14555 wrong, depending on the strict flags) to unify also
14556 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14557 arg refer to the same variable, even if through different
14558 classes. For instance:
14559
14560 struct A { int x; };
14561 struct B : A { };
14562
14563 Unification of &A::x and &B::x must succeed. */
14564 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
14565 PTRMEM_CST_MEMBER (arg), strict);
14566 }
14567
14568 case POINTER_TYPE:
14569 {
14570 if (TREE_CODE (arg) != POINTER_TYPE)
14571 return 1;
14572
14573 /* [temp.deduct.call]
14574
14575 A can be another pointer or pointer to member type that can
14576 be converted to the deduced A via a qualification
14577 conversion (_conv.qual_).
14578
14579 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14580 This will allow for additional cv-qualification of the
14581 pointed-to types if appropriate. */
14582
14583 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
14584 /* The derived-to-base conversion only persists through one
14585 level of pointers. */
14586 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
14587
14588 return unify (tparms, targs, TREE_TYPE (parm),
14589 TREE_TYPE (arg), strict);
14590 }
14591
14592 case REFERENCE_TYPE:
14593 if (TREE_CODE (arg) != REFERENCE_TYPE)
14594 return 1;
14595 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14596 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14597
14598 case ARRAY_TYPE:
14599 if (TREE_CODE (arg) != ARRAY_TYPE)
14600 return 1;
14601 if ((TYPE_DOMAIN (parm) == NULL_TREE)
14602 != (TYPE_DOMAIN (arg) == NULL_TREE))
14603 return 1;
14604 if (TYPE_DOMAIN (parm) != NULL_TREE)
14605 {
14606 tree parm_max;
14607 tree arg_max;
14608 bool parm_cst;
14609 bool arg_cst;
14610
14611 /* Our representation of array types uses "N - 1" as the
14612 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14613 not an integer constant. We cannot unify arbitrarily
14614 complex expressions, so we eliminate the MINUS_EXPRs
14615 here. */
14616 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
14617 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
14618 if (!parm_cst)
14619 {
14620 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
14621 parm_max = TREE_OPERAND (parm_max, 0);
14622 }
14623 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
14624 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
14625 if (!arg_cst)
14626 {
14627 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
14628 trying to unify the type of a variable with the type
14629 of a template parameter. For example:
14630
14631 template <unsigned int N>
14632 void f (char (&) [N]);
14633 int g();
14634 void h(int i) {
14635 char a[g(i)];
14636 f(a);
14637 }
14638
14639 Here, the type of the ARG will be "int [g(i)]", and
14640 may be a SAVE_EXPR, etc. */
14641 if (TREE_CODE (arg_max) != MINUS_EXPR)
14642 return 1;
14643 arg_max = TREE_OPERAND (arg_max, 0);
14644 }
14645
14646 /* If only one of the bounds used a MINUS_EXPR, compensate
14647 by adding one to the other bound. */
14648 if (parm_cst && !arg_cst)
14649 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
14650 integer_type_node,
14651 parm_max,
14652 integer_one_node);
14653 else if (arg_cst && !parm_cst)
14654 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
14655 integer_type_node,
14656 arg_max,
14657 integer_one_node);
14658
14659 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
14660 return 1;
14661 }
14662 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14663 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14664
14665 case REAL_TYPE:
14666 case COMPLEX_TYPE:
14667 case VECTOR_TYPE:
14668 case INTEGER_TYPE:
14669 case BOOLEAN_TYPE:
14670 case ENUMERAL_TYPE:
14671 case VOID_TYPE:
14672 if (TREE_CODE (arg) != TREE_CODE (parm))
14673 return 1;
14674
14675 /* We have already checked cv-qualification at the top of the
14676 function. */
14677 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
14678 return 1;
14679
14680 /* As far as unification is concerned, this wins. Later checks
14681 will invalidate it if necessary. */
14682 return 0;
14683
14684 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
14685 /* Type INTEGER_CST can come from ordinary constant template args. */
14686 case INTEGER_CST:
14687 while (TREE_CODE (arg) == NOP_EXPR)
14688 arg = TREE_OPERAND (arg, 0);
14689
14690 if (TREE_CODE (arg) != INTEGER_CST)
14691 return 1;
14692 return !tree_int_cst_equal (parm, arg);
14693
14694 case TREE_VEC:
14695 {
14696 int i;
14697 if (TREE_CODE (arg) != TREE_VEC)
14698 return 1;
14699 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
14700 return 1;
14701 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
14702 if (unify (tparms, targs,
14703 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
14704 UNIFY_ALLOW_NONE))
14705 return 1;
14706 return 0;
14707 }
14708
14709 case RECORD_TYPE:
14710 case UNION_TYPE:
14711 if (TREE_CODE (arg) != TREE_CODE (parm))
14712 return 1;
14713
14714 if (TYPE_PTRMEMFUNC_P (parm))
14715 {
14716 if (!TYPE_PTRMEMFUNC_P (arg))
14717 return 1;
14718
14719 return unify (tparms, targs,
14720 TYPE_PTRMEMFUNC_FN_TYPE (parm),
14721 TYPE_PTRMEMFUNC_FN_TYPE (arg),
14722 strict);
14723 }
14724
14725 if (CLASSTYPE_TEMPLATE_INFO (parm))
14726 {
14727 tree t = NULL_TREE;
14728
14729 if (strict_in & UNIFY_ALLOW_DERIVED)
14730 {
14731 /* First, we try to unify the PARM and ARG directly. */
14732 t = try_class_unification (tparms, targs,
14733 parm, arg);
14734
14735 if (!t)
14736 {
14737 /* Fallback to the special case allowed in
14738 [temp.deduct.call]:
14739
14740 If P is a class, and P has the form
14741 template-id, then A can be a derived class of
14742 the deduced A. Likewise, if P is a pointer to
14743 a class of the form template-id, A can be a
14744 pointer to a derived class pointed to by the
14745 deduced A. */
14746 t = get_template_base (tparms, targs, parm, arg);
14747
14748 if (!t)
14749 return 1;
14750 }
14751 }
14752 else if (CLASSTYPE_TEMPLATE_INFO (arg)
14753 && (CLASSTYPE_TI_TEMPLATE (parm)
14754 == CLASSTYPE_TI_TEMPLATE (arg)))
14755 /* Perhaps PARM is something like S<U> and ARG is S<int>.
14756 Then, we should unify `int' and `U'. */
14757 t = arg;
14758 else
14759 /* There's no chance of unification succeeding. */
14760 return 1;
14761
14762 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
14763 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
14764 }
14765 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
14766 return 1;
14767 return 0;
14768
14769 case METHOD_TYPE:
14770 case FUNCTION_TYPE:
14771 {
14772 unsigned int nargs;
14773 tree *args;
14774 tree a;
14775 unsigned int i;
14776
14777 if (TREE_CODE (arg) != TREE_CODE (parm))
14778 return 1;
14779
14780 /* CV qualifications for methods can never be deduced, they must
14781 match exactly. We need to check them explicitly here,
14782 because type_unification_real treats them as any other
14783 cv-qualified parameter. */
14784 if (TREE_CODE (parm) == METHOD_TYPE
14785 && (!check_cv_quals_for_unify
14786 (UNIFY_ALLOW_NONE,
14787 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
14788 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
14789 return 1;
14790
14791 if (unify (tparms, targs, TREE_TYPE (parm),
14792 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
14793 return 1;
14794
14795 nargs = list_length (TYPE_ARG_TYPES (arg));
14796 args = XALLOCAVEC (tree, nargs);
14797 for (a = TYPE_ARG_TYPES (arg), i = 0;
14798 a != NULL_TREE && a != void_list_node;
14799 a = TREE_CHAIN (a), ++i)
14800 args[i] = TREE_VALUE (a);
14801 nargs = i;
14802
14803 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
14804 args, nargs, 1, DEDUCE_EXACT,
14805 LOOKUP_NORMAL);
14806 }
14807
14808 case OFFSET_TYPE:
14809 /* Unify a pointer to member with a pointer to member function, which
14810 deduces the type of the member as a function type. */
14811 if (TYPE_PTRMEMFUNC_P (arg))
14812 {
14813 tree method_type;
14814 tree fntype;
14815 cp_cv_quals cv_quals;
14816
14817 /* Check top-level cv qualifiers */
14818 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
14819 return 1;
14820
14821 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14822 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
14823 return 1;
14824
14825 /* Determine the type of the function we are unifying against. */
14826 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
14827 fntype =
14828 build_function_type (TREE_TYPE (method_type),
14829 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
14830
14831 /* Extract the cv-qualifiers of the member function from the
14832 implicit object parameter and place them on the function
14833 type to be restored later. */
14834 cv_quals =
14835 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
14836 fntype = build_qualified_type (fntype, cv_quals);
14837 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
14838 }
14839
14840 if (TREE_CODE (arg) != OFFSET_TYPE)
14841 return 1;
14842 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14843 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
14844 return 1;
14845 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14846 strict);
14847
14848 case CONST_DECL:
14849 if (DECL_TEMPLATE_PARM_P (parm))
14850 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
14851 if (arg != integral_constant_value (parm))
14852 return 1;
14853 return 0;
14854
14855 case FIELD_DECL:
14856 case TEMPLATE_DECL:
14857 /* Matched cases are handled by the ARG == PARM test above. */
14858 return 1;
14859
14860 case TYPE_ARGUMENT_PACK:
14861 case NONTYPE_ARGUMENT_PACK:
14862 {
14863 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
14864 tree packed_args = ARGUMENT_PACK_ARGS (arg);
14865 int i, len = TREE_VEC_LENGTH (packed_parms);
14866 int argslen = TREE_VEC_LENGTH (packed_args);
14867 int parm_variadic_p = 0;
14868
14869 for (i = 0; i < len; ++i)
14870 {
14871 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
14872 {
14873 if (i == len - 1)
14874 /* We can unify against something with a trailing
14875 parameter pack. */
14876 parm_variadic_p = 1;
14877 else
14878 /* Since there is something following the pack
14879 expansion, we cannot unify this template argument
14880 list. */
14881 return 0;
14882 }
14883 }
14884
14885
14886 /* If we don't have enough arguments to satisfy the parameters
14887 (not counting the pack expression at the end), or we have
14888 too many arguments for a parameter list that doesn't end in
14889 a pack expression, we can't unify. */
14890 if (argslen < (len - parm_variadic_p)
14891 || (argslen > len && !parm_variadic_p))
14892 return 1;
14893
14894 /* Unify all of the parameters that precede the (optional)
14895 pack expression. */
14896 for (i = 0; i < len - parm_variadic_p; ++i)
14897 {
14898 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
14899 TREE_VEC_ELT (packed_args, i), strict))
14900 return 1;
14901 }
14902
14903 if (parm_variadic_p)
14904 return unify_pack_expansion (tparms, targs,
14905 packed_parms, packed_args,
14906 strict, /*call_args_p=*/false,
14907 /*subr=*/false);
14908 return 0;
14909 }
14910
14911 break;
14912
14913 case TYPEOF_TYPE:
14914 case DECLTYPE_TYPE:
14915 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
14916 nodes. */
14917 return 0;
14918
14919 case ERROR_MARK:
14920 /* Unification fails if we hit an error node. */
14921 return 1;
14922
14923 default:
14924 gcc_assert (EXPR_P (parm));
14925
14926 /* We must be looking at an expression. This can happen with
14927 something like:
14928
14929 template <int I>
14930 void foo(S<I>, S<I + 2>);
14931
14932 This is a "nondeduced context":
14933
14934 [deduct.type]
14935
14936 The nondeduced contexts are:
14937
14938 --A type that is a template-id in which one or more of
14939 the template-arguments is an expression that references
14940 a template-parameter.
14941
14942 In these cases, we assume deduction succeeded, but don't
14943 actually infer any unifications. */
14944
14945 if (!uses_template_parms (parm)
14946 && !template_args_equal (parm, arg))
14947 return 1;
14948 else
14949 return 0;
14950 }
14951 }
14952 \f
14953 /* Note that DECL can be defined in this translation unit, if
14954 required. */
14955
14956 static void
14957 mark_definable (tree decl)
14958 {
14959 tree clone;
14960 DECL_NOT_REALLY_EXTERN (decl) = 1;
14961 FOR_EACH_CLONE (clone, decl)
14962 DECL_NOT_REALLY_EXTERN (clone) = 1;
14963 }
14964
14965 /* Called if RESULT is explicitly instantiated, or is a member of an
14966 explicitly instantiated class. */
14967
14968 void
14969 mark_decl_instantiated (tree result, int extern_p)
14970 {
14971 SET_DECL_EXPLICIT_INSTANTIATION (result);
14972
14973 /* If this entity has already been written out, it's too late to
14974 make any modifications. */
14975 if (TREE_ASM_WRITTEN (result))
14976 return;
14977
14978 if (TREE_CODE (result) != FUNCTION_DECL)
14979 /* The TREE_PUBLIC flag for function declarations will have been
14980 set correctly by tsubst. */
14981 TREE_PUBLIC (result) = 1;
14982
14983 /* This might have been set by an earlier implicit instantiation. */
14984 DECL_COMDAT (result) = 0;
14985
14986 if (extern_p)
14987 DECL_NOT_REALLY_EXTERN (result) = 0;
14988 else
14989 {
14990 mark_definable (result);
14991 /* Always make artificials weak. */
14992 if (DECL_ARTIFICIAL (result) && flag_weak)
14993 comdat_linkage (result);
14994 /* For WIN32 we also want to put explicit instantiations in
14995 linkonce sections. */
14996 else if (TREE_PUBLIC (result))
14997 maybe_make_one_only (result);
14998 }
14999
15000 /* If EXTERN_P, then this function will not be emitted -- unless
15001 followed by an explicit instantiation, at which point its linkage
15002 will be adjusted. If !EXTERN_P, then this function will be
15003 emitted here. In neither circumstance do we want
15004 import_export_decl to adjust the linkage. */
15005 DECL_INTERFACE_KNOWN (result) = 1;
15006 }
15007
15008 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15009 important template arguments. If any are missing, we check whether
15010 they're important by using error_mark_node for substituting into any
15011 args that were used for partial ordering (the ones between ARGS and END)
15012 and seeing if it bubbles up. */
15013
15014 static bool
15015 check_undeduced_parms (tree targs, tree args, tree end)
15016 {
15017 bool found = false;
15018 int i;
15019 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
15020 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
15021 {
15022 found = true;
15023 TREE_VEC_ELT (targs, i) = error_mark_node;
15024 }
15025 if (found)
15026 {
15027 for (; args != end; args = TREE_CHAIN (args))
15028 {
15029 tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
15030 if (substed == error_mark_node)
15031 return true;
15032 }
15033 }
15034 return false;
15035 }
15036
15037 /* Given two function templates PAT1 and PAT2, return:
15038
15039 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15040 -1 if PAT2 is more specialized than PAT1.
15041 0 if neither is more specialized.
15042
15043 LEN indicates the number of parameters we should consider
15044 (defaulted parameters should not be considered).
15045
15046 The 1998 std underspecified function template partial ordering, and
15047 DR214 addresses the issue. We take pairs of arguments, one from
15048 each of the templates, and deduce them against each other. One of
15049 the templates will be more specialized if all the *other*
15050 template's arguments deduce against its arguments and at least one
15051 of its arguments *does* *not* deduce against the other template's
15052 corresponding argument. Deduction is done as for class templates.
15053 The arguments used in deduction have reference and top level cv
15054 qualifiers removed. Iff both arguments were originally reference
15055 types *and* deduction succeeds in both directions, the template
15056 with the more cv-qualified argument wins for that pairing (if
15057 neither is more cv-qualified, they both are equal). Unlike regular
15058 deduction, after all the arguments have been deduced in this way,
15059 we do *not* verify the deduced template argument values can be
15060 substituted into non-deduced contexts.
15061
15062 The logic can be a bit confusing here, because we look at deduce1 and
15063 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15064 can find template arguments for pat1 to make arg1 look like arg2, that
15065 means that arg2 is at least as specialized as arg1. */
15066
15067 int
15068 more_specialized_fn (tree pat1, tree pat2, int len)
15069 {
15070 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
15071 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
15072 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
15073 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
15074 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
15075 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
15076 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
15077 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
15078 tree origs1, origs2;
15079 bool lose1 = false;
15080 bool lose2 = false;
15081
15082 /* Remove the this parameter from non-static member functions. If
15083 one is a non-static member function and the other is not a static
15084 member function, remove the first parameter from that function
15085 also. This situation occurs for operator functions where we
15086 locate both a member function (with this pointer) and non-member
15087 operator (with explicit first operand). */
15088 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
15089 {
15090 len--; /* LEN is the number of significant arguments for DECL1 */
15091 args1 = TREE_CHAIN (args1);
15092 if (!DECL_STATIC_FUNCTION_P (decl2))
15093 args2 = TREE_CHAIN (args2);
15094 }
15095 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
15096 {
15097 args2 = TREE_CHAIN (args2);
15098 if (!DECL_STATIC_FUNCTION_P (decl1))
15099 {
15100 len--;
15101 args1 = TREE_CHAIN (args1);
15102 }
15103 }
15104
15105 /* If only one is a conversion operator, they are unordered. */
15106 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
15107 return 0;
15108
15109 /* Consider the return type for a conversion function */
15110 if (DECL_CONV_FN_P (decl1))
15111 {
15112 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
15113 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
15114 len++;
15115 }
15116
15117 processing_template_decl++;
15118
15119 origs1 = args1;
15120 origs2 = args2;
15121
15122 while (len--
15123 /* Stop when an ellipsis is seen. */
15124 && args1 != NULL_TREE && args2 != NULL_TREE)
15125 {
15126 tree arg1 = TREE_VALUE (args1);
15127 tree arg2 = TREE_VALUE (args2);
15128 int deduce1, deduce2;
15129 int quals1 = -1;
15130 int quals2 = -1;
15131
15132 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15133 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15134 {
15135 /* When both arguments are pack expansions, we need only
15136 unify the patterns themselves. */
15137 arg1 = PACK_EXPANSION_PATTERN (arg1);
15138 arg2 = PACK_EXPANSION_PATTERN (arg2);
15139
15140 /* This is the last comparison we need to do. */
15141 len = 0;
15142 }
15143
15144 if (TREE_CODE (arg1) == REFERENCE_TYPE)
15145 {
15146 arg1 = TREE_TYPE (arg1);
15147 quals1 = cp_type_quals (arg1);
15148 }
15149
15150 if (TREE_CODE (arg2) == REFERENCE_TYPE)
15151 {
15152 arg2 = TREE_TYPE (arg2);
15153 quals2 = cp_type_quals (arg2);
15154 }
15155
15156 if ((quals1 < 0) != (quals2 < 0))
15157 {
15158 /* Only of the args is a reference, see if we should apply
15159 array/function pointer decay to it. This is not part of
15160 DR214, but is, IMHO, consistent with the deduction rules
15161 for the function call itself, and with our earlier
15162 implementation of the underspecified partial ordering
15163 rules. (nathan). */
15164 if (quals1 >= 0)
15165 {
15166 switch (TREE_CODE (arg1))
15167 {
15168 case ARRAY_TYPE:
15169 arg1 = TREE_TYPE (arg1);
15170 /* FALLTHROUGH. */
15171 case FUNCTION_TYPE:
15172 arg1 = build_pointer_type (arg1);
15173 break;
15174
15175 default:
15176 break;
15177 }
15178 }
15179 else
15180 {
15181 switch (TREE_CODE (arg2))
15182 {
15183 case ARRAY_TYPE:
15184 arg2 = TREE_TYPE (arg2);
15185 /* FALLTHROUGH. */
15186 case FUNCTION_TYPE:
15187 arg2 = build_pointer_type (arg2);
15188 break;
15189
15190 default:
15191 break;
15192 }
15193 }
15194 }
15195
15196 arg1 = TYPE_MAIN_VARIANT (arg1);
15197 arg2 = TYPE_MAIN_VARIANT (arg2);
15198
15199 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
15200 {
15201 int i, len2 = list_length (args2);
15202 tree parmvec = make_tree_vec (1);
15203 tree argvec = make_tree_vec (len2);
15204 tree ta = args2;
15205
15206 /* Setup the parameter vector, which contains only ARG1. */
15207 TREE_VEC_ELT (parmvec, 0) = arg1;
15208
15209 /* Setup the argument vector, which contains the remaining
15210 arguments. */
15211 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
15212 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15213
15214 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
15215 argvec, UNIFY_ALLOW_NONE,
15216 /*call_args_p=*/false,
15217 /*subr=*/0);
15218
15219 /* We cannot deduce in the other direction, because ARG1 is
15220 a pack expansion but ARG2 is not. */
15221 deduce2 = 0;
15222 }
15223 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15224 {
15225 int i, len1 = list_length (args1);
15226 tree parmvec = make_tree_vec (1);
15227 tree argvec = make_tree_vec (len1);
15228 tree ta = args1;
15229
15230 /* Setup the parameter vector, which contains only ARG1. */
15231 TREE_VEC_ELT (parmvec, 0) = arg2;
15232
15233 /* Setup the argument vector, which contains the remaining
15234 arguments. */
15235 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
15236 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15237
15238 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
15239 argvec, UNIFY_ALLOW_NONE,
15240 /*call_args_p=*/false,
15241 /*subr=*/0);
15242
15243 /* We cannot deduce in the other direction, because ARG2 is
15244 a pack expansion but ARG1 is not.*/
15245 deduce1 = 0;
15246 }
15247
15248 else
15249 {
15250 /* The normal case, where neither argument is a pack
15251 expansion. */
15252 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
15253 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
15254 }
15255
15256 /* If we couldn't deduce arguments for tparms1 to make arg1 match
15257 arg2, then arg2 is not as specialized as arg1. */
15258 if (!deduce1)
15259 lose2 = true;
15260 if (!deduce2)
15261 lose1 = true;
15262
15263 /* "If, for a given type, deduction succeeds in both directions
15264 (i.e., the types are identical after the transformations above)
15265 and if the type from the argument template is more cv-qualified
15266 than the type from the parameter template (as described above)
15267 that type is considered to be more specialized than the other. If
15268 neither type is more cv-qualified than the other then neither type
15269 is more specialized than the other."
15270
15271 We check same_type_p explicitly because deduction can also succeed
15272 in both directions when there is a nondeduced context. */
15273 if (deduce1 && deduce2
15274 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0
15275 && same_type_p (arg1, arg2))
15276 {
15277 if ((quals1 & quals2) == quals2)
15278 lose2 = true;
15279 if ((quals1 & quals2) == quals1)
15280 lose1 = true;
15281 }
15282
15283 if (lose1 && lose2)
15284 /* We've failed to deduce something in either direction.
15285 These must be unordered. */
15286 break;
15287
15288 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15289 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15290 /* We have already processed all of the arguments in our
15291 handing of the pack expansion type. */
15292 len = 0;
15293
15294 args1 = TREE_CHAIN (args1);
15295 args2 = TREE_CHAIN (args2);
15296 }
15297
15298 /* "In most cases, all template parameters must have values in order for
15299 deduction to succeed, but for partial ordering purposes a template
15300 parameter may remain without a value provided it is not used in the
15301 types being used for partial ordering."
15302
15303 Thus, if we are missing any of the targs1 we need to substitute into
15304 origs1, then pat2 is not as specialized as pat1. This can happen when
15305 there is a nondeduced context. */
15306 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
15307 lose2 = true;
15308 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
15309 lose1 = true;
15310
15311 processing_template_decl--;
15312
15313 /* All things being equal, if the next argument is a pack expansion
15314 for one function but not for the other, prefer the
15315 non-variadic function. FIXME this is bogus; see c++/41958. */
15316 if (lose1 == lose2
15317 && args1 && TREE_VALUE (args1)
15318 && args2 && TREE_VALUE (args2))
15319 {
15320 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
15321 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
15322 }
15323
15324 if (lose1 == lose2)
15325 return 0;
15326 else if (!lose1)
15327 return 1;
15328 else
15329 return -1;
15330 }
15331
15332 /* Determine which of two partial specializations is more specialized.
15333
15334 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
15335 to the first partial specialization. The TREE_VALUE is the
15336 innermost set of template parameters for the partial
15337 specialization. PAT2 is similar, but for the second template.
15338
15339 Return 1 if the first partial specialization is more specialized;
15340 -1 if the second is more specialized; 0 if neither is more
15341 specialized.
15342
15343 See [temp.class.order] for information about determining which of
15344 two templates is more specialized. */
15345
15346 static int
15347 more_specialized_class (tree pat1, tree pat2)
15348 {
15349 tree targs;
15350 tree tmpl1, tmpl2;
15351 int winner = 0;
15352 bool any_deductions = false;
15353
15354 tmpl1 = TREE_TYPE (pat1);
15355 tmpl2 = TREE_TYPE (pat2);
15356
15357 /* Just like what happens for functions, if we are ordering between
15358 different class template specializations, we may encounter dependent
15359 types in the arguments, and we need our dependency check functions
15360 to behave correctly. */
15361 ++processing_template_decl;
15362 targs = get_class_bindings (TREE_VALUE (pat1),
15363 CLASSTYPE_TI_ARGS (tmpl1),
15364 CLASSTYPE_TI_ARGS (tmpl2));
15365 if (targs)
15366 {
15367 --winner;
15368 any_deductions = true;
15369 }
15370
15371 targs = get_class_bindings (TREE_VALUE (pat2),
15372 CLASSTYPE_TI_ARGS (tmpl2),
15373 CLASSTYPE_TI_ARGS (tmpl1));
15374 if (targs)
15375 {
15376 ++winner;
15377 any_deductions = true;
15378 }
15379 --processing_template_decl;
15380
15381 /* In the case of a tie where at least one of the class templates
15382 has a parameter pack at the end, the template with the most
15383 non-packed parameters wins. */
15384 if (winner == 0
15385 && any_deductions
15386 && (template_args_variadic_p (TREE_PURPOSE (pat1))
15387 || template_args_variadic_p (TREE_PURPOSE (pat2))))
15388 {
15389 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
15390 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
15391 int len1 = TREE_VEC_LENGTH (args1);
15392 int len2 = TREE_VEC_LENGTH (args2);
15393
15394 /* We don't count the pack expansion at the end. */
15395 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
15396 --len1;
15397 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
15398 --len2;
15399
15400 if (len1 > len2)
15401 return 1;
15402 else if (len1 < len2)
15403 return -1;
15404 }
15405
15406 return winner;
15407 }
15408
15409 /* Return the template arguments that will produce the function signature
15410 DECL from the function template FN, with the explicit template
15411 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
15412 also match. Return NULL_TREE if no satisfactory arguments could be
15413 found. */
15414
15415 static tree
15416 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
15417 {
15418 int ntparms = DECL_NTPARMS (fn);
15419 tree targs = make_tree_vec (ntparms);
15420 tree decl_type;
15421 tree decl_arg_types;
15422 tree *args;
15423 unsigned int nargs, ix;
15424 tree arg;
15425
15426 /* Substitute the explicit template arguments into the type of DECL.
15427 The call to fn_type_unification will handle substitution into the
15428 FN. */
15429 decl_type = TREE_TYPE (decl);
15430 if (explicit_args && uses_template_parms (decl_type))
15431 {
15432 tree tmpl;
15433 tree converted_args;
15434
15435 if (DECL_TEMPLATE_INFO (decl))
15436 tmpl = DECL_TI_TEMPLATE (decl);
15437 else
15438 /* We can get here for some invalid specializations. */
15439 return NULL_TREE;
15440
15441 converted_args
15442 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
15443 explicit_args, NULL_TREE,
15444 tf_none,
15445 /*require_all_args=*/false,
15446 /*use_default_args=*/false);
15447 if (converted_args == error_mark_node)
15448 return NULL_TREE;
15449
15450 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
15451 if (decl_type == error_mark_node)
15452 return NULL_TREE;
15453 }
15454
15455 /* Never do unification on the 'this' parameter. */
15456 decl_arg_types = skip_artificial_parms_for (decl,
15457 TYPE_ARG_TYPES (decl_type));
15458
15459 nargs = list_length (decl_arg_types);
15460 args = XALLOCAVEC (tree, nargs);
15461 for (arg = decl_arg_types, ix = 0;
15462 arg != NULL_TREE && arg != void_list_node;
15463 arg = TREE_CHAIN (arg), ++ix)
15464 args[ix] = TREE_VALUE (arg);
15465
15466 if (fn_type_unification (fn, explicit_args, targs,
15467 args, ix,
15468 (check_rettype || DECL_CONV_FN_P (fn)
15469 ? TREE_TYPE (decl_type) : NULL_TREE),
15470 DEDUCE_EXACT, LOOKUP_NORMAL))
15471 return NULL_TREE;
15472
15473 return targs;
15474 }
15475
15476 /* Return the innermost template arguments that, when applied to a
15477 template specialization whose innermost template parameters are
15478 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15479 ARGS.
15480
15481 For example, suppose we have:
15482
15483 template <class T, class U> struct S {};
15484 template <class T> struct S<T*, int> {};
15485
15486 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15487 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15488 int}. The resulting vector will be {double}, indicating that `T'
15489 is bound to `double'. */
15490
15491 static tree
15492 get_class_bindings (tree tparms, tree spec_args, tree args)
15493 {
15494 int i, ntparms = TREE_VEC_LENGTH (tparms);
15495 tree deduced_args;
15496 tree innermost_deduced_args;
15497
15498 innermost_deduced_args = make_tree_vec (ntparms);
15499 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15500 {
15501 deduced_args = copy_node (args);
15502 SET_TMPL_ARGS_LEVEL (deduced_args,
15503 TMPL_ARGS_DEPTH (deduced_args),
15504 innermost_deduced_args);
15505 }
15506 else
15507 deduced_args = innermost_deduced_args;
15508
15509 if (unify (tparms, deduced_args,
15510 INNERMOST_TEMPLATE_ARGS (spec_args),
15511 INNERMOST_TEMPLATE_ARGS (args),
15512 UNIFY_ALLOW_NONE))
15513 return NULL_TREE;
15514
15515 for (i = 0; i < ntparms; ++i)
15516 if (! TREE_VEC_ELT (innermost_deduced_args, i))
15517 return NULL_TREE;
15518
15519 /* Verify that nondeduced template arguments agree with the type
15520 obtained from argument deduction.
15521
15522 For example:
15523
15524 struct A { typedef int X; };
15525 template <class T, class U> struct C {};
15526 template <class T> struct C<T, typename T::X> {};
15527
15528 Then with the instantiation `C<A, int>', we can deduce that
15529 `T' is `A' but unify () does not check whether `typename T::X'
15530 is `int'. */
15531 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
15532 if (spec_args == error_mark_node
15533 /* We only need to check the innermost arguments; the other
15534 arguments will always agree. */
15535 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
15536 INNERMOST_TEMPLATE_ARGS (args)))
15537 return NULL_TREE;
15538
15539 /* Now that we have bindings for all of the template arguments,
15540 ensure that the arguments deduced for the template template
15541 parameters have compatible template parameter lists. See the use
15542 of template_template_parm_bindings_ok_p in fn_type_unification
15543 for more information. */
15544 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
15545 return NULL_TREE;
15546
15547 return deduced_args;
15548 }
15549
15550 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15551 Return the TREE_LIST node with the most specialized template, if
15552 any. If there is no most specialized template, the error_mark_node
15553 is returned.
15554
15555 Note that this function does not look at, or modify, the
15556 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15557 returned is one of the elements of INSTANTIATIONS, callers may
15558 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15559 and retrieve it from the value returned. */
15560
15561 tree
15562 most_specialized_instantiation (tree templates)
15563 {
15564 tree fn, champ;
15565
15566 ++processing_template_decl;
15567
15568 champ = templates;
15569 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
15570 {
15571 int fate = 0;
15572
15573 if (get_bindings (TREE_VALUE (champ),
15574 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15575 NULL_TREE, /*check_ret=*/false))
15576 fate--;
15577
15578 if (get_bindings (TREE_VALUE (fn),
15579 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15580 NULL_TREE, /*check_ret=*/false))
15581 fate++;
15582
15583 if (fate == -1)
15584 champ = fn;
15585 else if (!fate)
15586 {
15587 /* Equally specialized, move to next function. If there
15588 is no next function, nothing's most specialized. */
15589 fn = TREE_CHAIN (fn);
15590 champ = fn;
15591 if (!fn)
15592 break;
15593 }
15594 }
15595
15596 if (champ)
15597 /* Now verify that champ is better than everything earlier in the
15598 instantiation list. */
15599 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
15600 if (get_bindings (TREE_VALUE (champ),
15601 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15602 NULL_TREE, /*check_ret=*/false)
15603 || !get_bindings (TREE_VALUE (fn),
15604 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15605 NULL_TREE, /*check_ret=*/false))
15606 {
15607 champ = NULL_TREE;
15608 break;
15609 }
15610
15611 processing_template_decl--;
15612
15613 if (!champ)
15614 return error_mark_node;
15615
15616 return champ;
15617 }
15618
15619 /* If DECL is a specialization of some template, return the most
15620 general such template. Otherwise, returns NULL_TREE.
15621
15622 For example, given:
15623
15624 template <class T> struct S { template <class U> void f(U); };
15625
15626 if TMPL is `template <class U> void S<int>::f(U)' this will return
15627 the full template. This function will not trace past partial
15628 specializations, however. For example, given in addition:
15629
15630 template <class T> struct S<T*> { template <class U> void f(U); };
15631
15632 if TMPL is `template <class U> void S<int*>::f(U)' this will return
15633 `template <class T> template <class U> S<T*>::f(U)'. */
15634
15635 tree
15636 most_general_template (tree decl)
15637 {
15638 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
15639 an immediate specialization. */
15640 if (TREE_CODE (decl) == FUNCTION_DECL)
15641 {
15642 if (DECL_TEMPLATE_INFO (decl)) {
15643 decl = DECL_TI_TEMPLATE (decl);
15644
15645 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
15646 template friend. */
15647 if (TREE_CODE (decl) != TEMPLATE_DECL)
15648 return NULL_TREE;
15649 } else
15650 return NULL_TREE;
15651 }
15652
15653 /* Look for more and more general templates. */
15654 while (DECL_TEMPLATE_INFO (decl))
15655 {
15656 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
15657 (See cp-tree.h for details.) */
15658 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
15659 break;
15660
15661 if (CLASS_TYPE_P (TREE_TYPE (decl))
15662 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
15663 break;
15664
15665 /* Stop if we run into an explicitly specialized class template. */
15666 if (!DECL_NAMESPACE_SCOPE_P (decl)
15667 && DECL_CONTEXT (decl)
15668 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
15669 break;
15670
15671 decl = DECL_TI_TEMPLATE (decl);
15672 }
15673
15674 return decl;
15675 }
15676
15677 /* Return the most specialized of the class template partial
15678 specializations of TMPL which can produce TYPE, a specialization of
15679 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
15680 a _TYPE node corresponding to the partial specialization, while the
15681 TREE_PURPOSE is the set of template arguments that must be
15682 substituted into the TREE_TYPE in order to generate TYPE.
15683
15684 If the choice of partial specialization is ambiguous, a diagnostic
15685 is issued, and the error_mark_node is returned. If there are no
15686 partial specializations of TMPL matching TYPE, then NULL_TREE is
15687 returned. */
15688
15689 static tree
15690 most_specialized_class (tree type, tree tmpl)
15691 {
15692 tree list = NULL_TREE;
15693 tree t;
15694 tree champ;
15695 int fate;
15696 bool ambiguous_p;
15697 tree args;
15698 tree outer_args = NULL_TREE;
15699
15700 tmpl = most_general_template (tmpl);
15701 args = CLASSTYPE_TI_ARGS (type);
15702
15703 /* For determining which partial specialization to use, only the
15704 innermost args are interesting. */
15705 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15706 {
15707 outer_args = strip_innermost_template_args (args, 1);
15708 args = INNERMOST_TEMPLATE_ARGS (args);
15709 }
15710
15711 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
15712 {
15713 tree partial_spec_args;
15714 tree spec_args;
15715 tree parms = TREE_VALUE (t);
15716
15717 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
15718 if (outer_args)
15719 {
15720 int i;
15721
15722 ++processing_template_decl;
15723
15724 /* Discard the outer levels of args, and then substitute in the
15725 template args from the enclosing class. */
15726 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
15727 partial_spec_args = tsubst_template_args
15728 (partial_spec_args, outer_args, tf_none, NULL_TREE);
15729
15730 /* PARMS already refers to just the innermost parms, but the
15731 template parms in partial_spec_args had their levels lowered
15732 by tsubst, so we need to do the same for the parm list. We
15733 can't just tsubst the TREE_VEC itself, as tsubst wants to
15734 treat a TREE_VEC as an argument vector. */
15735 parms = copy_node (parms);
15736 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
15737 TREE_VEC_ELT (parms, i) =
15738 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
15739
15740 --processing_template_decl;
15741 }
15742 spec_args = get_class_bindings (parms,
15743 partial_spec_args,
15744 args);
15745 if (spec_args)
15746 {
15747 if (outer_args)
15748 spec_args = add_to_template_args (outer_args, spec_args);
15749 list = tree_cons (spec_args, TREE_VALUE (t), list);
15750 TREE_TYPE (list) = TREE_TYPE (t);
15751 }
15752 }
15753
15754 if (! list)
15755 return NULL_TREE;
15756
15757 ambiguous_p = false;
15758 t = list;
15759 champ = t;
15760 t = TREE_CHAIN (t);
15761 for (; t; t = TREE_CHAIN (t))
15762 {
15763 fate = more_specialized_class (champ, t);
15764 if (fate == 1)
15765 ;
15766 else
15767 {
15768 if (fate == 0)
15769 {
15770 t = TREE_CHAIN (t);
15771 if (! t)
15772 {
15773 ambiguous_p = true;
15774 break;
15775 }
15776 }
15777 champ = t;
15778 }
15779 }
15780
15781 if (!ambiguous_p)
15782 for (t = list; t && t != champ; t = TREE_CHAIN (t))
15783 {
15784 fate = more_specialized_class (champ, t);
15785 if (fate != 1)
15786 {
15787 ambiguous_p = true;
15788 break;
15789 }
15790 }
15791
15792 if (ambiguous_p)
15793 {
15794 const char *str;
15795 char *spaces = NULL;
15796 error ("ambiguous class template instantiation for %q#T", type);
15797 str = TREE_CHAIN (list) ? _("candidates are:") : _("candidate is:");
15798 for (t = list; t; t = TREE_CHAIN (t))
15799 {
15800 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
15801 spaces = spaces ? spaces : get_spaces (str);
15802 }
15803 free (spaces);
15804 return error_mark_node;
15805 }
15806
15807 return champ;
15808 }
15809
15810 /* Explicitly instantiate DECL. */
15811
15812 void
15813 do_decl_instantiation (tree decl, tree storage)
15814 {
15815 tree result = NULL_TREE;
15816 int extern_p = 0;
15817
15818 if (!decl || decl == error_mark_node)
15819 /* An error occurred, for which grokdeclarator has already issued
15820 an appropriate message. */
15821 return;
15822 else if (! DECL_LANG_SPECIFIC (decl))
15823 {
15824 error ("explicit instantiation of non-template %q#D", decl);
15825 return;
15826 }
15827 else if (TREE_CODE (decl) == VAR_DECL)
15828 {
15829 /* There is an asymmetry here in the way VAR_DECLs and
15830 FUNCTION_DECLs are handled by grokdeclarator. In the case of
15831 the latter, the DECL we get back will be marked as a
15832 template instantiation, and the appropriate
15833 DECL_TEMPLATE_INFO will be set up. This does not happen for
15834 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
15835 should handle VAR_DECLs as it currently handles
15836 FUNCTION_DECLs. */
15837 if (!DECL_CLASS_SCOPE_P (decl))
15838 {
15839 error ("%qD is not a static data member of a class template", decl);
15840 return;
15841 }
15842 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
15843 if (!result || TREE_CODE (result) != VAR_DECL)
15844 {
15845 error ("no matching template for %qD found", decl);
15846 return;
15847 }
15848 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
15849 {
15850 error ("type %qT for explicit instantiation %qD does not match "
15851 "declared type %qT", TREE_TYPE (result), decl,
15852 TREE_TYPE (decl));
15853 return;
15854 }
15855 }
15856 else if (TREE_CODE (decl) != FUNCTION_DECL)
15857 {
15858 error ("explicit instantiation of %q#D", decl);
15859 return;
15860 }
15861 else
15862 result = decl;
15863
15864 /* Check for various error cases. Note that if the explicit
15865 instantiation is valid the RESULT will currently be marked as an
15866 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
15867 until we get here. */
15868
15869 if (DECL_TEMPLATE_SPECIALIZATION (result))
15870 {
15871 /* DR 259 [temp.spec].
15872
15873 Both an explicit instantiation and a declaration of an explicit
15874 specialization shall not appear in a program unless the explicit
15875 instantiation follows a declaration of the explicit specialization.
15876
15877 For a given set of template parameters, if an explicit
15878 instantiation of a template appears after a declaration of an
15879 explicit specialization for that template, the explicit
15880 instantiation has no effect. */
15881 return;
15882 }
15883 else if (DECL_EXPLICIT_INSTANTIATION (result))
15884 {
15885 /* [temp.spec]
15886
15887 No program shall explicitly instantiate any template more
15888 than once.
15889
15890 We check DECL_NOT_REALLY_EXTERN so as not to complain when
15891 the first instantiation was `extern' and the second is not,
15892 and EXTERN_P for the opposite case. */
15893 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
15894 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
15895 /* If an "extern" explicit instantiation follows an ordinary
15896 explicit instantiation, the template is instantiated. */
15897 if (extern_p)
15898 return;
15899 }
15900 else if (!DECL_IMPLICIT_INSTANTIATION (result))
15901 {
15902 error ("no matching template for %qD found", result);
15903 return;
15904 }
15905 else if (!DECL_TEMPLATE_INFO (result))
15906 {
15907 permerror (input_location, "explicit instantiation of non-template %q#D", result);
15908 return;
15909 }
15910
15911 if (storage == NULL_TREE)
15912 ;
15913 else if (storage == ridpointers[(int) RID_EXTERN])
15914 {
15915 if (!in_system_header && (cxx_dialect == cxx98))
15916 pedwarn (input_location, OPT_pedantic,
15917 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
15918 "instantiations");
15919 extern_p = 1;
15920 }
15921 else
15922 error ("storage class %qD applied to template instantiation", storage);
15923
15924 check_explicit_instantiation_namespace (result);
15925 mark_decl_instantiated (result, extern_p);
15926 if (! extern_p)
15927 instantiate_decl (result, /*defer_ok=*/1,
15928 /*expl_inst_class_mem_p=*/false);
15929 }
15930
15931 static void
15932 mark_class_instantiated (tree t, int extern_p)
15933 {
15934 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
15935 SET_CLASSTYPE_INTERFACE_KNOWN (t);
15936 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
15937 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
15938 if (! extern_p)
15939 {
15940 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
15941 rest_of_type_compilation (t, 1);
15942 }
15943 }
15944
15945 /* Called from do_type_instantiation through binding_table_foreach to
15946 do recursive instantiation for the type bound in ENTRY. */
15947 static void
15948 bt_instantiate_type_proc (binding_entry entry, void *data)
15949 {
15950 tree storage = *(tree *) data;
15951
15952 if (MAYBE_CLASS_TYPE_P (entry->type)
15953 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
15954 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
15955 }
15956
15957 /* Called from do_type_instantiation to instantiate a member
15958 (a member function or a static member variable) of an
15959 explicitly instantiated class template. */
15960 static void
15961 instantiate_class_member (tree decl, int extern_p)
15962 {
15963 mark_decl_instantiated (decl, extern_p);
15964 if (! extern_p)
15965 instantiate_decl (decl, /*defer_ok=*/1,
15966 /*expl_inst_class_mem_p=*/true);
15967 }
15968
15969 /* Perform an explicit instantiation of template class T. STORAGE, if
15970 non-null, is the RID for extern, inline or static. COMPLAIN is
15971 nonzero if this is called from the parser, zero if called recursively,
15972 since the standard is unclear (as detailed below). */
15973
15974 void
15975 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
15976 {
15977 int extern_p = 0;
15978 int nomem_p = 0;
15979 int static_p = 0;
15980 int previous_instantiation_extern_p = 0;
15981
15982 if (TREE_CODE (t) == TYPE_DECL)
15983 t = TREE_TYPE (t);
15984
15985 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
15986 {
15987 error ("explicit instantiation of non-template type %qT", t);
15988 return;
15989 }
15990
15991 complete_type (t);
15992
15993 if (!COMPLETE_TYPE_P (t))
15994 {
15995 if (complain & tf_error)
15996 error ("explicit instantiation of %q#T before definition of template",
15997 t);
15998 return;
15999 }
16000
16001 if (storage != NULL_TREE)
16002 {
16003 if (!in_system_header)
16004 {
16005 if (storage == ridpointers[(int) RID_EXTERN])
16006 {
16007 if (cxx_dialect == cxx98)
16008 pedwarn (input_location, OPT_pedantic,
16009 "ISO C++ 1998 forbids the use of %<extern%> on "
16010 "explicit instantiations");
16011 }
16012 else
16013 pedwarn (input_location, OPT_pedantic,
16014 "ISO C++ forbids the use of %qE"
16015 " on explicit instantiations", storage);
16016 }
16017
16018 if (storage == ridpointers[(int) RID_INLINE])
16019 nomem_p = 1;
16020 else if (storage == ridpointers[(int) RID_EXTERN])
16021 extern_p = 1;
16022 else if (storage == ridpointers[(int) RID_STATIC])
16023 static_p = 1;
16024 else
16025 {
16026 error ("storage class %qD applied to template instantiation",
16027 storage);
16028 extern_p = 0;
16029 }
16030 }
16031
16032 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
16033 {
16034 /* DR 259 [temp.spec].
16035
16036 Both an explicit instantiation and a declaration of an explicit
16037 specialization shall not appear in a program unless the explicit
16038 instantiation follows a declaration of the explicit specialization.
16039
16040 For a given set of template parameters, if an explicit
16041 instantiation of a template appears after a declaration of an
16042 explicit specialization for that template, the explicit
16043 instantiation has no effect. */
16044 return;
16045 }
16046 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
16047 {
16048 /* [temp.spec]
16049
16050 No program shall explicitly instantiate any template more
16051 than once.
16052
16053 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16054 instantiation was `extern'. If EXTERN_P then the second is.
16055 These cases are OK. */
16056 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
16057
16058 if (!previous_instantiation_extern_p && !extern_p
16059 && (complain & tf_error))
16060 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
16061
16062 /* If we've already instantiated the template, just return now. */
16063 if (!CLASSTYPE_INTERFACE_ONLY (t))
16064 return;
16065 }
16066
16067 check_explicit_instantiation_namespace (TYPE_NAME (t));
16068 mark_class_instantiated (t, extern_p);
16069
16070 if (nomem_p)
16071 return;
16072
16073 {
16074 tree tmp;
16075
16076 /* In contrast to implicit instantiation, where only the
16077 declarations, and not the definitions, of members are
16078 instantiated, we have here:
16079
16080 [temp.explicit]
16081
16082 The explicit instantiation of a class template specialization
16083 implies the instantiation of all of its members not
16084 previously explicitly specialized in the translation unit
16085 containing the explicit instantiation.
16086
16087 Of course, we can't instantiate member template classes, since
16088 we don't have any arguments for them. Note that the standard
16089 is unclear on whether the instantiation of the members are
16090 *explicit* instantiations or not. However, the most natural
16091 interpretation is that it should be an explicit instantiation. */
16092
16093 if (! static_p)
16094 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
16095 if (TREE_CODE (tmp) == FUNCTION_DECL
16096 && DECL_TEMPLATE_INSTANTIATION (tmp))
16097 instantiate_class_member (tmp, extern_p);
16098
16099 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
16100 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
16101 instantiate_class_member (tmp, extern_p);
16102
16103 if (CLASSTYPE_NESTED_UTDS (t))
16104 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
16105 bt_instantiate_type_proc, &storage);
16106 }
16107 }
16108
16109 /* Given a function DECL, which is a specialization of TMPL, modify
16110 DECL to be a re-instantiation of TMPL with the same template
16111 arguments. TMPL should be the template into which tsubst'ing
16112 should occur for DECL, not the most general template.
16113
16114 One reason for doing this is a scenario like this:
16115
16116 template <class T>
16117 void f(const T&, int i);
16118
16119 void g() { f(3, 7); }
16120
16121 template <class T>
16122 void f(const T& t, const int i) { }
16123
16124 Note that when the template is first instantiated, with
16125 instantiate_template, the resulting DECL will have no name for the
16126 first parameter, and the wrong type for the second. So, when we go
16127 to instantiate the DECL, we regenerate it. */
16128
16129 static void
16130 regenerate_decl_from_template (tree decl, tree tmpl)
16131 {
16132 /* The arguments used to instantiate DECL, from the most general
16133 template. */
16134 tree args;
16135 tree code_pattern;
16136
16137 args = DECL_TI_ARGS (decl);
16138 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
16139
16140 /* Make sure that we can see identifiers, and compute access
16141 correctly. */
16142 push_access_scope (decl);
16143
16144 if (TREE_CODE (decl) == FUNCTION_DECL)
16145 {
16146 tree decl_parm;
16147 tree pattern_parm;
16148 tree specs;
16149 int args_depth;
16150 int parms_depth;
16151
16152 args_depth = TMPL_ARGS_DEPTH (args);
16153 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
16154 if (args_depth > parms_depth)
16155 args = get_innermost_template_args (args, parms_depth);
16156
16157 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
16158 args, tf_error, NULL_TREE);
16159 if (specs)
16160 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
16161 specs);
16162
16163 /* Merge parameter declarations. */
16164 decl_parm = skip_artificial_parms_for (decl,
16165 DECL_ARGUMENTS (decl));
16166 pattern_parm
16167 = skip_artificial_parms_for (code_pattern,
16168 DECL_ARGUMENTS (code_pattern));
16169 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
16170 {
16171 tree parm_type;
16172 tree attributes;
16173
16174 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16175 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
16176 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
16177 NULL_TREE);
16178 parm_type = type_decays_to (parm_type);
16179 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16180 TREE_TYPE (decl_parm) = parm_type;
16181 attributes = DECL_ATTRIBUTES (pattern_parm);
16182 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16183 {
16184 DECL_ATTRIBUTES (decl_parm) = attributes;
16185 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16186 }
16187 decl_parm = TREE_CHAIN (decl_parm);
16188 pattern_parm = TREE_CHAIN (pattern_parm);
16189 }
16190 /* Merge any parameters that match with the function parameter
16191 pack. */
16192 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
16193 {
16194 int i, len;
16195 tree expanded_types;
16196 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16197 the parameters in this function parameter pack. */
16198 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
16199 args, tf_error, NULL_TREE);
16200 len = TREE_VEC_LENGTH (expanded_types);
16201 for (i = 0; i < len; i++)
16202 {
16203 tree parm_type;
16204 tree attributes;
16205
16206 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16207 /* Rename the parameter to include the index. */
16208 DECL_NAME (decl_parm) =
16209 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
16210 parm_type = TREE_VEC_ELT (expanded_types, i);
16211 parm_type = type_decays_to (parm_type);
16212 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16213 TREE_TYPE (decl_parm) = parm_type;
16214 attributes = DECL_ATTRIBUTES (pattern_parm);
16215 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16216 {
16217 DECL_ATTRIBUTES (decl_parm) = attributes;
16218 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16219 }
16220 decl_parm = TREE_CHAIN (decl_parm);
16221 }
16222 }
16223 /* Merge additional specifiers from the CODE_PATTERN. */
16224 if (DECL_DECLARED_INLINE_P (code_pattern)
16225 && !DECL_DECLARED_INLINE_P (decl))
16226 DECL_DECLARED_INLINE_P (decl) = 1;
16227 }
16228 else if (TREE_CODE (decl) == VAR_DECL)
16229 DECL_INITIAL (decl) =
16230 tsubst_expr (DECL_INITIAL (code_pattern), args,
16231 tf_error, DECL_TI_TEMPLATE (decl),
16232 /*integral_constant_expression_p=*/false);
16233 else
16234 gcc_unreachable ();
16235
16236 pop_access_scope (decl);
16237 }
16238
16239 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
16240 substituted to get DECL. */
16241
16242 tree
16243 template_for_substitution (tree decl)
16244 {
16245 tree tmpl = DECL_TI_TEMPLATE (decl);
16246
16247 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
16248 for the instantiation. This is not always the most general
16249 template. Consider, for example:
16250
16251 template <class T>
16252 struct S { template <class U> void f();
16253 template <> void f<int>(); };
16254
16255 and an instantiation of S<double>::f<int>. We want TD to be the
16256 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
16257 while (/* An instantiation cannot have a definition, so we need a
16258 more general template. */
16259 DECL_TEMPLATE_INSTANTIATION (tmpl)
16260 /* We must also deal with friend templates. Given:
16261
16262 template <class T> struct S {
16263 template <class U> friend void f() {};
16264 };
16265
16266 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
16267 so far as the language is concerned, but that's still
16268 where we get the pattern for the instantiation from. On
16269 other hand, if the definition comes outside the class, say:
16270
16271 template <class T> struct S {
16272 template <class U> friend void f();
16273 };
16274 template <class U> friend void f() {}
16275
16276 we don't need to look any further. That's what the check for
16277 DECL_INITIAL is for. */
16278 || (TREE_CODE (decl) == FUNCTION_DECL
16279 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
16280 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
16281 {
16282 /* The present template, TD, should not be a definition. If it
16283 were a definition, we should be using it! Note that we
16284 cannot restructure the loop to just keep going until we find
16285 a template with a definition, since that might go too far if
16286 a specialization was declared, but not defined. */
16287 gcc_assert (TREE_CODE (decl) != VAR_DECL
16288 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
16289
16290 /* Fetch the more general template. */
16291 tmpl = DECL_TI_TEMPLATE (tmpl);
16292 }
16293
16294 return tmpl;
16295 }
16296
16297 /* Returns true if we need to instantiate this template instance even if we
16298 know we aren't going to emit it.. */
16299
16300 bool
16301 always_instantiate_p (tree decl)
16302 {
16303 /* We always instantiate inline functions so that we can inline them. An
16304 explicit instantiation declaration prohibits implicit instantiation of
16305 non-inline functions. With high levels of optimization, we would
16306 normally inline non-inline functions -- but we're not allowed to do
16307 that for "extern template" functions. Therefore, we check
16308 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
16309 return ((TREE_CODE (decl) == FUNCTION_DECL
16310 && DECL_DECLARED_INLINE_P (decl))
16311 /* And we need to instantiate static data members so that
16312 their initializers are available in integral constant
16313 expressions. */
16314 || (TREE_CODE (decl) == VAR_DECL
16315 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
16316 }
16317
16318 /* Produce the definition of D, a _DECL generated from a template. If
16319 DEFER_OK is nonzero, then we don't have to actually do the
16320 instantiation now; we just have to do it sometime. Normally it is
16321 an error if this is an explicit instantiation but D is undefined.
16322 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
16323 explicitly instantiated class template. */
16324
16325 tree
16326 instantiate_decl (tree d, int defer_ok,
16327 bool expl_inst_class_mem_p)
16328 {
16329 tree tmpl = DECL_TI_TEMPLATE (d);
16330 tree gen_args;
16331 tree args;
16332 tree td;
16333 tree code_pattern;
16334 tree spec;
16335 tree gen_tmpl;
16336 bool pattern_defined;
16337 int need_push;
16338 location_t saved_loc = input_location;
16339 bool external_p;
16340
16341 /* This function should only be used to instantiate templates for
16342 functions and static member variables. */
16343 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
16344 || TREE_CODE (d) == VAR_DECL);
16345
16346 /* Variables are never deferred; if instantiation is required, they
16347 are instantiated right away. That allows for better code in the
16348 case that an expression refers to the value of the variable --
16349 if the variable has a constant value the referring expression can
16350 take advantage of that fact. */
16351 if (TREE_CODE (d) == VAR_DECL)
16352 defer_ok = 0;
16353
16354 /* Don't instantiate cloned functions. Instead, instantiate the
16355 functions they cloned. */
16356 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
16357 d = DECL_CLONED_FUNCTION (d);
16358
16359 if (DECL_TEMPLATE_INSTANTIATED (d)
16360 || DECL_TEMPLATE_SPECIALIZATION (d))
16361 /* D has already been instantiated or explicitly specialized, so
16362 there's nothing for us to do here.
16363
16364 It might seem reasonable to check whether or not D is an explicit
16365 instantiation, and, if so, stop here. But when an explicit
16366 instantiation is deferred until the end of the compilation,
16367 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
16368 the instantiation. */
16369 return d;
16370
16371 /* Check to see whether we know that this template will be
16372 instantiated in some other file, as with "extern template"
16373 extension. */
16374 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
16375
16376 /* In general, we do not instantiate such templates. */
16377 if (external_p && !always_instantiate_p (d))
16378 return d;
16379
16380 gen_tmpl = most_general_template (tmpl);
16381 gen_args = DECL_TI_ARGS (d);
16382
16383 if (tmpl != gen_tmpl)
16384 /* We should already have the extra args. */
16385 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
16386 == TMPL_ARGS_DEPTH (gen_args));
16387 /* And what's in the hash table should match D. */
16388 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
16389 || spec == NULL_TREE);
16390
16391 /* This needs to happen before any tsubsting. */
16392 if (! push_tinst_level (d))
16393 return d;
16394
16395 timevar_push (TV_PARSE);
16396
16397 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
16398 for the instantiation. */
16399 td = template_for_substitution (d);
16400 code_pattern = DECL_TEMPLATE_RESULT (td);
16401
16402 /* We should never be trying to instantiate a member of a class
16403 template or partial specialization. */
16404 gcc_assert (d != code_pattern);
16405
16406 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
16407 || DECL_TEMPLATE_SPECIALIZATION (td))
16408 /* In the case of a friend template whose definition is provided
16409 outside the class, we may have too many arguments. Drop the
16410 ones we don't need. The same is true for specializations. */
16411 args = get_innermost_template_args
16412 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
16413 else
16414 args = gen_args;
16415
16416 if (TREE_CODE (d) == FUNCTION_DECL)
16417 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
16418 else
16419 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
16420
16421 /* We may be in the middle of deferred access check. Disable it now. */
16422 push_deferring_access_checks (dk_no_deferred);
16423
16424 /* Unless an explicit instantiation directive has already determined
16425 the linkage of D, remember that a definition is available for
16426 this entity. */
16427 if (pattern_defined
16428 && !DECL_INTERFACE_KNOWN (d)
16429 && !DECL_NOT_REALLY_EXTERN (d))
16430 mark_definable (d);
16431
16432 input_location = DECL_SOURCE_LOCATION (d);
16433
16434 /* If D is a member of an explicitly instantiated class template,
16435 and no definition is available, treat it like an implicit
16436 instantiation. */
16437 if (!pattern_defined && expl_inst_class_mem_p
16438 && DECL_EXPLICIT_INSTANTIATION (d))
16439 {
16440 DECL_NOT_REALLY_EXTERN (d) = 0;
16441 DECL_INTERFACE_KNOWN (d) = 0;
16442 SET_DECL_IMPLICIT_INSTANTIATION (d);
16443 }
16444
16445 /* Recheck the substitutions to obtain any warning messages
16446 about ignoring cv qualifiers. Don't do this for artificial decls,
16447 as it breaks the context-sensitive substitution for lambda op(). */
16448 if (!defer_ok && !DECL_ARTIFICIAL (d))
16449 {
16450 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
16451 tree type = TREE_TYPE (gen);
16452
16453 /* Make sure that we can see identifiers, and compute access
16454 correctly. D is already the target FUNCTION_DECL with the
16455 right context. */
16456 push_access_scope (d);
16457
16458 if (TREE_CODE (gen) == FUNCTION_DECL)
16459 {
16460 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
16461 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
16462 d);
16463 /* Don't simply tsubst the function type, as that will give
16464 duplicate warnings about poor parameter qualifications.
16465 The function arguments are the same as the decl_arguments
16466 without the top level cv qualifiers. */
16467 type = TREE_TYPE (type);
16468 }
16469 tsubst (type, gen_args, tf_warning_or_error, d);
16470
16471 pop_access_scope (d);
16472 }
16473
16474 /* Defer all other templates, unless we have been explicitly
16475 forbidden from doing so. */
16476 if (/* If there is no definition, we cannot instantiate the
16477 template. */
16478 ! pattern_defined
16479 /* If it's OK to postpone instantiation, do so. */
16480 || defer_ok
16481 /* If this is a static data member that will be defined
16482 elsewhere, we don't want to instantiate the entire data
16483 member, but we do want to instantiate the initializer so that
16484 we can substitute that elsewhere. */
16485 || (external_p && TREE_CODE (d) == VAR_DECL))
16486 {
16487 /* The definition of the static data member is now required so
16488 we must substitute the initializer. */
16489 if (TREE_CODE (d) == VAR_DECL
16490 && !DECL_INITIAL (d)
16491 && DECL_INITIAL (code_pattern))
16492 {
16493 tree ns;
16494 tree init;
16495
16496 ns = decl_namespace_context (d);
16497 push_nested_namespace (ns);
16498 push_nested_class (DECL_CONTEXT (d));
16499 init = tsubst_expr (DECL_INITIAL (code_pattern),
16500 args,
16501 tf_warning_or_error, NULL_TREE,
16502 /*integral_constant_expression_p=*/false);
16503 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
16504 /*asmspec_tree=*/NULL_TREE,
16505 LOOKUP_ONLYCONVERTING);
16506 pop_nested_class ();
16507 pop_nested_namespace (ns);
16508 }
16509
16510 /* We restore the source position here because it's used by
16511 add_pending_template. */
16512 input_location = saved_loc;
16513
16514 if (at_eof && !pattern_defined
16515 && DECL_EXPLICIT_INSTANTIATION (d)
16516 && DECL_NOT_REALLY_EXTERN (d))
16517 /* [temp.explicit]
16518
16519 The definition of a non-exported function template, a
16520 non-exported member function template, or a non-exported
16521 member function or static data member of a class template
16522 shall be present in every translation unit in which it is
16523 explicitly instantiated. */
16524 permerror (input_location, "explicit instantiation of %qD "
16525 "but no definition available", d);
16526
16527 /* ??? Historically, we have instantiated inline functions, even
16528 when marked as "extern template". */
16529 if (!(external_p && TREE_CODE (d) == VAR_DECL))
16530 add_pending_template (d);
16531 goto out;
16532 }
16533 /* Tell the repository that D is available in this translation unit
16534 -- and see if it is supposed to be instantiated here. */
16535 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
16536 {
16537 /* In a PCH file, despite the fact that the repository hasn't
16538 requested instantiation in the PCH it is still possible that
16539 an instantiation will be required in a file that includes the
16540 PCH. */
16541 if (pch_file)
16542 add_pending_template (d);
16543 /* Instantiate inline functions so that the inliner can do its
16544 job, even though we'll not be emitting a copy of this
16545 function. */
16546 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
16547 goto out;
16548 }
16549
16550 need_push = !cfun || !global_bindings_p ();
16551 if (need_push)
16552 push_to_top_level ();
16553
16554 /* Mark D as instantiated so that recursive calls to
16555 instantiate_decl do not try to instantiate it again. */
16556 DECL_TEMPLATE_INSTANTIATED (d) = 1;
16557
16558 /* Regenerate the declaration in case the template has been modified
16559 by a subsequent redeclaration. */
16560 regenerate_decl_from_template (d, td);
16561
16562 /* We already set the file and line above. Reset them now in case
16563 they changed as a result of calling regenerate_decl_from_template. */
16564 input_location = DECL_SOURCE_LOCATION (d);
16565
16566 if (TREE_CODE (d) == VAR_DECL)
16567 {
16568 tree init;
16569
16570 /* Clear out DECL_RTL; whatever was there before may not be right
16571 since we've reset the type of the declaration. */
16572 SET_DECL_RTL (d, NULL_RTX);
16573 DECL_IN_AGGR_P (d) = 0;
16574
16575 /* The initializer is placed in DECL_INITIAL by
16576 regenerate_decl_from_template. Pull it out so that
16577 cp_finish_decl can process it. */
16578 init = DECL_INITIAL (d);
16579 DECL_INITIAL (d) = NULL_TREE;
16580 DECL_INITIALIZED_P (d) = 0;
16581
16582 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16583 initializer. That function will defer actual emission until
16584 we have a chance to determine linkage. */
16585 DECL_EXTERNAL (d) = 0;
16586
16587 /* Enter the scope of D so that access-checking works correctly. */
16588 push_nested_class (DECL_CONTEXT (d));
16589 cp_finish_decl (d, init, false, NULL_TREE, 0);
16590 pop_nested_class ();
16591 }
16592 else if (TREE_CODE (d) == FUNCTION_DECL)
16593 {
16594 htab_t saved_local_specializations;
16595 tree subst_decl;
16596 tree tmpl_parm;
16597 tree spec_parm;
16598
16599 /* Save away the current list, in case we are instantiating one
16600 template from within the body of another. */
16601 saved_local_specializations = local_specializations;
16602
16603 /* Set up the list of local specializations. */
16604 local_specializations = htab_create (37,
16605 hash_local_specialization,
16606 eq_local_specializations,
16607 NULL);
16608
16609 /* Set up context. */
16610 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
16611
16612 /* Create substitution entries for the parameters. */
16613 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
16614 tmpl_parm = DECL_ARGUMENTS (subst_decl);
16615 spec_parm = DECL_ARGUMENTS (d);
16616 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
16617 {
16618 register_local_specialization (spec_parm, tmpl_parm);
16619 spec_parm = skip_artificial_parms_for (d, spec_parm);
16620 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
16621 }
16622 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16623 {
16624 register_local_specialization (spec_parm, tmpl_parm);
16625 tmpl_parm = TREE_CHAIN (tmpl_parm);
16626 spec_parm = TREE_CHAIN (spec_parm);
16627 }
16628 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16629 {
16630 /* Register the (value) argument pack as a specialization of
16631 TMPL_PARM, then move on. */
16632 tree argpack = make_fnparm_pack (spec_parm);
16633 register_local_specialization (argpack, tmpl_parm);
16634 tmpl_parm = TREE_CHAIN (tmpl_parm);
16635 spec_parm = NULL_TREE;
16636 }
16637 gcc_assert (!spec_parm);
16638
16639 /* Substitute into the body of the function. */
16640 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
16641 tf_warning_or_error, tmpl,
16642 /*integral_constant_expression_p=*/false);
16643
16644 /* Set the current input_location to the end of the function
16645 so that finish_function knows where we are. */
16646 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
16647
16648 /* We don't need the local specializations any more. */
16649 htab_delete (local_specializations);
16650 local_specializations = saved_local_specializations;
16651
16652 /* Finish the function. */
16653 d = finish_function (0);
16654 expand_or_defer_fn (d);
16655 }
16656
16657 /* We're not deferring instantiation any more. */
16658 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
16659
16660 if (need_push)
16661 pop_from_top_level ();
16662
16663 out:
16664 input_location = saved_loc;
16665 pop_deferring_access_checks ();
16666 pop_tinst_level ();
16667
16668 timevar_pop (TV_PARSE);
16669
16670 return d;
16671 }
16672
16673 /* Run through the list of templates that we wish we could
16674 instantiate, and instantiate any we can. RETRIES is the
16675 number of times we retry pending template instantiation. */
16676
16677 void
16678 instantiate_pending_templates (int retries)
16679 {
16680 int reconsider;
16681 location_t saved_loc = input_location;
16682
16683 /* Instantiating templates may trigger vtable generation. This in turn
16684 may require further template instantiations. We place a limit here
16685 to avoid infinite loop. */
16686 if (pending_templates && retries >= max_tinst_depth)
16687 {
16688 tree decl = pending_templates->tinst->decl;
16689
16690 error ("template instantiation depth exceeds maximum of %d"
16691 " instantiating %q+D, possibly from virtual table generation"
16692 " (use -ftemplate-depth-NN to increase the maximum)",
16693 max_tinst_depth, decl);
16694 if (TREE_CODE (decl) == FUNCTION_DECL)
16695 /* Pretend that we defined it. */
16696 DECL_INITIAL (decl) = error_mark_node;
16697 return;
16698 }
16699
16700 do
16701 {
16702 struct pending_template **t = &pending_templates;
16703 struct pending_template *last = NULL;
16704 reconsider = 0;
16705 while (*t)
16706 {
16707 tree instantiation = reopen_tinst_level ((*t)->tinst);
16708 bool complete = false;
16709
16710 if (TYPE_P (instantiation))
16711 {
16712 tree fn;
16713
16714 if (!COMPLETE_TYPE_P (instantiation))
16715 {
16716 instantiate_class_template (instantiation);
16717 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
16718 for (fn = TYPE_METHODS (instantiation);
16719 fn;
16720 fn = TREE_CHAIN (fn))
16721 if (! DECL_ARTIFICIAL (fn))
16722 instantiate_decl (fn,
16723 /*defer_ok=*/0,
16724 /*expl_inst_class_mem_p=*/false);
16725 if (COMPLETE_TYPE_P (instantiation))
16726 reconsider = 1;
16727 }
16728
16729 complete = COMPLETE_TYPE_P (instantiation);
16730 }
16731 else
16732 {
16733 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
16734 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
16735 {
16736 instantiation
16737 = instantiate_decl (instantiation,
16738 /*defer_ok=*/0,
16739 /*expl_inst_class_mem_p=*/false);
16740 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
16741 reconsider = 1;
16742 }
16743
16744 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
16745 || DECL_TEMPLATE_INSTANTIATED (instantiation));
16746 }
16747
16748 if (complete)
16749 /* If INSTANTIATION has been instantiated, then we don't
16750 need to consider it again in the future. */
16751 *t = (*t)->next;
16752 else
16753 {
16754 last = *t;
16755 t = &(*t)->next;
16756 }
16757 tinst_depth = 0;
16758 current_tinst_level = NULL;
16759 }
16760 last_pending_template = last;
16761 }
16762 while (reconsider);
16763
16764 input_location = saved_loc;
16765 }
16766
16767 /* Substitute ARGVEC into T, which is a list of initializers for
16768 either base class or a non-static data member. The TREE_PURPOSEs
16769 are DECLs, and the TREE_VALUEs are the initializer values. Used by
16770 instantiate_decl. */
16771
16772 static tree
16773 tsubst_initializer_list (tree t, tree argvec)
16774 {
16775 tree inits = NULL_TREE;
16776
16777 for (; t; t = TREE_CHAIN (t))
16778 {
16779 tree decl;
16780 tree init;
16781 tree expanded_bases = NULL_TREE;
16782 tree expanded_arguments = NULL_TREE;
16783 int i, len = 1;
16784
16785 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
16786 {
16787 tree expr;
16788 tree arg;
16789
16790 /* Expand the base class expansion type into separate base
16791 classes. */
16792 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
16793 tf_warning_or_error,
16794 NULL_TREE);
16795 if (expanded_bases == error_mark_node)
16796 continue;
16797
16798 /* We'll be building separate TREE_LISTs of arguments for
16799 each base. */
16800 len = TREE_VEC_LENGTH (expanded_bases);
16801 expanded_arguments = make_tree_vec (len);
16802 for (i = 0; i < len; i++)
16803 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
16804
16805 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
16806 expand each argument in the TREE_VALUE of t. */
16807 expr = make_node (EXPR_PACK_EXPANSION);
16808 PACK_EXPANSION_PARAMETER_PACKS (expr) =
16809 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
16810
16811 if (TREE_VALUE (t) == void_type_node)
16812 /* VOID_TYPE_NODE is used to indicate
16813 value-initialization. */
16814 {
16815 for (i = 0; i < len; i++)
16816 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
16817 }
16818 else
16819 {
16820 /* Substitute parameter packs into each argument in the
16821 TREE_LIST. */
16822 in_base_initializer = 1;
16823 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
16824 {
16825 tree expanded_exprs;
16826
16827 /* Expand the argument. */
16828 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
16829 expanded_exprs
16830 = tsubst_pack_expansion (expr, argvec,
16831 tf_warning_or_error,
16832 NULL_TREE);
16833 if (expanded_exprs == error_mark_node)
16834 continue;
16835
16836 /* Prepend each of the expanded expressions to the
16837 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
16838 for (i = 0; i < len; i++)
16839 {
16840 TREE_VEC_ELT (expanded_arguments, i) =
16841 tree_cons (NULL_TREE,
16842 TREE_VEC_ELT (expanded_exprs, i),
16843 TREE_VEC_ELT (expanded_arguments, i));
16844 }
16845 }
16846 in_base_initializer = 0;
16847
16848 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
16849 since we built them backwards. */
16850 for (i = 0; i < len; i++)
16851 {
16852 TREE_VEC_ELT (expanded_arguments, i) =
16853 nreverse (TREE_VEC_ELT (expanded_arguments, i));
16854 }
16855 }
16856 }
16857
16858 for (i = 0; i < len; ++i)
16859 {
16860 if (expanded_bases)
16861 {
16862 decl = TREE_VEC_ELT (expanded_bases, i);
16863 decl = expand_member_init (decl);
16864 init = TREE_VEC_ELT (expanded_arguments, i);
16865 }
16866 else
16867 {
16868 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
16869 tf_warning_or_error, NULL_TREE);
16870
16871 decl = expand_member_init (decl);
16872 if (decl && !DECL_P (decl))
16873 in_base_initializer = 1;
16874
16875 init = tsubst_expr (TREE_VALUE (t), argvec,
16876 tf_warning_or_error, NULL_TREE,
16877 /*integral_constant_expression_p=*/false);
16878 in_base_initializer = 0;
16879 }
16880
16881 if (decl)
16882 {
16883 init = build_tree_list (decl, init);
16884 TREE_CHAIN (init) = inits;
16885 inits = init;
16886 }
16887 }
16888 }
16889 return inits;
16890 }
16891
16892 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
16893
16894 static void
16895 set_current_access_from_decl (tree decl)
16896 {
16897 if (TREE_PRIVATE (decl))
16898 current_access_specifier = access_private_node;
16899 else if (TREE_PROTECTED (decl))
16900 current_access_specifier = access_protected_node;
16901 else
16902 current_access_specifier = access_public_node;
16903 }
16904
16905 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
16906 is the instantiation (which should have been created with
16907 start_enum) and ARGS are the template arguments to use. */
16908
16909 static void
16910 tsubst_enum (tree tag, tree newtag, tree args)
16911 {
16912 tree e;
16913
16914 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
16915 {
16916 tree value;
16917 tree decl;
16918
16919 decl = TREE_VALUE (e);
16920 /* Note that in a template enum, the TREE_VALUE is the
16921 CONST_DECL, not the corresponding INTEGER_CST. */
16922 value = tsubst_expr (DECL_INITIAL (decl),
16923 args, tf_warning_or_error, NULL_TREE,
16924 /*integral_constant_expression_p=*/true);
16925
16926 /* Give this enumeration constant the correct access. */
16927 set_current_access_from_decl (decl);
16928
16929 /* Actually build the enumerator itself. */
16930 build_enumerator (DECL_NAME (decl), value, newtag);
16931 }
16932
16933 finish_enum (newtag);
16934 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
16935 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
16936 }
16937
16938 /* DECL is a FUNCTION_DECL that is a template specialization. Return
16939 its type -- but without substituting the innermost set of template
16940 arguments. So, innermost set of template parameters will appear in
16941 the type. */
16942
16943 tree
16944 get_mostly_instantiated_function_type (tree decl)
16945 {
16946 tree fn_type;
16947 tree tmpl;
16948 tree targs;
16949 tree tparms;
16950 int parm_depth;
16951
16952 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16953 targs = DECL_TI_ARGS (decl);
16954 tparms = DECL_TEMPLATE_PARMS (tmpl);
16955 parm_depth = TMPL_PARMS_DEPTH (tparms);
16956
16957 /* There should be as many levels of arguments as there are levels
16958 of parameters. */
16959 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
16960
16961 fn_type = TREE_TYPE (tmpl);
16962
16963 if (parm_depth == 1)
16964 /* No substitution is necessary. */
16965 ;
16966 else
16967 {
16968 int i, save_access_control;
16969 tree partial_args;
16970
16971 /* Replace the innermost level of the TARGS with NULL_TREEs to
16972 let tsubst know not to substitute for those parameters. */
16973 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
16974 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
16975 SET_TMPL_ARGS_LEVEL (partial_args, i,
16976 TMPL_ARGS_LEVEL (targs, i));
16977 SET_TMPL_ARGS_LEVEL (partial_args,
16978 TMPL_ARGS_DEPTH (targs),
16979 make_tree_vec (DECL_NTPARMS (tmpl)));
16980
16981 /* Disable access control as this function is used only during
16982 name-mangling. */
16983 save_access_control = flag_access_control;
16984 flag_access_control = 0;
16985
16986 ++processing_template_decl;
16987 /* Now, do the (partial) substitution to figure out the
16988 appropriate function type. */
16989 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
16990 --processing_template_decl;
16991
16992 /* Substitute into the template parameters to obtain the real
16993 innermost set of parameters. This step is important if the
16994 innermost set of template parameters contains value
16995 parameters whose types depend on outer template parameters. */
16996 TREE_VEC_LENGTH (partial_args)--;
16997 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
16998
16999 flag_access_control = save_access_control;
17000 }
17001
17002 return fn_type;
17003 }
17004
17005 /* Return truthvalue if we're processing a template different from
17006 the last one involved in diagnostics. */
17007 int
17008 problematic_instantiation_changed (void)
17009 {
17010 return last_template_error_tick != tinst_level_tick;
17011 }
17012
17013 /* Remember current template involved in diagnostics. */
17014 void
17015 record_last_problematic_instantiation (void)
17016 {
17017 last_template_error_tick = tinst_level_tick;
17018 }
17019
17020 struct tinst_level *
17021 current_instantiation (void)
17022 {
17023 return current_tinst_level;
17024 }
17025
17026 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17027 type. Return zero for ok, nonzero for disallowed. Issue error and
17028 warning messages under control of COMPLAIN. */
17029
17030 static int
17031 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
17032 {
17033 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
17034 return 0;
17035 else if (POINTER_TYPE_P (type))
17036 return 0;
17037 else if (TYPE_PTR_TO_MEMBER_P (type))
17038 return 0;
17039 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17040 return 0;
17041 else if (TREE_CODE (type) == TYPENAME_TYPE)
17042 return 0;
17043
17044 if (complain & tf_error)
17045 error ("%q#T is not a valid type for a template constant parameter", type);
17046 return 1;
17047 }
17048
17049 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17050 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17051
17052 static bool
17053 dependent_type_p_r (tree type)
17054 {
17055 tree scope;
17056
17057 /* [temp.dep.type]
17058
17059 A type is dependent if it is:
17060
17061 -- a template parameter. Template template parameters are types
17062 for us (since TYPE_P holds true for them) so we handle
17063 them here. */
17064 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17065 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
17066 return true;
17067 /* -- a qualified-id with a nested-name-specifier which contains a
17068 class-name that names a dependent type or whose unqualified-id
17069 names a dependent type. */
17070 if (TREE_CODE (type) == TYPENAME_TYPE)
17071 return true;
17072 /* -- a cv-qualified type where the cv-unqualified type is
17073 dependent. */
17074 type = TYPE_MAIN_VARIANT (type);
17075 /* -- a compound type constructed from any dependent type. */
17076 if (TYPE_PTR_TO_MEMBER_P (type))
17077 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
17078 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17079 (type)));
17080 else if (TREE_CODE (type) == POINTER_TYPE
17081 || TREE_CODE (type) == REFERENCE_TYPE)
17082 return dependent_type_p (TREE_TYPE (type));
17083 else if (TREE_CODE (type) == FUNCTION_TYPE
17084 || TREE_CODE (type) == METHOD_TYPE)
17085 {
17086 tree arg_type;
17087
17088 if (dependent_type_p (TREE_TYPE (type)))
17089 return true;
17090 for (arg_type = TYPE_ARG_TYPES (type);
17091 arg_type;
17092 arg_type = TREE_CHAIN (arg_type))
17093 if (dependent_type_p (TREE_VALUE (arg_type)))
17094 return true;
17095 return false;
17096 }
17097 /* -- an array type constructed from any dependent type or whose
17098 size is specified by a constant expression that is
17099 value-dependent. */
17100 if (TREE_CODE (type) == ARRAY_TYPE)
17101 {
17102 if (TYPE_DOMAIN (type)
17103 && dependent_type_p (TYPE_DOMAIN (type)))
17104 return true;
17105 return dependent_type_p (TREE_TYPE (type));
17106 }
17107 else if (TREE_CODE (type) == INTEGER_TYPE
17108 && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
17109 {
17110 /* If this is the TYPE_DOMAIN of an array type, consider it
17111 dependent. We already checked for value-dependence in
17112 compute_array_index_type. */
17113 return type_dependent_expression_p (TYPE_MAX_VALUE (type));
17114 }
17115
17116 /* -- a template-id in which either the template name is a template
17117 parameter ... */
17118 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17119 return true;
17120 /* ... or any of the template arguments is a dependent type or
17121 an expression that is type-dependent or value-dependent. */
17122 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
17123 && (any_dependent_template_arguments_p
17124 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
17125 return true;
17126
17127 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17128 argument of the `typeof' expression is not type-dependent, then
17129 it should already been have resolved. */
17130 if (TREE_CODE (type) == TYPEOF_TYPE
17131 || TREE_CODE (type) == DECLTYPE_TYPE)
17132 return true;
17133
17134 /* A template argument pack is dependent if any of its packed
17135 arguments are. */
17136 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
17137 {
17138 tree args = ARGUMENT_PACK_ARGS (type);
17139 int i, len = TREE_VEC_LENGTH (args);
17140 for (i = 0; i < len; ++i)
17141 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17142 return true;
17143 }
17144
17145 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17146 be template parameters. */
17147 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
17148 return true;
17149
17150 /* The standard does not specifically mention types that are local
17151 to template functions or local classes, but they should be
17152 considered dependent too. For example:
17153
17154 template <int I> void f() {
17155 enum E { a = I };
17156 S<sizeof (E)> s;
17157 }
17158
17159 The size of `E' cannot be known until the value of `I' has been
17160 determined. Therefore, `E' must be considered dependent. */
17161 scope = TYPE_CONTEXT (type);
17162 if (scope && TYPE_P (scope))
17163 return dependent_type_p (scope);
17164 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
17165 return type_dependent_expression_p (scope);
17166
17167 /* Other types are non-dependent. */
17168 return false;
17169 }
17170
17171 /* Returns TRUE if TYPE is dependent, in the sense of
17172 [temp.dep.type]. */
17173
17174 bool
17175 dependent_type_p (tree type)
17176 {
17177 /* If there are no template parameters in scope, then there can't be
17178 any dependent types. */
17179 if (!processing_template_decl)
17180 {
17181 /* If we are not processing a template, then nobody should be
17182 providing us with a dependent type. */
17183 gcc_assert (type);
17184 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
17185 return false;
17186 }
17187
17188 /* If the type is NULL, we have not computed a type for the entity
17189 in question; in that case, the type is dependent. */
17190 if (!type)
17191 return true;
17192
17193 /* Erroneous types can be considered non-dependent. */
17194 if (type == error_mark_node)
17195 return false;
17196
17197 /* If we have not already computed the appropriate value for TYPE,
17198 do so now. */
17199 if (!TYPE_DEPENDENT_P_VALID (type))
17200 {
17201 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
17202 TYPE_DEPENDENT_P_VALID (type) = 1;
17203 }
17204
17205 return TYPE_DEPENDENT_P (type);
17206 }
17207
17208 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
17209 lookup. In other words, a dependent type that is not the current
17210 instantiation. */
17211
17212 bool
17213 dependent_scope_p (tree scope)
17214 {
17215 return (scope && TYPE_P (scope) && dependent_type_p (scope)
17216 && !currently_open_class (scope));
17217 }
17218
17219 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
17220
17221 static bool
17222 dependent_scope_ref_p (tree expression, bool criterion (tree))
17223 {
17224 tree scope;
17225 tree name;
17226
17227 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
17228
17229 if (!TYPE_P (TREE_OPERAND (expression, 0)))
17230 return true;
17231
17232 scope = TREE_OPERAND (expression, 0);
17233 name = TREE_OPERAND (expression, 1);
17234
17235 /* [temp.dep.expr]
17236
17237 An id-expression is type-dependent if it contains a
17238 nested-name-specifier that contains a class-name that names a
17239 dependent type. */
17240 /* The suggested resolution to Core Issue 224 implies that if the
17241 qualifying type is the current class, then we must peek
17242 inside it. */
17243 if (DECL_P (name)
17244 && currently_open_class (scope)
17245 && !criterion (name))
17246 return false;
17247 if (dependent_type_p (scope))
17248 return true;
17249
17250 return false;
17251 }
17252
17253 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
17254 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
17255 expression. */
17256
17257 bool
17258 value_dependent_expression_p (tree expression)
17259 {
17260 if (!processing_template_decl)
17261 return false;
17262
17263 /* A name declared with a dependent type. */
17264 if (DECL_P (expression) && type_dependent_expression_p (expression))
17265 return true;
17266
17267 switch (TREE_CODE (expression))
17268 {
17269 case IDENTIFIER_NODE:
17270 /* A name that has not been looked up -- must be dependent. */
17271 return true;
17272
17273 case TEMPLATE_PARM_INDEX:
17274 /* A non-type template parm. */
17275 return true;
17276
17277 case CONST_DECL:
17278 /* A non-type template parm. */
17279 if (DECL_TEMPLATE_PARM_P (expression))
17280 return true;
17281 return value_dependent_expression_p (DECL_INITIAL (expression));
17282
17283 case VAR_DECL:
17284 /* A constant with integral or enumeration type and is initialized
17285 with an expression that is value-dependent. */
17286 if (DECL_INITIAL (expression)
17287 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
17288 && value_dependent_expression_p (DECL_INITIAL (expression)))
17289 return true;
17290 return false;
17291
17292 case DYNAMIC_CAST_EXPR:
17293 case STATIC_CAST_EXPR:
17294 case CONST_CAST_EXPR:
17295 case REINTERPRET_CAST_EXPR:
17296 case CAST_EXPR:
17297 /* These expressions are value-dependent if the type to which
17298 the cast occurs is dependent or the expression being casted
17299 is value-dependent. */
17300 {
17301 tree type = TREE_TYPE (expression);
17302
17303 if (dependent_type_p (type))
17304 return true;
17305
17306 /* A functional cast has a list of operands. */
17307 expression = TREE_OPERAND (expression, 0);
17308 if (!expression)
17309 {
17310 /* If there are no operands, it must be an expression such
17311 as "int()". This should not happen for aggregate types
17312 because it would form non-constant expressions. */
17313 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
17314
17315 return false;
17316 }
17317
17318 if (TREE_CODE (expression) == TREE_LIST)
17319 return any_value_dependent_elements_p (expression);
17320
17321 return value_dependent_expression_p (expression);
17322 }
17323
17324 case SIZEOF_EXPR:
17325 case ALIGNOF_EXPR:
17326 /* A `sizeof' expression is value-dependent if the operand is
17327 type-dependent or is a pack expansion. */
17328 expression = TREE_OPERAND (expression, 0);
17329 if (PACK_EXPANSION_P (expression))
17330 return true;
17331 else if (TYPE_P (expression))
17332 return dependent_type_p (expression);
17333 return type_dependent_expression_p (expression);
17334
17335 case SCOPE_REF:
17336 return dependent_scope_ref_p (expression, value_dependent_expression_p);
17337
17338 case COMPONENT_REF:
17339 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
17340 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
17341
17342 case CALL_EXPR:
17343 /* A CALL_EXPR may appear in a constant expression if it is a
17344 call to a builtin function, e.g., __builtin_constant_p. All
17345 such calls are value-dependent. */
17346 return true;
17347
17348 case NONTYPE_ARGUMENT_PACK:
17349 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
17350 is value-dependent. */
17351 {
17352 tree values = ARGUMENT_PACK_ARGS (expression);
17353 int i, len = TREE_VEC_LENGTH (values);
17354
17355 for (i = 0; i < len; ++i)
17356 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
17357 return true;
17358
17359 return false;
17360 }
17361
17362 case TRAIT_EXPR:
17363 {
17364 tree type2 = TRAIT_EXPR_TYPE2 (expression);
17365 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
17366 || (type2 ? dependent_type_p (type2) : false));
17367 }
17368
17369 case MODOP_EXPR:
17370 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
17371 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
17372
17373 default:
17374 /* A constant expression is value-dependent if any subexpression is
17375 value-dependent. */
17376 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
17377 {
17378 case tcc_reference:
17379 case tcc_unary:
17380 return (value_dependent_expression_p
17381 (TREE_OPERAND (expression, 0)));
17382
17383 case tcc_comparison:
17384 case tcc_binary:
17385 return ((value_dependent_expression_p
17386 (TREE_OPERAND (expression, 0)))
17387 || (value_dependent_expression_p
17388 (TREE_OPERAND (expression, 1))));
17389
17390 case tcc_expression:
17391 case tcc_vl_exp:
17392 {
17393 int i;
17394 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
17395 /* In some cases, some of the operands may be missing.
17396 (For example, in the case of PREDECREMENT_EXPR, the
17397 amount to increment by may be missing.) That doesn't
17398 make the expression dependent. */
17399 if (TREE_OPERAND (expression, i)
17400 && (value_dependent_expression_p
17401 (TREE_OPERAND (expression, i))))
17402 return true;
17403 return false;
17404 }
17405
17406 default:
17407 break;
17408 }
17409 }
17410
17411 /* The expression is not value-dependent. */
17412 return false;
17413 }
17414
17415 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
17416 [temp.dep.expr]. */
17417
17418 bool
17419 type_dependent_expression_p (tree expression)
17420 {
17421 if (!processing_template_decl)
17422 return false;
17423
17424 if (expression == error_mark_node)
17425 return false;
17426
17427 /* An unresolved name is always dependent. */
17428 if (TREE_CODE (expression) == IDENTIFIER_NODE
17429 || TREE_CODE (expression) == USING_DECL)
17430 return true;
17431
17432 /* Some expression forms are never type-dependent. */
17433 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
17434 || TREE_CODE (expression) == SIZEOF_EXPR
17435 || TREE_CODE (expression) == ALIGNOF_EXPR
17436 || TREE_CODE (expression) == TRAIT_EXPR
17437 || TREE_CODE (expression) == TYPEID_EXPR
17438 || TREE_CODE (expression) == DELETE_EXPR
17439 || TREE_CODE (expression) == VEC_DELETE_EXPR
17440 || TREE_CODE (expression) == THROW_EXPR)
17441 return false;
17442
17443 /* The types of these expressions depends only on the type to which
17444 the cast occurs. */
17445 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
17446 || TREE_CODE (expression) == STATIC_CAST_EXPR
17447 || TREE_CODE (expression) == CONST_CAST_EXPR
17448 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
17449 || TREE_CODE (expression) == CAST_EXPR)
17450 return dependent_type_p (TREE_TYPE (expression));
17451
17452 /* The types of these expressions depends only on the type created
17453 by the expression. */
17454 if (TREE_CODE (expression) == NEW_EXPR
17455 || TREE_CODE (expression) == VEC_NEW_EXPR)
17456 {
17457 /* For NEW_EXPR tree nodes created inside a template, either
17458 the object type itself or a TREE_LIST may appear as the
17459 operand 1. */
17460 tree type = TREE_OPERAND (expression, 1);
17461 if (TREE_CODE (type) == TREE_LIST)
17462 /* This is an array type. We need to check array dimensions
17463 as well. */
17464 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
17465 || value_dependent_expression_p
17466 (TREE_OPERAND (TREE_VALUE (type), 1));
17467 else
17468 return dependent_type_p (type);
17469 }
17470
17471 if (TREE_CODE (expression) == SCOPE_REF
17472 && dependent_scope_ref_p (expression,
17473 type_dependent_expression_p))
17474 return true;
17475
17476 if (TREE_CODE (expression) == FUNCTION_DECL
17477 && DECL_LANG_SPECIFIC (expression)
17478 && DECL_TEMPLATE_INFO (expression)
17479 && (any_dependent_template_arguments_p
17480 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
17481 return true;
17482
17483 if (TREE_CODE (expression) == TEMPLATE_DECL
17484 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
17485 return false;
17486
17487 if (TREE_CODE (expression) == STMT_EXPR)
17488 expression = stmt_expr_value_expr (expression);
17489
17490 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
17491 {
17492 tree elt;
17493 unsigned i;
17494
17495 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
17496 {
17497 if (type_dependent_expression_p (elt))
17498 return true;
17499 }
17500 return false;
17501 }
17502
17503 if (TREE_TYPE (expression) == unknown_type_node)
17504 {
17505 if (TREE_CODE (expression) == ADDR_EXPR)
17506 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
17507 if (TREE_CODE (expression) == COMPONENT_REF
17508 || TREE_CODE (expression) == OFFSET_REF)
17509 {
17510 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
17511 return true;
17512 expression = TREE_OPERAND (expression, 1);
17513 if (TREE_CODE (expression) == IDENTIFIER_NODE)
17514 return false;
17515 }
17516 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17517 if (TREE_CODE (expression) == SCOPE_REF)
17518 return false;
17519
17520 if (TREE_CODE (expression) == BASELINK)
17521 expression = BASELINK_FUNCTIONS (expression);
17522
17523 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
17524 {
17525 if (any_dependent_template_arguments_p
17526 (TREE_OPERAND (expression, 1)))
17527 return true;
17528 expression = TREE_OPERAND (expression, 0);
17529 }
17530 gcc_assert (TREE_CODE (expression) == OVERLOAD
17531 || TREE_CODE (expression) == FUNCTION_DECL);
17532
17533 while (expression)
17534 {
17535 if (type_dependent_expression_p (OVL_CURRENT (expression)))
17536 return true;
17537 expression = OVL_NEXT (expression);
17538 }
17539 return false;
17540 }
17541
17542 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
17543
17544 return (dependent_type_p (TREE_TYPE (expression)));
17545 }
17546
17547 /* Like type_dependent_expression_p, but it also works while not processing
17548 a template definition, i.e. during substitution or mangling. */
17549
17550 bool
17551 type_dependent_expression_p_push (tree expr)
17552 {
17553 bool b;
17554 ++processing_template_decl;
17555 b = type_dependent_expression_p (expr);
17556 --processing_template_decl;
17557 return b;
17558 }
17559
17560 /* Returns TRUE if ARGS contains a type-dependent expression. */
17561
17562 bool
17563 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
17564 {
17565 unsigned int i;
17566 tree arg;
17567
17568 for (i = 0; VEC_iterate (tree, args, i, arg); ++i)
17569 {
17570 if (type_dependent_expression_p (arg))
17571 return true;
17572 }
17573 return false;
17574 }
17575
17576 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17577 expressions) contains any value-dependent expressions. */
17578
17579 bool
17580 any_value_dependent_elements_p (const_tree list)
17581 {
17582 for (; list; list = TREE_CHAIN (list))
17583 if (value_dependent_expression_p (TREE_VALUE (list)))
17584 return true;
17585
17586 return false;
17587 }
17588
17589 /* Returns TRUE if the ARG (a template argument) is dependent. */
17590
17591 bool
17592 dependent_template_arg_p (tree arg)
17593 {
17594 if (!processing_template_decl)
17595 return false;
17596
17597 if (TREE_CODE (arg) == TEMPLATE_DECL
17598 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17599 return dependent_template_p (arg);
17600 else if (ARGUMENT_PACK_P (arg))
17601 {
17602 tree args = ARGUMENT_PACK_ARGS (arg);
17603 int i, len = TREE_VEC_LENGTH (args);
17604 for (i = 0; i < len; ++i)
17605 {
17606 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17607 return true;
17608 }
17609
17610 return false;
17611 }
17612 else if (TYPE_P (arg))
17613 return dependent_type_p (arg);
17614 else
17615 return (type_dependent_expression_p (arg)
17616 || value_dependent_expression_p (arg));
17617 }
17618
17619 /* Returns true if ARGS (a collection of template arguments) contains
17620 any types that require structural equality testing. */
17621
17622 bool
17623 any_template_arguments_need_structural_equality_p (tree args)
17624 {
17625 int i;
17626 int j;
17627
17628 if (!args)
17629 return false;
17630 if (args == error_mark_node)
17631 return true;
17632
17633 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17634 {
17635 tree level = TMPL_ARGS_LEVEL (args, i + 1);
17636 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17637 {
17638 tree arg = TREE_VEC_ELT (level, j);
17639 tree packed_args = NULL_TREE;
17640 int k, len = 1;
17641
17642 if (ARGUMENT_PACK_P (arg))
17643 {
17644 /* Look inside the argument pack. */
17645 packed_args = ARGUMENT_PACK_ARGS (arg);
17646 len = TREE_VEC_LENGTH (packed_args);
17647 }
17648
17649 for (k = 0; k < len; ++k)
17650 {
17651 if (packed_args)
17652 arg = TREE_VEC_ELT (packed_args, k);
17653
17654 if (error_operand_p (arg))
17655 return true;
17656 else if (TREE_CODE (arg) == TEMPLATE_DECL
17657 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17658 continue;
17659 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
17660 return true;
17661 else if (!TYPE_P (arg) && TREE_TYPE (arg)
17662 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
17663 return true;
17664 }
17665 }
17666 }
17667
17668 return false;
17669 }
17670
17671 /* Returns true if ARGS (a collection of template arguments) contains
17672 any dependent arguments. */
17673
17674 bool
17675 any_dependent_template_arguments_p (const_tree args)
17676 {
17677 int i;
17678 int j;
17679
17680 if (!args)
17681 return false;
17682 if (args == error_mark_node)
17683 return true;
17684
17685 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17686 {
17687 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
17688 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17689 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
17690 return true;
17691 }
17692
17693 return false;
17694 }
17695
17696 /* Returns TRUE if the template TMPL is dependent. */
17697
17698 bool
17699 dependent_template_p (tree tmpl)
17700 {
17701 if (TREE_CODE (tmpl) == OVERLOAD)
17702 {
17703 while (tmpl)
17704 {
17705 if (dependent_template_p (OVL_FUNCTION (tmpl)))
17706 return true;
17707 tmpl = OVL_CHAIN (tmpl);
17708 }
17709 return false;
17710 }
17711
17712 /* Template template parameters are dependent. */
17713 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
17714 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
17715 return true;
17716 /* So are names that have not been looked up. */
17717 if (TREE_CODE (tmpl) == SCOPE_REF
17718 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
17719 return true;
17720 /* So are member templates of dependent classes. */
17721 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
17722 return dependent_type_p (DECL_CONTEXT (tmpl));
17723 return false;
17724 }
17725
17726 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
17727
17728 bool
17729 dependent_template_id_p (tree tmpl, tree args)
17730 {
17731 return (dependent_template_p (tmpl)
17732 || any_dependent_template_arguments_p (args));
17733 }
17734
17735 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
17736 is dependent. */
17737
17738 bool
17739 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
17740 {
17741 int i;
17742
17743 if (!processing_template_decl)
17744 return false;
17745
17746 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
17747 {
17748 tree decl = TREE_VEC_ELT (declv, i);
17749 tree init = TREE_VEC_ELT (initv, i);
17750 tree cond = TREE_VEC_ELT (condv, i);
17751 tree incr = TREE_VEC_ELT (incrv, i);
17752
17753 if (type_dependent_expression_p (decl))
17754 return true;
17755
17756 if (init && type_dependent_expression_p (init))
17757 return true;
17758
17759 if (type_dependent_expression_p (cond))
17760 return true;
17761
17762 if (COMPARISON_CLASS_P (cond)
17763 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
17764 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
17765 return true;
17766
17767 if (TREE_CODE (incr) == MODOP_EXPR)
17768 {
17769 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
17770 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
17771 return true;
17772 }
17773 else if (type_dependent_expression_p (incr))
17774 return true;
17775 else if (TREE_CODE (incr) == MODIFY_EXPR)
17776 {
17777 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
17778 return true;
17779 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
17780 {
17781 tree t = TREE_OPERAND (incr, 1);
17782 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
17783 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
17784 return true;
17785 }
17786 }
17787 }
17788
17789 return false;
17790 }
17791
17792 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
17793 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
17794 no such TYPE can be found. Note that this function peers inside
17795 uninstantiated templates and therefore should be used only in
17796 extremely limited situations. ONLY_CURRENT_P restricts this
17797 peering to the currently open classes hierarchy (which is required
17798 when comparing types). */
17799
17800 tree
17801 resolve_typename_type (tree type, bool only_current_p)
17802 {
17803 tree scope;
17804 tree name;
17805 tree decl;
17806 int quals;
17807 tree pushed_scope;
17808 tree result;
17809
17810 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
17811
17812 scope = TYPE_CONTEXT (type);
17813 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
17814 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
17815 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
17816 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
17817 identifier of the TYPENAME_TYPE anymore.
17818 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
17819 TYPENAME_TYPE instead, we avoid messing up with a possible
17820 typedef variant case. */
17821 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
17822
17823 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
17824 it first before we can figure out what NAME refers to. */
17825 if (TREE_CODE (scope) == TYPENAME_TYPE)
17826 scope = resolve_typename_type (scope, only_current_p);
17827 /* If we don't know what SCOPE refers to, then we cannot resolve the
17828 TYPENAME_TYPE. */
17829 if (TREE_CODE (scope) == TYPENAME_TYPE)
17830 return type;
17831 /* If the SCOPE is a template type parameter, we have no way of
17832 resolving the name. */
17833 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
17834 return type;
17835 /* If the SCOPE is not the current instantiation, there's no reason
17836 to look inside it. */
17837 if (only_current_p && !currently_open_class (scope))
17838 return type;
17839 /* If this is a typedef, we don't want to look inside (c++/11987). */
17840 if (typedef_variant_p (type))
17841 return type;
17842 /* If SCOPE isn't the template itself, it will not have a valid
17843 TYPE_FIELDS list. */
17844 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
17845 /* scope is either the template itself or a compatible instantiation
17846 like X<T>, so look up the name in the original template. */
17847 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
17848 else
17849 /* scope is a partial instantiation, so we can't do the lookup or we
17850 will lose the template arguments. */
17851 return type;
17852 /* Enter the SCOPE so that name lookup will be resolved as if we
17853 were in the class definition. In particular, SCOPE will no
17854 longer be considered a dependent type. */
17855 pushed_scope = push_scope (scope);
17856 /* Look up the declaration. */
17857 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
17858
17859 result = NULL_TREE;
17860
17861 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
17862 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
17863 if (!decl)
17864 /*nop*/;
17865 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
17866 && TREE_CODE (decl) == TYPE_DECL)
17867 {
17868 result = TREE_TYPE (decl);
17869 if (result == error_mark_node)
17870 result = NULL_TREE;
17871 }
17872 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
17873 && DECL_CLASS_TEMPLATE_P (decl))
17874 {
17875 tree tmpl;
17876 tree args;
17877 /* Obtain the template and the arguments. */
17878 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
17879 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
17880 /* Instantiate the template. */
17881 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
17882 /*entering_scope=*/0,
17883 tf_error | tf_user);
17884 if (result == error_mark_node)
17885 result = NULL_TREE;
17886 }
17887
17888 /* Leave the SCOPE. */
17889 if (pushed_scope)
17890 pop_scope (pushed_scope);
17891
17892 /* If we failed to resolve it, return the original typename. */
17893 if (!result)
17894 return type;
17895
17896 /* If lookup found a typename type, resolve that too. */
17897 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
17898 {
17899 /* Ill-formed programs can cause infinite recursion here, so we
17900 must catch that. */
17901 TYPENAME_IS_RESOLVING_P (type) = 1;
17902 result = resolve_typename_type (result, only_current_p);
17903 TYPENAME_IS_RESOLVING_P (type) = 0;
17904 }
17905
17906 /* Qualify the resulting type. */
17907 quals = cp_type_quals (type);
17908 if (quals)
17909 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
17910
17911 return result;
17912 }
17913
17914 /* EXPR is an expression which is not type-dependent. Return a proxy
17915 for EXPR that can be used to compute the types of larger
17916 expressions containing EXPR. */
17917
17918 tree
17919 build_non_dependent_expr (tree expr)
17920 {
17921 tree inner_expr;
17922
17923 /* Preserve null pointer constants so that the type of things like
17924 "p == 0" where "p" is a pointer can be determined. */
17925 if (null_ptr_cst_p (expr))
17926 return expr;
17927 /* Preserve OVERLOADs; the functions must be available to resolve
17928 types. */
17929 inner_expr = expr;
17930 if (TREE_CODE (inner_expr) == STMT_EXPR)
17931 inner_expr = stmt_expr_value_expr (inner_expr);
17932 if (TREE_CODE (inner_expr) == ADDR_EXPR)
17933 inner_expr = TREE_OPERAND (inner_expr, 0);
17934 if (TREE_CODE (inner_expr) == COMPONENT_REF)
17935 inner_expr = TREE_OPERAND (inner_expr, 1);
17936 if (is_overloaded_fn (inner_expr)
17937 || TREE_CODE (inner_expr) == OFFSET_REF)
17938 return expr;
17939 /* There is no need to return a proxy for a variable. */
17940 if (TREE_CODE (expr) == VAR_DECL)
17941 return expr;
17942 /* Preserve string constants; conversions from string constants to
17943 "char *" are allowed, even though normally a "const char *"
17944 cannot be used to initialize a "char *". */
17945 if (TREE_CODE (expr) == STRING_CST)
17946 return expr;
17947 /* Preserve arithmetic constants, as an optimization -- there is no
17948 reason to create a new node. */
17949 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
17950 return expr;
17951 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
17952 There is at least one place where we want to know that a
17953 particular expression is a throw-expression: when checking a ?:
17954 expression, there are special rules if the second or third
17955 argument is a throw-expression. */
17956 if (TREE_CODE (expr) == THROW_EXPR)
17957 return expr;
17958
17959 if (TREE_CODE (expr) == COND_EXPR)
17960 return build3 (COND_EXPR,
17961 TREE_TYPE (expr),
17962 TREE_OPERAND (expr, 0),
17963 (TREE_OPERAND (expr, 1)
17964 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
17965 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
17966 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
17967 if (TREE_CODE (expr) == COMPOUND_EXPR
17968 && !COMPOUND_EXPR_OVERLOADED (expr))
17969 return build2 (COMPOUND_EXPR,
17970 TREE_TYPE (expr),
17971 TREE_OPERAND (expr, 0),
17972 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
17973
17974 /* If the type is unknown, it can't really be non-dependent */
17975 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
17976
17977 /* Otherwise, build a NON_DEPENDENT_EXPR.
17978
17979 REFERENCE_TYPEs are not stripped for expressions in templates
17980 because doing so would play havoc with mangling. Consider, for
17981 example:
17982
17983 template <typename T> void f<T& g>() { g(); }
17984
17985 In the body of "f", the expression for "g" will have
17986 REFERENCE_TYPE, even though the standard says that it should
17987 not. The reason is that we must preserve the syntactic form of
17988 the expression so that mangling (say) "f<g>" inside the body of
17989 "f" works out correctly. Therefore, the REFERENCE_TYPE is
17990 stripped here. */
17991 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
17992 }
17993
17994 /* ARGS is a vector of expressions as arguments to a function call.
17995 Replace the arguments with equivalent non-dependent expressions.
17996 This modifies ARGS in place. */
17997
17998 void
17999 make_args_non_dependent (VEC(tree,gc) *args)
18000 {
18001 unsigned int ix;
18002 tree arg;
18003
18004 for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
18005 {
18006 tree newarg = build_non_dependent_expr (arg);
18007 if (newarg != arg)
18008 VEC_replace (tree, args, ix, newarg);
18009 }
18010 }
18011
18012 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18013 with a level one deeper than the actual template parms. */
18014
18015 tree
18016 make_auto (void)
18017 {
18018 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
18019 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
18020 TYPE_DECL, get_identifier ("auto"), au);
18021 TYPE_STUB_DECL (au) = TYPE_NAME (au);
18022 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
18023 (0, processing_template_decl + 1, processing_template_decl + 1,
18024 TYPE_NAME (au), NULL_TREE);
18025 TYPE_CANONICAL (au) = canonical_type_parameter (au);
18026 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
18027 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
18028
18029 return au;
18030 }
18031
18032 /* Given type ARG, return std::initializer_list<ARG>. */
18033
18034 static tree
18035 listify (tree arg)
18036 {
18037 tree std_init_list = namespace_binding
18038 (get_identifier ("initializer_list"), std_node);
18039 tree argvec;
18040 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
18041 {
18042 error ("deducing from brace-enclosed initializer list requires "
18043 "#include <initializer_list>");
18044 return error_mark_node;
18045 }
18046 argvec = make_tree_vec (1);
18047 TREE_VEC_ELT (argvec, 0) = arg;
18048 return lookup_template_class (std_init_list, argvec, NULL_TREE,
18049 NULL_TREE, 0, tf_warning_or_error);
18050 }
18051
18052 /* Replace auto in TYPE with std::initializer_list<auto>. */
18053
18054 static tree
18055 listify_autos (tree type, tree auto_node)
18056 {
18057 tree init_auto = listify (auto_node);
18058 tree argvec = make_tree_vec (1);
18059 TREE_VEC_ELT (argvec, 0) = init_auto;
18060 if (processing_template_decl)
18061 argvec = add_to_template_args (current_template_args (), argvec);
18062 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18063 }
18064
18065 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18066 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18067
18068 tree
18069 do_auto_deduction (tree type, tree init, tree auto_node)
18070 {
18071 tree parms, tparms, targs;
18072 tree args[1];
18073 int val;
18074
18075 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
18076 with either a new invented type template parameter U or, if the
18077 initializer is a braced-init-list (8.5.4), with
18078 std::initializer_list<U>. */
18079 if (BRACE_ENCLOSED_INITIALIZER_P (init))
18080 type = listify_autos (type, auto_node);
18081
18082 parms = build_tree_list (NULL_TREE, type);
18083 args[0] = init;
18084 tparms = make_tree_vec (1);
18085 targs = make_tree_vec (1);
18086 TREE_VEC_ELT (tparms, 0)
18087 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
18088 val = type_unification_real (tparms, targs, parms, args, 1, 0,
18089 DEDUCE_CALL, LOOKUP_NORMAL);
18090 if (val > 0)
18091 {
18092 error ("unable to deduce %qT from %qE", type, init);
18093 return error_mark_node;
18094 }
18095
18096 /* If the list of declarators contains more than one declarator, the type
18097 of each declared variable is determined as described above. If the
18098 type deduced for the template parameter U is not the same in each
18099 deduction, the program is ill-formed. */
18100 if (TREE_TYPE (auto_node)
18101 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
18102 {
18103 error ("inconsistent deduction for %qT: %qT and then %qT",
18104 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
18105 return error_mark_node;
18106 }
18107 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
18108
18109 if (processing_template_decl)
18110 targs = add_to_template_args (current_template_args (), targs);
18111 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
18112 }
18113
18114 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
18115 result. */
18116
18117 tree
18118 splice_late_return_type (tree type, tree late_return_type)
18119 {
18120 tree argvec;
18121
18122 if (late_return_type == NULL_TREE)
18123 return type;
18124 argvec = make_tree_vec (1);
18125 TREE_VEC_ELT (argvec, 0) = late_return_type;
18126 if (processing_template_decl)
18127 argvec = add_to_template_args (current_template_args (), argvec);
18128 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18129 }
18130
18131 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
18132
18133 bool
18134 is_auto (const_tree type)
18135 {
18136 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18137 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
18138 return true;
18139 else
18140 return false;
18141 }
18142
18143 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
18144 appear as a type-specifier for the declaration in question, we don't
18145 have to look through the whole type. */
18146
18147 tree
18148 type_uses_auto (tree type)
18149 {
18150 enum tree_code code;
18151 if (is_auto (type))
18152 return type;
18153
18154 code = TREE_CODE (type);
18155
18156 if (code == POINTER_TYPE || code == REFERENCE_TYPE
18157 || code == OFFSET_TYPE || code == FUNCTION_TYPE
18158 || code == METHOD_TYPE || code == ARRAY_TYPE)
18159 return type_uses_auto (TREE_TYPE (type));
18160
18161 if (TYPE_PTRMEMFUNC_P (type))
18162 return type_uses_auto (TREE_TYPE (TREE_TYPE
18163 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
18164
18165 return NULL_TREE;
18166 }
18167
18168 /* For a given template T, return the vector of typedefs referenced
18169 in T for which access check is needed at T instantiation time.
18170 T is either a FUNCTION_DECL or a RECORD_TYPE.
18171 Those typedefs were added to T by the function
18172 append_type_to_template_for_access_check. */
18173
18174 VEC(qualified_typedef_usage_t,gc)*
18175 get_types_needing_access_check (tree t)
18176 {
18177 tree ti;
18178 VEC(qualified_typedef_usage_t,gc) *result = NULL;
18179
18180 if (!t || t == error_mark_node)
18181 return NULL;
18182
18183 if (!(ti = get_template_info (t)))
18184 return NULL;
18185
18186 if (CLASS_TYPE_P (t)
18187 || TREE_CODE (t) == FUNCTION_DECL)
18188 {
18189 if (!TI_TEMPLATE (ti))
18190 return NULL;
18191
18192 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
18193 }
18194
18195 return result;
18196 }
18197
18198 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
18199 tied to T. That list of typedefs will be access checked at
18200 T instantiation time.
18201 T is either a FUNCTION_DECL or a RECORD_TYPE.
18202 TYPE_DECL is a TYPE_DECL node representing a typedef.
18203 SCOPE is the scope through which TYPE_DECL is accessed.
18204 LOCATION is the location of the usage point of TYPE_DECL.
18205
18206 This function is a subroutine of
18207 append_type_to_template_for_access_check. */
18208
18209 static void
18210 append_type_to_template_for_access_check_1 (tree t,
18211 tree type_decl,
18212 tree scope,
18213 location_t location)
18214 {
18215 qualified_typedef_usage_t typedef_usage;
18216 tree ti;
18217
18218 if (!t || t == error_mark_node)
18219 return;
18220
18221 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
18222 || CLASS_TYPE_P (t))
18223 && type_decl
18224 && TREE_CODE (type_decl) == TYPE_DECL
18225 && scope);
18226
18227 if (!(ti = get_template_info (t)))
18228 return;
18229
18230 gcc_assert (TI_TEMPLATE (ti));
18231
18232 typedef_usage.typedef_decl = type_decl;
18233 typedef_usage.context = scope;
18234 typedef_usage.locus = location;
18235
18236 VEC_safe_push (qualified_typedef_usage_t, gc,
18237 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
18238 &typedef_usage);
18239 }
18240
18241 /* Append TYPE_DECL to the template TEMPL.
18242 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
18243 At TEMPL instanciation time, TYPE_DECL will be checked to see
18244 if it can be accessed through SCOPE.
18245 LOCATION is the location of the usage point of TYPE_DECL.
18246
18247 e.g. consider the following code snippet:
18248
18249 class C
18250 {
18251 typedef int myint;
18252 };
18253
18254 template<class U> struct S
18255 {
18256 C::myint mi; // <-- usage point of the typedef C::myint
18257 };
18258
18259 S<char> s;
18260
18261 At S<char> instantiation time, we need to check the access of C::myint
18262 In other words, we need to check the access of the myint typedef through
18263 the C scope. For that purpose, this function will add the myint typedef
18264 and the scope C through which its being accessed to a list of typedefs
18265 tied to the template S. That list will be walked at template instantiation
18266 time and access check performed on each typedefs it contains.
18267 Note that this particular code snippet should yield an error because
18268 myint is private to C. */
18269
18270 void
18271 append_type_to_template_for_access_check (tree templ,
18272 tree type_decl,
18273 tree scope,
18274 location_t location)
18275 {
18276 qualified_typedef_usage_t *iter;
18277 int i;
18278
18279 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
18280
18281 /* Make sure we don't append the type to the template twice. */
18282 for (i = 0;
18283 VEC_iterate (qualified_typedef_usage_t,
18284 get_types_needing_access_check (templ),
18285 i, iter);
18286 ++i)
18287 if (iter->typedef_decl == type_decl && scope == iter->context)
18288 return;
18289
18290 append_type_to_template_for_access_check_1 (templ, type_decl,
18291 scope, location);
18292 }
18293
18294 /* Set up the hash tables for template instantiations. */
18295
18296 void
18297 init_template_processing (void)
18298 {
18299 decl_specializations = htab_create_ggc (37,
18300 hash_specialization,
18301 eq_specializations,
18302 ggc_free);
18303 type_specializations = htab_create_ggc (37,
18304 hash_specialization,
18305 eq_specializations,
18306 ggc_free);
18307 }
18308
18309 #include "gt-cp-pt.h"
This page took 0.884278 seconds and 5 git commands to generate.