]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Handle parameterized types (templates) for GNU C++. |
3febd123 | 2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
3fe82414 | 3 | 2001, 2002, 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc. |
8d08fdba | 4 | Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. |
fc378698 | 5 | Rewritten by Jason Merrill (jason@cygnus.com). |
8d08fdba | 6 | |
f5adbb8d | 7 | This file is part of GCC. |
8d08fdba | 8 | |
f5adbb8d | 9 | GCC is free software; you can redistribute it and/or modify |
8d08fdba | 10 | it under the terms of the GNU General Public License as published by |
e77f031d | 11 | the Free Software Foundation; either version 3, or (at your option) |
8d08fdba MS |
12 | any later version. |
13 | ||
f5adbb8d | 14 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
e77f031d NC |
20 | along with GCC; see the file COPYING3. If not see |
21 | <http://www.gnu.org/licenses/>. */ | |
8d08fdba MS |
22 | |
23 | /* Known bugs or deficiencies include: | |
e92cc029 | 24 | |
e92cc029 MS |
25 | all methods must be provided in header files; can't use a source |
26 | file that contains only the method templates and "just win". */ | |
8d08fdba MS |
27 | |
28 | #include "config.h" | |
8d052bc7 | 29 | #include "system.h" |
4977bab6 ZW |
30 | #include "coretypes.h" |
31 | #include "tm.h" | |
8d08fdba | 32 | #include "obstack.h" |
8d08fdba | 33 | #include "tree.h" |
0c58f841 | 34 | #include "pointer-set.h" |
8d08fdba | 35 | #include "flags.h" |
e58a9aa1 | 36 | #include "c-common.h" |
8d08fdba | 37 | #include "cp-tree.h" |
e58a9aa1 | 38 | #include "cp-objcp-common.h" |
25af8512 | 39 | #include "tree-inline.h" |
8d08fdba | 40 | #include "decl.h" |
e8abc66f | 41 | #include "output.h" |
49c249e1 | 42 | #include "except.h" |
54f92bfb | 43 | #include "toplev.h" |
3dcaad8b | 44 | #include "rtl.h" |
297a5329 | 45 | #include "timevar.h" |
325c3691 | 46 | #include "tree-iterator.h" |
53824026 | 47 | #include "vecprim.h" |
49c249e1 | 48 | |
050367a3 MM |
49 | /* The type of functions taking a tree, and some additional data, and |
50 | returning an int. */ | |
3a978d72 | 51 | typedef int (*tree_fn_t) (tree, void*); |
050367a3 | 52 | |
0aafb128 MM |
53 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose |
54 | instantiations have been deferred, either because their definitions | |
e2c3721c PB |
55 | were not yet available, or because we were putting off doing the work. */ |
56 | struct pending_template GTY (()) { | |
57 | struct pending_template *next; | |
58 | struct tinst_level *tinst; | |
59 | }; | |
60 | ||
61 | static GTY(()) struct pending_template *pending_templates; | |
62 | static GTY(()) struct pending_template *last_pending_template; | |
73aad9b9 | 63 | |
67ffc812 | 64 | int processing_template_parmlist; |
386b8a85 JM |
65 | static int template_header_count; |
66 | ||
e2500fed | 67 | static GTY(()) tree saved_trees; |
53824026 | 68 | static VEC(int,heap) *inline_parm_levels; |
75650646 | 69 | |
e2c3721c | 70 | static GTY(()) struct tinst_level *current_tinst_level; |
3ae18eaf | 71 | |
2b59fc25 KL |
72 | static GTY(()) tree saved_access_scope; |
73 | ||
0fe0caa6 RH |
74 | /* Live only within one (recursive) call to tsubst_expr. We use |
75 | this to pass the statement expression node from the STMT_EXPR | |
76 | to the EXPR_STMT that is its result. */ | |
77 | static tree cur_stmt_expr; | |
78 | ||
6dfbb909 MM |
79 | /* A map from local variable declarations in the body of the template |
80 | presently being instantiated to the corresponding instantiated | |
81 | local variables. */ | |
82 | static htab_t local_specializations; | |
83 | ||
410cf6e6 | 84 | /* Contains canonical template parameter types. The vector is indexed by |
06d40de8 DG |
85 | the TEMPLATE_TYPE_IDX of the template parameter. Each element is a |
86 | TREE_LIST, whose TREE_VALUEs contain the canonical template | |
87 | parameters of various types and levels. */ | |
88 | static GTY(()) VEC(tree,gc) *canonical_template_parms; | |
89 | ||
830bfa74 MM |
90 | #define UNIFY_ALLOW_NONE 0 |
91 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
92 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
93 | #define UNIFY_ALLOW_DERIVED 4 | |
161c12b0 | 94 | #define UNIFY_ALLOW_INTEGER 8 |
028d1f20 | 95 | #define UNIFY_ALLOW_OUTER_LEVEL 16 |
62e4a758 NS |
96 | #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 |
97 | #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 | |
830bfa74 | 98 | |
3a978d72 NN |
99 | static void push_access_scope (tree); |
100 | static void pop_access_scope (tree); | |
acde59b4 MM |
101 | static bool resolve_overloaded_unification (tree, tree, tree, tree, |
102 | unification_kind_t, int); | |
3a978d72 | 103 | static int try_one_overload (tree, tree, tree, tree, tree, |
f23fb7f5 | 104 | unification_kind_t, int, bool); |
3a978d72 NN |
105 | static int unify (tree, tree, tree, tree, int); |
106 | static void add_pending_template (tree); | |
aa9d8196 VR |
107 | static int push_tinst_level (tree); |
108 | static void pop_tinst_level (void); | |
e2c3721c | 109 | static tree reopen_tinst_level (struct tinst_level *); |
3a978d72 | 110 | static tree tsubst_initializer_list (tree, tree); |
3a978d72 | 111 | static tree get_class_bindings (tree, tree, tree); |
3db45ab5 | 112 | static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, |
e7e93965 | 113 | bool, bool); |
3a978d72 NN |
114 | static void tsubst_enum (tree, tree, tree); |
115 | static tree add_to_template_args (tree, tree); | |
116 | static tree add_outermost_template_args (tree, tree); | |
117 | static bool check_instantiated_args (tree, tree, tsubst_flags_t); | |
8af2fec4 RY |
118 | static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*, |
119 | tree); | |
3a978d72 | 120 | static int type_unification_real (tree, tree, tree, tree, |
30f86ec3 | 121 | int, unification_kind_t, int); |
3a978d72 | 122 | static void note_template_header (int); |
b6ab6892 | 123 | static tree convert_nontype_argument_function (tree, tree); |
3a978d72 NN |
124 | static tree convert_nontype_argument (tree, tree); |
125 | static tree convert_template_argument (tree, tree, tree, | |
126 | tsubst_flags_t, int, tree); | |
0c58f841 | 127 | static int for_each_template_parm (tree, tree_fn_t, void*, |
c095a4f8 | 128 | struct pointer_set_t*, bool); |
5d80a306 | 129 | static tree expand_template_argument_pack (tree); |
3a978d72 | 130 | static tree build_template_parm_index (int, int, int, tree, tree); |
ae95e46e | 131 | static bool inline_needs_template_parms (tree); |
3a978d72 | 132 | static void push_inline_template_parms_recursive (tree, int); |
3a978d72 | 133 | static tree retrieve_local_specialization (tree); |
3a978d72 | 134 | static void register_local_specialization (tree, tree); |
ef3b7b17 | 135 | static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t); |
3a978d72 NN |
136 | static int mark_template_parm (tree, void *); |
137 | static int template_parm_this_level_p (tree, void *); | |
138 | static tree tsubst_friend_function (tree, tree); | |
139 | static tree tsubst_friend_class (tree, tree); | |
140 | static int can_complete_type_without_circularity (tree); | |
a34d3336 | 141 | static tree get_bindings (tree, tree, tree, bool); |
3a978d72 NN |
142 | static int template_decl_level (tree); |
143 | static int check_cv_quals_for_unify (int, tree, tree); | |
5d80a306 DG |
144 | static void template_parm_level_and_index (tree, int*, int*); |
145 | static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool); | |
a91db711 NS |
146 | static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); |
147 | static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); | |
3a978d72 NN |
148 | static tree tsubst_template_parms (tree, tree, tsubst_flags_t); |
149 | static void regenerate_decl_from_template (tree, tree); | |
3a978d72 | 150 | static tree most_specialized_class (tree, tree); |
3a978d72 | 151 | static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); |
3a978d72 NN |
152 | static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree); |
153 | static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); | |
9b910171 | 154 | static bool check_specialization_scope (void); |
3a978d72 NN |
155 | static tree process_partial_specialization (tree); |
156 | static void set_current_access_from_decl (tree); | |
3a978d72 | 157 | static tree get_template_base (tree, tree, tree, tree); |
3a978d72 NN |
158 | static tree try_class_unification (tree, tree, tree, tree); |
159 | static int coerce_template_template_parms (tree, tree, tsubst_flags_t, | |
160 | tree, tree); | |
4cf36211 | 161 | static bool template_template_parm_bindings_ok_p (tree, tree); |
3a978d72 NN |
162 | static int template_args_equal (tree, tree); |
163 | static void tsubst_default_arguments (tree); | |
164 | static tree for_each_template_parm_r (tree *, int *, void *); | |
165 | static tree copy_default_args_to_explicit_spec_1 (tree, tree); | |
166 | static void copy_default_args_to_explicit_spec (tree); | |
167 | static int invalid_nontype_parm_type_p (tree, tsubst_flags_t); | |
a723baf1 | 168 | static int eq_local_specializations (const void *, const void *); |
5d80a306 | 169 | static bool dependent_template_arg_p (tree); |
06d40de8 | 170 | static bool any_template_arguments_need_structural_equality_p (tree); |
5552b43c | 171 | static bool dependent_type_p_r (tree); |
14d22dd6 | 172 | static tree tsubst (tree, tree, tsubst_flags_t, tree); |
015c2c66 | 173 | static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool); |
14d22dd6 | 174 | static tree tsubst_copy (tree, tree, tsubst_flags_t, tree); |
5d80a306 | 175 | static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree); |
36a117a5 | 176 | |
2b59fc25 KL |
177 | /* Make the current scope suitable for access checking when we are |
178 | processing T. T can be FUNCTION_DECL for instantiated function | |
2b907f5c KL |
179 | template, or VAR_DECL for static member variable (need by |
180 | instantiate_decl). */ | |
2b59fc25 | 181 | |
8ce33230 | 182 | static void |
2b907f5c | 183 | push_access_scope (tree t) |
2b59fc25 | 184 | { |
50bc768d NS |
185 | gcc_assert (TREE_CODE (t) == FUNCTION_DECL |
186 | || TREE_CODE (t) == VAR_DECL); | |
2b59fc25 | 187 | |
23ff7e2d KL |
188 | if (DECL_FRIEND_CONTEXT (t)) |
189 | push_nested_class (DECL_FRIEND_CONTEXT (t)); | |
190 | else if (DECL_CLASS_SCOPE_P (t)) | |
2b907f5c | 191 | push_nested_class (DECL_CONTEXT (t)); |
0f399e5f KL |
192 | else |
193 | push_to_top_level (); | |
c8094d83 | 194 | |
2b907f5c | 195 | if (TREE_CODE (t) == FUNCTION_DECL) |
0f399e5f KL |
196 | { |
197 | saved_access_scope = tree_cons | |
198 | (NULL_TREE, current_function_decl, saved_access_scope); | |
199 | current_function_decl = t; | |
200 | } | |
2b59fc25 KL |
201 | } |
202 | ||
2b59fc25 KL |
203 | /* Restore the scope set up by push_access_scope. T is the node we |
204 | are processing. */ | |
205 | ||
8ce33230 | 206 | static void |
3a978d72 | 207 | pop_access_scope (tree t) |
2b59fc25 | 208 | { |
2b907f5c | 209 | if (TREE_CODE (t) == FUNCTION_DECL) |
2b59fc25 KL |
210 | { |
211 | current_function_decl = TREE_VALUE (saved_access_scope); | |
212 | saved_access_scope = TREE_CHAIN (saved_access_scope); | |
213 | } | |
0f399e5f | 214 | |
23ff7e2d | 215 | if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t)) |
0f399e5f KL |
216 | pop_nested_class (); |
217 | else | |
218 | pop_from_top_level (); | |
2b59fc25 KL |
219 | } |
220 | ||
a723baf1 MM |
221 | /* Do any processing required when DECL (a member template |
222 | declaration) is finished. Returns the TEMPLATE_DECL corresponding | |
223 | to DECL, unless it is a specialization, in which case the DECL | |
224 | itself is returned. */ | |
e1467ff2 MM |
225 | |
226 | tree | |
3a978d72 | 227 | finish_member_template_decl (tree decl) |
e1467ff2 | 228 | { |
a723baf1 MM |
229 | if (decl == error_mark_node) |
230 | return error_mark_node; | |
231 | ||
50bc768d | 232 | gcc_assert (DECL_P (decl)); |
a723baf1 MM |
233 | |
234 | if (TREE_CODE (decl) == TYPE_DECL) | |
93cdc044 | 235 | { |
a723baf1 MM |
236 | tree type; |
237 | ||
238 | type = TREE_TYPE (decl); | |
c8094d83 | 239 | if (IS_AGGR_TYPE (type) |
a723baf1 MM |
240 | && CLASSTYPE_TEMPLATE_INFO (type) |
241 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
93cdc044 | 242 | { |
a723baf1 | 243 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
93cdc044 JM |
244 | check_member_template (tmpl); |
245 | return tmpl; | |
246 | } | |
8d019cef | 247 | return NULL_TREE; |
93cdc044 | 248 | } |
07c88314 | 249 | else if (TREE_CODE (decl) == FIELD_DECL) |
0f51ccfc | 250 | error ("data member %qD cannot be a member template", decl); |
a1da6cba | 251 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 252 | { |
a1da6cba MM |
253 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
254 | { | |
255 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
256 | return DECL_TI_TEMPLATE (decl); | |
257 | } | |
258 | else | |
259 | return decl; | |
c8094d83 | 260 | } |
a1da6cba | 261 | else |
0f51ccfc | 262 | error ("invalid member template declaration %qD", decl); |
e1467ff2 | 263 | |
a1da6cba | 264 | return error_mark_node; |
f84b4be9 | 265 | } |
e1467ff2 | 266 | |
91a77d68 JM |
267 | /* Return the template info node corresponding to T, whatever T is. */ |
268 | ||
269 | tree | |
270 | get_template_info (tree t) | |
271 | { | |
272 | tree tinfo = NULL_TREE; | |
273 | ||
274 | if (DECL_P (t) && DECL_LANG_SPECIFIC (t)) | |
275 | tinfo = DECL_TEMPLATE_INFO (t); | |
276 | ||
277 | if (!tinfo && TREE_CODE (t) == TYPE_DECL) | |
278 | t = TREE_TYPE (t); | |
279 | ||
280 | if (TAGGED_TYPE_P (t)) | |
281 | tinfo = TYPE_TEMPLATE_INFO (t); | |
282 | ||
283 | return tinfo; | |
284 | } | |
285 | ||
f84b4be9 | 286 | /* Returns the template nesting level of the indicated class TYPE. |
c8094d83 | 287 | |
f84b4be9 JM |
288 | For example, in: |
289 | template <class T> | |
290 | struct A | |
291 | { | |
292 | template <class U> | |
293 | struct B {}; | |
294 | }; | |
295 | ||
c8094d83 | 296 | A<T>::B<U> has depth two, while A<T> has depth one. |
39c01e4c | 297 | Both A<T>::B<int> and A<int>::B<U> have depth one, if |
260cd73f | 298 | they are instantiations, not specializations. |
39c01e4c MM |
299 | |
300 | This function is guaranteed to return 0 if passed NULL_TREE so | |
301 | that, for example, `template_class_depth (current_class_type)' is | |
302 | always safe. */ | |
f84b4be9 | 303 | |
260cd73f VR |
304 | int |
305 | template_class_depth (tree type) | |
f84b4be9 | 306 | { |
93cdc044 | 307 | int depth; |
f84b4be9 | 308 | |
c8094d83 | 309 | for (depth = 0; |
ed44da02 | 310 | type && TREE_CODE (type) != NAMESPACE_DECL; |
c8094d83 | 311 | type = (TREE_CODE (type) == FUNCTION_DECL) |
4f1c5b7d | 312 | ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) |
ed44da02 | 313 | { |
91a77d68 JM |
314 | tree tinfo = get_template_info (type); |
315 | ||
316 | if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) | |
317 | && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))) | |
318 | ++depth; | |
ed44da02 | 319 | } |
f84b4be9 JM |
320 | |
321 | return depth; | |
e1467ff2 | 322 | } |
98c1c668 | 323 | |
ae95e46e PC |
324 | /* Subroutine of maybe_begin_member_template_processing. |
325 | Returns true if processing DECL needs us to push template parms. */ | |
cae40af6 | 326 | |
ae95e46e | 327 | static bool |
3a978d72 | 328 | inline_needs_template_parms (tree decl) |
cae40af6 JM |
329 | { |
330 | if (! DECL_TEMPLATE_INFO (decl)) | |
ae95e46e | 331 | return false; |
f84b4be9 | 332 | |
36a117a5 | 333 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
334 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
335 | } | |
336 | ||
337 | /* Subroutine of maybe_begin_member_template_processing. | |
338 | Push the template parms in PARMS, starting from LEVELS steps into the | |
339 | chain, and ending at the beginning, since template parms are listed | |
340 | innermost first. */ | |
341 | ||
342 | static void | |
3a978d72 | 343 | push_inline_template_parms_recursive (tree parmlist, int levels) |
cae40af6 JM |
344 | { |
345 | tree parms = TREE_VALUE (parmlist); | |
346 | int i; | |
347 | ||
348 | if (levels > 1) | |
349 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 350 | |
98c1c668 | 351 | ++processing_template_decl; |
cae40af6 | 352 | current_template_parms |
4890c2f4 | 353 | = tree_cons (size_int (processing_template_decl), |
98c1c668 | 354 | parms, current_template_parms); |
cae40af6 JM |
355 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
356 | ||
ac20c67a | 357 | begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, |
0cbd7506 | 358 | NULL); |
c8094d83 | 359 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
98c1c668 | 360 | { |
0f67a82f | 361 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
42b304f1 | 362 | |
0f67a82f LM |
363 | if (parm == error_mark_node) |
364 | continue; | |
42b304f1 | 365 | |
50bc768d | 366 | gcc_assert (DECL_P (parm)); |
cae40af6 | 367 | |
98c1c668 JM |
368 | switch (TREE_CODE (parm)) |
369 | { | |
786b5245 | 370 | case TYPE_DECL: |
73b0fce8 | 371 | case TEMPLATE_DECL: |
98c1c668 JM |
372 | pushdecl (parm); |
373 | break; | |
786b5245 MM |
374 | |
375 | case PARM_DECL: | |
376 | { | |
fc03edb3 MM |
377 | /* Make a CONST_DECL as is done in process_template_parm. |
378 | It is ugly that we recreate this here; the original | |
379 | version built in process_template_parm is no longer | |
380 | available. */ | |
786b5245 MM |
381 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), |
382 | TREE_TYPE (parm)); | |
c727aa5e | 383 | DECL_ARTIFICIAL (decl) = 1; |
6de9cd9a DN |
384 | TREE_CONSTANT (decl) = 1; |
385 | TREE_INVARIANT (decl) = 1; | |
386 | TREE_READONLY (decl) = 1; | |
786b5245 | 387 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
cd9f6678 | 388 | SET_DECL_TEMPLATE_PARM_P (decl); |
786b5245 MM |
389 | pushdecl (decl); |
390 | } | |
cae40af6 | 391 | break; |
786b5245 | 392 | |
98c1c668 | 393 | default: |
315fb5db | 394 | gcc_unreachable (); |
98c1c668 JM |
395 | } |
396 | } | |
397 | } | |
398 | ||
cae40af6 JM |
399 | /* Restore the template parameter context for a member template or |
400 | a friend template defined in a class definition. */ | |
401 | ||
402 | void | |
3a978d72 | 403 | maybe_begin_member_template_processing (tree decl) |
cae40af6 JM |
404 | { |
405 | tree parms; | |
3dcaad8b | 406 | int levels = 0; |
cae40af6 | 407 | |
3dcaad8b MM |
408 | if (inline_needs_template_parms (decl)) |
409 | { | |
410 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
411 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
cae40af6 | 412 | |
3dcaad8b MM |
413 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
414 | { | |
415 | --levels; | |
416 | parms = TREE_CHAIN (parms); | |
417 | } | |
cae40af6 | 418 | |
3dcaad8b | 419 | push_inline_template_parms_recursive (parms, levels); |
cae40af6 JM |
420 | } |
421 | ||
3dcaad8b MM |
422 | /* Remember how many levels of template parameters we pushed so that |
423 | we can pop them later. */ | |
53824026 | 424 | VEC_safe_push (int, heap, inline_parm_levels, levels); |
cae40af6 JM |
425 | } |
426 | ||
1875c2b7 | 427 | /* Undo the effects of maybe_begin_member_template_processing. */ |
98c1c668 | 428 | |
c8094d83 | 429 | void |
3a978d72 | 430 | maybe_end_member_template_processing (void) |
98c1c668 | 431 | { |
3dcaad8b | 432 | int i; |
53824026 | 433 | int last; |
3dcaad8b | 434 | |
53824026 | 435 | if (VEC_length (int, inline_parm_levels) == 0) |
98c1c668 JM |
436 | return; |
437 | ||
53824026 KH |
438 | last = VEC_pop (int, inline_parm_levels); |
439 | for (i = 0; i < last; ++i) | |
cae40af6 JM |
440 | { |
441 | --processing_template_decl; | |
442 | current_template_parms = TREE_CHAIN (current_template_parms); | |
443 | poplevel (0, 0, 0); | |
444 | } | |
98c1c668 JM |
445 | } |
446 | ||
36a117a5 | 447 | /* Return a new template argument vector which contains all of ARGS, |
f9a7ae04 | 448 | but has as its innermost set of arguments the EXTRA_ARGS. */ |
e6f1275f JM |
449 | |
450 | static tree | |
3a978d72 | 451 | add_to_template_args (tree args, tree extra_args) |
e6f1275f | 452 | { |
36a117a5 MM |
453 | tree new_args; |
454 | int extra_depth; | |
455 | int i; | |
456 | int j; | |
e6f1275f | 457 | |
36a117a5 | 458 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
f31c0a32 | 459 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); |
e6f1275f | 460 | |
36a117a5 MM |
461 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
462 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 463 | |
36a117a5 MM |
464 | for (j = 1; j <= extra_depth; ++j, ++i) |
465 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
c8094d83 | 466 | |
e6f1275f JM |
467 | return new_args; |
468 | } | |
469 | ||
36a117a5 MM |
470 | /* Like add_to_template_args, but only the outermost ARGS are added to |
471 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
472 | (EXTRA_ARGS) levels are added. This function is used to combine | |
473 | the template arguments from a partial instantiation with the | |
474 | template arguments used to attain the full instantiation from the | |
475 | partial instantiation. */ | |
98c1c668 | 476 | |
4966381a | 477 | static tree |
3a978d72 | 478 | add_outermost_template_args (tree args, tree extra_args) |
98c1c668 JM |
479 | { |
480 | tree new_args; | |
481 | ||
e4a84209 MM |
482 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
483 | something very fishy is going on. */ | |
50bc768d | 484 | gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args)); |
e4a84209 MM |
485 | |
486 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
487 | them. */ | |
488 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
489 | return extra_args; | |
490 | ||
36a117a5 MM |
491 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
492 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
c8094d83 | 493 | |
36a117a5 | 494 | new_args = add_to_template_args (args, extra_args); |
98c1c668 | 495 | |
36a117a5 MM |
496 | /* Now, we restore ARGS to its full dimensions. */ |
497 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
498 | |
499 | return new_args; | |
500 | } | |
5566b478 | 501 | |
f9a7ae04 MM |
502 | /* Return the N levels of innermost template arguments from the ARGS. */ |
503 | ||
504 | tree | |
3a978d72 | 505 | get_innermost_template_args (tree args, int n) |
f9a7ae04 MM |
506 | { |
507 | tree new_args; | |
508 | int extra_levels; | |
509 | int i; | |
510 | ||
50bc768d | 511 | gcc_assert (n >= 0); |
f9a7ae04 MM |
512 | |
513 | /* If N is 1, just return the innermost set of template arguments. */ | |
514 | if (n == 1) | |
515 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); | |
c8094d83 | 516 | |
f9a7ae04 MM |
517 | /* If we're not removing anything, just return the arguments we were |
518 | given. */ | |
519 | extra_levels = TMPL_ARGS_DEPTH (args) - n; | |
50bc768d | 520 | gcc_assert (extra_levels >= 0); |
f9a7ae04 MM |
521 | if (extra_levels == 0) |
522 | return args; | |
523 | ||
524 | /* Make a new set of arguments, not containing the outer arguments. */ | |
525 | new_args = make_tree_vec (n); | |
526 | for (i = 1; i <= n; ++i) | |
c8094d83 | 527 | SET_TMPL_ARGS_LEVEL (new_args, i, |
f9a7ae04 MM |
528 | TMPL_ARGS_LEVEL (args, i + extra_levels)); |
529 | ||
530 | return new_args; | |
531 | } | |
532 | ||
dc28490d JM |
533 | /* The inverse of get_innermost_template_args: Return all but the innermost |
534 | EXTRA_LEVELS levels of template arguments from the ARGS. */ | |
535 | ||
536 | static tree | |
537 | strip_innermost_template_args (tree args, int extra_levels) | |
538 | { | |
539 | tree new_args; | |
540 | int n = TMPL_ARGS_DEPTH (args) - extra_levels; | |
541 | int i; | |
542 | ||
543 | gcc_assert (n >= 0); | |
544 | ||
545 | /* If N is 1, just return the outermost set of template arguments. */ | |
546 | if (n == 1) | |
547 | return TMPL_ARGS_LEVEL (args, 1); | |
548 | ||
549 | /* If we're not removing anything, just return the arguments we were | |
550 | given. */ | |
551 | gcc_assert (extra_levels >= 0); | |
552 | if (extra_levels == 0) | |
553 | return args; | |
554 | ||
555 | /* Make a new set of arguments, not containing the inner arguments. */ | |
556 | new_args = make_tree_vec (n); | |
557 | for (i = 1; i <= n; ++i) | |
558 | SET_TMPL_ARGS_LEVEL (new_args, i, | |
559 | TMPL_ARGS_LEVEL (args, i)); | |
560 | ||
561 | return new_args; | |
562 | } | |
563 | ||
5566b478 MS |
564 | /* We've got a template header coming up; push to a new level for storing |
565 | the parms. */ | |
8d08fdba | 566 | |
8d08fdba | 567 | void |
3a978d72 | 568 | begin_template_parm_list (void) |
8d08fdba | 569 | { |
6757edfe MM |
570 | /* We use a non-tag-transparent scope here, which causes pushtag to |
571 | put tags in this scope, rather than in the enclosing class or | |
572 | namespace scope. This is the right thing, since we want | |
573 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
574 | global template class, push_template_decl handles putting the | |
575 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
576 | e.g.: | |
577 | ||
578 | template <class T> struct S1 { | |
0cbd7506 | 579 | template <class T> struct S2 {}; |
6757edfe MM |
580 | }; |
581 | ||
582 | pushtag contains special code to call pushdecl_with_scope on the | |
583 | TEMPLATE_DECL for S2. */ | |
ac20c67a | 584 | begin_scope (sk_template_parms, NULL); |
5156628f | 585 | ++processing_template_decl; |
67ffc812 | 586 | ++processing_template_parmlist; |
386b8a85 JM |
587 | note_template_header (0); |
588 | } | |
589 | ||
6c30752f | 590 | /* This routine is called when a specialization is declared. If it is |
9b910171 LM |
591 | invalid to declare a specialization here, an error is reported and |
592 | false is returned, otherwise this routine will return true. */ | |
6c30752f | 593 | |
9b910171 | 594 | static bool |
3a978d72 | 595 | check_specialization_scope (void) |
6c30752f MM |
596 | { |
597 | tree scope = current_scope (); | |
3ddfb0e6 | 598 | |
c8094d83 MS |
599 | /* [temp.expl.spec] |
600 | ||
6c30752f MM |
601 | An explicit specialization shall be declared in the namespace of |
602 | which the template is a member, or, for member templates, in the | |
603 | namespace of which the enclosing class or enclosing class | |
604 | template is a member. An explicit specialization of a member | |
605 | function, member class or static data member of a class template | |
606 | shall be declared in the namespace of which the class template | |
607 | is a member. */ | |
608 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
9b910171 LM |
609 | { |
610 | error ("explicit specialization in non-namespace scope %qD", scope); | |
611 | return false; | |
612 | } | |
3ddfb0e6 | 613 | |
c8094d83 | 614 | /* [temp.expl.spec] |
6c30752f MM |
615 | |
616 | In an explicit specialization declaration for a member of a class | |
617 | template or a member template that appears in namespace scope, | |
618 | the member template and some of its enclosing class templates may | |
619 | remain unspecialized, except that the declaration shall not | |
620 | explicitly specialize a class member template if its enclosing | |
621 | class templates are not explicitly specialized as well. */ | |
c8094d83 | 622 | if (current_template_parms) |
9b910171 LM |
623 | { |
624 | error ("enclosing class templates are not explicitly specialized"); | |
625 | return false; | |
626 | } | |
627 | ||
628 | return true; | |
6c30752f MM |
629 | } |
630 | ||
c6002625 | 631 | /* We've just seen template <>. */ |
386b8a85 | 632 | |
9b910171 | 633 | bool |
3a978d72 | 634 | begin_specialization (void) |
386b8a85 | 635 | { |
ac20c67a | 636 | begin_scope (sk_template_spec, NULL); |
386b8a85 | 637 | note_template_header (1); |
9b910171 | 638 | return check_specialization_scope (); |
386b8a85 JM |
639 | } |
640 | ||
dc957d14 | 641 | /* Called at then end of processing a declaration preceded by |
386b8a85 JM |
642 | template<>. */ |
643 | ||
c8094d83 | 644 | void |
3a978d72 | 645 | end_specialization (void) |
386b8a85 | 646 | { |
74b846e0 | 647 | finish_scope (); |
386b8a85 JM |
648 | reset_specialization (); |
649 | } | |
650 | ||
386b8a85 | 651 | /* Any template <>'s that we have seen thus far are not referring to a |
c6002625 | 652 | function specialization. */ |
386b8a85 JM |
653 | |
654 | void | |
3a978d72 | 655 | reset_specialization (void) |
386b8a85 JM |
656 | { |
657 | processing_specialization = 0; | |
658 | template_header_count = 0; | |
659 | } | |
660 | ||
838dfd8a | 661 | /* We've just seen a template header. If SPECIALIZATION is nonzero, |
386b8a85 JM |
662 | it was of the form template <>. */ |
663 | ||
c8094d83 | 664 | static void |
3a978d72 | 665 | note_template_header (int specialization) |
386b8a85 JM |
666 | { |
667 | processing_specialization = specialization; | |
668 | template_header_count++; | |
669 | } | |
670 | ||
75650646 | 671 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 672 | |
75650646 | 673 | void |
3a978d72 | 674 | begin_explicit_instantiation (void) |
386b8a85 | 675 | { |
50bc768d | 676 | gcc_assert (!processing_explicit_instantiation); |
a723baf1 | 677 | processing_explicit_instantiation = true; |
75650646 | 678 | } |
386b8a85 | 679 | |
386b8a85 | 680 | |
75650646 | 681 | void |
3a978d72 | 682 | end_explicit_instantiation (void) |
75650646 | 683 | { |
50bc768d | 684 | gcc_assert (processing_explicit_instantiation); |
a723baf1 | 685 | processing_explicit_instantiation = false; |
75650646 | 686 | } |
386b8a85 | 687 | |
13a44ee0 | 688 | /* An explicit specialization or partial specialization TMPL is being |
b1cc95ce MM |
689 | declared. Check that the namespace in which the specialization is |
690 | occurring is permissible. Returns false iff it is invalid to | |
691 | specialize TMPL in the current namespace. */ | |
c8094d83 | 692 | |
b1cc95ce MM |
693 | static bool |
694 | check_specialization_namespace (tree tmpl) | |
695 | { | |
696 | tree tpl_ns = decl_namespace_context (tmpl); | |
697 | ||
698 | /* [tmpl.expl.spec] | |
c8094d83 | 699 | |
b1cc95ce MM |
700 | An explicit specialization shall be declared in the namespace of |
701 | which the template is a member, or, for member templates, in the | |
702 | namespace of which the enclosing class or enclosing class | |
703 | template is a member. An explicit specialization of a member | |
704 | function, member class or static data member of a class template | |
705 | shall be declared in the namespace of which the class template is | |
706 | a member. */ | |
707 | if (is_associated_namespace (current_namespace, tpl_ns)) | |
708 | /* Same or super-using namespace. */ | |
709 | return true; | |
710 | else | |
711 | { | |
0f51ccfc | 712 | pedwarn ("specialization of %qD in different namespace", tmpl); |
dee15844 | 713 | pedwarn (" from definition of %q+#D", tmpl); |
b1cc95ce MM |
714 | return false; |
715 | } | |
716 | } | |
717 | ||
9c12301f MM |
718 | /* SPEC is an explicit instantiation. Check that it is valid to |
719 | perform this explicit instantiation in the current namespace. */ | |
720 | ||
721 | static void | |
722 | check_explicit_instantiation_namespace (tree spec) | |
723 | { | |
724 | tree ns; | |
725 | ||
726 | /* DR 275: An explicit instantiation shall appear in an enclosing | |
3db45ab5 | 727 | namespace of its template. */ |
9c12301f MM |
728 | ns = decl_namespace_context (spec); |
729 | if (!is_ancestor (current_namespace, ns)) | |
730 | pedwarn ("explicit instantiation of %qD in namespace %qD " | |
312ea236 | 731 | "(which does not enclose namespace %qD)", |
9c12301f MM |
732 | spec, current_namespace, ns); |
733 | } | |
734 | ||
36a117a5 MM |
735 | /* The TYPE is being declared. If it is a template type, that means it |
736 | is a partial specialization. Do appropriate error-checking. */ | |
737 | ||
9ce387d9 | 738 | tree |
3a978d72 | 739 | maybe_process_partial_specialization (tree type) |
36a117a5 | 740 | { |
c9043d03 VR |
741 | tree context; |
742 | ||
743 | if (type == error_mark_node) | |
9ce387d9 VR |
744 | return error_mark_node; |
745 | ||
746 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
747 | { | |
748 | error ("name of class shadows template template parameter %qD", | |
749 | TYPE_NAME (type)); | |
750 | return error_mark_node; | |
751 | } | |
c9043d03 VR |
752 | |
753 | context = TYPE_CONTEXT (type); | |
6e049fcd | 754 | |
a723baf1 | 755 | if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) |
36a117a5 | 756 | { |
6e049fcd KL |
757 | /* This is for ordinary explicit specialization and partial |
758 | specialization of a template class such as: | |
759 | ||
760 | template <> class C<int>; | |
761 | ||
762 | or: | |
763 | ||
764 | template <class T> class C<T*>; | |
765 | ||
766 | Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ | |
767 | ||
36a117a5 | 768 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) |
d0f062fb | 769 | && !COMPLETE_TYPE_P (type)) |
36a117a5 | 770 | { |
b1cc95ce | 771 | check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)); |
370af2d5 | 772 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
36a117a5 MM |
773 | if (processing_template_decl) |
774 | push_template_decl (TYPE_MAIN_DECL (type)); | |
775 | } | |
776 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
0f51ccfc | 777 | error ("specialization of %qT after instantiation", type); |
36a117a5 | 778 | } |
6e049fcd KL |
779 | else if (CLASS_TYPE_P (type) |
780 | && !CLASSTYPE_USE_TEMPLATE (type) | |
781 | && CLASSTYPE_TEMPLATE_INFO (type) | |
782 | && context && CLASS_TYPE_P (context) | |
783 | && CLASSTYPE_TEMPLATE_INFO (context)) | |
784 | { | |
785 | /* This is for an explicit specialization of member class | |
786 | template according to [temp.expl.spec/18]: | |
787 | ||
788 | template <> template <class U> class C<int>::D; | |
789 | ||
790 | The context `C<int>' must be an implicit instantiation. | |
791 | Otherwise this is just a member class template declared | |
792 | earlier like: | |
793 | ||
794 | template <> class C<int> { template <class U> class D; }; | |
795 | template <> template <class U> class C<int>::D; | |
796 | ||
797 | In the first case, `C<int>::D' is a specialization of `C<T>::D' | |
798 | while in the second case, `C<int>::D' is a primary template | |
799 | and `C<T>::D' may not exist. */ | |
800 | ||
801 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) | |
802 | && !COMPLETE_TYPE_P (type)) | |
803 | { | |
804 | tree t; | |
805 | ||
806 | if (current_namespace | |
807 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
808 | { | |
0f51ccfc | 809 | pedwarn ("specializing %q#T in different namespace", type); |
dee15844 JM |
810 | pedwarn (" from definition of %q+#D", |
811 | CLASSTYPE_TI_TEMPLATE (type)); | |
6e049fcd KL |
812 | } |
813 | ||
814 | /* Check for invalid specialization after instantiation: | |
815 | ||
816 | template <> template <> class C<int>::D<int>; | |
817 | template <> template <class U> class C<int>::D; */ | |
818 | ||
819 | for (t = DECL_TEMPLATE_INSTANTIATIONS | |
820 | (most_general_template (CLASSTYPE_TI_TEMPLATE (type))); | |
821 | t; t = TREE_CHAIN (t)) | |
822 | if (TREE_VALUE (t) != type | |
823 | && TYPE_CONTEXT (TREE_VALUE (t)) == context) | |
0f51ccfc | 824 | error ("specialization %qT after instantiation %qT", |
6e049fcd KL |
825 | type, TREE_VALUE (t)); |
826 | ||
827 | /* Mark TYPE as a specialization. And as a result, we only | |
828 | have one level of template argument for the innermost | |
829 | class template. */ | |
830 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); | |
831 | CLASSTYPE_TI_ARGS (type) | |
832 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); | |
833 | } | |
834 | } | |
20496fa2 | 835 | else if (processing_specialization) |
9ce387d9 VR |
836 | { |
837 | error ("explicit specialization of non-template %qT", type); | |
838 | return error_mark_node; | |
839 | } | |
840 | ||
841 | return type; | |
36a117a5 MM |
842 | } |
843 | ||
ee81147e | 844 | /* Returns nonzero if we can optimize the retrieval of specializations |
c7222c02 MM |
845 | for TMPL, a TEMPLATE_DECL. In particular, for such a template, we |
846 | do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */ | |
847 | ||
c8094d83 | 848 | static inline bool |
c7222c02 MM |
849 | optimize_specialization_lookup_p (tree tmpl) |
850 | { | |
851 | return (DECL_FUNCTION_TEMPLATE_P (tmpl) | |
852 | && DECL_CLASS_SCOPE_P (tmpl) | |
853 | /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template | |
854 | parameter. */ | |
855 | && CLASS_TYPE_P (DECL_CONTEXT (tmpl)) | |
1ca939e5 MM |
856 | /* The optimized lookup depends on the fact that the |
857 | template arguments for the member function template apply | |
858 | purely to the containing class, which is not true if the | |
859 | containing class is an explicit or partial | |
860 | specialization. */ | |
861 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl)) | |
c7222c02 MM |
862 | && !DECL_MEMBER_TEMPLATE_P (tmpl) |
863 | && !DECL_CONV_FN_P (tmpl) | |
864 | /* It is possible to have a template that is not a member | |
865 | template and is not a member of a template class: | |
c8094d83 MS |
866 | |
867 | template <typename T> | |
c7222c02 | 868 | struct S { friend A::f(); }; |
c8094d83 | 869 | |
c7222c02 MM |
870 | Here, the friend function is a template, but the context does |
871 | not have template information. The optimized lookup relies | |
872 | on having ARGS be the template arguments for both the class | |
873 | and the function template. */ | |
874 | && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl))); | |
875 | } | |
876 | ||
75650646 MM |
877 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
878 | specialization is either an instantiation or an explicit | |
879 | specialization) of TMPL for the given template ARGS. If there is | |
880 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
881 | arguments, or a vector of vectors of arguments, in the case of | |
c8094d83 | 882 | templates with more than one level of parameters. |
c7222c02 MM |
883 | |
884 | If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true, | |
885 | then we search for a partial specialization matching ARGS. This | |
886 | parameter is ignored if TMPL is not a class template. */ | |
c8094d83 | 887 | |
75650646 | 888 | static tree |
c8094d83 | 889 | retrieve_specialization (tree tmpl, tree args, |
c7222c02 | 890 | bool class_specializations_p) |
75650646 | 891 | { |
2d8ba2c7 LM |
892 | if (args == error_mark_node) |
893 | return NULL_TREE; | |
894 | ||
50bc768d | 895 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 896 | |
36a117a5 MM |
897 | /* There should be as many levels of arguments as there are |
898 | levels of parameters. */ | |
c8094d83 | 899 | gcc_assert (TMPL_ARGS_DEPTH (args) |
50bc768d | 900 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); |
c8094d83 | 901 | |
c7222c02 MM |
902 | if (optimize_specialization_lookup_p (tmpl)) |
903 | { | |
904 | tree class_template; | |
905 | tree class_specialization; | |
d4e6fecb | 906 | VEC(tree,gc) *methods; |
c7222c02 MM |
907 | tree fns; |
908 | int idx; | |
909 | ||
910 | /* The template arguments actually apply to the containing | |
911 | class. Find the class specialization with those | |
912 | arguments. */ | |
913 | class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl)); | |
c8094d83 | 914 | class_specialization |
c7222c02 MM |
915 | = retrieve_specialization (class_template, args, |
916 | /*class_specializations_p=*/false); | |
917 | if (!class_specialization) | |
918 | return NULL_TREE; | |
919 | /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC | |
920 | for the specialization. */ | |
921 | idx = class_method_index_for_fn (class_specialization, tmpl); | |
922 | if (idx == -1) | |
923 | return NULL_TREE; | |
924 | /* Iterate through the methods with the indicated name, looking | |
925 | for the one that has an instance of TMPL. */ | |
926 | methods = CLASSTYPE_METHOD_VEC (class_specialization); | |
927 | for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns)) | |
928 | { | |
929 | tree fn = OVL_CURRENT (fns); | |
930 | if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl) | |
931 | return fn; | |
932 | } | |
933 | return NULL_TREE; | |
934 | } | |
935 | else | |
936 | { | |
937 | tree *sp; | |
938 | tree *head; | |
939 | ||
940 | /* Class templates store their instantiations on the | |
941 | DECL_TEMPLATE_INSTANTIATIONS list; other templates use the | |
942 | DECL_TEMPLATE_SPECIALIZATIONS list. */ | |
943 | if (!class_specializations_p | |
56d0c6e3 JM |
944 | && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL |
945 | && TAGGED_TYPE_P (TREE_TYPE (tmpl))) | |
c7222c02 MM |
946 | sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl); |
947 | else | |
948 | sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
949 | head = sp; | |
950 | /* Iterate through the list until we find a matching template. */ | |
951 | while (*sp != NULL_TREE) | |
952 | { | |
953 | tree spec = *sp; | |
c8094d83 | 954 | |
c7222c02 MM |
955 | if (comp_template_args (TREE_PURPOSE (spec), args)) |
956 | { | |
957 | /* Use the move-to-front heuristic to speed up future | |
c8094d83 | 958 | searches. */ |
c7222c02 MM |
959 | if (spec != *head) |
960 | { | |
961 | *sp = TREE_CHAIN (*sp); | |
962 | TREE_CHAIN (spec) = *head; | |
963 | *head = spec; | |
964 | } | |
965 | return TREE_VALUE (spec); | |
966 | } | |
967 | sp = &TREE_CHAIN (spec); | |
968 | } | |
969 | } | |
75650646 MM |
970 | |
971 | return NULL_TREE; | |
386b8a85 JM |
972 | } |
973 | ||
dc957d14 | 974 | /* Like retrieve_specialization, but for local declarations. */ |
9188c363 MM |
975 | |
976 | static tree | |
3a978d72 | 977 | retrieve_local_specialization (tree tmpl) |
9188c363 | 978 | { |
29b0d1fd JM |
979 | tree spec; |
980 | ||
981 | if (local_specializations == NULL) | |
982 | return NULL_TREE; | |
983 | ||
984 | spec = (tree) htab_find_with_hash (local_specializations, tmpl, | |
985 | htab_hash_pointer (tmpl)); | |
a723baf1 | 986 | return spec ? TREE_PURPOSE (spec) : NULL_TREE; |
9188c363 MM |
987 | } |
988 | ||
838dfd8a | 989 | /* Returns nonzero iff DECL is a specialization of TMPL. */ |
6757edfe MM |
990 | |
991 | int | |
3a978d72 | 992 | is_specialization_of (tree decl, tree tmpl) |
6757edfe MM |
993 | { |
994 | tree t; | |
995 | ||
996 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
997 | { | |
c8094d83 | 998 | for (t = decl; |
6757edfe MM |
999 | t != NULL_TREE; |
1000 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
1001 | if (t == tmpl) | |
1002 | return 1; | |
1003 | } | |
c8094d83 | 1004 | else |
6757edfe | 1005 | { |
50bc768d | 1006 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); |
6757edfe MM |
1007 | |
1008 | for (t = TREE_TYPE (decl); | |
1009 | t != NULL_TREE; | |
1010 | t = CLASSTYPE_USE_TEMPLATE (t) | |
1011 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
9edc3913 | 1012 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) |
6757edfe | 1013 | return 1; |
c8094d83 | 1014 | } |
386b8a85 | 1015 | |
6757edfe MM |
1016 | return 0; |
1017 | } | |
75650646 | 1018 | |
d43f603d KL |
1019 | /* Returns nonzero iff DECL is a specialization of friend declaration |
1020 | FRIEND according to [temp.friend]. */ | |
1021 | ||
1022 | bool | |
1023 | is_specialization_of_friend (tree decl, tree friend) | |
1024 | { | |
1025 | bool need_template = true; | |
1026 | int template_depth; | |
1027 | ||
b939a023 KL |
1028 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
1029 | || TREE_CODE (decl) == TYPE_DECL); | |
d43f603d KL |
1030 | |
1031 | /* For [temp.friend/6] when FRIEND is an ordinary member function | |
1032 | of a template class, we want to check if DECL is a specialization | |
1033 | if this. */ | |
1034 | if (TREE_CODE (friend) == FUNCTION_DECL | |
1035 | && DECL_TEMPLATE_INFO (friend) | |
1036 | && !DECL_USE_TEMPLATE (friend)) | |
1037 | { | |
b939a023 | 1038 | /* We want a TEMPLATE_DECL for `is_specialization_of'. */ |
d43f603d KL |
1039 | friend = DECL_TI_TEMPLATE (friend); |
1040 | need_template = false; | |
1041 | } | |
b939a023 KL |
1042 | else if (TREE_CODE (friend) == TEMPLATE_DECL |
1043 | && !PRIMARY_TEMPLATE_P (friend)) | |
1044 | need_template = false; | |
d43f603d KL |
1045 | |
1046 | /* There is nothing to do if this is not a template friend. */ | |
1047 | if (TREE_CODE (friend) != TEMPLATE_DECL) | |
b939a023 | 1048 | return false; |
d43f603d KL |
1049 | |
1050 | if (is_specialization_of (decl, friend)) | |
b939a023 | 1051 | return true; |
d43f603d KL |
1052 | |
1053 | /* [temp.friend/6] | |
1054 | A member of a class template may be declared to be a friend of a | |
1055 | non-template class. In this case, the corresponding member of | |
1056 | every specialization of the class template is a friend of the | |
1057 | class granting friendship. | |
c8094d83 | 1058 | |
d43f603d KL |
1059 | For example, given a template friend declaration |
1060 | ||
1061 | template <class T> friend void A<T>::f(); | |
1062 | ||
1063 | the member function below is considered a friend | |
1064 | ||
1065 | template <> struct A<int> { | |
1066 | void f(); | |
1067 | }; | |
1068 | ||
1069 | For this type of template friend, TEMPLATE_DEPTH below will be | |
5c425df5 | 1070 | nonzero. To determine if DECL is a friend of FRIEND, we first |
d43f603d KL |
1071 | check if the enclosing class is a specialization of another. */ |
1072 | ||
1073 | template_depth = template_class_depth (DECL_CONTEXT (friend)); | |
1074 | if (template_depth | |
1075 | && DECL_CLASS_SCOPE_P (decl) | |
c8094d83 | 1076 | && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), |
d43f603d KL |
1077 | CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend)))) |
1078 | { | |
1079 | /* Next, we check the members themselves. In order to handle | |
b939a023 | 1080 | a few tricky cases, such as when FRIEND's are |
d43f603d KL |
1081 | |
1082 | template <class T> friend void A<T>::g(T t); | |
1083 | template <class T> template <T t> friend void A<T>::h(); | |
1084 | ||
b939a023 KL |
1085 | and DECL's are |
1086 | ||
1087 | void A<int>::g(int); | |
1088 | template <int> void A<int>::h(); | |
1089 | ||
1090 | we need to figure out ARGS, the template arguments from | |
1091 | the context of DECL. This is required for template substitution | |
1092 | of `T' in the function parameter of `g' and template parameter | |
1093 | of `h' in the above examples. Here ARGS corresponds to `int'. */ | |
d43f603d KL |
1094 | |
1095 | tree context = DECL_CONTEXT (decl); | |
1096 | tree args = NULL_TREE; | |
1097 | int current_depth = 0; | |
b939a023 | 1098 | |
d43f603d KL |
1099 | while (current_depth < template_depth) |
1100 | { | |
1101 | if (CLASSTYPE_TEMPLATE_INFO (context)) | |
1102 | { | |
1103 | if (current_depth == 0) | |
1104 | args = TYPE_TI_ARGS (context); | |
1105 | else | |
1106 | args = add_to_template_args (TYPE_TI_ARGS (context), args); | |
1107 | current_depth++; | |
1108 | } | |
1109 | context = TYPE_CONTEXT (context); | |
1110 | } | |
1111 | ||
1112 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
1113 | { | |
1114 | bool is_template; | |
1115 | tree friend_type; | |
1116 | tree decl_type; | |
1117 | tree friend_args_type; | |
1118 | tree decl_args_type; | |
1119 | ||
1120 | /* Make sure that both DECL and FRIEND are templates or | |
1121 | non-templates. */ | |
1122 | is_template = DECL_TEMPLATE_INFO (decl) | |
1123 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); | |
1124 | if (need_template ^ is_template) | |
b939a023 | 1125 | return false; |
d43f603d KL |
1126 | else if (is_template) |
1127 | { | |
da1d7781 | 1128 | /* If both are templates, check template parameter list. */ |
d43f603d KL |
1129 | tree friend_parms |
1130 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1131 | args, tf_none); | |
1132 | if (!comp_template_parms | |
1133 | (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), | |
1134 | friend_parms)) | |
b939a023 | 1135 | return false; |
d43f603d KL |
1136 | |
1137 | decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); | |
1138 | } | |
1139 | else | |
1140 | decl_type = TREE_TYPE (decl); | |
1141 | ||
1142 | friend_type = tsubst_function_type (TREE_TYPE (friend), args, | |
1143 | tf_none, NULL_TREE); | |
1144 | if (friend_type == error_mark_node) | |
b939a023 | 1145 | return false; |
d43f603d KL |
1146 | |
1147 | /* Check if return types match. */ | |
1148 | if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) | |
b939a023 | 1149 | return false; |
d43f603d KL |
1150 | |
1151 | /* Check if function parameter types match, ignoring the | |
1152 | `this' parameter. */ | |
1153 | friend_args_type = TYPE_ARG_TYPES (friend_type); | |
1154 | decl_args_type = TYPE_ARG_TYPES (decl_type); | |
1155 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend)) | |
1156 | friend_args_type = TREE_CHAIN (friend_args_type); | |
1157 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1158 | decl_args_type = TREE_CHAIN (decl_args_type); | |
b939a023 KL |
1159 | |
1160 | return compparms (decl_args_type, friend_args_type); | |
1161 | } | |
1162 | else | |
1163 | { | |
1164 | /* DECL is a TYPE_DECL */ | |
1165 | bool is_template; | |
1166 | tree decl_type = TREE_TYPE (decl); | |
1167 | ||
1168 | /* Make sure that both DECL and FRIEND are templates or | |
1169 | non-templates. */ | |
1170 | is_template | |
1171 | = CLASSTYPE_TEMPLATE_INFO (decl_type) | |
1172 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type)); | |
1173 | ||
1174 | if (need_template ^ is_template) | |
1175 | return false; | |
1176 | else if (is_template) | |
1177 | { | |
1178 | tree friend_parms; | |
1179 | /* If both are templates, check the name of the two | |
1180 | TEMPLATE_DECL's first because is_friend didn't. */ | |
1181 | if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type)) | |
1182 | != DECL_NAME (friend)) | |
1183 | return false; | |
1184 | ||
1185 | /* Now check template parameter list. */ | |
1186 | friend_parms | |
1187 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1188 | args, tf_none); | |
1189 | return comp_template_parms | |
1190 | (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)), | |
1191 | friend_parms); | |
1192 | } | |
1193 | else | |
1194 | return (DECL_NAME (decl) | |
1195 | == DECL_NAME (friend)); | |
d43f603d KL |
1196 | } |
1197 | } | |
b939a023 | 1198 | return false; |
d43f603d KL |
1199 | } |
1200 | ||
75650646 | 1201 | /* Register the specialization SPEC as a specialization of TMPL with |
d63d5d0c ILT |
1202 | the indicated ARGS. IS_FRIEND indicates whether the specialization |
1203 | is actually just a friend declaration. Returns SPEC, or an | |
1204 | equivalent prior declaration, if available. */ | |
75650646 | 1205 | |
36a117a5 | 1206 | static tree |
d63d5d0c | 1207 | register_specialization (tree spec, tree tmpl, tree args, bool is_friend) |
75650646 | 1208 | { |
c7222c02 | 1209 | tree fn; |
75650646 | 1210 | |
50bc768d | 1211 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 1212 | |
c8094d83 | 1213 | if (TREE_CODE (spec) == FUNCTION_DECL |
36a117a5 MM |
1214 | && uses_template_parms (DECL_TI_ARGS (spec))) |
1215 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
1216 | register it; we want the corresponding TEMPLATE_DECL instead. | |
1217 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
1218 | the more obvious `uses_template_parms (spec)' to avoid problems | |
1219 | with default function arguments. In particular, given | |
1220 | something like this: | |
1221 | ||
0cbd7506 | 1222 | template <class T> void f(T t1, T t = T()) |
36a117a5 MM |
1223 | |
1224 | the default argument expression is not substituted for in an | |
1225 | instantiation unless and until it is actually needed. */ | |
1226 | return spec; | |
28ba38e3 | 1227 | |
c8094d83 | 1228 | fn = retrieve_specialization (tmpl, args, |
c7222c02 MM |
1229 | /*class_specializations_p=*/false); |
1230 | /* We can sometimes try to re-register a specialization that we've | |
1231 | already got. In particular, regenerate_decl_from_template calls | |
1232 | duplicate_decls which will update the specialization list. But, | |
1233 | we'll still get called again here anyhow. It's more convenient | |
1234 | to simply allow this than to try to prevent it. */ | |
1235 | if (fn == spec) | |
1236 | return spec; | |
1237 | else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
22e9174f | 1238 | { |
c7222c02 | 1239 | if (DECL_TEMPLATE_INSTANTIATION (fn)) |
22e9174f | 1240 | { |
c8094d83 | 1241 | if (TREE_USED (fn) |
c7222c02 | 1242 | || DECL_EXPLICIT_INSTANTIATION (fn)) |
22e9174f | 1243 | { |
c7222c02 MM |
1244 | error ("specialization of %qD after instantiation", |
1245 | fn); | |
99f4234a | 1246 | return error_mark_node; |
22e9174f | 1247 | } |
c7222c02 | 1248 | else |
b1cc95ce | 1249 | { |
a39041fa | 1250 | tree clone; |
c7222c02 MM |
1251 | /* This situation should occur only if the first |
1252 | specialization is an implicit instantiation, the | |
1253 | second is an explicit specialization, and the | |
1254 | implicit instantiation has not yet been used. That | |
1255 | situation can occur if we have implicitly | |
1256 | instantiated a member function and then specialized | |
1257 | it later. | |
c8094d83 | 1258 | |
c7222c02 MM |
1259 | We can also wind up here if a friend declaration that |
1260 | looked like an instantiation turns out to be a | |
1261 | specialization: | |
c8094d83 | 1262 | |
c7222c02 MM |
1263 | template <class T> void foo(T); |
1264 | class S { friend void foo<>(int) }; | |
c8094d83 MS |
1265 | template <> void foo(int); |
1266 | ||
c7222c02 MM |
1267 | We transform the existing DECL in place so that any |
1268 | pointers to it become pointers to the updated | |
1269 | declaration. | |
1270 | ||
1271 | If there was a definition for the template, but not | |
1272 | for the specialization, we want this to look as if | |
1273 | there were no definition, and vice versa. */ | |
1274 | DECL_INITIAL (fn) = NULL_TREE; | |
d63d5d0c | 1275 | duplicate_decls (spec, fn, is_friend); |
a39041fa | 1276 | /* The call to duplicate_decls will have applied |
3db45ab5 | 1277 | [temp.expl.spec]: |
a39041fa | 1278 | |
3db45ab5 | 1279 | An explicit specialization of a function template |
a39041fa | 1280 | is inline only if it is explicitly declared to be, |
bcf51da2 | 1281 | and independently of whether its function template |
a39041fa MM |
1282 | is. |
1283 | ||
1284 | to the primary function; now copy the inline bits to | |
3db45ab5 | 1285 | the various clones. */ |
a39041fa MM |
1286 | FOR_EACH_CLONE (clone, fn) |
1287 | { | |
1288 | DECL_DECLARED_INLINE_P (clone) | |
1289 | = DECL_DECLARED_INLINE_P (fn); | |
1290 | DECL_INLINE (clone) | |
1291 | = DECL_INLINE (fn); | |
1292 | } | |
9c12301f | 1293 | check_specialization_namespace (fn); |
c8094d83 | 1294 | |
b1cc95ce MM |
1295 | return fn; |
1296 | } | |
22e9174f | 1297 | } |
c7222c02 MM |
1298 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) |
1299 | { | |
d63d5d0c | 1300 | if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec)) |
c7222c02 MM |
1301 | /* Dup decl failed, but this is a new definition. Set the |
1302 | line number so any errors match this new | |
1303 | definition. */ | |
1304 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); | |
c8094d83 | 1305 | |
c7222c02 MM |
1306 | return fn; |
1307 | } | |
b1cc95ce MM |
1308 | } |
1309 | ||
1310 | /* A specialization must be declared in the same namespace as the | |
1311 | template it is specializing. */ | |
1312 | if (DECL_TEMPLATE_SPECIALIZATION (spec) | |
1313 | && !check_specialization_namespace (tmpl)) | |
74b0d490 | 1314 | DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl)); |
75650646 | 1315 | |
c7222c02 MM |
1316 | if (!optimize_specialization_lookup_p (tmpl)) |
1317 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
1318 | = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
36a117a5 MM |
1319 | |
1320 | return spec; | |
1321 | } | |
1322 | ||
1323 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
b3445994 MM |
1324 | Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true |
1325 | if the SPEC was listed as a specialization of TMPL. */ | |
36a117a5 | 1326 | |
b3445994 MM |
1327 | bool |
1328 | reregister_specialization (tree spec, tree tmpl, tree new_spec) | |
36a117a5 MM |
1329 | { |
1330 | tree* s; | |
1331 | ||
1332 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
1333 | *s != NULL_TREE; | |
1334 | s = &TREE_CHAIN (*s)) | |
1335 | if (TREE_VALUE (*s) == spec) | |
1336 | { | |
b3445994 MM |
1337 | if (!new_spec) |
1338 | *s = TREE_CHAIN (*s); | |
1339 | else | |
a3d87771 | 1340 | TREE_VALUE (*s) = new_spec; |
36a117a5 MM |
1341 | return 1; |
1342 | } | |
1343 | ||
1344 | return 0; | |
75650646 MM |
1345 | } |
1346 | ||
a723baf1 MM |
1347 | /* Compare an entry in the local specializations hash table P1 (which |
1348 | is really a pointer to a TREE_LIST) with P2 (which is really a | |
1349 | DECL). */ | |
1350 | ||
1351 | static int | |
1352 | eq_local_specializations (const void *p1, const void *p2) | |
1353 | { | |
741ac903 | 1354 | return TREE_VALUE ((const_tree) p1) == (const_tree) p2; |
a723baf1 MM |
1355 | } |
1356 | ||
69f794a7 MM |
1357 | /* Hash P1, an entry in the local specializations table. */ |
1358 | ||
1359 | static hashval_t | |
1360 | hash_local_specialization (const void* p1) | |
1361 | { | |
741ac903 | 1362 | return htab_hash_pointer (TREE_VALUE ((const_tree) p1)); |
69f794a7 MM |
1363 | } |
1364 | ||
6dfbb909 MM |
1365 | /* Like register_specialization, but for local declarations. We are |
1366 | registering SPEC, an instantiation of TMPL. */ | |
9188c363 | 1367 | |
414ea4aa | 1368 | static void |
3a978d72 | 1369 | register_local_specialization (tree spec, tree tmpl) |
9188c363 | 1370 | { |
6dfbb909 MM |
1371 | void **slot; |
1372 | ||
c8094d83 | 1373 | slot = htab_find_slot_with_hash (local_specializations, tmpl, |
69f794a7 | 1374 | htab_hash_pointer (tmpl), INSERT); |
a723baf1 | 1375 | *slot = build_tree_list (spec, tmpl); |
9188c363 MM |
1376 | } |
1377 | ||
9ba7a2f2 MM |
1378 | /* TYPE is a class type. Returns true if TYPE is an explicitly |
1379 | specialized class. */ | |
1380 | ||
1381 | bool | |
1382 | explicit_class_specialization_p (tree type) | |
1383 | { | |
1384 | if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
1385 | return false; | |
1386 | return !uses_template_parms (CLASSTYPE_TI_ARGS (type)); | |
1387 | } | |
1388 | ||
e1467ff2 MM |
1389 | /* Print the list of candidate FNS in an error message. */ |
1390 | ||
104bf76a | 1391 | void |
3a978d72 | 1392 | print_candidates (tree fns) |
e1467ff2 MM |
1393 | { |
1394 | tree fn; | |
1395 | ||
d8e178a0 | 1396 | const char *str = "candidates are:"; |
e1467ff2 MM |
1397 | |
1398 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
1399 | { | |
d6479fe7 MM |
1400 | tree f; |
1401 | ||
1402 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
dee15844 | 1403 | error ("%s %+#D", str, OVL_CURRENT (f)); |
e1467ff2 MM |
1404 | str = " "; |
1405 | } | |
1406 | } | |
1407 | ||
75650646 | 1408 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 1409 | which can be specialized to match the indicated DECL with the |
03017874 MM |
1410 | explicit template args given in TEMPLATE_ID. The DECL may be |
1411 | NULL_TREE if none is available. In that case, the functions in | |
1412 | TEMPLATE_ID are non-members. | |
1413 | ||
838dfd8a | 1414 | If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a |
03017874 MM |
1415 | specialization of a member template. |
1416 | ||
5fe7b654 GB |
1417 | The TEMPLATE_COUNT is the number of references to qualifying |
1418 | template classes that appeared in the name of the function. See | |
1419 | check_explicit_specialization for a more accurate description. | |
1420 | ||
29a1da1c MM |
1421 | TSK indicates what kind of template declaration (if any) is being |
1422 | declared. TSK_TEMPLATE indicates that the declaration given by | |
1423 | DECL, though a FUNCTION_DECL, has template parameters, and is | |
1424 | therefore a template function. | |
1425 | ||
03017874 MM |
1426 | The template args (those explicitly specified and those deduced) |
1427 | are output in a newly created vector *TARGS_OUT. | |
1428 | ||
1429 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 1430 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 1431 | |
e9659ab0 | 1432 | static tree |
c8094d83 | 1433 | determine_specialization (tree template_id, |
0cbd7506 MS |
1434 | tree decl, |
1435 | tree* targs_out, | |
5fe7b654 | 1436 | int need_member_template, |
29a1da1c MM |
1437 | int template_count, |
1438 | tmpl_spec_kind tsk) | |
386b8a85 | 1439 | { |
03017874 MM |
1440 | tree fns; |
1441 | tree targs; | |
1442 | tree explicit_targs; | |
1443 | tree candidates = NULL_TREE; | |
7ca383e6 MM |
1444 | /* A TREE_LIST of templates of which DECL may be a specialization. |
1445 | The TREE_VALUE of each node is a TEMPLATE_DECL. The | |
1446 | corresponding TREE_PURPOSE is the set of template arguments that, | |
1447 | when used to instantiate the template, would produce a function | |
1448 | with the signature of DECL. */ | |
03017874 | 1449 | tree templates = NULL_TREE; |
5fe7b654 GB |
1450 | int header_count; |
1451 | struct cp_binding_level *b; | |
386b8a85 | 1452 | |
e1467ff2 MM |
1453 | *targs_out = NULL_TREE; |
1454 | ||
728da672 | 1455 | if (template_id == error_mark_node || decl == error_mark_node) |
f2e48b67 BK |
1456 | return error_mark_node; |
1457 | ||
1458 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 1459 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 1460 | |
27fafc8d JM |
1461 | if (fns == error_mark_node) |
1462 | return error_mark_node; | |
1463 | ||
c6002625 | 1464 | /* Check for baselinks. */ |
91e490ab | 1465 | if (BASELINK_P (fns)) |
da15dae6 | 1466 | fns = BASELINK_FUNCTIONS (fns); |
386b8a85 | 1467 | |
91e490ab MM |
1468 | if (!is_overloaded_fn (fns)) |
1469 | { | |
0f51ccfc | 1470 | error ("%qD is not a function template", fns); |
91e490ab MM |
1471 | return error_mark_node; |
1472 | } | |
1473 | ||
5fe7b654 GB |
1474 | /* Count the number of template headers specified for this |
1475 | specialization. */ | |
1476 | header_count = 0; | |
c8094d83 | 1477 | for (b = current_binding_level; |
98f99d7f | 1478 | b->kind == sk_template_parms; |
5fe7b654 GB |
1479 | b = b->level_chain) |
1480 | ++header_count; | |
1481 | ||
2c73f9f5 | 1482 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 1483 | { |
9dfce8fd | 1484 | tree fn = OVL_CURRENT (fns); |
03017874 MM |
1485 | |
1486 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
d955f6ea KL |
1487 | { |
1488 | tree decl_arg_types; | |
0da99d4e | 1489 | tree fn_arg_types; |
d955f6ea | 1490 | |
5fe7b654 GB |
1491 | /* In case of explicit specialization, we need to check if |
1492 | the number of template headers appearing in the specialization | |
1493 | is correct. This is usually done in check_explicit_specialization, | |
1494 | but the check done there cannot be exhaustive when specializing | |
1495 | member functions. Consider the following code: | |
1496 | ||
1497 | template <> void A<int>::f(int); | |
1498 | template <> template <> void A<int>::f(int); | |
1499 | ||
1500 | Assuming that A<int> is not itself an explicit specialization | |
1501 | already, the first line specializes "f" which is a non-template | |
1502 | member function, whilst the second line specializes "f" which | |
1503 | is a template member function. So both lines are syntactically | |
1504 | correct, and check_explicit_specialization does not reject | |
1505 | them. | |
c8094d83 | 1506 | |
5fe7b654 GB |
1507 | Here, we can do better, as we are matching the specialization |
1508 | against the declarations. We count the number of template | |
1509 | headers, and we check if they match TEMPLATE_COUNT + 1 | |
1510 | (TEMPLATE_COUNT is the number of qualifying template classes, | |
1511 | plus there must be another header for the member template | |
1512 | itself). | |
c8094d83 | 1513 | |
5fe7b654 GB |
1514 | Notice that if header_count is zero, this is not a |
1515 | specialization but rather a template instantiation, so there | |
1516 | is no check we can perform here. */ | |
1517 | if (header_count && header_count != template_count + 1) | |
1518 | continue; | |
1519 | ||
98f99d7f MM |
1520 | /* Check that the number of template arguments at the |
1521 | innermost level for DECL is the same as for FN. */ | |
1522 | if (current_binding_level->kind == sk_template_parms | |
1523 | && !current_binding_level->explicit_spec_p | |
1524 | && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn)) | |
3db45ab5 | 1525 | != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS |
916b63c3 | 1526 | (current_template_parms)))) |
98f99d7f | 1527 | continue; |
c8094d83 | 1528 | |
ba139ba8 MM |
1529 | /* DECL might be a specialization of FN. */ |
1530 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1531 | fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
1532 | ||
1533 | /* For a non-static member function, we need to make sure | |
1534 | that the const qualification is the same. Since | |
1535 | get_bindings does not try to merge the "this" parameter, | |
1536 | we must do the comparison explicitly. */ | |
1537 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) | |
1538 | && !same_type_p (TREE_VALUE (fn_arg_types), | |
1539 | TREE_VALUE (decl_arg_types))) | |
1540 | continue; | |
1541 | ||
1542 | /* Skip the "this" parameter and, for constructors of | |
1543 | classes with virtual bases, the VTT parameter. A | |
1544 | full specialization of a constructor will have a VTT | |
1545 | parameter, but a template never will. */ | |
1546 | decl_arg_types | |
1547 | = skip_artificial_parms_for (decl, decl_arg_types); | |
1548 | fn_arg_types | |
1549 | = skip_artificial_parms_for (fn, fn_arg_types); | |
1550 | ||
1551 | /* Check that the number of function parameters matches. | |
1552 | For example, | |
1553 | template <class T> void f(int i = 0); | |
1554 | template <> void f<int>(); | |
1555 | The specialization f<int> is invalid but is not caught | |
1556 | by get_bindings below. */ | |
1557 | if (list_length (fn_arg_types) != list_length (decl_arg_types)) | |
1558 | continue; | |
1559 | ||
29a1da1c MM |
1560 | /* Function templates cannot be specializations; there are |
1561 | no partial specializations of functions. Therefore, if | |
1562 | the type of DECL does not match FN, there is no | |
1563 | match. */ | |
1564 | if (tsk == tsk_template) | |
1565 | { | |
ba139ba8 | 1566 | if (compparms (fn_arg_types, decl_arg_types)) |
29a1da1c MM |
1567 | candidates = tree_cons (NULL_TREE, fn, candidates); |
1568 | continue; | |
1569 | } | |
1570 | ||
d955f6ea KL |
1571 | /* See whether this function might be a specialization of this |
1572 | template. */ | |
a34d3336 | 1573 | targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true); |
d955f6ea KL |
1574 | |
1575 | if (!targs) | |
1576 | /* We cannot deduce template arguments that when used to | |
1577 | specialize TMPL will produce DECL. */ | |
1578 | continue; | |
1579 | ||
1580 | /* Save this template, and the arguments deduced. */ | |
1581 | templates = tree_cons (targs, fn, templates); | |
1582 | } | |
03017874 MM |
1583 | else if (need_member_template) |
1584 | /* FN is an ordinary member function, and we need a | |
1585 | specialization of a member template. */ | |
d955f6ea | 1586 | ; |
03017874 MM |
1587 | else if (TREE_CODE (fn) != FUNCTION_DECL) |
1588 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1589 | cases. */ | |
d955f6ea | 1590 | ; |
03017874 MM |
1591 | else if (!DECL_FUNCTION_MEMBER_P (fn)) |
1592 | /* This is just an ordinary non-member function. Nothing can | |
1593 | be a specialization of that. */ | |
d955f6ea | 1594 | ; |
3b82c249 KL |
1595 | else if (DECL_ARTIFICIAL (fn)) |
1596 | /* Cannot specialize functions that are created implicitly. */ | |
d955f6ea | 1597 | ; |
75650646 | 1598 | else |
03017874 MM |
1599 | { |
1600 | tree decl_arg_types; | |
386b8a85 | 1601 | |
03017874 MM |
1602 | /* This is an ordinary member function. However, since |
1603 | we're here, we can assume it's enclosing class is a | |
1604 | template class. For example, | |
c8094d83 | 1605 | |
03017874 MM |
1606 | template <typename T> struct S { void f(); }; |
1607 | template <> void S<int>::f() {} | |
1608 | ||
1609 | Here, S<int>::f is a non-template, but S<int> is a | |
1610 | template class. If FN has the same type as DECL, we | |
1611 | might be in business. */ | |
f5d47abd KL |
1612 | |
1613 | if (!DECL_TEMPLATE_INFO (fn)) | |
1614 | /* Its enclosing class is an explicit specialization | |
1615 | of a template class. This is not a candidate. */ | |
1616 | continue; | |
1617 | ||
03017874 MM |
1618 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
1619 | TREE_TYPE (TREE_TYPE (fn)))) | |
1620 | /* The return types differ. */ | |
1621 | continue; | |
1622 | ||
1623 | /* Adjust the type of DECL in case FN is a static member. */ | |
1624 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
c8094d83 | 1625 | if (DECL_STATIC_FUNCTION_P (fn) |
03017874 MM |
1626 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1627 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1628 | ||
c8094d83 | 1629 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), |
03017874 MM |
1630 | decl_arg_types)) |
1631 | /* They match! */ | |
1632 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
03017874 | 1633 | } |
386b8a85 | 1634 | } |
03017874 | 1635 | |
bf8f3f93 | 1636 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1637 | { |
03017874 | 1638 | /* We have: |
c8094d83 | 1639 | |
03017874 MM |
1640 | [temp.expl.spec] |
1641 | ||
1642 | It is possible for a specialization with a given function | |
1643 | signature to be instantiated from more than one function | |
1644 | template. In such cases, explicit specification of the | |
1645 | template arguments must be used to uniquely identify the | |
1646 | function template specialization being specialized. | |
1647 | ||
1648 | Note that here, there's no suggestion that we're supposed to | |
1649 | determine which of the candidate templates is most | |
1650 | specialized. However, we, also have: | |
1651 | ||
1652 | [temp.func.order] | |
1653 | ||
1654 | Partial ordering of overloaded function template | |
1655 | declarations is used in the following contexts to select | |
1656 | the function template to which a function template | |
c8094d83 | 1657 | specialization refers: |
03017874 | 1658 | |
0cbd7506 | 1659 | -- when an explicit specialization refers to a function |
c8094d83 | 1660 | template. |
03017874 MM |
1661 | |
1662 | So, we do use the partial ordering rules, at least for now. | |
0e339752 | 1663 | This extension can only serve to make invalid programs valid, |
03017874 MM |
1664 | so it's safe. And, there is strong anecdotal evidence that |
1665 | the committee intended the partial ordering rules to apply; | |
3b426391 | 1666 | the EDG front end has that behavior, and John Spicer claims |
03017874 MM |
1667 | that the committee simply forgot to delete the wording in |
1668 | [temp.expl.spec]. */ | |
7ca383e6 MM |
1669 | tree tmpl = most_specialized_instantiation (templates); |
1670 | if (tmpl != error_mark_node) | |
1671 | { | |
1672 | templates = tmpl; | |
1673 | TREE_CHAIN (templates) = NULL_TREE; | |
1674 | } | |
e1467ff2 MM |
1675 | } |
1676 | ||
03017874 | 1677 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1678 | { |
dee15844 JM |
1679 | error ("template-id %qD for %q+D does not match any template " |
1680 | "declaration", template_id, decl); | |
03017874 | 1681 | return error_mark_node; |
386b8a85 | 1682 | } |
03017874 | 1683 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1684 | || (candidates && TREE_CHAIN (candidates)) |
1685 | || (templates && candidates)) | |
386b8a85 | 1686 | { |
dee15844 JM |
1687 | error ("ambiguous template specialization %qD for %q+D", |
1688 | template_id, decl); | |
bf8f3f93 MM |
1689 | chainon (candidates, templates); |
1690 | print_candidates (candidates); | |
03017874 | 1691 | return error_mark_node; |
386b8a85 JM |
1692 | } |
1693 | ||
c6002625 | 1694 | /* We have one, and exactly one, match. */ |
03017874 MM |
1695 | if (candidates) |
1696 | { | |
29a1da1c MM |
1697 | tree fn = TREE_VALUE (candidates); |
1698 | /* DECL is a re-declaration of a template function. */ | |
1699 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
1700 | return fn; | |
03017874 MM |
1701 | /* It was a specialization of an ordinary member function in a |
1702 | template class. */ | |
29a1da1c MM |
1703 | *targs_out = copy_node (DECL_TI_ARGS (fn)); |
1704 | return DECL_TI_TEMPLATE (fn); | |
03017874 MM |
1705 | } |
1706 | ||
1707 | /* It was a specialization of a template. */ | |
17aec3eb | 1708 | targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); |
03017874 MM |
1709 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) |
1710 | { | |
1711 | *targs_out = copy_node (targs); | |
c8094d83 | 1712 | SET_TMPL_ARGS_LEVEL (*targs_out, |
03017874 MM |
1713 | TMPL_ARGS_DEPTH (*targs_out), |
1714 | TREE_PURPOSE (templates)); | |
1715 | } | |
1716 | else | |
1717 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1718 | return TREE_VALUE (templates); |
8d08fdba | 1719 | } |
8afa707f MM |
1720 | |
1721 | /* Returns a chain of parameter types, exactly like the SPEC_TYPES, | |
1722 | but with the default argument values filled in from those in the | |
1723 | TMPL_TYPES. */ | |
c8094d83 | 1724 | |
8afa707f | 1725 | static tree |
3a978d72 NN |
1726 | copy_default_args_to_explicit_spec_1 (tree spec_types, |
1727 | tree tmpl_types) | |
8afa707f MM |
1728 | { |
1729 | tree new_spec_types; | |
1730 | ||
1731 | if (!spec_types) | |
1732 | return NULL_TREE; | |
1733 | ||
1734 | if (spec_types == void_list_node) | |
1735 | return void_list_node; | |
1736 | ||
1737 | /* Substitute into the rest of the list. */ | |
1738 | new_spec_types = | |
1739 | copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), | |
1740 | TREE_CHAIN (tmpl_types)); | |
c8094d83 | 1741 | |
8afa707f MM |
1742 | /* Add the default argument for this parameter. */ |
1743 | return hash_tree_cons (TREE_PURPOSE (tmpl_types), | |
1744 | TREE_VALUE (spec_types), | |
1745 | new_spec_types); | |
1746 | } | |
1747 | ||
1748 | /* DECL is an explicit specialization. Replicate default arguments | |
1749 | from the template it specializes. (That way, code like: | |
1750 | ||
1751 | template <class T> void f(T = 3); | |
1752 | template <> void f(double); | |
c8094d83 | 1753 | void g () { f (); } |
8afa707f MM |
1754 | |
1755 | works, as required.) An alternative approach would be to look up | |
1756 | the correct default arguments at the call-site, but this approach | |
1757 | is consistent with how implicit instantiations are handled. */ | |
1758 | ||
1759 | static void | |
3a978d72 | 1760 | copy_default_args_to_explicit_spec (tree decl) |
8afa707f MM |
1761 | { |
1762 | tree tmpl; | |
1763 | tree spec_types; | |
1764 | tree tmpl_types; | |
1765 | tree new_spec_types; | |
1766 | tree old_type; | |
1767 | tree new_type; | |
1768 | tree t; | |
08c2df0f NS |
1769 | tree object_type = NULL_TREE; |
1770 | tree in_charge = NULL_TREE; | |
e0fff4b3 | 1771 | tree vtt = NULL_TREE; |
8afa707f MM |
1772 | |
1773 | /* See if there's anything we need to do. */ | |
1774 | tmpl = DECL_TI_TEMPLATE (decl); | |
1775 | tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); | |
1776 | for (t = tmpl_types; t; t = TREE_CHAIN (t)) | |
1777 | if (TREE_PURPOSE (t)) | |
1778 | break; | |
1779 | if (!t) | |
1780 | return; | |
1781 | ||
1782 | old_type = TREE_TYPE (decl); | |
1783 | spec_types = TYPE_ARG_TYPES (old_type); | |
c8094d83 | 1784 | |
8afa707f MM |
1785 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1786 | { | |
08c2df0f | 1787 | /* Remove the this pointer, but remember the object's type for |
0cbd7506 | 1788 | CV quals. */ |
08c2df0f | 1789 | object_type = TREE_TYPE (TREE_VALUE (spec_types)); |
8afa707f MM |
1790 | spec_types = TREE_CHAIN (spec_types); |
1791 | tmpl_types = TREE_CHAIN (tmpl_types); | |
c8094d83 | 1792 | |
8afa707f | 1793 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) |
0cbd7506 MS |
1794 | { |
1795 | /* DECL may contain more parameters than TMPL due to the extra | |
1796 | in-charge parameter in constructors and destructors. */ | |
1797 | in_charge = spec_types; | |
08c2df0f NS |
1798 | spec_types = TREE_CHAIN (spec_types); |
1799 | } | |
e0fff4b3 JM |
1800 | if (DECL_HAS_VTT_PARM_P (decl)) |
1801 | { | |
1802 | vtt = spec_types; | |
1803 | spec_types = TREE_CHAIN (spec_types); | |
1804 | } | |
8afa707f MM |
1805 | } |
1806 | ||
1807 | /* Compute the merged default arguments. */ | |
c8094d83 | 1808 | new_spec_types = |
8afa707f MM |
1809 | copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); |
1810 | ||
08c2df0f NS |
1811 | /* Compute the new FUNCTION_TYPE. */ |
1812 | if (object_type) | |
8afa707f | 1813 | { |
e0fff4b3 | 1814 | if (vtt) |
0cbd7506 MS |
1815 | new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), |
1816 | TREE_VALUE (vtt), | |
1817 | new_spec_types); | |
e0fff4b3 | 1818 | |
08c2df0f | 1819 | if (in_charge) |
0cbd7506 MS |
1820 | /* Put the in-charge parameter back. */ |
1821 | new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), | |
1822 | TREE_VALUE (in_charge), | |
1823 | new_spec_types); | |
08c2df0f | 1824 | |
43dc123f MM |
1825 | new_type = build_method_type_directly (object_type, |
1826 | TREE_TYPE (old_type), | |
1827 | new_spec_types); | |
8afa707f | 1828 | } |
8afa707f MM |
1829 | else |
1830 | new_type = build_function_type (TREE_TYPE (old_type), | |
08c2df0f | 1831 | new_spec_types); |
e9525111 MM |
1832 | new_type = cp_build_type_attribute_variant (new_type, |
1833 | TYPE_ATTRIBUTES (old_type)); | |
8afa707f MM |
1834 | new_type = build_exception_variant (new_type, |
1835 | TYPE_RAISES_EXCEPTIONS (old_type)); | |
1836 | TREE_TYPE (decl) = new_type; | |
1837 | } | |
1838 | ||
386b8a85 | 1839 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1840 | DECLARATOR, and in DECL, is a specialization of a function |
1841 | template. We may also discover that the declaration is an explicit | |
1842 | instantiation at this point. | |
1843 | ||
e1467ff2 | 1844 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
1845 | instead if all goes well. Issues an error message if something is |
1846 | amiss. Returns error_mark_node if the error is not easily | |
1847 | recoverable. | |
c8094d83 MS |
1848 | |
1849 | FLAGS is a bitmask consisting of the following flags: | |
75650646 | 1850 | |
75650646 MM |
1851 | 2: The function has a definition. |
1852 | 4: The function is a friend. | |
75650646 MM |
1853 | |
1854 | The TEMPLATE_COUNT is the number of references to qualifying | |
1855 | template classes that appeared in the name of the function. For | |
1856 | example, in | |
1857 | ||
1858 | template <class T> struct S { void f(); }; | |
1859 | void S<int>::f(); | |
c8094d83 | 1860 | |
75650646 MM |
1861 | the TEMPLATE_COUNT would be 1. However, explicitly specialized |
1862 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1863 | ||
1864 | template <class T> struct S {}; | |
1865 | template <> struct S<int> { void f(); } | |
36a117a5 | 1866 | template <> void S<int>::f(); |
75650646 MM |
1867 | |
1868 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
0e339752 | 1869 | invalid; there should be no template <>.) |
75650646 MM |
1870 | |
1871 | If the function is a specialization, it is marked as such via | |
1872 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
c8094d83 | 1873 | is set up correctly, and it is added to the list of specializations |
75650646 | 1874 | for that template. */ |
386b8a85 | 1875 | |
e1467ff2 | 1876 | tree |
c8094d83 | 1877 | check_explicit_specialization (tree declarator, |
0cbd7506 MS |
1878 | tree decl, |
1879 | int template_count, | |
1880 | int flags) | |
386b8a85 | 1881 | { |
75650646 MM |
1882 | int have_def = flags & 2; |
1883 | int is_friend = flags & 4; | |
1884 | int specialization = 0; | |
e1467ff2 | 1885 | int explicit_instantiation = 0; |
fd4de5ff | 1886 | int member_specialization = 0; |
75650646 MM |
1887 | tree ctype = DECL_CLASS_CONTEXT (decl); |
1888 | tree dname = DECL_NAME (decl); | |
74b846e0 | 1889 | tmpl_spec_kind tsk; |
386b8a85 | 1890 | |
f65b7de3 GB |
1891 | if (is_friend) |
1892 | { | |
1893 | if (!processing_specialization) | |
1894 | tsk = tsk_none; | |
1895 | else | |
1896 | tsk = tsk_excessive_parms; | |
1897 | } | |
1898 | else | |
1899 | tsk = current_tmpl_spec_kind (template_count); | |
75650646 | 1900 | |
74b846e0 MM |
1901 | switch (tsk) |
1902 | { | |
1903 | case tsk_none: | |
c8094d83 | 1904 | if (processing_specialization) |
75650646 | 1905 | { |
fd4de5ff MM |
1906 | specialization = 1; |
1907 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1908 | } |
74b846e0 | 1909 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
386b8a85 | 1910 | { |
74b846e0 MM |
1911 | if (is_friend) |
1912 | /* This could be something like: | |
75650646 | 1913 | |
74b846e0 MM |
1914 | template <class T> void f(T); |
1915 | class S { friend void f<>(int); } */ | |
1916 | specialization = 1; | |
1917 | else | |
1918 | { | |
1919 | /* This case handles bogus declarations like template <> | |
1920 | template <class T> void f<int>(); */ | |
1921 | ||
0f51ccfc | 1922 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 1923 | declarator); |
74b846e0 MM |
1924 | return decl; |
1925 | } | |
1926 | } | |
1927 | break; | |
1928 | ||
1929 | case tsk_invalid_member_spec: | |
1930 | /* The error has already been reported in | |
1931 | check_specialization_scope. */ | |
1932 | return error_mark_node; | |
1933 | ||
1934 | case tsk_invalid_expl_inst: | |
33bd39a2 | 1935 | error ("template parameter list used in explicit instantiation"); |
74b846e0 MM |
1936 | |
1937 | /* Fall through. */ | |
1938 | ||
1939 | case tsk_expl_inst: | |
fd4de5ff | 1940 | if (have_def) |
33bd39a2 | 1941 | error ("definition provided for explicit instantiation"); |
c8094d83 | 1942 | |
fd4de5ff | 1943 | explicit_instantiation = 1; |
74b846e0 | 1944 | break; |
fd4de5ff | 1945 | |
74b846e0 | 1946 | case tsk_excessive_parms: |
f03adc6b GB |
1947 | case tsk_insufficient_parms: |
1948 | if (tsk == tsk_excessive_parms) | |
0cbd7506 | 1949 | error ("too many template parameter lists in declaration of %qD", |
f03adc6b GB |
1950 | decl); |
1951 | else if (template_header_count) | |
0f51ccfc | 1952 | error("too few template parameter lists in declaration of %qD", decl); |
f03adc6b | 1953 | else |
0f51ccfc | 1954 | error("explicit specialization of %qD must be introduced by " |
9e637a26 | 1955 | "%<template <>%>", decl); |
75650646 | 1956 | |
74b846e0 MM |
1957 | /* Fall through. */ |
1958 | case tsk_expl_spec: | |
1959 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1960 | if (ctype) | |
1961 | member_specialization = 1; | |
1962 | else | |
1963 | specialization = 1; | |
1964 | break; | |
74b846e0 MM |
1965 | |
1966 | case tsk_template: | |
1967 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
c6f2ed0d | 1968 | { |
fd4de5ff MM |
1969 | /* This case handles bogus declarations like template <> |
1970 | template <class T> void f<int>(); */ | |
75650646 | 1971 | |
655dc6ee | 1972 | if (uses_template_parms (declarator)) |
0f51ccfc | 1973 | error ("function template partial specialization %qD " |
e1e93ad8 | 1974 | "is not allowed", declarator); |
655dc6ee | 1975 | else |
0f51ccfc | 1976 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 1977 | declarator); |
fd4de5ff | 1978 | return decl; |
386b8a85 | 1979 | } |
74b846e0 MM |
1980 | |
1981 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
1982 | /* This is a specialization of a member template, without | |
1983 | specialization the containing class. Something like: | |
1984 | ||
1985 | template <class T> struct S { | |
c8094d83 | 1986 | template <class U> void f (U); |
0cbd7506 | 1987 | }; |
74b846e0 | 1988 | template <> template <class U> void S<int>::f(U) {} |
c8094d83 | 1989 | |
74b846e0 MM |
1990 | That's a specialization -- but of the entire template. */ |
1991 | specialization = 1; | |
1992 | break; | |
1993 | ||
1994 | default: | |
315fb5db | 1995 | gcc_unreachable (); |
75650646 | 1996 | } |
386b8a85 | 1997 | |
670960ac JM |
1998 | if (specialization || member_specialization) |
1999 | { | |
2000 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
2001 | for (; t; t = TREE_CHAIN (t)) | |
2002 | if (TREE_PURPOSE (t)) | |
2003 | { | |
33bd39a2 | 2004 | pedwarn |
8251199e | 2005 | ("default argument specified in explicit specialization"); |
670960ac JM |
2006 | break; |
2007 | } | |
2008 | } | |
2009 | ||
e1467ff2 | 2010 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
2011 | { |
2012 | tree tmpl = NULL_TREE; | |
2013 | tree targs = NULL_TREE; | |
75650646 MM |
2014 | |
2015 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
2016 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
2017 | { | |
2018 | tree fns; | |
2019 | ||
50bc768d | 2020 | gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE); |
2f54a1db | 2021 | if (ctype) |
386b8a85 | 2022 | fns = dname; |
2f54a1db GB |
2023 | else |
2024 | { | |
2025 | /* If there is no class context, the explicit instantiation | |
0cbd7506 | 2026 | must be at namespace scope. */ |
50bc768d | 2027 | gcc_assert (DECL_NAMESPACE_SCOPE_P (decl)); |
2f54a1db GB |
2028 | |
2029 | /* Find the namespace binding, using the declaration | |
0cbd7506 | 2030 | context. */ |
ddf74938 | 2031 | fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname, |
664a90c0 | 2032 | false, true); |
97dc8e5b | 2033 | if (fns == error_mark_node || !is_overloaded_fn (fns)) |
4230cec2 NS |
2034 | { |
2035 | error ("%qD is not a template function", dname); | |
2036 | fns = error_mark_node; | |
2037 | } | |
664a90c0 JM |
2038 | else |
2039 | { | |
2040 | tree fn = OVL_CURRENT (fns); | |
ddf74938 | 2041 | if (!is_associated_namespace (CP_DECL_CONTEXT (decl), |
08d295c5 JM |
2042 | CP_DECL_CONTEXT (fn))) |
2043 | error ("%qD is not declared in %qD", | |
2044 | decl, current_namespace); | |
664a90c0 | 2045 | } |
2f54a1db | 2046 | } |
386b8a85 | 2047 | |
2f54a1db | 2048 | declarator = lookup_template_function (fns, NULL_TREE); |
386b8a85 JM |
2049 | } |
2050 | ||
f2e48b67 BK |
2051 | if (declarator == error_mark_node) |
2052 | return error_mark_node; | |
2053 | ||
75650646 MM |
2054 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
2055 | { | |
8ec2ac34 | 2056 | if (!explicit_instantiation) |
0e339752 | 2057 | /* A specialization in class scope. This is invalid, |
6c30752f MM |
2058 | but the error will already have been flagged by |
2059 | check_specialization_scope. */ | |
2060 | return error_mark_node; | |
8ec2ac34 | 2061 | else |
b370501f | 2062 | { |
0e339752 | 2063 | /* It's not valid to write an explicit instantiation in |
b370501f | 2064 | class scope, e.g.: |
8ec2ac34 | 2065 | |
0cbd7506 | 2066 | class C { template void f(); } |
8ec2ac34 | 2067 | |
b370501f KG |
2068 | This case is caught by the parser. However, on |
2069 | something like: | |
c8094d83 | 2070 | |
b370501f | 2071 | template class C { void f(); }; |
8ec2ac34 | 2072 | |
0e339752 | 2073 | (which is invalid) we can get here. The error will be |
b370501f KG |
2074 | issued later. */ |
2075 | ; | |
2076 | } | |
8ec2ac34 | 2077 | |
e1467ff2 | 2078 | return decl; |
75650646 | 2079 | } |
c8094d83 | 2080 | else if (ctype != NULL_TREE |
75650646 MM |
2081 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == |
2082 | IDENTIFIER_NODE)) | |
386b8a85 | 2083 | { |
75650646 MM |
2084 | /* Find the list of functions in ctype that have the same |
2085 | name as the declared function. */ | |
2086 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
2087 | tree fns = NULL_TREE; |
2088 | int idx; | |
2089 | ||
8ba658ee | 2090 | if (constructor_name_p (name, ctype)) |
386b8a85 | 2091 | { |
75650646 | 2092 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
c8094d83 | 2093 | |
75650646 | 2094 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) |
9f4faeae | 2095 | : !CLASSTYPE_DESTRUCTORS (ctype)) |
75650646 MM |
2096 | { |
2097 | /* From [temp.expl.spec]: | |
c8094d83 | 2098 | |
75650646 MM |
2099 | If such an explicit specialization for the member |
2100 | of a class template names an implicitly-declared | |
2101 | special member function (clause _special_), the | |
c8094d83 | 2102 | program is ill-formed. |
e1467ff2 MM |
2103 | |
2104 | Similar language is found in [temp.explicit]. */ | |
33bd39a2 | 2105 | error ("specialization of implicitly-declared special member function"); |
03017874 | 2106 | return error_mark_node; |
75650646 | 2107 | } |
386b8a85 | 2108 | |
42da2fd8 | 2109 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 2110 | } |
42da2fd8 | 2111 | |
421844e7 | 2112 | if (!DECL_CONV_FN_P (decl)) |
03017874 MM |
2113 | { |
2114 | idx = lookup_fnfields_1 (ctype, name); | |
2115 | if (idx >= 0) | |
aaaa46d2 | 2116 | fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx); |
03017874 MM |
2117 | } |
2118 | else | |
2119 | { | |
d4e6fecb | 2120 | VEC(tree,gc) *methods; |
aaaa46d2 | 2121 | tree ovl; |
03017874 MM |
2122 | |
2123 | /* For a type-conversion operator, we cannot do a | |
2124 | name-based lookup. We might be looking for `operator | |
2125 | int' which will be a specialization of `operator T'. | |
2126 | So, we find *all* the conversion operators, and then | |
2127 | select from them. */ | |
2128 | fns = NULL_TREE; | |
2129 | ||
2130 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
2131 | if (methods) | |
5dd236e2 | 2132 | for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; |
9ba5ff0f | 2133 | VEC_iterate (tree, methods, idx, ovl); |
aaaa46d2 | 2134 | ++idx) |
03017874 | 2135 | { |
aaaa46d2 | 2136 | if (!DECL_CONV_FN_P (OVL_CURRENT (ovl))) |
03017874 MM |
2137 | /* There are no more conversion functions. */ |
2138 | break; | |
2139 | ||
2140 | /* Glue all these conversion functions together | |
2141 | with those we already have. */ | |
2142 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
2143 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
2144 | } | |
2145 | } | |
c8094d83 MS |
2146 | |
2147 | if (fns == NULL_TREE) | |
386b8a85 | 2148 | { |
0f51ccfc | 2149 | error ("no member function %qD declared in %qT", name, ctype); |
03017874 | 2150 | return error_mark_node; |
386b8a85 JM |
2151 | } |
2152 | else | |
2153 | TREE_OPERAND (declarator, 0) = fns; | |
2154 | } | |
c8094d83 | 2155 | |
e1467ff2 MM |
2156 | /* Figure out what exactly is being specialized at this point. |
2157 | Note that for an explicit instantiation, even one for a | |
38e01259 | 2158 | member function, we cannot tell apriori whether the |
e1467ff2 | 2159 | instantiation is for a member template, or just a member |
36a117a5 MM |
2160 | function of a template class. Even if a member template is |
2161 | being instantiated, the member template arguments may be | |
2162 | elided if they can be deduced from the rest of the | |
2163 | declaration. */ | |
e1467ff2 | 2164 | tmpl = determine_specialization (declarator, decl, |
c8094d83 | 2165 | &targs, |
5fe7b654 | 2166 | member_specialization, |
29a1da1c MM |
2167 | template_count, |
2168 | tsk); | |
c8094d83 | 2169 | |
03017874 MM |
2170 | if (!tmpl || tmpl == error_mark_node) |
2171 | /* We couldn't figure out what this declaration was | |
2172 | specializing. */ | |
2173 | return error_mark_node; | |
2174 | else | |
386b8a85 | 2175 | { |
25aab5d0 | 2176 | tree gen_tmpl = most_general_template (tmpl); |
36a117a5 | 2177 | |
e1467ff2 MM |
2178 | if (explicit_instantiation) |
2179 | { | |
03d0f4af | 2180 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
c8094d83 | 2181 | is done by do_decl_instantiation later. */ |
25aab5d0 MM |
2182 | |
2183 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
2184 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
2185 | ||
2186 | if (arg_depth > parm_depth) | |
2187 | { | |
2188 | /* If TMPL is not the most general template (for | |
2189 | example, if TMPL is a friend template that is | |
2190 | injected into namespace scope), then there will | |
dc957d14 | 2191 | be too many levels of TARGS. Remove some of them |
25aab5d0 MM |
2192 | here. */ |
2193 | int i; | |
2194 | tree new_targs; | |
2195 | ||
f31c0a32 | 2196 | new_targs = make_tree_vec (parm_depth); |
25aab5d0 MM |
2197 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) |
2198 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
2199 | = TREE_VEC_ELT (targs, i); | |
2200 | targs = new_targs; | |
2201 | } | |
c8094d83 | 2202 | |
3e4a3562 | 2203 | return instantiate_template (tmpl, targs, tf_error); |
e1467ff2 | 2204 | } |
74b846e0 | 2205 | |
6c07f448 KL |
2206 | /* If we thought that the DECL was a member function, but it |
2207 | turns out to be specializing a static member function, | |
4546865e | 2208 | make DECL a static member function as well. */ |
6c07f448 KL |
2209 | if (DECL_STATIC_FUNCTION_P (tmpl) |
2210 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
4546865e | 2211 | revert_static_member_fn (decl); |
6c07f448 | 2212 | |
f9a7ae04 | 2213 | /* If this is a specialization of a member template of a |
29a1da1c MM |
2214 | template class, we want to return the TEMPLATE_DECL, not |
2215 | the specialization of it. */ | |
74b846e0 MM |
2216 | if (tsk == tsk_template) |
2217 | { | |
2218 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
f9a7ae04 | 2219 | DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE; |
b190f239 NS |
2220 | if (have_def) |
2221 | { | |
f31686a3 RH |
2222 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
2223 | DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl)) | |
2224 | = DECL_SOURCE_LOCATION (decl); | |
08167d1c AO |
2225 | /* We want to use the argument list specified in the |
2226 | definition, not in the original declaration. */ | |
2227 | DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl)) | |
2228 | = DECL_ARGUMENTS (decl); | |
b190f239 | 2229 | } |
74b846e0 MM |
2230 | return tmpl; |
2231 | } | |
2232 | ||
36a117a5 | 2233 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
e1b3e07d | 2234 | DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); |
36a117a5 | 2235 | |
8afa707f MM |
2236 | /* Inherit default function arguments from the template |
2237 | DECL is specializing. */ | |
2238 | copy_default_args_to_explicit_spec (decl); | |
2239 | ||
c750255c MM |
2240 | /* This specialization has the same protection as the |
2241 | template it specializes. */ | |
2242 | TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); | |
2243 | TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); | |
b9e75696 | 2244 | |
736b8100 SB |
2245 | /* 7.1.1-1 [dcl.stc] |
2246 | ||
2247 | A storage-class-specifier shall not be specified in an | |
2248 | explicit specialization... | |
2249 | ||
2250 | The parser rejects these, so unless action is taken here, | |
2251 | explicit function specializations will always appear with | |
2252 | global linkage. | |
2253 | ||
2254 | The action recommended by the C++ CWG in response to C++ | |
2255 | defect report 605 is to make the storage class and linkage | |
2256 | of the explicit specialization match the templated function: | |
2257 | ||
2258 | http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605 | |
2259 | */ | |
2260 | if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl)) | |
2261 | { | |
2262 | tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl); | |
2263 | gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL); | |
2264 | ||
d732e98f | 2265 | /* This specialization has the same linkage and visibility as |
736b8100 SB |
2266 | the function template it specializes. */ |
2267 | TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func); | |
92d0af97 JJ |
2268 | if (! TREE_PUBLIC (decl)) |
2269 | { | |
2270 | DECL_INTERFACE_KNOWN (decl) = 1; | |
2271 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
2272 | } | |
736b8100 SB |
2273 | DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func); |
2274 | if (DECL_VISIBILITY_SPECIFIED (tmpl_func)) | |
2275 | { | |
2276 | DECL_VISIBILITY_SPECIFIED (decl) = 1; | |
2277 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func); | |
2278 | } | |
2279 | } | |
2280 | ||
9c12301f MM |
2281 | /* If DECL is a friend declaration, declared using an |
2282 | unqualified name, the namespace associated with DECL may | |
2283 | have been set incorrectly. For example, in: | |
3db45ab5 MS |
2284 | |
2285 | template <typename T> void f(T); | |
2286 | namespace N { | |
2287 | struct S { friend void f<int>(int); } | |
2288 | } | |
2289 | ||
2290 | we will have set the DECL_CONTEXT for the friend | |
2291 | declaration to N, rather than to the global namespace. */ | |
9c12301f MM |
2292 | if (DECL_NAMESPACE_SCOPE_P (decl)) |
2293 | DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl); | |
8d039470 | 2294 | |
386b8a85 | 2295 | if (is_friend && !have_def) |
36a117a5 MM |
2296 | /* This is not really a declaration of a specialization. |
2297 | It's just the name of an instantiation. But, it's not | |
2298 | a request for an instantiation, either. */ | |
fbf1c34b | 2299 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
08511114 KL |
2300 | else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) |
2301 | /* This is indeed a specialization. In case of constructors | |
2302 | and destructors, we need in-charge and not-in-charge | |
2303 | versions in V3 ABI. */ | |
2304 | clone_function_decl (decl, /*update_method_vec_p=*/0); | |
75650646 | 2305 | |
36a117a5 MM |
2306 | /* Register this specialization so that we can find it |
2307 | again. */ | |
d63d5d0c | 2308 | decl = register_specialization (decl, gen_tmpl, targs, is_friend); |
386b8a85 JM |
2309 | } |
2310 | } | |
c8094d83 | 2311 | |
e1467ff2 | 2312 | return decl; |
386b8a85 | 2313 | } |
75650646 | 2314 | |
75650646 MM |
2315 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
2316 | parameters. These are represented in the same format used for | |
2317 | DECL_TEMPLATE_PARMS. */ | |
2318 | ||
50bc768d | 2319 | int |
58f9752a | 2320 | comp_template_parms (const_tree parms1, const_tree parms2) |
75650646 | 2321 | { |
58f9752a KG |
2322 | const_tree p1; |
2323 | const_tree p2; | |
75650646 MM |
2324 | |
2325 | if (parms1 == parms2) | |
2326 | return 1; | |
2327 | ||
c8094d83 | 2328 | for (p1 = parms1, p2 = parms2; |
75650646 MM |
2329 | p1 != NULL_TREE && p2 != NULL_TREE; |
2330 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
2331 | { | |
2332 | tree t1 = TREE_VALUE (p1); | |
2333 | tree t2 = TREE_VALUE (p2); | |
2334 | int i; | |
2335 | ||
50bc768d NS |
2336 | gcc_assert (TREE_CODE (t1) == TREE_VEC); |
2337 | gcc_assert (TREE_CODE (t2) == TREE_VEC); | |
75650646 MM |
2338 | |
2339 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
2340 | return 0; | |
2341 | ||
c8094d83 | 2342 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) |
75650646 | 2343 | { |
0f67a82f LM |
2344 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); |
2345 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
42b304f1 | 2346 | |
0f67a82f LM |
2347 | /* If either of the template parameters are invalid, assume |
2348 | they match for the sake of error recovery. */ | |
2349 | if (parm1 == error_mark_node || parm2 == error_mark_node) | |
2350 | return 1; | |
75650646 MM |
2351 | |
2352 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
2353 | return 0; | |
2354 | ||
5d80a306 DG |
2355 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM |
2356 | && (TEMPLATE_TYPE_PARAMETER_PACK (parm1) | |
2357 | == TEMPLATE_TYPE_PARAMETER_PACK (parm2))) | |
75650646 | 2358 | continue; |
3bfdc719 | 2359 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
2360 | return 0; |
2361 | } | |
2362 | } | |
2363 | ||
2364 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
2365 | /* One set of parameters has more parameters lists than the | |
2366 | other. */ | |
2367 | return 0; | |
2368 | ||
2369 | return 1; | |
2370 | } | |
2371 | ||
5d80a306 DG |
2372 | /* Determine whether PARM is a parameter pack. */ |
2373 | bool | |
58f9752a | 2374 | template_parameter_pack_p (const_tree parm) |
5d80a306 DG |
2375 | { |
2376 | /* Determine if we have a non-type template parameter pack. */ | |
2377 | if (TREE_CODE (parm) == PARM_DECL) | |
2378 | return (DECL_TEMPLATE_PARM_P (parm) | |
2379 | && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))); | |
2380 | ||
2381 | /* If this is a list of template parameters, we could get a | |
2382 | TYPE_DECL or a TEMPLATE_DECL. */ | |
2383 | if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL) | |
2384 | parm = TREE_TYPE (parm); | |
2385 | ||
2386 | return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
2387 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) | |
2388 | && TEMPLATE_TYPE_PARAMETER_PACK (parm)); | |
2389 | } | |
2390 | ||
5d80a306 DG |
2391 | /* Determine whether ARGS describes a variadic template args list, |
2392 | i.e., one that is terminated by a template argument pack. */ | |
2393 | static bool | |
2394 | template_args_variadic_p (tree args) | |
2395 | { | |
2396 | int nargs; | |
2397 | tree last_parm; | |
2398 | ||
2399 | if (args == NULL_TREE) | |
2400 | return false; | |
2401 | ||
2402 | args = INNERMOST_TEMPLATE_ARGS (args); | |
2403 | nargs = TREE_VEC_LENGTH (args); | |
2404 | ||
2405 | if (nargs == 0) | |
2406 | return false; | |
2407 | ||
2408 | last_parm = TREE_VEC_ELT (args, nargs - 1); | |
2409 | ||
2410 | return ARGUMENT_PACK_P (last_parm); | |
2411 | } | |
2412 | ||
2413 | /* Generate a new name for the parameter pack name NAME (an | |
2414 | IDENTIFIER_NODE) that incorporates its */ | |
2415 | static tree | |
2416 | make_ith_pack_parameter_name (tree name, int i) | |
2417 | { | |
2418 | /* Munge the name to include the parameter index. */ | |
2419 | char numbuf[128]; | |
2420 | char* newname; | |
2421 | ||
2422 | sprintf(numbuf, "%i", i); | |
2423 | newname = (char*)alloca (IDENTIFIER_LENGTH (name) + strlen(numbuf) + 2); | |
2424 | sprintf(newname, "%s#%i", IDENTIFIER_POINTER (name), i); | |
2425 | return get_identifier (newname); | |
2426 | } | |
2427 | ||
1ad8aeeb | 2428 | /* Structure used to track the progress of find_parameter_packs_r. */ |
5d80a306 DG |
2429 | struct find_parameter_pack_data |
2430 | { | |
1ad8aeeb DG |
2431 | /* TREE_LIST that will contain all of the parameter packs found by |
2432 | the traversal. */ | |
5d80a306 | 2433 | tree* parameter_packs; |
1ad8aeeb DG |
2434 | |
2435 | /* Set of AST nodes that have been visited by the traversal. */ | |
5d80a306 DG |
2436 | struct pointer_set_t *visited; |
2437 | }; | |
2438 | ||
88b82314 | 2439 | /* Identifies all of the argument packs that occur in a template |
5d80a306 | 2440 | argument and appends them to the TREE_LIST inside DATA, which is a |
88b82314 | 2441 | find_parameter_pack_data structure. This is a subroutine of |
5d80a306 DG |
2442 | make_pack_expansion and uses_parameter_packs. */ |
2443 | static tree | |
2444 | find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data) | |
2445 | { | |
2446 | tree t = *tp; | |
2447 | struct find_parameter_pack_data* ppd = | |
2448 | (struct find_parameter_pack_data*)data; | |
1ad8aeeb | 2449 | bool parameter_pack_p = false; |
5d80a306 | 2450 | |
7b3e2d46 | 2451 | /* Don't visit nodes twice. */ |
1ad8aeeb | 2452 | if (pointer_set_contains (ppd->visited, *tp)) |
5d80a306 | 2453 | { |
1ad8aeeb DG |
2454 | *walk_subtrees = 0; |
2455 | return NULL_TREE; | |
5d80a306 DG |
2456 | } |
2457 | ||
1ad8aeeb DG |
2458 | /* Identify whether this is a parameter pack or not. */ |
2459 | switch (TREE_CODE (t)) | |
2460 | { | |
2461 | case TEMPLATE_PARM_INDEX: | |
2462 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
2463 | parameter_pack_p = true; | |
2464 | break; | |
2465 | ||
2466 | case TEMPLATE_TYPE_PARM: | |
2467 | case TEMPLATE_TEMPLATE_PARM: | |
2468 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
2469 | parameter_pack_p = true; | |
2470 | break; | |
2471 | ||
2472 | case PARM_DECL: | |
2473 | if (FUNCTION_PARAMETER_PACK_P (t)) | |
2474 | { | |
2475 | /* We don't want to walk into the type of a PARM_DECL, | |
2476 | because we don't want to see the type parameter pack. */ | |
2477 | *walk_subtrees = 0; | |
2478 | parameter_pack_p = true; | |
2479 | } | |
2480 | break; | |
2481 | ||
2482 | default: | |
2483 | /* Not a parameter pack. */ | |
2484 | break; | |
2485 | } | |
2486 | ||
2487 | if (parameter_pack_p) | |
2488 | { | |
2489 | /* Add this parameter pack to the list. */ | |
2490 | *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs); | |
1ad8aeeb | 2491 | } |
7b3e2d46 DG |
2492 | |
2493 | /* Make sure we do not visit this node again. */ | |
2494 | pointer_set_insert (ppd->visited, *tp); | |
1ad8aeeb DG |
2495 | |
2496 | if (TYPE_P (t)) | |
2497 | cp_walk_tree (&TYPE_CONTEXT (t), | |
2498 | &find_parameter_packs_r, ppd, NULL); | |
2499 | ||
5d80a306 DG |
2500 | /* This switch statement will return immediately if we don't find a |
2501 | parameter pack. */ | |
2502 | switch (TREE_CODE (t)) | |
2503 | { | |
2504 | case TEMPLATE_PARM_INDEX: | |
5d80a306 DG |
2505 | return NULL_TREE; |
2506 | ||
2507 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
1ad8aeeb DG |
2508 | /* Check the template itself. */ |
2509 | cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)), | |
2510 | &find_parameter_packs_r, ppd, NULL); | |
5d80a306 | 2511 | /* Check the template arguments. */ |
14588106 | 2512 | cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd, |
1ad8aeeb | 2513 | NULL); |
5d80a306 | 2514 | *walk_subtrees = 0; |
1ad8aeeb | 2515 | return NULL_TREE; |
5d80a306 DG |
2516 | |
2517 | case TEMPLATE_TYPE_PARM: | |
2518 | case TEMPLATE_TEMPLATE_PARM: | |
5d80a306 DG |
2519 | return NULL_TREE; |
2520 | ||
2521 | case PARM_DECL: | |
5d80a306 DG |
2522 | return NULL_TREE; |
2523 | ||
2524 | case RECORD_TYPE: | |
2525 | if (TYPE_PTRMEMFUNC_P (t)) | |
2526 | return NULL_TREE; | |
2527 | /* Fall through. */ | |
2528 | ||
2529 | case UNION_TYPE: | |
2530 | case ENUMERAL_TYPE: | |
2531 | if (TYPE_TEMPLATE_INFO (t)) | |
1ad8aeeb DG |
2532 | cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t)), |
2533 | &find_parameter_packs_r, ppd, NULL); | |
5d80a306 DG |
2534 | |
2535 | *walk_subtrees = 0; | |
2536 | return NULL_TREE; | |
2537 | ||
2538 | case TEMPLATE_DECL: | |
1ad8aeeb DG |
2539 | cp_walk_tree (&TREE_TYPE (t), |
2540 | &find_parameter_packs_r, ppd, NULL); | |
5d80a306 | 2541 | return NULL_TREE; |
e1a18c68 DG |
2542 | |
2543 | case TYPENAME_TYPE: | |
2544 | cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r, | |
1ad8aeeb | 2545 | ppd, NULL); |
e1a18c68 DG |
2546 | *walk_subtrees = 0; |
2547 | return NULL_TREE; | |
e1a18c68 | 2548 | |
5d80a306 DG |
2549 | case TYPE_PACK_EXPANSION: |
2550 | case EXPR_PACK_EXPANSION: | |
2551 | *walk_subtrees = 0; | |
2552 | return NULL_TREE; | |
2553 | ||
d393153e | 2554 | case INTEGER_TYPE: |
14588106 | 2555 | cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r, |
1ad8aeeb | 2556 | ppd, NULL); |
d393153e DG |
2557 | *walk_subtrees = 0; |
2558 | return NULL_TREE; | |
2559 | ||
4439d02f DG |
2560 | case IDENTIFIER_NODE: |
2561 | cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd, NULL); | |
2562 | *walk_subtrees = 0; | |
2563 | return NULL_TREE; | |
2564 | ||
5d80a306 DG |
2565 | default: |
2566 | return NULL_TREE; | |
2567 | } | |
5d80a306 DG |
2568 | |
2569 | return NULL_TREE; | |
2570 | } | |
2571 | ||
2572 | /* Determines if the expression or type T uses any parameter packs. */ | |
2573 | bool | |
2574 | uses_parameter_packs (tree t) | |
2575 | { | |
2576 | tree parameter_packs = NULL_TREE; | |
2577 | struct find_parameter_pack_data ppd; | |
2578 | ppd.parameter_packs = ¶meter_packs; | |
2579 | ppd.visited = pointer_set_create (); | |
1ad8aeeb | 2580 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, NULL); |
30bcc028 | 2581 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
2582 | return parameter_packs != NULL_TREE; |
2583 | } | |
2584 | ||
2585 | /* Turn ARG, which may be an expression, type, or a TREE_LIST | |
2586 | representation a base-class initializer into a parameter pack | |
2587 | expansion. If all goes well, the resulting node will be an | |
2588 | EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST, | |
2589 | respectively. */ | |
2590 | tree | |
2591 | make_pack_expansion (tree arg) | |
2592 | { | |
2593 | tree result; | |
2594 | tree parameter_packs = NULL_TREE; | |
2595 | bool for_types = false; | |
2596 | struct find_parameter_pack_data ppd; | |
2597 | ||
2598 | if (!arg || arg == error_mark_node) | |
2599 | return arg; | |
2600 | ||
2601 | if (TREE_CODE (arg) == TREE_LIST) | |
2602 | { | |
2603 | /* The only time we will see a TREE_LIST here is for a base | |
2604 | class initializer. In this case, the TREE_PURPOSE will be a | |
2605 | _TYPE node (representing the base class expansion we're | |
2606 | initializing) and the TREE_VALUE will be a TREE_LIST | |
2607 | containing the initialization arguments. | |
2608 | ||
2609 | The resulting expansion looks somewhat different from most | |
2610 | expansions. Rather than returning just one _EXPANSION, we | |
2611 | return a TREE_LIST whose TREE_PURPOSE is a | |
2612 | TYPE_PACK_EXPANSION containing the bases that will be | |
2613 | initialized. The TREE_VALUE will be identical to the | |
2614 | original TREE_VALUE, which is a list of arguments that will | |
2615 | be passed to each base. We do not introduce any new pack | |
2616 | expansion nodes into the TREE_VALUE (although it is possible | |
2617 | that some already exist), because the TREE_PURPOSE and | |
2618 | TREE_VALUE all need to be expanded together with the same | |
2619 | _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the | |
2620 | resulting TREE_PURPOSE will mention the parameter packs in | |
2621 | both the bases and the arguments to the bases. */ | |
2622 | tree purpose; | |
2623 | tree value; | |
2624 | tree parameter_packs = NULL_TREE; | |
2625 | ||
2626 | /* Determine which parameter packs will be used by the base | |
2627 | class expansion. */ | |
2628 | ppd.visited = pointer_set_create (); | |
2629 | ppd.parameter_packs = ¶meter_packs; | |
14588106 | 2630 | cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r, |
1ad8aeeb | 2631 | &ppd, NULL); |
5d80a306 DG |
2632 | |
2633 | if (parameter_packs == NULL_TREE) | |
2634 | { | |
2635 | error ("base initializer expansion %<%T%> contains no parameter packs", arg); | |
30bcc028 | 2636 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
2637 | return error_mark_node; |
2638 | } | |
2639 | ||
2640 | if (TREE_VALUE (arg) != void_type_node) | |
2641 | { | |
2642 | /* Collect the sets of parameter packs used in each of the | |
2643 | initialization arguments. */ | |
2644 | for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value)) | |
2645 | { | |
2646 | /* Determine which parameter packs will be expanded in this | |
2647 | argument. */ | |
14588106 | 2648 | cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r, |
1ad8aeeb | 2649 | &ppd, NULL); |
5d80a306 DG |
2650 | } |
2651 | } | |
2652 | ||
30bcc028 DG |
2653 | pointer_set_destroy (ppd.visited); |
2654 | ||
5d80a306 DG |
2655 | /* Create the pack expansion type for the base type. */ |
2656 | purpose = make_node (TYPE_PACK_EXPANSION); | |
2657 | SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg)); | |
2658 | PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs; | |
2659 | ||
2660 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
2661 | they will rarely be compared to anything. */ | |
2662 | SET_TYPE_STRUCTURAL_EQUALITY (purpose); | |
2663 | ||
2664 | return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE); | |
2665 | } | |
2666 | ||
2667 | if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL) | |
2668 | for_types = true; | |
2669 | ||
2670 | /* Build the PACK_EXPANSION_* node. */ | |
2671 | result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION); | |
2672 | SET_PACK_EXPANSION_PATTERN (result, arg); | |
2673 | if (TREE_CODE (result) == EXPR_PACK_EXPANSION) | |
2674 | { | |
2675 | /* Propagate type and const-expression information. */ | |
2676 | TREE_TYPE (result) = TREE_TYPE (arg); | |
2677 | TREE_CONSTANT (result) = TREE_CONSTANT (arg); | |
2678 | } | |
2679 | else | |
2680 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
2681 | they will rarely be compared to anything. */ | |
2682 | SET_TYPE_STRUCTURAL_EQUALITY (result); | |
2683 | ||
2684 | /* Determine which parameter packs will be expanded. */ | |
2685 | ppd.parameter_packs = ¶meter_packs; | |
2686 | ppd.visited = pointer_set_create (); | |
1ad8aeeb | 2687 | cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, NULL); |
30bcc028 | 2688 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
2689 | |
2690 | /* Make sure we found some parameter packs. */ | |
2691 | if (parameter_packs == NULL_TREE) | |
2692 | { | |
2693 | if (TYPE_P (arg)) | |
2694 | error ("expansion pattern %<%T%> contains no argument packs", arg); | |
2695 | else | |
2696 | error ("expansion pattern %<%E%> contains no argument packs", arg); | |
2697 | return error_mark_node; | |
2698 | } | |
2699 | PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs; | |
2700 | ||
2701 | return result; | |
2702 | } | |
2703 | ||
2704 | /* Checks T for any "bare" parameter packs, which have not yet been | |
2705 | expanded, and issues an error if any are found. This operation can | |
2706 | only be done on full expressions or types (e.g., an expression | |
2707 | statement, "if" condition, etc.), because we could have expressions like: | |
2708 | ||
2709 | foo(f(g(h(args)))...) | |
2710 | ||
2711 | where "args" is a parameter pack. check_for_bare_parameter_packs | |
2712 | should not be called for the subexpressions args, h(args), | |
2713 | g(h(args)), or f(g(h(args))), because we would produce erroneous | |
b1d7b1c0 DG |
2714 | error messages. |
2715 | ||
4439d02f DG |
2716 | Returns TRUE and emits an error if there were bare parameter packs, |
2717 | returns FALSE otherwise. */ | |
b1d7b1c0 | 2718 | bool |
7b3e2d46 | 2719 | check_for_bare_parameter_packs (tree t) |
5d80a306 DG |
2720 | { |
2721 | tree parameter_packs = NULL_TREE; | |
2722 | struct find_parameter_pack_data ppd; | |
2723 | ||
7b3e2d46 | 2724 | if (!processing_template_decl || !t || t == error_mark_node) |
4439d02f | 2725 | return false; |
5d80a306 | 2726 | |
7b3e2d46 DG |
2727 | if (TREE_CODE (t) == TYPE_DECL) |
2728 | t = TREE_TYPE (t); | |
5d80a306 DG |
2729 | |
2730 | ppd.parameter_packs = ¶meter_packs; | |
2731 | ppd.visited = pointer_set_create (); | |
7b3e2d46 | 2732 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, NULL); |
30bcc028 | 2733 | pointer_set_destroy (ppd.visited); |
5d80a306 | 2734 | |
b1d7b1c0 DG |
2735 | if (parameter_packs) |
2736 | { | |
2737 | error ("parameter packs not expanded with `...':"); | |
2738 | while (parameter_packs) | |
2739 | { | |
2740 | tree pack = TREE_VALUE (parameter_packs); | |
2741 | tree name = NULL_TREE; | |
2742 | ||
2743 | if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM | |
2744 | || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM) | |
2745 | name = TYPE_NAME (pack); | |
2746 | else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX) | |
2747 | name = DECL_NAME (TEMPLATE_PARM_DECL (pack)); | |
2748 | else | |
2749 | name = DECL_NAME (pack); | |
1ad8aeeb DG |
2750 | |
2751 | if (name) | |
2752 | inform (" %qD", name); | |
2753 | else | |
2754 | inform (" <anonymous>"); | |
b1d7b1c0 DG |
2755 | |
2756 | parameter_packs = TREE_CHAIN (parameter_packs); | |
2757 | } | |
2758 | ||
1ad8aeeb DG |
2759 | /* Clean up any references to these parameter packs within the |
2760 | tree. */ | |
2761 | ppd.parameter_packs = ¶meter_packs; | |
2762 | ppd.visited = pointer_set_create (); | |
7b3e2d46 | 2763 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, NULL); |
1ad8aeeb DG |
2764 | pointer_set_destroy (ppd.visited); |
2765 | ||
4439d02f | 2766 | return true; |
b1d7b1c0 DG |
2767 | } |
2768 | ||
4439d02f | 2769 | return false; |
5d80a306 DG |
2770 | } |
2771 | ||
2772 | /* Expand any parameter packs that occur in the template arguments in | |
2773 | ARGS. */ | |
2774 | tree | |
2775 | expand_template_argument_pack (tree args) | |
2776 | { | |
2777 | tree result_args = NULL_TREE; | |
2778 | int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0; | |
2779 | int num_result_args = -1; | |
2780 | ||
2781 | /* First, determine if we need to expand anything, and the number of | |
2782 | slots we'll need. */ | |
2783 | for (in_arg = 0; in_arg < nargs; ++in_arg) | |
2784 | { | |
2785 | tree arg = TREE_VEC_ELT (args, in_arg); | |
2786 | if (ARGUMENT_PACK_P (arg)) | |
2787 | { | |
2788 | int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)); | |
2789 | if (num_result_args < 0) | |
2790 | num_result_args = in_arg + num_packed; | |
2791 | else | |
2792 | num_result_args += num_packed; | |
2793 | } | |
2794 | else | |
2795 | { | |
2796 | if (num_result_args >= 0) | |
2797 | num_result_args++; | |
2798 | } | |
2799 | } | |
2800 | ||
2801 | /* If no expansion is necessary, we're done. */ | |
2802 | if (num_result_args < 0) | |
2803 | return args; | |
2804 | ||
2805 | /* Expand arguments. */ | |
2806 | result_args = make_tree_vec (num_result_args); | |
2807 | for (in_arg = 0; in_arg < nargs; ++in_arg) | |
2808 | { | |
2809 | tree arg = TREE_VEC_ELT (args, in_arg); | |
2810 | if (ARGUMENT_PACK_P (arg)) | |
2811 | { | |
2812 | tree packed = ARGUMENT_PACK_ARGS (arg); | |
2813 | int i, num_packed = TREE_VEC_LENGTH (packed); | |
2814 | for (i = 0; i < num_packed; ++i, ++out_arg) | |
2815 | TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i); | |
2816 | } | |
2817 | else | |
2818 | { | |
2819 | TREE_VEC_ELT (result_args, out_arg) = arg; | |
2820 | ++out_arg; | |
2821 | } | |
2822 | } | |
2823 | ||
2824 | return result_args; | |
2825 | } | |
2826 | ||
f3400fe2 JM |
2827 | /* Complain if DECL shadows a template parameter. |
2828 | ||
2829 | [temp.local]: A template-parameter shall not be redeclared within its | |
2830 | scope (including nested scopes). */ | |
2831 | ||
2832 | void | |
3a978d72 | 2833 | check_template_shadow (tree decl) |
f3400fe2 | 2834 | { |
8f032717 MM |
2835 | tree olddecl; |
2836 | ||
b5d9b9ab MM |
2837 | /* If we're not in a template, we can't possibly shadow a template |
2838 | parameter. */ | |
2839 | if (!current_template_parms) | |
2840 | return; | |
2841 | ||
2842 | /* Figure out what we're shadowing. */ | |
8f032717 MM |
2843 | if (TREE_CODE (decl) == OVERLOAD) |
2844 | decl = OVL_CURRENT (decl); | |
90ea9897 | 2845 | olddecl = innermost_non_namespace_value (DECL_NAME (decl)); |
f3400fe2 | 2846 | |
b5d9b9ab MM |
2847 | /* If there's no previous binding for this name, we're not shadowing |
2848 | anything, let alone a template parameter. */ | |
2849 | if (!olddecl) | |
2850 | return; | |
2851 | ||
2852 | /* If we're not shadowing a template parameter, we're done. Note | |
2853 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
2854 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
2855 | node. */ | |
2f939d94 | 2856 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) |
b5d9b9ab MM |
2857 | return; |
2858 | ||
2859 | /* We check for decl != olddecl to avoid bogus errors for using a | |
2860 | name inside a class. We check TPFI to avoid duplicate errors for | |
2861 | inline member templates. */ | |
c8094d83 | 2862 | if (decl == olddecl |
b5d9b9ab MM |
2863 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) |
2864 | return; | |
2865 | ||
dee15844 JM |
2866 | error ("declaration of %q+#D", decl); |
2867 | error (" shadows template parm %q+#D", olddecl); | |
f3400fe2 | 2868 | } |
22a7be53 | 2869 | |
f3400fe2 | 2870 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
2871 | ORIG_LEVEL, DECL, and TYPE. */ |
2872 | ||
2873 | static tree | |
c8094d83 | 2874 | build_template_parm_index (int index, |
0cbd7506 MS |
2875 | int level, |
2876 | int orig_level, | |
2877 | tree decl, | |
2878 | tree type) | |
f84b4be9 JM |
2879 | { |
2880 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
2881 | TEMPLATE_PARM_IDX (t) = index; | |
2882 | TEMPLATE_PARM_LEVEL (t) = level; | |
2883 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
2884 | TEMPLATE_PARM_DECL (t) = decl; | |
2885 | TREE_TYPE (t) = type; | |
3e4a3562 | 2886 | TREE_CONSTANT (t) = TREE_CONSTANT (decl); |
6de9cd9a | 2887 | TREE_INVARIANT (t) = TREE_INVARIANT (decl); |
3e4a3562 | 2888 | TREE_READONLY (t) = TREE_READONLY (decl); |
f84b4be9 JM |
2889 | |
2890 | return t; | |
2891 | } | |
2892 | ||
06d40de8 | 2893 | /* Find the canonical type parameter for the given template type |
c3e188fc KH |
2894 | parameter. Returns the canonical type parameter, which may be TYPE |
2895 | if no such parameter existed. */ | |
06d40de8 DG |
2896 | static tree |
2897 | canonical_type_parameter (tree type) | |
2898 | { | |
2899 | tree list; | |
2900 | int idx = TEMPLATE_TYPE_IDX (type); | |
2901 | if (!canonical_template_parms) | |
2902 | canonical_template_parms = VEC_alloc (tree, gc, idx+1); | |
2903 | ||
2904 | while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx) | |
2905 | VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE); | |
2906 | ||
2907 | list = VEC_index (tree, canonical_template_parms, idx); | |
2908 | while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL)) | |
2909 | list = TREE_CHAIN (list); | |
2910 | ||
2911 | if (list) | |
2912 | return TREE_VALUE (list); | |
2913 | else | |
2914 | { | |
2915 | VEC_replace(tree, canonical_template_parms, idx, | |
2916 | tree_cons (NULL_TREE, type, | |
2917 | VEC_index (tree, canonical_template_parms, idx))); | |
2918 | return type; | |
2919 | } | |
2920 | } | |
2921 | ||
f84b4be9 | 2922 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 2923 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
2924 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
2925 | new one is created. */ | |
2926 | ||
c8094d83 | 2927 | static tree |
ef3b7b17 JM |
2928 | reduce_template_parm_level (tree index, tree type, int levels, tree args, |
2929 | tsubst_flags_t complain) | |
f84b4be9 JM |
2930 | { |
2931 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
2932 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 2933 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 | 2934 | { |
3e4a3562 NS |
2935 | tree orig_decl = TEMPLATE_PARM_DECL (index); |
2936 | tree decl, t; | |
c8094d83 | 2937 | |
3e4a3562 NS |
2938 | decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type); |
2939 | TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); | |
6de9cd9a | 2940 | TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl); |
3e4a3562 NS |
2941 | TREE_READONLY (decl) = TREE_READONLY (orig_decl); |
2942 | DECL_ARTIFICIAL (decl) = 1; | |
2943 | SET_DECL_TEMPLATE_PARM_P (decl); | |
c8094d83 | 2944 | |
3e4a3562 | 2945 | t = build_template_parm_index (TEMPLATE_PARM_IDX (index), |
93cdc044 | 2946 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
2947 | TEMPLATE_PARM_ORIG_LEVEL (index), |
2948 | decl, type); | |
2949 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
5d80a306 DG |
2950 | TEMPLATE_PARM_PARAMETER_PACK (t) |
2951 | = TEMPLATE_PARM_PARAMETER_PACK (index); | |
cae40af6 | 2952 | |
820cc88f | 2953 | /* Template template parameters need this. */ |
ef3b7b17 JM |
2954 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
2955 | DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms | |
2956 | (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)), | |
2957 | args, complain); | |
f84b4be9 JM |
2958 | } |
2959 | ||
2960 | return TEMPLATE_PARM_DESCENDANTS (index); | |
2961 | } | |
2962 | ||
a14de1af | 2963 | /* Process information from new template parameter PARM and append it to the |
058b15c1 | 2964 | LIST being built. This new parameter is a non-type parameter iff |
5d80a306 DG |
2965 | IS_NON_TYPE is true. This new parameter is a parameter |
2966 | pack iff IS_PARAMETER_PACK is true. */ | |
e92cc029 | 2967 | |
8d08fdba | 2968 | tree |
5d80a306 DG |
2969 | process_template_parm (tree list, tree parm, bool is_non_type, |
2970 | bool is_parameter_pack) | |
8d08fdba | 2971 | { |
8d08fdba | 2972 | tree decl = 0; |
a292b002 | 2973 | tree defval; |
0f67a82f | 2974 | tree err_parm_list; |
d47e3adf | 2975 | int idx = 0; |
f84b4be9 | 2976 | |
50bc768d | 2977 | gcc_assert (TREE_CODE (parm) == TREE_LIST); |
a292b002 | 2978 | defval = TREE_PURPOSE (parm); |
5566b478 MS |
2979 | |
2980 | if (list) | |
2981 | { | |
d47e3adf LM |
2982 | tree p = tree_last (list); |
2983 | ||
0f67a82f | 2984 | if (p && TREE_VALUE (p) != error_mark_node) |
d47e3adf LM |
2985 | { |
2986 | p = TREE_VALUE (p); | |
2987 | if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) | |
2988 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
2989 | else | |
2990 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); | |
2991 | } | |
5566b478 | 2992 | |
5566b478 MS |
2993 | ++idx; |
2994 | } | |
2995 | else | |
2996 | idx = 0; | |
2997 | ||
058b15c1 | 2998 | if (is_non_type) |
8d08fdba | 2999 | { |
058b15c1 MM |
3000 | parm = TREE_VALUE (parm); |
3001 | ||
833aa4c4 | 3002 | SET_DECL_TEMPLATE_PARM_P (parm); |
d490621d | 3003 | |
620188c9 | 3004 | if (TREE_TYPE (parm) == error_mark_node) |
0f67a82f LM |
3005 | { |
3006 | err_parm_list = build_tree_list (defval, parm); | |
3007 | TREE_VALUE (err_parm_list) = error_mark_node; | |
3008 | return chainon (list, err_parm_list); | |
3009 | } | |
620188c9 VR |
3010 | else |
3011 | { | |
3012 | /* [temp.param] | |
d490621d | 3013 | |
620188c9 VR |
3014 | The top-level cv-qualifiers on the template-parameter are |
3015 | ignored when determining its type. */ | |
3016 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
3017 | if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) | |
0f67a82f LM |
3018 | { |
3019 | err_parm_list = build_tree_list (defval, parm); | |
3020 | TREE_VALUE (err_parm_list) = error_mark_node; | |
3021 | return chainon (list, err_parm_list); | |
3022 | } | |
5d80a306 DG |
3023 | |
3024 | if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack) | |
3025 | { | |
3026 | /* This template parameter is not a parameter pack, but it | |
3027 | should be. Complain about "bare" parameter packs. */ | |
7b3e2d46 | 3028 | check_for_bare_parameter_packs (TREE_TYPE (parm)); |
5d80a306 DG |
3029 | |
3030 | /* Recover by calling this a parameter pack. */ | |
3031 | is_parameter_pack = true; | |
3032 | } | |
620188c9 | 3033 | } |
d490621d | 3034 | |
8d08fdba | 3035 | /* A template parameter is not modifiable. */ |
6de9cd9a DN |
3036 | TREE_CONSTANT (parm) = 1; |
3037 | TREE_INVARIANT (parm) = 1; | |
3038 | TREE_READONLY (parm) = 1; | |
8d08fdba | 3039 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
6de9cd9a DN |
3040 | TREE_CONSTANT (decl) = 1; |
3041 | TREE_INVARIANT (decl) = 1; | |
3042 | TREE_READONLY (decl) = 1; | |
c8094d83 | 3043 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
f84b4be9 JM |
3044 | = build_template_parm_index (idx, processing_template_decl, |
3045 | processing_template_decl, | |
3046 | decl, TREE_TYPE (parm)); | |
5d80a306 DG |
3047 | |
3048 | TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)) | |
3049 | = is_parameter_pack; | |
8d08fdba MS |
3050 | } |
3051 | else | |
3052 | { | |
73b0fce8 | 3053 | tree t; |
058b15c1 | 3054 | parm = TREE_VALUE (TREE_VALUE (parm)); |
c8094d83 | 3055 | |
73b0fce8 KL |
3056 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) |
3057 | { | |
33848bb0 | 3058 | t = make_aggr_type (TEMPLATE_TEMPLATE_PARM); |
c8094d83 | 3059 | /* This is for distinguishing between real templates and template |
73b0fce8 KL |
3060 | template parameters */ |
3061 | TREE_TYPE (parm) = t; | |
3062 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
3063 | decl = parm; | |
3064 | } | |
3065 | else | |
3066 | { | |
33848bb0 | 3067 | t = make_aggr_type (TEMPLATE_TYPE_PARM); |
f4f206f4 | 3068 | /* parm is either IDENTIFIER_NODE or NULL_TREE. */ |
73b0fce8 KL |
3069 | decl = build_decl (TYPE_DECL, parm, t); |
3070 | } | |
c8094d83 | 3071 | |
d2e5ee5c MS |
3072 | TYPE_NAME (t) = decl; |
3073 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 3074 | parm = decl; |
f84b4be9 | 3075 | TEMPLATE_TYPE_PARM_INDEX (t) |
c8094d83 | 3076 | = build_template_parm_index (idx, processing_template_decl, |
f84b4be9 JM |
3077 | processing_template_decl, |
3078 | decl, TREE_TYPE (parm)); | |
5d80a306 | 3079 | TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack; |
06d40de8 | 3080 | TYPE_CANONICAL (t) = canonical_type_parameter (t); |
8d08fdba | 3081 | } |
c727aa5e | 3082 | DECL_ARTIFICIAL (decl) = 1; |
cd9f6678 | 3083 | SET_DECL_TEMPLATE_PARM_P (decl); |
8d08fdba | 3084 | pushdecl (decl); |
a292b002 | 3085 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
3086 | return chainon (list, parm); |
3087 | } | |
3088 | ||
3089 | /* The end of a template parameter list has been reached. Process the | |
3090 | tree list into a parameter vector, converting each parameter into a more | |
3091 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
3092 | as PARM_DECLs. */ | |
3093 | ||
3094 | tree | |
3a978d72 | 3095 | end_template_parm_list (tree parms) |
8d08fdba | 3096 | { |
5566b478 | 3097 | int nparms; |
9471d3e2 | 3098 | tree parm, next; |
5566b478 MS |
3099 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
3100 | ||
5566b478 | 3101 | current_template_parms |
4890c2f4 | 3102 | = tree_cons (size_int (processing_template_decl), |
5566b478 | 3103 | saved_parmlist, current_template_parms); |
8d08fdba | 3104 | |
9471d3e2 NS |
3105 | for (parm = parms, nparms = 0; parm; parm = next, nparms++) |
3106 | { | |
3107 | next = TREE_CHAIN (parm); | |
3108 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; | |
3109 | TREE_CHAIN (parm) = NULL_TREE; | |
3110 | } | |
a292b002 | 3111 | |
67ffc812 MM |
3112 | --processing_template_parmlist; |
3113 | ||
8d08fdba MS |
3114 | return saved_parmlist; |
3115 | } | |
3116 | ||
5566b478 MS |
3117 | /* end_template_decl is called after a template declaration is seen. */ |
3118 | ||
8d08fdba | 3119 | void |
3a978d72 | 3120 | end_template_decl (void) |
8d08fdba | 3121 | { |
386b8a85 JM |
3122 | reset_specialization (); |
3123 | ||
5156628f | 3124 | if (! processing_template_decl) |
73aad9b9 JM |
3125 | return; |
3126 | ||
5566b478 | 3127 | /* This matches the pushlevel in begin_template_parm_list. */ |
74b846e0 | 3128 | finish_scope (); |
8d08fdba | 3129 | |
5566b478 MS |
3130 | --processing_template_decl; |
3131 | current_template_parms = TREE_CHAIN (current_template_parms); | |
5566b478 | 3132 | } |
8d08fdba | 3133 | |
03c17ccd MM |
3134 | /* Within the declaration of a template, return all levels of template |
3135 | parameters that apply. The template parameters are represented as | |
3136 | a TREE_VEC, in the form documented in cp-tree.h for template | |
3137 | arguments. */ | |
9a3b49ac | 3138 | |
b5791fdc | 3139 | static tree |
3a978d72 | 3140 | current_template_args (void) |
5566b478 | 3141 | { |
36a117a5 | 3142 | tree header; |
b370501f | 3143 | tree args = NULL_TREE; |
36a117a5 | 3144 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
3145 | int l = length; |
3146 | ||
36a117a5 MM |
3147 | /* If there is only one level of template parameters, we do not |
3148 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
3149 | TREE_VEC containing the arguments. */ | |
3150 | if (length > 1) | |
3151 | args = make_tree_vec (length); | |
3152 | ||
3153 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 3154 | { |
5566b478 | 3155 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
3156 | int i; |
3157 | ||
5566b478 | 3158 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 3159 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 3160 | { |
98c1c668 JM |
3161 | tree t = TREE_VEC_ELT (a, i); |
3162 | ||
36a117a5 | 3163 | /* T will be a list if we are called from within a |
98c1c668 JM |
3164 | begin/end_template_parm_list pair, but a vector directly |
3165 | if within a begin/end_member_template_processing pair. */ | |
c8094d83 | 3166 | if (TREE_CODE (t) == TREE_LIST) |
98c1c668 JM |
3167 | { |
3168 | t = TREE_VALUE (t); | |
c8094d83 | 3169 | |
0b93f014 | 3170 | if (!error_operand_p (t)) |
0f67a82f LM |
3171 | { |
3172 | if (TREE_CODE (t) == TYPE_DECL | |
3173 | || TREE_CODE (t) == TEMPLATE_DECL) | |
5d80a306 DG |
3174 | { |
3175 | t = TREE_TYPE (t); | |
3176 | ||
3177 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
3178 | { | |
3179 | /* Turn this argument into a TYPE_ARGUMENT_PACK | |
3180 | with a single element, which expands T. */ | |
3181 | tree vec = make_tree_vec (1); | |
3182 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3183 | ||
3184 | t = make_node (TYPE_ARGUMENT_PACK); | |
3185 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3186 | } | |
3187 | } | |
3188 | else | |
3189 | { | |
3190 | t = DECL_INITIAL (t); | |
3191 | ||
3192 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
3193 | { | |
3194 | /* Turn this argument into a NONTYPE_ARGUMENT_PACK | |
3195 | with a single element, which expands T. */ | |
3196 | tree vec = make_tree_vec (1); | |
3197 | tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t)); | |
3198 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3199 | ||
3200 | t = make_node (NONTYPE_ARGUMENT_PACK); | |
3201 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3202 | TREE_TYPE (t) = type; | |
3203 | } | |
3204 | } | |
bf98d3b6 | 3205 | TREE_VEC_ELT (a, i) = t; |
5d80a306 | 3206 | } |
98c1c668 | 3207 | } |
5566b478 | 3208 | } |
36a117a5 MM |
3209 | |
3210 | if (length > 1) | |
3211 | TREE_VEC_ELT (args, --l) = a; | |
3212 | else | |
3213 | args = a; | |
8d08fdba MS |
3214 | } |
3215 | ||
9a3b49ac MS |
3216 | return args; |
3217 | } | |
75650646 | 3218 | |
e1467ff2 | 3219 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
c7222c02 | 3220 | template PARMS. If MEMBER_TEMPLATE_P is true, the new template is |
ee81147e | 3221 | a member template. Used by push_template_decl below. */ |
e1467ff2 | 3222 | |
75650646 | 3223 | static tree |
c7222c02 | 3224 | build_template_decl (tree decl, tree parms, bool member_template_p) |
75650646 MM |
3225 | { |
3226 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
3227 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
3228 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
c7222c02 | 3229 | DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; |
75650646 MM |
3230 | if (DECL_LANG_SPECIFIC (decl)) |
3231 | { | |
8f032717 MM |
3232 | DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); |
3233 | DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); | |
c8460010 | 3234 | DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl); |
11f98788 | 3235 | DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); |
1f6e1acc AS |
3236 | DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl); |
3237 | if (DECL_OVERLOADED_OPERATOR_P (decl)) | |
c8094d83 | 3238 | SET_OVERLOADED_OPERATOR_CODE (tmpl, |
1f6e1acc | 3239 | DECL_OVERLOADED_OPERATOR_P (decl)); |
75650646 MM |
3240 | } |
3241 | ||
3242 | return tmpl; | |
3243 | } | |
3244 | ||
050367a3 MM |
3245 | struct template_parm_data |
3246 | { | |
6c30752f MM |
3247 | /* The level of the template parameters we are currently |
3248 | processing. */ | |
050367a3 | 3249 | int level; |
6c30752f MM |
3250 | |
3251 | /* The index of the specialization argument we are currently | |
3252 | processing. */ | |
3253 | int current_arg; | |
3254 | ||
3255 | /* An array whose size is the number of template parameters. The | |
838dfd8a | 3256 | elements are nonzero if the parameter has been used in any one |
6c30752f | 3257 | of the arguments processed so far. */ |
050367a3 | 3258 | int* parms; |
6c30752f MM |
3259 | |
3260 | /* An array whose size is the number of template arguments. The | |
838dfd8a | 3261 | elements are nonzero if the argument makes use of template |
6c30752f MM |
3262 | parameters of this level. */ |
3263 | int* arg_uses_template_parms; | |
050367a3 MM |
3264 | }; |
3265 | ||
3266 | /* Subroutine of push_template_decl used to see if each template | |
3267 | parameter in a partial specialization is used in the explicit | |
3268 | argument list. If T is of the LEVEL given in DATA (which is | |
3269 | treated as a template_parm_data*), then DATA->PARMS is marked | |
3270 | appropriately. */ | |
3271 | ||
3272 | static int | |
3a978d72 | 3273 | mark_template_parm (tree t, void* data) |
050367a3 MM |
3274 | { |
3275 | int level; | |
3276 | int idx; | |
3277 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
3278 | ||
3279 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
3280 | { | |
3281 | level = TEMPLATE_PARM_LEVEL (t); | |
3282 | idx = TEMPLATE_PARM_IDX (t); | |
3283 | } | |
3284 | else | |
3285 | { | |
3286 | level = TEMPLATE_TYPE_LEVEL (t); | |
3287 | idx = TEMPLATE_TYPE_IDX (t); | |
3288 | } | |
3289 | ||
3290 | if (level == tpd->level) | |
6c30752f MM |
3291 | { |
3292 | tpd->parms[idx] = 1; | |
3293 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
3294 | } | |
050367a3 MM |
3295 | |
3296 | /* Return zero so that for_each_template_parm will continue the | |
3297 | traversal of the tree; we want to mark *every* template parm. */ | |
3298 | return 0; | |
3299 | } | |
3300 | ||
6c30752f MM |
3301 | /* Process the partial specialization DECL. */ |
3302 | ||
e9659ab0 | 3303 | static tree |
3a978d72 | 3304 | process_partial_specialization (tree decl) |
6c30752f MM |
3305 | { |
3306 | tree type = TREE_TYPE (decl); | |
3307 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
3308 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
f9a7ae04 | 3309 | tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); |
6c30752f MM |
3310 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); |
3311 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
3312 | int nargs = TREE_VEC_LENGTH (inner_args); | |
3313 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
3314 | int i; | |
3315 | int did_error_intro = 0; | |
6c30752f MM |
3316 | struct template_parm_data tpd; |
3317 | struct template_parm_data tpd2; | |
3318 | ||
6c30752f MM |
3319 | /* We check that each of the template parameters given in the |
3320 | partial specialization is used in the argument list to the | |
3321 | specialization. For example: | |
3322 | ||
3323 | template <class T> struct S; | |
3324 | template <class T> struct S<T*>; | |
3325 | ||
3326 | The second declaration is OK because `T*' uses the template | |
3327 | parameter T, whereas | |
3328 | ||
3329 | template <class T> struct S<int>; | |
3330 | ||
3331 | is no good. Even trickier is: | |
3332 | ||
3333 | template <class T> | |
3334 | struct S1 | |
3335 | { | |
3336 | template <class U> | |
3337 | struct S2; | |
3338 | template <class U> | |
3339 | struct S2<T>; | |
3340 | }; | |
3341 | ||
0e339752 | 3342 | The S2<T> declaration is actually invalid; it is a |
c8094d83 | 3343 | full-specialization. Of course, |
6c30752f MM |
3344 | |
3345 | template <class U> | |
3346 | struct S2<T (*)(U)>; | |
3347 | ||
3348 | or some such would have been OK. */ | |
3349 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
67f5655f | 3350 | tpd.parms = (int *) alloca (sizeof (int) * ntparms); |
fad205ff | 3351 | memset (tpd.parms, 0, sizeof (int) * ntparms); |
6c30752f | 3352 | |
67f5655f | 3353 | tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs); |
fad205ff | 3354 | memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); |
6c30752f MM |
3355 | for (i = 0; i < nargs; ++i) |
3356 | { | |
3357 | tpd.current_arg = i; | |
3358 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
3359 | &mark_template_parm, | |
4f2c9d7e | 3360 | &tpd, |
c095a4f8 DG |
3361 | NULL, |
3362 | /*include_nondeduced_p=*/false); | |
6c30752f MM |
3363 | } |
3364 | for (i = 0; i < ntparms; ++i) | |
3365 | if (tpd.parms[i] == 0) | |
3366 | { | |
3367 | /* One of the template parms was not used in the | |
f9a7ae04 | 3368 | specialization. */ |
6c30752f MM |
3369 | if (!did_error_intro) |
3370 | { | |
33bd39a2 | 3371 | error ("template parameters not used in partial specialization:"); |
6c30752f MM |
3372 | did_error_intro = 1; |
3373 | } | |
3374 | ||
0f51ccfc | 3375 | error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
6c30752f MM |
3376 | } |
3377 | ||
3378 | /* [temp.class.spec] | |
3379 | ||
3380 | The argument list of the specialization shall not be identical to | |
3381 | the implicit argument list of the primary template. */ | |
c8094d83 MS |
3382 | if (comp_template_args |
3383 | (inner_args, | |
f9a7ae04 MM |
3384 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE |
3385 | (maintmpl))))) | |
0f51ccfc | 3386 | error ("partial specialization %qT does not specialize any template arguments", type); |
6c30752f MM |
3387 | |
3388 | /* [temp.class.spec] | |
3389 | ||
3390 | A partially specialized non-type argument expression shall not | |
3391 | involve template parameters of the partial specialization except | |
3392 | when the argument expression is a simple identifier. | |
3393 | ||
3394 | The type of a template parameter corresponding to a specialized | |
3395 | non-type argument shall not be dependent on a parameter of the | |
5d80a306 DG |
3396 | specialization. |
3397 | ||
3398 | Also, we verify that pack expansions only occur at the | |
3399 | end of the argument list. */ | |
50bc768d | 3400 | gcc_assert (nargs == DECL_NTPARMS (maintmpl)); |
6c30752f MM |
3401 | tpd2.parms = 0; |
3402 | for (i = 0; i < nargs; ++i) | |
3403 | { | |
5d80a306 | 3404 | tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i)); |
6c30752f | 3405 | tree arg = TREE_VEC_ELT (inner_args, i); |
5d80a306 DG |
3406 | tree packed_args = NULL_TREE; |
3407 | int j, len = 1; | |
6c30752f | 3408 | |
5d80a306 DG |
3409 | if (ARGUMENT_PACK_P (arg)) |
3410 | { | |
3411 | /* Extract the arguments from the argument pack. We'll be | |
3412 | iterating over these in the following loop. */ | |
3413 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
3414 | len = TREE_VEC_LENGTH (packed_args); | |
3415 | } | |
3416 | ||
3417 | for (j = 0; j < len; j++) | |
3418 | { | |
3419 | if (packed_args) | |
3420 | /* Get the Jth argument in the parameter pack. */ | |
3421 | arg = TREE_VEC_ELT (packed_args, j); | |
3422 | ||
3423 | if (PACK_EXPANSION_P (arg)) | |
3424 | { | |
3425 | /* Pack expansions must come at the end of the | |
3426 | argument list. */ | |
3427 | if ((packed_args && j < len - 1) | |
3428 | || (!packed_args && i < nargs - 1)) | |
3429 | { | |
3430 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
3431 | error ("parameter pack argument %qE must be at the end of the template argument list", arg); | |
3432 | else | |
1ad8aeeb DG |
3433 | error ("parameter pack argument %qT must be at the end of the template argument list", arg); |
3434 | ||
3435 | if (packed_args) | |
3436 | TREE_VEC_ELT (packed_args, j) = error_mark_node; | |
5d80a306 DG |
3437 | } |
3438 | } | |
3439 | ||
3440 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
3441 | /* We only care about the pattern. */ | |
3442 | arg = PACK_EXPANSION_PATTERN (arg); | |
3443 | ||
3444 | if (/* These first two lines are the `non-type' bit. */ | |
3445 | !TYPE_P (arg) | |
3446 | && TREE_CODE (arg) != TEMPLATE_DECL | |
3447 | /* This next line is the `argument expression is not just a | |
3448 | simple identifier' condition and also the `specialized | |
3449 | non-type argument' bit. */ | |
3450 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
3451 | { | |
3452 | if ((!packed_args && tpd.arg_uses_template_parms[i]) | |
3453 | || (packed_args && uses_template_parms (arg))) | |
3454 | error ("template argument %qE involves template parameter(s)", | |
3455 | arg); | |
3456 | else | |
3457 | { | |
3458 | /* Look at the corresponding template parameter, | |
3459 | marking which template parameters its type depends | |
3460 | upon. */ | |
3461 | tree type = TREE_TYPE (parm); | |
3462 | ||
3463 | if (!tpd2.parms) | |
3464 | { | |
3465 | /* We haven't yet initialized TPD2. Do so now. */ | |
3466 | tpd2.arg_uses_template_parms | |
3467 | = (int *) alloca (sizeof (int) * nargs); | |
3468 | /* The number of parameters here is the number in the | |
3469 | main template, which, as checked in the assertion | |
3470 | above, is NARGS. */ | |
3471 | tpd2.parms = (int *) alloca (sizeof (int) * nargs); | |
3472 | tpd2.level = | |
3473 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
3474 | } | |
3475 | ||
3476 | /* Mark the template parameters. But this time, we're | |
3477 | looking for the template parameters of the main | |
3478 | template, not in the specialization. */ | |
3479 | tpd2.current_arg = i; | |
3480 | tpd2.arg_uses_template_parms[i] = 0; | |
3481 | memset (tpd2.parms, 0, sizeof (int) * nargs); | |
3482 | for_each_template_parm (type, | |
3483 | &mark_template_parm, | |
3484 | &tpd2, | |
c095a4f8 DG |
3485 | NULL, |
3486 | /*include_nondeduced_p=*/false); | |
5d80a306 DG |
3487 | |
3488 | if (tpd2.arg_uses_template_parms [i]) | |
3489 | { | |
3490 | /* The type depended on some template parameters. | |
3491 | If they are fully specialized in the | |
3492 | specialization, that's OK. */ | |
3493 | int j; | |
3494 | for (j = 0; j < nargs; ++j) | |
3495 | if (tpd2.parms[j] != 0 | |
3496 | && tpd.arg_uses_template_parms [j]) | |
3497 | { | |
3498 | error ("type %qT of template argument %qE depends " | |
3499 | "on template parameter(s)", | |
3500 | type, | |
3501 | arg); | |
3502 | break; | |
3503 | } | |
3504 | } | |
3505 | } | |
3506 | } | |
3507 | } | |
6c30752f MM |
3508 | } |
3509 | ||
c8094d83 | 3510 | if (retrieve_specialization (maintmpl, specargs, |
c7222c02 | 3511 | /*class_specializations_p=*/true)) |
6c30752f MM |
3512 | /* We've already got this specialization. */ |
3513 | return decl; | |
3514 | ||
d8b64f80 | 3515 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) |
916b63c3 | 3516 | = tree_cons (specargs, inner_parms, |
b1d7b1c0 | 3517 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); |
6c30752f MM |
3518 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
3519 | return decl; | |
3520 | } | |
3521 | ||
85d85234 DG |
3522 | /* Check that a template declaration's use of default arguments and |
3523 | parameter packs is not invalid. Here, PARMS are the template | |
3524 | parameters. IS_PRIMARY is nonzero if DECL is the thing declared by | |
3525 | a primary template. IS_PARTIAL is nonzero if DECL is a partial | |
3526 | specialization. | |
9b7dd5e8 | 3527 | |
6ba89f8e | 3528 | |
9b7dd5e8 DG |
3529 | IS_FRIEND_DECL is nonzero if DECL is a friend function template |
3530 | declaration (but not a definition); 1 indicates a declaration, 2 | |
3531 | indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are | |
3532 | emitted for extraneous default arguments. | |
3533 | ||
3534 | Returns TRUE if there were no errors found, FALSE otherwise. */ | |
3535 | ||
3536 | bool | |
3537 | check_default_tmpl_args (tree decl, tree parms, int is_primary, | |
3538 | int is_partial, int is_friend_decl) | |
6ba89f8e | 3539 | { |
d8e178a0 | 3540 | const char *msg; |
66191c20 MM |
3541 | int last_level_to_check; |
3542 | tree parm_level; | |
9b7dd5e8 | 3543 | bool no_errors = true; |
6ba89f8e | 3544 | |
c8094d83 | 3545 | /* [temp.param] |
6ba89f8e MM |
3546 | |
3547 | A default template-argument shall not be specified in a | |
3548 | function template declaration or a function template definition, nor | |
3549 | in the template-parameter-list of the definition of a member of a | |
3550 | class template. */ | |
3551 | ||
4f1c5b7d | 3552 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
e0942dcd MM |
3553 | /* You can't have a function template declaration in a local |
3554 | scope, nor you can you define a member of a class template in a | |
3555 | local scope. */ | |
9b7dd5e8 | 3556 | return true; |
e0942dcd | 3557 | |
6ba89f8e MM |
3558 | if (current_class_type |
3559 | && !TYPE_BEING_DEFINED (current_class_type) | |
3d7de1fa | 3560 | && DECL_LANG_SPECIFIC (decl) |
5937a6f9 MM |
3561 | /* If this is either a friend defined in the scope of the class |
3562 | or a member function. */ | |
6df5158a NS |
3563 | && (DECL_FUNCTION_MEMBER_P (decl) |
3564 | ? same_type_p (DECL_CONTEXT (decl), current_class_type) | |
3565 | : DECL_FRIEND_CONTEXT (decl) | |
3566 | ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) | |
3567 | : false) | |
5937a6f9 MM |
3568 | /* And, if it was a member function, it really was defined in |
3569 | the scope of the class. */ | |
6df5158a NS |
3570 | && (!DECL_FUNCTION_MEMBER_P (decl) |
3571 | || DECL_INITIALIZED_IN_CLASS_P (decl))) | |
6ba89f8e | 3572 | /* We already checked these parameters when the template was |
5937a6f9 MM |
3573 | declared, so there's no need to do it again now. This function |
3574 | was defined in class scope, but we're processing it's body now | |
3575 | that the class is complete. */ | |
9b7dd5e8 | 3576 | return true; |
c8094d83 | 3577 | |
9b7dd5e8 DG |
3578 | /* Core issue 226 (C++0x only): the following only applies to class |
3579 | templates. */ | |
c1ae8be5 | 3580 | if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL) |
66191c20 | 3581 | { |
9b7dd5e8 | 3582 | /* [temp.param] |
66191c20 | 3583 | |
9b7dd5e8 DG |
3584 | If a template-parameter has a default template-argument, all |
3585 | subsequent template-parameters shall have a default | |
3586 | template-argument supplied. */ | |
3587 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
3588 | { | |
3589 | tree inner_parms = TREE_VALUE (parm_level); | |
3590 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
3591 | int seen_def_arg_p = 0; | |
3592 | int i; | |
42b304f1 | 3593 | |
9b7dd5e8 DG |
3594 | for (i = 0; i < ntparms; ++i) |
3595 | { | |
3596 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
42b304f1 | 3597 | |
9b7dd5e8 DG |
3598 | if (parm == error_mark_node) |
3599 | continue; | |
3600 | ||
3601 | if (TREE_PURPOSE (parm)) | |
3602 | seen_def_arg_p = 1; | |
3603 | else if (seen_def_arg_p) | |
3604 | { | |
3605 | error ("no default argument for %qD", TREE_VALUE (parm)); | |
3606 | /* For better subsequent error-recovery, we indicate that | |
3607 | there should have been a default argument. */ | |
3608 | TREE_PURPOSE (parm) = error_mark_node; | |
3609 | no_errors = false; | |
3610 | } | |
85d85234 DG |
3611 | else if (is_primary |
3612 | && !is_partial | |
3613 | && !is_friend_decl | |
3614 | && TREE_CODE (decl) == TYPE_DECL | |
3615 | && i < ntparms - 1 | |
3616 | && template_parameter_pack_p (TREE_VALUE (parm))) | |
3617 | { | |
3618 | /* A primary class template can only have one | |
3619 | parameter pack, at the end of the template | |
3620 | parameter list. */ | |
3621 | ||
3622 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL) | |
3623 | error ("parameter pack %qE must be at the end of the" | |
3624 | " template parameter list", TREE_VALUE (parm)); | |
3625 | else | |
3626 | error ("parameter pack %qT must be at the end of the" | |
3627 | " template parameter list", | |
3628 | TREE_TYPE (TREE_VALUE (parm))); | |
3629 | ||
3630 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i)) | |
3631 | = error_mark_node; | |
3632 | no_errors = false; | |
3633 | } | |
9b7dd5e8 DG |
3634 | } |
3635 | } | |
66191c20 MM |
3636 | } |
3637 | ||
c1ae8be5 | 3638 | if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL) |
9b7dd5e8 DG |
3639 | || is_partial |
3640 | || !is_primary | |
3641 | || is_friend_decl) | |
6ba89f8e MM |
3642 | /* For an ordinary class template, default template arguments are |
3643 | allowed at the innermost level, e.g.: | |
0cbd7506 | 3644 | template <class T = int> |
6ba89f8e MM |
3645 | struct S {}; |
3646 | but, in a partial specialization, they're not allowed even | |
3647 | there, as we have in [temp.class.spec]: | |
c8094d83 | 3648 | |
6ba89f8e | 3649 | The template parameter list of a specialization shall not |
c8094d83 | 3650 | contain default template argument values. |
6ba89f8e | 3651 | |
9b7dd5e8 DG |
3652 | So, for a partial specialization, or for a function template |
3653 | (in C++98/C++03), we look at all of them. */ | |
6ba89f8e MM |
3654 | ; |
3655 | else | |
3656 | /* But, for a primary class template that is not a partial | |
3657 | specialization we look at all template parameters except the | |
3658 | innermost ones. */ | |
3659 | parms = TREE_CHAIN (parms); | |
3660 | ||
3661 | /* Figure out what error message to issue. */ | |
9b7dd5e8 DG |
3662 | if (is_friend_decl == 2) |
3663 | msg = "default template arguments may not be used in function template friend re-declaration"; | |
3664 | else if (is_friend_decl) | |
3665 | msg = "default template arguments may not be used in function template friend declarations"; | |
c1ae8be5 | 3666 | else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98)) |
8653a2c3 | 3667 | msg = "default template arguments may not be used in function templates"; |
6ba89f8e | 3668 | else if (is_partial) |
8653a2c3 | 3669 | msg = "default template arguments may not be used in partial specializations"; |
6ba89f8e | 3670 | else |
0f51ccfc | 3671 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e MM |
3672 | |
3673 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
3674 | /* If we're inside a class definition, there's no need to | |
104bf76a | 3675 | examine the parameters to the class itself. On the one |
6ba89f8e | 3676 | hand, they will be checked when the class is defined, and, |
0e339752 | 3677 | on the other, default arguments are valid in things like: |
0cbd7506 MS |
3678 | template <class T = double> |
3679 | struct S { template <class U> void f(U); }; | |
6ba89f8e MM |
3680 | Here the default argument for `S' has no bearing on the |
3681 | declaration of `f'. */ | |
3682 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
3683 | else | |
3684 | /* Check everything. */ | |
3685 | last_level_to_check = 0; | |
3686 | ||
c8094d83 MS |
3687 | for (parm_level = parms; |
3688 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
66191c20 | 3689 | parm_level = TREE_CHAIN (parm_level)) |
6ba89f8e | 3690 | { |
66191c20 MM |
3691 | tree inner_parms = TREE_VALUE (parm_level); |
3692 | int i; | |
3693 | int ntparms; | |
6ba89f8e MM |
3694 | |
3695 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
c8094d83 | 3696 | for (i = 0; i < ntparms; ++i) |
42b304f1 LM |
3697 | { |
3698 | if (TREE_VEC_ELT (inner_parms, i) == error_mark_node) | |
3699 | continue; | |
6ba89f8e | 3700 | |
42b304f1 LM |
3701 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) |
3702 | { | |
3703 | if (msg) | |
3704 | { | |
9b7dd5e8 DG |
3705 | no_errors = false; |
3706 | if (is_friend_decl == 2) | |
3707 | return no_errors; | |
3708 | ||
42b304f1 LM |
3709 | error (msg, decl); |
3710 | msg = 0; | |
3711 | } | |
3712 | ||
3713 | /* Clear out the default argument so that we are not | |
3714 | confused later. */ | |
3715 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
3716 | } | |
3717 | } | |
6ba89f8e MM |
3718 | |
3719 | /* At this point, if we're still interested in issuing messages, | |
3720 | they must apply to classes surrounding the object declared. */ | |
3721 | if (msg) | |
c8094d83 | 3722 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e | 3723 | } |
9b7dd5e8 DG |
3724 | |
3725 | return no_errors; | |
6ba89f8e MM |
3726 | } |
3727 | ||
5dd236e2 NS |
3728 | /* Worker for push_template_decl_real, called via |
3729 | for_each_template_parm. DATA is really an int, indicating the | |
3730 | level of the parameters we are interested in. If T is a template | |
838dfd8a | 3731 | parameter of that level, return nonzero. */ |
5dd236e2 NS |
3732 | |
3733 | static int | |
3a978d72 | 3734 | template_parm_this_level_p (tree t, void* data) |
5dd236e2 | 3735 | { |
6e04241f | 3736 | int this_level = *(int *)data; |
5dd236e2 NS |
3737 | int level; |
3738 | ||
3739 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
3740 | level = TEMPLATE_PARM_LEVEL (t); | |
3741 | else | |
3742 | level = TEMPLATE_TYPE_LEVEL (t); | |
3743 | return level == this_level; | |
3744 | } | |
3745 | ||
3ac3d9ea | 3746 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
3747 | parameters given by current_template_args, or reuses a |
3748 | previously existing one, if appropriate. Returns the DECL, or an | |
c8094d83 | 3749 | equivalent one, if it is replaced via a call to duplicate_decls. |
6757edfe | 3750 | |
d63d5d0c | 3751 | If IS_FRIEND is true, DECL is a friend declaration. */ |
3ac3d9ea MM |
3752 | |
3753 | tree | |
d63d5d0c | 3754 | push_template_decl_real (tree decl, bool is_friend) |
9a3b49ac MS |
3755 | { |
3756 | tree tmpl; | |
f84b4be9 | 3757 | tree args; |
9a3b49ac | 3758 | tree info; |
f84b4be9 JM |
3759 | tree ctx; |
3760 | int primary; | |
6ba89f8e | 3761 | int is_partial; |
cfe507be | 3762 | int new_template_p = 0; |
c7222c02 MM |
3763 | /* True if the template is a member template, in the sense of |
3764 | [temp.mem]. */ | |
3765 | bool member_template_p = false; | |
6ba89f8e | 3766 | |
c0694c4b MM |
3767 | if (decl == error_mark_node) |
3768 | return decl; | |
3769 | ||
6ba89f8e | 3770 | /* See if this is a partial specialization. */ |
9188c363 | 3771 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
6ba89f8e | 3772 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
370af2d5 | 3773 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); |
6757edfe | 3774 | |
d63d5d0c ILT |
3775 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)) |
3776 | is_friend = true; | |
f84b4be9 JM |
3777 | |
3778 | if (is_friend) | |
3779 | /* For a friend, we want the context of the friend function, not | |
3780 | the type of which it is a friend. */ | |
3781 | ctx = DECL_CONTEXT (decl); | |
4f1c5b7d MM |
3782 | else if (CP_DECL_CONTEXT (decl) |
3783 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
3784 | /* In the case of a virtual function, we want the class in which |
3785 | it is defined. */ | |
4f1c5b7d | 3786 | ctx = CP_DECL_CONTEXT (decl); |
f84b4be9 | 3787 | else |
dc957d14 | 3788 | /* Otherwise, if we're currently defining some class, the DECL |
f84b4be9 | 3789 | is assumed to be a member of the class. */ |
9188c363 | 3790 | ctx = current_scope (); |
f84b4be9 | 3791 | |
2c73f9f5 ML |
3792 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
3793 | ctx = NULL_TREE; | |
3794 | ||
3795 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 3796 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 3797 | |
6ba89f8e | 3798 | /* See if this is a primary template. */ |
c9cbfca6 JM |
3799 | if (is_friend && ctx) |
3800 | /* A friend template that specifies a class context, i.e. | |
3801 | template <typename T> friend void A<T>::f(); | |
3802 | is not primary. */ | |
3803 | primary = 0; | |
3804 | else | |
3805 | primary = template_parm_scope_p (); | |
9a3b49ac | 3806 | |
83566abf JM |
3807 | if (primary) |
3808 | { | |
c7222c02 MM |
3809 | if (DECL_CLASS_SCOPE_P (decl)) |
3810 | member_template_p = true; | |
2f1b1731 MM |
3811 | if (TREE_CODE (decl) == TYPE_DECL |
3812 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
33bd39a2 | 3813 | error ("template class without a name"); |
717a7d5d | 3814 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
4b0d3cbe | 3815 | { |
717a7d5d MM |
3816 | if (DECL_DESTRUCTOR_P (decl)) |
3817 | { | |
3818 | /* [temp.mem] | |
c8094d83 | 3819 | |
0cbd7506 | 3820 | A destructor shall not be a member template. */ |
0f51ccfc | 3821 | error ("destructor %qD declared as member template", decl); |
717a7d5d MM |
3822 | return error_mark_node; |
3823 | } | |
3824 | if (NEW_DELETE_OPNAME_P (DECL_NAME (decl)) | |
3825 | && (!TYPE_ARG_TYPES (TREE_TYPE (decl)) | |
3826 | || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node | |
3827 | || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) | |
3828 | || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl)))) | |
3829 | == void_list_node))) | |
3830 | { | |
c8094d83 | 3831 | /* [basic.stc.dynamic.allocation] |
717a7d5d | 3832 | |
0cbd7506 | 3833 | An allocation function can be a function |
717a7d5d MM |
3834 | template. ... Template allocation functions shall |
3835 | have two or more parameters. */ | |
0f51ccfc | 3836 | error ("invalid template declaration of %qD", decl); |
7c60008e | 3837 | return error_mark_node; |
717a7d5d | 3838 | } |
4b0d3cbe | 3839 | } |
8ca4bf25 MM |
3840 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
3841 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
2aaf816d JM |
3842 | /* OK */; |
3843 | else | |
11325dcd | 3844 | { |
0f51ccfc | 3845 | error ("template declaration of %q#D", decl); |
11325dcd KL |
3846 | return error_mark_node; |
3847 | } | |
83566abf JM |
3848 | } |
3849 | ||
6ba89f8e MM |
3850 | /* Check to see that the rules regarding the use of default |
3851 | arguments are not being violated. */ | |
c8094d83 | 3852 | check_default_tmpl_args (decl, current_template_parms, |
9b7dd5e8 | 3853 | primary, is_partial, /*is_friend_decl=*/0); |
73aad9b9 | 3854 | |
5d80a306 DG |
3855 | /* Ensure that there are no parameter packs in the type of this |
3856 | declaration that have not been expanded. */ | |
b1d7b1c0 DG |
3857 | if (TREE_CODE (decl) == FUNCTION_DECL) |
3858 | { | |
3859 | /* Check each of the arguments individually to see if there are | |
3860 | any bare parameter packs. */ | |
3861 | tree type = TREE_TYPE (decl); | |
3862 | tree arg = DECL_ARGUMENTS (decl); | |
3863 | tree argtype = TYPE_ARG_TYPES (type); | |
3864 | ||
3865 | while (arg && argtype) | |
3866 | { | |
3867 | if (!FUNCTION_PARAMETER_PACK_P (arg) | |
7b3e2d46 | 3868 | && check_for_bare_parameter_packs (TREE_TYPE (arg))) |
b1d7b1c0 DG |
3869 | { |
3870 | /* This is a PARM_DECL that contains unexpanded parameter | |
3871 | packs. We have already complained about this in the | |
3872 | check_for_bare_parameter_packs call, so just replace | |
3873 | these types with ERROR_MARK_NODE. */ | |
3874 | TREE_TYPE (arg) = error_mark_node; | |
3875 | TREE_VALUE (argtype) = error_mark_node; | |
3876 | } | |
3877 | ||
3878 | arg = TREE_CHAIN (arg); | |
3879 | argtype = TREE_CHAIN (argtype); | |
3880 | } | |
3881 | ||
3882 | /* Check for bare parameter packs in the return type and the | |
3883 | exception specifiers. */ | |
7b3e2d46 | 3884 | if (check_for_bare_parameter_packs (TREE_TYPE (type))) |
046e4071 JJ |
3885 | /* Errors were already issued, set return type to int |
3886 | as the frontend doesn't expect error_mark_node as | |
3887 | the return type. */ | |
3888 | TREE_TYPE (type) = integer_type_node; | |
7b3e2d46 DG |
3889 | if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type))) |
3890 | TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE; | |
b1d7b1c0 | 3891 | } |
7b3e2d46 | 3892 | else if (check_for_bare_parameter_packs (TREE_TYPE (decl))) |
046e4071 | 3893 | return error_mark_node; |
5d80a306 | 3894 | |
6ba89f8e MM |
3895 | if (is_partial) |
3896 | return process_partial_specialization (decl); | |
d32789d8 | 3897 | |
9a3b49ac MS |
3898 | args = current_template_args (); |
3899 | ||
c8094d83 | 3900 | if (!ctx |
f84b4be9 | 3901 | || TREE_CODE (ctx) == FUNCTION_DECL |
a723baf1 | 3902 | || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) |
f84b4be9 | 3903 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) |
8d08fdba | 3904 | { |
75650646 | 3905 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
3906 | && DECL_TEMPLATE_INFO (decl) |
3907 | && DECL_TI_TEMPLATE (decl)) | |
3908 | tmpl = DECL_TI_TEMPLATE (decl); | |
1c10870d AS |
3909 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
3910 | be DECL_LANG_SPECIFIC. The information equivalent to | |
3911 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
c8094d83 | 3912 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
1c10870d AS |
3913 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) |
3914 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
3915 | { | |
3916 | /* Since a template declaration already existed for this | |
3917 | class-type, we must be redeclaring it here. Make sure | |
0e339752 | 3918 | that the redeclaration is valid. */ |
1c10870d AS |
3919 | redeclare_class_template (TREE_TYPE (decl), |
3920 | current_template_parms); | |
3921 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
3922 | one we already had. */ | |
3923 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
3924 | } | |
f84b4be9 | 3925 | else |
786b5245 | 3926 | { |
c7222c02 MM |
3927 | tmpl = build_template_decl (decl, current_template_parms, |
3928 | member_template_p); | |
cfe507be MM |
3929 | new_template_p = 1; |
3930 | ||
f84b4be9 JM |
3931 | if (DECL_LANG_SPECIFIC (decl) |
3932 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
3933 | { | |
3934 | /* A specialization of a member template of a template | |
c6002625 | 3935 | class. */ |
f84b4be9 JM |
3936 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
3937 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
3938 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
3939 | } | |
786b5245 | 3940 | } |
8d08fdba MS |
3941 | } |
3942 | else | |
3943 | { | |
e1a5ccf7 | 3944 | tree a, t, current, parms; |
ba4f4e5d | 3945 | int i; |
91a77d68 | 3946 | tree tinfo = get_template_info (decl); |
6633d636 | 3947 | |
91a77d68 | 3948 | if (!tinfo) |
c91a56d2 | 3949 | { |
0f51ccfc | 3950 | error ("template definition of non-template %q#D", decl); |
3fe82414 | 3951 | return error_mark_node; |
c91a56d2 | 3952 | } |
91a77d68 JM |
3953 | |
3954 | tmpl = TI_TEMPLATE (tinfo); | |
c8094d83 | 3955 | |
c353b8e3 | 3956 | if (DECL_FUNCTION_TEMPLATE_P (tmpl) |
c8094d83 | 3957 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) |
c353b8e3 | 3958 | && DECL_TEMPLATE_SPECIALIZATION (decl) |
c7222c02 | 3959 | && DECL_MEMBER_TEMPLATE_P (tmpl)) |
98c1c668 | 3960 | { |
e1a5ccf7 JM |
3961 | tree new_tmpl; |
3962 | ||
3963 | /* The declaration is a specialization of a member | |
3964 | template, declared outside the class. Therefore, the | |
3965 | innermost template arguments will be NULL, so we | |
3966 | replace them with the arguments determined by the | |
3967 | earlier call to check_explicit_specialization. */ | |
3968 | args = DECL_TI_ARGS (decl); | |
3969 | ||
c8094d83 | 3970 | new_tmpl |
c7222c02 MM |
3971 | = build_template_decl (decl, current_template_parms, |
3972 | member_template_p); | |
e1a5ccf7 JM |
3973 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; |
3974 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
3975 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
3976 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
c8094d83 | 3977 | DECL_TEMPLATE_INFO (new_tmpl) |
e1b3e07d | 3978 | = tree_cons (tmpl, args, NULL_TREE); |
e1a5ccf7 | 3979 | |
c8094d83 MS |
3980 | register_specialization (new_tmpl, |
3981 | most_general_template (tmpl), | |
d63d5d0c ILT |
3982 | args, |
3983 | is_friend); | |
e1a5ccf7 | 3984 | return decl; |
98c1c668 | 3985 | } |
98c1c668 | 3986 | |
e1a5ccf7 | 3987 | /* Make sure the template headers we got make sense. */ |
6633d636 | 3988 | |
e1a5ccf7 JM |
3989 | parms = DECL_TEMPLATE_PARMS (tmpl); |
3990 | i = TMPL_PARMS_DEPTH (parms); | |
3991 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 3992 | { |
0f51ccfc | 3993 | error ("expected %d levels of template parms for %q#D, got %d", |
0cbd7506 | 3994 | i, decl, TMPL_ARGS_DEPTH (args)); |
98c1c668 | 3995 | } |
e1a5ccf7 JM |
3996 | else |
3997 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
3998 | { | |
3999 | a = TMPL_ARGS_LEVEL (args, i); | |
4000 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
4001 | ||
4002 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
4003 | { | |
4004 | if (current == decl) | |
0f51ccfc | 4005 | error ("got %d template parameters for %q#D", |
0cbd7506 | 4006 | TREE_VEC_LENGTH (a), decl); |
e1a5ccf7 | 4007 | else |
0f51ccfc | 4008 | error ("got %d template parameters for %q#T", |
0cbd7506 | 4009 | TREE_VEC_LENGTH (a), current); |
33bd39a2 | 4010 | error (" but %d required", TREE_VEC_LENGTH (t)); |
f1cc0515 | 4011 | return error_mark_node; |
e1a5ccf7 | 4012 | } |
98c1c668 | 4013 | |
e1a5ccf7 JM |
4014 | if (current == decl) |
4015 | current = ctx; | |
4016 | else | |
ba75df2b PC |
4017 | current = (TYPE_P (current) |
4018 | ? TYPE_CONTEXT (current) | |
4019 | : DECL_CONTEXT (current)); | |
e1a5ccf7 | 4020 | } |
91a77d68 JM |
4021 | |
4022 | /* Check that the parms are used in the appropriate qualifying scopes | |
4023 | in the declarator. */ | |
4024 | if (!comp_template_args | |
4025 | (TI_ARGS (tinfo), | |
4026 | TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl))))) | |
4027 | { | |
4028 | error ("\ | |
4029 | template arguments to %qD do not match original template %qD", | |
4030 | decl, DECL_TEMPLATE_RESULT (tmpl)); | |
4031 | if (!uses_template_parms (TI_ARGS (tinfo))) | |
4032 | inform ("use template<> for an explicit specialization"); | |
4033 | /* Avoid crash in import_export_decl. */ | |
4034 | DECL_INTERFACE_KNOWN (decl) = 1; | |
4035 | return error_mark_node; | |
4036 | } | |
5566b478 | 4037 | } |
8d08fdba | 4038 | |
5566b478 MS |
4039 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
4040 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 4041 | |
36a117a5 MM |
4042 | /* Push template declarations for global functions and types. Note |
4043 | that we do not try to push a global template friend declared in a | |
4044 | template class; such a thing may well depend on the template | |
39c01e4c | 4045 | parameters of the class. */ |
c8094d83 | 4046 | if (new_template_p && !ctx |
36a117a5 | 4047 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
c6f9f83b | 4048 | { |
d63d5d0c | 4049 | tmpl = pushdecl_namespace_level (tmpl, is_friend); |
c6f9f83b KL |
4050 | if (tmpl == error_mark_node) |
4051 | return error_mark_node; | |
bd3d082e KL |
4052 | |
4053 | /* Hide template friend classes that haven't been declared yet. */ | |
4054 | if (is_friend && TREE_CODE (decl) == TYPE_DECL) | |
4055 | { | |
4056 | DECL_ANTICIPATED (tmpl) = 1; | |
4057 | DECL_FRIEND_P (tmpl) = 1; | |
4058 | } | |
c6f9f83b | 4059 | } |
8d08fdba | 4060 | |
5566b478 | 4061 | if (primary) |
5dd236e2 | 4062 | { |
ef3b7b17 JM |
4063 | tree parms = DECL_TEMPLATE_PARMS (tmpl); |
4064 | int i; | |
4065 | ||
5dd236e2 NS |
4066 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
4067 | if (DECL_CONV_FN_P (tmpl)) | |
4068 | { | |
ef3b7b17 | 4069 | int depth = TMPL_PARMS_DEPTH (parms); |
6e04241f | 4070 | |
5dd236e2 NS |
4071 | /* It is a conversion operator. See if the type converted to |
4072 | depends on innermost template operands. */ | |
c8094d83 | 4073 | |
d43f603d KL |
4074 | if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)), |
4075 | depth)) | |
5dd236e2 NS |
4076 | DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; |
4077 | } | |
ef3b7b17 JM |
4078 | |
4079 | /* Give template template parms a DECL_CONTEXT of the template | |
4080 | for which they are a parameter. */ | |
4081 | parms = INNERMOST_TEMPLATE_PARMS (parms); | |
4082 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) | |
4083 | { | |
4084 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
4085 | if (TREE_CODE (parm) == TEMPLATE_DECL) | |
4086 | DECL_CONTEXT (parm) = tmpl; | |
4087 | } | |
5dd236e2 | 4088 | } |
5566b478 | 4089 | |
a692ad2e | 4090 | /* The DECL_TI_ARGS of DECL contains full set of arguments referring |
28ba38e3 KL |
4091 | back to its most general template. If TMPL is a specialization, |
4092 | ARGS may only have the innermost set of arguments. Add the missing | |
4093 | argument levels if necessary. */ | |
4094 | if (DECL_TEMPLATE_INFO (tmpl)) | |
4095 | args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args); | |
4096 | ||
e1b3e07d | 4097 | info = tree_cons (tmpl, args, NULL_TREE); |
5566b478 | 4098 | |
9188c363 | 4099 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
a0ad3539 | 4100 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
2aaf816d | 4101 | else if (DECL_LANG_SPECIFIC (decl)) |
5566b478 | 4102 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
4103 | |
4104 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
4105 | } |
4106 | ||
6757edfe | 4107 | tree |
3a978d72 | 4108 | push_template_decl (tree decl) |
6757edfe | 4109 | { |
d63d5d0c | 4110 | return push_template_decl_real (decl, false); |
6757edfe MM |
4111 | } |
4112 | ||
4113 | /* Called when a class template TYPE is redeclared with the indicated | |
4114 | template PARMS, e.g.: | |
7fe6899f MM |
4115 | |
4116 | template <class T> struct S; | |
4117 | template <class T> struct S {}; */ | |
4118 | ||
60feef2c | 4119 | bool |
3a978d72 | 4120 | redeclare_class_template (tree type, tree parms) |
7fe6899f | 4121 | { |
3d7de1fa | 4122 | tree tmpl; |
6757edfe | 4123 | tree tmpl_parms; |
7fe6899f MM |
4124 | int i; |
4125 | ||
3d7de1fa MM |
4126 | if (!TYPE_TEMPLATE_INFO (type)) |
4127 | { | |
0f51ccfc | 4128 | error ("%qT is not a template type", type); |
60feef2c | 4129 | return false; |
3d7de1fa MM |
4130 | } |
4131 | ||
4132 | tmpl = TYPE_TI_TEMPLATE (type); | |
7fe6899f MM |
4133 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
4134 | /* The type is nested in some template class. Nothing to worry | |
4135 | about here; there are no new template parameters for the nested | |
4136 | type. */ | |
60feef2c | 4137 | return true; |
7fe6899f | 4138 | |
ee921ddf MM |
4139 | if (!parms) |
4140 | { | |
4141 | error ("template specifiers not specified in declaration of %qD", | |
4142 | tmpl); | |
60feef2c | 4143 | return false; |
ee921ddf MM |
4144 | } |
4145 | ||
6757edfe MM |
4146 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
4147 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
4148 | ||
7fe6899f MM |
4149 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
4150 | { | |
b131ad7c MLI |
4151 | error ("redeclared with %d template parameter(s)", |
4152 | TREE_VEC_LENGTH (parms)); | |
4153 | inform ("previous declaration %q+D used %d template parameter(s)", | |
4154 | tmpl, TREE_VEC_LENGTH (tmpl_parms)); | |
60feef2c | 4155 | return false; |
7fe6899f MM |
4156 | } |
4157 | ||
4158 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
4159 | { | |
2d8ba2c7 LM |
4160 | tree tmpl_parm; |
4161 | tree parm; | |
4162 | tree tmpl_default; | |
4163 | tree parm_default; | |
4164 | ||
4165 | if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node | |
4166 | || TREE_VEC_ELT (parms, i) == error_mark_node) | |
4167 | continue; | |
4168 | ||
4169 | tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
4170 | parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
4171 | tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
4172 | parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
7fe6899f | 4173 | |
2649701f KL |
4174 | /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or |
4175 | TEMPLATE_DECL. */ | |
0f67a82f | 4176 | if (tmpl_parm != error_mark_node |
a022041e DG |
4177 | && (TREE_CODE (tmpl_parm) != TREE_CODE (parm) |
4178 | || (TREE_CODE (tmpl_parm) != TYPE_DECL | |
4179 | && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))) | |
4180 | || (TREE_CODE (tmpl_parm) != PARM_DECL | |
4181 | && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm)) | |
4182 | != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))) | |
4183 | || (TREE_CODE (tmpl_parm) == PARM_DECL | |
4184 | && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm)) | |
4185 | != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))) | |
7fe6899f | 4186 | { |
dee15844 | 4187 | error ("template parameter %q+#D", tmpl_parm); |
0f51ccfc | 4188 | error ("redeclared here as %q#D", parm); |
60feef2c | 4189 | return false; |
7fe6899f MM |
4190 | } |
4191 | ||
4192 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
4193 | { | |
4194 | /* We have in [temp.param]: | |
4195 | ||
4196 | A template-parameter may not be given default arguments | |
4197 | by two different declarations in the same scope. */ | |
0f51ccfc | 4198 | error ("redefinition of default argument for %q#D", parm); |
b131ad7c | 4199 | inform ("%Joriginal definition appeared here", tmpl_parm); |
60feef2c | 4200 | return false; |
7fe6899f MM |
4201 | } |
4202 | ||
4203 | if (parm_default != NULL_TREE) | |
4204 | /* Update the previous template parameters (which are the ones | |
4205 | that will really count) with the new default value. */ | |
4206 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
7ce74c5e MM |
4207 | else if (tmpl_default != NULL_TREE) |
4208 | /* Update the new parameters, too; they'll be used as the | |
4209 | parameters for any members. */ | |
4210 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
7fe6899f | 4211 | } |
60feef2c LM |
4212 | |
4213 | return true; | |
7fe6899f | 4214 | } |
75650646 | 4215 | |
9baa27a9 MM |
4216 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
4217 | (possibly simplified) expression. */ | |
4218 | ||
4219 | tree | |
4220 | fold_non_dependent_expr (tree expr) | |
4221 | { | |
d4a200d3 SM |
4222 | if (expr == NULL_TREE) |
4223 | return NULL_TREE; | |
4224 | ||
9baa27a9 MM |
4225 | /* If we're in a template, but EXPR isn't value dependent, simplify |
4226 | it. We're supposed to treat: | |
c8094d83 | 4227 | |
9baa27a9 MM |
4228 | template <typename T> void f(T[1 + 1]); |
4229 | template <typename T> void f(T[2]); | |
c8094d83 | 4230 | |
9baa27a9 MM |
4231 | as two declarations of the same function, for example. */ |
4232 | if (processing_template_decl | |
4233 | && !type_dependent_expression_p (expr) | |
7416ab02 | 4234 | && !value_dependent_expression_p (expr)) |
9baa27a9 MM |
4235 | { |
4236 | HOST_WIDE_INT saved_processing_template_decl; | |
4237 | ||
4238 | saved_processing_template_decl = processing_template_decl; | |
4239 | processing_template_decl = 0; | |
4240 | expr = tsubst_copy_and_build (expr, | |
4241 | /*args=*/NULL_TREE, | |
4242 | tf_error, | |
4243 | /*in_decl=*/NULL_TREE, | |
015c2c66 MM |
4244 | /*function_p=*/false, |
4245 | /*integral_constant_expression_p=*/true); | |
9baa27a9 MM |
4246 | processing_template_decl = saved_processing_template_decl; |
4247 | } | |
4248 | return expr; | |
4249 | } | |
4250 | ||
b6ab6892 GB |
4251 | /* EXPR is an expression which is used in a constant-expression context. |
4252 | For instance, it could be a VAR_DECL with a constant initializer. | |
4253 | Extract the innest constant expression. | |
c8094d83 | 4254 | |
8a784e4a NS |
4255 | This is basically a more powerful version of |
4256 | integral_constant_value, which can be used also in templates where | |
4257 | initializers can maintain a syntactic rather than semantic form | |
4258 | (even if they are non-dependent, for access-checking purposes). */ | |
b6ab6892 | 4259 | |
993acaec | 4260 | static tree |
b6ab6892 GB |
4261 | fold_decl_constant_value (tree expr) |
4262 | { | |
4ef69b83 GB |
4263 | tree const_expr = expr; |
4264 | do | |
b6ab6892 | 4265 | { |
b6ab6892 | 4266 | expr = fold_non_dependent_expr (const_expr); |
4ef69b83 | 4267 | const_expr = integral_constant_value (expr); |
b6ab6892 | 4268 | } |
4ef69b83 | 4269 | while (expr != const_expr); |
b6ab6892 | 4270 | |
8a784e4a | 4271 | return expr; |
b6ab6892 GB |
4272 | } |
4273 | ||
4274 | /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which | |
4275 | must be a function or a pointer-to-function type, as specified | |
4276 | in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, | |
4277 | and check that the resulting function has external linkage. */ | |
4278 | ||
4279 | static tree | |
4280 | convert_nontype_argument_function (tree type, tree expr) | |
4281 | { | |
4282 | tree fns = expr; | |
4283 | tree fn, fn_no_ptr; | |
4284 | ||
4285 | fn = instantiate_type (type, fns, tf_none); | |
4286 | if (fn == error_mark_node) | |
4287 | return error_mark_node; | |
4288 | ||
4289 | fn_no_ptr = fn; | |
4290 | if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) | |
4291 | fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); | |
eff3a276 MM |
4292 | if (TREE_CODE (fn_no_ptr) == BASELINK) |
4293 | fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr); | |
4294 | ||
b6ab6892 GB |
4295 | /* [temp.arg.nontype]/1 |
4296 | ||
4297 | A template-argument for a non-type, non-template template-parameter | |
4298 | shall be one of: | |
4299 | [...] | |
4300 | -- the address of an object or function with external linkage. */ | |
4301 | if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr)) | |
4302 | { | |
4303 | error ("%qE is not a valid template argument for type %qT " | |
4304 | "because function %qD has not external linkage", | |
4305 | expr, type, fn_no_ptr); | |
4306 | return NULL_TREE; | |
4307 | } | |
4308 | ||
4309 | return fn; | |
4310 | } | |
4311 | ||
75650646 MM |
4312 | /* Attempt to convert the non-type template parameter EXPR to the |
4313 | indicated TYPE. If the conversion is successful, return the | |
dc957d14 | 4314 | converted value. If the conversion is unsuccessful, return |
75650646 MM |
4315 | NULL_TREE if we issued an error message, or error_mark_node if we |
4316 | did not. We issue error messages for out-and-out bad template | |
4317 | parameters, but not simply because the conversion failed, since we | |
9baa27a9 | 4318 | might be just trying to do argument deduction. Both TYPE and EXPR |
b6ab6892 GB |
4319 | must be non-dependent. |
4320 | ||
4321 | The conversion follows the special rules described in | |
4322 | [temp.arg.nontype], and it is much more strict than an implicit | |
4323 | conversion. | |
4324 | ||
4325 | This function is called twice for each template argument (see | |
4326 | lookup_template_class for a more accurate description of this | |
4327 | problem). This means that we need to handle expressions which | |
4328 | are not valid in a C++ source, but can be created from the | |
4329 | first call (for instance, casts to perform conversions). These | |
4330 | hacks can go away after we fix the double coercion problem. */ | |
75650646 MM |
4331 | |
4332 | static tree | |
3a978d72 | 4333 | convert_nontype_argument (tree type, tree expr) |
75650646 | 4334 | { |
9baa27a9 MM |
4335 | tree expr_type; |
4336 | ||
b6ab6892 GB |
4337 | /* Detect immediately string literals as invalid non-type argument. |
4338 | This special-case is not needed for correctness (we would easily | |
4339 | catch this later), but only to provide better diagnostic for this | |
4340 | common user mistake. As suggested by DR 100, we do not mention | |
4341 | linkage issues in the diagnostic as this is not the point. */ | |
4342 | if (TREE_CODE (expr) == STRING_CST) | |
4343 | { | |
4344 | error ("%qE is not a valid template argument for type %qT " | |
4345 | "because string literals can never be used in this context", | |
4346 | expr, type); | |
4347 | return NULL_TREE; | |
4348 | } | |
4349 | ||
9baa27a9 MM |
4350 | /* If we are in a template, EXPR may be non-dependent, but still |
4351 | have a syntactic, rather than semantic, form. For example, EXPR | |
4352 | might be a SCOPE_REF, rather than the VAR_DECL to which the | |
4353 | SCOPE_REF refers. Preserving the qualifying scope is necessary | |
4354 | so that access checking can be performed when the template is | |
4355 | instantiated -- but here we need the resolved form so that we can | |
4356 | convert the argument. */ | |
4357 | expr = fold_non_dependent_expr (expr); | |
b166a559 VR |
4358 | if (error_operand_p (expr)) |
4359 | return error_mark_node; | |
9baa27a9 | 4360 | expr_type = TREE_TYPE (expr); |
75650646 | 4361 | |
b6ab6892 GB |
4362 | /* HACK: Due to double coercion, we can get a |
4363 | NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here, | |
4364 | which is the tree that we built on the first call (see | |
4365 | below when coercing to reference to object or to reference to | |
4366 | function). We just strip everything and get to the arg. | |
4367 | See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C | |
4368 | for examples. */ | |
4369 | if (TREE_CODE (expr) == NOP_EXPR) | |
75650646 | 4370 | { |
b6ab6892 | 4371 | if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) |
75650646 | 4372 | { |
b6ab6892 GB |
4373 | /* ??? Maybe we could use convert_from_reference here, but we |
4374 | would need to relax its constraints because the NOP_EXPR | |
4375 | could actually change the type to something more cv-qualified, | |
4376 | and this is not folded by convert_from_reference. */ | |
4377 | tree addr = TREE_OPERAND (expr, 0); | |
4378 | gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE); | |
4379 | gcc_assert (TREE_CODE (addr) == ADDR_EXPR); | |
4380 | gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE); | |
4381 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4382 | (TREE_TYPE (expr_type), | |
4383 | TREE_TYPE (TREE_TYPE (addr)))); | |
4384 | ||
4385 | expr = TREE_OPERAND (addr, 0); | |
4386 | expr_type = TREE_TYPE (expr); | |
75650646 MM |
4387 | } |
4388 | ||
b6ab6892 GB |
4389 | /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the |
4390 | parameter is a pointer to object, through decay and | |
4391 | qualification conversion. Let's strip everything. */ | |
4392 | else if (TYPE_PTROBV_P (type)) | |
75650646 | 4393 | { |
b6ab6892 GB |
4394 | STRIP_NOPS (expr); |
4395 | gcc_assert (TREE_CODE (expr) == ADDR_EXPR); | |
4396 | gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE); | |
4397 | /* Skip the ADDR_EXPR only if it is part of the decay for | |
4398 | an array. Otherwise, it is part of the original argument | |
4399 | in the source code. */ | |
4400 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE) | |
4401 | expr = TREE_OPERAND (expr, 0); | |
4402 | expr_type = TREE_TYPE (expr); | |
f3400fe2 | 4403 | } |
75650646 | 4404 | } |
c61dce3a | 4405 | |
b6ab6892 | 4406 | /* [temp.arg.nontype]/5, bullet 1 |
75650646 | 4407 | |
b6ab6892 GB |
4408 | For a non-type template-parameter of integral or enumeration type, |
4409 | integral promotions (_conv.prom_) and integral conversions | |
4410 | (_conv.integral_) are applied. */ | |
4411 | if (INTEGRAL_TYPE_P (type)) | |
75650646 | 4412 | { |
75650646 MM |
4413 | if (!INTEGRAL_TYPE_P (expr_type)) |
4414 | return error_mark_node; | |
fddabb2c | 4415 | |
b6ab6892 GB |
4416 | expr = fold_decl_constant_value (expr); |
4417 | /* Notice that there are constant expressions like '4 % 0' which | |
4418 | do not fold into integer constants. */ | |
db02b6b9 | 4419 | if (TREE_CODE (expr) != INTEGER_CST) |
b6ab6892 GB |
4420 | { |
4421 | error ("%qE is not a valid template argument for type %qT " | |
4422 | "because it is a non-constant expression", expr, type); | |
4423 | return NULL_TREE; | |
4424 | } | |
75650646 | 4425 | |
b6ab6892 GB |
4426 | /* At this point, an implicit conversion does what we want, |
4427 | because we already know that the expression is of integral | |
4428 | type. */ | |
4429 | expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT); | |
4430 | if (expr == error_mark_node) | |
4431 | return error_mark_node; | |
75650646 | 4432 | |
b6ab6892 GB |
4433 | /* Conversion was allowed: fold it to a bare integer constant. */ |
4434 | expr = fold (expr); | |
4435 | } | |
4436 | /* [temp.arg.nontype]/5, bullet 2 | |
75650646 | 4437 | |
b6ab6892 GB |
4438 | For a non-type template-parameter of type pointer to object, |
4439 | qualification conversions (_conv.qual_) and the array-to-pointer | |
4440 | conversion (_conv.array_) are applied. */ | |
4441 | else if (TYPE_PTROBV_P (type)) | |
4442 | { | |
4443 | /* [temp.arg.nontype]/1 (TC1 version, DR 49): | |
75650646 | 4444 | |
b6ab6892 GB |
4445 | A template-argument for a non-type, non-template template-parameter |
4446 | shall be one of: [...] | |
75650646 | 4447 | |
b6ab6892 GB |
4448 | -- the name of a non-type template-parameter; |
4449 | -- the address of an object or function with external linkage, [...] | |
4450 | expressed as "& id-expression" where the & is optional if the name | |
4451 | refers to a function or array, or if the corresponding | |
4452 | template-parameter is a reference. | |
c8094d83 | 4453 | |
b6ab6892 GB |
4454 | Here, we do not care about functions, as they are invalid anyway |
4455 | for a parameter of type pointer-to-object. */ | |
1082fd10 MM |
4456 | |
4457 | if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)) | |
4458 | /* Non-type template parameters are OK. */ | |
4459 | ; | |
4460 | else if (TREE_CODE (expr) != ADDR_EXPR | |
4461 | && TREE_CODE (expr_type) != ARRAY_TYPE) | |
4462 | { | |
4463 | if (TREE_CODE (expr) == VAR_DECL) | |
4464 | { | |
4465 | error ("%qD is not a valid template argument " | |
4466 | "because %qD is a variable, not the address of " | |
4467 | "a variable", | |
4468 | expr, expr); | |
4469 | return NULL_TREE; | |
4470 | } | |
4471 | /* Other values, like integer constants, might be valid | |
4472 | non-type arguments of some other type. */ | |
4473 | return error_mark_node; | |
4474 | } | |
4475 | else | |
4476 | { | |
4477 | tree decl; | |
4478 | ||
4479 | decl = ((TREE_CODE (expr) == ADDR_EXPR) | |
4480 | ? TREE_OPERAND (expr, 0) : expr); | |
4481 | if (TREE_CODE (decl) != VAR_DECL) | |
4482 | { | |
4483 | error ("%qE is not a valid template argument of type %qT " | |
4484 | "because %qE is not a variable", | |
4485 | expr, type, decl); | |
4486 | return NULL_TREE; | |
4487 | } | |
4488 | else if (!DECL_EXTERNAL_LINKAGE_P (decl)) | |
4489 | { | |
4490 | error ("%qE is not a valid template argument of type %qT " | |
4491 | "because %qD does not have external linkage", | |
4492 | expr, type, decl); | |
4493 | return NULL_TREE; | |
4494 | } | |
4495 | } | |
b6ab6892 GB |
4496 | |
4497 | expr = decay_conversion (expr); | |
4498 | if (expr == error_mark_node) | |
4499 | return error_mark_node; | |
75650646 | 4500 | |
b6ab6892 GB |
4501 | expr = perform_qualification_conversions (type, expr); |
4502 | if (expr == error_mark_node) | |
4503 | return error_mark_node; | |
b6ab6892 GB |
4504 | } |
4505 | /* [temp.arg.nontype]/5, bullet 3 | |
4506 | ||
4507 | For a non-type template-parameter of type reference to object, no | |
4508 | conversions apply. The type referred to by the reference may be more | |
4509 | cv-qualified than the (otherwise identical) type of the | |
4510 | template-argument. The template-parameter is bound directly to the | |
4511 | template-argument, which must be an lvalue. */ | |
4512 | else if (TYPE_REF_OBJ_P (type)) | |
4513 | { | |
4514 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type), | |
4515 | expr_type)) | |
4516 | return error_mark_node; | |
75650646 | 4517 | |
b6ab6892 GB |
4518 | if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type)) |
4519 | { | |
4520 | error ("%qE is not a valid template argument for type %qT " | |
4521 | "because of conflicts in cv-qualification", expr, type); | |
4522 | return NULL_TREE; | |
4523 | } | |
c8094d83 | 4524 | |
b6ab6892 GB |
4525 | if (!real_lvalue_p (expr)) |
4526 | { | |
4527 | error ("%qE is not a valid template argument for type %qT " | |
944fd207 | 4528 | "because it is not an lvalue", expr, type); |
b6ab6892 GB |
4529 | return NULL_TREE; |
4530 | } | |
e6e174e5 | 4531 | |
b6ab6892 | 4532 | /* [temp.arg.nontype]/1 |
75650646 | 4533 | |
b6ab6892 GB |
4534 | A template-argument for a non-type, non-template template-parameter |
4535 | shall be one of: [...] | |
75650646 | 4536 | |
03fd3f84 | 4537 | -- the address of an object or function with external linkage. */ |
b6ab6892 GB |
4538 | if (!DECL_EXTERNAL_LINKAGE_P (expr)) |
4539 | { | |
4540 | error ("%qE is not a valid template argument for type %qT " | |
4541 | "because object %qD has not external linkage", | |
4542 | expr, type, expr); | |
4543 | return NULL_TREE; | |
4544 | } | |
0dc09a61 | 4545 | |
b6ab6892 GB |
4546 | expr = build_nop (type, build_address (expr)); |
4547 | } | |
4548 | /* [temp.arg.nontype]/5, bullet 4 | |
4549 | ||
4550 | For a non-type template-parameter of type pointer to function, only | |
4551 | the function-to-pointer conversion (_conv.func_) is applied. If the | |
4552 | template-argument represents a set of overloaded functions (or a | |
4553 | pointer to such), the matching function is selected from the set | |
4554 | (_over.over_). */ | |
4555 | else if (TYPE_PTRFN_P (type)) | |
4556 | { | |
4557 | /* If the argument is a template-id, we might not have enough | |
2c164de6 | 4558 | context information to decay the pointer. */ |
b6ab6892 GB |
4559 | if (!type_unknown_p (expr_type)) |
4560 | { | |
4561 | expr = decay_conversion (expr); | |
4562 | if (expr == error_mark_node) | |
4563 | return error_mark_node; | |
4564 | } | |
75650646 | 4565 | |
b6ab6892 GB |
4566 | expr = convert_nontype_argument_function (type, expr); |
4567 | if (!expr || expr == error_mark_node) | |
4568 | return expr; | |
4569 | } | |
4570 | /* [temp.arg.nontype]/5, bullet 5 | |
75650646 | 4571 | |
b6ab6892 GB |
4572 | For a non-type template-parameter of type reference to function, no |
4573 | conversions apply. If the template-argument represents a set of | |
4574 | overloaded functions, the matching function is selected from the set | |
4575 | (_over.over_). */ | |
4576 | else if (TYPE_REFFN_P (type)) | |
4577 | { | |
4578 | if (TREE_CODE (expr) == ADDR_EXPR) | |
4579 | { | |
4580 | error ("%qE is not a valid template argument for type %qT " | |
4581 | "because it is a pointer", expr, type); | |
4582 | inform ("try using %qE instead", TREE_OPERAND (expr, 0)); | |
4583 | return NULL_TREE; | |
4584 | } | |
75650646 | 4585 | |
b6ab6892 GB |
4586 | expr = convert_nontype_argument_function (TREE_TYPE (type), expr); |
4587 | if (!expr || expr == error_mark_node) | |
4588 | return expr; | |
75650646 | 4589 | |
7866705a | 4590 | expr = build_nop (type, build_address (expr)); |
b6ab6892 GB |
4591 | } |
4592 | /* [temp.arg.nontype]/5, bullet 6 | |
75650646 | 4593 | |
b6ab6892 GB |
4594 | For a non-type template-parameter of type pointer to member function, |
4595 | no conversions apply. If the template-argument represents a set of | |
4596 | overloaded member functions, the matching member function is selected | |
4597 | from the set (_over.over_). */ | |
4598 | else if (TYPE_PTRMEMFUNC_P (type)) | |
4599 | { | |
4600 | expr = instantiate_type (type, expr, tf_none); | |
4601 | if (expr == error_mark_node) | |
4602 | return error_mark_node; | |
75650646 | 4603 | |
b6ab6892 GB |
4604 | /* There is no way to disable standard conversions in |
4605 | resolve_address_of_overloaded_function (called by | |
4606 | instantiate_type). It is possible that the call succeeded by | |
4607 | converting &B::I to &D::I (where B is a base of D), so we need | |
4608 | to reject this conversion here. | |
75650646 | 4609 | |
b6ab6892 GB |
4610 | Actually, even if there was a way to disable standard conversions, |
4611 | it would still be better to reject them here so that we can | |
4612 | provide a superior diagnostic. */ | |
4613 | if (!same_type_p (TREE_TYPE (expr), type)) | |
4614 | { | |
4615 | /* Make sure we are just one standard conversion off. */ | |
4616 | gcc_assert (can_convert (type, TREE_TYPE (expr))); | |
4617 | error ("%qE is not a valid template argument for type %qT " | |
4618 | "because it is of type %qT", expr, type, | |
4619 | TREE_TYPE (expr)); | |
4620 | inform ("standard conversions are not allowed in this context"); | |
4621 | return NULL_TREE; | |
4622 | } | |
4623 | } | |
4624 | /* [temp.arg.nontype]/5, bullet 7 | |
59e7a901 | 4625 | |
b6ab6892 GB |
4626 | For a non-type template-parameter of type pointer to data member, |
4627 | qualification conversions (_conv.qual_) are applied. */ | |
4628 | else if (TYPE_PTRMEM_P (type)) | |
4629 | { | |
4630 | expr = perform_qualification_conversions (type, expr); | |
4631 | if (expr == error_mark_node) | |
75650646 | 4632 | return expr; |
75650646 | 4633 | } |
b6ab6892 GB |
4634 | /* A template non-type parameter must be one of the above. */ |
4635 | else | |
4636 | gcc_unreachable (); | |
75650646 | 4637 | |
b6ab6892 GB |
4638 | /* Sanity check: did we actually convert the argument to the |
4639 | right type? */ | |
4640 | gcc_assert (same_type_p (type, TREE_TYPE (expr))); | |
4641 | return expr; | |
75650646 MM |
4642 | } |
4643 | ||
b6ab6892 | 4644 | |
c8094d83 MS |
4645 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
4646 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
4647 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
744fac59 | 4648 | or PARM_DECL. |
c8094d83 | 4649 | |
744fac59 | 4650 | Consider the example: |
e7e93965 MM |
4651 | template <class T> class A; |
4652 | template<template <class U> class TT> class B; | |
744fac59 | 4653 | |
e7e93965 MM |
4654 | For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are |
4655 | the parameters to A, and OUTER_ARGS contains A. */ | |
744fac59 KL |
4656 | |
4657 | static int | |
c8094d83 | 4658 | coerce_template_template_parms (tree parm_parms, |
0cbd7506 MS |
4659 | tree arg_parms, |
4660 | tsubst_flags_t complain, | |
3a978d72 | 4661 | tree in_decl, |
0cbd7506 | 4662 | tree outer_args) |
744fac59 KL |
4663 | { |
4664 | int nparms, nargs, i; | |
4665 | tree parm, arg; | |
4666 | ||
50bc768d NS |
4667 | gcc_assert (TREE_CODE (parm_parms) == TREE_VEC); |
4668 | gcc_assert (TREE_CODE (arg_parms) == TREE_VEC); | |
744fac59 KL |
4669 | |
4670 | nparms = TREE_VEC_LENGTH (parm_parms); | |
4671 | nargs = TREE_VEC_LENGTH (arg_parms); | |
4672 | ||
e7e93965 | 4673 | if (nargs != nparms) |
744fac59 KL |
4674 | return 0; |
4675 | ||
4676 | for (i = 0; i < nparms; ++i) | |
4677 | { | |
2d8ba2c7 LM |
4678 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node |
4679 | || TREE_VEC_ELT (arg_parms, i) == error_mark_node) | |
42b304f1 LM |
4680 | continue; |
4681 | ||
744fac59 KL |
4682 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
4683 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
4684 | ||
4685 | if (arg == NULL_TREE || arg == error_mark_node | |
0cbd7506 | 4686 | || parm == NULL_TREE || parm == error_mark_node) |
744fac59 KL |
4687 | return 0; |
4688 | ||
4689 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
0cbd7506 | 4690 | return 0; |
744fac59 KL |
4691 | |
4692 | switch (TREE_CODE (parm)) | |
4693 | { | |
744fac59 KL |
4694 | case TEMPLATE_DECL: |
4695 | /* We encounter instantiations of templates like | |
4696 | template <template <template <class> class> class TT> | |
4697 | class C; */ | |
700466c2 JM |
4698 | { |
4699 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
4700 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
4701 | ||
8c6ab2db NS |
4702 | if (!coerce_template_template_parms |
4703 | (parmparm, argparm, complain, in_decl, outer_args)) | |
700466c2 JM |
4704 | return 0; |
4705 | } | |
5d80a306 DG |
4706 | /* Fall through. */ |
4707 | ||
4708 | case TYPE_DECL: | |
4709 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)) | |
4710 | != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))) | |
4711 | /* One is a parameter pack, the other is not. */ | |
4712 | return 0; | |
700466c2 | 4713 | break; |
744fac59 KL |
4714 | |
4715 | case PARM_DECL: | |
4716 | /* The tsubst call is used to handle cases such as | |
00bdb87f KL |
4717 | |
4718 | template <int> class C {}; | |
4719 | template <class T, template <T> class TT> class D {}; | |
4720 | D<int, C> d; | |
4721 | ||
744fac59 | 4722 | i.e. the parameter list of TT depends on earlier parameters. */ |
00bdb87f KL |
4723 | if (!dependent_type_p (TREE_TYPE (arg)) |
4724 | && !same_type_p | |
4725 | (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl), | |
4726 | TREE_TYPE (arg))) | |
744fac59 | 4727 | return 0; |
5d80a306 DG |
4728 | |
4729 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)) | |
4730 | != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))) | |
4731 | /* One is a parameter pack, the other is not. */ | |
4732 | return 0; | |
744fac59 | 4733 | break; |
c8094d83 | 4734 | |
744fac59 | 4735 | default: |
315fb5db | 4736 | gcc_unreachable (); |
744fac59 KL |
4737 | } |
4738 | } | |
4739 | return 1; | |
4740 | } | |
4741 | ||
4cf36211 DG |
4742 | /* Verifies that the deduced template arguments (in TARGS) for the |
4743 | template template parameters (in TPARMS) represent valid bindings, | |
4744 | by comparing the template parameter list of each template argument | |
4745 | to the template parameter list of its corresponding template | |
4746 | template parameter, in accordance with DR150. This | |
4747 | routine can only be called after all template arguments have been | |
4748 | deduced. It will return TRUE if all of the template template | |
4749 | parameter bindings are okay, FALSE otherwise. */ | |
4750 | bool | |
4751 | template_template_parm_bindings_ok_p (tree tparms, tree targs) | |
4752 | { | |
4753 | int i, ntparms = TREE_VEC_LENGTH (tparms); | |
4754 | ||
4755 | targs = INNERMOST_TEMPLATE_ARGS (targs); | |
4756 | ||
4757 | for (i = 0; i < ntparms; ++i) | |
4758 | { | |
4759 | tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
4760 | tree targ = TREE_VEC_ELT (targs, i); | |
4761 | ||
4762 | if (TREE_CODE (tparm) == TEMPLATE_DECL && targ) | |
4763 | { | |
4764 | tree packed_args = NULL_TREE; | |
4765 | int idx, len = 1; | |
4766 | ||
4767 | if (ARGUMENT_PACK_P (targ)) | |
4768 | { | |
4769 | /* Look inside the argument pack. */ | |
4770 | packed_args = ARGUMENT_PACK_ARGS (targ); | |
4771 | len = TREE_VEC_LENGTH (packed_args); | |
4772 | } | |
4773 | ||
4774 | for (idx = 0; idx < len; ++idx) | |
4775 | { | |
4776 | tree targ_parms = NULL_TREE; | |
4777 | ||
4778 | if (packed_args) | |
4779 | /* Extract the next argument from the argument | |
4780 | pack. */ | |
4781 | targ = TREE_VEC_ELT (packed_args, idx); | |
4782 | ||
4783 | if (PACK_EXPANSION_P (targ)) | |
4784 | /* Look at the pattern of the pack expansion. */ | |
4785 | targ = PACK_EXPANSION_PATTERN (targ); | |
4786 | ||
4787 | /* Extract the template parameters from the template | |
4788 | argument. */ | |
4789 | if (TREE_CODE (targ) == TEMPLATE_DECL) | |
4790 | targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ); | |
4791 | else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM) | |
4792 | targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ)); | |
4793 | ||
4794 | /* Verify that we can coerce the template template | |
4795 | parameters from the template argument to the template | |
4796 | parameter. This requires an exact match. */ | |
4797 | if (targ_parms | |
4798 | && !coerce_template_template_parms | |
4799 | (DECL_INNERMOST_TEMPLATE_PARMS (tparm), | |
4800 | targ_parms, | |
4801 | tf_none, | |
4802 | tparm, | |
4803 | targs)) | |
4804 | return false; | |
4805 | } | |
4806 | } | |
4807 | } | |
4808 | ||
4809 | /* Everything is okay. */ | |
4810 | return true; | |
4811 | } | |
4812 | ||
8b5b8b7c MM |
4813 | /* Convert the indicated template ARG as necessary to match the |
4814 | indicated template PARM. Returns the converted ARG, or | |
c2ea3a40 NS |
4815 | error_mark_node if the conversion was unsuccessful. Error and |
4816 | warning messages are issued under control of COMPLAIN. This | |
4817 | conversion is for the Ith parameter in the parameter list. ARGS is | |
4818 | the full set of template arguments deduced so far. */ | |
8b5b8b7c MM |
4819 | |
4820 | static tree | |
c8094d83 | 4821 | convert_template_argument (tree parm, |
0cbd7506 MS |
4822 | tree arg, |
4823 | tree args, | |
4824 | tsubst_flags_t complain, | |
4825 | int i, | |
4826 | tree in_decl) | |
8b5b8b7c | 4827 | { |
88b82314 | 4828 | tree orig_arg; |
8b5b8b7c | 4829 | tree val; |
8b5b8b7c | 4830 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; |
c8094d83 | 4831 | |
c8094d83 | 4832 | if (TREE_CODE (arg) == TREE_LIST |
a5ac359a | 4833 | && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF) |
c8094d83 | 4834 | { |
8b5b8b7c MM |
4835 | /* The template argument was the name of some |
4836 | member function. That's usually | |
0e339752 | 4837 | invalid, but static members are OK. In any |
8b5b8b7c MM |
4838 | case, grab the underlying fields/functions |
4839 | and issue an error later if required. */ | |
88b82314 | 4840 | orig_arg = TREE_VALUE (arg); |
8b5b8b7c MM |
4841 | TREE_TYPE (arg) = unknown_type_node; |
4842 | } | |
4843 | ||
88b82314 DG |
4844 | orig_arg = arg; |
4845 | ||
8b5b8b7c MM |
4846 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; |
4847 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
4848 | || requires_tmpl_type); | |
4849 | ||
d732e98f | 4850 | /* When determining whether an argument pack expansion is a template, |
5d80a306 | 4851 | look at the pattern. */ |
88b82314 DG |
4852 | if (TREE_CODE (arg) == TYPE_PACK_EXPANSION) |
4853 | arg = PACK_EXPANSION_PATTERN (arg); | |
5d80a306 DG |
4854 | |
4855 | is_tmpl_type = | |
88b82314 DG |
4856 | ((TREE_CODE (arg) == TEMPLATE_DECL |
4857 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
4858 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
4859 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); | |
c8094d83 | 4860 | |
b8c6534b KL |
4861 | if (is_tmpl_type |
4862 | && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
4863 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) | |
8b5b8b7c | 4864 | arg = TYPE_STUB_DECL (arg); |
8b5b8b7c | 4865 | |
2f939d94 | 4866 | is_type = TYPE_P (arg) || is_tmpl_type; |
8b5b8b7c MM |
4867 | |
4868 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
4869 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
4870 | { | |
0f51ccfc | 4871 | pedwarn ("to refer to a type member of a template parameter, " |
88b82314 | 4872 | "use %<typename %E%>", orig_arg); |
c8094d83 | 4873 | |
88b82314 DG |
4874 | orig_arg = make_typename_type (TREE_OPERAND (arg, 0), |
4875 | TREE_OPERAND (arg, 1), | |
4876 | typename_type, | |
4877 | complain & tf_error); | |
4878 | arg = orig_arg; | |
8b5b8b7c MM |
4879 | is_type = 1; |
4880 | } | |
4881 | if (is_type != requires_type) | |
4882 | { | |
4883 | if (in_decl) | |
4884 | { | |
c2ea3a40 | 4885 | if (complain & tf_error) |
8b5b8b7c | 4886 | { |
0f51ccfc | 4887 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
4888 | "parameter list for %qD", |
4889 | i + 1, in_decl); | |
8b5b8b7c | 4890 | if (is_type) |
0f51ccfc | 4891 | error (" expected a constant of type %qT, got %qT", |
0cbd7506 | 4892 | TREE_TYPE (parm), |
1ad8aeeb | 4893 | (DECL_P (arg) ? DECL_NAME (arg) : orig_arg)); |
d12a7283 | 4894 | else if (requires_tmpl_type) |
88b82314 | 4895 | error (" expected a class template, got %qE", orig_arg); |
8b5b8b7c | 4896 | else |
88b82314 | 4897 | error (" expected a type, got %qE", orig_arg); |
8b5b8b7c MM |
4898 | } |
4899 | } | |
4900 | return error_mark_node; | |
4901 | } | |
4902 | if (is_tmpl_type ^ requires_tmpl_type) | |
4903 | { | |
c2ea3a40 | 4904 | if (in_decl && (complain & tf_error)) |
8b5b8b7c | 4905 | { |
0f51ccfc | 4906 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
4907 | "parameter list for %qD", |
4908 | i + 1, in_decl); | |
8b5b8b7c | 4909 | if (is_tmpl_type) |
0f51ccfc | 4910 | error (" expected a type, got %qT", DECL_NAME (arg)); |
8b5b8b7c | 4911 | else |
88b82314 | 4912 | error (" expected a class template, got %qT", orig_arg); |
8b5b8b7c MM |
4913 | } |
4914 | return error_mark_node; | |
4915 | } | |
c8094d83 | 4916 | |
8b5b8b7c MM |
4917 | if (is_type) |
4918 | { | |
4919 | if (requires_tmpl_type) | |
4920 | { | |
b8c6534b KL |
4921 | if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) |
4922 | /* The number of argument required is not known yet. | |
4923 | Just accept it for now. */ | |
4924 | val = TREE_TYPE (arg); | |
8b5b8b7c MM |
4925 | else |
4926 | { | |
b8c6534b | 4927 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); |
5d80a306 DG |
4928 | tree argparm; |
4929 | ||
88b82314 | 4930 | argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); |
b8c6534b KL |
4931 | |
4932 | if (coerce_template_template_parms (parmparm, argparm, | |
4933 | complain, in_decl, | |
6150b602 | 4934 | args)) |
8b5b8b7c | 4935 | { |
88b82314 | 4936 | val = orig_arg; |
c8094d83 MS |
4937 | |
4938 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
b8c6534b | 4939 | TEMPLATE_DECL. */ |
5d80a306 DG |
4940 | if (val != error_mark_node) |
4941 | { | |
4942 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
4943 | val = TREE_TYPE (val); | |
4944 | else if (TREE_CODE (val) == TYPE_PACK_EXPANSION | |
88b82314 | 4945 | && DECL_TEMPLATE_TEMPLATE_PARM_P (arg)) |
5d80a306 | 4946 | { |
88b82314 | 4947 | val = TREE_TYPE (arg); |
5d80a306 DG |
4948 | val = make_pack_expansion (val); |
4949 | } | |
4950 | } | |
8b5b8b7c | 4951 | } |
b8c6534b KL |
4952 | else |
4953 | { | |
c2ea3a40 | 4954 | if (in_decl && (complain & tf_error)) |
b8c6534b | 4955 | { |
0f51ccfc | 4956 | error ("type/value mismatch at argument %d in " |
0cbd7506 MS |
4957 | "template parameter list for %qD", |
4958 | i + 1, in_decl); | |
0f51ccfc | 4959 | error (" expected a template of type %qD, got %qD", |
88b82314 | 4960 | parm, orig_arg); |
b8c6534b | 4961 | } |
c8094d83 | 4962 | |
b8c6534b KL |
4963 | val = error_mark_node; |
4964 | } | |
8b5b8b7c MM |
4965 | } |
4966 | } | |
4967 | else | |
88b82314 | 4968 | val = orig_arg; |
685e39c2 MM |
4969 | /* We only form one instance of each template specialization. |
4970 | Therefore, if we use a non-canonical variant (i.e., a | |
3db45ab5 | 4971 | typedef), any future messages referring to the type will use |
685e39c2 MM |
4972 | the typedef, which is confusing if those future uses do not |
4973 | themselves also use the typedef. */ | |
4974 | if (TYPE_P (val)) | |
4975 | val = canonical_type_variant (val); | |
8b5b8b7c MM |
4976 | } |
4977 | else | |
4978 | { | |
4393e105 | 4979 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c | 4980 | |
11b810f1 | 4981 | if (invalid_nontype_parm_type_p (t, complain)) |
0cbd7506 | 4982 | return error_mark_node; |
c8094d83 | 4983 | |
88b82314 | 4984 | if (!uses_template_parms (orig_arg) && !uses_template_parms (t)) |
8b5b8b7c MM |
4985 | /* We used to call digest_init here. However, digest_init |
4986 | will report errors, which we don't want when complain | |
4987 | is zero. More importantly, digest_init will try too | |
4988 | hard to convert things: for example, `0' should not be | |
4989 | converted to pointer type at this point according to | |
4990 | the standard. Accepting this is not merely an | |
4991 | extension, since deciding whether or not these | |
4992 | conversions can occur is part of determining which | |
dc957d14 | 4993 | function template to call, or whether a given explicit |
0e339752 | 4994 | argument specification is valid. */ |
88b82314 | 4995 | val = convert_nontype_argument (t, orig_arg); |
8b5b8b7c | 4996 | else |
88b82314 | 4997 | val = orig_arg; |
8b5b8b7c MM |
4998 | |
4999 | if (val == NULL_TREE) | |
5000 | val = error_mark_node; | |
c2ea3a40 | 5001 | else if (val == error_mark_node && (complain & tf_error)) |
88b82314 | 5002 | error ("could not convert template argument %qE to %qT", orig_arg, t); |
8b5b8b7c MM |
5003 | } |
5004 | ||
5005 | return val; | |
5006 | } | |
5007 | ||
b1d7b1c0 DG |
5008 | /* Coerces the remaining template arguments in INNER_ARGS (from |
5009 | ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS. | |
5010 | Returns the coerced argument pack. PARM_IDX is the position of this | |
5011 | parameter in the template parameter list. ARGS is the original | |
5012 | template argument list. */ | |
5013 | static tree | |
5014 | coerce_template_parameter_pack (tree parms, | |
5015 | int parm_idx, | |
5016 | tree args, | |
5017 | tree inner_args, | |
5018 | int arg_idx, | |
5019 | tree new_args, | |
5020 | int* lost, | |
5021 | tree in_decl, | |
5022 | tsubst_flags_t complain) | |
5023 | { | |
5024 | tree parm = TREE_VEC_ELT (parms, parm_idx); | |
5025 | int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; | |
5026 | tree packed_args; | |
5027 | tree argument_pack; | |
5028 | tree packed_types = NULL_TREE; | |
5029 | ||
5030 | if (arg_idx > nargs) | |
5031 | arg_idx = nargs; | |
5032 | ||
5033 | packed_args = make_tree_vec (nargs - arg_idx); | |
5034 | ||
5035 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL | |
5036 | && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm)))) | |
5037 | { | |
5038 | /* When the template parameter is a non-type template | |
5039 | parameter pack whose type uses parameter packs, we need | |
5040 | to look at each of the template arguments | |
5041 | separately. Build a vector of the types for these | |
5042 | non-type template parameters in PACKED_TYPES. */ | |
5043 | tree expansion | |
5044 | = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm))); | |
5045 | packed_types = tsubst_pack_expansion (expansion, args, | |
5046 | complain, in_decl); | |
5047 | ||
5048 | if (packed_types == error_mark_node) | |
5049 | return error_mark_node; | |
5050 | ||
5051 | /* Check that we have the right number of arguments. */ | |
5052 | if (arg_idx < nargs | |
5053 | && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)) | |
5054 | && nargs - arg_idx != TREE_VEC_LENGTH (packed_types)) | |
5055 | { | |
5056 | int needed_parms | |
5057 | = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types); | |
5058 | error ("wrong number of template arguments (%d, should be %d)", | |
5059 | nargs, needed_parms); | |
5060 | return error_mark_node; | |
5061 | } | |
5062 | ||
5063 | /* If we aren't able to check the actual arguments now | |
5064 | (because they haven't been expanded yet), we can at least | |
5065 | verify that all of the types used for the non-type | |
5066 | template parameter pack are, in fact, valid for non-type | |
5067 | template parameters. */ | |
5068 | if (arg_idx < nargs | |
5069 | && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))) | |
5070 | { | |
5071 | int j, len = TREE_VEC_LENGTH (packed_types); | |
5072 | for (j = 0; j < len; ++j) | |
5073 | { | |
5074 | tree t = TREE_VEC_ELT (packed_types, j); | |
5075 | if (invalid_nontype_parm_type_p (t, complain)) | |
5076 | return error_mark_node; | |
5077 | } | |
5078 | } | |
5079 | } | |
5080 | ||
5081 | /* Convert the remaining arguments, which will be a part of the | |
5082 | parameter pack "parm". */ | |
5083 | for (; arg_idx < nargs; ++arg_idx) | |
5084 | { | |
5085 | tree arg = TREE_VEC_ELT (inner_args, arg_idx); | |
5086 | tree actual_parm = TREE_VALUE (parm); | |
5087 | ||
5088 | if (packed_types && !PACK_EXPANSION_P (arg)) | |
5089 | { | |
5090 | /* When we have a vector of types (corresponding to the | |
5091 | non-type template parameter pack that uses parameter | |
5092 | packs in its type, as mention above), and the | |
5093 | argument is not an expansion (which expands to a | |
5094 | currently unknown number of arguments), clone the | |
5095 | parm and give it the next type in PACKED_TYPES. */ | |
5096 | actual_parm = copy_node (actual_parm); | |
5097 | TREE_TYPE (actual_parm) = | |
5098 | TREE_VEC_ELT (packed_types, arg_idx - parm_idx); | |
5099 | } | |
5100 | ||
c343d5a7 PC |
5101 | if (arg != error_mark_node) |
5102 | arg = convert_template_argument (actual_parm, | |
5103 | arg, new_args, complain, parm_idx, | |
5104 | in_decl); | |
b1d7b1c0 DG |
5105 | if (arg == error_mark_node) |
5106 | (*lost)++; | |
5107 | TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg; | |
5108 | } | |
5109 | ||
5110 | if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL | |
5111 | || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL) | |
5112 | argument_pack = make_node (TYPE_ARGUMENT_PACK); | |
5113 | else | |
5114 | { | |
5115 | argument_pack = make_node (NONTYPE_ARGUMENT_PACK); | |
88b82314 | 5116 | TREE_TYPE (argument_pack) |
cdcae745 | 5117 | = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl); |
b1d7b1c0 DG |
5118 | TREE_CONSTANT (argument_pack) = 1; |
5119 | } | |
5120 | ||
5121 | SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args); | |
5122 | return argument_pack; | |
5123 | } | |
5124 | ||
8b5b8b7c MM |
5125 | /* Convert all template arguments to their appropriate types, and |
5126 | return a vector containing the innermost resulting template | |
c2ea3a40 | 5127 | arguments. If any error occurs, return error_mark_node. Error and |
3e4a3562 | 5128 | warning messages are issued under control of COMPLAIN. |
75650646 | 5129 | |
e7e93965 | 5130 | If REQUIRE_ALL_ARGS is false, argument deduction will be performed |
84dc00e8 | 5131 | for arguments not specified in ARGS. Otherwise, if |
e7e93965 MM |
5132 | USE_DEFAULT_ARGS is true, default arguments will be used to fill in |
5133 | unspecified arguments. If REQUIRE_ALL_ARGS is true, but | |
5134 | USE_DEFAULT_ARGS is false, then all arguments must be specified in | |
5135 | ARGS. */ | |
c8094d83 | 5136 | |
8d08fdba | 5137 | static tree |
c8094d83 | 5138 | coerce_template_parms (tree parms, |
0cbd7506 MS |
5139 | tree args, |
5140 | tree in_decl, | |
3a978d72 | 5141 | tsubst_flags_t complain, |
e7e93965 MM |
5142 | bool require_all_args, |
5143 | bool use_default_args) | |
8d08fdba | 5144 | { |
b1d7b1c0 | 5145 | int nparms, nargs, parm_idx, arg_idx, lost = 0; |
e4a84209 | 5146 | tree inner_args; |
8b5b8b7c MM |
5147 | tree new_args; |
5148 | tree new_inner_args; | |
a2c5ed87 | 5149 | bool saved_skip_evaluation; |
a292b002 | 5150 | |
5d80a306 DG |
5151 | /* When used as a boolean value, indicates whether this is a |
5152 | variadic template parameter list. Since it's an int, we can also | |
5153 | subtract it from nparms to get the number of non-variadic | |
5154 | parameters. */ | |
b1d7b1c0 | 5155 | int variadic_p = 0; |
5d80a306 DG |
5156 | |
5157 | inner_args | |
5158 | = expand_template_argument_pack (INNERMOST_TEMPLATE_ARGS (args)); | |
5159 | ||
bf12d54d | 5160 | nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; |
a292b002 MS |
5161 | nparms = TREE_VEC_LENGTH (parms); |
5162 | ||
b1d7b1c0 DG |
5163 | /* Determine if there are any parameter packs. */ |
5164 | for (parm_idx = 0; parm_idx < nparms; ++parm_idx) | |
5165 | { | |
5166 | tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx)); | |
5167 | if (template_parameter_pack_p (tparm)) | |
5168 | { | |
5169 | variadic_p = 1; | |
5170 | break; | |
5171 | } | |
5172 | } | |
5173 | ||
5d80a306 DG |
5174 | if ((nargs > nparms - variadic_p && !variadic_p) |
5175 | || (nargs < nparms - variadic_p | |
e7e93965 MM |
5176 | && require_all_args |
5177 | && (!use_default_args | |
42b304f1 LM |
5178 | || (TREE_VEC_ELT (parms, nargs) != error_mark_node |
5179 | && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))) | |
8d08fdba | 5180 | { |
c8094d83 | 5181 | if (complain & tf_error) |
75650646 | 5182 | { |
5d80a306 DG |
5183 | const char *or_more = ""; |
5184 | if (variadic_p) | |
5185 | { | |
5186 | or_more = " or more"; | |
5187 | --nparms; | |
5188 | } | |
5189 | ||
5190 | error ("wrong number of template arguments (%d, should be %d%s)", | |
5191 | nargs, nparms, or_more); | |
c8094d83 | 5192 | |
75650646 | 5193 | if (in_decl) |
dee15844 | 5194 | error ("provided for %q+D", in_decl); |
75650646 MM |
5195 | } |
5196 | ||
8d08fdba MS |
5197 | return error_mark_node; |
5198 | } | |
5199 | ||
a2c5ed87 MM |
5200 | /* We need to evaluate the template arguments, even though this |
5201 | template-id may be nested within a "sizeof". */ | |
5202 | saved_skip_evaluation = skip_evaluation; | |
5203 | skip_evaluation = false; | |
f31c0a32 | 5204 | new_inner_args = make_tree_vec (nparms); |
8b5b8b7c | 5205 | new_args = add_outermost_template_args (args, new_inner_args); |
b1d7b1c0 | 5206 | for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++) |
8d08fdba | 5207 | { |
8b5b8b7c MM |
5208 | tree arg; |
5209 | tree parm; | |
e4a84209 | 5210 | |
8b5b8b7c | 5211 | /* Get the Ith template parameter. */ |
b1d7b1c0 | 5212 | parm = TREE_VEC_ELT (parms, parm_idx); |
42b304f1 LM |
5213 | |
5214 | if (parm == error_mark_node) | |
2d8ba2c7 | 5215 | { |
b1d7b1c0 | 5216 | TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node; |
42b304f1 | 5217 | continue; |
2d8ba2c7 | 5218 | } |
75650646 | 5219 | |
b1d7b1c0 DG |
5220 | /* Calculate the next argument. */ |
5221 | if (template_parameter_pack_p (TREE_VALUE (parm))) | |
5d80a306 | 5222 | { |
b1d7b1c0 DG |
5223 | /* All remaining arguments will be placed in the |
5224 | template parameter pack PARM. */ | |
5225 | arg = coerce_template_parameter_pack (parms, parm_idx, args, | |
5226 | inner_args, arg_idx, | |
5227 | new_args, &lost, | |
5228 | in_decl, complain); | |
5229 | ||
5230 | /* Store this argument. */ | |
5231 | if (arg == error_mark_node) | |
5232 | lost++; | |
5233 | TREE_VEC_ELT (new_inner_args, parm_idx) = arg; | |
5234 | ||
5235 | /* We are done with all of the arguments. */ | |
5236 | arg_idx = nargs; | |
5237 | ||
5238 | continue; | |
5239 | } | |
5240 | else if (arg_idx < nargs) | |
5241 | { | |
5242 | arg = TREE_VEC_ELT (inner_args, arg_idx); | |
5243 | ||
5244 | if (arg && PACK_EXPANSION_P (arg)) | |
5d80a306 | 5245 | { |
4cf36211 DG |
5246 | if (complain & tf_error) |
5247 | { | |
5248 | /* If ARG is a pack expansion, but PARM is not a | |
5249 | template parameter pack (if it were, we would have | |
5250 | handled it above), we're trying to expand into a | |
5251 | fixed-length argument list. */ | |
5252 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
5253 | error ("cannot expand %<%E%> into a fixed-length " | |
5254 | "argument list", arg); | |
5255 | else | |
5256 | error ("cannot expand %<%T%> into a fixed-length " | |
5257 | "argument list", arg); | |
5258 | } | |
c343d5a7 | 5259 | return error_mark_node; |
5d80a306 DG |
5260 | } |
5261 | } | |
e7e93965 | 5262 | else if (require_all_args) |
b1d7b1c0 DG |
5263 | /* There must be a default arg in this case. */ |
5264 | arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args, | |
5265 | complain, in_decl); | |
ffd49b19 NS |
5266 | else |
5267 | break; | |
c8094d83 | 5268 | |
ffd49b19 | 5269 | if (arg == error_mark_node) |
e34b0922 KL |
5270 | { |
5271 | if (complain & tf_error) | |
b1d7b1c0 | 5272 | error ("template argument %d is invalid", arg_idx + 1); |
e34b0922 | 5273 | } |
b1d7b1c0 DG |
5274 | else if (!arg) |
5275 | /* This only occurs if there was an error in the template | |
5276 | parameter list itself (which we would already have | |
5277 | reported) that we are trying to recover from, e.g., a class | |
5278 | template with a parameter list such as | |
5279 | template<typename..., typename>. */ | |
5280 | return error_mark_node; | |
c8094d83 MS |
5281 | else |
5282 | arg = convert_template_argument (TREE_VALUE (parm), | |
b1d7b1c0 DG |
5283 | arg, new_args, complain, |
5284 | parm_idx, in_decl); | |
c8094d83 | 5285 | |
8b5b8b7c | 5286 | if (arg == error_mark_node) |
8d08fdba | 5287 | lost++; |
b1d7b1c0 | 5288 | TREE_VEC_ELT (new_inner_args, arg_idx) = arg; |
8d08fdba | 5289 | } |
a2c5ed87 | 5290 | skip_evaluation = saved_skip_evaluation; |
8b5b8b7c | 5291 | |
8d08fdba MS |
5292 | if (lost) |
5293 | return error_mark_node; | |
8b5b8b7c MM |
5294 | |
5295 | return new_inner_args; | |
8d08fdba MS |
5296 | } |
5297 | ||
34016c81 JM |
5298 | /* Returns 1 if template args OT and NT are equivalent. */ |
5299 | ||
d8e178a0 | 5300 | static int |
3a978d72 | 5301 | template_args_equal (tree ot, tree nt) |
34016c81 JM |
5302 | { |
5303 | if (nt == ot) | |
5304 | return 1; | |
74601d7c | 5305 | |
34016c81 JM |
5306 | if (TREE_CODE (nt) == TREE_VEC) |
5307 | /* For member templates */ | |
74601d7c | 5308 | return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); |
5d80a306 DG |
5309 | else if (PACK_EXPANSION_P (ot)) |
5310 | return PACK_EXPANSION_P (nt) | |
5311 | && template_args_equal (PACK_EXPANSION_PATTERN (ot), | |
5312 | PACK_EXPANSION_PATTERN (nt)); | |
74601d7c | 5313 | else if (TYPE_P (nt)) |
660845bf | 5314 | return TYPE_P (ot) && same_type_p (ot, nt); |
74601d7c KL |
5315 | else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) |
5316 | return 0; | |
34016c81 | 5317 | else |
c8a209ca | 5318 | return cp_tree_equal (ot, nt); |
34016c81 JM |
5319 | } |
5320 | ||
5321 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
5322 | of template arguments. Returns 0 otherwise. */ |
5323 | ||
6757edfe | 5324 | int |
3a978d72 | 5325 | comp_template_args (tree oldargs, tree newargs) |
5566b478 MS |
5326 | { |
5327 | int i; | |
5328 | ||
5d80a306 DG |
5329 | oldargs = expand_template_argument_pack (oldargs); |
5330 | newargs = expand_template_argument_pack (newargs); | |
5331 | ||
386b8a85 JM |
5332 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
5333 | return 0; | |
5334 | ||
5566b478 MS |
5335 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
5336 | { | |
5337 | tree nt = TREE_VEC_ELT (newargs, i); | |
5338 | tree ot = TREE_VEC_ELT (oldargs, i); | |
5339 | ||
34016c81 | 5340 | if (! template_args_equal (ot, nt)) |
61a127b3 | 5341 | return 0; |
5566b478 MS |
5342 | } |
5343 | return 1; | |
5344 | } | |
5345 | ||
5566b478 | 5346 | static void |
3a978d72 | 5347 | add_pending_template (tree d) |
5566b478 | 5348 | { |
3ae18eaf JM |
5349 | tree ti = (TYPE_P (d) |
5350 | ? CLASSTYPE_TEMPLATE_INFO (d) | |
5351 | : DECL_TEMPLATE_INFO (d)); | |
e2c3721c | 5352 | struct pending_template *pt; |
3ae18eaf | 5353 | int level; |
e92cc029 | 5354 | |
824b9a4c | 5355 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
5356 | return; |
5357 | ||
3ae18eaf JM |
5358 | /* We are called both from instantiate_decl, where we've already had a |
5359 | tinst_level pushed, and instantiate_template, where we haven't. | |
5360 | Compensate. */ | |
e2c3721c | 5361 | level = !current_tinst_level || current_tinst_level->decl != d; |
3ae18eaf JM |
5362 | |
5363 | if (level) | |
5364 | push_tinst_level (d); | |
5365 | ||
e2c3721c PB |
5366 | pt = GGC_NEW (struct pending_template); |
5367 | pt->next = NULL; | |
5368 | pt->tinst = current_tinst_level; | |
46ccf50a | 5369 | if (last_pending_template) |
e2c3721c | 5370 | last_pending_template->next = pt; |
46ccf50a JM |
5371 | else |
5372 | pending_templates = pt; | |
5373 | ||
5374 | last_pending_template = pt; | |
5375 | ||
824b9a4c | 5376 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
3ae18eaf JM |
5377 | |
5378 | if (level) | |
5379 | pop_tinst_level (); | |
5566b478 MS |
5380 | } |
5381 | ||
386b8a85 | 5382 | |
4ba126e4 MM |
5383 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and |
5384 | ARGLIST. Valid choices for FNS are given in the cp-tree.def | |
5385 | documentation for TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
5386 | |
5387 | tree | |
3a978d72 | 5388 | lookup_template_function (tree fns, tree arglist) |
386b8a85 | 5389 | { |
2c73f9f5 | 5390 | tree type; |
050367a3 | 5391 | |
4ba126e4 MM |
5392 | if (fns == error_mark_node || arglist == error_mark_node) |
5393 | return error_mark_node; | |
5394 | ||
50bc768d | 5395 | gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); |
4230cec2 NS |
5396 | gcc_assert (fns && (is_overloaded_fn (fns) |
5397 | || TREE_CODE (fns) == IDENTIFIER_NODE)); | |
4ba126e4 | 5398 | |
50ad9642 MM |
5399 | if (BASELINK_P (fns)) |
5400 | { | |
f293ce4b RS |
5401 | BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR, |
5402 | unknown_type_node, | |
5403 | BASELINK_FUNCTIONS (fns), | |
5404 | arglist); | |
50ad9642 MM |
5405 | return fns; |
5406 | } | |
5407 | ||
2c73f9f5 ML |
5408 | type = TREE_TYPE (fns); |
5409 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
5410 | type = unknown_type_node; | |
c8094d83 | 5411 | |
f293ce4b | 5412 | return build2 (TEMPLATE_ID_EXPR, type, fns, arglist); |
386b8a85 JM |
5413 | } |
5414 | ||
a2b60a0e MM |
5415 | /* Within the scope of a template class S<T>, the name S gets bound |
5416 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
5417 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
5418 | or one of its enclosing classes, and that type is a template, | |
5419 | return the associated TEMPLATE_DECL. Otherwise, the original | |
5420 | DECL is returned. */ | |
5421 | ||
a723baf1 | 5422 | tree |
3a978d72 | 5423 | maybe_get_template_decl_from_type_decl (tree decl) |
a2b60a0e MM |
5424 | { |
5425 | return (decl != NULL_TREE | |
c8094d83 | 5426 | && TREE_CODE (decl) == TYPE_DECL |
a2b60a0e | 5427 | && DECL_ARTIFICIAL (decl) |
511b60ff | 5428 | && CLASS_TYPE_P (TREE_TYPE (decl)) |
c8094d83 | 5429 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
a2b60a0e MM |
5430 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; |
5431 | } | |
386b8a85 | 5432 | |
8d08fdba MS |
5433 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
5434 | parameters, find the desired type. | |
5435 | ||
5436 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
8d08fdba MS |
5437 | |
5438 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
c8094d83 | 5439 | instantiate. |
75650646 | 5440 | |
838dfd8a | 5441 | If ENTERING_SCOPE is nonzero, we are about to enter the scope of |
36a117a5 | 5442 | the class we are looking up. |
c8094d83 | 5443 | |
c2ea3a40 | 5444 | Issue error and warning messages under control of COMPLAIN. |
36a117a5 | 5445 | |
75650646 MM |
5446 | If the template class is really a local class in a template |
5447 | function, then the FUNCTION_CONTEXT is the function in which it is | |
c8094d83 | 5448 | being instantiated. |
b6ab6892 GB |
5449 | |
5450 | ??? Note that this function is currently called *twice* for each | |
5451 | template-id: the first time from the parser, while creating the | |
5452 | incomplete type (finish_template_type), and the second type during the | |
5453 | real instantiation (instantiate_template_class). This is surely something | |
5454 | that we want to avoid. It also causes some problems with argument | |
5455 | coercion (see convert_nontype_argument for more information on this). */ | |
e92cc029 | 5456 | |
8d08fdba | 5457 | tree |
c8094d83 | 5458 | lookup_template_class (tree d1, |
0cbd7506 MS |
5459 | tree arglist, |
5460 | tree in_decl, | |
5461 | tree context, | |
5462 | int entering_scope, | |
5463 | tsubst_flags_t complain) | |
8d08fdba | 5464 | { |
a703fb38 | 5465 | tree template = NULL_TREE, parmlist; |
dbfe2124 | 5466 | tree t; |
c8094d83 | 5467 | |
fd295cb2 | 5468 | timevar_push (TV_NAME_LOOKUP); |
c8094d83 | 5469 | |
5566b478 MS |
5470 | if (TREE_CODE (d1) == IDENTIFIER_NODE) |
5471 | { | |
90ea9897 MM |
5472 | tree value = innermost_non_namespace_value (d1); |
5473 | if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) | |
5474 | template = value; | |
73b0fce8 KL |
5475 | else |
5476 | { | |
2c73f9f5 ML |
5477 | if (context) |
5478 | push_decl_namespace (context); | |
10e6657a | 5479 | template = lookup_name (d1); |
3ebc5c52 | 5480 | template = maybe_get_template_decl_from_type_decl (template); |
2c73f9f5 ML |
5481 | if (context) |
5482 | pop_decl_namespace (); | |
73b0fce8 | 5483 | } |
8d019cef JM |
5484 | if (template) |
5485 | context = DECL_CONTEXT (template); | |
5566b478 | 5486 | } |
c91a56d2 MS |
5487 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
5488 | { | |
802dbc34 JM |
5489 | tree type = TREE_TYPE (d1); |
5490 | ||
5491 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
5492 | an implicit typename for the second A. Deal with it. */ | |
5493 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
5494 | type = TREE_TYPE (type); | |
c8094d83 | 5495 | |
802dbc34 | 5496 | if (CLASSTYPE_TEMPLATE_INFO (type)) |
f3400fe2 | 5497 | { |
802dbc34 | 5498 | template = CLASSTYPE_TI_TEMPLATE (type); |
f3400fe2 JM |
5499 | d1 = DECL_NAME (template); |
5500 | } | |
c91a56d2 | 5501 | } |
c8094d83 | 5502 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
2f939d94 | 5503 | || (TYPE_P (d1) && IS_AGGR_TYPE (d1))) |
5566b478 | 5504 | { |
ed44da02 | 5505 | template = TYPE_TI_TEMPLATE (d1); |
5566b478 MS |
5506 | d1 = DECL_NAME (template); |
5507 | } | |
93cdc044 | 5508 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
17aec3eb | 5509 | && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) |
93cdc044 JM |
5510 | { |
5511 | template = d1; | |
5512 | d1 = DECL_NAME (template); | |
5513 | context = DECL_CONTEXT (template); | |
5514 | } | |
8d08fdba | 5515 | |
90ea9897 | 5516 | /* Issue an error message if we didn't find a template. */ |
8d08fdba | 5517 | if (! template) |
f3400fe2 | 5518 | { |
c2ea3a40 | 5519 | if (complain & tf_error) |
0cbd7506 | 5520 | error ("%qT is not a template", d1); |
fd295cb2 | 5521 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
f3400fe2 | 5522 | } |
2c73f9f5 | 5523 | |
a87b4257 | 5524 | if (TREE_CODE (template) != TEMPLATE_DECL |
0cbd7506 | 5525 | /* Make sure it's a user visible template, if it was named by |
42eaed49 NS |
5526 | the user. */ |
5527 | || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template) | |
5528 | && !PRIMARY_TEMPLATE_P (template))) | |
8d08fdba | 5529 | { |
c2ea3a40 | 5530 | if (complain & tf_error) |
0cbd7506 MS |
5531 | { |
5532 | error ("non-template type %qT used as a template", d1); | |
5533 | if (in_decl) | |
dee15844 | 5534 | error ("for template declaration %q+D", in_decl); |
f9c244b8 | 5535 | } |
fd295cb2 | 5536 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
8d08fdba | 5537 | } |
8d08fdba | 5538 | |
42eaed49 | 5539 | complain &= ~tf_user; |
c8094d83 | 5540 | |
73b0fce8 KL |
5541 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
5542 | { | |
5543 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
0cbd7506 | 5544 | template arguments */ |
73b0fce8 | 5545 | |
1899c3a4 | 5546 | tree parm; |
73b0fce8 | 5547 | tree arglist2; |
ef3b7b17 | 5548 | tree outer; |
73b0fce8 | 5549 | |
73b0fce8 KL |
5550 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
5551 | ||
4f96ff63 KL |
5552 | /* Consider an example where a template template parameter declared as |
5553 | ||
5554 | template <class T, class U = std::allocator<T> > class TT | |
5555 | ||
c8094d83 MS |
5556 | The template parameter level of T and U are one level larger than |
5557 | of TT. To proper process the default argument of U, say when an | |
4f96ff63 | 5558 | instantiation `TT<int>' is seen, we need to build the full |
342cea95 KL |
5559 | arguments containing {int} as the innermost level. Outer levels, |
5560 | available when not appearing as default template argument, can be | |
ef3b7b17 | 5561 | obtained from the arguments of the enclosing template. |
4f96ff63 | 5562 | |
342cea95 KL |
5563 | Suppose that TT is later substituted with std::vector. The above |
5564 | instantiation is `TT<int, std::allocator<T> >' with TT at | |
5565 | level 1, and T at level 2, while the template arguments at level 1 | |
5566 | becomes {std::vector} and the inner level 2 is {int}. */ | |
5567 | ||
ef3b7b17 JM |
5568 | outer = DECL_CONTEXT (template); |
5569 | if (outer) | |
5570 | outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer))); | |
5571 | else if (current_template_parms) | |
5572 | /* This is an argument of the current template, so we haven't set | |
5573 | DECL_CONTEXT yet. */ | |
5574 | outer = current_template_args (); | |
5575 | ||
5576 | if (outer) | |
5577 | arglist = add_to_template_args (outer, arglist); | |
4f96ff63 | 5578 | |
f9c244b8 | 5579 | arglist2 = coerce_template_parms (parmlist, arglist, template, |
3db45ab5 | 5580 | complain, |
e7e93965 MM |
5581 | /*require_all_args=*/true, |
5582 | /*use_default_args=*/true); | |
3e4a3562 | 5583 | if (arglist2 == error_mark_node |
544aef8c | 5584 | || (!uses_template_parms (arglist2) |
3e4a3562 | 5585 | && check_instantiated_args (template, arglist2, complain))) |
0cbd7506 | 5586 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
73b0fce8 | 5587 | |
dac65501 | 5588 | parm = bind_template_template_parm (TREE_TYPE (template), arglist2); |
fd295cb2 | 5589 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm); |
73b0fce8 | 5590 | } |
c8094d83 | 5591 | else |
8d08fdba | 5592 | { |
36a117a5 | 5593 | tree template_type = TREE_TYPE (template); |
7ac7b28f | 5594 | tree gen_tmpl; |
36a117a5 MM |
5595 | tree type_decl; |
5596 | tree found = NULL_TREE; | |
5597 | int arg_depth; | |
5598 | int parm_depth; | |
dbfe2124 | 5599 | int is_partial_instantiation; |
830bfa74 | 5600 | |
7ac7b28f MM |
5601 | gen_tmpl = most_general_template (template); |
5602 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); | |
36a117a5 MM |
5603 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
5604 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
5605 | ||
5606 | if (arg_depth == 1 && parm_depth > 1) | |
5607 | { | |
39c01e4c | 5608 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
5609 | For example, given: |
5610 | ||
5611 | template <class T> struct S1 { | |
0cbd7506 | 5612 | template <class U> struct S2 {}; |
36a117a5 | 5613 | template <class U> struct S2<U*> {}; |
0cbd7506 | 5614 | }; |
c8094d83 | 5615 | |
36a117a5 MM |
5616 | we will be called with an ARGLIST of `U*', but the |
5617 | TEMPLATE will be `template <class T> template | |
5618 | <class U> struct S1<T>::S2'. We must fill in the missing | |
5619 | arguments. */ | |
c8094d83 | 5620 | arglist |
7ac7b28f MM |
5621 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), |
5622 | arglist); | |
36a117a5 MM |
5623 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
5624 | } | |
5566b478 | 5625 | |
41f5d4b1 | 5626 | /* Now we should have enough arguments. */ |
50bc768d | 5627 | gcc_assert (parm_depth == arg_depth); |
c8094d83 | 5628 | |
7ac7b28f MM |
5629 | /* From here on, we're only interested in the most general |
5630 | template. */ | |
5631 | template = gen_tmpl; | |
5632 | ||
36a117a5 MM |
5633 | /* Calculate the BOUND_ARGS. These will be the args that are |
5634 | actually tsubst'd into the definition to create the | |
5635 | instantiation. */ | |
5636 | if (parm_depth > 1) | |
830bfa74 MM |
5637 | { |
5638 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 5639 | int i; |
e4a84209 | 5640 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 5641 | |
f31c0a32 | 5642 | tree bound_args = make_tree_vec (parm_depth); |
c8094d83 | 5643 | |
e4a84209 | 5644 | for (i = saved_depth, |
c8094d83 | 5645 | t = DECL_TEMPLATE_PARMS (template); |
e4a84209 | 5646 | i > 0 && t != NULL_TREE; |
830bfa74 | 5647 | --i, t = TREE_CHAIN (t)) |
e4a84209 | 5648 | { |
ee3071ef NS |
5649 | tree a = coerce_template_parms (TREE_VALUE (t), |
5650 | arglist, template, | |
3db45ab5 | 5651 | complain, |
e7e93965 MM |
5652 | /*require_all_args=*/true, |
5653 | /*use_default_args=*/true); | |
88e98cfe KL |
5654 | |
5655 | /* Don't process further if one of the levels fails. */ | |
5656 | if (a == error_mark_node) | |
5657 | { | |
5658 | /* Restore the ARGLIST to its full size. */ | |
5659 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
5660 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
5661 | } | |
c8094d83 | 5662 | |
e4a84209 MM |
5663 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); |
5664 | ||
5665 | /* We temporarily reduce the length of the ARGLIST so | |
5666 | that coerce_template_parms will see only the arguments | |
5667 | corresponding to the template parameters it is | |
5668 | examining. */ | |
5669 | TREE_VEC_LENGTH (arglist)--; | |
5670 | } | |
5671 | ||
5672 | /* Restore the ARGLIST to its full size. */ | |
5673 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
5674 | ||
36a117a5 | 5675 | arglist = bound_args; |
830bfa74 MM |
5676 | } |
5677 | else | |
36a117a5 MM |
5678 | arglist |
5679 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
f9a7ae04 | 5680 | INNERMOST_TEMPLATE_ARGS (arglist), |
f9c244b8 | 5681 | template, |
3db45ab5 | 5682 | complain, |
e7e93965 MM |
5683 | /*require_all_args=*/true, |
5684 | /*use_default_args=*/true); | |
36a117a5 | 5685 | |
c353b8e3 | 5686 | if (arglist == error_mark_node) |
36a117a5 | 5687 | /* We were unable to bind the arguments. */ |
fd295cb2 | 5688 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
5566b478 | 5689 | |
36a117a5 MM |
5690 | /* In the scope of a template class, explicit references to the |
5691 | template class refer to the type of the template, not any | |
5692 | instantiation of it. For example, in: | |
c8094d83 | 5693 | |
36a117a5 MM |
5694 | template <class T> class C { void f(C<T>); } |
5695 | ||
5696 | the `C<T>' is just the same as `C'. Outside of the | |
5697 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 5698 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
5699 | arglist)) |
5700 | { | |
5701 | found = template_type; | |
c8094d83 | 5702 | |
36a117a5 | 5703 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) |
5566b478 | 5704 | { |
36a117a5 | 5705 | tree ctx; |
c8094d83 MS |
5706 | |
5707 | for (ctx = current_class_type; | |
5f04800c KL |
5708 | ctx && TREE_CODE (ctx) != NAMESPACE_DECL; |
5709 | ctx = (TYPE_P (ctx) | |
5710 | ? TYPE_CONTEXT (ctx) | |
5711 | : DECL_CONTEXT (ctx))) | |
5712 | if (TYPE_P (ctx) && same_type_p (ctx, template_type)) | |
5713 | goto found_ctx; | |
c8094d83 | 5714 | |
6df5158a NS |
5715 | /* We're not in the scope of the class, so the |
5716 | TEMPLATE_TYPE is not the type we want after all. */ | |
5717 | found = NULL_TREE; | |
5718 | found_ctx:; | |
5566b478 MS |
5719 | } |
5720 | } | |
36a117a5 | 5721 | if (found) |
0cbd7506 | 5722 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
414ea4aa | 5723 | |
c7222c02 MM |
5724 | /* If we already have this specialization, return it. */ |
5725 | found = retrieve_specialization (template, arglist, | |
5726 | /*class_specializations_p=*/false); | |
5727 | if (found) | |
5728 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); | |
5566b478 | 5729 | |
dbfe2124 | 5730 | /* This type is a "partial instantiation" if any of the template |
ab097535 | 5731 | arguments still involve template parameters. Note that we set |
486e4077 MM |
5732 | IS_PARTIAL_INSTANTIATION for partial specializations as |
5733 | well. */ | |
dbfe2124 MM |
5734 | is_partial_instantiation = uses_template_parms (arglist); |
5735 | ||
c353b8e3 MM |
5736 | /* If the deduced arguments are invalid, then the binding |
5737 | failed. */ | |
5738 | if (!is_partial_instantiation | |
5739 | && check_instantiated_args (template, | |
5740 | INNERMOST_TEMPLATE_ARGS (arglist), | |
5741 | complain)) | |
5742 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
c8094d83 MS |
5743 | |
5744 | if (!is_partial_instantiation | |
3ebc5c52 MM |
5745 | && !PRIMARY_TEMPLATE_P (template) |
5746 | && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) | |
5747 | { | |
3ebc5c52 MM |
5748 | found = xref_tag_from_type (TREE_TYPE (template), |
5749 | DECL_NAME (template), | |
29ef83de | 5750 | /*tag_scope=*/ts_global); |
fd295cb2 | 5751 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
3ebc5c52 | 5752 | } |
c8094d83 | 5753 | |
95ee998c | 5754 | context = tsubst (DECL_CONTEXT (template), arglist, |
c2ea3a40 | 5755 | complain, in_decl); |
95ee998c MM |
5756 | if (!context) |
5757 | context = global_namespace; | |
5758 | ||
36a117a5 | 5759 | /* Create the type. */ |
ed44da02 MM |
5760 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
5761 | { | |
dbfe2124 | 5762 | if (!is_partial_instantiation) |
92777ce4 NS |
5763 | { |
5764 | set_current_access_from_decl (TYPE_NAME (template_type)); | |
5765 | t = start_enum (TYPE_IDENTIFIER (template_type)); | |
5766 | } | |
ed44da02 | 5767 | else |
dbfe2124 | 5768 | /* We don't want to call start_enum for this type, since |
ed44da02 MM |
5769 | the values for the enumeration constants may involve |
5770 | template parameters. And, no one should be interested | |
5771 | in the enumeration constants for such a type. */ | |
5772 | t = make_node (ENUMERAL_TYPE); | |
5773 | } | |
5774 | else | |
5775 | { | |
33848bb0 | 5776 | t = make_aggr_type (TREE_CODE (template_type)); |
c8094d83 | 5777 | CLASSTYPE_DECLARED_CLASS (t) |
ed44da02 | 5778 | = CLASSTYPE_DECLARED_CLASS (template_type); |
ed44da02 | 5779 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); |
f668f160 | 5780 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ae673f14 JM |
5781 | |
5782 | /* A local class. Make sure the decl gets registered properly. */ | |
5783 | if (context == current_function_decl) | |
bd3d082e | 5784 | pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current); |
06d40de8 DG |
5785 | |
5786 | if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist)) | |
5787 | /* This instantiation is another name for the primary | |
5788 | template type. Set the TYPE_CANONICAL field | |
5789 | appropriately. */ | |
5790 | TYPE_CANONICAL (t) = template_type; | |
5791 | else if (any_template_arguments_need_structural_equality_p (arglist)) | |
5792 | /* Some of the template arguments require structural | |
5793 | equality testing, so this template class requires | |
5794 | structural equality testing. */ | |
5795 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
ed44da02 MM |
5796 | } |
5797 | ||
ae673f14 JM |
5798 | /* If we called start_enum or pushtag above, this information |
5799 | will already be set up. */ | |
ed44da02 MM |
5800 | if (!TYPE_NAME (t)) |
5801 | { | |
5802 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
c8094d83 | 5803 | |
9188c363 | 5804 | type_decl = create_implicit_typedef (DECL_NAME (template), t); |
ed44da02 | 5805 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
9188c363 | 5806 | TYPE_STUB_DECL (t) = type_decl; |
c8094d83 | 5807 | DECL_SOURCE_LOCATION (type_decl) |
f31686a3 | 5808 | = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); |
ed44da02 MM |
5809 | } |
5810 | else | |
5811 | type_decl = TYPE_NAME (t); | |
36a117a5 | 5812 | |
cab7a9a3 KL |
5813 | TREE_PRIVATE (type_decl) |
5814 | = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); | |
5815 | TREE_PROTECTED (type_decl) | |
5816 | = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); | |
12af7ba3 MM |
5817 | DECL_IN_SYSTEM_HEADER (type_decl) |
5818 | = DECL_IN_SYSTEM_HEADER (template); | |
8d039470 MS |
5819 | if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type)) |
5820 | { | |
5821 | DECL_VISIBILITY_SPECIFIED (type_decl) = 1; | |
5822 | DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type); | |
5823 | } | |
cab7a9a3 | 5824 | |
9fbf56f7 MM |
5825 | /* Set up the template information. We have to figure out which |
5826 | template is the immediate parent if this is a full | |
5827 | instantiation. */ | |
5828 | if (parm_depth == 1 || is_partial_instantiation | |
5829 | || !PRIMARY_TEMPLATE_P (template)) | |
5830 | /* This case is easy; there are no member templates involved. */ | |
5831 | found = template; | |
5832 | else | |
5833 | { | |
ab097535 NS |
5834 | /* This is a full instantiation of a member template. Look |
5835 | for a partial instantiation of which this is an instance. */ | |
9fbf56f7 MM |
5836 | |
5837 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
5838 | found; found = TREE_CHAIN (found)) | |
5839 | { | |
5840 | int success; | |
5841 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
5842 | ||
5843 | /* We only want partial instantiations, here, not | |
5844 | specializations or full instantiations. */ | |
370af2d5 | 5845 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) |
9fbf56f7 MM |
5846 | || !uses_template_parms (TREE_VALUE (found))) |
5847 | continue; | |
5848 | ||
5849 | /* Temporarily reduce by one the number of levels in the | |
5850 | ARGLIST and in FOUND so as to avoid comparing the | |
5851 | last set of arguments. */ | |
5852 | TREE_VEC_LENGTH (arglist)--; | |
5853 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
5854 | ||
5855 | /* See if the arguments match. If they do, then TMPL is | |
5856 | the partial instantiation we want. */ | |
5857 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
5858 | ||
5859 | /* Restore the argument vectors to their full size. */ | |
5860 | TREE_VEC_LENGTH (arglist)++; | |
5861 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
5862 | ||
5863 | if (success) | |
5864 | { | |
5865 | found = tmpl; | |
5866 | break; | |
5867 | } | |
5868 | } | |
5869 | ||
5870 | if (!found) | |
ab097535 NS |
5871 | { |
5872 | /* There was no partial instantiation. This happens | |
0cbd7506 MS |
5873 | where C<T> is a member template of A<T> and it's used |
5874 | in something like | |
c8094d83 | 5875 | |
0cbd7506 MS |
5876 | template <typename T> struct B { A<T>::C<int> m; }; |
5877 | B<float>; | |
c8094d83 | 5878 | |
0cbd7506 MS |
5879 | Create the partial instantiation. |
5880 | */ | |
5881 | TREE_VEC_LENGTH (arglist)--; | |
5882 | found = tsubst (template, arglist, complain, NULL_TREE); | |
5883 | TREE_VEC_LENGTH (arglist)++; | |
5884 | } | |
9fbf56f7 MM |
5885 | } |
5886 | ||
c8094d83 MS |
5887 | SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE)); |
5888 | DECL_TEMPLATE_INSTANTIATIONS (template) | |
5889 | = tree_cons (arglist, t, | |
dbfe2124 MM |
5890 | DECL_TEMPLATE_INSTANTIATIONS (template)); |
5891 | ||
c8094d83 | 5892 | if (TREE_CODE (t) == ENUMERAL_TYPE |
dbfe2124 | 5893 | && !is_partial_instantiation) |
61a127b3 MM |
5894 | /* Now that the type has been registered on the instantiations |
5895 | list, we set up the enumerators. Because the enumeration | |
5896 | constants may involve the enumeration type itself, we make | |
5897 | sure to register the type first, and then create the | |
5898 | constants. That way, doing tsubst_expr for the enumeration | |
5899 | constants won't result in recursive calls here; we'll find | |
5900 | the instantiation and exit above. */ | |
5901 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 | 5902 | |
7813d14c | 5903 | if (is_partial_instantiation) |
077e7015 MM |
5904 | /* If the type makes use of template parameters, the |
5905 | code that generates debugging information will crash. */ | |
5906 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 5907 | |
b9e75696 JM |
5908 | /* Possibly limit visibility based on template args. */ |
5909 | TREE_PUBLIC (type_decl) = 1; | |
5910 | determine_visibility (type_decl); | |
5911 | ||
fd295cb2 | 5912 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
36a117a5 | 5913 | } |
fd295cb2 | 5914 | timevar_pop (TV_NAME_LOOKUP); |
8d08fdba MS |
5915 | } |
5916 | \f | |
c8094d83 | 5917 | struct pair_fn_data |
8d08fdba | 5918 | { |
8dfaeb63 MM |
5919 | tree_fn_t fn; |
5920 | void *data; | |
c095a4f8 DG |
5921 | /* True when we should also visit template parameters that occur in |
5922 | non-deduced contexts. */ | |
5923 | bool include_nondeduced_p; | |
0c58f841 | 5924 | struct pointer_set_t *visited; |
8dfaeb63 MM |
5925 | }; |
5926 | ||
5927 | /* Called from for_each_template_parm via walk_tree. */ | |
581d38d0 | 5928 | |
8dfaeb63 | 5929 | static tree |
350fae66 | 5930 | for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) |
8dfaeb63 MM |
5931 | { |
5932 | tree t = *tp; | |
5933 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; | |
5934 | tree_fn_t fn = pfd->fn; | |
5935 | void *data = pfd->data; | |
4f2c9d7e | 5936 | |
2f939d94 | 5937 | if (TYPE_P (t) |
c095a4f8 DG |
5938 | && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE) |
5939 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited, | |
5940 | pfd->include_nondeduced_p)) | |
8dfaeb63 | 5941 | return error_mark_node; |
581d38d0 | 5942 | |
8d08fdba MS |
5943 | switch (TREE_CODE (t)) |
5944 | { | |
8d08fdba | 5945 | case RECORD_TYPE: |
9076e292 | 5946 | if (TYPE_PTRMEMFUNC_P (t)) |
8dfaeb63 | 5947 | break; |
ed44da02 MM |
5948 | /* Fall through. */ |
5949 | ||
8d08fdba | 5950 | case UNION_TYPE: |
ed44da02 | 5951 | case ENUMERAL_TYPE: |
8dfaeb63 MM |
5952 | if (!TYPE_TEMPLATE_INFO (t)) |
5953 | *walk_subtrees = 0; | |
5954 | else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)), | |
c095a4f8 DG |
5955 | fn, data, pfd->visited, |
5956 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
5957 | return error_mark_node; |
5958 | break; | |
5959 | ||
8f6e6bf3 EB |
5960 | case INTEGER_TYPE: |
5961 | if (for_each_template_parm (TYPE_MIN_VALUE (t), | |
c095a4f8 DG |
5962 | fn, data, pfd->visited, |
5963 | pfd->include_nondeduced_p) | |
8f6e6bf3 | 5964 | || for_each_template_parm (TYPE_MAX_VALUE (t), |
c095a4f8 DG |
5965 | fn, data, pfd->visited, |
5966 | pfd->include_nondeduced_p)) | |
8f6e6bf3 EB |
5967 | return error_mark_node; |
5968 | break; | |
5969 | ||
588c2d1c | 5970 | case METHOD_TYPE: |
8dfaeb63 MM |
5971 | /* Since we're not going to walk subtrees, we have to do this |
5972 | explicitly here. */ | |
4f2c9d7e | 5973 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, |
c095a4f8 | 5974 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 | 5975 | return error_mark_node; |
4890c2f4 | 5976 | /* Fall through. */ |
588c2d1c MM |
5977 | |
5978 | case FUNCTION_TYPE: | |
8dfaeb63 | 5979 | /* Check the return type. */ |
c095a4f8 DG |
5980 | if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited, |
5981 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
5982 | return error_mark_node; |
5983 | ||
588c2d1c MM |
5984 | /* Check the parameter types. Since default arguments are not |
5985 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
5986 | contain expressions that involve template parameters. But, | |
5987 | no-one should be looking at them yet. And, once they're | |
5988 | instantiated, they don't contain template parameters, so | |
5989 | there's no point in looking at them then, either. */ | |
5990 | { | |
5991 | tree parm; | |
5992 | ||
5993 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4f2c9d7e | 5994 | if (for_each_template_parm (TREE_VALUE (parm), fn, data, |
c095a4f8 | 5995 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 | 5996 | return error_mark_node; |
5566b478 | 5997 | |
8dfaeb63 MM |
5998 | /* Since we've already handled the TYPE_ARG_TYPES, we don't |
5999 | want walk_tree walking into them itself. */ | |
6000 | *walk_subtrees = 0; | |
6001 | } | |
6002 | break; | |
3ac3d9ea | 6003 | |
a723baf1 | 6004 | case TYPEOF_TYPE: |
c095a4f8 DG |
6005 | if (pfd->include_nondeduced_p |
6006 | && for_each_template_parm (TYPE_FIELDS (t), fn, data, | |
6007 | pfd->visited, | |
6008 | pfd->include_nondeduced_p)) | |
a723baf1 MM |
6009 | return error_mark_node; |
6010 | break; | |
6011 | ||
8d08fdba | 6012 | case FUNCTION_DECL: |
5566b478 | 6013 | case VAR_DECL: |
5566b478 | 6014 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
4f2c9d7e | 6015 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data, |
c095a4f8 | 6016 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 MM |
6017 | return error_mark_node; |
6018 | /* Fall through. */ | |
6019 | ||
8d08fdba | 6020 | case PARM_DECL: |
a723baf1 MM |
6021 | case CONST_DECL: |
6022 | if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t) | |
6023 | && for_each_template_parm (DECL_INITIAL (t), fn, data, | |
c095a4f8 | 6024 | pfd->visited, pfd->include_nondeduced_p)) |
a723baf1 | 6025 | return error_mark_node; |
c8094d83 | 6026 | if (DECL_CONTEXT (t) |
c095a4f8 | 6027 | && pfd->include_nondeduced_p |
4f2c9d7e | 6028 | && for_each_template_parm (DECL_CONTEXT (t), fn, data, |
c095a4f8 | 6029 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 MM |
6030 | return error_mark_node; |
6031 | break; | |
8d08fdba | 6032 | |
a1281f45 | 6033 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
744fac59 | 6034 | /* Record template parameters such as `T' inside `TT<T>'. */ |
c095a4f8 DG |
6035 | if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited, |
6036 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
6037 | return error_mark_node; |
6038 | /* Fall through. */ | |
6039 | ||
a1281f45 | 6040 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 6041 | case TEMPLATE_TYPE_PARM: |
f84b4be9 | 6042 | case TEMPLATE_PARM_INDEX: |
8dfaeb63 MM |
6043 | if (fn && (*fn)(t, data)) |
6044 | return error_mark_node; | |
6045 | else if (!fn) | |
6046 | return error_mark_node; | |
6047 | break; | |
5156628f | 6048 | |
8dfaeb63 | 6049 | case TEMPLATE_DECL: |
f4f206f4 | 6050 | /* A template template parameter is encountered. */ |
8dfaeb63 | 6051 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) |
c095a4f8 DG |
6052 | && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited, |
6053 | pfd->include_nondeduced_p)) | |
8dfaeb63 | 6054 | return error_mark_node; |
db5ae43f | 6055 | |
8dfaeb63 MM |
6056 | /* Already substituted template template parameter */ |
6057 | *walk_subtrees = 0; | |
6058 | break; | |
b8865407 | 6059 | |
4699c561 | 6060 | case TYPENAME_TYPE: |
c8094d83 | 6061 | if (!fn |
4f2c9d7e | 6062 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, |
c095a4f8 DG |
6063 | data, pfd->visited, |
6064 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
6065 | return error_mark_node; |
6066 | break; | |
4699c561 | 6067 | |
8dfaeb63 MM |
6068 | case CONSTRUCTOR: |
6069 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) | |
c095a4f8 | 6070 | && pfd->include_nondeduced_p |
8dfaeb63 | 6071 | && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE |
4f2c9d7e | 6072 | (TREE_TYPE (t)), fn, data, |
c095a4f8 | 6073 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 MM |
6074 | return error_mark_node; |
6075 | break; | |
c8094d83 | 6076 | |
b8865407 MM |
6077 | case INDIRECT_REF: |
6078 | case COMPONENT_REF: | |
4699c561 | 6079 | /* If there's no type, then this thing must be some expression |
b8865407 | 6080 | involving template parameters. */ |
4699c561 | 6081 | if (!fn && !TREE_TYPE (t)) |
8dfaeb63 MM |
6082 | return error_mark_node; |
6083 | break; | |
b8865407 | 6084 | |
42976354 BK |
6085 | case MODOP_EXPR: |
6086 | case CAST_EXPR: | |
6087 | case REINTERPRET_CAST_EXPR: | |
6088 | case CONST_CAST_EXPR: | |
6089 | case STATIC_CAST_EXPR: | |
6090 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
6091 | case ARROW_EXPR: |
6092 | case DOTSTAR_EXPR: | |
6093 | case TYPEID_EXPR: | |
40242ccf | 6094 | case PSEUDO_DTOR_EXPR: |
b8865407 | 6095 | if (!fn) |
8dfaeb63 MM |
6096 | return error_mark_node; |
6097 | break; | |
abff8e06 | 6098 | |
8d08fdba | 6099 | default: |
8dfaeb63 | 6100 | break; |
8d08fdba | 6101 | } |
8dfaeb63 MM |
6102 | |
6103 | /* We didn't find any template parameters we liked. */ | |
6104 | return NULL_TREE; | |
6105 | } | |
6106 | ||
c8094d83 MS |
6107 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, |
6108 | BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, | |
a1281f45 | 6109 | call FN with the parameter and the DATA. |
838dfd8a | 6110 | If FN returns nonzero, the iteration is terminated, and |
8dfaeb63 | 6111 | for_each_template_parm returns 1. Otherwise, the iteration |
838dfd8a | 6112 | continues. If FN never returns a nonzero value, the value |
8dfaeb63 | 6113 | returned by for_each_template_parm is 0. If FN is NULL, it is |
c095a4f8 DG |
6114 | considered to be the function which always returns 1. |
6115 | ||
6116 | If INCLUDE_NONDEDUCED_P, then this routine will also visit template | |
6117 | parameters that occur in non-deduced contexts. When false, only | |
6118 | visits those template parameters that can be deduced. */ | |
8dfaeb63 MM |
6119 | |
6120 | static int | |
0c58f841 | 6121 | for_each_template_parm (tree t, tree_fn_t fn, void* data, |
c095a4f8 DG |
6122 | struct pointer_set_t *visited, |
6123 | bool include_nondeduced_p) | |
8dfaeb63 MM |
6124 | { |
6125 | struct pair_fn_data pfd; | |
ad2ae3b2 | 6126 | int result; |
8dfaeb63 MM |
6127 | |
6128 | /* Set up. */ | |
6129 | pfd.fn = fn; | |
6130 | pfd.data = data; | |
c095a4f8 | 6131 | pfd.include_nondeduced_p = include_nondeduced_p; |
8dfaeb63 | 6132 | |
4890c2f4 MM |
6133 | /* Walk the tree. (Conceptually, we would like to walk without |
6134 | duplicates, but for_each_template_parm_r recursively calls | |
6135 | for_each_template_parm, so we would need to reorganize a fair | |
4f2c9d7e MM |
6136 | bit to use walk_tree_without_duplicates, so we keep our own |
6137 | visited list.) */ | |
6138 | if (visited) | |
6139 | pfd.visited = visited; | |
6140 | else | |
0c58f841 | 6141 | pfd.visited = pointer_set_create (); |
14588106 RG |
6142 | result = cp_walk_tree (&t, |
6143 | for_each_template_parm_r, | |
6144 | &pfd, | |
6145 | pfd.visited) != NULL_TREE; | |
ad2ae3b2 MM |
6146 | |
6147 | /* Clean up. */ | |
6148 | if (!visited) | |
0c58f841 MA |
6149 | { |
6150 | pointer_set_destroy (pfd.visited); | |
6151 | pfd.visited = 0; | |
6152 | } | |
ad2ae3b2 MM |
6153 | |
6154 | return result; | |
8d08fdba MS |
6155 | } |
6156 | ||
d43f603d KL |
6157 | /* Returns true if T depends on any template parameter. */ |
6158 | ||
050367a3 | 6159 | int |
3a978d72 | 6160 | uses_template_parms (tree t) |
050367a3 | 6161 | { |
c353b8e3 MM |
6162 | bool dependent_p; |
6163 | int saved_processing_template_decl; | |
6164 | ||
6165 | saved_processing_template_decl = processing_template_decl; | |
6166 | if (!saved_processing_template_decl) | |
6167 | processing_template_decl = 1; | |
6168 | if (TYPE_P (t)) | |
6169 | dependent_p = dependent_type_p (t); | |
6170 | else if (TREE_CODE (t) == TREE_VEC) | |
6171 | dependent_p = any_dependent_template_arguments_p (t); | |
6172 | else if (TREE_CODE (t) == TREE_LIST) | |
6173 | dependent_p = (uses_template_parms (TREE_VALUE (t)) | |
6174 | || uses_template_parms (TREE_CHAIN (t))); | |
3ce5fa4f NS |
6175 | else if (TREE_CODE (t) == TYPE_DECL) |
6176 | dependent_p = dependent_type_p (TREE_TYPE (t)); | |
c8094d83 MS |
6177 | else if (DECL_P (t) |
6178 | || EXPR_P (t) | |
c353b8e3 MM |
6179 | || TREE_CODE (t) == TEMPLATE_PARM_INDEX |
6180 | || TREE_CODE (t) == OVERLOAD | |
6181 | || TREE_CODE (t) == BASELINK | |
b207d6e2 | 6182 | || TREE_CODE (t) == IDENTIFIER_NODE |
cb68ec50 | 6183 | || TREE_CODE (t) == TRAIT_EXPR |
6615c446 | 6184 | || CONSTANT_CLASS_P (t)) |
c353b8e3 MM |
6185 | dependent_p = (type_dependent_expression_p (t) |
6186 | || value_dependent_expression_p (t)); | |
315fb5db NS |
6187 | else |
6188 | { | |
6189 | gcc_assert (t == error_mark_node); | |
6190 | dependent_p = false; | |
6191 | } | |
c8094d83 | 6192 | |
c353b8e3 MM |
6193 | processing_template_decl = saved_processing_template_decl; |
6194 | ||
6195 | return dependent_p; | |
050367a3 MM |
6196 | } |
6197 | ||
d43f603d KL |
6198 | /* Returns true if T depends on any template parameter with level LEVEL. */ |
6199 | ||
6200 | int | |
6201 | uses_template_parms_level (tree t, int level) | |
6202 | { | |
c095a4f8 DG |
6203 | return for_each_template_parm (t, template_parm_this_level_p, &level, NULL, |
6204 | /*include_nondeduced_p=*/true); | |
d43f603d KL |
6205 | } |
6206 | ||
27fafc8d | 6207 | static int tinst_depth; |
e9f32eb5 | 6208 | extern int max_tinst_depth; |
5566b478 | 6209 | #ifdef GATHER_STATISTICS |
27fafc8d | 6210 | int depth_reached; |
5566b478 | 6211 | #endif |
8dfaeb63 MM |
6212 | static int tinst_level_tick; |
6213 | static int last_template_error_tick; | |
8d08fdba | 6214 | |
3ae18eaf JM |
6215 | /* We're starting to instantiate D; record the template instantiation context |
6216 | for diagnostics and to restore it later. */ | |
6217 | ||
aa9d8196 | 6218 | static int |
3a978d72 | 6219 | push_tinst_level (tree d) |
8d08fdba | 6220 | { |
e2c3721c | 6221 | struct tinst_level *new; |
8d08fdba | 6222 | |
7215f9a0 MS |
6223 | if (tinst_depth >= max_tinst_depth) |
6224 | { | |
8adf5b5e JM |
6225 | /* If the instantiation in question still has unbound template parms, |
6226 | we don't really care if we can't instantiate it, so just return. | |
0cbd7506 | 6227 | This happens with base instantiation for implicit `typename'. */ |
8adf5b5e JM |
6228 | if (uses_template_parms (d)) |
6229 | return 0; | |
6230 | ||
1139b3d8 | 6231 | last_template_error_tick = tinst_level_tick; |
0f51ccfc | 6232 | error ("template instantiation depth exceeds maximum of %d (use " |
0cbd7506 | 6233 | "-ftemplate-depth-NN to increase the maximum) instantiating %qD", |
b4f4233d | 6234 | max_tinst_depth, d); |
5566b478 | 6235 | |
cb753e49 | 6236 | print_instantiation_context (); |
5566b478 | 6237 | |
7215f9a0 MS |
6238 | return 0; |
6239 | } | |
6240 | ||
e2c3721c PB |
6241 | new = GGC_NEW (struct tinst_level); |
6242 | new->decl = d; | |
6243 | new->locus = input_location; | |
6244 | new->in_system_header_p = in_system_header; | |
6245 | new->next = current_tinst_level; | |
8d08fdba | 6246 | current_tinst_level = new; |
5566b478 | 6247 | |
7215f9a0 | 6248 | ++tinst_depth; |
5566b478 MS |
6249 | #ifdef GATHER_STATISTICS |
6250 | if (tinst_depth > depth_reached) | |
6251 | depth_reached = tinst_depth; | |
6252 | #endif | |
6253 | ||
27fafc8d | 6254 | ++tinst_level_tick; |
7215f9a0 | 6255 | return 1; |
8d08fdba MS |
6256 | } |
6257 | ||
3ae18eaf JM |
6258 | /* We're done instantiating this template; return to the instantiation |
6259 | context. */ | |
6260 | ||
aa9d8196 | 6261 | static void |
3a978d72 | 6262 | pop_tinst_level (void) |
8d08fdba | 6263 | { |
ae58fa02 MM |
6264 | /* Restore the filename and line number stashed away when we started |
6265 | this instantiation. */ | |
e2c3721c PB |
6266 | input_location = current_tinst_level->locus; |
6267 | in_system_header = current_tinst_level->in_system_header_p; | |
6268 | current_tinst_level = current_tinst_level->next; | |
7215f9a0 | 6269 | --tinst_depth; |
27fafc8d | 6270 | ++tinst_level_tick; |
8d08fdba MS |
6271 | } |
6272 | ||
3ae18eaf JM |
6273 | /* We're instantiating a deferred template; restore the template |
6274 | instantiation context in which the instantiation was requested, which | |
e2c3721c | 6275 | is one step out from LEVEL. Return the corresponding DECL or TYPE. */ |
3ae18eaf | 6276 | |
e2c3721c PB |
6277 | static tree |
6278 | reopen_tinst_level (struct tinst_level *level) | |
3ae18eaf | 6279 | { |
e2c3721c | 6280 | struct tinst_level *t; |
3ae18eaf JM |
6281 | |
6282 | tinst_depth = 0; | |
e2c3721c | 6283 | for (t = level; t; t = t->next) |
3ae18eaf JM |
6284 | ++tinst_depth; |
6285 | ||
6286 | current_tinst_level = level; | |
6287 | pop_tinst_level (); | |
e2c3721c | 6288 | return level->decl; |
3ae18eaf JM |
6289 | } |
6290 | ||
61172206 JM |
6291 | /* Returns the TINST_LEVEL which gives the original instantiation |
6292 | context. */ | |
6293 | ||
e2c3721c | 6294 | struct tinst_level * |
61172206 JM |
6295 | outermost_tinst_level (void) |
6296 | { | |
e2c3721c PB |
6297 | struct tinst_level *level = current_tinst_level; |
6298 | if (level) | |
6299 | while (level->next) | |
6300 | level = level->next; | |
6301 | return level; | |
61172206 JM |
6302 | } |
6303 | ||
f84b4be9 JM |
6304 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
6305 | vector of template arguments, as for tsubst. | |
6306 | ||
dc957d14 | 6307 | Returns an appropriate tsubst'd friend declaration. */ |
f84b4be9 JM |
6308 | |
6309 | static tree | |
3a978d72 | 6310 | tsubst_friend_function (tree decl, tree args) |
f84b4be9 JM |
6311 | { |
6312 | tree new_friend; | |
27fafc8d | 6313 | |
c8094d83 | 6314 | if (TREE_CODE (decl) == FUNCTION_DECL |
f84b4be9 JM |
6315 | && DECL_TEMPLATE_INSTANTIATION (decl) |
6316 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
6317 | /* This was a friend declared with an explicit template | |
6318 | argument list, e.g.: | |
c8094d83 | 6319 | |
f84b4be9 | 6320 | friend void f<>(T); |
c8094d83 | 6321 | |
f84b4be9 JM |
6322 | to indicate that f was a template instantiation, not a new |
6323 | function declaration. Now, we have to figure out what | |
6324 | instantiation of what template. */ | |
6325 | { | |
76e57b45 | 6326 | tree template_id, arglist, fns; |
f84b4be9 JM |
6327 | tree new_args; |
6328 | tree tmpl; | |
ed2fa432 | 6329 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); |
c8094d83 | 6330 | |
76e57b45 | 6331 | /* Friend functions are looked up in the containing namespace scope. |
0cbd7506 MS |
6332 | We must enter that scope, to avoid finding member functions of the |
6333 | current cless with same name. */ | |
76e57b45 NS |
6334 | push_nested_namespace (ns); |
6335 | fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, | |
015c2c66 MM |
6336 | tf_warning_or_error, NULL_TREE, |
6337 | /*integral_constant_expression_p=*/false); | |
76e57b45 NS |
6338 | pop_nested_namespace (ns); |
6339 | arglist = tsubst (DECL_TI_ARGS (decl), args, | |
23fca1f5 | 6340 | tf_warning_or_error, NULL_TREE); |
76e57b45 | 6341 | template_id = lookup_template_function (fns, arglist); |
c8094d83 | 6342 | |
23fca1f5 | 6343 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
36a117a5 | 6344 | tmpl = determine_specialization (template_id, new_friend, |
c8094d83 | 6345 | &new_args, |
5fe7b654 | 6346 | /*need_member_template=*/0, |
29a1da1c MM |
6347 | TREE_VEC_LENGTH (args), |
6348 | tsk_none); | |
6e432b31 | 6349 | return instantiate_template (tmpl, new_args, tf_error); |
f84b4be9 | 6350 | } |
36a117a5 | 6351 | |
23fca1f5 | 6352 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
c8094d83 | 6353 | |
36a117a5 | 6354 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
6355 | compiler, but is not an instantiation from the point of view of |
6356 | the language. For example, we might have had: | |
c8094d83 | 6357 | |
f84b4be9 JM |
6358 | template <class T> struct S { |
6359 | template <class U> friend void f(T, U); | |
6360 | }; | |
c8094d83 | 6361 | |
f84b4be9 JM |
6362 | Then, in S<int>, template <class U> void f(int, U) is not an |
6363 | instantiation of anything. */ | |
ac2b3222 AP |
6364 | if (new_friend == error_mark_node) |
6365 | return error_mark_node; | |
c8094d83 | 6366 | |
f84b4be9 JM |
6367 | DECL_USE_TEMPLATE (new_friend) = 0; |
6368 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
655dc6ee JM |
6369 | { |
6370 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
6371 | DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) | |
6372 | = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); | |
6373 | } | |
36a117a5 | 6374 | |
92643fea MM |
6375 | /* The mangled name for the NEW_FRIEND is incorrect. The function |
6376 | is not a template instantiation and should not be mangled like | |
6377 | one. Therefore, we forget the mangling here; we'll recompute it | |
6378 | later if we need it. */ | |
36a117a5 MM |
6379 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) |
6380 | { | |
19e7881c | 6381 | SET_DECL_RTL (new_friend, NULL_RTX); |
92643fea | 6382 | SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); |
36a117a5 | 6383 | } |
c8094d83 | 6384 | |
6eb3bb27 | 6385 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 6386 | { |
36a117a5 | 6387 | tree old_decl; |
fbf1c34b MM |
6388 | tree new_friend_template_info; |
6389 | tree new_friend_result_template_info; | |
92da7074 | 6390 | tree ns; |
fbf1c34b MM |
6391 | int new_friend_is_defn; |
6392 | ||
6393 | /* We must save some information from NEW_FRIEND before calling | |
6394 | duplicate decls since that function will free NEW_FRIEND if | |
6395 | possible. */ | |
6396 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
5bd61841 | 6397 | new_friend_is_defn = |
c8094d83 | 6398 | (DECL_INITIAL (DECL_TEMPLATE_RESULT |
5bd61841 MM |
6399 | (template_for_substitution (new_friend))) |
6400 | != NULL_TREE); | |
f84b4be9 | 6401 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
6402 | { |
6403 | /* This declaration is a `primary' template. */ | |
6404 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
c8094d83 | 6405 | |
fbf1c34b | 6406 | new_friend_result_template_info |
17aec3eb | 6407 | = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); |
fbf1c34b MM |
6408 | } |
6409 | else | |
5bd61841 | 6410 | new_friend_result_template_info = NULL_TREE; |
36a117a5 | 6411 | |
b01e6d2b JM |
6412 | /* Make the init_value nonzero so pushdecl knows this is a defn. */ |
6413 | if (new_friend_is_defn) | |
6414 | DECL_INITIAL (new_friend) = error_mark_node; | |
6415 | ||
1c227897 MM |
6416 | /* Inside pushdecl_namespace_level, we will push into the |
6417 | current namespace. However, the friend function should go | |
c6002625 | 6418 | into the namespace of the template. */ |
92da7074 ML |
6419 | ns = decl_namespace_context (new_friend); |
6420 | push_nested_namespace (ns); | |
d63d5d0c | 6421 | old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true); |
92da7074 | 6422 | pop_nested_namespace (ns); |
36a117a5 | 6423 | |
0014c247 VR |
6424 | if (old_decl == error_mark_node) |
6425 | return error_mark_node; | |
6426 | ||
36a117a5 MM |
6427 | if (old_decl != new_friend) |
6428 | { | |
6429 | /* This new friend declaration matched an existing | |
6430 | declaration. For example, given: | |
6431 | ||
6432 | template <class T> void f(T); | |
c8094d83 MS |
6433 | template <class U> class C { |
6434 | template <class T> friend void f(T) {} | |
36a117a5 MM |
6435 | }; |
6436 | ||
6437 | the friend declaration actually provides the definition | |
6438 | of `f', once C has been instantiated for some type. So, | |
6439 | old_decl will be the out-of-class template declaration, | |
6440 | while new_friend is the in-class definition. | |
6441 | ||
6442 | But, if `f' was called before this point, the | |
6443 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
6444 | to `T' but not to `U', references to which might appear | |
6445 | in the definition of `f'. Previously, the most general | |
6446 | template for an instantiation of `f' was the out-of-class | |
6447 | version; now it is the in-class version. Therefore, we | |
6448 | run through all specialization of `f', adding to their | |
6449 | DECL_TI_ARGS appropriately. In particular, they need a | |
6450 | new set of outer arguments, corresponding to the | |
c8094d83 | 6451 | arguments for this class instantiation. |
36a117a5 MM |
6452 | |
6453 | The same situation can arise with something like this: | |
6454 | ||
6455 | friend void f(int); | |
c8094d83 | 6456 | template <class T> class C { |
0cbd7506 MS |
6457 | friend void f(T) {} |
6458 | }; | |
36a117a5 MM |
6459 | |
6460 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
6461 | in the class. */ | |
6462 | ||
fbf1c34b MM |
6463 | if (!new_friend_is_defn) |
6464 | /* On the other hand, if the in-class declaration does | |
6465 | *not* provide a definition, then we don't want to alter | |
6466 | existing definitions. We can just leave everything | |
6467 | alone. */ | |
36a117a5 | 6468 | ; |
fbf1c34b | 6469 | else |
36a117a5 | 6470 | { |
fbf1c34b MM |
6471 | /* Overwrite whatever template info was there before, if |
6472 | any, with the new template information pertaining to | |
6473 | the declaration. */ | |
6474 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
6475 | ||
6476 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
8d83f792 MM |
6477 | reregister_specialization (new_friend, |
6478 | most_general_template (old_decl), | |
6479 | old_decl); | |
c8094d83 | 6480 | else |
36a117a5 | 6481 | { |
fbf1c34b MM |
6482 | tree t; |
6483 | tree new_friend_args; | |
6484 | ||
c8094d83 | 6485 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) |
fbf1c34b | 6486 | = new_friend_result_template_info; |
c8094d83 | 6487 | |
fbf1c34b | 6488 | new_friend_args = TI_ARGS (new_friend_template_info); |
c8094d83 | 6489 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); |
fbf1c34b MM |
6490 | t != NULL_TREE; |
6491 | t = TREE_CHAIN (t)) | |
6492 | { | |
6493 | tree spec = TREE_VALUE (t); | |
c8094d83 MS |
6494 | |
6495 | DECL_TI_ARGS (spec) | |
fbf1c34b MM |
6496 | = add_outermost_template_args (new_friend_args, |
6497 | DECL_TI_ARGS (spec)); | |
fbf1c34b MM |
6498 | } |
6499 | ||
6500 | /* Now, since specializations are always supposed to | |
6501 | hang off of the most general template, we must move | |
6502 | them. */ | |
6503 | t = most_general_template (old_decl); | |
6504 | if (t != old_decl) | |
6505 | { | |
6506 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
6507 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
6508 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
6509 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
6510 | } | |
36a117a5 MM |
6511 | } |
6512 | } | |
6513 | ||
6514 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
6515 | by duplicate_decls. */ | |
6516 | new_friend = old_decl; | |
6517 | } | |
f84b4be9 | 6518 | } |
6e432b31 | 6519 | else |
f84b4be9 | 6520 | { |
6e432b31 KL |
6521 | tree context = DECL_CONTEXT (new_friend); |
6522 | bool dependent_p; | |
6523 | ||
6524 | /* In the code | |
6525 | template <class T> class C { | |
6526 | template <class U> friend void C1<U>::f (); // case 1 | |
6527 | friend void C2<T>::f (); // case 2 | |
6528 | }; | |
6529 | we only need to make sure CONTEXT is a complete type for | |
6530 | case 2. To distinguish between the two cases, we note that | |
6531 | CONTEXT of case 1 remains dependent type after tsubst while | |
6532 | this isn't true for case 2. */ | |
6533 | ++processing_template_decl; | |
6534 | dependent_p = dependent_type_p (context); | |
6535 | --processing_template_decl; | |
6536 | ||
6537 | if (!dependent_p | |
6538 | && !complete_type_or_else (context, NULL_TREE)) | |
6539 | return error_mark_node; | |
6540 | ||
6541 | if (COMPLETE_TYPE_P (context)) | |
6542 | { | |
6543 | /* Check to see that the declaration is really present, and, | |
6544 | possibly obtain an improved declaration. */ | |
6545 | tree fn = check_classfn (context, | |
6546 | new_friend, NULL_TREE); | |
6547 | ||
684939ce | 6548 | if (fn) |
6e432b31 KL |
6549 | new_friend = fn; |
6550 | } | |
f84b4be9 JM |
6551 | } |
6552 | ||
6553 | return new_friend; | |
6554 | } | |
6555 | ||
1aed5355 MM |
6556 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
6557 | template arguments, as for tsubst. | |
6757edfe | 6558 | |
dc957d14 | 6559 | Returns an appropriate tsubst'd friend type or error_mark_node on |
4b054b80 | 6560 | failure. */ |
6757edfe MM |
6561 | |
6562 | static tree | |
3a978d72 | 6563 | tsubst_friend_class (tree friend_tmpl, tree args) |
6757edfe | 6564 | { |
1aed5355 | 6565 | tree friend_type; |
25aab5d0 | 6566 | tree tmpl; |
3e0ec82f | 6567 | tree context; |
6757edfe | 6568 | |
3e0ec82f MM |
6569 | context = DECL_CONTEXT (friend_tmpl); |
6570 | ||
6571 | if (context) | |
77adef84 | 6572 | { |
3e0ec82f MM |
6573 | if (TREE_CODE (context) == NAMESPACE_DECL) |
6574 | push_nested_namespace (context); | |
6575 | else | |
c8094d83 | 6576 | push_nested_class (tsubst (context, args, tf_none, NULL_TREE)); |
3e0ec82f | 6577 | } |
25aab5d0 | 6578 | |
105d72c5 MM |
6579 | /* Look for a class template declaration. We look for hidden names |
6580 | because two friend declarations of the same template are the | |
6581 | same. For example, in: | |
6582 | ||
6583 | struct A { | |
6584 | template <typename> friend class F; | |
6585 | }; | |
6586 | template <typename> struct B { | |
6587 | template <typename> friend class F; | |
6588 | }; | |
6589 | ||
6590 | both F templates are the same. */ | |
6591 | tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0, | |
6592 | /*block_p=*/true, 0, | |
6593 | LOOKUP_COMPLAIN | LOOKUP_HIDDEN); | |
25aab5d0 | 6594 | |
3e0ec82f MM |
6595 | /* But, if we don't find one, it might be because we're in a |
6596 | situation like this: | |
77adef84 | 6597 | |
3e0ec82f MM |
6598 | template <class T> |
6599 | struct S { | |
6600 | template <class U> | |
6601 | friend struct S; | |
6602 | }; | |
6603 | ||
6604 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
6605 | for `S<int>', not the TEMPLATE_DECL. */ | |
6606 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
6607 | { | |
10e6657a | 6608 | tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1); |
3e0ec82f | 6609 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); |
25aab5d0 | 6610 | } |
6757edfe | 6611 | |
25aab5d0 | 6612 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
6757edfe MM |
6613 | { |
6614 | /* The friend template has already been declared. Just | |
36a117a5 MM |
6615 | check to see that the declarations match, and install any new |
6616 | default parameters. We must tsubst the default parameters, | |
6617 | of course. We only need the innermost template parameters | |
6618 | because that is all that redeclare_class_template will look | |
6619 | at. */ | |
3e0ec82f MM |
6620 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) |
6621 | > TMPL_ARGS_DEPTH (args)) | |
6622 | { | |
6623 | tree parms; | |
b131ad7c | 6624 | location_t saved_input_location; |
3e0ec82f | 6625 | parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), |
23fca1f5 | 6626 | args, tf_warning_or_error); |
b131ad7c MLI |
6627 | |
6628 | saved_input_location = input_location; | |
6629 | input_location = DECL_SOURCE_LOCATION (friend_tmpl); | |
3e0ec82f | 6630 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
b131ad7c MLI |
6631 | input_location = saved_input_location; |
6632 | ||
3e0ec82f MM |
6633 | } |
6634 | ||
6757edfe MM |
6635 | friend_type = TREE_TYPE (tmpl); |
6636 | } | |
6637 | else | |
6638 | { | |
6639 | /* The friend template has not already been declared. In this | |
6640 | case, the instantiation of the template class will cause the | |
6641 | injection of this template into the global scope. */ | |
23fca1f5 | 6642 | tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE); |
dca56f77 VR |
6643 | if (tmpl == error_mark_node) |
6644 | return error_mark_node; | |
6757edfe MM |
6645 | |
6646 | /* The new TMPL is not an instantiation of anything, so we | |
0cbd7506 | 6647 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for |
6757edfe MM |
6648 | the new type because that is supposed to be the corresponding |
6649 | template decl, i.e., TMPL. */ | |
6650 | DECL_USE_TEMPLATE (tmpl) = 0; | |
6651 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
6652 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
68c78847 KL |
6653 | CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) |
6654 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); | |
6757edfe MM |
6655 | |
6656 | /* Inject this template into the global scope. */ | |
d63d5d0c | 6657 | friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true)); |
6757edfe MM |
6658 | } |
6659 | ||
c8094d83 | 6660 | if (context) |
3e0ec82f MM |
6661 | { |
6662 | if (TREE_CODE (context) == NAMESPACE_DECL) | |
6663 | pop_nested_namespace (context); | |
6664 | else | |
6665 | pop_nested_class (); | |
6666 | } | |
6667 | ||
6757edfe MM |
6668 | return friend_type; |
6669 | } | |
f84b4be9 | 6670 | |
17f29637 KL |
6671 | /* Returns zero if TYPE cannot be completed later due to circularity. |
6672 | Otherwise returns one. */ | |
6673 | ||
d5372501 | 6674 | static int |
3a978d72 | 6675 | can_complete_type_without_circularity (tree type) |
17f29637 KL |
6676 | { |
6677 | if (type == NULL_TREE || type == error_mark_node) | |
6678 | return 0; | |
6679 | else if (COMPLETE_TYPE_P (type)) | |
6680 | return 1; | |
6681 | else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) | |
6682 | return can_complete_type_without_circularity (TREE_TYPE (type)); | |
8c6ab2db NS |
6683 | else if (CLASS_TYPE_P (type) |
6684 | && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) | |
17f29637 KL |
6685 | return 0; |
6686 | else | |
6687 | return 1; | |
6688 | } | |
6689 | ||
4f4141ff JM |
6690 | /* Apply any attributes which had to be deferred until instantiation |
6691 | time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes; | |
6692 | ARGS, COMPLAIN, IN_DECL are as tsubst. */ | |
6693 | ||
6694 | static void | |
6695 | apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags, | |
6696 | tree args, tsubst_flags_t complain, tree in_decl) | |
6697 | { | |
5818c8e4 | 6698 | tree last_dep = NULL_TREE; |
4f4141ff | 6699 | tree t; |
5818c8e4 JM |
6700 | tree *p; |
6701 | ||
6702 | for (t = attributes; t; t = TREE_CHAIN (t)) | |
6703 | if (ATTR_IS_DEPENDENT (t)) | |
6704 | { | |
6705 | last_dep = t; | |
6706 | attributes = copy_list (attributes); | |
6707 | break; | |
6708 | } | |
4f4141ff JM |
6709 | |
6710 | if (DECL_P (*decl_p)) | |
823e5f7f JJ |
6711 | { |
6712 | if (TREE_TYPE (*decl_p) == error_mark_node) | |
6713 | return; | |
6714 | p = &DECL_ATTRIBUTES (*decl_p); | |
6715 | } | |
4f4141ff | 6716 | else |
5818c8e4 | 6717 | p = &TYPE_ATTRIBUTES (*decl_p); |
4f4141ff | 6718 | |
5818c8e4 JM |
6719 | if (last_dep) |
6720 | { | |
6721 | tree late_attrs = NULL_TREE; | |
6722 | tree *q = &late_attrs; | |
6723 | ||
6724 | for (*p = attributes; *p; ) | |
6725 | { | |
6726 | t = *p; | |
6727 | if (ATTR_IS_DEPENDENT (t)) | |
6728 | { | |
6729 | *p = TREE_CHAIN (t); | |
6730 | TREE_CHAIN (t) = NULL_TREE; | |
6731 | TREE_VALUE (t) | |
6732 | = tsubst_expr (TREE_VALUE (t), args, complain, in_decl, | |
6733 | /*integral_constant_expression_p=*/false); | |
6734 | *q = t; | |
6735 | q = &TREE_CHAIN (t); | |
6736 | } | |
6737 | else | |
6738 | p = &TREE_CHAIN (t); | |
6739 | } | |
4f4141ff | 6740 | |
5818c8e4 JM |
6741 | cplus_decl_attributes (decl_p, late_attrs, attr_flags); |
6742 | } | |
4f4141ff JM |
6743 | } |
6744 | ||
8d08fdba | 6745 | tree |
3a978d72 | 6746 | instantiate_class_template (tree type) |
8d08fdba | 6747 | { |
7088fca9 | 6748 | tree template, args, pattern, t, member; |
36a117a5 | 6749 | tree typedecl; |
dbbf88d1 | 6750 | tree pbinfo; |
cad7e87b | 6751 | tree base_list; |
c8094d83 | 6752 | |
5566b478 | 6753 | if (type == error_mark_node) |
8d08fdba MS |
6754 | return error_mark_node; |
6755 | ||
c8094d83 | 6756 | if (TYPE_BEING_DEFINED (type) |
ca099ac8 MM |
6757 | || COMPLETE_TYPE_P (type) |
6758 | || dependent_type_p (type)) | |
5566b478 MS |
6759 | return type; |
6760 | ||
6bdb985f | 6761 | /* Figure out which template is being instantiated. */ |
36a117a5 | 6762 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
50bc768d | 6763 | gcc_assert (TREE_CODE (template) == TEMPLATE_DECL); |
73aad9b9 | 6764 | |
4c571114 MM |
6765 | /* Determine what specialization of the original template to |
6766 | instantiate. */ | |
916b63c3 | 6767 | t = most_specialized_class (type, template); |
8fbc5ae7 | 6768 | if (t == error_mark_node) |
73aad9b9 | 6769 | { |
8fbc5ae7 MM |
6770 | TYPE_BEING_DEFINED (type) = 1; |
6771 | return error_mark_node; | |
73aad9b9 | 6772 | } |
916b63c3 MM |
6773 | else if (t) |
6774 | { | |
6775 | /* This TYPE is actually an instantiation of a partial | |
6776 | specialization. We replace the innermost set of ARGS with | |
6777 | the arguments appropriate for substitution. For example, | |
6778 | given: | |
3db45ab5 MS |
6779 | |
6780 | template <class T> struct S {}; | |
916b63c3 | 6781 | template <class T> struct S<T*> {}; |
6bdb985f | 6782 | |
916b63c3 | 6783 | and supposing that we are instantiating S<int*>, ARGS will |
3db45ab5 | 6784 | presently be {int*} -- but we need {int}. */ |
916b63c3 MM |
6785 | pattern = TREE_TYPE (t); |
6786 | args = TREE_PURPOSE (t); | |
6787 | } | |
73aad9b9 | 6788 | else |
916b63c3 MM |
6789 | { |
6790 | pattern = TREE_TYPE (template); | |
6791 | args = CLASSTYPE_TI_ARGS (type); | |
6792 | } | |
5566b478 | 6793 | |
4c571114 MM |
6794 | /* If the template we're instantiating is incomplete, then clearly |
6795 | there's nothing we can do. */ | |
d0f062fb | 6796 | if (!COMPLETE_TYPE_P (pattern)) |
f31c0a32 | 6797 | return type; |
5566b478 | 6798 | |
4c571114 MM |
6799 | /* If we've recursively instantiated too many templates, stop. */ |
6800 | if (! push_tinst_level (type)) | |
f31c0a32 | 6801 | return type; |
4c571114 MM |
6802 | |
6803 | /* Now we're really doing the instantiation. Mark the type as in | |
6804 | the process of being defined. */ | |
6805 | TYPE_BEING_DEFINED (type) = 1; | |
6806 | ||
78757caa KL |
6807 | /* We may be in the middle of deferred access check. Disable |
6808 | it now. */ | |
6809 | push_deferring_access_checks (dk_no_deferred); | |
6810 | ||
c353b8e3 | 6811 | push_to_top_level (); |
4c571114 | 6812 | |
7813d14c | 6813 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
8d08fdba | 6814 | |
68ea098a NS |
6815 | /* Set the input location to the template definition. This is needed |
6816 | if tsubsting causes an error. */ | |
12af7ba3 MM |
6817 | typedecl = TYPE_MAIN_DECL (type); |
6818 | input_location = DECL_SOURCE_LOCATION (typedecl); | |
6819 | in_system_header = DECL_IN_SYSTEM_HEADER (typedecl); | |
68ea098a | 6820 | |
f7da6097 | 6821 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
834c6dff MM |
6822 | TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); |
6823 | TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); | |
f7da6097 | 6824 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); |
f7da6097 MS |
6825 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); |
6826 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
f7da6097 MS |
6827 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); |
6828 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
6829 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
6830 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
f7da6097 MS |
6831 | TYPE_PACKED (type) = TYPE_PACKED (pattern); |
6832 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
11cf4d18 | 6833 | TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); |
eff71ab0 | 6834 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
6bdb8141 JM |
6835 | if (ANON_AGGR_TYPE_P (pattern)) |
6836 | SET_ANON_AGGR_TYPE_P (type); | |
8d039470 MS |
6837 | if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern)) |
6838 | { | |
6839 | CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1; | |
6840 | CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern); | |
6841 | } | |
f7da6097 | 6842 | |
dbbf88d1 | 6843 | pbinfo = TYPE_BINFO (pattern); |
1456deaf | 6844 | |
315fb5db NS |
6845 | /* We should never instantiate a nested class before its enclosing |
6846 | class; we need to look up the nested class by name before we can | |
6847 | instantiate it, and that lookup should instantiate the enclosing | |
6848 | class. */ | |
6849 | gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) | |
6850 | || COMPLETE_TYPE_P (TYPE_CONTEXT (type)) | |
6851 | || TYPE_BEING_DEFINED (TYPE_CONTEXT (type))); | |
1456deaf | 6852 | |
cad7e87b | 6853 | base_list = NULL_TREE; |
fa743e8c | 6854 | if (BINFO_N_BASE_BINFOS (pbinfo)) |
3fd71a52 | 6855 | { |
fa743e8c | 6856 | tree pbase_binfo; |
a2507277 | 6857 | tree context = TYPE_CONTEXT (type); |
4514aa8c | 6858 | tree pushed_scope; |
3fd71a52 | 6859 | int i; |
5566b478 | 6860 | |
a2507277 NS |
6861 | /* We must enter the scope containing the type, as that is where |
6862 | the accessibility of types named in dependent bases are | |
6863 | looked up from. */ | |
4514aa8c | 6864 | pushed_scope = push_scope (context ? context : global_namespace); |
c8094d83 | 6865 | |
3fd71a52 MM |
6866 | /* Substitute into each of the bases to determine the actual |
6867 | basetypes. */ | |
fa743e8c | 6868 | for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) |
3fd71a52 MM |
6869 | { |
6870 | tree base; | |
fa743e8c | 6871 | tree access = BINFO_BASE_ACCESS (pbinfo, i); |
5d80a306 DG |
6872 | tree expanded_bases = NULL_TREE; |
6873 | int idx, len = 1; | |
6874 | ||
6875 | if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo))) | |
6876 | { | |
6877 | expanded_bases = | |
6878 | tsubst_pack_expansion (BINFO_TYPE (pbase_binfo), | |
6879 | args, tf_error, NULL_TREE); | |
6880 | if (expanded_bases == error_mark_node) | |
6881 | continue; | |
6882 | ||
6883 | len = TREE_VEC_LENGTH (expanded_bases); | |
6884 | } | |
6885 | ||
6886 | for (idx = 0; idx < len; idx++) | |
6887 | { | |
6888 | if (expanded_bases) | |
6889 | /* Extract the already-expanded base class. */ | |
6890 | base = TREE_VEC_ELT (expanded_bases, idx); | |
6891 | else | |
6892 | /* Substitute to figure out the base class. */ | |
6893 | base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, | |
6894 | NULL_TREE); | |
6895 | ||
6896 | if (base == error_mark_node) | |
6897 | continue; | |
6898 | ||
6899 | base_list = tree_cons (access, base, base_list); | |
6900 | if (BINFO_VIRTUAL_P (pbase_binfo)) | |
6901 | TREE_TYPE (base_list) = integer_type_node; | |
6902 | } | |
3fd71a52 | 6903 | } |
dfbcd65a | 6904 | |
3fd71a52 MM |
6905 | /* The list is now in reverse order; correct that. */ |
6906 | base_list = nreverse (base_list); | |
6907 | ||
4514aa8c NS |
6908 | if (pushed_scope) |
6909 | pop_scope (pushed_scope); | |
3fd71a52 | 6910 | } |
cad7e87b NS |
6911 | /* Now call xref_basetypes to set up all the base-class |
6912 | information. */ | |
6913 | xref_basetypes (type, base_list); | |
6914 | ||
4f4141ff JM |
6915 | apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern), |
6916 | (int) ATTR_FLAG_TYPE_IN_PLACE, | |
6917 | args, tf_error, NULL_TREE); | |
5566b478 | 6918 | |
b74a0560 MM |
6919 | /* Now that our base classes are set up, enter the scope of the |
6920 | class, so that name lookups into base classes, etc. will work | |
dc957d14 | 6921 | correctly. This is precisely analogous to what we do in |
b74a0560 | 6922 | begin_class_definition when defining an ordinary non-template |
56d0c6e3 JM |
6923 | class, except we also need to push the enclosing classes. */ |
6924 | push_nested_class (type); | |
b74a0560 | 6925 | |
7088fca9 | 6926 | /* Now members are processed in the order of declaration. */ |
8c6ab2db NS |
6927 | for (member = CLASSTYPE_DECL_LIST (pattern); |
6928 | member; member = TREE_CHAIN (member)) | |
8d08fdba | 6929 | { |
7088fca9 | 6930 | tree t = TREE_VALUE (member); |
5566b478 | 6931 | |
7088fca9 | 6932 | if (TREE_PURPOSE (member)) |
ed44da02 | 6933 | { |
7088fca9 KL |
6934 | if (TYPE_P (t)) |
6935 | { | |
5e0c54e5 | 6936 | /* Build new CLASSTYPE_NESTED_UTDS. */ |
8d08fdba | 6937 | |
7088fca9 | 6938 | tree newtag; |
883a2bff MM |
6939 | bool class_template_p; |
6940 | ||
2678bae8 VR |
6941 | class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE |
6942 | && TYPE_LANG_SPECIFIC (t) | |
6943 | && CLASSTYPE_IS_TEMPLATE (t)); | |
883a2bff | 6944 | /* If the member is a class template, then -- even after |
6c745393 | 6945 | substitution -- there may be dependent types in the |
883a2bff MM |
6946 | template argument list for the class. We increment |
6947 | PROCESSING_TEMPLATE_DECL so that dependent_type_p, as | |
6948 | that function will assume that no types are dependent | |
6949 | when outside of a template. */ | |
6950 | if (class_template_p) | |
6951 | ++processing_template_decl; | |
2678bae8 | 6952 | newtag = tsubst (t, args, tf_error, NULL_TREE); |
883a2bff MM |
6953 | if (class_template_p) |
6954 | --processing_template_decl; | |
2620d095 KL |
6955 | if (newtag == error_mark_node) |
6956 | continue; | |
6957 | ||
7088fca9 KL |
6958 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
6959 | { | |
2678bae8 VR |
6960 | tree name = TYPE_IDENTIFIER (t); |
6961 | ||
883a2bff | 6962 | if (class_template_p) |
7088fca9 KL |
6963 | /* Unfortunately, lookup_template_class sets |
6964 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
3afd2e20 NS |
6965 | instantiation (i.e., for the type of a member |
6966 | template class nested within a template class.) | |
6967 | This behavior is required for | |
6968 | maybe_process_partial_specialization to work | |
6969 | correctly, but is not accurate in this case; | |
6970 | the TAG is not an instantiation of anything. | |
6971 | (The corresponding TEMPLATE_DECL is an | |
6972 | instantiation, but the TYPE is not.) */ | |
7088fca9 KL |
6973 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; |
6974 | ||
6975 | /* Now, we call pushtag to put this NEWTAG into the scope of | |
6976 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
6977 | pushtag calling push_template_decl. We don't have to do | |
6978 | this for enums because it will already have been done in | |
6979 | tsubst_enum. */ | |
6980 | if (name) | |
6981 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
bd3d082e | 6982 | pushtag (name, newtag, /*tag_scope=*/ts_current); |
7088fca9 KL |
6983 | } |
6984 | } | |
c8094d83 | 6985 | else if (TREE_CODE (t) == FUNCTION_DECL |
7088fca9 KL |
6986 | || DECL_FUNCTION_TEMPLATE_P (t)) |
6987 | { | |
6988 | /* Build new TYPE_METHODS. */ | |
fecafe5e | 6989 | tree r; |
c8094d83 | 6990 | |
fecafe5e | 6991 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 6992 | ++processing_template_decl; |
fecafe5e NS |
6993 | r = tsubst (t, args, tf_error, NULL_TREE); |
6994 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
9579624e | 6995 | --processing_template_decl; |
7088fca9 | 6996 | set_current_access_from_decl (r); |
7088fca9 KL |
6997 | finish_member_declaration (r); |
6998 | } | |
6999 | else | |
7000 | { | |
7001 | /* Build new TYPE_FIELDS. */ | |
55a3debe DG |
7002 | if (TREE_CODE (t) == STATIC_ASSERT) |
7003 | { | |
7004 | tree condition = | |
7005 | tsubst_expr (STATIC_ASSERT_CONDITION (t), args, | |
7006 | tf_warning_or_error, NULL_TREE, | |
7007 | /*integral_constant_expression_p=*/true); | |
7008 | finish_static_assert (condition, | |
7009 | STATIC_ASSERT_MESSAGE (t), | |
7010 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
7011 | /*member_p=*/true); | |
7012 | } | |
7013 | else if (TREE_CODE (t) != CONST_DECL) | |
7088fca9 KL |
7014 | { |
7015 | tree r; | |
fa8d6e85 | 7016 | |
d479d37f NS |
7017 | /* The the file and line for this declaration, to |
7018 | assist in error message reporting. Since we | |
7019 | called push_tinst_level above, we don't need to | |
7020 | restore these. */ | |
f31686a3 | 7021 | input_location = DECL_SOURCE_LOCATION (t); |
fa8d6e85 | 7022 | |
fb5ce3c9 | 7023 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 7024 | ++processing_template_decl; |
23fca1f5 | 7025 | r = tsubst (t, args, tf_warning_or_error, NULL_TREE); |
fb5ce3c9 | 7026 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 7027 | --processing_template_decl; |
7088fca9 KL |
7028 | if (TREE_CODE (r) == VAR_DECL) |
7029 | { | |
b794e321 MM |
7030 | /* In [temp.inst]: |
7031 | ||
7032 | [t]he initialization (and any associated | |
7033 | side-effects) of a static data member does | |
7034 | not occur unless the static data member is | |
7035 | itself used in a way that requires the | |
7036 | definition of the static data member to | |
3db45ab5 | 7037 | exist. |
b794e321 MM |
7038 | |
7039 | Therefore, we do not substitute into the | |
3db45ab5 MS |
7040 | initialized for the static data member here. */ |
7041 | finish_static_data_member_decl | |
7042 | (r, | |
7043 | /*init=*/NULL_TREE, | |
d174af6c | 7044 | /*init_const_expr_p=*/false, |
3db45ab5 | 7045 | /*asmspec_tree=*/NULL_TREE, |
b794e321 | 7046 | /*flags=*/0); |
7088fca9 KL |
7047 | if (DECL_INITIALIZED_IN_CLASS_P (r)) |
7048 | check_static_variable_definition (r, TREE_TYPE (r)); | |
7049 | } | |
7050 | else if (TREE_CODE (r) == FIELD_DECL) | |
7051 | { | |
7052 | /* Determine whether R has a valid type and can be | |
7053 | completed later. If R is invalid, then it is | |
7054 | replaced by error_mark_node so that it will not be | |
7055 | added to TYPE_FIELDS. */ | |
7056 | tree rtype = TREE_TYPE (r); | |
7057 | if (can_complete_type_without_circularity (rtype)) | |
7058 | complete_type (rtype); | |
7059 | ||
7060 | if (!COMPLETE_TYPE_P (rtype)) | |
7061 | { | |
7062 | cxx_incomplete_type_error (r, rtype); | |
0cbd7506 | 7063 | r = error_mark_node; |
7088fca9 KL |
7064 | } |
7065 | } | |
5566b478 | 7066 | |
7088fca9 KL |
7067 | /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, |
7068 | such a thing will already have been added to the field | |
7069 | list by tsubst_enum in finish_member_declaration in the | |
5e0c54e5 | 7070 | CLASSTYPE_NESTED_UTDS case above. */ |
7088fca9 KL |
7071 | if (!(TREE_CODE (r) == TYPE_DECL |
7072 | && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE | |
43b26a01 | 7073 | && DECL_ARTIFICIAL (r))) |
7088fca9 KL |
7074 | { |
7075 | set_current_access_from_decl (r); | |
7076 | finish_member_declaration (r); | |
7077 | } | |
0cbd7506 | 7078 | } |
7088fca9 | 7079 | } |
61fc8c9e | 7080 | } |
7088fca9 KL |
7081 | else |
7082 | { | |
7083 | if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) | |
7084 | { | |
7085 | /* Build new CLASSTYPE_FRIEND_CLASSES. */ | |
7086 | ||
7087 | tree friend_type = t; | |
b939a023 | 7088 | bool adjust_processing_template_decl = false; |
1aed5355 | 7089 | |
7088fca9 | 7090 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
b939a023 | 7091 | { |
5a24482e | 7092 | /* template <class T> friend class C; */ |
b939a023 | 7093 | friend_type = tsubst_friend_class (friend_type, args); |
0cbd7506 | 7094 | adjust_processing_template_decl = true; |
b939a023 KL |
7095 | } |
7096 | else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE) | |
7097 | { | |
5a24482e | 7098 | /* template <class T> friend class C::D; */ |
b939a023 | 7099 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 7100 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
7101 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
7102 | friend_type = TREE_TYPE (friend_type); | |
0cbd7506 | 7103 | adjust_processing_template_decl = true; |
b939a023 KL |
7104 | } |
7105 | else if (TREE_CODE (friend_type) == TYPENAME_TYPE) | |
7106 | { | |
5a24482e KL |
7107 | /* This could be either |
7108 | ||
7109 | friend class T::C; | |
7110 | ||
7111 | when dependent_type_p is false or | |
7112 | ||
7113 | template <class U> friend class T::C; | |
7114 | ||
7115 | otherwise. */ | |
b939a023 | 7116 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 7117 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
7118 | /* Bump processing_template_decl for correct |
7119 | dependent_type_p calculation. */ | |
7120 | ++processing_template_decl; | |
7121 | if (dependent_type_p (friend_type)) | |
7122 | adjust_processing_template_decl = true; | |
7123 | --processing_template_decl; | |
7124 | } | |
5a24482e KL |
7125 | else if (!CLASSTYPE_USE_TEMPLATE (friend_type) |
7126 | && hidden_name_p (TYPE_NAME (friend_type))) | |
7088fca9 | 7127 | { |
5a24482e KL |
7128 | /* friend class C; |
7129 | ||
7130 | where C hasn't been declared yet. Let's lookup name | |
7131 | from namespace scope directly, bypassing any name that | |
7132 | come from dependent base class. */ | |
7088fca9 KL |
7133 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); |
7134 | ||
7135 | /* The call to xref_tag_from_type does injection for friend | |
7136 | classes. */ | |
7137 | push_nested_namespace (ns); | |
c8094d83 MS |
7138 | friend_type = |
7139 | xref_tag_from_type (friend_type, NULL_TREE, | |
5a24482e | 7140 | /*tag_scope=*/ts_current); |
7088fca9 KL |
7141 | pop_nested_namespace (ns); |
7142 | } | |
5a24482e KL |
7143 | else if (uses_template_parms (friend_type)) |
7144 | /* friend class C<T>; */ | |
7145 | friend_type = tsubst (friend_type, args, | |
23fca1f5 | 7146 | tf_warning_or_error, NULL_TREE); |
5a24482e KL |
7147 | /* Otherwise it's |
7148 | ||
7149 | friend class C; | |
7150 | ||
7151 | where C is already declared or | |
7152 | ||
7153 | friend class C<int>; | |
7154 | ||
0cbd7506 | 7155 | We don't have to do anything in these cases. */ |
1aed5355 | 7156 | |
b939a023 | 7157 | if (adjust_processing_template_decl) |
7088fca9 KL |
7158 | /* Trick make_friend_class into realizing that the friend |
7159 | we're adding is a template, not an ordinary class. It's | |
7160 | important that we use make_friend_class since it will | |
7161 | perform some error-checking and output cross-reference | |
7162 | information. */ | |
7163 | ++processing_template_decl; | |
fc378698 | 7164 | |
b939a023 | 7165 | if (friend_type != error_mark_node) |
0cbd7506 | 7166 | make_friend_class (type, friend_type, /*complain=*/false); |
fc378698 | 7167 | |
b939a023 | 7168 | if (adjust_processing_template_decl) |
7088fca9 KL |
7169 | --processing_template_decl; |
7170 | } | |
7171 | else | |
9579624e KL |
7172 | { |
7173 | /* Build new DECL_FRIENDLIST. */ | |
7174 | tree r; | |
7175 | ||
6e432b31 KL |
7176 | /* The the file and line for this declaration, to |
7177 | assist in error message reporting. Since we | |
7178 | called push_tinst_level above, we don't need to | |
7179 | restore these. */ | |
7180 | input_location = DECL_SOURCE_LOCATION (t); | |
7181 | ||
9579624e | 7182 | if (TREE_CODE (t) == TEMPLATE_DECL) |
cad7e87b NS |
7183 | { |
7184 | ++processing_template_decl; | |
7185 | push_deferring_access_checks (dk_no_check); | |
7186 | } | |
c8094d83 | 7187 | |
9579624e | 7188 | r = tsubst_friend_function (t, args); |
9579624e | 7189 | add_friend (type, r, /*complain=*/false); |
cad7e87b NS |
7190 | if (TREE_CODE (t) == TEMPLATE_DECL) |
7191 | { | |
7192 | pop_deferring_access_checks (); | |
7193 | --processing_template_decl; | |
7194 | } | |
9579624e | 7195 | } |
7088fca9 KL |
7196 | } |
7197 | } | |
5566b478 | 7198 | |
61a127b3 MM |
7199 | /* Set the file and line number information to whatever is given for |
7200 | the class itself. This puts error messages involving generated | |
7201 | implicit functions at a predictable point, and the same point | |
7202 | that would be used for non-template classes. */ | |
f31686a3 | 7203 | input_location = DECL_SOURCE_LOCATION (typedecl); |
6de9cd9a | 7204 | |
61a127b3 | 7205 | unreverse_member_declarations (type); |
9f33663b | 7206 | finish_struct_1 (type); |
5524676d | 7207 | TYPE_BEING_DEFINED (type) = 0; |
8d08fdba | 7208 | |
9188c363 MM |
7209 | /* Now that the class is complete, instantiate default arguments for |
7210 | any member functions. We don't do this earlier because the | |
7211 | default arguments may reference members of the class. */ | |
7212 | if (!PRIMARY_TEMPLATE_P (template)) | |
7213 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) | |
c8094d83 | 7214 | if (TREE_CODE (t) == FUNCTION_DECL |
dc957d14 | 7215 | /* Implicitly generated member functions will not have template |
9188c363 MM |
7216 | information; they are not instantiations, but instead are |
7217 | created "fresh" for each instantiation. */ | |
7218 | && DECL_TEMPLATE_INFO (t)) | |
7219 | tsubst_default_arguments (t); | |
7220 | ||
56d0c6e3 | 7221 | pop_nested_class (); |
5566b478 | 7222 | pop_from_top_level (); |
78757caa | 7223 | pop_deferring_access_checks (); |
5566b478 MS |
7224 | pop_tinst_level (); |
7225 | ||
4684cd27 MM |
7226 | /* The vtable for a template class can be emitted in any translation |
7227 | unit in which the class is instantiated. When there is no key | |
7228 | method, however, finish_struct_1 will already have added TYPE to | |
7229 | the keyed_classes list. */ | |
7230 | if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type)) | |
9aad8f83 MA |
7231 | keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); |
7232 | ||
5566b478 | 7233 | return type; |
8d08fdba MS |
7234 | } |
7235 | ||
00d3396f | 7236 | static tree |
a91db711 | 7237 | tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
00d3396f | 7238 | { |
a91db711 | 7239 | tree r; |
c8094d83 | 7240 | |
a91db711 NS |
7241 | if (!t) |
7242 | r = t; | |
7243 | else if (TYPE_P (t)) | |
7244 | r = tsubst (t, args, complain, in_decl); | |
7245 | else | |
00d3396f | 7246 | { |
015c2c66 MM |
7247 | r = tsubst_expr (t, args, complain, in_decl, |
7248 | /*integral_constant_expression_p=*/true); | |
6f25cb35 | 7249 | r = fold_non_dependent_expr (r); |
bd83b409 | 7250 | } |
5d80a306 DG |
7251 | return r; |
7252 | } | |
7253 | ||
7254 | /* Substitute ARGS into T, which is an pack expansion | |
7255 | (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a | |
7256 | TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node | |
7257 | (if only a partial substitution could be performed) or | |
7258 | ERROR_MARK_NODE if there was an error. */ | |
7259 | tree | |
7260 | tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, | |
7261 | tree in_decl) | |
7262 | { | |
7263 | tree pattern; | |
7264 | tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE; | |
7265 | tree first_arg_pack; int i, len = -1; | |
7266 | tree result; | |
7267 | int incomplete = 0; | |
7268 | ||
7269 | gcc_assert (PACK_EXPANSION_P (t)); | |
7270 | pattern = PACK_EXPANSION_PATTERN (t); | |
7271 | ||
7272 | /* Determine the argument packs that will instantiate the parameter | |
7273 | packs used in the expansion expression. While we're at it, | |
7274 | compute the number of arguments to be expanded and make sure it | |
7275 | is consistent. */ | |
7276 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack; | |
7277 | pack = TREE_CHAIN (pack)) | |
7278 | { | |
7279 | tree parm_pack = TREE_VALUE (pack); | |
7280 | tree arg_pack = NULL_TREE; | |
7281 | tree orig_arg = NULL_TREE; | |
7282 | ||
7283 | if (TREE_CODE (parm_pack) == PARM_DECL) | |
29b0d1fd | 7284 | arg_pack = retrieve_local_specialization (parm_pack); |
5d80a306 DG |
7285 | else |
7286 | { | |
7287 | int level, idx, levels; | |
7288 | template_parm_level_and_index (parm_pack, &level, &idx); | |
7289 | ||
7290 | levels = TMPL_ARGS_DEPTH (args); | |
7291 | if (level <= levels) | |
7292 | arg_pack = TMPL_ARG (args, level, idx); | |
7293 | } | |
7294 | ||
7295 | orig_arg = arg_pack; | |
7296 | if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT) | |
7297 | arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack); | |
7298 | ||
d393153e DG |
7299 | if (arg_pack && !ARGUMENT_PACK_P (arg_pack)) |
7300 | /* This can only happen if we forget to expand an argument | |
7301 | pack somewhere else. Just return an error, silently. */ | |
7302 | { | |
7303 | result = make_tree_vec (1); | |
7304 | TREE_VEC_ELT (result, 0) = error_mark_node; | |
7305 | return result; | |
7306 | } | |
7307 | ||
88b82314 DG |
7308 | if (arg_pack |
7309 | && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1 | |
7310 | && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0))) | |
7311 | { | |
7312 | tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0); | |
7313 | tree pattern = PACK_EXPANSION_PATTERN (expansion); | |
7314 | if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack)) | |
7315 | || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern))) | |
7316 | /* The argument pack that the parameter maps to is just an | |
7317 | expansion of the parameter itself, such as one would | |
7318 | find in the implicit typedef of a class inside the | |
7319 | class itself. Consider this parameter "unsubstituted", | |
7320 | so that we will maintain the outer pack expansion. */ | |
7321 | arg_pack = NULL_TREE; | |
7322 | } | |
7323 | ||
5d80a306 DG |
7324 | if (arg_pack) |
7325 | { | |
7326 | int my_len = | |
7327 | TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)); | |
7328 | ||
7329 | /* It's all-or-nothing with incomplete argument packs. */ | |
7330 | if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
7331 | return error_mark_node; | |
7332 | ||
7333 | if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
7334 | incomplete = 1; | |
7335 | ||
7336 | if (len < 0) | |
7337 | { | |
7338 | len = my_len; | |
7339 | first_arg_pack = arg_pack; | |
7340 | } | |
7341 | else if (len != my_len) | |
7342 | { | |
7343 | if (TREE_CODE (t) == TYPE_PACK_EXPANSION) | |
7344 | error ("mismatched argument pack lengths while expanding " | |
7345 | "%<%T%>", | |
7346 | pattern); | |
7347 | else | |
7348 | error ("mismatched argument pack lengths while expanding " | |
7349 | "%<%E%>", | |
7350 | pattern); | |
7351 | return error_mark_node; | |
7352 | } | |
7353 | ||
7354 | /* Keep track of the parameter packs and their corresponding | |
7355 | argument packs. */ | |
7356 | packs = tree_cons (parm_pack, arg_pack, packs); | |
7357 | TREE_TYPE (packs) = orig_arg; | |
7358 | } | |
7359 | else | |
7360 | /* We can't substitute for this parameter pack. */ | |
7361 | unsubstituted_packs = tree_cons (TREE_PURPOSE (pack), | |
7362 | TREE_VALUE (pack), | |
7363 | unsubstituted_packs); | |
7364 | } | |
7365 | ||
7366 | /* We cannot expand this expansion expression, because we don't have | |
7367 | all of the argument packs we need. Substitute into the pattern | |
7368 | and return a PACK_EXPANSION_*. The caller will need to deal with | |
7369 | that. */ | |
7370 | if (unsubstituted_packs) | |
7371 | return make_pack_expansion (tsubst (pattern, args, complain, | |
7372 | in_decl)); | |
7373 | ||
7374 | /* We could not find any argument packs that work. */ | |
7375 | if (len < 0) | |
7376 | return error_mark_node; | |
7377 | ||
7378 | /* For each argument in each argument pack, substitute into the | |
7379 | pattern. */ | |
7380 | result = make_tree_vec (len + incomplete); | |
7381 | for (i = 0; i < len + incomplete; ++i) | |
7382 | { | |
7383 | /* For parameter pack, change the substitution of the parameter | |
7384 | pack to the ith argument in its argument pack, then expand | |
7385 | the pattern. */ | |
7386 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
7387 | { | |
7388 | tree parm = TREE_PURPOSE (pack); | |
7389 | ||
7390 | if (TREE_CODE (parm) == PARM_DECL) | |
7391 | { | |
7392 | /* Select the Ith argument from the pack. */ | |
7393 | tree arg = make_node (ARGUMENT_PACK_SELECT); | |
7394 | ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack); | |
7395 | ARGUMENT_PACK_SELECT_INDEX (arg) = i; | |
7396 | mark_used (parm); | |
7397 | register_local_specialization (arg, parm); | |
7398 | } | |
7399 | else | |
7400 | { | |
7401 | tree value = parm; | |
7402 | int idx, level; | |
7403 | template_parm_level_and_index (parm, &level, &idx); | |
7404 | ||
7405 | if (i < len) | |
7406 | { | |
7407 | /* Select the Ith argument from the pack. */ | |
7408 | value = make_node (ARGUMENT_PACK_SELECT); | |
7409 | ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack); | |
7410 | ARGUMENT_PACK_SELECT_INDEX (value) = i; | |
7411 | } | |
7412 | ||
7413 | /* Update the corresponding argument. */ | |
7414 | TMPL_ARG (args, level, idx) = value; | |
7415 | } | |
7416 | } | |
7417 | ||
7418 | /* Substitute into the PATTERN with the altered arguments. */ | |
7419 | if (TREE_CODE (t) == EXPR_PACK_EXPANSION) | |
7420 | TREE_VEC_ELT (result, i) = | |
7421 | tsubst_expr (pattern, args, complain, in_decl, | |
7422 | /*integral_constant_expression_p=*/false); | |
7423 | else | |
7424 | TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl); | |
7425 | ||
7426 | if (i == len) | |
7427 | /* When we have incomplete argument packs, the last "expanded" | |
7428 | result is itself a pack expansion, which allows us | |
7429 | to deduce more arguments. */ | |
7430 | TREE_VEC_ELT (result, i) = | |
7431 | make_pack_expansion (TREE_VEC_ELT (result, i)); | |
7432 | ||
7433 | if (TREE_VEC_ELT (result, i) == error_mark_node) | |
7434 | { | |
7435 | result = error_mark_node; | |
7436 | break; | |
7437 | } | |
7438 | } | |
7439 | ||
7440 | /* Update ARGS to restore the substitution from parameter packs to | |
7441 | their argument packs. */ | |
7442 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
7443 | { | |
7444 | tree parm = TREE_PURPOSE (pack); | |
7445 | ||
7446 | if (TREE_CODE (parm) == PARM_DECL) | |
7447 | register_local_specialization (TREE_TYPE (pack), parm); | |
7448 | else | |
7449 | { | |
7450 | int idx, level; | |
7451 | template_parm_level_and_index (parm, &level, &idx); | |
7452 | ||
7453 | /* Update the corresponding argument. */ | |
7454 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
7455 | TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) = | |
7456 | TREE_TYPE (pack); | |
7457 | else | |
7458 | TREE_VEC_ELT (args, idx) = TREE_TYPE (pack); | |
7459 | } | |
7460 | } | |
7461 | ||
7462 | return result; | |
bd83b409 NS |
7463 | } |
7464 | ||
a91db711 | 7465 | /* Substitute ARGS into the vector or list of template arguments T. */ |
830bfa74 | 7466 | |
e9659ab0 | 7467 | static tree |
a91db711 | 7468 | tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
830bfa74 | 7469 | { |
5d80a306 | 7470 | tree orig_t = t; |
bf12d54d | 7471 | int len = TREE_VEC_LENGTH (t); |
5d80a306 | 7472 | int need_new = 0, i, expanded_len_adjust = 0, out; |
67f5655f | 7473 | tree *elts = (tree *) alloca (len * sizeof (tree)); |
c8094d83 | 7474 | |
830bfa74 MM |
7475 | for (i = 0; i < len; i++) |
7476 | { | |
bf12d54d NS |
7477 | tree orig_arg = TREE_VEC_ELT (t, i); |
7478 | tree new_arg; | |
a91db711 | 7479 | |
bf12d54d NS |
7480 | if (TREE_CODE (orig_arg) == TREE_VEC) |
7481 | new_arg = tsubst_template_args (orig_arg, args, complain, in_decl); | |
5d80a306 DG |
7482 | else if (PACK_EXPANSION_P (orig_arg)) |
7483 | { | |
7484 | /* Substitute into an expansion expression. */ | |
7485 | new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl); | |
7486 | ||
7487 | if (TREE_CODE (new_arg) == TREE_VEC) | |
7488 | /* Add to the expanded length adjustment the number of | |
7489 | expanded arguments. We subtract one from this | |
7490 | measurement, because the argument pack expression | |
7491 | itself is already counted as 1 in | |
7492 | LEN. EXPANDED_LEN_ADJUST can actually be negative, if | |
7493 | the argument pack is empty. */ | |
7494 | expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1; | |
7495 | } | |
7496 | else if (ARGUMENT_PACK_P (orig_arg)) | |
7497 | { | |
7498 | /* Substitute into each of the arguments. */ | |
7499 | new_arg = make_node (TREE_CODE (orig_arg)); | |
7500 | ||
7501 | SET_ARGUMENT_PACK_ARGS ( | |
7502 | new_arg, | |
7503 | tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg), | |
7504 | args, complain, in_decl)); | |
7505 | ||
7506 | if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node) | |
7507 | new_arg = error_mark_node; | |
7508 | ||
7509 | if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) { | |
7510 | TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args, | |
7511 | complain, in_decl); | |
7512 | TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg); | |
7513 | ||
7514 | if (TREE_TYPE (new_arg) == error_mark_node) | |
7515 | new_arg = error_mark_node; | |
7516 | } | |
7517 | } | |
830bfa74 | 7518 | else |
a91db711 | 7519 | new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl); |
c8094d83 | 7520 | |
a91db711 | 7521 | if (new_arg == error_mark_node) |
08e72a19 JM |
7522 | return error_mark_node; |
7523 | ||
a91db711 NS |
7524 | elts[i] = new_arg; |
7525 | if (new_arg != orig_arg) | |
830bfa74 MM |
7526 | need_new = 1; |
7527 | } | |
c8094d83 | 7528 | |
830bfa74 MM |
7529 | if (!need_new) |
7530 | return t; | |
a91db711 | 7531 | |
5d80a306 DG |
7532 | /* Make space for the expanded arguments coming from template |
7533 | argument packs. */ | |
7534 | t = make_tree_vec (len + expanded_len_adjust); | |
7535 | for (i = 0, out = 0; i < len; i++) | |
7536 | { | |
7537 | if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i)) | |
7538 | || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i))) | |
7539 | && TREE_CODE (elts[i]) == TREE_VEC) | |
7540 | { | |
7541 | int idx; | |
7542 | ||
7543 | /* Now expand the template argument pack "in place". */ | |
7544 | for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++) | |
7545 | TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx); | |
7546 | } | |
7547 | else | |
7548 | { | |
7549 | TREE_VEC_ELT (t, out) = elts[i]; | |
7550 | out++; | |
7551 | } | |
7552 | } | |
c8094d83 | 7553 | |
830bfa74 MM |
7554 | return t; |
7555 | } | |
7556 | ||
36a117a5 MM |
7557 | /* Return the result of substituting ARGS into the template parameters |
7558 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
7559 | PARMS, then the result will contain n levels of PARMS. For | |
7560 | example, if PARMS is `template <class T> template <class U> | |
7561 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
7562 | result will be `template <int*, double, class V>'. */ | |
7563 | ||
e9659ab0 | 7564 | static tree |
3a978d72 | 7565 | tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain) |
36a117a5 | 7566 | { |
f71f87f9 MM |
7567 | tree r = NULL_TREE; |
7568 | tree* new_parms; | |
36a117a5 | 7569 | |
8ca4bf25 MM |
7570 | /* When substituting into a template, we must set |
7571 | PROCESSING_TEMPLATE_DECL as the template parameters may be | |
7572 | dependent if they are based on one-another, and the dependency | |
7573 | predicates are short-circuit outside of templates. */ | |
7574 | ++processing_template_decl; | |
7575 | ||
36a117a5 MM |
7576 | for (new_parms = &r; |
7577 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
7578 | new_parms = &(TREE_CHAIN (*new_parms)), | |
7579 | parms = TREE_CHAIN (parms)) | |
7580 | { | |
c8094d83 | 7581 | tree new_vec = |
36a117a5 MM |
7582 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); |
7583 | int i; | |
c8094d83 | 7584 | |
36a117a5 MM |
7585 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) |
7586 | { | |
42b304f1 LM |
7587 | tree tuple; |
7588 | tree default_value; | |
7589 | tree parm_decl; | |
7590 | ||
7591 | if (parms == error_mark_node) | |
7592 | continue; | |
7593 | ||
7594 | tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); | |
7595 | ||
7596 | if (tuple == error_mark_node) | |
7597 | continue; | |
7598 | ||
7599 | default_value = TREE_PURPOSE (tuple); | |
7600 | parm_decl = TREE_VALUE (tuple); | |
833aa4c4 NS |
7601 | |
7602 | parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); | |
a207780f VR |
7603 | if (TREE_CODE (parm_decl) == PARM_DECL |
7604 | && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain)) | |
7605 | parm_decl = error_mark_node; | |
a91db711 NS |
7606 | default_value = tsubst_template_arg (default_value, args, |
7607 | complain, NULL_TREE); | |
c8094d83 | 7608 | |
a91db711 | 7609 | tuple = build_tree_list (default_value, parm_decl); |
833aa4c4 | 7610 | TREE_VEC_ELT (new_vec, i) = tuple; |
36a117a5 | 7611 | } |
c8094d83 MS |
7612 | |
7613 | *new_parms = | |
7614 | tree_cons (size_int (TMPL_PARMS_DEPTH (parms) | |
4890c2f4 | 7615 | - TMPL_ARGS_DEPTH (args)), |
36a117a5 MM |
7616 | new_vec, NULL_TREE); |
7617 | } | |
7618 | ||
8ca4bf25 MM |
7619 | --processing_template_decl; |
7620 | ||
36a117a5 MM |
7621 | return r; |
7622 | } | |
7623 | ||
ed44da02 MM |
7624 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
7625 | type T. If T is not an aggregate or enumeration type, it is | |
7626 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
838dfd8a | 7627 | ENTERING_SCOPE is nonzero, T is the context for a template which |
dc957d14 | 7628 | we are presently tsubst'ing. Return the substituted value. */ |
36a117a5 | 7629 | |
e9659ab0 | 7630 | static tree |
c8094d83 | 7631 | tsubst_aggr_type (tree t, |
0cbd7506 MS |
7632 | tree args, |
7633 | tsubst_flags_t complain, | |
7634 | tree in_decl, | |
7635 | int entering_scope) | |
36a117a5 MM |
7636 | { |
7637 | if (t == NULL_TREE) | |
7638 | return NULL_TREE; | |
7639 | ||
7640 | switch (TREE_CODE (t)) | |
7641 | { | |
7642 | case RECORD_TYPE: | |
7643 | if (TYPE_PTRMEMFUNC_P (t)) | |
46c895ac | 7644 | return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); |
36a117a5 | 7645 | |
f4f206f4 | 7646 | /* Else fall through. */ |
ed44da02 | 7647 | case ENUMERAL_TYPE: |
36a117a5 | 7648 | case UNION_TYPE: |
82390eb6 | 7649 | if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t)) |
36a117a5 MM |
7650 | { |
7651 | tree argvec; | |
7652 | tree context; | |
7653 | tree r; | |
3489ea0c MM |
7654 | bool saved_skip_evaluation; |
7655 | ||
7656 | /* In "sizeof(X<I>)" we need to evaluate "I". */ | |
7657 | saved_skip_evaluation = skip_evaluation; | |
7658 | skip_evaluation = false; | |
36a117a5 MM |
7659 | |
7660 | /* First, determine the context for the type we are looking | |
7661 | up. */ | |
4f7847ca NS |
7662 | context = TYPE_CONTEXT (t); |
7663 | if (context) | |
29b0d1fd JM |
7664 | { |
7665 | context = tsubst_aggr_type (context, args, complain, | |
7666 | in_decl, /*entering_scope=*/1); | |
7667 | /* If context is a nested class inside a class template, | |
7668 | it may still need to be instantiated (c++/33959). */ | |
7669 | if (TYPE_P (context)) | |
7670 | context = complete_type (context); | |
7671 | } | |
36a117a5 MM |
7672 | |
7673 | /* Then, figure out what arguments are appropriate for the | |
7674 | type we are trying to find. For example, given: | |
7675 | ||
7676 | template <class T> struct S; | |
7677 | template <class T, class U> void f(T, U) { S<U> su; } | |
7678 | ||
7679 | and supposing that we are instantiating f<int, double>, | |
7680 | then our ARGS will be {int, double}, but, when looking up | |
7681 | S we only want {double}. */ | |
a91db711 NS |
7682 | argvec = tsubst_template_args (TYPE_TI_ARGS (t), args, |
7683 | complain, in_decl); | |
08e72a19 | 7684 | if (argvec == error_mark_node) |
3489ea0c MM |
7685 | r = error_mark_node; |
7686 | else | |
7687 | { | |
7688 | r = lookup_template_class (t, argvec, in_decl, context, | |
7689 | entering_scope, complain); | |
7690 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); | |
7691 | } | |
3db45ab5 | 7692 | |
3489ea0c | 7693 | skip_evaluation = saved_skip_evaluation; |
36a117a5 | 7694 | |
3489ea0c | 7695 | return r; |
36a117a5 | 7696 | } |
c8094d83 | 7697 | else |
36a117a5 MM |
7698 | /* This is not a template type, so there's nothing to do. */ |
7699 | return t; | |
7700 | ||
7701 | default: | |
4393e105 | 7702 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
7703 | } |
7704 | } | |
7705 | ||
9188c363 MM |
7706 | /* Substitute into the default argument ARG (a default argument for |
7707 | FN), which has the indicated TYPE. */ | |
7708 | ||
7709 | tree | |
3a978d72 | 7710 | tsubst_default_argument (tree fn, tree type, tree arg) |
9188c363 | 7711 | { |
2436b51f MM |
7712 | tree saved_class_ptr = NULL_TREE; |
7713 | tree saved_class_ref = NULL_TREE; | |
7714 | ||
9188c363 MM |
7715 | /* This default argument came from a template. Instantiate the |
7716 | default argument here, not in tsubst. In the case of | |
c8094d83 MS |
7717 | something like: |
7718 | ||
9188c363 MM |
7719 | template <class T> |
7720 | struct S { | |
7721 | static T t(); | |
7722 | void f(T = t()); | |
7723 | }; | |
c8094d83 | 7724 | |
9188c363 | 7725 | we must be careful to do name lookup in the scope of S<T>, |
d5a10cf0 | 7726 | rather than in the current class. */ |
2b59fc25 | 7727 | push_access_scope (fn); |
2436b51f MM |
7728 | /* The "this" pointer is not valid in a default argument. */ |
7729 | if (cfun) | |
7730 | { | |
7731 | saved_class_ptr = current_class_ptr; | |
7732 | cp_function_chain->x_current_class_ptr = NULL_TREE; | |
7733 | saved_class_ref = current_class_ref; | |
7734 | cp_function_chain->x_current_class_ref = NULL_TREE; | |
7735 | } | |
9188c363 | 7736 | |
d5a10cf0 | 7737 | push_deferring_access_checks(dk_no_deferred); |
5a8613b2 MM |
7738 | /* The default argument expression may cause implicitly defined |
7739 | member functions to be synthesized, which will result in garbage | |
7740 | collection. We must treat this situation as if we were within | |
7741 | the body of function so as to avoid collecting live data on the | |
7742 | stack. */ | |
7743 | ++function_depth; | |
c2ea3a40 | 7744 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), |
015c2c66 MM |
7745 | tf_warning_or_error, NULL_TREE, |
7746 | /*integral_constant_expression_p=*/false); | |
5a8613b2 | 7747 | --function_depth; |
d5a10cf0 MM |
7748 | pop_deferring_access_checks(); |
7749 | ||
2436b51f MM |
7750 | /* Restore the "this" pointer. */ |
7751 | if (cfun) | |
7752 | { | |
7753 | cp_function_chain->x_current_class_ptr = saved_class_ptr; | |
7754 | cp_function_chain->x_current_class_ref = saved_class_ref; | |
7755 | } | |
7756 | ||
2b59fc25 | 7757 | pop_access_scope (fn); |
9188c363 MM |
7758 | |
7759 | /* Make sure the default argument is reasonable. */ | |
7760 | arg = check_default_argument (type, arg); | |
7761 | ||
7762 | return arg; | |
7763 | } | |
7764 | ||
7765 | /* Substitute into all the default arguments for FN. */ | |
7766 | ||
7767 | static void | |
3a978d72 | 7768 | tsubst_default_arguments (tree fn) |
9188c363 MM |
7769 | { |
7770 | tree arg; | |
7771 | tree tmpl_args; | |
7772 | ||
7773 | tmpl_args = DECL_TI_ARGS (fn); | |
7774 | ||
7775 | /* If this function is not yet instantiated, we certainly don't need | |
7776 | its default arguments. */ | |
7777 | if (uses_template_parms (tmpl_args)) | |
7778 | return; | |
7779 | ||
c8094d83 MS |
7780 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
7781 | arg; | |
9188c363 MM |
7782 | arg = TREE_CHAIN (arg)) |
7783 | if (TREE_PURPOSE (arg)) | |
c8094d83 | 7784 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, |
9188c363 MM |
7785 | TREE_VALUE (arg), |
7786 | TREE_PURPOSE (arg)); | |
7787 | } | |
7788 | ||
fc6a28d7 MM |
7789 | /* Substitute the ARGS into the T, which is a _DECL. Return the |
7790 | result of the substitution. Issue error and warning messages under | |
7791 | control of COMPLAIN. */ | |
00d3396f | 7792 | |
e9659ab0 | 7793 | static tree |
fc6a28d7 | 7794 | tsubst_decl (tree t, tree args, tsubst_flags_t complain) |
8d08fdba | 7795 | { |
82a98427 | 7796 | location_t saved_loc; |
b370501f | 7797 | tree r = NULL_TREE; |
4b2811e9 | 7798 | tree in_decl = t; |
830bfa74 | 7799 | |
ae58fa02 | 7800 | /* Set the filename and linenumber to improve error-reporting. */ |
82a98427 | 7801 | saved_loc = input_location; |
f31686a3 | 7802 | input_location = DECL_SOURCE_LOCATION (t); |
b7484fbe | 7803 | |
8d08fdba MS |
7804 | switch (TREE_CODE (t)) |
7805 | { | |
98c1c668 JM |
7806 | case TEMPLATE_DECL: |
7807 | { | |
28e42b7e KL |
7808 | /* We can get here when processing a member function template, |
7809 | member class template, and template template parameter of | |
7810 | a template class. */ | |
98c1c668 | 7811 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 7812 | tree spec; |
28e42b7e KL |
7813 | tree tmpl_args; |
7814 | tree full_args; | |
98c1c668 | 7815 | |
28e42b7e | 7816 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
db2767b6 | 7817 | { |
28e42b7e KL |
7818 | /* Template template parameter is treated here. */ |
7819 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
7820 | if (new_type == error_mark_node) | |
7821 | return error_mark_node; | |
36a117a5 | 7822 | |
28e42b7e KL |
7823 | r = copy_decl (t); |
7824 | TREE_CHAIN (r) = NULL_TREE; | |
7825 | TREE_TYPE (r) = new_type; | |
7826 | DECL_TEMPLATE_RESULT (r) | |
7827 | = build_decl (TYPE_DECL, DECL_NAME (decl), new_type); | |
c8094d83 | 7828 | DECL_TEMPLATE_PARMS (r) |
28e42b7e KL |
7829 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
7830 | complain); | |
7831 | TYPE_NAME (new_type) = r; | |
7832 | break; | |
7833 | } | |
36a117a5 | 7834 | |
28e42b7e KL |
7835 | /* We might already have an instance of this template. |
7836 | The ARGS are for the surrounding class type, so the | |
7837 | full args contain the tsubst'd args for the context, | |
7838 | plus the innermost args from the template decl. */ | |
c8094d83 | 7839 | tmpl_args = DECL_CLASS_TEMPLATE_P (t) |
28e42b7e KL |
7840 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) |
7841 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); | |
8ca4bf25 MM |
7842 | /* Because this is a template, the arguments will still be |
7843 | dependent, even after substitution. If | |
7844 | PROCESSING_TEMPLATE_DECL is not set, the dependency | |
7845 | predicates will short-circuit. */ | |
7846 | ++processing_template_decl; | |
28e42b7e KL |
7847 | full_args = tsubst_template_args (tmpl_args, args, |
7848 | complain, in_decl); | |
8ca4bf25 | 7849 | --processing_template_decl; |
bf2f7328 VR |
7850 | if (full_args == error_mark_node) |
7851 | return error_mark_node; | |
28e42b7e KL |
7852 | |
7853 | /* tsubst_template_args doesn't copy the vector if | |
7854 | nothing changed. But, *something* should have | |
7855 | changed. */ | |
7856 | gcc_assert (full_args != tmpl_args); | |
7857 | ||
7858 | spec = retrieve_specialization (t, full_args, | |
7859 | /*class_specializations_p=*/true); | |
7860 | if (spec != NULL_TREE) | |
7861 | { | |
7862 | r = spec; | |
7863 | break; | |
db2767b6 | 7864 | } |
98c1c668 JM |
7865 | |
7866 | /* Make a new template decl. It will be similar to the | |
c8094d83 | 7867 | original, but will record the current template arguments. |
98c1c668 JM |
7868 | We also create a new function declaration, which is just |
7869 | like the old one, but points to this new template, rather | |
7870 | than the old one. */ | |
0acf7199 | 7871 | r = copy_decl (t); |
50bc768d | 7872 | gcc_assert (DECL_LANG_SPECIFIC (r) != 0); |
ae58fa02 | 7873 | TREE_CHAIN (r) = NULL_TREE; |
db2767b6 | 7874 | |
ae58fa02 | 7875 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
7876 | |
7877 | if (TREE_CODE (decl) == TYPE_DECL) | |
7878 | { | |
8ca4bf25 MM |
7879 | tree new_type; |
7880 | ++processing_template_decl; | |
7881 | new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
3db45ab5 | 7882 | --processing_template_decl; |
2620d095 KL |
7883 | if (new_type == error_mark_node) |
7884 | return error_mark_node; | |
7885 | ||
ae58fa02 MM |
7886 | TREE_TYPE (r) = new_type; |
7887 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
17aec3eb | 7888 | DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); |
ae58fa02 | 7889 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); |
8ca4bf25 | 7890 | DECL_CONTEXT (r) = TYPE_CONTEXT (new_type); |
93cdc044 JM |
7891 | } |
7892 | else | |
7893 | { | |
8ca4bf25 MM |
7894 | tree new_decl; |
7895 | ++processing_template_decl; | |
7896 | new_decl = tsubst (decl, args, complain, in_decl); | |
7897 | --processing_template_decl; | |
caec1dc0 KL |
7898 | if (new_decl == error_mark_node) |
7899 | return error_mark_node; | |
17aec3eb RK |
7900 | |
7901 | DECL_TEMPLATE_RESULT (r) = new_decl; | |
ae58fa02 MM |
7902 | DECL_TI_TEMPLATE (new_decl) = r; |
7903 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
7904 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
3db45ab5 | 7905 | DECL_CONTEXT (r) = DECL_CONTEXT (new_decl); |
93cdc044 JM |
7906 | } |
7907 | ||
ae58fa02 MM |
7908 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
7909 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
7910 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
7911 | |
7912 | /* The template parameters for this new template are all the | |
7913 | template parameters for the old template, except the | |
c6002625 | 7914 | outermost level of parameters. */ |
c8094d83 | 7915 | DECL_TEMPLATE_PARMS (r) |
4393e105 | 7916 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
c2ea3a40 | 7917 | complain); |
98c1c668 | 7918 | |
93cdc044 | 7919 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 7920 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 7921 | |
8c6ab2db NS |
7922 | if (TREE_CODE (decl) != TYPE_DECL) |
7923 | /* Record this non-type partial instantiation. */ | |
c8094d83 | 7924 | register_specialization (r, t, |
d63d5d0c ILT |
7925 | DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)), |
7926 | false); | |
98c1c668 | 7927 | } |
ae58fa02 | 7928 | break; |
8d08fdba MS |
7929 | |
7930 | case FUNCTION_DECL: | |
7931 | { | |
386b8a85 | 7932 | tree ctx; |
87603ed0 | 7933 | tree argvec = NULL_TREE; |
cf38f48a | 7934 | tree *friends; |
36a117a5 | 7935 | tree gen_tmpl; |
fc6a28d7 | 7936 | tree type; |
5566b478 | 7937 | int member; |
d8c4447d MM |
7938 | int args_depth; |
7939 | int parms_depth; | |
5566b478 | 7940 | |
36a117a5 | 7941 | /* Nobody should be tsubst'ing into non-template functions. */ |
50bc768d | 7942 | gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE); |
36a117a5 MM |
7943 | |
7944 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
7945 | { | |
7946 | tree spec; | |
00cf3e31 MM |
7947 | bool dependent_p; |
7948 | ||
7949 | /* If T is not dependent, just return it. We have to | |
7950 | increment PROCESSING_TEMPLATE_DECL because | |
7951 | value_dependent_expression_p assumes that nothing is | |
7952 | dependent when PROCESSING_TEMPLATE_DECL is zero. */ | |
7953 | ++processing_template_decl; | |
7954 | dependent_p = value_dependent_expression_p (t); | |
7955 | --processing_template_decl; | |
7956 | if (!dependent_p) | |
7957 | return t; | |
36a117a5 MM |
7958 | |
7959 | /* Calculate the most general template of which R is a | |
7960 | specialization, and the complete set of arguments used to | |
7961 | specialize R. */ | |
7962 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
c8094d83 | 7963 | argvec = tsubst_template_args (DECL_TI_ARGS |
a91db711 | 7964 | (DECL_TEMPLATE_RESULT (gen_tmpl)), |
c8094d83 | 7965 | args, complain, in_decl); |
36a117a5 MM |
7966 | |
7967 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
7968 | spec = retrieve_specialization (gen_tmpl, argvec, |
7969 | /*class_specializations_p=*/false); | |
7ddedda4 | 7970 | |
36a117a5 | 7971 | if (spec) |
ae58fa02 MM |
7972 | { |
7973 | r = spec; | |
7974 | break; | |
7975 | } | |
d8c4447d | 7976 | |
f9a7ae04 MM |
7977 | /* We can see more levels of arguments than parameters if |
7978 | there was a specialization of a member template, like | |
7979 | this: | |
7980 | ||
0cbd7506 | 7981 | template <class T> struct S { template <class U> void f(); } |
c8094d83 | 7982 | template <> template <class U> void S<int>::f(U); |
f9a7ae04 | 7983 | |
dc957d14 | 7984 | Here, we'll be substituting into the specialization, |
f9a7ae04 MM |
7985 | because that's where we can find the code we actually |
7986 | want to generate, but we'll have enough arguments for | |
c8094d83 | 7987 | the most general template. |
f9a7ae04 MM |
7988 | |
7989 | We also deal with the peculiar case: | |
d8c4447d | 7990 | |
c8094d83 | 7991 | template <class T> struct S { |
d8c4447d MM |
7992 | template <class U> friend void f(); |
7993 | }; | |
74b846e0 | 7994 | template <class U> void f() {} |
d8c4447d MM |
7995 | template S<int>; |
7996 | template void f<double>(); | |
7997 | ||
7998 | Here, the ARGS for the instantiation of will be {int, | |
7999 | double}. But, we only need as many ARGS as there are | |
8000 | levels of template parameters in CODE_PATTERN. We are | |
8001 | careful not to get fooled into reducing the ARGS in | |
8002 | situations like: | |
8003 | ||
8004 | template <class T> struct S { template <class U> void f(U); } | |
8005 | template <class T> template <> void S<T>::f(int) {} | |
8006 | ||
8007 | which we can spot because the pattern will be a | |
8008 | specialization in this case. */ | |
8009 | args_depth = TMPL_ARGS_DEPTH (args); | |
c8094d83 MS |
8010 | parms_depth = |
8011 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
d8c4447d MM |
8012 | if (args_depth > parms_depth |
8013 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
f9a7ae04 | 8014 | args = get_innermost_template_args (args, parms_depth); |
36a117a5 MM |
8015 | } |
8016 | else | |
8017 | { | |
8018 | /* This special case arises when we have something like this: | |
8019 | ||
0cbd7506 | 8020 | template <class T> struct S { |
c8094d83 | 8021 | friend void f<int>(int, double); |
36a117a5 MM |
8022 | }; |
8023 | ||
8024 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
10b1d5e7 MM |
8025 | will be an IDENTIFIER_NODE. We are being called from |
8026 | tsubst_friend_function, and we want only to create a | |
8027 | new decl (R) with appropriate types so that we can call | |
8028 | determine_specialization. */ | |
36a117a5 MM |
8029 | gen_tmpl = NULL_TREE; |
8030 | } | |
8031 | ||
6eb3bb27 | 8032 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 8033 | { |
5566b478 MS |
8034 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
8035 | member = 2; | |
8036 | else | |
8037 | member = 1; | |
c8094d83 | 8038 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
8c6ab2db | 8039 | complain, t, /*entering_scope=*/1); |
5566b478 MS |
8040 | } |
8041 | else | |
8042 | { | |
8043 | member = 0; | |
4f1c5b7d | 8044 | ctx = DECL_CONTEXT (t); |
5566b478 | 8045 | } |
fc6a28d7 | 8046 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
b3da7bb1 MM |
8047 | if (type == error_mark_node) |
8048 | return error_mark_node; | |
8d08fdba | 8049 | |
5566b478 | 8050 | /* We do NOT check for matching decls pushed separately at this |
0cbd7506 MS |
8051 | point, as they may not represent instantiations of this |
8052 | template, and in any case are considered separate under the | |
8053 | discrete model. */ | |
0acf7199 | 8054 | r = copy_decl (t); |
e1467ff2 | 8055 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 | 8056 | TREE_TYPE (r) = type; |
92643fea MM |
8057 | /* Clear out the mangled name and RTL for the instantiation. */ |
8058 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
8059 | SET_DECL_RTL (r, NULL_RTX); | |
5bd61841 | 8060 | DECL_INITIAL (r) = NULL_TREE; |
4f1c5b7d | 8061 | DECL_CONTEXT (r) = ctx; |
5566b478 | 8062 | |
c8094d83 | 8063 | if (member && DECL_CONV_FN_P (r)) |
1f84ec23 MM |
8064 | /* Type-conversion operator. Reconstruct the name, in |
8065 | case it's the name of one of the template's parameters. */ | |
8066 | DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); | |
5566b478 | 8067 | |
4393e105 | 8068 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
c2ea3a40 | 8069 | complain, t); |
477f6664 | 8070 | DECL_RESULT (r) = NULL_TREE; |
711734a9 JM |
8071 | |
8072 | TREE_STATIC (r) = 0; | |
8073 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
8074 | DECL_EXTERNAL (r) = 1; | |
4684cd27 MM |
8075 | /* If this is an instantiation of a function with internal |
8076 | linkage, we already know what object file linkage will be | |
8077 | assigned to the instantiation. */ | |
8078 | DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r); | |
711734a9 JM |
8079 | DECL_DEFER_OUTPUT (r) = 0; |
8080 | TREE_CHAIN (r) = NULL_TREE; | |
8081 | DECL_PENDING_INLINE_INFO (r) = 0; | |
59026e79 | 8082 | DECL_PENDING_INLINE_P (r) = 0; |
655dc6ee | 8083 | DECL_SAVED_TREE (r) = NULL_TREE; |
711734a9 | 8084 | TREE_USED (r) = 0; |
db9b2174 MM |
8085 | if (DECL_CLONED_FUNCTION (r)) |
8086 | { | |
8087 | DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t), | |
c2ea3a40 | 8088 | args, complain, t); |
db9b2174 MM |
8089 | TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r)); |
8090 | TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r; | |
8091 | } | |
711734a9 | 8092 | |
92643fea MM |
8093 | /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do |
8094 | this in the special friend case mentioned above where | |
8095 | GEN_TMPL is NULL. */ | |
36a117a5 | 8096 | if (gen_tmpl) |
386b8a85 | 8097 | { |
c8094d83 | 8098 | DECL_TEMPLATE_INFO (r) |
e1b3e07d | 8099 | = tree_cons (gen_tmpl, argvec, NULL_TREE); |
36a117a5 | 8100 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
d63d5d0c | 8101 | register_specialization (r, gen_tmpl, argvec, false); |
36a117a5 | 8102 | |
9188c363 MM |
8103 | /* We're not supposed to instantiate default arguments |
8104 | until they are called, for a template. But, for a | |
8105 | declaration like: | |
8106 | ||
0cbd7506 MS |
8107 | template <class T> void f () |
8108 | { extern void g(int i = T()); } | |
c8094d83 | 8109 | |
9188c363 MM |
8110 | we should do the substitution when the template is |
8111 | instantiated. We handle the member function case in | |
8112 | instantiate_class_template since the default arguments | |
8113 | might refer to other members of the class. */ | |
8114 | if (!member | |
8115 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
8116 | && !uses_template_parms (argvec)) | |
8117 | tsubst_default_arguments (r); | |
386b8a85 | 8118 | } |
c43e95f8 MM |
8119 | else |
8120 | DECL_TEMPLATE_INFO (r) = NULL_TREE; | |
f181d4ae | 8121 | |
cf38f48a MM |
8122 | /* Copy the list of befriending classes. */ |
8123 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
8124 | *friends; | |
c8094d83 | 8125 | friends = &TREE_CHAIN (*friends)) |
cf38f48a MM |
8126 | { |
8127 | *friends = copy_node (*friends); | |
8128 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
c2ea3a40 | 8129 | args, complain, |
cf38f48a MM |
8130 | in_decl); |
8131 | } | |
8132 | ||
212e7048 | 8133 | if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) |
f181d4ae MM |
8134 | { |
8135 | maybe_retrofit_in_chrg (r); | |
212e7048 MM |
8136 | if (DECL_CONSTRUCTOR_P (r)) |
8137 | grok_ctor_properties (ctx, r); | |
2be678ff JM |
8138 | /* If this is an instantiation of a member template, clone it. |
8139 | If it isn't, that'll be handled by | |
8140 | clone_constructors_and_destructors. */ | |
5e818b93 | 8141 | if (PRIMARY_TEMPLATE_P (gen_tmpl)) |
271e6f02 | 8142 | clone_function_decl (r, /*update_method_vec_p=*/0); |
f181d4ae | 8143 | } |
398cd199 VR |
8144 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)) |
8145 | && !grok_op_properties (r, (complain & tf_error) != 0)) | |
8146 | return error_mark_node; | |
c8b2e872 MM |
8147 | |
8148 | if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t)) | |
8149 | SET_DECL_FRIEND_CONTEXT (r, | |
8150 | tsubst (DECL_FRIEND_CONTEXT (t), | |
8151 | args, complain, in_decl)); | |
b9e75696 JM |
8152 | |
8153 | /* Possibly limit visibility based on template args. */ | |
8154 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
8155 | if (DECL_VISIBILITY_SPECIFIED (t)) |
8156 | { | |
8157 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
8158 | DECL_ATTRIBUTES (r) | |
8159 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
8160 | } | |
b9e75696 | 8161 | determine_visibility (r); |
4f4141ff JM |
8162 | |
8163 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, | |
8164 | args, complain, in_decl); | |
8d08fdba | 8165 | } |
ae58fa02 | 8166 | break; |
8d08fdba MS |
8167 | |
8168 | case PARM_DECL: | |
8169 | { | |
5d80a306 DG |
8170 | tree type = NULL_TREE; |
8171 | int i, len = 1; | |
8172 | tree expanded_types = NULL_TREE; | |
8173 | tree prev_r = NULL_TREE; | |
8174 | tree first_r = NULL_TREE; | |
1b8899d1 | 8175 | |
5d80a306 DG |
8176 | if (FUNCTION_PARAMETER_PACK_P (t)) |
8177 | { | |
8178 | /* If there is a local specialization that isn't a | |
8179 | parameter pack, it means that we're doing a "simple" | |
8180 | substitution from inside tsubst_pack_expansion. Just | |
a757b5c5 | 8181 | return the local specialization (which will be a single |
5d80a306 | 8182 | parm). */ |
29b0d1fd | 8183 | tree spec = retrieve_local_specialization (t); |
5d80a306 DG |
8184 | if (spec |
8185 | && TREE_CODE (spec) == PARM_DECL | |
8186 | && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION) | |
8187 | return spec; | |
8188 | ||
8189 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
8190 | the parameters in this function parameter pack. */ | |
8191 | expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args, | |
8192 | complain, in_decl); | |
8193 | if (TREE_CODE (expanded_types) == TREE_VEC) | |
8194 | { | |
8195 | len = TREE_VEC_LENGTH (expanded_types); | |
8196 | ||
8197 | /* Zero-length parameter packs are boring. Just substitute | |
8198 | into the chain. */ | |
8199 | if (len == 0) | |
8200 | return tsubst (TREE_CHAIN (t), args, complain, | |
8201 | TREE_CHAIN (t)); | |
8202 | } | |
8203 | else | |
8204 | { | |
8205 | /* All we did was update the type. Make a note of that. */ | |
8206 | type = expanded_types; | |
8207 | expanded_types = NULL_TREE; | |
8208 | } | |
8209 | } | |
db2767b6 | 8210 | |
5d80a306 DG |
8211 | /* Loop through all of the parameter's we'll build. When T is |
8212 | a function parameter pack, LEN is the number of expanded | |
8213 | types in EXPANDED_TYPES; otherwise, LEN is 1. */ | |
8214 | r = NULL_TREE; | |
8215 | for (i = 0; i < len; ++i) | |
8216 | { | |
8217 | prev_r = r; | |
8218 | r = copy_node (t); | |
8219 | if (DECL_TEMPLATE_PARM_P (t)) | |
8220 | SET_DECL_TEMPLATE_PARM_P (r); | |
8221 | ||
8222 | if (expanded_types) | |
8223 | /* We're on the Ith parameter of the function parameter | |
8224 | pack. */ | |
8225 | { | |
8226 | /* Get the Ith type. */ | |
8227 | type = TREE_VEC_ELT (expanded_types, i); | |
8228 | ||
8229 | if (DECL_NAME (r)) | |
8230 | /* Rename the parameter to include the index. */ | |
8231 | DECL_NAME (r) = | |
8232 | make_ith_pack_parameter_name (DECL_NAME (r), i); | |
8233 | } | |
8234 | else if (!type) | |
8235 | /* We're dealing with a normal parameter. */ | |
8236 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
8237 | ||
8238 | type = type_decays_to (type); | |
8239 | TREE_TYPE (r) = type; | |
8240 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); | |
8241 | ||
8242 | if (DECL_INITIAL (r)) | |
8243 | { | |
8244 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) | |
8245 | DECL_INITIAL (r) = TREE_TYPE (r); | |
8246 | else | |
8247 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, | |
8248 | complain, in_decl); | |
8249 | } | |
8250 | ||
8251 | DECL_CONTEXT (r) = NULL_TREE; | |
8252 | ||
8253 | if (!DECL_TEMPLATE_PARM_P (r)) | |
8254 | DECL_ARG_TYPE (r) = type_passed_as (type); | |
8255 | ||
4f4141ff JM |
8256 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, |
8257 | args, complain, in_decl); | |
8258 | ||
5d80a306 DG |
8259 | /* Keep track of the first new parameter we |
8260 | generate. That's what will be returned to the | |
8261 | caller. */ | |
8262 | if (!first_r) | |
8263 | first_r = r; | |
8264 | ||
8265 | /* Build a proper chain of parameters when substituting | |
8266 | into a function parameter pack. */ | |
8267 | if (prev_r) | |
8268 | TREE_CHAIN (prev_r) = r; | |
8269 | } | |
8e51619a | 8270 | |
8d08fdba | 8271 | if (TREE_CHAIN (t)) |
4393e105 | 8272 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
c2ea3a40 | 8273 | complain, TREE_CHAIN (t)); |
5d80a306 DG |
8274 | |
8275 | /* FIRST_R contains the start of the chain we've built. */ | |
8276 | r = first_r; | |
8d08fdba | 8277 | } |
ae58fa02 | 8278 | break; |
8d08fdba | 8279 | |
5566b478 MS |
8280 | case FIELD_DECL: |
8281 | { | |
fc6a28d7 MM |
8282 | tree type; |
8283 | ||
0acf7199 | 8284 | r = copy_decl (t); |
fc6a28d7 MM |
8285 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
8286 | if (type == error_mark_node) | |
8287 | return error_mark_node; | |
1b8899d1 | 8288 | TREE_TYPE (r) = type; |
9804209d | 8289 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
1b8899d1 | 8290 | |
015c2c66 MM |
8291 | /* DECL_INITIAL gives the number of bits in a bit-field. */ |
8292 | DECL_INITIAL (r) | |
8293 | = tsubst_expr (DECL_INITIAL (t), args, | |
8294 | complain, in_decl, | |
8295 | /*integral_constant_expression_p=*/true); | |
1b8899d1 MM |
8296 | /* We don't have to set DECL_CONTEXT here; it is set by |
8297 | finish_member_declaration. */ | |
5566b478 | 8298 | TREE_CHAIN (r) = NULL_TREE; |
c8094d83 | 8299 | if (VOID_TYPE_P (type)) |
dee15844 | 8300 | error ("instantiation of %q+D as type %qT", r, type); |
4f4141ff JM |
8301 | |
8302 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, | |
8303 | args, complain, in_decl); | |
5566b478 | 8304 | } |
ae58fa02 | 8305 | break; |
5566b478 MS |
8306 | |
8307 | case USING_DECL: | |
98ed9dae NS |
8308 | /* We reach here only for member using decls. */ |
8309 | if (DECL_DEPENDENT_P (t)) | |
8310 | { | |
8311 | r = do_class_using_decl | |
8312 | (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl), | |
8313 | tsubst_copy (DECL_NAME (t), args, complain, in_decl)); | |
8314 | if (!r) | |
8315 | r = error_mark_node; | |
8316 | } | |
8317 | else | |
8318 | { | |
8319 | r = copy_node (t); | |
8320 | TREE_CHAIN (r) = NULL_TREE; | |
8321 | } | |
ae58fa02 | 8322 | break; |
5566b478 | 8323 | |
9188c363 | 8324 | case TYPE_DECL: |
5566b478 MS |
8325 | case VAR_DECL: |
8326 | { | |
1cea0434 KG |
8327 | tree argvec = NULL_TREE; |
8328 | tree gen_tmpl = NULL_TREE; | |
36a117a5 | 8329 | tree spec; |
1cea0434 | 8330 | tree tmpl = NULL_TREE; |
9188c363 | 8331 | tree ctx; |
fc6a28d7 | 8332 | tree type = NULL_TREE; |
f604fc3b | 8333 | bool local_p; |
9188c363 | 8334 | |
56d0c6e3 | 8335 | if (TREE_CODE (t) == TYPE_DECL |
a7f6bc8c | 8336 | && t == TYPE_MAIN_DECL (TREE_TYPE (t))) |
fc6a28d7 | 8337 | { |
a7f6bc8c JM |
8338 | /* If this is the canonical decl, we don't have to |
8339 | mess with instantiations, and often we can't (for | |
8340 | typename, template type parms and such). Note that | |
8341 | TYPE_NAME is not correct for the above test if | |
8342 | we've copied the type for a typedef. */ | |
fc6a28d7 | 8343 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
c343d5a7 PC |
8344 | if (type == error_mark_node) |
8345 | return error_mark_node; | |
a7f6bc8c JM |
8346 | r = TYPE_NAME (type); |
8347 | break; | |
fc6a28d7 | 8348 | } |
c8094d83 | 8349 | |
f604fc3b MM |
8350 | /* Check to see if we already have the specialization we |
8351 | need. */ | |
8352 | spec = NULL_TREE; | |
8353 | if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)) | |
8354 | { | |
8355 | /* T is a static data member or namespace-scope entity. | |
8356 | We have to substitute into namespace-scope variables | |
8357 | (even though such entities are never templates) because | |
8358 | of cases like: | |
8359 | ||
8360 | template <class T> void f() { extern T t; } | |
8361 | ||
8362 | where the entity referenced is not known until | |
8363 | instantiation time. */ | |
8364 | local_p = false; | |
8365 | ctx = DECL_CONTEXT (t); | |
8366 | if (DECL_CLASS_SCOPE_P (t)) | |
8367 | { | |
8368 | ctx = tsubst_aggr_type (ctx, args, | |
8369 | complain, | |
8370 | in_decl, /*entering_scope=*/1); | |
8371 | /* If CTX is unchanged, then T is in fact the | |
8372 | specialization we want. That situation occurs when | |
8373 | referencing a static data member within in its own | |
8374 | class. We can use pointer equality, rather than | |
8375 | same_type_p, because DECL_CONTEXT is always | |
8376 | canonical. */ | |
8377 | if (ctx == DECL_CONTEXT (t)) | |
8378 | spec = t; | |
8379 | } | |
5566b478 | 8380 | |
f604fc3b MM |
8381 | if (!spec) |
8382 | { | |
8383 | tmpl = DECL_TI_TEMPLATE (t); | |
8384 | gen_tmpl = most_general_template (tmpl); | |
8385 | argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); | |
8386 | spec = (retrieve_specialization | |
8387 | (gen_tmpl, argvec, | |
8388 | /*class_specializations_p=*/false)); | |
8389 | } | |
8390 | } | |
9188c363 | 8391 | else |
6dfbb909 | 8392 | { |
f604fc3b MM |
8393 | /* A local variable. */ |
8394 | local_p = true; | |
6dfbb909 MM |
8395 | /* Subsequent calls to pushdecl will fill this in. */ |
8396 | ctx = NULL_TREE; | |
f604fc3b | 8397 | spec = retrieve_local_specialization (t); |
6dfbb909 | 8398 | } |
f604fc3b MM |
8399 | /* If we already have the specialization we need, there is |
8400 | nothing more to do. */ | |
36a117a5 | 8401 | if (spec) |
ae58fa02 MM |
8402 | { |
8403 | r = spec; | |
8404 | break; | |
8405 | } | |
5566b478 | 8406 | |
f604fc3b | 8407 | /* Create a new node for the specialization we need. */ |
0acf7199 | 8408 | r = copy_decl (t); |
56d0c6e3 JM |
8409 | if (type == NULL_TREE) |
8410 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
edebf865 | 8411 | if (TREE_CODE (r) == VAR_DECL) |
39703eb9 | 8412 | { |
8b0a8500 MM |
8413 | /* Even if the original location is out of scope, the |
8414 | newly substituted one is not. */ | |
8415 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
8416 | DECL_INITIALIZED_P (r) = 0; | |
8417 | DECL_TEMPLATE_INSTANTIATED (r) = 0; | |
fc6a28d7 MM |
8418 | if (type == error_mark_node) |
8419 | return error_mark_node; | |
2c05d05e MM |
8420 | if (TREE_CODE (type) == FUNCTION_TYPE) |
8421 | { | |
8422 | /* It may seem that this case cannot occur, since: | |
8423 | ||
8424 | typedef void f(); | |
8425 | void g() { f x; } | |
8426 | ||
8427 | declares a function, not a variable. However: | |
8428 | ||
8429 | typedef void f(); | |
8430 | template <typename T> void g() { T t; } | |
8431 | template void g<f>(); | |
8432 | ||
8433 | is an attempt to declare a variable with function | |
8434 | type. */ | |
8435 | error ("variable %qD has function type", | |
8436 | /* R is not yet sufficiently initialized, so we | |
8437 | just use its name. */ | |
8438 | DECL_NAME (r)); | |
8439 | return error_mark_node; | |
8440 | } | |
39703eb9 MM |
8441 | type = complete_type (type); |
8442 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) | |
8443 | = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t); | |
62e19030 | 8444 | type = check_var_type (DECL_NAME (r), type); |
56b4ea3d RH |
8445 | |
8446 | if (DECL_HAS_VALUE_EXPR_P (t)) | |
8447 | { | |
8448 | tree ve = DECL_VALUE_EXPR (t); | |
015c2c66 MM |
8449 | ve = tsubst_expr (ve, args, complain, in_decl, |
8450 | /*constant_expression_p=*/false); | |
3db45ab5 | 8451 | SET_DECL_VALUE_EXPR (r, ve); |
56b4ea3d | 8452 | } |
39703eb9 | 8453 | } |
a3d87771 MM |
8454 | else if (DECL_SELF_REFERENCE_P (t)) |
8455 | SET_DECL_SELF_REFERENCE_P (r); | |
01f4137f | 8456 | TREE_TYPE (r) = type; |
9804209d | 8457 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
5566b478 | 8458 | DECL_CONTEXT (r) = ctx; |
92643fea MM |
8459 | /* Clear out the mangled name and RTL for the instantiation. */ |
8460 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
820cc88f DB |
8461 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
8462 | SET_DECL_RTL (r, NULL_RTX); | |
8b0a8500 MM |
8463 | /* The initializer must not be expanded until it is required; |
8464 | see [temp.inst]. */ | |
d11ad92e | 8465 | DECL_INITIAL (r) = NULL_TREE; |
820cc88f DB |
8466 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
8467 | SET_DECL_RTL (r, NULL_RTX); | |
06ceef4e | 8468 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; |
b9e75696 JM |
8469 | if (TREE_CODE (r) == VAR_DECL) |
8470 | { | |
8471 | /* Possibly limit visibility based on template args. */ | |
8472 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
8473 | if (DECL_VISIBILITY_SPECIFIED (t)) |
8474 | { | |
8475 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
8476 | DECL_ATTRIBUTES (r) | |
8477 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
8478 | } | |
b9e75696 JM |
8479 | determine_visibility (r); |
8480 | } | |
5044ab0e JM |
8481 | /* Preserve a typedef that names a type. */ |
8482 | else if (TREE_CODE (r) == TYPE_DECL | |
8483 | && DECL_ORIGINAL_TYPE (t) | |
8484 | && type != error_mark_node) | |
8485 | { | |
8486 | DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t), | |
8487 | args, complain, in_decl); | |
8488 | TREE_TYPE (r) = type = build_variant_type_copy (type); | |
8489 | TYPE_NAME (type) = r; | |
8490 | } | |
5566b478 | 8491 | |
6dfbb909 MM |
8492 | if (!local_p) |
8493 | { | |
8494 | /* A static data member declaration is always marked | |
8495 | external when it is declared in-class, even if an | |
8496 | initializer is present. We mimic the non-template | |
8497 | processing here. */ | |
8498 | DECL_EXTERNAL (r) = 1; | |
fa8d6e85 | 8499 | |
d63d5d0c | 8500 | register_specialization (r, gen_tmpl, argvec, false); |
6dfbb909 MM |
8501 | DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); |
8502 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
8503 | } | |
9188c363 | 8504 | else |
6dfbb909 | 8505 | register_local_specialization (r, t); |
5566b478 | 8506 | |
5566b478 | 8507 | TREE_CHAIN (r) = NULL_TREE; |
4f4141ff | 8508 | |
5044ab0e JM |
8509 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), |
8510 | (int) ATTR_FLAG_TYPE_IN_PLACE, | |
4f4141ff | 8511 | args, complain, in_decl); |
edebf865 | 8512 | layout_decl (r, 0); |
5566b478 | 8513 | } |
ae58fa02 | 8514 | break; |
5566b478 | 8515 | |
ae58fa02 | 8516 | default: |
315fb5db | 8517 | gcc_unreachable (); |
c8094d83 | 8518 | } |
ae58fa02 MM |
8519 | |
8520 | /* Restore the file and line information. */ | |
82a98427 | 8521 | input_location = saved_loc; |
ae58fa02 MM |
8522 | |
8523 | return r; | |
8524 | } | |
8525 | ||
34cd5ae7 | 8526 | /* Substitute into the ARG_TYPES of a function type. */ |
cabc336a | 8527 | |
e9659ab0 | 8528 | static tree |
c8094d83 | 8529 | tsubst_arg_types (tree arg_types, |
0cbd7506 MS |
8530 | tree args, |
8531 | tsubst_flags_t complain, | |
8532 | tree in_decl) | |
cabc336a MM |
8533 | { |
8534 | tree remaining_arg_types; | |
5d80a306 DG |
8535 | tree type = NULL_TREE; |
8536 | int i = 1; | |
8537 | tree expanded_args = NULL_TREE; | |
5e97d404 | 8538 | tree default_arg; |
cabc336a MM |
8539 | |
8540 | if (!arg_types || arg_types == void_list_node) | |
8541 | return arg_types; | |
c8094d83 | 8542 | |
cabc336a | 8543 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), |
4393e105 MM |
8544 | args, complain, in_decl); |
8545 | if (remaining_arg_types == error_mark_node) | |
8546 | return error_mark_node; | |
8547 | ||
5d80a306 | 8548 | if (PACK_EXPANSION_P (TREE_VALUE (arg_types))) |
4b2811e9 | 8549 | { |
5d80a306 DG |
8550 | /* For a pack expansion, perform substitution on the |
8551 | entire expression. Later on, we'll handle the arguments | |
8552 | one-by-one. */ | |
8553 | expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types), | |
8554 | args, complain, in_decl); | |
cabc336a | 8555 | |
5d80a306 DG |
8556 | if (TREE_CODE (expanded_args) == TREE_VEC) |
8557 | /* So that we'll spin through the parameters, one by one. */ | |
8558 | i = TREE_VEC_LENGTH (expanded_args); | |
8559 | else | |
8560 | { | |
8561 | /* We only partially substituted into the parameter | |
8562 | pack. Our type is TYPE_PACK_EXPANSION. */ | |
8563 | type = expanded_args; | |
8564 | expanded_args = NULL_TREE; | |
8565 | } | |
8566 | } | |
cabc336a | 8567 | |
5d80a306 DG |
8568 | while (i > 0) { |
8569 | --i; | |
8570 | ||
8571 | if (expanded_args) | |
8572 | type = TREE_VEC_ELT (expanded_args, i); | |
8573 | else if (!type) | |
8574 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
c8094d83 | 8575 | |
5d80a306 DG |
8576 | if (type == error_mark_node) |
8577 | return error_mark_node; | |
8578 | if (VOID_TYPE_P (type)) | |
8579 | { | |
8580 | if (complain & tf_error) | |
8581 | { | |
8582 | error ("invalid parameter type %qT", type); | |
8583 | if (in_decl) | |
8584 | error ("in declaration %q+D", in_decl); | |
8585 | } | |
8586 | return error_mark_node; | |
5e97d404 | 8587 | } |
5d80a306 DG |
8588 | |
8589 | /* Do array-to-pointer, function-to-pointer conversion, and ignore | |
8590 | top-level qualifiers as required. */ | |
8591 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
c8094d83 | 8592 | |
5d80a306 DG |
8593 | /* We do not substitute into default arguments here. The standard |
8594 | mandates that they be instantiated only when needed, which is | |
8595 | done in build_over_call. */ | |
8596 | default_arg = TREE_PURPOSE (arg_types); | |
8597 | ||
8598 | if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG) | |
8599 | { | |
8600 | /* We've instantiated a template before its default arguments | |
8601 | have been parsed. This can happen for a nested template | |
8602 | class, and is not an error unless we require the default | |
8603 | argument in a call of this function. */ | |
8604 | remaining_arg_types = | |
8605 | tree_cons (default_arg, type, remaining_arg_types); | |
8606 | VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), | |
8607 | remaining_arg_types); | |
8608 | } | |
8609 | else | |
8610 | remaining_arg_types = | |
8611 | hash_tree_cons (default_arg, type, remaining_arg_types); | |
8612 | } | |
8613 | ||
8614 | return remaining_arg_types; | |
cabc336a MM |
8615 | } |
8616 | ||
4393e105 MM |
8617 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
8618 | *not* handle the exception-specification for FNTYPE, because the | |
8619 | initial substitution of explicitly provided template parameters | |
8620 | during argument deduction forbids substitution into the | |
8621 | exception-specification: | |
8622 | ||
8623 | [temp.deduct] | |
8624 | ||
8625 | All references in the function type of the function template to the | |
8626 | corresponding template parameters are replaced by the specified tem- | |
8627 | plate argument values. If a substitution in a template parameter or | |
8628 | in the function type of the function template results in an invalid | |
8629 | type, type deduction fails. [Note: The equivalent substitution in | |
8630 | exception specifications is done only when the function is instanti- | |
8631 | ated, at which point a program is ill-formed if the substitution | |
8632 | results in an invalid type.] */ | |
8633 | ||
8634 | static tree | |
c8094d83 | 8635 | tsubst_function_type (tree t, |
0cbd7506 MS |
8636 | tree args, |
8637 | tsubst_flags_t complain, | |
8638 | tree in_decl) | |
4393e105 MM |
8639 | { |
8640 | tree return_type; | |
8641 | tree arg_types; | |
8642 | tree fntype; | |
8643 | ||
8dd3f57a | 8644 | /* The TYPE_CONTEXT is not used for function/method types. */ |
50bc768d | 8645 | gcc_assert (TYPE_CONTEXT (t) == NULL_TREE); |
4393e105 | 8646 | |
46c895ac | 8647 | /* Substitute the return type. */ |
4393e105 MM |
8648 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
8649 | if (return_type == error_mark_node) | |
8650 | return error_mark_node; | |
6e2993bf MM |
8651 | /* The standard does not presently indicate that creation of a |
8652 | function type with an invalid return type is a deduction failure. | |
270af55d | 8653 | However, that is clearly analogous to creating an array of "void" |
c8094d83 | 8654 | or a reference to a reference. This is core issue #486. */ |
6e2993bf MM |
8655 | if (TREE_CODE (return_type) == ARRAY_TYPE |
8656 | || TREE_CODE (return_type) == FUNCTION_TYPE) | |
8657 | { | |
8658 | if (complain & tf_error) | |
8659 | { | |
8660 | if (TREE_CODE (return_type) == ARRAY_TYPE) | |
8661 | error ("function returning an array"); | |
8662 | else | |
8663 | error ("function returning a function"); | |
8664 | } | |
8665 | return error_mark_node; | |
8666 | } | |
4393e105 | 8667 | |
34cd5ae7 | 8668 | /* Substitute the argument types. */ |
4393e105 | 8669 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, |
c8094d83 | 8670 | complain, in_decl); |
4393e105 MM |
8671 | if (arg_types == error_mark_node) |
8672 | return error_mark_node; | |
c8094d83 | 8673 | |
1891dec4 DM |
8674 | if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED |
8675 | && in_decl != NULL_TREE | |
8676 | && !TREE_NO_WARNING (in_decl) | |
8677 | && (SCALAR_TYPE_P (return_type) || VOID_TYPE_P (return_type))) | |
5db2e9ca | 8678 | warning (OPT_Wignored_qualifiers, |
1891dec4 DM |
8679 | "type qualifiers ignored on function return type"); |
8680 | ||
4393e105 MM |
8681 | /* Construct a new type node and return it. */ |
8682 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
8683 | fntype = build_function_type (return_type, arg_types); | |
8684 | else | |
8685 | { | |
8686 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
8687 | if (! IS_AGGR_TYPE (r)) | |
8688 | { | |
8689 | /* [temp.deduct] | |
c8094d83 | 8690 | |
4393e105 MM |
8691 | Type deduction may fail for any of the following |
8692 | reasons: | |
c8094d83 | 8693 | |
4393e105 MM |
8694 | -- Attempting to create "pointer to member of T" when T |
8695 | is not a class type. */ | |
c2ea3a40 | 8696 | if (complain & tf_error) |
0f51ccfc | 8697 | error ("creating pointer to member function of non-class type %qT", |
4393e105 MM |
8698 | r); |
8699 | return error_mark_node; | |
8700 | } | |
c8094d83 MS |
8701 | |
8702 | fntype = build_method_type_directly (r, return_type, | |
43dc123f | 8703 | TREE_CHAIN (arg_types)); |
4393e105 | 8704 | } |
c2ea3a40 | 8705 | fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain); |
e9525111 | 8706 | fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
c8094d83 MS |
8707 | |
8708 | return fntype; | |
4393e105 MM |
8709 | } |
8710 | ||
c7222c02 MM |
8711 | /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template |
8712 | ARGS into that specification, and return the substituted | |
8713 | specification. If there is no specification, return NULL_TREE. */ | |
8714 | ||
8715 | static tree | |
c8094d83 MS |
8716 | tsubst_exception_specification (tree fntype, |
8717 | tree args, | |
c7222c02 MM |
8718 | tsubst_flags_t complain, |
8719 | tree in_decl) | |
8720 | { | |
8721 | tree specs; | |
8722 | tree new_specs; | |
8723 | ||
8724 | specs = TYPE_RAISES_EXCEPTIONS (fntype); | |
8725 | new_specs = NULL_TREE; | |
8726 | if (specs) | |
8727 | { | |
8728 | if (! TREE_VALUE (specs)) | |
8729 | new_specs = specs; | |
8730 | else | |
8731 | while (specs) | |
8732 | { | |
8733 | tree spec; | |
5d80a306 DG |
8734 | int i, len = 1; |
8735 | tree expanded_specs = NULL_TREE; | |
8736 | ||
8737 | if (PACK_EXPANSION_P (TREE_VALUE (specs))) | |
8738 | { | |
8739 | /* Expand the pack expansion type. */ | |
8740 | expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs), | |
8741 | args, complain, | |
8742 | in_decl); | |
2f93f02c DG |
8743 | |
8744 | if (expanded_specs == error_mark_node) | |
8745 | return error_mark_node; | |
8746 | else if (TREE_CODE (expanded_specs) == TREE_VEC) | |
8747 | len = TREE_VEC_LENGTH (expanded_specs); | |
8748 | else | |
8749 | { | |
8750 | /* We're substituting into a member template, so | |
8751 | we got a TYPE_PACK_EXPANSION back. Add that | |
8752 | expansion and move on. */ | |
8753 | gcc_assert (TREE_CODE (expanded_specs) | |
8754 | == TYPE_PACK_EXPANSION); | |
8755 | new_specs = add_exception_specifier (new_specs, | |
8756 | expanded_specs, | |
8757 | complain); | |
8758 | specs = TREE_CHAIN (specs); | |
8759 | continue; | |
8760 | } | |
5d80a306 DG |
8761 | } |
8762 | ||
8763 | for (i = 0; i < len; ++i) | |
8764 | { | |
8765 | if (expanded_specs) | |
8766 | spec = TREE_VEC_ELT (expanded_specs, i); | |
8767 | else | |
8768 | spec = tsubst (TREE_VALUE (specs), args, complain, in_decl); | |
8769 | if (spec == error_mark_node) | |
8770 | return spec; | |
8771 | new_specs = add_exception_specifier (new_specs, spec, | |
8772 | complain); | |
8773 | } | |
8774 | ||
8775 | specs = TREE_CHAIN (specs); | |
c7222c02 MM |
8776 | } |
8777 | } | |
8778 | return new_specs; | |
8779 | } | |
8780 | ||
4393e105 MM |
8781 | /* Take the tree structure T and replace template parameters used |
8782 | therein with the argument vector ARGS. IN_DECL is an associated | |
8783 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
c2ea3a40 NS |
8784 | Issue error and warning messages under control of COMPLAIN. Note |
8785 | that we must be relatively non-tolerant of extensions here, in | |
8786 | order to preserve conformance; if we allow substitutions that | |
8787 | should not be allowed, we may allow argument deductions that should | |
8788 | not succeed, and therefore report ambiguous overload situations | |
8789 | where there are none. In theory, we could allow the substitution, | |
8790 | but indicate that it should have failed, and allow our caller to | |
8791 | make sure that the right thing happens, but we don't try to do this | |
8792 | yet. | |
4393e105 MM |
8793 | |
8794 | This function is used for dealing with types, decls and the like; | |
8795 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 | 8796 | |
14d22dd6 | 8797 | static tree |
3a978d72 | 8798 | tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
ae58fa02 | 8799 | { |
0ecfe0b4 | 8800 | tree type, r; |
ae58fa02 MM |
8801 | |
8802 | if (t == NULL_TREE || t == error_mark_node | |
8803 | || t == integer_type_node | |
8804 | || t == void_type_node | |
8805 | || t == char_type_node | |
539599c1 | 8806 | || t == unknown_type_node |
ae58fa02 MM |
8807 | || TREE_CODE (t) == NAMESPACE_DECL) |
8808 | return t; | |
8809 | ||
fc6a28d7 MM |
8810 | if (DECL_P (t)) |
8811 | return tsubst_decl (t, args, complain); | |
8812 | ||
ae58fa02 MM |
8813 | if (TREE_CODE (t) == IDENTIFIER_NODE) |
8814 | type = IDENTIFIER_TYPE_VALUE (t); | |
8815 | else | |
8816 | type = TREE_TYPE (t); | |
399dedb9 | 8817 | |
50bc768d | 8818 | gcc_assert (type != unknown_type_node); |
ae58fa02 | 8819 | |
56d0c6e3 | 8820 | /* Reuse typedefs. We need to do this to handle dependent attributes, |
a7f6bc8c | 8821 | such as attribute aligned. */ |
5044ab0e JM |
8822 | if (TYPE_P (t) |
8823 | && TYPE_NAME (t) | |
a7f6bc8c | 8824 | && TYPE_NAME (t) != TYPE_MAIN_DECL (t)) |
5044ab0e JM |
8825 | { |
8826 | tree decl = TYPE_NAME (t); | |
56d0c6e3 | 8827 | |
a7f6bc8c JM |
8828 | if (DECL_CLASS_SCOPE_P (decl) |
8829 | && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))) | |
56d0c6e3 JM |
8830 | { |
8831 | tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
8832 | tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl); | |
8833 | r = retrieve_specialization (tmpl, gen_args, false); | |
8834 | } | |
29b0d1fd JM |
8835 | else if (DECL_FUNCTION_SCOPE_P (decl) |
8836 | && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))) | |
56d0c6e3 JM |
8837 | r = retrieve_local_specialization (decl); |
8838 | else | |
29b0d1fd JM |
8839 | /* The typedef is from a non-template context. */ |
8840 | return t; | |
8841 | ||
56d0c6e3 JM |
8842 | if (r) |
8843 | { | |
8844 | r = TREE_TYPE (r); | |
8845 | r = cp_build_qualified_type_real | |
8846 | (r, cp_type_quals (t) | cp_type_quals (r), | |
8847 | complain | tf_ignore_bad_quals); | |
8848 | return r; | |
8849 | } | |
8850 | /* Else we must be instantiating the typedef, so fall through. */ | |
5044ab0e JM |
8851 | } |
8852 | ||
fc6a28d7 | 8853 | if (type |
ae58fa02 | 8854 | && TREE_CODE (t) != TYPENAME_TYPE |
4393e105 MM |
8855 | && TREE_CODE (t) != IDENTIFIER_NODE |
8856 | && TREE_CODE (t) != FUNCTION_TYPE | |
8857 | && TREE_CODE (t) != METHOD_TYPE) | |
8858 | type = tsubst (type, args, complain, in_decl); | |
8859 | if (type == error_mark_node) | |
8860 | return error_mark_node; | |
ae58fa02 | 8861 | |
ae58fa02 MM |
8862 | switch (TREE_CODE (t)) |
8863 | { | |
8864 | case RECORD_TYPE: | |
8865 | case UNION_TYPE: | |
8866 | case ENUMERAL_TYPE: | |
4393e105 MM |
8867 | return tsubst_aggr_type (t, args, complain, in_decl, |
8868 | /*entering_scope=*/0); | |
ae58fa02 MM |
8869 | |
8870 | case ERROR_MARK: | |
8871 | case IDENTIFIER_NODE: | |
ae58fa02 MM |
8872 | case VOID_TYPE: |
8873 | case REAL_TYPE: | |
8874 | case COMPLEX_TYPE: | |
c00996a3 | 8875 | case VECTOR_TYPE: |
ae58fa02 MM |
8876 | case BOOLEAN_TYPE: |
8877 | case INTEGER_CST: | |
8878 | case REAL_CST: | |
8879 | case STRING_CST: | |
8880 | return t; | |
8881 | ||
8882 | case INTEGER_TYPE: | |
8883 | if (t == integer_type_node) | |
8884 | return t; | |
8885 | ||
8886 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
8887 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
8888 | return t; | |
d2e5ee5c | 8889 | |
5566b478 | 8890 | { |
ddce3528 | 8891 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 8892 | |
6ee3ffe8 MM |
8893 | max = tsubst_expr (omax, args, complain, in_decl, |
8894 | /*integral_constant_expression_p=*/false); | |
4ef69b83 | 8895 | max = fold_decl_constant_value (max); |
8dd3f57a | 8896 | |
eff3a276 MM |
8897 | if (TREE_CODE (max) != INTEGER_CST |
8898 | && TREE_CODE (max) != TEMPLATE_PARM_INDEX | |
8899 | && !at_function_scope_p ()) | |
8900 | { | |
8901 | if (complain & tf_error) | |
8902 | error ("array bound is not an integer constant"); | |
8903 | return error_mark_node; | |
8904 | } | |
8905 | ||
95cd6f6f | 8906 | /* [temp.deduct] |
4393e105 | 8907 | |
95cd6f6f JC |
8908 | Type deduction may fail for any of the following |
8909 | reasons: | |
4393e105 | 8910 | |
95cd6f6f JC |
8911 | Attempting to create an array with a size that is |
8912 | zero or negative. */ | |
8913 | if (integer_zerop (max) && !(complain & tf_error)) | |
8914 | /* We must fail if performing argument deduction (as | |
8915 | indicated by the state of complain), so that | |
8916 | another substitution can be found. */ | |
8917 | return error_mark_node; | |
95cd6f6f JC |
8918 | else if (TREE_CODE (max) == INTEGER_CST |
8919 | && INT_CST_LT (max, integer_zero_node)) | |
8920 | { | |
c2ea3a40 | 8921 | if (complain & tf_error) |
95cd6f6f | 8922 | error ("creating array with negative size (%qE)", max); |
4393e105 MM |
8923 | |
8924 | return error_mark_node; | |
0ecfe0b4 JM |
8925 | } |
8926 | ||
c95cd22e | 8927 | return compute_array_index_type (NULL_TREE, max); |
ae58fa02 MM |
8928 | } |
8929 | ||
8930 | case TEMPLATE_TYPE_PARM: | |
8931 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 8932 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
ae58fa02 MM |
8933 | case TEMPLATE_PARM_INDEX: |
8934 | { | |
8935 | int idx; | |
8936 | int level; | |
8937 | int levels; | |
315fb5db | 8938 | tree arg = NULL_TREE; |
0ecfe0b4 JM |
8939 | |
8940 | r = NULL_TREE; | |
ae58fa02 | 8941 | |
315fb5db | 8942 | gcc_assert (TREE_VEC_LENGTH (args) > 0); |
f4205442 | 8943 | template_parm_level_and_index (t, &level, &idx); |
ae58fa02 | 8944 | |
315fb5db NS |
8945 | levels = TMPL_ARGS_DEPTH (args); |
8946 | if (level <= levels) | |
5d80a306 DG |
8947 | { |
8948 | arg = TMPL_ARG (args, level, idx); | |
8949 | ||
8950 | if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT) | |
8951 | /* See through ARGUMENT_PACK_SELECT arguments. */ | |
8952 | arg = ARGUMENT_PACK_SELECT_ARG (arg); | |
8953 | } | |
ae58fa02 | 8954 | |
315fb5db NS |
8955 | if (arg == error_mark_node) |
8956 | return error_mark_node; | |
8957 | else if (arg != NULL_TREE) | |
8958 | { | |
5d80a306 DG |
8959 | if (ARGUMENT_PACK_P (arg)) |
8960 | /* If ARG is an argument pack, we don't actually want to | |
8961 | perform a substitution here, because substitutions | |
8962 | for argument packs are only done | |
8963 | element-by-element. We can get to this point when | |
8964 | substituting the type of a non-type template | |
8965 | parameter pack, when that type actually contains | |
8966 | template parameter packs from an outer template, e.g., | |
8967 | ||
8968 | template<typename... Types> struct A { | |
8969 | template<Types... Values> struct B { }; | |
8970 | }; */ | |
8971 | return t; | |
8972 | ||
315fb5db | 8973 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) |
ae58fa02 | 8974 | { |
9180c238 | 8975 | int quals; |
315fb5db | 8976 | gcc_assert (TYPE_P (arg)); |
9180c238 JM |
8977 | |
8978 | /* cv-quals from the template are discarded when | |
8979 | substituting in a function or reference type. */ | |
8980 | if (TREE_CODE (arg) == FUNCTION_TYPE | |
8981 | || TREE_CODE (arg) == METHOD_TYPE | |
8982 | || TREE_CODE (arg) == REFERENCE_TYPE) | |
8983 | quals = cp_type_quals (arg); | |
8984 | else | |
8985 | quals = cp_type_quals (arg) | cp_type_quals (t); | |
8986 | ||
315fb5db | 8987 | return cp_build_qualified_type_real |
9180c238 | 8988 | (arg, quals, complain | tf_ignore_bad_quals); |
315fb5db NS |
8989 | } |
8990 | else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
8991 | { | |
8992 | /* We are processing a type constructed from a | |
8993 | template template parameter. */ | |
8994 | tree argvec = tsubst (TYPE_TI_ARGS (t), | |
8995 | args, complain, in_decl); | |
8996 | if (argvec == error_mark_node) | |
8997 | return error_mark_node; | |
c8094d83 | 8998 | |
315fb5db NS |
8999 | /* We can get a TEMPLATE_TEMPLATE_PARM here when we |
9000 | are resolving nested-types in the signature of a | |
9001 | member function templates. Otherwise ARG is a | |
9002 | TEMPLATE_DECL and is the real template to be | |
9003 | instantiated. */ | |
9004 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
9005 | arg = TYPE_NAME (arg); | |
c8094d83 MS |
9006 | |
9007 | r = lookup_template_class (arg, | |
9008 | argvec, in_decl, | |
315fb5db NS |
9009 | DECL_CONTEXT (arg), |
9010 | /*entering_scope=*/0, | |
9011 | complain); | |
9012 | return cp_build_qualified_type_real | |
9013 | (r, TYPE_QUALS (t), complain); | |
ae58fa02 | 9014 | } |
315fb5db NS |
9015 | else |
9016 | /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ | |
9017 | return arg; | |
ae58fa02 MM |
9018 | } |
9019 | ||
9020 | if (level == 1) | |
9021 | /* This can happen during the attempted tsubst'ing in | |
9022 | unify. This means that we don't yet have any information | |
9023 | about the template parameter in question. */ | |
9024 | return t; | |
9025 | ||
9026 | /* If we get here, we must have been looking at a parm for a | |
9027 | more deeply nested template. Make a new version of this | |
9028 | template parameter, but with a lower level. */ | |
9029 | switch (TREE_CODE (t)) | |
9030 | { | |
9031 | case TEMPLATE_TYPE_PARM: | |
9032 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 9033 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
89d684bb | 9034 | if (cp_type_quals (t)) |
ae58fa02 | 9035 | { |
9ccf6541 | 9036 | r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); |
0cbd7506 MS |
9037 | r = cp_build_qualified_type_real |
9038 | (r, cp_type_quals (t), | |
4f2b0fb2 NS |
9039 | complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
9040 | ? tf_ignore_bad_quals : 0)); | |
9ccf6541 MM |
9041 | } |
9042 | else | |
9043 | { | |
11e74ea6 | 9044 | r = copy_type (t); |
9ccf6541 MM |
9045 | TEMPLATE_TYPE_PARM_INDEX (r) |
9046 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
ef3b7b17 | 9047 | r, levels, args, complain); |
9ccf6541 MM |
9048 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); |
9049 | TYPE_MAIN_VARIANT (r) = r; | |
9050 | TYPE_POINTER_TO (r) = NULL_TREE; | |
9051 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
9052 | ||
06d40de8 DG |
9053 | if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM) |
9054 | /* We have reduced the level of the template | |
9055 | template parameter, but not the levels of its | |
9056 | template parameters, so canonical_type_parameter | |
9057 | will not be able to find the canonical template | |
9058 | template parameter for this level. Thus, we | |
9059 | require structural equality checking to compare | |
9060 | TEMPLATE_TEMPLATE_PARMs. */ | |
9061 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
3d761c46 DG |
9062 | else if (TYPE_STRUCTURAL_EQUALITY_P (t)) |
9063 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
06d40de8 DG |
9064 | else |
9065 | TYPE_CANONICAL (r) = canonical_type_parameter (r); | |
9066 | ||
a1281f45 | 9067 | if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
9ccf6541 MM |
9068 | { |
9069 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, | |
c8094d83 | 9070 | complain, in_decl); |
9ccf6541 MM |
9071 | if (argvec == error_mark_node) |
9072 | return error_mark_node; | |
4393e105 | 9073 | |
9ccf6541 MM |
9074 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
9075 | = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE); | |
9076 | } | |
ae58fa02 MM |
9077 | } |
9078 | break; | |
9079 | ||
9080 | case TEMPLATE_PARM_INDEX: | |
ef3b7b17 | 9081 | r = reduce_template_parm_level (t, type, levels, args, complain); |
ae58fa02 | 9082 | break; |
c8094d83 | 9083 | |
ae58fa02 | 9084 | default: |
315fb5db | 9085 | gcc_unreachable (); |
ae58fa02 MM |
9086 | } |
9087 | ||
5566b478 | 9088 | return r; |
ae58fa02 | 9089 | } |
5566b478 | 9090 | |
8d08fdba MS |
9091 | case TREE_LIST: |
9092 | { | |
058b15c1 | 9093 | tree purpose, value, chain; |
8d08fdba MS |
9094 | |
9095 | if (t == void_list_node) | |
9096 | return t; | |
9097 | ||
8d08fdba MS |
9098 | purpose = TREE_PURPOSE (t); |
9099 | if (purpose) | |
4393e105 MM |
9100 | { |
9101 | purpose = tsubst (purpose, args, complain, in_decl); | |
9102 | if (purpose == error_mark_node) | |
9103 | return error_mark_node; | |
9104 | } | |
8d08fdba MS |
9105 | value = TREE_VALUE (t); |
9106 | if (value) | |
4393e105 MM |
9107 | { |
9108 | value = tsubst (value, args, complain, in_decl); | |
9109 | if (value == error_mark_node) | |
9110 | return error_mark_node; | |
9111 | } | |
8d08fdba MS |
9112 | chain = TREE_CHAIN (t); |
9113 | if (chain && chain != void_type_node) | |
4393e105 MM |
9114 | { |
9115 | chain = tsubst (chain, args, complain, in_decl); | |
9116 | if (chain == error_mark_node) | |
9117 | return error_mark_node; | |
9118 | } | |
8d08fdba MS |
9119 | if (purpose == TREE_PURPOSE (t) |
9120 | && value == TREE_VALUE (t) | |
9121 | && chain == TREE_CHAIN (t)) | |
9122 | return t; | |
058b15c1 | 9123 | return hash_tree_cons (purpose, value, chain); |
8d08fdba | 9124 | } |
c8094d83 | 9125 | |
95b4aca6 | 9126 | case TREE_BINFO: |
bd7eccc4 | 9127 | /* We should never be tsubsting a binfo. */ |
315fb5db | 9128 | gcc_unreachable (); |
85b71cf2 | 9129 | |
95b4aca6 NS |
9130 | case TREE_VEC: |
9131 | /* A vector of template arguments. */ | |
50bc768d | 9132 | gcc_assert (!type); |
a91db711 | 9133 | return tsubst_template_args (t, args, complain, in_decl); |
8d08fdba | 9134 | |
8d08fdba MS |
9135 | case POINTER_TYPE: |
9136 | case REFERENCE_TYPE: | |
9137 | { | |
8d08fdba | 9138 | enum tree_code code; |
79a7c7fa | 9139 | |
46c895ac | 9140 | if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) |
8d08fdba MS |
9141 | return t; |
9142 | ||
9143 | code = TREE_CODE (t); | |
4393e105 MM |
9144 | |
9145 | ||
9146 | /* [temp.deduct] | |
c8094d83 | 9147 | |
4393e105 | 9148 | Type deduction may fail for any of the following |
c8094d83 | 9149 | reasons: |
4393e105 MM |
9150 | |
9151 | -- Attempting to create a pointer to reference type. | |
9152 | -- Attempting to create a reference to a reference type or | |
8af2fec4 RY |
9153 | a reference to void. |
9154 | ||
9155 | Core issue 106 says that creating a reference to a reference | |
9156 | during instantiation is no longer a cause for failure. We | |
9157 | only enforce this check in strict C++98 mode. */ | |
9158 | if ((TREE_CODE (type) == REFERENCE_TYPE | |
c1ae8be5 | 9159 | && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE)) |
0ecfe0b4 | 9160 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) |
79a7c7fa | 9161 | { |
82a98427 | 9162 | static location_t last_loc; |
79a7c7fa JM |
9163 | |
9164 | /* We keep track of the last time we issued this error | |
9165 | message to avoid spewing a ton of messages during a | |
9166 | single bad template instantiation. */ | |
c2ea3a40 | 9167 | if (complain & tf_error |
93409b8c | 9168 | #ifdef USE_MAPPED_LOCATION |
9b60dfd7 | 9169 | && last_loc != input_location |
93409b8c | 9170 | #else |
82a98427 | 9171 | && (last_loc.line != input_line |
9b60dfd7 | 9172 | || last_loc.file != input_filename) |
93409b8c | 9173 | #endif |
9b60dfd7 | 9174 | ) |
79a7c7fa | 9175 | { |
0ecfe0b4 | 9176 | if (TREE_CODE (type) == VOID_TYPE) |
33bd39a2 | 9177 | error ("forming reference to void"); |
0ecfe0b4 | 9178 | else |
0f51ccfc | 9179 | error ("forming %s to reference type %qT", |
0cbd7506 MS |
9180 | (code == POINTER_TYPE) ? "pointer" : "reference", |
9181 | type); | |
82a98427 | 9182 | last_loc = input_location; |
79a7c7fa JM |
9183 | } |
9184 | ||
4393e105 | 9185 | return error_mark_node; |
79a7c7fa JM |
9186 | } |
9187 | else if (code == POINTER_TYPE) | |
46c895ac NS |
9188 | { |
9189 | r = build_pointer_type (type); | |
9190 | if (TREE_CODE (type) == METHOD_TYPE) | |
9191 | r = build_ptrmemfunc_type (r); | |
9192 | } | |
8af2fec4 RY |
9193 | else if (TREE_CODE (type) == REFERENCE_TYPE) |
9194 | /* In C++0x, during template argument substitution, when there is an | |
9195 | attempt to create a reference to a reference type, reference | |
9196 | collapsing is applied as described in [14.3.1/4 temp.arg.type]: | |
9197 | ||
9198 | "If a template-argument for a template-parameter T names a type | |
9199 | that is a reference to a type A, an attempt to create the type | |
9200 | 'lvalue reference to cv T' creates the type 'lvalue reference to | |
9201 | A,' while an attempt to create the type type rvalue reference to | |
9202 | cv T' creates the type T" | |
9203 | */ | |
9204 | r = cp_build_reference_type | |
9205 | (TREE_TYPE (type), | |
9206 | TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type)); | |
8d08fdba | 9207 | else |
8af2fec4 | 9208 | r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t)); |
adecb3f4 | 9209 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
79a7c7fa | 9210 | |
a5f1c5f8 NS |
9211 | if (r != error_mark_node) |
9212 | /* Will this ever be needed for TYPE_..._TO values? */ | |
9213 | layout_type (r); | |
c8094d83 | 9214 | |
8d08fdba MS |
9215 | return r; |
9216 | } | |
a4443a08 | 9217 | case OFFSET_TYPE: |
0ecfe0b4 | 9218 | { |
4393e105 MM |
9219 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
9220 | if (r == error_mark_node || !IS_AGGR_TYPE (r)) | |
9221 | { | |
9222 | /* [temp.deduct] | |
9223 | ||
9224 | Type deduction may fail for any of the following | |
9225 | reasons: | |
c8094d83 | 9226 | |
4393e105 | 9227 | -- Attempting to create "pointer to member of T" when T |
0cbd7506 | 9228 | is not a class type. */ |
c2ea3a40 | 9229 | if (complain & tf_error) |
0f51ccfc | 9230 | error ("creating pointer to member of non-class type %qT", r); |
4393e105 MM |
9231 | return error_mark_node; |
9232 | } | |
46c895ac NS |
9233 | if (TREE_CODE (type) == REFERENCE_TYPE) |
9234 | { | |
4f09be91 | 9235 | if (complain & tf_error) |
0f51ccfc | 9236 | error ("creating pointer to member reference type %qT", type); |
cb6d4a9f VR |
9237 | return error_mark_node; |
9238 | } | |
9239 | if (TREE_CODE (type) == VOID_TYPE) | |
9240 | { | |
9241 | if (complain & tf_error) | |
9242 | error ("creating pointer to member of type void"); | |
46c895ac NS |
9243 | return error_mark_node; |
9244 | } | |
50bc768d | 9245 | gcc_assert (TREE_CODE (type) != METHOD_TYPE); |
46c895ac | 9246 | if (TREE_CODE (type) == FUNCTION_TYPE) |
a5ac359a | 9247 | { |
0cbd7506 MS |
9248 | /* The type of the implicit object parameter gets its |
9249 | cv-qualifiers from the FUNCTION_TYPE. */ | |
a5ac359a | 9250 | tree method_type; |
0cbd7506 MS |
9251 | tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r), |
9252 | cp_type_quals (type)); | |
9253 | tree memptr; | |
9254 | method_type = build_method_type_directly (this_type, | |
43dc123f MM |
9255 | TREE_TYPE (type), |
9256 | TYPE_ARG_TYPES (type)); | |
0cbd7506 MS |
9257 | memptr = build_ptrmemfunc_type (build_pointer_type (method_type)); |
9258 | return cp_build_qualified_type_real (memptr, cp_type_quals (t), | |
9259 | complain); | |
a5ac359a | 9260 | } |
46c895ac | 9261 | else |
b7a78333 MM |
9262 | return cp_build_qualified_type_real (build_ptrmem_type (r, type), |
9263 | TYPE_QUALS (t), | |
9264 | complain); | |
0ecfe0b4 | 9265 | } |
8d08fdba MS |
9266 | case FUNCTION_TYPE: |
9267 | case METHOD_TYPE: | |
9268 | { | |
c11b6f21 | 9269 | tree fntype; |
c7222c02 | 9270 | tree specs; |
4393e105 MM |
9271 | fntype = tsubst_function_type (t, args, complain, in_decl); |
9272 | if (fntype == error_mark_node) | |
9273 | return error_mark_node; | |
cabc336a | 9274 | |
34cd5ae7 | 9275 | /* Substitute the exception specification. */ |
c8094d83 | 9276 | specs = tsubst_exception_specification (t, args, complain, |
c7222c02 | 9277 | in_decl); |
9f6206d9 VR |
9278 | if (specs == error_mark_node) |
9279 | return error_mark_node; | |
c7222c02 MM |
9280 | if (specs) |
9281 | fntype = build_exception_variant (fntype, specs); | |
c11b6f21 | 9282 | return fntype; |
8d08fdba MS |
9283 | } |
9284 | case ARRAY_TYPE: | |
9285 | { | |
4393e105 MM |
9286 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
9287 | if (domain == error_mark_node) | |
9288 | return error_mark_node; | |
9289 | ||
9290 | /* As an optimization, we avoid regenerating the array type if | |
9291 | it will obviously be the same as T. */ | |
8d08fdba MS |
9292 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
9293 | return t; | |
0ecfe0b4 | 9294 | |
c8094d83 | 9295 | /* These checks should match the ones in grokdeclarator. |
4393e105 | 9296 | |
c8094d83 MS |
9297 | [temp.deduct] |
9298 | ||
9299 | The deduction may fail for any of the following reasons: | |
4393e105 MM |
9300 | |
9301 | -- Attempting to create an array with an element type that | |
c8094d83 | 9302 | is void, a function type, or a reference type, or [DR337] |
cfb91b67 | 9303 | an abstract class type. */ |
c8094d83 | 9304 | if (TREE_CODE (type) == VOID_TYPE |
4393e105 MM |
9305 | || TREE_CODE (type) == FUNCTION_TYPE |
9306 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 9307 | { |
c2ea3a40 | 9308 | if (complain & tf_error) |
0f51ccfc | 9309 | error ("creating array of %qT", type); |
4393e105 | 9310 | return error_mark_node; |
0ecfe0b4 | 9311 | } |
cfb91b67 GB |
9312 | if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type)) |
9313 | { | |
9314 | if (complain & tf_error) | |
c8094d83 | 9315 | error ("creating array of %qT, which is an abstract class type", |
cfb91b67 | 9316 | type); |
c8094d83 | 9317 | return error_mark_node; |
cfb91b67 | 9318 | } |
0ecfe0b4 | 9319 | |
8d08fdba | 9320 | r = build_cplus_array_type (type, domain); |
5044ab0e JM |
9321 | |
9322 | if (TYPE_USER_ALIGN (t)) | |
9323 | { | |
9324 | TYPE_ALIGN (r) = TYPE_ALIGN (t); | |
9325 | TYPE_USER_ALIGN (r) = 1; | |
9326 | } | |
9327 | ||
8d08fdba MS |
9328 | return r; |
9329 | } | |
9330 | ||
8d08fdba | 9331 | case PLUS_EXPR: |
5566b478 | 9332 | case MINUS_EXPR: |
4393e105 | 9333 | { |
c2ea3a40 NS |
9334 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
9335 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
4393e105 MM |
9336 | |
9337 | if (e1 == error_mark_node || e2 == error_mark_node) | |
9338 | return error_mark_node; | |
9339 | ||
7866705a | 9340 | return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2); |
4393e105 | 9341 | } |
8d08fdba MS |
9342 | |
9343 | case NEGATE_EXPR: | |
9344 | case NOP_EXPR: | |
4393e105 | 9345 | { |
c2ea3a40 | 9346 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
9347 | if (e == error_mark_node) |
9348 | return error_mark_node; | |
9349 | ||
7866705a | 9350 | return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e); |
4393e105 | 9351 | } |
8d08fdba | 9352 | |
5566b478 MS |
9353 | case TYPENAME_TYPE: |
9354 | { | |
4393e105 MM |
9355 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
9356 | in_decl, /*entering_scope=*/1); | |
9357 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
c8094d83 | 9358 | complain, in_decl); |
4393e105 MM |
9359 | |
9360 | if (ctx == error_mark_node || f == error_mark_node) | |
9361 | return error_mark_node; | |
ae58fa02 | 9362 | |
bf8f3f93 MM |
9363 | if (!IS_AGGR_TYPE (ctx)) |
9364 | { | |
c2ea3a40 | 9365 | if (complain & tf_error) |
0f51ccfc | 9366 | error ("%qT is not a class, struct, or union type", ctx); |
bf8f3f93 MM |
9367 | return error_mark_node; |
9368 | } | |
9369 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
9370 | { | |
9371 | /* Normally, make_typename_type does not require that the CTX | |
9372 | have complete type in order to allow things like: | |
c8094d83 | 9373 | |
0cbd7506 | 9374 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 9375 | |
bf8f3f93 MM |
9376 | But, such constructs have already been resolved by this |
9377 | point, so here CTX really should have complete type, unless | |
9378 | it's a partial instantiation. */ | |
4393e105 | 9379 | ctx = complete_type (ctx); |
d0f062fb | 9380 | if (!COMPLETE_TYPE_P (ctx)) |
4393e105 | 9381 | { |
c2ea3a40 | 9382 | if (complain & tf_error) |
7a228918 | 9383 | cxx_incomplete_type_error (NULL_TREE, ctx); |
4393e105 MM |
9384 | return error_mark_node; |
9385 | } | |
9386 | } | |
ae58fa02 | 9387 | |
fc6a28d7 | 9388 | f = make_typename_type (ctx, f, typename_type, |
4f2b0fb2 | 9389 | (complain & tf_error) | tf_keep_type_decl); |
f0bcd168 MM |
9390 | if (f == error_mark_node) |
9391 | return f; | |
0cbd7506 MS |
9392 | if (TREE_CODE (f) == TYPE_DECL) |
9393 | { | |
4f2b0fb2 | 9394 | complain |= tf_ignore_bad_quals; |
0cbd7506 MS |
9395 | f = TREE_TYPE (f); |
9396 | } | |
c8094d83 | 9397 | |
fc6a28d7 MM |
9398 | if (TREE_CODE (f) != TYPENAME_TYPE) |
9399 | { | |
9400 | if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE) | |
c8094d83 | 9401 | error ("%qT resolves to %qT, which is not an enumeration type", |
fc6a28d7 MM |
9402 | t, f); |
9403 | else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f)) | |
c8094d83 | 9404 | error ("%qT resolves to %qT, which is is not a class type", |
fc6a28d7 MM |
9405 | t, f); |
9406 | } | |
9407 | ||
0cbd7506 MS |
9408 | return cp_build_qualified_type_real |
9409 | (f, cp_type_quals (f) | cp_type_quals (t), complain); | |
5566b478 | 9410 | } |
c8094d83 | 9411 | |
b8c6534b KL |
9412 | case UNBOUND_CLASS_TEMPLATE: |
9413 | { | |
9414 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, | |
9415 | in_decl, /*entering_scope=*/1); | |
9416 | tree name = TYPE_IDENTIFIER (t); | |
b939a023 | 9417 | tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t)); |
b8c6534b KL |
9418 | |
9419 | if (ctx == error_mark_node || name == error_mark_node) | |
9420 | return error_mark_node; | |
9421 | ||
b939a023 KL |
9422 | if (parm_list) |
9423 | parm_list = tsubst_template_parms (parm_list, args, complain); | |
9424 | return make_unbound_class_template (ctx, name, parm_list, complain); | |
b8c6534b KL |
9425 | } |
9426 | ||
5566b478 | 9427 | case INDIRECT_REF: |
5566b478 | 9428 | case ADDR_EXPR: |
058b15c1 | 9429 | case CALL_EXPR: |
315fb5db | 9430 | gcc_unreachable (); |
5566b478 MS |
9431 | |
9432 | case ARRAY_REF: | |
4393e105 | 9433 | { |
c2ea3a40 | 9434 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
015c2c66 MM |
9435 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl, |
9436 | /*integral_constant_expression_p=*/false); | |
4393e105 MM |
9437 | if (e1 == error_mark_node || e2 == error_mark_node) |
9438 | return error_mark_node; | |
9439 | ||
44de5aeb | 9440 | return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE); |
4393e105 | 9441 | } |
5566b478 | 9442 | |
fc378698 | 9443 | case SCOPE_REF: |
4393e105 | 9444 | { |
c2ea3a40 | 9445 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
9446 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); |
9447 | if (e1 == error_mark_node || e2 == error_mark_node) | |
9448 | return error_mark_node; | |
9449 | ||
02ed62dd MM |
9450 | return build_qualified_name (/*type=*/NULL_TREE, |
9451 | e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t)); | |
4393e105 | 9452 | } |
fc378698 | 9453 | |
b894fc05 | 9454 | case TYPEOF_TYPE: |
4393e105 | 9455 | { |
b830b74c | 9456 | tree type; |
4393e105 | 9457 | |
015c2c66 MM |
9458 | type = finish_typeof (tsubst_expr |
9459 | (TYPEOF_TYPE_EXPR (t), args, | |
9460 | complain, in_decl, | |
9461 | /*integral_constant_expression_p=*/false)); | |
b830b74c | 9462 | return cp_build_qualified_type_real (type, |
dce50630 | 9463 | cp_type_quals (t) |
b830b74c | 9464 | | cp_type_quals (type), |
dce50630 | 9465 | complain); |
4393e105 | 9466 | } |
b894fc05 | 9467 | |
3ad6a8e1 DG |
9468 | case DECLTYPE_TYPE: |
9469 | { | |
9470 | tree type; | |
9471 | ||
9472 | type = | |
9473 | finish_decltype_type (tsubst_expr | |
9474 | (DECLTYPE_TYPE_EXPR (t), args, | |
9475 | complain, in_decl, | |
9476 | /*integral_constant_expression_p=*/false), | |
9477 | DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t)); | |
9478 | return cp_build_qualified_type_real (type, | |
9479 | cp_type_quals (t) | |
9480 | | cp_type_quals (type), | |
9481 | complain); | |
9482 | } | |
9483 | ||
5d80a306 DG |
9484 | case TYPE_ARGUMENT_PACK: |
9485 | case NONTYPE_ARGUMENT_PACK: | |
9486 | { | |
9487 | tree r = make_node (TREE_CODE (t)); | |
9488 | tree packed_out = | |
9489 | tsubst_template_args (ARGUMENT_PACK_ARGS (t), | |
9490 | args, | |
9491 | complain, | |
9492 | in_decl); | |
9493 | SET_ARGUMENT_PACK_ARGS (r, packed_out); | |
9494 | ||
9495 | /* For template nontype argument packs, also substitute into | |
9496 | the type. */ | |
9497 | if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK) | |
9498 | TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
9499 | ||
9500 | return r; | |
9501 | } | |
9502 | break; | |
9503 | ||
8d08fdba | 9504 | default: |
9e637a26 | 9505 | sorry ("use of %qs in template", |
8d08fdba MS |
9506 | tree_code_name [(int) TREE_CODE (t)]); |
9507 | return error_mark_node; | |
9508 | } | |
9509 | } | |
9510 | ||
ee76b931 MM |
9511 | /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the |
9512 | type of the expression on the left-hand side of the "." or "->" | |
9513 | operator. */ | |
9514 | ||
9515 | static tree | |
9516 | tsubst_baselink (tree baselink, tree object_type, | |
9517 | tree args, tsubst_flags_t complain, tree in_decl) | |
9518 | { | |
9519 | tree name; | |
9520 | tree qualifying_scope; | |
9521 | tree fns; | |
6f67f000 | 9522 | tree optype; |
ee76b931 MM |
9523 | tree template_args = 0; |
9524 | bool template_id_p = false; | |
9525 | ||
51a203d9 MM |
9526 | /* A baselink indicates a function from a base class. Both the |
9527 | BASELINK_ACCESS_BINFO and the base class referenced may | |
9528 | indicate bases of the template class, rather than the | |
9529 | instantiated class. In addition, lookups that were not | |
9530 | ambiguous before may be ambiguous now. Therefore, we perform | |
9531 | the lookup again. */ | |
ee76b931 | 9532 | qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)); |
51a203d9 MM |
9533 | qualifying_scope = tsubst (qualifying_scope, args, |
9534 | complain, in_decl); | |
ee76b931 | 9535 | fns = BASELINK_FUNCTIONS (baselink); |
6f67f000 | 9536 | optype = BASELINK_OPTYPE (baselink); |
ee76b931 MM |
9537 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) |
9538 | { | |
9539 | template_id_p = true; | |
9540 | template_args = TREE_OPERAND (fns, 1); | |
9541 | fns = TREE_OPERAND (fns, 0); | |
bf12d54d NS |
9542 | if (template_args) |
9543 | template_args = tsubst_template_args (template_args, args, | |
9544 | complain, in_decl); | |
ee76b931 MM |
9545 | } |
9546 | name = DECL_NAME (get_first_fn (fns)); | |
9547 | baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1); | |
c8094d83 | 9548 | |
9b60dfd7 MM |
9549 | /* If lookup found a single function, mark it as used at this |
9550 | point. (If it lookup found multiple functions the one selected | |
9551 | later by overload resolution will be marked as used at that | |
9552 | point.) */ | |
9553 | if (BASELINK_P (baselink)) | |
9554 | fns = BASELINK_FUNCTIONS (baselink); | |
9555 | if (!template_id_p && !really_overloaded_fn (fns)) | |
9556 | mark_used (OVL_CURRENT (fns)); | |
9557 | ||
9558 | /* Add back the template arguments, if present. */ | |
ee76b931 | 9559 | if (BASELINK_P (baselink) && template_id_p) |
c8094d83 | 9560 | BASELINK_FUNCTIONS (baselink) |
ee76b931 MM |
9561 | = build_nt (TEMPLATE_ID_EXPR, |
9562 | BASELINK_FUNCTIONS (baselink), | |
9563 | template_args); | |
6f67f000 MM |
9564 | /* Update the conversion operator type. */ |
9565 | BASELINK_OPTYPE (baselink) | |
9566 | = tsubst (optype, args, complain, in_decl); | |
9b60dfd7 | 9567 | |
ee76b931 MM |
9568 | if (!object_type) |
9569 | object_type = current_class_type; | |
c8094d83 | 9570 | return adjust_result_of_qualified_name_lookup (baselink, |
ee76b931 MM |
9571 | qualifying_scope, |
9572 | object_type); | |
9573 | } | |
9574 | ||
9575 | /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is | |
9576 | true if the qualified-id will be a postfix-expression in-and-of | |
9577 | itself; false if more of the postfix-expression follows the | |
9578 | QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand | |
9579 | of "&". */ | |
9580 | ||
9581 | static tree | |
c8094d83 | 9582 | tsubst_qualified_id (tree qualified_id, tree args, |
ee76b931 MM |
9583 | tsubst_flags_t complain, tree in_decl, |
9584 | bool done, bool address_p) | |
9585 | { | |
9586 | tree expr; | |
9587 | tree scope; | |
9588 | tree name; | |
9589 | bool is_template; | |
9590 | tree template_args; | |
9591 | ||
50bc768d | 9592 | gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF); |
ee76b931 | 9593 | |
ee76b931 MM |
9594 | /* Figure out what name to look up. */ |
9595 | name = TREE_OPERAND (qualified_id, 1); | |
9596 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
9597 | { | |
9598 | is_template = true; | |
bf12d54d NS |
9599 | template_args = TREE_OPERAND (name, 1); |
9600 | if (template_args) | |
9601 | template_args = tsubst_template_args (template_args, args, | |
9602 | complain, in_decl); | |
ee76b931 MM |
9603 | name = TREE_OPERAND (name, 0); |
9604 | } | |
9605 | else | |
9606 | { | |
9607 | is_template = false; | |
9608 | template_args = NULL_TREE; | |
9609 | } | |
9610 | ||
6eeba0cc MM |
9611 | /* Substitute into the qualifying scope. When there are no ARGS, we |
9612 | are just trying to simplify a non-dependent expression. In that | |
9613 | case the qualifying scope may be dependent, and, in any case, | |
9614 | substituting will not help. */ | |
9615 | scope = TREE_OPERAND (qualified_id, 0); | |
9616 | if (args) | |
9617 | { | |
9618 | scope = tsubst (scope, args, complain, in_decl); | |
9619 | expr = tsubst_copy (name, args, complain, in_decl); | |
9620 | } | |
9621 | else | |
9622 | expr = name; | |
10b1d5e7 | 9623 | |
ab73670a | 9624 | if (dependent_type_p (scope)) |
3db45ab5 MS |
9625 | return build_qualified_name (/*type=*/NULL_TREE, |
9626 | scope, expr, | |
02ed62dd | 9627 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id)); |
c8094d83 | 9628 | |
5e08432e | 9629 | if (!BASELINK_P (name) && !DECL_P (expr)) |
12483c9f | 9630 | { |
8ca4bf25 MM |
9631 | if (TREE_CODE (expr) == BIT_NOT_EXPR) |
9632 | /* If this were actually a destructor call, it would have been | |
9633 | parsed as such by the parser. */ | |
9634 | expr = error_mark_node; | |
9635 | else | |
9636 | expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false); | |
12483c9f NS |
9637 | if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL |
9638 | ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL) | |
9639 | { | |
9640 | if (complain & tf_error) | |
b2693faf | 9641 | { |
0f51ccfc | 9642 | error ("dependent-name %qE is parsed as a non-type, but " |
b2693faf | 9643 | "instantiation yields a type", qualified_id); |
0f51ccfc | 9644 | inform ("say %<typename %E%> if a type is meant", qualified_id); |
b2693faf | 9645 | } |
12483c9f NS |
9646 | return error_mark_node; |
9647 | } | |
9648 | } | |
c8094d83 | 9649 | |
279b8466 | 9650 | if (DECL_P (expr)) |
8f78f01f MM |
9651 | { |
9652 | check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE, | |
9653 | scope); | |
9654 | /* Remember that there was a reference to this entity. */ | |
9655 | mark_used (expr); | |
9656 | } | |
9657 | ||
9658 | if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST) | |
9659 | { | |
9660 | if (complain & tf_error) | |
c8094d83 | 9661 | qualified_name_lookup_error (scope, |
8f78f01f MM |
9662 | TREE_OPERAND (qualified_id, 1), |
9663 | expr); | |
9664 | return error_mark_node; | |
9665 | } | |
ee76b931 MM |
9666 | |
9667 | if (is_template) | |
10b1d5e7 | 9668 | expr = lookup_template_function (expr, template_args); |
ee76b931 | 9669 | |
22038b2c | 9670 | if (expr == error_mark_node && complain & tf_error) |
8f78f01f MM |
9671 | qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1), |
9672 | expr); | |
22038b2c | 9673 | else if (TYPE_P (scope)) |
ee76b931 | 9674 | { |
c8094d83 | 9675 | expr = (adjust_result_of_qualified_name_lookup |
ee76b931 | 9676 | (expr, scope, current_class_type)); |
3db45ab5 | 9677 | expr = (finish_qualified_id_expr |
02ed62dd MM |
9678 | (scope, expr, done, address_p, |
9679 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id), | |
9680 | /*template_arg_p=*/false)); | |
ee76b931 | 9681 | } |
c8094d83 | 9682 | |
015c2c66 MM |
9683 | /* Expressions do not generally have reference type. */ |
9684 | if (TREE_CODE (expr) != SCOPE_REF | |
9685 | /* However, if we're about to form a pointer-to-member, we just | |
9686 | want the referenced member referenced. */ | |
9687 | && TREE_CODE (expr) != OFFSET_REF) | |
aec5ba60 | 9688 | expr = convert_from_reference (expr); |
ee76b931 MM |
9689 | |
9690 | return expr; | |
9691 | } | |
9692 | ||
00d3396f JM |
9693 | /* Like tsubst, but deals with expressions. This function just replaces |
9694 | template parms; to finish processing the resultant expression, use | |
9695 | tsubst_expr. */ | |
9696 | ||
14d22dd6 | 9697 | static tree |
3a978d72 | 9698 | tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
5566b478 MS |
9699 | { |
9700 | enum tree_code code; | |
8452b1d3 | 9701 | tree r; |
8d08fdba | 9702 | |
5566b478 MS |
9703 | if (t == NULL_TREE || t == error_mark_node) |
9704 | return t; | |
9705 | ||
9706 | code = TREE_CODE (t); | |
b7484fbe | 9707 | |
5566b478 MS |
9708 | switch (code) |
9709 | { | |
9710 | case PARM_DECL: | |
a723baf1 | 9711 | r = retrieve_local_specialization (t); |
50bc768d | 9712 | gcc_assert (r != NULL); |
5d80a306 DG |
9713 | if (TREE_CODE (r) == ARGUMENT_PACK_SELECT) |
9714 | r = ARGUMENT_PACK_SELECT_ARG (r); | |
c0694c4b | 9715 | mark_used (r); |
a723baf1 | 9716 | return r; |
5566b478 MS |
9717 | |
9718 | case CONST_DECL: | |
ed44da02 MM |
9719 | { |
9720 | tree enum_type; | |
9721 | tree v; | |
9722 | ||
a723baf1 MM |
9723 | if (DECL_TEMPLATE_PARM_P (t)) |
9724 | return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); | |
7b6d72fc MM |
9725 | /* There is no need to substitute into namespace-scope |
9726 | enumerators. */ | |
9727 | if (DECL_NAMESPACE_SCOPE_P (t)) | |
ed44da02 | 9728 | return t; |
d5a10cf0 MM |
9729 | /* If ARGS is NULL, then T is known to be non-dependent. */ |
9730 | if (args == NULL_TREE) | |
8a784e4a | 9731 | return integral_constant_value (t); |
ed44da02 MM |
9732 | |
9733 | /* Unfortunately, we cannot just call lookup_name here. | |
9188c363 | 9734 | Consider: |
c8094d83 | 9735 | |
9188c363 MM |
9736 | template <int I> int f() { |
9737 | enum E { a = I }; | |
9738 | struct S { void g() { E e = a; } }; | |
9739 | }; | |
c8094d83 | 9740 | |
9188c363 MM |
9741 | When we instantiate f<7>::S::g(), say, lookup_name is not |
9742 | clever enough to find f<7>::a. */ | |
c8094d83 MS |
9743 | enum_type |
9744 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, | |
ed44da02 MM |
9745 | /*entering_scope=*/0); |
9746 | ||
c8094d83 MS |
9747 | for (v = TYPE_VALUES (enum_type); |
9748 | v != NULL_TREE; | |
ed44da02 MM |
9749 | v = TREE_CHAIN (v)) |
9750 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
9751 | return TREE_VALUE (v); | |
9752 | ||
9753 | /* We didn't find the name. That should never happen; if | |
9754 | name-lookup found it during preliminary parsing, we | |
9755 | should find it again here during instantiation. */ | |
315fb5db | 9756 | gcc_unreachable (); |
ed44da02 | 9757 | } |
db04386f | 9758 | return t; |
ed44da02 | 9759 | |
5566b478 MS |
9760 | case FIELD_DECL: |
9761 | if (DECL_CONTEXT (t)) | |
9762 | { | |
0978790f | 9763 | tree ctx; |
0978790f | 9764 | |
4393e105 | 9765 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 9766 | /*entering_scope=*/1); |
0978790f | 9767 | if (ctx != DECL_CONTEXT (t)) |
bad1f462 KL |
9768 | { |
9769 | tree r = lookup_field (ctx, DECL_NAME (t), 0, false); | |
9770 | if (!r) | |
9771 | { | |
9772 | if (complain & tf_error) | |
9773 | error ("using invalid field %qD", t); | |
9774 | return error_mark_node; | |
9775 | } | |
9776 | return r; | |
9777 | } | |
5566b478 | 9778 | } |
c8094d83 | 9779 | |
5566b478 MS |
9780 | return t; |
9781 | ||
9782 | case VAR_DECL: | |
9783 | case FUNCTION_DECL: | |
a723baf1 MM |
9784 | if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) |
9785 | || local_variable_p (t)) | |
4393e105 | 9786 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
9787 | mark_used (t); |
9788 | return t; | |
9789 | ||
a723baf1 | 9790 | case BASELINK: |
ee76b931 | 9791 | return tsubst_baselink (t, current_class_type, args, complain, in_decl); |
a723baf1 | 9792 | |
98c1c668 | 9793 | case TEMPLATE_DECL: |
a723baf1 | 9794 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
c8094d83 | 9795 | return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), |
a723baf1 | 9796 | args, complain, in_decl); |
c7222c02 | 9797 | else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t)) |
4393e105 | 9798 | return tsubst (t, args, complain, in_decl); |
fcea7401 KL |
9799 | else if (DECL_CLASS_SCOPE_P (t) |
9800 | && uses_template_parms (DECL_CONTEXT (t))) | |
9801 | { | |
9802 | /* Template template argument like the following example need | |
9803 | special treatment: | |
9804 | ||
9805 | template <template <class> class TT> struct C {}; | |
9806 | template <class T> struct D { | |
9807 | template <class U> struct E {}; | |
0cbd7506 | 9808 | C<E> c; // #1 |
fcea7401 KL |
9809 | }; |
9810 | D<int> d; // #2 | |
9811 | ||
9812 | We are processing the template argument `E' in #1 for | |
9813 | the template instantiation #2. Originally, `E' is a | |
9814 | TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we | |
9815 | have to substitute this with one having context `D<int>'. */ | |
9816 | ||
9817 | tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl); | |
9818 | return lookup_field (context, DECL_NAME(t), 0, false); | |
9819 | } | |
98c1c668 | 9820 | else |
fcea7401 | 9821 | /* Ordinary template template argument. */ |
98c1c668 JM |
9822 | return t; |
9823 | ||
5566b478 MS |
9824 | case CAST_EXPR: |
9825 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
9826 | case CONST_CAST_EXPR: |
9827 | case STATIC_CAST_EXPR: | |
9828 | case DYNAMIC_CAST_EXPR: | |
51924768 | 9829 | case NOP_EXPR: |
5566b478 | 9830 | return build1 |
4393e105 MM |
9831 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
9832 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 | 9833 | |
5d80a306 DG |
9834 | case SIZEOF_EXPR: |
9835 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) | |
9836 | { | |
9837 | /* We only want to compute the number of arguments. */ | |
9838 | tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args, | |
9839 | complain, in_decl); | |
4745e4eb JJ |
9840 | if (expanded == error_mark_node) |
9841 | return error_mark_node; | |
5d80a306 DG |
9842 | return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded)); |
9843 | } | |
9844 | /* Fall through */ | |
9845 | ||
5566b478 | 9846 | case INDIRECT_REF: |
5566b478 MS |
9847 | case NEGATE_EXPR: |
9848 | case TRUTH_NOT_EXPR: | |
b87692e5 | 9849 | case BIT_NOT_EXPR: |
5566b478 | 9850 | case ADDR_EXPR: |
392e3d51 | 9851 | case UNARY_PLUS_EXPR: /* Unary + */ |
abff8e06 | 9852 | case ALIGNOF_EXPR: |
5566b478 | 9853 | case ARROW_EXPR: |
fc378698 | 9854 | case THROW_EXPR: |
5156628f | 9855 | case TYPEID_EXPR: |
f5733617 SS |
9856 | case REALPART_EXPR: |
9857 | case IMAGPART_EXPR: | |
5566b478 | 9858 | return build1 |
6a629cac | 9859 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 9860 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 | 9861 | |
a723baf1 MM |
9862 | case COMPONENT_REF: |
9863 | { | |
9864 | tree object; | |
9865 | tree name; | |
9866 | ||
9867 | object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
9868 | name = TREE_OPERAND (t, 1); | |
c8094d83 | 9869 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
a723baf1 MM |
9870 | { |
9871 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
9872 | complain, in_decl); | |
9873 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
9874 | } | |
9875 | else if (TREE_CODE (name) == SCOPE_REF | |
9876 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
9877 | { | |
9878 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, | |
9879 | complain, in_decl); | |
9880 | name = TREE_OPERAND (name, 1); | |
9881 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
9882 | complain, in_decl); | |
9883 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
02ed62dd | 9884 | name = build_qualified_name (/*type=*/NULL_TREE, |
3db45ab5 | 9885 | base, name, |
02ed62dd | 9886 | /*template_p=*/false); |
a723baf1 | 9887 | } |
ee76b931 | 9888 | else if (TREE_CODE (name) == BASELINK) |
c8094d83 MS |
9889 | name = tsubst_baselink (name, |
9890 | non_reference (TREE_TYPE (object)), | |
9891 | args, complain, | |
ee76b931 | 9892 | in_decl); |
a723baf1 | 9893 | else |
ee76b931 | 9894 | name = tsubst_copy (name, args, complain, in_decl); |
44de5aeb | 9895 | return build_nt (COMPONENT_REF, object, name, NULL_TREE); |
a723baf1 MM |
9896 | } |
9897 | ||
5566b478 MS |
9898 | case PLUS_EXPR: |
9899 | case MINUS_EXPR: | |
9900 | case MULT_EXPR: | |
9901 | case TRUNC_DIV_EXPR: | |
9902 | case CEIL_DIV_EXPR: | |
9903 | case FLOOR_DIV_EXPR: | |
9904 | case ROUND_DIV_EXPR: | |
9905 | case EXACT_DIV_EXPR: | |
9906 | case BIT_AND_EXPR: | |
5566b478 MS |
9907 | case BIT_IOR_EXPR: |
9908 | case BIT_XOR_EXPR: | |
9909 | case TRUNC_MOD_EXPR: | |
9910 | case FLOOR_MOD_EXPR: | |
9911 | case TRUTH_ANDIF_EXPR: | |
9912 | case TRUTH_ORIF_EXPR: | |
9913 | case TRUTH_AND_EXPR: | |
9914 | case TRUTH_OR_EXPR: | |
9915 | case RSHIFT_EXPR: | |
9916 | case LSHIFT_EXPR: | |
9917 | case RROTATE_EXPR: | |
9918 | case LROTATE_EXPR: | |
9919 | case EQ_EXPR: | |
9920 | case NE_EXPR: | |
9921 | case MAX_EXPR: | |
9922 | case MIN_EXPR: | |
9923 | case LE_EXPR: | |
9924 | case GE_EXPR: | |
9925 | case LT_EXPR: | |
9926 | case GT_EXPR: | |
5566b478 | 9927 | case COMPOUND_EXPR: |
5566b478 MS |
9928 | case DOTSTAR_EXPR: |
9929 | case MEMBER_REF: | |
519c9806 MM |
9930 | case PREDECREMENT_EXPR: |
9931 | case PREINCREMENT_EXPR: | |
9932 | case POSTDECREMENT_EXPR: | |
9933 | case POSTINCREMENT_EXPR: | |
5566b478 | 9934 | return build_nt |
4393e105 MM |
9935 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
9936 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 | 9937 | |
02ed62dd MM |
9938 | case SCOPE_REF: |
9939 | return build_qualified_name (/*type=*/NULL_TREE, | |
9940 | tsubst_copy (TREE_OPERAND (t, 0), | |
9941 | args, complain, in_decl), | |
9942 | tsubst_copy (TREE_OPERAND (t, 1), | |
9943 | args, complain, in_decl), | |
9944 | QUALIFIED_NAME_IS_TEMPLATE (t)); | |
9945 | ||
d8987adb NS |
9946 | case ARRAY_REF: |
9947 | return build_nt | |
9948 | (ARRAY_REF, | |
9949 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
9950 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
9951 | NULL_TREE, NULL_TREE); | |
9952 | ||
5566b478 | 9953 | case CALL_EXPR: |
5039610b SL |
9954 | { |
9955 | int n = VL_EXP_OPERAND_LENGTH (t); | |
9956 | tree result = build_vl_exp (CALL_EXPR, n); | |
9957 | int i; | |
9958 | for (i = 0; i < n; i++) | |
9959 | TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args, | |
9960 | complain, in_decl); | |
9961 | return result; | |
9962 | } | |
5566b478 | 9963 | |
5566b478 MS |
9964 | case COND_EXPR: |
9965 | case MODOP_EXPR: | |
40242ccf | 9966 | case PSEUDO_DTOR_EXPR: |
67da3287 | 9967 | { |
8452b1d3 | 9968 | r = build_nt |
4393e105 MM |
9969 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
9970 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
9971 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
e4c2c34b | 9972 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); |
67da3287 MM |
9973 | return r; |
9974 | } | |
5566b478 MS |
9975 | |
9976 | case NEW_EXPR: | |
9977 | { | |
8452b1d3 | 9978 | r = build_nt |
4393e105 MM |
9979 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
9980 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
9981 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
9982 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
9983 | return r; | |
9984 | } | |
9985 | ||
9986 | case DELETE_EXPR: | |
9987 | { | |
8452b1d3 | 9988 | r = build_nt |
4393e105 MM |
9989 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
9990 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
9991 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
9992 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
9993 | return r; | |
9994 | } | |
9995 | ||
386b8a85 JM |
9996 | case TEMPLATE_ID_EXPR: |
9997 | { | |
0cbd7506 | 9998 | /* Substituted template arguments */ |
a91db711 NS |
9999 | tree fn = TREE_OPERAND (t, 0); |
10000 | tree targs = TREE_OPERAND (t, 1); | |
856216bb | 10001 | |
a91db711 | 10002 | fn = tsubst_copy (fn, args, complain, in_decl); |
bf12d54d NS |
10003 | if (targs) |
10004 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 10005 | |
a91db711 | 10006 | return lookup_template_function (fn, targs); |
386b8a85 JM |
10007 | } |
10008 | ||
5566b478 MS |
10009 | case TREE_LIST: |
10010 | { | |
10011 | tree purpose, value, chain; | |
10012 | ||
10013 | if (t == void_list_node) | |
10014 | return t; | |
10015 | ||
10016 | purpose = TREE_PURPOSE (t); | |
10017 | if (purpose) | |
4393e105 | 10018 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
10019 | value = TREE_VALUE (t); |
10020 | if (value) | |
4393e105 | 10021 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
10022 | chain = TREE_CHAIN (t); |
10023 | if (chain && chain != void_type_node) | |
4393e105 | 10024 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
10025 | if (purpose == TREE_PURPOSE (t) |
10026 | && value == TREE_VALUE (t) | |
10027 | && chain == TREE_CHAIN (t)) | |
10028 | return t; | |
10029 | return tree_cons (purpose, value, chain); | |
10030 | } | |
10031 | ||
10032 | case RECORD_TYPE: | |
10033 | case UNION_TYPE: | |
10034 | case ENUMERAL_TYPE: | |
10035 | case INTEGER_TYPE: | |
10036 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 10037 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 10038 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 10039 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
10040 | case POINTER_TYPE: |
10041 | case REFERENCE_TYPE: | |
10042 | case OFFSET_TYPE: | |
10043 | case FUNCTION_TYPE: | |
10044 | case METHOD_TYPE: | |
10045 | case ARRAY_TYPE: | |
10046 | case TYPENAME_TYPE: | |
b8c6534b | 10047 | case UNBOUND_CLASS_TEMPLATE: |
2a2b1d56 | 10048 | case TYPEOF_TYPE: |
3ad6a8e1 | 10049 | case DECLTYPE_TYPE: |
f84b4be9 | 10050 | case TYPE_DECL: |
4393e105 | 10051 | return tsubst (t, args, complain, in_decl); |
5566b478 | 10052 | |
e92cc029 | 10053 | case IDENTIFIER_NODE: |
421844e7 | 10054 | if (IDENTIFIER_TYPENAME_P (t)) |
1f6e1acc AS |
10055 | { |
10056 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
1f84ec23 | 10057 | return mangle_conv_op_name_for_type (new_type); |
1f6e1acc | 10058 | } |
e92cc029 MS |
10059 | else |
10060 | return t; | |
10061 | ||
5156628f | 10062 | case CONSTRUCTOR: |
4038c495 GB |
10063 | /* This is handled by tsubst_copy_and_build. */ |
10064 | gcc_unreachable (); | |
5156628f | 10065 | |
371534a9 | 10066 | case VA_ARG_EXPR: |
ea333e1c | 10067 | return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, |
c2ea3a40 NS |
10068 | in_decl), |
10069 | tsubst (TREE_TYPE (t), args, complain, in_decl)); | |
f9817201 | 10070 | |
543a0daa RH |
10071 | case CLEANUP_POINT_EXPR: |
10072 | /* We shouldn't have built any of these during initial template | |
10073 | generation. Instead, they should be built during instantiation | |
10074 | in response to the saved STMT_IS_FULL_EXPR_P setting. */ | |
315fb5db | 10075 | gcc_unreachable (); |
543a0daa | 10076 | |
7eab6e7b MM |
10077 | case OFFSET_REF: |
10078 | mark_used (TREE_OPERAND (t, 1)); | |
10079 | return t; | |
10080 | ||
5d80a306 DG |
10081 | case EXPR_PACK_EXPANSION: |
10082 | error ("invalid use of pack expansion expression"); | |
10083 | return error_mark_node; | |
10084 | ||
10085 | case NONTYPE_ARGUMENT_PACK: | |
10086 | error ("use %<...%> to expand argument pack"); | |
10087 | return error_mark_node; | |
10088 | ||
5566b478 MS |
10089 | default: |
10090 | return t; | |
10091 | } | |
10092 | } | |
10093 | ||
1799e5d5 RH |
10094 | /* Like tsubst_copy, but specifically for OpenMP clauses. */ |
10095 | ||
10096 | static tree | |
10097 | tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain, | |
10098 | tree in_decl) | |
10099 | { | |
10100 | tree new_clauses = NULL, nc, oc; | |
10101 | ||
10102 | for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc)) | |
10103 | { | |
10104 | nc = copy_node (oc); | |
10105 | OMP_CLAUSE_CHAIN (nc) = new_clauses; | |
10106 | new_clauses = nc; | |
10107 | ||
10108 | switch (OMP_CLAUSE_CODE (nc)) | |
10109 | { | |
10110 | case OMP_CLAUSE_PRIVATE: | |
10111 | case OMP_CLAUSE_SHARED: | |
10112 | case OMP_CLAUSE_FIRSTPRIVATE: | |
10113 | case OMP_CLAUSE_LASTPRIVATE: | |
10114 | case OMP_CLAUSE_REDUCTION: | |
10115 | case OMP_CLAUSE_COPYIN: | |
10116 | case OMP_CLAUSE_COPYPRIVATE: | |
10117 | case OMP_CLAUSE_IF: | |
10118 | case OMP_CLAUSE_NUM_THREADS: | |
10119 | case OMP_CLAUSE_SCHEDULE: | |
10120 | OMP_CLAUSE_OPERAND (nc, 0) | |
015c2c66 MM |
10121 | = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain, |
10122 | in_decl, /*integral_constant_expression_p=*/false); | |
1799e5d5 RH |
10123 | break; |
10124 | case OMP_CLAUSE_NOWAIT: | |
10125 | case OMP_CLAUSE_ORDERED: | |
10126 | case OMP_CLAUSE_DEFAULT: | |
10127 | break; | |
10128 | default: | |
10129 | gcc_unreachable (); | |
10130 | } | |
10131 | } | |
10132 | ||
10133 | return finish_omp_clauses (nreverse (new_clauses)); | |
10134 | } | |
10135 | ||
f74dcfb7 JJ |
10136 | /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */ |
10137 | ||
10138 | static tree | |
10139 | tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain, | |
10140 | tree in_decl) | |
10141 | { | |
10142 | #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl) | |
10143 | ||
10144 | tree purpose, value, chain; | |
10145 | ||
10146 | if (t == NULL) | |
10147 | return t; | |
10148 | ||
10149 | if (TREE_CODE (t) != TREE_LIST) | |
10150 | return tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
10151 | /*function_p=*/false, |
10152 | /*integral_constant_expression_p=*/false); | |
f74dcfb7 JJ |
10153 | |
10154 | if (t == void_list_node) | |
10155 | return t; | |
10156 | ||
10157 | purpose = TREE_PURPOSE (t); | |
10158 | if (purpose) | |
10159 | purpose = RECUR (purpose); | |
10160 | value = TREE_VALUE (t); | |
10161 | if (value) | |
10162 | value = RECUR (value); | |
10163 | chain = TREE_CHAIN (t); | |
10164 | if (chain && chain != void_type_node) | |
10165 | chain = RECUR (chain); | |
10166 | return tree_cons (purpose, value, chain); | |
10167 | #undef RECUR | |
10168 | } | |
10169 | ||
cc23546e JO |
10170 | /* Like tsubst_copy for expressions, etc. but also does semantic |
10171 | processing. */ | |
00d3396f | 10172 | |
14d22dd6 | 10173 | static tree |
015c2c66 MM |
10174 | tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, |
10175 | bool integral_constant_expression_p) | |
5566b478 | 10176 | { |
015c2c66 MM |
10177 | #define RECUR(NODE) \ |
10178 | tsubst_expr ((NODE), args, complain, in_decl, \ | |
10179 | integral_constant_expression_p) | |
10180 | ||
fd10dd09 | 10181 | tree stmt, tmp; |
558475f0 | 10182 | |
5566b478 MS |
10183 | if (t == NULL_TREE || t == error_mark_node) |
10184 | return t; | |
10185 | ||
93409b8c PB |
10186 | if (EXPR_HAS_LOCATION (t)) |
10187 | input_location = EXPR_LOCATION (t); | |
7c34ced1 RH |
10188 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
10189 | current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t); | |
10190 | ||
5566b478 | 10191 | switch (TREE_CODE (t)) |
8d08fdba | 10192 | { |
325c3691 RH |
10193 | case STATEMENT_LIST: |
10194 | { | |
10195 | tree_stmt_iterator i; | |
10196 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
015c2c66 | 10197 | RECUR (tsi_stmt (i)); |
325c3691 RH |
10198 | break; |
10199 | } | |
10200 | ||
558475f0 | 10201 | case CTOR_INITIALIZER: |
c8094d83 | 10202 | finish_mem_initializers (tsubst_initializer_list |
2282d28d MM |
10203 | (TREE_OPERAND (t, 0), args)); |
10204 | break; | |
558475f0 | 10205 | |
5088b058 | 10206 | case RETURN_EXPR: |
015c2c66 | 10207 | finish_return_stmt (RECUR (TREE_OPERAND (t, 0))); |
5566b478 MS |
10208 | break; |
10209 | ||
10210 | case EXPR_STMT: | |
015c2c66 | 10211 | tmp = RECUR (EXPR_STMT_EXPR (t)); |
7c34ced1 RH |
10212 | if (EXPR_STMT_STMT_EXPR_RESULT (t)) |
10213 | finish_stmt_expr_expr (tmp, cur_stmt_expr); | |
10214 | else | |
10215 | finish_expr_stmt (tmp); | |
10216 | break; | |
5566b478 | 10217 | |
9da99f7d | 10218 | case USING_STMT: |
015c2c66 | 10219 | do_using_directive (RECUR (USING_STMT_NAMESPACE (t))); |
9da99f7d | 10220 | break; |
c8094d83 | 10221 | |
350fae66 | 10222 | case DECL_EXPR: |
5566b478 | 10223 | { |
e0942dcd MM |
10224 | tree decl; |
10225 | tree init; | |
5566b478 | 10226 | |
350fae66 | 10227 | decl = DECL_EXPR_DECL (t); |
acef433b MM |
10228 | if (TREE_CODE (decl) == LABEL_DECL) |
10229 | finish_label_decl (DECL_NAME (decl)); | |
fbfe8c9e NS |
10230 | else if (TREE_CODE (decl) == USING_DECL) |
10231 | { | |
98ed9dae | 10232 | tree scope = USING_DECL_SCOPE (decl); |
fbfe8c9e | 10233 | tree name = DECL_NAME (decl); |
22038b2c | 10234 | tree decl; |
c8094d83 | 10235 | |
015c2c66 | 10236 | scope = RECUR (scope); |
22038b2c | 10237 | decl = lookup_qualified_name (scope, name, |
12483c9f NS |
10238 | /*is_type_p=*/false, |
10239 | /*complain=*/false); | |
8f78f01f MM |
10240 | if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) |
10241 | qualified_name_lookup_error (scope, name, decl); | |
22038b2c | 10242 | else |
ed5f054f | 10243 | do_local_using_decl (decl, scope, name); |
fbfe8c9e | 10244 | } |
acef433b MM |
10245 | else |
10246 | { | |
10247 | init = DECL_INITIAL (decl); | |
10248 | decl = tsubst (decl, args, complain, in_decl); | |
ce2e5191 NS |
10249 | if (decl != error_mark_node) |
10250 | { | |
0cbd7506 MS |
10251 | /* By marking the declaration as instantiated, we avoid |
10252 | trying to instantiate it. Since instantiate_decl can't | |
10253 | handle local variables, and since we've already done | |
10254 | all that needs to be done, that's the right thing to | |
10255 | do. */ | |
10256 | if (TREE_CODE (decl) == VAR_DECL) | |
10257 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
edebf865 MM |
10258 | if (TREE_CODE (decl) == VAR_DECL |
10259 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) | |
10260 | /* Anonymous aggregates are a special case. */ | |
10261 | finish_anon_union (decl); | |
c8094d83 | 10262 | else |
ed2fa432 | 10263 | { |
edebf865 | 10264 | maybe_push_decl (decl); |
39703eb9 MM |
10265 | if (TREE_CODE (decl) == VAR_DECL |
10266 | && DECL_PRETTY_FUNCTION_P (decl)) | |
edebf865 MM |
10267 | { |
10268 | /* For __PRETTY_FUNCTION__ we have to adjust the | |
10269 | initializer. */ | |
10270 | const char *const name | |
10271 | = cxx_printable_name (current_function_decl, 2); | |
bb885938 | 10272 | init = cp_fname_init (name, &TREE_TYPE (decl)); |
edebf865 MM |
10273 | } |
10274 | else | |
268127ce DG |
10275 | { |
10276 | tree t = RECUR (init); | |
10277 | ||
10278 | if (init && !t) | |
10279 | /* If we had an initializer but it | |
10280 | instantiated to nothing, | |
10281 | value-initialize the object. This will | |
10282 | only occur when the initializer was a | |
10283 | pack expansion where the parameter packs | |
10284 | used in that expansion were of length | |
10285 | zero. */ | |
10286 | init = build_default_init (TREE_TYPE (decl), | |
10287 | NULL_TREE); | |
10288 | else | |
10289 | init = t; | |
10290 | } | |
10291 | ||
d174af6c | 10292 | finish_decl (decl, init, NULL_TREE); |
ed2fa432 | 10293 | } |
ce2e5191 | 10294 | } |
acef433b | 10295 | } |
fd10dd09 | 10296 | |
350fae66 | 10297 | /* A DECL_EXPR can also be used as an expression, in the condition |
325c3691 RH |
10298 | clause of an if/for/while construct. */ |
10299 | return decl; | |
5566b478 | 10300 | } |
8d08fdba | 10301 | |
5566b478 | 10302 | case FOR_STMT: |
7c34ced1 | 10303 | stmt = begin_for_stmt (); |
015c2c66 | 10304 | RECUR (FOR_INIT_STMT (t)); |
7c34ced1 | 10305 | finish_for_init_stmt (stmt); |
015c2c66 | 10306 | tmp = RECUR (FOR_COND (t)); |
7c34ced1 | 10307 | finish_for_cond (tmp, stmt); |
015c2c66 | 10308 | tmp = RECUR (FOR_EXPR (t)); |
7c34ced1 | 10309 | finish_for_expr (tmp, stmt); |
015c2c66 | 10310 | RECUR (FOR_BODY (t)); |
7c34ced1 | 10311 | finish_for_stmt (stmt); |
5566b478 | 10312 | break; |
8d08fdba | 10313 | |
5566b478 | 10314 | case WHILE_STMT: |
7c34ced1 | 10315 | stmt = begin_while_stmt (); |
015c2c66 | 10316 | tmp = RECUR (WHILE_COND (t)); |
7c34ced1 | 10317 | finish_while_stmt_cond (tmp, stmt); |
015c2c66 | 10318 | RECUR (WHILE_BODY (t)); |
7c34ced1 | 10319 | finish_while_stmt (stmt); |
5566b478 | 10320 | break; |
8d08fdba | 10321 | |
5566b478 | 10322 | case DO_STMT: |
7c34ced1 | 10323 | stmt = begin_do_stmt (); |
015c2c66 | 10324 | RECUR (DO_BODY (t)); |
7c34ced1 | 10325 | finish_do_body (stmt); |
015c2c66 | 10326 | tmp = RECUR (DO_COND (t)); |
7c34ced1 | 10327 | finish_do_stmt (tmp, stmt); |
5566b478 | 10328 | break; |
a0a33927 | 10329 | |
5566b478 | 10330 | case IF_STMT: |
7c34ced1 | 10331 | stmt = begin_if_stmt (); |
015c2c66 | 10332 | tmp = RECUR (IF_COND (t)); |
7c34ced1 | 10333 | finish_if_stmt_cond (tmp, stmt); |
015c2c66 | 10334 | RECUR (THEN_CLAUSE (t)); |
7c34ced1 | 10335 | finish_then_clause (stmt); |
8d08fdba | 10336 | |
7c34ced1 RH |
10337 | if (ELSE_CLAUSE (t)) |
10338 | { | |
10339 | begin_else_clause (stmt); | |
015c2c66 | 10340 | RECUR (ELSE_CLAUSE (t)); |
7c34ced1 RH |
10341 | finish_else_clause (stmt); |
10342 | } | |
10343 | ||
10344 | finish_if_stmt (stmt); | |
5566b478 | 10345 | break; |
8d08fdba | 10346 | |
5882f0f3 | 10347 | case BIND_EXPR: |
7c34ced1 RH |
10348 | if (BIND_EXPR_BODY_BLOCK (t)) |
10349 | stmt = begin_function_body (); | |
10350 | else | |
10351 | stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t) | |
10352 | ? BCS_TRY_BLOCK : 0); | |
ade3dc07 | 10353 | |
015c2c66 | 10354 | RECUR (BIND_EXPR_BODY (t)); |
ade3dc07 | 10355 | |
7c34ced1 RH |
10356 | if (BIND_EXPR_BODY_BLOCK (t)) |
10357 | finish_function_body (stmt); | |
10358 | else | |
10359 | finish_compound_stmt (stmt); | |
5566b478 | 10360 | break; |
8d08fdba | 10361 | |
5566b478 | 10362 | case BREAK_STMT: |
ad321293 | 10363 | finish_break_stmt (); |
5566b478 | 10364 | break; |
8d08fdba | 10365 | |
6467930b | 10366 | case CONTINUE_STMT: |
ad321293 | 10367 | finish_continue_stmt (); |
6467930b MS |
10368 | break; |
10369 | ||
5566b478 | 10370 | case SWITCH_STMT: |
7c34ced1 | 10371 | stmt = begin_switch_stmt (); |
015c2c66 | 10372 | tmp = RECUR (SWITCH_STMT_COND (t)); |
7c34ced1 | 10373 | finish_switch_cond (tmp, stmt); |
015c2c66 | 10374 | RECUR (SWITCH_STMT_BODY (t)); |
7c34ced1 | 10375 | finish_switch_stmt (stmt); |
5566b478 MS |
10376 | break; |
10377 | ||
8c161995 | 10378 | case CASE_LABEL_EXPR: |
015c2c66 MM |
10379 | finish_case_label (RECUR (CASE_LOW (t)), |
10380 | RECUR (CASE_HIGH (t))); | |
5566b478 MS |
10381 | break; |
10382 | ||
9e14e18f | 10383 | case LABEL_EXPR: |
9e14e18f | 10384 | finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t))); |
5566b478 MS |
10385 | break; |
10386 | ||
9e14e18f | 10387 | case GOTO_EXPR: |
fd10dd09 JM |
10388 | tmp = GOTO_DESTINATION (t); |
10389 | if (TREE_CODE (tmp) != LABEL_DECL) | |
aa09da44 MM |
10390 | /* Computed goto's must be tsubst'd into. On the other hand, |
10391 | non-computed gotos must not be; the identifier in question | |
10392 | will have no binding. */ | |
015c2c66 | 10393 | tmp = RECUR (tmp); |
3fa56191 | 10394 | else |
fd10dd09 JM |
10395 | tmp = DECL_NAME (tmp); |
10396 | finish_goto_stmt (tmp); | |
ad321293 MM |
10397 | break; |
10398 | ||
e130a54b | 10399 | case ASM_EXPR: |
c87978aa | 10400 | tmp = finish_asm_stmt |
6de9cd9a | 10401 | (ASM_VOLATILE_P (t), |
015c2c66 | 10402 | RECUR (ASM_STRING (t)), |
f74dcfb7 JJ |
10403 | tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl), |
10404 | tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl), | |
10405 | tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl)); | |
131263fa AP |
10406 | { |
10407 | tree asm_expr = tmp; | |
10408 | if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR) | |
10409 | asm_expr = TREE_OPERAND (asm_expr, 0); | |
10410 | ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t); | |
10411 | } | |
5566b478 | 10412 | break; |
faf5394a MS |
10413 | |
10414 | case TRY_BLOCK: | |
f1dedc31 | 10415 | if (CLEANUP_P (t)) |
62409b39 | 10416 | { |
57b52417 | 10417 | stmt = begin_try_block (); |
015c2c66 | 10418 | RECUR (TRY_STMTS (t)); |
62409b39 | 10419 | finish_cleanup_try_block (stmt); |
015c2c66 | 10420 | finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt); |
62409b39 | 10421 | } |
f1dedc31 MM |
10422 | else |
10423 | { | |
eaf6fb90 MM |
10424 | tree compound_stmt = NULL_TREE; |
10425 | ||
62409b39 | 10426 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 10427 | stmt = begin_function_try_block (&compound_stmt); |
62409b39 MM |
10428 | else |
10429 | stmt = begin_try_block (); | |
10430 | ||
015c2c66 | 10431 | RECUR (TRY_STMTS (t)); |
62409b39 MM |
10432 | |
10433 | if (FN_TRY_BLOCK_P (t)) | |
10434 | finish_function_try_block (stmt); | |
10435 | else | |
10436 | finish_try_block (stmt); | |
10437 | ||
015c2c66 | 10438 | RECUR (TRY_HANDLERS (t)); |
84df082b | 10439 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 10440 | finish_function_handler_sequence (stmt, compound_stmt); |
84df082b MM |
10441 | else |
10442 | finish_handler_sequence (stmt); | |
f1dedc31 | 10443 | } |
faf5394a | 10444 | break; |
c8094d83 | 10445 | |
faf5394a | 10446 | case HANDLER: |
b35d4555 | 10447 | { |
3a2419a7 | 10448 | tree decl = HANDLER_PARMS (t); |
b35d4555 | 10449 | |
3a2419a7 | 10450 | if (decl) |
b35d4555 | 10451 | { |
b35d4555 | 10452 | decl = tsubst (decl, args, complain, in_decl); |
f8191e64 MM |
10453 | /* Prevent instantiate_decl from trying to instantiate |
10454 | this variable. We've already done all that needs to be | |
10455 | done. */ | |
3a2419a7 VR |
10456 | if (decl != error_mark_node) |
10457 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
b35d4555 | 10458 | } |
3a2419a7 | 10459 | stmt = begin_handler (); |
1a6025b4 | 10460 | finish_handler_parms (decl, stmt); |
015c2c66 | 10461 | RECUR (HANDLER_BODY (t)); |
1a6025b4 | 10462 | finish_handler (stmt); |
b35d4555 | 10463 | } |
faf5394a MS |
10464 | break; |
10465 | ||
b87692e5 | 10466 | case TAG_DEFN: |
fd10dd09 | 10467 | tsubst (TREE_TYPE (t), args, complain, NULL_TREE); |
b87692e5 | 10468 | break; |
55a3debe DG |
10469 | |
10470 | case STATIC_ASSERT: | |
10471 | { | |
10472 | tree condition = | |
10473 | tsubst_expr (STATIC_ASSERT_CONDITION (t), | |
10474 | args, | |
10475 | complain, in_decl, | |
10476 | /*integral_constant_expression_p=*/true); | |
10477 | finish_static_assert (condition, | |
10478 | STATIC_ASSERT_MESSAGE (t), | |
10479 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
10480 | /*member_p=*/false); | |
10481 | } | |
10482 | break; | |
b87692e5 | 10483 | |
1799e5d5 RH |
10484 | case OMP_PARALLEL: |
10485 | tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), | |
10486 | args, complain, in_decl); | |
10487 | stmt = begin_omp_parallel (); | |
015c2c66 | 10488 | RECUR (OMP_PARALLEL_BODY (t)); |
761041be JJ |
10489 | OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt)) |
10490 | = OMP_PARALLEL_COMBINED (t); | |
1799e5d5 RH |
10491 | break; |
10492 | ||
10493 | case OMP_FOR: | |
10494 | { | |
10495 | tree clauses, decl, init, cond, incr, body, pre_body; | |
10496 | ||
10497 | clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), | |
10498 | args, complain, in_decl); | |
10499 | init = OMP_FOR_INIT (t); | |
10500 | gcc_assert (TREE_CODE (init) == MODIFY_EXPR); | |
015c2c66 MM |
10501 | decl = RECUR (TREE_OPERAND (init, 0)); |
10502 | init = RECUR (TREE_OPERAND (init, 1)); | |
10503 | cond = RECUR (OMP_FOR_COND (t)); | |
10504 | incr = RECUR (OMP_FOR_INCR (t)); | |
1799e5d5 RH |
10505 | |
10506 | stmt = begin_omp_structured_block (); | |
10507 | ||
10508 | pre_body = push_stmt_list (); | |
015c2c66 | 10509 | RECUR (OMP_FOR_PRE_BODY (t)); |
1799e5d5 RH |
10510 | pre_body = pop_stmt_list (pre_body); |
10511 | ||
10512 | body = push_stmt_list (); | |
015c2c66 | 10513 | RECUR (OMP_FOR_BODY (t)); |
1799e5d5 RH |
10514 | body = pop_stmt_list (body); |
10515 | ||
10516 | t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body, | |
10517 | pre_body); | |
10518 | if (t) | |
10519 | OMP_FOR_CLAUSES (t) = clauses; | |
10520 | ||
10521 | add_stmt (finish_omp_structured_block (stmt)); | |
10522 | } | |
10523 | break; | |
10524 | ||
10525 | case OMP_SECTIONS: | |
10526 | case OMP_SINGLE: | |
10527 | tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl); | |
10528 | stmt = push_stmt_list (); | |
015c2c66 | 10529 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
10530 | stmt = pop_stmt_list (stmt); |
10531 | ||
10532 | t = copy_node (t); | |
10533 | OMP_BODY (t) = stmt; | |
10534 | OMP_CLAUSES (t) = tmp; | |
10535 | add_stmt (t); | |
10536 | break; | |
10537 | ||
10538 | case OMP_SECTION: | |
10539 | case OMP_CRITICAL: | |
10540 | case OMP_MASTER: | |
10541 | case OMP_ORDERED: | |
10542 | stmt = push_stmt_list (); | |
015c2c66 | 10543 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
10544 | stmt = pop_stmt_list (stmt); |
10545 | ||
10546 | t = copy_node (t); | |
10547 | OMP_BODY (t) = stmt; | |
10548 | add_stmt (t); | |
10549 | break; | |
10550 | ||
10551 | case OMP_ATOMIC: | |
239371f9 JJ |
10552 | gcc_assert (OMP_ATOMIC_DEPENDENT_P (t)); |
10553 | { | |
10554 | tree op1 = TREE_OPERAND (t, 1); | |
10555 | tree lhs = RECUR (TREE_OPERAND (op1, 0)); | |
10556 | tree rhs = RECUR (TREE_OPERAND (op1, 1)); | |
10557 | finish_omp_atomic (TREE_CODE (op1), lhs, rhs); | |
10558 | } | |
1799e5d5 RH |
10559 | break; |
10560 | ||
5d80a306 DG |
10561 | case EXPR_PACK_EXPANSION: |
10562 | error ("invalid use of pack expansion expression"); | |
10563 | return error_mark_node; | |
10564 | ||
10565 | case NONTYPE_ARGUMENT_PACK: | |
10566 | error ("use %<...%> to expand argument pack"); | |
10567 | return error_mark_node; | |
10568 | ||
5566b478 | 10569 | default: |
315fb5db | 10570 | gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); |
c8094d83 | 10571 | |
315fb5db | 10572 | return tsubst_copy_and_build (t, args, complain, in_decl, |
015c2c66 MM |
10573 | /*function_p=*/false, |
10574 | integral_constant_expression_p); | |
5566b478 | 10575 | } |
fd10dd09 | 10576 | |
325c3691 | 10577 | return NULL_TREE; |
015c2c66 | 10578 | #undef RECUR |
8d08fdba MS |
10579 | } |
10580 | ||
b3445994 MM |
10581 | /* T is a postfix-expression that is not being used in a function |
10582 | call. Return the substituted version of T. */ | |
10583 | ||
10584 | static tree | |
c8094d83 | 10585 | tsubst_non_call_postfix_expression (tree t, tree args, |
b3445994 MM |
10586 | tsubst_flags_t complain, |
10587 | tree in_decl) | |
10588 | { | |
10589 | if (TREE_CODE (t) == SCOPE_REF) | |
10590 | t = tsubst_qualified_id (t, args, complain, in_decl, | |
10591 | /*done=*/false, /*address_p=*/false); | |
10592 | else | |
10593 | t = tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
10594 | /*function_p=*/false, |
10595 | /*integral_constant_expression_p=*/false); | |
b3445994 MM |
10596 | |
10597 | return t; | |
10598 | } | |
10599 | ||
cc23546e | 10600 | /* Like tsubst but deals with expressions and performs semantic |
b3445994 | 10601 | analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */ |
cc23546e | 10602 | |
ee76b931 | 10603 | tree |
c8094d83 | 10604 | tsubst_copy_and_build (tree t, |
0cbd7506 MS |
10605 | tree args, |
10606 | tsubst_flags_t complain, | |
10607 | tree in_decl, | |
015c2c66 MM |
10608 | bool function_p, |
10609 | bool integral_constant_expression_p) | |
cc23546e | 10610 | { |
015c2c66 MM |
10611 | #define RECUR(NODE) \ |
10612 | tsubst_copy_and_build (NODE, args, complain, in_decl, \ | |
10613 | /*function_p=*/false, \ | |
10614 | integral_constant_expression_p) | |
b3445994 | 10615 | |
ee76b931 MM |
10616 | tree op1; |
10617 | ||
cc23546e JO |
10618 | if (t == NULL_TREE || t == error_mark_node) |
10619 | return t; | |
10620 | ||
10621 | switch (TREE_CODE (t)) | |
10622 | { | |
399dedb9 NS |
10623 | case USING_DECL: |
10624 | t = DECL_NAME (t); | |
852dcbdd | 10625 | /* Fall through. */ |
b3445994 | 10626 | case IDENTIFIER_NODE: |
cc23546e | 10627 | { |
b3445994 | 10628 | tree decl; |
b3445994 | 10629 | cp_id_kind idk; |
67c03833 | 10630 | bool non_integral_constant_expression_p; |
b3445994 MM |
10631 | const char *error_msg; |
10632 | ||
b3445994 | 10633 | if (IDENTIFIER_TYPENAME_P (t)) |
cc23546e | 10634 | { |
b3445994 MM |
10635 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
10636 | t = mangle_conv_op_name_for_type (new_type); | |
cc23546e | 10637 | } |
b3445994 MM |
10638 | |
10639 | /* Look up the name. */ | |
10e6657a | 10640 | decl = lookup_name (t); |
b3445994 MM |
10641 | |
10642 | /* By convention, expressions use ERROR_MARK_NODE to indicate | |
10643 | failure, not NULL_TREE. */ | |
10644 | if (decl == NULL_TREE) | |
10645 | decl = error_mark_node; | |
10646 | ||
10b1d5e7 | 10647 | decl = finish_id_expression (t, decl, NULL_TREE, |
b3445994 | 10648 | &idk, |
015c2c66 | 10649 | integral_constant_expression_p, |
67c03833 JM |
10650 | /*allow_non_integral_constant_expression_p=*/false, |
10651 | &non_integral_constant_expression_p, | |
02ed62dd MM |
10652 | /*template_p=*/false, |
10653 | /*done=*/true, | |
10654 | /*address_p=*/false, | |
10655 | /*template_arg_p=*/false, | |
b3445994 MM |
10656 | &error_msg); |
10657 | if (error_msg) | |
10658 | error (error_msg); | |
10659 | if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE) | |
10660 | decl = unqualified_name_lookup_error (decl); | |
10661 | return decl; | |
cc23546e JO |
10662 | } |
10663 | ||
10664 | case TEMPLATE_ID_EXPR: | |
10665 | { | |
10666 | tree object; | |
b3445994 | 10667 | tree template = RECUR (TREE_OPERAND (t, 0)); |
bf12d54d NS |
10668 | tree targs = TREE_OPERAND (t, 1); |
10669 | ||
10670 | if (targs) | |
10671 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 10672 | |
cc23546e JO |
10673 | if (TREE_CODE (template) == COMPONENT_REF) |
10674 | { | |
10675 | object = TREE_OPERAND (template, 0); | |
10676 | template = TREE_OPERAND (template, 1); | |
10677 | } | |
10678 | else | |
10679 | object = NULL_TREE; | |
bd83b409 | 10680 | template = lookup_template_function (template, targs); |
c8094d83 | 10681 | |
cc23546e | 10682 | if (object) |
c8094d83 | 10683 | return build3 (COMPONENT_REF, TREE_TYPE (template), |
f293ce4b | 10684 | object, template, NULL_TREE); |
cc23546e | 10685 | else |
eff3a276 | 10686 | return baselink_for_fns (template); |
cc23546e JO |
10687 | } |
10688 | ||
10689 | case INDIRECT_REF: | |
db24eb1f NS |
10690 | { |
10691 | tree r = RECUR (TREE_OPERAND (t, 0)); | |
10692 | ||
10693 | if (REFERENCE_REF_P (t)) | |
10694 | { | |
e8c66fe0 NS |
10695 | /* A type conversion to reference type will be enclosed in |
10696 | such an indirect ref, but the substitution of the cast | |
10697 | will have also added such an indirect ref. */ | |
10698 | if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE) | |
10699 | r = convert_from_reference (r); | |
db24eb1f NS |
10700 | } |
10701 | else | |
10702 | r = build_x_indirect_ref (r, "unary *"); | |
10703 | return r; | |
10704 | } | |
cc23546e | 10705 | |
0da99d4e GB |
10706 | case NOP_EXPR: |
10707 | return build_nop | |
10708 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
10709 | RECUR (TREE_OPERAND (t, 0))); | |
10710 | ||
cc23546e | 10711 | case CAST_EXPR: |
cc23546e | 10712 | case REINTERPRET_CAST_EXPR: |
cc23546e | 10713 | case CONST_CAST_EXPR: |
cc23546e | 10714 | case DYNAMIC_CAST_EXPR: |
cc23546e | 10715 | case STATIC_CAST_EXPR: |
015c2c66 MM |
10716 | { |
10717 | tree type; | |
10718 | tree op; | |
10719 | ||
10720 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
10721 | if (integral_constant_expression_p | |
10722 | && !cast_valid_in_integral_constant_expression_p (type)) | |
10723 | { | |
10724 | error ("a cast to a type other than an integral or " | |
10725 | "enumeration type cannot appear in a constant-expression"); | |
10726 | return error_mark_node; | |
10727 | } | |
10728 | ||
10729 | op = RECUR (TREE_OPERAND (t, 0)); | |
10730 | ||
10731 | switch (TREE_CODE (t)) | |
10732 | { | |
10733 | case CAST_EXPR: | |
10734 | return build_functional_cast (type, op); | |
10735 | case REINTERPRET_CAST_EXPR: | |
10736 | return build_reinterpret_cast (type, op); | |
10737 | case CONST_CAST_EXPR: | |
10738 | return build_const_cast (type, op); | |
10739 | case DYNAMIC_CAST_EXPR: | |
10740 | return build_dynamic_cast (type, op); | |
10741 | case STATIC_CAST_EXPR: | |
10742 | return build_static_cast (type, op); | |
10743 | default: | |
10744 | gcc_unreachable (); | |
10745 | } | |
10746 | } | |
cc23546e | 10747 | |
cc23546e JO |
10748 | case POSTDECREMENT_EXPR: |
10749 | case POSTINCREMENT_EXPR: | |
b3445994 MM |
10750 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
10751 | args, complain, in_decl); | |
ee76b931 MM |
10752 | return build_x_unary_op (TREE_CODE (t), op1); |
10753 | ||
10754 | case PREDECREMENT_EXPR: | |
10755 | case PREINCREMENT_EXPR: | |
cc23546e JO |
10756 | case NEGATE_EXPR: |
10757 | case BIT_NOT_EXPR: | |
cc23546e | 10758 | case ABS_EXPR: |
d17811fd | 10759 | case TRUTH_NOT_EXPR: |
392e3d51 | 10760 | case UNARY_PLUS_EXPR: /* Unary + */ |
d17811fd MM |
10761 | case REALPART_EXPR: |
10762 | case IMAGPART_EXPR: | |
b3445994 | 10763 | return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0))); |
cc23546e | 10764 | |
cc23546e | 10765 | case ADDR_EXPR: |
ee76b931 MM |
10766 | op1 = TREE_OPERAND (t, 0); |
10767 | if (TREE_CODE (op1) == SCOPE_REF) | |
c8094d83 | 10768 | op1 = tsubst_qualified_id (op1, args, complain, in_decl, |
ee76b931 MM |
10769 | /*done=*/true, /*address_p=*/true); |
10770 | else | |
c8094d83 | 10771 | op1 = tsubst_non_call_postfix_expression (op1, args, complain, |
b3445994 | 10772 | in_decl); |
fc2b8477 MM |
10773 | if (TREE_CODE (op1) == LABEL_DECL) |
10774 | return finish_label_address_expr (DECL_NAME (op1)); | |
ee76b931 MM |
10775 | return build_x_unary_op (ADDR_EXPR, op1); |
10776 | ||
cc23546e JO |
10777 | case PLUS_EXPR: |
10778 | case MINUS_EXPR: | |
10779 | case MULT_EXPR: | |
10780 | case TRUNC_DIV_EXPR: | |
10781 | case CEIL_DIV_EXPR: | |
10782 | case FLOOR_DIV_EXPR: | |
10783 | case ROUND_DIV_EXPR: | |
10784 | case EXACT_DIV_EXPR: | |
10785 | case BIT_AND_EXPR: | |
cc23546e JO |
10786 | case BIT_IOR_EXPR: |
10787 | case BIT_XOR_EXPR: | |
10788 | case TRUNC_MOD_EXPR: | |
10789 | case FLOOR_MOD_EXPR: | |
10790 | case TRUTH_ANDIF_EXPR: | |
10791 | case TRUTH_ORIF_EXPR: | |
10792 | case TRUTH_AND_EXPR: | |
10793 | case TRUTH_OR_EXPR: | |
10794 | case RSHIFT_EXPR: | |
10795 | case LSHIFT_EXPR: | |
10796 | case RROTATE_EXPR: | |
10797 | case LROTATE_EXPR: | |
10798 | case EQ_EXPR: | |
10799 | case NE_EXPR: | |
10800 | case MAX_EXPR: | |
10801 | case MIN_EXPR: | |
10802 | case LE_EXPR: | |
10803 | case GE_EXPR: | |
10804 | case LT_EXPR: | |
10805 | case GT_EXPR: | |
10806 | case MEMBER_REF: | |
b3445994 | 10807 | case DOTSTAR_EXPR: |
cc23546e | 10808 | return build_x_binary_op |
c8094d83 | 10809 | (TREE_CODE (t), |
b3445994 | 10810 | RECUR (TREE_OPERAND (t, 0)), |
2a67bec2 ILT |
10811 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) |
10812 | ? ERROR_MARK | |
10813 | : TREE_CODE (TREE_OPERAND (t, 0))), | |
ec835fb2 | 10814 | RECUR (TREE_OPERAND (t, 1)), |
2a67bec2 ILT |
10815 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) |
10816 | ? ERROR_MARK | |
10817 | : TREE_CODE (TREE_OPERAND (t, 1))), | |
ec835fb2 | 10818 | /*overloaded_p=*/NULL); |
cc23546e JO |
10819 | |
10820 | case SCOPE_REF: | |
ee76b931 MM |
10821 | return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, |
10822 | /*address_p=*/false); | |
cc23546e | 10823 | case ARRAY_REF: |
b3445994 MM |
10824 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
10825 | args, complain, in_decl); | |
2a67bec2 ILT |
10826 | return build_x_binary_op (ARRAY_REF, op1, |
10827 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) | |
10828 | ? ERROR_MARK | |
10829 | : TREE_CODE (TREE_OPERAND (t, 0))), | |
10830 | RECUR (TREE_OPERAND (t, 1)), | |
10831 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) | |
10832 | ? ERROR_MARK | |
10833 | : TREE_CODE (TREE_OPERAND (t, 1))), | |
d8987adb | 10834 | /*overloaded_p=*/NULL); |
c8094d83 | 10835 | |
cc23546e | 10836 | case SIZEOF_EXPR: |
5d80a306 DG |
10837 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) |
10838 | { | |
10839 | /* We only want to compute the number of arguments. */ | |
10840 | tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args, | |
10841 | complain, in_decl); | |
4745e4eb JJ |
10842 | if (expanded == error_mark_node) |
10843 | return error_mark_node; | |
5d80a306 DG |
10844 | return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded)); |
10845 | } | |
10846 | /* Fall through */ | |
10847 | ||
cc23546e | 10848 | case ALIGNOF_EXPR: |
d17811fd MM |
10849 | op1 = TREE_OPERAND (t, 0); |
10850 | if (!args) | |
10851 | { | |
10852 | /* When there are no ARGS, we are trying to evaluate a | |
10853 | non-dependent expression from the parser. Trying to do | |
10854 | the substitutions may not work. */ | |
10855 | if (!TYPE_P (op1)) | |
10856 | op1 = TREE_TYPE (op1); | |
10857 | } | |
10858 | else | |
10859 | { | |
10860 | ++skip_evaluation; | |
015c2c66 MM |
10861 | op1 = tsubst_copy_and_build (op1, args, complain, in_decl, |
10862 | /*function_p=*/false, | |
10863 | /*integral_constant_expression_p=*/false); | |
d17811fd MM |
10864 | --skip_evaluation; |
10865 | } | |
7a18b933 NS |
10866 | if (TYPE_P (op1)) |
10867 | return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true); | |
d17811fd | 10868 | else |
7a18b933 | 10869 | return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t)); |
cc23546e JO |
10870 | |
10871 | case MODOP_EXPR: | |
e4c2c34b JM |
10872 | { |
10873 | tree r = build_x_modify_expr | |
10874 | (RECUR (TREE_OPERAND (t, 0)), | |
10875 | TREE_CODE (TREE_OPERAND (t, 1)), | |
10876 | RECUR (TREE_OPERAND (t, 2))); | |
bcf9a914 JM |
10877 | /* TREE_NO_WARNING must be set if either the expression was |
10878 | parenthesized or it uses an operator such as >>= rather | |
10879 | than plain assignment. In the former case, it was already | |
10880 | set and must be copied. In the latter case, | |
10881 | build_x_modify_expr sets it and it must not be reset | |
10882 | here. */ | |
10883 | if (TREE_NO_WARNING (t)) | |
10884 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); | |
e4c2c34b JM |
10885 | return r; |
10886 | } | |
cc23546e JO |
10887 | |
10888 | case ARROW_EXPR: | |
b3445994 MM |
10889 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
10890 | args, complain, in_decl); | |
ee76b931 MM |
10891 | /* Remember that there was a reference to this entity. */ |
10892 | if (DECL_P (op1)) | |
10893 | mark_used (op1); | |
10894 | return build_x_arrow (op1); | |
cc23546e JO |
10895 | |
10896 | case NEW_EXPR: | |
268127ce DG |
10897 | { |
10898 | tree init = RECUR (TREE_OPERAND (t, 3)); | |
10899 | ||
10900 | if (TREE_OPERAND (t, 3) && !init) | |
10901 | /* If there was an initializer in the the original tree, but | |
10902 | it instantiated to an empty list, then we should pass on | |
10903 | VOID_ZERO_NODE to tell build_new that it was an empty | |
10904 | initializer () rather than no initializer. This can only | |
10905 | happen when the initializer is a pack expansion whose | |
10906 | parameter packs are of length zero. */ | |
10907 | init = void_zero_node; | |
10908 | ||
10909 | return build_new | |
10910 | (RECUR (TREE_OPERAND (t, 0)), | |
10911 | RECUR (TREE_OPERAND (t, 1)), | |
10912 | RECUR (TREE_OPERAND (t, 2)), | |
10913 | init, | |
10914 | NEW_EXPR_USE_GLOBAL (t)); | |
10915 | } | |
cc23546e JO |
10916 | |
10917 | case DELETE_EXPR: | |
10918 | return delete_sanity | |
b3445994 MM |
10919 | (RECUR (TREE_OPERAND (t, 0)), |
10920 | RECUR (TREE_OPERAND (t, 1)), | |
cc23546e JO |
10921 | DELETE_EXPR_USE_VEC (t), |
10922 | DELETE_EXPR_USE_GLOBAL (t)); | |
10923 | ||
10924 | case COMPOUND_EXPR: | |
b3445994 MM |
10925 | return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)), |
10926 | RECUR (TREE_OPERAND (t, 1))); | |
cc23546e | 10927 | |
ee76b931 | 10928 | case CALL_EXPR: |
cc23546e | 10929 | { |
ee76b931 MM |
10930 | tree function; |
10931 | tree call_args; | |
ee76b931 | 10932 | bool qualified_p; |
ee935db4 | 10933 | bool koenig_p; |
ee76b931 | 10934 | |
5039610b | 10935 | function = CALL_EXPR_FN (t); |
6d80c4b9 MM |
10936 | /* When we parsed the expression, we determined whether or |
10937 | not Koenig lookup should be performed. */ | |
10938 | koenig_p = KOENIG_LOOKUP_P (t); | |
ee76b931 | 10939 | if (TREE_CODE (function) == SCOPE_REF) |
cc23546e | 10940 | { |
ee76b931 MM |
10941 | qualified_p = true; |
10942 | function = tsubst_qualified_id (function, args, complain, in_decl, | |
c8094d83 | 10943 | /*done=*/false, |
ee76b931 | 10944 | /*address_p=*/false); |
cc23546e | 10945 | } |
ee76b931 | 10946 | else |
cc23546e | 10947 | { |
61e71a9e NS |
10948 | if (TREE_CODE (function) == COMPONENT_REF) |
10949 | { | |
10950 | tree op = TREE_OPERAND (function, 1); | |
10951 | ||
10952 | qualified_p = (TREE_CODE (op) == SCOPE_REF | |
10953 | || (BASELINK_P (op) | |
10954 | && BASELINK_QUALIFIED_P (op))); | |
10955 | } | |
10956 | else | |
10957 | qualified_p = false; | |
3db45ab5 | 10958 | |
c8094d83 | 10959 | function = tsubst_copy_and_build (function, args, complain, |
b3445994 | 10960 | in_decl, |
015c2c66 MM |
10961 | !qualified_p, |
10962 | integral_constant_expression_p); | |
10963 | ||
6d80c4b9 MM |
10964 | if (BASELINK_P (function)) |
10965 | qualified_p = true; | |
cc23546e | 10966 | } |
cc23546e | 10967 | |
5039610b SL |
10968 | /* FIXME: Rewrite this so as not to construct an arglist. */ |
10969 | call_args = RECUR (CALL_EXPR_ARGS (t)); | |
676e33ca MM |
10970 | |
10971 | /* We do not perform argument-dependent lookup if normal | |
10972 | lookup finds a non-function, in accordance with the | |
10973 | expected resolution of DR 218. */ | |
ee935db4 | 10974 | if (koenig_p |
44370687 MM |
10975 | && ((is_overloaded_fn (function) |
10976 | /* If lookup found a member function, the Koenig lookup is | |
10977 | not appropriate, even if an unqualified-name was used | |
10978 | to denote the function. */ | |
10979 | && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) | |
b3445994 | 10980 | || TREE_CODE (function) == IDENTIFIER_NODE)) |
6d80c4b9 MM |
10981 | function = perform_koenig_lookup (function, call_args); |
10982 | ||
10983 | if (TREE_CODE (function) == IDENTIFIER_NODE) | |
b3445994 | 10984 | { |
6d80c4b9 MM |
10985 | unqualified_name_lookup_error (function); |
10986 | return error_mark_node; | |
b3445994 MM |
10987 | } |
10988 | ||
10989 | /* Remember that there was a reference to this entity. */ | |
10990 | if (DECL_P (function)) | |
10991 | mark_used (function); | |
10992 | ||
d17811fd MM |
10993 | if (TREE_CODE (function) == OFFSET_REF) |
10994 | return build_offset_ref_call_from_tree (function, call_args); | |
10995 | if (TREE_CODE (function) == COMPONENT_REF) | |
9f880ef9 MM |
10996 | { |
10997 | if (!BASELINK_P (TREE_OPERAND (function, 1))) | |
10998 | return finish_call_expr (function, call_args, | |
10999 | /*disallow_virtual=*/false, | |
11000 | /*koenig_p=*/false); | |
11001 | else | |
c8094d83 | 11002 | return (build_new_method_call |
9f880ef9 MM |
11003 | (TREE_OPERAND (function, 0), |
11004 | TREE_OPERAND (function, 1), | |
c8094d83 | 11005 | call_args, NULL_TREE, |
63c9a190 MM |
11006 | qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL, |
11007 | /*fn_p=*/NULL)); | |
9f880ef9 | 11008 | } |
c8094d83 | 11009 | return finish_call_expr (function, call_args, |
6d80c4b9 MM |
11010 | /*disallow_virtual=*/qualified_p, |
11011 | koenig_p); | |
cc23546e JO |
11012 | } |
11013 | ||
11014 | case COND_EXPR: | |
11015 | return build_x_conditional_expr | |
b3445994 MM |
11016 | (RECUR (TREE_OPERAND (t, 0)), |
11017 | RECUR (TREE_OPERAND (t, 1)), | |
11018 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
11019 | |
11020 | case PSEUDO_DTOR_EXPR: | |
c8094d83 | 11021 | return finish_pseudo_destructor_expr |
b3445994 MM |
11022 | (RECUR (TREE_OPERAND (t, 0)), |
11023 | RECUR (TREE_OPERAND (t, 1)), | |
11024 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
11025 | |
11026 | case TREE_LIST: | |
11027 | { | |
11028 | tree purpose, value, chain; | |
11029 | ||
11030 | if (t == void_list_node) | |
11031 | return t; | |
11032 | ||
5d80a306 DG |
11033 | if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t))) |
11034 | || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))) | |
11035 | { | |
11036 | /* We have pack expansions, so expand those and | |
11037 | create a new list out of it. */ | |
11038 | tree purposevec = NULL_TREE; | |
11039 | tree valuevec = NULL_TREE; | |
11040 | tree chain; | |
11041 | int i, len = -1; | |
11042 | ||
11043 | /* Expand the argument expressions. */ | |
11044 | if (TREE_PURPOSE (t)) | |
11045 | purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args, | |
11046 | complain, in_decl); | |
11047 | if (TREE_VALUE (t)) | |
11048 | valuevec = tsubst_pack_expansion (TREE_VALUE (t), args, | |
11049 | complain, in_decl); | |
11050 | ||
11051 | /* Build the rest of the list. */ | |
11052 | chain = TREE_CHAIN (t); | |
11053 | if (chain && chain != void_type_node) | |
11054 | chain = RECUR (chain); | |
11055 | ||
11056 | /* Determine the number of arguments. */ | |
11057 | if (purposevec && TREE_CODE (purposevec) == TREE_VEC) | |
11058 | { | |
11059 | len = TREE_VEC_LENGTH (purposevec); | |
11060 | gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec)); | |
11061 | } | |
11062 | else if (TREE_CODE (valuevec) == TREE_VEC) | |
11063 | len = TREE_VEC_LENGTH (valuevec); | |
11064 | else | |
11065 | { | |
11066 | /* Since we only performed a partial substitution into | |
11067 | the argument pack, we only return a single list | |
11068 | node. */ | |
11069 | if (purposevec == TREE_PURPOSE (t) | |
11070 | && valuevec == TREE_VALUE (t) | |
11071 | && chain == TREE_CHAIN (t)) | |
11072 | return t; | |
11073 | ||
11074 | return tree_cons (purposevec, valuevec, chain); | |
11075 | } | |
11076 | ||
11077 | /* Convert the argument vectors into a TREE_LIST */ | |
11078 | i = len; | |
11079 | while (i > 0) | |
11080 | { | |
11081 | /* Grab the Ith values. */ | |
11082 | i--; | |
11083 | purpose = purposevec ? TREE_VEC_ELT (purposevec, i) | |
11084 | : NULL_TREE; | |
11085 | value | |
11086 | = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i)) | |
11087 | : NULL_TREE; | |
11088 | ||
11089 | /* Build the list (backwards). */ | |
11090 | chain = tree_cons (purpose, value, chain); | |
11091 | } | |
11092 | ||
11093 | return chain; | |
11094 | } | |
11095 | ||
cc23546e JO |
11096 | purpose = TREE_PURPOSE (t); |
11097 | if (purpose) | |
b3445994 | 11098 | purpose = RECUR (purpose); |
cc23546e JO |
11099 | value = TREE_VALUE (t); |
11100 | if (value) | |
b3445994 | 11101 | value = RECUR (value); |
cc23546e JO |
11102 | chain = TREE_CHAIN (t); |
11103 | if (chain && chain != void_type_node) | |
b3445994 | 11104 | chain = RECUR (chain); |
cc23546e JO |
11105 | if (purpose == TREE_PURPOSE (t) |
11106 | && value == TREE_VALUE (t) | |
11107 | && chain == TREE_CHAIN (t)) | |
11108 | return t; | |
11109 | return tree_cons (purpose, value, chain); | |
11110 | } | |
11111 | ||
11112 | case COMPONENT_REF: | |
11113 | { | |
ee76b931 | 11114 | tree object; |
3897c0aa | 11115 | tree object_type; |
ee76b931 MM |
11116 | tree member; |
11117 | ||
b3445994 MM |
11118 | object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
11119 | args, complain, in_decl); | |
ee76b931 MM |
11120 | /* Remember that there was a reference to this entity. */ |
11121 | if (DECL_P (object)) | |
11122 | mark_used (object); | |
3897c0aa | 11123 | object_type = TREE_TYPE (object); |
ee76b931 MM |
11124 | |
11125 | member = TREE_OPERAND (t, 1); | |
11126 | if (BASELINK_P (member)) | |
c8094d83 | 11127 | member = tsubst_baselink (member, |
ee76b931 MM |
11128 | non_reference (TREE_TYPE (object)), |
11129 | args, complain, in_decl); | |
11130 | else | |
11131 | member = tsubst_copy (member, args, complain, in_decl); | |
bad1f462 KL |
11132 | if (member == error_mark_node) |
11133 | return error_mark_node; | |
3897c0aa MM |
11134 | |
11135 | if (object_type && !CLASS_TYPE_P (object_type)) | |
cc23546e | 11136 | { |
3d9b2eb6 JJ |
11137 | if (SCALAR_TYPE_P (object_type)) |
11138 | { | |
11139 | tree s = NULL_TREE; | |
11140 | tree dtor = member; | |
11141 | ||
11142 | if (TREE_CODE (dtor) == SCOPE_REF) | |
11143 | { | |
11144 | s = TREE_OPERAND (dtor, 0); | |
11145 | dtor = TREE_OPERAND (dtor, 1); | |
11146 | } | |
11147 | if (TREE_CODE (dtor) == BIT_NOT_EXPR) | |
11148 | { | |
11149 | dtor = TREE_OPERAND (dtor, 0); | |
11150 | if (TYPE_P (dtor)) | |
11151 | return finish_pseudo_destructor_expr (object, s, dtor); | |
11152 | } | |
11153 | } | |
cc23546e JO |
11154 | } |
11155 | else if (TREE_CODE (member) == SCOPE_REF | |
11156 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) | |
11157 | { | |
11158 | tree tmpl; | |
11159 | tree args; | |
c8094d83 | 11160 | |
cc23546e JO |
11161 | /* Lookup the template functions now that we know what the |
11162 | scope is. */ | |
11163 | tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); | |
11164 | args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); | |
c8094d83 | 11165 | member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl, |
12483c9f NS |
11166 | /*is_type_p=*/false, |
11167 | /*complain=*/false); | |
cc23546e | 11168 | if (BASELINK_P (member)) |
44370687 | 11169 | { |
c8094d83 | 11170 | BASELINK_FUNCTIONS (member) |
44370687 MM |
11171 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), |
11172 | args); | |
c8094d83 MS |
11173 | member = (adjust_result_of_qualified_name_lookup |
11174 | (member, BINFO_TYPE (BASELINK_BINFO (member)), | |
3897c0aa | 11175 | object_type)); |
44370687 | 11176 | } |
cc23546e JO |
11177 | else |
11178 | { | |
3897c0aa | 11179 | qualified_name_lookup_error (object_type, tmpl, member); |
cc23546e JO |
11180 | return error_mark_node; |
11181 | } | |
11182 | } | |
2436b51f MM |
11183 | else if (TREE_CODE (member) == SCOPE_REF |
11184 | && !CLASS_TYPE_P (TREE_OPERAND (member, 0)) | |
11185 | && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL) | |
11186 | { | |
11187 | if (complain & tf_error) | |
11188 | { | |
11189 | if (TYPE_P (TREE_OPERAND (member, 0))) | |
c8094d83 | 11190 | error ("%qT is not a class or namespace", |
2436b51f MM |
11191 | TREE_OPERAND (member, 0)); |
11192 | else | |
c8094d83 | 11193 | error ("%qD is not a class or namespace", |
2436b51f MM |
11194 | TREE_OPERAND (member, 0)); |
11195 | } | |
11196 | return error_mark_node; | |
11197 | } | |
a3f10e50 NS |
11198 | else if (TREE_CODE (member) == FIELD_DECL) |
11199 | return finish_non_static_data_member (member, object, NULL_TREE); | |
cc23546e | 11200 | |
02ed62dd MM |
11201 | return finish_class_member_access_expr (object, member, |
11202 | /*template_p=*/false); | |
cc23546e JO |
11203 | } |
11204 | ||
11205 | case THROW_EXPR: | |
11206 | return build_throw | |
b3445994 | 11207 | (RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
11208 | |
11209 | case CONSTRUCTOR: | |
11210 | { | |
4038c495 GB |
11211 | VEC(constructor_elt,gc) *n; |
11212 | constructor_elt *ce; | |
11213 | unsigned HOST_WIDE_INT idx; | |
cc23546e | 11214 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
4038c495 | 11215 | bool process_index_p; |
5d80a306 DG |
11216 | int newlen; |
11217 | bool need_copy_p = false; | |
cc23546e | 11218 | |
a97728cf VR |
11219 | if (type == error_mark_node) |
11220 | return error_mark_node; | |
11221 | ||
cc23546e JO |
11222 | /* digest_init will do the wrong thing if we let it. */ |
11223 | if (type && TYPE_PTRMEMFUNC_P (type)) | |
11224 | return t; | |
11225 | ||
4038c495 | 11226 | /* We do not want to process the index of aggregate |
cc23546e JO |
11227 | initializers as they are identifier nodes which will be |
11228 | looked up by digest_init. */ | |
4038c495 | 11229 | process_index_p = !(type && IS_AGGR_TYPE (type)); |
c8094d83 | 11230 | |
4038c495 | 11231 | n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t)); |
5d80a306 | 11232 | newlen = VEC_length (constructor_elt, n); |
4038c495 GB |
11233 | for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++) |
11234 | { | |
11235 | if (ce->index && process_index_p) | |
11236 | ce->index = RECUR (ce->index); | |
5d80a306 DG |
11237 | |
11238 | if (PACK_EXPANSION_P (ce->value)) | |
11239 | { | |
11240 | /* Substitute into the pack expansion. */ | |
11241 | ce->value = tsubst_pack_expansion (ce->value, args, complain, | |
11242 | in_decl); | |
11243 | ||
4745e4eb JJ |
11244 | if (ce->value == error_mark_node) |
11245 | ; | |
11246 | else if (TREE_VEC_LENGTH (ce->value) == 1) | |
5d80a306 DG |
11247 | /* Just move the argument into place. */ |
11248 | ce->value = TREE_VEC_ELT (ce->value, 0); | |
11249 | else | |
11250 | { | |
11251 | /* Update the length of the final CONSTRUCTOR | |
11252 | arguments vector, and note that we will need to | |
11253 | copy.*/ | |
11254 | newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1; | |
11255 | need_copy_p = true; | |
11256 | } | |
11257 | } | |
11258 | else | |
11259 | ce->value = RECUR (ce->value); | |
cc23546e | 11260 | } |
c8094d83 | 11261 | |
5d80a306 DG |
11262 | if (need_copy_p) |
11263 | { | |
11264 | VEC(constructor_elt,gc) *old_n = n; | |
11265 | ||
11266 | n = VEC_alloc (constructor_elt, gc, newlen); | |
11267 | for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce); | |
11268 | idx++) | |
11269 | { | |
11270 | if (TREE_CODE (ce->value) == TREE_VEC) | |
11271 | { | |
11272 | int i, len = TREE_VEC_LENGTH (ce->value); | |
11273 | for (i = 0; i < len; ++i) | |
11274 | CONSTRUCTOR_APPEND_ELT (n, 0, | |
11275 | TREE_VEC_ELT (ce->value, i)); | |
11276 | } | |
11277 | else | |
11278 | CONSTRUCTOR_APPEND_ELT (n, 0, ce->value); | |
11279 | } | |
11280 | } | |
11281 | ||
05b22df9 MM |
11282 | if (TREE_HAS_CONSTRUCTOR (t)) |
11283 | return finish_compound_literal (type, n); | |
cc23546e | 11284 | |
05b22df9 | 11285 | return build_constructor (NULL_TREE, n); |
cc23546e JO |
11286 | } |
11287 | ||
11288 | case TYPEID_EXPR: | |
11289 | { | |
b3445994 | 11290 | tree operand_0 = RECUR (TREE_OPERAND (t, 0)); |
cc23546e JO |
11291 | if (TYPE_P (operand_0)) |
11292 | return get_typeid (operand_0); | |
11293 | return build_typeid (operand_0); | |
11294 | } | |
11295 | ||
cc23546e | 11296 | case VAR_DECL: |
db24eb1f NS |
11297 | if (!args) |
11298 | return t; | |
11299 | /* Fall through */ | |
c8094d83 | 11300 | |
db24eb1f NS |
11301 | case PARM_DECL: |
11302 | { | |
11303 | tree r = tsubst_copy (t, args, complain, in_decl); | |
c8094d83 | 11304 | |
db24eb1f NS |
11305 | if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE) |
11306 | /* If the original type was a reference, we'll be wrapped in | |
11307 | the appropriate INDIRECT_REF. */ | |
11308 | r = convert_from_reference (r); | |
11309 | return r; | |
11310 | } | |
cc23546e JO |
11311 | |
11312 | case VA_ARG_EXPR: | |
b3445994 | 11313 | return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)), |
c8094d83 | 11314 | tsubst_copy (TREE_TYPE (t), args, complain, |
b3445994 | 11315 | in_decl)); |
cc23546e | 11316 | |
4bceb077 | 11317 | case OFFSETOF_EXPR: |
c291f8b1 | 11318 | return finish_offsetof (RECUR (TREE_OPERAND (t, 0))); |
4bceb077 | 11319 | |
cb68ec50 PC |
11320 | case TRAIT_EXPR: |
11321 | { | |
11322 | tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args, | |
11323 | complain, in_decl); | |
11324 | ||
11325 | tree type2 = TRAIT_EXPR_TYPE2 (t); | |
11326 | if (type2) | |
11327 | type2 = tsubst_copy (type2, args, complain, in_decl); | |
11328 | ||
11329 | return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2); | |
11330 | } | |
11331 | ||
0fe0caa6 RH |
11332 | case STMT_EXPR: |
11333 | { | |
11334 | tree old_stmt_expr = cur_stmt_expr; | |
11335 | tree stmt_expr = begin_stmt_expr (); | |
11336 | ||
11337 | cur_stmt_expr = stmt_expr; | |
015c2c66 MM |
11338 | tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl, |
11339 | integral_constant_expression_p); | |
0fe0caa6 RH |
11340 | stmt_expr = finish_stmt_expr (stmt_expr, false); |
11341 | cur_stmt_expr = old_stmt_expr; | |
11342 | ||
11343 | return stmt_expr; | |
11344 | } | |
11345 | ||
a5201a91 MM |
11346 | case CONST_DECL: |
11347 | t = tsubst_copy (t, args, complain, in_decl); | |
11348 | /* As in finish_id_expression, we resolve enumeration constants | |
11349 | to their underlying values. */ | |
11350 | if (TREE_CODE (t) == CONST_DECL) | |
6193b8b7 DJ |
11351 | { |
11352 | used_types_insert (TREE_TYPE (t)); | |
11353 | return DECL_INITIAL (t); | |
11354 | } | |
a5201a91 MM |
11355 | return t; |
11356 | ||
cc23546e | 11357 | default: |
e58a9aa1 ZL |
11358 | /* Handle Objective-C++ constructs, if appropriate. */ |
11359 | { | |
11360 | tree subst | |
11361 | = objcp_tsubst_copy_and_build (t, args, complain, | |
11362 | in_decl, /*function_p=*/false); | |
11363 | if (subst) | |
11364 | return subst; | |
11365 | } | |
cc23546e JO |
11366 | return tsubst_copy (t, args, complain, in_decl); |
11367 | } | |
b3445994 MM |
11368 | |
11369 | #undef RECUR | |
cc23546e JO |
11370 | } |
11371 | ||
3e4a3562 NS |
11372 | /* Verify that the instantiated ARGS are valid. For type arguments, |
11373 | make sure that the type's linkage is ok. For non-type arguments, | |
34cd5ae7 | 11374 | make sure they are constants if they are integral or enumerations. |
9bcb9aae | 11375 | Emit an error under control of COMPLAIN, and return TRUE on error. */ |
3e4a3562 NS |
11376 | |
11377 | static bool | |
3a978d72 | 11378 | check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) |
3e4a3562 NS |
11379 | { |
11380 | int ix, len = DECL_NTPARMS (tmpl); | |
11381 | bool result = false; | |
11382 | ||
11383 | for (ix = 0; ix != len; ix++) | |
11384 | { | |
11385 | tree t = TREE_VEC_ELT (args, ix); | |
c8094d83 | 11386 | |
3e4a3562 NS |
11387 | if (TYPE_P (t)) |
11388 | { | |
11389 | /* [basic.link]: A name with no linkage (notably, the name | |
11390 | of a class or enumeration declared in a local scope) | |
11391 | shall not be used to declare an entity with linkage. | |
11392 | This implies that names with no linkage cannot be used as | |
11393 | template arguments. */ | |
4684cd27 | 11394 | tree nt = no_linkage_check (t, /*relaxed_p=*/false); |
3e4a3562 NS |
11395 | |
11396 | if (nt) | |
11397 | { | |
cb807ba3 | 11398 | /* DR 488 makes use of a type with no linkage cause |
3db45ab5 | 11399 | type deduction to fail. */ |
2010cdcd MM |
11400 | if (complain & tf_error) |
11401 | { | |
11402 | if (TYPE_ANONYMOUS_P (nt)) | |
11403 | error ("%qT is/uses anonymous type", t); | |
11404 | else | |
9df540a8 | 11405 | error ("template argument for %qD uses local type %qT", |
3db45ab5 | 11406 | tmpl, t); |
2010cdcd | 11407 | } |
3e4a3562 NS |
11408 | result = true; |
11409 | } | |
11410 | /* In order to avoid all sorts of complications, we do not | |
11411 | allow variably-modified types as template arguments. */ | |
5377d5ba | 11412 | else if (variably_modified_type_p (t, NULL_TREE)) |
3e4a3562 NS |
11413 | { |
11414 | if (complain & tf_error) | |
0f51ccfc | 11415 | error ("%qT is a variably modified type", t); |
3e4a3562 NS |
11416 | result = true; |
11417 | } | |
11418 | } | |
11419 | /* A non-type argument of integral or enumerated type must be a | |
11420 | constant. */ | |
11421 | else if (TREE_TYPE (t) | |
11422 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) | |
11423 | && !TREE_CONSTANT (t)) | |
11424 | { | |
11425 | if (complain & tf_error) | |
0f51ccfc | 11426 | error ("integral expression %qE is not constant", t); |
3e4a3562 NS |
11427 | result = true; |
11428 | } | |
11429 | } | |
2010cdcd | 11430 | if (result && (complain & tf_error)) |
0f51ccfc | 11431 | error (" trying to instantiate %qD", tmpl); |
3e4a3562 NS |
11432 | return result; |
11433 | } | |
11434 | ||
6ba89f8e | 11435 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
11436 | the template arguments in TARG_PTR. */ |
11437 | ||
5566b478 | 11438 | tree |
3a978d72 | 11439 | instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain) |
8d08fdba | 11440 | { |
5566b478 | 11441 | tree fndecl; |
36a117a5 MM |
11442 | tree gen_tmpl; |
11443 | tree spec; | |
a95799ec | 11444 | HOST_WIDE_INT saved_processing_template_decl; |
5566b478 | 11445 | |
27fafc8d JM |
11446 | if (tmpl == error_mark_node) |
11447 | return error_mark_node; | |
11448 | ||
50bc768d | 11449 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
386b8a85 | 11450 | |
db9b2174 MM |
11451 | /* If this function is a clone, handle it specially. */ |
11452 | if (DECL_CLONED_FUNCTION_P (tmpl)) | |
3ad97789 | 11453 | { |
a30f62e0 | 11454 | tree spec; |
3ad97789 | 11455 | tree clone; |
c8094d83 | 11456 | |
a30f62e0 MM |
11457 | spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr, |
11458 | complain); | |
11459 | if (spec == error_mark_node) | |
11460 | return error_mark_node; | |
11461 | ||
c6002625 | 11462 | /* Look for the clone. */ |
4684cd27 | 11463 | FOR_EACH_CLONE (clone, spec) |
3ad97789 NS |
11464 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) |
11465 | return clone; | |
11466 | /* We should always have found the clone by now. */ | |
315fb5db | 11467 | gcc_unreachable (); |
3ad97789 NS |
11468 | return NULL_TREE; |
11469 | } | |
c8094d83 | 11470 | |
36a117a5 | 11471 | /* Check to see if we already have this specialization. */ |
c8094d83 | 11472 | spec = retrieve_specialization (tmpl, targ_ptr, |
c7222c02 | 11473 | /*class_specializations_p=*/false); |
36a117a5 MM |
11474 | if (spec != NULL_TREE) |
11475 | return spec; | |
11476 | ||
f9a7ae04 MM |
11477 | gen_tmpl = most_general_template (tmpl); |
11478 | if (tmpl != gen_tmpl) | |
386b8a85 | 11479 | { |
36a117a5 MM |
11480 | /* The TMPL is a partial instantiation. To get a full set of |
11481 | arguments we must add the arguments used to perform the | |
11482 | partial instantiation. */ | |
11483 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
11484 | targ_ptr); | |
36a117a5 MM |
11485 | |
11486 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
11487 | spec = retrieve_specialization (gen_tmpl, targ_ptr, |
11488 | /*class_specializations_p=*/false); | |
75650646 MM |
11489 | if (spec != NULL_TREE) |
11490 | return spec; | |
386b8a85 JM |
11491 | } |
11492 | ||
3e4a3562 NS |
11493 | if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), |
11494 | complain)) | |
11495 | return error_mark_node; | |
c8094d83 | 11496 | |
2b907f5c KL |
11497 | /* We are building a FUNCTION_DECL, during which the access of its |
11498 | parameters and return types have to be checked. However this | |
11499 | FUNCTION_DECL which is the desired context for access checking | |
11500 | is not built yet. We solve this chicken-and-egg problem by | |
11501 | deferring all checks until we have the FUNCTION_DECL. */ | |
11502 | push_deferring_access_checks (dk_deferred); | |
5c74d5b0 | 11503 | |
a95799ec MM |
11504 | /* Although PROCESSING_TEMPLATE_DECL may be true at this point |
11505 | (because, for example, we have encountered a non-dependent | |
a1d08991 MM |
11506 | function call in the body of a template function and must now |
11507 | determine which of several overloaded functions will be called), | |
11508 | within the instantiation itself we are not processing a | |
11509 | template. */ | |
a95799ec MM |
11510 | saved_processing_template_decl = processing_template_decl; |
11511 | processing_template_decl = 0; | |
11512 | /* Substitute template parameters to obtain the specialization. */ | |
17aec3eb | 11513 | fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), |
3e4a3562 | 11514 | targ_ptr, complain, gen_tmpl); |
a95799ec | 11515 | processing_template_decl = saved_processing_template_decl; |
6b6b60af MM |
11516 | if (fndecl == error_mark_node) |
11517 | return error_mark_node; | |
5c74d5b0 | 11518 | |
2b907f5c KL |
11519 | /* Now we know the specialization, compute access previously |
11520 | deferred. */ | |
11521 | push_access_scope (fndecl); | |
11522 | perform_deferred_access_checks (); | |
11523 | pop_access_scope (fndecl); | |
11524 | pop_deferring_access_checks (); | |
5c74d5b0 | 11525 | |
36a117a5 MM |
11526 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
11527 | template, not the most general template. */ | |
11528 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 11529 | |
94350948 | 11530 | /* If we've just instantiated the main entry point for a function, |
3ad97789 NS |
11531 | instantiate all the alternate entry points as well. We do this |
11532 | by cloning the instantiation of the main entry point, not by | |
11533 | instantiating the template clones. */ | |
11534 | if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl))) | |
11535 | clone_function_decl (fndecl, /*update_method_vec_p=*/0); | |
94350948 | 11536 | |
5566b478 | 11537 | return fndecl; |
8d08fdba | 11538 | } |
5566b478 | 11539 | |
4393e105 MM |
11540 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the |
11541 | arguments that are being used when calling it. TARGS is a vector | |
c8094d83 | 11542 | into which the deduced template arguments are placed. |
8d08fdba MS |
11543 | |
11544 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
11545 | all the types, and 1 for complete failure. An error message will be | |
11546 | printed only for an incomplete match. | |
11547 | ||
e5214479 JM |
11548 | If FN is a conversion operator, or we are trying to produce a specific |
11549 | specialization, RETURN_TYPE is the return type desired. | |
9f54c803 MM |
11550 | |
11551 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
11552 | template-id. | |
6467930b | 11553 | |
830bfa74 MM |
11554 | The parameter STRICT is one of: |
11555 | ||
c8094d83 | 11556 | DEDUCE_CALL: |
830bfa74 MM |
11557 | We are deducing arguments for a function call, as in |
11558 | [temp.deduct.call]. | |
11559 | ||
11560 | DEDUCE_CONV: | |
c8094d83 | 11561 | We are deducing arguments for a conversion function, as in |
830bfa74 MM |
11562 | [temp.deduct.conv]. |
11563 | ||
11564 | DEDUCE_EXACT: | |
62e4a758 NS |
11565 | We are deducing arguments when doing an explicit instantiation |
11566 | as in [temp.explicit], when determining an explicit specialization | |
11567 | as in [temp.expl.spec], or when taking the address of a function | |
a34d3336 | 11568 | template, as in [temp.deduct.funcaddr]. */ |
8d08fdba MS |
11569 | |
11570 | int | |
c8094d83 | 11571 | fn_type_unification (tree fn, |
0cbd7506 MS |
11572 | tree explicit_targs, |
11573 | tree targs, | |
11574 | tree args, | |
11575 | tree return_type, | |
30f86ec3 FJ |
11576 | unification_kind_t strict, |
11577 | int flags) | |
386b8a85 | 11578 | { |
4393e105 MM |
11579 | tree parms; |
11580 | tree fntype; | |
adecb3f4 | 11581 | int result; |
5d80a306 | 11582 | bool incomplete_argument_packs_p = false; |
386b8a85 | 11583 | |
50bc768d | 11584 | gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL); |
2b59fc25 | 11585 | |
4393e105 MM |
11586 | fntype = TREE_TYPE (fn); |
11587 | if (explicit_targs) | |
75650646 | 11588 | { |
4393e105 | 11589 | /* [temp.deduct] |
c8094d83 | 11590 | |
4393e105 MM |
11591 | The specified template arguments must match the template |
11592 | parameters in kind (i.e., type, nontype, template), and there | |
11593 | must not be more arguments than there are parameters; | |
11594 | otherwise type deduction fails. | |
11595 | ||
11596 | Nontype arguments must match the types of the corresponding | |
11597 | nontype template parameters, or must be convertible to the | |
11598 | types of the corresponding nontype parameters as specified in | |
11599 | _temp.arg.nontype_, otherwise type deduction fails. | |
11600 | ||
11601 | All references in the function type of the function template | |
11602 | to the corresponding template parameters are replaced by the | |
11603 | specified template argument values. If a substitution in a | |
11604 | template parameter or in the function type of the function | |
11605 | template results in an invalid type, type deduction fails. */ | |
5d80a306 DG |
11606 | tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn); |
11607 | int i, len = TREE_VEC_LENGTH (tparms); | |
4393e105 | 11608 | tree converted_args; |
5d80a306 | 11609 | bool incomplete = false; |
75650646 | 11610 | |
c8a7ed43 AO |
11611 | if (explicit_targs == error_mark_node) |
11612 | return 1; | |
11613 | ||
4393e105 | 11614 | converted_args |
5d80a306 | 11615 | = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none, |
e7e93965 MM |
11616 | /*require_all_args=*/false, |
11617 | /*use_default_args=*/false)); | |
4393e105 | 11618 | if (converted_args == error_mark_node) |
75650646 | 11619 | return 1; |
386b8a85 | 11620 | |
ffd49b19 | 11621 | /* Substitute the explicit args into the function type. This is |
0cbd7506 MS |
11622 | necessary so that, for instance, explicitly declared function |
11623 | arguments can match null pointed constants. If we were given | |
11624 | an incomplete set of explicit args, we must not do semantic | |
11625 | processing during substitution as we could create partial | |
11626 | instantiations. */ | |
5d80a306 DG |
11627 | for (i = 0; i < len; i++) |
11628 | { | |
11629 | tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
11630 | bool parameter_pack = false; | |
11631 | ||
11632 | /* Dig out the actual parm. */ | |
11633 | if (TREE_CODE (parm) == TYPE_DECL | |
11634 | || TREE_CODE (parm) == TEMPLATE_DECL) | |
11635 | { | |
11636 | parm = TREE_TYPE (parm); | |
11637 | parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm); | |
11638 | } | |
11639 | else if (TREE_CODE (parm) == PARM_DECL) | |
11640 | { | |
11641 | parm = DECL_INITIAL (parm); | |
11642 | parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm); | |
11643 | } | |
11644 | ||
11645 | if (parameter_pack) | |
11646 | { | |
11647 | int level, idx; | |
11648 | tree targ; | |
11649 | template_parm_level_and_index (parm, &level, &idx); | |
11650 | ||
11651 | /* Mark the argument pack as "incomplete". We could | |
11652 | still deduce more arguments during unification. */ | |
11653 | targ = TMPL_ARG (converted_args, level, idx); | |
b1d7b1c0 DG |
11654 | if (targ) |
11655 | { | |
11656 | ARGUMENT_PACK_INCOMPLETE_P(targ) = 1; | |
11657 | ARGUMENT_PACK_EXPLICIT_ARGS (targ) | |
11658 | = ARGUMENT_PACK_ARGS (targ); | |
11659 | } | |
5d80a306 DG |
11660 | |
11661 | /* We have some incomplete argument packs. */ | |
11662 | incomplete_argument_packs_p = true; | |
11663 | } | |
11664 | } | |
11665 | ||
11666 | if (incomplete_argument_packs_p) | |
11667 | /* Any substitution is guaranteed to be incomplete if there | |
11668 | are incomplete argument packs, because we can still deduce | |
11669 | more arguments. */ | |
11670 | incomplete = 1; | |
11671 | else | |
11672 | incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs); | |
11673 | ||
ffd49b19 | 11674 | processing_template_decl += incomplete; |
c2ea3a40 | 11675 | fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE); |
ffd49b19 | 11676 | processing_template_decl -= incomplete; |
c8094d83 | 11677 | |
4393e105 MM |
11678 | if (fntype == error_mark_node) |
11679 | return 1; | |
050367a3 | 11680 | |
4393e105 | 11681 | /* Place the explicitly specified arguments in TARGS. */ |
ffd49b19 | 11682 | for (i = NUM_TMPL_ARGS (converted_args); i--;) |
4393e105 | 11683 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); |
75650646 | 11684 | } |
c8094d83 | 11685 | |
e5214479 | 11686 | /* Never do unification on the 'this' parameter. */ |
ba139ba8 | 11687 | parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype)); |
c8094d83 | 11688 | |
8d3631f8 NS |
11689 | if (return_type) |
11690 | { | |
8d3631f8 NS |
11691 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); |
11692 | args = tree_cons (NULL_TREE, return_type, args); | |
4393e105 MM |
11693 | } |
11694 | ||
4393e105 MM |
11695 | /* We allow incomplete unification without an error message here |
11696 | because the standard doesn't seem to explicitly prohibit it. Our | |
11697 | callers must be ready to deal with unification failures in any | |
11698 | event. */ | |
c8094d83 | 11699 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
adecb3f4 | 11700 | targs, parms, args, /*subr=*/0, |
30f86ec3 | 11701 | strict, flags); |
adecb3f4 | 11702 | |
5d80a306 DG |
11703 | if (result == 0 && incomplete_argument_packs_p) |
11704 | { | |
11705 | int i, len = NUM_TMPL_ARGS (targs); | |
11706 | ||
11707 | /* Clear the "incomplete" flags on all argument packs. */ | |
11708 | for (i = 0; i < len; i++) | |
11709 | { | |
11710 | tree arg = TREE_VEC_ELT (targs, i); | |
11711 | if (ARGUMENT_PACK_P (arg)) | |
11712 | { | |
11713 | ARGUMENT_PACK_INCOMPLETE_P (arg) = 0; | |
11714 | ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE; | |
11715 | } | |
11716 | } | |
11717 | } | |
11718 | ||
4cf36211 DG |
11719 | /* Now that we have bindings for all of the template arguments, |
11720 | ensure that the arguments deduced for the template template | |
11721 | parameters have compatible template parameter lists. We cannot | |
11722 | check this property before we have deduced all template | |
11723 | arguments, because the template parameter types of a template | |
11724 | template parameter might depend on prior template parameters | |
11725 | deduced after the template template parameter. The following | |
11726 | ill-formed example illustrates this issue: | |
11727 | ||
11728 | template<typename T, template<T> class C> void f(C<5>, T); | |
11729 | ||
11730 | template<int N> struct X {}; | |
11731 | ||
11732 | void g() { | |
11733 | f(X<5>(), 5l); // error: template argument deduction fails | |
11734 | } | |
11735 | ||
11736 | The template parameter list of 'C' depends on the template type | |
11737 | parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to | |
11738 | 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the | |
11739 | time that we deduce 'C'. */ | |
11740 | if (result == 0 | |
11741 | && !template_template_parm_bindings_ok_p | |
11742 | (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs)) | |
11743 | return 1; | |
11744 | ||
c8094d83 | 11745 | if (result == 0) |
adecb3f4 | 11746 | /* All is well so far. Now, check: |
c8094d83 MS |
11747 | |
11748 | [temp.deduct] | |
11749 | ||
adecb3f4 MM |
11750 | When all template arguments have been deduced, all uses of |
11751 | template parameters in nondeduced contexts are replaced with | |
11752 | the corresponding deduced argument values. If the | |
11753 | substitution results in an invalid type, as described above, | |
11754 | type deduction fails. */ | |
c2ea3a40 | 11755 | if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE) |
adecb3f4 MM |
11756 | == error_mark_node) |
11757 | return 1; | |
11758 | ||
11759 | return result; | |
830bfa74 MM |
11760 | } |
11761 | ||
11762 | /* Adjust types before performing type deduction, as described in | |
11763 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
11764 | sections are symmetric. PARM is the type of a function parameter |
11765 | or the return type of the conversion function. ARG is the type of | |
11766 | the argument passed to the call, or the type of the value | |
8af2fec4 RY |
11767 | initialized with the result of the conversion function. |
11768 | ARG_EXPR is the original argument expression, which may be null. */ | |
386b8a85 | 11769 | |
62e4a758 | 11770 | static int |
c8094d83 | 11771 | maybe_adjust_types_for_deduction (unification_kind_t strict, |
0cbd7506 | 11772 | tree* parm, |
8af2fec4 RY |
11773 | tree* arg, |
11774 | tree arg_expr) | |
830bfa74 | 11775 | { |
62e4a758 | 11776 | int result = 0; |
c8094d83 | 11777 | |
830bfa74 MM |
11778 | switch (strict) |
11779 | { | |
11780 | case DEDUCE_CALL: | |
11781 | break; | |
11782 | ||
11783 | case DEDUCE_CONV: | |
11784 | { | |
4c7d0dff MM |
11785 | /* Swap PARM and ARG throughout the remainder of this |
11786 | function; the handling is precisely symmetric since PARM | |
11787 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
11788 | tree* temp = parm; |
11789 | parm = arg; | |
11790 | arg = temp; | |
11791 | break; | |
11792 | } | |
11793 | ||
11794 | case DEDUCE_EXACT: | |
11795 | /* There is nothing to do in this case. */ | |
62e4a758 | 11796 | return 0; |
830bfa74 MM |
11797 | |
11798 | default: | |
315fb5db | 11799 | gcc_unreachable (); |
830bfa74 MM |
11800 | } |
11801 | ||
11802 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
11803 | { | |
11804 | /* [temp.deduct.call] | |
c8094d83 | 11805 | |
830bfa74 | 11806 | If P is not a reference type: |
c8094d83 | 11807 | |
830bfa74 MM |
11808 | --If A is an array type, the pointer type produced by the |
11809 | array-to-pointer standard conversion (_conv.array_) is | |
11810 | used in place of A for type deduction; otherwise, | |
c8094d83 | 11811 | |
830bfa74 MM |
11812 | --If A is a function type, the pointer type produced by |
11813 | the function-to-pointer standard conversion | |
11814 | (_conv.func_) is used in place of A for type deduction; | |
11815 | otherwise, | |
c8094d83 | 11816 | |
830bfa74 MM |
11817 | --If A is a cv-qualified type, the top level |
11818 | cv-qualifiers of A's type are ignored for type | |
11819 | deduction. */ | |
11820 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
11821 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 11822 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
11823 | *arg = build_pointer_type (*arg); |
11824 | else | |
11825 | *arg = TYPE_MAIN_VARIANT (*arg); | |
11826 | } | |
c8094d83 | 11827 | |
8af2fec4 RY |
11828 | /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is |
11829 | of the form T&&, where T is a template parameter, and the argument | |
11830 | is an lvalue, T is deduced as A& */ | |
11831 | if (TREE_CODE (*parm) == REFERENCE_TYPE | |
11832 | && TYPE_REF_IS_RVALUE (*parm) | |
11833 | && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM | |
11834 | && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED | |
11835 | && arg_expr && real_lvalue_p (arg_expr)) | |
11836 | *arg = build_reference_type (*arg); | |
11837 | ||
830bfa74 | 11838 | /* [temp.deduct.call] |
c8094d83 | 11839 | |
830bfa74 MM |
11840 | If P is a cv-qualified type, the top level cv-qualifiers |
11841 | of P's type are ignored for type deduction. If P is a | |
11842 | reference type, the type referred to by P is used for | |
11843 | type deduction. */ | |
11844 | *parm = TYPE_MAIN_VARIANT (*parm); | |
11845 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
62e4a758 NS |
11846 | { |
11847 | *parm = TREE_TYPE (*parm); | |
11848 | result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; | |
11849 | } | |
1c82cc90 NS |
11850 | |
11851 | /* DR 322. For conversion deduction, remove a reference type on parm | |
11852 | too (which has been swapped into ARG). */ | |
11853 | if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE) | |
11854 | *arg = TREE_TYPE (*arg); | |
c8094d83 | 11855 | |
62e4a758 | 11856 | return result; |
386b8a85 JM |
11857 | } |
11858 | ||
e5214479 | 11859 | /* Most parms like fn_type_unification. |
9f54c803 MM |
11860 | |
11861 | If SUBR is 1, we're being called recursively (to unify the | |
11862 | arguments of a function or method parameter of a function | |
38d18b1a | 11863 | template). */ |
386b8a85 | 11864 | |
4966381a | 11865 | static int |
c8094d83 | 11866 | type_unification_real (tree tparms, |
0cbd7506 MS |
11867 | tree targs, |
11868 | tree xparms, | |
11869 | tree xargs, | |
11870 | int subr, | |
30f86ec3 FJ |
11871 | unification_kind_t strict, |
11872 | int flags) | |
8d08fdba | 11873 | { |
8af2fec4 | 11874 | tree parm, arg, arg_expr; |
8d08fdba MS |
11875 | int i; |
11876 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 11877 | int sub_strict; |
bd0d5d4a JM |
11878 | int saw_undeduced = 0; |
11879 | tree parms, args; | |
8d08fdba | 11880 | |
50bc768d NS |
11881 | gcc_assert (TREE_CODE (tparms) == TREE_VEC); |
11882 | gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST); | |
11883 | gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST); | |
11884 | gcc_assert (ntparms > 0); | |
8d08fdba | 11885 | |
830bfa74 MM |
11886 | switch (strict) |
11887 | { | |
11888 | case DEDUCE_CALL: | |
028d1f20 | 11889 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL |
0cbd7506 | 11890 | | UNIFY_ALLOW_DERIVED); |
830bfa74 | 11891 | break; |
c8094d83 | 11892 | |
830bfa74 MM |
11893 | case DEDUCE_CONV: |
11894 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
11895 | break; | |
11896 | ||
11897 | case DEDUCE_EXACT: | |
11898 | sub_strict = UNIFY_ALLOW_NONE; | |
11899 | break; | |
c8094d83 | 11900 | |
830bfa74 | 11901 | default: |
315fb5db | 11902 | gcc_unreachable (); |
830bfa74 MM |
11903 | } |
11904 | ||
bd0d5d4a JM |
11905 | again: |
11906 | parms = xparms; | |
11907 | args = xargs; | |
bd0d5d4a | 11908 | |
a34d3336 NS |
11909 | while (parms && parms != void_list_node |
11910 | && args && args != void_list_node) | |
8d08fdba | 11911 | { |
5d80a306 DG |
11912 | if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) |
11913 | break; | |
11914 | ||
8d08fdba MS |
11915 | parm = TREE_VALUE (parms); |
11916 | parms = TREE_CHAIN (parms); | |
11917 | arg = TREE_VALUE (args); | |
11918 | args = TREE_CHAIN (args); | |
8af2fec4 | 11919 | arg_expr = NULL; |
8d08fdba MS |
11920 | |
11921 | if (arg == error_mark_node) | |
11922 | return 1; | |
11923 | if (arg == unknown_type_node) | |
34016c81 JM |
11924 | /* We can't deduce anything from this, but we might get all the |
11925 | template args from other function args. */ | |
11926 | continue; | |
b7484fbe | 11927 | |
03e70705 JM |
11928 | /* Conversions will be performed on a function argument that |
11929 | corresponds with a function parameter that contains only | |
11930 | non-deducible template parameters and explicitly specified | |
11931 | template parameters. */ | |
c353b8e3 | 11932 | if (!uses_template_parms (parm)) |
b7484fbe | 11933 | { |
03e70705 JM |
11934 | tree type; |
11935 | ||
2f939d94 | 11936 | if (!TYPE_P (arg)) |
03e70705 JM |
11937 | type = TREE_TYPE (arg); |
11938 | else | |
c353b8e3 | 11939 | type = arg; |
03e70705 | 11940 | |
fad86f7a | 11941 | if (same_type_p (parm, type)) |
343c89cd | 11942 | continue; |
fad86f7a | 11943 | if (strict != DEDUCE_EXACT |
3db45ab5 | 11944 | && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg, |
30f86ec3 | 11945 | flags)) |
fad86f7a | 11946 | continue; |
3db45ab5 | 11947 | |
b7484fbe MS |
11948 | return 1; |
11949 | } | |
c8094d83 | 11950 | |
2f939d94 | 11951 | if (!TYPE_P (arg)) |
8d08fdba | 11952 | { |
50bc768d | 11953 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); |
34016c81 | 11954 | if (type_unknown_p (arg)) |
28cbf42c | 11955 | { |
acde59b4 | 11956 | /* [temp.deduct.type] |
34016c81 | 11957 | |
acde59b4 MM |
11958 | A template-argument can be deduced from a pointer to |
11959 | function or pointer to member function argument if | |
11960 | the set of overloaded functions does not contain | |
11961 | function templates and at most one of a set of | |
11962 | overloaded functions provides a unique match. */ | |
34016c81 | 11963 | if (resolve_overloaded_unification |
acde59b4 MM |
11964 | (tparms, targs, parm, arg, strict, sub_strict)) |
11965 | continue; | |
11966 | ||
11967 | return 1; | |
28cbf42c | 11968 | } |
8af2fec4 | 11969 | arg_expr = arg; |
f9aa54d3 | 11970 | arg = unlowered_expr_type (arg); |
08476342 NS |
11971 | if (arg == error_mark_node) |
11972 | return 1; | |
8d08fdba | 11973 | } |
c8094d83 | 11974 | |
62e4a758 | 11975 | { |
0cbd7506 | 11976 | int arg_strict = sub_strict; |
c8094d83 | 11977 | |
0cbd7506 | 11978 | if (!subr) |
8af2fec4 RY |
11979 | arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, |
11980 | arg_expr); | |
4393e105 | 11981 | |
0cbd7506 MS |
11982 | if (unify (tparms, targs, parm, arg, arg_strict)) |
11983 | return 1; | |
62e4a758 | 11984 | } |
8d08fdba | 11985 | } |
c8094d83 | 11986 | |
5d80a306 DG |
11987 | |
11988 | if (parms | |
11989 | && parms != void_list_node | |
11990 | && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) | |
11991 | { | |
11992 | /* Unify the remaining arguments with the pack expansion type. */ | |
11993 | tree argvec; | |
11994 | tree parmvec = make_tree_vec (1); | |
11995 | int len = 0; | |
11996 | tree t; | |
11997 | ||
11998 | /* Count the number of arguments that remain. */ | |
11999 | for (t = args; t && t != void_list_node; t = TREE_CHAIN (t)) | |
12000 | len++; | |
12001 | ||
12002 | /* Allocate a TREE_VEC and copy in all of the arguments */ | |
12003 | argvec = make_tree_vec (len); | |
12004 | for (i = 0; args && args != void_list_node; args = TREE_CHAIN (args)) | |
12005 | { | |
12006 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (args); | |
12007 | ++i; | |
12008 | } | |
12009 | ||
12010 | /* Copy the parameter into parmvec. */ | |
12011 | TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms); | |
12012 | if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict, | |
12013 | /*call_args_p=*/true, /*subr=*/subr)) | |
12014 | return 1; | |
12015 | ||
12016 | /* Advance to the end of the list of parameters. */ | |
12017 | parms = TREE_CHAIN (parms); | |
12018 | } | |
12019 | ||
8d08fdba MS |
12020 | /* Fail if we've reached the end of the parm list, and more args |
12021 | are present, and the parm list isn't variadic. */ | |
12022 | if (args && args != void_list_node && parms == void_list_node) | |
12023 | return 1; | |
f4f206f4 | 12024 | /* Fail if parms are left and they don't have default values. */ |
a34d3336 | 12025 | if (parms && parms != void_list_node |
8d08fdba MS |
12026 | && TREE_PURPOSE (parms) == NULL_TREE) |
12027 | return 1; | |
bd0d5d4a | 12028 | |
8d08fdba MS |
12029 | if (!subr) |
12030 | for (i = 0; i < ntparms; i++) | |
a34d3336 | 12031 | if (!TREE_VEC_ELT (targs, i)) |
8d08fdba | 12032 | { |
2d8ba2c7 LM |
12033 | tree tparm; |
12034 | ||
12035 | if (TREE_VEC_ELT (tparms, i) == error_mark_node) | |
12036 | continue; | |
12037 | ||
12038 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
bd0d5d4a JM |
12039 | |
12040 | /* If this is an undeduced nontype parameter that depends on | |
12041 | a type parameter, try another pass; its type may have been | |
12042 | deduced from a later argument than the one from which | |
12043 | this parameter can be deduced. */ | |
12044 | if (TREE_CODE (tparm) == PARM_DECL | |
12045 | && uses_template_parms (TREE_TYPE (tparm)) | |
12046 | && !saw_undeduced++) | |
12047 | goto again; | |
12048 | ||
9b7dd5e8 DG |
12049 | /* Core issue #226 (C++0x) [temp.deduct]: |
12050 | ||
12051 | If a template argument has not been deduced, its | |
12052 | default template argument, if any, is used. | |
12053 | ||
c1ae8be5 SM |
12054 | When we are in C++98 mode, TREE_PURPOSE will either |
12055 | be NULL_TREE or ERROR_MARK_NODE, so we do not need | |
12056 | to explicitly check cxx_dialect here. */ | |
9b7dd5e8 DG |
12057 | if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i))) |
12058 | { | |
12059 | tree arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)), | |
12060 | targs, tf_none, NULL_TREE); | |
12061 | if (arg == error_mark_node) | |
12062 | return 1; | |
12063 | else | |
12064 | { | |
12065 | TREE_VEC_ELT (targs, i) = arg; | |
12066 | continue; | |
12067 | } | |
12068 | } | |
12069 | ||
b1d7b1c0 DG |
12070 | /* If the type parameter is a parameter pack, then it will |
12071 | be deduced to an empty parameter pack. */ | |
12072 | if (template_parameter_pack_p (tparm)) | |
12073 | { | |
12074 | tree arg; | |
12075 | ||
12076 | if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX) | |
12077 | { | |
12078 | arg = make_node (NONTYPE_ARGUMENT_PACK); | |
12079 | TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm)); | |
12080 | TREE_CONSTANT (arg) = 1; | |
12081 | } | |
12082 | else | |
12083 | arg = make_node (TYPE_ARGUMENT_PACK); | |
12084 | ||
12085 | SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0)); | |
12086 | ||
12087 | TREE_VEC_ELT (targs, i) = arg; | |
12088 | continue; | |
12089 | } | |
12090 | ||
8d08fdba MS |
12091 | return 2; |
12092 | } | |
c8094d83 | 12093 | |
8d08fdba MS |
12094 | return 0; |
12095 | } | |
12096 | ||
acde59b4 MM |
12097 | /* Subroutine of type_unification_real. Args are like the variables |
12098 | at the call site. ARG is an overloaded function (or template-id); | |
12099 | we try deducing template args from each of the overloads, and if | |
12100 | only one succeeds, we go with that. Modifies TARGS and returns | |
12101 | true on success. */ | |
34016c81 | 12102 | |
acde59b4 | 12103 | static bool |
c8094d83 | 12104 | resolve_overloaded_unification (tree tparms, |
0cbd7506 MS |
12105 | tree targs, |
12106 | tree parm, | |
12107 | tree arg, | |
12108 | unification_kind_t strict, | |
3a978d72 | 12109 | int sub_strict) |
34016c81 JM |
12110 | { |
12111 | tree tempargs = copy_node (targs); | |
12112 | int good = 0; | |
f23fb7f5 | 12113 | bool addr_p; |
34016c81 JM |
12114 | |
12115 | if (TREE_CODE (arg) == ADDR_EXPR) | |
f23fb7f5 MM |
12116 | { |
12117 | arg = TREE_OPERAND (arg, 0); | |
12118 | addr_p = true; | |
12119 | } | |
12120 | else | |
12121 | addr_p = false; | |
9f3d9e46 | 12122 | |
d8f8dca1 MM |
12123 | if (TREE_CODE (arg) == COMPONENT_REF) |
12124 | /* Handle `&x' where `x' is some static or non-static member | |
12125 | function name. */ | |
12126 | arg = TREE_OPERAND (arg, 1); | |
12127 | ||
05e0b2f4 JM |
12128 | if (TREE_CODE (arg) == OFFSET_REF) |
12129 | arg = TREE_OPERAND (arg, 1); | |
12130 | ||
9f3d9e46 | 12131 | /* Strip baselink information. */ |
50ad9642 MM |
12132 | if (BASELINK_P (arg)) |
12133 | arg = BASELINK_FUNCTIONS (arg); | |
9f3d9e46 | 12134 | |
34016c81 JM |
12135 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
12136 | { | |
12137 | /* If we got some explicit template args, we need to plug them into | |
12138 | the affected templates before we try to unify, in case the | |
12139 | explicit args will completely resolve the templates in question. */ | |
12140 | ||
12141 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
12142 | arg = TREE_OPERAND (arg, 0); | |
12143 | ||
12144 | for (; arg; arg = OVL_NEXT (arg)) | |
12145 | { | |
12146 | tree fn = OVL_CURRENT (arg); | |
12147 | tree subargs, elem; | |
12148 | ||
12149 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
12150 | continue; | |
12151 | ||
a34d3336 NS |
12152 | subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn), |
12153 | expl_subargs, /*check_ret=*/false); | |
34016c81 JM |
12154 | if (subargs) |
12155 | { | |
c2ea3a40 | 12156 | elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); |
c8094d83 | 12157 | good += try_one_overload (tparms, targs, tempargs, parm, |
f23fb7f5 | 12158 | elem, strict, sub_strict, addr_p); |
34016c81 JM |
12159 | } |
12160 | } | |
12161 | } | |
acde59b4 MM |
12162 | else if (TREE_CODE (arg) != OVERLOAD |
12163 | && TREE_CODE (arg) != FUNCTION_DECL) | |
12164 | /* If ARG is, for example, "(0, &f)" then its type will be unknown | |
12165 | -- but the deduction does not succeed because the expression is | |
12166 | not just the function on its own. */ | |
12167 | return false; | |
315fb5db | 12168 | else |
acde59b4 MM |
12169 | for (; arg; arg = OVL_NEXT (arg)) |
12170 | good += try_one_overload (tparms, targs, tempargs, parm, | |
12171 | TREE_TYPE (OVL_CURRENT (arg)), | |
12172 | strict, sub_strict, addr_p); | |
34016c81 JM |
12173 | |
12174 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
12175 | to function or pointer to member function argument if the set of | |
12176 | overloaded functions does not contain function templates and at most | |
12177 | one of a set of overloaded functions provides a unique match. | |
12178 | ||
12179 | So if we found multiple possibilities, we return success but don't | |
12180 | deduce anything. */ | |
12181 | ||
12182 | if (good == 1) | |
12183 | { | |
12184 | int i = TREE_VEC_LENGTH (targs); | |
12185 | for (; i--; ) | |
12186 | if (TREE_VEC_ELT (tempargs, i)) | |
12187 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
12188 | } | |
12189 | if (good) | |
acde59b4 | 12190 | return true; |
34016c81 | 12191 | |
acde59b4 | 12192 | return false; |
34016c81 JM |
12193 | } |
12194 | ||
12195 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
12196 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
12197 | different overloads deduce different arguments for a given parm. | |
f23fb7f5 MM |
12198 | ADDR_P is true if the expression for which deduction is being |
12199 | performed was of the form "& fn" rather than simply "fn". | |
12200 | ||
34016c81 JM |
12201 | Returns 1 on success. */ |
12202 | ||
12203 | static int | |
3a978d72 | 12204 | try_one_overload (tree tparms, |
0cbd7506 MS |
12205 | tree orig_targs, |
12206 | tree targs, | |
12207 | tree parm, | |
12208 | tree arg, | |
12209 | unification_kind_t strict, | |
f23fb7f5 MM |
12210 | int sub_strict, |
12211 | bool addr_p) | |
34016c81 JM |
12212 | { |
12213 | int nargs; | |
12214 | tree tempargs; | |
12215 | int i; | |
12216 | ||
12217 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
12218 | to function or pointer to member function argument if the set of | |
12219 | overloaded functions does not contain function templates and at most | |
12220 | one of a set of overloaded functions provides a unique match. | |
12221 | ||
12222 | So if this is a template, just return success. */ | |
12223 | ||
12224 | if (uses_template_parms (arg)) | |
12225 | return 1; | |
12226 | ||
f23fb7f5 MM |
12227 | if (TREE_CODE (arg) == METHOD_TYPE) |
12228 | arg = build_ptrmemfunc_type (build_pointer_type (arg)); | |
12229 | else if (addr_p) | |
12230 | arg = build_pointer_type (arg); | |
12231 | ||
8af2fec4 | 12232 | sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL); |
34016c81 JM |
12233 | |
12234 | /* We don't copy orig_targs for this because if we have already deduced | |
12235 | some template args from previous args, unify would complain when we | |
12236 | try to deduce a template parameter for the same argument, even though | |
12237 | there isn't really a conflict. */ | |
12238 | nargs = TREE_VEC_LENGTH (targs); | |
f31c0a32 | 12239 | tempargs = make_tree_vec (nargs); |
34016c81 | 12240 | |
4393e105 | 12241 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
12242 | return 0; |
12243 | ||
12244 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 12245 | explicitly specified args. */ |
34016c81 JM |
12246 | for (i = nargs; i--; ) |
12247 | { | |
12248 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 12249 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 | 12250 | |
a34d3336 NS |
12251 | if (!elt) |
12252 | /*NOP*/; | |
34016c81 | 12253 | else if (uses_template_parms (elt)) |
a34d3336 NS |
12254 | /* Since we're unifying against ourselves, we will fill in |
12255 | template args used in the function parm list with our own | |
12256 | template parms. Discard them. */ | |
12257 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
12258 | else if (oldelt && !template_args_equal (oldelt, elt)) | |
34016c81 JM |
12259 | return 0; |
12260 | } | |
12261 | ||
12262 | for (i = nargs; i--; ) | |
12263 | { | |
12264 | tree elt = TREE_VEC_ELT (tempargs, i); | |
12265 | ||
12266 | if (elt) | |
12267 | TREE_VEC_ELT (targs, i) = elt; | |
12268 | } | |
12269 | ||
12270 | return 1; | |
12271 | } | |
12272 | ||
4393e105 MM |
12273 | /* PARM is a template class (perhaps with unbound template |
12274 | parameters). ARG is a fully instantiated type. If ARG can be | |
12275 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
12276 | TARGS are as for unify. */ | |
fcfb9f96 MM |
12277 | |
12278 | static tree | |
3a978d72 | 12279 | try_class_unification (tree tparms, tree targs, tree parm, tree arg) |
4393e105 | 12280 | { |
4393e105 MM |
12281 | tree copy_of_targs; |
12282 | ||
12283 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
c8094d83 | 12284 | || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg)) |
68361a03 | 12285 | != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))) |
4393e105 MM |
12286 | return NULL_TREE; |
12287 | ||
12288 | /* We need to make a new template argument vector for the call to | |
12289 | unify. If we used TARGS, we'd clutter it up with the result of | |
12290 | the attempted unification, even if this class didn't work out. | |
12291 | We also don't want to commit ourselves to all the unifications | |
12292 | we've already done, since unification is supposed to be done on | |
12293 | an argument-by-argument basis. In other words, consider the | |
12294 | following pathological case: | |
12295 | ||
12296 | template <int I, int J, int K> | |
12297 | struct S {}; | |
c8094d83 | 12298 | |
4393e105 MM |
12299 | template <int I, int J> |
12300 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
c8094d83 | 12301 | |
4393e105 MM |
12302 | template <int I, int J, int K> |
12303 | void f(S<I, J, K>, S<I, I, I>); | |
c8094d83 | 12304 | |
4393e105 | 12305 | void g() { |
0cbd7506 MS |
12306 | S<0, 0, 0> s0; |
12307 | S<0, 1, 2> s2; | |
c8094d83 | 12308 | |
0cbd7506 | 12309 | f(s0, s2); |
4393e105 MM |
12310 | } |
12311 | ||
12312 | Now, by the time we consider the unification involving `s2', we | |
12313 | already know that we must have `f<0, 0, 0>'. But, even though | |
0e339752 | 12314 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid |
4393e105 MM |
12315 | because there are two ways to unify base classes of S<0, 1, 2> |
12316 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
12317 | would reject the possibility I=1. */ | |
f31c0a32 | 12318 | copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); |
c8094d83 | 12319 | |
4393e105 | 12320 | /* If unification failed, we're done. */ |
74601d7c KL |
12321 | if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), |
12322 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE)) | |
4393e105 | 12323 | return NULL_TREE; |
74601d7c KL |
12324 | |
12325 | return arg; | |
4393e105 MM |
12326 | } |
12327 | ||
a3a0fc7f NS |
12328 | /* Given a template type PARM and a class type ARG, find the unique |
12329 | base type in ARG that is an instance of PARM. We do not examine | |
12330 | ARG itself; only its base-classes. If there is not exactly one | |
12331 | appropriate base class, return NULL_TREE. PARM may be the type of | |
12332 | a partial specialization, as well as a plain template type. Used | |
12333 | by unify. */ | |
4393e105 MM |
12334 | |
12335 | static tree | |
a3a0fc7f | 12336 | get_template_base (tree tparms, tree targs, tree parm, tree arg) |
fcfb9f96 | 12337 | { |
a3a0fc7f NS |
12338 | tree rval = NULL_TREE; |
12339 | tree binfo; | |
12340 | ||
12341 | gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg))); | |
c8094d83 | 12342 | |
a3a0fc7f NS |
12343 | binfo = TYPE_BINFO (complete_type (arg)); |
12344 | if (!binfo) | |
12345 | /* The type could not be completed. */ | |
12346 | return NULL_TREE; | |
fcfb9f96 | 12347 | |
a3a0fc7f NS |
12348 | /* Walk in inheritance graph order. The search order is not |
12349 | important, and this avoids multiple walks of virtual bases. */ | |
12350 | for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo)) | |
fcfb9f96 | 12351 | { |
a3a0fc7f | 12352 | tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo)); |
fcfb9f96 | 12353 | |
8d83768f NS |
12354 | if (r) |
12355 | { | |
12356 | /* If there is more than one satisfactory baseclass, then: | |
fcfb9f96 | 12357 | |
8d83768f | 12358 | [temp.deduct.call] |
fcfb9f96 | 12359 | |
8d83768f NS |
12360 | If they yield more than one possible deduced A, the type |
12361 | deduction fails. | |
4393e105 | 12362 | |
8d83768f | 12363 | applies. */ |
a3a0fc7f NS |
12364 | if (rval && !same_type_p (r, rval)) |
12365 | return NULL_TREE; | |
c8094d83 | 12366 | |
a3a0fc7f | 12367 | rval = r; |
8d83768f | 12368 | } |
fcfb9f96 MM |
12369 | } |
12370 | ||
a3a0fc7f | 12371 | return rval; |
fcfb9f96 MM |
12372 | } |
12373 | ||
db2767b6 MM |
12374 | /* Returns the level of DECL, which declares a template parameter. */ |
12375 | ||
e9659ab0 | 12376 | static int |
3a978d72 | 12377 | template_decl_level (tree decl) |
db2767b6 MM |
12378 | { |
12379 | switch (TREE_CODE (decl)) | |
12380 | { | |
12381 | case TYPE_DECL: | |
12382 | case TEMPLATE_DECL: | |
12383 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
12384 | ||
12385 | case PARM_DECL: | |
12386 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
12387 | ||
12388 | default: | |
315fb5db | 12389 | gcc_unreachable (); |
db2767b6 | 12390 | } |
315fb5db | 12391 | return 0; |
db2767b6 MM |
12392 | } |
12393 | ||
830bfa74 MM |
12394 | /* Decide whether ARG can be unified with PARM, considering only the |
12395 | cv-qualifiers of each type, given STRICT as documented for unify. | |
324f9dfb | 12396 | Returns nonzero iff the unification is OK on that basis. */ |
e92cc029 | 12397 | |
e9659ab0 | 12398 | static int |
3a978d72 | 12399 | check_cv_quals_for_unify (int strict, tree arg, tree parm) |
830bfa74 | 12400 | { |
4f2b0fb2 NS |
12401 | int arg_quals = cp_type_quals (arg); |
12402 | int parm_quals = cp_type_quals (parm); | |
12403 | ||
355f774d NS |
12404 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
12405 | && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) | |
4f2b0fb2 | 12406 | { |
2e9ceb77 | 12407 | /* Although a CVR qualifier is ignored when being applied to a |
0cbd7506 MS |
12408 | substituted template parameter ([8.3.2]/1 for example), that |
12409 | does not apply during deduction [14.8.2.4]/1, (even though | |
12410 | that is not explicitly mentioned, [14.8.2.4]/9 indicates | |
12411 | this). Except when we're allowing additional CV qualifiers | |
12412 | at the outer level [14.8.2.1]/3,1st bullet. */ | |
2e9ceb77 NS |
12413 | if ((TREE_CODE (arg) == REFERENCE_TYPE |
12414 | || TREE_CODE (arg) == FUNCTION_TYPE | |
12415 | || TREE_CODE (arg) == METHOD_TYPE) | |
12416 | && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))) | |
12417 | return 0; | |
12418 | ||
12419 | if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM) | |
12420 | && (parm_quals & TYPE_QUAL_RESTRICT)) | |
12421 | return 0; | |
4f2b0fb2 | 12422 | } |
2e9ceb77 | 12423 | |
62e4a758 | 12424 | if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
4f2b0fb2 | 12425 | && (arg_quals & parm_quals) != parm_quals) |
ef637255 MM |
12426 | return 0; |
12427 | ||
62e4a758 | 12428 | if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) |
4f2b0fb2 | 12429 | && (parm_quals & arg_quals) != arg_quals) |
ef637255 MM |
12430 | return 0; |
12431 | ||
ef637255 | 12432 | return 1; |
830bfa74 MM |
12433 | } |
12434 | ||
5d80a306 DG |
12435 | /* Determines the LEVEL and INDEX for the template parameter PARM. */ |
12436 | void | |
12437 | template_parm_level_and_index (tree parm, int* level, int* index) | |
12438 | { | |
12439 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
12440 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
12441 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
12442 | { | |
12443 | *index = TEMPLATE_TYPE_IDX (parm); | |
12444 | *level = TEMPLATE_TYPE_LEVEL (parm); | |
12445 | } | |
12446 | else | |
12447 | { | |
12448 | *index = TEMPLATE_PARM_IDX (parm); | |
12449 | *level = TEMPLATE_PARM_LEVEL (parm); | |
12450 | } | |
12451 | } | |
12452 | ||
12453 | /* Unifies the remaining arguments in PACKED_ARGS with the pack | |
12454 | expansion at the end of PACKED_PARMS. Returns 0 if the type | |
12455 | deduction succeeds, 1 otherwise. STRICT is the same as in | |
12456 | unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function | |
12457 | call argument list. We'll need to adjust the arguments to make them | |
12458 | types. SUBR tells us if this is from a recursive call to | |
12459 | type_unification_real. */ | |
12460 | int | |
12461 | unify_pack_expansion (tree tparms, tree targs, tree packed_parms, | |
12462 | tree packed_args, int strict, bool call_args_p, | |
12463 | bool subr) | |
12464 | { | |
12465 | tree parm | |
12466 | = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1); | |
12467 | tree pattern = PACK_EXPANSION_PATTERN (parm); | |
12468 | tree pack, packs = NULL_TREE; | |
12469 | int i, start = TREE_VEC_LENGTH (packed_parms) - 1; | |
12470 | int len = TREE_VEC_LENGTH (packed_args); | |
12471 | ||
12472 | /* Determine the parameter packs we will be deducing from the | |
12473 | pattern, and record their current deductions. */ | |
12474 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm); | |
12475 | pack; pack = TREE_CHAIN (pack)) | |
12476 | { | |
12477 | tree parm_pack = TREE_VALUE (pack); | |
12478 | int idx, level; | |
12479 | ||
12480 | /* Determine the index and level of this parameter pack. */ | |
12481 | template_parm_level_and_index (parm_pack, &level, &idx); | |
12482 | ||
12483 | /* Keep track of the parameter packs and their corresponding | |
12484 | argument packs. */ | |
12485 | packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs); | |
12486 | TREE_TYPE (packs) = make_tree_vec (len - start); | |
12487 | } | |
12488 | ||
12489 | /* Loop through all of the arguments that have not yet been | |
12490 | unified and unify each with the pattern. */ | |
12491 | for (i = start; i < len; i++) | |
12492 | { | |
12493 | tree parm = pattern; | |
12494 | ||
12495 | /* For each parameter pack, clear out the deduced value so that | |
12496 | we can deduce it again. */ | |
12497 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
12498 | { | |
12499 | int idx, level; | |
12500 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12501 | ||
12502 | TMPL_ARG (targs, level, idx) = NULL_TREE; | |
12503 | } | |
12504 | ||
12505 | /* Unify the pattern with the current argument. */ | |
12506 | { | |
12507 | tree arg = TREE_VEC_ELT (packed_args, i); | |
2afad0f6 | 12508 | tree arg_expr = NULL_TREE; |
5d80a306 DG |
12509 | int arg_strict = strict; |
12510 | bool skip_arg_p = false; | |
12511 | ||
12512 | if (call_args_p) | |
12513 | { | |
12514 | int sub_strict; | |
12515 | ||
12516 | /* This mirrors what we do in type_unification_real. */ | |
12517 | switch (strict) | |
12518 | { | |
12519 | case DEDUCE_CALL: | |
12520 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | |
12521 | | UNIFY_ALLOW_MORE_CV_QUAL | |
12522 | | UNIFY_ALLOW_DERIVED); | |
12523 | break; | |
12524 | ||
12525 | case DEDUCE_CONV: | |
12526 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
12527 | break; | |
12528 | ||
12529 | case DEDUCE_EXACT: | |
12530 | sub_strict = UNIFY_ALLOW_NONE; | |
12531 | break; | |
12532 | ||
12533 | default: | |
12534 | gcc_unreachable (); | |
12535 | } | |
12536 | ||
12537 | if (!TYPE_P (arg)) | |
12538 | { | |
12539 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); | |
12540 | if (type_unknown_p (arg)) | |
12541 | { | |
12542 | /* [temp.deduct.type] A template-argument can be | |
12543 | deduced from a pointer to function or pointer | |
12544 | to member function argument if the set of | |
12545 | overloaded functions does not contain function | |
12546 | templates and at most one of a set of | |
12547 | overloaded functions provides a unique | |
12548 | match. */ | |
12549 | ||
12550 | if (resolve_overloaded_unification | |
12551 | (tparms, targs, parm, arg, strict, sub_strict) | |
12552 | != 0) | |
12553 | return 1; | |
12554 | skip_arg_p = true; | |
12555 | } | |
12556 | ||
12557 | if (!skip_arg_p) | |
12558 | { | |
2afad0f6 DG |
12559 | arg_expr = arg; |
12560 | arg = unlowered_expr_type (arg); | |
5d80a306 DG |
12561 | if (arg == error_mark_node) |
12562 | return 1; | |
12563 | } | |
12564 | } | |
12565 | ||
12566 | arg_strict = sub_strict; | |
12567 | ||
12568 | if (!subr) | |
12569 | arg_strict |= | |
2afad0f6 DG |
12570 | maybe_adjust_types_for_deduction (strict, &parm, &arg, |
12571 | arg_expr); | |
5d80a306 DG |
12572 | } |
12573 | ||
12574 | if (!skip_arg_p) | |
12575 | { | |
12576 | if (unify (tparms, targs, parm, arg, arg_strict)) | |
12577 | return 1; | |
12578 | } | |
12579 | } | |
12580 | ||
12581 | /* For each parameter pack, collect the deduced value. */ | |
12582 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
12583 | { | |
12584 | int idx, level; | |
12585 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12586 | ||
12587 | TREE_VEC_ELT (TREE_TYPE (pack), i - start) = | |
12588 | TMPL_ARG (targs, level, idx); | |
12589 | } | |
12590 | } | |
12591 | ||
12592 | /* Verify that the results of unification with the parameter packs | |
12593 | produce results consistent with what we've seen before, and make | |
12594 | the deduced argument packs available. */ | |
12595 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
12596 | { | |
12597 | tree old_pack = TREE_VALUE (pack); | |
12598 | tree new_args = TREE_TYPE (pack); | |
db60ff18 DG |
12599 | int i, len = TREE_VEC_LENGTH (new_args); |
12600 | bool nondeduced_p = false; | |
12601 | ||
12602 | /* If NEW_ARGS contains any NULL_TREE entries, we didn't | |
12603 | actually deduce anything. */ | |
12604 | for (i = 0; i < len && !nondeduced_p; ++i) | |
12605 | if (TREE_VEC_ELT (new_args, i) == NULL_TREE) | |
12606 | nondeduced_p = true; | |
12607 | if (nondeduced_p) | |
12608 | continue; | |
5d80a306 DG |
12609 | |
12610 | if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack)) | |
12611 | { | |
12612 | /* Prepend the explicit arguments onto NEW_ARGS. */ | |
12613 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); | |
12614 | tree old_args = new_args; | |
12615 | int i, explicit_len = TREE_VEC_LENGTH (explicit_args); | |
12616 | int len = explicit_len + TREE_VEC_LENGTH (old_args); | |
12617 | ||
12618 | /* Copy the explicit arguments. */ | |
12619 | new_args = make_tree_vec (len); | |
12620 | for (i = 0; i < explicit_len; i++) | |
12621 | TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i); | |
12622 | ||
12623 | /* Copy the deduced arguments. */ | |
12624 | for (; i < len; i++) | |
12625 | TREE_VEC_ELT (new_args, i) = | |
12626 | TREE_VEC_ELT (old_args, i - explicit_len); | |
12627 | } | |
12628 | ||
12629 | if (!old_pack) | |
12630 | { | |
12631 | tree result; | |
12632 | int idx, level; | |
12633 | ||
12634 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12635 | ||
12636 | /* Build the deduced *_ARGUMENT_PACK. */ | |
12637 | if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX) | |
12638 | { | |
12639 | result = make_node (NONTYPE_ARGUMENT_PACK); | |
12640 | TREE_TYPE (result) = | |
12641 | TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack))); | |
12642 | TREE_CONSTANT (result) = 1; | |
12643 | } | |
12644 | else | |
12645 | result = make_node (TYPE_ARGUMENT_PACK); | |
12646 | ||
12647 | SET_ARGUMENT_PACK_ARGS (result, new_args); | |
12648 | ||
12649 | /* Note the deduced argument packs for this parameter | |
12650 | pack. */ | |
12651 | TMPL_ARG (targs, level, idx) = result; | |
12652 | } | |
12653 | else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack) | |
12654 | && (ARGUMENT_PACK_ARGS (old_pack) | |
12655 | == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack))) | |
12656 | { | |
12657 | /* We only had the explicitly-provided arguments before, but | |
12658 | now we have a complete set of arguments. */ | |
12659 | int idx, level; | |
12660 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); | |
12661 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12662 | ||
12663 | /* Keep the original deduced argument pack. */ | |
12664 | TMPL_ARG (targs, level, idx) = old_pack; | |
12665 | ||
12666 | SET_ARGUMENT_PACK_ARGS (old_pack, new_args); | |
12667 | ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1; | |
12668 | ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args; | |
12669 | } | |
12670 | else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack), | |
12671 | new_args)) | |
12672 | /* Inconsistent unification of this parameter pack. */ | |
12673 | return 1; | |
12674 | else | |
12675 | { | |
12676 | int idx, level; | |
12677 | ||
12678 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
12679 | ||
12680 | /* Keep the original deduced argument pack. */ | |
12681 | TMPL_ARG (targs, level, idx) = old_pack; | |
12682 | } | |
12683 | } | |
12684 | ||
12685 | return 0; | |
12686 | } | |
12687 | ||
916b63c3 MM |
12688 | /* Deduce the value of template parameters. TPARMS is the (innermost) |
12689 | set of template parameters to a template. TARGS is the bindings | |
12690 | for those template parameters, as determined thus far; TARGS may | |
12691 | include template arguments for outer levels of template parameters | |
12692 | as well. PARM is a parameter to a template function, or a | |
12693 | subcomponent of that parameter; ARG is the corresponding argument. | |
12694 | This function attempts to match PARM with ARG in a manner | |
12695 | consistent with the existing assignments in TARGS. If more values | |
12696 | are deduced, then TARGS is updated. | |
12697 | ||
12698 | Returns 0 if the type deduction succeeds, 1 otherwise. The | |
12699 | parameter STRICT is a bitwise or of the following flags: | |
830bfa74 MM |
12700 | |
12701 | UNIFY_ALLOW_NONE: | |
12702 | Require an exact match between PARM and ARG. | |
12703 | UNIFY_ALLOW_MORE_CV_QUAL: | |
028d1f20 NS |
12704 | Allow the deduced ARG to be more cv-qualified (by qualification |
12705 | conversion) than ARG. | |
830bfa74 MM |
12706 | UNIFY_ALLOW_LESS_CV_QUAL: |
12707 | Allow the deduced ARG to be less cv-qualified than ARG. | |
12708 | UNIFY_ALLOW_DERIVED: | |
12709 | Allow the deduced ARG to be a template base class of ARG, | |
12710 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
12711 | ARG. |
12712 | UNIFY_ALLOW_INTEGER: | |
12713 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
c8094d83 | 12714 | case for more information. |
028d1f20 NS |
12715 | UNIFY_ALLOW_OUTER_LEVEL: |
12716 | This is the outermost level of a deduction. Used to determine validity | |
12717 | of qualification conversions. A valid qualification conversion must | |
12718 | have const qualified pointers leading up to the inner type which | |
12719 | requires additional CV quals, except at the outer level, where const | |
12720 | is not required [conv.qual]. It would be normal to set this flag in | |
62e4a758 NS |
12721 | addition to setting UNIFY_ALLOW_MORE_CV_QUAL. |
12722 | UNIFY_ALLOW_OUTER_MORE_CV_QUAL: | |
12723 | This is the outermost level of a deduction, and PARM can be more CV | |
12724 | qualified at this point. | |
12725 | UNIFY_ALLOW_OUTER_LESS_CV_QUAL: | |
12726 | This is the outermost level of a deduction, and PARM can be less CV | |
8baddbf1 | 12727 | qualified at this point. */ |
830bfa74 | 12728 | |
e9659ab0 | 12729 | static int |
3a978d72 | 12730 | unify (tree tparms, tree targs, tree parm, tree arg, int strict) |
8d08fdba MS |
12731 | { |
12732 | int idx; | |
050367a3 | 12733 | tree targ; |
db2767b6 | 12734 | tree tparm; |
028d1f20 | 12735 | int strict_in = strict; |
8d08fdba MS |
12736 | |
12737 | /* I don't think this will do the right thing with respect to types. | |
12738 | But the only case I've seen it in so far has been array bounds, where | |
12739 | signedness is the only information lost, and I think that will be | |
12740 | okay. */ | |
12741 | while (TREE_CODE (parm) == NOP_EXPR) | |
12742 | parm = TREE_OPERAND (parm, 0); | |
12743 | ||
12744 | if (arg == error_mark_node) | |
12745 | return 1; | |
12746 | if (arg == unknown_type_node) | |
34016c81 JM |
12747 | /* We can't deduce anything from this, but we might get all the |
12748 | template args from other function args. */ | |
12749 | return 0; | |
12750 | ||
db2767b6 | 12751 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 12752 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
12753 | template parameters. We might need them if we're trying to |
12754 | figure out which of two things is more specialized. */ | |
12755 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
12756 | return 0; |
12757 | ||
830bfa74 MM |
12758 | /* Immediately reject some pairs that won't unify because of |
12759 | cv-qualification mismatches. */ | |
12760 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
2f939d94 | 12761 | && TYPE_P (arg) |
d0ab7624 | 12762 | /* It is the elements of the array which hold the cv quals of an array |
0cbd7506 MS |
12763 | type, and the elements might be template type parms. We'll check |
12764 | when we recurse. */ | |
d0ab7624 | 12765 | && TREE_CODE (arg) != ARRAY_TYPE |
830bfa74 MM |
12766 | /* We check the cv-qualifiers when unifying with template type |
12767 | parameters below. We want to allow ARG `const T' to unify with | |
12768 | PARM `T' for example, when computing which of two templates | |
12769 | is more specialized, for example. */ | |
12770 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
028d1f20 | 12771 | && !check_cv_quals_for_unify (strict_in, arg, parm)) |
49432171 JM |
12772 | return 1; |
12773 | ||
028d1f20 | 12774 | if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) |
3ea099f1 | 12775 | && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) |
028d1f20 NS |
12776 | strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; |
12777 | strict &= ~UNIFY_ALLOW_OUTER_LEVEL; | |
12778 | strict &= ~UNIFY_ALLOW_DERIVED; | |
62e4a758 NS |
12779 | strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
12780 | strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; | |
c8094d83 | 12781 | |
8d08fdba MS |
12782 | switch (TREE_CODE (parm)) |
12783 | { | |
2ca340ae | 12784 | case TYPENAME_TYPE: |
fccef71e | 12785 | case SCOPE_REF: |
b8c6534b | 12786 | case UNBOUND_CLASS_TEMPLATE: |
2ca340ae JM |
12787 | /* In a type which contains a nested-name-specifier, template |
12788 | argument values cannot be deduced for template parameters used | |
12789 | within the nested-name-specifier. */ | |
12790 | return 0; | |
12791 | ||
8d08fdba | 12792 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 12793 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 12794 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
db2767b6 | 12795 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
833be229 PC |
12796 | if (tparm == error_mark_node) |
12797 | return 1; | |
db2767b6 MM |
12798 | |
12799 | if (TEMPLATE_TYPE_LEVEL (parm) | |
12800 | != template_decl_level (tparm)) | |
12801 | /* The PARM is not one we're trying to unify. Just check | |
12802 | to see if it matches ARG. */ | |
12803 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 12804 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 12805 | idx = TEMPLATE_TYPE_IDX (parm); |
916b63c3 | 12806 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 12807 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 12808 | |
73b0fce8 | 12809 | /* Check for mixed types and values. */ |
db2767b6 MM |
12810 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
12811 | && TREE_CODE (tparm) != TYPE_DECL) | |
c8094d83 | 12812 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
db2767b6 | 12813 | && TREE_CODE (tparm) != TEMPLATE_DECL)) |
73b0fce8 KL |
12814 | return 1; |
12815 | ||
a1281f45 | 12816 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 12817 | { |
b429fdf0 KL |
12818 | /* ARG must be constructed from a template class or a template |
12819 | template parameter. */ | |
12820 | if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM | |
4e95268d | 12821 | && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) |
a1281f45 | 12822 | return 1; |
73b0fce8 | 12823 | |
a1281f45 | 12824 | { |
a1281f45 | 12825 | tree parmvec = TYPE_TI_ARGS (parm); |
6df91b00 | 12826 | tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg)); |
a1281f45 | 12827 | tree argtmplvec |
b429fdf0 | 12828 | = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg)); |
4cf36211 DG |
12829 | int i, len; |
12830 | int parm_variadic_p = 0; | |
73b0fce8 | 12831 | |
e7e93965 MM |
12832 | /* The resolution to DR150 makes clear that default |
12833 | arguments for an N-argument may not be used to bind T | |
12834 | to a template template parameter with fewer than N | |
12835 | parameters. It is not safe to permit the binding of | |
12836 | default arguments as an extension, as that may change | |
12837 | the meaning of a conforming program. Consider: | |
12838 | ||
12839 | struct Dense { static const unsigned int dim = 1; }; | |
12840 | ||
12841 | template <template <typename> class View, | |
12842 | typename Block> | |
12843 | void operator+(float, View<Block> const&); | |
12844 | ||
3db45ab5 MS |
12845 | template <typename Block, |
12846 | unsigned int Dim = Block::dim> | |
e7e93965 MM |
12847 | struct Lvalue_proxy { operator float() const; }; |
12848 | ||
12849 | void | |
12850 | test_1d (void) { | |
12851 | Lvalue_proxy<Dense> p; | |
12852 | float b; | |
12853 | b + p; | |
12854 | } | |
a1281f45 | 12855 | |
e7e93965 MM |
12856 | Here, if Lvalue_proxy is permitted to bind to View, then |
12857 | the global operator+ will be used; if they are not, the | |
3db45ab5 MS |
12858 | Lvalue_proxy will be converted to float. */ |
12859 | if (coerce_template_parms (argtmplvec, parmvec, | |
e7e93965 MM |
12860 | TYPE_TI_TEMPLATE (parm), |
12861 | tf_none, | |
12862 | /*require_all_args=*/true, | |
12863 | /*use_default_args=*/false) | |
0cbd7506 | 12864 | == error_mark_node) |
a1281f45 | 12865 | return 1; |
c8094d83 MS |
12866 | |
12867 | /* Deduce arguments T, i from TT<T> or TT<i>. | |
a1281f45 KL |
12868 | We check each element of PARMVEC and ARGVEC individually |
12869 | rather than the whole TREE_VEC since they can have | |
12870 | different number of elements. */ | |
6b9b6b15 | 12871 | |
4cf36211 DG |
12872 | parmvec = expand_template_argument_pack (parmvec); |
12873 | argvec = expand_template_argument_pack (argvec); | |
12874 | ||
12875 | len = TREE_VEC_LENGTH (parmvec); | |
12876 | ||
12877 | /* Check if the parameters end in a pack, making them | |
12878 | variadic. */ | |
12879 | if (len > 0 | |
12880 | && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1))) | |
12881 | parm_variadic_p = 1; | |
12882 | ||
12883 | if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p) | |
12884 | return 1; | |
12885 | ||
12886 | for (i = 0; i < len - parm_variadic_p; ++i) | |
a1281f45 | 12887 | { |
0cbd7506 | 12888 | if (unify (tparms, targs, |
c8094d83 MS |
12889 | TREE_VEC_ELT (parmvec, i), |
12890 | TREE_VEC_ELT (argvec, i), | |
a1281f45 KL |
12891 | UNIFY_ALLOW_NONE)) |
12892 | return 1; | |
73b0fce8 | 12893 | } |
4cf36211 DG |
12894 | |
12895 | if (parm_variadic_p | |
12896 | && unify_pack_expansion (tparms, targs, | |
12897 | parmvec, argvec, | |
12898 | UNIFY_ALLOW_NONE, | |
12899 | /*call_args_p=*/false, | |
12900 | /*subr=*/false)) | |
12901 | return 1; | |
a1281f45 | 12902 | } |
b429fdf0 | 12903 | arg = TYPE_TI_TEMPLATE (arg); |
a1281f45 KL |
12904 | |
12905 | /* Fall through to deduce template name. */ | |
12906 | } | |
12907 | ||
12908 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
12909 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
12910 | { | |
12911 | /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ | |
12912 | ||
12913 | /* Simple cases: Value already set, does match or doesn't. */ | |
12914 | if (targ != NULL_TREE && template_args_equal (targ, arg)) | |
12915 | return 0; | |
12916 | else if (targ) | |
12917 | return 1; | |
db2767b6 MM |
12918 | } |
12919 | else | |
12920 | { | |
830bfa74 MM |
12921 | /* If PARM is `const T' and ARG is only `int', we don't have |
12922 | a match unless we are allowing additional qualification. | |
12923 | If ARG is `const int' and PARM is just `T' that's OK; | |
12924 | that binds `const int' to `T'. */ | |
c8094d83 | 12925 | if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, |
830bfa74 | 12926 | arg, parm)) |
db2767b6 MM |
12927 | return 1; |
12928 | ||
830bfa74 MM |
12929 | /* Consider the case where ARG is `const volatile int' and |
12930 | PARM is `const T'. Then, T should be `volatile int'. */ | |
c2ea3a40 NS |
12931 | arg = cp_build_qualified_type_real |
12932 | (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); | |
adecb3f4 MM |
12933 | if (arg == error_mark_node) |
12934 | return 1; | |
73b0fce8 | 12935 | |
a1281f45 KL |
12936 | /* Simple cases: Value already set, does match or doesn't. */ |
12937 | if (targ != NULL_TREE && same_type_p (targ, arg)) | |
12938 | return 0; | |
12939 | else if (targ) | |
12940 | return 1; | |
61cd552e | 12941 | |
94fc547c MM |
12942 | /* Make sure that ARG is not a variable-sized array. (Note |
12943 | that were talking about variable-sized arrays (like | |
12944 | `int[n]'), rather than arrays of unknown size (like | |
12945 | `int[]').) We'll get very confused by such a type since | |
12946 | the bound of the array will not be computable in an | |
12947 | instantiation. Besides, such types are not allowed in | |
12948 | ISO C++, so we can do as we please here. */ | |
5377d5ba | 12949 | if (variably_modified_type_p (arg, NULL_TREE)) |
94fc547c MM |
12950 | return 1; |
12951 | } | |
61cd552e | 12952 | |
5d80a306 DG |
12953 | /* If ARG is a parameter pack or an expansion, we cannot unify |
12954 | against it unless PARM is also a parameter pack. */ | |
12955 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
12956 | && !template_parameter_pack_p (parm)) | |
12957 | return 1; | |
12958 | ||
916b63c3 | 12959 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
73b0fce8 KL |
12960 | return 0; |
12961 | ||
f84b4be9 | 12962 | case TEMPLATE_PARM_INDEX: |
db2767b6 | 12963 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
3e9ac7e5 VR |
12964 | if (tparm == error_mark_node) |
12965 | return 1; | |
db2767b6 | 12966 | |
c8094d83 | 12967 | if (TEMPLATE_PARM_LEVEL (parm) |
db2767b6 MM |
12968 | != template_decl_level (tparm)) |
12969 | /* The PARM is not one we're trying to unify. Just check | |
12970 | to see if it matches ARG. */ | |
c8a209ca NS |
12971 | return !(TREE_CODE (arg) == TREE_CODE (parm) |
12972 | && cp_tree_equal (parm, arg)); | |
db2767b6 | 12973 | |
f84b4be9 | 12974 | idx = TEMPLATE_PARM_IDX (parm); |
916b63c3 | 12975 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 12976 | |
050367a3 | 12977 | if (targ) |
c8a209ca | 12978 | return !cp_tree_equal (targ, arg); |
8d08fdba | 12979 | |
161c12b0 JM |
12980 | /* [temp.deduct.type] If, in the declaration of a function template |
12981 | with a non-type template-parameter, the non-type | |
12982 | template-parameter is used in an expression in the function | |
12983 | parameter-list and, if the corresponding template-argument is | |
12984 | deduced, the template-argument type shall match the type of the | |
12985 | template-parameter exactly, except that a template-argument | |
c8094d83 | 12986 | deduced from an array bound may be of any integral type. |
d7c4edd0 | 12987 | The non-type parameter might use already deduced type parameters. */ |
bd0d5d4a | 12988 | tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); |
e2005c8d KL |
12989 | if (!TREE_TYPE (arg)) |
12990 | /* Template-parameter dependent expression. Just accept it for now. | |
12991 | It will later be processed in convert_template_argument. */ | |
12992 | ; | |
12993 | else if (same_type_p (TREE_TYPE (arg), tparm)) | |
12994 | /* OK */; | |
161c12b0 | 12995 | else if ((strict & UNIFY_ALLOW_INTEGER) |
bd0d5d4a JM |
12996 | && (TREE_CODE (tparm) == INTEGER_TYPE |
12997 | || TREE_CODE (tparm) == BOOLEAN_TYPE)) | |
8baddbf1 MM |
12998 | /* Convert the ARG to the type of PARM; the deduced non-type |
12999 | template argument must exactly match the types of the | |
13000 | corresponding parameter. */ | |
13001 | arg = fold (build_nop (TREE_TYPE (parm), arg)); | |
bd0d5d4a JM |
13002 | else if (uses_template_parms (tparm)) |
13003 | /* We haven't deduced the type of this parameter yet. Try again | |
13004 | later. */ | |
13005 | return 0; | |
161c12b0 JM |
13006 | else |
13007 | return 1; | |
13008 | ||
5d80a306 DG |
13009 | /* If ARG is a parameter pack or an expansion, we cannot unify |
13010 | against it unless PARM is also a parameter pack. */ | |
13011 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
13012 | && !TEMPLATE_PARM_PARAMETER_PACK (parm)) | |
13013 | return 1; | |
13014 | ||
916b63c3 | 13015 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
8d08fdba MS |
13016 | return 0; |
13017 | ||
28e8f3a0 GB |
13018 | case PTRMEM_CST: |
13019 | { | |
0cbd7506 MS |
13020 | /* A pointer-to-member constant can be unified only with |
13021 | another constant. */ | |
28e8f3a0 | 13022 | if (TREE_CODE (arg) != PTRMEM_CST) |
0cbd7506 | 13023 | return 1; |
28e8f3a0 GB |
13024 | |
13025 | /* Just unify the class member. It would be useless (and possibly | |
0cbd7506 MS |
13026 | wrong, depending on the strict flags) to unify also |
13027 | PTRMEM_CST_CLASS, because we want to be sure that both parm and | |
13028 | arg refer to the same variable, even if through different | |
13029 | classes. For instance: | |
28e8f3a0 | 13030 | |
0cbd7506 MS |
13031 | struct A { int x; }; |
13032 | struct B : A { }; | |
28e8f3a0 | 13033 | |
0cbd7506 | 13034 | Unification of &A::x and &B::x must succeed. */ |
28e8f3a0 | 13035 | return unify (tparms, targs, PTRMEM_CST_MEMBER (parm), |
0cbd7506 | 13036 | PTRMEM_CST_MEMBER (arg), strict); |
28e8f3a0 GB |
13037 | } |
13038 | ||
8d08fdba | 13039 | case POINTER_TYPE: |
830bfa74 | 13040 | { |
830bfa74 MM |
13041 | if (TREE_CODE (arg) != POINTER_TYPE) |
13042 | return 1; | |
c8094d83 | 13043 | |
830bfa74 MM |
13044 | /* [temp.deduct.call] |
13045 | ||
13046 | A can be another pointer or pointer to member type that can | |
13047 | be converted to the deduced A via a qualification | |
13048 | conversion (_conv.qual_). | |
13049 | ||
13050 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
13051 | This will allow for additional cv-qualification of the | |
028d1f20 | 13052 | pointed-to types if appropriate. */ |
c8094d83 | 13053 | |
028d1f20 | 13054 | if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) |
830bfa74 MM |
13055 | /* The derived-to-base conversion only persists through one |
13056 | level of pointers. */ | |
028d1f20 | 13057 | strict |= (strict_in & UNIFY_ALLOW_DERIVED); |
830bfa74 | 13058 | |
c8094d83 | 13059 | return unify (tparms, targs, TREE_TYPE (parm), |
028d1f20 | 13060 | TREE_TYPE (arg), strict); |
830bfa74 | 13061 | } |
8d08fdba MS |
13062 | |
13063 | case REFERENCE_TYPE: | |
830bfa74 MM |
13064 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
13065 | return 1; | |
13066 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
028d1f20 | 13067 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
13068 | |
13069 | case ARRAY_TYPE: | |
13070 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
13071 | return 1; | |
3042d5be MM |
13072 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
13073 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
13074 | return 1; | |
8baddbf1 MM |
13075 | if (TYPE_DOMAIN (parm) != NULL_TREE) |
13076 | { | |
13077 | tree parm_max; | |
13078 | tree arg_max; | |
6ee3ffe8 MM |
13079 | bool parm_cst; |
13080 | bool arg_cst; | |
8baddbf1 MM |
13081 | |
13082 | /* Our representation of array types uses "N - 1" as the | |
13083 | TYPE_MAX_VALUE for an array with "N" elements, if "N" is | |
6ee3ffe8 MM |
13084 | not an integer constant. We cannot unify arbitrarily |
13085 | complex expressions, so we eliminate the MINUS_EXPRs | |
13086 | here. */ | |
13087 | parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm)); | |
13088 | parm_cst = TREE_CODE (parm_max) == INTEGER_CST; | |
13089 | if (!parm_cst) | |
8baddbf1 | 13090 | { |
6ee3ffe8 | 13091 | gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR); |
8baddbf1 MM |
13092 | parm_max = TREE_OPERAND (parm_max, 0); |
13093 | } | |
6ee3ffe8 MM |
13094 | arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg)); |
13095 | arg_cst = TREE_CODE (arg_max) == INTEGER_CST; | |
13096 | if (!arg_cst) | |
13097 | { | |
13098 | /* The ARG_MAX may not be a simple MINUS_EXPR, if we are | |
13099 | trying to unify the type of a variable with the type | |
13100 | of a template parameter. For example: | |
13101 | ||
13102 | template <unsigned int N> | |
13103 | void f (char (&) [N]); | |
13104 | int g(); | |
13105 | void h(int i) { | |
13106 | char a[g(i)]; | |
13107 | f(a); | |
13108 | } | |
13109 | ||
13110 | Here, the type of the ARG will be "int [g(i)]", and | |
13111 | may be a SAVE_EXPR, etc. */ | |
13112 | if (TREE_CODE (arg_max) != MINUS_EXPR) | |
13113 | return 1; | |
13114 | arg_max = TREE_OPERAND (arg_max, 0); | |
13115 | } | |
13116 | ||
13117 | /* If only one of the bounds used a MINUS_EXPR, compensate | |
13118 | by adding one to the other bound. */ | |
13119 | if (parm_cst && !arg_cst) | |
13120 | parm_max = fold_build2 (PLUS_EXPR, | |
13121 | integer_type_node, | |
13122 | parm_max, | |
13123 | integer_one_node); | |
13124 | else if (arg_cst && !parm_cst) | |
13125 | arg_max = fold_build2 (PLUS_EXPR, | |
13126 | integer_type_node, | |
13127 | arg_max, | |
13128 | integer_one_node); | |
8baddbf1 MM |
13129 | |
13130 | if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER)) | |
13131 | return 1; | |
13132 | } | |
830bfa74 | 13133 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
712467a4 | 13134 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
13135 | |
13136 | case REAL_TYPE: | |
37c46b43 | 13137 | case COMPLEX_TYPE: |
c00996a3 | 13138 | case VECTOR_TYPE: |
8d08fdba | 13139 | case INTEGER_TYPE: |
42976354 | 13140 | case BOOLEAN_TYPE: |
3590f0a6 | 13141 | case ENUMERAL_TYPE: |
5ad5a526 | 13142 | case VOID_TYPE: |
f376e137 MS |
13143 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
13144 | return 1; | |
c8094d83 | 13145 | |
9edc3913 | 13146 | /* We have already checked cv-qualification at the top of the |
514a1f18 | 13147 | function. */ |
8baddbf1 | 13148 | if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) |
ca79f85d JM |
13149 | return 1; |
13150 | ||
8d08fdba MS |
13151 | /* As far as unification is concerned, this wins. Later checks |
13152 | will invalidate it if necessary. */ | |
13153 | return 0; | |
13154 | ||
13155 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 13156 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 13157 | case INTEGER_CST: |
bd6dd845 MS |
13158 | while (TREE_CODE (arg) == NOP_EXPR) |
13159 | arg = TREE_OPERAND (arg, 0); | |
13160 | ||
8d08fdba MS |
13161 | if (TREE_CODE (arg) != INTEGER_CST) |
13162 | return 1; | |
13163 | return !tree_int_cst_equal (parm, arg); | |
13164 | ||
8d08fdba MS |
13165 | case TREE_VEC: |
13166 | { | |
13167 | int i; | |
13168 | if (TREE_CODE (arg) != TREE_VEC) | |
13169 | return 1; | |
13170 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
13171 | return 1; | |
0dc09a61 | 13172 | for (i = 0; i < TREE_VEC_LENGTH (parm); ++i) |
830bfa74 | 13173 | if (unify (tparms, targs, |
8d08fdba | 13174 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 13175 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
13176 | return 1; |
13177 | return 0; | |
13178 | } | |
13179 | ||
8d08fdba | 13180 | case RECORD_TYPE: |
f181d4ae | 13181 | case UNION_TYPE: |
f181d4ae | 13182 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 13183 | return 1; |
c8094d83 | 13184 | |
a7a64a77 MM |
13185 | if (TYPE_PTRMEMFUNC_P (parm)) |
13186 | { | |
13187 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
13188 | return 1; | |
13189 | ||
c8094d83 | 13190 | return unify (tparms, targs, |
a7a64a77 MM |
13191 | TYPE_PTRMEMFUNC_FN_TYPE (parm), |
13192 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
13193 | strict); | |
13194 | } | |
13195 | ||
5db698f6 | 13196 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 13197 | { |
6467930b | 13198 | tree t = NULL_TREE; |
4393e105 | 13199 | |
028d1f20 | 13200 | if (strict_in & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
13201 | { |
13202 | /* First, we try to unify the PARM and ARG directly. */ | |
13203 | t = try_class_unification (tparms, targs, | |
13204 | parm, arg); | |
13205 | ||
13206 | if (!t) | |
13207 | { | |
13208 | /* Fallback to the special case allowed in | |
13209 | [temp.deduct.call]: | |
c8094d83 | 13210 | |
4393e105 MM |
13211 | If P is a class, and P has the form |
13212 | template-id, then A can be a derived class of | |
13213 | the deduced A. Likewise, if P is a pointer to | |
13214 | a class of the form template-id, A can be a | |
13215 | pointer to a derived class pointed to by the | |
13216 | deduced A. */ | |
8d83768f | 13217 | t = get_template_base (tparms, targs, parm, arg); |
4393e105 | 13218 | |
8d83768f | 13219 | if (!t) |
4393e105 MM |
13220 | return 1; |
13221 | } | |
13222 | } | |
c8094d83 MS |
13223 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
13224 | && (CLASSTYPE_TI_TEMPLATE (parm) | |
9fbf56f7 | 13225 | == CLASSTYPE_TI_TEMPLATE (arg))) |
6df47b06 MM |
13226 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
13227 | Then, we should unify `int' and `U'. */ | |
6467930b | 13228 | t = arg; |
4393e105 | 13229 | else |
dc957d14 | 13230 | /* There's no chance of unification succeeding. */ |
5566b478 | 13231 | return 1; |
6467930b | 13232 | |
830bfa74 | 13233 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 13234 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 13235 | } |
9edc3913 | 13236 | else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) |
5566b478 | 13237 | return 1; |
a4443a08 | 13238 | return 0; |
8d08fdba MS |
13239 | |
13240 | case METHOD_TYPE: | |
8d08fdba | 13241 | case FUNCTION_TYPE: |
830bfa74 | 13242 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 13243 | return 1; |
830bfa74 | 13244 | |
38d18b1a | 13245 | /* CV qualifications for methods can never be deduced, they must |
3db45ab5 MS |
13246 | match exactly. We need to check them explicitly here, |
13247 | because type_unification_real treats them as any other | |
13248 | cvqualified parameter. */ | |
38d18b1a NS |
13249 | if (TREE_CODE (parm) == METHOD_TYPE |
13250 | && (!check_cv_quals_for_unify | |
13251 | (UNIFY_ALLOW_NONE, | |
13252 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))), | |
13253 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm)))))) | |
13254 | return 1; | |
13255 | ||
830bfa74 | 13256 | if (unify (tparms, targs, TREE_TYPE (parm), |
4393e105 | 13257 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) |
28cbf42c | 13258 | return 1; |
386b8a85 | 13259 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
30f86ec3 FJ |
13260 | TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT, |
13261 | LOOKUP_NORMAL); | |
a4443a08 MS |
13262 | |
13263 | case OFFSET_TYPE: | |
9804209d | 13264 | /* Unify a pointer to member with a pointer to member function, which |
0cbd7506 | 13265 | deduces the type of the member as a function type. */ |
9804209d | 13266 | if (TYPE_PTRMEMFUNC_P (arg)) |
0cbd7506 MS |
13267 | { |
13268 | tree method_type; | |
13269 | tree fntype; | |
13270 | cp_cv_quals cv_quals; | |
13271 | ||
13272 | /* Check top-level cv qualifiers */ | |
13273 | if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm)) | |
13274 | return 1; | |
13275 | ||
13276 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), | |
13277 | TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE)) | |
13278 | return 1; | |
13279 | ||
13280 | /* Determine the type of the function we are unifying against. */ | |
13281 | method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg)); | |
13282 | fntype = | |
13283 | build_function_type (TREE_TYPE (method_type), | |
13284 | TREE_CHAIN (TYPE_ARG_TYPES (method_type))); | |
13285 | ||
13286 | /* Extract the cv-qualifiers of the member function from the | |
13287 | implicit object parameter and place them on the function | |
13288 | type to be restored later. */ | |
13289 | cv_quals = | |
13290 | cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type)))); | |
13291 | fntype = build_qualified_type (fntype, cv_quals); | |
13292 | return unify (tparms, targs, TREE_TYPE (parm), fntype, strict); | |
13293 | } | |
9804209d | 13294 | |
a4443a08 MS |
13295 | if (TREE_CODE (arg) != OFFSET_TYPE) |
13296 | return 1; | |
830bfa74 | 13297 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 13298 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 13299 | return 1; |
830bfa74 | 13300 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 13301 | strict); |
a4443a08 | 13302 | |
f62dbf03 | 13303 | case CONST_DECL: |
a723baf1 MM |
13304 | if (DECL_TEMPLATE_PARM_P (parm)) |
13305 | return unify (tparms, targs, DECL_INITIAL (parm), arg, strict); | |
c8094d83 | 13306 | if (arg != integral_constant_value (parm)) |
f62dbf03 JM |
13307 | return 1; |
13308 | return 0; | |
13309 | ||
28e8f3a0 | 13310 | case FIELD_DECL: |
027905b4 KL |
13311 | case TEMPLATE_DECL: |
13312 | /* Matched cases are handled by the ARG == PARM test above. */ | |
13313 | return 1; | |
13314 | ||
5d80a306 DG |
13315 | case TYPE_ARGUMENT_PACK: |
13316 | case NONTYPE_ARGUMENT_PACK: | |
13317 | { | |
13318 | tree packed_parms = ARGUMENT_PACK_ARGS (parm); | |
13319 | tree packed_args = ARGUMENT_PACK_ARGS (arg); | |
13320 | int i, len = TREE_VEC_LENGTH (packed_parms); | |
13321 | int argslen = TREE_VEC_LENGTH (packed_args); | |
13322 | int parm_variadic_p = 0; | |
13323 | ||
db60ff18 DG |
13324 | for (i = 0; i < len; ++i) |
13325 | { | |
13326 | if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i))) | |
13327 | { | |
13328 | if (i == len - 1) | |
13329 | /* We can unify against something with a trailing | |
13330 | parameter pack. */ | |
13331 | parm_variadic_p = 1; | |
13332 | else | |
13333 | /* Since there is something following the pack | |
13334 | expansion, we cannot unify this template argument | |
13335 | list. */ | |
13336 | return 0; | |
13337 | } | |
13338 | } | |
13339 | ||
5d80a306 DG |
13340 | |
13341 | /* If we don't have enough arguments to satisfy the parameters | |
13342 | (not counting the pack expression at the end), or we have | |
13343 | too many arguments for a parameter list that doesn't end in | |
13344 | a pack expression, we can't unify. */ | |
13345 | if (argslen < (len - parm_variadic_p) | |
13346 | || (argslen > len && !parm_variadic_p)) | |
13347 | return 1; | |
13348 | ||
13349 | /* Unify all of the parameters that precede the (optional) | |
13350 | pack expression. */ | |
13351 | for (i = 0; i < len - parm_variadic_p; ++i) | |
13352 | { | |
13353 | if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i), | |
13354 | TREE_VEC_ELT (packed_args, i), strict)) | |
13355 | return 1; | |
13356 | } | |
13357 | ||
13358 | if (parm_variadic_p) | |
13359 | return unify_pack_expansion (tparms, targs, | |
13360 | packed_parms, packed_args, | |
13361 | strict, /*call_args_p=*/false, | |
13362 | /*subr=*/false); | |
13363 | return 0; | |
13364 | } | |
13365 | ||
13366 | break; | |
13367 | ||
3ad6a8e1 DG |
13368 | case TYPEOF_TYPE: |
13369 | case DECLTYPE_TYPE: | |
13370 | /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE | |
13371 | nodes. */ | |
13372 | return 0; | |
13373 | ||
1ad8aeeb DG |
13374 | case ERROR_MARK: |
13375 | /* Unification fails if we hit an error node. */ | |
13376 | return 1; | |
13377 | ||
8d08fdba | 13378 | default: |
6615c446 | 13379 | gcc_assert (EXPR_P (parm)); |
c8094d83 | 13380 | |
98ddffc1 | 13381 | /* We must be looking at an expression. This can happen with |
c8094d83 MS |
13382 | something like: |
13383 | ||
98ddffc1 NS |
13384 | template <int I> |
13385 | void foo(S<I>, S<I + 2>); | |
050367a3 | 13386 | |
98ddffc1 | 13387 | This is a "nondeduced context": |
050367a3 | 13388 | |
98ddffc1 | 13389 | [deduct.type] |
c8094d83 | 13390 | |
98ddffc1 | 13391 | The nondeduced contexts are: |
050367a3 | 13392 | |
98ddffc1 NS |
13393 | --A type that is a template-id in which one or more of |
13394 | the template-arguments is an expression that references | |
c8094d83 | 13395 | a template-parameter. |
050367a3 | 13396 | |
98ddffc1 NS |
13397 | In these cases, we assume deduction succeeded, but don't |
13398 | actually infer any unifications. */ | |
74601d7c | 13399 | |
98ddffc1 NS |
13400 | if (!uses_template_parms (parm) |
13401 | && !template_args_equal (parm, arg)) | |
13402 | return 1; | |
13403 | else | |
13404 | return 0; | |
8d08fdba MS |
13405 | } |
13406 | } | |
8d08fdba | 13407 | \f |
4684cd27 MM |
13408 | /* Note that DECL can be defined in this translation unit, if |
13409 | required. */ | |
13410 | ||
13411 | static void | |
13412 | mark_definable (tree decl) | |
13413 | { | |
13414 | tree clone; | |
13415 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
13416 | FOR_EACH_CLONE (clone, decl) | |
13417 | DECL_NOT_REALLY_EXTERN (clone) = 1; | |
13418 | } | |
13419 | ||
03d0f4af | 13420 | /* Called if RESULT is explicitly instantiated, or is a member of an |
4684cd27 | 13421 | explicitly instantiated class. */ |
03d0f4af | 13422 | |
faae18ab | 13423 | void |
3a978d72 | 13424 | mark_decl_instantiated (tree result, int extern_p) |
faae18ab | 13425 | { |
415c974c | 13426 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
3ae18eaf | 13427 | |
1f6f0cb6 MM |
13428 | /* If this entity has already been written out, it's too late to |
13429 | make any modifications. */ | |
13430 | if (TREE_ASM_WRITTEN (result)) | |
13431 | return; | |
13432 | ||
13433 | if (TREE_CODE (result) != FUNCTION_DECL) | |
13434 | /* The TREE_PUBLIC flag for function declarations will have been | |
13435 | set correctly by tsubst. */ | |
13436 | TREE_PUBLIC (result) = 1; | |
13437 | ||
346eeb15 JM |
13438 | /* This might have been set by an earlier implicit instantiation. */ |
13439 | DECL_COMDAT (result) = 0; | |
13440 | ||
4684cd27 MM |
13441 | if (extern_p) |
13442 | DECL_NOT_REALLY_EXTERN (result) = 0; | |
13443 | else | |
faae18ab | 13444 | { |
4684cd27 | 13445 | mark_definable (result); |
1a408d07 JM |
13446 | /* Always make artificials weak. */ |
13447 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
13448 | comdat_linkage (result); | |
a7d87521 JM |
13449 | /* For WIN32 we also want to put explicit instantiations in |
13450 | linkonce sections. */ | |
1a408d07 | 13451 | else if (TREE_PUBLIC (result)) |
b385c841 | 13452 | maybe_make_one_only (result); |
faae18ab | 13453 | } |
c8094d83 | 13454 | |
4684cd27 MM |
13455 | /* If EXTERN_P, then this function will not be emitted -- unless |
13456 | followed by an explicit instantiation, at which point its linkage | |
13457 | will be adjusted. If !EXTERN_P, then this function will be | |
13458 | emitted here. In neither circumstance do we want | |
13459 | import_export_decl to adjust the linkage. */ | |
c8094d83 | 13460 | DECL_INTERFACE_KNOWN (result) = 1; |
faae18ab MS |
13461 | } |
13462 | ||
e5214479 | 13463 | /* Given two function templates PAT1 and PAT2, return: |
6467930b MS |
13464 | |
13465 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
13466 | -1 if PAT2 is more specialized than PAT1. | |
e5214479 JM |
13467 | 0 if neither is more specialized. |
13468 | ||
dda04398 NS |
13469 | LEN indicates the number of parameters we should consider |
13470 | (defaulted parameters should not be considered). | |
13471 | ||
13472 | The 1998 std underspecified function template partial ordering, and | |
13473 | DR214 addresses the issue. We take pairs of arguments, one from | |
c51940a2 | 13474 | each of the templates, and deduce them against each other. One of |
dda04398 NS |
13475 | the templates will be more specialized if all the *other* |
13476 | template's arguments deduce against its arguments and at least one | |
13477 | of its arguments *does* *not* deduce against the other template's | |
13478 | corresponding argument. Deduction is done as for class templates. | |
13479 | The arguments used in deduction have reference and top level cv | |
13480 | qualifiers removed. Iff both arguments were originally reference | |
13481 | types *and* deduction succeeds in both directions, the template | |
13482 | with the more cv-qualified argument wins for that pairing (if | |
13483 | neither is more cv-qualified, they both are equal). Unlike regular | |
13484 | deduction, after all the arguments have been deduced in this way, | |
13485 | we do *not* verify the deduced template argument values can be | |
13486 | substituted into non-deduced contexts, nor do we have to verify | |
13487 | that all template arguments have been deduced. */ | |
c8094d83 | 13488 | |
6467930b | 13489 | int |
dda04398 NS |
13490 | more_specialized_fn (tree pat1, tree pat2, int len) |
13491 | { | |
13492 | tree decl1 = DECL_TEMPLATE_RESULT (pat1); | |
13493 | tree decl2 = DECL_TEMPLATE_RESULT (pat2); | |
13494 | tree targs1 = make_tree_vec (DECL_NTPARMS (pat1)); | |
13495 | tree targs2 = make_tree_vec (DECL_NTPARMS (pat2)); | |
13496 | tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1); | |
13497 | tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2); | |
13498 | tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); | |
13499 | tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2)); | |
13500 | int better1 = 0; | |
13501 | int better2 = 0; | |
3db45ab5 | 13502 | |
48884537 NS |
13503 | /* Remove the this parameter from non-static member functions. If |
13504 | one is a non-static member function and the other is not a static | |
13505 | member function, remove the first parameter from that function | |
13506 | also. This situation occurs for operator functions where we | |
13507 | locate both a member function (with this pointer) and non-member | |
13508 | operator (with explicit first operand). */ | |
dda04398 | 13509 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1)) |
48884537 NS |
13510 | { |
13511 | len--; /* LEN is the number of significant arguments for DECL1 */ | |
13512 | args1 = TREE_CHAIN (args1); | |
13513 | if (!DECL_STATIC_FUNCTION_P (decl2)) | |
13514 | args2 = TREE_CHAIN (args2); | |
13515 | } | |
13516 | else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2)) | |
13517 | { | |
13518 | args2 = TREE_CHAIN (args2); | |
13519 | if (!DECL_STATIC_FUNCTION_P (decl1)) | |
13520 | { | |
13521 | len--; | |
13522 | args1 = TREE_CHAIN (args1); | |
13523 | } | |
13524 | } | |
3db45ab5 | 13525 | |
ee307009 NS |
13526 | /* If only one is a conversion operator, they are unordered. */ |
13527 | if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2)) | |
13528 | return 0; | |
c8094d83 | 13529 | |
dda04398 NS |
13530 | /* Consider the return type for a conversion function */ |
13531 | if (DECL_CONV_FN_P (decl1)) | |
13532 | { | |
dda04398 NS |
13533 | args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1); |
13534 | args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2); | |
13535 | len++; | |
13536 | } | |
c8094d83 | 13537 | |
dda04398 | 13538 | processing_template_decl++; |
c8094d83 | 13539 | |
dda04398 NS |
13540 | while (len--) |
13541 | { | |
13542 | tree arg1 = TREE_VALUE (args1); | |
13543 | tree arg2 = TREE_VALUE (args2); | |
13544 | int deduce1, deduce2; | |
13545 | int quals1 = -1; | |
13546 | int quals2 = -1; | |
6467930b | 13547 | |
5d80a306 DG |
13548 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
13549 | && TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
13550 | { | |
13551 | /* When both arguments are pack expansions, we need only | |
13552 | unify the patterns themselves. */ | |
13553 | arg1 = PACK_EXPANSION_PATTERN (arg1); | |
13554 | arg2 = PACK_EXPANSION_PATTERN (arg2); | |
13555 | ||
13556 | /* This is the last comparison we need to do. */ | |
13557 | len = 0; | |
13558 | } | |
13559 | ||
dda04398 NS |
13560 | if (TREE_CODE (arg1) == REFERENCE_TYPE) |
13561 | { | |
13562 | arg1 = TREE_TYPE (arg1); | |
13563 | quals1 = cp_type_quals (arg1); | |
13564 | } | |
c8094d83 | 13565 | |
dda04398 NS |
13566 | if (TREE_CODE (arg2) == REFERENCE_TYPE) |
13567 | { | |
13568 | arg2 = TREE_TYPE (arg2); | |
13569 | quals2 = cp_type_quals (arg2); | |
13570 | } | |
6467930b | 13571 | |
dda04398 NS |
13572 | if ((quals1 < 0) != (quals2 < 0)) |
13573 | { | |
13574 | /* Only of the args is a reference, see if we should apply | |
13575 | array/function pointer decay to it. This is not part of | |
13576 | DR214, but is, IMHO, consistent with the deduction rules | |
13577 | for the function call itself, and with our earlier | |
13578 | implementation of the underspecified partial ordering | |
13579 | rules. (nathan). */ | |
13580 | if (quals1 >= 0) | |
13581 | { | |
13582 | switch (TREE_CODE (arg1)) | |
13583 | { | |
13584 | case ARRAY_TYPE: | |
13585 | arg1 = TREE_TYPE (arg1); | |
13586 | /* FALLTHROUGH. */ | |
13587 | case FUNCTION_TYPE: | |
13588 | arg1 = build_pointer_type (arg1); | |
13589 | break; | |
c8094d83 | 13590 | |
dda04398 NS |
13591 | default: |
13592 | break; | |
13593 | } | |
13594 | } | |
13595 | else | |
13596 | { | |
13597 | switch (TREE_CODE (arg2)) | |
13598 | { | |
13599 | case ARRAY_TYPE: | |
13600 | arg2 = TREE_TYPE (arg2); | |
13601 | /* FALLTHROUGH. */ | |
13602 | case FUNCTION_TYPE: | |
13603 | arg2 = build_pointer_type (arg2); | |
13604 | break; | |
c8094d83 | 13605 | |
dda04398 NS |
13606 | default: |
13607 | break; | |
13608 | } | |
13609 | } | |
13610 | } | |
c8094d83 | 13611 | |
dda04398 NS |
13612 | arg1 = TYPE_MAIN_VARIANT (arg1); |
13613 | arg2 = TYPE_MAIN_VARIANT (arg2); | |
c8094d83 | 13614 | |
5d80a306 DG |
13615 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION) |
13616 | { | |
b1d7b1c0 | 13617 | int i, len2 = list_length (args2); |
5d80a306 DG |
13618 | tree parmvec = make_tree_vec (1); |
13619 | tree argvec = make_tree_vec (len2); | |
13620 | tree ta = args2; | |
13621 | ||
13622 | /* Setup the parameter vector, which contains only ARG1. */ | |
13623 | TREE_VEC_ELT (parmvec, 0) = arg1; | |
13624 | ||
13625 | /* Setup the argument vector, which contains the remaining | |
13626 | arguments. */ | |
13627 | for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta)) | |
13628 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
13629 | ||
13630 | deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec, | |
13631 | argvec, UNIFY_ALLOW_NONE, | |
13632 | /*call_args_p=*/false, | |
13633 | /*subr=*/0); | |
13634 | ||
13635 | /* We cannot deduce in the other direction, because ARG1 is | |
13636 | a pack expansion but ARG2 is not. */ | |
13637 | deduce2 = 0; | |
13638 | } | |
13639 | else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
13640 | { | |
b1d7b1c0 | 13641 | int i, len1 = list_length (args1); |
5d80a306 DG |
13642 | tree parmvec = make_tree_vec (1); |
13643 | tree argvec = make_tree_vec (len1); | |
13644 | tree ta = args1; | |
13645 | ||
13646 | /* Setup the parameter vector, which contains only ARG1. */ | |
13647 | TREE_VEC_ELT (parmvec, 0) = arg2; | |
13648 | ||
13649 | /* Setup the argument vector, which contains the remaining | |
13650 | arguments. */ | |
13651 | for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta)) | |
13652 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
13653 | ||
13654 | deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec, | |
13655 | argvec, UNIFY_ALLOW_NONE, | |
13656 | /*call_args_p=*/false, | |
13657 | /*subr=*/0); | |
13658 | ||
13659 | /* We cannot deduce in the other direction, because ARG2 is | |
13660 | a pack expansion but ARG1 is not.*/ | |
13661 | deduce1 = 0; | |
13662 | } | |
13663 | ||
13664 | else | |
13665 | { | |
13666 | /* The normal case, where neither argument is a pack | |
13667 | expansion. */ | |
13668 | deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE); | |
13669 | deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE); | |
13670 | } | |
dda04398 NS |
13671 | |
13672 | if (!deduce1) | |
13673 | better2 = -1; | |
13674 | if (!deduce2) | |
13675 | better1 = -1; | |
13676 | if (better1 < 0 && better2 < 0) | |
13677 | /* We've failed to deduce something in either direction. | |
13678 | These must be unordered. */ | |
13679 | break; | |
c8094d83 | 13680 | |
dda04398 NS |
13681 | if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0) |
13682 | { | |
13683 | /* Deduces in both directions, see if quals can | |
13684 | disambiguate. Pretend the worse one failed to deduce. */ | |
13685 | if ((quals1 & quals2) == quals2) | |
13686 | deduce1 = 0; | |
13687 | if ((quals1 & quals2) == quals1) | |
13688 | deduce2 = 0; | |
13689 | } | |
13690 | if (deduce1 && !deduce2 && !better2) | |
13691 | better2 = 1; | |
13692 | if (deduce2 && !deduce1 && !better1) | |
13693 | better1 = 1; | |
c8094d83 | 13694 | |
5d80a306 DG |
13695 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
13696 | || TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
13697 | /* We have already processed all of the arguments in our | |
13698 | handing of the pack expansion type. */ | |
13699 | len = 0; | |
13700 | ||
dda04398 NS |
13701 | args1 = TREE_CHAIN (args1); |
13702 | args2 = TREE_CHAIN (args2); | |
63d34078 JJ |
13703 | |
13704 | /* Stop when an ellipsis is seen. */ | |
13705 | if (args1 == NULL_TREE || args2 == NULL_TREE) | |
13706 | break; | |
dda04398 NS |
13707 | } |
13708 | ||
13709 | processing_template_decl--; | |
13710 | ||
5d80a306 DG |
13711 | /* All things being equal, if the next argument is a pack expansion |
13712 | for one function but not for the other, prefer the | |
13713 | non-variadic function. */ | |
13714 | if ((better1 > 0) - (better2 > 0) == 0 | |
13715 | && args1 && TREE_VALUE (args1) | |
13716 | && args2 && TREE_VALUE (args2)) | |
13717 | { | |
13718 | if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION) | |
13719 | return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1; | |
13720 | else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION) | |
13721 | return 1; | |
13722 | } | |
13723 | ||
dda04398 | 13724 | return (better1 > 0) - (better2 > 0); |
73aad9b9 | 13725 | } |
6467930b | 13726 | |
916b63c3 | 13727 | /* Determine which of two partial specializations is more specialized. |
6467930b | 13728 | |
916b63c3 MM |
13729 | PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding |
13730 | to the first partial specialization. The TREE_VALUE is the | |
13731 | innermost set of template parameters for the partial | |
13732 | specialization. PAT2 is similar, but for the second template. | |
13733 | ||
13734 | Return 1 if the first partial specialization is more specialized; | |
13735 | -1 if the second is more specialized; 0 if neither is more | |
13736 | specialized. | |
55ece1b3 | 13737 | |
916b63c3 | 13738 | See [temp.class.order] for information about determining which of |
3db45ab5 | 13739 | two templates is more specialized. */ |
c8094d83 | 13740 | |
b5791fdc | 13741 | static int |
916b63c3 | 13742 | more_specialized_class (tree pat1, tree pat2) |
73aad9b9 JM |
13743 | { |
13744 | tree targs; | |
916b63c3 | 13745 | tree tmpl1, tmpl2; |
73aad9b9 | 13746 | int winner = 0; |
5d80a306 | 13747 | bool any_deductions = false; |
73aad9b9 | 13748 | |
916b63c3 MM |
13749 | tmpl1 = TREE_TYPE (pat1); |
13750 | tmpl2 = TREE_TYPE (pat2); | |
13751 | ||
c8094d83 | 13752 | /* Just like what happens for functions, if we are ordering between |
baa49730 GB |
13753 | different class template specializations, we may encounter dependent |
13754 | types in the arguments, and we need our dependency check functions | |
13755 | to behave correctly. */ | |
13756 | ++processing_template_decl; | |
3db45ab5 | 13757 | targs = get_class_bindings (TREE_VALUE (pat1), |
916b63c3 MM |
13758 | CLASSTYPE_TI_ARGS (tmpl1), |
13759 | CLASSTYPE_TI_ARGS (tmpl2)); | |
73aad9b9 | 13760 | if (targs) |
5d80a306 DG |
13761 | { |
13762 | --winner; | |
13763 | any_deductions = true; | |
13764 | } | |
73aad9b9 | 13765 | |
3db45ab5 | 13766 | targs = get_class_bindings (TREE_VALUE (pat2), |
916b63c3 MM |
13767 | CLASSTYPE_TI_ARGS (tmpl2), |
13768 | CLASSTYPE_TI_ARGS (tmpl1)); | |
73aad9b9 | 13769 | if (targs) |
5d80a306 DG |
13770 | { |
13771 | ++winner; | |
13772 | any_deductions = true; | |
13773 | } | |
baa49730 | 13774 | --processing_template_decl; |
6467930b | 13775 | |
5d80a306 DG |
13776 | /* In the case of a tie where at least one of the class templates |
13777 | has a parameter pack at the end, the template with the most | |
13778 | non-packed parameters wins. */ | |
13779 | if (winner == 0 | |
13780 | && any_deductions | |
13781 | && (template_args_variadic_p (TREE_PURPOSE (pat1)) | |
13782 | || template_args_variadic_p (TREE_PURPOSE (pat2)))) | |
13783 | { | |
13784 | tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1)); | |
13785 | tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2)); | |
13786 | int len1 = TREE_VEC_LENGTH (args1); | |
13787 | int len2 = TREE_VEC_LENGTH (args2); | |
13788 | ||
13789 | /* We don't count the pack expansion at the end. */ | |
13790 | if (template_args_variadic_p (TREE_PURPOSE (pat1))) | |
13791 | --len1; | |
13792 | if (template_args_variadic_p (TREE_PURPOSE (pat2))) | |
13793 | --len2; | |
13794 | ||
13795 | if (len1 > len2) | |
13796 | return 1; | |
13797 | else if (len1 < len2) | |
13798 | return -1; | |
13799 | } | |
13800 | ||
6467930b MS |
13801 | return winner; |
13802 | } | |
73aad9b9 JM |
13803 | |
13804 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 13805 | DECL from the function template FN, with the explicit template |
a34d3336 | 13806 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must |
4393e105 | 13807 | also match. Return NULL_TREE if no satisfactory arguments could be |
a34d3336 | 13808 | found. */ |
c8094d83 | 13809 | |
76b9a14d | 13810 | static tree |
a34d3336 | 13811 | get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype) |
73aad9b9 | 13812 | { |
98c1c668 | 13813 | int ntparms = DECL_NTPARMS (fn); |
f31c0a32 | 13814 | tree targs = make_tree_vec (ntparms); |
4393e105 | 13815 | tree decl_type; |
03017874 | 13816 | tree decl_arg_types; |
98c1c668 | 13817 | |
4393e105 MM |
13818 | /* Substitute the explicit template arguments into the type of DECL. |
13819 | The call to fn_type_unification will handle substitution into the | |
13820 | FN. */ | |
13821 | decl_type = TREE_TYPE (decl); | |
13822 | if (explicit_args && uses_template_parms (decl_type)) | |
13823 | { | |
13824 | tree tmpl; | |
13825 | tree converted_args; | |
13826 | ||
13827 | if (DECL_TEMPLATE_INFO (decl)) | |
13828 | tmpl = DECL_TI_TEMPLATE (decl); | |
13829 | else | |
0e339752 | 13830 | /* We can get here for some invalid specializations. */ |
4393e105 MM |
13831 | return NULL_TREE; |
13832 | ||
13833 | converted_args | |
e7e93965 MM |
13834 | = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), |
13835 | explicit_args, NULL_TREE, | |
3db45ab5 | 13836 | tf_none, |
e7e93965 MM |
13837 | /*require_all_args=*/false, |
13838 | /*use_default_args=*/false); | |
4393e105 MM |
13839 | if (converted_args == error_mark_node) |
13840 | return NULL_TREE; | |
c8094d83 MS |
13841 | |
13842 | decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); | |
4393e105 MM |
13843 | if (decl_type == error_mark_node) |
13844 | return NULL_TREE; | |
13845 | } | |
13846 | ||
e5214479 | 13847 | /* Never do unification on the 'this' parameter. */ |
ba139ba8 MM |
13848 | decl_arg_types = skip_artificial_parms_for (decl, |
13849 | TYPE_ARG_TYPES (decl_type)); | |
d7684f2d | 13850 | |
c8094d83 | 13851 | if (fn_type_unification (fn, explicit_args, targs, |
03017874 | 13852 | decl_arg_types, |
8d3631f8 | 13853 | (check_rettype || DECL_CONV_FN_P (fn) |
0cbd7506 | 13854 | ? TREE_TYPE (decl_type) : NULL_TREE), |
30f86ec3 | 13855 | DEDUCE_EXACT, LOOKUP_NORMAL)) |
76b9a14d JM |
13856 | return NULL_TREE; |
13857 | ||
76b9a14d JM |
13858 | return targs; |
13859 | } | |
13860 | ||
36a117a5 MM |
13861 | /* Return the innermost template arguments that, when applied to a |
13862 | template specialization whose innermost template parameters are | |
9471d3e2 | 13863 | TPARMS, and whose specialization arguments are PARMS, yield the |
c8094d83 | 13864 | ARGS. |
36a117a5 MM |
13865 | |
13866 | For example, suppose we have: | |
13867 | ||
13868 | template <class T, class U> struct S {}; | |
13869 | template <class T> struct S<T*, int> {}; | |
13870 | ||
13871 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
916b63c3 | 13872 | {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*, |
36a117a5 MM |
13873 | int}. The resulting vector will be {double}, indicating that `T' |
13874 | is bound to `double'. */ | |
13875 | ||
bd6dd845 | 13876 | static tree |
916b63c3 | 13877 | get_class_bindings (tree tparms, tree spec_args, tree args) |
73aad9b9 | 13878 | { |
3b3ba9f0 | 13879 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
916b63c3 MM |
13880 | tree deduced_args; |
13881 | tree innermost_deduced_args; | |
73aad9b9 | 13882 | |
916b63c3 MM |
13883 | innermost_deduced_args = make_tree_vec (ntparms); |
13884 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
13885 | { | |
13886 | deduced_args = copy_node (args); | |
13887 | SET_TMPL_ARGS_LEVEL (deduced_args, | |
13888 | TMPL_ARGS_DEPTH (deduced_args), | |
13889 | innermost_deduced_args); | |
13890 | } | |
13891 | else | |
3db45ab5 | 13892 | deduced_args = innermost_deduced_args; |
916b63c3 | 13893 | |
3db45ab5 MS |
13894 | if (unify (tparms, deduced_args, |
13895 | INNERMOST_TEMPLATE_ARGS (spec_args), | |
13896 | INNERMOST_TEMPLATE_ARGS (args), | |
0cbd7506 | 13897 | UNIFY_ALLOW_NONE)) |
fcfb9f96 | 13898 | return NULL_TREE; |
73aad9b9 JM |
13899 | |
13900 | for (i = 0; i < ntparms; ++i) | |
916b63c3 | 13901 | if (! TREE_VEC_ELT (innermost_deduced_args, i)) |
73aad9b9 JM |
13902 | return NULL_TREE; |
13903 | ||
916b63c3 MM |
13904 | /* Verify that nondeduced template arguments agree with the type |
13905 | obtained from argument deduction. | |
3db45ab5 | 13906 | |
916b63c3 MM |
13907 | For example: |
13908 | ||
13909 | struct A { typedef int X; }; | |
13910 | template <class T, class U> struct C {}; | |
13911 | template <class T> struct C<T, typename T::X> {}; | |
13912 | ||
13913 | Then with the instantiation `C<A, int>', we can deduce that | |
13914 | `T' is `A' but unify () does not check whether `typename T::X' | |
13915 | is `int'. */ | |
13916 | spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE); | |
13917 | if (spec_args == error_mark_node | |
13918 | /* We only need to check the innermost arguments; the other | |
13919 | arguments will always agree. */ | |
13920 | || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args), | |
13921 | INNERMOST_TEMPLATE_ARGS (args))) | |
74601d7c KL |
13922 | return NULL_TREE; |
13923 | ||
4cf36211 DG |
13924 | /* Now that we have bindings for all of the template arguments, |
13925 | ensure that the arguments deduced for the template template | |
13926 | parameters have compatible template parameter lists. See the use | |
13927 | of template_template_parm_bindings_ok_p in fn_type_unification | |
13928 | for more information. */ | |
13929 | if (!template_template_parm_bindings_ok_p (tparms, deduced_args)) | |
13930 | return NULL_TREE; | |
13931 | ||
916b63c3 | 13932 | return deduced_args; |
73aad9b9 JM |
13933 | } |
13934 | ||
7ca383e6 MM |
13935 | /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL. |
13936 | Return the TREE_LIST node with the most specialized template, if | |
13937 | any. If there is no most specialized template, the error_mark_node | |
13938 | is returned. | |
13939 | ||
13940 | Note that this function does not look at, or modify, the | |
13941 | TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node | |
13942 | returned is one of the elements of INSTANTIATIONS, callers may | |
13943 | store information in the TREE_PURPOSE or TREE_TYPE of the nodes, | |
13944 | and retrieve it from the value returned. */ | |
73aad9b9 JM |
13945 | |
13946 | tree | |
7ca383e6 | 13947 | most_specialized_instantiation (tree templates) |
73aad9b9 | 13948 | { |
104bf76a | 13949 | tree fn, champ; |
73aad9b9 | 13950 | |
dda04398 | 13951 | ++processing_template_decl; |
c8094d83 | 13952 | |
7ca383e6 MM |
13953 | champ = templates; |
13954 | for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 13955 | { |
dda04398 | 13956 | int fate = 0; |
c8094d83 | 13957 | |
a34d3336 NS |
13958 | if (get_bindings (TREE_VALUE (champ), |
13959 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
13960 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
13961 | fate--; |
13962 | ||
a34d3336 NS |
13963 | if (get_bindings (TREE_VALUE (fn), |
13964 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
13965 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 | 13966 | fate++; |
c8094d83 | 13967 | |
8ca4bf25 MM |
13968 | if (fate == -1) |
13969 | champ = fn; | |
13970 | else if (!fate) | |
73aad9b9 | 13971 | { |
8ca4bf25 MM |
13972 | /* Equally specialized, move to next function. If there |
13973 | is no next function, nothing's most specialized. */ | |
13974 | fn = TREE_CHAIN (fn); | |
7ca383e6 | 13975 | champ = fn; |
8ca4bf25 MM |
13976 | if (!fn) |
13977 | break; | |
73aad9b9 JM |
13978 | } |
13979 | } | |
c8094d83 | 13980 | |
dda04398 NS |
13981 | if (champ) |
13982 | /* Now verify that champ is better than everything earlier in the | |
13983 | instantiation list. */ | |
7ca383e6 | 13984 | for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) |
a34d3336 NS |
13985 | if (get_bindings (TREE_VALUE (champ), |
13986 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
13987 | NULL_TREE, /*check_ret=*/false) | |
13988 | || !get_bindings (TREE_VALUE (fn), | |
13989 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
13990 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
13991 | { |
13992 | champ = NULL_TREE; | |
13993 | break; | |
13994 | } | |
c8094d83 | 13995 | |
dda04398 | 13996 | processing_template_decl--; |
c8094d83 | 13997 | |
dda04398 NS |
13998 | if (!champ) |
13999 | return error_mark_node; | |
73aad9b9 | 14000 | |
7ca383e6 | 14001 | return champ; |
73aad9b9 JM |
14002 | } |
14003 | ||
36a117a5 | 14004 | /* If DECL is a specialization of some template, return the most |
f9a7ae04 MM |
14005 | general such template. Otherwise, returns NULL_TREE. |
14006 | ||
14007 | For example, given: | |
36a117a5 MM |
14008 | |
14009 | template <class T> struct S { template <class U> void f(U); }; | |
14010 | ||
14011 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
14012 | the full template. This function will not trace past partial | |
14013 | specializations, however. For example, given in addition: | |
14014 | ||
14015 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
14016 | ||
14017 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
14018 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 14019 | |
612c671a | 14020 | tree |
3a978d72 | 14021 | most_general_template (tree decl) |
73aad9b9 | 14022 | { |
f9a7ae04 MM |
14023 | /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is |
14024 | an immediate specialization. */ | |
14025 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
14026 | { | |
14027 | if (DECL_TEMPLATE_INFO (decl)) { | |
14028 | decl = DECL_TI_TEMPLATE (decl); | |
14029 | ||
14030 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a | |
14031 | template friend. */ | |
14032 | if (TREE_CODE (decl) != TEMPLATE_DECL) | |
14033 | return NULL_TREE; | |
14034 | } else | |
14035 | return NULL_TREE; | |
14036 | } | |
14037 | ||
14038 | /* Look for more and more general templates. */ | |
14039 | while (DECL_TEMPLATE_INFO (decl)) | |
14040 | { | |
10b1d5e7 MM |
14041 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases. |
14042 | (See cp-tree.h for details.) */ | |
f9a7ae04 MM |
14043 | if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
14044 | break; | |
14045 | ||
6e049fcd KL |
14046 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
14047 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) | |
14048 | break; | |
14049 | ||
f9a7ae04 MM |
14050 | /* Stop if we run into an explicitly specialized class template. */ |
14051 | if (!DECL_NAMESPACE_SCOPE_P (decl) | |
14052 | && DECL_CONTEXT (decl) | |
14053 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) | |
14054 | break; | |
14055 | ||
14056 | decl = DECL_TI_TEMPLATE (decl); | |
14057 | } | |
36a117a5 MM |
14058 | |
14059 | return decl; | |
14060 | } | |
14061 | ||
916b63c3 MM |
14062 | /* Return the most specialized of the class template partial |
14063 | specializations of TMPL which can produce TYPE, a specialization of | |
14064 | TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is | |
14065 | a _TYPE node corresponding to the partial specialization, while the | |
14066 | TREE_PURPOSE is the set of template arguments that must be | |
14067 | substituted into the TREE_TYPE in order to generate TYPE. | |
14068 | ||
14069 | If the choice of partial specialization is ambiguous, a diagnostic | |
14070 | is issued, and the error_mark_node is returned. If there are no | |
14071 | partial specializations of TMPL matching TYPE, then NULL_TREE is | |
14072 | returned. */ | |
36a117a5 | 14073 | |
e9659ab0 | 14074 | static tree |
916b63c3 | 14075 | most_specialized_class (tree type, tree tmpl) |
36a117a5 MM |
14076 | { |
14077 | tree list = NULL_TREE; | |
14078 | tree t; | |
14079 | tree champ; | |
73aad9b9 | 14080 | int fate; |
916b63c3 MM |
14081 | bool ambiguous_p; |
14082 | tree args; | |
dc28490d | 14083 | tree outer_args = NULL_TREE; |
73aad9b9 | 14084 | |
36a117a5 | 14085 | tmpl = most_general_template (tmpl); |
916b63c3 | 14086 | args = CLASSTYPE_TI_ARGS (type); |
dc28490d JM |
14087 | |
14088 | /* For determining which partial specialization to use, only the | |
14089 | innermost args are interesting. */ | |
14090 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
14091 | { | |
14092 | outer_args = strip_innermost_template_args (args, 1); | |
14093 | args = INNERMOST_TEMPLATE_ARGS (args); | |
14094 | } | |
14095 | ||
36a117a5 | 14096 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) |
73aad9b9 | 14097 | { |
916b63c3 MM |
14098 | tree partial_spec_args; |
14099 | tree spec_args; | |
dc28490d | 14100 | tree parms = TREE_VALUE (t); |
916b63c3 MM |
14101 | |
14102 | partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t)); | |
dc28490d JM |
14103 | if (outer_args) |
14104 | { | |
14105 | int i; | |
14106 | ||
fd452cef JM |
14107 | ++processing_template_decl; |
14108 | ||
dc28490d JM |
14109 | /* Discard the outer levels of args, and then substitute in the |
14110 | template args from the enclosing class. */ | |
14111 | partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args); | |
14112 | partial_spec_args = tsubst_template_args | |
14113 | (partial_spec_args, outer_args, tf_none, NULL_TREE); | |
14114 | ||
14115 | /* PARMS already refers to just the innermost parms, but the | |
14116 | template parms in partial_spec_args had their levels lowered | |
14117 | by tsubst, so we need to do the same for the parm list. We | |
14118 | can't just tsubst the TREE_VEC itself, as tsubst wants to | |
14119 | treat a TREE_VEC as an argument vector. */ | |
14120 | parms = copy_node (parms); | |
14121 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) | |
14122 | TREE_VEC_ELT (parms, i) = | |
14123 | tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE); | |
fd452cef JM |
14124 | |
14125 | --processing_template_decl; | |
dc28490d JM |
14126 | } |
14127 | spec_args = get_class_bindings (parms, | |
3db45ab5 | 14128 | partial_spec_args, |
916b63c3 | 14129 | args); |
36a117a5 | 14130 | if (spec_args) |
73aad9b9 | 14131 | { |
dc28490d JM |
14132 | if (outer_args) |
14133 | spec_args = add_to_template_args (outer_args, spec_args); | |
916b63c3 | 14134 | list = tree_cons (spec_args, TREE_VALUE (t), list); |
73aad9b9 JM |
14135 | TREE_TYPE (list) = TREE_TYPE (t); |
14136 | } | |
14137 | } | |
14138 | ||
14139 | if (! list) | |
14140 | return NULL_TREE; | |
14141 | ||
916b63c3 | 14142 | ambiguous_p = false; |
73aad9b9 JM |
14143 | t = list; |
14144 | champ = t; | |
14145 | t = TREE_CHAIN (t); | |
14146 | for (; t; t = TREE_CHAIN (t)) | |
14147 | { | |
916b63c3 | 14148 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
14149 | if (fate == 1) |
14150 | ; | |
14151 | else | |
14152 | { | |
14153 | if (fate == 0) | |
14154 | { | |
14155 | t = TREE_CHAIN (t); | |
14156 | if (! t) | |
916b63c3 MM |
14157 | { |
14158 | ambiguous_p = true; | |
14159 | break; | |
14160 | } | |
73aad9b9 JM |
14161 | } |
14162 | champ = t; | |
14163 | } | |
14164 | } | |
14165 | ||
916b63c3 MM |
14166 | if (!ambiguous_p) |
14167 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
14168 | { | |
14169 | fate = more_specialized_class (champ, t); | |
14170 | if (fate != 1) | |
14171 | { | |
14172 | ambiguous_p = true; | |
14173 | break; | |
14174 | } | |
14175 | } | |
14176 | ||
14177 | if (ambiguous_p) | |
73aad9b9 | 14178 | { |
916b63c3 MM |
14179 | const char *str = "candidates are:"; |
14180 | error ("ambiguous class template instantiation for %q#T", type); | |
14181 | for (t = list; t; t = TREE_CHAIN (t)) | |
14182 | { | |
14183 | error ("%s %+#T", str, TREE_TYPE (t)); | |
14184 | str = " "; | |
14185 | } | |
14186 | return error_mark_node; | |
73aad9b9 JM |
14187 | } |
14188 | ||
14189 | return champ; | |
14190 | } | |
14191 | ||
eb8845be | 14192 | /* Explicitly instantiate DECL. */ |
e92cc029 | 14193 | |
8d08fdba | 14194 | void |
eb8845be | 14195 | do_decl_instantiation (tree decl, tree storage) |
8d08fdba | 14196 | { |
8d08fdba | 14197 | tree result = NULL_TREE; |
faae18ab | 14198 | int extern_p = 0; |
e8abc66f | 14199 | |
ad47b891 | 14200 | if (!decl || decl == error_mark_node) |
dc957d14 | 14201 | /* An error occurred, for which grokdeclarator has already issued |
3fa56191 MM |
14202 | an appropriate message. */ |
14203 | return; | |
14204 | else if (! DECL_LANG_SPECIFIC (decl)) | |
ec255269 | 14205 | { |
0f51ccfc | 14206 | error ("explicit instantiation of non-template %q#D", decl); |
ec255269 MS |
14207 | return; |
14208 | } | |
03d0f4af | 14209 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 14210 | { |
03d0f4af MM |
14211 | /* There is an asymmetry here in the way VAR_DECLs and |
14212 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
14213 | the latter, the DECL we get back will be marked as a | |
14214 | template instantiation, and the appropriate | |
14215 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
14216 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
14217 | should handle VAR_DECLs as it currently handles | |
14218 | FUNCTION_DECLs. */ | |
8259e4f5 PC |
14219 | if (!DECL_CLASS_SCOPE_P (decl)) |
14220 | { | |
14221 | error ("%qD is not a static data member of a class template", decl); | |
14222 | return; | |
14223 | } | |
86ac0575 | 14224 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); |
e0860732 | 14225 | if (!result || TREE_CODE (result) != VAR_DECL) |
03d0f4af | 14226 | { |
0f51ccfc | 14227 | error ("no matching template for %qD found", decl); |
03d0f4af MM |
14228 | return; |
14229 | } | |
8ea6dfae SM |
14230 | if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl))) |
14231 | { | |
14232 | error ("type %qT for explicit instantiation %qD does not match " | |
14233 | "declared type %qT", TREE_TYPE (result), decl, | |
14234 | TREE_TYPE (decl)); | |
14235 | return; | |
14236 | } | |
6633d636 MS |
14237 | } |
14238 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
14239 | { | |
0f51ccfc | 14240 | error ("explicit instantiation of %q#D", decl); |
6633d636 MS |
14241 | return; |
14242 | } | |
03d0f4af MM |
14243 | else |
14244 | result = decl; | |
672476cb | 14245 | |
03d0f4af | 14246 | /* Check for various error cases. Note that if the explicit |
0e339752 | 14247 | instantiation is valid the RESULT will currently be marked as an |
03d0f4af MM |
14248 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set |
14249 | until we get here. */ | |
14250 | ||
14251 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 14252 | { |
07782718 KL |
14253 | /* DR 259 [temp.spec]. |
14254 | ||
14255 | Both an explicit instantiation and a declaration of an explicit | |
14256 | specialization shall not appear in a program unless the explicit | |
14257 | instantiation follows a declaration of the explicit specialization. | |
03d0f4af | 14258 | |
07782718 KL |
14259 | For a given set of template parameters, if an explicit |
14260 | instantiation of a template appears after a declaration of an | |
14261 | explicit specialization for that template, the explicit | |
14262 | instantiation has no effect. */ | |
672476cb MM |
14263 | return; |
14264 | } | |
03d0f4af MM |
14265 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
14266 | { | |
14267 | /* [temp.spec] | |
98c1c668 | 14268 | |
03d0f4af | 14269 | No program shall explicitly instantiate any template more |
c8094d83 | 14270 | than once. |
03d0f4af | 14271 | |
4684cd27 MM |
14272 | We check DECL_NOT_REALLY_EXTERN so as not to complain when |
14273 | the first instantiation was `extern' and the second is not, | |
14274 | and EXTERN_P for the opposite case. */ | |
14275 | if (DECL_NOT_REALLY_EXTERN (result) && !extern_p) | |
0f51ccfc | 14276 | pedwarn ("duplicate explicit instantiation of %q#D", result); |
4684cd27 MM |
14277 | /* If an "extern" explicit instantiation follows an ordinary |
14278 | explicit instantiation, the template is instantiated. */ | |
14279 | if (extern_p) | |
03d0f4af MM |
14280 | return; |
14281 | } | |
14282 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 14283 | { |
0f51ccfc | 14284 | error ("no matching template for %qD found", result); |
faae18ab MS |
14285 | return; |
14286 | } | |
03d0f4af | 14287 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 14288 | { |
0f51ccfc | 14289 | pedwarn ("explicit instantiation of non-template %q#D", result); |
6633d636 MS |
14290 | return; |
14291 | } | |
14292 | ||
f0e01782 | 14293 | if (storage == NULL_TREE) |
00595019 | 14294 | ; |
faae18ab | 14295 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af | 14296 | { |
c02f5e29 | 14297 | if (pedantic && !in_system_header) |
0f51ccfc | 14298 | pedwarn ("ISO C++ forbids the use of %<extern%> on explicit " |
0cbd7506 | 14299 | "instantiations"); |
03d0f4af MM |
14300 | extern_p = 1; |
14301 | } | |
f0e01782 | 14302 | else |
0f51ccfc | 14303 | error ("storage class %qD applied to template instantiation", storage); |
3db45ab5 | 14304 | |
9c12301f | 14305 | check_explicit_instantiation_namespace (result); |
5566b478 | 14306 | mark_decl_instantiated (result, extern_p); |
c91a56d2 | 14307 | if (! extern_p) |
3db45ab5 | 14308 | instantiate_decl (result, /*defer_ok=*/1, |
eba839f9 | 14309 | /*expl_inst_class_mem_p=*/false); |
7177d104 MS |
14310 | } |
14311 | ||
b5791fdc | 14312 | static void |
3a978d72 | 14313 | mark_class_instantiated (tree t, int extern_p) |
faae18ab MS |
14314 | { |
14315 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
14316 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
14317 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
14318 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; |
14319 | if (! extern_p) | |
14320 | { | |
14321 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
14322 | rest_of_type_compilation (t, 1); | |
14323 | } | |
c8094d83 | 14324 | } |
e8abc66f | 14325 | |
5e0c54e5 | 14326 | /* Called from do_type_instantiation through binding_table_foreach to |
9bcb9aae | 14327 | do recursive instantiation for the type bound in ENTRY. */ |
5e0c54e5 GDR |
14328 | static void |
14329 | bt_instantiate_type_proc (binding_entry entry, void *data) | |
14330 | { | |
14331 | tree storage = *(tree *) data; | |
14332 | ||
14333 | if (IS_AGGR_TYPE (entry->type) | |
14334 | && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type))) | |
14335 | do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0); | |
14336 | } | |
14337 | ||
415c974c MA |
14338 | /* Called from do_type_instantiation to instantiate a member |
14339 | (a member function or a static member variable) of an | |
03fd3f84 | 14340 | explicitly instantiated class template. */ |
415c974c MA |
14341 | static void |
14342 | instantiate_class_member (tree decl, int extern_p) | |
14343 | { | |
14344 | mark_decl_instantiated (decl, extern_p); | |
415c974c | 14345 | if (! extern_p) |
3db45ab5 | 14346 | instantiate_decl (decl, /*defer_ok=*/1, |
eba839f9 | 14347 | /*expl_inst_class_mem_p=*/true); |
415c974c MA |
14348 | } |
14349 | ||
a1bcc528 JM |
14350 | /* Perform an explicit instantiation of template class T. STORAGE, if |
14351 | non-null, is the RID for extern, inline or static. COMPLAIN is | |
838dfd8a | 14352 | nonzero if this is called from the parser, zero if called recursively, |
a1bcc528 | 14353 | since the standard is unclear (as detailed below). */ |
c8094d83 | 14354 | |
7177d104 | 14355 | void |
3a978d72 | 14356 | do_type_instantiation (tree t, tree storage, tsubst_flags_t complain) |
7177d104 | 14357 | { |
e8abc66f MS |
14358 | int extern_p = 0; |
14359 | int nomem_p = 0; | |
5566b478 | 14360 | int static_p = 0; |
4746cf84 | 14361 | int previous_instantiation_extern_p = 0; |
5566b478 | 14362 | |
ca79f85d JM |
14363 | if (TREE_CODE (t) == TYPE_DECL) |
14364 | t = TREE_TYPE (t); | |
14365 | ||
7ddedda4 | 14366 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 14367 | { |
0f51ccfc | 14368 | error ("explicit instantiation of non-template type %qT", t); |
ca79f85d JM |
14369 | return; |
14370 | } | |
14371 | ||
5566b478 | 14372 | complete_type (t); |
7177d104 | 14373 | |
d0f062fb | 14374 | if (!COMPLETE_TYPE_P (t)) |
f0e01782 | 14375 | { |
c2ea3a40 | 14376 | if (complain & tf_error) |
0f51ccfc | 14377 | error ("explicit instantiation of %q#T before definition of template", |
0cbd7506 | 14378 | t); |
f0e01782 MS |
14379 | return; |
14380 | } | |
14381 | ||
03d0f4af | 14382 | if (storage != NULL_TREE) |
f0e01782 | 14383 | { |
c02f5e29 | 14384 | if (pedantic && !in_system_header) |
c8094d83 | 14385 | pedwarn("ISO C++ forbids the use of %qE on explicit instantiations", |
0cbd7506 | 14386 | storage); |
03d0f4af MM |
14387 | |
14388 | if (storage == ridpointers[(int) RID_INLINE]) | |
14389 | nomem_p = 1; | |
14390 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
14391 | extern_p = 1; | |
14392 | else if (storage == ridpointers[(int) RID_STATIC]) | |
14393 | static_p = 1; | |
14394 | else | |
14395 | { | |
0f51ccfc | 14396 | error ("storage class %qD applied to template instantiation", |
0cbd7506 | 14397 | storage); |
03d0f4af MM |
14398 | extern_p = 0; |
14399 | } | |
f0e01782 MS |
14400 | } |
14401 | ||
370af2d5 | 14402 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
03d0f4af | 14403 | { |
07782718 | 14404 | /* DR 259 [temp.spec]. |
a292b002 | 14405 | |
07782718 KL |
14406 | Both an explicit instantiation and a declaration of an explicit |
14407 | specialization shall not appear in a program unless the explicit | |
14408 | instantiation follows a declaration of the explicit specialization. | |
14409 | ||
14410 | For a given set of template parameters, if an explicit | |
14411 | instantiation of a template appears after a declaration of an | |
14412 | explicit specialization for that template, the explicit | |
14413 | instantiation has no effect. */ | |
03d0f4af MM |
14414 | return; |
14415 | } | |
14416 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 14417 | { |
03d0f4af MM |
14418 | /* [temp.spec] |
14419 | ||
14420 | No program shall explicitly instantiate any template more | |
c8094d83 | 14421 | than once. |
03d0f4af | 14422 | |
0cbd7506 | 14423 | If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit |
4746cf84 | 14424 | instantiation was `extern'. If EXTERN_P then the second is. |
4684cd27 | 14425 | These cases are OK. */ |
4746cf84 MA |
14426 | previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t); |
14427 | ||
14428 | if (!previous_instantiation_extern_p && !extern_p | |
c2ea3a40 | 14429 | && (complain & tf_error)) |
0f51ccfc | 14430 | pedwarn ("duplicate explicit instantiation of %q#T", t); |
c8094d83 | 14431 | |
03d0f4af MM |
14432 | /* If we've already instantiated the template, just return now. */ |
14433 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
14434 | return; | |
44a8d0b3 | 14435 | } |
e8abc66f | 14436 | |
9c12301f | 14437 | check_explicit_instantiation_namespace (TYPE_NAME (t)); |
03d0f4af | 14438 | mark_class_instantiated (t, extern_p); |
03d0f4af | 14439 | |
e8abc66f MS |
14440 | if (nomem_p) |
14441 | return; | |
14442 | ||
7177d104 | 14443 | { |
db5ae43f | 14444 | tree tmp; |
5566b478 | 14445 | |
03d0f4af MM |
14446 | /* In contrast to implicit instantiation, where only the |
14447 | declarations, and not the definitions, of members are | |
14448 | instantiated, we have here: | |
14449 | ||
0cbd7506 | 14450 | [temp.explicit] |
03d0f4af MM |
14451 | |
14452 | The explicit instantiation of a class template specialization | |
14453 | implies the instantiation of all of its members not | |
14454 | previously explicitly specialized in the translation unit | |
c8094d83 | 14455 | containing the explicit instantiation. |
03d0f4af MM |
14456 | |
14457 | Of course, we can't instantiate member template classes, since | |
14458 | we don't have any arguments for them. Note that the standard | |
dc957d14 | 14459 | is unclear on whether the instantiation of the members are |
415c974c | 14460 | *explicit* instantiations or not. However, the most natural |
03fd3f84 | 14461 | interpretation is that it should be an explicit instantiation. */ |
03d0f4af | 14462 | |
5566b478 MS |
14463 | if (! static_p) |
14464 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 14465 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 14466 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
415c974c | 14467 | instantiate_class_member (tmp, extern_p); |
5566b478 MS |
14468 | |
14469 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
14470 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
415c974c | 14471 | instantiate_class_member (tmp, extern_p); |
7177d104 | 14472 | |
5e0c54e5 GDR |
14473 | if (CLASSTYPE_NESTED_UTDS (t)) |
14474 | binding_table_foreach (CLASSTYPE_NESTED_UTDS (t), | |
0cbd7506 | 14475 | bt_instantiate_type_proc, &storage); |
a292b002 | 14476 | } |
8d08fdba | 14477 | } |
a28e3c7f | 14478 | |
36a117a5 MM |
14479 | /* Given a function DECL, which is a specialization of TMPL, modify |
14480 | DECL to be a re-instantiation of TMPL with the same template | |
14481 | arguments. TMPL should be the template into which tsubst'ing | |
14482 | should occur for DECL, not the most general template. | |
830bfa74 MM |
14483 | |
14484 | One reason for doing this is a scenario like this: | |
14485 | ||
14486 | template <class T> | |
14487 | void f(const T&, int i); | |
14488 | ||
14489 | void g() { f(3, 7); } | |
14490 | ||
14491 | template <class T> | |
14492 | void f(const T& t, const int i) { } | |
14493 | ||
14494 | Note that when the template is first instantiated, with | |
14495 | instantiate_template, the resulting DECL will have no name for the | |
14496 | first parameter, and the wrong type for the second. So, when we go | |
14497 | to instantiate the DECL, we regenerate it. */ | |
14498 | ||
e9659ab0 | 14499 | static void |
3a978d72 | 14500 | regenerate_decl_from_template (tree decl, tree tmpl) |
830bfa74 | 14501 | { |
f9a7ae04 MM |
14502 | /* The arguments used to instantiate DECL, from the most general |
14503 | template. */ | |
830bfa74 | 14504 | tree args; |
830bfa74 | 14505 | tree code_pattern; |
830bfa74 MM |
14506 | |
14507 | args = DECL_TI_ARGS (decl); | |
14508 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
14509 | ||
2b907f5c KL |
14510 | /* Make sure that we can see identifiers, and compute access |
14511 | correctly. */ | |
14512 | push_access_scope (decl); | |
14513 | ||
c7222c02 MM |
14514 | if (TREE_CODE (decl) == FUNCTION_DECL) |
14515 | { | |
14516 | tree decl_parm; | |
14517 | tree pattern_parm; | |
14518 | tree specs; | |
14519 | int args_depth; | |
14520 | int parms_depth; | |
c8094d83 | 14521 | |
c7222c02 | 14522 | args_depth = TMPL_ARGS_DEPTH (args); |
c8094d83 | 14523 | parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
c7222c02 MM |
14524 | if (args_depth > parms_depth) |
14525 | args = get_innermost_template_args (args, parms_depth); | |
14526 | ||
14527 | specs = tsubst_exception_specification (TREE_TYPE (code_pattern), | |
14528 | args, tf_error, NULL_TREE); | |
14529 | if (specs) | |
14530 | TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), | |
14531 | specs); | |
14532 | ||
14533 | /* Merge parameter declarations. */ | |
c8094d83 | 14534 | decl_parm = skip_artificial_parms_for (decl, |
c7222c02 | 14535 | DECL_ARGUMENTS (decl)); |
c8094d83 | 14536 | pattern_parm |
c7222c02 MM |
14537 | = skip_artificial_parms_for (code_pattern, |
14538 | DECL_ARGUMENTS (code_pattern)); | |
5d80a306 | 14539 | while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm)) |
c7222c02 MM |
14540 | { |
14541 | tree parm_type; | |
b17bba6d | 14542 | tree attributes; |
5d80a306 | 14543 | |
c7222c02 MM |
14544 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) |
14545 | DECL_NAME (decl_parm) = DECL_NAME (pattern_parm); | |
14546 | parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error, | |
14547 | NULL_TREE); | |
02bab9db | 14548 | parm_type = type_decays_to (parm_type); |
c7222c02 MM |
14549 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) |
14550 | TREE_TYPE (decl_parm) = parm_type; | |
b17bba6d MM |
14551 | attributes = DECL_ATTRIBUTES (pattern_parm); |
14552 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
14553 | { | |
14554 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
14555 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
14556 | } | |
c7222c02 MM |
14557 | decl_parm = TREE_CHAIN (decl_parm); |
14558 | pattern_parm = TREE_CHAIN (pattern_parm); | |
14559 | } | |
5d80a306 DG |
14560 | /* Merge any parameters that match with the function parameter |
14561 | pack. */ | |
14562 | if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm)) | |
14563 | { | |
14564 | int i, len; | |
14565 | tree expanded_types; | |
14566 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
14567 | the parameters in this function parameter pack. */ | |
14568 | expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm), | |
14569 | args, tf_error, NULL_TREE); | |
14570 | len = TREE_VEC_LENGTH (expanded_types); | |
14571 | for (i = 0; i < len; i++) | |
14572 | { | |
14573 | tree parm_type; | |
14574 | tree attributes; | |
14575 | ||
14576 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) | |
14577 | /* Rename the parameter to include the index. */ | |
14578 | DECL_NAME (decl_parm) = | |
14579 | make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i); | |
14580 | parm_type = TREE_VEC_ELT (expanded_types, i); | |
14581 | parm_type = type_decays_to (parm_type); | |
14582 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) | |
14583 | TREE_TYPE (decl_parm) = parm_type; | |
14584 | attributes = DECL_ATTRIBUTES (pattern_parm); | |
14585 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
14586 | { | |
14587 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
14588 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
14589 | } | |
14590 | decl_parm = TREE_CHAIN (decl_parm); | |
14591 | } | |
14592 | } | |
c7222c02 MM |
14593 | /* Merge additional specifiers from the CODE_PATTERN. */ |
14594 | if (DECL_DECLARED_INLINE_P (code_pattern) | |
14595 | && !DECL_DECLARED_INLINE_P (decl)) | |
14596 | DECL_DECLARED_INLINE_P (decl) = 1; | |
14597 | if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl)) | |
14598 | DECL_INLINE (decl) = 1; | |
14599 | } | |
14600 | else if (TREE_CODE (decl) == VAR_DECL) | |
b794e321 MM |
14601 | DECL_INITIAL (decl) = |
14602 | tsubst_expr (DECL_INITIAL (code_pattern), args, | |
015c2c66 MM |
14603 | tf_error, DECL_TI_TEMPLATE (decl), |
14604 | /*integral_constant_expression_p=*/false); | |
c7222c02 MM |
14605 | else |
14606 | gcc_unreachable (); | |
36a117a5 | 14607 | |
2b59fc25 | 14608 | pop_access_scope (decl); |
830bfa74 MM |
14609 | } |
14610 | ||
a723baf1 MM |
14611 | /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be |
14612 | substituted to get DECL. */ | |
14613 | ||
d58b7c2d | 14614 | tree |
a723baf1 MM |
14615 | template_for_substitution (tree decl) |
14616 | { | |
14617 | tree tmpl = DECL_TI_TEMPLATE (decl); | |
14618 | ||
14619 | /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern | |
14620 | for the instantiation. This is not always the most general | |
14621 | template. Consider, for example: | |
14622 | ||
0cbd7506 | 14623 | template <class T> |
a723baf1 | 14624 | struct S { template <class U> void f(); |
0cbd7506 | 14625 | template <> void f<int>(); }; |
a723baf1 MM |
14626 | |
14627 | and an instantiation of S<double>::f<int>. We want TD to be the | |
14628 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
14629 | while (/* An instantiation cannot have a definition, so we need a | |
14630 | more general template. */ | |
14631 | DECL_TEMPLATE_INSTANTIATION (tmpl) | |
14632 | /* We must also deal with friend templates. Given: | |
14633 | ||
c8094d83 | 14634 | template <class T> struct S { |
a723baf1 MM |
14635 | template <class U> friend void f() {}; |
14636 | }; | |
14637 | ||
14638 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
14639 | so far as the language is concerned, but that's still | |
14640 | where we get the pattern for the instantiation from. On | |
14641 | other hand, if the definition comes outside the class, say: | |
14642 | ||
c8094d83 | 14643 | template <class T> struct S { |
a723baf1 MM |
14644 | template <class U> friend void f(); |
14645 | }; | |
14646 | template <class U> friend void f() {} | |
14647 | ||
14648 | we don't need to look any further. That's what the check for | |
14649 | DECL_INITIAL is for. */ | |
14650 | || (TREE_CODE (decl) == FUNCTION_DECL | |
14651 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) | |
14652 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) | |
14653 | { | |
14654 | /* The present template, TD, should not be a definition. If it | |
14655 | were a definition, we should be using it! Note that we | |
14656 | cannot restructure the loop to just keep going until we find | |
14657 | a template with a definition, since that might go too far if | |
14658 | a specialization was declared, but not defined. */ | |
50bc768d NS |
14659 | gcc_assert (TREE_CODE (decl) != VAR_DECL |
14660 | || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))); | |
c8094d83 | 14661 | |
a723baf1 MM |
14662 | /* Fetch the more general template. */ |
14663 | tmpl = DECL_TI_TEMPLATE (tmpl); | |
14664 | } | |
14665 | ||
14666 | return tmpl; | |
14667 | } | |
14668 | ||
16d53b64 | 14669 | /* Produce the definition of D, a _DECL generated from a template. If |
838dfd8a | 14670 | DEFER_OK is nonzero, then we don't have to actually do the |
415c974c MA |
14671 | instantiation now; we just have to do it sometime. Normally it is |
14672 | an error if this is an explicit instantiation but D is undefined. | |
eba839f9 MM |
14673 | EXPL_INST_CLASS_MEM_P is true iff D is a member of an |
14674 | explicitly instantiated class template. */ | |
f84b4be9 | 14675 | |
a28e3c7f | 14676 | tree |
3db45ab5 | 14677 | instantiate_decl (tree d, int defer_ok, |
eba839f9 | 14678 | bool expl_inst_class_mem_p) |
a28e3c7f | 14679 | { |
36a117a5 | 14680 | tree tmpl = DECL_TI_TEMPLATE (d); |
65f8b0fb MM |
14681 | tree gen_args; |
14682 | tree args; | |
830bfa74 | 14683 | tree td; |
36a117a5 MM |
14684 | tree code_pattern; |
14685 | tree spec; | |
14686 | tree gen_tmpl; | |
b794e321 | 14687 | bool pattern_defined; |
31a714f6 | 14688 | int need_push; |
82a98427 | 14689 | location_t saved_loc = input_location; |
912291ca | 14690 | int saved_in_system_header = in_system_header; |
f7e4e484 | 14691 | bool external_p; |
c8094d83 | 14692 | |
36a117a5 MM |
14693 | /* This function should only be used to instantiate templates for |
14694 | functions and static member variables. */ | |
50bc768d NS |
14695 | gcc_assert (TREE_CODE (d) == FUNCTION_DECL |
14696 | || TREE_CODE (d) == VAR_DECL); | |
36a117a5 | 14697 | |
cec24319 MM |
14698 | /* Variables are never deferred; if instantiation is required, they |
14699 | are instantiated right away. That allows for better code in the | |
14700 | case that an expression refers to the value of the variable -- | |
14701 | if the variable has a constant value the referring expression can | |
14702 | take advantage of that fact. */ | |
14703 | if (TREE_CODE (d) == VAR_DECL) | |
14704 | defer_ok = 0; | |
14705 | ||
db9b2174 MM |
14706 | /* Don't instantiate cloned functions. Instead, instantiate the |
14707 | functions they cloned. */ | |
14708 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) | |
14709 | d = DECL_CLONED_FUNCTION (d); | |
14710 | ||
fbf1c34b | 14711 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
03d0f4af | 14712 | /* D has already been instantiated. It might seem reasonable to |
dc957d14 | 14713 | check whether or not D is an explicit instantiation, and, if so, |
03d0f4af MM |
14714 | stop here. But when an explicit instantiation is deferred |
14715 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
14716 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
14717 | return d; |
14718 | ||
14719 | /* If we already have a specialization of this declaration, then | |
14720 | there's no reason to instantiate it. Note that | |
14721 | retrieve_specialization gives us both instantiations and | |
14722 | specializations, so we must explicitly check | |
14723 | DECL_TEMPLATE_SPECIALIZATION. */ | |
14724 | gen_tmpl = most_general_template (tmpl); | |
65f8b0fb | 14725 | gen_args = DECL_TI_ARGS (d); |
c7222c02 MM |
14726 | spec = retrieve_specialization (gen_tmpl, gen_args, |
14727 | /*class_specializations_p=*/false); | |
36a117a5 MM |
14728 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) |
14729 | return spec; | |
14730 | ||
14731 | /* This needs to happen before any tsubsting. */ | |
14732 | if (! push_tinst_level (d)) | |
14733 | return d; | |
14734 | ||
297a5329 JM |
14735 | timevar_push (TV_PARSE); |
14736 | ||
4d85e00e | 14737 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
a723baf1 MM |
14738 | for the instantiation. */ |
14739 | td = template_for_substitution (d); | |
fee23f54 | 14740 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 14741 | |
2d22db1f | 14742 | /* We should never be trying to instantiate a member of a class |
3db45ab5 | 14743 | template or partial specialization. */ |
2d22db1f | 14744 | gcc_assert (d != code_pattern); |
3db45ab5 | 14745 | |
76d3baad KL |
14746 | if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) |
14747 | || DECL_TEMPLATE_SPECIALIZATION (td)) | |
649fc72d NS |
14748 | /* In the case of a friend template whose definition is provided |
14749 | outside the class, we may have too many arguments. Drop the | |
76d3baad | 14750 | ones we don't need. The same is true for specializations. */ |
649fc72d NS |
14751 | args = get_innermost_template_args |
14752 | (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); | |
14753 | else | |
14754 | args = gen_args; | |
65f8b0fb | 14755 | |
5566b478 | 14756 | if (TREE_CODE (d) == FUNCTION_DECL) |
39c76b4f | 14757 | pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); |
5566b478 | 14758 | else |
36a117a5 | 14759 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
b794e321 MM |
14760 | |
14761 | /* We may be in the middle of deferred access check. Disable it now. */ | |
14762 | push_deferring_access_checks (dk_no_deferred); | |
14763 | ||
4684cd27 MM |
14764 | /* Unless an explicit instantiation directive has already determined |
14765 | the linkage of D, remember that a definition is available for | |
14766 | this entity. */ | |
c8094d83 | 14767 | if (pattern_defined |
4684cd27 MM |
14768 | && !DECL_INTERFACE_KNOWN (d) |
14769 | && !DECL_NOT_REALLY_EXTERN (d)) | |
14770 | mark_definable (d); | |
de22184b | 14771 | |
f31686a3 | 14772 | input_location = DECL_SOURCE_LOCATION (d); |
912291ca | 14773 | in_system_header = DECL_IN_SYSTEM_HEADER (d); |
de22184b | 14774 | |
eba839f9 MM |
14775 | /* If D is a member of an explicitly instantiated class template, |
14776 | and no definition is available, treat it like an implicit | |
3db45ab5 MS |
14777 | instantiation. */ |
14778 | if (!pattern_defined && expl_inst_class_mem_p | |
14779 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
5566b478 | 14780 | { |
4684cd27 | 14781 | DECL_NOT_REALLY_EXTERN (d) = 0; |
eba839f9 | 14782 | DECL_INTERFACE_KNOWN (d) = 0; |
4684cd27 | 14783 | SET_DECL_IMPLICIT_INSTANTIATION (d); |
5566b478 MS |
14784 | } |
14785 | ||
4f2b0fb2 NS |
14786 | if (!defer_ok) |
14787 | { | |
14788 | /* Recheck the substitutions to obtain any warning messages | |
14789 | about ignoring cv qualifiers. */ | |
14790 | tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); | |
14791 | tree type = TREE_TYPE (gen); | |
14792 | ||
0e902d98 | 14793 | /* Make sure that we can see identifiers, and compute access |
2b59fc25 KL |
14794 | correctly. D is already the target FUNCTION_DECL with the |
14795 | right context. */ | |
14796 | push_access_scope (d); | |
0e902d98 | 14797 | |
4f2b0fb2 NS |
14798 | if (TREE_CODE (gen) == FUNCTION_DECL) |
14799 | { | |
23fca1f5 | 14800 | tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d); |
5d80a306 DG |
14801 | tsubst_exception_specification (type, gen_args, tf_warning_or_error, |
14802 | d); | |
4f2b0fb2 NS |
14803 | /* Don't simply tsubst the function type, as that will give |
14804 | duplicate warnings about poor parameter qualifications. | |
14805 | The function arguments are the same as the decl_arguments | |
c6002625 | 14806 | without the top level cv qualifiers. */ |
4f2b0fb2 NS |
14807 | type = TREE_TYPE (type); |
14808 | } | |
23fca1f5 | 14809 | tsubst (type, gen_args, tf_warning_or_error, d); |
0e902d98 | 14810 | |
2b59fc25 | 14811 | pop_access_scope (d); |
4f2b0fb2 | 14812 | } |
c8094d83 | 14813 | |
f7e4e484 MM |
14814 | /* Check to see whether we know that this template will be |
14815 | instantiated in some other file, as with "extern template" | |
14816 | extension. */ | |
14817 | external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d)); | |
14818 | /* In general, we do not instantiate such templates... */ | |
14819 | if (external_p | |
14820 | /* ... but we instantiate inline functions so that we can inline | |
14821 | them and ... */ | |
14822 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)) | |
14823 | /* ... we instantiate static data members whose values are | |
14824 | needed in integral constant expressions. */ | |
3db45ab5 | 14825 | && ! (TREE_CODE (d) == VAR_DECL |
f7e4e484 | 14826 | && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d))) |
930cd796 | 14827 | goto out; |
16d53b64 | 14828 | /* Defer all other templates, unless we have been explicitly |
f7e4e484 MM |
14829 | forbidden from doing so. */ |
14830 | if (/* If there is no definition, we cannot instantiate the | |
14831 | template. */ | |
3db45ab5 | 14832 | ! pattern_defined |
f7e4e484 MM |
14833 | /* If it's OK to postpone instantiation, do so. */ |
14834 | || defer_ok | |
14835 | /* If this is a static data member that will be defined | |
14836 | elsewhere, we don't want to instantiate the entire data | |
14837 | member, but we do want to instantiate the initializer so that | |
14838 | we can substitute that elsewhere. */ | |
14839 | || (external_p && TREE_CODE (d) == VAR_DECL)) | |
16d53b64 | 14840 | { |
b794e321 MM |
14841 | /* The definition of the static data member is now required so |
14842 | we must substitute the initializer. */ | |
14843 | if (TREE_CODE (d) == VAR_DECL | |
3db45ab5 | 14844 | && !DECL_INITIAL (d) |
b794e321 MM |
14845 | && DECL_INITIAL (code_pattern)) |
14846 | { | |
4bff36d3 MM |
14847 | tree ns; |
14848 | tree init; | |
14849 | ||
14850 | ns = decl_namespace_context (d); | |
27a725e2 | 14851 | push_nested_namespace (ns); |
b794e321 | 14852 | push_nested_class (DECL_CONTEXT (d)); |
3db45ab5 | 14853 | init = tsubst_expr (DECL_INITIAL (code_pattern), |
4bff36d3 | 14854 | args, |
015c2c66 MM |
14855 | tf_warning_or_error, NULL_TREE, |
14856 | /*integral_constant_expression_p=*/false); | |
d174af6c MM |
14857 | cp_finish_decl (d, init, /*init_const_expr_p=*/false, |
14858 | /*asmspec_tree=*/NULL_TREE, | |
86414779 | 14859 | LOOKUP_ONLYCONVERTING); |
b794e321 | 14860 | pop_nested_class (); |
27a725e2 | 14861 | pop_nested_namespace (ns); |
b794e321 MM |
14862 | } |
14863 | ||
f7e4e484 MM |
14864 | /* We restore the source position here because it's used by |
14865 | add_pending_template. */ | |
82a98427 | 14866 | input_location = saved_loc; |
c27be9b9 | 14867 | |
c8094d83 | 14868 | if (at_eof && !pattern_defined |
03d0f4af MM |
14869 | && DECL_EXPLICIT_INSTANTIATION (d)) |
14870 | /* [temp.explicit] | |
14871 | ||
14872 | The definition of a non-exported function template, a | |
14873 | non-exported member function template, or a non-exported | |
14874 | member function or static data member of a class template | |
14875 | shall be present in every translation unit in which it is | |
14876 | explicitly instantiated. */ | |
33bd39a2 | 14877 | pedwarn |
0f51ccfc | 14878 | ("explicit instantiation of %qD but no definition available", d); |
03d0f4af | 14879 | |
f7e4e484 MM |
14880 | /* ??? Historically, we have instantiated inline functions, even |
14881 | when marked as "extern template". */ | |
14882 | if (!(external_p && TREE_CODE (d) == VAR_DECL)) | |
14883 | add_pending_template (d); | |
de22184b | 14884 | goto out; |
5566b478 | 14885 | } |
4684cd27 MM |
14886 | /* Tell the repository that D is available in this translation unit |
14887 | -- and see if it is supposed to be instantiated here. */ | |
14888 | if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d)) | |
14889 | { | |
14890 | /* In a PCH file, despite the fact that the repository hasn't | |
14891 | requested instantiation in the PCH it is still possible that | |
14892 | an instantiation will be required in a file that includes the | |
14893 | PCH. */ | |
14894 | if (pch_file) | |
14895 | add_pending_template (d); | |
14896 | /* Instantiate inline functions so that the inliner can do its | |
14897 | job, even though we'll not be emitting a copy of this | |
14898 | function. */ | |
c2a124b2 | 14899 | if (!(TREE_CODE (d) == FUNCTION_DECL |
c8094d83 | 14900 | && flag_inline_trees |
c2a124b2 | 14901 | && DECL_DECLARED_INLINE_P (d))) |
4684cd27 MM |
14902 | goto out; |
14903 | } | |
5566b478 | 14904 | |
6de9cd9a | 14905 | need_push = !cfun || !global_bindings_p (); |
31a714f6 MM |
14906 | if (need_push) |
14907 | push_to_top_level (); | |
414ea4aa | 14908 | |
66e0c440 KL |
14909 | /* Mark D as instantiated so that recursive calls to |
14910 | instantiate_decl do not try to instantiate it again. */ | |
14911 | DECL_TEMPLATE_INSTANTIATED (d) = 1; | |
14912 | ||
2b0a63a3 MM |
14913 | /* Regenerate the declaration in case the template has been modified |
14914 | by a subsequent redeclaration. */ | |
14915 | regenerate_decl_from_template (d, td); | |
4684cd27 | 14916 | |
120722ac | 14917 | /* We already set the file and line above. Reset them now in case |
6de9cd9a | 14918 | they changed as a result of calling regenerate_decl_from_template. */ |
f31686a3 | 14919 | input_location = DECL_SOURCE_LOCATION (d); |
5156628f | 14920 | |
5566b478 MS |
14921 | if (TREE_CODE (d) == VAR_DECL) |
14922 | { | |
e92fb501 MM |
14923 | tree init; |
14924 | ||
1d62c33e MM |
14925 | /* Clear out DECL_RTL; whatever was there before may not be right |
14926 | since we've reset the type of the declaration. */ | |
14927 | SET_DECL_RTL (d, NULL_RTX); | |
5566b478 | 14928 | DECL_IN_AGGR_P (d) = 0; |
ea56c40c | 14929 | |
e92fb501 MM |
14930 | /* The initializer is placed in DECL_INITIAL by |
14931 | regenerate_decl_from_template. Pull it out so that | |
14932 | finish_decl can process it. */ | |
14933 | init = DECL_INITIAL (d); | |
14934 | DECL_INITIAL (d) = NULL_TREE; | |
14935 | DECL_INITIALIZED_P (d) = 0; | |
14936 | ||
4684cd27 MM |
14937 | /* Clear DECL_EXTERNAL so that cp_finish_decl will process the |
14938 | initializer. That function will defer actual emission until | |
14939 | we have a chance to determine linkage. */ | |
14940 | DECL_EXTERNAL (d) = 0; | |
14941 | ||
73a8adb6 | 14942 | /* Enter the scope of D so that access-checking works correctly. */ |
4684cd27 | 14943 | push_nested_class (DECL_CONTEXT (d)); |
e92fb501 | 14944 | finish_decl (d, init, NULL_TREE); |
73a8adb6 | 14945 | pop_nested_class (); |
5566b478 MS |
14946 | } |
14947 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
14948 | { | |
6bbf1598 | 14949 | htab_t saved_local_specializations; |
a723baf1 MM |
14950 | tree subst_decl; |
14951 | tree tmpl_parm; | |
14952 | tree spec_parm; | |
6bbf1598 MM |
14953 | |
14954 | /* Save away the current list, in case we are instantiating one | |
14955 | template from within the body of another. */ | |
14956 | saved_local_specializations = local_specializations; | |
14957 | ||
6dfbb909 | 14958 | /* Set up the list of local specializations. */ |
c8094d83 | 14959 | local_specializations = htab_create (37, |
69f794a7 | 14960 | hash_local_specialization, |
a723baf1 | 14961 | eq_local_specializations, |
6dfbb909 MM |
14962 | NULL); |
14963 | ||
558475f0 | 14964 | /* Set up context. */ |
058b15c1 | 14965 | start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED); |
5566b478 | 14966 | |
a723baf1 MM |
14967 | /* Create substitution entries for the parameters. */ |
14968 | subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d)); | |
14969 | tmpl_parm = DECL_ARGUMENTS (subst_decl); | |
14970 | spec_parm = DECL_ARGUMENTS (d); | |
14971 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d)) | |
14972 | { | |
14973 | register_local_specialization (spec_parm, tmpl_parm); | |
14974 | spec_parm = skip_artificial_parms_for (d, spec_parm); | |
14975 | tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm); | |
14976 | } | |
5d80a306 | 14977 | while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
a723baf1 MM |
14978 | { |
14979 | register_local_specialization (spec_parm, tmpl_parm); | |
14980 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
14981 | spec_parm = TREE_CHAIN (spec_parm); | |
14982 | } | |
5d80a306 DG |
14983 | if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
14984 | { | |
14985 | /* Collect all of the extra "packed" parameters into an | |
14986 | argument pack. */ | |
14987 | tree parmvec; | |
14988 | tree parmtypevec; | |
14989 | tree argpack = make_node (NONTYPE_ARGUMENT_PACK); | |
14990 | tree argtypepack = make_node (TYPE_ARGUMENT_PACK); | |
14991 | int i, len = 0; | |
14992 | tree t; | |
14993 | ||
14994 | /* Count how many parameters remain. */ | |
14995 | for (t = spec_parm; t; t = TREE_CHAIN (t)) | |
14996 | len++; | |
14997 | ||
14998 | /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */ | |
14999 | parmvec = make_tree_vec (len); | |
15000 | parmtypevec = make_tree_vec (len); | |
15001 | for(i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm)) | |
15002 | { | |
15003 | TREE_VEC_ELT (parmvec, i) = spec_parm; | |
15004 | TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm); | |
15005 | } | |
15006 | ||
15007 | /* Build the argument packs. */ | |
15008 | SET_ARGUMENT_PACK_ARGS (argpack, parmvec); | |
15009 | SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec); | |
15010 | TREE_TYPE (argpack) = argtypepack; | |
15011 | ||
15012 | /* Register the (value) argument pack as a specialization of | |
15013 | TMPL_PARM, then move on. */ | |
15014 | register_local_specialization (argpack, tmpl_parm); | |
15015 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
15016 | } | |
50bc768d | 15017 | gcc_assert (!spec_parm); |
a723baf1 | 15018 | |
558475f0 MM |
15019 | /* Substitute into the body of the function. */ |
15020 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
015c2c66 MM |
15021 | tf_warning_or_error, tmpl, |
15022 | /*integral_constant_expression_p=*/false); | |
558475f0 | 15023 | |
75407da3 SP |
15024 | /* Set the current input_location to the end of the function |
15025 | so that finish_function knows where we are. */ | |
15026 | input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus; | |
15027 | ||
6dfbb909 MM |
15028 | /* We don't need the local specializations any more. */ |
15029 | htab_delete (local_specializations); | |
6bbf1598 | 15030 | local_specializations = saved_local_specializations; |
6dfbb909 | 15031 | |
4d6abc1c | 15032 | /* Finish the function. */ |
b2dd096b | 15033 | d = finish_function (0); |
8cd2462c | 15034 | expand_or_defer_fn (d); |
5566b478 MS |
15035 | } |
15036 | ||
971cbc14 MM |
15037 | /* We're not deferring instantiation any more. */ |
15038 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; | |
15039 | ||
31a714f6 MM |
15040 | if (need_push) |
15041 | pop_from_top_level (); | |
414ea4aa | 15042 | |
de22184b | 15043 | out: |
82a98427 | 15044 | input_location = saved_loc; |
912291ca | 15045 | in_system_header = saved_in_system_header; |
7d021397 | 15046 | pop_deferring_access_checks (); |
5566b478 | 15047 | pop_tinst_level (); |
a28e3c7f | 15048 | |
297a5329 JM |
15049 | timevar_pop (TV_PARSE); |
15050 | ||
a28e3c7f MS |
15051 | return d; |
15052 | } | |
5566b478 | 15053 | |
0aafb128 | 15054 | /* Run through the list of templates that we wish we could |
35046a54 KL |
15055 | instantiate, and instantiate any we can. RETRIES is the |
15056 | number of times we retry pending template instantiation. */ | |
0aafb128 | 15057 | |
35046a54 KL |
15058 | void |
15059 | instantiate_pending_templates (int retries) | |
0aafb128 | 15060 | { |
0aafb128 | 15061 | int reconsider; |
aad626f7 | 15062 | location_t saved_loc = input_location; |
12af7ba3 | 15063 | int saved_in_system_header = in_system_header; |
35046a54 KL |
15064 | |
15065 | /* Instantiating templates may trigger vtable generation. This in turn | |
15066 | may require further template instantiations. We place a limit here | |
15067 | to avoid infinite loop. */ | |
15068 | if (pending_templates && retries >= max_tinst_depth) | |
15069 | { | |
e2c3721c | 15070 | tree decl = pending_templates->tinst->decl; |
96c993a8 | 15071 | |
dee15844 | 15072 | error ("template instantiation depth exceeds maximum of %d" |
96c993a8 JM |
15073 | " instantiating %q+D, possibly from virtual table generation" |
15074 | " (use -ftemplate-depth-NN to increase the maximum)", | |
15075 | max_tinst_depth, decl); | |
15076 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
15077 | /* Pretend that we defined it. */ | |
15078 | DECL_INITIAL (decl) = error_mark_node; | |
35046a54 KL |
15079 | return; |
15080 | } | |
15081 | ||
c8094d83 | 15082 | do |
0aafb128 | 15083 | { |
e2c3721c PB |
15084 | struct pending_template **t = &pending_templates; |
15085 | struct pending_template *last = NULL; | |
0aafb128 | 15086 | reconsider = 0; |
0aafb128 MM |
15087 | while (*t) |
15088 | { | |
e2c3721c PB |
15089 | tree instantiation = reopen_tinst_level ((*t)->tinst); |
15090 | bool complete = false; | |
0aafb128 | 15091 | |
2f939d94 | 15092 | if (TYPE_P (instantiation)) |
0aafb128 MM |
15093 | { |
15094 | tree fn; | |
15095 | ||
d0f062fb | 15096 | if (!COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
15097 | { |
15098 | instantiate_class_template (instantiation); | |
15099 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
c8094d83 | 15100 | for (fn = TYPE_METHODS (instantiation); |
0aafb128 MM |
15101 | fn; |
15102 | fn = TREE_CHAIN (fn)) | |
15103 | if (! DECL_ARTIFICIAL (fn)) | |
3db45ab5 | 15104 | instantiate_decl (fn, |
eba839f9 MM |
15105 | /*defer_ok=*/0, |
15106 | /*expl_inst_class_mem_p=*/false); | |
d0f062fb | 15107 | if (COMPLETE_TYPE_P (instantiation)) |
35046a54 | 15108 | reconsider = 1; |
0aafb128 MM |
15109 | } |
15110 | ||
e2c3721c | 15111 | complete = COMPLETE_TYPE_P (instantiation); |
0aafb128 MM |
15112 | } |
15113 | else | |
15114 | { | |
16d53b64 | 15115 | if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
15116 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) |
15117 | { | |
3db45ab5 | 15118 | instantiation |
eba839f9 MM |
15119 | = instantiate_decl (instantiation, |
15120 | /*defer_ok=*/0, | |
15121 | /*expl_inst_class_mem_p=*/false); | |
0aafb128 | 15122 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) |
35046a54 | 15123 | reconsider = 1; |
0aafb128 MM |
15124 | } |
15125 | ||
e2c3721c PB |
15126 | complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation) |
15127 | || DECL_TEMPLATE_INSTANTIATED (instantiation)); | |
15128 | } | |
15129 | ||
15130 | if (complete) | |
15131 | /* If INSTANTIATION has been instantiated, then we don't | |
15132 | need to consider it again in the future. */ | |
15133 | *t = (*t)->next; | |
15134 | else | |
15135 | { | |
15136 | last = *t; | |
15137 | t = &(*t)->next; | |
0aafb128 | 15138 | } |
84e5ca0f | 15139 | tinst_depth = 0; |
e2c3721c | 15140 | current_tinst_level = NULL; |
0aafb128 | 15141 | } |
46ccf50a | 15142 | last_pending_template = last; |
c8094d83 | 15143 | } |
0aafb128 MM |
15144 | while (reconsider); |
15145 | ||
aad626f7 | 15146 | input_location = saved_loc; |
12af7ba3 | 15147 | in_system_header = saved_in_system_header; |
0aafb128 MM |
15148 | } |
15149 | ||
fd74ca0b MM |
15150 | /* Substitute ARGVEC into T, which is a list of initializers for |
15151 | either base class or a non-static data member. The TREE_PURPOSEs | |
15152 | are DECLs, and the TREE_VALUEs are the initializer values. Used by | |
15153 | instantiate_decl. */ | |
4393e105 | 15154 | |
824b9a4c | 15155 | static tree |
3a978d72 | 15156 | tsubst_initializer_list (tree t, tree argvec) |
5566b478 | 15157 | { |
2282d28d | 15158 | tree inits = NULL_TREE; |
5566b478 MS |
15159 | |
15160 | for (; t; t = TREE_CHAIN (t)) | |
15161 | { | |
fd74ca0b MM |
15162 | tree decl; |
15163 | tree init; | |
5d80a306 DG |
15164 | tree expanded_bases = NULL_TREE; |
15165 | tree expanded_arguments = NULL_TREE; | |
15166 | int i, len = 1; | |
fd74ca0b | 15167 | |
5d80a306 DG |
15168 | if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION) |
15169 | { | |
15170 | tree expr; | |
15171 | tree arg; | |
15172 | ||
15173 | /* Expand the base class expansion type into separate base | |
15174 | classes. */ | |
15175 | expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec, | |
15176 | tf_warning_or_error, | |
15177 | NULL_TREE); | |
15178 | if (expanded_bases == error_mark_node) | |
15179 | continue; | |
15180 | ||
15181 | /* We'll be building separate TREE_LISTs of arguments for | |
15182 | each base. */ | |
15183 | len = TREE_VEC_LENGTH (expanded_bases); | |
15184 | expanded_arguments = make_tree_vec (len); | |
15185 | for (i = 0; i < len; i++) | |
15186 | TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE; | |
15187 | ||
15188 | /* Build a dummy EXPR_PACK_EXPANSION that will be used to | |
15189 | expand each argument in the TREE_VALUE of t. */ | |
15190 | expr = make_node (EXPR_PACK_EXPANSION); | |
15191 | PACK_EXPANSION_PARAMETER_PACKS (expr) = | |
15192 | PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t)); | |
15193 | ||
15194 | /* Substitute parameter packs into each argument in the | |
15195 | TREE_LIST. */ | |
15196 | in_base_initializer = 1; | |
15197 | for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg)) | |
15198 | { | |
15199 | tree expanded_exprs; | |
15200 | ||
15201 | /* Expand the argument. */ | |
15202 | SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg)); | |
15203 | expanded_exprs = tsubst_pack_expansion (expr, argvec, | |
15204 | tf_warning_or_error, | |
15205 | NULL_TREE); | |
15206 | ||
15207 | /* Prepend each of the expanded expressions to the | |
15208 | corresponding TREE_LIST in EXPANDED_ARGUMENTS. */ | |
15209 | for (i = 0; i < len; i++) | |
15210 | { | |
15211 | TREE_VEC_ELT (expanded_arguments, i) = | |
15212 | tree_cons (NULL_TREE, TREE_VEC_ELT (expanded_exprs, i), | |
15213 | TREE_VEC_ELT (expanded_arguments, i)); | |
15214 | } | |
15215 | } | |
15216 | in_base_initializer = 0; | |
15217 | ||
15218 | /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS, | |
15219 | since we built them backwards. */ | |
15220 | for (i = 0; i < len; i++) | |
15221 | { | |
15222 | TREE_VEC_ELT (expanded_arguments, i) = | |
15223 | nreverse (TREE_VEC_ELT (expanded_arguments, i)); | |
15224 | } | |
15225 | } | |
1f5a253a | 15226 | |
5d80a306 DG |
15227 | for (i = 0; i < len; ++i) |
15228 | { | |
15229 | if (expanded_bases) | |
15230 | { | |
15231 | decl = TREE_VEC_ELT (expanded_bases, i); | |
15232 | decl = expand_member_init (decl); | |
15233 | init = TREE_VEC_ELT (expanded_arguments, i); | |
15234 | } | |
15235 | else | |
15236 | { | |
15237 | decl = tsubst_copy (TREE_PURPOSE (t), argvec, | |
15238 | tf_warning_or_error, NULL_TREE); | |
15239 | ||
15240 | decl = expand_member_init (decl); | |
15241 | if (decl && !DECL_P (decl)) | |
15242 | in_base_initializer = 1; | |
15243 | ||
15244 | init = tsubst_expr (TREE_VALUE (t), argvec, | |
15245 | tf_warning_or_error, NULL_TREE, | |
15246 | /*integral_constant_expression_p=*/false); | |
15247 | in_base_initializer = 0; | |
15248 | } | |
15249 | ||
15250 | if (decl) | |
15251 | { | |
15252 | init = build_tree_list (decl, init); | |
15253 | TREE_CHAIN (init) = inits; | |
15254 | inits = init; | |
15255 | } | |
15256 | } | |
5566b478 | 15257 | } |
2282d28d | 15258 | return inits; |
5566b478 MS |
15259 | } |
15260 | ||
61a127b3 MM |
15261 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
15262 | ||
15263 | static void | |
3a978d72 | 15264 | set_current_access_from_decl (tree decl) |
61a127b3 MM |
15265 | { |
15266 | if (TREE_PRIVATE (decl)) | |
15267 | current_access_specifier = access_private_node; | |
15268 | else if (TREE_PROTECTED (decl)) | |
15269 | current_access_specifier = access_protected_node; | |
15270 | else | |
15271 | current_access_specifier = access_public_node; | |
15272 | } | |
15273 | ||
dbfe2124 MM |
15274 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
15275 | is the instantiation (which should have been created with | |
15276 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 15277 | |
dbfe2124 | 15278 | static void |
3a978d72 | 15279 | tsubst_enum (tree tag, tree newtag, tree args) |
b87692e5 | 15280 | { |
dbfe2124 | 15281 | tree e; |
b87692e5 MS |
15282 | |
15283 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
15284 | { | |
61a127b3 | 15285 | tree value; |
7b6d72fc MM |
15286 | tree decl; |
15287 | ||
15288 | decl = TREE_VALUE (e); | |
61a127b3 MM |
15289 | /* Note that in a template enum, the TREE_VALUE is the |
15290 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
c8094d83 | 15291 | value = tsubst_expr (DECL_INITIAL (decl), |
015c2c66 MM |
15292 | args, tf_warning_or_error, NULL_TREE, |
15293 | /*integral_constant_expression_p=*/true); | |
61a127b3 MM |
15294 | |
15295 | /* Give this enumeration constant the correct access. */ | |
7b6d72fc | 15296 | set_current_access_from_decl (decl); |
61a127b3 MM |
15297 | |
15298 | /* Actually build the enumerator itself. */ | |
c8094d83 | 15299 | build_enumerator (DECL_NAME (decl), value, newtag); |
dbfe2124 | 15300 | } |
b3d5a58b | 15301 | |
219670f1 | 15302 | finish_enum (newtag); |
f31686a3 RH |
15303 | DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) |
15304 | = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); | |
b87692e5 | 15305 | } |
36a117a5 | 15306 | |
1f6e1acc AS |
15307 | /* DECL is a FUNCTION_DECL that is a template specialization. Return |
15308 | its type -- but without substituting the innermost set of template | |
15309 | arguments. So, innermost set of template parameters will appear in | |
5c74d5b0 | 15310 | the type. */ |
1f6e1acc | 15311 | |
c8094d83 | 15312 | tree |
3a978d72 | 15313 | get_mostly_instantiated_function_type (tree decl) |
1f6e1acc | 15314 | { |
1f6e1acc AS |
15315 | tree fn_type; |
15316 | tree tmpl; | |
15317 | tree targs; | |
15318 | tree tparms; | |
15319 | int parm_depth; | |
15320 | ||
15321 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
15322 | targs = DECL_TI_ARGS (decl); | |
15323 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
15324 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
15325 | ||
15326 | /* There should be as many levels of arguments as there are levels | |
15327 | of parameters. */ | |
50bc768d | 15328 | gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs)); |
1f6e1acc AS |
15329 | |
15330 | fn_type = TREE_TYPE (tmpl); | |
1f6e1acc AS |
15331 | |
15332 | if (parm_depth == 1) | |
15333 | /* No substitution is necessary. */ | |
15334 | ; | |
15335 | else | |
15336 | { | |
fae6e246 | 15337 | int i, save_access_control; |
1f6e1acc AS |
15338 | tree partial_args; |
15339 | ||
15340 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
dc957d14 | 15341 | let tsubst know not to substitute for those parameters. */ |
1f6e1acc AS |
15342 | partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); |
15343 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
15344 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
15345 | TMPL_ARGS_LEVEL (targs, i)); | |
15346 | SET_TMPL_ARGS_LEVEL (partial_args, | |
15347 | TMPL_ARGS_DEPTH (targs), | |
15348 | make_tree_vec (DECL_NTPARMS (tmpl))); | |
15349 | ||
fae6e246 RH |
15350 | /* Disable access control as this function is used only during |
15351 | name-mangling. */ | |
15352 | save_access_control = flag_access_control; | |
15353 | flag_access_control = 0; | |
5c74d5b0 | 15354 | |
9579624e | 15355 | ++processing_template_decl; |
1f6e1acc AS |
15356 | /* Now, do the (partial) substitution to figure out the |
15357 | appropriate function type. */ | |
c2ea3a40 | 15358 | fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); |
9579624e | 15359 | --processing_template_decl; |
1f6e1acc AS |
15360 | |
15361 | /* Substitute into the template parameters to obtain the real | |
15362 | innermost set of parameters. This step is important if the | |
15363 | innermost set of template parameters contains value | |
15364 | parameters whose types depend on outer template parameters. */ | |
15365 | TREE_VEC_LENGTH (partial_args)--; | |
c2ea3a40 | 15366 | tparms = tsubst_template_parms (tparms, partial_args, tf_error); |
1f6e1acc | 15367 | |
fae6e246 | 15368 | flag_access_control = save_access_control; |
5c74d5b0 | 15369 | } |
1f6e1acc AS |
15370 | |
15371 | return fn_type; | |
15372 | } | |
669ec2b4 | 15373 | |
cb753e49 | 15374 | /* Return truthvalue if we're processing a template different from |
dc957d14 | 15375 | the last one involved in diagnostics. */ |
cb753e49 | 15376 | int |
3a978d72 | 15377 | problematic_instantiation_changed (void) |
cb753e49 GDR |
15378 | { |
15379 | return last_template_error_tick != tinst_level_tick; | |
15380 | } | |
15381 | ||
15382 | /* Remember current template involved in diagnostics. */ | |
15383 | void | |
3a978d72 | 15384 | record_last_problematic_instantiation (void) |
cb753e49 GDR |
15385 | { |
15386 | last_template_error_tick = tinst_level_tick; | |
15387 | } | |
15388 | ||
e2c3721c | 15389 | struct tinst_level * |
3a978d72 | 15390 | current_instantiation (void) |
cb753e49 GDR |
15391 | { |
15392 | return current_tinst_level; | |
15393 | } | |
db3f4e4e NS |
15394 | |
15395 | /* [temp.param] Check that template non-type parm TYPE is of an allowable | |
838dfd8a | 15396 | type. Return zero for ok, nonzero for disallowed. Issue error and |
c2ea3a40 | 15397 | warning messages under control of COMPLAIN. */ |
db3f4e4e NS |
15398 | |
15399 | static int | |
3a978d72 | 15400 | invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain) |
db3f4e4e NS |
15401 | { |
15402 | if (INTEGRAL_TYPE_P (type)) | |
15403 | return 0; | |
15404 | else if (POINTER_TYPE_P (type)) | |
15405 | return 0; | |
a5ac359a | 15406 | else if (TYPE_PTR_TO_MEMBER_P (type)) |
db3f4e4e | 15407 | return 0; |
db3f4e4e NS |
15408 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
15409 | return 0; | |
15410 | else if (TREE_CODE (type) == TYPENAME_TYPE) | |
15411 | return 0; | |
c8094d83 | 15412 | |
c2ea3a40 | 15413 | if (complain & tf_error) |
0f51ccfc | 15414 | error ("%q#T is not a valid type for a template constant parameter", type); |
db3f4e4e NS |
15415 | return 1; |
15416 | } | |
e2500fed | 15417 | |
5552b43c MM |
15418 | /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. |
15419 | Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ | |
1fb3244a | 15420 | |
5552b43c MM |
15421 | static bool |
15422 | dependent_type_p_r (tree type) | |
1fb3244a MM |
15423 | { |
15424 | tree scope; | |
15425 | ||
1fb3244a MM |
15426 | /* [temp.dep.type] |
15427 | ||
15428 | A type is dependent if it is: | |
15429 | ||
6615c446 JO |
15430 | -- a template parameter. Template template parameters are types |
15431 | for us (since TYPE_P holds true for them) so we handle | |
15432 | them here. */ | |
c8094d83 | 15433 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
6bf92cb6 | 15434 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) |
1fb3244a MM |
15435 | return true; |
15436 | /* -- a qualified-id with a nested-name-specifier which contains a | |
0cbd7506 | 15437 | class-name that names a dependent type or whose unqualified-id |
1fb3244a MM |
15438 | names a dependent type. */ |
15439 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
15440 | return true; | |
15441 | /* -- a cv-qualified type where the cv-unqualified type is | |
0cbd7506 | 15442 | dependent. */ |
1fb3244a MM |
15443 | type = TYPE_MAIN_VARIANT (type); |
15444 | /* -- a compound type constructed from any dependent type. */ | |
a5ac359a | 15445 | if (TYPE_PTR_TO_MEMBER_P (type)) |
1fb3244a | 15446 | return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) |
c8094d83 | 15447 | || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE |
1fb3244a MM |
15448 | (type))); |
15449 | else if (TREE_CODE (type) == POINTER_TYPE | |
15450 | || TREE_CODE (type) == REFERENCE_TYPE) | |
15451 | return dependent_type_p (TREE_TYPE (type)); | |
15452 | else if (TREE_CODE (type) == FUNCTION_TYPE | |
15453 | || TREE_CODE (type) == METHOD_TYPE) | |
15454 | { | |
15455 | tree arg_type; | |
15456 | ||
15457 | if (dependent_type_p (TREE_TYPE (type))) | |
15458 | return true; | |
c8094d83 MS |
15459 | for (arg_type = TYPE_ARG_TYPES (type); |
15460 | arg_type; | |
1fb3244a MM |
15461 | arg_type = TREE_CHAIN (arg_type)) |
15462 | if (dependent_type_p (TREE_VALUE (arg_type))) | |
15463 | return true; | |
15464 | return false; | |
15465 | } | |
15466 | /* -- an array type constructed from any dependent type or whose | |
0cbd7506 | 15467 | size is specified by a constant expression that is |
1fb3244a MM |
15468 | value-dependent. */ |
15469 | if (TREE_CODE (type) == ARRAY_TYPE) | |
15470 | { | |
15471 | if (TYPE_DOMAIN (type) | |
82390eb6 | 15472 | && dependent_type_p (TYPE_DOMAIN (type))) |
1fb3244a MM |
15473 | return true; |
15474 | return dependent_type_p (TREE_TYPE (type)); | |
15475 | } | |
82390eb6 JM |
15476 | else if (TREE_CODE (type) == INTEGER_TYPE |
15477 | && !TREE_CONSTANT (TYPE_MAX_VALUE (type))) | |
15478 | { | |
15479 | /* If this is the TYPE_DOMAIN of an array type, consider it | |
15480 | dependent. */ | |
15481 | return (value_dependent_expression_p (TYPE_MAX_VALUE (type)) | |
15482 | || type_dependent_expression_p (TYPE_MAX_VALUE (type))); | |
15483 | } | |
c8094d83 | 15484 | |
1fb3244a | 15485 | /* -- a template-id in which either the template name is a template |
86306a6b NS |
15486 | parameter ... */ |
15487 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
1fb3244a | 15488 | return true; |
86306a6b | 15489 | /* ... or any of the template arguments is a dependent type or |
04c06002 | 15490 | an expression that is type-dependent or value-dependent. */ |
86306a6b | 15491 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) |
c8094d83 | 15492 | && (any_dependent_template_arguments_p |
7e99327d | 15493 | (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type))))) |
1fb3244a | 15494 | return true; |
c8094d83 | 15495 | |
3ad6a8e1 DG |
15496 | /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the |
15497 | argument of the `typeof' expression is not type-dependent, then | |
15498 | it should already been have resolved. */ | |
15499 | if (TREE_CODE (type) == TYPEOF_TYPE | |
15500 | || TREE_CODE (type) == DECLTYPE_TYPE) | |
1fb3244a | 15501 | return true; |
c8094d83 | 15502 | |
5d80a306 DG |
15503 | /* A template argument pack is dependent if any of its packed |
15504 | arguments are. */ | |
15505 | if (TREE_CODE (type) == TYPE_ARGUMENT_PACK) | |
15506 | { | |
15507 | tree args = ARGUMENT_PACK_ARGS (type); | |
15508 | int i, len = TREE_VEC_LENGTH (args); | |
15509 | for (i = 0; i < len; ++i) | |
15510 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
15511 | return true; | |
15512 | } | |
15513 | ||
15514 | /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must | |
15515 | be template parameters. */ | |
15516 | if (TREE_CODE (type) == TYPE_PACK_EXPANSION) | |
15517 | return true; | |
15518 | ||
1fb3244a MM |
15519 | /* The standard does not specifically mention types that are local |
15520 | to template functions or local classes, but they should be | |
15521 | considered dependent too. For example: | |
15522 | ||
c8094d83 | 15523 | template <int I> void f() { |
0cbd7506 | 15524 | enum E { a = I }; |
1fb3244a MM |
15525 | S<sizeof (E)> s; |
15526 | } | |
15527 | ||
15528 | The size of `E' cannot be known until the value of `I' has been | |
15529 | determined. Therefore, `E' must be considered dependent. */ | |
15530 | scope = TYPE_CONTEXT (type); | |
15531 | if (scope && TYPE_P (scope)) | |
15532 | return dependent_type_p (scope); | |
15533 | else if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
15534 | return type_dependent_expression_p (scope); | |
15535 | ||
15536 | /* Other types are non-dependent. */ | |
15537 | return false; | |
15538 | } | |
15539 | ||
5552b43c MM |
15540 | /* Returns TRUE if TYPE is dependent, in the sense of |
15541 | [temp.dep.type]. */ | |
15542 | ||
15543 | bool | |
15544 | dependent_type_p (tree type) | |
15545 | { | |
15546 | /* If there are no template parameters in scope, then there can't be | |
15547 | any dependent types. */ | |
15548 | if (!processing_template_decl) | |
e7e93965 MM |
15549 | { |
15550 | /* If we are not processing a template, then nobody should be | |
15551 | providing us with a dependent type. */ | |
15552 | gcc_assert (type); | |
15553 | gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM); | |
15554 | return false; | |
15555 | } | |
5552b43c MM |
15556 | |
15557 | /* If the type is NULL, we have not computed a type for the entity | |
15558 | in question; in that case, the type is dependent. */ | |
15559 | if (!type) | |
15560 | return true; | |
15561 | ||
15562 | /* Erroneous types can be considered non-dependent. */ | |
15563 | if (type == error_mark_node) | |
15564 | return false; | |
15565 | ||
15566 | /* If we have not already computed the appropriate value for TYPE, | |
15567 | do so now. */ | |
15568 | if (!TYPE_DEPENDENT_P_VALID (type)) | |
15569 | { | |
15570 | TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); | |
15571 | TYPE_DEPENDENT_P_VALID (type) = 1; | |
15572 | } | |
15573 | ||
15574 | return TYPE_DEPENDENT_P (type); | |
15575 | } | |
15576 | ||
8d83f792 MM |
15577 | /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */ |
15578 | ||
15579 | static bool | |
15580 | dependent_scope_ref_p (tree expression, bool criterion (tree)) | |
15581 | { | |
15582 | tree scope; | |
15583 | tree name; | |
15584 | ||
50bc768d | 15585 | gcc_assert (TREE_CODE (expression) == SCOPE_REF); |
8d83f792 MM |
15586 | |
15587 | if (!TYPE_P (TREE_OPERAND (expression, 0))) | |
15588 | return true; | |
15589 | ||
15590 | scope = TREE_OPERAND (expression, 0); | |
15591 | name = TREE_OPERAND (expression, 1); | |
15592 | ||
15593 | /* [temp.dep.expr] | |
15594 | ||
15595 | An id-expression is type-dependent if it contains a | |
15596 | nested-name-specifier that contains a class-name that names a | |
15597 | dependent type. */ | |
15598 | /* The suggested resolution to Core Issue 2 implies that if the | |
15599 | qualifying type is the current class, then we must peek | |
15600 | inside it. */ | |
c8094d83 | 15601 | if (DECL_P (name) |
8d83f792 MM |
15602 | && currently_open_class (scope) |
15603 | && !criterion (name)) | |
15604 | return false; | |
15605 | if (dependent_type_p (scope)) | |
15606 | return true; | |
15607 | ||
15608 | return false; | |
15609 | } | |
15610 | ||
20929c7f | 15611 | /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of |
fe89d797 MM |
15612 | [temp.dep.constexpr]. EXPRESSION is already known to be a constant |
15613 | expression. */ | |
1fb3244a | 15614 | |
14d22dd6 | 15615 | bool |
1fb3244a MM |
15616 | value_dependent_expression_p (tree expression) |
15617 | { | |
15618 | if (!processing_template_decl) | |
15619 | return false; | |
15620 | ||
15621 | /* A name declared with a dependent type. */ | |
7416ab02 | 15622 | if (DECL_P (expression) && type_dependent_expression_p (expression)) |
1fb3244a | 15623 | return true; |
c8094d83 | 15624 | |
7416ab02 NS |
15625 | switch (TREE_CODE (expression)) |
15626 | { | |
15627 | case IDENTIFIER_NODE: | |
15628 | /* A name that has not been looked up -- must be dependent. */ | |
15629 | return true; | |
15630 | ||
15631 | case TEMPLATE_PARM_INDEX: | |
15632 | /* A non-type template parm. */ | |
15633 | return true; | |
15634 | ||
15635 | case CONST_DECL: | |
15636 | /* A non-type template parm. */ | |
15637 | if (DECL_TEMPLATE_PARM_P (expression)) | |
d17811fd | 15638 | return true; |
7416ab02 | 15639 | return false; |
d36d5600 | 15640 | |
7416ab02 | 15641 | case VAR_DECL: |
c8094d83 | 15642 | /* A constant with integral or enumeration type and is initialized |
0cbd7506 | 15643 | with an expression that is value-dependent. */ |
7416ab02 NS |
15644 | if (DECL_INITIAL (expression) |
15645 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression)) | |
15646 | && value_dependent_expression_p (DECL_INITIAL (expression))) | |
15647 | return true; | |
15648 | return false; | |
15649 | ||
15650 | case DYNAMIC_CAST_EXPR: | |
15651 | case STATIC_CAST_EXPR: | |
15652 | case CONST_CAST_EXPR: | |
15653 | case REINTERPRET_CAST_EXPR: | |
15654 | case CAST_EXPR: | |
15655 | /* These expressions are value-dependent if the type to which | |
0cbd7506 MS |
15656 | the cast occurs is dependent or the expression being casted |
15657 | is value-dependent. */ | |
7416ab02 NS |
15658 | { |
15659 | tree type = TREE_TYPE (expression); | |
c8094d83 | 15660 | |
7416ab02 NS |
15661 | if (dependent_type_p (type)) |
15662 | return true; | |
c8094d83 | 15663 | |
7416ab02 NS |
15664 | /* A functional cast has a list of operands. */ |
15665 | expression = TREE_OPERAND (expression, 0); | |
15666 | if (!expression) | |
15667 | { | |
15668 | /* If there are no operands, it must be an expression such | |
15669 | as "int()". This should not happen for aggregate types | |
15670 | because it would form non-constant expressions. */ | |
15671 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); | |
c8094d83 | 15672 | |
7416ab02 NS |
15673 | return false; |
15674 | } | |
c8094d83 | 15675 | |
7416ab02 | 15676 | if (TREE_CODE (expression) == TREE_LIST) |
32a11c08 | 15677 | return any_value_dependent_elements_p (expression); |
c8094d83 | 15678 | |
d17811fd | 15679 | return value_dependent_expression_p (expression); |
7416ab02 | 15680 | } |
c8094d83 | 15681 | |
7416ab02 NS |
15682 | case SIZEOF_EXPR: |
15683 | case ALIGNOF_EXPR: | |
15684 | /* A `sizeof' expression is value-dependent if the operand is | |
5d80a306 | 15685 | type-dependent or is a pack expansion. */ |
d17811fd | 15686 | expression = TREE_OPERAND (expression, 0); |
5d80a306 DG |
15687 | if (PACK_EXPANSION_P (expression)) |
15688 | return true; | |
15689 | else if (TYPE_P (expression)) | |
d17811fd MM |
15690 | return dependent_type_p (expression); |
15691 | return type_dependent_expression_p (expression); | |
100d337a | 15692 | |
7416ab02 NS |
15693 | case SCOPE_REF: |
15694 | return dependent_scope_ref_p (expression, value_dependent_expression_p); | |
100d337a | 15695 | |
7416ab02 NS |
15696 | case COMPONENT_REF: |
15697 | return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) | |
15698 | || value_dependent_expression_p (TREE_OPERAND (expression, 1))); | |
15699 | ||
15700 | case CALL_EXPR: | |
fe89d797 MM |
15701 | /* A CALL_EXPR may appear in a constant expression if it is a |
15702 | call to a builtin function, e.g., __builtin_constant_p. All | |
15703 | such calls are value-dependent. */ | |
15704 | return true; | |
7416ab02 | 15705 | |
5d80a306 DG |
15706 | case NONTYPE_ARGUMENT_PACK: |
15707 | /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument | |
15708 | is value-dependent. */ | |
15709 | { | |
15710 | tree values = ARGUMENT_PACK_ARGS (expression); | |
15711 | int i, len = TREE_VEC_LENGTH (values); | |
15712 | ||
15713 | for (i = 0; i < len; ++i) | |
15714 | if (value_dependent_expression_p (TREE_VEC_ELT (values, i))) | |
15715 | return true; | |
15716 | ||
15717 | return false; | |
15718 | } | |
15719 | ||
cb68ec50 PC |
15720 | case TRAIT_EXPR: |
15721 | { | |
15722 | tree type2 = TRAIT_EXPR_TYPE2 (expression); | |
15723 | return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)) | |
15724 | || (type2 ? dependent_type_p (type2) : false)); | |
15725 | } | |
15726 | ||
f01ec37d SM |
15727 | case MODOP_EXPR: |
15728 | return ((value_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
15729 | || (value_dependent_expression_p (TREE_OPERAND (expression, 2)))); | |
15730 | ||
7416ab02 NS |
15731 | default: |
15732 | /* A constant expression is value-dependent if any subexpression is | |
0cbd7506 | 15733 | value-dependent. */ |
1fb3244a MM |
15734 | switch (TREE_CODE_CLASS (TREE_CODE (expression))) |
15735 | { | |
7416ab02 | 15736 | case tcc_reference: |
6615c446 | 15737 | case tcc_unary: |
c8094d83 | 15738 | return (value_dependent_expression_p |
1fb3244a | 15739 | (TREE_OPERAND (expression, 0))); |
c8094d83 | 15740 | |
6615c446 JO |
15741 | case tcc_comparison: |
15742 | case tcc_binary: | |
c8094d83 | 15743 | return ((value_dependent_expression_p |
1fb3244a | 15744 | (TREE_OPERAND (expression, 0))) |
c8094d83 | 15745 | || (value_dependent_expression_p |
1fb3244a | 15746 | (TREE_OPERAND (expression, 1)))); |
c8094d83 | 15747 | |
6615c446 | 15748 | case tcc_expression: |
5039610b | 15749 | case tcc_vl_exp: |
1fb3244a MM |
15750 | { |
15751 | int i; | |
5039610b | 15752 | for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i) |
14d22dd6 MM |
15753 | /* In some cases, some of the operands may be missing. |
15754 | (For example, in the case of PREDECREMENT_EXPR, the | |
15755 | amount to increment by may be missing.) That doesn't | |
15756 | make the expression dependent. */ | |
15757 | if (TREE_OPERAND (expression, i) | |
15758 | && (value_dependent_expression_p | |
15759 | (TREE_OPERAND (expression, i)))) | |
1fb3244a MM |
15760 | return true; |
15761 | return false; | |
15762 | } | |
c8094d83 | 15763 | |
6615c446 | 15764 | default: |
7416ab02 | 15765 | break; |
1fb3244a MM |
15766 | } |
15767 | } | |
c8094d83 | 15768 | |
1fb3244a MM |
15769 | /* The expression is not value-dependent. */ |
15770 | return false; | |
15771 | } | |
15772 | ||
15773 | /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of | |
15774 | [temp.dep.expr]. */ | |
15775 | ||
15776 | bool | |
3a978d72 | 15777 | type_dependent_expression_p (tree expression) |
1fb3244a MM |
15778 | { |
15779 | if (!processing_template_decl) | |
15780 | return false; | |
15781 | ||
7efa3e22 NS |
15782 | if (expression == error_mark_node) |
15783 | return false; | |
10b1d5e7 MM |
15784 | |
15785 | /* An unresolved name is always dependent. */ | |
b01e6d2b JM |
15786 | if (TREE_CODE (expression) == IDENTIFIER_NODE |
15787 | || TREE_CODE (expression) == USING_DECL) | |
10b1d5e7 | 15788 | return true; |
c8094d83 | 15789 | |
1fb3244a MM |
15790 | /* Some expression forms are never type-dependent. */ |
15791 | if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR | |
15792 | || TREE_CODE (expression) == SIZEOF_EXPR | |
15793 | || TREE_CODE (expression) == ALIGNOF_EXPR | |
cb68ec50 | 15794 | || TREE_CODE (expression) == TRAIT_EXPR |
1fb3244a MM |
15795 | || TREE_CODE (expression) == TYPEID_EXPR |
15796 | || TREE_CODE (expression) == DELETE_EXPR | |
15797 | || TREE_CODE (expression) == VEC_DELETE_EXPR | |
15798 | || TREE_CODE (expression) == THROW_EXPR) | |
15799 | return false; | |
15800 | ||
15801 | /* The types of these expressions depends only on the type to which | |
15802 | the cast occurs. */ | |
15803 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR | |
15804 | || TREE_CODE (expression) == STATIC_CAST_EXPR | |
15805 | || TREE_CODE (expression) == CONST_CAST_EXPR | |
15806 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR | |
15807 | || TREE_CODE (expression) == CAST_EXPR) | |
15808 | return dependent_type_p (TREE_TYPE (expression)); | |
d17811fd | 15809 | |
1fb3244a MM |
15810 | /* The types of these expressions depends only on the type created |
15811 | by the expression. */ | |
d17811fd MM |
15812 | if (TREE_CODE (expression) == NEW_EXPR |
15813 | || TREE_CODE (expression) == VEC_NEW_EXPR) | |
09d2f85f KL |
15814 | { |
15815 | /* For NEW_EXPR tree nodes created inside a template, either | |
15816 | the object type itself or a TREE_LIST may appear as the | |
15817 | operand 1. */ | |
15818 | tree type = TREE_OPERAND (expression, 1); | |
15819 | if (TREE_CODE (type) == TREE_LIST) | |
15820 | /* This is an array type. We need to check array dimensions | |
15821 | as well. */ | |
15822 | return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) | |
15823 | || value_dependent_expression_p | |
15824 | (TREE_OPERAND (TREE_VALUE (type), 1)); | |
15825 | else | |
15826 | return dependent_type_p (type); | |
15827 | } | |
1fb3244a | 15828 | |
5a57f1b2 JM |
15829 | if (TREE_CODE (expression) == SCOPE_REF |
15830 | && dependent_scope_ref_p (expression, | |
15831 | type_dependent_expression_p)) | |
15832 | return true; | |
15833 | ||
15834 | if (TREE_CODE (expression) == FUNCTION_DECL | |
15835 | && DECL_LANG_SPECIFIC (expression) | |
15836 | && DECL_TEMPLATE_INFO (expression) | |
15837 | && (any_dependent_template_arguments_p | |
15838 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) | |
15839 | return true; | |
15840 | ||
15841 | if (TREE_CODE (expression) == TEMPLATE_DECL | |
15842 | && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression)) | |
15843 | return false; | |
15844 | ||
c2acde1e JM |
15845 | if (TREE_CODE (expression) == STMT_EXPR) |
15846 | expression = stmt_expr_value_expr (expression); | |
15847 | ||
d17811fd MM |
15848 | if (TREE_TYPE (expression) == unknown_type_node) |
15849 | { | |
15850 | if (TREE_CODE (expression) == ADDR_EXPR) | |
15851 | return type_dependent_expression_p (TREE_OPERAND (expression, 0)); | |
c353b8e3 MM |
15852 | if (TREE_CODE (expression) == COMPONENT_REF |
15853 | || TREE_CODE (expression) == OFFSET_REF) | |
6cb89308 NS |
15854 | { |
15855 | if (type_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
15856 | return true; | |
15857 | expression = TREE_OPERAND (expression, 1); | |
15858 | if (TREE_CODE (expression) == IDENTIFIER_NODE) | |
15859 | return false; | |
15860 | } | |
3601f003 KL |
15861 | /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */ |
15862 | if (TREE_CODE (expression) == SCOPE_REF) | |
15863 | return false; | |
c8094d83 | 15864 | |
d17811fd MM |
15865 | if (TREE_CODE (expression) == BASELINK) |
15866 | expression = BASELINK_FUNCTIONS (expression); | |
c8094d83 | 15867 | |
d17811fd MM |
15868 | if (TREE_CODE (expression) == TEMPLATE_ID_EXPR) |
15869 | { | |
ee3071ef NS |
15870 | if (any_dependent_template_arguments_p |
15871 | (TREE_OPERAND (expression, 1))) | |
d17811fd MM |
15872 | return true; |
15873 | expression = TREE_OPERAND (expression, 0); | |
15874 | } | |
b207d6e2 MM |
15875 | gcc_assert (TREE_CODE (expression) == OVERLOAD |
15876 | || TREE_CODE (expression) == FUNCTION_DECL); | |
c8094d83 | 15877 | |
315fb5db | 15878 | while (expression) |
d17811fd | 15879 | { |
315fb5db NS |
15880 | if (type_dependent_expression_p (OVL_CURRENT (expression))) |
15881 | return true; | |
15882 | expression = OVL_NEXT (expression); | |
d17811fd | 15883 | } |
315fb5db | 15884 | return false; |
d17811fd | 15885 | } |
c8094d83 | 15886 | |
3ce5fa4f | 15887 | gcc_assert (TREE_CODE (expression) != TYPE_DECL); |
3db45ab5 | 15888 | |
1fb3244a MM |
15889 | return (dependent_type_p (TREE_TYPE (expression))); |
15890 | } | |
15891 | ||
d17811fd MM |
15892 | /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call) |
15893 | contains a type-dependent expression. */ | |
1fb3244a MM |
15894 | |
15895 | bool | |
58f9752a | 15896 | any_type_dependent_arguments_p (const_tree args) |
d17811fd MM |
15897 | { |
15898 | while (args) | |
15899 | { | |
7efa3e22 NS |
15900 | tree arg = TREE_VALUE (args); |
15901 | ||
15902 | if (type_dependent_expression_p (arg)) | |
d17811fd MM |
15903 | return true; |
15904 | args = TREE_CHAIN (args); | |
15905 | } | |
15906 | return false; | |
15907 | } | |
15908 | ||
32a11c08 MM |
15909 | /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are |
15910 | expressions) contains any value-dependent expressions. */ | |
15911 | ||
15912 | bool | |
58f9752a | 15913 | any_value_dependent_elements_p (const_tree list) |
32a11c08 MM |
15914 | { |
15915 | for (; list; list = TREE_CHAIN (list)) | |
15916 | if (value_dependent_expression_p (TREE_VALUE (list))) | |
15917 | return true; | |
15918 | ||
15919 | return false; | |
15920 | } | |
15921 | ||
d17811fd MM |
15922 | /* Returns TRUE if the ARG (a template argument) is dependent. */ |
15923 | ||
5d80a306 | 15924 | bool |
1fb3244a MM |
15925 | dependent_template_arg_p (tree arg) |
15926 | { | |
15927 | if (!processing_template_decl) | |
15928 | return false; | |
15929 | ||
15930 | if (TREE_CODE (arg) == TEMPLATE_DECL | |
15931 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
15932 | return dependent_template_p (arg); | |
5d80a306 DG |
15933 | else if (ARGUMENT_PACK_P (arg)) |
15934 | { | |
15935 | tree args = ARGUMENT_PACK_ARGS (arg); | |
15936 | int i, len = TREE_VEC_LENGTH (args); | |
15937 | for (i = 0; i < len; ++i) | |
15938 | { | |
15939 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
15940 | return true; | |
15941 | } | |
15942 | ||
15943 | return false; | |
15944 | } | |
1fb3244a MM |
15945 | else if (TYPE_P (arg)) |
15946 | return dependent_type_p (arg); | |
15947 | else | |
15948 | return (type_dependent_expression_p (arg) | |
15949 | || value_dependent_expression_p (arg)); | |
15950 | } | |
15951 | ||
06d40de8 DG |
15952 | /* Returns true if ARGS (a collection of template arguments) contains |
15953 | any types that require structural equality testing. */ | |
15954 | ||
15955 | bool | |
15956 | any_template_arguments_need_structural_equality_p (tree args) | |
15957 | { | |
15958 | int i; | |
15959 | int j; | |
15960 | ||
15961 | if (!args) | |
15962 | return false; | |
15963 | if (args == error_mark_node) | |
15964 | return true; | |
15965 | ||
15966 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) | |
15967 | { | |
15968 | tree level = TMPL_ARGS_LEVEL (args, i + 1); | |
15969 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) | |
15970 | { | |
15971 | tree arg = TREE_VEC_ELT (level, j); | |
5d80a306 DG |
15972 | tree packed_args = NULL_TREE; |
15973 | int k, len = 1; | |
15974 | ||
15975 | if (ARGUMENT_PACK_P (arg)) | |
15976 | { | |
15977 | /* Look inside the argument pack. */ | |
15978 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
15979 | len = TREE_VEC_LENGTH (packed_args); | |
15980 | } | |
15981 | ||
15982 | for (k = 0; k < len; ++k) | |
15983 | { | |
15984 | if (packed_args) | |
15985 | arg = TREE_VEC_ELT (packed_args, k); | |
15986 | ||
15987 | if (error_operand_p (arg)) | |
15988 | return true; | |
15989 | else if (TREE_CODE (arg) == TEMPLATE_DECL | |
15990 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
15991 | continue; | |
15992 | else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg)) | |
15993 | return true; | |
15994 | else if (!TYPE_P (arg) && TREE_TYPE (arg) | |
15995 | && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg))) | |
15996 | return true; | |
15997 | } | |
06d40de8 DG |
15998 | } |
15999 | } | |
16000 | ||
16001 | return false; | |
16002 | } | |
16003 | ||
d17811fd MM |
16004 | /* Returns true if ARGS (a collection of template arguments) contains |
16005 | any dependent arguments. */ | |
1fb3244a | 16006 | |
d17811fd | 16007 | bool |
58f9752a | 16008 | any_dependent_template_arguments_p (const_tree args) |
1fb3244a | 16009 | { |
bf12d54d | 16010 | int i; |
c353b8e3 MM |
16011 | int j; |
16012 | ||
d17811fd MM |
16013 | if (!args) |
16014 | return false; | |
7e497d0c VR |
16015 | if (args == error_mark_node) |
16016 | return true; | |
d17811fd | 16017 | |
c353b8e3 MM |
16018 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
16019 | { | |
58f9752a | 16020 | const_tree level = TMPL_ARGS_LEVEL (args, i + 1); |
c353b8e3 MM |
16021 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) |
16022 | if (dependent_template_arg_p (TREE_VEC_ELT (level, j))) | |
16023 | return true; | |
16024 | } | |
1fb3244a | 16025 | |
1fb3244a MM |
16026 | return false; |
16027 | } | |
16028 | ||
16029 | /* Returns TRUE if the template TMPL is dependent. */ | |
16030 | ||
16031 | bool | |
16032 | dependent_template_p (tree tmpl) | |
16033 | { | |
b95cc51a MM |
16034 | if (TREE_CODE (tmpl) == OVERLOAD) |
16035 | { | |
16036 | while (tmpl) | |
16037 | { | |
16038 | if (dependent_template_p (OVL_FUNCTION (tmpl))) | |
16039 | return true; | |
16040 | tmpl = OVL_CHAIN (tmpl); | |
16041 | } | |
16042 | return false; | |
16043 | } | |
16044 | ||
1fb3244a MM |
16045 | /* Template template parameters are dependent. */ |
16046 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) | |
16047 | || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) | |
16048 | return true; | |
27ab0504 | 16049 | /* So are names that have not been looked up. */ |
acccf788 MM |
16050 | if (TREE_CODE (tmpl) == SCOPE_REF |
16051 | || TREE_CODE (tmpl) == IDENTIFIER_NODE) | |
d17811fd | 16052 | return true; |
1fb3244a MM |
16053 | /* So are member templates of dependent classes. */ |
16054 | if (TYPE_P (CP_DECL_CONTEXT (tmpl))) | |
16055 | return dependent_type_p (DECL_CONTEXT (tmpl)); | |
16056 | return false; | |
16057 | } | |
16058 | ||
d17811fd MM |
16059 | /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ |
16060 | ||
16061 | bool | |
16062 | dependent_template_id_p (tree tmpl, tree args) | |
16063 | { | |
16064 | return (dependent_template_p (tmpl) | |
16065 | || any_dependent_template_arguments_p (args)); | |
16066 | } | |
16067 | ||
14d22dd6 | 16068 | /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the |
4195a767 NS |
16069 | TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if |
16070 | no such TYPE can be found. Note that this function peers inside | |
16071 | uninstantiated templates and therefore should be used only in | |
16072 | extremely limited situations. ONLY_CURRENT_P restricts this | |
16073 | peering to the currently open classes hierarchy (which is required | |
16074 | when comparing types). */ | |
14d22dd6 MM |
16075 | |
16076 | tree | |
16077 | resolve_typename_type (tree type, bool only_current_p) | |
16078 | { | |
16079 | tree scope; | |
16080 | tree name; | |
16081 | tree decl; | |
16082 | int quals; | |
4514aa8c | 16083 | tree pushed_scope; |
4195a767 | 16084 | tree result; |
14d22dd6 | 16085 | |
50bc768d | 16086 | gcc_assert (TREE_CODE (type) == TYPENAME_TYPE); |
14d22dd6 MM |
16087 | |
16088 | scope = TYPE_CONTEXT (type); | |
16089 | name = TYPE_IDENTIFIER (type); | |
16090 | ||
16091 | /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve | |
16092 | it first before we can figure out what NAME refers to. */ | |
16093 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
16094 | scope = resolve_typename_type (scope, only_current_p); | |
16095 | /* If we don't know what SCOPE refers to, then we cannot resolve the | |
16096 | TYPENAME_TYPE. */ | |
4195a767 NS |
16097 | if (TREE_CODE (scope) == TYPENAME_TYPE) |
16098 | return type; | |
14d22dd6 MM |
16099 | /* If the SCOPE is a template type parameter, we have no way of |
16100 | resolving the name. */ | |
16101 | if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM) | |
16102 | return type; | |
16103 | /* If the SCOPE is not the current instantiation, there's no reason | |
16104 | to look inside it. */ | |
16105 | if (only_current_p && !currently_open_class (scope)) | |
4195a767 | 16106 | return type; |
7a20d689 JM |
16107 | /* If SCOPE isn't the template itself, it will not have a valid |
16108 | TYPE_FIELDS list. */ | |
16109 | if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope))) | |
16110 | /* scope is either the template itself or a compatible instantiation | |
16111 | like X<T>, so look up the name in the original template. */ | |
16112 | scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope); | |
16113 | else | |
16114 | /* scope is a partial instantiation, so we can't do the lookup or we | |
16115 | will lose the template arguments. */ | |
16116 | return type; | |
14d22dd6 MM |
16117 | /* Enter the SCOPE so that name lookup will be resolved as if we |
16118 | were in the class definition. In particular, SCOPE will no | |
16119 | longer be considered a dependent type. */ | |
4514aa8c | 16120 | pushed_scope = push_scope (scope); |
14d22dd6 | 16121 | /* Look up the declaration. */ |
86ac0575 | 16122 | decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true); |
4195a767 NS |
16123 | |
16124 | result = NULL_TREE; | |
16125 | ||
14d22dd6 MM |
16126 | /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to |
16127 | find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ | |
16128 | if (!decl) | |
4195a767 | 16129 | /*nop*/; |
14d22dd6 MM |
16130 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE |
16131 | && TREE_CODE (decl) == TYPE_DECL) | |
4195a767 NS |
16132 | { |
16133 | result = TREE_TYPE (decl); | |
16134 | if (result == error_mark_node) | |
16135 | result = NULL_TREE; | |
16136 | } | |
14d22dd6 MM |
16137 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR |
16138 | && DECL_CLASS_TEMPLATE_P (decl)) | |
16139 | { | |
16140 | tree tmpl; | |
16141 | tree args; | |
16142 | /* Obtain the template and the arguments. */ | |
16143 | tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0); | |
16144 | args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1); | |
16145 | /* Instantiate the template. */ | |
4195a767 NS |
16146 | result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, |
16147 | /*entering_scope=*/0, | |
16148 | tf_error | tf_user); | |
16149 | if (result == error_mark_node) | |
16150 | result = NULL_TREE; | |
14d22dd6 | 16151 | } |
4195a767 | 16152 | |
14d22dd6 | 16153 | /* Leave the SCOPE. */ |
4514aa8c NS |
16154 | if (pushed_scope) |
16155 | pop_scope (pushed_scope); | |
14d22dd6 | 16156 | |
4195a767 NS |
16157 | /* If we failed to resolve it, return the original typename. */ |
16158 | if (!result) | |
16159 | return type; | |
16160 | ||
16161 | /* If lookup found a typename type, resolve that too. */ | |
16162 | if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result)) | |
16163 | { | |
16164 | /* Ill-formed programs can cause infinite recursion here, so we | |
16165 | must catch that. */ | |
16166 | TYPENAME_IS_RESOLVING_P (type) = 1; | |
16167 | result = resolve_typename_type (result, only_current_p); | |
16168 | TYPENAME_IS_RESOLVING_P (type) = 0; | |
16169 | } | |
16170 | ||
16171 | /* Qualify the resulting type. */ | |
16172 | quals = cp_type_quals (type); | |
16173 | if (quals) | |
16174 | result = cp_build_qualified_type (result, cp_type_quals (result) | quals); | |
16175 | ||
16176 | return result; | |
14d22dd6 MM |
16177 | } |
16178 | ||
d17811fd MM |
16179 | /* EXPR is an expression which is not type-dependent. Return a proxy |
16180 | for EXPR that can be used to compute the types of larger | |
16181 | expressions containing EXPR. */ | |
16182 | ||
16183 | tree | |
16184 | build_non_dependent_expr (tree expr) | |
16185 | { | |
0deb916c MM |
16186 | tree inner_expr; |
16187 | ||
c8094d83 | 16188 | /* Preserve null pointer constants so that the type of things like |
d17811fd MM |
16189 | "p == 0" where "p" is a pointer can be determined. */ |
16190 | if (null_ptr_cst_p (expr)) | |
16191 | return expr; | |
16192 | /* Preserve OVERLOADs; the functions must be available to resolve | |
16193 | types. */ | |
c497db75 | 16194 | inner_expr = expr; |
c2acde1e JM |
16195 | if (TREE_CODE (inner_expr) == STMT_EXPR) |
16196 | inner_expr = stmt_expr_value_expr (inner_expr); | |
c497db75 AO |
16197 | if (TREE_CODE (inner_expr) == ADDR_EXPR) |
16198 | inner_expr = TREE_OPERAND (inner_expr, 0); | |
16199 | if (TREE_CODE (inner_expr) == COMPONENT_REF) | |
16200 | inner_expr = TREE_OPERAND (inner_expr, 1); | |
2226e997 | 16201 | if (is_overloaded_fn (inner_expr) |
6439fffd | 16202 | || TREE_CODE (inner_expr) == OFFSET_REF) |
d17811fd | 16203 | return expr; |
5ae9ba3e MM |
16204 | /* There is no need to return a proxy for a variable. */ |
16205 | if (TREE_CODE (expr) == VAR_DECL) | |
16206 | return expr; | |
7433e6d4 MM |
16207 | /* Preserve string constants; conversions from string constants to |
16208 | "char *" are allowed, even though normally a "const char *" | |
16209 | cannot be used to initialize a "char *". */ | |
16210 | if (TREE_CODE (expr) == STRING_CST) | |
16211 | return expr; | |
b7c707d1 MM |
16212 | /* Preserve arithmetic constants, as an optimization -- there is no |
16213 | reason to create a new node. */ | |
16214 | if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST) | |
16215 | return expr; | |
9b7be7b5 MM |
16216 | /* Preserve THROW_EXPRs -- all throw-expressions have type "void". |
16217 | There is at least one place where we want to know that a | |
16218 | particular expression is a throw-expression: when checking a ?: | |
16219 | expression, there are special rules if the second or third | |
878cbb73 | 16220 | argument is a throw-expression. */ |
9b7be7b5 MM |
16221 | if (TREE_CODE (expr) == THROW_EXPR) |
16222 | return expr; | |
47d4c811 NS |
16223 | |
16224 | if (TREE_CODE (expr) == COND_EXPR) | |
f293ce4b RS |
16225 | return build3 (COND_EXPR, |
16226 | TREE_TYPE (expr), | |
16227 | TREE_OPERAND (expr, 0), | |
c8094d83 | 16228 | (TREE_OPERAND (expr, 1) |
f293ce4b RS |
16229 | ? build_non_dependent_expr (TREE_OPERAND (expr, 1)) |
16230 | : build_non_dependent_expr (TREE_OPERAND (expr, 0))), | |
16231 | build_non_dependent_expr (TREE_OPERAND (expr, 2))); | |
8e1daa34 NS |
16232 | if (TREE_CODE (expr) == COMPOUND_EXPR |
16233 | && !COMPOUND_EXPR_OVERLOADED (expr)) | |
f293ce4b RS |
16234 | return build2 (COMPOUND_EXPR, |
16235 | TREE_TYPE (expr), | |
16236 | TREE_OPERAND (expr, 0), | |
16237 | build_non_dependent_expr (TREE_OPERAND (expr, 1))); | |
c8094d83 | 16238 | |
fdeff563 NS |
16239 | /* If the type is unknown, it can't really be non-dependent */ |
16240 | gcc_assert (TREE_TYPE (expr) != unknown_type_node); | |
3db45ab5 | 16241 | |
c8094d83 | 16242 | /* Otherwise, build a NON_DEPENDENT_EXPR. |
d17811fd MM |
16243 | |
16244 | REFERENCE_TYPEs are not stripped for expressions in templates | |
16245 | because doing so would play havoc with mangling. Consider, for | |
16246 | example: | |
16247 | ||
c8094d83 | 16248 | template <typename T> void f<T& g>() { g(); } |
d17811fd MM |
16249 | |
16250 | In the body of "f", the expression for "g" will have | |
16251 | REFERENCE_TYPE, even though the standard says that it should | |
16252 | not. The reason is that we must preserve the syntactic form of | |
16253 | the expression so that mangling (say) "f<g>" inside the body of | |
16254 | "f" works out correctly. Therefore, the REFERENCE_TYPE is | |
16255 | stripped here. */ | |
018a5803 | 16256 | return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr); |
d17811fd MM |
16257 | } |
16258 | ||
16259 | /* ARGS is a TREE_LIST of expressions as arguments to a function call. | |
16260 | Return a new TREE_LIST with the various arguments replaced with | |
16261 | equivalent non-dependent expressions. */ | |
16262 | ||
16263 | tree | |
16264 | build_non_dependent_args (tree args) | |
16265 | { | |
16266 | tree a; | |
16267 | tree new_args; | |
16268 | ||
16269 | new_args = NULL_TREE; | |
16270 | for (a = args; a; a = TREE_CHAIN (a)) | |
c8094d83 | 16271 | new_args = tree_cons (NULL_TREE, |
d17811fd MM |
16272 | build_non_dependent_expr (TREE_VALUE (a)), |
16273 | new_args); | |
16274 | return nreverse (new_args); | |
16275 | } | |
16276 | ||
e2500fed | 16277 | #include "gt-cp-pt.h" |