]>
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, |
aac73a6d | 3 | 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010 |
493e377c | 4 | Free Software Foundation, Inc. |
8d08fdba | 5 | Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. |
fc378698 | 6 | Rewritten by Jason Merrill (jason@cygnus.com). |
8d08fdba | 7 | |
f5adbb8d | 8 | This file is part of GCC. |
8d08fdba | 9 | |
f5adbb8d | 10 | GCC is free software; you can redistribute it and/or modify |
8d08fdba | 11 | it under the terms of the GNU General Public License as published by |
e77f031d | 12 | the Free Software Foundation; either version 3, or (at your option) |
8d08fdba MS |
13 | any later version. |
14 | ||
f5adbb8d | 15 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
e77f031d NC |
21 | along with GCC; see the file COPYING3. If not see |
22 | <http://www.gnu.org/licenses/>. */ | |
8d08fdba MS |
23 | |
24 | /* Known bugs or deficiencies include: | |
e92cc029 | 25 | |
e92cc029 MS |
26 | all methods must be provided in header files; can't use a source |
27 | file that contains only the method templates and "just win". */ | |
8d08fdba MS |
28 | |
29 | #include "config.h" | |
8d052bc7 | 30 | #include "system.h" |
4977bab6 ZW |
31 | #include "coretypes.h" |
32 | #include "tm.h" | |
8d08fdba | 33 | #include "tree.h" |
6ba6f70d | 34 | #include "intl.h" |
0c58f841 | 35 | #include "pointer-set.h" |
8d08fdba MS |
36 | #include "flags.h" |
37 | #include "cp-tree.h" | |
39dabefd | 38 | #include "c-family/c-common.h" |
e58a9aa1 | 39 | #include "cp-objcp-common.h" |
25af8512 | 40 | #include "tree-inline.h" |
8d08fdba | 41 | #include "decl.h" |
e8abc66f | 42 | #include "output.h" |
54f92bfb | 43 | #include "toplev.h" |
297a5329 | 44 | #include "timevar.h" |
325c3691 | 45 | #include "tree-iterator.h" |
53824026 | 46 | #include "vecprim.h" |
49c249e1 | 47 | |
050367a3 MM |
48 | /* The type of functions taking a tree, and some additional data, and |
49 | returning an int. */ | |
3a978d72 | 50 | typedef int (*tree_fn_t) (tree, void*); |
050367a3 | 51 | |
0aafb128 MM |
52 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose |
53 | instantiations have been deferred, either because their definitions | |
e2c3721c | 54 | were not yet available, or because we were putting off doing the work. */ |
d1b38208 | 55 | struct GTY (()) pending_template { |
e2c3721c PB |
56 | struct pending_template *next; |
57 | struct tinst_level *tinst; | |
58 | }; | |
59 | ||
60 | static GTY(()) struct pending_template *pending_templates; | |
61 | static GTY(()) struct pending_template *last_pending_template; | |
73aad9b9 | 62 | |
67ffc812 | 63 | int processing_template_parmlist; |
386b8a85 JM |
64 | static int template_header_count; |
65 | ||
e2500fed | 66 | static GTY(()) tree saved_trees; |
53824026 | 67 | static VEC(int,heap) *inline_parm_levels; |
75650646 | 68 | |
e2c3721c | 69 | static GTY(()) struct tinst_level *current_tinst_level; |
3ae18eaf | 70 | |
2b59fc25 KL |
71 | static GTY(()) tree saved_access_scope; |
72 | ||
0fe0caa6 RH |
73 | /* Live only within one (recursive) call to tsubst_expr. We use |
74 | this to pass the statement expression node from the STMT_EXPR | |
75 | to the EXPR_STMT that is its result. */ | |
76 | static tree cur_stmt_expr; | |
77 | ||
6dfbb909 MM |
78 | /* A map from local variable declarations in the body of the template |
79 | presently being instantiated to the corresponding instantiated | |
80 | local variables. */ | |
81 | static htab_t local_specializations; | |
82 | ||
2b110bfc JM |
83 | typedef struct GTY(()) spec_entry |
84 | { | |
85 | tree tmpl; | |
86 | tree args; | |
87 | tree spec; | |
88 | } spec_entry; | |
89 | ||
90 | static GTY ((param_is (spec_entry))) | |
91 | htab_t decl_specializations; | |
92 | ||
93 | static GTY ((param_is (spec_entry))) | |
94 | htab_t type_specializations; | |
95 | ||
410cf6e6 | 96 | /* Contains canonical template parameter types. The vector is indexed by |
06d40de8 DG |
97 | the TEMPLATE_TYPE_IDX of the template parameter. Each element is a |
98 | TREE_LIST, whose TREE_VALUEs contain the canonical template | |
99 | parameters of various types and levels. */ | |
100 | static GTY(()) VEC(tree,gc) *canonical_template_parms; | |
101 | ||
830bfa74 MM |
102 | #define UNIFY_ALLOW_NONE 0 |
103 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
104 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
105 | #define UNIFY_ALLOW_DERIVED 4 | |
161c12b0 | 106 | #define UNIFY_ALLOW_INTEGER 8 |
028d1f20 | 107 | #define UNIFY_ALLOW_OUTER_LEVEL 16 |
62e4a758 NS |
108 | #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 |
109 | #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 | |
830bfa74 | 110 | |
3a978d72 NN |
111 | static void push_access_scope (tree); |
112 | static void pop_access_scope (tree); | |
acde59b4 MM |
113 | static bool resolve_overloaded_unification (tree, tree, tree, tree, |
114 | unification_kind_t, int); | |
3a978d72 | 115 | static int try_one_overload (tree, tree, tree, tree, tree, |
f23fb7f5 | 116 | unification_kind_t, int, bool); |
3a978d72 NN |
117 | static int unify (tree, tree, tree, tree, int); |
118 | static void add_pending_template (tree); | |
e2c3721c | 119 | static tree reopen_tinst_level (struct tinst_level *); |
3a978d72 | 120 | static tree tsubst_initializer_list (tree, tree); |
3a978d72 | 121 | static tree get_class_bindings (tree, tree, tree); |
3db45ab5 | 122 | static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, |
e7e93965 | 123 | bool, bool); |
3a978d72 NN |
124 | static void tsubst_enum (tree, tree, tree); |
125 | static tree add_to_template_args (tree, tree); | |
126 | static tree add_outermost_template_args (tree, tree); | |
127 | static bool check_instantiated_args (tree, tree, tsubst_flags_t); | |
8af2fec4 RY |
128 | static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*, |
129 | tree); | |
c166b898 ILT |
130 | static int type_unification_real (tree, tree, tree, const tree *, |
131 | unsigned int, int, unification_kind_t, int); | |
3a978d72 | 132 | static void note_template_header (int); |
b6ab6892 | 133 | static tree convert_nontype_argument_function (tree, tree); |
40013af7 | 134 | static tree convert_nontype_argument (tree, tree, tsubst_flags_t); |
3a978d72 NN |
135 | static tree convert_template_argument (tree, tree, tree, |
136 | tsubst_flags_t, int, tree); | |
0c58f841 | 137 | static int for_each_template_parm (tree, tree_fn_t, void*, |
c095a4f8 | 138 | struct pointer_set_t*, bool); |
5d80a306 | 139 | static tree expand_template_argument_pack (tree); |
6f1abb06 | 140 | static tree build_template_parm_index (int, int, int, int, tree, tree); |
ae95e46e | 141 | static bool inline_needs_template_parms (tree); |
3a978d72 | 142 | static void push_inline_template_parms_recursive (tree, int); |
3a978d72 | 143 | static tree retrieve_local_specialization (tree); |
3a978d72 | 144 | static void register_local_specialization (tree, tree); |
2b110bfc | 145 | static hashval_t hash_specialization (const void *p); |
ef3b7b17 | 146 | static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t); |
3a978d72 NN |
147 | static int mark_template_parm (tree, void *); |
148 | static int template_parm_this_level_p (tree, void *); | |
149 | static tree tsubst_friend_function (tree, tree); | |
150 | static tree tsubst_friend_class (tree, tree); | |
151 | static int can_complete_type_without_circularity (tree); | |
a34d3336 | 152 | static tree get_bindings (tree, tree, tree, bool); |
3a978d72 NN |
153 | static int template_decl_level (tree); |
154 | static int check_cv_quals_for_unify (int, tree, tree); | |
5d80a306 DG |
155 | static void template_parm_level_and_index (tree, int*, int*); |
156 | static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool); | |
a91db711 NS |
157 | static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); |
158 | static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); | |
3a978d72 NN |
159 | static tree tsubst_template_parms (tree, tree, tsubst_flags_t); |
160 | static void regenerate_decl_from_template (tree, tree); | |
ebc258f1 | 161 | static tree most_specialized_class (tree, tree, tsubst_flags_t); |
3a978d72 | 162 | static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); |
3a978d72 NN |
163 | static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree); |
164 | static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); | |
9b910171 | 165 | static bool check_specialization_scope (void); |
3a978d72 NN |
166 | static tree process_partial_specialization (tree); |
167 | static void set_current_access_from_decl (tree); | |
3a978d72 | 168 | static tree get_template_base (tree, tree, tree, tree); |
3a978d72 NN |
169 | static tree try_class_unification (tree, tree, tree, tree); |
170 | static int coerce_template_template_parms (tree, tree, tsubst_flags_t, | |
171 | tree, tree); | |
4cf36211 | 172 | static bool template_template_parm_bindings_ok_p (tree, tree); |
3a978d72 NN |
173 | static int template_args_equal (tree, tree); |
174 | static void tsubst_default_arguments (tree); | |
175 | static tree for_each_template_parm_r (tree *, int *, void *); | |
176 | static tree copy_default_args_to_explicit_spec_1 (tree, tree); | |
177 | static void copy_default_args_to_explicit_spec (tree); | |
178 | static int invalid_nontype_parm_type_p (tree, tsubst_flags_t); | |
a723baf1 | 179 | static int eq_local_specializations (const void *, const void *); |
5d80a306 | 180 | static bool dependent_template_arg_p (tree); |
06d40de8 | 181 | static bool any_template_arguments_need_structural_equality_p (tree); |
5552b43c | 182 | static bool dependent_type_p_r (tree); |
015c2c66 | 183 | static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool); |
14d22dd6 | 184 | static tree tsubst_copy (tree, tree, tsubst_flags_t, tree); |
5d80a306 | 185 | static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree); |
6afcfe0a | 186 | static tree tsubst_decl (tree, tree, tsubst_flags_t); |
d0940d56 | 187 | static void perform_typedefs_access_check (tree tmpl, tree targs); |
aa373032 DS |
188 | static void append_type_to_template_for_access_check_1 (tree, tree, tree, |
189 | location_t); | |
5f5babf1 JM |
190 | static tree listify (tree); |
191 | static tree listify_autos (tree, tree); | |
6f1abb06 DS |
192 | static tree template_parm_to_arg (tree t); |
193 | static tree current_template_args (void); | |
194 | static tree fixup_template_type_parm_type (tree, int); | |
195 | static tree fixup_template_parm_index (tree, tree, int); | |
196 | static void fixup_template_parms (void); | |
197 | static tree tsubst_template_parm (tree, tree, tsubst_flags_t); | |
36a117a5 | 198 | |
2b59fc25 KL |
199 | /* Make the current scope suitable for access checking when we are |
200 | processing T. T can be FUNCTION_DECL for instantiated function | |
2b907f5c KL |
201 | template, or VAR_DECL for static member variable (need by |
202 | instantiate_decl). */ | |
2b59fc25 | 203 | |
8ce33230 | 204 | static void |
2b907f5c | 205 | push_access_scope (tree t) |
2b59fc25 | 206 | { |
50bc768d NS |
207 | gcc_assert (TREE_CODE (t) == FUNCTION_DECL |
208 | || TREE_CODE (t) == VAR_DECL); | |
2b59fc25 | 209 | |
23ff7e2d KL |
210 | if (DECL_FRIEND_CONTEXT (t)) |
211 | push_nested_class (DECL_FRIEND_CONTEXT (t)); | |
212 | else if (DECL_CLASS_SCOPE_P (t)) | |
2b907f5c | 213 | push_nested_class (DECL_CONTEXT (t)); |
0f399e5f KL |
214 | else |
215 | push_to_top_level (); | |
c8094d83 | 216 | |
2b907f5c | 217 | if (TREE_CODE (t) == FUNCTION_DECL) |
0f399e5f KL |
218 | { |
219 | saved_access_scope = tree_cons | |
220 | (NULL_TREE, current_function_decl, saved_access_scope); | |
221 | current_function_decl = t; | |
222 | } | |
2b59fc25 KL |
223 | } |
224 | ||
2b59fc25 KL |
225 | /* Restore the scope set up by push_access_scope. T is the node we |
226 | are processing. */ | |
227 | ||
8ce33230 | 228 | static void |
3a978d72 | 229 | pop_access_scope (tree t) |
2b59fc25 | 230 | { |
2b907f5c | 231 | if (TREE_CODE (t) == FUNCTION_DECL) |
2b59fc25 KL |
232 | { |
233 | current_function_decl = TREE_VALUE (saved_access_scope); | |
234 | saved_access_scope = TREE_CHAIN (saved_access_scope); | |
235 | } | |
0f399e5f | 236 | |
23ff7e2d | 237 | if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t)) |
0f399e5f KL |
238 | pop_nested_class (); |
239 | else | |
240 | pop_from_top_level (); | |
2b59fc25 KL |
241 | } |
242 | ||
a723baf1 MM |
243 | /* Do any processing required when DECL (a member template |
244 | declaration) is finished. Returns the TEMPLATE_DECL corresponding | |
245 | to DECL, unless it is a specialization, in which case the DECL | |
246 | itself is returned. */ | |
e1467ff2 MM |
247 | |
248 | tree | |
3a978d72 | 249 | finish_member_template_decl (tree decl) |
e1467ff2 | 250 | { |
a723baf1 MM |
251 | if (decl == error_mark_node) |
252 | return error_mark_node; | |
253 | ||
50bc768d | 254 | gcc_assert (DECL_P (decl)); |
a723baf1 MM |
255 | |
256 | if (TREE_CODE (decl) == TYPE_DECL) | |
93cdc044 | 257 | { |
a723baf1 MM |
258 | tree type; |
259 | ||
260 | type = TREE_TYPE (decl); | |
92fab505 DG |
261 | if (type == error_mark_node) |
262 | return error_mark_node; | |
9e1e64ec | 263 | if (MAYBE_CLASS_TYPE_P (type) |
a723baf1 MM |
264 | && CLASSTYPE_TEMPLATE_INFO (type) |
265 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
93cdc044 | 266 | { |
a723baf1 | 267 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
93cdc044 JM |
268 | check_member_template (tmpl); |
269 | return tmpl; | |
270 | } | |
8d019cef | 271 | return NULL_TREE; |
93cdc044 | 272 | } |
07c88314 | 273 | else if (TREE_CODE (decl) == FIELD_DECL) |
0f51ccfc | 274 | error ("data member %qD cannot be a member template", decl); |
a1da6cba | 275 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 276 | { |
a1da6cba MM |
277 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
278 | { | |
279 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
280 | return DECL_TI_TEMPLATE (decl); | |
281 | } | |
282 | else | |
283 | return decl; | |
c8094d83 | 284 | } |
a1da6cba | 285 | else |
0f51ccfc | 286 | error ("invalid member template declaration %qD", decl); |
e1467ff2 | 287 | |
a1da6cba | 288 | return error_mark_node; |
f84b4be9 | 289 | } |
e1467ff2 | 290 | |
aa373032 DS |
291 | /* Create a template info node. */ |
292 | ||
293 | tree | |
294 | build_template_info (tree template_decl, tree template_args) | |
295 | { | |
296 | tree result = make_node (TEMPLATE_INFO); | |
297 | TI_TEMPLATE (result) = template_decl; | |
298 | TI_ARGS (result) = template_args; | |
299 | return result; | |
300 | } | |
301 | ||
91a77d68 JM |
302 | /* Return the template info node corresponding to T, whatever T is. */ |
303 | ||
304 | tree | |
f9329c35 | 305 | get_template_info (const_tree t) |
91a77d68 JM |
306 | { |
307 | tree tinfo = NULL_TREE; | |
308 | ||
d40a19da DS |
309 | if (!t || t == error_mark_node) |
310 | return NULL; | |
311 | ||
91a77d68 JM |
312 | if (DECL_P (t) && DECL_LANG_SPECIFIC (t)) |
313 | tinfo = DECL_TEMPLATE_INFO (t); | |
314 | ||
d5f4eddd | 315 | if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t)) |
91a77d68 JM |
316 | t = TREE_TYPE (t); |
317 | ||
318 | if (TAGGED_TYPE_P (t)) | |
319 | tinfo = TYPE_TEMPLATE_INFO (t); | |
20c202f3 DS |
320 | else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
321 | tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t); | |
91a77d68 JM |
322 | |
323 | return tinfo; | |
324 | } | |
325 | ||
f84b4be9 | 326 | /* Returns the template nesting level of the indicated class TYPE. |
c8094d83 | 327 | |
f84b4be9 JM |
328 | For example, in: |
329 | template <class T> | |
330 | struct A | |
331 | { | |
332 | template <class U> | |
333 | struct B {}; | |
334 | }; | |
335 | ||
c8094d83 | 336 | A<T>::B<U> has depth two, while A<T> has depth one. |
39c01e4c | 337 | Both A<T>::B<int> and A<int>::B<U> have depth one, if |
260cd73f | 338 | they are instantiations, not specializations. |
39c01e4c MM |
339 | |
340 | This function is guaranteed to return 0 if passed NULL_TREE so | |
341 | that, for example, `template_class_depth (current_class_type)' is | |
342 | always safe. */ | |
f84b4be9 | 343 | |
260cd73f VR |
344 | int |
345 | template_class_depth (tree type) | |
f84b4be9 | 346 | { |
93cdc044 | 347 | int depth; |
f84b4be9 | 348 | |
c8094d83 | 349 | for (depth = 0; |
ed44da02 | 350 | type && TREE_CODE (type) != NAMESPACE_DECL; |
c8094d83 | 351 | type = (TREE_CODE (type) == FUNCTION_DECL) |
725214ac | 352 | ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type)) |
ed44da02 | 353 | { |
91a77d68 JM |
354 | tree tinfo = get_template_info (type); |
355 | ||
356 | if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) | |
357 | && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))) | |
358 | ++depth; | |
ed44da02 | 359 | } |
f84b4be9 JM |
360 | |
361 | return depth; | |
e1467ff2 | 362 | } |
98c1c668 | 363 | |
ae95e46e PC |
364 | /* Subroutine of maybe_begin_member_template_processing. |
365 | Returns true if processing DECL needs us to push template parms. */ | |
cae40af6 | 366 | |
ae95e46e | 367 | static bool |
3a978d72 | 368 | inline_needs_template_parms (tree decl) |
cae40af6 JM |
369 | { |
370 | if (! DECL_TEMPLATE_INFO (decl)) | |
ae95e46e | 371 | return false; |
f84b4be9 | 372 | |
36a117a5 | 373 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
374 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
375 | } | |
376 | ||
377 | /* Subroutine of maybe_begin_member_template_processing. | |
378 | Push the template parms in PARMS, starting from LEVELS steps into the | |
379 | chain, and ending at the beginning, since template parms are listed | |
380 | innermost first. */ | |
381 | ||
382 | static void | |
3a978d72 | 383 | push_inline_template_parms_recursive (tree parmlist, int levels) |
cae40af6 JM |
384 | { |
385 | tree parms = TREE_VALUE (parmlist); | |
386 | int i; | |
387 | ||
388 | if (levels > 1) | |
389 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 390 | |
98c1c668 | 391 | ++processing_template_decl; |
cae40af6 | 392 | current_template_parms |
4890c2f4 | 393 | = tree_cons (size_int (processing_template_decl), |
98c1c668 | 394 | parms, current_template_parms); |
cae40af6 JM |
395 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
396 | ||
ac20c67a | 397 | begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, |
0cbd7506 | 398 | NULL); |
c8094d83 | 399 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
98c1c668 | 400 | { |
0f67a82f | 401 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
42b304f1 | 402 | |
0f67a82f LM |
403 | if (parm == error_mark_node) |
404 | continue; | |
42b304f1 | 405 | |
50bc768d | 406 | gcc_assert (DECL_P (parm)); |
cae40af6 | 407 | |
98c1c668 JM |
408 | switch (TREE_CODE (parm)) |
409 | { | |
786b5245 | 410 | case TYPE_DECL: |
73b0fce8 | 411 | case TEMPLATE_DECL: |
98c1c668 JM |
412 | pushdecl (parm); |
413 | break; | |
786b5245 MM |
414 | |
415 | case PARM_DECL: | |
416 | { | |
fc03edb3 MM |
417 | /* Make a CONST_DECL as is done in process_template_parm. |
418 | It is ugly that we recreate this here; the original | |
419 | version built in process_template_parm is no longer | |
420 | available. */ | |
c2255bc4 AH |
421 | tree decl = build_decl (DECL_SOURCE_LOCATION (parm), |
422 | CONST_DECL, DECL_NAME (parm), | |
786b5245 | 423 | TREE_TYPE (parm)); |
c727aa5e | 424 | DECL_ARTIFICIAL (decl) = 1; |
6de9cd9a | 425 | TREE_CONSTANT (decl) = 1; |
6de9cd9a | 426 | TREE_READONLY (decl) = 1; |
786b5245 | 427 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
cd9f6678 | 428 | SET_DECL_TEMPLATE_PARM_P (decl); |
786b5245 MM |
429 | pushdecl (decl); |
430 | } | |
cae40af6 | 431 | break; |
786b5245 | 432 | |
98c1c668 | 433 | default: |
315fb5db | 434 | gcc_unreachable (); |
98c1c668 JM |
435 | } |
436 | } | |
437 | } | |
438 | ||
cae40af6 JM |
439 | /* Restore the template parameter context for a member template or |
440 | a friend template defined in a class definition. */ | |
441 | ||
442 | void | |
3a978d72 | 443 | maybe_begin_member_template_processing (tree decl) |
cae40af6 JM |
444 | { |
445 | tree parms; | |
3dcaad8b | 446 | int levels = 0; |
cae40af6 | 447 | |
3dcaad8b MM |
448 | if (inline_needs_template_parms (decl)) |
449 | { | |
450 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
451 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
cae40af6 | 452 | |
3dcaad8b MM |
453 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
454 | { | |
455 | --levels; | |
456 | parms = TREE_CHAIN (parms); | |
457 | } | |
cae40af6 | 458 | |
3dcaad8b | 459 | push_inline_template_parms_recursive (parms, levels); |
cae40af6 JM |
460 | } |
461 | ||
3dcaad8b MM |
462 | /* Remember how many levels of template parameters we pushed so that |
463 | we can pop them later. */ | |
53824026 | 464 | VEC_safe_push (int, heap, inline_parm_levels, levels); |
cae40af6 JM |
465 | } |
466 | ||
1875c2b7 | 467 | /* Undo the effects of maybe_begin_member_template_processing. */ |
98c1c668 | 468 | |
c8094d83 | 469 | void |
3a978d72 | 470 | maybe_end_member_template_processing (void) |
98c1c668 | 471 | { |
3dcaad8b | 472 | int i; |
53824026 | 473 | int last; |
3dcaad8b | 474 | |
53824026 | 475 | if (VEC_length (int, inline_parm_levels) == 0) |
98c1c668 JM |
476 | return; |
477 | ||
53824026 KH |
478 | last = VEC_pop (int, inline_parm_levels); |
479 | for (i = 0; i < last; ++i) | |
cae40af6 JM |
480 | { |
481 | --processing_template_decl; | |
482 | current_template_parms = TREE_CHAIN (current_template_parms); | |
483 | poplevel (0, 0, 0); | |
484 | } | |
98c1c668 JM |
485 | } |
486 | ||
36a117a5 | 487 | /* Return a new template argument vector which contains all of ARGS, |
f9a7ae04 | 488 | but has as its innermost set of arguments the EXTRA_ARGS. */ |
e6f1275f JM |
489 | |
490 | static tree | |
3a978d72 | 491 | add_to_template_args (tree args, tree extra_args) |
e6f1275f | 492 | { |
36a117a5 MM |
493 | tree new_args; |
494 | int extra_depth; | |
495 | int i; | |
496 | int j; | |
e6f1275f | 497 | |
fd9852df | 498 | if (args == NULL_TREE || extra_args == error_mark_node) |
5f431950 DS |
499 | return extra_args; |
500 | ||
36a117a5 | 501 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
f31c0a32 | 502 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); |
e6f1275f | 503 | |
36a117a5 MM |
504 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
505 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 506 | |
36a117a5 MM |
507 | for (j = 1; j <= extra_depth; ++j, ++i) |
508 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
c8094d83 | 509 | |
e6f1275f JM |
510 | return new_args; |
511 | } | |
512 | ||
36a117a5 MM |
513 | /* Like add_to_template_args, but only the outermost ARGS are added to |
514 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
515 | (EXTRA_ARGS) levels are added. This function is used to combine | |
516 | the template arguments from a partial instantiation with the | |
517 | template arguments used to attain the full instantiation from the | |
518 | partial instantiation. */ | |
98c1c668 | 519 | |
4966381a | 520 | static tree |
3a978d72 | 521 | add_outermost_template_args (tree args, tree extra_args) |
98c1c668 JM |
522 | { |
523 | tree new_args; | |
524 | ||
e4a84209 MM |
525 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
526 | something very fishy is going on. */ | |
50bc768d | 527 | gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args)); |
e4a84209 MM |
528 | |
529 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
530 | them. */ | |
531 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
532 | return extra_args; | |
533 | ||
36a117a5 MM |
534 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
535 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
c8094d83 | 536 | |
36a117a5 | 537 | new_args = add_to_template_args (args, extra_args); |
98c1c668 | 538 | |
36a117a5 MM |
539 | /* Now, we restore ARGS to its full dimensions. */ |
540 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
541 | |
542 | return new_args; | |
543 | } | |
5566b478 | 544 | |
f9a7ae04 MM |
545 | /* Return the N levels of innermost template arguments from the ARGS. */ |
546 | ||
547 | tree | |
3a978d72 | 548 | get_innermost_template_args (tree args, int n) |
f9a7ae04 MM |
549 | { |
550 | tree new_args; | |
551 | int extra_levels; | |
552 | int i; | |
553 | ||
50bc768d | 554 | gcc_assert (n >= 0); |
f9a7ae04 MM |
555 | |
556 | /* If N is 1, just return the innermost set of template arguments. */ | |
557 | if (n == 1) | |
558 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); | |
c8094d83 | 559 | |
f9a7ae04 MM |
560 | /* If we're not removing anything, just return the arguments we were |
561 | given. */ | |
562 | extra_levels = TMPL_ARGS_DEPTH (args) - n; | |
50bc768d | 563 | gcc_assert (extra_levels >= 0); |
f9a7ae04 MM |
564 | if (extra_levels == 0) |
565 | return args; | |
566 | ||
567 | /* Make a new set of arguments, not containing the outer arguments. */ | |
568 | new_args = make_tree_vec (n); | |
569 | for (i = 1; i <= n; ++i) | |
c8094d83 | 570 | SET_TMPL_ARGS_LEVEL (new_args, i, |
f9a7ae04 MM |
571 | TMPL_ARGS_LEVEL (args, i + extra_levels)); |
572 | ||
573 | return new_args; | |
574 | } | |
575 | ||
dc28490d JM |
576 | /* The inverse of get_innermost_template_args: Return all but the innermost |
577 | EXTRA_LEVELS levels of template arguments from the ARGS. */ | |
578 | ||
579 | static tree | |
580 | strip_innermost_template_args (tree args, int extra_levels) | |
581 | { | |
582 | tree new_args; | |
583 | int n = TMPL_ARGS_DEPTH (args) - extra_levels; | |
584 | int i; | |
585 | ||
586 | gcc_assert (n >= 0); | |
587 | ||
588 | /* If N is 1, just return the outermost set of template arguments. */ | |
589 | if (n == 1) | |
590 | return TMPL_ARGS_LEVEL (args, 1); | |
591 | ||
592 | /* If we're not removing anything, just return the arguments we were | |
593 | given. */ | |
594 | gcc_assert (extra_levels >= 0); | |
595 | if (extra_levels == 0) | |
596 | return args; | |
597 | ||
598 | /* Make a new set of arguments, not containing the inner arguments. */ | |
599 | new_args = make_tree_vec (n); | |
600 | for (i = 1; i <= n; ++i) | |
601 | SET_TMPL_ARGS_LEVEL (new_args, i, | |
602 | TMPL_ARGS_LEVEL (args, i)); | |
603 | ||
604 | return new_args; | |
605 | } | |
606 | ||
5566b478 MS |
607 | /* We've got a template header coming up; push to a new level for storing |
608 | the parms. */ | |
8d08fdba | 609 | |
8d08fdba | 610 | void |
3a978d72 | 611 | begin_template_parm_list (void) |
8d08fdba | 612 | { |
6757edfe MM |
613 | /* We use a non-tag-transparent scope here, which causes pushtag to |
614 | put tags in this scope, rather than in the enclosing class or | |
615 | namespace scope. This is the right thing, since we want | |
616 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
617 | global template class, push_template_decl handles putting the | |
618 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
619 | e.g.: | |
620 | ||
621 | template <class T> struct S1 { | |
0cbd7506 | 622 | template <class T> struct S2 {}; |
6757edfe MM |
623 | }; |
624 | ||
625 | pushtag contains special code to call pushdecl_with_scope on the | |
626 | TEMPLATE_DECL for S2. */ | |
ac20c67a | 627 | begin_scope (sk_template_parms, NULL); |
5156628f | 628 | ++processing_template_decl; |
67ffc812 | 629 | ++processing_template_parmlist; |
386b8a85 JM |
630 | note_template_header (0); |
631 | } | |
632 | ||
6c30752f | 633 | /* This routine is called when a specialization is declared. If it is |
9b910171 LM |
634 | invalid to declare a specialization here, an error is reported and |
635 | false is returned, otherwise this routine will return true. */ | |
6c30752f | 636 | |
9b910171 | 637 | static bool |
3a978d72 | 638 | check_specialization_scope (void) |
6c30752f MM |
639 | { |
640 | tree scope = current_scope (); | |
3ddfb0e6 | 641 | |
c8094d83 MS |
642 | /* [temp.expl.spec] |
643 | ||
6c30752f MM |
644 | An explicit specialization shall be declared in the namespace of |
645 | which the template is a member, or, for member templates, in the | |
646 | namespace of which the enclosing class or enclosing class | |
647 | template is a member. An explicit specialization of a member | |
648 | function, member class or static data member of a class template | |
649 | shall be declared in the namespace of which the class template | |
650 | is a member. */ | |
651 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
9b910171 LM |
652 | { |
653 | error ("explicit specialization in non-namespace scope %qD", scope); | |
654 | return false; | |
655 | } | |
3ddfb0e6 | 656 | |
c8094d83 | 657 | /* [temp.expl.spec] |
6c30752f MM |
658 | |
659 | In an explicit specialization declaration for a member of a class | |
660 | template or a member template that appears in namespace scope, | |
661 | the member template and some of its enclosing class templates may | |
662 | remain unspecialized, except that the declaration shall not | |
663 | explicitly specialize a class member template if its enclosing | |
664 | class templates are not explicitly specialized as well. */ | |
c8094d83 | 665 | if (current_template_parms) |
9b910171 LM |
666 | { |
667 | error ("enclosing class templates are not explicitly specialized"); | |
668 | return false; | |
669 | } | |
670 | ||
671 | return true; | |
6c30752f MM |
672 | } |
673 | ||
c6002625 | 674 | /* We've just seen template <>. */ |
386b8a85 | 675 | |
9b910171 | 676 | bool |
3a978d72 | 677 | begin_specialization (void) |
386b8a85 | 678 | { |
ac20c67a | 679 | begin_scope (sk_template_spec, NULL); |
386b8a85 | 680 | note_template_header (1); |
9b910171 | 681 | return check_specialization_scope (); |
386b8a85 JM |
682 | } |
683 | ||
dc957d14 | 684 | /* Called at then end of processing a declaration preceded by |
386b8a85 JM |
685 | template<>. */ |
686 | ||
c8094d83 | 687 | void |
3a978d72 | 688 | end_specialization (void) |
386b8a85 | 689 | { |
74b846e0 | 690 | finish_scope (); |
386b8a85 JM |
691 | reset_specialization (); |
692 | } | |
693 | ||
386b8a85 | 694 | /* Any template <>'s that we have seen thus far are not referring to a |
c6002625 | 695 | function specialization. */ |
386b8a85 JM |
696 | |
697 | void | |
3a978d72 | 698 | reset_specialization (void) |
386b8a85 JM |
699 | { |
700 | processing_specialization = 0; | |
701 | template_header_count = 0; | |
702 | } | |
703 | ||
838dfd8a | 704 | /* We've just seen a template header. If SPECIALIZATION is nonzero, |
386b8a85 JM |
705 | it was of the form template <>. */ |
706 | ||
c8094d83 | 707 | static void |
3a978d72 | 708 | note_template_header (int specialization) |
386b8a85 JM |
709 | { |
710 | processing_specialization = specialization; | |
711 | template_header_count++; | |
712 | } | |
713 | ||
75650646 | 714 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 715 | |
75650646 | 716 | void |
3a978d72 | 717 | begin_explicit_instantiation (void) |
386b8a85 | 718 | { |
50bc768d | 719 | gcc_assert (!processing_explicit_instantiation); |
a723baf1 | 720 | processing_explicit_instantiation = true; |
75650646 | 721 | } |
386b8a85 | 722 | |
386b8a85 | 723 | |
75650646 | 724 | void |
3a978d72 | 725 | end_explicit_instantiation (void) |
75650646 | 726 | { |
50bc768d | 727 | gcc_assert (processing_explicit_instantiation); |
a723baf1 | 728 | processing_explicit_instantiation = false; |
75650646 | 729 | } |
386b8a85 | 730 | |
13a44ee0 | 731 | /* An explicit specialization or partial specialization TMPL is being |
b1cc95ce MM |
732 | declared. Check that the namespace in which the specialization is |
733 | occurring is permissible. Returns false iff it is invalid to | |
734 | specialize TMPL in the current namespace. */ | |
c8094d83 | 735 | |
b1cc95ce MM |
736 | static bool |
737 | check_specialization_namespace (tree tmpl) | |
738 | { | |
739 | tree tpl_ns = decl_namespace_context (tmpl); | |
740 | ||
741 | /* [tmpl.expl.spec] | |
c8094d83 | 742 | |
b1cc95ce MM |
743 | An explicit specialization shall be declared in the namespace of |
744 | which the template is a member, or, for member templates, in the | |
745 | namespace of which the enclosing class or enclosing class | |
746 | template is a member. An explicit specialization of a member | |
747 | function, member class or static data member of a class template | |
748 | shall be declared in the namespace of which the class template is | |
749 | a member. */ | |
64776356 JM |
750 | if (current_scope() != DECL_CONTEXT (tmpl) |
751 | && !at_namespace_scope_p ()) | |
752 | { | |
753 | error ("specialization of %qD must appear at namespace scope", tmpl); | |
754 | return false; | |
755 | } | |
b1cc95ce MM |
756 | if (is_associated_namespace (current_namespace, tpl_ns)) |
757 | /* Same or super-using namespace. */ | |
758 | return true; | |
759 | else | |
760 | { | |
cbe5f3b3 MLI |
761 | permerror (input_location, "specialization of %qD in different namespace", tmpl); |
762 | permerror (input_location, " from definition of %q+#D", tmpl); | |
b1cc95ce MM |
763 | return false; |
764 | } | |
765 | } | |
766 | ||
9c12301f MM |
767 | /* SPEC is an explicit instantiation. Check that it is valid to |
768 | perform this explicit instantiation in the current namespace. */ | |
769 | ||
770 | static void | |
771 | check_explicit_instantiation_namespace (tree spec) | |
772 | { | |
773 | tree ns; | |
774 | ||
775 | /* DR 275: An explicit instantiation shall appear in an enclosing | |
3db45ab5 | 776 | namespace of its template. */ |
9c12301f MM |
777 | ns = decl_namespace_context (spec); |
778 | if (!is_ancestor (current_namespace, ns)) | |
cbe5f3b3 | 779 | permerror (input_location, "explicit instantiation of %qD in namespace %qD " |
37ec60ed JW |
780 | "(which does not enclose namespace %qD)", |
781 | spec, current_namespace, ns); | |
9c12301f MM |
782 | } |
783 | ||
36a117a5 MM |
784 | /* The TYPE is being declared. If it is a template type, that means it |
785 | is a partial specialization. Do appropriate error-checking. */ | |
786 | ||
9ce387d9 | 787 | tree |
3a978d72 | 788 | maybe_process_partial_specialization (tree type) |
36a117a5 | 789 | { |
c9043d03 VR |
790 | tree context; |
791 | ||
792 | if (type == error_mark_node) | |
9ce387d9 VR |
793 | return error_mark_node; |
794 | ||
795 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
796 | { | |
797 | error ("name of class shadows template template parameter %qD", | |
798 | TYPE_NAME (type)); | |
799 | return error_mark_node; | |
800 | } | |
c9043d03 VR |
801 | |
802 | context = TYPE_CONTEXT (type); | |
6e049fcd | 803 | |
a723baf1 | 804 | if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) |
36a117a5 | 805 | { |
6e049fcd KL |
806 | /* This is for ordinary explicit specialization and partial |
807 | specialization of a template class such as: | |
808 | ||
809 | template <> class C<int>; | |
810 | ||
811 | or: | |
812 | ||
813 | template <class T> class C<T*>; | |
814 | ||
815 | Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ | |
816 | ||
36a117a5 | 817 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) |
d0f062fb | 818 | && !COMPLETE_TYPE_P (type)) |
36a117a5 | 819 | { |
b1cc95ce | 820 | check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)); |
370af2d5 | 821 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
36a117a5 | 822 | if (processing_template_decl) |
c5285718 PC |
823 | { |
824 | if (push_template_decl (TYPE_MAIN_DECL (type)) | |
825 | == error_mark_node) | |
826 | return error_mark_node; | |
827 | } | |
36a117a5 MM |
828 | } |
829 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
0f51ccfc | 830 | error ("specialization of %qT after instantiation", type); |
36a117a5 | 831 | } |
6e049fcd KL |
832 | else if (CLASS_TYPE_P (type) |
833 | && !CLASSTYPE_USE_TEMPLATE (type) | |
834 | && CLASSTYPE_TEMPLATE_INFO (type) | |
835 | && context && CLASS_TYPE_P (context) | |
836 | && CLASSTYPE_TEMPLATE_INFO (context)) | |
837 | { | |
838 | /* This is for an explicit specialization of member class | |
839 | template according to [temp.expl.spec/18]: | |
840 | ||
841 | template <> template <class U> class C<int>::D; | |
842 | ||
843 | The context `C<int>' must be an implicit instantiation. | |
844 | Otherwise this is just a member class template declared | |
845 | earlier like: | |
846 | ||
847 | template <> class C<int> { template <class U> class D; }; | |
848 | template <> template <class U> class C<int>::D; | |
849 | ||
850 | In the first case, `C<int>::D' is a specialization of `C<T>::D' | |
851 | while in the second case, `C<int>::D' is a primary template | |
852 | and `C<T>::D' may not exist. */ | |
853 | ||
854 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) | |
855 | && !COMPLETE_TYPE_P (type)) | |
856 | { | |
857 | tree t; | |
2b110bfc | 858 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
6e049fcd KL |
859 | |
860 | if (current_namespace | |
2b110bfc | 861 | != decl_namespace_context (tmpl)) |
6e049fcd | 862 | { |
cbe5f3b3 | 863 | permerror (input_location, "specializing %q#T in different namespace", type); |
2b110bfc | 864 | permerror (input_location, " from definition of %q+#D", tmpl); |
6e049fcd KL |
865 | } |
866 | ||
867 | /* Check for invalid specialization after instantiation: | |
868 | ||
869 | template <> template <> class C<int>::D<int>; | |
870 | template <> template <class U> class C<int>::D; */ | |
871 | ||
2b110bfc | 872 | for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); |
6e049fcd | 873 | t; t = TREE_CHAIN (t)) |
2b110bfc JM |
874 | { |
875 | tree inst = TREE_VALUE (t); | |
876 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)) | |
877 | { | |
878 | /* We already have a full specialization of this partial | |
879 | instantiation. Reassign it to the new member | |
880 | specialization template. */ | |
881 | spec_entry elt; | |
882 | spec_entry **slot; | |
883 | ||
884 | elt.tmpl = most_general_template (tmpl); | |
885 | elt.args = CLASSTYPE_TI_ARGS (inst); | |
886 | elt.spec = inst; | |
887 | ||
888 | htab_remove_elt (type_specializations, &elt); | |
889 | ||
890 | elt.tmpl = tmpl; | |
891 | elt.args = INNERMOST_TEMPLATE_ARGS (elt.args); | |
892 | ||
893 | slot = (spec_entry **) | |
894 | htab_find_slot (type_specializations, &elt, INSERT); | |
a9429e29 | 895 | *slot = ggc_alloc_spec_entry (); |
2b110bfc JM |
896 | **slot = elt; |
897 | } | |
01628e54 | 898 | else if (COMPLETE_OR_OPEN_TYPE_P (inst)) |
2b110bfc JM |
899 | /* But if we've had an implicit instantiation, that's a |
900 | problem ([temp.expl.spec]/6). */ | |
901 | error ("specialization %qT after instantiation %qT", | |
902 | type, inst); | |
903 | } | |
6e049fcd KL |
904 | |
905 | /* Mark TYPE as a specialization. And as a result, we only | |
906 | have one level of template argument for the innermost | |
907 | class template. */ | |
908 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); | |
909 | CLASSTYPE_TI_ARGS (type) | |
910 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); | |
911 | } | |
912 | } | |
20496fa2 | 913 | else if (processing_specialization) |
9ce387d9 | 914 | { |
90ecaf87 RRC |
915 | /* Someday C++0x may allow for enum template specialization. */ |
916 | if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE | |
917 | && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context)) | |
918 | pedwarn (input_location, OPT_pedantic, "template specialization " | |
919 | "of %qD not allowed by ISO C++", type); | |
920 | else | |
921 | { | |
922 | error ("explicit specialization of non-template %qT", type); | |
923 | return error_mark_node; | |
924 | } | |
9ce387d9 VR |
925 | } |
926 | ||
927 | return type; | |
36a117a5 MM |
928 | } |
929 | ||
ee81147e | 930 | /* Returns nonzero if we can optimize the retrieval of specializations |
c7222c02 MM |
931 | for TMPL, a TEMPLATE_DECL. In particular, for such a template, we |
932 | do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */ | |
933 | ||
c8094d83 | 934 | static inline bool |
c7222c02 MM |
935 | optimize_specialization_lookup_p (tree tmpl) |
936 | { | |
937 | return (DECL_FUNCTION_TEMPLATE_P (tmpl) | |
938 | && DECL_CLASS_SCOPE_P (tmpl) | |
939 | /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template | |
940 | parameter. */ | |
941 | && CLASS_TYPE_P (DECL_CONTEXT (tmpl)) | |
1ca939e5 MM |
942 | /* The optimized lookup depends on the fact that the |
943 | template arguments for the member function template apply | |
944 | purely to the containing class, which is not true if the | |
945 | containing class is an explicit or partial | |
946 | specialization. */ | |
947 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl)) | |
c7222c02 MM |
948 | && !DECL_MEMBER_TEMPLATE_P (tmpl) |
949 | && !DECL_CONV_FN_P (tmpl) | |
950 | /* It is possible to have a template that is not a member | |
951 | template and is not a member of a template class: | |
c8094d83 MS |
952 | |
953 | template <typename T> | |
c7222c02 | 954 | struct S { friend A::f(); }; |
c8094d83 | 955 | |
c7222c02 MM |
956 | Here, the friend function is a template, but the context does |
957 | not have template information. The optimized lookup relies | |
958 | on having ARGS be the template arguments for both the class | |
959 | and the function template. */ | |
960 | && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl))); | |
961 | } | |
962 | ||
75650646 MM |
963 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
964 | specialization is either an instantiation or an explicit | |
965 | specialization) of TMPL for the given template ARGS. If there is | |
966 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
967 | arguments, or a vector of vectors of arguments, in the case of | |
c8094d83 | 968 | templates with more than one level of parameters. |
c7222c02 MM |
969 | |
970 | If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true, | |
971 | then we search for a partial specialization matching ARGS. This | |
972 | parameter is ignored if TMPL is not a class template. */ | |
c8094d83 | 973 | |
75650646 | 974 | static tree |
2b110bfc | 975 | retrieve_specialization (tree tmpl, tree args, hashval_t hash) |
75650646 | 976 | { |
2d8ba2c7 LM |
977 | if (args == error_mark_node) |
978 | return NULL_TREE; | |
979 | ||
50bc768d | 980 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 981 | |
36a117a5 MM |
982 | /* There should be as many levels of arguments as there are |
983 | levels of parameters. */ | |
c8094d83 | 984 | gcc_assert (TMPL_ARGS_DEPTH (args) |
50bc768d | 985 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); |
c8094d83 | 986 | |
c7222c02 MM |
987 | if (optimize_specialization_lookup_p (tmpl)) |
988 | { | |
989 | tree class_template; | |
990 | tree class_specialization; | |
d4e6fecb | 991 | VEC(tree,gc) *methods; |
c7222c02 MM |
992 | tree fns; |
993 | int idx; | |
994 | ||
995 | /* The template arguments actually apply to the containing | |
996 | class. Find the class specialization with those | |
997 | arguments. */ | |
998 | class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl)); | |
c8094d83 | 999 | class_specialization |
2b110bfc | 1000 | = retrieve_specialization (class_template, args, 0); |
c7222c02 MM |
1001 | if (!class_specialization) |
1002 | return NULL_TREE; | |
1003 | /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC | |
1004 | for the specialization. */ | |
1005 | idx = class_method_index_for_fn (class_specialization, tmpl); | |
1006 | if (idx == -1) | |
1007 | return NULL_TREE; | |
1008 | /* Iterate through the methods with the indicated name, looking | |
1009 | for the one that has an instance of TMPL. */ | |
1010 | methods = CLASSTYPE_METHOD_VEC (class_specialization); | |
1011 | for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns)) | |
1012 | { | |
1013 | tree fn = OVL_CURRENT (fns); | |
eb5c3f05 JM |
1014 | if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl |
1015 | /* using-declarations can add base methods to the method vec, | |
1016 | and we don't want those here. */ | |
1017 | && DECL_CONTEXT (fn) == class_specialization) | |
c7222c02 MM |
1018 | return fn; |
1019 | } | |
1020 | return NULL_TREE; | |
1021 | } | |
1022 | else | |
1023 | { | |
2b110bfc JM |
1024 | spec_entry *found; |
1025 | spec_entry elt; | |
1026 | htab_t specializations; | |
1027 | ||
1028 | elt.tmpl = tmpl; | |
1029 | elt.args = args; | |
1030 | elt.spec = NULL_TREE; | |
1031 | ||
1032 | if (DECL_CLASS_TEMPLATE_P (tmpl)) | |
1033 | specializations = type_specializations; | |
c7222c02 | 1034 | else |
2b110bfc | 1035 | specializations = decl_specializations; |
c8094d83 | 1036 | |
2b110bfc JM |
1037 | if (hash == 0) |
1038 | hash = hash_specialization (&elt); | |
1039 | found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash); | |
1040 | if (found) | |
1041 | return found->spec; | |
c7222c02 | 1042 | } |
75650646 MM |
1043 | |
1044 | return NULL_TREE; | |
386b8a85 JM |
1045 | } |
1046 | ||
dc957d14 | 1047 | /* Like retrieve_specialization, but for local declarations. */ |
9188c363 MM |
1048 | |
1049 | static tree | |
3a978d72 | 1050 | retrieve_local_specialization (tree tmpl) |
9188c363 | 1051 | { |
29b0d1fd JM |
1052 | tree spec; |
1053 | ||
1054 | if (local_specializations == NULL) | |
1055 | return NULL_TREE; | |
1056 | ||
1057 | spec = (tree) htab_find_with_hash (local_specializations, tmpl, | |
1058 | htab_hash_pointer (tmpl)); | |
a723baf1 | 1059 | return spec ? TREE_PURPOSE (spec) : NULL_TREE; |
9188c363 MM |
1060 | } |
1061 | ||
838dfd8a | 1062 | /* Returns nonzero iff DECL is a specialization of TMPL. */ |
6757edfe MM |
1063 | |
1064 | int | |
3a978d72 | 1065 | is_specialization_of (tree decl, tree tmpl) |
6757edfe MM |
1066 | { |
1067 | tree t; | |
1068 | ||
1069 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
1070 | { | |
c8094d83 | 1071 | for (t = decl; |
6757edfe MM |
1072 | t != NULL_TREE; |
1073 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
1074 | if (t == tmpl) | |
1075 | return 1; | |
1076 | } | |
c8094d83 | 1077 | else |
6757edfe | 1078 | { |
50bc768d | 1079 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); |
6757edfe MM |
1080 | |
1081 | for (t = TREE_TYPE (decl); | |
1082 | t != NULL_TREE; | |
1083 | t = CLASSTYPE_USE_TEMPLATE (t) | |
1084 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
9edc3913 | 1085 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) |
6757edfe | 1086 | return 1; |
c8094d83 | 1087 | } |
386b8a85 | 1088 | |
6757edfe MM |
1089 | return 0; |
1090 | } | |
75650646 | 1091 | |
d43f603d | 1092 | /* Returns nonzero iff DECL is a specialization of friend declaration |
be93747e | 1093 | FRIEND_DECL according to [temp.friend]. */ |
d43f603d KL |
1094 | |
1095 | bool | |
be93747e | 1096 | is_specialization_of_friend (tree decl, tree friend_decl) |
d43f603d KL |
1097 | { |
1098 | bool need_template = true; | |
1099 | int template_depth; | |
1100 | ||
b939a023 KL |
1101 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
1102 | || TREE_CODE (decl) == TYPE_DECL); | |
d43f603d | 1103 | |
be93747e | 1104 | /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function |
d43f603d KL |
1105 | of a template class, we want to check if DECL is a specialization |
1106 | if this. */ | |
be93747e KG |
1107 | if (TREE_CODE (friend_decl) == FUNCTION_DECL |
1108 | && DECL_TEMPLATE_INFO (friend_decl) | |
1109 | && !DECL_USE_TEMPLATE (friend_decl)) | |
d43f603d | 1110 | { |
b939a023 | 1111 | /* We want a TEMPLATE_DECL for `is_specialization_of'. */ |
be93747e | 1112 | friend_decl = DECL_TI_TEMPLATE (friend_decl); |
d43f603d KL |
1113 | need_template = false; |
1114 | } | |
be93747e KG |
1115 | else if (TREE_CODE (friend_decl) == TEMPLATE_DECL |
1116 | && !PRIMARY_TEMPLATE_P (friend_decl)) | |
b939a023 | 1117 | need_template = false; |
d43f603d KL |
1118 | |
1119 | /* There is nothing to do if this is not a template friend. */ | |
be93747e | 1120 | if (TREE_CODE (friend_decl) != TEMPLATE_DECL) |
b939a023 | 1121 | return false; |
d43f603d | 1122 | |
be93747e | 1123 | if (is_specialization_of (decl, friend_decl)) |
b939a023 | 1124 | return true; |
d43f603d KL |
1125 | |
1126 | /* [temp.friend/6] | |
1127 | A member of a class template may be declared to be a friend of a | |
1128 | non-template class. In this case, the corresponding member of | |
1129 | every specialization of the class template is a friend of the | |
1130 | class granting friendship. | |
c8094d83 | 1131 | |
d43f603d KL |
1132 | For example, given a template friend declaration |
1133 | ||
1134 | template <class T> friend void A<T>::f(); | |
1135 | ||
1136 | the member function below is considered a friend | |
1137 | ||
1138 | template <> struct A<int> { | |
1139 | void f(); | |
1140 | }; | |
1141 | ||
1142 | For this type of template friend, TEMPLATE_DEPTH below will be | |
5c425df5 | 1143 | nonzero. To determine if DECL is a friend of FRIEND, we first |
d43f603d KL |
1144 | check if the enclosing class is a specialization of another. */ |
1145 | ||
725214ac | 1146 | template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl)); |
d43f603d KL |
1147 | if (template_depth |
1148 | && DECL_CLASS_SCOPE_P (decl) | |
c8094d83 | 1149 | && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), |
be93747e | 1150 | CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl)))) |
d43f603d KL |
1151 | { |
1152 | /* Next, we check the members themselves. In order to handle | |
be93747e | 1153 | a few tricky cases, such as when FRIEND_DECL's are |
d43f603d KL |
1154 | |
1155 | template <class T> friend void A<T>::g(T t); | |
1156 | template <class T> template <T t> friend void A<T>::h(); | |
1157 | ||
b939a023 KL |
1158 | and DECL's are |
1159 | ||
1160 | void A<int>::g(int); | |
1161 | template <int> void A<int>::h(); | |
1162 | ||
1163 | we need to figure out ARGS, the template arguments from | |
1164 | the context of DECL. This is required for template substitution | |
1165 | of `T' in the function parameter of `g' and template parameter | |
1166 | of `h' in the above examples. Here ARGS corresponds to `int'. */ | |
d43f603d KL |
1167 | |
1168 | tree context = DECL_CONTEXT (decl); | |
1169 | tree args = NULL_TREE; | |
1170 | int current_depth = 0; | |
b939a023 | 1171 | |
d43f603d KL |
1172 | while (current_depth < template_depth) |
1173 | { | |
1174 | if (CLASSTYPE_TEMPLATE_INFO (context)) | |
1175 | { | |
1176 | if (current_depth == 0) | |
1177 | args = TYPE_TI_ARGS (context); | |
1178 | else | |
1179 | args = add_to_template_args (TYPE_TI_ARGS (context), args); | |
1180 | current_depth++; | |
1181 | } | |
1182 | context = TYPE_CONTEXT (context); | |
1183 | } | |
1184 | ||
1185 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
1186 | { | |
1187 | bool is_template; | |
1188 | tree friend_type; | |
1189 | tree decl_type; | |
1190 | tree friend_args_type; | |
1191 | tree decl_args_type; | |
1192 | ||
be93747e | 1193 | /* Make sure that both DECL and FRIEND_DECL are templates or |
d43f603d KL |
1194 | non-templates. */ |
1195 | is_template = DECL_TEMPLATE_INFO (decl) | |
1196 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); | |
1197 | if (need_template ^ is_template) | |
b939a023 | 1198 | return false; |
d43f603d KL |
1199 | else if (is_template) |
1200 | { | |
da1d7781 | 1201 | /* If both are templates, check template parameter list. */ |
d43f603d | 1202 | tree friend_parms |
be93747e | 1203 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl), |
d43f603d KL |
1204 | args, tf_none); |
1205 | if (!comp_template_parms | |
1206 | (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), | |
1207 | friend_parms)) | |
b939a023 | 1208 | return false; |
d43f603d KL |
1209 | |
1210 | decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); | |
1211 | } | |
1212 | else | |
1213 | decl_type = TREE_TYPE (decl); | |
1214 | ||
be93747e | 1215 | friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args, |
d43f603d KL |
1216 | tf_none, NULL_TREE); |
1217 | if (friend_type == error_mark_node) | |
b939a023 | 1218 | return false; |
d43f603d KL |
1219 | |
1220 | /* Check if return types match. */ | |
1221 | if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) | |
b939a023 | 1222 | return false; |
d43f603d KL |
1223 | |
1224 | /* Check if function parameter types match, ignoring the | |
1225 | `this' parameter. */ | |
1226 | friend_args_type = TYPE_ARG_TYPES (friend_type); | |
1227 | decl_args_type = TYPE_ARG_TYPES (decl_type); | |
be93747e | 1228 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl)) |
d43f603d KL |
1229 | friend_args_type = TREE_CHAIN (friend_args_type); |
1230 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1231 | decl_args_type = TREE_CHAIN (decl_args_type); | |
b939a023 KL |
1232 | |
1233 | return compparms (decl_args_type, friend_args_type); | |
1234 | } | |
1235 | else | |
1236 | { | |
1237 | /* DECL is a TYPE_DECL */ | |
1238 | bool is_template; | |
1239 | tree decl_type = TREE_TYPE (decl); | |
1240 | ||
be93747e | 1241 | /* Make sure that both DECL and FRIEND_DECL are templates or |
b939a023 KL |
1242 | non-templates. */ |
1243 | is_template | |
1244 | = CLASSTYPE_TEMPLATE_INFO (decl_type) | |
1245 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type)); | |
1246 | ||
1247 | if (need_template ^ is_template) | |
1248 | return false; | |
1249 | else if (is_template) | |
1250 | { | |
1251 | tree friend_parms; | |
1252 | /* If both are templates, check the name of the two | |
1253 | TEMPLATE_DECL's first because is_friend didn't. */ | |
1254 | if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type)) | |
be93747e | 1255 | != DECL_NAME (friend_decl)) |
b939a023 KL |
1256 | return false; |
1257 | ||
1258 | /* Now check template parameter list. */ | |
1259 | friend_parms | |
be93747e | 1260 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl), |
b939a023 KL |
1261 | args, tf_none); |
1262 | return comp_template_parms | |
1263 | (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)), | |
1264 | friend_parms); | |
1265 | } | |
1266 | else | |
1267 | return (DECL_NAME (decl) | |
be93747e | 1268 | == DECL_NAME (friend_decl)); |
d43f603d KL |
1269 | } |
1270 | } | |
b939a023 | 1271 | return false; |
d43f603d KL |
1272 | } |
1273 | ||
75650646 | 1274 | /* Register the specialization SPEC as a specialization of TMPL with |
d63d5d0c ILT |
1275 | the indicated ARGS. IS_FRIEND indicates whether the specialization |
1276 | is actually just a friend declaration. Returns SPEC, or an | |
1277 | equivalent prior declaration, if available. */ | |
75650646 | 1278 | |
36a117a5 | 1279 | static tree |
2b110bfc JM |
1280 | register_specialization (tree spec, tree tmpl, tree args, bool is_friend, |
1281 | hashval_t hash) | |
75650646 | 1282 | { |
c7222c02 | 1283 | tree fn; |
2b110bfc JM |
1284 | spec_entry **slot = NULL; |
1285 | spec_entry elt; | |
75650646 | 1286 | |
2b110bfc | 1287 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec)); |
75650646 | 1288 | |
c8094d83 | 1289 | if (TREE_CODE (spec) == FUNCTION_DECL |
36a117a5 MM |
1290 | && uses_template_parms (DECL_TI_ARGS (spec))) |
1291 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
1292 | register it; we want the corresponding TEMPLATE_DECL instead. | |
1293 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
1294 | the more obvious `uses_template_parms (spec)' to avoid problems | |
1295 | with default function arguments. In particular, given | |
1296 | something like this: | |
1297 | ||
0cbd7506 | 1298 | template <class T> void f(T t1, T t = T()) |
36a117a5 MM |
1299 | |
1300 | the default argument expression is not substituted for in an | |
1301 | instantiation unless and until it is actually needed. */ | |
1302 | return spec; | |
28ba38e3 | 1303 | |
2b110bfc JM |
1304 | if (optimize_specialization_lookup_p (tmpl)) |
1305 | /* We don't put these specializations in the hash table, but we might | |
1306 | want to give an error about a mismatch. */ | |
1307 | fn = retrieve_specialization (tmpl, args, 0); | |
1308 | else | |
1309 | { | |
1310 | elt.tmpl = tmpl; | |
1311 | elt.args = args; | |
1312 | elt.spec = spec; | |
1313 | ||
1314 | if (hash == 0) | |
1315 | hash = hash_specialization (&elt); | |
1316 | ||
1317 | slot = (spec_entry **) | |
1318 | htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT); | |
1319 | if (*slot) | |
1320 | fn = (*slot)->spec; | |
1321 | else | |
1322 | fn = NULL_TREE; | |
1323 | } | |
1324 | ||
c7222c02 MM |
1325 | /* We can sometimes try to re-register a specialization that we've |
1326 | already got. In particular, regenerate_decl_from_template calls | |
1327 | duplicate_decls which will update the specialization list. But, | |
1328 | we'll still get called again here anyhow. It's more convenient | |
1329 | to simply allow this than to try to prevent it. */ | |
1330 | if (fn == spec) | |
1331 | return spec; | |
1332 | else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
22e9174f | 1333 | { |
c7222c02 | 1334 | if (DECL_TEMPLATE_INSTANTIATION (fn)) |
22e9174f | 1335 | { |
3146f36f | 1336 | if (DECL_ODR_USED (fn) |
c7222c02 | 1337 | || DECL_EXPLICIT_INSTANTIATION (fn)) |
22e9174f | 1338 | { |
c7222c02 MM |
1339 | error ("specialization of %qD after instantiation", |
1340 | fn); | |
99f4234a | 1341 | return error_mark_node; |
22e9174f | 1342 | } |
c7222c02 | 1343 | else |
b1cc95ce | 1344 | { |
a39041fa | 1345 | tree clone; |
c7222c02 MM |
1346 | /* This situation should occur only if the first |
1347 | specialization is an implicit instantiation, the | |
1348 | second is an explicit specialization, and the | |
1349 | implicit instantiation has not yet been used. That | |
1350 | situation can occur if we have implicitly | |
1351 | instantiated a member function and then specialized | |
1352 | it later. | |
c8094d83 | 1353 | |
c7222c02 MM |
1354 | We can also wind up here if a friend declaration that |
1355 | looked like an instantiation turns out to be a | |
1356 | specialization: | |
c8094d83 | 1357 | |
c7222c02 MM |
1358 | template <class T> void foo(T); |
1359 | class S { friend void foo<>(int) }; | |
c8094d83 MS |
1360 | template <> void foo(int); |
1361 | ||
c7222c02 MM |
1362 | We transform the existing DECL in place so that any |
1363 | pointers to it become pointers to the updated | |
1364 | declaration. | |
1365 | ||
1366 | If there was a definition for the template, but not | |
1367 | for the specialization, we want this to look as if | |
1368 | there were no definition, and vice versa. */ | |
1369 | DECL_INITIAL (fn) = NULL_TREE; | |
d63d5d0c | 1370 | duplicate_decls (spec, fn, is_friend); |
a39041fa | 1371 | /* The call to duplicate_decls will have applied |
3db45ab5 | 1372 | [temp.expl.spec]: |
a39041fa | 1373 | |
3db45ab5 | 1374 | An explicit specialization of a function template |
a39041fa | 1375 | is inline only if it is explicitly declared to be, |
bcf51da2 | 1376 | and independently of whether its function template |
a39041fa MM |
1377 | is. |
1378 | ||
1379 | to the primary function; now copy the inline bits to | |
3db45ab5 | 1380 | the various clones. */ |
a39041fa | 1381 | FOR_EACH_CLONE (clone, fn) |
2968d410 JM |
1382 | { |
1383 | DECL_DECLARED_INLINE_P (clone) | |
1384 | = DECL_DECLARED_INLINE_P (fn); | |
1385 | DECL_SOURCE_LOCATION (clone) | |
1386 | = DECL_SOURCE_LOCATION (fn); | |
1387 | } | |
9c12301f | 1388 | check_specialization_namespace (fn); |
c8094d83 | 1389 | |
b1cc95ce MM |
1390 | return fn; |
1391 | } | |
22e9174f | 1392 | } |
c7222c02 MM |
1393 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) |
1394 | { | |
d63d5d0c | 1395 | if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec)) |
c7222c02 MM |
1396 | /* Dup decl failed, but this is a new definition. Set the |
1397 | line number so any errors match this new | |
1398 | definition. */ | |
1399 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); | |
c8094d83 | 1400 | |
c7222c02 MM |
1401 | return fn; |
1402 | } | |
b1cc95ce | 1403 | } |
62e3e13a JM |
1404 | else if (fn) |
1405 | return duplicate_decls (spec, fn, is_friend); | |
b1cc95ce MM |
1406 | |
1407 | /* A specialization must be declared in the same namespace as the | |
1408 | template it is specializing. */ | |
1409 | if (DECL_TEMPLATE_SPECIALIZATION (spec) | |
1410 | && !check_specialization_namespace (tmpl)) | |
db10c6ec | 1411 | DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl); |
75650646 | 1412 | |
c7222c02 | 1413 | if (!optimize_specialization_lookup_p (tmpl)) |
2b110bfc JM |
1414 | { |
1415 | gcc_assert (tmpl && args && spec); | |
a9429e29 | 1416 | *slot = ggc_alloc_spec_entry (); |
2b110bfc JM |
1417 | **slot = elt; |
1418 | if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec) | |
1419 | && PRIMARY_TEMPLATE_P (tmpl) | |
1420 | && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE) | |
1421 | /* TMPL is a forward declaration of a template function; keep a list | |
1422 | of all specializations in case we need to reassign them to a friend | |
1423 | template later in tsubst_friend_function. */ | |
1424 | DECL_TEMPLATE_INSTANTIATIONS (tmpl) | |
1425 | = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl)); | |
1426 | } | |
36a117a5 MM |
1427 | |
1428 | return spec; | |
1429 | } | |
1430 | ||
2b110bfc JM |
1431 | /* Returns true iff two spec_entry nodes are equivalent. Only compares the |
1432 | TMPL and ARGS members, ignores SPEC. */ | |
1433 | ||
1434 | static int | |
1435 | eq_specializations (const void *p1, const void *p2) | |
1436 | { | |
1437 | const spec_entry *e1 = (const spec_entry *)p1; | |
1438 | const spec_entry *e2 = (const spec_entry *)p2; | |
1439 | ||
1440 | return (e1->tmpl == e2->tmpl | |
1441 | && comp_template_args (e1->args, e2->args)); | |
1442 | } | |
1443 | ||
1444 | /* Returns a hash for a template TMPL and template arguments ARGS. */ | |
1445 | ||
1446 | static hashval_t | |
1447 | hash_tmpl_and_args (tree tmpl, tree args) | |
1448 | { | |
1449 | hashval_t val = DECL_UID (tmpl); | |
1450 | return iterative_hash_template_arg (args, val); | |
1451 | } | |
1452 | ||
1453 | /* Returns a hash for a spec_entry node based on the TMPL and ARGS members, | |
1454 | ignoring SPEC. */ | |
1455 | ||
1456 | static hashval_t | |
1457 | hash_specialization (const void *p) | |
1458 | { | |
1459 | const spec_entry *e = (const spec_entry *)p; | |
1460 | return hash_tmpl_and_args (e->tmpl, e->args); | |
1461 | } | |
1462 | ||
1463 | /* Recursively calculate a hash value for a template argument ARG, for use | |
1464 | in the hash tables of template specializations. */ | |
1465 | ||
c41095db | 1466 | hashval_t |
2b110bfc JM |
1467 | iterative_hash_template_arg (tree arg, hashval_t val) |
1468 | { | |
1469 | unsigned HOST_WIDE_INT i; | |
1470 | enum tree_code code; | |
1471 | char tclass; | |
1472 | ||
1473 | if (arg == NULL_TREE) | |
1474 | return iterative_hash_object (arg, val); | |
1475 | ||
1476 | if (!TYPE_P (arg)) | |
1477 | STRIP_NOPS (arg); | |
1478 | ||
8a5d50bd JM |
1479 | if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT) |
1480 | /* We can get one of these when re-hashing a previous entry in the middle | |
1481 | of substituting into a pack expansion. Just look through it. */ | |
1482 | arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg); | |
1483 | ||
2b110bfc JM |
1484 | code = TREE_CODE (arg); |
1485 | tclass = TREE_CODE_CLASS (code); | |
1486 | ||
1487 | val = iterative_hash_object (code, val); | |
1488 | ||
1489 | switch (code) | |
1490 | { | |
1491 | case ERROR_MARK: | |
1492 | return val; | |
1493 | ||
1494 | case IDENTIFIER_NODE: | |
1495 | return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val); | |
1496 | ||
1497 | case TREE_VEC: | |
1498 | { | |
1499 | int i, len = TREE_VEC_LENGTH (arg); | |
1500 | for (i = 0; i < len; ++i) | |
1501 | val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val); | |
1502 | return val; | |
1503 | } | |
1504 | ||
1505 | case TYPE_PACK_EXPANSION: | |
1506 | case EXPR_PACK_EXPANSION: | |
1507 | return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val); | |
1508 | ||
2b110bfc JM |
1509 | case TYPE_ARGUMENT_PACK: |
1510 | case NONTYPE_ARGUMENT_PACK: | |
1511 | return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val); | |
1512 | ||
1513 | case TREE_LIST: | |
1514 | for (; arg; arg = TREE_CHAIN (arg)) | |
1515 | val = iterative_hash_template_arg (TREE_VALUE (arg), val); | |
1516 | return val; | |
1517 | ||
1518 | case OVERLOAD: | |
1519 | for (; arg; arg = OVL_CHAIN (arg)) | |
1520 | val = iterative_hash_template_arg (OVL_FUNCTION (arg), val); | |
1521 | return val; | |
1522 | ||
1523 | case CONSTRUCTOR: | |
1524 | { | |
1525 | tree field, value; | |
1526 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value) | |
1527 | { | |
1528 | val = iterative_hash_template_arg (field, val); | |
1529 | val = iterative_hash_template_arg (value, val); | |
1530 | } | |
1531 | return val; | |
1532 | } | |
1533 | ||
1534 | case PARM_DECL: | |
649fb657 DS |
1535 | if (!DECL_ARTIFICIAL (arg)) |
1536 | val = iterative_hash_object (DECL_PARM_INDEX (arg), val); | |
2b110bfc JM |
1537 | return iterative_hash_template_arg (TREE_TYPE (arg), val); |
1538 | ||
1539 | case TARGET_EXPR: | |
1540 | return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val); | |
1541 | ||
1542 | case PTRMEM_CST: | |
1543 | val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val); | |
1544 | return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val); | |
1545 | ||
1546 | case TEMPLATE_PARM_INDEX: | |
1547 | val = iterative_hash_template_arg | |
1548 | (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val); | |
1549 | val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val); | |
1550 | return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val); | |
1551 | ||
1552 | case TRAIT_EXPR: | |
1553 | val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val); | |
1554 | val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val); | |
1555 | return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val); | |
1556 | ||
1557 | case BASELINK: | |
1558 | val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)), | |
1559 | val); | |
1560 | return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)), | |
1561 | val); | |
1562 | ||
1563 | case MODOP_EXPR: | |
1564 | val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val); | |
1565 | code = TREE_CODE (TREE_OPERAND (arg, 1)); | |
1566 | val = iterative_hash_object (code, val); | |
1567 | return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val); | |
1568 | ||
4db98b6e JM |
1569 | case ARRAY_TYPE: |
1570 | /* layout_type sets structural equality for arrays of | |
1571 | incomplete type, so we can't rely on the canonical type | |
1572 | for hashing. */ | |
1573 | val = iterative_hash_template_arg (TREE_TYPE (arg), val); | |
1574 | return iterative_hash_template_arg (TYPE_DOMAIN (arg), val); | |
1575 | ||
44b2dc6c JM |
1576 | case LAMBDA_EXPR: |
1577 | /* A lambda can't appear in a template arg, but don't crash on | |
1578 | erroneous input. */ | |
1da2ed5f | 1579 | gcc_assert (seen_error ()); |
44b2dc6c JM |
1580 | return val; |
1581 | ||
ab73eba8 JM |
1582 | case CAST_EXPR: |
1583 | case STATIC_CAST_EXPR: | |
1584 | case REINTERPRET_CAST_EXPR: | |
1585 | case CONST_CAST_EXPR: | |
1586 | case DYNAMIC_CAST_EXPR: | |
1587 | case NEW_EXPR: | |
1588 | val = iterative_hash_template_arg (TREE_TYPE (arg), val); | |
1589 | /* Now hash operands as usual. */ | |
1590 | break; | |
1591 | ||
2b110bfc | 1592 | default: |
ab73eba8 JM |
1593 | break; |
1594 | } | |
2b110bfc | 1595 | |
ab73eba8 JM |
1596 | switch (tclass) |
1597 | { | |
1598 | case tcc_type: | |
1599 | if (TYPE_CANONICAL (arg)) | |
1600 | return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)), | |
1601 | val); | |
1602 | else if (TREE_CODE (arg) == DECLTYPE_TYPE) | |
1603 | return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val); | |
1604 | /* Otherwise just compare the types during lookup. */ | |
1605 | return val; | |
2b110bfc | 1606 | |
ab73eba8 JM |
1607 | case tcc_declaration: |
1608 | case tcc_constant: | |
1609 | return iterative_hash_expr (arg, val); | |
1610 | ||
1611 | default: | |
1612 | gcc_assert (IS_EXPR_CODE_CLASS (tclass)); | |
1613 | { | |
1614 | unsigned n = TREE_OPERAND_LENGTH (arg); | |
1615 | for (i = 0; i < n; ++i) | |
1616 | val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val); | |
1617 | return val; | |
1618 | } | |
2b110bfc JM |
1619 | } |
1620 | gcc_unreachable (); | |
1621 | return 0; | |
1622 | } | |
1623 | ||
36a117a5 | 1624 | /* Unregister the specialization SPEC as a specialization of TMPL. |
b3445994 | 1625 | Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true |
2b110bfc JM |
1626 | if the SPEC was listed as a specialization of TMPL. |
1627 | ||
1628 | Note that SPEC has been ggc_freed, so we can't look inside it. */ | |
36a117a5 | 1629 | |
b3445994 | 1630 | bool |
2b110bfc | 1631 | reregister_specialization (tree spec, tree tinfo, tree new_spec) |
36a117a5 | 1632 | { |
2b110bfc JM |
1633 | spec_entry **slot; |
1634 | spec_entry elt; | |
36a117a5 | 1635 | |
2b110bfc JM |
1636 | elt.tmpl = most_general_template (TI_TEMPLATE (tinfo)); |
1637 | elt.args = TI_ARGS (tinfo); | |
1638 | elt.spec = NULL_TREE; | |
1639 | ||
1640 | slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT); | |
1641 | if (*slot) | |
1642 | { | |
1643 | gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec); | |
1644 | gcc_assert (new_spec != NULL_TREE); | |
1645 | (*slot)->spec = new_spec; | |
1646 | return 1; | |
1647 | } | |
36a117a5 MM |
1648 | |
1649 | return 0; | |
75650646 MM |
1650 | } |
1651 | ||
a723baf1 MM |
1652 | /* Compare an entry in the local specializations hash table P1 (which |
1653 | is really a pointer to a TREE_LIST) with P2 (which is really a | |
1654 | DECL). */ | |
1655 | ||
1656 | static int | |
1657 | eq_local_specializations (const void *p1, const void *p2) | |
1658 | { | |
741ac903 | 1659 | return TREE_VALUE ((const_tree) p1) == (const_tree) p2; |
a723baf1 MM |
1660 | } |
1661 | ||
69f794a7 MM |
1662 | /* Hash P1, an entry in the local specializations table. */ |
1663 | ||
1664 | static hashval_t | |
1665 | hash_local_specialization (const void* p1) | |
1666 | { | |
741ac903 | 1667 | return htab_hash_pointer (TREE_VALUE ((const_tree) p1)); |
69f794a7 MM |
1668 | } |
1669 | ||
6dfbb909 MM |
1670 | /* Like register_specialization, but for local declarations. We are |
1671 | registering SPEC, an instantiation of TMPL. */ | |
9188c363 | 1672 | |
414ea4aa | 1673 | static void |
3a978d72 | 1674 | register_local_specialization (tree spec, tree tmpl) |
9188c363 | 1675 | { |
6dfbb909 MM |
1676 | void **slot; |
1677 | ||
c8094d83 | 1678 | slot = htab_find_slot_with_hash (local_specializations, tmpl, |
69f794a7 | 1679 | htab_hash_pointer (tmpl), INSERT); |
a723baf1 | 1680 | *slot = build_tree_list (spec, tmpl); |
9188c363 MM |
1681 | } |
1682 | ||
9ba7a2f2 MM |
1683 | /* TYPE is a class type. Returns true if TYPE is an explicitly |
1684 | specialized class. */ | |
1685 | ||
1686 | bool | |
1687 | explicit_class_specialization_p (tree type) | |
1688 | { | |
1689 | if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
1690 | return false; | |
1691 | return !uses_template_parms (CLASSTYPE_TI_ARGS (type)); | |
1692 | } | |
1693 | ||
6ba6f70d PB |
1694 | /* Print the list of functions at FNS, going through all the overloads |
1695 | for each element of the list. Alternatively, FNS can not be a | |
1696 | TREE_LIST, in which case it will be printed together with all the | |
1697 | overloads. | |
1698 | ||
1699 | MORE and *STR should respectively be FALSE and NULL when the function | |
1700 | is called from the outside. They are used internally on recursive | |
1701 | calls. print_candidates manages the two parameters and leaves NULL | |
1702 | in *STR when it ends. */ | |
85539630 PC |
1703 | |
1704 | static void | |
6ba6f70d | 1705 | print_candidates_1 (tree fns, bool more, const char **str) |
85539630 | 1706 | { |
6ba6f70d PB |
1707 | tree fn, fn2; |
1708 | char *spaces = NULL; | |
1709 | ||
85539630 | 1710 | for (fn = fns; fn; fn = OVL_NEXT (fn)) |
6ba6f70d PB |
1711 | if (TREE_CODE (fn) == TREE_LIST) |
1712 | { | |
1713 | gcc_assert (!OVL_NEXT (fn) && !is_overloaded_fn (fn)); | |
1714 | for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2)) | |
1715 | print_candidates_1 (TREE_VALUE (fn2), | |
1716 | TREE_CHAIN (fn2) || more, str); | |
1717 | } | |
1718 | else | |
1719 | { | |
1720 | if (!*str) | |
1721 | { | |
1722 | /* Pick the prefix string. */ | |
1723 | if (!more && !OVL_NEXT (fns)) | |
1724 | { | |
1725 | error ("candidate is: %+#D", OVL_CURRENT (fn)); | |
1726 | continue; | |
1727 | } | |
1728 | ||
1729 | *str = _("candidates are:"); | |
1730 | spaces = get_spaces (*str); | |
1731 | } | |
1732 | error ("%s %+#D", *str, OVL_CURRENT (fn)); | |
1733 | *str = spaces ? spaces : *str; | |
1734 | } | |
1735 | ||
1736 | if (!more) | |
85539630 | 1737 | { |
6ba6f70d PB |
1738 | free (spaces); |
1739 | *str = NULL; | |
85539630 PC |
1740 | } |
1741 | } | |
1742 | ||
e1467ff2 MM |
1743 | /* Print the list of candidate FNS in an error message. */ |
1744 | ||
104bf76a | 1745 | void |
3a978d72 | 1746 | print_candidates (tree fns) |
e1467ff2 | 1747 | { |
6ba6f70d PB |
1748 | const char *str = NULL; |
1749 | print_candidates_1 (fns, false, &str); | |
1750 | gcc_assert (str == NULL); | |
e1467ff2 MM |
1751 | } |
1752 | ||
75650646 | 1753 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 1754 | which can be specialized to match the indicated DECL with the |
03017874 MM |
1755 | explicit template args given in TEMPLATE_ID. The DECL may be |
1756 | NULL_TREE if none is available. In that case, the functions in | |
1757 | TEMPLATE_ID are non-members. | |
1758 | ||
838dfd8a | 1759 | If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a |
03017874 MM |
1760 | specialization of a member template. |
1761 | ||
5fe7b654 GB |
1762 | The TEMPLATE_COUNT is the number of references to qualifying |
1763 | template classes that appeared in the name of the function. See | |
1764 | check_explicit_specialization for a more accurate description. | |
1765 | ||
29a1da1c MM |
1766 | TSK indicates what kind of template declaration (if any) is being |
1767 | declared. TSK_TEMPLATE indicates that the declaration given by | |
1768 | DECL, though a FUNCTION_DECL, has template parameters, and is | |
1769 | therefore a template function. | |
1770 | ||
03017874 MM |
1771 | The template args (those explicitly specified and those deduced) |
1772 | are output in a newly created vector *TARGS_OUT. | |
1773 | ||
1774 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 1775 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 1776 | |
e9659ab0 | 1777 | static tree |
c8094d83 | 1778 | determine_specialization (tree template_id, |
0cbd7506 MS |
1779 | tree decl, |
1780 | tree* targs_out, | |
5fe7b654 | 1781 | int need_member_template, |
29a1da1c MM |
1782 | int template_count, |
1783 | tmpl_spec_kind tsk) | |
386b8a85 | 1784 | { |
03017874 MM |
1785 | tree fns; |
1786 | tree targs; | |
1787 | tree explicit_targs; | |
1788 | tree candidates = NULL_TREE; | |
7ca383e6 MM |
1789 | /* A TREE_LIST of templates of which DECL may be a specialization. |
1790 | The TREE_VALUE of each node is a TEMPLATE_DECL. The | |
1791 | corresponding TREE_PURPOSE is the set of template arguments that, | |
1792 | when used to instantiate the template, would produce a function | |
1793 | with the signature of DECL. */ | |
03017874 | 1794 | tree templates = NULL_TREE; |
5fe7b654 GB |
1795 | int header_count; |
1796 | struct cp_binding_level *b; | |
386b8a85 | 1797 | |
e1467ff2 MM |
1798 | *targs_out = NULL_TREE; |
1799 | ||
728da672 | 1800 | if (template_id == error_mark_node || decl == error_mark_node) |
f2e48b67 BK |
1801 | return error_mark_node; |
1802 | ||
1803 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 1804 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 1805 | |
27fafc8d JM |
1806 | if (fns == error_mark_node) |
1807 | return error_mark_node; | |
1808 | ||
c6002625 | 1809 | /* Check for baselinks. */ |
91e490ab | 1810 | if (BASELINK_P (fns)) |
da15dae6 | 1811 | fns = BASELINK_FUNCTIONS (fns); |
386b8a85 | 1812 | |
91e490ab MM |
1813 | if (!is_overloaded_fn (fns)) |
1814 | { | |
0f51ccfc | 1815 | error ("%qD is not a function template", fns); |
91e490ab MM |
1816 | return error_mark_node; |
1817 | } | |
1818 | ||
5fe7b654 GB |
1819 | /* Count the number of template headers specified for this |
1820 | specialization. */ | |
1821 | header_count = 0; | |
c8094d83 | 1822 | for (b = current_binding_level; |
98f99d7f | 1823 | b->kind == sk_template_parms; |
5fe7b654 GB |
1824 | b = b->level_chain) |
1825 | ++header_count; | |
1826 | ||
2c73f9f5 | 1827 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 1828 | { |
9dfce8fd | 1829 | tree fn = OVL_CURRENT (fns); |
03017874 MM |
1830 | |
1831 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
d955f6ea KL |
1832 | { |
1833 | tree decl_arg_types; | |
0da99d4e | 1834 | tree fn_arg_types; |
d955f6ea | 1835 | |
5fe7b654 GB |
1836 | /* In case of explicit specialization, we need to check if |
1837 | the number of template headers appearing in the specialization | |
1838 | is correct. This is usually done in check_explicit_specialization, | |
1839 | but the check done there cannot be exhaustive when specializing | |
1840 | member functions. Consider the following code: | |
1841 | ||
1842 | template <> void A<int>::f(int); | |
1843 | template <> template <> void A<int>::f(int); | |
1844 | ||
1845 | Assuming that A<int> is not itself an explicit specialization | |
1846 | already, the first line specializes "f" which is a non-template | |
1847 | member function, whilst the second line specializes "f" which | |
1848 | is a template member function. So both lines are syntactically | |
1849 | correct, and check_explicit_specialization does not reject | |
1850 | them. | |
c8094d83 | 1851 | |
5fe7b654 GB |
1852 | Here, we can do better, as we are matching the specialization |
1853 | against the declarations. We count the number of template | |
1854 | headers, and we check if they match TEMPLATE_COUNT + 1 | |
1855 | (TEMPLATE_COUNT is the number of qualifying template classes, | |
1856 | plus there must be another header for the member template | |
1857 | itself). | |
c8094d83 | 1858 | |
5fe7b654 GB |
1859 | Notice that if header_count is zero, this is not a |
1860 | specialization but rather a template instantiation, so there | |
1861 | is no check we can perform here. */ | |
1862 | if (header_count && header_count != template_count + 1) | |
1863 | continue; | |
1864 | ||
98f99d7f MM |
1865 | /* Check that the number of template arguments at the |
1866 | innermost level for DECL is the same as for FN. */ | |
1867 | if (current_binding_level->kind == sk_template_parms | |
1868 | && !current_binding_level->explicit_spec_p | |
1869 | && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn)) | |
3db45ab5 | 1870 | != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS |
916b63c3 | 1871 | (current_template_parms)))) |
98f99d7f | 1872 | continue; |
c8094d83 | 1873 | |
ba139ba8 MM |
1874 | /* DECL might be a specialization of FN. */ |
1875 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1876 | fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
1877 | ||
1878 | /* For a non-static member function, we need to make sure | |
1879 | that the const qualification is the same. Since | |
1880 | get_bindings does not try to merge the "this" parameter, | |
1881 | we must do the comparison explicitly. */ | |
1882 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) | |
1883 | && !same_type_p (TREE_VALUE (fn_arg_types), | |
1884 | TREE_VALUE (decl_arg_types))) | |
1885 | continue; | |
1886 | ||
1887 | /* Skip the "this" parameter and, for constructors of | |
1888 | classes with virtual bases, the VTT parameter. A | |
1889 | full specialization of a constructor will have a VTT | |
1890 | parameter, but a template never will. */ | |
1891 | decl_arg_types | |
1892 | = skip_artificial_parms_for (decl, decl_arg_types); | |
1893 | fn_arg_types | |
1894 | = skip_artificial_parms_for (fn, fn_arg_types); | |
1895 | ||
1896 | /* Check that the number of function parameters matches. | |
1897 | For example, | |
1898 | template <class T> void f(int i = 0); | |
1899 | template <> void f<int>(); | |
1900 | The specialization f<int> is invalid but is not caught | |
1901 | by get_bindings below. */ | |
1902 | if (list_length (fn_arg_types) != list_length (decl_arg_types)) | |
1903 | continue; | |
1904 | ||
29a1da1c MM |
1905 | /* Function templates cannot be specializations; there are |
1906 | no partial specializations of functions. Therefore, if | |
1907 | the type of DECL does not match FN, there is no | |
1908 | match. */ | |
54062fc0 PC |
1909 | if (tsk == tsk_template) |
1910 | { | |
1911 | if (compparms (fn_arg_types, decl_arg_types)) | |
1912 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
1913 | continue; | |
1914 | } | |
29a1da1c | 1915 | |
d955f6ea KL |
1916 | /* See whether this function might be a specialization of this |
1917 | template. */ | |
a34d3336 | 1918 | targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true); |
d955f6ea KL |
1919 | |
1920 | if (!targs) | |
1921 | /* We cannot deduce template arguments that when used to | |
1922 | specialize TMPL will produce DECL. */ | |
1923 | continue; | |
1924 | ||
1925 | /* Save this template, and the arguments deduced. */ | |
1926 | templates = tree_cons (targs, fn, templates); | |
1927 | } | |
03017874 MM |
1928 | else if (need_member_template) |
1929 | /* FN is an ordinary member function, and we need a | |
1930 | specialization of a member template. */ | |
d955f6ea | 1931 | ; |
03017874 MM |
1932 | else if (TREE_CODE (fn) != FUNCTION_DECL) |
1933 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1934 | cases. */ | |
d955f6ea | 1935 | ; |
03017874 MM |
1936 | else if (!DECL_FUNCTION_MEMBER_P (fn)) |
1937 | /* This is just an ordinary non-member function. Nothing can | |
1938 | be a specialization of that. */ | |
d955f6ea | 1939 | ; |
3b82c249 KL |
1940 | else if (DECL_ARTIFICIAL (fn)) |
1941 | /* Cannot specialize functions that are created implicitly. */ | |
d955f6ea | 1942 | ; |
75650646 | 1943 | else |
03017874 MM |
1944 | { |
1945 | tree decl_arg_types; | |
386b8a85 | 1946 | |
03017874 MM |
1947 | /* This is an ordinary member function. However, since |
1948 | we're here, we can assume it's enclosing class is a | |
1949 | template class. For example, | |
c8094d83 | 1950 | |
03017874 MM |
1951 | template <typename T> struct S { void f(); }; |
1952 | template <> void S<int>::f() {} | |
1953 | ||
1954 | Here, S<int>::f is a non-template, but S<int> is a | |
1955 | template class. If FN has the same type as DECL, we | |
1956 | might be in business. */ | |
f5d47abd KL |
1957 | |
1958 | if (!DECL_TEMPLATE_INFO (fn)) | |
1959 | /* Its enclosing class is an explicit specialization | |
1960 | of a template class. This is not a candidate. */ | |
1961 | continue; | |
1962 | ||
03017874 MM |
1963 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
1964 | TREE_TYPE (TREE_TYPE (fn)))) | |
1965 | /* The return types differ. */ | |
1966 | continue; | |
1967 | ||
1968 | /* Adjust the type of DECL in case FN is a static member. */ | |
1969 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
c8094d83 | 1970 | if (DECL_STATIC_FUNCTION_P (fn) |
03017874 MM |
1971 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1972 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1973 | ||
c8094d83 | 1974 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), |
03017874 MM |
1975 | decl_arg_types)) |
1976 | /* They match! */ | |
1977 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
03017874 | 1978 | } |
386b8a85 | 1979 | } |
03017874 | 1980 | |
bf8f3f93 | 1981 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1982 | { |
03017874 | 1983 | /* We have: |
c8094d83 | 1984 | |
03017874 MM |
1985 | [temp.expl.spec] |
1986 | ||
1987 | It is possible for a specialization with a given function | |
1988 | signature to be instantiated from more than one function | |
1989 | template. In such cases, explicit specification of the | |
1990 | template arguments must be used to uniquely identify the | |
1991 | function template specialization being specialized. | |
1992 | ||
1993 | Note that here, there's no suggestion that we're supposed to | |
1994 | determine which of the candidate templates is most | |
1995 | specialized. However, we, also have: | |
1996 | ||
1997 | [temp.func.order] | |
1998 | ||
1999 | Partial ordering of overloaded function template | |
2000 | declarations is used in the following contexts to select | |
2001 | the function template to which a function template | |
c8094d83 | 2002 | specialization refers: |
03017874 | 2003 | |
0cbd7506 | 2004 | -- when an explicit specialization refers to a function |
c8094d83 | 2005 | template. |
03017874 MM |
2006 | |
2007 | So, we do use the partial ordering rules, at least for now. | |
0e339752 | 2008 | This extension can only serve to make invalid programs valid, |
03017874 MM |
2009 | so it's safe. And, there is strong anecdotal evidence that |
2010 | the committee intended the partial ordering rules to apply; | |
3b426391 | 2011 | the EDG front end has that behavior, and John Spicer claims |
03017874 MM |
2012 | that the committee simply forgot to delete the wording in |
2013 | [temp.expl.spec]. */ | |
7ca383e6 MM |
2014 | tree tmpl = most_specialized_instantiation (templates); |
2015 | if (tmpl != error_mark_node) | |
2016 | { | |
2017 | templates = tmpl; | |
2018 | TREE_CHAIN (templates) = NULL_TREE; | |
2019 | } | |
e1467ff2 MM |
2020 | } |
2021 | ||
03017874 | 2022 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 2023 | { |
dee15844 JM |
2024 | error ("template-id %qD for %q+D does not match any template " |
2025 | "declaration", template_id, decl); | |
3474a3b7 JM |
2026 | if (header_count && header_count != template_count + 1) |
2027 | inform (input_location, "saw %d %<template<>%>, need %d for " | |
2028 | "specializing a member function template", | |
2029 | header_count, template_count + 1); | |
03017874 | 2030 | return error_mark_node; |
386b8a85 | 2031 | } |
03017874 | 2032 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
2033 | || (candidates && TREE_CHAIN (candidates)) |
2034 | || (templates && candidates)) | |
386b8a85 | 2035 | { |
dee15844 JM |
2036 | error ("ambiguous template specialization %qD for %q+D", |
2037 | template_id, decl); | |
3cb9d1d5 | 2038 | candidates = chainon (candidates, templates); |
bf8f3f93 | 2039 | print_candidates (candidates); |
03017874 | 2040 | return error_mark_node; |
386b8a85 JM |
2041 | } |
2042 | ||
c6002625 | 2043 | /* We have one, and exactly one, match. */ |
03017874 MM |
2044 | if (candidates) |
2045 | { | |
29a1da1c | 2046 | tree fn = TREE_VALUE (candidates); |
62e3e13a JM |
2047 | *targs_out = copy_node (DECL_TI_ARGS (fn)); |
2048 | /* DECL is a re-declaration or partial instantiation of a template | |
2049 | function. */ | |
29a1da1c MM |
2050 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
2051 | return fn; | |
03017874 MM |
2052 | /* It was a specialization of an ordinary member function in a |
2053 | template class. */ | |
29a1da1c | 2054 | return DECL_TI_TEMPLATE (fn); |
03017874 MM |
2055 | } |
2056 | ||
2057 | /* It was a specialization of a template. */ | |
17aec3eb | 2058 | targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); |
03017874 MM |
2059 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) |
2060 | { | |
2061 | *targs_out = copy_node (targs); | |
c8094d83 | 2062 | SET_TMPL_ARGS_LEVEL (*targs_out, |
03017874 MM |
2063 | TMPL_ARGS_DEPTH (*targs_out), |
2064 | TREE_PURPOSE (templates)); | |
2065 | } | |
2066 | else | |
2067 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 2068 | return TREE_VALUE (templates); |
8d08fdba | 2069 | } |
8afa707f MM |
2070 | |
2071 | /* Returns a chain of parameter types, exactly like the SPEC_TYPES, | |
2072 | but with the default argument values filled in from those in the | |
2073 | TMPL_TYPES. */ | |
c8094d83 | 2074 | |
8afa707f | 2075 | static tree |
3a978d72 NN |
2076 | copy_default_args_to_explicit_spec_1 (tree spec_types, |
2077 | tree tmpl_types) | |
8afa707f MM |
2078 | { |
2079 | tree new_spec_types; | |
2080 | ||
2081 | if (!spec_types) | |
2082 | return NULL_TREE; | |
2083 | ||
2084 | if (spec_types == void_list_node) | |
2085 | return void_list_node; | |
2086 | ||
2087 | /* Substitute into the rest of the list. */ | |
2088 | new_spec_types = | |
2089 | copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), | |
2090 | TREE_CHAIN (tmpl_types)); | |
c8094d83 | 2091 | |
8afa707f MM |
2092 | /* Add the default argument for this parameter. */ |
2093 | return hash_tree_cons (TREE_PURPOSE (tmpl_types), | |
2094 | TREE_VALUE (spec_types), | |
2095 | new_spec_types); | |
2096 | } | |
2097 | ||
2098 | /* DECL is an explicit specialization. Replicate default arguments | |
2099 | from the template it specializes. (That way, code like: | |
2100 | ||
2101 | template <class T> void f(T = 3); | |
2102 | template <> void f(double); | |
c8094d83 | 2103 | void g () { f (); } |
8afa707f MM |
2104 | |
2105 | works, as required.) An alternative approach would be to look up | |
2106 | the correct default arguments at the call-site, but this approach | |
2107 | is consistent with how implicit instantiations are handled. */ | |
2108 | ||
2109 | static void | |
3a978d72 | 2110 | copy_default_args_to_explicit_spec (tree decl) |
8afa707f MM |
2111 | { |
2112 | tree tmpl; | |
2113 | tree spec_types; | |
2114 | tree tmpl_types; | |
2115 | tree new_spec_types; | |
2116 | tree old_type; | |
2117 | tree new_type; | |
2118 | tree t; | |
08c2df0f NS |
2119 | tree object_type = NULL_TREE; |
2120 | tree in_charge = NULL_TREE; | |
e0fff4b3 | 2121 | tree vtt = NULL_TREE; |
8afa707f MM |
2122 | |
2123 | /* See if there's anything we need to do. */ | |
2124 | tmpl = DECL_TI_TEMPLATE (decl); | |
2125 | tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); | |
2126 | for (t = tmpl_types; t; t = TREE_CHAIN (t)) | |
2127 | if (TREE_PURPOSE (t)) | |
2128 | break; | |
2129 | if (!t) | |
2130 | return; | |
2131 | ||
2132 | old_type = TREE_TYPE (decl); | |
2133 | spec_types = TYPE_ARG_TYPES (old_type); | |
c8094d83 | 2134 | |
8afa707f MM |
2135 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
2136 | { | |
08c2df0f | 2137 | /* Remove the this pointer, but remember the object's type for |
0cbd7506 | 2138 | CV quals. */ |
08c2df0f | 2139 | object_type = TREE_TYPE (TREE_VALUE (spec_types)); |
8afa707f MM |
2140 | spec_types = TREE_CHAIN (spec_types); |
2141 | tmpl_types = TREE_CHAIN (tmpl_types); | |
c8094d83 | 2142 | |
8afa707f | 2143 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) |
0cbd7506 MS |
2144 | { |
2145 | /* DECL may contain more parameters than TMPL due to the extra | |
2146 | in-charge parameter in constructors and destructors. */ | |
2147 | in_charge = spec_types; | |
08c2df0f NS |
2148 | spec_types = TREE_CHAIN (spec_types); |
2149 | } | |
e0fff4b3 JM |
2150 | if (DECL_HAS_VTT_PARM_P (decl)) |
2151 | { | |
2152 | vtt = spec_types; | |
2153 | spec_types = TREE_CHAIN (spec_types); | |
2154 | } | |
8afa707f MM |
2155 | } |
2156 | ||
2157 | /* Compute the merged default arguments. */ | |
c8094d83 | 2158 | new_spec_types = |
8afa707f MM |
2159 | copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); |
2160 | ||
08c2df0f NS |
2161 | /* Compute the new FUNCTION_TYPE. */ |
2162 | if (object_type) | |
8afa707f | 2163 | { |
e0fff4b3 | 2164 | if (vtt) |
0cbd7506 MS |
2165 | new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), |
2166 | TREE_VALUE (vtt), | |
2167 | new_spec_types); | |
e0fff4b3 | 2168 | |
08c2df0f | 2169 | if (in_charge) |
0cbd7506 MS |
2170 | /* Put the in-charge parameter back. */ |
2171 | new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), | |
2172 | TREE_VALUE (in_charge), | |
2173 | new_spec_types); | |
08c2df0f | 2174 | |
43dc123f MM |
2175 | new_type = build_method_type_directly (object_type, |
2176 | TREE_TYPE (old_type), | |
2177 | new_spec_types); | |
8afa707f | 2178 | } |
8afa707f MM |
2179 | else |
2180 | new_type = build_function_type (TREE_TYPE (old_type), | |
08c2df0f | 2181 | new_spec_types); |
e9525111 MM |
2182 | new_type = cp_build_type_attribute_variant (new_type, |
2183 | TYPE_ATTRIBUTES (old_type)); | |
8afa707f MM |
2184 | new_type = build_exception_variant (new_type, |
2185 | TYPE_RAISES_EXCEPTIONS (old_type)); | |
2186 | TREE_TYPE (decl) = new_type; | |
2187 | } | |
2188 | ||
386b8a85 | 2189 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
2190 | DECLARATOR, and in DECL, is a specialization of a function |
2191 | template. We may also discover that the declaration is an explicit | |
2192 | instantiation at this point. | |
2193 | ||
e1467ff2 | 2194 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
2195 | instead if all goes well. Issues an error message if something is |
2196 | amiss. Returns error_mark_node if the error is not easily | |
2197 | recoverable. | |
c8094d83 MS |
2198 | |
2199 | FLAGS is a bitmask consisting of the following flags: | |
75650646 | 2200 | |
75650646 MM |
2201 | 2: The function has a definition. |
2202 | 4: The function is a friend. | |
75650646 MM |
2203 | |
2204 | The TEMPLATE_COUNT is the number of references to qualifying | |
2205 | template classes that appeared in the name of the function. For | |
2206 | example, in | |
2207 | ||
2208 | template <class T> struct S { void f(); }; | |
2209 | void S<int>::f(); | |
c8094d83 | 2210 | |
75650646 MM |
2211 | the TEMPLATE_COUNT would be 1. However, explicitly specialized |
2212 | classes are not counted in the TEMPLATE_COUNT, so that in | |
2213 | ||
2214 | template <class T> struct S {}; | |
2215 | template <> struct S<int> { void f(); } | |
36a117a5 | 2216 | template <> void S<int>::f(); |
75650646 MM |
2217 | |
2218 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
0e339752 | 2219 | invalid; there should be no template <>.) |
75650646 MM |
2220 | |
2221 | If the function is a specialization, it is marked as such via | |
2222 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
c8094d83 | 2223 | is set up correctly, and it is added to the list of specializations |
75650646 | 2224 | for that template. */ |
386b8a85 | 2225 | |
e1467ff2 | 2226 | tree |
c8094d83 | 2227 | check_explicit_specialization (tree declarator, |
0cbd7506 MS |
2228 | tree decl, |
2229 | int template_count, | |
2230 | int flags) | |
386b8a85 | 2231 | { |
75650646 MM |
2232 | int have_def = flags & 2; |
2233 | int is_friend = flags & 4; | |
2234 | int specialization = 0; | |
e1467ff2 | 2235 | int explicit_instantiation = 0; |
fd4de5ff | 2236 | int member_specialization = 0; |
75650646 MM |
2237 | tree ctype = DECL_CLASS_CONTEXT (decl); |
2238 | tree dname = DECL_NAME (decl); | |
74b846e0 | 2239 | tmpl_spec_kind tsk; |
386b8a85 | 2240 | |
f65b7de3 GB |
2241 | if (is_friend) |
2242 | { | |
2243 | if (!processing_specialization) | |
2244 | tsk = tsk_none; | |
2245 | else | |
2246 | tsk = tsk_excessive_parms; | |
2247 | } | |
2248 | else | |
2249 | tsk = current_tmpl_spec_kind (template_count); | |
75650646 | 2250 | |
74b846e0 MM |
2251 | switch (tsk) |
2252 | { | |
2253 | case tsk_none: | |
c8094d83 | 2254 | if (processing_specialization) |
75650646 | 2255 | { |
fd4de5ff MM |
2256 | specialization = 1; |
2257 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 2258 | } |
74b846e0 | 2259 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
386b8a85 | 2260 | { |
74b846e0 MM |
2261 | if (is_friend) |
2262 | /* This could be something like: | |
75650646 | 2263 | |
74b846e0 MM |
2264 | template <class T> void f(T); |
2265 | class S { friend void f<>(int); } */ | |
2266 | specialization = 1; | |
2267 | else | |
2268 | { | |
2269 | /* This case handles bogus declarations like template <> | |
2270 | template <class T> void f<int>(); */ | |
2271 | ||
0f51ccfc | 2272 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 2273 | declarator); |
74b846e0 MM |
2274 | return decl; |
2275 | } | |
2276 | } | |
2277 | break; | |
2278 | ||
2279 | case tsk_invalid_member_spec: | |
2280 | /* The error has already been reported in | |
2281 | check_specialization_scope. */ | |
2282 | return error_mark_node; | |
2283 | ||
2284 | case tsk_invalid_expl_inst: | |
33bd39a2 | 2285 | error ("template parameter list used in explicit instantiation"); |
74b846e0 MM |
2286 | |
2287 | /* Fall through. */ | |
2288 | ||
2289 | case tsk_expl_inst: | |
fd4de5ff | 2290 | if (have_def) |
33bd39a2 | 2291 | error ("definition provided for explicit instantiation"); |
c8094d83 | 2292 | |
fd4de5ff | 2293 | explicit_instantiation = 1; |
74b846e0 | 2294 | break; |
fd4de5ff | 2295 | |
74b846e0 | 2296 | case tsk_excessive_parms: |
f03adc6b GB |
2297 | case tsk_insufficient_parms: |
2298 | if (tsk == tsk_excessive_parms) | |
0cbd7506 | 2299 | error ("too many template parameter lists in declaration of %qD", |
f03adc6b GB |
2300 | decl); |
2301 | else if (template_header_count) | |
0f51ccfc | 2302 | error("too few template parameter lists in declaration of %qD", decl); |
f03adc6b | 2303 | else |
0f51ccfc | 2304 | error("explicit specialization of %qD must be introduced by " |
9e637a26 | 2305 | "%<template <>%>", decl); |
75650646 | 2306 | |
74b846e0 MM |
2307 | /* Fall through. */ |
2308 | case tsk_expl_spec: | |
2309 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
2310 | if (ctype) | |
2311 | member_specialization = 1; | |
2312 | else | |
2313 | specialization = 1; | |
2314 | break; | |
74b846e0 MM |
2315 | |
2316 | case tsk_template: | |
2317 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
c6f2ed0d | 2318 | { |
fd4de5ff MM |
2319 | /* This case handles bogus declarations like template <> |
2320 | template <class T> void f<int>(); */ | |
75650646 | 2321 | |
655dc6ee | 2322 | if (uses_template_parms (declarator)) |
0f51ccfc | 2323 | error ("function template partial specialization %qD " |
e1e93ad8 | 2324 | "is not allowed", declarator); |
655dc6ee | 2325 | else |
0f51ccfc | 2326 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 2327 | declarator); |
fd4de5ff | 2328 | return decl; |
386b8a85 | 2329 | } |
74b846e0 MM |
2330 | |
2331 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
2332 | /* This is a specialization of a member template, without | |
2333 | specialization the containing class. Something like: | |
2334 | ||
2335 | template <class T> struct S { | |
c8094d83 | 2336 | template <class U> void f (U); |
0cbd7506 | 2337 | }; |
74b846e0 | 2338 | template <> template <class U> void S<int>::f(U) {} |
c8094d83 | 2339 | |
74b846e0 MM |
2340 | That's a specialization -- but of the entire template. */ |
2341 | specialization = 1; | |
2342 | break; | |
2343 | ||
2344 | default: | |
315fb5db | 2345 | gcc_unreachable (); |
75650646 | 2346 | } |
386b8a85 | 2347 | |
670960ac JM |
2348 | if (specialization || member_specialization) |
2349 | { | |
2350 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
2351 | for (; t; t = TREE_CHAIN (t)) | |
2352 | if (TREE_PURPOSE (t)) | |
2353 | { | |
cbe5f3b3 MLI |
2354 | permerror (input_location, |
2355 | "default argument specified in explicit specialization"); | |
670960ac JM |
2356 | break; |
2357 | } | |
2358 | } | |
2359 | ||
e1467ff2 | 2360 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
2361 | { |
2362 | tree tmpl = NULL_TREE; | |
2363 | tree targs = NULL_TREE; | |
75650646 MM |
2364 | |
2365 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
2366 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
2367 | { | |
2368 | tree fns; | |
2369 | ||
50bc768d | 2370 | gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE); |
2f54a1db | 2371 | if (ctype) |
386b8a85 | 2372 | fns = dname; |
2f54a1db GB |
2373 | else |
2374 | { | |
2375 | /* If there is no class context, the explicit instantiation | |
0cbd7506 | 2376 | must be at namespace scope. */ |
50bc768d | 2377 | gcc_assert (DECL_NAMESPACE_SCOPE_P (decl)); |
2f54a1db GB |
2378 | |
2379 | /* Find the namespace binding, using the declaration | |
0cbd7506 | 2380 | context. */ |
ddf74938 | 2381 | fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname, |
664a90c0 | 2382 | false, true); |
97dc8e5b | 2383 | if (fns == error_mark_node || !is_overloaded_fn (fns)) |
4230cec2 NS |
2384 | { |
2385 | error ("%qD is not a template function", dname); | |
2386 | fns = error_mark_node; | |
2387 | } | |
664a90c0 JM |
2388 | else |
2389 | { | |
2390 | tree fn = OVL_CURRENT (fns); | |
ddf74938 | 2391 | if (!is_associated_namespace (CP_DECL_CONTEXT (decl), |
08d295c5 JM |
2392 | CP_DECL_CONTEXT (fn))) |
2393 | error ("%qD is not declared in %qD", | |
2394 | decl, current_namespace); | |
664a90c0 | 2395 | } |
2f54a1db | 2396 | } |
386b8a85 | 2397 | |
2f54a1db | 2398 | declarator = lookup_template_function (fns, NULL_TREE); |
386b8a85 JM |
2399 | } |
2400 | ||
f2e48b67 BK |
2401 | if (declarator == error_mark_node) |
2402 | return error_mark_node; | |
2403 | ||
75650646 MM |
2404 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
2405 | { | |
8ec2ac34 | 2406 | if (!explicit_instantiation) |
0e339752 | 2407 | /* A specialization in class scope. This is invalid, |
6c30752f MM |
2408 | but the error will already have been flagged by |
2409 | check_specialization_scope. */ | |
2410 | return error_mark_node; | |
8ec2ac34 | 2411 | else |
b370501f | 2412 | { |
0e339752 | 2413 | /* It's not valid to write an explicit instantiation in |
b370501f | 2414 | class scope, e.g.: |
8ec2ac34 | 2415 | |
0cbd7506 | 2416 | class C { template void f(); } |
8ec2ac34 | 2417 | |
b370501f KG |
2418 | This case is caught by the parser. However, on |
2419 | something like: | |
c8094d83 | 2420 | |
b370501f | 2421 | template class C { void f(); }; |
8ec2ac34 | 2422 | |
0e339752 | 2423 | (which is invalid) we can get here. The error will be |
b370501f KG |
2424 | issued later. */ |
2425 | ; | |
2426 | } | |
8ec2ac34 | 2427 | |
e1467ff2 | 2428 | return decl; |
75650646 | 2429 | } |
c8094d83 | 2430 | else if (ctype != NULL_TREE |
75650646 MM |
2431 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == |
2432 | IDENTIFIER_NODE)) | |
386b8a85 | 2433 | { |
75650646 MM |
2434 | /* Find the list of functions in ctype that have the same |
2435 | name as the declared function. */ | |
2436 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
2437 | tree fns = NULL_TREE; |
2438 | int idx; | |
2439 | ||
8ba658ee | 2440 | if (constructor_name_p (name, ctype)) |
386b8a85 | 2441 | { |
75650646 | 2442 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
c8094d83 | 2443 | |
0fcedd9c | 2444 | if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype) |
9f4faeae | 2445 | : !CLASSTYPE_DESTRUCTORS (ctype)) |
75650646 MM |
2446 | { |
2447 | /* From [temp.expl.spec]: | |
c8094d83 | 2448 | |
75650646 MM |
2449 | If such an explicit specialization for the member |
2450 | of a class template names an implicitly-declared | |
2451 | special member function (clause _special_), the | |
c8094d83 | 2452 | program is ill-formed. |
e1467ff2 MM |
2453 | |
2454 | Similar language is found in [temp.explicit]. */ | |
33bd39a2 | 2455 | error ("specialization of implicitly-declared special member function"); |
03017874 | 2456 | return error_mark_node; |
75650646 | 2457 | } |
386b8a85 | 2458 | |
42da2fd8 | 2459 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 2460 | } |
42da2fd8 | 2461 | |
421844e7 | 2462 | if (!DECL_CONV_FN_P (decl)) |
03017874 MM |
2463 | { |
2464 | idx = lookup_fnfields_1 (ctype, name); | |
2465 | if (idx >= 0) | |
aaaa46d2 | 2466 | fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx); |
03017874 MM |
2467 | } |
2468 | else | |
2469 | { | |
d4e6fecb | 2470 | VEC(tree,gc) *methods; |
aaaa46d2 | 2471 | tree ovl; |
03017874 MM |
2472 | |
2473 | /* For a type-conversion operator, we cannot do a | |
2474 | name-based lookup. We might be looking for `operator | |
2475 | int' which will be a specialization of `operator T'. | |
2476 | So, we find *all* the conversion operators, and then | |
2477 | select from them. */ | |
2478 | fns = NULL_TREE; | |
2479 | ||
2480 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
2481 | if (methods) | |
5dd236e2 | 2482 | for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; |
9ba5ff0f | 2483 | VEC_iterate (tree, methods, idx, ovl); |
aaaa46d2 | 2484 | ++idx) |
03017874 | 2485 | { |
aaaa46d2 | 2486 | if (!DECL_CONV_FN_P (OVL_CURRENT (ovl))) |
03017874 MM |
2487 | /* There are no more conversion functions. */ |
2488 | break; | |
2489 | ||
2490 | /* Glue all these conversion functions together | |
2491 | with those we already have. */ | |
2492 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
2493 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
2494 | } | |
2495 | } | |
c8094d83 MS |
2496 | |
2497 | if (fns == NULL_TREE) | |
386b8a85 | 2498 | { |
0f51ccfc | 2499 | error ("no member function %qD declared in %qT", name, ctype); |
03017874 | 2500 | return error_mark_node; |
386b8a85 JM |
2501 | } |
2502 | else | |
2503 | TREE_OPERAND (declarator, 0) = fns; | |
2504 | } | |
c8094d83 | 2505 | |
e1467ff2 MM |
2506 | /* Figure out what exactly is being specialized at this point. |
2507 | Note that for an explicit instantiation, even one for a | |
38e01259 | 2508 | member function, we cannot tell apriori whether the |
e1467ff2 | 2509 | instantiation is for a member template, or just a member |
36a117a5 MM |
2510 | function of a template class. Even if a member template is |
2511 | being instantiated, the member template arguments may be | |
2512 | elided if they can be deduced from the rest of the | |
2513 | declaration. */ | |
e1467ff2 | 2514 | tmpl = determine_specialization (declarator, decl, |
c8094d83 | 2515 | &targs, |
5fe7b654 | 2516 | member_specialization, |
29a1da1c MM |
2517 | template_count, |
2518 | tsk); | |
c8094d83 | 2519 | |
03017874 MM |
2520 | if (!tmpl || tmpl == error_mark_node) |
2521 | /* We couldn't figure out what this declaration was | |
2522 | specializing. */ | |
2523 | return error_mark_node; | |
2524 | else | |
386b8a85 | 2525 | { |
25aab5d0 | 2526 | tree gen_tmpl = most_general_template (tmpl); |
36a117a5 | 2527 | |
e1467ff2 MM |
2528 | if (explicit_instantiation) |
2529 | { | |
03d0f4af | 2530 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
c8094d83 | 2531 | is done by do_decl_instantiation later. */ |
25aab5d0 MM |
2532 | |
2533 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
2534 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
2535 | ||
2536 | if (arg_depth > parm_depth) | |
2537 | { | |
2538 | /* If TMPL is not the most general template (for | |
2539 | example, if TMPL is a friend template that is | |
2540 | injected into namespace scope), then there will | |
dc957d14 | 2541 | be too many levels of TARGS. Remove some of them |
25aab5d0 MM |
2542 | here. */ |
2543 | int i; | |
2544 | tree new_targs; | |
2545 | ||
f31c0a32 | 2546 | new_targs = make_tree_vec (parm_depth); |
25aab5d0 MM |
2547 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) |
2548 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
2549 | = TREE_VEC_ELT (targs, i); | |
2550 | targs = new_targs; | |
2551 | } | |
c8094d83 | 2552 | |
3e4a3562 | 2553 | return instantiate_template (tmpl, targs, tf_error); |
e1467ff2 | 2554 | } |
74b846e0 | 2555 | |
6c07f448 KL |
2556 | /* If we thought that the DECL was a member function, but it |
2557 | turns out to be specializing a static member function, | |
4546865e | 2558 | make DECL a static member function as well. */ |
6c07f448 KL |
2559 | if (DECL_STATIC_FUNCTION_P (tmpl) |
2560 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
4546865e | 2561 | revert_static_member_fn (decl); |
6c07f448 | 2562 | |
f9a7ae04 | 2563 | /* If this is a specialization of a member template of a |
29a1da1c MM |
2564 | template class, we want to return the TEMPLATE_DECL, not |
2565 | the specialization of it. */ | |
74b846e0 MM |
2566 | if (tsk == tsk_template) |
2567 | { | |
448545cb | 2568 | tree result = DECL_TEMPLATE_RESULT (tmpl); |
74b846e0 | 2569 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
448545cb | 2570 | DECL_INITIAL (result) = NULL_TREE; |
b190f239 NS |
2571 | if (have_def) |
2572 | { | |
448545cb | 2573 | tree parm; |
f31686a3 | 2574 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
448545cb | 2575 | DECL_SOURCE_LOCATION (result) |
f31686a3 | 2576 | = DECL_SOURCE_LOCATION (decl); |
08167d1c AO |
2577 | /* We want to use the argument list specified in the |
2578 | definition, not in the original declaration. */ | |
448545cb JM |
2579 | DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl); |
2580 | for (parm = DECL_ARGUMENTS (result); parm; | |
910ad8de | 2581 | parm = DECL_CHAIN (parm)) |
448545cb | 2582 | DECL_CONTEXT (parm) = result; |
b190f239 | 2583 | } |
62e3e13a | 2584 | return register_specialization (tmpl, gen_tmpl, targs, |
2b110bfc | 2585 | is_friend, 0); |
74b846e0 MM |
2586 | } |
2587 | ||
36a117a5 | 2588 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
aa373032 | 2589 | DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs); |
36a117a5 | 2590 | |
8afa707f MM |
2591 | /* Inherit default function arguments from the template |
2592 | DECL is specializing. */ | |
2593 | copy_default_args_to_explicit_spec (decl); | |
2594 | ||
c750255c MM |
2595 | /* This specialization has the same protection as the |
2596 | template it specializes. */ | |
2597 | TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); | |
2598 | TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); | |
b9e75696 | 2599 | |
736b8100 SB |
2600 | /* 7.1.1-1 [dcl.stc] |
2601 | ||
2602 | A storage-class-specifier shall not be specified in an | |
2603 | explicit specialization... | |
2604 | ||
2605 | The parser rejects these, so unless action is taken here, | |
2606 | explicit function specializations will always appear with | |
2607 | global linkage. | |
2608 | ||
2609 | The action recommended by the C++ CWG in response to C++ | |
2610 | defect report 605 is to make the storage class and linkage | |
2611 | of the explicit specialization match the templated function: | |
2612 | ||
2613 | http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605 | |
2614 | */ | |
2615 | if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl)) | |
2616 | { | |
2617 | tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl); | |
2618 | gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL); | |
2619 | ||
d732e98f | 2620 | /* This specialization has the same linkage and visibility as |
736b8100 SB |
2621 | the function template it specializes. */ |
2622 | TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func); | |
92d0af97 JJ |
2623 | if (! TREE_PUBLIC (decl)) |
2624 | { | |
2625 | DECL_INTERFACE_KNOWN (decl) = 1; | |
2626 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
2627 | } | |
736b8100 SB |
2628 | DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func); |
2629 | if (DECL_VISIBILITY_SPECIFIED (tmpl_func)) | |
2630 | { | |
2631 | DECL_VISIBILITY_SPECIFIED (decl) = 1; | |
2632 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func); | |
2633 | } | |
2634 | } | |
2635 | ||
9c12301f MM |
2636 | /* If DECL is a friend declaration, declared using an |
2637 | unqualified name, the namespace associated with DECL may | |
2638 | have been set incorrectly. For example, in: | |
3db45ab5 MS |
2639 | |
2640 | template <typename T> void f(T); | |
2641 | namespace N { | |
2642 | struct S { friend void f<int>(int); } | |
2643 | } | |
2644 | ||
2645 | we will have set the DECL_CONTEXT for the friend | |
2646 | declaration to N, rather than to the global namespace. */ | |
9c12301f MM |
2647 | if (DECL_NAMESPACE_SCOPE_P (decl)) |
2648 | DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl); | |
8d039470 | 2649 | |
386b8a85 | 2650 | if (is_friend && !have_def) |
36a117a5 MM |
2651 | /* This is not really a declaration of a specialization. |
2652 | It's just the name of an instantiation. But, it's not | |
2653 | a request for an instantiation, either. */ | |
fbf1c34b | 2654 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
08511114 KL |
2655 | else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) |
2656 | /* This is indeed a specialization. In case of constructors | |
2657 | and destructors, we need in-charge and not-in-charge | |
2658 | versions in V3 ABI. */ | |
2659 | clone_function_decl (decl, /*update_method_vec_p=*/0); | |
75650646 | 2660 | |
36a117a5 MM |
2661 | /* Register this specialization so that we can find it |
2662 | again. */ | |
2b110bfc | 2663 | decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0); |
386b8a85 JM |
2664 | } |
2665 | } | |
c8094d83 | 2666 | |
e1467ff2 | 2667 | return decl; |
386b8a85 | 2668 | } |
75650646 | 2669 | |
75650646 MM |
2670 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
2671 | parameters. These are represented in the same format used for | |
2672 | DECL_TEMPLATE_PARMS. */ | |
2673 | ||
50bc768d | 2674 | int |
58f9752a | 2675 | comp_template_parms (const_tree parms1, const_tree parms2) |
75650646 | 2676 | { |
58f9752a KG |
2677 | const_tree p1; |
2678 | const_tree p2; | |
75650646 MM |
2679 | |
2680 | if (parms1 == parms2) | |
2681 | return 1; | |
2682 | ||
c8094d83 | 2683 | for (p1 = parms1, p2 = parms2; |
75650646 MM |
2684 | p1 != NULL_TREE && p2 != NULL_TREE; |
2685 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
2686 | { | |
2687 | tree t1 = TREE_VALUE (p1); | |
2688 | tree t2 = TREE_VALUE (p2); | |
2689 | int i; | |
2690 | ||
50bc768d NS |
2691 | gcc_assert (TREE_CODE (t1) == TREE_VEC); |
2692 | gcc_assert (TREE_CODE (t2) == TREE_VEC); | |
75650646 MM |
2693 | |
2694 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
2695 | return 0; | |
2696 | ||
c8094d83 | 2697 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) |
75650646 | 2698 | { |
0f67a82f LM |
2699 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); |
2700 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
42b304f1 | 2701 | |
0f67a82f LM |
2702 | /* If either of the template parameters are invalid, assume |
2703 | they match for the sake of error recovery. */ | |
2704 | if (parm1 == error_mark_node || parm2 == error_mark_node) | |
2705 | return 1; | |
75650646 MM |
2706 | |
2707 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
2708 | return 0; | |
2709 | ||
5d80a306 DG |
2710 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM |
2711 | && (TEMPLATE_TYPE_PARAMETER_PACK (parm1) | |
2712 | == TEMPLATE_TYPE_PARAMETER_PACK (parm2))) | |
75650646 | 2713 | continue; |
3bfdc719 | 2714 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
2715 | return 0; |
2716 | } | |
2717 | } | |
2718 | ||
2719 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
2720 | /* One set of parameters has more parameters lists than the | |
2721 | other. */ | |
2722 | return 0; | |
2723 | ||
2724 | return 1; | |
2725 | } | |
2726 | ||
5d80a306 | 2727 | /* Determine whether PARM is a parameter pack. */ |
d40a19da | 2728 | |
5d80a306 | 2729 | bool |
58f9752a | 2730 | template_parameter_pack_p (const_tree parm) |
5d80a306 DG |
2731 | { |
2732 | /* Determine if we have a non-type template parameter pack. */ | |
2733 | if (TREE_CODE (parm) == PARM_DECL) | |
2734 | return (DECL_TEMPLATE_PARM_P (parm) | |
2735 | && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))); | |
2736 | ||
2737 | /* If this is a list of template parameters, we could get a | |
2738 | TYPE_DECL or a TEMPLATE_DECL. */ | |
2739 | if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL) | |
2740 | parm = TREE_TYPE (parm); | |
2741 | ||
2742 | return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
2743 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) | |
2744 | && TEMPLATE_TYPE_PARAMETER_PACK (parm)); | |
2745 | } | |
2746 | ||
d40a19da DS |
2747 | /* Determine if T is a function parameter pack. */ |
2748 | ||
2749 | bool | |
2750 | function_parameter_pack_p (const_tree t) | |
2751 | { | |
2752 | if (t && TREE_CODE (t) == PARM_DECL) | |
2753 | return FUNCTION_PARAMETER_PACK_P (t); | |
2754 | return false; | |
2755 | } | |
2756 | ||
2757 | /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST. | |
2758 | PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */ | |
2759 | ||
2760 | tree | |
2761 | get_function_template_decl (const_tree primary_func_tmpl_inst) | |
2762 | { | |
2763 | if (! primary_func_tmpl_inst | |
2764 | || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL | |
2765 | || ! primary_template_instantiation_p (primary_func_tmpl_inst)) | |
2766 | return NULL; | |
2767 | ||
2768 | return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst)); | |
2769 | } | |
2770 | ||
2771 | /* Return true iff the function parameter PARAM_DECL was expanded | |
2772 | from the function parameter pack PACK. */ | |
2773 | ||
2774 | bool | |
2775 | function_parameter_expanded_from_pack_p (tree param_decl, tree pack) | |
2776 | { | |
0857d1f0 JM |
2777 | if (DECL_ARTIFICIAL (param_decl) |
2778 | || !function_parameter_pack_p (pack)) | |
2779 | return false; | |
d40a19da | 2780 | |
0857d1f0 JM |
2781 | /* The parameter pack and its pack arguments have the same |
2782 | DECL_PARM_INDEX. */ | |
2783 | return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl); | |
d40a19da DS |
2784 | } |
2785 | ||
5d80a306 DG |
2786 | /* Determine whether ARGS describes a variadic template args list, |
2787 | i.e., one that is terminated by a template argument pack. */ | |
d40a19da | 2788 | |
5d80a306 DG |
2789 | static bool |
2790 | template_args_variadic_p (tree args) | |
2791 | { | |
2792 | int nargs; | |
2793 | tree last_parm; | |
2794 | ||
2795 | if (args == NULL_TREE) | |
2796 | return false; | |
2797 | ||
2798 | args = INNERMOST_TEMPLATE_ARGS (args); | |
2799 | nargs = TREE_VEC_LENGTH (args); | |
2800 | ||
2801 | if (nargs == 0) | |
2802 | return false; | |
2803 | ||
2804 | last_parm = TREE_VEC_ELT (args, nargs - 1); | |
2805 | ||
2806 | return ARGUMENT_PACK_P (last_parm); | |
2807 | } | |
2808 | ||
2809 | /* Generate a new name for the parameter pack name NAME (an | |
2810 | IDENTIFIER_NODE) that incorporates its */ | |
d40a19da | 2811 | |
5d80a306 DG |
2812 | static tree |
2813 | make_ith_pack_parameter_name (tree name, int i) | |
2814 | { | |
2815 | /* Munge the name to include the parameter index. */ | |
f9329c35 DS |
2816 | #define NUMBUF_LEN 128 |
2817 | char numbuf[NUMBUF_LEN]; | |
5d80a306 | 2818 | char* newname; |
f9329c35 DS |
2819 | int newname_len; |
2820 | ||
2821 | snprintf (numbuf, NUMBUF_LEN, "%i", i); | |
2822 | newname_len = IDENTIFIER_LENGTH (name) | |
d3bc57f2 | 2823 | + strlen (numbuf) + 2; |
f9329c35 DS |
2824 | newname = (char*)alloca (newname_len); |
2825 | snprintf (newname, newname_len, | |
2826 | "%s#%i", IDENTIFIER_POINTER (name), i); | |
5d80a306 DG |
2827 | return get_identifier (newname); |
2828 | } | |
2829 | ||
f9329c35 DS |
2830 | /* Return true if T is a primary function |
2831 | or class template instantiation. */ | |
2832 | ||
58926110 | 2833 | bool |
f9329c35 DS |
2834 | primary_template_instantiation_p (const_tree t) |
2835 | { | |
2836 | if (!t) | |
2837 | return false; | |
2838 | ||
2839 | if (TREE_CODE (t) == FUNCTION_DECL) | |
2840 | return DECL_LANG_SPECIFIC (t) | |
2841 | && DECL_TEMPLATE_INSTANTIATION (t) | |
2842 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)); | |
2843 | else if (CLASS_TYPE_P (t)) | |
2844 | return CLASSTYPE_TEMPLATE_INSTANTIATION (t) | |
2845 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)); | |
2846 | return false; | |
2847 | } | |
2848 | ||
2849 | /* Return true if PARM is a template template parameter. */ | |
2850 | ||
2851 | bool | |
2852 | template_template_parameter_p (const_tree parm) | |
2853 | { | |
2854 | return DECL_TEMPLATE_TEMPLATE_PARM_P (parm); | |
2855 | } | |
2856 | ||
2857 | /* Return the template parameters of T if T is a | |
2858 | primary template instantiation, NULL otherwise. */ | |
2859 | ||
2860 | tree | |
2861 | get_primary_template_innermost_parameters (const_tree t) | |
2862 | { | |
2863 | tree parms = NULL, template_info = NULL; | |
2864 | ||
2865 | if ((template_info = get_template_info (t)) | |
2866 | && primary_template_instantiation_p (t)) | |
2867 | parms = INNERMOST_TEMPLATE_PARMS | |
2868 | (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info))); | |
2869 | ||
2870 | return parms; | |
2871 | } | |
2872 | ||
58f5f6b4 DS |
2873 | /* Return the template parameters of the LEVELth level from the full list |
2874 | of template parameters PARMS. */ | |
2875 | ||
2876 | tree | |
3910807d | 2877 | get_template_parms_at_level (tree parms, int level) |
58f5f6b4 DS |
2878 | { |
2879 | tree p; | |
2880 | if (!parms | |
2881 | || TREE_CODE (parms) != TREE_LIST | |
2882 | || level > TMPL_PARMS_DEPTH (parms)) | |
2883 | return NULL_TREE; | |
2884 | ||
2885 | for (p = parms; p; p = TREE_CHAIN (p)) | |
2886 | if (TMPL_PARMS_DEPTH (p) == level) | |
2887 | return p; | |
2888 | ||
2889 | return NULL_TREE; | |
2890 | } | |
2891 | ||
f9329c35 DS |
2892 | /* Returns the template arguments of T if T is a template instantiation, |
2893 | NULL otherwise. */ | |
2894 | ||
2895 | tree | |
2896 | get_template_innermost_arguments (const_tree t) | |
2897 | { | |
2898 | tree args = NULL, template_info = NULL; | |
2899 | ||
2900 | if ((template_info = get_template_info (t)) | |
2901 | && TI_ARGS (template_info)) | |
2902 | args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info)); | |
2903 | ||
2904 | return args; | |
2905 | } | |
2906 | ||
d40a19da DS |
2907 | /* Return the argument pack elements of T if T is a template argument pack, |
2908 | NULL otherwise. */ | |
f9329c35 DS |
2909 | |
2910 | tree | |
2911 | get_template_argument_pack_elems (const_tree t) | |
2912 | { | |
2913 | if (TREE_CODE (t) != TYPE_ARGUMENT_PACK | |
2914 | && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK) | |
2915 | return NULL; | |
2916 | ||
2917 | return ARGUMENT_PACK_ARGS (t); | |
2918 | } | |
2919 | ||
1ad8aeeb | 2920 | /* Structure used to track the progress of find_parameter_packs_r. */ |
5d80a306 DG |
2921 | struct find_parameter_pack_data |
2922 | { | |
1ad8aeeb DG |
2923 | /* TREE_LIST that will contain all of the parameter packs found by |
2924 | the traversal. */ | |
5d80a306 | 2925 | tree* parameter_packs; |
1ad8aeeb DG |
2926 | |
2927 | /* Set of AST nodes that have been visited by the traversal. */ | |
5d80a306 DG |
2928 | struct pointer_set_t *visited; |
2929 | }; | |
2930 | ||
88b82314 | 2931 | /* Identifies all of the argument packs that occur in a template |
5d80a306 | 2932 | argument and appends them to the TREE_LIST inside DATA, which is a |
88b82314 | 2933 | find_parameter_pack_data structure. This is a subroutine of |
5d80a306 DG |
2934 | make_pack_expansion and uses_parameter_packs. */ |
2935 | static tree | |
2936 | find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data) | |
2937 | { | |
2938 | tree t = *tp; | |
2939 | struct find_parameter_pack_data* ppd = | |
2940 | (struct find_parameter_pack_data*)data; | |
1ad8aeeb | 2941 | bool parameter_pack_p = false; |
5d80a306 | 2942 | |
1ad8aeeb DG |
2943 | /* Identify whether this is a parameter pack or not. */ |
2944 | switch (TREE_CODE (t)) | |
2945 | { | |
2946 | case TEMPLATE_PARM_INDEX: | |
2947 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
2948 | parameter_pack_p = true; | |
2949 | break; | |
2950 | ||
2951 | case TEMPLATE_TYPE_PARM: | |
2952 | case TEMPLATE_TEMPLATE_PARM: | |
2953 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
2954 | parameter_pack_p = true; | |
2955 | break; | |
2956 | ||
2957 | case PARM_DECL: | |
2958 | if (FUNCTION_PARAMETER_PACK_P (t)) | |
2959 | { | |
2960 | /* We don't want to walk into the type of a PARM_DECL, | |
2961 | because we don't want to see the type parameter pack. */ | |
2962 | *walk_subtrees = 0; | |
2963 | parameter_pack_p = true; | |
2964 | } | |
2965 | break; | |
2966 | ||
2967 | default: | |
2968 | /* Not a parameter pack. */ | |
2969 | break; | |
2970 | } | |
2971 | ||
2972 | if (parameter_pack_p) | |
2973 | { | |
2974 | /* Add this parameter pack to the list. */ | |
2975 | *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs); | |
1ad8aeeb | 2976 | } |
7b3e2d46 | 2977 | |
1ad8aeeb DG |
2978 | if (TYPE_P (t)) |
2979 | cp_walk_tree (&TYPE_CONTEXT (t), | |
92fab505 | 2980 | &find_parameter_packs_r, ppd, ppd->visited); |
1ad8aeeb | 2981 | |
5d80a306 DG |
2982 | /* This switch statement will return immediately if we don't find a |
2983 | parameter pack. */ | |
2984 | switch (TREE_CODE (t)) | |
2985 | { | |
2986 | case TEMPLATE_PARM_INDEX: | |
5d80a306 DG |
2987 | return NULL_TREE; |
2988 | ||
2989 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
1ad8aeeb DG |
2990 | /* Check the template itself. */ |
2991 | cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)), | |
92fab505 | 2992 | &find_parameter_packs_r, ppd, ppd->visited); |
5d80a306 | 2993 | /* Check the template arguments. */ |
14588106 | 2994 | cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd, |
92fab505 | 2995 | ppd->visited); |
5d80a306 | 2996 | *walk_subtrees = 0; |
1ad8aeeb | 2997 | return NULL_TREE; |
5d80a306 DG |
2998 | |
2999 | case TEMPLATE_TYPE_PARM: | |
3000 | case TEMPLATE_TEMPLATE_PARM: | |
5d80a306 DG |
3001 | return NULL_TREE; |
3002 | ||
3003 | case PARM_DECL: | |
5d80a306 DG |
3004 | return NULL_TREE; |
3005 | ||
3006 | case RECORD_TYPE: | |
3007 | if (TYPE_PTRMEMFUNC_P (t)) | |
3008 | return NULL_TREE; | |
3009 | /* Fall through. */ | |
3010 | ||
3011 | case UNION_TYPE: | |
3012 | case ENUMERAL_TYPE: | |
3013 | if (TYPE_TEMPLATE_INFO (t)) | |
aa373032 | 3014 | cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)), |
92fab505 | 3015 | &find_parameter_packs_r, ppd, ppd->visited); |
5d80a306 DG |
3016 | |
3017 | *walk_subtrees = 0; | |
3018 | return NULL_TREE; | |
3019 | ||
3020 | case TEMPLATE_DECL: | |
1ad8aeeb | 3021 | cp_walk_tree (&TREE_TYPE (t), |
92fab505 | 3022 | &find_parameter_packs_r, ppd, ppd->visited); |
5d80a306 | 3023 | return NULL_TREE; |
e1a18c68 DG |
3024 | |
3025 | case TYPENAME_TYPE: | |
3026 | cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r, | |
92fab505 | 3027 | ppd, ppd->visited); |
e1a18c68 DG |
3028 | *walk_subtrees = 0; |
3029 | return NULL_TREE; | |
e1a18c68 | 3030 | |
5d80a306 DG |
3031 | case TYPE_PACK_EXPANSION: |
3032 | case EXPR_PACK_EXPANSION: | |
3033 | *walk_subtrees = 0; | |
3034 | return NULL_TREE; | |
3035 | ||
d393153e | 3036 | case INTEGER_TYPE: |
14588106 | 3037 | cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r, |
92fab505 | 3038 | ppd, ppd->visited); |
d393153e DG |
3039 | *walk_subtrees = 0; |
3040 | return NULL_TREE; | |
3041 | ||
4439d02f | 3042 | case IDENTIFIER_NODE: |
92fab505 DG |
3043 | cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd, |
3044 | ppd->visited); | |
4439d02f DG |
3045 | *walk_subtrees = 0; |
3046 | return NULL_TREE; | |
3047 | ||
5d80a306 DG |
3048 | default: |
3049 | return NULL_TREE; | |
3050 | } | |
5d80a306 DG |
3051 | |
3052 | return NULL_TREE; | |
3053 | } | |
3054 | ||
3055 | /* Determines if the expression or type T uses any parameter packs. */ | |
3056 | bool | |
3057 | uses_parameter_packs (tree t) | |
3058 | { | |
3059 | tree parameter_packs = NULL_TREE; | |
3060 | struct find_parameter_pack_data ppd; | |
3061 | ppd.parameter_packs = ¶meter_packs; | |
3062 | ppd.visited = pointer_set_create (); | |
92fab505 | 3063 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); |
30bcc028 | 3064 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
3065 | return parameter_packs != NULL_TREE; |
3066 | } | |
3067 | ||
3068 | /* Turn ARG, which may be an expression, type, or a TREE_LIST | |
3069 | representation a base-class initializer into a parameter pack | |
3070 | expansion. If all goes well, the resulting node will be an | |
3071 | EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST, | |
3072 | respectively. */ | |
3073 | tree | |
3074 | make_pack_expansion (tree arg) | |
3075 | { | |
3076 | tree result; | |
3077 | tree parameter_packs = NULL_TREE; | |
3078 | bool for_types = false; | |
3079 | struct find_parameter_pack_data ppd; | |
3080 | ||
3081 | if (!arg || arg == error_mark_node) | |
3082 | return arg; | |
3083 | ||
3084 | if (TREE_CODE (arg) == TREE_LIST) | |
3085 | { | |
3086 | /* The only time we will see a TREE_LIST here is for a base | |
3087 | class initializer. In this case, the TREE_PURPOSE will be a | |
3088 | _TYPE node (representing the base class expansion we're | |
3089 | initializing) and the TREE_VALUE will be a TREE_LIST | |
3090 | containing the initialization arguments. | |
3091 | ||
3092 | The resulting expansion looks somewhat different from most | |
3093 | expansions. Rather than returning just one _EXPANSION, we | |
3094 | return a TREE_LIST whose TREE_PURPOSE is a | |
3095 | TYPE_PACK_EXPANSION containing the bases that will be | |
3096 | initialized. The TREE_VALUE will be identical to the | |
3097 | original TREE_VALUE, which is a list of arguments that will | |
3098 | be passed to each base. We do not introduce any new pack | |
3099 | expansion nodes into the TREE_VALUE (although it is possible | |
3100 | that some already exist), because the TREE_PURPOSE and | |
3101 | TREE_VALUE all need to be expanded together with the same | |
3102 | _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the | |
3103 | resulting TREE_PURPOSE will mention the parameter packs in | |
3104 | both the bases and the arguments to the bases. */ | |
3105 | tree purpose; | |
3106 | tree value; | |
3107 | tree parameter_packs = NULL_TREE; | |
3108 | ||
3109 | /* Determine which parameter packs will be used by the base | |
3110 | class expansion. */ | |
3111 | ppd.visited = pointer_set_create (); | |
3112 | ppd.parameter_packs = ¶meter_packs; | |
14588106 | 3113 | cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r, |
92fab505 | 3114 | &ppd, ppd.visited); |
5d80a306 DG |
3115 | |
3116 | if (parameter_packs == NULL_TREE) | |
3117 | { | |
3118 | error ("base initializer expansion %<%T%> contains no parameter packs", arg); | |
30bcc028 | 3119 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
3120 | return error_mark_node; |
3121 | } | |
3122 | ||
3123 | if (TREE_VALUE (arg) != void_type_node) | |
3124 | { | |
3125 | /* Collect the sets of parameter packs used in each of the | |
3126 | initialization arguments. */ | |
3127 | for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value)) | |
3128 | { | |
3129 | /* Determine which parameter packs will be expanded in this | |
3130 | argument. */ | |
14588106 | 3131 | cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r, |
92fab505 | 3132 | &ppd, ppd.visited); |
5d80a306 DG |
3133 | } |
3134 | } | |
3135 | ||
30bcc028 DG |
3136 | pointer_set_destroy (ppd.visited); |
3137 | ||
5d80a306 | 3138 | /* Create the pack expansion type for the base type. */ |
7ecbca9d | 3139 | purpose = cxx_make_type (TYPE_PACK_EXPANSION); |
5d80a306 DG |
3140 | SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg)); |
3141 | PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs; | |
3142 | ||
3143 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
3144 | they will rarely be compared to anything. */ | |
3145 | SET_TYPE_STRUCTURAL_EQUALITY (purpose); | |
3146 | ||
3147 | return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE); | |
3148 | } | |
3149 | ||
3150 | if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL) | |
3151 | for_types = true; | |
3152 | ||
3153 | /* Build the PACK_EXPANSION_* node. */ | |
7ecbca9d GDR |
3154 | result = for_types |
3155 | ? cxx_make_type (TYPE_PACK_EXPANSION) | |
3156 | : make_node (EXPR_PACK_EXPANSION); | |
5d80a306 DG |
3157 | SET_PACK_EXPANSION_PATTERN (result, arg); |
3158 | if (TREE_CODE (result) == EXPR_PACK_EXPANSION) | |
3159 | { | |
3160 | /* Propagate type and const-expression information. */ | |
3161 | TREE_TYPE (result) = TREE_TYPE (arg); | |
3162 | TREE_CONSTANT (result) = TREE_CONSTANT (arg); | |
3163 | } | |
3164 | else | |
3165 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
3166 | they will rarely be compared to anything. */ | |
3167 | SET_TYPE_STRUCTURAL_EQUALITY (result); | |
3168 | ||
3169 | /* Determine which parameter packs will be expanded. */ | |
3170 | ppd.parameter_packs = ¶meter_packs; | |
3171 | ppd.visited = pointer_set_create (); | |
92fab505 | 3172 | cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited); |
30bcc028 | 3173 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
3174 | |
3175 | /* Make sure we found some parameter packs. */ | |
3176 | if (parameter_packs == NULL_TREE) | |
3177 | { | |
3178 | if (TYPE_P (arg)) | |
3179 | error ("expansion pattern %<%T%> contains no argument packs", arg); | |
3180 | else | |
3181 | error ("expansion pattern %<%E%> contains no argument packs", arg); | |
3182 | return error_mark_node; | |
3183 | } | |
3184 | PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs; | |
3185 | ||
3186 | return result; | |
3187 | } | |
3188 | ||
3189 | /* Checks T for any "bare" parameter packs, which have not yet been | |
3190 | expanded, and issues an error if any are found. This operation can | |
3191 | only be done on full expressions or types (e.g., an expression | |
3192 | statement, "if" condition, etc.), because we could have expressions like: | |
3193 | ||
3194 | foo(f(g(h(args)))...) | |
3195 | ||
3196 | where "args" is a parameter pack. check_for_bare_parameter_packs | |
3197 | should not be called for the subexpressions args, h(args), | |
3198 | g(h(args)), or f(g(h(args))), because we would produce erroneous | |
b1d7b1c0 DG |
3199 | error messages. |
3200 | ||
4439d02f DG |
3201 | Returns TRUE and emits an error if there were bare parameter packs, |
3202 | returns FALSE otherwise. */ | |
b1d7b1c0 | 3203 | bool |
7b3e2d46 | 3204 | check_for_bare_parameter_packs (tree t) |
5d80a306 DG |
3205 | { |
3206 | tree parameter_packs = NULL_TREE; | |
3207 | struct find_parameter_pack_data ppd; | |
3208 | ||
7b3e2d46 | 3209 | if (!processing_template_decl || !t || t == error_mark_node) |
4439d02f | 3210 | return false; |
5d80a306 | 3211 | |
7b3e2d46 DG |
3212 | if (TREE_CODE (t) == TYPE_DECL) |
3213 | t = TREE_TYPE (t); | |
5d80a306 DG |
3214 | |
3215 | ppd.parameter_packs = ¶meter_packs; | |
3216 | ppd.visited = pointer_set_create (); | |
92fab505 | 3217 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); |
30bcc028 | 3218 | pointer_set_destroy (ppd.visited); |
5d80a306 | 3219 | |
b1d7b1c0 DG |
3220 | if (parameter_packs) |
3221 | { | |
bcac2b89 | 3222 | error ("parameter packs not expanded with %<...%>:"); |
b1d7b1c0 DG |
3223 | while (parameter_packs) |
3224 | { | |
3225 | tree pack = TREE_VALUE (parameter_packs); | |
3226 | tree name = NULL_TREE; | |
3227 | ||
3228 | if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM | |
3229 | || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM) | |
3230 | name = TYPE_NAME (pack); | |
3231 | else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX) | |
3232 | name = DECL_NAME (TEMPLATE_PARM_DECL (pack)); | |
3233 | else | |
3234 | name = DECL_NAME (pack); | |
1ad8aeeb DG |
3235 | |
3236 | if (name) | |
1f5b3869 | 3237 | inform (input_location, " %qD", name); |
1ad8aeeb | 3238 | else |
1f5b3869 | 3239 | inform (input_location, " <anonymous>"); |
b1d7b1c0 DG |
3240 | |
3241 | parameter_packs = TREE_CHAIN (parameter_packs); | |
3242 | } | |
3243 | ||
4439d02f | 3244 | return true; |
b1d7b1c0 DG |
3245 | } |
3246 | ||
4439d02f | 3247 | return false; |
5d80a306 DG |
3248 | } |
3249 | ||
3250 | /* Expand any parameter packs that occur in the template arguments in | |
3251 | ARGS. */ | |
3252 | tree | |
3253 | expand_template_argument_pack (tree args) | |
3254 | { | |
3255 | tree result_args = NULL_TREE; | |
3256 | int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0; | |
3257 | int num_result_args = -1; | |
1a048f82 | 3258 | int non_default_args_count = -1; |
5d80a306 DG |
3259 | |
3260 | /* First, determine if we need to expand anything, and the number of | |
3261 | slots we'll need. */ | |
3262 | for (in_arg = 0; in_arg < nargs; ++in_arg) | |
3263 | { | |
3264 | tree arg = TREE_VEC_ELT (args, in_arg); | |
5b6dad5d JM |
3265 | if (arg == NULL_TREE) |
3266 | return args; | |
5d80a306 DG |
3267 | if (ARGUMENT_PACK_P (arg)) |
3268 | { | |
3269 | int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)); | |
3270 | if (num_result_args < 0) | |
3271 | num_result_args = in_arg + num_packed; | |
3272 | else | |
3273 | num_result_args += num_packed; | |
3274 | } | |
3275 | else | |
3276 | { | |
3277 | if (num_result_args >= 0) | |
3278 | num_result_args++; | |
3279 | } | |
3280 | } | |
3281 | ||
3282 | /* If no expansion is necessary, we're done. */ | |
3283 | if (num_result_args < 0) | |
3284 | return args; | |
3285 | ||
3286 | /* Expand arguments. */ | |
3287 | result_args = make_tree_vec (num_result_args); | |
1a048f82 DS |
3288 | if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args)) |
3289 | non_default_args_count = | |
3290 | GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args); | |
5d80a306 DG |
3291 | for (in_arg = 0; in_arg < nargs; ++in_arg) |
3292 | { | |
3293 | tree arg = TREE_VEC_ELT (args, in_arg); | |
3294 | if (ARGUMENT_PACK_P (arg)) | |
3295 | { | |
3296 | tree packed = ARGUMENT_PACK_ARGS (arg); | |
3297 | int i, num_packed = TREE_VEC_LENGTH (packed); | |
3298 | for (i = 0; i < num_packed; ++i, ++out_arg) | |
3299 | TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i); | |
1a048f82 DS |
3300 | if (non_default_args_count > 0) |
3301 | non_default_args_count += num_packed; | |
5d80a306 DG |
3302 | } |
3303 | else | |
3304 | { | |
3305 | TREE_VEC_ELT (result_args, out_arg) = arg; | |
3306 | ++out_arg; | |
3307 | } | |
3308 | } | |
1a048f82 DS |
3309 | if (non_default_args_count >= 0) |
3310 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count); | |
5d80a306 DG |
3311 | return result_args; |
3312 | } | |
3313 | ||
157b0647 | 3314 | /* Checks if DECL shadows a template parameter. |
f3400fe2 JM |
3315 | |
3316 | [temp.local]: A template-parameter shall not be redeclared within its | |
157b0647 | 3317 | scope (including nested scopes). |
f3400fe2 | 3318 | |
157b0647 PC |
3319 | Emits an error and returns TRUE if the DECL shadows a parameter, |
3320 | returns FALSE otherwise. */ | |
3321 | ||
3322 | bool | |
3a978d72 | 3323 | check_template_shadow (tree decl) |
f3400fe2 | 3324 | { |
8f032717 MM |
3325 | tree olddecl; |
3326 | ||
b5d9b9ab MM |
3327 | /* If we're not in a template, we can't possibly shadow a template |
3328 | parameter. */ | |
3329 | if (!current_template_parms) | |
157b0647 | 3330 | return true; |
b5d9b9ab MM |
3331 | |
3332 | /* Figure out what we're shadowing. */ | |
8f032717 MM |
3333 | if (TREE_CODE (decl) == OVERLOAD) |
3334 | decl = OVL_CURRENT (decl); | |
90ea9897 | 3335 | olddecl = innermost_non_namespace_value (DECL_NAME (decl)); |
f3400fe2 | 3336 | |
b5d9b9ab MM |
3337 | /* If there's no previous binding for this name, we're not shadowing |
3338 | anything, let alone a template parameter. */ | |
3339 | if (!olddecl) | |
157b0647 | 3340 | return true; |
b5d9b9ab MM |
3341 | |
3342 | /* If we're not shadowing a template parameter, we're done. Note | |
3343 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
3344 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
3345 | node. */ | |
2f939d94 | 3346 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) |
157b0647 | 3347 | return true; |
b5d9b9ab MM |
3348 | |
3349 | /* We check for decl != olddecl to avoid bogus errors for using a | |
3350 | name inside a class. We check TPFI to avoid duplicate errors for | |
3351 | inline member templates. */ | |
c8094d83 | 3352 | if (decl == olddecl |
b5d9b9ab | 3353 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) |
157b0647 | 3354 | return true; |
b5d9b9ab | 3355 | |
dee15844 JM |
3356 | error ("declaration of %q+#D", decl); |
3357 | error (" shadows template parm %q+#D", olddecl); | |
157b0647 | 3358 | return false; |
f3400fe2 | 3359 | } |
22a7be53 | 3360 | |
f3400fe2 | 3361 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
6f1abb06 DS |
3362 | ORIG_LEVEL, DECL, and TYPE. NUM_SIBLINGS is the total number of |
3363 | template parameters. */ | |
f84b4be9 JM |
3364 | |
3365 | static tree | |
c8094d83 | 3366 | build_template_parm_index (int index, |
0cbd7506 MS |
3367 | int level, |
3368 | int orig_level, | |
6f1abb06 | 3369 | int num_siblings, |
0cbd7506 MS |
3370 | tree decl, |
3371 | tree type) | |
f84b4be9 JM |
3372 | { |
3373 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
3374 | TEMPLATE_PARM_IDX (t) = index; | |
3375 | TEMPLATE_PARM_LEVEL (t) = level; | |
3376 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
6f1abb06 | 3377 | TEMPLATE_PARM_NUM_SIBLINGS (t) = num_siblings; |
f84b4be9 JM |
3378 | TEMPLATE_PARM_DECL (t) = decl; |
3379 | TREE_TYPE (t) = type; | |
3e4a3562 NS |
3380 | TREE_CONSTANT (t) = TREE_CONSTANT (decl); |
3381 | TREE_READONLY (t) = TREE_READONLY (decl); | |
f84b4be9 JM |
3382 | |
3383 | return t; | |
3384 | } | |
3385 | ||
06d40de8 | 3386 | /* Find the canonical type parameter for the given template type |
c3e188fc KH |
3387 | parameter. Returns the canonical type parameter, which may be TYPE |
3388 | if no such parameter existed. */ | |
6f1abb06 | 3389 | |
06d40de8 DG |
3390 | static tree |
3391 | canonical_type_parameter (tree type) | |
3392 | { | |
3393 | tree list; | |
3394 | int idx = TEMPLATE_TYPE_IDX (type); | |
3395 | if (!canonical_template_parms) | |
3396 | canonical_template_parms = VEC_alloc (tree, gc, idx+1); | |
3397 | ||
3398 | while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx) | |
3399 | VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE); | |
3400 | ||
3401 | list = VEC_index (tree, canonical_template_parms, idx); | |
96d84882 | 3402 | while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL)) |
06d40de8 DG |
3403 | list = TREE_CHAIN (list); |
3404 | ||
3405 | if (list) | |
3406 | return TREE_VALUE (list); | |
3407 | else | |
3408 | { | |
3409 | VEC_replace(tree, canonical_template_parms, idx, | |
3410 | tree_cons (NULL_TREE, type, | |
3411 | VEC_index (tree, canonical_template_parms, idx))); | |
3412 | return type; | |
3413 | } | |
3414 | } | |
3415 | ||
f84b4be9 | 3416 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 3417 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
3418 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
3419 | new one is created. */ | |
3420 | ||
c8094d83 | 3421 | static tree |
ef3b7b17 JM |
3422 | reduce_template_parm_level (tree index, tree type, int levels, tree args, |
3423 | tsubst_flags_t complain) | |
f84b4be9 JM |
3424 | { |
3425 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
3426 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
28651057 JM |
3427 | != TEMPLATE_PARM_LEVEL (index) - levels) |
3428 | || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index)))) | |
f84b4be9 | 3429 | { |
3e4a3562 NS |
3430 | tree orig_decl = TEMPLATE_PARM_DECL (index); |
3431 | tree decl, t; | |
c8094d83 | 3432 | |
c2255bc4 AH |
3433 | decl = build_decl (DECL_SOURCE_LOCATION (orig_decl), |
3434 | TREE_CODE (orig_decl), DECL_NAME (orig_decl), type); | |
3e4a3562 NS |
3435 | TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); |
3436 | TREE_READONLY (decl) = TREE_READONLY (orig_decl); | |
3437 | DECL_ARTIFICIAL (decl) = 1; | |
3438 | SET_DECL_TEMPLATE_PARM_P (decl); | |
c8094d83 | 3439 | |
3e4a3562 | 3440 | t = build_template_parm_index (TEMPLATE_PARM_IDX (index), |
93cdc044 | 3441 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 | 3442 | TEMPLATE_PARM_ORIG_LEVEL (index), |
6f1abb06 | 3443 | TEMPLATE_PARM_NUM_SIBLINGS (index), |
f84b4be9 JM |
3444 | decl, type); |
3445 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
5d80a306 DG |
3446 | TEMPLATE_PARM_PARAMETER_PACK (t) |
3447 | = TEMPLATE_PARM_PARAMETER_PACK (index); | |
cae40af6 | 3448 | |
820cc88f | 3449 | /* Template template parameters need this. */ |
ef3b7b17 JM |
3450 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
3451 | DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms | |
3452 | (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)), | |
3453 | args, complain); | |
f84b4be9 JM |
3454 | } |
3455 | ||
3456 | return TEMPLATE_PARM_DESCENDANTS (index); | |
3457 | } | |
3458 | ||
6f1abb06 DS |
3459 | /* Process information from new template parameter PARM and append it |
3460 | to the LIST being built. This new parameter is a non-type | |
3461 | parameter iff IS_NON_TYPE is true. This new parameter is a | |
3462 | parameter pack iff IS_PARAMETER_PACK is true. The location of PARM | |
3463 | is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template | |
3464 | parameter list PARM belongs to. This is used used to create a | |
3465 | proper canonical type for the type of PARM that is to be created, | |
3466 | iff PARM is a type. If the size is not known, this parameter shall | |
3467 | be set to 0. */ | |
e92cc029 | 3468 | |
8d08fdba | 3469 | tree |
6f1abb06 DS |
3470 | process_template_parm (tree list, location_t parm_loc, tree parm, |
3471 | bool is_non_type, bool is_parameter_pack, | |
3472 | unsigned num_template_parms) | |
8d08fdba | 3473 | { |
8d08fdba | 3474 | tree decl = 0; |
a292b002 | 3475 | tree defval; |
0f67a82f | 3476 | tree err_parm_list; |
d47e3adf | 3477 | int idx = 0; |
f84b4be9 | 3478 | |
50bc768d | 3479 | gcc_assert (TREE_CODE (parm) == TREE_LIST); |
a292b002 | 3480 | defval = TREE_PURPOSE (parm); |
5566b478 MS |
3481 | |
3482 | if (list) | |
3483 | { | |
d47e3adf LM |
3484 | tree p = tree_last (list); |
3485 | ||
0f67a82f | 3486 | if (p && TREE_VALUE (p) != error_mark_node) |
d47e3adf LM |
3487 | { |
3488 | p = TREE_VALUE (p); | |
3489 | if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) | |
3490 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
3491 | else | |
3492 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); | |
3493 | } | |
5566b478 | 3494 | |
5566b478 MS |
3495 | ++idx; |
3496 | } | |
3497 | else | |
3498 | idx = 0; | |
3499 | ||
058b15c1 | 3500 | if (is_non_type) |
8d08fdba | 3501 | { |
058b15c1 MM |
3502 | parm = TREE_VALUE (parm); |
3503 | ||
833aa4c4 | 3504 | SET_DECL_TEMPLATE_PARM_P (parm); |
d490621d | 3505 | |
620188c9 | 3506 | if (TREE_TYPE (parm) == error_mark_node) |
0f67a82f LM |
3507 | { |
3508 | err_parm_list = build_tree_list (defval, parm); | |
3509 | TREE_VALUE (err_parm_list) = error_mark_node; | |
3510 | return chainon (list, err_parm_list); | |
3511 | } | |
620188c9 VR |
3512 | else |
3513 | { | |
3514 | /* [temp.param] | |
d490621d | 3515 | |
620188c9 VR |
3516 | The top-level cv-qualifiers on the template-parameter are |
3517 | ignored when determining its type. */ | |
3518 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
3519 | if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) | |
0f67a82f LM |
3520 | { |
3521 | err_parm_list = build_tree_list (defval, parm); | |
3522 | TREE_VALUE (err_parm_list) = error_mark_node; | |
3523 | return chainon (list, err_parm_list); | |
3524 | } | |
5d80a306 DG |
3525 | |
3526 | if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack) | |
3527 | { | |
3528 | /* This template parameter is not a parameter pack, but it | |
3529 | should be. Complain about "bare" parameter packs. */ | |
7b3e2d46 | 3530 | check_for_bare_parameter_packs (TREE_TYPE (parm)); |
5d80a306 DG |
3531 | |
3532 | /* Recover by calling this a parameter pack. */ | |
3533 | is_parameter_pack = true; | |
3534 | } | |
620188c9 | 3535 | } |
d490621d | 3536 | |
8d08fdba | 3537 | /* A template parameter is not modifiable. */ |
6de9cd9a | 3538 | TREE_CONSTANT (parm) = 1; |
6de9cd9a | 3539 | TREE_READONLY (parm) = 1; |
c2255bc4 AH |
3540 | decl = build_decl (parm_loc, |
3541 | CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); | |
6de9cd9a | 3542 | TREE_CONSTANT (decl) = 1; |
6de9cd9a | 3543 | TREE_READONLY (decl) = 1; |
c8094d83 | 3544 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
f84b4be9 JM |
3545 | = build_template_parm_index (idx, processing_template_decl, |
3546 | processing_template_decl, | |
6f1abb06 | 3547 | num_template_parms, |
f84b4be9 | 3548 | decl, TREE_TYPE (parm)); |
5d80a306 DG |
3549 | |
3550 | TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)) | |
3551 | = is_parameter_pack; | |
8d08fdba MS |
3552 | } |
3553 | else | |
3554 | { | |
73b0fce8 | 3555 | tree t; |
058b15c1 | 3556 | parm = TREE_VALUE (TREE_VALUE (parm)); |
c8094d83 | 3557 | |
73b0fce8 KL |
3558 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) |
3559 | { | |
9e1e64ec | 3560 | t = cxx_make_type (TEMPLATE_TEMPLATE_PARM); |
c8094d83 | 3561 | /* This is for distinguishing between real templates and template |
73b0fce8 KL |
3562 | template parameters */ |
3563 | TREE_TYPE (parm) = t; | |
3564 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
3565 | decl = parm; | |
3566 | } | |
3567 | else | |
3568 | { | |
9e1e64ec | 3569 | t = cxx_make_type (TEMPLATE_TYPE_PARM); |
f4f206f4 | 3570 | /* parm is either IDENTIFIER_NODE or NULL_TREE. */ |
c2255bc4 AH |
3571 | decl = build_decl (parm_loc, |
3572 | TYPE_DECL, parm, t); | |
73b0fce8 | 3573 | } |
c8094d83 | 3574 | |
d2e5ee5c MS |
3575 | TYPE_NAME (t) = decl; |
3576 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 3577 | parm = decl; |
f84b4be9 | 3578 | TEMPLATE_TYPE_PARM_INDEX (t) |
c8094d83 | 3579 | = build_template_parm_index (idx, processing_template_decl, |
f84b4be9 | 3580 | processing_template_decl, |
6f1abb06 | 3581 | num_template_parms, |
f84b4be9 | 3582 | decl, TREE_TYPE (parm)); |
5d80a306 | 3583 | TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack; |
06d40de8 | 3584 | TYPE_CANONICAL (t) = canonical_type_parameter (t); |
8d08fdba | 3585 | } |
c727aa5e | 3586 | DECL_ARTIFICIAL (decl) = 1; |
cd9f6678 | 3587 | SET_DECL_TEMPLATE_PARM_P (decl); |
8d08fdba | 3588 | pushdecl (decl); |
a292b002 | 3589 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
3590 | return chainon (list, parm); |
3591 | } | |
3592 | ||
3593 | /* The end of a template parameter list has been reached. Process the | |
3594 | tree list into a parameter vector, converting each parameter into a more | |
3595 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
3596 | as PARM_DECLs. */ | |
3597 | ||
3598 | tree | |
3a978d72 | 3599 | end_template_parm_list (tree parms) |
8d08fdba | 3600 | { |
5566b478 | 3601 | int nparms; |
9471d3e2 | 3602 | tree parm, next; |
5566b478 MS |
3603 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
3604 | ||
5566b478 | 3605 | current_template_parms |
4890c2f4 | 3606 | = tree_cons (size_int (processing_template_decl), |
5566b478 | 3607 | saved_parmlist, current_template_parms); |
8d08fdba | 3608 | |
9471d3e2 NS |
3609 | for (parm = parms, nparms = 0; parm; parm = next, nparms++) |
3610 | { | |
3611 | next = TREE_CHAIN (parm); | |
3612 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; | |
3613 | TREE_CHAIN (parm) = NULL_TREE; | |
3614 | } | |
a292b002 | 3615 | |
6f1abb06 DS |
3616 | fixup_template_parms (); |
3617 | ||
67ffc812 MM |
3618 | --processing_template_parmlist; |
3619 | ||
8d08fdba MS |
3620 | return saved_parmlist; |
3621 | } | |
3622 | ||
6f1abb06 DS |
3623 | /* Create a new type almost identical to TYPE but which has the |
3624 | following differences: | |
3625 | ||
3626 | 1/ T has a new TEMPLATE_PARM_INDEX that carries the new number of | |
3627 | template sibling parameters of T. | |
3628 | ||
3629 | 2/ T has a new canonical type that matches the new number | |
3630 | of sibling parms. | |
3631 | ||
3632 | 3/ From now on, T is going to be what lookups referring to the | |
3633 | name of TYPE will return. No lookup should return TYPE anymore. | |
3634 | ||
3635 | NUM_PARMS is the new number of sibling parms TYPE belongs to. | |
3636 | ||
3637 | This is a subroutine of fixup_template_parms. */ | |
3638 | ||
3639 | static tree | |
3640 | fixup_template_type_parm_type (tree type, int num_parms) | |
3641 | { | |
3642 | tree orig_idx = TEMPLATE_TYPE_PARM_INDEX (type), idx; | |
3643 | tree t; | |
3644 | /* This is the decl which name is inserted into the symbol table for | |
3645 | the template parm type. So whenever we lookup the type name, this | |
3646 | is the DECL we get. */ | |
3647 | tree decl; | |
3648 | ||
3649 | /* Do not fix up the type twice. */ | |
3650 | if (orig_idx && TEMPLATE_PARM_NUM_SIBLINGS (orig_idx) != 0) | |
3651 | return type; | |
3652 | ||
3653 | t = copy_type (type); | |
3654 | decl = TYPE_NAME (t); | |
3655 | ||
3656 | TYPE_MAIN_VARIANT (t) = t; | |
3657 | TYPE_NEXT_VARIANT (t)= NULL_TREE; | |
3658 | TYPE_POINTER_TO (t) = 0; | |
3659 | TYPE_REFERENCE_TO (t) = 0; | |
3660 | ||
3661 | idx = build_template_parm_index (TEMPLATE_PARM_IDX (orig_idx), | |
3662 | TEMPLATE_PARM_LEVEL (orig_idx), | |
3663 | TEMPLATE_PARM_ORIG_LEVEL (orig_idx), | |
3664 | num_parms, | |
3665 | decl, t); | |
3666 | TEMPLATE_PARM_DESCENDANTS (idx) = TEMPLATE_PARM_DESCENDANTS (orig_idx); | |
3667 | TEMPLATE_PARM_PARAMETER_PACK (idx) = TEMPLATE_PARM_PARAMETER_PACK (orig_idx); | |
3668 | TEMPLATE_TYPE_PARM_INDEX (t) = idx; | |
3669 | ||
3670 | TYPE_STUB_DECL (t) = decl; | |
3671 | TEMPLATE_TYPE_DECL (t) = decl; | |
3672 | if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) | |
3673 | TREE_TYPE (DECL_TEMPLATE_RESULT (decl)) = t; | |
3674 | ||
3675 | /* Update the type associated to the type name stored in the symbol | |
3676 | table. Now, whenever the type name is looked up, the resulting | |
3677 | type is properly fixed up. */ | |
3678 | TREE_TYPE (decl) = t; | |
3679 | ||
3680 | TYPE_CANONICAL (t) = canonical_type_parameter (t); | |
3681 | ||
3682 | return t; | |
3683 | } | |
3684 | ||
3685 | /* Create and return a new TEMPLATE_PARM_INDEX that is almost | |
3686 | identical to I, but that is fixed up as to: | |
3687 | ||
3688 | 1/ carry the number of sibling parms (NUM_PARMS) of the template | |
3689 | parm represented by I. | |
3690 | ||
3691 | 2/ replace all references to template parm types declared before I | |
3692 | (in the same template parm list as I) by references to template | |
3693 | parm types contained in ARGS. ARGS should contain the list of | |
3694 | template parms that have been fixed up so far, in a form suitable | |
3695 | to be passed to tsubst. | |
3696 | ||
3697 | This is a subroutine of fixup_template_parms. */ | |
3698 | ||
3699 | static tree | |
3700 | fixup_template_parm_index (tree i, tree args, int num_parms) | |
3701 | { | |
3702 | tree index, decl, type; | |
3703 | ||
3704 | if (i == NULL_TREE | |
3705 | || TREE_CODE (i) != TEMPLATE_PARM_INDEX | |
3706 | /* Do not fix up the index twice. */ | |
3707 | || (TEMPLATE_PARM_NUM_SIBLINGS (i) != 0)) | |
3708 | return i; | |
3709 | ||
3710 | decl = TEMPLATE_PARM_DECL (i); | |
3711 | type = TREE_TYPE (decl); | |
3712 | ||
3713 | index = build_template_parm_index (TEMPLATE_PARM_IDX (i), | |
3714 | TEMPLATE_PARM_LEVEL (i), | |
3715 | TEMPLATE_PARM_ORIG_LEVEL (i), | |
3716 | num_parms, | |
3717 | decl, type); | |
3718 | ||
3719 | TEMPLATE_PARM_DESCENDANTS (index) = TEMPLATE_PARM_DESCENDANTS (i); | |
3720 | TEMPLATE_PARM_PARAMETER_PACK (index) = TEMPLATE_PARM_PARAMETER_PACK (i); | |
3721 | ||
3722 | type = tsubst (type, args, tf_none, NULL_TREE); | |
3723 | ||
3724 | TREE_TYPE (decl) = type; | |
3725 | TREE_TYPE (index) = type; | |
3726 | ||
3727 | return index; | |
3728 | } | |
3729 | ||
3730 | /* | |
3731 | This is a subroutine of fixup_template_parms. | |
3732 | ||
3733 | It computes the canonical type of the type of the template | |
3734 | parameter PARM_DESC and update all references to that type so that | |
3735 | they use the newly computed canonical type. No access check is | |
3736 | performed during the fixup. PARM_DESC is a TREE_LIST which | |
3737 | TREE_VALUE is the template parameter and its TREE_PURPOSE is the | |
3738 | default argument of the template parm if any. IDX is the index of | |
3739 | the template parameter, starting at 0. NUM_PARMS is the number of | |
3740 | template parameters in the set PARM_DESC belongs to. ARGLIST is a | |
3741 | TREE_VEC containing the full set of template parameters in a form | |
3742 | suitable to be passed to substs functions as their ARGS | |
3743 | argument. This is what current_template_args returns for a given | |
3744 | template. The innermost vector of args in ARGLIST is the set of | |
3745 | template parms that have been fixed up so far. This function adds | |
3746 | the fixed up parameter into that vector. */ | |
3747 | ||
3748 | static void | |
3749 | fixup_template_parm (tree parm_desc, | |
3750 | int idx, | |
3751 | int num_parms, | |
3752 | tree arglist) | |
3753 | { | |
3754 | tree parm = TREE_VALUE (parm_desc); | |
3755 | tree fixedup_args = INNERMOST_TEMPLATE_ARGS (arglist); | |
3756 | ||
3757 | push_deferring_access_checks (dk_no_check); | |
3758 | ||
3759 | if (TREE_CODE (parm) == TYPE_DECL) | |
3760 | { | |
3761 | /* PARM is a template type parameter. Fix up its type, add | |
3762 | the fixed-up template parm to the vector of fixed-up | |
3763 | template parms so far, and substitute the fixed-up | |
3764 | template parms into the default argument of this | |
3765 | parameter. */ | |
3766 | tree t = | |
3767 | fixup_template_type_parm_type (TREE_TYPE (parm), num_parms); | |
3768 | TREE_TYPE (parm) = t; | |
3769 | ||
3770 | TREE_VEC_ELT (fixedup_args, idx) = template_parm_to_arg (parm_desc); | |
3771 | } | |
3772 | else if (TREE_CODE (parm) == TEMPLATE_DECL) | |
3773 | { | |
3774 | /* PARM is a template template parameter. This is going to | |
3775 | be interesting. */ | |
3776 | tree tparms, targs, innermost_args; | |
3777 | int j; | |
3778 | ||
3779 | /* First, fix up the type of the parm. */ | |
3780 | ||
3781 | tree t = | |
3782 | fixup_template_type_parm_type (TREE_TYPE (parm), num_parms); | |
3783 | TREE_TYPE (parm) = t; | |
3784 | ||
3785 | TREE_VEC_ELT (fixedup_args, idx) = | |
3786 | template_parm_to_arg (parm_desc); | |
3787 | ||
3788 | /* Now we need to substitute the template parm types that | |
3789 | have been fixed up so far into the non-type template | |
3790 | parms of this template template parm. E.g, consider this: | |
3791 | ||
3792 | template<class T, template<T u> class TT> class S; | |
3793 | ||
3794 | In this case we want to substitute T into the | |
3795 | template parameters of TT. | |
3796 | ||
3797 | So let's walk the template parms of PARM here, and | |
3798 | tsubst ARGLIST into into each of the template | |
3799 | parms. */ | |
3800 | ||
3801 | /* For this substitution we need to build the full set of | |
3802 | template parameters and use that as arguments for the | |
3803 | tsubsting function. */ | |
3804 | tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm)); | |
3805 | ||
3806 | /* This will contain the innermost parms of PARM into which | |
3807 | we have substituted so far. */ | |
3808 | innermost_args = make_tree_vec (TREE_VEC_LENGTH (tparms)); | |
3809 | targs = add_to_template_args (arglist, innermost_args); | |
3810 | for (j = 0; j < TREE_VEC_LENGTH (tparms); ++j) | |
3811 | { | |
3812 | tree parameter; | |
3813 | ||
3814 | parameter = TREE_VEC_ELT (tparms, j); | |
3815 | ||
3816 | /* INNERMOST_ARGS needs to have at least the same number | |
3817 | of elements as the index PARAMETER, ortherwise | |
3818 | tsubsting into PARAMETER will result in partially | |
3819 | instantiating it, reducing its tempate parm | |
3820 | level. Let's tactically fill INNERMOST_ARGS for that | |
3821 | purpose. */ | |
3822 | TREE_VEC_ELT (innermost_args, j) = | |
3823 | template_parm_to_arg (parameter); | |
3824 | ||
3825 | fixup_template_parm (parameter, j, | |
3826 | TREE_VEC_LENGTH (tparms), | |
3827 | targs); | |
3828 | } | |
3829 | } | |
3830 | else if (TREE_CODE (parm) == PARM_DECL) | |
3831 | { | |
3832 | /* PARM is a non-type template parameter. We need to: | |
3833 | ||
3834 | * Fix up its TEMPLATE_PARM_INDEX to make it carry the | |
3835 | proper number of sibling parameters. | |
3836 | ||
3837 | * Make lookups of the template parameter return a reference | |
3838 | to the fixed-up index. No lookup should return references | |
3839 | to the former index anymore. | |
3840 | ||
3841 | * Substitute the template parms that got fixed up so far | |
3842 | ||
3843 | * into the type of PARM. */ | |
3844 | ||
3845 | tree index = DECL_INITIAL (parm); | |
3846 | ||
3847 | /* PUSHED_DECL is the decl added to the symbol table with | |
3848 | the name of the parameter. E,g: | |
3849 | ||
3850 | template<class T, T u> //#0 | |
3851 | auto my_function(T t) -> decltype(u); //#1 | |
3852 | ||
3853 | Here, when looking up u at //#1, we get the decl of u | |
3854 | resulting from the declaration in #0. This is what | |
3855 | PUSHED_DECL is. We need to replace the reference to the | |
3856 | old TEMPLATE_PARM_INDEX carried by PUSHED_DECL by the | |
3857 | fixed-up TEMPLATE_PARM_INDEX. */ | |
3858 | tree pushed_decl = TEMPLATE_PARM_DECL (index); | |
3859 | ||
3860 | /* Let's fix up the TEMPLATE_PARM_INDEX then. Note that we must | |
3861 | fixup the type of PUSHED_DECL as well and luckily | |
3862 | fixup_template_parm_index does it for us too. */ | |
3863 | tree fixed_up_index = | |
3864 | fixup_template_parm_index (index, arglist, num_parms); | |
3865 | ||
3866 | DECL_INITIAL (pushed_decl) = DECL_INITIAL (parm) = fixed_up_index; | |
3867 | ||
3868 | /* Add this fixed up PARM to the template parms we've fixed | |
3869 | up so far and use that to substitute the fixed-up | |
3870 | template parms into the type of PARM. */ | |
3871 | TREE_VEC_ELT (fixedup_args, idx) = | |
3872 | template_parm_to_arg (parm_desc); | |
3873 | TREE_TYPE (parm) = tsubst (TREE_TYPE (parm), arglist, | |
3874 | tf_none, NULL_TREE); | |
3875 | } | |
3876 | ||
3877 | TREE_PURPOSE (parm_desc) = | |
3878 | tsubst_template_arg (TREE_PURPOSE (parm_desc), | |
3879 | arglist, tf_none, parm); | |
3880 | ||
3881 | pop_deferring_access_checks (); | |
3882 | } | |
3883 | ||
3884 | /* Walk current the template parms and properly compute the canonical | |
3885 | types of the dependent types created during | |
3886 | cp_parser_template_parameter_list. */ | |
3887 | ||
3888 | static void | |
3889 | fixup_template_parms (void) | |
3890 | { | |
3891 | tree arglist; | |
3892 | tree parameter_vec; | |
3893 | tree fixedup_args; | |
3894 | int i, num_parms; | |
3895 | ||
3896 | parameter_vec = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
3897 | if (parameter_vec == NULL_TREE) | |
3898 | return; | |
3899 | ||
3900 | num_parms = TREE_VEC_LENGTH (parameter_vec); | |
3901 | ||
3902 | /* This vector contains the current innermost template parms that | |
3903 | have been fixed up so far. The form of FIXEDUP_ARGS is suitable | |
3904 | to be passed to tsubst* functions as their ARGS argument. */ | |
3905 | fixedup_args = make_tree_vec (num_parms); | |
3906 | ||
3907 | /* This vector contains the full set of template parms in a form | |
3908 | suitable to be passed to substs functions as their ARGS | |
3909 | argument. */ | |
3910 | arglist = current_template_args (); | |
3911 | arglist = add_outermost_template_args (arglist, fixedup_args); | |
3912 | ||
3913 | fixedup_args = INNERMOST_TEMPLATE_ARGS (arglist); | |
3914 | ||
3915 | /* Let's do the proper fixup now. */ | |
3916 | for (i = 0; i < num_parms; ++i) | |
3917 | fixup_template_parm (TREE_VEC_ELT (parameter_vec, i), | |
3918 | i, num_parms, arglist); | |
3919 | } | |
3920 | ||
5566b478 MS |
3921 | /* end_template_decl is called after a template declaration is seen. */ |
3922 | ||
8d08fdba | 3923 | void |
3a978d72 | 3924 | end_template_decl (void) |
8d08fdba | 3925 | { |
386b8a85 JM |
3926 | reset_specialization (); |
3927 | ||
5156628f | 3928 | if (! processing_template_decl) |
73aad9b9 JM |
3929 | return; |
3930 | ||
5566b478 | 3931 | /* This matches the pushlevel in begin_template_parm_list. */ |
74b846e0 | 3932 | finish_scope (); |
8d08fdba | 3933 | |
5566b478 MS |
3934 | --processing_template_decl; |
3935 | current_template_parms = TREE_CHAIN (current_template_parms); | |
5566b478 | 3936 | } |
8d08fdba | 3937 | |
6f1abb06 DS |
3938 | /* Takes a TREE_LIST representing a template parameter and convert it |
3939 | into an argument suitable to be passed to the type substitution | |
3940 | functions. Note that If the TREE_LIST contains an error_mark | |
3941 | node, the returned argument is error_mark_node. */ | |
3942 | ||
3943 | static tree | |
3944 | template_parm_to_arg (tree t) | |
3945 | { | |
3946 | ||
3947 | if (t == NULL_TREE | |
3948 | || TREE_CODE (t) != TREE_LIST) | |
3949 | return t; | |
3950 | ||
3951 | if (error_operand_p (TREE_VALUE (t))) | |
3952 | return error_mark_node; | |
3953 | ||
3954 | t = TREE_VALUE (t); | |
3955 | ||
3956 | if (TREE_CODE (t) == TYPE_DECL | |
3957 | || TREE_CODE (t) == TEMPLATE_DECL) | |
3958 | { | |
3959 | t = TREE_TYPE (t); | |
3960 | ||
3961 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
3962 | { | |
3963 | /* Turn this argument into a TYPE_ARGUMENT_PACK | |
3964 | with a single element, which expands T. */ | |
3965 | tree vec = make_tree_vec (1); | |
3966 | #ifdef ENABLE_CHECKING | |
3967 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT | |
3968 | (vec, TREE_VEC_LENGTH (vec)); | |
3969 | #endif | |
3970 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3971 | ||
3972 | t = cxx_make_type (TYPE_ARGUMENT_PACK); | |
3973 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3974 | } | |
3975 | } | |
3976 | else | |
3977 | { | |
3978 | t = DECL_INITIAL (t); | |
3979 | ||
3980 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
3981 | { | |
3982 | /* Turn this argument into a NONTYPE_ARGUMENT_PACK | |
3983 | with a single element, which expands T. */ | |
3984 | tree vec = make_tree_vec (1); | |
3985 | tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t)); | |
3986 | #ifdef ENABLE_CHECKING | |
3987 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT | |
3988 | (vec, TREE_VEC_LENGTH (vec)); | |
3989 | #endif | |
3990 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3991 | ||
3992 | t = make_node (NONTYPE_ARGUMENT_PACK); | |
3993 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3994 | TREE_TYPE (t) = type; | |
3995 | } | |
3996 | } | |
3997 | return t; | |
3998 | } | |
3999 | ||
03c17ccd MM |
4000 | /* Within the declaration of a template, return all levels of template |
4001 | parameters that apply. The template parameters are represented as | |
4002 | a TREE_VEC, in the form documented in cp-tree.h for template | |
4003 | arguments. */ | |
9a3b49ac | 4004 | |
b5791fdc | 4005 | static tree |
3a978d72 | 4006 | current_template_args (void) |
5566b478 | 4007 | { |
36a117a5 | 4008 | tree header; |
b370501f | 4009 | tree args = NULL_TREE; |
36a117a5 | 4010 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
4011 | int l = length; |
4012 | ||
36a117a5 MM |
4013 | /* If there is only one level of template parameters, we do not |
4014 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
4015 | TREE_VEC containing the arguments. */ | |
4016 | if (length > 1) | |
4017 | args = make_tree_vec (length); | |
4018 | ||
4019 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 4020 | { |
5566b478 | 4021 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
4022 | int i; |
4023 | ||
5566b478 | 4024 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 4025 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
6f1abb06 | 4026 | TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i)); |
36a117a5 | 4027 | |
1a048f82 DS |
4028 | #ifdef ENABLE_CHECKING |
4029 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a)); | |
4030 | #endif | |
4031 | ||
36a117a5 MM |
4032 | if (length > 1) |
4033 | TREE_VEC_ELT (args, --l) = a; | |
4034 | else | |
4035 | args = a; | |
8d08fdba MS |
4036 | } |
4037 | ||
6f1abb06 DS |
4038 | if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE) |
4039 | /* This can happen for template parms of a template template | |
4040 | parameter, e.g: | |
4041 | ||
4042 | template<template<class T, class U> class TT> struct S; | |
4043 | ||
4044 | Consider the level of the parms of TT; T and U both have | |
4045 | level 2; TT has no template parm of level 1. So in this case | |
4046 | the first element of full_template_args is NULL_TREE. If we | |
4047 | leave it like this TMPL_ARG_DEPTH on args returns 1 instead | |
4048 | of 2. This will make tsubst wrongly consider that T and U | |
4049 | have level 1. Instead, let's create a dummy vector as the | |
4050 | first element of full_template_args so that TMPL_ARG_DEPTH | |
4051 | returns the correct depth for args. */ | |
4052 | TREE_VEC_ELT (args, 0) = make_tree_vec (1); | |
9a3b49ac MS |
4053 | return args; |
4054 | } | |
75650646 | 4055 | |
04daa92b JM |
4056 | /* Update the declared TYPE by doing any lookups which were thought to be |
4057 | dependent, but are not now that we know the SCOPE of the declarator. */ | |
4058 | ||
4059 | tree | |
4060 | maybe_update_decl_type (tree orig_type, tree scope) | |
4061 | { | |
4062 | tree type = orig_type; | |
4063 | ||
4064 | if (type == NULL_TREE) | |
4065 | return type; | |
4066 | ||
4067 | if (TREE_CODE (orig_type) == TYPE_DECL) | |
4068 | type = TREE_TYPE (type); | |
4069 | ||
4070 | if (scope && TYPE_P (scope) && dependent_type_p (scope) | |
4071 | && dependent_type_p (type) | |
4072 | /* Don't bother building up the args in this case. */ | |
4073 | && TREE_CODE (type) != TEMPLATE_TYPE_PARM) | |
4074 | { | |
4075 | /* tsubst in the args corresponding to the template parameters, | |
4076 | including auto if present. Most things will be unchanged, but | |
4077 | make_typename_type and tsubst_qualified_id will resolve | |
4078 | TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */ | |
4079 | tree args = current_template_args (); | |
4080 | tree auto_node = type_uses_auto (type); | |
d5eebac0 | 4081 | tree pushed; |
04daa92b JM |
4082 | if (auto_node) |
4083 | { | |
4084 | tree auto_vec = make_tree_vec (1); | |
4085 | TREE_VEC_ELT (auto_vec, 0) = auto_node; | |
4086 | args = add_to_template_args (args, auto_vec); | |
4087 | } | |
d5eebac0 | 4088 | pushed = push_scope (scope); |
04daa92b | 4089 | type = tsubst (type, args, tf_warning_or_error, NULL_TREE); |
d5eebac0 JM |
4090 | if (pushed) |
4091 | pop_scope (scope); | |
04daa92b JM |
4092 | } |
4093 | ||
4094 | if (type == error_mark_node) | |
4095 | return orig_type; | |
4096 | ||
4097 | if (TREE_CODE (orig_type) == TYPE_DECL) | |
4098 | { | |
4099 | if (same_type_p (type, TREE_TYPE (orig_type))) | |
4100 | type = orig_type; | |
4101 | else | |
4102 | type = TYPE_NAME (type); | |
4103 | } | |
4104 | return type; | |
4105 | } | |
4106 | ||
e1467ff2 | 4107 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
c7222c02 | 4108 | template PARMS. If MEMBER_TEMPLATE_P is true, the new template is |
ee81147e | 4109 | a member template. Used by push_template_decl below. */ |
e1467ff2 | 4110 | |
75650646 | 4111 | static tree |
c7222c02 | 4112 | build_template_decl (tree decl, tree parms, bool member_template_p) |
75650646 MM |
4113 | { |
4114 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
4115 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
4116 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
c7222c02 | 4117 | DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; |
75650646 MM |
4118 | |
4119 | return tmpl; | |
4120 | } | |
4121 | ||
050367a3 MM |
4122 | struct template_parm_data |
4123 | { | |
6c30752f MM |
4124 | /* The level of the template parameters we are currently |
4125 | processing. */ | |
050367a3 | 4126 | int level; |
6c30752f MM |
4127 | |
4128 | /* The index of the specialization argument we are currently | |
4129 | processing. */ | |
4130 | int current_arg; | |
4131 | ||
4132 | /* An array whose size is the number of template parameters. The | |
838dfd8a | 4133 | elements are nonzero if the parameter has been used in any one |
6c30752f | 4134 | of the arguments processed so far. */ |
050367a3 | 4135 | int* parms; |
6c30752f MM |
4136 | |
4137 | /* An array whose size is the number of template arguments. The | |
838dfd8a | 4138 | elements are nonzero if the argument makes use of template |
6c30752f MM |
4139 | parameters of this level. */ |
4140 | int* arg_uses_template_parms; | |
050367a3 MM |
4141 | }; |
4142 | ||
4143 | /* Subroutine of push_template_decl used to see if each template | |
4144 | parameter in a partial specialization is used in the explicit | |
4145 | argument list. If T is of the LEVEL given in DATA (which is | |
4146 | treated as a template_parm_data*), then DATA->PARMS is marked | |
4147 | appropriately. */ | |
4148 | ||
4149 | static int | |
3a978d72 | 4150 | mark_template_parm (tree t, void* data) |
050367a3 MM |
4151 | { |
4152 | int level; | |
4153 | int idx; | |
4154 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
4155 | ||
4156 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
4157 | { | |
4158 | level = TEMPLATE_PARM_LEVEL (t); | |
4159 | idx = TEMPLATE_PARM_IDX (t); | |
4160 | } | |
4161 | else | |
4162 | { | |
4163 | level = TEMPLATE_TYPE_LEVEL (t); | |
4164 | idx = TEMPLATE_TYPE_IDX (t); | |
4165 | } | |
4166 | ||
4167 | if (level == tpd->level) | |
6c30752f MM |
4168 | { |
4169 | tpd->parms[idx] = 1; | |
4170 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
4171 | } | |
050367a3 MM |
4172 | |
4173 | /* Return zero so that for_each_template_parm will continue the | |
4174 | traversal of the tree; we want to mark *every* template parm. */ | |
4175 | return 0; | |
4176 | } | |
4177 | ||
6c30752f MM |
4178 | /* Process the partial specialization DECL. */ |
4179 | ||
e9659ab0 | 4180 | static tree |
3a978d72 | 4181 | process_partial_specialization (tree decl) |
6c30752f MM |
4182 | { |
4183 | tree type = TREE_TYPE (decl); | |
4184 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
4185 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
f9a7ae04 | 4186 | tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); |
6c30752f | 4187 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); |
b17c40be | 4188 | tree inner_parms; |
ebc258f1 | 4189 | tree inst; |
6c30752f | 4190 | int nargs = TREE_VEC_LENGTH (inner_args); |
b17c40be | 4191 | int ntparms; |
6c30752f | 4192 | int i; |
aac73a6d | 4193 | bool did_error_intro = false; |
6c30752f MM |
4194 | struct template_parm_data tpd; |
4195 | struct template_parm_data tpd2; | |
4196 | ||
b17c40be PC |
4197 | gcc_assert (current_template_parms); |
4198 | ||
4199 | inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
4200 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
4201 | ||
6c30752f MM |
4202 | /* We check that each of the template parameters given in the |
4203 | partial specialization is used in the argument list to the | |
4204 | specialization. For example: | |
4205 | ||
4206 | template <class T> struct S; | |
4207 | template <class T> struct S<T*>; | |
4208 | ||
4209 | The second declaration is OK because `T*' uses the template | |
4210 | parameter T, whereas | |
4211 | ||
4212 | template <class T> struct S<int>; | |
4213 | ||
4214 | is no good. Even trickier is: | |
4215 | ||
4216 | template <class T> | |
4217 | struct S1 | |
4218 | { | |
4219 | template <class U> | |
4220 | struct S2; | |
4221 | template <class U> | |
4222 | struct S2<T>; | |
4223 | }; | |
4224 | ||
0e339752 | 4225 | The S2<T> declaration is actually invalid; it is a |
c8094d83 | 4226 | full-specialization. Of course, |
6c30752f MM |
4227 | |
4228 | template <class U> | |
4229 | struct S2<T (*)(U)>; | |
4230 | ||
4231 | or some such would have been OK. */ | |
4232 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
86b8fed1 | 4233 | tpd.parms = XALLOCAVEC (int, ntparms); |
fad205ff | 4234 | memset (tpd.parms, 0, sizeof (int) * ntparms); |
6c30752f | 4235 | |
86b8fed1 | 4236 | tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs); |
fad205ff | 4237 | memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); |
6c30752f MM |
4238 | for (i = 0; i < nargs; ++i) |
4239 | { | |
4240 | tpd.current_arg = i; | |
4241 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
4242 | &mark_template_parm, | |
4f2c9d7e | 4243 | &tpd, |
c095a4f8 DG |
4244 | NULL, |
4245 | /*include_nondeduced_p=*/false); | |
6c30752f MM |
4246 | } |
4247 | for (i = 0; i < ntparms; ++i) | |
4248 | if (tpd.parms[i] == 0) | |
4249 | { | |
4250 | /* One of the template parms was not used in the | |
f9a7ae04 | 4251 | specialization. */ |
6c30752f MM |
4252 | if (!did_error_intro) |
4253 | { | |
33bd39a2 | 4254 | error ("template parameters not used in partial specialization:"); |
aac73a6d | 4255 | did_error_intro = true; |
6c30752f MM |
4256 | } |
4257 | ||
0f51ccfc | 4258 | error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
6c30752f MM |
4259 | } |
4260 | ||
aac73a6d PC |
4261 | if (did_error_intro) |
4262 | return error_mark_node; | |
4263 | ||
6c30752f MM |
4264 | /* [temp.class.spec] |
4265 | ||
4266 | The argument list of the specialization shall not be identical to | |
4267 | the implicit argument list of the primary template. */ | |
c8094d83 MS |
4268 | if (comp_template_args |
4269 | (inner_args, | |
f9a7ae04 MM |
4270 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE |
4271 | (maintmpl))))) | |
0f51ccfc | 4272 | error ("partial specialization %qT does not specialize any template arguments", type); |
6c30752f MM |
4273 | |
4274 | /* [temp.class.spec] | |
4275 | ||
4276 | A partially specialized non-type argument expression shall not | |
4277 | involve template parameters of the partial specialization except | |
4278 | when the argument expression is a simple identifier. | |
4279 | ||
4280 | The type of a template parameter corresponding to a specialized | |
4281 | non-type argument shall not be dependent on a parameter of the | |
5d80a306 DG |
4282 | specialization. |
4283 | ||
4284 | Also, we verify that pack expansions only occur at the | |
4285 | end of the argument list. */ | |
50bc768d | 4286 | gcc_assert (nargs == DECL_NTPARMS (maintmpl)); |
6c30752f MM |
4287 | tpd2.parms = 0; |
4288 | for (i = 0; i < nargs; ++i) | |
4289 | { | |
5d80a306 | 4290 | tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i)); |
6c30752f | 4291 | tree arg = TREE_VEC_ELT (inner_args, i); |
5d80a306 DG |
4292 | tree packed_args = NULL_TREE; |
4293 | int j, len = 1; | |
6c30752f | 4294 | |
5d80a306 DG |
4295 | if (ARGUMENT_PACK_P (arg)) |
4296 | { | |
4297 | /* Extract the arguments from the argument pack. We'll be | |
4298 | iterating over these in the following loop. */ | |
4299 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
4300 | len = TREE_VEC_LENGTH (packed_args); | |
4301 | } | |
4302 | ||
4303 | for (j = 0; j < len; j++) | |
4304 | { | |
4305 | if (packed_args) | |
4306 | /* Get the Jth argument in the parameter pack. */ | |
4307 | arg = TREE_VEC_ELT (packed_args, j); | |
4308 | ||
4309 | if (PACK_EXPANSION_P (arg)) | |
4310 | { | |
4311 | /* Pack expansions must come at the end of the | |
4312 | argument list. */ | |
4313 | if ((packed_args && j < len - 1) | |
4314 | || (!packed_args && i < nargs - 1)) | |
4315 | { | |
4316 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
2282976b SM |
4317 | error ("parameter pack argument %qE must be at the " |
4318 | "end of the template argument list", arg); | |
5d80a306 | 4319 | else |
2282976b SM |
4320 | error ("parameter pack argument %qT must be at the " |
4321 | "end of the template argument list", arg); | |
5d80a306 DG |
4322 | } |
4323 | } | |
4324 | ||
4325 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
4326 | /* We only care about the pattern. */ | |
4327 | arg = PACK_EXPANSION_PATTERN (arg); | |
4328 | ||
4329 | if (/* These first two lines are the `non-type' bit. */ | |
4330 | !TYPE_P (arg) | |
4331 | && TREE_CODE (arg) != TEMPLATE_DECL | |
4332 | /* This next line is the `argument expression is not just a | |
4333 | simple identifier' condition and also the `specialized | |
4334 | non-type argument' bit. */ | |
4335 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
4336 | { | |
4337 | if ((!packed_args && tpd.arg_uses_template_parms[i]) | |
4338 | || (packed_args && uses_template_parms (arg))) | |
4339 | error ("template argument %qE involves template parameter(s)", | |
4340 | arg); | |
4341 | else | |
4342 | { | |
4343 | /* Look at the corresponding template parameter, | |
4344 | marking which template parameters its type depends | |
4345 | upon. */ | |
4346 | tree type = TREE_TYPE (parm); | |
4347 | ||
4348 | if (!tpd2.parms) | |
4349 | { | |
4350 | /* We haven't yet initialized TPD2. Do so now. */ | |
86b8fed1 | 4351 | tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs); |
5d80a306 DG |
4352 | /* The number of parameters here is the number in the |
4353 | main template, which, as checked in the assertion | |
4354 | above, is NARGS. */ | |
86b8fed1 | 4355 | tpd2.parms = XALLOCAVEC (int, nargs); |
5d80a306 DG |
4356 | tpd2.level = |
4357 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
4358 | } | |
4359 | ||
4360 | /* Mark the template parameters. But this time, we're | |
4361 | looking for the template parameters of the main | |
4362 | template, not in the specialization. */ | |
4363 | tpd2.current_arg = i; | |
4364 | tpd2.arg_uses_template_parms[i] = 0; | |
4365 | memset (tpd2.parms, 0, sizeof (int) * nargs); | |
4366 | for_each_template_parm (type, | |
4367 | &mark_template_parm, | |
4368 | &tpd2, | |
c095a4f8 DG |
4369 | NULL, |
4370 | /*include_nondeduced_p=*/false); | |
5d80a306 DG |
4371 | |
4372 | if (tpd2.arg_uses_template_parms [i]) | |
4373 | { | |
4374 | /* The type depended on some template parameters. | |
4375 | If they are fully specialized in the | |
4376 | specialization, that's OK. */ | |
4377 | int j; | |
83ff92fb | 4378 | int count = 0; |
5d80a306 DG |
4379 | for (j = 0; j < nargs; ++j) |
4380 | if (tpd2.parms[j] != 0 | |
4381 | && tpd.arg_uses_template_parms [j]) | |
83ff92fb MP |
4382 | ++count; |
4383 | if (count != 0) | |
4384 | error_n (input_location, count, | |
4385 | "type %qT of template argument %qE depends " | |
4386 | "on a template parameter", | |
4387 | "type %qT of template argument %qE depends " | |
4388 | "on template parameters", | |
4389 | type, | |
4390 | arg); | |
5d80a306 DG |
4391 | } |
4392 | } | |
4393 | } | |
4394 | } | |
6c30752f MM |
4395 | } |
4396 | ||
2b110bfc JM |
4397 | /* We should only get here once. */ |
4398 | gcc_assert (!COMPLETE_TYPE_P (type)); | |
6c30752f | 4399 | |
d8b64f80 | 4400 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) |
916b63c3 | 4401 | = tree_cons (specargs, inner_parms, |
b1d7b1c0 | 4402 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); |
6c30752f | 4403 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
ebc258f1 JM |
4404 | |
4405 | for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst; | |
4406 | inst = TREE_CHAIN (inst)) | |
4407 | { | |
4408 | tree inst_type = TREE_VALUE (inst); | |
4409 | if (COMPLETE_TYPE_P (inst_type) | |
4410 | && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type)) | |
4411 | { | |
4412 | tree spec = most_specialized_class (inst_type, maintmpl, tf_none); | |
4413 | if (spec && TREE_TYPE (spec) == type) | |
4414 | permerror (input_location, | |
4415 | "partial specialization of %qT after instantiation " | |
4416 | "of %qT", type, inst_type); | |
4417 | } | |
4418 | } | |
4419 | ||
6c30752f MM |
4420 | return decl; |
4421 | } | |
4422 | ||
85d85234 DG |
4423 | /* Check that a template declaration's use of default arguments and |
4424 | parameter packs is not invalid. Here, PARMS are the template | |
4425 | parameters. IS_PRIMARY is nonzero if DECL is the thing declared by | |
4426 | a primary template. IS_PARTIAL is nonzero if DECL is a partial | |
4427 | specialization. | |
9b7dd5e8 | 4428 | |
6ba89f8e | 4429 | |
9b7dd5e8 DG |
4430 | IS_FRIEND_DECL is nonzero if DECL is a friend function template |
4431 | declaration (but not a definition); 1 indicates a declaration, 2 | |
4432 | indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are | |
4433 | emitted for extraneous default arguments. | |
4434 | ||
4435 | Returns TRUE if there were no errors found, FALSE otherwise. */ | |
4436 | ||
4437 | bool | |
4438 | check_default_tmpl_args (tree decl, tree parms, int is_primary, | |
4439 | int is_partial, int is_friend_decl) | |
6ba89f8e | 4440 | { |
d8e178a0 | 4441 | const char *msg; |
66191c20 MM |
4442 | int last_level_to_check; |
4443 | tree parm_level; | |
9b7dd5e8 | 4444 | bool no_errors = true; |
6ba89f8e | 4445 | |
c8094d83 | 4446 | /* [temp.param] |
6ba89f8e MM |
4447 | |
4448 | A default template-argument shall not be specified in a | |
4449 | function template declaration or a function template definition, nor | |
4450 | in the template-parameter-list of the definition of a member of a | |
4451 | class template. */ | |
4452 | ||
4f1c5b7d | 4453 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
e0942dcd MM |
4454 | /* You can't have a function template declaration in a local |
4455 | scope, nor you can you define a member of a class template in a | |
4456 | local scope. */ | |
9b7dd5e8 | 4457 | return true; |
e0942dcd | 4458 | |
6ba89f8e MM |
4459 | if (current_class_type |
4460 | && !TYPE_BEING_DEFINED (current_class_type) | |
3d7de1fa | 4461 | && DECL_LANG_SPECIFIC (decl) |
b97e8a14 | 4462 | && DECL_DECLARES_FUNCTION_P (decl) |
5937a6f9 MM |
4463 | /* If this is either a friend defined in the scope of the class |
4464 | or a member function. */ | |
6df5158a NS |
4465 | && (DECL_FUNCTION_MEMBER_P (decl) |
4466 | ? same_type_p (DECL_CONTEXT (decl), current_class_type) | |
4467 | : DECL_FRIEND_CONTEXT (decl) | |
4468 | ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) | |
4469 | : false) | |
5937a6f9 MM |
4470 | /* And, if it was a member function, it really was defined in |
4471 | the scope of the class. */ | |
6df5158a NS |
4472 | && (!DECL_FUNCTION_MEMBER_P (decl) |
4473 | || DECL_INITIALIZED_IN_CLASS_P (decl))) | |
6ba89f8e | 4474 | /* We already checked these parameters when the template was |
5937a6f9 MM |
4475 | declared, so there's no need to do it again now. This function |
4476 | was defined in class scope, but we're processing it's body now | |
4477 | that the class is complete. */ | |
9b7dd5e8 | 4478 | return true; |
c8094d83 | 4479 | |
9b7dd5e8 DG |
4480 | /* Core issue 226 (C++0x only): the following only applies to class |
4481 | templates. */ | |
c1ae8be5 | 4482 | if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL) |
66191c20 | 4483 | { |
9b7dd5e8 | 4484 | /* [temp.param] |
66191c20 | 4485 | |
9b7dd5e8 DG |
4486 | If a template-parameter has a default template-argument, all |
4487 | subsequent template-parameters shall have a default | |
4488 | template-argument supplied. */ | |
4489 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
4490 | { | |
4491 | tree inner_parms = TREE_VALUE (parm_level); | |
4492 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
4493 | int seen_def_arg_p = 0; | |
4494 | int i; | |
42b304f1 | 4495 | |
9b7dd5e8 DG |
4496 | for (i = 0; i < ntparms; ++i) |
4497 | { | |
4498 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
42b304f1 | 4499 | |
9b7dd5e8 DG |
4500 | if (parm == error_mark_node) |
4501 | continue; | |
4502 | ||
4503 | if (TREE_PURPOSE (parm)) | |
4504 | seen_def_arg_p = 1; | |
ffaf1e05 JM |
4505 | else if (seen_def_arg_p |
4506 | && !template_parameter_pack_p (TREE_VALUE (parm))) | |
9b7dd5e8 DG |
4507 | { |
4508 | error ("no default argument for %qD", TREE_VALUE (parm)); | |
4509 | /* For better subsequent error-recovery, we indicate that | |
4510 | there should have been a default argument. */ | |
4511 | TREE_PURPOSE (parm) = error_mark_node; | |
4512 | no_errors = false; | |
4513 | } | |
85d85234 DG |
4514 | else if (is_primary |
4515 | && !is_partial | |
4516 | && !is_friend_decl | |
ffaf1e05 JM |
4517 | /* Don't complain about an enclosing partial |
4518 | specialization. */ | |
4519 | && parm_level == parms | |
85d85234 DG |
4520 | && TREE_CODE (decl) == TYPE_DECL |
4521 | && i < ntparms - 1 | |
4522 | && template_parameter_pack_p (TREE_VALUE (parm))) | |
4523 | { | |
4524 | /* A primary class template can only have one | |
4525 | parameter pack, at the end of the template | |
4526 | parameter list. */ | |
4527 | ||
4528 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL) | |
4529 | error ("parameter pack %qE must be at the end of the" | |
4530 | " template parameter list", TREE_VALUE (parm)); | |
4531 | else | |
4532 | error ("parameter pack %qT must be at the end of the" | |
4533 | " template parameter list", | |
4534 | TREE_TYPE (TREE_VALUE (parm))); | |
4535 | ||
4536 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i)) | |
4537 | = error_mark_node; | |
4538 | no_errors = false; | |
4539 | } | |
9b7dd5e8 DG |
4540 | } |
4541 | } | |
66191c20 MM |
4542 | } |
4543 | ||
c1ae8be5 | 4544 | if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL) |
9b7dd5e8 DG |
4545 | || is_partial |
4546 | || !is_primary | |
4547 | || is_friend_decl) | |
6ba89f8e MM |
4548 | /* For an ordinary class template, default template arguments are |
4549 | allowed at the innermost level, e.g.: | |
0cbd7506 | 4550 | template <class T = int> |
6ba89f8e MM |
4551 | struct S {}; |
4552 | but, in a partial specialization, they're not allowed even | |
4553 | there, as we have in [temp.class.spec]: | |
c8094d83 | 4554 | |
6ba89f8e | 4555 | The template parameter list of a specialization shall not |
c8094d83 | 4556 | contain default template argument values. |
6ba89f8e | 4557 | |
9b7dd5e8 DG |
4558 | So, for a partial specialization, or for a function template |
4559 | (in C++98/C++03), we look at all of them. */ | |
6ba89f8e MM |
4560 | ; |
4561 | else | |
4562 | /* But, for a primary class template that is not a partial | |
4563 | specialization we look at all template parameters except the | |
4564 | innermost ones. */ | |
4565 | parms = TREE_CHAIN (parms); | |
4566 | ||
4567 | /* Figure out what error message to issue. */ | |
9b7dd5e8 | 4568 | if (is_friend_decl == 2) |
f25a2b52 SZ |
4569 | msg = G_("default template arguments may not be used in function template " |
4570 | "friend re-declaration"); | |
9b7dd5e8 | 4571 | else if (is_friend_decl) |
f25a2b52 SZ |
4572 | msg = G_("default template arguments may not be used in function template " |
4573 | "friend declarations"); | |
c1ae8be5 | 4574 | else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98)) |
f25a2b52 SZ |
4575 | msg = G_("default template arguments may not be used in function templates " |
4576 | "without -std=c++0x or -std=gnu++0x"); | |
6ba89f8e | 4577 | else if (is_partial) |
f25a2b52 SZ |
4578 | msg = G_("default template arguments may not be used in " |
4579 | "partial specializations"); | |
6ba89f8e | 4580 | else |
f25a2b52 | 4581 | msg = G_("default argument for template parameter for class enclosing %qD"); |
6ba89f8e MM |
4582 | |
4583 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
4584 | /* If we're inside a class definition, there's no need to | |
104bf76a | 4585 | examine the parameters to the class itself. On the one |
6ba89f8e | 4586 | hand, they will be checked when the class is defined, and, |
0e339752 | 4587 | on the other, default arguments are valid in things like: |
0cbd7506 MS |
4588 | template <class T = double> |
4589 | struct S { template <class U> void f(U); }; | |
6ba89f8e MM |
4590 | Here the default argument for `S' has no bearing on the |
4591 | declaration of `f'. */ | |
4592 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
4593 | else | |
4594 | /* Check everything. */ | |
4595 | last_level_to_check = 0; | |
4596 | ||
c8094d83 MS |
4597 | for (parm_level = parms; |
4598 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
66191c20 | 4599 | parm_level = TREE_CHAIN (parm_level)) |
6ba89f8e | 4600 | { |
66191c20 MM |
4601 | tree inner_parms = TREE_VALUE (parm_level); |
4602 | int i; | |
4603 | int ntparms; | |
6ba89f8e MM |
4604 | |
4605 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
c8094d83 | 4606 | for (i = 0; i < ntparms; ++i) |
42b304f1 LM |
4607 | { |
4608 | if (TREE_VEC_ELT (inner_parms, i) == error_mark_node) | |
4609 | continue; | |
6ba89f8e | 4610 | |
42b304f1 LM |
4611 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) |
4612 | { | |
4613 | if (msg) | |
4614 | { | |
9b7dd5e8 DG |
4615 | no_errors = false; |
4616 | if (is_friend_decl == 2) | |
4617 | return no_errors; | |
4618 | ||
42b304f1 LM |
4619 | error (msg, decl); |
4620 | msg = 0; | |
4621 | } | |
4622 | ||
4623 | /* Clear out the default argument so that we are not | |
4624 | confused later. */ | |
4625 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
4626 | } | |
4627 | } | |
6ba89f8e MM |
4628 | |
4629 | /* At this point, if we're still interested in issuing messages, | |
4630 | they must apply to classes surrounding the object declared. */ | |
4631 | if (msg) | |
f25a2b52 SZ |
4632 | msg = G_("default argument for template parameter for class " |
4633 | "enclosing %qD"); | |
6ba89f8e | 4634 | } |
9b7dd5e8 DG |
4635 | |
4636 | return no_errors; | |
6ba89f8e MM |
4637 | } |
4638 | ||
5dd236e2 NS |
4639 | /* Worker for push_template_decl_real, called via |
4640 | for_each_template_parm. DATA is really an int, indicating the | |
4641 | level of the parameters we are interested in. If T is a template | |
838dfd8a | 4642 | parameter of that level, return nonzero. */ |
5dd236e2 NS |
4643 | |
4644 | static int | |
3a978d72 | 4645 | template_parm_this_level_p (tree t, void* data) |
5dd236e2 | 4646 | { |
6e04241f | 4647 | int this_level = *(int *)data; |
5dd236e2 NS |
4648 | int level; |
4649 | ||
4650 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
4651 | level = TEMPLATE_PARM_LEVEL (t); | |
4652 | else | |
4653 | level = TEMPLATE_TYPE_LEVEL (t); | |
4654 | return level == this_level; | |
4655 | } | |
4656 | ||
3ac3d9ea | 4657 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
4658 | parameters given by current_template_args, or reuses a |
4659 | previously existing one, if appropriate. Returns the DECL, or an | |
c8094d83 | 4660 | equivalent one, if it is replaced via a call to duplicate_decls. |
6757edfe | 4661 | |
d63d5d0c | 4662 | If IS_FRIEND is true, DECL is a friend declaration. */ |
3ac3d9ea MM |
4663 | |
4664 | tree | |
d63d5d0c | 4665 | push_template_decl_real (tree decl, bool is_friend) |
9a3b49ac MS |
4666 | { |
4667 | tree tmpl; | |
f84b4be9 | 4668 | tree args; |
9a3b49ac | 4669 | tree info; |
f84b4be9 JM |
4670 | tree ctx; |
4671 | int primary; | |
6ba89f8e | 4672 | int is_partial; |
cfe507be | 4673 | int new_template_p = 0; |
c7222c02 MM |
4674 | /* True if the template is a member template, in the sense of |
4675 | [temp.mem]. */ | |
4676 | bool member_template_p = false; | |
6ba89f8e | 4677 | |
b17c40be PC |
4678 | if (decl == error_mark_node || !current_template_parms) |
4679 | return error_mark_node; | |
c0694c4b | 4680 | |
6ba89f8e | 4681 | /* See if this is a partial specialization. */ |
9188c363 | 4682 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
6ba89f8e | 4683 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
370af2d5 | 4684 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); |
6757edfe | 4685 | |
d63d5d0c ILT |
4686 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)) |
4687 | is_friend = true; | |
f84b4be9 JM |
4688 | |
4689 | if (is_friend) | |
4690 | /* For a friend, we want the context of the friend function, not | |
4691 | the type of which it is a friend. */ | |
725214ac | 4692 | ctx = CP_DECL_CONTEXT (decl); |
4f1c5b7d MM |
4693 | else if (CP_DECL_CONTEXT (decl) |
4694 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
4695 | /* In the case of a virtual function, we want the class in which |
4696 | it is defined. */ | |
4f1c5b7d | 4697 | ctx = CP_DECL_CONTEXT (decl); |
f84b4be9 | 4698 | else |
dc957d14 | 4699 | /* Otherwise, if we're currently defining some class, the DECL |
f84b4be9 | 4700 | is assumed to be a member of the class. */ |
9188c363 | 4701 | ctx = current_scope (); |
f84b4be9 | 4702 | |
2c73f9f5 ML |
4703 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
4704 | ctx = NULL_TREE; | |
4705 | ||
4706 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 4707 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 4708 | |
6ba89f8e | 4709 | /* See if this is a primary template. */ |
c9cbfca6 JM |
4710 | if (is_friend && ctx) |
4711 | /* A friend template that specifies a class context, i.e. | |
4712 | template <typename T> friend void A<T>::f(); | |
4713 | is not primary. */ | |
4714 | primary = 0; | |
4715 | else | |
4716 | primary = template_parm_scope_p (); | |
9a3b49ac | 4717 | |
83566abf JM |
4718 | if (primary) |
4719 | { | |
c7222c02 MM |
4720 | if (DECL_CLASS_SCOPE_P (decl)) |
4721 | member_template_p = true; | |
2f1b1731 MM |
4722 | if (TREE_CODE (decl) == TYPE_DECL |
4723 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
80d7287f PC |
4724 | { |
4725 | error ("template class without a name"); | |
4726 | return error_mark_node; | |
4727 | } | |
717a7d5d | 4728 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
4b0d3cbe | 4729 | { |
717a7d5d MM |
4730 | if (DECL_DESTRUCTOR_P (decl)) |
4731 | { | |
4732 | /* [temp.mem] | |
c8094d83 | 4733 | |
0cbd7506 | 4734 | A destructor shall not be a member template. */ |
0f51ccfc | 4735 | error ("destructor %qD declared as member template", decl); |
717a7d5d MM |
4736 | return error_mark_node; |
4737 | } | |
4738 | if (NEW_DELETE_OPNAME_P (DECL_NAME (decl)) | |
4739 | && (!TYPE_ARG_TYPES (TREE_TYPE (decl)) | |
4740 | || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node | |
4741 | || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) | |
4742 | || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl)))) | |
4743 | == void_list_node))) | |
4744 | { | |
c8094d83 | 4745 | /* [basic.stc.dynamic.allocation] |
717a7d5d | 4746 | |
0cbd7506 | 4747 | An allocation function can be a function |
717a7d5d MM |
4748 | template. ... Template allocation functions shall |
4749 | have two or more parameters. */ | |
0f51ccfc | 4750 | error ("invalid template declaration of %qD", decl); |
7c60008e | 4751 | return error_mark_node; |
717a7d5d | 4752 | } |
4b0d3cbe | 4753 | } |
8ca4bf25 MM |
4754 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
4755 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
2aaf816d JM |
4756 | /* OK */; |
4757 | else | |
11325dcd | 4758 | { |
0f51ccfc | 4759 | error ("template declaration of %q#D", decl); |
11325dcd KL |
4760 | return error_mark_node; |
4761 | } | |
83566abf JM |
4762 | } |
4763 | ||
6ba89f8e MM |
4764 | /* Check to see that the rules regarding the use of default |
4765 | arguments are not being violated. */ | |
c8094d83 | 4766 | check_default_tmpl_args (decl, current_template_parms, |
9b7dd5e8 | 4767 | primary, is_partial, /*is_friend_decl=*/0); |
73aad9b9 | 4768 | |
5d80a306 DG |
4769 | /* Ensure that there are no parameter packs in the type of this |
4770 | declaration that have not been expanded. */ | |
b1d7b1c0 DG |
4771 | if (TREE_CODE (decl) == FUNCTION_DECL) |
4772 | { | |
4773 | /* Check each of the arguments individually to see if there are | |
4774 | any bare parameter packs. */ | |
4775 | tree type = TREE_TYPE (decl); | |
4776 | tree arg = DECL_ARGUMENTS (decl); | |
4777 | tree argtype = TYPE_ARG_TYPES (type); | |
4778 | ||
4779 | while (arg && argtype) | |
4780 | { | |
4781 | if (!FUNCTION_PARAMETER_PACK_P (arg) | |
7b3e2d46 | 4782 | && check_for_bare_parameter_packs (TREE_TYPE (arg))) |
b1d7b1c0 DG |
4783 | { |
4784 | /* This is a PARM_DECL that contains unexpanded parameter | |
4785 | packs. We have already complained about this in the | |
4786 | check_for_bare_parameter_packs call, so just replace | |
4787 | these types with ERROR_MARK_NODE. */ | |
4788 | TREE_TYPE (arg) = error_mark_node; | |
4789 | TREE_VALUE (argtype) = error_mark_node; | |
4790 | } | |
4791 | ||
910ad8de | 4792 | arg = DECL_CHAIN (arg); |
b1d7b1c0 DG |
4793 | argtype = TREE_CHAIN (argtype); |
4794 | } | |
4795 | ||
4796 | /* Check for bare parameter packs in the return type and the | |
4797 | exception specifiers. */ | |
7b3e2d46 | 4798 | if (check_for_bare_parameter_packs (TREE_TYPE (type))) |
046e4071 JJ |
4799 | /* Errors were already issued, set return type to int |
4800 | as the frontend doesn't expect error_mark_node as | |
4801 | the return type. */ | |
4802 | TREE_TYPE (type) = integer_type_node; | |
7b3e2d46 DG |
4803 | if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type))) |
4804 | TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE; | |
b1d7b1c0 | 4805 | } |
7b3e2d46 | 4806 | else if (check_for_bare_parameter_packs (TREE_TYPE (decl))) |
92fab505 DG |
4807 | { |
4808 | TREE_TYPE (decl) = error_mark_node; | |
4809 | return error_mark_node; | |
4810 | } | |
5d80a306 | 4811 | |
6ba89f8e MM |
4812 | if (is_partial) |
4813 | return process_partial_specialization (decl); | |
d32789d8 | 4814 | |
9a3b49ac MS |
4815 | args = current_template_args (); |
4816 | ||
c8094d83 | 4817 | if (!ctx |
f84b4be9 | 4818 | || TREE_CODE (ctx) == FUNCTION_DECL |
a723baf1 | 4819 | || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) |
f84b4be9 | 4820 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) |
8d08fdba | 4821 | { |
75650646 | 4822 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
4823 | && DECL_TEMPLATE_INFO (decl) |
4824 | && DECL_TI_TEMPLATE (decl)) | |
4825 | tmpl = DECL_TI_TEMPLATE (decl); | |
1c10870d AS |
4826 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
4827 | be DECL_LANG_SPECIFIC. The information equivalent to | |
4828 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
c8094d83 | 4829 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
1c10870d AS |
4830 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) |
4831 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
4832 | { | |
4833 | /* Since a template declaration already existed for this | |
4834 | class-type, we must be redeclaring it here. Make sure | |
0e339752 | 4835 | that the redeclaration is valid. */ |
1c10870d AS |
4836 | redeclare_class_template (TREE_TYPE (decl), |
4837 | current_template_parms); | |
4838 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
4839 | one we already had. */ | |
4840 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
4841 | } | |
f84b4be9 | 4842 | else |
786b5245 | 4843 | { |
c7222c02 MM |
4844 | tmpl = build_template_decl (decl, current_template_parms, |
4845 | member_template_p); | |
cfe507be MM |
4846 | new_template_p = 1; |
4847 | ||
f84b4be9 JM |
4848 | if (DECL_LANG_SPECIFIC (decl) |
4849 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
4850 | { | |
4851 | /* A specialization of a member template of a template | |
c6002625 | 4852 | class. */ |
f84b4be9 JM |
4853 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
4854 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
4855 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
4856 | } | |
786b5245 | 4857 | } |
8d08fdba MS |
4858 | } |
4859 | else | |
4860 | { | |
e1a5ccf7 | 4861 | tree a, t, current, parms; |
ba4f4e5d | 4862 | int i; |
91a77d68 | 4863 | tree tinfo = get_template_info (decl); |
6633d636 | 4864 | |
91a77d68 | 4865 | if (!tinfo) |
c91a56d2 | 4866 | { |
0f51ccfc | 4867 | error ("template definition of non-template %q#D", decl); |
3fe82414 | 4868 | return error_mark_node; |
c91a56d2 | 4869 | } |
91a77d68 JM |
4870 | |
4871 | tmpl = TI_TEMPLATE (tinfo); | |
c8094d83 | 4872 | |
c353b8e3 | 4873 | if (DECL_FUNCTION_TEMPLATE_P (tmpl) |
c8094d83 | 4874 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) |
c353b8e3 | 4875 | && DECL_TEMPLATE_SPECIALIZATION (decl) |
c7222c02 | 4876 | && DECL_MEMBER_TEMPLATE_P (tmpl)) |
98c1c668 | 4877 | { |
e1a5ccf7 JM |
4878 | tree new_tmpl; |
4879 | ||
4880 | /* The declaration is a specialization of a member | |
4881 | template, declared outside the class. Therefore, the | |
4882 | innermost template arguments will be NULL, so we | |
4883 | replace them with the arguments determined by the | |
4884 | earlier call to check_explicit_specialization. */ | |
4885 | args = DECL_TI_ARGS (decl); | |
4886 | ||
c8094d83 | 4887 | new_tmpl |
c7222c02 MM |
4888 | = build_template_decl (decl, current_template_parms, |
4889 | member_template_p); | |
e1a5ccf7 JM |
4890 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; |
4891 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
4892 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
4893 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
c8094d83 | 4894 | DECL_TEMPLATE_INFO (new_tmpl) |
aa373032 | 4895 | = build_template_info (tmpl, args); |
e1a5ccf7 | 4896 | |
c8094d83 MS |
4897 | register_specialization (new_tmpl, |
4898 | most_general_template (tmpl), | |
d63d5d0c | 4899 | args, |
2b110bfc | 4900 | is_friend, 0); |
e1a5ccf7 | 4901 | return decl; |
98c1c668 | 4902 | } |
98c1c668 | 4903 | |
e1a5ccf7 | 4904 | /* Make sure the template headers we got make sense. */ |
6633d636 | 4905 | |
e1a5ccf7 JM |
4906 | parms = DECL_TEMPLATE_PARMS (tmpl); |
4907 | i = TMPL_PARMS_DEPTH (parms); | |
4908 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 4909 | { |
0f51ccfc | 4910 | error ("expected %d levels of template parms for %q#D, got %d", |
0cbd7506 | 4911 | i, decl, TMPL_ARGS_DEPTH (args)); |
98c1c668 | 4912 | } |
e1a5ccf7 JM |
4913 | else |
4914 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
4915 | { | |
4916 | a = TMPL_ARGS_LEVEL (args, i); | |
4917 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
4918 | ||
4919 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
4920 | { | |
4921 | if (current == decl) | |
0f51ccfc | 4922 | error ("got %d template parameters for %q#D", |
0cbd7506 | 4923 | TREE_VEC_LENGTH (a), decl); |
e1a5ccf7 | 4924 | else |
0f51ccfc | 4925 | error ("got %d template parameters for %q#T", |
0cbd7506 | 4926 | TREE_VEC_LENGTH (a), current); |
33bd39a2 | 4927 | error (" but %d required", TREE_VEC_LENGTH (t)); |
f1cc0515 | 4928 | return error_mark_node; |
e1a5ccf7 | 4929 | } |
98c1c668 | 4930 | |
e1a5ccf7 JM |
4931 | if (current == decl) |
4932 | current = ctx; | |
74537078 JM |
4933 | else if (current == NULL_TREE) |
4934 | /* Can happen in erroneous input. */ | |
4935 | break; | |
e1a5ccf7 | 4936 | else |
ba75df2b PC |
4937 | current = (TYPE_P (current) |
4938 | ? TYPE_CONTEXT (current) | |
4939 | : DECL_CONTEXT (current)); | |
e1a5ccf7 | 4940 | } |
91a77d68 JM |
4941 | |
4942 | /* Check that the parms are used in the appropriate qualifying scopes | |
4943 | in the declarator. */ | |
4944 | if (!comp_template_args | |
4945 | (TI_ARGS (tinfo), | |
4946 | TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl))))) | |
4947 | { | |
4948 | error ("\ | |
4949 | template arguments to %qD do not match original template %qD", | |
4950 | decl, DECL_TEMPLATE_RESULT (tmpl)); | |
4951 | if (!uses_template_parms (TI_ARGS (tinfo))) | |
1f5b3869 | 4952 | inform (input_location, "use template<> for an explicit specialization"); |
91a77d68 JM |
4953 | /* Avoid crash in import_export_decl. */ |
4954 | DECL_INTERFACE_KNOWN (decl) = 1; | |
4955 | return error_mark_node; | |
4956 | } | |
5566b478 | 4957 | } |
8d08fdba | 4958 | |
5566b478 MS |
4959 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
4960 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 4961 | |
36a117a5 MM |
4962 | /* Push template declarations for global functions and types. Note |
4963 | that we do not try to push a global template friend declared in a | |
4964 | template class; such a thing may well depend on the template | |
39c01e4c | 4965 | parameters of the class. */ |
c8094d83 | 4966 | if (new_template_p && !ctx |
36a117a5 | 4967 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
c6f9f83b | 4968 | { |
d63d5d0c | 4969 | tmpl = pushdecl_namespace_level (tmpl, is_friend); |
c6f9f83b KL |
4970 | if (tmpl == error_mark_node) |
4971 | return error_mark_node; | |
bd3d082e KL |
4972 | |
4973 | /* Hide template friend classes that haven't been declared yet. */ | |
4974 | if (is_friend && TREE_CODE (decl) == TYPE_DECL) | |
4975 | { | |
4976 | DECL_ANTICIPATED (tmpl) = 1; | |
4977 | DECL_FRIEND_P (tmpl) = 1; | |
4978 | } | |
c6f9f83b | 4979 | } |
8d08fdba | 4980 | |
5566b478 | 4981 | if (primary) |
5dd236e2 | 4982 | { |
ef3b7b17 JM |
4983 | tree parms = DECL_TEMPLATE_PARMS (tmpl); |
4984 | int i; | |
4985 | ||
5dd236e2 NS |
4986 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
4987 | if (DECL_CONV_FN_P (tmpl)) | |
4988 | { | |
ef3b7b17 | 4989 | int depth = TMPL_PARMS_DEPTH (parms); |
6e04241f | 4990 | |
5dd236e2 NS |
4991 | /* It is a conversion operator. See if the type converted to |
4992 | depends on innermost template operands. */ | |
c8094d83 | 4993 | |
d43f603d KL |
4994 | if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)), |
4995 | depth)) | |
5dd236e2 NS |
4996 | DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; |
4997 | } | |
ef3b7b17 JM |
4998 | |
4999 | /* Give template template parms a DECL_CONTEXT of the template | |
5000 | for which they are a parameter. */ | |
5001 | parms = INNERMOST_TEMPLATE_PARMS (parms); | |
5002 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) | |
5003 | { | |
5004 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
5005 | if (TREE_CODE (parm) == TEMPLATE_DECL) | |
5006 | DECL_CONTEXT (parm) = tmpl; | |
5007 | } | |
5dd236e2 | 5008 | } |
5566b478 | 5009 | |
a692ad2e | 5010 | /* The DECL_TI_ARGS of DECL contains full set of arguments referring |
28ba38e3 KL |
5011 | back to its most general template. If TMPL is a specialization, |
5012 | ARGS may only have the innermost set of arguments. Add the missing | |
5013 | argument levels if necessary. */ | |
5014 | if (DECL_TEMPLATE_INFO (tmpl)) | |
5015 | args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args); | |
5016 | ||
aa373032 | 5017 | info = build_template_info (tmpl, args); |
5566b478 | 5018 | |
9188c363 | 5019 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
a0ad3539 | 5020 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
2aaf816d | 5021 | else if (DECL_LANG_SPECIFIC (decl)) |
5566b478 | 5022 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
5023 | |
5024 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
5025 | } |
5026 | ||
6757edfe | 5027 | tree |
3a978d72 | 5028 | push_template_decl (tree decl) |
6757edfe | 5029 | { |
d63d5d0c | 5030 | return push_template_decl_real (decl, false); |
6757edfe MM |
5031 | } |
5032 | ||
5033 | /* Called when a class template TYPE is redeclared with the indicated | |
5034 | template PARMS, e.g.: | |
7fe6899f MM |
5035 | |
5036 | template <class T> struct S; | |
5037 | template <class T> struct S {}; */ | |
5038 | ||
60feef2c | 5039 | bool |
3a978d72 | 5040 | redeclare_class_template (tree type, tree parms) |
7fe6899f | 5041 | { |
3d7de1fa | 5042 | tree tmpl; |
6757edfe | 5043 | tree tmpl_parms; |
7fe6899f MM |
5044 | int i; |
5045 | ||
3d7de1fa MM |
5046 | if (!TYPE_TEMPLATE_INFO (type)) |
5047 | { | |
0f51ccfc | 5048 | error ("%qT is not a template type", type); |
60feef2c | 5049 | return false; |
3d7de1fa MM |
5050 | } |
5051 | ||
5052 | tmpl = TYPE_TI_TEMPLATE (type); | |
7fe6899f MM |
5053 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
5054 | /* The type is nested in some template class. Nothing to worry | |
5055 | about here; there are no new template parameters for the nested | |
5056 | type. */ | |
60feef2c | 5057 | return true; |
7fe6899f | 5058 | |
ee921ddf MM |
5059 | if (!parms) |
5060 | { | |
5061 | error ("template specifiers not specified in declaration of %qD", | |
5062 | tmpl); | |
60feef2c | 5063 | return false; |
ee921ddf MM |
5064 | } |
5065 | ||
6757edfe MM |
5066 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
5067 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
5068 | ||
7fe6899f MM |
5069 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
5070 | { | |
894e2652 SZ |
5071 | error_n (input_location, TREE_VEC_LENGTH (parms), |
5072 | "redeclared with %d template parameter", | |
5073 | "redeclared with %d template parameters", | |
5074 | TREE_VEC_LENGTH (parms)); | |
5075 | inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms), | |
5076 | "previous declaration %q+D used %d template parameter", | |
5077 | "previous declaration %q+D used %d template parameters", | |
5078 | tmpl, TREE_VEC_LENGTH (tmpl_parms)); | |
60feef2c | 5079 | return false; |
7fe6899f MM |
5080 | } |
5081 | ||
5082 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
5083 | { | |
2d8ba2c7 LM |
5084 | tree tmpl_parm; |
5085 | tree parm; | |
5086 | tree tmpl_default; | |
5087 | tree parm_default; | |
5088 | ||
5089 | if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node | |
5090 | || TREE_VEC_ELT (parms, i) == error_mark_node) | |
5091 | continue; | |
5092 | ||
5093 | tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
baae602e SM |
5094 | if (tmpl_parm == error_mark_node) |
5095 | return false; | |
5096 | ||
2d8ba2c7 LM |
5097 | parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
5098 | tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
5099 | parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
7fe6899f | 5100 | |
2649701f KL |
5101 | /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or |
5102 | TEMPLATE_DECL. */ | |
baae602e SM |
5103 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm) |
5104 | || (TREE_CODE (tmpl_parm) != TYPE_DECL | |
5105 | && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))) | |
5106 | || (TREE_CODE (tmpl_parm) != PARM_DECL | |
5107 | && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm)) | |
5108 | != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))) | |
5109 | || (TREE_CODE (tmpl_parm) == PARM_DECL | |
5110 | && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm)) | |
5111 | != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))) | |
7fe6899f | 5112 | { |
dee15844 | 5113 | error ("template parameter %q+#D", tmpl_parm); |
0f51ccfc | 5114 | error ("redeclared here as %q#D", parm); |
60feef2c | 5115 | return false; |
7fe6899f MM |
5116 | } |
5117 | ||
5118 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
5119 | { | |
5120 | /* We have in [temp.param]: | |
5121 | ||
5122 | A template-parameter may not be given default arguments | |
5123 | by two different declarations in the same scope. */ | |
c5d75364 MLI |
5124 | error_at (input_location, "redefinition of default argument for %q#D", parm); |
5125 | inform (DECL_SOURCE_LOCATION (tmpl_parm), | |
5126 | "original definition appeared here"); | |
60feef2c | 5127 | return false; |
7fe6899f MM |
5128 | } |
5129 | ||
5130 | if (parm_default != NULL_TREE) | |
5131 | /* Update the previous template parameters (which are the ones | |
5132 | that will really count) with the new default value. */ | |
5133 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
7ce74c5e MM |
5134 | else if (tmpl_default != NULL_TREE) |
5135 | /* Update the new parameters, too; they'll be used as the | |
5136 | parameters for any members. */ | |
5137 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
7fe6899f | 5138 | } |
60feef2c LM |
5139 | |
5140 | return true; | |
7fe6899f | 5141 | } |
75650646 | 5142 | |
9baa27a9 MM |
5143 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
5144 | (possibly simplified) expression. */ | |
5145 | ||
40013af7 JM |
5146 | static tree |
5147 | fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain) | |
9baa27a9 | 5148 | { |
d4a200d3 SM |
5149 | if (expr == NULL_TREE) |
5150 | return NULL_TREE; | |
5151 | ||
9baa27a9 MM |
5152 | /* If we're in a template, but EXPR isn't value dependent, simplify |
5153 | it. We're supposed to treat: | |
c8094d83 | 5154 | |
9baa27a9 MM |
5155 | template <typename T> void f(T[1 + 1]); |
5156 | template <typename T> void f(T[2]); | |
c8094d83 | 5157 | |
9baa27a9 MM |
5158 | as two declarations of the same function, for example. */ |
5159 | if (processing_template_decl | |
5160 | && !type_dependent_expression_p (expr) | |
7416ab02 | 5161 | && !value_dependent_expression_p (expr)) |
9baa27a9 MM |
5162 | { |
5163 | HOST_WIDE_INT saved_processing_template_decl; | |
5164 | ||
5165 | saved_processing_template_decl = processing_template_decl; | |
5166 | processing_template_decl = 0; | |
5167 | expr = tsubst_copy_and_build (expr, | |
5168 | /*args=*/NULL_TREE, | |
40013af7 | 5169 | complain, |
9baa27a9 | 5170 | /*in_decl=*/NULL_TREE, |
015c2c66 MM |
5171 | /*function_p=*/false, |
5172 | /*integral_constant_expression_p=*/true); | |
9baa27a9 MM |
5173 | processing_template_decl = saved_processing_template_decl; |
5174 | } | |
5175 | return expr; | |
5176 | } | |
5177 | ||
40013af7 JM |
5178 | tree |
5179 | fold_non_dependent_expr (tree expr) | |
5180 | { | |
5181 | return fold_non_dependent_expr_sfinae (expr, tf_error); | |
5182 | } | |
5183 | ||
b6ab6892 GB |
5184 | /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which |
5185 | must be a function or a pointer-to-function type, as specified | |
5186 | in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, | |
5187 | and check that the resulting function has external linkage. */ | |
5188 | ||
5189 | static tree | |
5190 | convert_nontype_argument_function (tree type, tree expr) | |
5191 | { | |
5192 | tree fns = expr; | |
5193 | tree fn, fn_no_ptr; | |
5194 | ||
5195 | fn = instantiate_type (type, fns, tf_none); | |
5196 | if (fn == error_mark_node) | |
5197 | return error_mark_node; | |
5198 | ||
5199 | fn_no_ptr = fn; | |
5200 | if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) | |
5201 | fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); | |
eff3a276 MM |
5202 | if (TREE_CODE (fn_no_ptr) == BASELINK) |
5203 | fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr); | |
5204 | ||
b6ab6892 GB |
5205 | /* [temp.arg.nontype]/1 |
5206 | ||
5207 | A template-argument for a non-type, non-template template-parameter | |
5208 | shall be one of: | |
5209 | [...] | |
5210 | -- the address of an object or function with external linkage. */ | |
5211 | if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr)) | |
5212 | { | |
5213 | error ("%qE is not a valid template argument for type %qT " | |
5214 | "because function %qD has not external linkage", | |
5215 | expr, type, fn_no_ptr); | |
5216 | return NULL_TREE; | |
5217 | } | |
5218 | ||
5219 | return fn; | |
5220 | } | |
5221 | ||
9d5874cf DS |
5222 | /* Subroutine of convert_nontype_argument. |
5223 | Check if EXPR of type TYPE is a valid pointer-to-member constant. | |
5224 | Emit an error otherwise. */ | |
5225 | ||
5226 | static bool | |
574e2ba2 DS |
5227 | check_valid_ptrmem_cst_expr (tree type, tree expr, |
5228 | tsubst_flags_t complain) | |
9d5874cf DS |
5229 | { |
5230 | STRIP_NOPS (expr); | |
5231 | if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST)) | |
5232 | return true; | |
574e2ba2 DS |
5233 | if (complain & tf_error) |
5234 | { | |
5235 | error ("%qE is not a valid template argument for type %qT", | |
5236 | expr, type); | |
d8a07487 | 5237 | error ("it must be a pointer-to-member of the form %<&X::Y%>"); |
574e2ba2 | 5238 | } |
9d5874cf DS |
5239 | return false; |
5240 | } | |
5241 | ||
d3a79fcc JM |
5242 | /* Returns TRUE iff the address of OP is value-dependent. |
5243 | ||
5244 | 14.6.2.4 [temp.dep.temp]: | |
5245 | A non-integral non-type template-argument is dependent if its type is | |
5246 | dependent or it has either of the following forms | |
5247 | qualified-id | |
5248 | & qualified-id | |
5249 | and contains a nested-name-specifier which specifies a class-name that | |
5250 | names a dependent type. | |
5251 | ||
5252 | We generalize this to just say that the address of a member of a | |
5253 | dependent class is value-dependent; the above doesn't cover the | |
5254 | address of a static data member named with an unqualified-id. */ | |
5255 | ||
5256 | static bool | |
5257 | has_value_dependent_address (tree op) | |
5258 | { | |
5259 | /* We could use get_inner_reference here, but there's no need; | |
5260 | this is only relevant for template non-type arguments, which | |
5261 | can only be expressed as &id-expression. */ | |
5262 | if (DECL_P (op)) | |
5263 | { | |
5264 | tree ctx = CP_DECL_CONTEXT (op); | |
5265 | if (TYPE_P (ctx) && dependent_type_p (ctx)) | |
5266 | return true; | |
5267 | } | |
5268 | ||
5269 | return false; | |
5270 | } | |
5271 | ||
75650646 MM |
5272 | /* Attempt to convert the non-type template parameter EXPR to the |
5273 | indicated TYPE. If the conversion is successful, return the | |
dc957d14 | 5274 | converted value. If the conversion is unsuccessful, return |
75650646 MM |
5275 | NULL_TREE if we issued an error message, or error_mark_node if we |
5276 | did not. We issue error messages for out-and-out bad template | |
5277 | parameters, but not simply because the conversion failed, since we | |
9baa27a9 | 5278 | might be just trying to do argument deduction. Both TYPE and EXPR |
b6ab6892 GB |
5279 | must be non-dependent. |
5280 | ||
5281 | The conversion follows the special rules described in | |
5282 | [temp.arg.nontype], and it is much more strict than an implicit | |
5283 | conversion. | |
5284 | ||
5285 | This function is called twice for each template argument (see | |
5286 | lookup_template_class for a more accurate description of this | |
5287 | problem). This means that we need to handle expressions which | |
5288 | are not valid in a C++ source, but can be created from the | |
5289 | first call (for instance, casts to perform conversions). These | |
5290 | hacks can go away after we fix the double coercion problem. */ | |
75650646 MM |
5291 | |
5292 | static tree | |
40013af7 | 5293 | convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) |
75650646 | 5294 | { |
9baa27a9 MM |
5295 | tree expr_type; |
5296 | ||
b6ab6892 GB |
5297 | /* Detect immediately string literals as invalid non-type argument. |
5298 | This special-case is not needed for correctness (we would easily | |
5299 | catch this later), but only to provide better diagnostic for this | |
5300 | common user mistake. As suggested by DR 100, we do not mention | |
5301 | linkage issues in the diagnostic as this is not the point. */ | |
40013af7 | 5302 | /* FIXME we're making this OK. */ |
b6ab6892 GB |
5303 | if (TREE_CODE (expr) == STRING_CST) |
5304 | { | |
40013af7 JM |
5305 | if (complain & tf_error) |
5306 | error ("%qE is not a valid template argument for type %qT " | |
5307 | "because string literals can never be used in this context", | |
5308 | expr, type); | |
b6ab6892 GB |
5309 | return NULL_TREE; |
5310 | } | |
5311 | ||
d3a79fcc JM |
5312 | /* Add the ADDR_EXPR now for the benefit of |
5313 | value_dependent_expression_p. */ | |
5314 | if (TYPE_PTROBV_P (type)) | |
5315 | expr = decay_conversion (expr); | |
5316 | ||
9baa27a9 MM |
5317 | /* If we are in a template, EXPR may be non-dependent, but still |
5318 | have a syntactic, rather than semantic, form. For example, EXPR | |
5319 | might be a SCOPE_REF, rather than the VAR_DECL to which the | |
5320 | SCOPE_REF refers. Preserving the qualifying scope is necessary | |
5321 | so that access checking can be performed when the template is | |
5322 | instantiated -- but here we need the resolved form so that we can | |
5323 | convert the argument. */ | |
d3a79fcc JM |
5324 | if (TYPE_REF_OBJ_P (type) |
5325 | && has_value_dependent_address (expr)) | |
5326 | /* If we want the address and it's value-dependent, don't fold. */; | |
40013af7 JM |
5327 | else if (!type_unknown_p (expr)) |
5328 | expr = fold_non_dependent_expr_sfinae (expr, complain); | |
b166a559 VR |
5329 | if (error_operand_p (expr)) |
5330 | return error_mark_node; | |
9baa27a9 | 5331 | expr_type = TREE_TYPE (expr); |
2d660b7f JM |
5332 | if (TREE_CODE (type) == REFERENCE_TYPE) |
5333 | expr = mark_lvalue_use (expr); | |
5334 | else | |
5335 | expr = mark_rvalue_use (expr); | |
75650646 | 5336 | |
b6ab6892 GB |
5337 | /* HACK: Due to double coercion, we can get a |
5338 | NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here, | |
5339 | which is the tree that we built on the first call (see | |
5340 | below when coercing to reference to object or to reference to | |
5341 | function). We just strip everything and get to the arg. | |
5342 | See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C | |
5343 | for examples. */ | |
5344 | if (TREE_CODE (expr) == NOP_EXPR) | |
75650646 | 5345 | { |
b6ab6892 | 5346 | if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) |
75650646 | 5347 | { |
b6ab6892 GB |
5348 | /* ??? Maybe we could use convert_from_reference here, but we |
5349 | would need to relax its constraints because the NOP_EXPR | |
5350 | could actually change the type to something more cv-qualified, | |
5351 | and this is not folded by convert_from_reference. */ | |
5352 | tree addr = TREE_OPERAND (expr, 0); | |
5353 | gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE); | |
5354 | gcc_assert (TREE_CODE (addr) == ADDR_EXPR); | |
5355 | gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE); | |
5356 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
5357 | (TREE_TYPE (expr_type), | |
5358 | TREE_TYPE (TREE_TYPE (addr)))); | |
5359 | ||
5360 | expr = TREE_OPERAND (addr, 0); | |
5361 | expr_type = TREE_TYPE (expr); | |
75650646 MM |
5362 | } |
5363 | ||
b6ab6892 GB |
5364 | /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the |
5365 | parameter is a pointer to object, through decay and | |
5366 | qualification conversion. Let's strip everything. */ | |
5367 | else if (TYPE_PTROBV_P (type)) | |
75650646 | 5368 | { |
b6ab6892 GB |
5369 | STRIP_NOPS (expr); |
5370 | gcc_assert (TREE_CODE (expr) == ADDR_EXPR); | |
5371 | gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE); | |
5372 | /* Skip the ADDR_EXPR only if it is part of the decay for | |
5373 | an array. Otherwise, it is part of the original argument | |
5374 | in the source code. */ | |
5375 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE) | |
5376 | expr = TREE_OPERAND (expr, 0); | |
5377 | expr_type = TREE_TYPE (expr); | |
f3400fe2 | 5378 | } |
75650646 | 5379 | } |
c61dce3a | 5380 | |
b6ab6892 | 5381 | /* [temp.arg.nontype]/5, bullet 1 |
75650646 | 5382 | |
b6ab6892 GB |
5383 | For a non-type template-parameter of integral or enumeration type, |
5384 | integral promotions (_conv.prom_) and integral conversions | |
5385 | (_conv.integral_) are applied. */ | |
550a799d | 5386 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
75650646 | 5387 | { |
61f8d165 | 5388 | tree t = build_integral_nontype_arg_conv (type, expr, complain); |
fa2200cb | 5389 | t = maybe_constant_value (t); |
61f8d165 JM |
5390 | if (t != error_mark_node) |
5391 | expr = t; | |
5392 | ||
5393 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr))) | |
75650646 | 5394 | return error_mark_node; |
fddabb2c | 5395 | |
b6ab6892 GB |
5396 | /* Notice that there are constant expressions like '4 % 0' which |
5397 | do not fold into integer constants. */ | |
db02b6b9 | 5398 | if (TREE_CODE (expr) != INTEGER_CST) |
b6ab6892 | 5399 | { |
40013af7 | 5400 | if (complain & tf_error) |
fa2200cb JM |
5401 | { |
5402 | error ("%qE is not a valid template argument for type %qT " | |
5403 | "because it is a non-constant expression", expr, type); | |
5404 | cxx_constant_value (expr); | |
5405 | } | |
b6ab6892 GB |
5406 | return NULL_TREE; |
5407 | } | |
b6ab6892 GB |
5408 | } |
5409 | /* [temp.arg.nontype]/5, bullet 2 | |
75650646 | 5410 | |
b6ab6892 GB |
5411 | For a non-type template-parameter of type pointer to object, |
5412 | qualification conversions (_conv.qual_) and the array-to-pointer | |
5413 | conversion (_conv.array_) are applied. */ | |
5414 | else if (TYPE_PTROBV_P (type)) | |
5415 | { | |
5416 | /* [temp.arg.nontype]/1 (TC1 version, DR 49): | |
75650646 | 5417 | |
b6ab6892 GB |
5418 | A template-argument for a non-type, non-template template-parameter |
5419 | shall be one of: [...] | |
75650646 | 5420 | |
b6ab6892 GB |
5421 | -- the name of a non-type template-parameter; |
5422 | -- the address of an object or function with external linkage, [...] | |
5423 | expressed as "& id-expression" where the & is optional if the name | |
5424 | refers to a function or array, or if the corresponding | |
5425 | template-parameter is a reference. | |
c8094d83 | 5426 | |
b6ab6892 GB |
5427 | Here, we do not care about functions, as they are invalid anyway |
5428 | for a parameter of type pointer-to-object. */ | |
1082fd10 MM |
5429 | |
5430 | if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)) | |
5431 | /* Non-type template parameters are OK. */ | |
5432 | ; | |
5433 | else if (TREE_CODE (expr) != ADDR_EXPR | |
5434 | && TREE_CODE (expr_type) != ARRAY_TYPE) | |
5435 | { | |
5436 | if (TREE_CODE (expr) == VAR_DECL) | |
5437 | { | |
5438 | error ("%qD is not a valid template argument " | |
5439 | "because %qD is a variable, not the address of " | |
5440 | "a variable", | |
5441 | expr, expr); | |
5442 | return NULL_TREE; | |
5443 | } | |
5444 | /* Other values, like integer constants, might be valid | |
5445 | non-type arguments of some other type. */ | |
5446 | return error_mark_node; | |
5447 | } | |
5448 | else | |
5449 | { | |
5450 | tree decl; | |
5451 | ||
5452 | decl = ((TREE_CODE (expr) == ADDR_EXPR) | |
5453 | ? TREE_OPERAND (expr, 0) : expr); | |
5454 | if (TREE_CODE (decl) != VAR_DECL) | |
5455 | { | |
5456 | error ("%qE is not a valid template argument of type %qT " | |
5457 | "because %qE is not a variable", | |
5458 | expr, type, decl); | |
5459 | return NULL_TREE; | |
5460 | } | |
5461 | else if (!DECL_EXTERNAL_LINKAGE_P (decl)) | |
5462 | { | |
5463 | error ("%qE is not a valid template argument of type %qT " | |
5464 | "because %qD does not have external linkage", | |
5465 | expr, type, decl); | |
5466 | return NULL_TREE; | |
5467 | } | |
5468 | } | |
b6ab6892 GB |
5469 | |
5470 | expr = decay_conversion (expr); | |
5471 | if (expr == error_mark_node) | |
5472 | return error_mark_node; | |
75650646 | 5473 | |
b6ab6892 GB |
5474 | expr = perform_qualification_conversions (type, expr); |
5475 | if (expr == error_mark_node) | |
5476 | return error_mark_node; | |
b6ab6892 GB |
5477 | } |
5478 | /* [temp.arg.nontype]/5, bullet 3 | |
5479 | ||
5480 | For a non-type template-parameter of type reference to object, no | |
5481 | conversions apply. The type referred to by the reference may be more | |
5482 | cv-qualified than the (otherwise identical) type of the | |
5483 | template-argument. The template-parameter is bound directly to the | |
5484 | template-argument, which must be an lvalue. */ | |
5485 | else if (TYPE_REF_OBJ_P (type)) | |
5486 | { | |
5487 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type), | |
5488 | expr_type)) | |
5489 | return error_mark_node; | |
75650646 | 5490 | |
b6ab6892 GB |
5491 | if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type)) |
5492 | { | |
5493 | error ("%qE is not a valid template argument for type %qT " | |
5494 | "because of conflicts in cv-qualification", expr, type); | |
5495 | return NULL_TREE; | |
5496 | } | |
c8094d83 | 5497 | |
b6ab6892 GB |
5498 | if (!real_lvalue_p (expr)) |
5499 | { | |
5500 | error ("%qE is not a valid template argument for type %qT " | |
944fd207 | 5501 | "because it is not an lvalue", expr, type); |
b6ab6892 GB |
5502 | return NULL_TREE; |
5503 | } | |
e6e174e5 | 5504 | |
b6ab6892 | 5505 | /* [temp.arg.nontype]/1 |
75650646 | 5506 | |
b6ab6892 GB |
5507 | A template-argument for a non-type, non-template template-parameter |
5508 | shall be one of: [...] | |
75650646 | 5509 | |
03fd3f84 | 5510 | -- the address of an object or function with external linkage. */ |
a38f55db JM |
5511 | if (TREE_CODE (expr) == INDIRECT_REF |
5512 | && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0)))) | |
5513 | { | |
5514 | expr = TREE_OPERAND (expr, 0); | |
5515 | if (DECL_P (expr)) | |
5516 | { | |
5517 | error ("%q#D is not a valid template argument for type %qT " | |
5518 | "because a reference variable does not have a constant " | |
5519 | "address", expr, type); | |
5520 | return NULL_TREE; | |
5521 | } | |
5522 | } | |
5523 | ||
5524 | if (!DECL_P (expr)) | |
5525 | { | |
5526 | error ("%qE is not a valid template argument for type %qT " | |
5527 | "because it is not an object with external linkage", | |
5528 | expr, type); | |
5529 | return NULL_TREE; | |
5530 | } | |
5531 | ||
b6ab6892 GB |
5532 | if (!DECL_EXTERNAL_LINKAGE_P (expr)) |
5533 | { | |
5534 | error ("%qE is not a valid template argument for type %qT " | |
5535 | "because object %qD has not external linkage", | |
5536 | expr, type, expr); | |
5537 | return NULL_TREE; | |
5538 | } | |
0dc09a61 | 5539 | |
b6ab6892 GB |
5540 | expr = build_nop (type, build_address (expr)); |
5541 | } | |
5542 | /* [temp.arg.nontype]/5, bullet 4 | |
5543 | ||
5544 | For a non-type template-parameter of type pointer to function, only | |
5545 | the function-to-pointer conversion (_conv.func_) is applied. If the | |
5546 | template-argument represents a set of overloaded functions (or a | |
5547 | pointer to such), the matching function is selected from the set | |
5548 | (_over.over_). */ | |
5549 | else if (TYPE_PTRFN_P (type)) | |
5550 | { | |
5551 | /* If the argument is a template-id, we might not have enough | |
2c164de6 | 5552 | context information to decay the pointer. */ |
b6ab6892 GB |
5553 | if (!type_unknown_p (expr_type)) |
5554 | { | |
5555 | expr = decay_conversion (expr); | |
5556 | if (expr == error_mark_node) | |
5557 | return error_mark_node; | |
5558 | } | |
75650646 | 5559 | |
b6ab6892 GB |
5560 | expr = convert_nontype_argument_function (type, expr); |
5561 | if (!expr || expr == error_mark_node) | |
5562 | return expr; | |
e350dbbd PB |
5563 | |
5564 | if (TREE_CODE (expr) != ADDR_EXPR) | |
5565 | { | |
5566 | error ("%qE is not a valid template argument for type %qT", expr, type); | |
5567 | error ("it must be the address of a function with external linkage"); | |
5568 | return NULL_TREE; | |
5569 | } | |
b6ab6892 GB |
5570 | } |
5571 | /* [temp.arg.nontype]/5, bullet 5 | |
75650646 | 5572 | |
b6ab6892 GB |
5573 | For a non-type template-parameter of type reference to function, no |
5574 | conversions apply. If the template-argument represents a set of | |
5575 | overloaded functions, the matching function is selected from the set | |
5576 | (_over.over_). */ | |
5577 | else if (TYPE_REFFN_P (type)) | |
5578 | { | |
5579 | if (TREE_CODE (expr) == ADDR_EXPR) | |
5580 | { | |
5581 | error ("%qE is not a valid template argument for type %qT " | |
5582 | "because it is a pointer", expr, type); | |
1f5b3869 | 5583 | inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0)); |
b6ab6892 GB |
5584 | return NULL_TREE; |
5585 | } | |
75650646 | 5586 | |
b6ab6892 GB |
5587 | expr = convert_nontype_argument_function (TREE_TYPE (type), expr); |
5588 | if (!expr || expr == error_mark_node) | |
5589 | return expr; | |
75650646 | 5590 | |
7866705a | 5591 | expr = build_nop (type, build_address (expr)); |
b6ab6892 GB |
5592 | } |
5593 | /* [temp.arg.nontype]/5, bullet 6 | |
75650646 | 5594 | |
b6ab6892 GB |
5595 | For a non-type template-parameter of type pointer to member function, |
5596 | no conversions apply. If the template-argument represents a set of | |
5597 | overloaded member functions, the matching member function is selected | |
5598 | from the set (_over.over_). */ | |
5599 | else if (TYPE_PTRMEMFUNC_P (type)) | |
5600 | { | |
5601 | expr = instantiate_type (type, expr, tf_none); | |
5602 | if (expr == error_mark_node) | |
5603 | return error_mark_node; | |
75650646 | 5604 | |
9d5874cf DS |
5605 | /* [temp.arg.nontype] bullet 1 says the pointer to member |
5606 | expression must be a pointer-to-member constant. */ | |
574e2ba2 | 5607 | if (!check_valid_ptrmem_cst_expr (type, expr, complain)) |
9d5874cf DS |
5608 | return error_mark_node; |
5609 | ||
b6ab6892 GB |
5610 | /* There is no way to disable standard conversions in |
5611 | resolve_address_of_overloaded_function (called by | |
5612 | instantiate_type). It is possible that the call succeeded by | |
5613 | converting &B::I to &D::I (where B is a base of D), so we need | |
5614 | to reject this conversion here. | |
75650646 | 5615 | |
b6ab6892 GB |
5616 | Actually, even if there was a way to disable standard conversions, |
5617 | it would still be better to reject them here so that we can | |
5618 | provide a superior diagnostic. */ | |
5619 | if (!same_type_p (TREE_TYPE (expr), type)) | |
5620 | { | |
b6ab6892 GB |
5621 | error ("%qE is not a valid template argument for type %qT " |
5622 | "because it is of type %qT", expr, type, | |
5623 | TREE_TYPE (expr)); | |
d29760ad JM |
5624 | /* If we are just one standard conversion off, explain. */ |
5625 | if (can_convert (type, TREE_TYPE (expr))) | |
5626 | inform (input_location, | |
5627 | "standard conversions are not allowed in this context"); | |
b6ab6892 GB |
5628 | return NULL_TREE; |
5629 | } | |
5630 | } | |
5631 | /* [temp.arg.nontype]/5, bullet 7 | |
59e7a901 | 5632 | |
b6ab6892 GB |
5633 | For a non-type template-parameter of type pointer to data member, |
5634 | qualification conversions (_conv.qual_) are applied. */ | |
5635 | else if (TYPE_PTRMEM_P (type)) | |
5636 | { | |
9d5874cf DS |
5637 | /* [temp.arg.nontype] bullet 1 says the pointer to member |
5638 | expression must be a pointer-to-member constant. */ | |
574e2ba2 | 5639 | if (!check_valid_ptrmem_cst_expr (type, expr, complain)) |
9d5874cf DS |
5640 | return error_mark_node; |
5641 | ||
b6ab6892 GB |
5642 | expr = perform_qualification_conversions (type, expr); |
5643 | if (expr == error_mark_node) | |
75650646 | 5644 | return expr; |
75650646 | 5645 | } |
b6ab6892 GB |
5646 | /* A template non-type parameter must be one of the above. */ |
5647 | else | |
5648 | gcc_unreachable (); | |
75650646 | 5649 | |
b6ab6892 GB |
5650 | /* Sanity check: did we actually convert the argument to the |
5651 | right type? */ | |
40013af7 JM |
5652 | gcc_assert (same_type_ignoring_top_level_qualifiers_p |
5653 | (type, TREE_TYPE (expr))); | |
b6ab6892 | 5654 | return expr; |
75650646 MM |
5655 | } |
5656 | ||
a0a5f30f DG |
5657 | /* Subroutine of coerce_template_template_parms, which returns 1 if |
5658 | PARM_PARM and ARG_PARM match using the rule for the template | |
5659 | parameters of template template parameters. Both PARM and ARG are | |
5660 | template parameters; the rest of the arguments are the same as for | |
5661 | coerce_template_template_parms. | |
5662 | */ | |
5663 | static int | |
5664 | coerce_template_template_parm (tree parm, | |
5665 | tree arg, | |
5666 | tsubst_flags_t complain, | |
5667 | tree in_decl, | |
5668 | tree outer_args) | |
5669 | { | |
5670 | if (arg == NULL_TREE || arg == error_mark_node | |
5671 | || parm == NULL_TREE || parm == error_mark_node) | |
5672 | return 0; | |
5673 | ||
5674 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
5675 | return 0; | |
5676 | ||
5677 | switch (TREE_CODE (parm)) | |
5678 | { | |
5679 | case TEMPLATE_DECL: | |
5680 | /* We encounter instantiations of templates like | |
5681 | template <template <template <class> class> class TT> | |
5682 | class C; */ | |
5683 | { | |
5684 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
5685 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
5686 | ||
5687 | if (!coerce_template_template_parms | |
5688 | (parmparm, argparm, complain, in_decl, outer_args)) | |
5689 | return 0; | |
5690 | } | |
5691 | /* Fall through. */ | |
5692 | ||
5693 | case TYPE_DECL: | |
5694 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg)) | |
5695 | && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) | |
5696 | /* Argument is a parameter pack but parameter is not. */ | |
5697 | return 0; | |
5698 | break; | |
5699 | ||
5700 | case PARM_DECL: | |
5701 | /* The tsubst call is used to handle cases such as | |
5702 | ||
5703 | template <int> class C {}; | |
5704 | template <class T, template <T> class TT> class D {}; | |
5705 | D<int, C> d; | |
5706 | ||
5707 | i.e. the parameter list of TT depends on earlier parameters. */ | |
8320114a | 5708 | if (!uses_template_parms (TREE_TYPE (arg)) |
a0a5f30f DG |
5709 | && !same_type_p |
5710 | (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl), | |
5711 | TREE_TYPE (arg))) | |
5712 | return 0; | |
5713 | ||
5714 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg)) | |
5715 | && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) | |
5716 | /* Argument is a parameter pack but parameter is not. */ | |
5717 | return 0; | |
5718 | ||
5719 | break; | |
5720 | ||
5721 | default: | |
5722 | gcc_unreachable (); | |
5723 | } | |
5724 | ||
5725 | return 1; | |
5726 | } | |
5727 | ||
b6ab6892 | 5728 | |
c8094d83 MS |
5729 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
5730 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
5731 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
744fac59 | 5732 | or PARM_DECL. |
c8094d83 | 5733 | |
744fac59 | 5734 | Consider the example: |
e7e93965 MM |
5735 | template <class T> class A; |
5736 | template<template <class U> class TT> class B; | |
744fac59 | 5737 | |
e7e93965 MM |
5738 | For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are |
5739 | the parameters to A, and OUTER_ARGS contains A. */ | |
744fac59 KL |
5740 | |
5741 | static int | |
c8094d83 | 5742 | coerce_template_template_parms (tree parm_parms, |
0cbd7506 MS |
5743 | tree arg_parms, |
5744 | tsubst_flags_t complain, | |
3a978d72 | 5745 | tree in_decl, |
0cbd7506 | 5746 | tree outer_args) |
744fac59 KL |
5747 | { |
5748 | int nparms, nargs, i; | |
5749 | tree parm, arg; | |
a0a5f30f | 5750 | int variadic_p = 0; |
744fac59 | 5751 | |
50bc768d NS |
5752 | gcc_assert (TREE_CODE (parm_parms) == TREE_VEC); |
5753 | gcc_assert (TREE_CODE (arg_parms) == TREE_VEC); | |
744fac59 KL |
5754 | |
5755 | nparms = TREE_VEC_LENGTH (parm_parms); | |
5756 | nargs = TREE_VEC_LENGTH (arg_parms); | |
5757 | ||
a0a5f30f DG |
5758 | /* Determine whether we have a parameter pack at the end of the |
5759 | template template parameter's template parameter list. */ | |
5760 | if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node) | |
5761 | { | |
5762 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1)); | |
5763 | ||
8c8d4a46 L |
5764 | if (parm == error_mark_node) |
5765 | return 0; | |
5766 | ||
a0a5f30f DG |
5767 | switch (TREE_CODE (parm)) |
5768 | { | |
5769 | case TEMPLATE_DECL: | |
5770 | case TYPE_DECL: | |
5771 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) | |
5772 | variadic_p = 1; | |
5773 | break; | |
5774 | ||
5775 | case PARM_DECL: | |
5776 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) | |
5777 | variadic_p = 1; | |
5778 | break; | |
5779 | ||
5780 | default: | |
5781 | gcc_unreachable (); | |
5782 | } | |
5783 | } | |
5784 | ||
5785 | if (nargs != nparms | |
5786 | && !(variadic_p && nargs >= nparms - 1)) | |
744fac59 KL |
5787 | return 0; |
5788 | ||
a0a5f30f DG |
5789 | /* Check all of the template parameters except the parameter pack at |
5790 | the end (if any). */ | |
5791 | for (i = 0; i < nparms - variadic_p; ++i) | |
744fac59 | 5792 | { |
2d8ba2c7 LM |
5793 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node |
5794 | || TREE_VEC_ELT (arg_parms, i) == error_mark_node) | |
42b304f1 LM |
5795 | continue; |
5796 | ||
744fac59 KL |
5797 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
5798 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
5799 | ||
a0a5f30f DG |
5800 | if (!coerce_template_template_parm (parm, arg, complain, in_decl, |
5801 | outer_args)) | |
744fac59 KL |
5802 | return 0; |
5803 | ||
a0a5f30f | 5804 | } |
00bdb87f | 5805 | |
a0a5f30f DG |
5806 | if (variadic_p) |
5807 | { | |
5808 | /* Check each of the template parameters in the template | |
5809 | argument against the template parameter pack at the end of | |
5810 | the template template parameter. */ | |
5811 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node) | |
5812 | return 0; | |
5d80a306 | 5813 | |
a0a5f30f | 5814 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
c8094d83 | 5815 | |
a0a5f30f DG |
5816 | for (; i < nargs; ++i) |
5817 | { | |
5818 | if (TREE_VEC_ELT (arg_parms, i) == error_mark_node) | |
5819 | continue; | |
5820 | ||
5821 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
5822 | ||
5823 | if (!coerce_template_template_parm (parm, arg, complain, in_decl, | |
5824 | outer_args)) | |
5825 | return 0; | |
5826 | } | |
744fac59 | 5827 | } |
a0a5f30f | 5828 | |
744fac59 KL |
5829 | return 1; |
5830 | } | |
5831 | ||
4cf36211 DG |
5832 | /* Verifies that the deduced template arguments (in TARGS) for the |
5833 | template template parameters (in TPARMS) represent valid bindings, | |
5834 | by comparing the template parameter list of each template argument | |
5835 | to the template parameter list of its corresponding template | |
5836 | template parameter, in accordance with DR150. This | |
5837 | routine can only be called after all template arguments have been | |
5838 | deduced. It will return TRUE if all of the template template | |
5839 | parameter bindings are okay, FALSE otherwise. */ | |
5840 | bool | |
5841 | template_template_parm_bindings_ok_p (tree tparms, tree targs) | |
5842 | { | |
5843 | int i, ntparms = TREE_VEC_LENGTH (tparms); | |
ee34d21a JM |
5844 | bool ret = true; |
5845 | ||
5846 | /* We're dealing with template parms in this process. */ | |
5847 | ++processing_template_decl; | |
4cf36211 DG |
5848 | |
5849 | targs = INNERMOST_TEMPLATE_ARGS (targs); | |
5850 | ||
5851 | for (i = 0; i < ntparms; ++i) | |
5852 | { | |
5853 | tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
5854 | tree targ = TREE_VEC_ELT (targs, i); | |
5855 | ||
5856 | if (TREE_CODE (tparm) == TEMPLATE_DECL && targ) | |
5857 | { | |
5858 | tree packed_args = NULL_TREE; | |
5859 | int idx, len = 1; | |
5860 | ||
5861 | if (ARGUMENT_PACK_P (targ)) | |
5862 | { | |
5863 | /* Look inside the argument pack. */ | |
5864 | packed_args = ARGUMENT_PACK_ARGS (targ); | |
5865 | len = TREE_VEC_LENGTH (packed_args); | |
5866 | } | |
5867 | ||
5868 | for (idx = 0; idx < len; ++idx) | |
5869 | { | |
5870 | tree targ_parms = NULL_TREE; | |
5871 | ||
5872 | if (packed_args) | |
5873 | /* Extract the next argument from the argument | |
5874 | pack. */ | |
5875 | targ = TREE_VEC_ELT (packed_args, idx); | |
5876 | ||
5877 | if (PACK_EXPANSION_P (targ)) | |
5878 | /* Look at the pattern of the pack expansion. */ | |
5879 | targ = PACK_EXPANSION_PATTERN (targ); | |
5880 | ||
5881 | /* Extract the template parameters from the template | |
5882 | argument. */ | |
5883 | if (TREE_CODE (targ) == TEMPLATE_DECL) | |
5884 | targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ); | |
5885 | else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM) | |
5886 | targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ)); | |
5887 | ||
5888 | /* Verify that we can coerce the template template | |
5889 | parameters from the template argument to the template | |
5890 | parameter. This requires an exact match. */ | |
5891 | if (targ_parms | |
5892 | && !coerce_template_template_parms | |
5893 | (DECL_INNERMOST_TEMPLATE_PARMS (tparm), | |
5894 | targ_parms, | |
5895 | tf_none, | |
5896 | tparm, | |
5897 | targs)) | |
ee34d21a JM |
5898 | { |
5899 | ret = false; | |
5900 | goto out; | |
5901 | } | |
4cf36211 DG |
5902 | } |
5903 | } | |
5904 | } | |
5905 | ||
ee34d21a JM |
5906 | out: |
5907 | ||
5908 | --processing_template_decl; | |
5909 | return ret; | |
4cf36211 DG |
5910 | } |
5911 | ||
8b5b8b7c MM |
5912 | /* Convert the indicated template ARG as necessary to match the |
5913 | indicated template PARM. Returns the converted ARG, or | |
c2ea3a40 NS |
5914 | error_mark_node if the conversion was unsuccessful. Error and |
5915 | warning messages are issued under control of COMPLAIN. This | |
5916 | conversion is for the Ith parameter in the parameter list. ARGS is | |
5917 | the full set of template arguments deduced so far. */ | |
8b5b8b7c MM |
5918 | |
5919 | static tree | |
c8094d83 | 5920 | convert_template_argument (tree parm, |
0cbd7506 MS |
5921 | tree arg, |
5922 | tree args, | |
5923 | tsubst_flags_t complain, | |
5924 | int i, | |
5925 | tree in_decl) | |
8b5b8b7c | 5926 | { |
88b82314 | 5927 | tree orig_arg; |
8b5b8b7c | 5928 | tree val; |
8b5b8b7c | 5929 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; |
c8094d83 | 5930 | |
c8094d83 | 5931 | if (TREE_CODE (arg) == TREE_LIST |
a5ac359a | 5932 | && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF) |
c8094d83 | 5933 | { |
8b5b8b7c MM |
5934 | /* The template argument was the name of some |
5935 | member function. That's usually | |
0e339752 | 5936 | invalid, but static members are OK. In any |
8b5b8b7c MM |
5937 | case, grab the underlying fields/functions |
5938 | and issue an error later if required. */ | |
88b82314 | 5939 | orig_arg = TREE_VALUE (arg); |
8b5b8b7c MM |
5940 | TREE_TYPE (arg) = unknown_type_node; |
5941 | } | |
5942 | ||
88b82314 DG |
5943 | orig_arg = arg; |
5944 | ||
8b5b8b7c MM |
5945 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; |
5946 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
5947 | || requires_tmpl_type); | |
5948 | ||
d732e98f | 5949 | /* When determining whether an argument pack expansion is a template, |
5d80a306 | 5950 | look at the pattern. */ |
88b82314 DG |
5951 | if (TREE_CODE (arg) == TYPE_PACK_EXPANSION) |
5952 | arg = PACK_EXPANSION_PATTERN (arg); | |
5d80a306 | 5953 | |
98fba7f7 JM |
5954 | /* Deal with an injected-class-name used as a template template arg. */ |
5955 | if (requires_tmpl_type && CLASS_TYPE_P (arg)) | |
5956 | { | |
5957 | tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg)); | |
5958 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
5959 | { | |
5960 | if (complain & tf_warning_or_error) | |
5961 | pedwarn (input_location, OPT_pedantic, "injected-class-name %qD" | |
5962 | " used as template template argument", TYPE_NAME (arg)); | |
5963 | else if (flag_pedantic_errors) | |
5964 | t = arg; | |
5965 | ||
5966 | arg = t; | |
5967 | } | |
5968 | } | |
5969 | ||
5d80a306 | 5970 | is_tmpl_type = |
88b82314 DG |
5971 | ((TREE_CODE (arg) == TEMPLATE_DECL |
5972 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
5973 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
5974 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); | |
c8094d83 | 5975 | |
b8c6534b KL |
5976 | if (is_tmpl_type |
5977 | && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
5978 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) | |
8b5b8b7c | 5979 | arg = TYPE_STUB_DECL (arg); |
8b5b8b7c | 5980 | |
2f939d94 | 5981 | is_type = TYPE_P (arg) || is_tmpl_type; |
8b5b8b7c MM |
5982 | |
5983 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
5984 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
5985 | { | |
cbe5f3b3 | 5986 | permerror (input_location, "to refer to a type member of a template parameter, " |
37ec60ed | 5987 | "use %<typename %E%>", orig_arg); |
c8094d83 | 5988 | |
88b82314 DG |
5989 | orig_arg = make_typename_type (TREE_OPERAND (arg, 0), |
5990 | TREE_OPERAND (arg, 1), | |
5991 | typename_type, | |
5992 | complain & tf_error); | |
5993 | arg = orig_arg; | |
8b5b8b7c MM |
5994 | is_type = 1; |
5995 | } | |
5996 | if (is_type != requires_type) | |
5997 | { | |
5998 | if (in_decl) | |
5999 | { | |
c2ea3a40 | 6000 | if (complain & tf_error) |
8b5b8b7c | 6001 | { |
0f51ccfc | 6002 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
6003 | "parameter list for %qD", |
6004 | i + 1, in_decl); | |
8b5b8b7c | 6005 | if (is_type) |
0f51ccfc | 6006 | error (" expected a constant of type %qT, got %qT", |
0cbd7506 | 6007 | TREE_TYPE (parm), |
1ad8aeeb | 6008 | (DECL_P (arg) ? DECL_NAME (arg) : orig_arg)); |
d12a7283 | 6009 | else if (requires_tmpl_type) |
88b82314 | 6010 | error (" expected a class template, got %qE", orig_arg); |
8b5b8b7c | 6011 | else |
88b82314 | 6012 | error (" expected a type, got %qE", orig_arg); |
8b5b8b7c MM |
6013 | } |
6014 | } | |
6015 | return error_mark_node; | |
6016 | } | |
6017 | if (is_tmpl_type ^ requires_tmpl_type) | |
6018 | { | |
c2ea3a40 | 6019 | if (in_decl && (complain & tf_error)) |
8b5b8b7c | 6020 | { |
0f51ccfc | 6021 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
6022 | "parameter list for %qD", |
6023 | i + 1, in_decl); | |
8b5b8b7c | 6024 | if (is_tmpl_type) |
0f51ccfc | 6025 | error (" expected a type, got %qT", DECL_NAME (arg)); |
8b5b8b7c | 6026 | else |
88b82314 | 6027 | error (" expected a class template, got %qT", orig_arg); |
8b5b8b7c MM |
6028 | } |
6029 | return error_mark_node; | |
6030 | } | |
c8094d83 | 6031 | |
8b5b8b7c MM |
6032 | if (is_type) |
6033 | { | |
6034 | if (requires_tmpl_type) | |
6035 | { | |
b8c6534b KL |
6036 | if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) |
6037 | /* The number of argument required is not known yet. | |
6038 | Just accept it for now. */ | |
6039 | val = TREE_TYPE (arg); | |
8b5b8b7c MM |
6040 | else |
6041 | { | |
b8c6534b | 6042 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); |
5d80a306 DG |
6043 | tree argparm; |
6044 | ||
88b82314 | 6045 | argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); |
b8c6534b KL |
6046 | |
6047 | if (coerce_template_template_parms (parmparm, argparm, | |
6048 | complain, in_decl, | |
6150b602 | 6049 | args)) |
8b5b8b7c | 6050 | { |
5a080ad7 | 6051 | val = arg; |
c8094d83 MS |
6052 | |
6053 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
b8c6534b | 6054 | TEMPLATE_DECL. */ |
5d80a306 DG |
6055 | if (val != error_mark_node) |
6056 | { | |
6057 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
6058 | val = TREE_TYPE (val); | |
5a080ad7 JM |
6059 | if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION) |
6060 | val = make_pack_expansion (val); | |
5d80a306 | 6061 | } |
8b5b8b7c | 6062 | } |
b8c6534b KL |
6063 | else |
6064 | { | |
c2ea3a40 | 6065 | if (in_decl && (complain & tf_error)) |
b8c6534b | 6066 | { |
0f51ccfc | 6067 | error ("type/value mismatch at argument %d in " |
0cbd7506 MS |
6068 | "template parameter list for %qD", |
6069 | i + 1, in_decl); | |
5b6dad5d | 6070 | error (" expected a template of type %qD, got %qT", |
88b82314 | 6071 | parm, orig_arg); |
b8c6534b | 6072 | } |
c8094d83 | 6073 | |
b8c6534b KL |
6074 | val = error_mark_node; |
6075 | } | |
8b5b8b7c MM |
6076 | } |
6077 | } | |
6078 | else | |
88b82314 | 6079 | val = orig_arg; |
685e39c2 MM |
6080 | /* We only form one instance of each template specialization. |
6081 | Therefore, if we use a non-canonical variant (i.e., a | |
3db45ab5 | 6082 | typedef), any future messages referring to the type will use |
685e39c2 MM |
6083 | the typedef, which is confusing if those future uses do not |
6084 | themselves also use the typedef. */ | |
6085 | if (TYPE_P (val)) | |
cd41d410 | 6086 | val = strip_typedefs (val); |
8b5b8b7c MM |
6087 | } |
6088 | else | |
6089 | { | |
4393e105 | 6090 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c | 6091 | |
11b810f1 | 6092 | if (invalid_nontype_parm_type_p (t, complain)) |
0cbd7506 | 6093 | return error_mark_node; |
c8094d83 | 6094 | |
ffaf1e05 JM |
6095 | if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg)) |
6096 | { | |
6097 | if (same_type_p (t, TREE_TYPE (orig_arg))) | |
6098 | val = orig_arg; | |
6099 | else | |
6100 | { | |
6101 | /* Not sure if this is reachable, but it doesn't hurt | |
6102 | to be robust. */ | |
6103 | error ("type mismatch in nontype parameter pack"); | |
6104 | val = error_mark_node; | |
6105 | } | |
6106 | } | |
6107 | else if (!uses_template_parms (orig_arg) && !uses_template_parms (t)) | |
8b5b8b7c MM |
6108 | /* We used to call digest_init here. However, digest_init |
6109 | will report errors, which we don't want when complain | |
6110 | is zero. More importantly, digest_init will try too | |
6111 | hard to convert things: for example, `0' should not be | |
6112 | converted to pointer type at this point according to | |
6113 | the standard. Accepting this is not merely an | |
6114 | extension, since deciding whether or not these | |
6115 | conversions can occur is part of determining which | |
dc957d14 | 6116 | function template to call, or whether a given explicit |
0e339752 | 6117 | argument specification is valid. */ |
40013af7 | 6118 | val = convert_nontype_argument (t, orig_arg, complain); |
8b5b8b7c | 6119 | else |
88b82314 | 6120 | val = orig_arg; |
8b5b8b7c MM |
6121 | |
6122 | if (val == NULL_TREE) | |
6123 | val = error_mark_node; | |
c2ea3a40 | 6124 | else if (val == error_mark_node && (complain & tf_error)) |
88b82314 | 6125 | error ("could not convert template argument %qE to %qT", orig_arg, t); |
c9e90045 DS |
6126 | |
6127 | if (TREE_CODE (val) == SCOPE_REF) | |
6128 | { | |
6129 | /* Strip typedefs from the SCOPE_REF. */ | |
6130 | tree type = strip_typedefs (TREE_TYPE (val)); | |
6131 | tree scope = strip_typedefs (TREE_OPERAND (val, 0)); | |
6132 | val = build_qualified_name (type, scope, TREE_OPERAND (val, 1), | |
6133 | QUALIFIED_NAME_IS_TEMPLATE (val)); | |
6134 | } | |
8b5b8b7c MM |
6135 | } |
6136 | ||
6137 | return val; | |
6138 | } | |
6139 | ||
b1d7b1c0 DG |
6140 | /* Coerces the remaining template arguments in INNER_ARGS (from |
6141 | ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS. | |
6142 | Returns the coerced argument pack. PARM_IDX is the position of this | |
6143 | parameter in the template parameter list. ARGS is the original | |
6144 | template argument list. */ | |
6145 | static tree | |
6146 | coerce_template_parameter_pack (tree parms, | |
6147 | int parm_idx, | |
6148 | tree args, | |
6149 | tree inner_args, | |
6150 | int arg_idx, | |
6151 | tree new_args, | |
6152 | int* lost, | |
6153 | tree in_decl, | |
6154 | tsubst_flags_t complain) | |
6155 | { | |
6156 | tree parm = TREE_VEC_ELT (parms, parm_idx); | |
6157 | int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; | |
6158 | tree packed_args; | |
6159 | tree argument_pack; | |
6160 | tree packed_types = NULL_TREE; | |
6161 | ||
6162 | if (arg_idx > nargs) | |
6163 | arg_idx = nargs; | |
6164 | ||
6165 | packed_args = make_tree_vec (nargs - arg_idx); | |
6166 | ||
6167 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL | |
6168 | && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm)))) | |
6169 | { | |
6170 | /* When the template parameter is a non-type template | |
6171 | parameter pack whose type uses parameter packs, we need | |
6172 | to look at each of the template arguments | |
6173 | separately. Build a vector of the types for these | |
6174 | non-type template parameters in PACKED_TYPES. */ | |
6175 | tree expansion | |
6176 | = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm))); | |
6177 | packed_types = tsubst_pack_expansion (expansion, args, | |
6178 | complain, in_decl); | |
6179 | ||
6180 | if (packed_types == error_mark_node) | |
6181 | return error_mark_node; | |
6182 | ||
6183 | /* Check that we have the right number of arguments. */ | |
6184 | if (arg_idx < nargs | |
6185 | && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)) | |
6186 | && nargs - arg_idx != TREE_VEC_LENGTH (packed_types)) | |
6187 | { | |
6188 | int needed_parms | |
6189 | = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types); | |
6190 | error ("wrong number of template arguments (%d, should be %d)", | |
6191 | nargs, needed_parms); | |
6192 | return error_mark_node; | |
6193 | } | |
6194 | ||
6195 | /* If we aren't able to check the actual arguments now | |
6196 | (because they haven't been expanded yet), we can at least | |
6197 | verify that all of the types used for the non-type | |
6198 | template parameter pack are, in fact, valid for non-type | |
6199 | template parameters. */ | |
6200 | if (arg_idx < nargs | |
6201 | && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))) | |
6202 | { | |
6203 | int j, len = TREE_VEC_LENGTH (packed_types); | |
6204 | for (j = 0; j < len; ++j) | |
6205 | { | |
6206 | tree t = TREE_VEC_ELT (packed_types, j); | |
6207 | if (invalid_nontype_parm_type_p (t, complain)) | |
6208 | return error_mark_node; | |
6209 | } | |
6210 | } | |
6211 | } | |
6212 | ||
6213 | /* Convert the remaining arguments, which will be a part of the | |
6214 | parameter pack "parm". */ | |
6215 | for (; arg_idx < nargs; ++arg_idx) | |
6216 | { | |
6217 | tree arg = TREE_VEC_ELT (inner_args, arg_idx); | |
6218 | tree actual_parm = TREE_VALUE (parm); | |
6219 | ||
6220 | if (packed_types && !PACK_EXPANSION_P (arg)) | |
6221 | { | |
6222 | /* When we have a vector of types (corresponding to the | |
6223 | non-type template parameter pack that uses parameter | |
6224 | packs in its type, as mention above), and the | |
6225 | argument is not an expansion (which expands to a | |
6226 | currently unknown number of arguments), clone the | |
6227 | parm and give it the next type in PACKED_TYPES. */ | |
6228 | actual_parm = copy_node (actual_parm); | |
6229 | TREE_TYPE (actual_parm) = | |
6230 | TREE_VEC_ELT (packed_types, arg_idx - parm_idx); | |
6231 | } | |
6232 | ||
c343d5a7 PC |
6233 | if (arg != error_mark_node) |
6234 | arg = convert_template_argument (actual_parm, | |
6235 | arg, new_args, complain, parm_idx, | |
6236 | in_decl); | |
b1d7b1c0 DG |
6237 | if (arg == error_mark_node) |
6238 | (*lost)++; | |
6239 | TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg; | |
6240 | } | |
6241 | ||
6242 | if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL | |
6243 | || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL) | |
7ecbca9d | 6244 | argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK); |
b1d7b1c0 DG |
6245 | else |
6246 | { | |
6247 | argument_pack = make_node (NONTYPE_ARGUMENT_PACK); | |
88b82314 | 6248 | TREE_TYPE (argument_pack) |
cdcae745 | 6249 | = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl); |
b1d7b1c0 DG |
6250 | TREE_CONSTANT (argument_pack) = 1; |
6251 | } | |
6252 | ||
6253 | SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args); | |
1a048f82 DS |
6254 | #ifdef ENABLE_CHECKING |
6255 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args, | |
6256 | TREE_VEC_LENGTH (packed_args)); | |
6257 | #endif | |
b1d7b1c0 DG |
6258 | return argument_pack; |
6259 | } | |
6260 | ||
8b5b8b7c MM |
6261 | /* Convert all template arguments to their appropriate types, and |
6262 | return a vector containing the innermost resulting template | |
c2ea3a40 | 6263 | arguments. If any error occurs, return error_mark_node. Error and |
3e4a3562 | 6264 | warning messages are issued under control of COMPLAIN. |
75650646 | 6265 | |
e7e93965 | 6266 | If REQUIRE_ALL_ARGS is false, argument deduction will be performed |
84dc00e8 | 6267 | for arguments not specified in ARGS. Otherwise, if |
e7e93965 MM |
6268 | USE_DEFAULT_ARGS is true, default arguments will be used to fill in |
6269 | unspecified arguments. If REQUIRE_ALL_ARGS is true, but | |
6270 | USE_DEFAULT_ARGS is false, then all arguments must be specified in | |
6271 | ARGS. */ | |
c8094d83 | 6272 | |
8d08fdba | 6273 | static tree |
c8094d83 | 6274 | coerce_template_parms (tree parms, |
0cbd7506 MS |
6275 | tree args, |
6276 | tree in_decl, | |
3a978d72 | 6277 | tsubst_flags_t complain, |
e7e93965 MM |
6278 | bool require_all_args, |
6279 | bool use_default_args) | |
8d08fdba | 6280 | { |
b1d7b1c0 | 6281 | int nparms, nargs, parm_idx, arg_idx, lost = 0; |
e4a84209 | 6282 | tree inner_args; |
8b5b8b7c MM |
6283 | tree new_args; |
6284 | tree new_inner_args; | |
7d882b83 ILT |
6285 | int saved_unevaluated_operand; |
6286 | int saved_inhibit_evaluation_warnings; | |
a292b002 | 6287 | |
5d80a306 DG |
6288 | /* When used as a boolean value, indicates whether this is a |
6289 | variadic template parameter list. Since it's an int, we can also | |
6290 | subtract it from nparms to get the number of non-variadic | |
6291 | parameters. */ | |
b1d7b1c0 | 6292 | int variadic_p = 0; |
5d80a306 | 6293 | |
fd9852df PC |
6294 | if (args == error_mark_node) |
6295 | return error_mark_node; | |
6296 | ||
a292b002 MS |
6297 | nparms = TREE_VEC_LENGTH (parms); |
6298 | ||
b1d7b1c0 DG |
6299 | /* Determine if there are any parameter packs. */ |
6300 | for (parm_idx = 0; parm_idx < nparms; ++parm_idx) | |
6301 | { | |
6302 | tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx)); | |
6303 | if (template_parameter_pack_p (tparm)) | |
ffaf1e05 | 6304 | ++variadic_p; |
b1d7b1c0 DG |
6305 | } |
6306 | ||
ffaf1e05 JM |
6307 | inner_args = INNERMOST_TEMPLATE_ARGS (args); |
6308 | /* If there are 0 or 1 parameter packs, we need to expand any argument | |
6309 | packs so that we can deduce a parameter pack from some non-packed args | |
6310 | followed by an argument pack, as in variadic85.C. If there are more | |
6311 | than that, we need to leave argument packs intact so the arguments are | |
6312 | assigned to the right parameter packs. This should only happen when | |
6313 | dealing with a nested class inside a partial specialization of a class | |
6314 | template, as in variadic92.C. */ | |
6315 | if (variadic_p <= 1) | |
6316 | inner_args = expand_template_argument_pack (inner_args); | |
6317 | ||
6318 | nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; | |
6319 | if ((nargs > nparms && !variadic_p) | |
5d80a306 | 6320 | || (nargs < nparms - variadic_p |
e7e93965 MM |
6321 | && require_all_args |
6322 | && (!use_default_args | |
42b304f1 LM |
6323 | || (TREE_VEC_ELT (parms, nargs) != error_mark_node |
6324 | && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))) | |
8d08fdba | 6325 | { |
c8094d83 | 6326 | if (complain & tf_error) |
75650646 | 6327 | { |
5d80a306 DG |
6328 | if (variadic_p) |
6329 | { | |
5d80a306 | 6330 | --nparms; |
2f5b91f5 SZ |
6331 | error ("wrong number of template arguments " |
6332 | "(%d, should be %d or more)", nargs, nparms); | |
5d80a306 | 6333 | } |
2f5b91f5 SZ |
6334 | else |
6335 | error ("wrong number of template arguments " | |
6336 | "(%d, should be %d)", nargs, nparms); | |
c8094d83 | 6337 | |
75650646 | 6338 | if (in_decl) |
dee15844 | 6339 | error ("provided for %q+D", in_decl); |
75650646 MM |
6340 | } |
6341 | ||
8d08fdba MS |
6342 | return error_mark_node; |
6343 | } | |
6344 | ||
a2c5ed87 MM |
6345 | /* We need to evaluate the template arguments, even though this |
6346 | template-id may be nested within a "sizeof". */ | |
7d882b83 ILT |
6347 | saved_unevaluated_operand = cp_unevaluated_operand; |
6348 | cp_unevaluated_operand = 0; | |
6349 | saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; | |
6350 | c_inhibit_evaluation_warnings = 0; | |
f31c0a32 | 6351 | new_inner_args = make_tree_vec (nparms); |
8b5b8b7c | 6352 | new_args = add_outermost_template_args (args, new_inner_args); |
b1d7b1c0 | 6353 | for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++) |
8d08fdba | 6354 | { |
8b5b8b7c MM |
6355 | tree arg; |
6356 | tree parm; | |
e4a84209 | 6357 | |
8b5b8b7c | 6358 | /* Get the Ith template parameter. */ |
b1d7b1c0 | 6359 | parm = TREE_VEC_ELT (parms, parm_idx); |
42b304f1 LM |
6360 | |
6361 | if (parm == error_mark_node) | |
2d8ba2c7 | 6362 | { |
b1d7b1c0 | 6363 | TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node; |
42b304f1 | 6364 | continue; |
2d8ba2c7 | 6365 | } |
75650646 | 6366 | |
b1d7b1c0 | 6367 | /* Calculate the next argument. */ |
ffaf1e05 JM |
6368 | if (arg_idx < nargs) |
6369 | arg = TREE_VEC_ELT (inner_args, arg_idx); | |
6370 | else | |
6371 | arg = NULL_TREE; | |
6372 | ||
6373 | if (template_parameter_pack_p (TREE_VALUE (parm)) | |
6374 | && !(arg && ARGUMENT_PACK_P (arg))) | |
5d80a306 | 6375 | { |
ffaf1e05 JM |
6376 | /* All remaining arguments will be placed in the |
6377 | template parameter pack PARM. */ | |
6378 | arg = coerce_template_parameter_pack (parms, parm_idx, args, | |
6379 | inner_args, arg_idx, | |
6380 | new_args, &lost, | |
6381 | in_decl, complain); | |
6382 | ||
b1d7b1c0 DG |
6383 | /* Store this argument. */ |
6384 | if (arg == error_mark_node) | |
6385 | lost++; | |
6386 | TREE_VEC_ELT (new_inner_args, parm_idx) = arg; | |
6387 | ||
ffaf1e05 JM |
6388 | /* We are done with all of the arguments. */ |
6389 | arg_idx = nargs; | |
6390 | ||
b1d7b1c0 DG |
6391 | continue; |
6392 | } | |
ffaf1e05 JM |
6393 | else if (arg) |
6394 | { | |
6395 | if (PACK_EXPANSION_P (arg)) | |
5d80a306 | 6396 | { |
4cf36211 DG |
6397 | if (complain & tf_error) |
6398 | { | |
ffaf1e05 JM |
6399 | /* FIXME this restriction was removed by N2555; see |
6400 | bug 35722. */ | |
4cf36211 DG |
6401 | /* If ARG is a pack expansion, but PARM is not a |
6402 | template parameter pack (if it were, we would have | |
6403 | handled it above), we're trying to expand into a | |
6404 | fixed-length argument list. */ | |
6405 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
ffaf1e05 | 6406 | sorry ("cannot expand %<%E%> into a fixed-length " |
4cf36211 DG |
6407 | "argument list", arg); |
6408 | else | |
ffaf1e05 | 6409 | sorry ("cannot expand %<%T%> into a fixed-length " |
4cf36211 DG |
6410 | "argument list", arg); |
6411 | } | |
c343d5a7 | 6412 | return error_mark_node; |
5d80a306 DG |
6413 | } |
6414 | } | |
e7e93965 | 6415 | else if (require_all_args) |
1a048f82 DS |
6416 | { |
6417 | /* There must be a default arg in this case. */ | |
6418 | arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args, | |
6419 | complain, in_decl); | |
6420 | /* The position of the first default template argument, | |
6421 | is also the number of non-defaulted arguments in NEW_INNER_ARGS. | |
6422 | Record that. */ | |
6423 | if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args)) | |
6424 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx); | |
6425 | } | |
ffd49b19 NS |
6426 | else |
6427 | break; | |
c8094d83 | 6428 | |
ffd49b19 | 6429 | if (arg == error_mark_node) |
e34b0922 KL |
6430 | { |
6431 | if (complain & tf_error) | |
b1d7b1c0 | 6432 | error ("template argument %d is invalid", arg_idx + 1); |
e34b0922 | 6433 | } |
b1d7b1c0 DG |
6434 | else if (!arg) |
6435 | /* This only occurs if there was an error in the template | |
6436 | parameter list itself (which we would already have | |
6437 | reported) that we are trying to recover from, e.g., a class | |
6438 | template with a parameter list such as | |
6439 | template<typename..., typename>. */ | |
6440 | return error_mark_node; | |
c8094d83 MS |
6441 | else |
6442 | arg = convert_template_argument (TREE_VALUE (parm), | |
b1d7b1c0 DG |
6443 | arg, new_args, complain, |
6444 | parm_idx, in_decl); | |
c8094d83 | 6445 | |
8b5b8b7c | 6446 | if (arg == error_mark_node) |
8d08fdba | 6447 | lost++; |
b1d7b1c0 | 6448 | TREE_VEC_ELT (new_inner_args, arg_idx) = arg; |
8d08fdba | 6449 | } |
7d882b83 ILT |
6450 | cp_unevaluated_operand = saved_unevaluated_operand; |
6451 | c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings; | |
8b5b8b7c | 6452 | |
8d08fdba MS |
6453 | if (lost) |
6454 | return error_mark_node; | |
8b5b8b7c | 6455 | |
1a048f82 DS |
6456 | #ifdef ENABLE_CHECKING |
6457 | if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args)) | |
6458 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, | |
6459 | TREE_VEC_LENGTH (new_inner_args)); | |
6460 | #endif | |
6461 | ||
8b5b8b7c | 6462 | return new_inner_args; |
8d08fdba MS |
6463 | } |
6464 | ||
34016c81 JM |
6465 | /* Returns 1 if template args OT and NT are equivalent. */ |
6466 | ||
d8e178a0 | 6467 | static int |
3a978d72 | 6468 | template_args_equal (tree ot, tree nt) |
34016c81 JM |
6469 | { |
6470 | if (nt == ot) | |
6471 | return 1; | |
74601d7c | 6472 | |
34016c81 JM |
6473 | if (TREE_CODE (nt) == TREE_VEC) |
6474 | /* For member templates */ | |
74601d7c | 6475 | return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); |
5d80a306 DG |
6476 | else if (PACK_EXPANSION_P (ot)) |
6477 | return PACK_EXPANSION_P (nt) | |
6478 | && template_args_equal (PACK_EXPANSION_PATTERN (ot), | |
6479 | PACK_EXPANSION_PATTERN (nt)); | |
ffaf1e05 JM |
6480 | else if (ARGUMENT_PACK_P (ot)) |
6481 | { | |
6482 | int i, len; | |
6483 | tree opack, npack; | |
6484 | ||
6485 | if (!ARGUMENT_PACK_P (nt)) | |
6486 | return 0; | |
6487 | ||
6488 | opack = ARGUMENT_PACK_ARGS (ot); | |
6489 | npack = ARGUMENT_PACK_ARGS (nt); | |
6490 | len = TREE_VEC_LENGTH (opack); | |
6491 | if (TREE_VEC_LENGTH (npack) != len) | |
6492 | return 0; | |
6493 | for (i = 0; i < len; ++i) | |
6494 | if (!template_args_equal (TREE_VEC_ELT (opack, i), | |
6495 | TREE_VEC_ELT (npack, i))) | |
6496 | return 0; | |
6497 | return 1; | |
6498 | } | |
f96d6fd0 DS |
6499 | else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT) |
6500 | { | |
6501 | /* We get here probably because we are in the middle of substituting | |
6502 | into the pattern of a pack expansion. In that case the | |
6503 | ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are | |
6504 | interested in. So we want to use the initial pack argument for | |
6505 | the comparison. */ | |
6506 | ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot); | |
6507 | if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT) | |
6508 | nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt); | |
6509 | return template_args_equal (ot, nt); | |
6510 | } | |
74601d7c | 6511 | else if (TYPE_P (nt)) |
660845bf | 6512 | return TYPE_P (ot) && same_type_p (ot, nt); |
74601d7c KL |
6513 | else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) |
6514 | return 0; | |
34016c81 | 6515 | else |
c8a209ca | 6516 | return cp_tree_equal (ot, nt); |
34016c81 JM |
6517 | } |
6518 | ||
6519 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
6520 | of template arguments. Returns 0 otherwise. */ |
6521 | ||
6757edfe | 6522 | int |
3a978d72 | 6523 | comp_template_args (tree oldargs, tree newargs) |
5566b478 MS |
6524 | { |
6525 | int i; | |
6526 | ||
386b8a85 JM |
6527 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
6528 | return 0; | |
6529 | ||
5566b478 MS |
6530 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
6531 | { | |
6532 | tree nt = TREE_VEC_ELT (newargs, i); | |
6533 | tree ot = TREE_VEC_ELT (oldargs, i); | |
6534 | ||
34016c81 | 6535 | if (! template_args_equal (ot, nt)) |
61a127b3 | 6536 | return 0; |
5566b478 MS |
6537 | } |
6538 | return 1; | |
6539 | } | |
6540 | ||
5566b478 | 6541 | static void |
3a978d72 | 6542 | add_pending_template (tree d) |
5566b478 | 6543 | { |
3ae18eaf JM |
6544 | tree ti = (TYPE_P (d) |
6545 | ? CLASSTYPE_TEMPLATE_INFO (d) | |
6546 | : DECL_TEMPLATE_INFO (d)); | |
e2c3721c | 6547 | struct pending_template *pt; |
3ae18eaf | 6548 | int level; |
e92cc029 | 6549 | |
824b9a4c | 6550 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
6551 | return; |
6552 | ||
3ae18eaf JM |
6553 | /* We are called both from instantiate_decl, where we've already had a |
6554 | tinst_level pushed, and instantiate_template, where we haven't. | |
6555 | Compensate. */ | |
e2c3721c | 6556 | level = !current_tinst_level || current_tinst_level->decl != d; |
3ae18eaf JM |
6557 | |
6558 | if (level) | |
6559 | push_tinst_level (d); | |
6560 | ||
a9429e29 | 6561 | pt = ggc_alloc_pending_template (); |
e2c3721c PB |
6562 | pt->next = NULL; |
6563 | pt->tinst = current_tinst_level; | |
46ccf50a | 6564 | if (last_pending_template) |
e2c3721c | 6565 | last_pending_template->next = pt; |
46ccf50a JM |
6566 | else |
6567 | pending_templates = pt; | |
6568 | ||
6569 | last_pending_template = pt; | |
6570 | ||
824b9a4c | 6571 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
3ae18eaf JM |
6572 | |
6573 | if (level) | |
6574 | pop_tinst_level (); | |
5566b478 MS |
6575 | } |
6576 | ||
386b8a85 | 6577 | |
4ba126e4 MM |
6578 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and |
6579 | ARGLIST. Valid choices for FNS are given in the cp-tree.def | |
6580 | documentation for TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
6581 | |
6582 | tree | |
3a978d72 | 6583 | lookup_template_function (tree fns, tree arglist) |
386b8a85 | 6584 | { |
2c73f9f5 | 6585 | tree type; |
050367a3 | 6586 | |
4ba126e4 MM |
6587 | if (fns == error_mark_node || arglist == error_mark_node) |
6588 | return error_mark_node; | |
6589 | ||
50bc768d | 6590 | gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); |
4230cec2 NS |
6591 | gcc_assert (fns && (is_overloaded_fn (fns) |
6592 | || TREE_CODE (fns) == IDENTIFIER_NODE)); | |
4ba126e4 | 6593 | |
50ad9642 MM |
6594 | if (BASELINK_P (fns)) |
6595 | { | |
f293ce4b RS |
6596 | BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR, |
6597 | unknown_type_node, | |
6598 | BASELINK_FUNCTIONS (fns), | |
6599 | arglist); | |
50ad9642 MM |
6600 | return fns; |
6601 | } | |
6602 | ||
2c73f9f5 ML |
6603 | type = TREE_TYPE (fns); |
6604 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
6605 | type = unknown_type_node; | |
c8094d83 | 6606 | |
f293ce4b | 6607 | return build2 (TEMPLATE_ID_EXPR, type, fns, arglist); |
386b8a85 JM |
6608 | } |
6609 | ||
a2b60a0e MM |
6610 | /* Within the scope of a template class S<T>, the name S gets bound |
6611 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
6612 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
6613 | or one of its enclosing classes, and that type is a template, | |
6614 | return the associated TEMPLATE_DECL. Otherwise, the original | |
5a080ad7 JM |
6615 | DECL is returned. |
6616 | ||
6617 | Also handle the case when DECL is a TREE_LIST of ambiguous | |
6618 | injected-class-names from different bases. */ | |
a2b60a0e | 6619 | |
a723baf1 | 6620 | tree |
3a978d72 | 6621 | maybe_get_template_decl_from_type_decl (tree decl) |
a2b60a0e | 6622 | { |
5a080ad7 JM |
6623 | if (decl == NULL_TREE) |
6624 | return decl; | |
6625 | ||
6626 | /* DR 176: A lookup that finds an injected-class-name (10.2 | |
6627 | [class.member.lookup]) can result in an ambiguity in certain cases | |
6628 | (for example, if it is found in more than one base class). If all of | |
6629 | the injected-class-names that are found refer to specializations of | |
6630 | the same class template, and if the name is followed by a | |
6631 | template-argument-list, the reference refers to the class template | |
6632 | itself and not a specialization thereof, and is not ambiguous. */ | |
6633 | if (TREE_CODE (decl) == TREE_LIST) | |
6634 | { | |
6635 | tree t, tmpl = NULL_TREE; | |
6636 | for (t = decl; t; t = TREE_CHAIN (t)) | |
6637 | { | |
6638 | tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t)); | |
6639 | if (!tmpl) | |
6640 | tmpl = elt; | |
6641 | else if (tmpl != elt) | |
6642 | break; | |
6643 | } | |
6644 | if (tmpl && t == NULL_TREE) | |
6645 | return tmpl; | |
6646 | else | |
6647 | return decl; | |
6648 | } | |
6649 | ||
a2b60a0e | 6650 | return (decl != NULL_TREE |
5a080ad7 | 6651 | && DECL_SELF_REFERENCE_P (decl) |
c8094d83 | 6652 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
a2b60a0e MM |
6653 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; |
6654 | } | |
386b8a85 | 6655 | |
8d08fdba MS |
6656 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
6657 | parameters, find the desired type. | |
6658 | ||
6659 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
8d08fdba MS |
6660 | |
6661 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
c8094d83 | 6662 | instantiate. |
75650646 | 6663 | |
838dfd8a | 6664 | If ENTERING_SCOPE is nonzero, we are about to enter the scope of |
36a117a5 | 6665 | the class we are looking up. |
c8094d83 | 6666 | |
c2ea3a40 | 6667 | Issue error and warning messages under control of COMPLAIN. |
36a117a5 | 6668 | |
75650646 MM |
6669 | If the template class is really a local class in a template |
6670 | function, then the FUNCTION_CONTEXT is the function in which it is | |
c8094d83 | 6671 | being instantiated. |
b6ab6892 GB |
6672 | |
6673 | ??? Note that this function is currently called *twice* for each | |
6674 | template-id: the first time from the parser, while creating the | |
6675 | incomplete type (finish_template_type), and the second type during the | |
6676 | real instantiation (instantiate_template_class). This is surely something | |
6677 | that we want to avoid. It also causes some problems with argument | |
6678 | coercion (see convert_nontype_argument for more information on this). */ | |
e92cc029 | 6679 | |
8d08fdba | 6680 | tree |
c8094d83 | 6681 | lookup_template_class (tree d1, |
0cbd7506 MS |
6682 | tree arglist, |
6683 | tree in_decl, | |
6684 | tree context, | |
6685 | int entering_scope, | |
6686 | tsubst_flags_t complain) | |
8d08fdba | 6687 | { |
be93747e | 6688 | tree templ = NULL_TREE, parmlist; |
dbfe2124 | 6689 | tree t; |
2b110bfc JM |
6690 | spec_entry **slot; |
6691 | spec_entry *entry; | |
6692 | spec_entry elt; | |
6693 | hashval_t hash; | |
c8094d83 | 6694 | |
fd295cb2 | 6695 | timevar_push (TV_NAME_LOOKUP); |
c8094d83 | 6696 | |
5566b478 MS |
6697 | if (TREE_CODE (d1) == IDENTIFIER_NODE) |
6698 | { | |
90ea9897 MM |
6699 | tree value = innermost_non_namespace_value (d1); |
6700 | if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) | |
be93747e | 6701 | templ = value; |
73b0fce8 KL |
6702 | else |
6703 | { | |
2c73f9f5 ML |
6704 | if (context) |
6705 | push_decl_namespace (context); | |
be93747e KG |
6706 | templ = lookup_name (d1); |
6707 | templ = maybe_get_template_decl_from_type_decl (templ); | |
2c73f9f5 ML |
6708 | if (context) |
6709 | pop_decl_namespace (); | |
73b0fce8 | 6710 | } |
be93747e KG |
6711 | if (templ) |
6712 | context = DECL_CONTEXT (templ); | |
5566b478 | 6713 | } |
9e1e64ec | 6714 | else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1))) |
c91a56d2 | 6715 | { |
802dbc34 JM |
6716 | tree type = TREE_TYPE (d1); |
6717 | ||
6718 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
6719 | an implicit typename for the second A. Deal with it. */ | |
6720 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
6721 | type = TREE_TYPE (type); | |
c8094d83 | 6722 | |
802dbc34 | 6723 | if (CLASSTYPE_TEMPLATE_INFO (type)) |
f3400fe2 | 6724 | { |
be93747e KG |
6725 | templ = CLASSTYPE_TI_TEMPLATE (type); |
6726 | d1 = DECL_NAME (templ); | |
f3400fe2 | 6727 | } |
c91a56d2 | 6728 | } |
c8094d83 | 6729 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
9e1e64ec | 6730 | || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1))) |
5566b478 | 6731 | { |
be93747e KG |
6732 | templ = TYPE_TI_TEMPLATE (d1); |
6733 | d1 = DECL_NAME (templ); | |
5566b478 | 6734 | } |
93cdc044 | 6735 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
b55276c8 | 6736 | && DECL_TEMPLATE_RESULT (d1) |
17aec3eb | 6737 | && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) |
93cdc044 | 6738 | { |
be93747e KG |
6739 | templ = d1; |
6740 | d1 = DECL_NAME (templ); | |
6741 | context = DECL_CONTEXT (templ); | |
93cdc044 | 6742 | } |
8d08fdba | 6743 | |
90ea9897 | 6744 | /* Issue an error message if we didn't find a template. */ |
be93747e | 6745 | if (! templ) |
f3400fe2 | 6746 | { |
c2ea3a40 | 6747 | if (complain & tf_error) |
0cbd7506 | 6748 | error ("%qT is not a template", d1); |
fd295cb2 | 6749 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
f3400fe2 | 6750 | } |
2c73f9f5 | 6751 | |
be93747e | 6752 | if (TREE_CODE (templ) != TEMPLATE_DECL |
0cbd7506 | 6753 | /* Make sure it's a user visible template, if it was named by |
42eaed49 | 6754 | the user. */ |
be93747e KG |
6755 | || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ) |
6756 | && !PRIMARY_TEMPLATE_P (templ))) | |
8d08fdba | 6757 | { |
c2ea3a40 | 6758 | if (complain & tf_error) |
0cbd7506 MS |
6759 | { |
6760 | error ("non-template type %qT used as a template", d1); | |
6761 | if (in_decl) | |
dee15844 | 6762 | error ("for template declaration %q+D", in_decl); |
f9c244b8 | 6763 | } |
fd295cb2 | 6764 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
8d08fdba | 6765 | } |
8d08fdba | 6766 | |
42eaed49 | 6767 | complain &= ~tf_user; |
c8094d83 | 6768 | |
be93747e | 6769 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ)) |
73b0fce8 KL |
6770 | { |
6771 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
0cbd7506 | 6772 | template arguments */ |
73b0fce8 | 6773 | |
1899c3a4 | 6774 | tree parm; |
73b0fce8 | 6775 | tree arglist2; |
ef3b7b17 | 6776 | tree outer; |
73b0fce8 | 6777 | |
be93747e | 6778 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ); |
73b0fce8 | 6779 | |
4f96ff63 KL |
6780 | /* Consider an example where a template template parameter declared as |
6781 | ||
6782 | template <class T, class U = std::allocator<T> > class TT | |
6783 | ||
c8094d83 MS |
6784 | The template parameter level of T and U are one level larger than |
6785 | of TT. To proper process the default argument of U, say when an | |
4f96ff63 | 6786 | instantiation `TT<int>' is seen, we need to build the full |
342cea95 KL |
6787 | arguments containing {int} as the innermost level. Outer levels, |
6788 | available when not appearing as default template argument, can be | |
ef3b7b17 | 6789 | obtained from the arguments of the enclosing template. |
4f96ff63 | 6790 | |
342cea95 KL |
6791 | Suppose that TT is later substituted with std::vector. The above |
6792 | instantiation is `TT<int, std::allocator<T> >' with TT at | |
6793 | level 1, and T at level 2, while the template arguments at level 1 | |
6794 | becomes {std::vector} and the inner level 2 is {int}. */ | |
6795 | ||
be93747e | 6796 | outer = DECL_CONTEXT (templ); |
ef3b7b17 JM |
6797 | if (outer) |
6798 | outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer))); | |
6799 | else if (current_template_parms) | |
6800 | /* This is an argument of the current template, so we haven't set | |
6801 | DECL_CONTEXT yet. */ | |
6802 | outer = current_template_args (); | |
6803 | ||
6804 | if (outer) | |
6805 | arglist = add_to_template_args (outer, arglist); | |
4f96ff63 | 6806 | |
be93747e | 6807 | arglist2 = coerce_template_parms (parmlist, arglist, templ, |
3db45ab5 | 6808 | complain, |
e7e93965 MM |
6809 | /*require_all_args=*/true, |
6810 | /*use_default_args=*/true); | |
3e4a3562 | 6811 | if (arglist2 == error_mark_node |
544aef8c | 6812 | || (!uses_template_parms (arglist2) |
be93747e | 6813 | && check_instantiated_args (templ, arglist2, complain))) |
0cbd7506 | 6814 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
73b0fce8 | 6815 | |
be93747e | 6816 | parm = bind_template_template_parm (TREE_TYPE (templ), arglist2); |
fd295cb2 | 6817 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm); |
73b0fce8 | 6818 | } |
c8094d83 | 6819 | else |
8d08fdba | 6820 | { |
be93747e | 6821 | tree template_type = TREE_TYPE (templ); |
7ac7b28f | 6822 | tree gen_tmpl; |
36a117a5 MM |
6823 | tree type_decl; |
6824 | tree found = NULL_TREE; | |
6825 | int arg_depth; | |
6826 | int parm_depth; | |
04afbf1c DS |
6827 | int is_dependent_type; |
6828 | int use_partial_inst_tmpl = false; | |
830bfa74 | 6829 | |
be93747e | 6830 | gen_tmpl = most_general_template (templ); |
7ac7b28f | 6831 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); |
36a117a5 MM |
6832 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
6833 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
6834 | ||
6835 | if (arg_depth == 1 && parm_depth > 1) | |
6836 | { | |
39c01e4c | 6837 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
6838 | For example, given: |
6839 | ||
6840 | template <class T> struct S1 { | |
0cbd7506 | 6841 | template <class U> struct S2 {}; |
36a117a5 | 6842 | template <class U> struct S2<U*> {}; |
0cbd7506 | 6843 | }; |
c8094d83 | 6844 | |
36a117a5 MM |
6845 | we will be called with an ARGLIST of `U*', but the |
6846 | TEMPLATE will be `template <class T> template | |
6847 | <class U> struct S1<T>::S2'. We must fill in the missing | |
6848 | arguments. */ | |
c8094d83 | 6849 | arglist |
be93747e | 6850 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)), |
7ac7b28f | 6851 | arglist); |
36a117a5 MM |
6852 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
6853 | } | |
5566b478 | 6854 | |
41f5d4b1 | 6855 | /* Now we should have enough arguments. */ |
50bc768d | 6856 | gcc_assert (parm_depth == arg_depth); |
c8094d83 | 6857 | |
7ac7b28f MM |
6858 | /* From here on, we're only interested in the most general |
6859 | template. */ | |
7ac7b28f | 6860 | |
36a117a5 MM |
6861 | /* Calculate the BOUND_ARGS. These will be the args that are |
6862 | actually tsubst'd into the definition to create the | |
6863 | instantiation. */ | |
6864 | if (parm_depth > 1) | |
830bfa74 MM |
6865 | { |
6866 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 6867 | int i; |
e4a84209 | 6868 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 6869 | |
f31c0a32 | 6870 | tree bound_args = make_tree_vec (parm_depth); |
c8094d83 | 6871 | |
e4a84209 | 6872 | for (i = saved_depth, |
2b110bfc | 6873 | t = DECL_TEMPLATE_PARMS (gen_tmpl); |
e4a84209 | 6874 | i > 0 && t != NULL_TREE; |
830bfa74 | 6875 | --i, t = TREE_CHAIN (t)) |
e4a84209 | 6876 | { |
7a25d520 JM |
6877 | tree a; |
6878 | if (i == saved_depth) | |
6879 | a = coerce_template_parms (TREE_VALUE (t), | |
6880 | arglist, gen_tmpl, | |
6881 | complain, | |
6882 | /*require_all_args=*/true, | |
6883 | /*use_default_args=*/true); | |
6884 | else | |
6885 | /* Outer levels should have already been coerced. */ | |
6886 | a = TMPL_ARGS_LEVEL (arglist, i); | |
88e98cfe KL |
6887 | |
6888 | /* Don't process further if one of the levels fails. */ | |
6889 | if (a == error_mark_node) | |
6890 | { | |
6891 | /* Restore the ARGLIST to its full size. */ | |
6892 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
6893 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
6894 | } | |
c8094d83 | 6895 | |
e4a84209 MM |
6896 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); |
6897 | ||
6898 | /* We temporarily reduce the length of the ARGLIST so | |
6899 | that coerce_template_parms will see only the arguments | |
6900 | corresponding to the template parameters it is | |
6901 | examining. */ | |
6902 | TREE_VEC_LENGTH (arglist)--; | |
6903 | } | |
6904 | ||
6905 | /* Restore the ARGLIST to its full size. */ | |
6906 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
6907 | ||
36a117a5 | 6908 | arglist = bound_args; |
830bfa74 MM |
6909 | } |
6910 | else | |
859b9bfa JM |
6911 | arglist |
6912 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
6913 | INNERMOST_TEMPLATE_ARGS (arglist), | |
6914 | gen_tmpl, | |
6915 | complain, | |
6916 | /*require_all_args=*/true, | |
6917 | /*use_default_args=*/true); | |
36a117a5 | 6918 | |
c353b8e3 | 6919 | if (arglist == error_mark_node) |
36a117a5 | 6920 | /* We were unable to bind the arguments. */ |
fd295cb2 | 6921 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
5566b478 | 6922 | |
36a117a5 MM |
6923 | /* In the scope of a template class, explicit references to the |
6924 | template class refer to the type of the template, not any | |
6925 | instantiation of it. For example, in: | |
c8094d83 | 6926 | |
36a117a5 MM |
6927 | template <class T> class C { void f(C<T>); } |
6928 | ||
6929 | the `C<T>' is just the same as `C'. Outside of the | |
6930 | class, however, such a reference is an instantiation. */ | |
fdfacfa1 | 6931 | if ((entering_scope |
2b110bfc | 6932 | || !PRIMARY_TEMPLATE_P (gen_tmpl) |
fdfacfa1 JM |
6933 | || currently_open_class (template_type)) |
6934 | /* comp_template_args is expensive, check it last. */ | |
6935 | && comp_template_args (TYPE_TI_ARGS (template_type), | |
6936 | arglist)) | |
6937 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type); | |
414ea4aa | 6938 | |
c7222c02 | 6939 | /* If we already have this specialization, return it. */ |
2b110bfc JM |
6940 | elt.tmpl = gen_tmpl; |
6941 | elt.args = arglist; | |
6942 | hash = hash_specialization (&elt); | |
6943 | entry = (spec_entry *) htab_find_with_hash (type_specializations, | |
6944 | &elt, hash); | |
6945 | ||
6946 | if (entry) | |
6947 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec); | |
5566b478 | 6948 | |
04afbf1c | 6949 | is_dependent_type = uses_template_parms (arglist); |
dbfe2124 | 6950 | |
c353b8e3 MM |
6951 | /* If the deduced arguments are invalid, then the binding |
6952 | failed. */ | |
04afbf1c | 6953 | if (!is_dependent_type |
2b110bfc | 6954 | && check_instantiated_args (gen_tmpl, |
c353b8e3 MM |
6955 | INNERMOST_TEMPLATE_ARGS (arglist), |
6956 | complain)) | |
6957 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
c8094d83 | 6958 | |
04afbf1c | 6959 | if (!is_dependent_type |
2b110bfc | 6960 | && !PRIMARY_TEMPLATE_P (gen_tmpl) |
d5f4eddd | 6961 | && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl)) |
2b110bfc | 6962 | && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL) |
3ebc5c52 | 6963 | { |
2b110bfc JM |
6964 | found = xref_tag_from_type (TREE_TYPE (gen_tmpl), |
6965 | DECL_NAME (gen_tmpl), | |
29ef83de | 6966 | /*tag_scope=*/ts_global); |
fd295cb2 | 6967 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
3ebc5c52 | 6968 | } |
c8094d83 | 6969 | |
2b110bfc | 6970 | context = tsubst (DECL_CONTEXT (gen_tmpl), arglist, |
c2ea3a40 | 6971 | complain, in_decl); |
95ee998c MM |
6972 | if (!context) |
6973 | context = global_namespace; | |
6974 | ||
36a117a5 | 6975 | /* Create the type. */ |
ed44da02 MM |
6976 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
6977 | { | |
04afbf1c | 6978 | if (!is_dependent_type) |
92777ce4 NS |
6979 | { |
6980 | set_current_access_from_decl (TYPE_NAME (template_type)); | |
90ecaf87 RRC |
6981 | t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE, |
6982 | tsubst (ENUM_UNDERLYING_TYPE (template_type), | |
6983 | arglist, complain, in_decl), | |
6984 | SCOPED_ENUM_P (template_type), NULL); | |
92777ce4 | 6985 | } |
ed44da02 | 6986 | else |
adf2edec DG |
6987 | { |
6988 | /* We don't want to call start_enum for this type, since | |
6989 | the values for the enumeration constants may involve | |
6990 | template parameters. And, no one should be interested | |
6991 | in the enumeration constants for such a type. */ | |
7ecbca9d | 6992 | t = cxx_make_type (ENUMERAL_TYPE); |
adf2edec DG |
6993 | SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type)); |
6994 | } | |
90ecaf87 | 6995 | SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type)); |
ed44da02 MM |
6996 | } |
6997 | else | |
6998 | { | |
9e1e64ec | 6999 | t = make_class_type (TREE_CODE (template_type)); |
c8094d83 | 7000 | CLASSTYPE_DECLARED_CLASS (t) |
ed44da02 | 7001 | = CLASSTYPE_DECLARED_CLASS (template_type); |
ed44da02 | 7002 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); |
f668f160 | 7003 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ae673f14 JM |
7004 | |
7005 | /* A local class. Make sure the decl gets registered properly. */ | |
7006 | if (context == current_function_decl) | |
2b110bfc | 7007 | pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current); |
06d40de8 DG |
7008 | |
7009 | if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist)) | |
7010 | /* This instantiation is another name for the primary | |
7011 | template type. Set the TYPE_CANONICAL field | |
7012 | appropriately. */ | |
7013 | TYPE_CANONICAL (t) = template_type; | |
7014 | else if (any_template_arguments_need_structural_equality_p (arglist)) | |
7015 | /* Some of the template arguments require structural | |
7016 | equality testing, so this template class requires | |
7017 | structural equality testing. */ | |
7018 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
ed44da02 MM |
7019 | } |
7020 | ||
ae673f14 JM |
7021 | /* If we called start_enum or pushtag above, this information |
7022 | will already be set up. */ | |
ed44da02 MM |
7023 | if (!TYPE_NAME (t)) |
7024 | { | |
7025 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
c8094d83 | 7026 | |
2b110bfc | 7027 | type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t); |
ed44da02 | 7028 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
c8094d83 | 7029 | DECL_SOURCE_LOCATION (type_decl) |
f31686a3 | 7030 | = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); |
ed44da02 MM |
7031 | } |
7032 | else | |
7033 | type_decl = TYPE_NAME (t); | |
36a117a5 | 7034 | |
cab7a9a3 KL |
7035 | TREE_PRIVATE (type_decl) |
7036 | = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); | |
7037 | TREE_PROTECTED (type_decl) | |
7038 | = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); | |
8d039470 MS |
7039 | if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type)) |
7040 | { | |
7041 | DECL_VISIBILITY_SPECIFIED (type_decl) = 1; | |
7042 | DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type); | |
7043 | } | |
cab7a9a3 | 7044 | |
04afbf1c DS |
7045 | /* Let's consider the explicit specialization of a member |
7046 | of a class template specialization that is implicitely instantiated, | |
7047 | e.g.: | |
7048 | template<class T> | |
7049 | struct S | |
7050 | { | |
7051 | template<class U> struct M {}; //#0 | |
7052 | }; | |
7053 | ||
7054 | template<> | |
7055 | template<> | |
7056 | struct S<int>::M<char> //#1 | |
7057 | { | |
7058 | int i; | |
7059 | }; | |
7060 | [temp.expl.spec]/4 says this is valid. | |
7061 | ||
7062 | In this case, when we write: | |
7063 | S<int>::M<char> m; | |
7064 | ||
7065 | M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from | |
7066 | the one of #0. | |
7067 | ||
7068 | When we encounter #1, we want to store the partial instantiation | |
7069 | of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE. | |
7070 | ||
7071 | For all cases other than this "explicit specialization of member of a | |
7072 | class template", we just want to store the most general template into | |
7073 | the CLASSTYPE_TI_TEMPLATE of M. | |
7074 | ||
7075 | This case of "explicit specialization of member of a class template" | |
7076 | only happens when: | |
7077 | 1/ the enclosing class is an instantiation of, and therefore not | |
7078 | the same as, the context of the most general template, and | |
7079 | 2/ we aren't looking at the partial instantiation itself, i.e. | |
7080 | the innermost arguments are not the same as the innermost parms of | |
7081 | the most general template. | |
7082 | ||
7083 | So it's only when 1/ and 2/ happens that we want to use the partial | |
7084 | instantiation of the member template in lieu of its most general | |
7085 | template. */ | |
7086 | ||
7087 | if (PRIMARY_TEMPLATE_P (gen_tmpl) | |
7088 | && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist) | |
7089 | /* the enclosing class must be an instantiation... */ | |
7090 | && CLASS_TYPE_P (context) | |
7091 | && !same_type_p (context, DECL_CONTEXT (gen_tmpl))) | |
7092 | { | |
7093 | tree partial_inst_args; | |
7094 | TREE_VEC_LENGTH (arglist)--; | |
7095 | ++processing_template_decl; | |
7096 | partial_inst_args = | |
7097 | tsubst (INNERMOST_TEMPLATE_ARGS | |
7098 | (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))), | |
7099 | arglist, complain, NULL_TREE); | |
7100 | --processing_template_decl; | |
7101 | TREE_VEC_LENGTH (arglist)++; | |
7102 | use_partial_inst_tmpl = | |
7103 | /*...and we must not be looking at the partial instantiation | |
7104 | itself. */ | |
7105 | !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist), | |
7106 | partial_inst_args); | |
7107 | } | |
7108 | ||
7109 | if (!use_partial_inst_tmpl) | |
9fbf56f7 | 7110 | /* This case is easy; there are no member templates involved. */ |
2b110bfc | 7111 | found = gen_tmpl; |
9fbf56f7 MM |
7112 | else |
7113 | { | |
2b110bfc JM |
7114 | /* This is a full instantiation of a member template. Find |
7115 | the partial instantiation of which this is an instance. */ | |
7116 | ||
7117 | /* Temporarily reduce by one the number of levels in the ARGLIST | |
7118 | so as to avoid comparing the last set of arguments. */ | |
7119 | TREE_VEC_LENGTH (arglist)--; | |
7120 | found = tsubst (gen_tmpl, arglist, complain, NULL_TREE); | |
7121 | TREE_VEC_LENGTH (arglist)++; | |
7122 | found = CLASSTYPE_TI_TEMPLATE (found); | |
9fbf56f7 MM |
7123 | } |
7124 | ||
aa373032 | 7125 | SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist)); |
2b110bfc JM |
7126 | |
7127 | elt.spec = t; | |
7128 | slot = (spec_entry **) htab_find_slot_with_hash (type_specializations, | |
7129 | &elt, hash, INSERT); | |
a9429e29 | 7130 | *slot = ggc_alloc_spec_entry (); |
2b110bfc JM |
7131 | **slot = elt; |
7132 | ||
7133 | /* Note this use of the partial instantiation so we can check it | |
7134 | later in maybe_process_partial_specialization. */ | |
2f86b26a JM |
7135 | DECL_TEMPLATE_INSTANTIATIONS (templ) |
7136 | = tree_cons (arglist, t, | |
7137 | DECL_TEMPLATE_INSTANTIATIONS (templ)); | |
dbfe2124 | 7138 | |
04afbf1c | 7139 | if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type) |
61a127b3 MM |
7140 | /* Now that the type has been registered on the instantiations |
7141 | list, we set up the enumerators. Because the enumeration | |
7142 | constants may involve the enumeration type itself, we make | |
7143 | sure to register the type first, and then create the | |
7144 | constants. That way, doing tsubst_expr for the enumeration | |
7145 | constants won't result in recursive calls here; we'll find | |
7146 | the instantiation and exit above. */ | |
7147 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 | 7148 | |
04afbf1c | 7149 | if (is_dependent_type) |
077e7015 MM |
7150 | /* If the type makes use of template parameters, the |
7151 | code that generates debugging information will crash. */ | |
7152 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 7153 | |
b9e75696 JM |
7154 | /* Possibly limit visibility based on template args. */ |
7155 | TREE_PUBLIC (type_decl) = 1; | |
7156 | determine_visibility (type_decl); | |
7157 | ||
fd295cb2 | 7158 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
36a117a5 | 7159 | } |
fd295cb2 | 7160 | timevar_pop (TV_NAME_LOOKUP); |
8d08fdba MS |
7161 | } |
7162 | \f | |
c8094d83 | 7163 | struct pair_fn_data |
8d08fdba | 7164 | { |
8dfaeb63 MM |
7165 | tree_fn_t fn; |
7166 | void *data; | |
c095a4f8 DG |
7167 | /* True when we should also visit template parameters that occur in |
7168 | non-deduced contexts. */ | |
7169 | bool include_nondeduced_p; | |
0c58f841 | 7170 | struct pointer_set_t *visited; |
8dfaeb63 MM |
7171 | }; |
7172 | ||
7173 | /* Called from for_each_template_parm via walk_tree. */ | |
581d38d0 | 7174 | |
8dfaeb63 | 7175 | static tree |
350fae66 | 7176 | for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) |
8dfaeb63 MM |
7177 | { |
7178 | tree t = *tp; | |
7179 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; | |
7180 | tree_fn_t fn = pfd->fn; | |
7181 | void *data = pfd->data; | |
4f2c9d7e | 7182 | |
2f939d94 | 7183 | if (TYPE_P (t) |
c095a4f8 DG |
7184 | && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE) |
7185 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited, | |
7186 | pfd->include_nondeduced_p)) | |
8dfaeb63 | 7187 | return error_mark_node; |
581d38d0 | 7188 | |
8d08fdba MS |
7189 | switch (TREE_CODE (t)) |
7190 | { | |
8d08fdba | 7191 | case RECORD_TYPE: |
9076e292 | 7192 | if (TYPE_PTRMEMFUNC_P (t)) |
8dfaeb63 | 7193 | break; |
ed44da02 MM |
7194 | /* Fall through. */ |
7195 | ||
8d08fdba | 7196 | case UNION_TYPE: |
ed44da02 | 7197 | case ENUMERAL_TYPE: |
8dfaeb63 MM |
7198 | if (!TYPE_TEMPLATE_INFO (t)) |
7199 | *walk_subtrees = 0; | |
aa373032 | 7200 | else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)), |
c095a4f8 DG |
7201 | fn, data, pfd->visited, |
7202 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
7203 | return error_mark_node; |
7204 | break; | |
7205 | ||
8f6e6bf3 EB |
7206 | case INTEGER_TYPE: |
7207 | if (for_each_template_parm (TYPE_MIN_VALUE (t), | |
c095a4f8 DG |
7208 | fn, data, pfd->visited, |
7209 | pfd->include_nondeduced_p) | |
8f6e6bf3 | 7210 | || for_each_template_parm (TYPE_MAX_VALUE (t), |
c095a4f8 DG |
7211 | fn, data, pfd->visited, |
7212 | pfd->include_nondeduced_p)) | |
8f6e6bf3 EB |
7213 | return error_mark_node; |
7214 | break; | |
7215 | ||
588c2d1c | 7216 | case METHOD_TYPE: |
8dfaeb63 MM |
7217 | /* Since we're not going to walk subtrees, we have to do this |
7218 | explicitly here. */ | |
4f2c9d7e | 7219 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, |
c095a4f8 | 7220 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 | 7221 | return error_mark_node; |
4890c2f4 | 7222 | /* Fall through. */ |
588c2d1c MM |
7223 | |
7224 | case FUNCTION_TYPE: | |
8dfaeb63 | 7225 | /* Check the return type. */ |
c095a4f8 DG |
7226 | if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited, |
7227 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
7228 | return error_mark_node; |
7229 | ||
588c2d1c MM |
7230 | /* Check the parameter types. Since default arguments are not |
7231 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
7232 | contain expressions that involve template parameters. But, | |
7233 | no-one should be looking at them yet. And, once they're | |
7234 | instantiated, they don't contain template parameters, so | |
7235 | there's no point in looking at them then, either. */ | |
7236 | { | |
7237 | tree parm; | |
7238 | ||
7239 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4f2c9d7e | 7240 | if (for_each_template_parm (TREE_VALUE (parm), fn, data, |
c095a4f8 | 7241 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 | 7242 | return error_mark_node; |
5566b478 | 7243 | |
8dfaeb63 MM |
7244 | /* Since we've already handled the TYPE_ARG_TYPES, we don't |
7245 | want walk_tree walking into them itself. */ | |
7246 | *walk_subtrees = 0; | |
7247 | } | |
7248 | break; | |
3ac3d9ea | 7249 | |
a723baf1 | 7250 | case TYPEOF_TYPE: |
c095a4f8 DG |
7251 | if (pfd->include_nondeduced_p |
7252 | && for_each_template_parm (TYPE_FIELDS (t), fn, data, | |
7253 | pfd->visited, | |
7254 | pfd->include_nondeduced_p)) | |
a723baf1 MM |
7255 | return error_mark_node; |
7256 | break; | |
7257 | ||
8d08fdba | 7258 | case FUNCTION_DECL: |
5566b478 | 7259 | case VAR_DECL: |
5566b478 | 7260 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
4f2c9d7e | 7261 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data, |
c095a4f8 | 7262 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 MM |
7263 | return error_mark_node; |
7264 | /* Fall through. */ | |
7265 | ||
8d08fdba | 7266 | case PARM_DECL: |
a723baf1 MM |
7267 | case CONST_DECL: |
7268 | if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t) | |
7269 | && for_each_template_parm (DECL_INITIAL (t), fn, data, | |
c095a4f8 | 7270 | pfd->visited, pfd->include_nondeduced_p)) |
a723baf1 | 7271 | return error_mark_node; |
c8094d83 | 7272 | if (DECL_CONTEXT (t) |
c095a4f8 | 7273 | && pfd->include_nondeduced_p |
4f2c9d7e | 7274 | && for_each_template_parm (DECL_CONTEXT (t), fn, data, |
c095a4f8 | 7275 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 MM |
7276 | return error_mark_node; |
7277 | break; | |
8d08fdba | 7278 | |
a1281f45 | 7279 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
744fac59 | 7280 | /* Record template parameters such as `T' inside `TT<T>'. */ |
c095a4f8 DG |
7281 | if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited, |
7282 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
7283 | return error_mark_node; |
7284 | /* Fall through. */ | |
7285 | ||
a1281f45 | 7286 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 7287 | case TEMPLATE_TYPE_PARM: |
f84b4be9 | 7288 | case TEMPLATE_PARM_INDEX: |
8dfaeb63 MM |
7289 | if (fn && (*fn)(t, data)) |
7290 | return error_mark_node; | |
7291 | else if (!fn) | |
7292 | return error_mark_node; | |
7293 | break; | |
5156628f | 7294 | |
8dfaeb63 | 7295 | case TEMPLATE_DECL: |
f4f206f4 | 7296 | /* A template template parameter is encountered. */ |
8dfaeb63 | 7297 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) |
c095a4f8 DG |
7298 | && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited, |
7299 | pfd->include_nondeduced_p)) | |
8dfaeb63 | 7300 | return error_mark_node; |
db5ae43f | 7301 | |
8dfaeb63 MM |
7302 | /* Already substituted template template parameter */ |
7303 | *walk_subtrees = 0; | |
7304 | break; | |
b8865407 | 7305 | |
4699c561 | 7306 | case TYPENAME_TYPE: |
c8094d83 | 7307 | if (!fn |
4f2c9d7e | 7308 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, |
c095a4f8 DG |
7309 | data, pfd->visited, |
7310 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
7311 | return error_mark_node; |
7312 | break; | |
4699c561 | 7313 | |
8dfaeb63 MM |
7314 | case CONSTRUCTOR: |
7315 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) | |
c095a4f8 | 7316 | && pfd->include_nondeduced_p |
8dfaeb63 | 7317 | && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE |
4f2c9d7e | 7318 | (TREE_TYPE (t)), fn, data, |
c095a4f8 | 7319 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 MM |
7320 | return error_mark_node; |
7321 | break; | |
c8094d83 | 7322 | |
b8865407 MM |
7323 | case INDIRECT_REF: |
7324 | case COMPONENT_REF: | |
4699c561 | 7325 | /* If there's no type, then this thing must be some expression |
b8865407 | 7326 | involving template parameters. */ |
4699c561 | 7327 | if (!fn && !TREE_TYPE (t)) |
8dfaeb63 MM |
7328 | return error_mark_node; |
7329 | break; | |
b8865407 | 7330 | |
42976354 BK |
7331 | case MODOP_EXPR: |
7332 | case CAST_EXPR: | |
7333 | case REINTERPRET_CAST_EXPR: | |
7334 | case CONST_CAST_EXPR: | |
7335 | case STATIC_CAST_EXPR: | |
7336 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
7337 | case ARROW_EXPR: |
7338 | case DOTSTAR_EXPR: | |
7339 | case TYPEID_EXPR: | |
40242ccf | 7340 | case PSEUDO_DTOR_EXPR: |
b8865407 | 7341 | if (!fn) |
8dfaeb63 MM |
7342 | return error_mark_node; |
7343 | break; | |
abff8e06 | 7344 | |
8d08fdba | 7345 | default: |
8dfaeb63 | 7346 | break; |
8d08fdba | 7347 | } |
8dfaeb63 MM |
7348 | |
7349 | /* We didn't find any template parameters we liked. */ | |
7350 | return NULL_TREE; | |
7351 | } | |
7352 | ||
c8094d83 MS |
7353 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, |
7354 | BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, | |
a1281f45 | 7355 | call FN with the parameter and the DATA. |
838dfd8a | 7356 | If FN returns nonzero, the iteration is terminated, and |
8dfaeb63 | 7357 | for_each_template_parm returns 1. Otherwise, the iteration |
838dfd8a | 7358 | continues. If FN never returns a nonzero value, the value |
8dfaeb63 | 7359 | returned by for_each_template_parm is 0. If FN is NULL, it is |
c095a4f8 DG |
7360 | considered to be the function which always returns 1. |
7361 | ||
7362 | If INCLUDE_NONDEDUCED_P, then this routine will also visit template | |
7363 | parameters that occur in non-deduced contexts. When false, only | |
7364 | visits those template parameters that can be deduced. */ | |
8dfaeb63 MM |
7365 | |
7366 | static int | |
0c58f841 | 7367 | for_each_template_parm (tree t, tree_fn_t fn, void* data, |
c095a4f8 DG |
7368 | struct pointer_set_t *visited, |
7369 | bool include_nondeduced_p) | |
8dfaeb63 MM |
7370 | { |
7371 | struct pair_fn_data pfd; | |
ad2ae3b2 | 7372 | int result; |
8dfaeb63 MM |
7373 | |
7374 | /* Set up. */ | |
7375 | pfd.fn = fn; | |
7376 | pfd.data = data; | |
c095a4f8 | 7377 | pfd.include_nondeduced_p = include_nondeduced_p; |
8dfaeb63 | 7378 | |
4890c2f4 MM |
7379 | /* Walk the tree. (Conceptually, we would like to walk without |
7380 | duplicates, but for_each_template_parm_r recursively calls | |
7381 | for_each_template_parm, so we would need to reorganize a fair | |
4f2c9d7e MM |
7382 | bit to use walk_tree_without_duplicates, so we keep our own |
7383 | visited list.) */ | |
7384 | if (visited) | |
7385 | pfd.visited = visited; | |
7386 | else | |
0c58f841 | 7387 | pfd.visited = pointer_set_create (); |
14588106 RG |
7388 | result = cp_walk_tree (&t, |
7389 | for_each_template_parm_r, | |
7390 | &pfd, | |
7391 | pfd.visited) != NULL_TREE; | |
ad2ae3b2 MM |
7392 | |
7393 | /* Clean up. */ | |
7394 | if (!visited) | |
0c58f841 MA |
7395 | { |
7396 | pointer_set_destroy (pfd.visited); | |
7397 | pfd.visited = 0; | |
7398 | } | |
ad2ae3b2 MM |
7399 | |
7400 | return result; | |
8d08fdba MS |
7401 | } |
7402 | ||
d43f603d KL |
7403 | /* Returns true if T depends on any template parameter. */ |
7404 | ||
050367a3 | 7405 | int |
3a978d72 | 7406 | uses_template_parms (tree t) |
050367a3 | 7407 | { |
c353b8e3 MM |
7408 | bool dependent_p; |
7409 | int saved_processing_template_decl; | |
7410 | ||
7411 | saved_processing_template_decl = processing_template_decl; | |
7412 | if (!saved_processing_template_decl) | |
7413 | processing_template_decl = 1; | |
7414 | if (TYPE_P (t)) | |
7415 | dependent_p = dependent_type_p (t); | |
7416 | else if (TREE_CODE (t) == TREE_VEC) | |
7417 | dependent_p = any_dependent_template_arguments_p (t); | |
7418 | else if (TREE_CODE (t) == TREE_LIST) | |
7419 | dependent_p = (uses_template_parms (TREE_VALUE (t)) | |
7420 | || uses_template_parms (TREE_CHAIN (t))); | |
3ce5fa4f NS |
7421 | else if (TREE_CODE (t) == TYPE_DECL) |
7422 | dependent_p = dependent_type_p (TREE_TYPE (t)); | |
c8094d83 MS |
7423 | else if (DECL_P (t) |
7424 | || EXPR_P (t) | |
c353b8e3 MM |
7425 | || TREE_CODE (t) == TEMPLATE_PARM_INDEX |
7426 | || TREE_CODE (t) == OVERLOAD | |
7427 | || TREE_CODE (t) == BASELINK | |
b207d6e2 | 7428 | || TREE_CODE (t) == IDENTIFIER_NODE |
cb68ec50 | 7429 | || TREE_CODE (t) == TRAIT_EXPR |
c1b93f2b | 7430 | || TREE_CODE (t) == CONSTRUCTOR |
6615c446 | 7431 | || CONSTANT_CLASS_P (t)) |
c353b8e3 MM |
7432 | dependent_p = (type_dependent_expression_p (t) |
7433 | || value_dependent_expression_p (t)); | |
315fb5db NS |
7434 | else |
7435 | { | |
7436 | gcc_assert (t == error_mark_node); | |
7437 | dependent_p = false; | |
7438 | } | |
c8094d83 | 7439 | |
c353b8e3 MM |
7440 | processing_template_decl = saved_processing_template_decl; |
7441 | ||
7442 | return dependent_p; | |
050367a3 MM |
7443 | } |
7444 | ||
d43f603d KL |
7445 | /* Returns true if T depends on any template parameter with level LEVEL. */ |
7446 | ||
7447 | int | |
7448 | uses_template_parms_level (tree t, int level) | |
7449 | { | |
c095a4f8 DG |
7450 | return for_each_template_parm (t, template_parm_this_level_p, &level, NULL, |
7451 | /*include_nondeduced_p=*/true); | |
d43f603d KL |
7452 | } |
7453 | ||
27fafc8d | 7454 | static int tinst_depth; |
e9f32eb5 | 7455 | extern int max_tinst_depth; |
5566b478 | 7456 | #ifdef GATHER_STATISTICS |
27fafc8d | 7457 | int depth_reached; |
5566b478 | 7458 | #endif |
8dfaeb63 MM |
7459 | static int tinst_level_tick; |
7460 | static int last_template_error_tick; | |
8d08fdba | 7461 | |
3ae18eaf JM |
7462 | /* We're starting to instantiate D; record the template instantiation context |
7463 | for diagnostics and to restore it later. */ | |
7464 | ||
2a3dbebf | 7465 | int |
3a978d72 | 7466 | push_tinst_level (tree d) |
8d08fdba | 7467 | { |
be93747e | 7468 | struct tinst_level *new_level; |
8d08fdba | 7469 | |
7215f9a0 MS |
7470 | if (tinst_depth >= max_tinst_depth) |
7471 | { | |
8adf5b5e JM |
7472 | /* If the instantiation in question still has unbound template parms, |
7473 | we don't really care if we can't instantiate it, so just return. | |
0cbd7506 | 7474 | This happens with base instantiation for implicit `typename'. */ |
8adf5b5e JM |
7475 | if (uses_template_parms (d)) |
7476 | return 0; | |
7477 | ||
1139b3d8 | 7478 | last_template_error_tick = tinst_level_tick; |
0f51ccfc | 7479 | error ("template instantiation depth exceeds maximum of %d (use " |
124e0d27 | 7480 | "-ftemplate-depth= to increase the maximum) instantiating %qD", |
b4f4233d | 7481 | max_tinst_depth, d); |
5566b478 | 7482 | |
cb753e49 | 7483 | print_instantiation_context (); |
5566b478 | 7484 | |
7215f9a0 MS |
7485 | return 0; |
7486 | } | |
7487 | ||
a9429e29 | 7488 | new_level = ggc_alloc_tinst_level (); |
be93747e KG |
7489 | new_level->decl = d; |
7490 | new_level->locus = input_location; | |
7491 | new_level->in_system_header_p = in_system_header; | |
7492 | new_level->next = current_tinst_level; | |
7493 | current_tinst_level = new_level; | |
5566b478 | 7494 | |
7215f9a0 | 7495 | ++tinst_depth; |
5566b478 MS |
7496 | #ifdef GATHER_STATISTICS |
7497 | if (tinst_depth > depth_reached) | |
7498 | depth_reached = tinst_depth; | |
7499 | #endif | |
7500 | ||
27fafc8d | 7501 | ++tinst_level_tick; |
7215f9a0 | 7502 | return 1; |
8d08fdba MS |
7503 | } |
7504 | ||
3ae18eaf JM |
7505 | /* We're done instantiating this template; return to the instantiation |
7506 | context. */ | |
7507 | ||
2a3dbebf | 7508 | void |
3a978d72 | 7509 | pop_tinst_level (void) |
8d08fdba | 7510 | { |
ae58fa02 MM |
7511 | /* Restore the filename and line number stashed away when we started |
7512 | this instantiation. */ | |
e2c3721c | 7513 | input_location = current_tinst_level->locus; |
e2c3721c | 7514 | current_tinst_level = current_tinst_level->next; |
7215f9a0 | 7515 | --tinst_depth; |
27fafc8d | 7516 | ++tinst_level_tick; |
8d08fdba MS |
7517 | } |
7518 | ||
3ae18eaf JM |
7519 | /* We're instantiating a deferred template; restore the template |
7520 | instantiation context in which the instantiation was requested, which | |
e2c3721c | 7521 | is one step out from LEVEL. Return the corresponding DECL or TYPE. */ |
3ae18eaf | 7522 | |
e2c3721c PB |
7523 | static tree |
7524 | reopen_tinst_level (struct tinst_level *level) | |
3ae18eaf | 7525 | { |
e2c3721c | 7526 | struct tinst_level *t; |
3ae18eaf JM |
7527 | |
7528 | tinst_depth = 0; | |
e2c3721c | 7529 | for (t = level; t; t = t->next) |
3ae18eaf JM |
7530 | ++tinst_depth; |
7531 | ||
7532 | current_tinst_level = level; | |
7533 | pop_tinst_level (); | |
e2c3721c | 7534 | return level->decl; |
3ae18eaf JM |
7535 | } |
7536 | ||
61172206 JM |
7537 | /* Returns the TINST_LEVEL which gives the original instantiation |
7538 | context. */ | |
7539 | ||
e2c3721c | 7540 | struct tinst_level * |
61172206 JM |
7541 | outermost_tinst_level (void) |
7542 | { | |
e2c3721c PB |
7543 | struct tinst_level *level = current_tinst_level; |
7544 | if (level) | |
7545 | while (level->next) | |
7546 | level = level->next; | |
7547 | return level; | |
61172206 JM |
7548 | } |
7549 | ||
172a4594 DS |
7550 | /* Returns TRUE if PARM is a parameter of the template TEMPL. */ |
7551 | ||
7552 | bool | |
7553 | parameter_of_template_p (tree parm, tree templ) | |
7554 | { | |
7555 | tree parms; | |
7556 | int i; | |
7557 | ||
7558 | if (!parm || !templ) | |
7559 | return false; | |
7560 | ||
7561 | gcc_assert (DECL_TEMPLATE_PARM_P (parm)); | |
7562 | gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL); | |
7563 | ||
7564 | parms = DECL_TEMPLATE_PARMS (templ); | |
7565 | parms = INNERMOST_TEMPLATE_PARMS (parms); | |
7566 | ||
7567 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) | |
7568 | if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i))) | |
7569 | return true; | |
7570 | ||
7571 | return false; | |
7572 | } | |
7573 | ||
f84b4be9 JM |
7574 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
7575 | vector of template arguments, as for tsubst. | |
7576 | ||
dc957d14 | 7577 | Returns an appropriate tsubst'd friend declaration. */ |
f84b4be9 JM |
7578 | |
7579 | static tree | |
3a978d72 | 7580 | tsubst_friend_function (tree decl, tree args) |
f84b4be9 JM |
7581 | { |
7582 | tree new_friend; | |
27fafc8d | 7583 | |
c8094d83 | 7584 | if (TREE_CODE (decl) == FUNCTION_DECL |
f84b4be9 JM |
7585 | && DECL_TEMPLATE_INSTANTIATION (decl) |
7586 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
7587 | /* This was a friend declared with an explicit template | |
7588 | argument list, e.g.: | |
c8094d83 | 7589 | |
f84b4be9 | 7590 | friend void f<>(T); |
c8094d83 | 7591 | |
f84b4be9 JM |
7592 | to indicate that f was a template instantiation, not a new |
7593 | function declaration. Now, we have to figure out what | |
7594 | instantiation of what template. */ | |
7595 | { | |
76e57b45 | 7596 | tree template_id, arglist, fns; |
f84b4be9 JM |
7597 | tree new_args; |
7598 | tree tmpl; | |
ed2fa432 | 7599 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); |
c8094d83 | 7600 | |
76e57b45 | 7601 | /* Friend functions are looked up in the containing namespace scope. |
0cbd7506 | 7602 | We must enter that scope, to avoid finding member functions of the |
39a13be5 | 7603 | current class with same name. */ |
76e57b45 NS |
7604 | push_nested_namespace (ns); |
7605 | fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, | |
015c2c66 MM |
7606 | tf_warning_or_error, NULL_TREE, |
7607 | /*integral_constant_expression_p=*/false); | |
76e57b45 NS |
7608 | pop_nested_namespace (ns); |
7609 | arglist = tsubst (DECL_TI_ARGS (decl), args, | |
23fca1f5 | 7610 | tf_warning_or_error, NULL_TREE); |
76e57b45 | 7611 | template_id = lookup_template_function (fns, arglist); |
c8094d83 | 7612 | |
23fca1f5 | 7613 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
36a117a5 | 7614 | tmpl = determine_specialization (template_id, new_friend, |
c8094d83 | 7615 | &new_args, |
5fe7b654 | 7616 | /*need_member_template=*/0, |
29a1da1c MM |
7617 | TREE_VEC_LENGTH (args), |
7618 | tsk_none); | |
6e432b31 | 7619 | return instantiate_template (tmpl, new_args, tf_error); |
f84b4be9 | 7620 | } |
36a117a5 | 7621 | |
23fca1f5 | 7622 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
c8094d83 | 7623 | |
36a117a5 | 7624 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
7625 | compiler, but is not an instantiation from the point of view of |
7626 | the language. For example, we might have had: | |
c8094d83 | 7627 | |
f84b4be9 JM |
7628 | template <class T> struct S { |
7629 | template <class U> friend void f(T, U); | |
7630 | }; | |
c8094d83 | 7631 | |
f84b4be9 JM |
7632 | Then, in S<int>, template <class U> void f(int, U) is not an |
7633 | instantiation of anything. */ | |
ac2b3222 AP |
7634 | if (new_friend == error_mark_node) |
7635 | return error_mark_node; | |
c8094d83 | 7636 | |
f84b4be9 JM |
7637 | DECL_USE_TEMPLATE (new_friend) = 0; |
7638 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
655dc6ee JM |
7639 | { |
7640 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
7641 | DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) | |
7642 | = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); | |
7643 | } | |
36a117a5 | 7644 | |
92643fea MM |
7645 | /* The mangled name for the NEW_FRIEND is incorrect. The function |
7646 | is not a template instantiation and should not be mangled like | |
7647 | one. Therefore, we forget the mangling here; we'll recompute it | |
7648 | later if we need it. */ | |
36a117a5 MM |
7649 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) |
7650 | { | |
245763e3 | 7651 | SET_DECL_RTL (new_friend, NULL); |
92643fea | 7652 | SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); |
36a117a5 | 7653 | } |
c8094d83 | 7654 | |
6eb3bb27 | 7655 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 7656 | { |
36a117a5 | 7657 | tree old_decl; |
fbf1c34b MM |
7658 | tree new_friend_template_info; |
7659 | tree new_friend_result_template_info; | |
92da7074 | 7660 | tree ns; |
fbf1c34b MM |
7661 | int new_friend_is_defn; |
7662 | ||
7663 | /* We must save some information from NEW_FRIEND before calling | |
7664 | duplicate decls since that function will free NEW_FRIEND if | |
7665 | possible. */ | |
7666 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
5bd61841 | 7667 | new_friend_is_defn = |
c8094d83 | 7668 | (DECL_INITIAL (DECL_TEMPLATE_RESULT |
5bd61841 MM |
7669 | (template_for_substitution (new_friend))) |
7670 | != NULL_TREE); | |
f84b4be9 | 7671 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
7672 | { |
7673 | /* This declaration is a `primary' template. */ | |
7674 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
c8094d83 | 7675 | |
fbf1c34b | 7676 | new_friend_result_template_info |
17aec3eb | 7677 | = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); |
fbf1c34b MM |
7678 | } |
7679 | else | |
5bd61841 | 7680 | new_friend_result_template_info = NULL_TREE; |
36a117a5 | 7681 | |
b01e6d2b JM |
7682 | /* Make the init_value nonzero so pushdecl knows this is a defn. */ |
7683 | if (new_friend_is_defn) | |
7684 | DECL_INITIAL (new_friend) = error_mark_node; | |
7685 | ||
1c227897 MM |
7686 | /* Inside pushdecl_namespace_level, we will push into the |
7687 | current namespace. However, the friend function should go | |
c6002625 | 7688 | into the namespace of the template. */ |
92da7074 ML |
7689 | ns = decl_namespace_context (new_friend); |
7690 | push_nested_namespace (ns); | |
d63d5d0c | 7691 | old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true); |
92da7074 | 7692 | pop_nested_namespace (ns); |
36a117a5 | 7693 | |
0014c247 VR |
7694 | if (old_decl == error_mark_node) |
7695 | return error_mark_node; | |
7696 | ||
36a117a5 MM |
7697 | if (old_decl != new_friend) |
7698 | { | |
7699 | /* This new friend declaration matched an existing | |
7700 | declaration. For example, given: | |
7701 | ||
7702 | template <class T> void f(T); | |
c8094d83 MS |
7703 | template <class U> class C { |
7704 | template <class T> friend void f(T) {} | |
36a117a5 MM |
7705 | }; |
7706 | ||
7707 | the friend declaration actually provides the definition | |
7708 | of `f', once C has been instantiated for some type. So, | |
7709 | old_decl will be the out-of-class template declaration, | |
7710 | while new_friend is the in-class definition. | |
7711 | ||
7712 | But, if `f' was called before this point, the | |
7713 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
7714 | to `T' but not to `U', references to which might appear | |
7715 | in the definition of `f'. Previously, the most general | |
7716 | template for an instantiation of `f' was the out-of-class | |
7717 | version; now it is the in-class version. Therefore, we | |
7718 | run through all specialization of `f', adding to their | |
7719 | DECL_TI_ARGS appropriately. In particular, they need a | |
7720 | new set of outer arguments, corresponding to the | |
c8094d83 | 7721 | arguments for this class instantiation. |
36a117a5 MM |
7722 | |
7723 | The same situation can arise with something like this: | |
7724 | ||
7725 | friend void f(int); | |
c8094d83 | 7726 | template <class T> class C { |
0cbd7506 MS |
7727 | friend void f(T) {} |
7728 | }; | |
36a117a5 MM |
7729 | |
7730 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
7731 | in the class. */ | |
7732 | ||
fbf1c34b MM |
7733 | if (!new_friend_is_defn) |
7734 | /* On the other hand, if the in-class declaration does | |
7735 | *not* provide a definition, then we don't want to alter | |
7736 | existing definitions. We can just leave everything | |
7737 | alone. */ | |
36a117a5 | 7738 | ; |
fbf1c34b | 7739 | else |
36a117a5 | 7740 | { |
2b110bfc JM |
7741 | tree new_template = TI_TEMPLATE (new_friend_template_info); |
7742 | tree new_args = TI_ARGS (new_friend_template_info); | |
7743 | ||
fbf1c34b MM |
7744 | /* Overwrite whatever template info was there before, if |
7745 | any, with the new template information pertaining to | |
7746 | the declaration. */ | |
7747 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
7748 | ||
7749 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
feb3b88a JM |
7750 | { |
7751 | /* We should have called reregister_specialization in | |
7752 | duplicate_decls. */ | |
7753 | gcc_assert (retrieve_specialization (new_template, | |
7754 | new_args, 0) | |
7755 | == old_decl); | |
7756 | ||
7757 | /* Instantiate it if the global has already been used. */ | |
7758 | if (DECL_ODR_USED (old_decl)) | |
7759 | instantiate_decl (old_decl, /*defer_ok=*/true, | |
7760 | /*expl_inst_class_mem_p=*/false); | |
7761 | } | |
c8094d83 | 7762 | else |
36a117a5 | 7763 | { |
fbf1c34b | 7764 | tree t; |
fbf1c34b | 7765 | |
2b110bfc JM |
7766 | /* Indicate that the old function template is a partial |
7767 | instantiation. */ | |
c8094d83 | 7768 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) |
fbf1c34b | 7769 | = new_friend_result_template_info; |
c8094d83 | 7770 | |
2b110bfc JM |
7771 | gcc_assert (new_template |
7772 | == most_general_template (new_template)); | |
7773 | gcc_assert (new_template != old_decl); | |
7774 | ||
7775 | /* Reassign any specializations already in the hash table | |
7776 | to the new more general template, and add the | |
7777 | additional template args. */ | |
7778 | for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl); | |
fbf1c34b MM |
7779 | t != NULL_TREE; |
7780 | t = TREE_CHAIN (t)) | |
7781 | { | |
7782 | tree spec = TREE_VALUE (t); | |
2b110bfc JM |
7783 | spec_entry elt; |
7784 | ||
7785 | elt.tmpl = old_decl; | |
7786 | elt.args = DECL_TI_ARGS (spec); | |
7787 | elt.spec = NULL_TREE; | |
7788 | ||
7789 | htab_remove_elt (decl_specializations, &elt); | |
c8094d83 MS |
7790 | |
7791 | DECL_TI_ARGS (spec) | |
2b110bfc | 7792 | = add_outermost_template_args (new_args, |
fbf1c34b | 7793 | DECL_TI_ARGS (spec)); |
fbf1c34b | 7794 | |
2b110bfc JM |
7795 | register_specialization |
7796 | (spec, new_template, DECL_TI_ARGS (spec), true, 0); | |
7797 | ||
fbf1c34b | 7798 | } |
2b110bfc | 7799 | DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE; |
36a117a5 MM |
7800 | } |
7801 | } | |
7802 | ||
7803 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
7804 | by duplicate_decls. */ | |
7805 | new_friend = old_decl; | |
7806 | } | |
f84b4be9 | 7807 | } |
6e432b31 | 7808 | else |
f84b4be9 | 7809 | { |
6e432b31 KL |
7810 | tree context = DECL_CONTEXT (new_friend); |
7811 | bool dependent_p; | |
7812 | ||
7813 | /* In the code | |
7814 | template <class T> class C { | |
7815 | template <class U> friend void C1<U>::f (); // case 1 | |
7816 | friend void C2<T>::f (); // case 2 | |
7817 | }; | |
7818 | we only need to make sure CONTEXT is a complete type for | |
7819 | case 2. To distinguish between the two cases, we note that | |
7820 | CONTEXT of case 1 remains dependent type after tsubst while | |
7821 | this isn't true for case 2. */ | |
7822 | ++processing_template_decl; | |
7823 | dependent_p = dependent_type_p (context); | |
7824 | --processing_template_decl; | |
7825 | ||
7826 | if (!dependent_p | |
7827 | && !complete_type_or_else (context, NULL_TREE)) | |
7828 | return error_mark_node; | |
7829 | ||
7830 | if (COMPLETE_TYPE_P (context)) | |
7831 | { | |
7832 | /* Check to see that the declaration is really present, and, | |
7833 | possibly obtain an improved declaration. */ | |
7834 | tree fn = check_classfn (context, | |
7835 | new_friend, NULL_TREE); | |
7836 | ||
684939ce | 7837 | if (fn) |
6e432b31 KL |
7838 | new_friend = fn; |
7839 | } | |
f84b4be9 JM |
7840 | } |
7841 | ||
7842 | return new_friend; | |
7843 | } | |
7844 | ||
1aed5355 MM |
7845 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
7846 | template arguments, as for tsubst. | |
6757edfe | 7847 | |
dc957d14 | 7848 | Returns an appropriate tsubst'd friend type or error_mark_node on |
4b054b80 | 7849 | failure. */ |
6757edfe MM |
7850 | |
7851 | static tree | |
3a978d72 | 7852 | tsubst_friend_class (tree friend_tmpl, tree args) |
6757edfe | 7853 | { |
1aed5355 | 7854 | tree friend_type; |
25aab5d0 | 7855 | tree tmpl; |
3e0ec82f | 7856 | tree context; |
6757edfe | 7857 | |
725214ac | 7858 | context = CP_DECL_CONTEXT (friend_tmpl); |
3e0ec82f | 7859 | |
725214ac | 7860 | if (context != global_namespace) |
77adef84 | 7861 | { |
3e0ec82f MM |
7862 | if (TREE_CODE (context) == NAMESPACE_DECL) |
7863 | push_nested_namespace (context); | |
7864 | else | |
c8094d83 | 7865 | push_nested_class (tsubst (context, args, tf_none, NULL_TREE)); |
3e0ec82f | 7866 | } |
25aab5d0 | 7867 | |
105d72c5 MM |
7868 | /* Look for a class template declaration. We look for hidden names |
7869 | because two friend declarations of the same template are the | |
7870 | same. For example, in: | |
7871 | ||
7872 | struct A { | |
7873 | template <typename> friend class F; | |
7874 | }; | |
7875 | template <typename> struct B { | |
7876 | template <typename> friend class F; | |
7877 | }; | |
7878 | ||
7879 | both F templates are the same. */ | |
7880 | tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0, | |
7881 | /*block_p=*/true, 0, | |
7882 | LOOKUP_COMPLAIN | LOOKUP_HIDDEN); | |
25aab5d0 | 7883 | |
3e0ec82f MM |
7884 | /* But, if we don't find one, it might be because we're in a |
7885 | situation like this: | |
77adef84 | 7886 | |
3e0ec82f MM |
7887 | template <class T> |
7888 | struct S { | |
7889 | template <class U> | |
7890 | friend struct S; | |
7891 | }; | |
7892 | ||
7893 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
7894 | for `S<int>', not the TEMPLATE_DECL. */ | |
7895 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
7896 | { | |
10e6657a | 7897 | tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1); |
3e0ec82f | 7898 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); |
25aab5d0 | 7899 | } |
6757edfe | 7900 | |
25aab5d0 | 7901 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
6757edfe MM |
7902 | { |
7903 | /* The friend template has already been declared. Just | |
36a117a5 MM |
7904 | check to see that the declarations match, and install any new |
7905 | default parameters. We must tsubst the default parameters, | |
7906 | of course. We only need the innermost template parameters | |
7907 | because that is all that redeclare_class_template will look | |
7908 | at. */ | |
3e0ec82f MM |
7909 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) |
7910 | > TMPL_ARGS_DEPTH (args)) | |
7911 | { | |
7912 | tree parms; | |
b131ad7c | 7913 | location_t saved_input_location; |
3e0ec82f | 7914 | parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), |
23fca1f5 | 7915 | args, tf_warning_or_error); |
b131ad7c MLI |
7916 | |
7917 | saved_input_location = input_location; | |
7918 | input_location = DECL_SOURCE_LOCATION (friend_tmpl); | |
3e0ec82f | 7919 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
b131ad7c MLI |
7920 | input_location = saved_input_location; |
7921 | ||
3e0ec82f MM |
7922 | } |
7923 | ||
6757edfe MM |
7924 | friend_type = TREE_TYPE (tmpl); |
7925 | } | |
7926 | else | |
7927 | { | |
7928 | /* The friend template has not already been declared. In this | |
7929 | case, the instantiation of the template class will cause the | |
7930 | injection of this template into the global scope. */ | |
23fca1f5 | 7931 | tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE); |
dca56f77 VR |
7932 | if (tmpl == error_mark_node) |
7933 | return error_mark_node; | |
6757edfe MM |
7934 | |
7935 | /* The new TMPL is not an instantiation of anything, so we | |
0cbd7506 | 7936 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for |
6757edfe MM |
7937 | the new type because that is supposed to be the corresponding |
7938 | template decl, i.e., TMPL. */ | |
7939 | DECL_USE_TEMPLATE (tmpl) = 0; | |
7940 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
7941 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
68c78847 KL |
7942 | CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) |
7943 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); | |
6757edfe MM |
7944 | |
7945 | /* Inject this template into the global scope. */ | |
d63d5d0c | 7946 | friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true)); |
6757edfe MM |
7947 | } |
7948 | ||
725214ac | 7949 | if (context != global_namespace) |
3e0ec82f MM |
7950 | { |
7951 | if (TREE_CODE (context) == NAMESPACE_DECL) | |
7952 | pop_nested_namespace (context); | |
7953 | else | |
7954 | pop_nested_class (); | |
7955 | } | |
7956 | ||
6757edfe MM |
7957 | return friend_type; |
7958 | } | |
f84b4be9 | 7959 | |
17f29637 KL |
7960 | /* Returns zero if TYPE cannot be completed later due to circularity. |
7961 | Otherwise returns one. */ | |
7962 | ||
d5372501 | 7963 | static int |
3a978d72 | 7964 | can_complete_type_without_circularity (tree type) |
17f29637 KL |
7965 | { |
7966 | if (type == NULL_TREE || type == error_mark_node) | |
7967 | return 0; | |
7968 | else if (COMPLETE_TYPE_P (type)) | |
7969 | return 1; | |
7970 | else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) | |
7971 | return can_complete_type_without_circularity (TREE_TYPE (type)); | |
8c6ab2db NS |
7972 | else if (CLASS_TYPE_P (type) |
7973 | && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) | |
17f29637 KL |
7974 | return 0; |
7975 | else | |
7976 | return 1; | |
7977 | } | |
7978 | ||
4f4141ff JM |
7979 | /* Apply any attributes which had to be deferred until instantiation |
7980 | time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes; | |
7981 | ARGS, COMPLAIN, IN_DECL are as tsubst. */ | |
7982 | ||
7983 | static void | |
7984 | apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags, | |
7985 | tree args, tsubst_flags_t complain, tree in_decl) | |
7986 | { | |
5818c8e4 | 7987 | tree last_dep = NULL_TREE; |
4f4141ff | 7988 | tree t; |
5818c8e4 JM |
7989 | tree *p; |
7990 | ||
7991 | for (t = attributes; t; t = TREE_CHAIN (t)) | |
7992 | if (ATTR_IS_DEPENDENT (t)) | |
7993 | { | |
7994 | last_dep = t; | |
7995 | attributes = copy_list (attributes); | |
7996 | break; | |
7997 | } | |
4f4141ff JM |
7998 | |
7999 | if (DECL_P (*decl_p)) | |
823e5f7f JJ |
8000 | { |
8001 | if (TREE_TYPE (*decl_p) == error_mark_node) | |
8002 | return; | |
8003 | p = &DECL_ATTRIBUTES (*decl_p); | |
8004 | } | |
4f4141ff | 8005 | else |
5818c8e4 | 8006 | p = &TYPE_ATTRIBUTES (*decl_p); |
4f4141ff | 8007 | |
5818c8e4 JM |
8008 | if (last_dep) |
8009 | { | |
8010 | tree late_attrs = NULL_TREE; | |
8011 | tree *q = &late_attrs; | |
8012 | ||
8013 | for (*p = attributes; *p; ) | |
8014 | { | |
8015 | t = *p; | |
8016 | if (ATTR_IS_DEPENDENT (t)) | |
8017 | { | |
8018 | *p = TREE_CHAIN (t); | |
8019 | TREE_CHAIN (t) = NULL_TREE; | |
ff2f1c5f JJ |
8020 | /* If the first attribute argument is an identifier, don't |
8021 | pass it through tsubst. Attributes like mode, format, | |
8022 | cleanup and several target specific attributes expect it | |
8023 | unmodified. */ | |
8024 | if (TREE_VALUE (t) | |
8025 | && TREE_CODE (TREE_VALUE (t)) == TREE_LIST | |
8026 | && TREE_VALUE (TREE_VALUE (t)) | |
8027 | && (TREE_CODE (TREE_VALUE (TREE_VALUE (t))) | |
8028 | == IDENTIFIER_NODE)) | |
8029 | { | |
8030 | tree chain | |
8031 | = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain, | |
8032 | in_decl, | |
8033 | /*integral_constant_expression_p=*/false); | |
8034 | if (chain != TREE_CHAIN (TREE_VALUE (t))) | |
8035 | TREE_VALUE (t) | |
8036 | = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)), | |
8037 | chain); | |
8038 | } | |
8039 | else | |
8040 | TREE_VALUE (t) | |
8041 | = tsubst_expr (TREE_VALUE (t), args, complain, in_decl, | |
8042 | /*integral_constant_expression_p=*/false); | |
5818c8e4 JM |
8043 | *q = t; |
8044 | q = &TREE_CHAIN (t); | |
8045 | } | |
8046 | else | |
8047 | p = &TREE_CHAIN (t); | |
8048 | } | |
4f4141ff | 8049 | |
5818c8e4 JM |
8050 | cplus_decl_attributes (decl_p, late_attrs, attr_flags); |
8051 | } | |
4f4141ff JM |
8052 | } |
8053 | ||
d0940d56 DS |
8054 | /* Perform (or defer) access check for typedefs that were referenced |
8055 | from within the template TMPL code. | |
8056 | This is a subroutine of instantiate_template and instantiate_class_template. | |
8057 | TMPL is the template to consider and TARGS is the list of arguments of | |
8058 | that template. */ | |
8059 | ||
8060 | static void | |
8061 | perform_typedefs_access_check (tree tmpl, tree targs) | |
8062 | { | |
aa373032 DS |
8063 | location_t saved_location; |
8064 | int i; | |
8065 | qualified_typedef_usage_t *iter; | |
d0940d56 | 8066 | |
2eb25c98 | 8067 | if (!tmpl |
9dbed529 | 8068 | || (!CLASS_TYPE_P (tmpl) |
2eb25c98 | 8069 | && TREE_CODE (tmpl) != FUNCTION_DECL)) |
d0940d56 DS |
8070 | return; |
8071 | ||
aa373032 | 8072 | saved_location = input_location; |
ac47786e | 8073 | FOR_EACH_VEC_ELT (qualified_typedef_usage_t, |
aa373032 | 8074 | get_types_needing_access_check (tmpl), |
ac47786e | 8075 | i, iter) |
d0940d56 | 8076 | { |
aa373032 DS |
8077 | tree type_decl = iter->typedef_decl; |
8078 | tree type_scope = iter->context; | |
d0940d56 DS |
8079 | |
8080 | if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope)) | |
8081 | continue; | |
8082 | ||
8083 | if (uses_template_parms (type_decl)) | |
8084 | type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE); | |
8085 | if (uses_template_parms (type_scope)) | |
8086 | type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE); | |
8087 | ||
aa373032 DS |
8088 | /* Make access check error messages point to the location |
8089 | of the use of the typedef. */ | |
8090 | input_location = iter->locus; | |
2eb25c98 DS |
8091 | perform_or_defer_access_check (TYPE_BINFO (type_scope), |
8092 | type_decl, type_decl); | |
d0940d56 | 8093 | } |
aa373032 | 8094 | input_location = saved_location; |
d0940d56 DS |
8095 | } |
8096 | ||
8d08fdba | 8097 | tree |
3a978d72 | 8098 | instantiate_class_template (tree type) |
8d08fdba | 8099 | { |
be93747e | 8100 | tree templ, args, pattern, t, member; |
36a117a5 | 8101 | tree typedecl; |
dbbf88d1 | 8102 | tree pbinfo; |
cad7e87b | 8103 | tree base_list; |
040ca4b3 | 8104 | unsigned int saved_maximum_field_alignment; |
c8094d83 | 8105 | |
5566b478 | 8106 | if (type == error_mark_node) |
8d08fdba MS |
8107 | return error_mark_node; |
8108 | ||
01628e54 | 8109 | if (COMPLETE_OR_OPEN_TYPE_P (type) |
2395cd2e | 8110 | || uses_template_parms (type)) |
5566b478 MS |
8111 | return type; |
8112 | ||
6bdb985f | 8113 | /* Figure out which template is being instantiated. */ |
be93747e KG |
8114 | templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
8115 | gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL); | |
73aad9b9 | 8116 | |
4c571114 MM |
8117 | /* Determine what specialization of the original template to |
8118 | instantiate. */ | |
ebc258f1 | 8119 | t = most_specialized_class (type, templ, tf_warning_or_error); |
8fbc5ae7 | 8120 | if (t == error_mark_node) |
73aad9b9 | 8121 | { |
8fbc5ae7 MM |
8122 | TYPE_BEING_DEFINED (type) = 1; |
8123 | return error_mark_node; | |
73aad9b9 | 8124 | } |
916b63c3 MM |
8125 | else if (t) |
8126 | { | |
8127 | /* This TYPE is actually an instantiation of a partial | |
8128 | specialization. We replace the innermost set of ARGS with | |
8129 | the arguments appropriate for substitution. For example, | |
8130 | given: | |
3db45ab5 MS |
8131 | |
8132 | template <class T> struct S {}; | |
916b63c3 | 8133 | template <class T> struct S<T*> {}; |
6bdb985f | 8134 | |
916b63c3 | 8135 | and supposing that we are instantiating S<int*>, ARGS will |
3db45ab5 | 8136 | presently be {int*} -- but we need {int}. */ |
916b63c3 MM |
8137 | pattern = TREE_TYPE (t); |
8138 | args = TREE_PURPOSE (t); | |
8139 | } | |
73aad9b9 | 8140 | else |
916b63c3 | 8141 | { |
be93747e | 8142 | pattern = TREE_TYPE (templ); |
916b63c3 MM |
8143 | args = CLASSTYPE_TI_ARGS (type); |
8144 | } | |
5566b478 | 8145 | |
4c571114 MM |
8146 | /* If the template we're instantiating is incomplete, then clearly |
8147 | there's nothing we can do. */ | |
d0f062fb | 8148 | if (!COMPLETE_TYPE_P (pattern)) |
f31c0a32 | 8149 | return type; |
5566b478 | 8150 | |
4c571114 MM |
8151 | /* If we've recursively instantiated too many templates, stop. */ |
8152 | if (! push_tinst_level (type)) | |
f31c0a32 | 8153 | return type; |
4c571114 MM |
8154 | |
8155 | /* Now we're really doing the instantiation. Mark the type as in | |
8156 | the process of being defined. */ | |
8157 | TYPE_BEING_DEFINED (type) = 1; | |
8158 | ||
78757caa KL |
8159 | /* We may be in the middle of deferred access check. Disable |
8160 | it now. */ | |
8161 | push_deferring_access_checks (dk_no_deferred); | |
8162 | ||
c353b8e3 | 8163 | push_to_top_level (); |
040ca4b3 JM |
8164 | /* Use #pragma pack from the template context. */ |
8165 | saved_maximum_field_alignment = maximum_field_alignment; | |
8166 | maximum_field_alignment = TYPE_PRECISION (pattern); | |
4c571114 | 8167 | |
7813d14c | 8168 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
8d08fdba | 8169 | |
2eb25c98 DS |
8170 | /* Set the input location to the most specialized template definition. |
8171 | This is needed if tsubsting causes an error. */ | |
8172 | typedecl = TYPE_MAIN_DECL (pattern); | |
0603fe68 JY |
8173 | input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) = |
8174 | DECL_SOURCE_LOCATION (typedecl); | |
68ea098a | 8175 | |
0fcedd9c | 8176 | TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern); |
834c6dff MM |
8177 | TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); |
8178 | TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); | |
f7da6097 | 8179 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); |
066ec0a4 JM |
8180 | TYPE_HAS_COPY_ASSIGN (type) = TYPE_HAS_COPY_ASSIGN (pattern); |
8181 | TYPE_HAS_CONST_COPY_ASSIGN (type) = TYPE_HAS_CONST_COPY_ASSIGN (pattern); | |
8182 | TYPE_HAS_COPY_CTOR (type) = TYPE_HAS_COPY_CTOR (pattern); | |
8183 | TYPE_HAS_CONST_COPY_CTOR (type) = TYPE_HAS_CONST_COPY_CTOR (pattern); | |
f7da6097 MS |
8184 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
8185 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
f7da6097 MS |
8186 | TYPE_PACKED (type) = TYPE_PACKED (pattern); |
8187 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
11cf4d18 | 8188 | TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); |
eff71ab0 | 8189 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
6bdb8141 JM |
8190 | if (ANON_AGGR_TYPE_P (pattern)) |
8191 | SET_ANON_AGGR_TYPE_P (type); | |
8d039470 MS |
8192 | if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern)) |
8193 | { | |
8194 | CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1; | |
8195 | CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern); | |
8196 | } | |
f7da6097 | 8197 | |
dbbf88d1 | 8198 | pbinfo = TYPE_BINFO (pattern); |
1456deaf | 8199 | |
315fb5db NS |
8200 | /* We should never instantiate a nested class before its enclosing |
8201 | class; we need to look up the nested class by name before we can | |
8202 | instantiate it, and that lookup should instantiate the enclosing | |
8203 | class. */ | |
8204 | gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) | |
01628e54 | 8205 | || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type))); |
1456deaf | 8206 | |
cad7e87b | 8207 | base_list = NULL_TREE; |
fa743e8c | 8208 | if (BINFO_N_BASE_BINFOS (pbinfo)) |
3fd71a52 | 8209 | { |
fa743e8c | 8210 | tree pbase_binfo; |
4514aa8c | 8211 | tree pushed_scope; |
3fd71a52 | 8212 | int i; |
5566b478 | 8213 | |
a2507277 NS |
8214 | /* We must enter the scope containing the type, as that is where |
8215 | the accessibility of types named in dependent bases are | |
8216 | looked up from. */ | |
725214ac | 8217 | pushed_scope = push_scope (CP_TYPE_CONTEXT (type)); |
c8094d83 | 8218 | |
3fd71a52 MM |
8219 | /* Substitute into each of the bases to determine the actual |
8220 | basetypes. */ | |
fa743e8c | 8221 | for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) |
3fd71a52 MM |
8222 | { |
8223 | tree base; | |
fa743e8c | 8224 | tree access = BINFO_BASE_ACCESS (pbinfo, i); |
5d80a306 DG |
8225 | tree expanded_bases = NULL_TREE; |
8226 | int idx, len = 1; | |
8227 | ||
8228 | if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo))) | |
8229 | { | |
8230 | expanded_bases = | |
8231 | tsubst_pack_expansion (BINFO_TYPE (pbase_binfo), | |
8232 | args, tf_error, NULL_TREE); | |
8233 | if (expanded_bases == error_mark_node) | |
8234 | continue; | |
8235 | ||
8236 | len = TREE_VEC_LENGTH (expanded_bases); | |
8237 | } | |
8238 | ||
8239 | for (idx = 0; idx < len; idx++) | |
8240 | { | |
8241 | if (expanded_bases) | |
8242 | /* Extract the already-expanded base class. */ | |
8243 | base = TREE_VEC_ELT (expanded_bases, idx); | |
8244 | else | |
8245 | /* Substitute to figure out the base class. */ | |
8246 | base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, | |
8247 | NULL_TREE); | |
8248 | ||
8249 | if (base == error_mark_node) | |
8250 | continue; | |
8251 | ||
8252 | base_list = tree_cons (access, base, base_list); | |
8253 | if (BINFO_VIRTUAL_P (pbase_binfo)) | |
8254 | TREE_TYPE (base_list) = integer_type_node; | |
8255 | } | |
3fd71a52 | 8256 | } |
dfbcd65a | 8257 | |
3fd71a52 MM |
8258 | /* The list is now in reverse order; correct that. */ |
8259 | base_list = nreverse (base_list); | |
8260 | ||
4514aa8c NS |
8261 | if (pushed_scope) |
8262 | pop_scope (pushed_scope); | |
3fd71a52 | 8263 | } |
cad7e87b NS |
8264 | /* Now call xref_basetypes to set up all the base-class |
8265 | information. */ | |
8266 | xref_basetypes (type, base_list); | |
8267 | ||
4f4141ff JM |
8268 | apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern), |
8269 | (int) ATTR_FLAG_TYPE_IN_PLACE, | |
8270 | args, tf_error, NULL_TREE); | |
8943989d | 8271 | fixup_attribute_variants (type); |
5566b478 | 8272 | |
b74a0560 MM |
8273 | /* Now that our base classes are set up, enter the scope of the |
8274 | class, so that name lookups into base classes, etc. will work | |
dc957d14 | 8275 | correctly. This is precisely analogous to what we do in |
b74a0560 | 8276 | begin_class_definition when defining an ordinary non-template |
56d0c6e3 JM |
8277 | class, except we also need to push the enclosing classes. */ |
8278 | push_nested_class (type); | |
b74a0560 | 8279 | |
7088fca9 | 8280 | /* Now members are processed in the order of declaration. */ |
8c6ab2db NS |
8281 | for (member = CLASSTYPE_DECL_LIST (pattern); |
8282 | member; member = TREE_CHAIN (member)) | |
8d08fdba | 8283 | { |
7088fca9 | 8284 | tree t = TREE_VALUE (member); |
5566b478 | 8285 | |
7088fca9 | 8286 | if (TREE_PURPOSE (member)) |
ed44da02 | 8287 | { |
7088fca9 KL |
8288 | if (TYPE_P (t)) |
8289 | { | |
5e0c54e5 | 8290 | /* Build new CLASSTYPE_NESTED_UTDS. */ |
8d08fdba | 8291 | |
7088fca9 | 8292 | tree newtag; |
883a2bff MM |
8293 | bool class_template_p; |
8294 | ||
2678bae8 VR |
8295 | class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE |
8296 | && TYPE_LANG_SPECIFIC (t) | |
8297 | && CLASSTYPE_IS_TEMPLATE (t)); | |
883a2bff | 8298 | /* If the member is a class template, then -- even after |
6c745393 | 8299 | substitution -- there may be dependent types in the |
883a2bff MM |
8300 | template argument list for the class. We increment |
8301 | PROCESSING_TEMPLATE_DECL so that dependent_type_p, as | |
8302 | that function will assume that no types are dependent | |
8303 | when outside of a template. */ | |
8304 | if (class_template_p) | |
8305 | ++processing_template_decl; | |
2678bae8 | 8306 | newtag = tsubst (t, args, tf_error, NULL_TREE); |
883a2bff MM |
8307 | if (class_template_p) |
8308 | --processing_template_decl; | |
2620d095 KL |
8309 | if (newtag == error_mark_node) |
8310 | continue; | |
8311 | ||
7088fca9 KL |
8312 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
8313 | { | |
2678bae8 VR |
8314 | tree name = TYPE_IDENTIFIER (t); |
8315 | ||
883a2bff | 8316 | if (class_template_p) |
7088fca9 KL |
8317 | /* Unfortunately, lookup_template_class sets |
8318 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
3afd2e20 NS |
8319 | instantiation (i.e., for the type of a member |
8320 | template class nested within a template class.) | |
8321 | This behavior is required for | |
8322 | maybe_process_partial_specialization to work | |
8323 | correctly, but is not accurate in this case; | |
8324 | the TAG is not an instantiation of anything. | |
8325 | (The corresponding TEMPLATE_DECL is an | |
8326 | instantiation, but the TYPE is not.) */ | |
7088fca9 KL |
8327 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; |
8328 | ||
8329 | /* Now, we call pushtag to put this NEWTAG into the scope of | |
8330 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
8331 | pushtag calling push_template_decl. We don't have to do | |
8332 | this for enums because it will already have been done in | |
8333 | tsubst_enum. */ | |
8334 | if (name) | |
8335 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
bd3d082e | 8336 | pushtag (name, newtag, /*tag_scope=*/ts_current); |
7088fca9 KL |
8337 | } |
8338 | } | |
c8094d83 | 8339 | else if (TREE_CODE (t) == FUNCTION_DECL |
7088fca9 KL |
8340 | || DECL_FUNCTION_TEMPLATE_P (t)) |
8341 | { | |
8342 | /* Build new TYPE_METHODS. */ | |
fecafe5e | 8343 | tree r; |
c8094d83 | 8344 | |
fecafe5e | 8345 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 8346 | ++processing_template_decl; |
fecafe5e NS |
8347 | r = tsubst (t, args, tf_error, NULL_TREE); |
8348 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
9579624e | 8349 | --processing_template_decl; |
7088fca9 | 8350 | set_current_access_from_decl (r); |
7088fca9 KL |
8351 | finish_member_declaration (r); |
8352 | } | |
8353 | else | |
8354 | { | |
8355 | /* Build new TYPE_FIELDS. */ | |
55a3debe DG |
8356 | if (TREE_CODE (t) == STATIC_ASSERT) |
8357 | { | |
8358 | tree condition = | |
8359 | tsubst_expr (STATIC_ASSERT_CONDITION (t), args, | |
8360 | tf_warning_or_error, NULL_TREE, | |
8361 | /*integral_constant_expression_p=*/true); | |
8362 | finish_static_assert (condition, | |
8363 | STATIC_ASSERT_MESSAGE (t), | |
8364 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
8365 | /*member_p=*/true); | |
8366 | } | |
8367 | else if (TREE_CODE (t) != CONST_DECL) | |
7088fca9 KL |
8368 | { |
8369 | tree r; | |
fa8d6e85 | 8370 | |
39a13be5 | 8371 | /* The file and line for this declaration, to |
d479d37f NS |
8372 | assist in error message reporting. Since we |
8373 | called push_tinst_level above, we don't need to | |
8374 | restore these. */ | |
f31686a3 | 8375 | input_location = DECL_SOURCE_LOCATION (t); |
fa8d6e85 | 8376 | |
fb5ce3c9 | 8377 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 8378 | ++processing_template_decl; |
23fca1f5 | 8379 | r = tsubst (t, args, tf_warning_or_error, NULL_TREE); |
fb5ce3c9 | 8380 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 8381 | --processing_template_decl; |
7088fca9 KL |
8382 | if (TREE_CODE (r) == VAR_DECL) |
8383 | { | |
b794e321 MM |
8384 | /* In [temp.inst]: |
8385 | ||
8386 | [t]he initialization (and any associated | |
8387 | side-effects) of a static data member does | |
8388 | not occur unless the static data member is | |
8389 | itself used in a way that requires the | |
8390 | definition of the static data member to | |
3db45ab5 | 8391 | exist. |
b794e321 MM |
8392 | |
8393 | Therefore, we do not substitute into the | |
3db45ab5 MS |
8394 | initialized for the static data member here. */ |
8395 | finish_static_data_member_decl | |
8396 | (r, | |
8397 | /*init=*/NULL_TREE, | |
d174af6c | 8398 | /*init_const_expr_p=*/false, |
3db45ab5 | 8399 | /*asmspec_tree=*/NULL_TREE, |
b794e321 | 8400 | /*flags=*/0); |
7088fca9 KL |
8401 | if (DECL_INITIALIZED_IN_CLASS_P (r)) |
8402 | check_static_variable_definition (r, TREE_TYPE (r)); | |
8403 | } | |
8404 | else if (TREE_CODE (r) == FIELD_DECL) | |
8405 | { | |
8406 | /* Determine whether R has a valid type and can be | |
8407 | completed later. If R is invalid, then it is | |
8408 | replaced by error_mark_node so that it will not be | |
8409 | added to TYPE_FIELDS. */ | |
8410 | tree rtype = TREE_TYPE (r); | |
8411 | if (can_complete_type_without_circularity (rtype)) | |
8412 | complete_type (rtype); | |
8413 | ||
8414 | if (!COMPLETE_TYPE_P (rtype)) | |
8415 | { | |
8416 | cxx_incomplete_type_error (r, rtype); | |
0cbd7506 | 8417 | r = error_mark_node; |
7088fca9 KL |
8418 | } |
8419 | } | |
5566b478 | 8420 | |
7088fca9 KL |
8421 | /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, |
8422 | such a thing will already have been added to the field | |
8423 | list by tsubst_enum in finish_member_declaration in the | |
5e0c54e5 | 8424 | CLASSTYPE_NESTED_UTDS case above. */ |
7088fca9 KL |
8425 | if (!(TREE_CODE (r) == TYPE_DECL |
8426 | && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE | |
43b26a01 | 8427 | && DECL_ARTIFICIAL (r))) |
7088fca9 KL |
8428 | { |
8429 | set_current_access_from_decl (r); | |
8430 | finish_member_declaration (r); | |
8431 | } | |
0cbd7506 | 8432 | } |
7088fca9 | 8433 | } |
61fc8c9e | 8434 | } |
7088fca9 KL |
8435 | else |
8436 | { | |
8437 | if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) | |
8438 | { | |
8439 | /* Build new CLASSTYPE_FRIEND_CLASSES. */ | |
8440 | ||
8441 | tree friend_type = t; | |
b939a023 | 8442 | bool adjust_processing_template_decl = false; |
1aed5355 | 8443 | |
7088fca9 | 8444 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
b939a023 | 8445 | { |
5a24482e | 8446 | /* template <class T> friend class C; */ |
b939a023 | 8447 | friend_type = tsubst_friend_class (friend_type, args); |
0cbd7506 | 8448 | adjust_processing_template_decl = true; |
b939a023 KL |
8449 | } |
8450 | else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE) | |
8451 | { | |
5a24482e | 8452 | /* template <class T> friend class C::D; */ |
b939a023 | 8453 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 8454 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
8455 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
8456 | friend_type = TREE_TYPE (friend_type); | |
0cbd7506 | 8457 | adjust_processing_template_decl = true; |
b939a023 KL |
8458 | } |
8459 | else if (TREE_CODE (friend_type) == TYPENAME_TYPE) | |
8460 | { | |
5a24482e KL |
8461 | /* This could be either |
8462 | ||
8463 | friend class T::C; | |
8464 | ||
8465 | when dependent_type_p is false or | |
8466 | ||
8467 | template <class U> friend class T::C; | |
8468 | ||
8469 | otherwise. */ | |
b939a023 | 8470 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 8471 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
8472 | /* Bump processing_template_decl for correct |
8473 | dependent_type_p calculation. */ | |
8474 | ++processing_template_decl; | |
8475 | if (dependent_type_p (friend_type)) | |
8476 | adjust_processing_template_decl = true; | |
8477 | --processing_template_decl; | |
8478 | } | |
5a24482e KL |
8479 | else if (!CLASSTYPE_USE_TEMPLATE (friend_type) |
8480 | && hidden_name_p (TYPE_NAME (friend_type))) | |
7088fca9 | 8481 | { |
5a24482e KL |
8482 | /* friend class C; |
8483 | ||
8484 | where C hasn't been declared yet. Let's lookup name | |
8485 | from namespace scope directly, bypassing any name that | |
8486 | come from dependent base class. */ | |
7088fca9 KL |
8487 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); |
8488 | ||
8489 | /* The call to xref_tag_from_type does injection for friend | |
8490 | classes. */ | |
8491 | push_nested_namespace (ns); | |
c8094d83 MS |
8492 | friend_type = |
8493 | xref_tag_from_type (friend_type, NULL_TREE, | |
5a24482e | 8494 | /*tag_scope=*/ts_current); |
7088fca9 KL |
8495 | pop_nested_namespace (ns); |
8496 | } | |
5a24482e KL |
8497 | else if (uses_template_parms (friend_type)) |
8498 | /* friend class C<T>; */ | |
8499 | friend_type = tsubst (friend_type, args, | |
23fca1f5 | 8500 | tf_warning_or_error, NULL_TREE); |
5a24482e KL |
8501 | /* Otherwise it's |
8502 | ||
8503 | friend class C; | |
8504 | ||
8505 | where C is already declared or | |
8506 | ||
8507 | friend class C<int>; | |
8508 | ||
0cbd7506 | 8509 | We don't have to do anything in these cases. */ |
1aed5355 | 8510 | |
b939a023 | 8511 | if (adjust_processing_template_decl) |
7088fca9 KL |
8512 | /* Trick make_friend_class into realizing that the friend |
8513 | we're adding is a template, not an ordinary class. It's | |
8514 | important that we use make_friend_class since it will | |
8515 | perform some error-checking and output cross-reference | |
8516 | information. */ | |
8517 | ++processing_template_decl; | |
fc378698 | 8518 | |
b939a023 | 8519 | if (friend_type != error_mark_node) |
0cbd7506 | 8520 | make_friend_class (type, friend_type, /*complain=*/false); |
fc378698 | 8521 | |
b939a023 | 8522 | if (adjust_processing_template_decl) |
7088fca9 KL |
8523 | --processing_template_decl; |
8524 | } | |
8525 | else | |
9579624e KL |
8526 | { |
8527 | /* Build new DECL_FRIENDLIST. */ | |
8528 | tree r; | |
8529 | ||
39a13be5 | 8530 | /* The file and line for this declaration, to |
6e432b31 KL |
8531 | assist in error message reporting. Since we |
8532 | called push_tinst_level above, we don't need to | |
8533 | restore these. */ | |
8534 | input_location = DECL_SOURCE_LOCATION (t); | |
8535 | ||
9579624e | 8536 | if (TREE_CODE (t) == TEMPLATE_DECL) |
cad7e87b NS |
8537 | { |
8538 | ++processing_template_decl; | |
8539 | push_deferring_access_checks (dk_no_check); | |
8540 | } | |
c8094d83 | 8541 | |
9579624e | 8542 | r = tsubst_friend_function (t, args); |
9579624e | 8543 | add_friend (type, r, /*complain=*/false); |
cad7e87b NS |
8544 | if (TREE_CODE (t) == TEMPLATE_DECL) |
8545 | { | |
8546 | pop_deferring_access_checks (); | |
8547 | --processing_template_decl; | |
8548 | } | |
9579624e | 8549 | } |
7088fca9 KL |
8550 | } |
8551 | } | |
5566b478 | 8552 | |
61a127b3 MM |
8553 | /* Set the file and line number information to whatever is given for |
8554 | the class itself. This puts error messages involving generated | |
8555 | implicit functions at a predictable point, and the same point | |
8556 | that would be used for non-template classes. */ | |
f31686a3 | 8557 | input_location = DECL_SOURCE_LOCATION (typedecl); |
6de9cd9a | 8558 | |
61a127b3 | 8559 | unreverse_member_declarations (type); |
9f33663b | 8560 | finish_struct_1 (type); |
5524676d | 8561 | TYPE_BEING_DEFINED (type) = 0; |
8d08fdba | 8562 | |
59dd34c4 JM |
8563 | /* We don't instantiate default arguments for member functions. 14.7.1: |
8564 | ||
8565 | The implicit instantiation of a class template specialization causes | |
8566 | the implicit instantiation of the declarations, but not of the | |
8567 | definitions or default arguments, of the class member functions, | |
8568 | member classes, static data members and member templates.... */ | |
9188c363 | 8569 | |
d0940d56 DS |
8570 | /* Some typedefs referenced from within the template code need to be access |
8571 | checked at template instantiation time, i.e now. These types were | |
8572 | added to the template at parsing time. Let's get those and perform | |
2eb25c98 DS |
8573 | the access checks then. */ |
8574 | perform_typedefs_access_check (pattern, args); | |
d0940d56 | 8575 | perform_deferred_access_checks (); |
56d0c6e3 | 8576 | pop_nested_class (); |
040ca4b3 | 8577 | maximum_field_alignment = saved_maximum_field_alignment; |
5566b478 | 8578 | pop_from_top_level (); |
78757caa | 8579 | pop_deferring_access_checks (); |
5566b478 MS |
8580 | pop_tinst_level (); |
8581 | ||
4684cd27 MM |
8582 | /* The vtable for a template class can be emitted in any translation |
8583 | unit in which the class is instantiated. When there is no key | |
8584 | method, however, finish_struct_1 will already have added TYPE to | |
8585 | the keyed_classes list. */ | |
8586 | if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type)) | |
9aad8f83 MA |
8587 | keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); |
8588 | ||
5566b478 | 8589 | return type; |
8d08fdba MS |
8590 | } |
8591 | ||
00d3396f | 8592 | static tree |
a91db711 | 8593 | tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
00d3396f | 8594 | { |
a91db711 | 8595 | tree r; |
c8094d83 | 8596 | |
a91db711 NS |
8597 | if (!t) |
8598 | r = t; | |
8599 | else if (TYPE_P (t)) | |
8600 | r = tsubst (t, args, complain, in_decl); | |
8601 | else | |
00d3396f | 8602 | { |
40013af7 JM |
8603 | if (!(complain & tf_warning)) |
8604 | ++c_inhibit_evaluation_warnings; | |
015c2c66 MM |
8605 | r = tsubst_expr (t, args, complain, in_decl, |
8606 | /*integral_constant_expression_p=*/true); | |
40013af7 JM |
8607 | if (!(complain & tf_warning)) |
8608 | --c_inhibit_evaluation_warnings; | |
bd83b409 | 8609 | } |
5d80a306 DG |
8610 | return r; |
8611 | } | |
8612 | ||
6afcfe0a JM |
8613 | /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a |
8614 | NONTYPE_ARGUMENT_PACK. */ | |
8615 | ||
8616 | static tree | |
8617 | make_fnparm_pack (tree spec_parm) | |
8618 | { | |
8619 | /* Collect all of the extra "packed" parameters into an | |
8620 | argument pack. */ | |
8621 | tree parmvec; | |
8622 | tree parmtypevec; | |
8623 | tree argpack = make_node (NONTYPE_ARGUMENT_PACK); | |
7ecbca9d | 8624 | tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK); |
6afcfe0a JM |
8625 | int i, len = list_length (spec_parm); |
8626 | ||
8627 | /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */ | |
8628 | parmvec = make_tree_vec (len); | |
8629 | parmtypevec = make_tree_vec (len); | |
910ad8de | 8630 | for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm)) |
6afcfe0a JM |
8631 | { |
8632 | TREE_VEC_ELT (parmvec, i) = spec_parm; | |
8633 | TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm); | |
8634 | } | |
8635 | ||
8636 | /* Build the argument packs. */ | |
8637 | SET_ARGUMENT_PACK_ARGS (argpack, parmvec); | |
8638 | SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec); | |
8639 | TREE_TYPE (argpack) = argtypepack; | |
8640 | ||
8641 | return argpack; | |
8642 | } | |
8643 | ||
5d80a306 DG |
8644 | /* Substitute ARGS into T, which is an pack expansion |
8645 | (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a | |
8646 | TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node | |
8647 | (if only a partial substitution could be performed) or | |
8648 | ERROR_MARK_NODE if there was an error. */ | |
8649 | tree | |
8650 | tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, | |
8651 | tree in_decl) | |
8652 | { | |
8653 | tree pattern; | |
8654 | tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE; | |
db80e34e | 8655 | int i, len = -1; |
5d80a306 DG |
8656 | tree result; |
8657 | int incomplete = 0; | |
1dba29f8 | 8658 | htab_t saved_local_specializations = NULL; |
5d80a306 DG |
8659 | |
8660 | gcc_assert (PACK_EXPANSION_P (t)); | |
8661 | pattern = PACK_EXPANSION_PATTERN (t); | |
8662 | ||
8663 | /* Determine the argument packs that will instantiate the parameter | |
8664 | packs used in the expansion expression. While we're at it, | |
8665 | compute the number of arguments to be expanded and make sure it | |
8666 | is consistent. */ | |
8667 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack; | |
8668 | pack = TREE_CHAIN (pack)) | |
8669 | { | |
8670 | tree parm_pack = TREE_VALUE (pack); | |
8671 | tree arg_pack = NULL_TREE; | |
8672 | tree orig_arg = NULL_TREE; | |
8673 | ||
8674 | if (TREE_CODE (parm_pack) == PARM_DECL) | |
6afcfe0a | 8675 | { |
1dba29f8 JM |
8676 | if (!cp_unevaluated_operand) |
8677 | arg_pack = retrieve_local_specialization (parm_pack); | |
8678 | else | |
6afcfe0a | 8679 | { |
1dba29f8 JM |
8680 | /* We can't rely on local_specializations for a parameter |
8681 | name used later in a function declaration (such as in a | |
8682 | late-specified return type). Even if it exists, it might | |
8683 | have the wrong value for a recursive call. Just make a | |
8684 | dummy decl, since it's only used for its type. */ | |
6afcfe0a JM |
8685 | arg_pack = tsubst_decl (parm_pack, args, complain); |
8686 | arg_pack = make_fnparm_pack (arg_pack); | |
8687 | } | |
8688 | } | |
5d80a306 DG |
8689 | else |
8690 | { | |
8691 | int level, idx, levels; | |
8692 | template_parm_level_and_index (parm_pack, &level, &idx); | |
8693 | ||
8694 | levels = TMPL_ARGS_DEPTH (args); | |
8695 | if (level <= levels) | |
8696 | arg_pack = TMPL_ARG (args, level, idx); | |
8697 | } | |
8698 | ||
8699 | orig_arg = arg_pack; | |
8700 | if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT) | |
8701 | arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack); | |
8702 | ||
d393153e DG |
8703 | if (arg_pack && !ARGUMENT_PACK_P (arg_pack)) |
8704 | /* This can only happen if we forget to expand an argument | |
8705 | pack somewhere else. Just return an error, silently. */ | |
8706 | { | |
8707 | result = make_tree_vec (1); | |
8708 | TREE_VEC_ELT (result, 0) = error_mark_node; | |
8709 | return result; | |
8710 | } | |
8711 | ||
88b82314 DG |
8712 | if (arg_pack |
8713 | && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1 | |
8714 | && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0))) | |
8715 | { | |
8716 | tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0); | |
8717 | tree pattern = PACK_EXPANSION_PATTERN (expansion); | |
8718 | if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack)) | |
8719 | || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern))) | |
8720 | /* The argument pack that the parameter maps to is just an | |
8721 | expansion of the parameter itself, such as one would | |
8722 | find in the implicit typedef of a class inside the | |
8723 | class itself. Consider this parameter "unsubstituted", | |
8724 | so that we will maintain the outer pack expansion. */ | |
8725 | arg_pack = NULL_TREE; | |
8726 | } | |
8727 | ||
5d80a306 DG |
8728 | if (arg_pack) |
8729 | { | |
8730 | int my_len = | |
8731 | TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)); | |
8732 | ||
8733 | /* It's all-or-nothing with incomplete argument packs. */ | |
8734 | if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
8735 | return error_mark_node; | |
8736 | ||
8737 | if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
8738 | incomplete = 1; | |
8739 | ||
8740 | if (len < 0) | |
db80e34e | 8741 | len = my_len; |
5d80a306 DG |
8742 | else if (len != my_len) |
8743 | { | |
f484a91a JM |
8744 | if (incomplete) |
8745 | /* We got explicit args for some packs but not others; | |
8746 | do nothing now and try again after deduction. */ | |
8747 | return t; | |
5d80a306 DG |
8748 | if (TREE_CODE (t) == TYPE_PACK_EXPANSION) |
8749 | error ("mismatched argument pack lengths while expanding " | |
8750 | "%<%T%>", | |
8751 | pattern); | |
8752 | else | |
8753 | error ("mismatched argument pack lengths while expanding " | |
8754 | "%<%E%>", | |
8755 | pattern); | |
8756 | return error_mark_node; | |
8757 | } | |
8758 | ||
8759 | /* Keep track of the parameter packs and their corresponding | |
8760 | argument packs. */ | |
8761 | packs = tree_cons (parm_pack, arg_pack, packs); | |
8762 | TREE_TYPE (packs) = orig_arg; | |
8763 | } | |
8764 | else | |
8765 | /* We can't substitute for this parameter pack. */ | |
8766 | unsubstituted_packs = tree_cons (TREE_PURPOSE (pack), | |
8767 | TREE_VALUE (pack), | |
8768 | unsubstituted_packs); | |
8769 | } | |
8770 | ||
8771 | /* We cannot expand this expansion expression, because we don't have | |
8772 | all of the argument packs we need. Substitute into the pattern | |
8773 | and return a PACK_EXPANSION_*. The caller will need to deal with | |
8774 | that. */ | |
8775 | if (unsubstituted_packs) | |
0db906f1 JM |
8776 | { |
8777 | tree new_pat; | |
8778 | if (TREE_CODE (t) == EXPR_PACK_EXPANSION) | |
8779 | new_pat = tsubst_expr (pattern, args, complain, in_decl, | |
8780 | /*integral_constant_expression_p=*/false); | |
8781 | else | |
8782 | new_pat = tsubst (pattern, args, complain, in_decl); | |
8783 | return make_pack_expansion (new_pat); | |
8784 | } | |
5d80a306 DG |
8785 | |
8786 | /* We could not find any argument packs that work. */ | |
8787 | if (len < 0) | |
8788 | return error_mark_node; | |
8789 | ||
1dba29f8 | 8790 | if (cp_unevaluated_operand) |
6afcfe0a | 8791 | { |
1dba29f8 JM |
8792 | /* We're in a late-specified return type, so create our own local |
8793 | specializations table; the current table is either NULL or (in the | |
8794 | case of recursive unification) might have bindings that we don't | |
8795 | want to use or alter. */ | |
8796 | saved_local_specializations = local_specializations; | |
6afcfe0a JM |
8797 | local_specializations = htab_create (37, |
8798 | hash_local_specialization, | |
8799 | eq_local_specializations, | |
8800 | NULL); | |
8801 | } | |
8802 | ||
5d80a306 DG |
8803 | /* For each argument in each argument pack, substitute into the |
8804 | pattern. */ | |
8805 | result = make_tree_vec (len + incomplete); | |
8806 | for (i = 0; i < len + incomplete; ++i) | |
8807 | { | |
8808 | /* For parameter pack, change the substitution of the parameter | |
8809 | pack to the ith argument in its argument pack, then expand | |
8810 | the pattern. */ | |
8811 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
8812 | { | |
8813 | tree parm = TREE_PURPOSE (pack); | |
8814 | ||
8815 | if (TREE_CODE (parm) == PARM_DECL) | |
8816 | { | |
8817 | /* Select the Ith argument from the pack. */ | |
8818 | tree arg = make_node (ARGUMENT_PACK_SELECT); | |
8819 | ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack); | |
8820 | ARGUMENT_PACK_SELECT_INDEX (arg) = i; | |
8821 | mark_used (parm); | |
8822 | register_local_specialization (arg, parm); | |
8823 | } | |
8824 | else | |
8825 | { | |
8826 | tree value = parm; | |
8827 | int idx, level; | |
8828 | template_parm_level_and_index (parm, &level, &idx); | |
8829 | ||
8830 | if (i < len) | |
8831 | { | |
8832 | /* Select the Ith argument from the pack. */ | |
8833 | value = make_node (ARGUMENT_PACK_SELECT); | |
8834 | ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack); | |
8835 | ARGUMENT_PACK_SELECT_INDEX (value) = i; | |
8836 | } | |
8837 | ||
8838 | /* Update the corresponding argument. */ | |
8839 | TMPL_ARG (args, level, idx) = value; | |
8840 | } | |
8841 | } | |
8842 | ||
8843 | /* Substitute into the PATTERN with the altered arguments. */ | |
8844 | if (TREE_CODE (t) == EXPR_PACK_EXPANSION) | |
8845 | TREE_VEC_ELT (result, i) = | |
8846 | tsubst_expr (pattern, args, complain, in_decl, | |
8847 | /*integral_constant_expression_p=*/false); | |
8848 | else | |
8849 | TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl); | |
8850 | ||
8851 | if (i == len) | |
8852 | /* When we have incomplete argument packs, the last "expanded" | |
8853 | result is itself a pack expansion, which allows us | |
8854 | to deduce more arguments. */ | |
8855 | TREE_VEC_ELT (result, i) = | |
8856 | make_pack_expansion (TREE_VEC_ELT (result, i)); | |
8857 | ||
8858 | if (TREE_VEC_ELT (result, i) == error_mark_node) | |
8859 | { | |
8860 | result = error_mark_node; | |
8861 | break; | |
8862 | } | |
8863 | } | |
6afcfe0a | 8864 | |
5d80a306 DG |
8865 | /* Update ARGS to restore the substitution from parameter packs to |
8866 | their argument packs. */ | |
8867 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
8868 | { | |
8869 | tree parm = TREE_PURPOSE (pack); | |
8870 | ||
8871 | if (TREE_CODE (parm) == PARM_DECL) | |
8872 | register_local_specialization (TREE_TYPE (pack), parm); | |
8873 | else | |
8874 | { | |
8875 | int idx, level; | |
8876 | template_parm_level_and_index (parm, &level, &idx); | |
8877 | ||
8878 | /* Update the corresponding argument. */ | |
8879 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
8880 | TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) = | |
8881 | TREE_TYPE (pack); | |
8882 | else | |
8883 | TREE_VEC_ELT (args, idx) = TREE_TYPE (pack); | |
8884 | } | |
8885 | } | |
8886 | ||
1dba29f8 | 8887 | if (saved_local_specializations) |
6afcfe0a JM |
8888 | { |
8889 | htab_delete (local_specializations); | |
1dba29f8 | 8890 | local_specializations = saved_local_specializations; |
6afcfe0a JM |
8891 | } |
8892 | ||
5d80a306 | 8893 | return result; |
bd83b409 NS |
8894 | } |
8895 | ||
5f5babf1 JM |
8896 | /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template |
8897 | TMPL. We do this using DECL_PARM_INDEX, which should work even with | |
8898 | parameter packs; all parms generated from a function parameter pack will | |
8899 | have the same DECL_PARM_INDEX. */ | |
8900 | ||
8901 | tree | |
8902 | get_pattern_parm (tree parm, tree tmpl) | |
8903 | { | |
8904 | tree pattern = DECL_TEMPLATE_RESULT (tmpl); | |
8905 | tree patparm; | |
8906 | ||
8907 | if (DECL_ARTIFICIAL (parm)) | |
8908 | { | |
8909 | for (patparm = DECL_ARGUMENTS (pattern); | |
910ad8de | 8910 | patparm; patparm = DECL_CHAIN (patparm)) |
5f5babf1 JM |
8911 | if (DECL_ARTIFICIAL (patparm) |
8912 | && DECL_NAME (parm) == DECL_NAME (patparm)) | |
8913 | break; | |
8914 | } | |
8915 | else | |
8916 | { | |
8917 | patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl)); | |
8918 | patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm); | |
8919 | gcc_assert (DECL_PARM_INDEX (patparm) | |
8920 | == DECL_PARM_INDEX (parm)); | |
8921 | } | |
8922 | ||
8923 | return patparm; | |
8924 | } | |
8925 | ||
a91db711 | 8926 | /* Substitute ARGS into the vector or list of template arguments T. */ |
830bfa74 | 8927 | |
e9659ab0 | 8928 | static tree |
a91db711 | 8929 | tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
830bfa74 | 8930 | { |
5d80a306 | 8931 | tree orig_t = t; |
bf12d54d | 8932 | int len = TREE_VEC_LENGTH (t); |
5d80a306 | 8933 | int need_new = 0, i, expanded_len_adjust = 0, out; |
86b8fed1 | 8934 | tree *elts = XALLOCAVEC (tree, len); |
c8094d83 | 8935 | |
830bfa74 MM |
8936 | for (i = 0; i < len; i++) |
8937 | { | |
bf12d54d NS |
8938 | tree orig_arg = TREE_VEC_ELT (t, i); |
8939 | tree new_arg; | |
a91db711 | 8940 | |
bf12d54d NS |
8941 | if (TREE_CODE (orig_arg) == TREE_VEC) |
8942 | new_arg = tsubst_template_args (orig_arg, args, complain, in_decl); | |
5d80a306 DG |
8943 | else if (PACK_EXPANSION_P (orig_arg)) |
8944 | { | |
8945 | /* Substitute into an expansion expression. */ | |
8946 | new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl); | |
8947 | ||
8948 | if (TREE_CODE (new_arg) == TREE_VEC) | |
8949 | /* Add to the expanded length adjustment the number of | |
8950 | expanded arguments. We subtract one from this | |
8951 | measurement, because the argument pack expression | |
8952 | itself is already counted as 1 in | |
8953 | LEN. EXPANDED_LEN_ADJUST can actually be negative, if | |
8954 | the argument pack is empty. */ | |
8955 | expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1; | |
8956 | } | |
8957 | else if (ARGUMENT_PACK_P (orig_arg)) | |
8958 | { | |
8959 | /* Substitute into each of the arguments. */ | |
7ecbca9d GDR |
8960 | new_arg = TYPE_P (orig_arg) |
8961 | ? cxx_make_type (TREE_CODE (orig_arg)) | |
8962 | : make_node (TREE_CODE (orig_arg)); | |
5d80a306 DG |
8963 | |
8964 | SET_ARGUMENT_PACK_ARGS ( | |
8965 | new_arg, | |
8966 | tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg), | |
8967 | args, complain, in_decl)); | |
8968 | ||
8969 | if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node) | |
8970 | new_arg = error_mark_node; | |
8971 | ||
8972 | if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) { | |
8973 | TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args, | |
8974 | complain, in_decl); | |
8975 | TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg); | |
8976 | ||
8977 | if (TREE_TYPE (new_arg) == error_mark_node) | |
8978 | new_arg = error_mark_node; | |
8979 | } | |
8980 | } | |
830bfa74 | 8981 | else |
a91db711 | 8982 | new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl); |
c8094d83 | 8983 | |
a91db711 | 8984 | if (new_arg == error_mark_node) |
08e72a19 JM |
8985 | return error_mark_node; |
8986 | ||
a91db711 NS |
8987 | elts[i] = new_arg; |
8988 | if (new_arg != orig_arg) | |
830bfa74 MM |
8989 | need_new = 1; |
8990 | } | |
c8094d83 | 8991 | |
830bfa74 MM |
8992 | if (!need_new) |
8993 | return t; | |
a91db711 | 8994 | |
5d80a306 DG |
8995 | /* Make space for the expanded arguments coming from template |
8996 | argument packs. */ | |
8997 | t = make_tree_vec (len + expanded_len_adjust); | |
1a048f82 DS |
8998 | /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the |
8999 | arguments for a member template. | |
9000 | In that case each TREE_VEC in ORIG_T represents a level of template | |
9001 | arguments, and ORIG_T won't carry any non defaulted argument count. | |
9002 | It will rather be the nested TREE_VECs that will carry one. | |
9003 | In other words, ORIG_T carries a non defaulted argument count only | |
9004 | if it doesn't contain any nested TREE_VEC. */ | |
9005 | if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t)) | |
9006 | { | |
9007 | int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t); | |
9008 | count += expanded_len_adjust; | |
9009 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count); | |
9010 | } | |
5d80a306 DG |
9011 | for (i = 0, out = 0; i < len; i++) |
9012 | { | |
9013 | if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i)) | |
9014 | || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i))) | |
9015 | && TREE_CODE (elts[i]) == TREE_VEC) | |
9016 | { | |
9017 | int idx; | |
9018 | ||
9019 | /* Now expand the template argument pack "in place". */ | |
9020 | for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++) | |
9021 | TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx); | |
9022 | } | |
9023 | else | |
9024 | { | |
9025 | TREE_VEC_ELT (t, out) = elts[i]; | |
9026 | out++; | |
9027 | } | |
9028 | } | |
c8094d83 | 9029 | |
830bfa74 MM |
9030 | return t; |
9031 | } | |
9032 | ||
36a117a5 MM |
9033 | /* Return the result of substituting ARGS into the template parameters |
9034 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
9035 | PARMS, then the result will contain n levels of PARMS. For | |
9036 | example, if PARMS is `template <class T> template <class U> | |
9037 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
9038 | result will be `template <int*, double, class V>'. */ | |
9039 | ||
e9659ab0 | 9040 | static tree |
3a978d72 | 9041 | tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain) |
36a117a5 | 9042 | { |
f71f87f9 MM |
9043 | tree r = NULL_TREE; |
9044 | tree* new_parms; | |
36a117a5 | 9045 | |
8ca4bf25 MM |
9046 | /* When substituting into a template, we must set |
9047 | PROCESSING_TEMPLATE_DECL as the template parameters may be | |
9048 | dependent if they are based on one-another, and the dependency | |
9049 | predicates are short-circuit outside of templates. */ | |
9050 | ++processing_template_decl; | |
9051 | ||
36a117a5 MM |
9052 | for (new_parms = &r; |
9053 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
9054 | new_parms = &(TREE_CHAIN (*new_parms)), | |
9055 | parms = TREE_CHAIN (parms)) | |
9056 | { | |
c8094d83 | 9057 | tree new_vec = |
36a117a5 MM |
9058 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); |
9059 | int i; | |
c8094d83 | 9060 | |
36a117a5 MM |
9061 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) |
9062 | { | |
42b304f1 | 9063 | tree tuple; |
42b304f1 LM |
9064 | |
9065 | if (parms == error_mark_node) | |
9066 | continue; | |
9067 | ||
9068 | tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); | |
9069 | ||
9070 | if (tuple == error_mark_node) | |
9071 | continue; | |
9072 | ||
6f1abb06 DS |
9073 | TREE_VEC_ELT (new_vec, i) = |
9074 | tsubst_template_parm (tuple, args, complain); | |
36a117a5 | 9075 | } |
c8094d83 MS |
9076 | |
9077 | *new_parms = | |
9078 | tree_cons (size_int (TMPL_PARMS_DEPTH (parms) | |
4890c2f4 | 9079 | - TMPL_ARGS_DEPTH (args)), |
36a117a5 MM |
9080 | new_vec, NULL_TREE); |
9081 | } | |
9082 | ||
8ca4bf25 MM |
9083 | --processing_template_decl; |
9084 | ||
36a117a5 MM |
9085 | return r; |
9086 | } | |
9087 | ||
6f1abb06 DS |
9088 | /* Return the result of substituting ARGS into one template parameter |
9089 | given by T. T Must be a TREE_LIST which TREE_VALUE is the template | |
9090 | parameter and which TREE_PURPOSE is the default argument of the | |
9091 | template parameter. */ | |
9092 | ||
9093 | static tree | |
9094 | tsubst_template_parm (tree t, tree args, tsubst_flags_t complain) | |
9095 | { | |
9096 | tree default_value, parm_decl; | |
9097 | ||
9098 | if (args == NULL_TREE | |
9099 | || t == NULL_TREE | |
9100 | || t == error_mark_node) | |
9101 | return t; | |
9102 | ||
9103 | gcc_assert (TREE_CODE (t) == TREE_LIST); | |
9104 | ||
9105 | default_value = TREE_PURPOSE (t); | |
9106 | parm_decl = TREE_VALUE (t); | |
9107 | ||
9108 | parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); | |
9109 | if (TREE_CODE (parm_decl) == PARM_DECL | |
9110 | && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain)) | |
9111 | parm_decl = error_mark_node; | |
9112 | default_value = tsubst_template_arg (default_value, args, | |
9113 | complain, NULL_TREE); | |
9114 | ||
9115 | return build_tree_list (default_value, parm_decl); | |
9116 | } | |
9117 | ||
ed44da02 MM |
9118 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
9119 | type T. If T is not an aggregate or enumeration type, it is | |
9120 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
838dfd8a | 9121 | ENTERING_SCOPE is nonzero, T is the context for a template which |
dc957d14 | 9122 | we are presently tsubst'ing. Return the substituted value. */ |
36a117a5 | 9123 | |
e9659ab0 | 9124 | static tree |
c8094d83 | 9125 | tsubst_aggr_type (tree t, |
0cbd7506 MS |
9126 | tree args, |
9127 | tsubst_flags_t complain, | |
9128 | tree in_decl, | |
9129 | int entering_scope) | |
36a117a5 MM |
9130 | { |
9131 | if (t == NULL_TREE) | |
9132 | return NULL_TREE; | |
9133 | ||
9134 | switch (TREE_CODE (t)) | |
9135 | { | |
9136 | case RECORD_TYPE: | |
9137 | if (TYPE_PTRMEMFUNC_P (t)) | |
46c895ac | 9138 | return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); |
36a117a5 | 9139 | |
f4f206f4 | 9140 | /* Else fall through. */ |
ed44da02 | 9141 | case ENUMERAL_TYPE: |
36a117a5 | 9142 | case UNION_TYPE: |
82390eb6 | 9143 | if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t)) |
36a117a5 MM |
9144 | { |
9145 | tree argvec; | |
9146 | tree context; | |
9147 | tree r; | |
7d882b83 ILT |
9148 | int saved_unevaluated_operand; |
9149 | int saved_inhibit_evaluation_warnings; | |
3489ea0c MM |
9150 | |
9151 | /* In "sizeof(X<I>)" we need to evaluate "I". */ | |
7d882b83 ILT |
9152 | saved_unevaluated_operand = cp_unevaluated_operand; |
9153 | cp_unevaluated_operand = 0; | |
9154 | saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; | |
9155 | c_inhibit_evaluation_warnings = 0; | |
36a117a5 MM |
9156 | |
9157 | /* First, determine the context for the type we are looking | |
9158 | up. */ | |
4f7847ca NS |
9159 | context = TYPE_CONTEXT (t); |
9160 | if (context) | |
29b0d1fd JM |
9161 | { |
9162 | context = tsubst_aggr_type (context, args, complain, | |
9163 | in_decl, /*entering_scope=*/1); | |
9164 | /* If context is a nested class inside a class template, | |
9165 | it may still need to be instantiated (c++/33959). */ | |
9166 | if (TYPE_P (context)) | |
9167 | context = complete_type (context); | |
9168 | } | |
36a117a5 MM |
9169 | |
9170 | /* Then, figure out what arguments are appropriate for the | |
9171 | type we are trying to find. For example, given: | |
9172 | ||
9173 | template <class T> struct S; | |
9174 | template <class T, class U> void f(T, U) { S<U> su; } | |
9175 | ||
9176 | and supposing that we are instantiating f<int, double>, | |
9177 | then our ARGS will be {int, double}, but, when looking up | |
9178 | S we only want {double}. */ | |
a91db711 NS |
9179 | argvec = tsubst_template_args (TYPE_TI_ARGS (t), args, |
9180 | complain, in_decl); | |
08e72a19 | 9181 | if (argvec == error_mark_node) |
3489ea0c MM |
9182 | r = error_mark_node; |
9183 | else | |
9184 | { | |
9185 | r = lookup_template_class (t, argvec, in_decl, context, | |
9186 | entering_scope, complain); | |
a3360e77 | 9187 | r = cp_build_qualified_type_real (r, cp_type_quals (t), complain); |
3489ea0c | 9188 | } |
3db45ab5 | 9189 | |
7d882b83 ILT |
9190 | cp_unevaluated_operand = saved_unevaluated_operand; |
9191 | c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings; | |
36a117a5 | 9192 | |
3489ea0c | 9193 | return r; |
36a117a5 | 9194 | } |
c8094d83 | 9195 | else |
36a117a5 MM |
9196 | /* This is not a template type, so there's nothing to do. */ |
9197 | return t; | |
9198 | ||
9199 | default: | |
4393e105 | 9200 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
9201 | } |
9202 | } | |
9203 | ||
9188c363 MM |
9204 | /* Substitute into the default argument ARG (a default argument for |
9205 | FN), which has the indicated TYPE. */ | |
9206 | ||
9207 | tree | |
3a978d72 | 9208 | tsubst_default_argument (tree fn, tree type, tree arg) |
9188c363 | 9209 | { |
2436b51f MM |
9210 | tree saved_class_ptr = NULL_TREE; |
9211 | tree saved_class_ref = NULL_TREE; | |
9212 | ||
e90b1c4a JM |
9213 | /* This can happen in invalid code. */ |
9214 | if (TREE_CODE (arg) == DEFAULT_ARG) | |
9215 | return arg; | |
9216 | ||
9188c363 MM |
9217 | /* This default argument came from a template. Instantiate the |
9218 | default argument here, not in tsubst. In the case of | |
c8094d83 MS |
9219 | something like: |
9220 | ||
9188c363 MM |
9221 | template <class T> |
9222 | struct S { | |
9223 | static T t(); | |
9224 | void f(T = t()); | |
9225 | }; | |
c8094d83 | 9226 | |
9188c363 | 9227 | we must be careful to do name lookup in the scope of S<T>, |
d5a10cf0 | 9228 | rather than in the current class. */ |
2b59fc25 | 9229 | push_access_scope (fn); |
2436b51f MM |
9230 | /* The "this" pointer is not valid in a default argument. */ |
9231 | if (cfun) | |
9232 | { | |
9233 | saved_class_ptr = current_class_ptr; | |
9234 | cp_function_chain->x_current_class_ptr = NULL_TREE; | |
9235 | saved_class_ref = current_class_ref; | |
9236 | cp_function_chain->x_current_class_ref = NULL_TREE; | |
9237 | } | |
9188c363 | 9238 | |
d5a10cf0 | 9239 | push_deferring_access_checks(dk_no_deferred); |
5a8613b2 MM |
9240 | /* The default argument expression may cause implicitly defined |
9241 | member functions to be synthesized, which will result in garbage | |
9242 | collection. We must treat this situation as if we were within | |
9243 | the body of function so as to avoid collecting live data on the | |
9244 | stack. */ | |
9245 | ++function_depth; | |
c2ea3a40 | 9246 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), |
015c2c66 MM |
9247 | tf_warning_or_error, NULL_TREE, |
9248 | /*integral_constant_expression_p=*/false); | |
5a8613b2 | 9249 | --function_depth; |
d5a10cf0 MM |
9250 | pop_deferring_access_checks(); |
9251 | ||
2436b51f MM |
9252 | /* Restore the "this" pointer. */ |
9253 | if (cfun) | |
9254 | { | |
9255 | cp_function_chain->x_current_class_ptr = saved_class_ptr; | |
9256 | cp_function_chain->x_current_class_ref = saved_class_ref; | |
9257 | } | |
9258 | ||
9188c363 MM |
9259 | /* Make sure the default argument is reasonable. */ |
9260 | arg = check_default_argument (type, arg); | |
9261 | ||
3693f266 DS |
9262 | pop_access_scope (fn); |
9263 | ||
9188c363 MM |
9264 | return arg; |
9265 | } | |
9266 | ||
9267 | /* Substitute into all the default arguments for FN. */ | |
9268 | ||
9269 | static void | |
3a978d72 | 9270 | tsubst_default_arguments (tree fn) |
9188c363 MM |
9271 | { |
9272 | tree arg; | |
9273 | tree tmpl_args; | |
9274 | ||
9275 | tmpl_args = DECL_TI_ARGS (fn); | |
9276 | ||
9277 | /* If this function is not yet instantiated, we certainly don't need | |
9278 | its default arguments. */ | |
9279 | if (uses_template_parms (tmpl_args)) | |
9280 | return; | |
e90b1c4a JM |
9281 | /* Don't do this again for clones. */ |
9282 | if (DECL_CLONED_FUNCTION_P (fn)) | |
9283 | return; | |
9188c363 | 9284 | |
c8094d83 MS |
9285 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
9286 | arg; | |
9188c363 MM |
9287 | arg = TREE_CHAIN (arg)) |
9288 | if (TREE_PURPOSE (arg)) | |
c8094d83 | 9289 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, |
9188c363 MM |
9290 | TREE_VALUE (arg), |
9291 | TREE_PURPOSE (arg)); | |
9292 | } | |
9293 | ||
fc6a28d7 MM |
9294 | /* Substitute the ARGS into the T, which is a _DECL. Return the |
9295 | result of the substitution. Issue error and warning messages under | |
9296 | control of COMPLAIN. */ | |
00d3396f | 9297 | |
e9659ab0 | 9298 | static tree |
fc6a28d7 | 9299 | tsubst_decl (tree t, tree args, tsubst_flags_t complain) |
8d08fdba | 9300 | { |
ccb05613 | 9301 | #define RETURN(EXP) do { r = (EXP); goto out; } while(0) |
82a98427 | 9302 | location_t saved_loc; |
b370501f | 9303 | tree r = NULL_TREE; |
4b2811e9 | 9304 | tree in_decl = t; |
2b110bfc | 9305 | hashval_t hash = 0; |
830bfa74 | 9306 | |
ae58fa02 | 9307 | /* Set the filename and linenumber to improve error-reporting. */ |
82a98427 | 9308 | saved_loc = input_location; |
f31686a3 | 9309 | input_location = DECL_SOURCE_LOCATION (t); |
b7484fbe | 9310 | |
8d08fdba MS |
9311 | switch (TREE_CODE (t)) |
9312 | { | |
98c1c668 JM |
9313 | case TEMPLATE_DECL: |
9314 | { | |
28e42b7e | 9315 | /* We can get here when processing a member function template, |
438c0fa8 | 9316 | member class template, or template template parameter. */ |
98c1c668 | 9317 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 9318 | tree spec; |
28e42b7e KL |
9319 | tree tmpl_args; |
9320 | tree full_args; | |
98c1c668 | 9321 | |
28e42b7e | 9322 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
db2767b6 | 9323 | { |
28e42b7e KL |
9324 | /* Template template parameter is treated here. */ |
9325 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
9326 | if (new_type == error_mark_node) | |
ccb05613 | 9327 | RETURN (error_mark_node); |
36a117a5 | 9328 | |
28e42b7e | 9329 | r = copy_decl (t); |
910ad8de | 9330 | DECL_CHAIN (r) = NULL_TREE; |
28e42b7e KL |
9331 | TREE_TYPE (r) = new_type; |
9332 | DECL_TEMPLATE_RESULT (r) | |
c2255bc4 AH |
9333 | = build_decl (DECL_SOURCE_LOCATION (decl), |
9334 | TYPE_DECL, DECL_NAME (decl), new_type); | |
c8094d83 | 9335 | DECL_TEMPLATE_PARMS (r) |
28e42b7e KL |
9336 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
9337 | complain); | |
9338 | TYPE_NAME (new_type) = r; | |
9339 | break; | |
9340 | } | |
36a117a5 | 9341 | |
28e42b7e KL |
9342 | /* We might already have an instance of this template. |
9343 | The ARGS are for the surrounding class type, so the | |
9344 | full args contain the tsubst'd args for the context, | |
9345 | plus the innermost args from the template decl. */ | |
c8094d83 | 9346 | tmpl_args = DECL_CLASS_TEMPLATE_P (t) |
28e42b7e KL |
9347 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) |
9348 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); | |
8ca4bf25 MM |
9349 | /* Because this is a template, the arguments will still be |
9350 | dependent, even after substitution. If | |
9351 | PROCESSING_TEMPLATE_DECL is not set, the dependency | |
9352 | predicates will short-circuit. */ | |
9353 | ++processing_template_decl; | |
28e42b7e KL |
9354 | full_args = tsubst_template_args (tmpl_args, args, |
9355 | complain, in_decl); | |
8ca4bf25 | 9356 | --processing_template_decl; |
bf2f7328 | 9357 | if (full_args == error_mark_node) |
ccb05613 | 9358 | RETURN (error_mark_node); |
28e42b7e | 9359 | |
438c0fa8 JM |
9360 | /* If this is a default template template argument, |
9361 | tsubst might not have changed anything. */ | |
9362 | if (full_args == tmpl_args) | |
ccb05613 | 9363 | RETURN (t); |
28e42b7e | 9364 | |
2b110bfc JM |
9365 | hash = hash_tmpl_and_args (t, full_args); |
9366 | spec = retrieve_specialization (t, full_args, hash); | |
28e42b7e KL |
9367 | if (spec != NULL_TREE) |
9368 | { | |
9369 | r = spec; | |
9370 | break; | |
db2767b6 | 9371 | } |
98c1c668 JM |
9372 | |
9373 | /* Make a new template decl. It will be similar to the | |
c8094d83 | 9374 | original, but will record the current template arguments. |
98c1c668 JM |
9375 | We also create a new function declaration, which is just |
9376 | like the old one, but points to this new template, rather | |
9377 | than the old one. */ | |
0acf7199 | 9378 | r = copy_decl (t); |
50bc768d | 9379 | gcc_assert (DECL_LANG_SPECIFIC (r) != 0); |
910ad8de | 9380 | DECL_CHAIN (r) = NULL_TREE; |
db2767b6 | 9381 | |
aa373032 | 9382 | DECL_TEMPLATE_INFO (r) = build_template_info (t, args); |
93cdc044 JM |
9383 | |
9384 | if (TREE_CODE (decl) == TYPE_DECL) | |
9385 | { | |
8ca4bf25 MM |
9386 | tree new_type; |
9387 | ++processing_template_decl; | |
9388 | new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
3db45ab5 | 9389 | --processing_template_decl; |
2620d095 | 9390 | if (new_type == error_mark_node) |
ccb05613 | 9391 | RETURN (error_mark_node); |
2620d095 | 9392 | |
ae58fa02 MM |
9393 | TREE_TYPE (r) = new_type; |
9394 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
17aec3eb | 9395 | DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); |
ae58fa02 | 9396 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); |
8ca4bf25 | 9397 | DECL_CONTEXT (r) = TYPE_CONTEXT (new_type); |
93cdc044 JM |
9398 | } |
9399 | else | |
9400 | { | |
8ca4bf25 MM |
9401 | tree new_decl; |
9402 | ++processing_template_decl; | |
9403 | new_decl = tsubst (decl, args, complain, in_decl); | |
9404 | --processing_template_decl; | |
caec1dc0 | 9405 | if (new_decl == error_mark_node) |
ccb05613 | 9406 | RETURN (error_mark_node); |
17aec3eb RK |
9407 | |
9408 | DECL_TEMPLATE_RESULT (r) = new_decl; | |
ae58fa02 MM |
9409 | DECL_TI_TEMPLATE (new_decl) = r; |
9410 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
9411 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
3db45ab5 | 9412 | DECL_CONTEXT (r) = DECL_CONTEXT (new_decl); |
93cdc044 JM |
9413 | } |
9414 | ||
ae58fa02 MM |
9415 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
9416 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
9417 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
9418 | |
9419 | /* The template parameters for this new template are all the | |
9420 | template parameters for the old template, except the | |
c6002625 | 9421 | outermost level of parameters. */ |
c8094d83 | 9422 | DECL_TEMPLATE_PARMS (r) |
4393e105 | 9423 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
c2ea3a40 | 9424 | complain); |
98c1c668 | 9425 | |
93cdc044 | 9426 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 9427 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 9428 | |
8c6ab2db NS |
9429 | if (TREE_CODE (decl) != TYPE_DECL) |
9430 | /* Record this non-type partial instantiation. */ | |
c8094d83 | 9431 | register_specialization (r, t, |
d63d5d0c | 9432 | DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)), |
2b110bfc | 9433 | false, hash); |
98c1c668 | 9434 | } |
ae58fa02 | 9435 | break; |
8d08fdba MS |
9436 | |
9437 | case FUNCTION_DECL: | |
9438 | { | |
386b8a85 | 9439 | tree ctx; |
87603ed0 | 9440 | tree argvec = NULL_TREE; |
cf38f48a | 9441 | tree *friends; |
36a117a5 | 9442 | tree gen_tmpl; |
fc6a28d7 | 9443 | tree type; |
5566b478 | 9444 | int member; |
d8c4447d MM |
9445 | int args_depth; |
9446 | int parms_depth; | |
5566b478 | 9447 | |
36a117a5 | 9448 | /* Nobody should be tsubst'ing into non-template functions. */ |
50bc768d | 9449 | gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE); |
36a117a5 MM |
9450 | |
9451 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
9452 | { | |
9453 | tree spec; | |
00cf3e31 MM |
9454 | bool dependent_p; |
9455 | ||
9456 | /* If T is not dependent, just return it. We have to | |
9457 | increment PROCESSING_TEMPLATE_DECL because | |
9458 | value_dependent_expression_p assumes that nothing is | |
9459 | dependent when PROCESSING_TEMPLATE_DECL is zero. */ | |
9460 | ++processing_template_decl; | |
9461 | dependent_p = value_dependent_expression_p (t); | |
9462 | --processing_template_decl; | |
9463 | if (!dependent_p) | |
ccb05613 | 9464 | RETURN (t); |
36a117a5 MM |
9465 | |
9466 | /* Calculate the most general template of which R is a | |
9467 | specialization, and the complete set of arguments used to | |
9468 | specialize R. */ | |
9469 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
c8094d83 | 9470 | argvec = tsubst_template_args (DECL_TI_ARGS |
5fd5c97a DS |
9471 | (DECL_TEMPLATE_RESULT |
9472 | (DECL_TI_TEMPLATE (t))), | |
c8094d83 | 9473 | args, complain, in_decl); |
36a117a5 MM |
9474 | |
9475 | /* Check to see if we already have this specialization. */ | |
2b110bfc JM |
9476 | hash = hash_tmpl_and_args (gen_tmpl, argvec); |
9477 | spec = retrieve_specialization (gen_tmpl, argvec, hash); | |
7ddedda4 | 9478 | |
36a117a5 | 9479 | if (spec) |
ae58fa02 MM |
9480 | { |
9481 | r = spec; | |
9482 | break; | |
9483 | } | |
d8c4447d | 9484 | |
f9a7ae04 MM |
9485 | /* We can see more levels of arguments than parameters if |
9486 | there was a specialization of a member template, like | |
9487 | this: | |
9488 | ||
0cbd7506 | 9489 | template <class T> struct S { template <class U> void f(); } |
c8094d83 | 9490 | template <> template <class U> void S<int>::f(U); |
f9a7ae04 | 9491 | |
dc957d14 | 9492 | Here, we'll be substituting into the specialization, |
f9a7ae04 MM |
9493 | because that's where we can find the code we actually |
9494 | want to generate, but we'll have enough arguments for | |
c8094d83 | 9495 | the most general template. |
f9a7ae04 MM |
9496 | |
9497 | We also deal with the peculiar case: | |
d8c4447d | 9498 | |
c8094d83 | 9499 | template <class T> struct S { |
d8c4447d MM |
9500 | template <class U> friend void f(); |
9501 | }; | |
74b846e0 | 9502 | template <class U> void f() {} |
d8c4447d MM |
9503 | template S<int>; |
9504 | template void f<double>(); | |
9505 | ||
9506 | Here, the ARGS for the instantiation of will be {int, | |
9507 | double}. But, we only need as many ARGS as there are | |
9508 | levels of template parameters in CODE_PATTERN. We are | |
9509 | careful not to get fooled into reducing the ARGS in | |
9510 | situations like: | |
9511 | ||
9512 | template <class T> struct S { template <class U> void f(U); } | |
9513 | template <class T> template <> void S<T>::f(int) {} | |
9514 | ||
9515 | which we can spot because the pattern will be a | |
9516 | specialization in this case. */ | |
9517 | args_depth = TMPL_ARGS_DEPTH (args); | |
c8094d83 MS |
9518 | parms_depth = |
9519 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
d8c4447d MM |
9520 | if (args_depth > parms_depth |
9521 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
f9a7ae04 | 9522 | args = get_innermost_template_args (args, parms_depth); |
36a117a5 MM |
9523 | } |
9524 | else | |
9525 | { | |
9526 | /* This special case arises when we have something like this: | |
9527 | ||
0cbd7506 | 9528 | template <class T> struct S { |
c8094d83 | 9529 | friend void f<int>(int, double); |
36a117a5 MM |
9530 | }; |
9531 | ||
9532 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
10b1d5e7 MM |
9533 | will be an IDENTIFIER_NODE. We are being called from |
9534 | tsubst_friend_function, and we want only to create a | |
9535 | new decl (R) with appropriate types so that we can call | |
9536 | determine_specialization. */ | |
36a117a5 MM |
9537 | gen_tmpl = NULL_TREE; |
9538 | } | |
9539 | ||
6eb3bb27 | 9540 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 9541 | { |
5566b478 MS |
9542 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
9543 | member = 2; | |
9544 | else | |
9545 | member = 1; | |
c8094d83 | 9546 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
8c6ab2db | 9547 | complain, t, /*entering_scope=*/1); |
5566b478 MS |
9548 | } |
9549 | else | |
9550 | { | |
9551 | member = 0; | |
4f1c5b7d | 9552 | ctx = DECL_CONTEXT (t); |
5566b478 | 9553 | } |
fc6a28d7 | 9554 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
b3da7bb1 | 9555 | if (type == error_mark_node) |
ccb05613 | 9556 | RETURN (error_mark_node); |
8d08fdba | 9557 | |
5566b478 | 9558 | /* We do NOT check for matching decls pushed separately at this |
0cbd7506 MS |
9559 | point, as they may not represent instantiations of this |
9560 | template, and in any case are considered separate under the | |
9561 | discrete model. */ | |
0acf7199 | 9562 | r = copy_decl (t); |
e1467ff2 | 9563 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 | 9564 | TREE_TYPE (r) = type; |
92643fea MM |
9565 | /* Clear out the mangled name and RTL for the instantiation. */ |
9566 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
245763e3 | 9567 | SET_DECL_RTL (r, NULL); |
9b26c96e JM |
9568 | /* Leave DECL_INITIAL set on deleted instantiations. */ |
9569 | if (!DECL_DELETED_FN (r)) | |
9570 | DECL_INITIAL (r) = NULL_TREE; | |
4f1c5b7d | 9571 | DECL_CONTEXT (r) = ctx; |
5566b478 | 9572 | |
c8094d83 | 9573 | if (member && DECL_CONV_FN_P (r)) |
1f84ec23 MM |
9574 | /* Type-conversion operator. Reconstruct the name, in |
9575 | case it's the name of one of the template's parameters. */ | |
9576 | DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); | |
5566b478 | 9577 | |
4393e105 | 9578 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
c2ea3a40 | 9579 | complain, t); |
477f6664 | 9580 | DECL_RESULT (r) = NULL_TREE; |
711734a9 JM |
9581 | |
9582 | TREE_STATIC (r) = 0; | |
9583 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
9584 | DECL_EXTERNAL (r) = 1; | |
4684cd27 MM |
9585 | /* If this is an instantiation of a function with internal |
9586 | linkage, we already know what object file linkage will be | |
9587 | assigned to the instantiation. */ | |
9588 | DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r); | |
711734a9 | 9589 | DECL_DEFER_OUTPUT (r) = 0; |
910ad8de | 9590 | DECL_CHAIN (r) = NULL_TREE; |
711734a9 | 9591 | DECL_PENDING_INLINE_INFO (r) = 0; |
59026e79 | 9592 | DECL_PENDING_INLINE_P (r) = 0; |
655dc6ee | 9593 | DECL_SAVED_TREE (r) = NULL_TREE; |
29d7f409 | 9594 | DECL_STRUCT_FUNCTION (r) = NULL; |
711734a9 | 9595 | TREE_USED (r) = 0; |
b97e8a14 JM |
9596 | /* We'll re-clone as appropriate in instantiate_template. */ |
9597 | DECL_CLONED_FUNCTION (r) = NULL_TREE; | |
711734a9 | 9598 | |
ccb05613 JM |
9599 | /* If we aren't complaining now, return on error before we register |
9600 | the specialization so that we'll complain eventually. */ | |
9601 | if ((complain & tf_error) == 0 | |
9602 | && IDENTIFIER_OPNAME_P (DECL_NAME (r)) | |
9603 | && !grok_op_properties (r, /*complain=*/false)) | |
9604 | RETURN (error_mark_node); | |
9605 | ||
92643fea MM |
9606 | /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do |
9607 | this in the special friend case mentioned above where | |
9608 | GEN_TMPL is NULL. */ | |
36a117a5 | 9609 | if (gen_tmpl) |
386b8a85 | 9610 | { |
c8094d83 | 9611 | DECL_TEMPLATE_INFO (r) |
aa373032 | 9612 | = build_template_info (gen_tmpl, argvec); |
36a117a5 | 9613 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
2b110bfc | 9614 | register_specialization (r, gen_tmpl, argvec, false, hash); |
36a117a5 | 9615 | |
9188c363 MM |
9616 | /* We're not supposed to instantiate default arguments |
9617 | until they are called, for a template. But, for a | |
9618 | declaration like: | |
9619 | ||
0cbd7506 MS |
9620 | template <class T> void f () |
9621 | { extern void g(int i = T()); } | |
c8094d83 | 9622 | |
9188c363 MM |
9623 | we should do the substitution when the template is |
9624 | instantiated. We handle the member function case in | |
9625 | instantiate_class_template since the default arguments | |
9626 | might refer to other members of the class. */ | |
9627 | if (!member | |
9628 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
9629 | && !uses_template_parms (argvec)) | |
9630 | tsubst_default_arguments (r); | |
386b8a85 | 9631 | } |
c43e95f8 MM |
9632 | else |
9633 | DECL_TEMPLATE_INFO (r) = NULL_TREE; | |
f181d4ae | 9634 | |
cf38f48a MM |
9635 | /* Copy the list of befriending classes. */ |
9636 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
9637 | *friends; | |
c8094d83 | 9638 | friends = &TREE_CHAIN (*friends)) |
cf38f48a MM |
9639 | { |
9640 | *friends = copy_node (*friends); | |
9641 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
c2ea3a40 | 9642 | args, complain, |
cf38f48a MM |
9643 | in_decl); |
9644 | } | |
9645 | ||
212e7048 | 9646 | if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) |
f181d4ae MM |
9647 | { |
9648 | maybe_retrofit_in_chrg (r); | |
212e7048 MM |
9649 | if (DECL_CONSTRUCTOR_P (r)) |
9650 | grok_ctor_properties (ctx, r); | |
2be678ff JM |
9651 | /* If this is an instantiation of a member template, clone it. |
9652 | If it isn't, that'll be handled by | |
9653 | clone_constructors_and_destructors. */ | |
5e818b93 | 9654 | if (PRIMARY_TEMPLATE_P (gen_tmpl)) |
271e6f02 | 9655 | clone_function_decl (r, /*update_method_vec_p=*/0); |
f181d4ae | 9656 | } |
ccb05613 JM |
9657 | else if ((complain & tf_error) != 0 |
9658 | && IDENTIFIER_OPNAME_P (DECL_NAME (r)) | |
9659 | && !grok_op_properties (r, /*complain=*/true)) | |
9660 | RETURN (error_mark_node); | |
c8b2e872 MM |
9661 | |
9662 | if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t)) | |
9663 | SET_DECL_FRIEND_CONTEXT (r, | |
9664 | tsubst (DECL_FRIEND_CONTEXT (t), | |
9665 | args, complain, in_decl)); | |
b9e75696 JM |
9666 | |
9667 | /* Possibly limit visibility based on template args. */ | |
9668 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
9669 | if (DECL_VISIBILITY_SPECIFIED (t)) |
9670 | { | |
9671 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
9672 | DECL_ATTRIBUTES (r) | |
9673 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
9674 | } | |
b9e75696 | 9675 | determine_visibility (r); |
20f2653e JM |
9676 | if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r) |
9677 | && !processing_template_decl) | |
9678 | defaulted_late_check (r); | |
4f4141ff JM |
9679 | |
9680 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, | |
9681 | args, complain, in_decl); | |
8d08fdba | 9682 | } |
ae58fa02 | 9683 | break; |
8d08fdba MS |
9684 | |
9685 | case PARM_DECL: | |
9686 | { | |
5d80a306 DG |
9687 | tree type = NULL_TREE; |
9688 | int i, len = 1; | |
9689 | tree expanded_types = NULL_TREE; | |
9690 | tree prev_r = NULL_TREE; | |
9691 | tree first_r = NULL_TREE; | |
1b8899d1 | 9692 | |
5d80a306 DG |
9693 | if (FUNCTION_PARAMETER_PACK_P (t)) |
9694 | { | |
9695 | /* If there is a local specialization that isn't a | |
9696 | parameter pack, it means that we're doing a "simple" | |
9697 | substitution from inside tsubst_pack_expansion. Just | |
a757b5c5 | 9698 | return the local specialization (which will be a single |
5d80a306 | 9699 | parm). */ |
29b0d1fd | 9700 | tree spec = retrieve_local_specialization (t); |
5d80a306 DG |
9701 | if (spec |
9702 | && TREE_CODE (spec) == PARM_DECL | |
9703 | && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION) | |
ccb05613 | 9704 | RETURN (spec); |
5d80a306 DG |
9705 | |
9706 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
9707 | the parameters in this function parameter pack. */ | |
9708 | expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args, | |
9709 | complain, in_decl); | |
9710 | if (TREE_CODE (expanded_types) == TREE_VEC) | |
9711 | { | |
9712 | len = TREE_VEC_LENGTH (expanded_types); | |
9713 | ||
9714 | /* Zero-length parameter packs are boring. Just substitute | |
9715 | into the chain. */ | |
9716 | if (len == 0) | |
ccb05613 JM |
9717 | RETURN (tsubst (TREE_CHAIN (t), args, complain, |
9718 | TREE_CHAIN (t))); | |
5d80a306 DG |
9719 | } |
9720 | else | |
9721 | { | |
9722 | /* All we did was update the type. Make a note of that. */ | |
9723 | type = expanded_types; | |
9724 | expanded_types = NULL_TREE; | |
9725 | } | |
9726 | } | |
db2767b6 | 9727 | |
5d80a306 DG |
9728 | /* Loop through all of the parameter's we'll build. When T is |
9729 | a function parameter pack, LEN is the number of expanded | |
9730 | types in EXPANDED_TYPES; otherwise, LEN is 1. */ | |
9731 | r = NULL_TREE; | |
9732 | for (i = 0; i < len; ++i) | |
9733 | { | |
9734 | prev_r = r; | |
9735 | r = copy_node (t); | |
9736 | if (DECL_TEMPLATE_PARM_P (t)) | |
9737 | SET_DECL_TEMPLATE_PARM_P (r); | |
9738 | ||
d40a19da DS |
9739 | /* An argument of a function parameter pack is not a parameter |
9740 | pack. */ | |
9741 | FUNCTION_PARAMETER_PACK_P (r) = false; | |
9742 | ||
5d80a306 DG |
9743 | if (expanded_types) |
9744 | /* We're on the Ith parameter of the function parameter | |
9745 | pack. */ | |
9746 | { | |
9747 | /* Get the Ith type. */ | |
9748 | type = TREE_VEC_ELT (expanded_types, i); | |
9749 | ||
9750 | if (DECL_NAME (r)) | |
9751 | /* Rename the parameter to include the index. */ | |
9752 | DECL_NAME (r) = | |
9753 | make_ith_pack_parameter_name (DECL_NAME (r), i); | |
9754 | } | |
9755 | else if (!type) | |
9756 | /* We're dealing with a normal parameter. */ | |
9757 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
9758 | ||
9759 | type = type_decays_to (type); | |
9760 | TREE_TYPE (r) = type; | |
9761 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); | |
9762 | ||
9763 | if (DECL_INITIAL (r)) | |
9764 | { | |
9765 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) | |
9766 | DECL_INITIAL (r) = TREE_TYPE (r); | |
9767 | else | |
9768 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, | |
9769 | complain, in_decl); | |
9770 | } | |
9771 | ||
9772 | DECL_CONTEXT (r) = NULL_TREE; | |
9773 | ||
9774 | if (!DECL_TEMPLATE_PARM_P (r)) | |
9775 | DECL_ARG_TYPE (r) = type_passed_as (type); | |
9776 | ||
4f4141ff JM |
9777 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, |
9778 | args, complain, in_decl); | |
9779 | ||
5d80a306 DG |
9780 | /* Keep track of the first new parameter we |
9781 | generate. That's what will be returned to the | |
9782 | caller. */ | |
9783 | if (!first_r) | |
9784 | first_r = r; | |
9785 | ||
9786 | /* Build a proper chain of parameters when substituting | |
9787 | into a function parameter pack. */ | |
9788 | if (prev_r) | |
910ad8de | 9789 | DECL_CHAIN (prev_r) = r; |
5d80a306 | 9790 | } |
8e51619a | 9791 | |
910ad8de NF |
9792 | if (DECL_CHAIN (t)) |
9793 | DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args, | |
9794 | complain, DECL_CHAIN (t)); | |
5d80a306 DG |
9795 | |
9796 | /* FIRST_R contains the start of the chain we've built. */ | |
9797 | r = first_r; | |
8d08fdba | 9798 | } |
ae58fa02 | 9799 | break; |
8d08fdba | 9800 | |
5566b478 MS |
9801 | case FIELD_DECL: |
9802 | { | |
fc6a28d7 MM |
9803 | tree type; |
9804 | ||
0acf7199 | 9805 | r = copy_decl (t); |
fc6a28d7 MM |
9806 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
9807 | if (type == error_mark_node) | |
ccb05613 | 9808 | RETURN (error_mark_node); |
1b8899d1 | 9809 | TREE_TYPE (r) = type; |
9804209d | 9810 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
1b8899d1 | 9811 | |
015c2c66 MM |
9812 | /* DECL_INITIAL gives the number of bits in a bit-field. */ |
9813 | DECL_INITIAL (r) | |
9814 | = tsubst_expr (DECL_INITIAL (t), args, | |
9815 | complain, in_decl, | |
9816 | /*integral_constant_expression_p=*/true); | |
1b8899d1 MM |
9817 | /* We don't have to set DECL_CONTEXT here; it is set by |
9818 | finish_member_declaration. */ | |
910ad8de | 9819 | DECL_CHAIN (r) = NULL_TREE; |
c8094d83 | 9820 | if (VOID_TYPE_P (type)) |
dee15844 | 9821 | error ("instantiation of %q+D as type %qT", r, type); |
4f4141ff JM |
9822 | |
9823 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, | |
9824 | args, complain, in_decl); | |
5566b478 | 9825 | } |
ae58fa02 | 9826 | break; |
5566b478 MS |
9827 | |
9828 | case USING_DECL: | |
98ed9dae NS |
9829 | /* We reach here only for member using decls. */ |
9830 | if (DECL_DEPENDENT_P (t)) | |
9831 | { | |
9832 | r = do_class_using_decl | |
9833 | (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl), | |
9834 | tsubst_copy (DECL_NAME (t), args, complain, in_decl)); | |
9835 | if (!r) | |
9836 | r = error_mark_node; | |
db8470f6 JM |
9837 | else |
9838 | { | |
9839 | TREE_PROTECTED (r) = TREE_PROTECTED (t); | |
9840 | TREE_PRIVATE (r) = TREE_PRIVATE (t); | |
9841 | } | |
98ed9dae NS |
9842 | } |
9843 | else | |
9844 | { | |
9845 | r = copy_node (t); | |
910ad8de | 9846 | DECL_CHAIN (r) = NULL_TREE; |
98ed9dae | 9847 | } |
ae58fa02 | 9848 | break; |
5566b478 | 9849 | |
9188c363 | 9850 | case TYPE_DECL: |
5566b478 MS |
9851 | case VAR_DECL: |
9852 | { | |
1cea0434 KG |
9853 | tree argvec = NULL_TREE; |
9854 | tree gen_tmpl = NULL_TREE; | |
36a117a5 | 9855 | tree spec; |
1cea0434 | 9856 | tree tmpl = NULL_TREE; |
9188c363 | 9857 | tree ctx; |
fc6a28d7 | 9858 | tree type = NULL_TREE; |
f604fc3b | 9859 | bool local_p; |
9188c363 | 9860 | |
56d0c6e3 | 9861 | if (TREE_CODE (t) == TYPE_DECL |
a7f6bc8c | 9862 | && t == TYPE_MAIN_DECL (TREE_TYPE (t))) |
fc6a28d7 | 9863 | { |
a7f6bc8c JM |
9864 | /* If this is the canonical decl, we don't have to |
9865 | mess with instantiations, and often we can't (for | |
9866 | typename, template type parms and such). Note that | |
9867 | TYPE_NAME is not correct for the above test if | |
9868 | we've copied the type for a typedef. */ | |
fc6a28d7 | 9869 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
c343d5a7 | 9870 | if (type == error_mark_node) |
ccb05613 | 9871 | RETURN (error_mark_node); |
a7f6bc8c JM |
9872 | r = TYPE_NAME (type); |
9873 | break; | |
fc6a28d7 | 9874 | } |
c8094d83 | 9875 | |
f604fc3b MM |
9876 | /* Check to see if we already have the specialization we |
9877 | need. */ | |
9878 | spec = NULL_TREE; | |
9879 | if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)) | |
9880 | { | |
9881 | /* T is a static data member or namespace-scope entity. | |
9882 | We have to substitute into namespace-scope variables | |
9883 | (even though such entities are never templates) because | |
9884 | of cases like: | |
9885 | ||
9886 | template <class T> void f() { extern T t; } | |
9887 | ||
9888 | where the entity referenced is not known until | |
9889 | instantiation time. */ | |
9890 | local_p = false; | |
9891 | ctx = DECL_CONTEXT (t); | |
9892 | if (DECL_CLASS_SCOPE_P (t)) | |
9893 | { | |
9894 | ctx = tsubst_aggr_type (ctx, args, | |
9895 | complain, | |
9896 | in_decl, /*entering_scope=*/1); | |
9897 | /* If CTX is unchanged, then T is in fact the | |
9898 | specialization we want. That situation occurs when | |
9899 | referencing a static data member within in its own | |
9900 | class. We can use pointer equality, rather than | |
9901 | same_type_p, because DECL_CONTEXT is always | |
9902 | canonical. */ | |
9903 | if (ctx == DECL_CONTEXT (t)) | |
9904 | spec = t; | |
9905 | } | |
5566b478 | 9906 | |
f604fc3b MM |
9907 | if (!spec) |
9908 | { | |
9909 | tmpl = DECL_TI_TEMPLATE (t); | |
9910 | gen_tmpl = most_general_template (tmpl); | |
9911 | argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); | |
2b110bfc JM |
9912 | hash = hash_tmpl_and_args (gen_tmpl, argvec); |
9913 | spec = retrieve_specialization (gen_tmpl, argvec, hash); | |
f604fc3b MM |
9914 | } |
9915 | } | |
9188c363 | 9916 | else |
6dfbb909 | 9917 | { |
f604fc3b MM |
9918 | /* A local variable. */ |
9919 | local_p = true; | |
6dfbb909 MM |
9920 | /* Subsequent calls to pushdecl will fill this in. */ |
9921 | ctx = NULL_TREE; | |
f604fc3b | 9922 | spec = retrieve_local_specialization (t); |
6dfbb909 | 9923 | } |
f604fc3b MM |
9924 | /* If we already have the specialization we need, there is |
9925 | nothing more to do. */ | |
36a117a5 | 9926 | if (spec) |
ae58fa02 MM |
9927 | { |
9928 | r = spec; | |
9929 | break; | |
9930 | } | |
5566b478 | 9931 | |
f604fc3b | 9932 | /* Create a new node for the specialization we need. */ |
0acf7199 | 9933 | r = copy_decl (t); |
56d0c6e3 | 9934 | if (type == NULL_TREE) |
0857d1f0 JM |
9935 | { |
9936 | if (is_typedef_decl (t)) | |
9937 | type = DECL_ORIGINAL_TYPE (t); | |
9938 | else | |
9939 | type = TREE_TYPE (t); | |
48326487 JM |
9940 | if (TREE_CODE (t) == VAR_DECL && VAR_HAD_UNKNOWN_BOUND (t)) |
9941 | type = strip_array_domain (type); | |
0857d1f0 JM |
9942 | type = tsubst (type, args, complain, in_decl); |
9943 | } | |
edebf865 | 9944 | if (TREE_CODE (r) == VAR_DECL) |
39703eb9 | 9945 | { |
8b0a8500 MM |
9946 | /* Even if the original location is out of scope, the |
9947 | newly substituted one is not. */ | |
9948 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
9949 | DECL_INITIALIZED_P (r) = 0; | |
9950 | DECL_TEMPLATE_INSTANTIATED (r) = 0; | |
fc6a28d7 | 9951 | if (type == error_mark_node) |
ccb05613 | 9952 | RETURN (error_mark_node); |
2c05d05e MM |
9953 | if (TREE_CODE (type) == FUNCTION_TYPE) |
9954 | { | |
9955 | /* It may seem that this case cannot occur, since: | |
9956 | ||
9957 | typedef void f(); | |
9958 | void g() { f x; } | |
9959 | ||
9960 | declares a function, not a variable. However: | |
9961 | ||
9962 | typedef void f(); | |
9963 | template <typename T> void g() { T t; } | |
9964 | template void g<f>(); | |
9965 | ||
9966 | is an attempt to declare a variable with function | |
9967 | type. */ | |
9968 | error ("variable %qD has function type", | |
9969 | /* R is not yet sufficiently initialized, so we | |
9970 | just use its name. */ | |
9971 | DECL_NAME (r)); | |
ccb05613 | 9972 | RETURN (error_mark_node); |
2c05d05e | 9973 | } |
39703eb9 | 9974 | type = complete_type (type); |
fa2200cb JM |
9975 | /* Wait until cp_finish_decl to set this again, to handle |
9976 | circular dependency (template/instantiate6.C). */ | |
9977 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0; | |
62e19030 | 9978 | type = check_var_type (DECL_NAME (r), type); |
56b4ea3d RH |
9979 | |
9980 | if (DECL_HAS_VALUE_EXPR_P (t)) | |
9981 | { | |
9982 | tree ve = DECL_VALUE_EXPR (t); | |
015c2c66 MM |
9983 | ve = tsubst_expr (ve, args, complain, in_decl, |
9984 | /*constant_expression_p=*/false); | |
3db45ab5 | 9985 | SET_DECL_VALUE_EXPR (r, ve); |
56b4ea3d | 9986 | } |
39703eb9 | 9987 | } |
a3d87771 MM |
9988 | else if (DECL_SELF_REFERENCE_P (t)) |
9989 | SET_DECL_SELF_REFERENCE_P (r); | |
01f4137f | 9990 | TREE_TYPE (r) = type; |
9804209d | 9991 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
5566b478 | 9992 | DECL_CONTEXT (r) = ctx; |
92643fea MM |
9993 | /* Clear out the mangled name and RTL for the instantiation. */ |
9994 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
820cc88f | 9995 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
245763e3 | 9996 | SET_DECL_RTL (r, NULL); |
8b0a8500 MM |
9997 | /* The initializer must not be expanded until it is required; |
9998 | see [temp.inst]. */ | |
d11ad92e | 9999 | DECL_INITIAL (r) = NULL_TREE; |
820cc88f | 10000 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
245763e3 | 10001 | SET_DECL_RTL (r, NULL); |
06ceef4e | 10002 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; |
b9e75696 JM |
10003 | if (TREE_CODE (r) == VAR_DECL) |
10004 | { | |
10005 | /* Possibly limit visibility based on template args. */ | |
10006 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
10007 | if (DECL_VISIBILITY_SPECIFIED (t)) |
10008 | { | |
10009 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
10010 | DECL_ATTRIBUTES (r) | |
10011 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
10012 | } | |
b9e75696 JM |
10013 | determine_visibility (r); |
10014 | } | |
5566b478 | 10015 | |
6dfbb909 MM |
10016 | if (!local_p) |
10017 | { | |
10018 | /* A static data member declaration is always marked | |
10019 | external when it is declared in-class, even if an | |
10020 | initializer is present. We mimic the non-template | |
10021 | processing here. */ | |
10022 | DECL_EXTERNAL (r) = 1; | |
fa8d6e85 | 10023 | |
2b110bfc | 10024 | register_specialization (r, gen_tmpl, argvec, false, hash); |
aa373032 | 10025 | DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec); |
6dfbb909 MM |
10026 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
10027 | } | |
d5f4eddd JM |
10028 | else if (cp_unevaluated_operand) |
10029 | { | |
10030 | /* We're substituting this var in a decltype outside of its | |
10031 | scope, such as for a lambda return type. Don't add it to | |
10032 | local_specializations, do perform auto deduction. */ | |
10033 | tree auto_node = type_uses_auto (type); | |
10034 | tree init | |
10035 | = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl, | |
10036 | /*constant_expression_p=*/false); | |
10037 | ||
2e5748d2 | 10038 | if (auto_node && init) |
d5f4eddd | 10039 | { |
2e5748d2 JM |
10040 | init = resolve_nondeduced_context (init); |
10041 | if (describable_type (init)) | |
10042 | { | |
10043 | type = do_auto_deduction (type, init, auto_node); | |
10044 | TREE_TYPE (r) = type; | |
10045 | } | |
d5f4eddd JM |
10046 | } |
10047 | } | |
9188c363 | 10048 | else |
6dfbb909 | 10049 | register_local_specialization (r, t); |
5566b478 | 10050 | |
910ad8de | 10051 | DECL_CHAIN (r) = NULL_TREE; |
4f4141ff | 10052 | |
5044ab0e | 10053 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), |
282c399b | 10054 | /*flags=*/0, |
4f4141ff | 10055 | args, complain, in_decl); |
0857d1f0 JM |
10056 | |
10057 | /* Preserve a typedef that names a type. */ | |
10058 | if (is_typedef_decl (r)) | |
10059 | { | |
10060 | DECL_ORIGINAL_TYPE (r) = NULL_TREE; | |
10061 | set_underlying_type (r); | |
10062 | } | |
10063 | ||
edebf865 | 10064 | layout_decl (r, 0); |
5566b478 | 10065 | } |
ae58fa02 | 10066 | break; |
5566b478 | 10067 | |
ae58fa02 | 10068 | default: |
315fb5db | 10069 | gcc_unreachable (); |
c8094d83 | 10070 | } |
ccb05613 | 10071 | #undef RETURN |
ae58fa02 | 10072 | |
ccb05613 | 10073 | out: |
ae58fa02 | 10074 | /* Restore the file and line information. */ |
82a98427 | 10075 | input_location = saved_loc; |
ae58fa02 MM |
10076 | |
10077 | return r; | |
10078 | } | |
10079 | ||
34cd5ae7 | 10080 | /* Substitute into the ARG_TYPES of a function type. */ |
cabc336a | 10081 | |
e9659ab0 | 10082 | static tree |
c8094d83 | 10083 | tsubst_arg_types (tree arg_types, |
0cbd7506 MS |
10084 | tree args, |
10085 | tsubst_flags_t complain, | |
10086 | tree in_decl) | |
cabc336a MM |
10087 | { |
10088 | tree remaining_arg_types; | |
5d80a306 DG |
10089 | tree type = NULL_TREE; |
10090 | int i = 1; | |
10091 | tree expanded_args = NULL_TREE; | |
5e97d404 | 10092 | tree default_arg; |
cabc336a MM |
10093 | |
10094 | if (!arg_types || arg_types == void_list_node) | |
10095 | return arg_types; | |
c8094d83 | 10096 | |
cabc336a | 10097 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), |
4393e105 MM |
10098 | args, complain, in_decl); |
10099 | if (remaining_arg_types == error_mark_node) | |
10100 | return error_mark_node; | |
10101 | ||
5d80a306 | 10102 | if (PACK_EXPANSION_P (TREE_VALUE (arg_types))) |
4b2811e9 | 10103 | { |
5d80a306 DG |
10104 | /* For a pack expansion, perform substitution on the |
10105 | entire expression. Later on, we'll handle the arguments | |
10106 | one-by-one. */ | |
10107 | expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types), | |
10108 | args, complain, in_decl); | |
cabc336a | 10109 | |
5d80a306 DG |
10110 | if (TREE_CODE (expanded_args) == TREE_VEC) |
10111 | /* So that we'll spin through the parameters, one by one. */ | |
10112 | i = TREE_VEC_LENGTH (expanded_args); | |
10113 | else | |
10114 | { | |
10115 | /* We only partially substituted into the parameter | |
10116 | pack. Our type is TYPE_PACK_EXPANSION. */ | |
10117 | type = expanded_args; | |
10118 | expanded_args = NULL_TREE; | |
10119 | } | |
10120 | } | |
cabc336a | 10121 | |
5d80a306 DG |
10122 | while (i > 0) { |
10123 | --i; | |
10124 | ||
10125 | if (expanded_args) | |
10126 | type = TREE_VEC_ELT (expanded_args, i); | |
10127 | else if (!type) | |
10128 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
c8094d83 | 10129 | |
5d80a306 DG |
10130 | if (type == error_mark_node) |
10131 | return error_mark_node; | |
10132 | if (VOID_TYPE_P (type)) | |
10133 | { | |
10134 | if (complain & tf_error) | |
10135 | { | |
10136 | error ("invalid parameter type %qT", type); | |
10137 | if (in_decl) | |
10138 | error ("in declaration %q+D", in_decl); | |
10139 | } | |
10140 | return error_mark_node; | |
5e97d404 | 10141 | } |
5d80a306 DG |
10142 | |
10143 | /* Do array-to-pointer, function-to-pointer conversion, and ignore | |
10144 | top-level qualifiers as required. */ | |
10145 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
c8094d83 | 10146 | |
5d80a306 DG |
10147 | /* We do not substitute into default arguments here. The standard |
10148 | mandates that they be instantiated only when needed, which is | |
10149 | done in build_over_call. */ | |
10150 | default_arg = TREE_PURPOSE (arg_types); | |
10151 | ||
10152 | if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG) | |
10153 | { | |
10154 | /* We've instantiated a template before its default arguments | |
10155 | have been parsed. This can happen for a nested template | |
10156 | class, and is not an error unless we require the default | |
10157 | argument in a call of this function. */ | |
10158 | remaining_arg_types = | |
10159 | tree_cons (default_arg, type, remaining_arg_types); | |
10160 | VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), | |
10161 | remaining_arg_types); | |
10162 | } | |
10163 | else | |
10164 | remaining_arg_types = | |
10165 | hash_tree_cons (default_arg, type, remaining_arg_types); | |
10166 | } | |
10167 | ||
10168 | return remaining_arg_types; | |
cabc336a MM |
10169 | } |
10170 | ||
4393e105 MM |
10171 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
10172 | *not* handle the exception-specification for FNTYPE, because the | |
10173 | initial substitution of explicitly provided template parameters | |
10174 | during argument deduction forbids substitution into the | |
10175 | exception-specification: | |
10176 | ||
10177 | [temp.deduct] | |
10178 | ||
10179 | All references in the function type of the function template to the | |
10180 | corresponding template parameters are replaced by the specified tem- | |
10181 | plate argument values. If a substitution in a template parameter or | |
10182 | in the function type of the function template results in an invalid | |
10183 | type, type deduction fails. [Note: The equivalent substitution in | |
10184 | exception specifications is done only when the function is instanti- | |
10185 | ated, at which point a program is ill-formed if the substitution | |
10186 | results in an invalid type.] */ | |
10187 | ||
10188 | static tree | |
c8094d83 | 10189 | tsubst_function_type (tree t, |
0cbd7506 MS |
10190 | tree args, |
10191 | tsubst_flags_t complain, | |
10192 | tree in_decl) | |
4393e105 MM |
10193 | { |
10194 | tree return_type; | |
10195 | tree arg_types; | |
10196 | tree fntype; | |
10197 | ||
8dd3f57a | 10198 | /* The TYPE_CONTEXT is not used for function/method types. */ |
50bc768d | 10199 | gcc_assert (TYPE_CONTEXT (t) == NULL_TREE); |
4393e105 | 10200 | |
46c895ac | 10201 | /* Substitute the return type. */ |
4393e105 MM |
10202 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
10203 | if (return_type == error_mark_node) | |
10204 | return error_mark_node; | |
6e2993bf MM |
10205 | /* The standard does not presently indicate that creation of a |
10206 | function type with an invalid return type is a deduction failure. | |
270af55d | 10207 | However, that is clearly analogous to creating an array of "void" |
c8094d83 | 10208 | or a reference to a reference. This is core issue #486. */ |
6e2993bf MM |
10209 | if (TREE_CODE (return_type) == ARRAY_TYPE |
10210 | || TREE_CODE (return_type) == FUNCTION_TYPE) | |
10211 | { | |
10212 | if (complain & tf_error) | |
10213 | { | |
10214 | if (TREE_CODE (return_type) == ARRAY_TYPE) | |
10215 | error ("function returning an array"); | |
10216 | else | |
10217 | error ("function returning a function"); | |
10218 | } | |
10219 | return error_mark_node; | |
10220 | } | |
4393e105 | 10221 | |
34cd5ae7 | 10222 | /* Substitute the argument types. */ |
4393e105 | 10223 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, |
c8094d83 | 10224 | complain, in_decl); |
4393e105 MM |
10225 | if (arg_types == error_mark_node) |
10226 | return error_mark_node; | |
c8094d83 | 10227 | |
4393e105 MM |
10228 | /* Construct a new type node and return it. */ |
10229 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
2872152c JM |
10230 | { |
10231 | fntype = build_function_type (return_type, arg_types); | |
10232 | fntype = apply_memfn_quals (fntype, type_memfn_quals (t)); | |
10233 | } | |
4393e105 MM |
10234 | else |
10235 | { | |
10236 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
9e1e64ec | 10237 | if (! MAYBE_CLASS_TYPE_P (r)) |
4393e105 MM |
10238 | { |
10239 | /* [temp.deduct] | |
c8094d83 | 10240 | |
4393e105 MM |
10241 | Type deduction may fail for any of the following |
10242 | reasons: | |
c8094d83 | 10243 | |
4393e105 MM |
10244 | -- Attempting to create "pointer to member of T" when T |
10245 | is not a class type. */ | |
c2ea3a40 | 10246 | if (complain & tf_error) |
0f51ccfc | 10247 | error ("creating pointer to member function of non-class type %qT", |
4393e105 MM |
10248 | r); |
10249 | return error_mark_node; | |
10250 | } | |
c8094d83 MS |
10251 | |
10252 | fntype = build_method_type_directly (r, return_type, | |
43dc123f | 10253 | TREE_CHAIN (arg_types)); |
4393e105 | 10254 | } |
e9525111 | 10255 | fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
c8094d83 MS |
10256 | |
10257 | return fntype; | |
4393e105 MM |
10258 | } |
10259 | ||
c7222c02 MM |
10260 | /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template |
10261 | ARGS into that specification, and return the substituted | |
10262 | specification. If there is no specification, return NULL_TREE. */ | |
10263 | ||
10264 | static tree | |
c8094d83 MS |
10265 | tsubst_exception_specification (tree fntype, |
10266 | tree args, | |
c7222c02 MM |
10267 | tsubst_flags_t complain, |
10268 | tree in_decl) | |
10269 | { | |
10270 | tree specs; | |
10271 | tree new_specs; | |
10272 | ||
10273 | specs = TYPE_RAISES_EXCEPTIONS (fntype); | |
10274 | new_specs = NULL_TREE; | |
3a55fb4c JM |
10275 | if (specs && TREE_PURPOSE (specs)) |
10276 | { | |
10277 | /* A noexcept-specifier. */ | |
10278 | new_specs = tsubst_copy_and_build | |
10279 | (TREE_PURPOSE (specs), args, complain, in_decl, /*function_p=*/false, | |
10280 | /*integral_constant_expression_p=*/true); | |
10281 | new_specs = build_noexcept_spec (new_specs, complain); | |
10282 | } | |
10283 | else if (specs) | |
c7222c02 MM |
10284 | { |
10285 | if (! TREE_VALUE (specs)) | |
10286 | new_specs = specs; | |
10287 | else | |
10288 | while (specs) | |
10289 | { | |
10290 | tree spec; | |
5d80a306 DG |
10291 | int i, len = 1; |
10292 | tree expanded_specs = NULL_TREE; | |
10293 | ||
10294 | if (PACK_EXPANSION_P (TREE_VALUE (specs))) | |
10295 | { | |
10296 | /* Expand the pack expansion type. */ | |
10297 | expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs), | |
10298 | args, complain, | |
10299 | in_decl); | |
2f93f02c DG |
10300 | |
10301 | if (expanded_specs == error_mark_node) | |
10302 | return error_mark_node; | |
10303 | else if (TREE_CODE (expanded_specs) == TREE_VEC) | |
10304 | len = TREE_VEC_LENGTH (expanded_specs); | |
10305 | else | |
10306 | { | |
10307 | /* We're substituting into a member template, so | |
10308 | we got a TYPE_PACK_EXPANSION back. Add that | |
10309 | expansion and move on. */ | |
10310 | gcc_assert (TREE_CODE (expanded_specs) | |
10311 | == TYPE_PACK_EXPANSION); | |
10312 | new_specs = add_exception_specifier (new_specs, | |
10313 | expanded_specs, | |
10314 | complain); | |
10315 | specs = TREE_CHAIN (specs); | |
10316 | continue; | |
10317 | } | |
5d80a306 DG |
10318 | } |
10319 | ||
10320 | for (i = 0; i < len; ++i) | |
10321 | { | |
10322 | if (expanded_specs) | |
10323 | spec = TREE_VEC_ELT (expanded_specs, i); | |
10324 | else | |
10325 | spec = tsubst (TREE_VALUE (specs), args, complain, in_decl); | |
10326 | if (spec == error_mark_node) | |
10327 | return spec; | |
10328 | new_specs = add_exception_specifier (new_specs, spec, | |
10329 | complain); | |
10330 | } | |
10331 | ||
10332 | specs = TREE_CHAIN (specs); | |
c7222c02 MM |
10333 | } |
10334 | } | |
10335 | return new_specs; | |
10336 | } | |
10337 | ||
4393e105 MM |
10338 | /* Take the tree structure T and replace template parameters used |
10339 | therein with the argument vector ARGS. IN_DECL is an associated | |
10340 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
c2ea3a40 NS |
10341 | Issue error and warning messages under control of COMPLAIN. Note |
10342 | that we must be relatively non-tolerant of extensions here, in | |
10343 | order to preserve conformance; if we allow substitutions that | |
10344 | should not be allowed, we may allow argument deductions that should | |
10345 | not succeed, and therefore report ambiguous overload situations | |
10346 | where there are none. In theory, we could allow the substitution, | |
10347 | but indicate that it should have failed, and allow our caller to | |
10348 | make sure that the right thing happens, but we don't try to do this | |
10349 | yet. | |
4393e105 MM |
10350 | |
10351 | This function is used for dealing with types, decls and the like; | |
10352 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 | 10353 | |
d5c8be27 | 10354 | tree |
3a978d72 | 10355 | tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
ae58fa02 | 10356 | { |
394473ab | 10357 | enum tree_code code; |
0ecfe0b4 | 10358 | tree type, r; |
ae58fa02 MM |
10359 | |
10360 | if (t == NULL_TREE || t == error_mark_node | |
10361 | || t == integer_type_node | |
10362 | || t == void_type_node | |
10363 | || t == char_type_node | |
539599c1 | 10364 | || t == unknown_type_node |
725214ac RG |
10365 | || TREE_CODE (t) == NAMESPACE_DECL |
10366 | || TREE_CODE (t) == TRANSLATION_UNIT_DECL) | |
ae58fa02 MM |
10367 | return t; |
10368 | ||
fc6a28d7 MM |
10369 | if (DECL_P (t)) |
10370 | return tsubst_decl (t, args, complain); | |
10371 | ||
5fa0e853 SE |
10372 | if (args == NULL_TREE) |
10373 | return t; | |
10374 | ||
394473ab PC |
10375 | code = TREE_CODE (t); |
10376 | ||
10377 | if (code == IDENTIFIER_NODE) | |
ae58fa02 MM |
10378 | type = IDENTIFIER_TYPE_VALUE (t); |
10379 | else | |
10380 | type = TREE_TYPE (t); | |
399dedb9 | 10381 | |
50bc768d | 10382 | gcc_assert (type != unknown_type_node); |
ae58fa02 | 10383 | |
56d0c6e3 | 10384 | /* Reuse typedefs. We need to do this to handle dependent attributes, |
a7f6bc8c | 10385 | such as attribute aligned. */ |
5044ab0e | 10386 | if (TYPE_P (t) |
e90b1c4a | 10387 | && typedef_variant_p (t)) |
5044ab0e JM |
10388 | { |
10389 | tree decl = TYPE_NAME (t); | |
56d0c6e3 | 10390 | |
a7f6bc8c | 10391 | if (DECL_CLASS_SCOPE_P (decl) |
02e52ae5 JM |
10392 | && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl)) |
10393 | && uses_template_parms (DECL_CONTEXT (decl))) | |
56d0c6e3 JM |
10394 | { |
10395 | tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
10396 | tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl); | |
2b110bfc | 10397 | r = retrieve_specialization (tmpl, gen_args, 0); |
56d0c6e3 | 10398 | } |
29b0d1fd | 10399 | else if (DECL_FUNCTION_SCOPE_P (decl) |
02e52ae5 JM |
10400 | && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl)) |
10401 | && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl)))) | |
56d0c6e3 JM |
10402 | r = retrieve_local_specialization (decl); |
10403 | else | |
29b0d1fd JM |
10404 | /* The typedef is from a non-template context. */ |
10405 | return t; | |
10406 | ||
56d0c6e3 JM |
10407 | if (r) |
10408 | { | |
10409 | r = TREE_TYPE (r); | |
10410 | r = cp_build_qualified_type_real | |
10411 | (r, cp_type_quals (t) | cp_type_quals (r), | |
10412 | complain | tf_ignore_bad_quals); | |
10413 | return r; | |
10414 | } | |
10415 | /* Else we must be instantiating the typedef, so fall through. */ | |
5044ab0e JM |
10416 | } |
10417 | ||
fc6a28d7 | 10418 | if (type |
394473ab PC |
10419 | && code != TYPENAME_TYPE |
10420 | && code != TEMPLATE_TYPE_PARM | |
10421 | && code != IDENTIFIER_NODE | |
10422 | && code != FUNCTION_TYPE | |
10423 | && code != METHOD_TYPE) | |
4393e105 MM |
10424 | type = tsubst (type, args, complain, in_decl); |
10425 | if (type == error_mark_node) | |
10426 | return error_mark_node; | |
ae58fa02 | 10427 | |
394473ab | 10428 | switch (code) |
ae58fa02 MM |
10429 | { |
10430 | case RECORD_TYPE: | |
10431 | case UNION_TYPE: | |
10432 | case ENUMERAL_TYPE: | |
4393e105 MM |
10433 | return tsubst_aggr_type (t, args, complain, in_decl, |
10434 | /*entering_scope=*/0); | |
ae58fa02 MM |
10435 | |
10436 | case ERROR_MARK: | |
10437 | case IDENTIFIER_NODE: | |
ae58fa02 MM |
10438 | case VOID_TYPE: |
10439 | case REAL_TYPE: | |
10440 | case COMPLEX_TYPE: | |
c00996a3 | 10441 | case VECTOR_TYPE: |
ae58fa02 | 10442 | case BOOLEAN_TYPE: |
1e85e720 | 10443 | case NULLPTR_TYPE: |
e90b1c4a | 10444 | case LANG_TYPE: |
ae58fa02 MM |
10445 | return t; |
10446 | ||
10447 | case INTEGER_TYPE: | |
10448 | if (t == integer_type_node) | |
10449 | return t; | |
10450 | ||
10451 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
10452 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
10453 | return t; | |
d2e5ee5c | 10454 | |
5566b478 | 10455 | { |
ddce3528 | 10456 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 10457 | |
6ee3ffe8 MM |
10458 | max = tsubst_expr (omax, args, complain, in_decl, |
10459 | /*integral_constant_expression_p=*/false); | |
5bbca039 JJ |
10460 | |
10461 | /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if | |
10462 | needed. */ | |
10463 | if (TREE_CODE (max) == NOP_EXPR | |
10464 | && TREE_SIDE_EFFECTS (omax) | |
10465 | && !TREE_TYPE (max)) | |
10466 | TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0)); | |
10467 | ||
c1165535 JM |
10468 | /* If we're in a partial instantiation, preserve the magic NOP_EXPR |
10469 | with TREE_SIDE_EFFECTS that indicates this is not an integral | |
10470 | constant expression. */ | |
10471 | if (processing_template_decl | |
10472 | && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR) | |
10473 | { | |
10474 | gcc_assert (TREE_CODE (max) == NOP_EXPR); | |
10475 | TREE_SIDE_EFFECTS (max) = 1; | |
10476 | } | |
10477 | ||
fa2200cb | 10478 | return compute_array_index_type (NULL_TREE, max, complain); |
ae58fa02 MM |
10479 | } |
10480 | ||
10481 | case TEMPLATE_TYPE_PARM: | |
10482 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 10483 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
ae58fa02 MM |
10484 | case TEMPLATE_PARM_INDEX: |
10485 | { | |
10486 | int idx; | |
10487 | int level; | |
10488 | int levels; | |
315fb5db | 10489 | tree arg = NULL_TREE; |
0ecfe0b4 JM |
10490 | |
10491 | r = NULL_TREE; | |
ae58fa02 | 10492 | |
315fb5db | 10493 | gcc_assert (TREE_VEC_LENGTH (args) > 0); |
f4205442 | 10494 | template_parm_level_and_index (t, &level, &idx); |
ae58fa02 | 10495 | |
315fb5db NS |
10496 | levels = TMPL_ARGS_DEPTH (args); |
10497 | if (level <= levels) | |
5d80a306 DG |
10498 | { |
10499 | arg = TMPL_ARG (args, level, idx); | |
10500 | ||
10501 | if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT) | |
10502 | /* See through ARGUMENT_PACK_SELECT arguments. */ | |
10503 | arg = ARGUMENT_PACK_SELECT_ARG (arg); | |
10504 | } | |
ae58fa02 | 10505 | |
315fb5db NS |
10506 | if (arg == error_mark_node) |
10507 | return error_mark_node; | |
10508 | else if (arg != NULL_TREE) | |
10509 | { | |
5d80a306 DG |
10510 | if (ARGUMENT_PACK_P (arg)) |
10511 | /* If ARG is an argument pack, we don't actually want to | |
10512 | perform a substitution here, because substitutions | |
10513 | for argument packs are only done | |
10514 | element-by-element. We can get to this point when | |
10515 | substituting the type of a non-type template | |
10516 | parameter pack, when that type actually contains | |
10517 | template parameter packs from an outer template, e.g., | |
10518 | ||
10519 | template<typename... Types> struct A { | |
10520 | template<Types... Values> struct B { }; | |
10521 | }; */ | |
10522 | return t; | |
10523 | ||
394473ab | 10524 | if (code == TEMPLATE_TYPE_PARM) |
ae58fa02 | 10525 | { |
9180c238 | 10526 | int quals; |
315fb5db | 10527 | gcc_assert (TYPE_P (arg)); |
9180c238 | 10528 | |
2872152c | 10529 | quals = cp_type_quals (arg) | cp_type_quals (t); |
9180c238 | 10530 | |
315fb5db | 10531 | return cp_build_qualified_type_real |
9180c238 | 10532 | (arg, quals, complain | tf_ignore_bad_quals); |
315fb5db | 10533 | } |
394473ab | 10534 | else if (code == BOUND_TEMPLATE_TEMPLATE_PARM) |
315fb5db NS |
10535 | { |
10536 | /* We are processing a type constructed from a | |
10537 | template template parameter. */ | |
10538 | tree argvec = tsubst (TYPE_TI_ARGS (t), | |
10539 | args, complain, in_decl); | |
10540 | if (argvec == error_mark_node) | |
10541 | return error_mark_node; | |
c8094d83 | 10542 | |
315fb5db NS |
10543 | /* We can get a TEMPLATE_TEMPLATE_PARM here when we |
10544 | are resolving nested-types in the signature of a | |
10545 | member function templates. Otherwise ARG is a | |
10546 | TEMPLATE_DECL and is the real template to be | |
10547 | instantiated. */ | |
10548 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
10549 | arg = TYPE_NAME (arg); | |
c8094d83 MS |
10550 | |
10551 | r = lookup_template_class (arg, | |
10552 | argvec, in_decl, | |
315fb5db NS |
10553 | DECL_CONTEXT (arg), |
10554 | /*entering_scope=*/0, | |
10555 | complain); | |
10556 | return cp_build_qualified_type_real | |
a3360e77 | 10557 | (r, cp_type_quals (t), complain); |
ae58fa02 | 10558 | } |
315fb5db NS |
10559 | else |
10560 | /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ | |
1f910942 | 10561 | return unshare_expr (arg); |
ae58fa02 MM |
10562 | } |
10563 | ||
10564 | if (level == 1) | |
10565 | /* This can happen during the attempted tsubst'ing in | |
10566 | unify. This means that we don't yet have any information | |
10567 | about the template parameter in question. */ | |
10568 | return t; | |
10569 | ||
10570 | /* If we get here, we must have been looking at a parm for a | |
10571 | more deeply nested template. Make a new version of this | |
10572 | template parameter, but with a lower level. */ | |
394473ab | 10573 | switch (code) |
ae58fa02 MM |
10574 | { |
10575 | case TEMPLATE_TYPE_PARM: | |
10576 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 10577 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
89d684bb | 10578 | if (cp_type_quals (t)) |
ae58fa02 | 10579 | { |
9ccf6541 | 10580 | r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); |
0cbd7506 MS |
10581 | r = cp_build_qualified_type_real |
10582 | (r, cp_type_quals (t), | |
394473ab | 10583 | complain | (code == TEMPLATE_TYPE_PARM |
4f2b0fb2 | 10584 | ? tf_ignore_bad_quals : 0)); |
9ccf6541 MM |
10585 | } |
10586 | else | |
10587 | { | |
11e74ea6 | 10588 | r = copy_type (t); |
9ccf6541 MM |
10589 | TEMPLATE_TYPE_PARM_INDEX (r) |
10590 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
ef3b7b17 | 10591 | r, levels, args, complain); |
9ccf6541 MM |
10592 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); |
10593 | TYPE_MAIN_VARIANT (r) = r; | |
10594 | TYPE_POINTER_TO (r) = NULL_TREE; | |
10595 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
10596 | ||
06d40de8 DG |
10597 | if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM) |
10598 | /* We have reduced the level of the template | |
10599 | template parameter, but not the levels of its | |
10600 | template parameters, so canonical_type_parameter | |
10601 | will not be able to find the canonical template | |
10602 | template parameter for this level. Thus, we | |
10603 | require structural equality checking to compare | |
10604 | TEMPLATE_TEMPLATE_PARMs. */ | |
10605 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
3d761c46 DG |
10606 | else if (TYPE_STRUCTURAL_EQUALITY_P (t)) |
10607 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
06d40de8 DG |
10608 | else |
10609 | TYPE_CANONICAL (r) = canonical_type_parameter (r); | |
10610 | ||
394473ab | 10611 | if (code == BOUND_TEMPLATE_TEMPLATE_PARM) |
9ccf6541 MM |
10612 | { |
10613 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, | |
c8094d83 | 10614 | complain, in_decl); |
9ccf6541 MM |
10615 | if (argvec == error_mark_node) |
10616 | return error_mark_node; | |
4393e105 | 10617 | |
9ccf6541 | 10618 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
aa373032 | 10619 | = build_template_info (TYPE_TI_TEMPLATE (t), argvec); |
9ccf6541 | 10620 | } |
ae58fa02 MM |
10621 | } |
10622 | break; | |
10623 | ||
10624 | case TEMPLATE_PARM_INDEX: | |
ef3b7b17 | 10625 | r = reduce_template_parm_level (t, type, levels, args, complain); |
ae58fa02 | 10626 | break; |
c8094d83 | 10627 | |
ae58fa02 | 10628 | default: |
315fb5db | 10629 | gcc_unreachable (); |
ae58fa02 MM |
10630 | } |
10631 | ||
5566b478 | 10632 | return r; |
ae58fa02 | 10633 | } |
5566b478 | 10634 | |
8d08fdba MS |
10635 | case TREE_LIST: |
10636 | { | |
058b15c1 | 10637 | tree purpose, value, chain; |
8d08fdba MS |
10638 | |
10639 | if (t == void_list_node) | |
10640 | return t; | |
10641 | ||
8d08fdba MS |
10642 | purpose = TREE_PURPOSE (t); |
10643 | if (purpose) | |
4393e105 MM |
10644 | { |
10645 | purpose = tsubst (purpose, args, complain, in_decl); | |
10646 | if (purpose == error_mark_node) | |
10647 | return error_mark_node; | |
10648 | } | |
8d08fdba MS |
10649 | value = TREE_VALUE (t); |
10650 | if (value) | |
4393e105 MM |
10651 | { |
10652 | value = tsubst (value, args, complain, in_decl); | |
10653 | if (value == error_mark_node) | |
10654 | return error_mark_node; | |
10655 | } | |
8d08fdba MS |
10656 | chain = TREE_CHAIN (t); |
10657 | if (chain && chain != void_type_node) | |
4393e105 MM |
10658 | { |
10659 | chain = tsubst (chain, args, complain, in_decl); | |
10660 | if (chain == error_mark_node) | |
10661 | return error_mark_node; | |
10662 | } | |
8d08fdba MS |
10663 | if (purpose == TREE_PURPOSE (t) |
10664 | && value == TREE_VALUE (t) | |
10665 | && chain == TREE_CHAIN (t)) | |
10666 | return t; | |
058b15c1 | 10667 | return hash_tree_cons (purpose, value, chain); |
8d08fdba | 10668 | } |
c8094d83 | 10669 | |
95b4aca6 | 10670 | case TREE_BINFO: |
bd7eccc4 | 10671 | /* We should never be tsubsting a binfo. */ |
315fb5db | 10672 | gcc_unreachable (); |
85b71cf2 | 10673 | |
95b4aca6 NS |
10674 | case TREE_VEC: |
10675 | /* A vector of template arguments. */ | |
50bc768d | 10676 | gcc_assert (!type); |
a91db711 | 10677 | return tsubst_template_args (t, args, complain, in_decl); |
8d08fdba | 10678 | |
8d08fdba MS |
10679 | case POINTER_TYPE: |
10680 | case REFERENCE_TYPE: | |
10681 | { | |
46c895ac | 10682 | if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) |
8d08fdba MS |
10683 | return t; |
10684 | ||
4393e105 | 10685 | /* [temp.deduct] |
c8094d83 | 10686 | |
4393e105 | 10687 | Type deduction may fail for any of the following |
c8094d83 | 10688 | reasons: |
4393e105 MM |
10689 | |
10690 | -- Attempting to create a pointer to reference type. | |
10691 | -- Attempting to create a reference to a reference type or | |
8af2fec4 RY |
10692 | a reference to void. |
10693 | ||
10694 | Core issue 106 says that creating a reference to a reference | |
10695 | during instantiation is no longer a cause for failure. We | |
10696 | only enforce this check in strict C++98 mode. */ | |
10697 | if ((TREE_CODE (type) == REFERENCE_TYPE | |
c1ae8be5 | 10698 | && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE)) |
0ecfe0b4 | 10699 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) |
79a7c7fa | 10700 | { |
82a98427 | 10701 | static location_t last_loc; |
79a7c7fa JM |
10702 | |
10703 | /* We keep track of the last time we issued this error | |
10704 | message to avoid spewing a ton of messages during a | |
10705 | single bad template instantiation. */ | |
c2ea3a40 | 10706 | if (complain & tf_error |
2d593c86 | 10707 | && last_loc != input_location) |
79a7c7fa | 10708 | { |
0ecfe0b4 | 10709 | if (TREE_CODE (type) == VOID_TYPE) |
33bd39a2 | 10710 | error ("forming reference to void"); |
4cd5a50a PB |
10711 | else if (code == POINTER_TYPE) |
10712 | error ("forming pointer to reference type %qT", type); | |
10713 | else | |
10714 | error ("forming reference to reference type %qT", type); | |
82a98427 | 10715 | last_loc = input_location; |
79a7c7fa JM |
10716 | } |
10717 | ||
4393e105 | 10718 | return error_mark_node; |
79a7c7fa JM |
10719 | } |
10720 | else if (code == POINTER_TYPE) | |
46c895ac NS |
10721 | { |
10722 | r = build_pointer_type (type); | |
10723 | if (TREE_CODE (type) == METHOD_TYPE) | |
10724 | r = build_ptrmemfunc_type (r); | |
10725 | } | |
8af2fec4 RY |
10726 | else if (TREE_CODE (type) == REFERENCE_TYPE) |
10727 | /* In C++0x, during template argument substitution, when there is an | |
10728 | attempt to create a reference to a reference type, reference | |
10729 | collapsing is applied as described in [14.3.1/4 temp.arg.type]: | |
10730 | ||
10731 | "If a template-argument for a template-parameter T names a type | |
10732 | that is a reference to a type A, an attempt to create the type | |
10733 | 'lvalue reference to cv T' creates the type 'lvalue reference to | |
10734 | A,' while an attempt to create the type type rvalue reference to | |
10735 | cv T' creates the type T" | |
10736 | */ | |
10737 | r = cp_build_reference_type | |
10738 | (TREE_TYPE (type), | |
10739 | TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type)); | |
8d08fdba | 10740 | else |
8af2fec4 | 10741 | r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t)); |
a3360e77 | 10742 | r = cp_build_qualified_type_real (r, cp_type_quals (t), complain); |
79a7c7fa | 10743 | |
a5f1c5f8 NS |
10744 | if (r != error_mark_node) |
10745 | /* Will this ever be needed for TYPE_..._TO values? */ | |
10746 | layout_type (r); | |
c8094d83 | 10747 | |
8d08fdba MS |
10748 | return r; |
10749 | } | |
a4443a08 | 10750 | case OFFSET_TYPE: |
0ecfe0b4 | 10751 | { |
4393e105 | 10752 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
9e1e64ec | 10753 | if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r)) |
4393e105 MM |
10754 | { |
10755 | /* [temp.deduct] | |
10756 | ||
10757 | Type deduction may fail for any of the following | |
10758 | reasons: | |
c8094d83 | 10759 | |
4393e105 | 10760 | -- Attempting to create "pointer to member of T" when T |
0cbd7506 | 10761 | is not a class type. */ |
c2ea3a40 | 10762 | if (complain & tf_error) |
0f51ccfc | 10763 | error ("creating pointer to member of non-class type %qT", r); |
4393e105 MM |
10764 | return error_mark_node; |
10765 | } | |
46c895ac NS |
10766 | if (TREE_CODE (type) == REFERENCE_TYPE) |
10767 | { | |
4f09be91 | 10768 | if (complain & tf_error) |
0f51ccfc | 10769 | error ("creating pointer to member reference type %qT", type); |
cb6d4a9f VR |
10770 | return error_mark_node; |
10771 | } | |
10772 | if (TREE_CODE (type) == VOID_TYPE) | |
10773 | { | |
10774 | if (complain & tf_error) | |
10775 | error ("creating pointer to member of type void"); | |
46c895ac NS |
10776 | return error_mark_node; |
10777 | } | |
50bc768d | 10778 | gcc_assert (TREE_CODE (type) != METHOD_TYPE); |
46c895ac | 10779 | if (TREE_CODE (type) == FUNCTION_TYPE) |
a5ac359a | 10780 | { |
0cbd7506 MS |
10781 | /* The type of the implicit object parameter gets its |
10782 | cv-qualifiers from the FUNCTION_TYPE. */ | |
0cbd7506 | 10783 | tree memptr; |
2872152c | 10784 | tree method_type = build_memfn_type (type, r, type_memfn_quals (type)); |
0cbd7506 MS |
10785 | memptr = build_ptrmemfunc_type (build_pointer_type (method_type)); |
10786 | return cp_build_qualified_type_real (memptr, cp_type_quals (t), | |
10787 | complain); | |
a5ac359a | 10788 | } |
46c895ac | 10789 | else |
b7a78333 | 10790 | return cp_build_qualified_type_real (build_ptrmem_type (r, type), |
a3360e77 | 10791 | cp_type_quals (t), |
b7a78333 | 10792 | complain); |
0ecfe0b4 | 10793 | } |
8d08fdba MS |
10794 | case FUNCTION_TYPE: |
10795 | case METHOD_TYPE: | |
10796 | { | |
c11b6f21 | 10797 | tree fntype; |
c7222c02 | 10798 | tree specs; |
4393e105 MM |
10799 | fntype = tsubst_function_type (t, args, complain, in_decl); |
10800 | if (fntype == error_mark_node) | |
10801 | return error_mark_node; | |
cabc336a | 10802 | |
34cd5ae7 | 10803 | /* Substitute the exception specification. */ |
c8094d83 | 10804 | specs = tsubst_exception_specification (t, args, complain, |
c7222c02 | 10805 | in_decl); |
9f6206d9 VR |
10806 | if (specs == error_mark_node) |
10807 | return error_mark_node; | |
c7222c02 MM |
10808 | if (specs) |
10809 | fntype = build_exception_variant (fntype, specs); | |
c11b6f21 | 10810 | return fntype; |
8d08fdba MS |
10811 | } |
10812 | case ARRAY_TYPE: | |
10813 | { | |
4393e105 MM |
10814 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
10815 | if (domain == error_mark_node) | |
10816 | return error_mark_node; | |
10817 | ||
10818 | /* As an optimization, we avoid regenerating the array type if | |
10819 | it will obviously be the same as T. */ | |
8d08fdba MS |
10820 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
10821 | return t; | |
0ecfe0b4 | 10822 | |
c8094d83 | 10823 | /* These checks should match the ones in grokdeclarator. |
4393e105 | 10824 | |
c8094d83 MS |
10825 | [temp.deduct] |
10826 | ||
10827 | The deduction may fail for any of the following reasons: | |
4393e105 MM |
10828 | |
10829 | -- Attempting to create an array with an element type that | |
c8094d83 | 10830 | is void, a function type, or a reference type, or [DR337] |
cfb91b67 | 10831 | an abstract class type. */ |
c8094d83 | 10832 | if (TREE_CODE (type) == VOID_TYPE |
4393e105 MM |
10833 | || TREE_CODE (type) == FUNCTION_TYPE |
10834 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 10835 | { |
c2ea3a40 | 10836 | if (complain & tf_error) |
0f51ccfc | 10837 | error ("creating array of %qT", type); |
4393e105 | 10838 | return error_mark_node; |
0ecfe0b4 | 10839 | } |
cfb91b67 GB |
10840 | if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type)) |
10841 | { | |
10842 | if (complain & tf_error) | |
c8094d83 | 10843 | error ("creating array of %qT, which is an abstract class type", |
cfb91b67 | 10844 | type); |
c8094d83 | 10845 | return error_mark_node; |
cfb91b67 | 10846 | } |
0ecfe0b4 | 10847 | |
8d08fdba | 10848 | r = build_cplus_array_type (type, domain); |
5044ab0e JM |
10849 | |
10850 | if (TYPE_USER_ALIGN (t)) | |
10851 | { | |
10852 | TYPE_ALIGN (r) = TYPE_ALIGN (t); | |
10853 | TYPE_USER_ALIGN (r) = 1; | |
10854 | } | |
10855 | ||
8d08fdba MS |
10856 | return r; |
10857 | } | |
10858 | ||
5566b478 MS |
10859 | case TYPENAME_TYPE: |
10860 | { | |
4393e105 MM |
10861 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
10862 | in_decl, /*entering_scope=*/1); | |
10863 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
c8094d83 | 10864 | complain, in_decl); |
4393e105 MM |
10865 | |
10866 | if (ctx == error_mark_node || f == error_mark_node) | |
10867 | return error_mark_node; | |
ae58fa02 | 10868 | |
9e1e64ec | 10869 | if (!MAYBE_CLASS_TYPE_P (ctx)) |
bf8f3f93 | 10870 | { |
c2ea3a40 | 10871 | if (complain & tf_error) |
0f51ccfc | 10872 | error ("%qT is not a class, struct, or union type", ctx); |
bf8f3f93 MM |
10873 | return error_mark_node; |
10874 | } | |
10875 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
10876 | { | |
10877 | /* Normally, make_typename_type does not require that the CTX | |
10878 | have complete type in order to allow things like: | |
c8094d83 | 10879 | |
0cbd7506 | 10880 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 10881 | |
bf8f3f93 MM |
10882 | But, such constructs have already been resolved by this |
10883 | point, so here CTX really should have complete type, unless | |
10884 | it's a partial instantiation. */ | |
34e1a144 | 10885 | ctx = complete_type (ctx); |
d0f062fb | 10886 | if (!COMPLETE_TYPE_P (ctx)) |
4393e105 | 10887 | { |
c2ea3a40 | 10888 | if (complain & tf_error) |
7a228918 | 10889 | cxx_incomplete_type_error (NULL_TREE, ctx); |
4393e105 MM |
10890 | return error_mark_node; |
10891 | } | |
10892 | } | |
ae58fa02 | 10893 | |
fc6a28d7 | 10894 | f = make_typename_type (ctx, f, typename_type, |
4f2b0fb2 | 10895 | (complain & tf_error) | tf_keep_type_decl); |
f0bcd168 MM |
10896 | if (f == error_mark_node) |
10897 | return f; | |
0cbd7506 MS |
10898 | if (TREE_CODE (f) == TYPE_DECL) |
10899 | { | |
4f2b0fb2 | 10900 | complain |= tf_ignore_bad_quals; |
0cbd7506 MS |
10901 | f = TREE_TYPE (f); |
10902 | } | |
c8094d83 | 10903 | |
fc6a28d7 MM |
10904 | if (TREE_CODE (f) != TYPENAME_TYPE) |
10905 | { | |
10906 | if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE) | |
c8094d83 | 10907 | error ("%qT resolves to %qT, which is not an enumeration type", |
fc6a28d7 MM |
10908 | t, f); |
10909 | else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f)) | |
c8094d83 | 10910 | error ("%qT resolves to %qT, which is is not a class type", |
fc6a28d7 MM |
10911 | t, f); |
10912 | } | |
10913 | ||
0cbd7506 MS |
10914 | return cp_build_qualified_type_real |
10915 | (f, cp_type_quals (f) | cp_type_quals (t), complain); | |
5566b478 | 10916 | } |
c8094d83 | 10917 | |
b8c6534b KL |
10918 | case UNBOUND_CLASS_TEMPLATE: |
10919 | { | |
10920 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, | |
10921 | in_decl, /*entering_scope=*/1); | |
10922 | tree name = TYPE_IDENTIFIER (t); | |
b939a023 | 10923 | tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t)); |
b8c6534b KL |
10924 | |
10925 | if (ctx == error_mark_node || name == error_mark_node) | |
10926 | return error_mark_node; | |
10927 | ||
b939a023 KL |
10928 | if (parm_list) |
10929 | parm_list = tsubst_template_parms (parm_list, args, complain); | |
10930 | return make_unbound_class_template (ctx, name, parm_list, complain); | |
b8c6534b KL |
10931 | } |
10932 | ||
b894fc05 | 10933 | case TYPEOF_TYPE: |
4393e105 | 10934 | { |
b830b74c | 10935 | tree type; |
4393e105 | 10936 | |
ecd0e976 JM |
10937 | ++cp_unevaluated_operand; |
10938 | ++c_inhibit_evaluation_warnings; | |
10939 | ||
10940 | type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args, | |
10941 | complain, in_decl, | |
10942 | /*integral_constant_expression_p=*/false); | |
10943 | ||
10944 | --cp_unevaluated_operand; | |
10945 | --c_inhibit_evaluation_warnings; | |
10946 | ||
10947 | type = finish_typeof (type); | |
b830b74c | 10948 | return cp_build_qualified_type_real (type, |
dce50630 | 10949 | cp_type_quals (t) |
b830b74c | 10950 | | cp_type_quals (type), |
dce50630 | 10951 | complain); |
4393e105 | 10952 | } |
b894fc05 | 10953 | |
3ad6a8e1 DG |
10954 | case DECLTYPE_TYPE: |
10955 | { | |
10956 | tree type; | |
10957 | ||
7d882b83 ILT |
10958 | ++cp_unevaluated_operand; |
10959 | ++c_inhibit_evaluation_warnings; | |
b344d949 JM |
10960 | |
10961 | type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args, | |
10962 | complain, in_decl, | |
10963 | /*integral_constant_expression_p=*/false); | |
10964 | ||
7d882b83 ILT |
10965 | --cp_unevaluated_operand; |
10966 | --c_inhibit_evaluation_warnings; | |
b344d949 | 10967 | |
d5f4eddd JM |
10968 | if (DECLTYPE_FOR_LAMBDA_CAPTURE (t)) |
10969 | type = lambda_capture_field_type (type); | |
10970 | else if (DECLTYPE_FOR_LAMBDA_RETURN (t)) | |
10971 | type = lambda_return_type (type); | |
10972 | else | |
10973 | type = finish_decltype_type | |
10974 | (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t)); | |
3ad6a8e1 DG |
10975 | return cp_build_qualified_type_real (type, |
10976 | cp_type_quals (t) | |
10977 | | cp_type_quals (type), | |
10978 | complain); | |
10979 | } | |
10980 | ||
5d80a306 DG |
10981 | case TYPE_ARGUMENT_PACK: |
10982 | case NONTYPE_ARGUMENT_PACK: | |
10983 | { | |
394473ab | 10984 | tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code); |
5d80a306 DG |
10985 | tree packed_out = |
10986 | tsubst_template_args (ARGUMENT_PACK_ARGS (t), | |
10987 | args, | |
10988 | complain, | |
10989 | in_decl); | |
10990 | SET_ARGUMENT_PACK_ARGS (r, packed_out); | |
10991 | ||
10992 | /* For template nontype argument packs, also substitute into | |
10993 | the type. */ | |
394473ab | 10994 | if (code == NONTYPE_ARGUMENT_PACK) |
5d80a306 DG |
10995 | TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl); |
10996 | ||
10997 | return r; | |
10998 | } | |
10999 | break; | |
11000 | ||
e90b1c4a JM |
11001 | case INTEGER_CST: |
11002 | case REAL_CST: | |
11003 | case STRING_CST: | |
11004 | case PLUS_EXPR: | |
11005 | case MINUS_EXPR: | |
11006 | case NEGATE_EXPR: | |
11007 | case NOP_EXPR: | |
11008 | case INDIRECT_REF: | |
11009 | case ADDR_EXPR: | |
11010 | case CALL_EXPR: | |
11011 | case ARRAY_REF: | |
11012 | case SCOPE_REF: | |
11013 | /* We should use one of the expression tsubsts for these codes. */ | |
11014 | gcc_unreachable (); | |
11015 | ||
8d08fdba | 11016 | default: |
394473ab | 11017 | sorry ("use of %qs in template", tree_code_name [(int) code]); |
8d08fdba MS |
11018 | return error_mark_node; |
11019 | } | |
11020 | } | |
11021 | ||
ee76b931 MM |
11022 | /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the |
11023 | type of the expression on the left-hand side of the "." or "->" | |
11024 | operator. */ | |
11025 | ||
11026 | static tree | |
11027 | tsubst_baselink (tree baselink, tree object_type, | |
11028 | tree args, tsubst_flags_t complain, tree in_decl) | |
11029 | { | |
11030 | tree name; | |
11031 | tree qualifying_scope; | |
11032 | tree fns; | |
6f67f000 | 11033 | tree optype; |
ee76b931 MM |
11034 | tree template_args = 0; |
11035 | bool template_id_p = false; | |
11036 | ||
51a203d9 MM |
11037 | /* A baselink indicates a function from a base class. Both the |
11038 | BASELINK_ACCESS_BINFO and the base class referenced may | |
11039 | indicate bases of the template class, rather than the | |
11040 | instantiated class. In addition, lookups that were not | |
11041 | ambiguous before may be ambiguous now. Therefore, we perform | |
11042 | the lookup again. */ | |
ee76b931 | 11043 | qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)); |
51a203d9 MM |
11044 | qualifying_scope = tsubst (qualifying_scope, args, |
11045 | complain, in_decl); | |
ee76b931 | 11046 | fns = BASELINK_FUNCTIONS (baselink); |
ff14c1f7 | 11047 | optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl); |
ee76b931 MM |
11048 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) |
11049 | { | |
11050 | template_id_p = true; | |
11051 | template_args = TREE_OPERAND (fns, 1); | |
11052 | fns = TREE_OPERAND (fns, 0); | |
bf12d54d NS |
11053 | if (template_args) |
11054 | template_args = tsubst_template_args (template_args, args, | |
11055 | complain, in_decl); | |
ee76b931 MM |
11056 | } |
11057 | name = DECL_NAME (get_first_fn (fns)); | |
ff14c1f7 JM |
11058 | if (IDENTIFIER_TYPENAME_P (name)) |
11059 | name = mangle_conv_op_name_for_type (optype); | |
ee76b931 | 11060 | baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1); |
baf7c318 PC |
11061 | if (!baselink) |
11062 | return error_mark_node; | |
c8094d83 | 11063 | |
9b60dfd7 MM |
11064 | /* If lookup found a single function, mark it as used at this |
11065 | point. (If it lookup found multiple functions the one selected | |
11066 | later by overload resolution will be marked as used at that | |
11067 | point.) */ | |
11068 | if (BASELINK_P (baselink)) | |
11069 | fns = BASELINK_FUNCTIONS (baselink); | |
11070 | if (!template_id_p && !really_overloaded_fn (fns)) | |
11071 | mark_used (OVL_CURRENT (fns)); | |
11072 | ||
11073 | /* Add back the template arguments, if present. */ | |
ee76b931 | 11074 | if (BASELINK_P (baselink) && template_id_p) |
c8094d83 | 11075 | BASELINK_FUNCTIONS (baselink) |
ee76b931 MM |
11076 | = build_nt (TEMPLATE_ID_EXPR, |
11077 | BASELINK_FUNCTIONS (baselink), | |
11078 | template_args); | |
6f67f000 | 11079 | /* Update the conversion operator type. */ |
ff14c1f7 | 11080 | BASELINK_OPTYPE (baselink) = optype; |
9b60dfd7 | 11081 | |
ee76b931 MM |
11082 | if (!object_type) |
11083 | object_type = current_class_type; | |
c8094d83 | 11084 | return adjust_result_of_qualified_name_lookup (baselink, |
ee76b931 MM |
11085 | qualifying_scope, |
11086 | object_type); | |
11087 | } | |
11088 | ||
11089 | /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is | |
11090 | true if the qualified-id will be a postfix-expression in-and-of | |
11091 | itself; false if more of the postfix-expression follows the | |
11092 | QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand | |
11093 | of "&". */ | |
11094 | ||
11095 | static tree | |
c8094d83 | 11096 | tsubst_qualified_id (tree qualified_id, tree args, |
ee76b931 MM |
11097 | tsubst_flags_t complain, tree in_decl, |
11098 | bool done, bool address_p) | |
11099 | { | |
11100 | tree expr; | |
11101 | tree scope; | |
11102 | tree name; | |
11103 | bool is_template; | |
11104 | tree template_args; | |
11105 | ||
50bc768d | 11106 | gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF); |
ee76b931 | 11107 | |
ee76b931 MM |
11108 | /* Figure out what name to look up. */ |
11109 | name = TREE_OPERAND (qualified_id, 1); | |
11110 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
11111 | { | |
11112 | is_template = true; | |
bf12d54d NS |
11113 | template_args = TREE_OPERAND (name, 1); |
11114 | if (template_args) | |
11115 | template_args = tsubst_template_args (template_args, args, | |
11116 | complain, in_decl); | |
ee76b931 MM |
11117 | name = TREE_OPERAND (name, 0); |
11118 | } | |
11119 | else | |
11120 | { | |
11121 | is_template = false; | |
11122 | template_args = NULL_TREE; | |
11123 | } | |
11124 | ||
6eeba0cc MM |
11125 | /* Substitute into the qualifying scope. When there are no ARGS, we |
11126 | are just trying to simplify a non-dependent expression. In that | |
11127 | case the qualifying scope may be dependent, and, in any case, | |
11128 | substituting will not help. */ | |
11129 | scope = TREE_OPERAND (qualified_id, 0); | |
11130 | if (args) | |
11131 | { | |
11132 | scope = tsubst (scope, args, complain, in_decl); | |
11133 | expr = tsubst_copy (name, args, complain, in_decl); | |
11134 | } | |
11135 | else | |
11136 | expr = name; | |
10b1d5e7 | 11137 | |
04daa92b JM |
11138 | if (dependent_scope_p (scope)) |
11139 | return build_qualified_name (NULL_TREE, scope, expr, | |
11140 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id)); | |
c8094d83 | 11141 | |
5e08432e | 11142 | if (!BASELINK_P (name) && !DECL_P (expr)) |
12483c9f | 11143 | { |
8ca4bf25 | 11144 | if (TREE_CODE (expr) == BIT_NOT_EXPR) |
7e361ae6 JM |
11145 | { |
11146 | /* A BIT_NOT_EXPR is used to represent a destructor. */ | |
11147 | if (!check_dtor_name (scope, TREE_OPERAND (expr, 0))) | |
11148 | { | |
11149 | error ("qualifying type %qT does not match destructor name ~%qT", | |
11150 | scope, TREE_OPERAND (expr, 0)); | |
11151 | expr = error_mark_node; | |
11152 | } | |
11153 | else | |
11154 | expr = lookup_qualified_name (scope, complete_dtor_identifier, | |
11155 | /*is_type_p=*/0, false); | |
11156 | } | |
8ca4bf25 MM |
11157 | else |
11158 | expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false); | |
12483c9f NS |
11159 | if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL |
11160 | ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL) | |
11161 | { | |
11162 | if (complain & tf_error) | |
b2693faf | 11163 | { |
0f51ccfc | 11164 | error ("dependent-name %qE is parsed as a non-type, but " |
b2693faf | 11165 | "instantiation yields a type", qualified_id); |
1f5b3869 | 11166 | inform (input_location, "say %<typename %E%> if a type is meant", qualified_id); |
b2693faf | 11167 | } |
12483c9f NS |
11168 | return error_mark_node; |
11169 | } | |
11170 | } | |
c8094d83 | 11171 | |
279b8466 | 11172 | if (DECL_P (expr)) |
8f78f01f MM |
11173 | { |
11174 | check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE, | |
11175 | scope); | |
11176 | /* Remember that there was a reference to this entity. */ | |
11177 | mark_used (expr); | |
11178 | } | |
11179 | ||
11180 | if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST) | |
11181 | { | |
11182 | if (complain & tf_error) | |
c8094d83 | 11183 | qualified_name_lookup_error (scope, |
8f78f01f | 11184 | TREE_OPERAND (qualified_id, 1), |
2b7a3abf | 11185 | expr, input_location); |
8f78f01f MM |
11186 | return error_mark_node; |
11187 | } | |
ee76b931 MM |
11188 | |
11189 | if (is_template) | |
10b1d5e7 | 11190 | expr = lookup_template_function (expr, template_args); |
ee76b931 | 11191 | |
22038b2c | 11192 | if (expr == error_mark_node && complain & tf_error) |
8f78f01f | 11193 | qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1), |
2b7a3abf | 11194 | expr, input_location); |
22038b2c | 11195 | else if (TYPE_P (scope)) |
ee76b931 | 11196 | { |
c8094d83 | 11197 | expr = (adjust_result_of_qualified_name_lookup |
ee76b931 | 11198 | (expr, scope, current_class_type)); |
3db45ab5 | 11199 | expr = (finish_qualified_id_expr |
02ed62dd MM |
11200 | (scope, expr, done, address_p, |
11201 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id), | |
11202 | /*template_arg_p=*/false)); | |
ee76b931 | 11203 | } |
c8094d83 | 11204 | |
015c2c66 MM |
11205 | /* Expressions do not generally have reference type. */ |
11206 | if (TREE_CODE (expr) != SCOPE_REF | |
11207 | /* However, if we're about to form a pointer-to-member, we just | |
11208 | want the referenced member referenced. */ | |
11209 | && TREE_CODE (expr) != OFFSET_REF) | |
aec5ba60 | 11210 | expr = convert_from_reference (expr); |
ee76b931 MM |
11211 | |
11212 | return expr; | |
11213 | } | |
11214 | ||
00d3396f JM |
11215 | /* Like tsubst, but deals with expressions. This function just replaces |
11216 | template parms; to finish processing the resultant expression, use | |
11217 | tsubst_expr. */ | |
11218 | ||
14d22dd6 | 11219 | static tree |
3a978d72 | 11220 | tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
5566b478 MS |
11221 | { |
11222 | enum tree_code code; | |
8452b1d3 | 11223 | tree r; |
8d08fdba | 11224 | |
b77ba909 | 11225 | if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE) |
5566b478 MS |
11226 | return t; |
11227 | ||
11228 | code = TREE_CODE (t); | |
b7484fbe | 11229 | |
5566b478 MS |
11230 | switch (code) |
11231 | { | |
11232 | case PARM_DECL: | |
a723baf1 | 11233 | r = retrieve_local_specialization (t); |
b344d949 JM |
11234 | |
11235 | if (r == NULL) | |
11236 | { | |
c7cb9f42 | 11237 | tree c; |
b344d949 | 11238 | /* This can happen for a parameter name used later in a function |
448545cb JM |
11239 | declaration (such as in a late-specified return type). Just |
11240 | make a dummy decl, since it's only used for its type. */ | |
7d882b83 | 11241 | gcc_assert (cp_unevaluated_operand != 0); |
c7cb9f42 DS |
11242 | /* We copy T because want to tsubst the PARM_DECL only, |
11243 | not the following PARM_DECLs that are chained to T. */ | |
11244 | c = copy_node (t); | |
11245 | r = tsubst_decl (c, args, complain); | |
448545cb JM |
11246 | /* Give it the template pattern as its context; its true context |
11247 | hasn't been instantiated yet and this is good enough for | |
11248 | mangling. */ | |
11249 | DECL_CONTEXT (r) = DECL_CONTEXT (t); | |
b344d949 JM |
11250 | } |
11251 | ||
5d80a306 DG |
11252 | if (TREE_CODE (r) == ARGUMENT_PACK_SELECT) |
11253 | r = ARGUMENT_PACK_SELECT_ARG (r); | |
c0694c4b | 11254 | mark_used (r); |
a723baf1 | 11255 | return r; |
5566b478 MS |
11256 | |
11257 | case CONST_DECL: | |
ed44da02 MM |
11258 | { |
11259 | tree enum_type; | |
11260 | tree v; | |
11261 | ||
a723baf1 MM |
11262 | if (DECL_TEMPLATE_PARM_P (t)) |
11263 | return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); | |
7b6d72fc MM |
11264 | /* There is no need to substitute into namespace-scope |
11265 | enumerators. */ | |
11266 | if (DECL_NAMESPACE_SCOPE_P (t)) | |
ed44da02 | 11267 | return t; |
d5a10cf0 MM |
11268 | /* If ARGS is NULL, then T is known to be non-dependent. */ |
11269 | if (args == NULL_TREE) | |
8a784e4a | 11270 | return integral_constant_value (t); |
ed44da02 MM |
11271 | |
11272 | /* Unfortunately, we cannot just call lookup_name here. | |
9188c363 | 11273 | Consider: |
c8094d83 | 11274 | |
9188c363 MM |
11275 | template <int I> int f() { |
11276 | enum E { a = I }; | |
11277 | struct S { void g() { E e = a; } }; | |
11278 | }; | |
c8094d83 | 11279 | |
9188c363 MM |
11280 | When we instantiate f<7>::S::g(), say, lookup_name is not |
11281 | clever enough to find f<7>::a. */ | |
c8094d83 MS |
11282 | enum_type |
11283 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, | |
ed44da02 MM |
11284 | /*entering_scope=*/0); |
11285 | ||
c8094d83 MS |
11286 | for (v = TYPE_VALUES (enum_type); |
11287 | v != NULL_TREE; | |
ed44da02 MM |
11288 | v = TREE_CHAIN (v)) |
11289 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
11290 | return TREE_VALUE (v); | |
11291 | ||
11292 | /* We didn't find the name. That should never happen; if | |
11293 | name-lookup found it during preliminary parsing, we | |
11294 | should find it again here during instantiation. */ | |
315fb5db | 11295 | gcc_unreachable (); |
ed44da02 | 11296 | } |
db04386f | 11297 | return t; |
ed44da02 | 11298 | |
5566b478 MS |
11299 | case FIELD_DECL: |
11300 | if (DECL_CONTEXT (t)) | |
11301 | { | |
0978790f | 11302 | tree ctx; |
0978790f | 11303 | |
4393e105 | 11304 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 11305 | /*entering_scope=*/1); |
0978790f | 11306 | if (ctx != DECL_CONTEXT (t)) |
bad1f462 KL |
11307 | { |
11308 | tree r = lookup_field (ctx, DECL_NAME (t), 0, false); | |
11309 | if (!r) | |
11310 | { | |
11311 | if (complain & tf_error) | |
11312 | error ("using invalid field %qD", t); | |
11313 | return error_mark_node; | |
11314 | } | |
11315 | return r; | |
11316 | } | |
5566b478 | 11317 | } |
c8094d83 | 11318 | |
5566b478 MS |
11319 | return t; |
11320 | ||
11321 | case VAR_DECL: | |
11322 | case FUNCTION_DECL: | |
a723baf1 MM |
11323 | if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) |
11324 | || local_variable_p (t)) | |
4393e105 | 11325 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
11326 | mark_used (t); |
11327 | return t; | |
11328 | ||
e90b1c4a JM |
11329 | case OVERLOAD: |
11330 | /* An OVERLOAD will always be a non-dependent overload set; an | |
11331 | overload set from function scope will just be represented with an | |
11332 | IDENTIFIER_NODE, and from class scope with a BASELINK. */ | |
11333 | gcc_assert (!uses_template_parms (t)); | |
11334 | return t; | |
11335 | ||
a723baf1 | 11336 | case BASELINK: |
ee76b931 | 11337 | return tsubst_baselink (t, current_class_type, args, complain, in_decl); |
a723baf1 | 11338 | |
98c1c668 | 11339 | case TEMPLATE_DECL: |
a723baf1 | 11340 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
c8094d83 | 11341 | return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), |
a723baf1 | 11342 | args, complain, in_decl); |
c7222c02 | 11343 | else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t)) |
4393e105 | 11344 | return tsubst (t, args, complain, in_decl); |
fcea7401 KL |
11345 | else if (DECL_CLASS_SCOPE_P (t) |
11346 | && uses_template_parms (DECL_CONTEXT (t))) | |
11347 | { | |
11348 | /* Template template argument like the following example need | |
11349 | special treatment: | |
11350 | ||
11351 | template <template <class> class TT> struct C {}; | |
11352 | template <class T> struct D { | |
11353 | template <class U> struct E {}; | |
0cbd7506 | 11354 | C<E> c; // #1 |
fcea7401 KL |
11355 | }; |
11356 | D<int> d; // #2 | |
11357 | ||
11358 | We are processing the template argument `E' in #1 for | |
11359 | the template instantiation #2. Originally, `E' is a | |
11360 | TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we | |
11361 | have to substitute this with one having context `D<int>'. */ | |
11362 | ||
11363 | tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl); | |
11364 | return lookup_field (context, DECL_NAME(t), 0, false); | |
11365 | } | |
98c1c668 | 11366 | else |
fcea7401 | 11367 | /* Ordinary template template argument. */ |
98c1c668 JM |
11368 | return t; |
11369 | ||
5566b478 MS |
11370 | case CAST_EXPR: |
11371 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
11372 | case CONST_CAST_EXPR: |
11373 | case STATIC_CAST_EXPR: | |
11374 | case DYNAMIC_CAST_EXPR: | |
51924768 | 11375 | case NOP_EXPR: |
5566b478 | 11376 | return build1 |
4393e105 MM |
11377 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
11378 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 | 11379 | |
5d80a306 DG |
11380 | case SIZEOF_EXPR: |
11381 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) | |
11382 | { | |
11383 | /* We only want to compute the number of arguments. */ | |
11384 | tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args, | |
11385 | complain, in_decl); | |
e68c63e3 | 11386 | int len = 0; |
063cc99c DG |
11387 | |
11388 | if (TREE_CODE (expanded) == TREE_VEC) | |
11389 | len = TREE_VEC_LENGTH (expanded); | |
11390 | ||
4745e4eb JJ |
11391 | if (expanded == error_mark_node) |
11392 | return error_mark_node; | |
063cc99c DG |
11393 | else if (PACK_EXPANSION_P (expanded) |
11394 | || (TREE_CODE (expanded) == TREE_VEC | |
11395 | && len > 0 | |
11396 | && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1)))) | |
11397 | { | |
11398 | if (TREE_CODE (expanded) == TREE_VEC) | |
11399 | expanded = TREE_VEC_ELT (expanded, len - 1); | |
11400 | ||
11401 | if (TYPE_P (expanded)) | |
11402 | return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR, | |
11403 | complain & tf_error); | |
11404 | else | |
11405 | return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR, | |
11406 | complain & tf_error); | |
11407 | } | |
11408 | else | |
11409 | return build_int_cst (size_type_node, len); | |
5d80a306 DG |
11410 | } |
11411 | /* Fall through */ | |
11412 | ||
5566b478 | 11413 | case INDIRECT_REF: |
5566b478 MS |
11414 | case NEGATE_EXPR: |
11415 | case TRUTH_NOT_EXPR: | |
b87692e5 | 11416 | case BIT_NOT_EXPR: |
5566b478 | 11417 | case ADDR_EXPR: |
392e3d51 | 11418 | case UNARY_PLUS_EXPR: /* Unary + */ |
abff8e06 | 11419 | case ALIGNOF_EXPR: |
c154b3d8 | 11420 | case AT_ENCODE_EXPR: |
5566b478 | 11421 | case ARROW_EXPR: |
fc378698 | 11422 | case THROW_EXPR: |
5156628f | 11423 | case TYPEID_EXPR: |
f5733617 SS |
11424 | case REALPART_EXPR: |
11425 | case IMAGPART_EXPR: | |
5566b478 | 11426 | return build1 |
6a629cac | 11427 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 11428 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 | 11429 | |
a723baf1 MM |
11430 | case COMPONENT_REF: |
11431 | { | |
11432 | tree object; | |
11433 | tree name; | |
11434 | ||
11435 | object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
11436 | name = TREE_OPERAND (t, 1); | |
c8094d83 | 11437 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
a723baf1 MM |
11438 | { |
11439 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
11440 | complain, in_decl); | |
11441 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
11442 | } | |
11443 | else if (TREE_CODE (name) == SCOPE_REF | |
11444 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
11445 | { | |
11446 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, | |
11447 | complain, in_decl); | |
11448 | name = TREE_OPERAND (name, 1); | |
11449 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
11450 | complain, in_decl); | |
11451 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
02ed62dd | 11452 | name = build_qualified_name (/*type=*/NULL_TREE, |
3db45ab5 | 11453 | base, name, |
02ed62dd | 11454 | /*template_p=*/false); |
a723baf1 | 11455 | } |
ee76b931 | 11456 | else if (TREE_CODE (name) == BASELINK) |
c8094d83 MS |
11457 | name = tsubst_baselink (name, |
11458 | non_reference (TREE_TYPE (object)), | |
11459 | args, complain, | |
ee76b931 | 11460 | in_decl); |
a723baf1 | 11461 | else |
ee76b931 | 11462 | name = tsubst_copy (name, args, complain, in_decl); |
44de5aeb | 11463 | return build_nt (COMPONENT_REF, object, name, NULL_TREE); |
a723baf1 MM |
11464 | } |
11465 | ||
5566b478 MS |
11466 | case PLUS_EXPR: |
11467 | case MINUS_EXPR: | |
11468 | case MULT_EXPR: | |
11469 | case TRUNC_DIV_EXPR: | |
11470 | case CEIL_DIV_EXPR: | |
11471 | case FLOOR_DIV_EXPR: | |
11472 | case ROUND_DIV_EXPR: | |
11473 | case EXACT_DIV_EXPR: | |
11474 | case BIT_AND_EXPR: | |
5566b478 MS |
11475 | case BIT_IOR_EXPR: |
11476 | case BIT_XOR_EXPR: | |
11477 | case TRUNC_MOD_EXPR: | |
11478 | case FLOOR_MOD_EXPR: | |
11479 | case TRUTH_ANDIF_EXPR: | |
11480 | case TRUTH_ORIF_EXPR: | |
11481 | case TRUTH_AND_EXPR: | |
11482 | case TRUTH_OR_EXPR: | |
11483 | case RSHIFT_EXPR: | |
11484 | case LSHIFT_EXPR: | |
11485 | case RROTATE_EXPR: | |
11486 | case LROTATE_EXPR: | |
11487 | case EQ_EXPR: | |
11488 | case NE_EXPR: | |
11489 | case MAX_EXPR: | |
11490 | case MIN_EXPR: | |
11491 | case LE_EXPR: | |
11492 | case GE_EXPR: | |
11493 | case LT_EXPR: | |
11494 | case GT_EXPR: | |
5566b478 | 11495 | case COMPOUND_EXPR: |
5566b478 MS |
11496 | case DOTSTAR_EXPR: |
11497 | case MEMBER_REF: | |
519c9806 MM |
11498 | case PREDECREMENT_EXPR: |
11499 | case PREINCREMENT_EXPR: | |
11500 | case POSTDECREMENT_EXPR: | |
11501 | case POSTINCREMENT_EXPR: | |
5566b478 | 11502 | return build_nt |
4393e105 MM |
11503 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
11504 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 | 11505 | |
02ed62dd MM |
11506 | case SCOPE_REF: |
11507 | return build_qualified_name (/*type=*/NULL_TREE, | |
11508 | tsubst_copy (TREE_OPERAND (t, 0), | |
11509 | args, complain, in_decl), | |
11510 | tsubst_copy (TREE_OPERAND (t, 1), | |
11511 | args, complain, in_decl), | |
11512 | QUALIFIED_NAME_IS_TEMPLATE (t)); | |
11513 | ||
d8987adb NS |
11514 | case ARRAY_REF: |
11515 | return build_nt | |
11516 | (ARRAY_REF, | |
11517 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
11518 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
11519 | NULL_TREE, NULL_TREE); | |
11520 | ||
5566b478 | 11521 | case CALL_EXPR: |
5039610b SL |
11522 | { |
11523 | int n = VL_EXP_OPERAND_LENGTH (t); | |
11524 | tree result = build_vl_exp (CALL_EXPR, n); | |
11525 | int i; | |
11526 | for (i = 0; i < n; i++) | |
11527 | TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args, | |
11528 | complain, in_decl); | |
11529 | return result; | |
11530 | } | |
5566b478 | 11531 | |
5566b478 MS |
11532 | case COND_EXPR: |
11533 | case MODOP_EXPR: | |
40242ccf | 11534 | case PSEUDO_DTOR_EXPR: |
67da3287 | 11535 | { |
8452b1d3 | 11536 | r = build_nt |
4393e105 MM |
11537 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
11538 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
11539 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
e4c2c34b | 11540 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); |
67da3287 MM |
11541 | return r; |
11542 | } | |
5566b478 MS |
11543 | |
11544 | case NEW_EXPR: | |
11545 | { | |
8452b1d3 | 11546 | r = build_nt |
4393e105 MM |
11547 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
11548 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
11549 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
11550 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
11551 | return r; | |
11552 | } | |
11553 | ||
11554 | case DELETE_EXPR: | |
11555 | { | |
8452b1d3 | 11556 | r = build_nt |
4393e105 MM |
11557 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
11558 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
11559 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
11560 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
11561 | return r; | |
11562 | } | |
11563 | ||
386b8a85 JM |
11564 | case TEMPLATE_ID_EXPR: |
11565 | { | |
0cbd7506 | 11566 | /* Substituted template arguments */ |
a91db711 NS |
11567 | tree fn = TREE_OPERAND (t, 0); |
11568 | tree targs = TREE_OPERAND (t, 1); | |
856216bb | 11569 | |
a91db711 | 11570 | fn = tsubst_copy (fn, args, complain, in_decl); |
bf12d54d NS |
11571 | if (targs) |
11572 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 11573 | |
a91db711 | 11574 | return lookup_template_function (fn, targs); |
386b8a85 JM |
11575 | } |
11576 | ||
5566b478 MS |
11577 | case TREE_LIST: |
11578 | { | |
11579 | tree purpose, value, chain; | |
11580 | ||
11581 | if (t == void_list_node) | |
11582 | return t; | |
11583 | ||
11584 | purpose = TREE_PURPOSE (t); | |
11585 | if (purpose) | |
4393e105 | 11586 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
11587 | value = TREE_VALUE (t); |
11588 | if (value) | |
4393e105 | 11589 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
11590 | chain = TREE_CHAIN (t); |
11591 | if (chain && chain != void_type_node) | |
4393e105 | 11592 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
11593 | if (purpose == TREE_PURPOSE (t) |
11594 | && value == TREE_VALUE (t) | |
11595 | && chain == TREE_CHAIN (t)) | |
11596 | return t; | |
11597 | return tree_cons (purpose, value, chain); | |
11598 | } | |
11599 | ||
11600 | case RECORD_TYPE: | |
11601 | case UNION_TYPE: | |
11602 | case ENUMERAL_TYPE: | |
11603 | case INTEGER_TYPE: | |
11604 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 11605 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 11606 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 11607 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
11608 | case POINTER_TYPE: |
11609 | case REFERENCE_TYPE: | |
11610 | case OFFSET_TYPE: | |
11611 | case FUNCTION_TYPE: | |
11612 | case METHOD_TYPE: | |
11613 | case ARRAY_TYPE: | |
11614 | case TYPENAME_TYPE: | |
b8c6534b | 11615 | case UNBOUND_CLASS_TEMPLATE: |
2a2b1d56 | 11616 | case TYPEOF_TYPE: |
3ad6a8e1 | 11617 | case DECLTYPE_TYPE: |
f84b4be9 | 11618 | case TYPE_DECL: |
4393e105 | 11619 | return tsubst (t, args, complain, in_decl); |
5566b478 | 11620 | |
e92cc029 | 11621 | case IDENTIFIER_NODE: |
421844e7 | 11622 | if (IDENTIFIER_TYPENAME_P (t)) |
1f6e1acc AS |
11623 | { |
11624 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
1f84ec23 | 11625 | return mangle_conv_op_name_for_type (new_type); |
1f6e1acc | 11626 | } |
e92cc029 MS |
11627 | else |
11628 | return t; | |
11629 | ||
5156628f | 11630 | case CONSTRUCTOR: |
4038c495 GB |
11631 | /* This is handled by tsubst_copy_and_build. */ |
11632 | gcc_unreachable (); | |
5156628f | 11633 | |
371534a9 | 11634 | case VA_ARG_EXPR: |
ea333e1c | 11635 | return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, |
c2ea3a40 NS |
11636 | in_decl), |
11637 | tsubst (TREE_TYPE (t), args, complain, in_decl)); | |
f9817201 | 11638 | |
543a0daa RH |
11639 | case CLEANUP_POINT_EXPR: |
11640 | /* We shouldn't have built any of these during initial template | |
11641 | generation. Instead, they should be built during instantiation | |
11642 | in response to the saved STMT_IS_FULL_EXPR_P setting. */ | |
315fb5db | 11643 | gcc_unreachable (); |
543a0daa | 11644 | |
7eab6e7b | 11645 | case OFFSET_REF: |
627bc938 JM |
11646 | r = build2 |
11647 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), | |
11648 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
11649 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
11650 | PTRMEM_OK_P (r) = PTRMEM_OK_P (t); | |
11651 | mark_used (TREE_OPERAND (r, 1)); | |
11652 | return r; | |
7eab6e7b | 11653 | |
5d80a306 DG |
11654 | case EXPR_PACK_EXPANSION: |
11655 | error ("invalid use of pack expansion expression"); | |
11656 | return error_mark_node; | |
11657 | ||
11658 | case NONTYPE_ARGUMENT_PACK: | |
11659 | error ("use %<...%> to expand argument pack"); | |
11660 | return error_mark_node; | |
11661 | ||
9c3c8ad7 JM |
11662 | case INTEGER_CST: |
11663 | case REAL_CST: | |
11664 | case STRING_CST: | |
fc36036b | 11665 | case COMPLEX_CST: |
9c3c8ad7 JM |
11666 | { |
11667 | /* Instantiate any typedefs in the type. */ | |
11668 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
11669 | r = fold_convert (type, t); | |
11670 | gcc_assert (TREE_CODE (r) == code); | |
11671 | return r; | |
11672 | } | |
11673 | ||
e90b1c4a JM |
11674 | case PTRMEM_CST: |
11675 | /* These can sometimes show up in a partial instantiation, but never | |
11676 | involve template parms. */ | |
11677 | gcc_assert (!uses_template_parms (t)); | |
5566b478 | 11678 | return t; |
e90b1c4a JM |
11679 | |
11680 | default: | |
11681 | gcc_unreachable (); | |
5566b478 MS |
11682 | } |
11683 | } | |
11684 | ||
1799e5d5 RH |
11685 | /* Like tsubst_copy, but specifically for OpenMP clauses. */ |
11686 | ||
11687 | static tree | |
11688 | tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain, | |
11689 | tree in_decl) | |
11690 | { | |
11691 | tree new_clauses = NULL, nc, oc; | |
11692 | ||
11693 | for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc)) | |
11694 | { | |
11695 | nc = copy_node (oc); | |
11696 | OMP_CLAUSE_CHAIN (nc) = new_clauses; | |
11697 | new_clauses = nc; | |
11698 | ||
11699 | switch (OMP_CLAUSE_CODE (nc)) | |
11700 | { | |
a68ab351 JJ |
11701 | case OMP_CLAUSE_LASTPRIVATE: |
11702 | if (OMP_CLAUSE_LASTPRIVATE_STMT (oc)) | |
11703 | { | |
11704 | OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list (); | |
11705 | tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain, | |
11706 | in_decl, /*integral_constant_expression_p=*/false); | |
11707 | OMP_CLAUSE_LASTPRIVATE_STMT (nc) | |
11708 | = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc)); | |
11709 | } | |
11710 | /* FALLTHRU */ | |
1799e5d5 RH |
11711 | case OMP_CLAUSE_PRIVATE: |
11712 | case OMP_CLAUSE_SHARED: | |
11713 | case OMP_CLAUSE_FIRSTPRIVATE: | |
1799e5d5 RH |
11714 | case OMP_CLAUSE_REDUCTION: |
11715 | case OMP_CLAUSE_COPYIN: | |
11716 | case OMP_CLAUSE_COPYPRIVATE: | |
11717 | case OMP_CLAUSE_IF: | |
11718 | case OMP_CLAUSE_NUM_THREADS: | |
11719 | case OMP_CLAUSE_SCHEDULE: | |
a68ab351 | 11720 | case OMP_CLAUSE_COLLAPSE: |
1799e5d5 | 11721 | OMP_CLAUSE_OPERAND (nc, 0) |
015c2c66 MM |
11722 | = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain, |
11723 | in_decl, /*integral_constant_expression_p=*/false); | |
1799e5d5 RH |
11724 | break; |
11725 | case OMP_CLAUSE_NOWAIT: | |
11726 | case OMP_CLAUSE_ORDERED: | |
11727 | case OMP_CLAUSE_DEFAULT: | |
a68ab351 | 11728 | case OMP_CLAUSE_UNTIED: |
1799e5d5 RH |
11729 | break; |
11730 | default: | |
11731 | gcc_unreachable (); | |
11732 | } | |
11733 | } | |
11734 | ||
11735 | return finish_omp_clauses (nreverse (new_clauses)); | |
11736 | } | |
11737 | ||
f74dcfb7 JJ |
11738 | /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */ |
11739 | ||
11740 | static tree | |
11741 | tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain, | |
11742 | tree in_decl) | |
11743 | { | |
11744 | #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl) | |
11745 | ||
11746 | tree purpose, value, chain; | |
11747 | ||
11748 | if (t == NULL) | |
11749 | return t; | |
11750 | ||
11751 | if (TREE_CODE (t) != TREE_LIST) | |
11752 | return tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
11753 | /*function_p=*/false, |
11754 | /*integral_constant_expression_p=*/false); | |
f74dcfb7 JJ |
11755 | |
11756 | if (t == void_list_node) | |
11757 | return t; | |
11758 | ||
11759 | purpose = TREE_PURPOSE (t); | |
11760 | if (purpose) | |
11761 | purpose = RECUR (purpose); | |
11762 | value = TREE_VALUE (t); | |
1c384bf1 | 11763 | if (value && TREE_CODE (value) != LABEL_DECL) |
f74dcfb7 JJ |
11764 | value = RECUR (value); |
11765 | chain = TREE_CHAIN (t); | |
11766 | if (chain && chain != void_type_node) | |
11767 | chain = RECUR (chain); | |
11768 | return tree_cons (purpose, value, chain); | |
11769 | #undef RECUR | |
11770 | } | |
11771 | ||
a68ab351 JJ |
11772 | /* Substitute one OMP_FOR iterator. */ |
11773 | ||
11774 | static void | |
11775 | tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv, | |
11776 | tree condv, tree incrv, tree *clauses, | |
11777 | tree args, tsubst_flags_t complain, tree in_decl, | |
11778 | bool integral_constant_expression_p) | |
11779 | { | |
11780 | #define RECUR(NODE) \ | |
11781 | tsubst_expr ((NODE), args, complain, in_decl, \ | |
11782 | integral_constant_expression_p) | |
a2d4cdc9 | 11783 | tree decl, init, cond, incr, auto_node; |
a68ab351 JJ |
11784 | |
11785 | init = TREE_VEC_ELT (OMP_FOR_INIT (t), i); | |
11786 | gcc_assert (TREE_CODE (init) == MODIFY_EXPR); | |
11787 | decl = RECUR (TREE_OPERAND (init, 0)); | |
11788 | init = TREE_OPERAND (init, 1); | |
a2d4cdc9 JJ |
11789 | auto_node = type_uses_auto (TREE_TYPE (decl)); |
11790 | if (auto_node && init) | |
11791 | { | |
11792 | tree init_expr = init; | |
a2d4cdc9 JJ |
11793 | if (TREE_CODE (init_expr) == DECL_EXPR) |
11794 | init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr)); | |
749db154 | 11795 | init_expr = RECUR (init_expr); |
a2d4cdc9 JJ |
11796 | TREE_TYPE (decl) |
11797 | = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node); | |
a2d4cdc9 | 11798 | } |
a68ab351 JJ |
11799 | gcc_assert (!type_dependent_expression_p (decl)); |
11800 | ||
11801 | if (!CLASS_TYPE_P (TREE_TYPE (decl))) | |
11802 | { | |
11803 | cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i)); | |
11804 | incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); | |
11805 | if (TREE_CODE (incr) == MODIFY_EXPR) | |
11806 | incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR, | |
11807 | RECUR (TREE_OPERAND (incr, 1)), | |
11808 | complain); | |
11809 | else | |
11810 | incr = RECUR (incr); | |
11811 | TREE_VEC_ELT (declv, i) = decl; | |
11812 | TREE_VEC_ELT (initv, i) = init; | |
11813 | TREE_VEC_ELT (condv, i) = cond; | |
11814 | TREE_VEC_ELT (incrv, i) = incr; | |
11815 | return; | |
11816 | } | |
11817 | ||
11818 | if (init && TREE_CODE (init) != DECL_EXPR) | |
11819 | { | |
11820 | tree c; | |
11821 | for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c)) | |
11822 | { | |
11823 | if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE | |
11824 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE) | |
11825 | && OMP_CLAUSE_DECL (c) == decl) | |
11826 | break; | |
11827 | else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE | |
11828 | && OMP_CLAUSE_DECL (c) == decl) | |
11829 | error ("iteration variable %qD should not be firstprivate", decl); | |
11830 | else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION | |
11831 | && OMP_CLAUSE_DECL (c) == decl) | |
11832 | error ("iteration variable %qD should not be reduction", decl); | |
11833 | } | |
11834 | if (c == NULL) | |
11835 | { | |
c2255bc4 | 11836 | c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE); |
a68ab351 JJ |
11837 | OMP_CLAUSE_DECL (c) = decl; |
11838 | c = finish_omp_clauses (c); | |
11839 | if (c) | |
11840 | { | |
11841 | OMP_CLAUSE_CHAIN (c) = *clauses; | |
11842 | *clauses = c; | |
11843 | } | |
11844 | } | |
11845 | } | |
11846 | cond = TREE_VEC_ELT (OMP_FOR_COND (t), i); | |
11847 | if (COMPARISON_CLASS_P (cond)) | |
11848 | cond = build2 (TREE_CODE (cond), boolean_type_node, | |
11849 | RECUR (TREE_OPERAND (cond, 0)), | |
11850 | RECUR (TREE_OPERAND (cond, 1))); | |
11851 | else | |
11852 | cond = RECUR (cond); | |
11853 | incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); | |
11854 | switch (TREE_CODE (incr)) | |
11855 | { | |
11856 | case PREINCREMENT_EXPR: | |
11857 | case PREDECREMENT_EXPR: | |
11858 | case POSTINCREMENT_EXPR: | |
11859 | case POSTDECREMENT_EXPR: | |
11860 | incr = build2 (TREE_CODE (incr), TREE_TYPE (decl), | |
11861 | RECUR (TREE_OPERAND (incr, 0)), NULL_TREE); | |
11862 | break; | |
11863 | case MODIFY_EXPR: | |
11864 | if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR | |
11865 | || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) | |
11866 | { | |
11867 | tree rhs = TREE_OPERAND (incr, 1); | |
11868 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), | |
11869 | RECUR (TREE_OPERAND (incr, 0)), | |
11870 | build2 (TREE_CODE (rhs), TREE_TYPE (decl), | |
11871 | RECUR (TREE_OPERAND (rhs, 0)), | |
11872 | RECUR (TREE_OPERAND (rhs, 1)))); | |
11873 | } | |
11874 | else | |
11875 | incr = RECUR (incr); | |
11876 | break; | |
11877 | case MODOP_EXPR: | |
11878 | if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR | |
11879 | || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) | |
11880 | { | |
11881 | tree lhs = RECUR (TREE_OPERAND (incr, 0)); | |
11882 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs, | |
11883 | build2 (TREE_CODE (TREE_OPERAND (incr, 1)), | |
11884 | TREE_TYPE (decl), lhs, | |
11885 | RECUR (TREE_OPERAND (incr, 2)))); | |
11886 | } | |
11887 | else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR | |
11888 | && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR | |
11889 | || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR))) | |
11890 | { | |
11891 | tree rhs = TREE_OPERAND (incr, 2); | |
11892 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), | |
11893 | RECUR (TREE_OPERAND (incr, 0)), | |
11894 | build2 (TREE_CODE (rhs), TREE_TYPE (decl), | |
11895 | RECUR (TREE_OPERAND (rhs, 0)), | |
11896 | RECUR (TREE_OPERAND (rhs, 1)))); | |
11897 | } | |
11898 | else | |
11899 | incr = RECUR (incr); | |
11900 | break; | |
11901 | default: | |
11902 | incr = RECUR (incr); | |
11903 | break; | |
11904 | } | |
11905 | ||
11906 | TREE_VEC_ELT (declv, i) = decl; | |
11907 | TREE_VEC_ELT (initv, i) = init; | |
11908 | TREE_VEC_ELT (condv, i) = cond; | |
11909 | TREE_VEC_ELT (incrv, i) = incr; | |
11910 | #undef RECUR | |
11911 | } | |
11912 | ||
cc23546e JO |
11913 | /* Like tsubst_copy for expressions, etc. but also does semantic |
11914 | processing. */ | |
00d3396f | 11915 | |
14d22dd6 | 11916 | static tree |
015c2c66 MM |
11917 | tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, |
11918 | bool integral_constant_expression_p) | |
5566b478 | 11919 | { |
015c2c66 MM |
11920 | #define RECUR(NODE) \ |
11921 | tsubst_expr ((NODE), args, complain, in_decl, \ | |
11922 | integral_constant_expression_p) | |
11923 | ||
fd10dd09 | 11924 | tree stmt, tmp; |
558475f0 | 11925 | |
5566b478 MS |
11926 | if (t == NULL_TREE || t == error_mark_node) |
11927 | return t; | |
11928 | ||
93409b8c PB |
11929 | if (EXPR_HAS_LOCATION (t)) |
11930 | input_location = EXPR_LOCATION (t); | |
7c34ced1 RH |
11931 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
11932 | current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t); | |
11933 | ||
5566b478 | 11934 | switch (TREE_CODE (t)) |
8d08fdba | 11935 | { |
325c3691 RH |
11936 | case STATEMENT_LIST: |
11937 | { | |
11938 | tree_stmt_iterator i; | |
11939 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
015c2c66 | 11940 | RECUR (tsi_stmt (i)); |
325c3691 RH |
11941 | break; |
11942 | } | |
11943 | ||
558475f0 | 11944 | case CTOR_INITIALIZER: |
c8094d83 | 11945 | finish_mem_initializers (tsubst_initializer_list |
2282d28d MM |
11946 | (TREE_OPERAND (t, 0), args)); |
11947 | break; | |
558475f0 | 11948 | |
5088b058 | 11949 | case RETURN_EXPR: |
015c2c66 | 11950 | finish_return_stmt (RECUR (TREE_OPERAND (t, 0))); |
5566b478 MS |
11951 | break; |
11952 | ||
11953 | case EXPR_STMT: | |
015c2c66 | 11954 | tmp = RECUR (EXPR_STMT_EXPR (t)); |
7c34ced1 RH |
11955 | if (EXPR_STMT_STMT_EXPR_RESULT (t)) |
11956 | finish_stmt_expr_expr (tmp, cur_stmt_expr); | |
11957 | else | |
11958 | finish_expr_stmt (tmp); | |
11959 | break; | |
5566b478 | 11960 | |
9da99f7d | 11961 | case USING_STMT: |
e90b1c4a | 11962 | do_using_directive (USING_STMT_NAMESPACE (t)); |
9da99f7d | 11963 | break; |
c8094d83 | 11964 | |
350fae66 | 11965 | case DECL_EXPR: |
5566b478 | 11966 | { |
fa2200cb | 11967 | tree decl, pattern_decl; |
e0942dcd | 11968 | tree init; |
5566b478 | 11969 | |
fa2200cb | 11970 | pattern_decl = decl = DECL_EXPR_DECL (t); |
acef433b MM |
11971 | if (TREE_CODE (decl) == LABEL_DECL) |
11972 | finish_label_decl (DECL_NAME (decl)); | |
fbfe8c9e NS |
11973 | else if (TREE_CODE (decl) == USING_DECL) |
11974 | { | |
98ed9dae | 11975 | tree scope = USING_DECL_SCOPE (decl); |
fbfe8c9e | 11976 | tree name = DECL_NAME (decl); |
22038b2c | 11977 | tree decl; |
c8094d83 | 11978 | |
e90b1c4a | 11979 | scope = tsubst (scope, args, complain, in_decl); |
22038b2c | 11980 | decl = lookup_qualified_name (scope, name, |
12483c9f NS |
11981 | /*is_type_p=*/false, |
11982 | /*complain=*/false); | |
8f78f01f | 11983 | if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) |
2b7a3abf | 11984 | qualified_name_lookup_error (scope, name, decl, input_location); |
22038b2c | 11985 | else |
ed5f054f | 11986 | do_local_using_decl (decl, scope, name); |
fbfe8c9e | 11987 | } |
acef433b MM |
11988 | else |
11989 | { | |
11990 | init = DECL_INITIAL (decl); | |
11991 | decl = tsubst (decl, args, complain, in_decl); | |
ce2e5191 NS |
11992 | if (decl != error_mark_node) |
11993 | { | |
0cbd7506 MS |
11994 | /* By marking the declaration as instantiated, we avoid |
11995 | trying to instantiate it. Since instantiate_decl can't | |
11996 | handle local variables, and since we've already done | |
11997 | all that needs to be done, that's the right thing to | |
11998 | do. */ | |
11999 | if (TREE_CODE (decl) == VAR_DECL) | |
12000 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
edebf865 MM |
12001 | if (TREE_CODE (decl) == VAR_DECL |
12002 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) | |
12003 | /* Anonymous aggregates are a special case. */ | |
12004 | finish_anon_union (decl); | |
c8094d83 | 12005 | else |
ed2fa432 | 12006 | { |
fa2200cb | 12007 | int const_init = false; |
edebf865 | 12008 | maybe_push_decl (decl); |
39703eb9 MM |
12009 | if (TREE_CODE (decl) == VAR_DECL |
12010 | && DECL_PRETTY_FUNCTION_P (decl)) | |
edebf865 MM |
12011 | { |
12012 | /* For __PRETTY_FUNCTION__ we have to adjust the | |
12013 | initializer. */ | |
12014 | const char *const name | |
12015 | = cxx_printable_name (current_function_decl, 2); | |
bb885938 | 12016 | init = cp_fname_init (name, &TREE_TYPE (decl)); |
edebf865 MM |
12017 | } |
12018 | else | |
268127ce DG |
12019 | { |
12020 | tree t = RECUR (init); | |
12021 | ||
12022 | if (init && !t) | |
462aa169 JM |
12023 | { |
12024 | /* If we had an initializer but it | |
12025 | instantiated to nothing, | |
12026 | value-initialize the object. This will | |
12027 | only occur when the initializer was a | |
12028 | pack expansion where the parameter packs | |
12029 | used in that expansion were of length | |
12030 | zero. */ | |
309714d4 JM |
12031 | init = build_value_init (TREE_TYPE (decl), |
12032 | complain); | |
462aa169 JM |
12033 | if (TREE_CODE (init) == AGGR_INIT_EXPR) |
12034 | init = get_target_expr (init); | |
12035 | } | |
268127ce DG |
12036 | else |
12037 | init = t; | |
12038 | } | |
12039 | ||
fa2200cb JM |
12040 | if (TREE_CODE (decl) == VAR_DECL) |
12041 | const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P | |
12042 | (pattern_decl)); | |
12043 | cp_finish_decl (decl, init, const_init, NULL_TREE, 0); | |
ed2fa432 | 12044 | } |
ce2e5191 | 12045 | } |
acef433b | 12046 | } |
fd10dd09 | 12047 | |
350fae66 | 12048 | /* A DECL_EXPR can also be used as an expression, in the condition |
325c3691 RH |
12049 | clause of an if/for/while construct. */ |
12050 | return decl; | |
5566b478 | 12051 | } |
8d08fdba | 12052 | |
5566b478 | 12053 | case FOR_STMT: |
7c34ced1 | 12054 | stmt = begin_for_stmt (); |
f9132eb7 | 12055 | RECUR (FOR_INIT_STMT (t)); |
7c34ced1 | 12056 | finish_for_init_stmt (stmt); |
015c2c66 | 12057 | tmp = RECUR (FOR_COND (t)); |
7c34ced1 | 12058 | finish_for_cond (tmp, stmt); |
015c2c66 | 12059 | tmp = RECUR (FOR_EXPR (t)); |
7c34ced1 | 12060 | finish_for_expr (tmp, stmt); |
015c2c66 | 12061 | RECUR (FOR_BODY (t)); |
7c34ced1 | 12062 | finish_for_stmt (stmt); |
5566b478 | 12063 | break; |
8d08fdba | 12064 | |
f9132eb7 RRC |
12065 | case RANGE_FOR_STMT: |
12066 | { | |
12067 | tree decl, expr; | |
12068 | stmt = begin_for_stmt (); | |
12069 | decl = RANGE_FOR_DECL (t); | |
12070 | decl = tsubst (decl, args, complain, in_decl); | |
12071 | maybe_push_decl (decl); | |
12072 | expr = RECUR (RANGE_FOR_EXPR (t)); | |
12073 | stmt = cp_convert_range_for (stmt, decl, expr); | |
12074 | RECUR (RANGE_FOR_BODY (t)); | |
12075 | finish_for_stmt (stmt); | |
12076 | } | |
12077 | break; | |
12078 | ||
5566b478 | 12079 | case WHILE_STMT: |
7c34ced1 | 12080 | stmt = begin_while_stmt (); |
015c2c66 | 12081 | tmp = RECUR (WHILE_COND (t)); |
7c34ced1 | 12082 | finish_while_stmt_cond (tmp, stmt); |
015c2c66 | 12083 | RECUR (WHILE_BODY (t)); |
7c34ced1 | 12084 | finish_while_stmt (stmt); |
5566b478 | 12085 | break; |
8d08fdba | 12086 | |
5566b478 | 12087 | case DO_STMT: |
7c34ced1 | 12088 | stmt = begin_do_stmt (); |
015c2c66 | 12089 | RECUR (DO_BODY (t)); |
7c34ced1 | 12090 | finish_do_body (stmt); |
015c2c66 | 12091 | tmp = RECUR (DO_COND (t)); |
7c34ced1 | 12092 | finish_do_stmt (tmp, stmt); |
5566b478 | 12093 | break; |
a0a33927 | 12094 | |
5566b478 | 12095 | case IF_STMT: |
7c34ced1 | 12096 | stmt = begin_if_stmt (); |
015c2c66 | 12097 | tmp = RECUR (IF_COND (t)); |
7c34ced1 | 12098 | finish_if_stmt_cond (tmp, stmt); |
015c2c66 | 12099 | RECUR (THEN_CLAUSE (t)); |
7c34ced1 | 12100 | finish_then_clause (stmt); |
8d08fdba | 12101 | |
7c34ced1 RH |
12102 | if (ELSE_CLAUSE (t)) |
12103 | { | |
12104 | begin_else_clause (stmt); | |
015c2c66 | 12105 | RECUR (ELSE_CLAUSE (t)); |
7c34ced1 RH |
12106 | finish_else_clause (stmt); |
12107 | } | |
12108 | ||
12109 | finish_if_stmt (stmt); | |
5566b478 | 12110 | break; |
8d08fdba | 12111 | |
5882f0f3 | 12112 | case BIND_EXPR: |
7c34ced1 RH |
12113 | if (BIND_EXPR_BODY_BLOCK (t)) |
12114 | stmt = begin_function_body (); | |
12115 | else | |
12116 | stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t) | |
12117 | ? BCS_TRY_BLOCK : 0); | |
ade3dc07 | 12118 | |
015c2c66 | 12119 | RECUR (BIND_EXPR_BODY (t)); |
ade3dc07 | 12120 | |
7c34ced1 RH |
12121 | if (BIND_EXPR_BODY_BLOCK (t)) |
12122 | finish_function_body (stmt); | |
12123 | else | |
12124 | finish_compound_stmt (stmt); | |
5566b478 | 12125 | break; |
8d08fdba | 12126 | |
5566b478 | 12127 | case BREAK_STMT: |
ad321293 | 12128 | finish_break_stmt (); |
5566b478 | 12129 | break; |
8d08fdba | 12130 | |
6467930b | 12131 | case CONTINUE_STMT: |
ad321293 | 12132 | finish_continue_stmt (); |
6467930b MS |
12133 | break; |
12134 | ||
5566b478 | 12135 | case SWITCH_STMT: |
7c34ced1 | 12136 | stmt = begin_switch_stmt (); |
015c2c66 | 12137 | tmp = RECUR (SWITCH_STMT_COND (t)); |
7c34ced1 | 12138 | finish_switch_cond (tmp, stmt); |
015c2c66 | 12139 | RECUR (SWITCH_STMT_BODY (t)); |
7c34ced1 | 12140 | finish_switch_stmt (stmt); |
5566b478 MS |
12141 | break; |
12142 | ||
8c161995 | 12143 | case CASE_LABEL_EXPR: |
c2255bc4 AH |
12144 | finish_case_label (EXPR_LOCATION (t), |
12145 | RECUR (CASE_LOW (t)), | |
015c2c66 | 12146 | RECUR (CASE_HIGH (t))); |
5566b478 MS |
12147 | break; |
12148 | ||
9e14e18f | 12149 | case LABEL_EXPR: |
5bca4e80 ILT |
12150 | { |
12151 | tree decl = LABEL_EXPR_LABEL (t); | |
12152 | tree label; | |
12153 | ||
12154 | label = finish_label_stmt (DECL_NAME (decl)); | |
12155 | if (DECL_ATTRIBUTES (decl) != NULL_TREE) | |
12156 | cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0); | |
12157 | } | |
5566b478 MS |
12158 | break; |
12159 | ||
9e14e18f | 12160 | case GOTO_EXPR: |
fd10dd09 JM |
12161 | tmp = GOTO_DESTINATION (t); |
12162 | if (TREE_CODE (tmp) != LABEL_DECL) | |
aa09da44 MM |
12163 | /* Computed goto's must be tsubst'd into. On the other hand, |
12164 | non-computed gotos must not be; the identifier in question | |
12165 | will have no binding. */ | |
015c2c66 | 12166 | tmp = RECUR (tmp); |
3fa56191 | 12167 | else |
fd10dd09 JM |
12168 | tmp = DECL_NAME (tmp); |
12169 | finish_goto_stmt (tmp); | |
ad321293 MM |
12170 | break; |
12171 | ||
e130a54b | 12172 | case ASM_EXPR: |
c87978aa | 12173 | tmp = finish_asm_stmt |
6de9cd9a | 12174 | (ASM_VOLATILE_P (t), |
015c2c66 | 12175 | RECUR (ASM_STRING (t)), |
f74dcfb7 JJ |
12176 | tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl), |
12177 | tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl), | |
1c384bf1 RH |
12178 | tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl), |
12179 | tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl)); | |
131263fa AP |
12180 | { |
12181 | tree asm_expr = tmp; | |
12182 | if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR) | |
12183 | asm_expr = TREE_OPERAND (asm_expr, 0); | |
12184 | ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t); | |
12185 | } | |
5566b478 | 12186 | break; |
faf5394a MS |
12187 | |
12188 | case TRY_BLOCK: | |
f1dedc31 | 12189 | if (CLEANUP_P (t)) |
62409b39 | 12190 | { |
57b52417 | 12191 | stmt = begin_try_block (); |
015c2c66 | 12192 | RECUR (TRY_STMTS (t)); |
62409b39 | 12193 | finish_cleanup_try_block (stmt); |
015c2c66 | 12194 | finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt); |
62409b39 | 12195 | } |
f1dedc31 MM |
12196 | else |
12197 | { | |
eaf6fb90 MM |
12198 | tree compound_stmt = NULL_TREE; |
12199 | ||
62409b39 | 12200 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 12201 | stmt = begin_function_try_block (&compound_stmt); |
62409b39 MM |
12202 | else |
12203 | stmt = begin_try_block (); | |
12204 | ||
015c2c66 | 12205 | RECUR (TRY_STMTS (t)); |
62409b39 MM |
12206 | |
12207 | if (FN_TRY_BLOCK_P (t)) | |
12208 | finish_function_try_block (stmt); | |
12209 | else | |
12210 | finish_try_block (stmt); | |
12211 | ||
015c2c66 | 12212 | RECUR (TRY_HANDLERS (t)); |
84df082b | 12213 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 12214 | finish_function_handler_sequence (stmt, compound_stmt); |
84df082b MM |
12215 | else |
12216 | finish_handler_sequence (stmt); | |
f1dedc31 | 12217 | } |
faf5394a | 12218 | break; |
c8094d83 | 12219 | |
faf5394a | 12220 | case HANDLER: |
b35d4555 | 12221 | { |
3a2419a7 | 12222 | tree decl = HANDLER_PARMS (t); |
b35d4555 | 12223 | |
3a2419a7 | 12224 | if (decl) |
b35d4555 | 12225 | { |
b35d4555 | 12226 | decl = tsubst (decl, args, complain, in_decl); |
f8191e64 MM |
12227 | /* Prevent instantiate_decl from trying to instantiate |
12228 | this variable. We've already done all that needs to be | |
12229 | done. */ | |
3a2419a7 VR |
12230 | if (decl != error_mark_node) |
12231 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
b35d4555 | 12232 | } |
3a2419a7 | 12233 | stmt = begin_handler (); |
1a6025b4 | 12234 | finish_handler_parms (decl, stmt); |
015c2c66 | 12235 | RECUR (HANDLER_BODY (t)); |
1a6025b4 | 12236 | finish_handler (stmt); |
b35d4555 | 12237 | } |
faf5394a MS |
12238 | break; |
12239 | ||
b87692e5 | 12240 | case TAG_DEFN: |
fd10dd09 | 12241 | tsubst (TREE_TYPE (t), args, complain, NULL_TREE); |
b87692e5 | 12242 | break; |
55a3debe DG |
12243 | |
12244 | case STATIC_ASSERT: | |
12245 | { | |
12246 | tree condition = | |
12247 | tsubst_expr (STATIC_ASSERT_CONDITION (t), | |
12248 | args, | |
12249 | complain, in_decl, | |
12250 | /*integral_constant_expression_p=*/true); | |
12251 | finish_static_assert (condition, | |
12252 | STATIC_ASSERT_MESSAGE (t), | |
12253 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
12254 | /*member_p=*/false); | |
12255 | } | |
12256 | break; | |
b87692e5 | 12257 | |
1799e5d5 RH |
12258 | case OMP_PARALLEL: |
12259 | tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), | |
12260 | args, complain, in_decl); | |
12261 | stmt = begin_omp_parallel (); | |
015c2c66 | 12262 | RECUR (OMP_PARALLEL_BODY (t)); |
761041be JJ |
12263 | OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt)) |
12264 | = OMP_PARALLEL_COMBINED (t); | |
1799e5d5 RH |
12265 | break; |
12266 | ||
a68ab351 JJ |
12267 | case OMP_TASK: |
12268 | tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), | |
12269 | args, complain, in_decl); | |
12270 | stmt = begin_omp_task (); | |
12271 | RECUR (OMP_TASK_BODY (t)); | |
12272 | finish_omp_task (tmp, stmt); | |
12273 | break; | |
12274 | ||
1799e5d5 RH |
12275 | case OMP_FOR: |
12276 | { | |
a68ab351 JJ |
12277 | tree clauses, body, pre_body; |
12278 | tree declv, initv, condv, incrv; | |
12279 | int i; | |
1799e5d5 RH |
12280 | |
12281 | clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), | |
12282 | args, complain, in_decl); | |
a68ab351 JJ |
12283 | declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); |
12284 | initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); | |
12285 | condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); | |
12286 | incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); | |
12287 | ||
12288 | for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++) | |
12289 | tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv, | |
12290 | &clauses, args, complain, in_decl, | |
12291 | integral_constant_expression_p); | |
1799e5d5 RH |
12292 | |
12293 | stmt = begin_omp_structured_block (); | |
12294 | ||
a68ab351 JJ |
12295 | for (i = 0; i < TREE_VEC_LENGTH (initv); i++) |
12296 | if (TREE_VEC_ELT (initv, i) == NULL | |
12297 | || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR) | |
12298 | TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i)); | |
12299 | else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i)))) | |
12300 | { | |
12301 | tree init = RECUR (TREE_VEC_ELT (initv, i)); | |
12302 | gcc_assert (init == TREE_VEC_ELT (declv, i)); | |
12303 | TREE_VEC_ELT (initv, i) = NULL_TREE; | |
12304 | } | |
12305 | else | |
12306 | { | |
12307 | tree decl_expr = TREE_VEC_ELT (initv, i); | |
12308 | tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr)); | |
12309 | gcc_assert (init != NULL); | |
12310 | TREE_VEC_ELT (initv, i) = RECUR (init); | |
12311 | DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL; | |
12312 | RECUR (decl_expr); | |
12313 | DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init; | |
12314 | } | |
12315 | ||
1799e5d5 | 12316 | pre_body = push_stmt_list (); |
015c2c66 | 12317 | RECUR (OMP_FOR_PRE_BODY (t)); |
1799e5d5 RH |
12318 | pre_body = pop_stmt_list (pre_body); |
12319 | ||
12320 | body = push_stmt_list (); | |
015c2c66 | 12321 | RECUR (OMP_FOR_BODY (t)); |
1799e5d5 RH |
12322 | body = pop_stmt_list (body); |
12323 | ||
a68ab351 JJ |
12324 | t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv, |
12325 | body, pre_body, clauses); | |
1799e5d5 RH |
12326 | |
12327 | add_stmt (finish_omp_structured_block (stmt)); | |
12328 | } | |
12329 | break; | |
12330 | ||
12331 | case OMP_SECTIONS: | |
12332 | case OMP_SINGLE: | |
12333 | tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl); | |
12334 | stmt = push_stmt_list (); | |
015c2c66 | 12335 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
12336 | stmt = pop_stmt_list (stmt); |
12337 | ||
12338 | t = copy_node (t); | |
12339 | OMP_BODY (t) = stmt; | |
12340 | OMP_CLAUSES (t) = tmp; | |
12341 | add_stmt (t); | |
12342 | break; | |
12343 | ||
12344 | case OMP_SECTION: | |
12345 | case OMP_CRITICAL: | |
12346 | case OMP_MASTER: | |
12347 | case OMP_ORDERED: | |
12348 | stmt = push_stmt_list (); | |
015c2c66 | 12349 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
12350 | stmt = pop_stmt_list (stmt); |
12351 | ||
12352 | t = copy_node (t); | |
12353 | OMP_BODY (t) = stmt; | |
12354 | add_stmt (t); | |
12355 | break; | |
12356 | ||
12357 | case OMP_ATOMIC: | |
239371f9 JJ |
12358 | gcc_assert (OMP_ATOMIC_DEPENDENT_P (t)); |
12359 | { | |
12360 | tree op1 = TREE_OPERAND (t, 1); | |
12361 | tree lhs = RECUR (TREE_OPERAND (op1, 0)); | |
12362 | tree rhs = RECUR (TREE_OPERAND (op1, 1)); | |
12363 | finish_omp_atomic (TREE_CODE (op1), lhs, rhs); | |
12364 | } | |
1799e5d5 RH |
12365 | break; |
12366 | ||
5d80a306 DG |
12367 | case EXPR_PACK_EXPANSION: |
12368 | error ("invalid use of pack expansion expression"); | |
12369 | return error_mark_node; | |
12370 | ||
12371 | case NONTYPE_ARGUMENT_PACK: | |
12372 | error ("use %<...%> to expand argument pack"); | |
12373 | return error_mark_node; | |
12374 | ||
5566b478 | 12375 | default: |
315fb5db | 12376 | gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); |
c8094d83 | 12377 | |
315fb5db | 12378 | return tsubst_copy_and_build (t, args, complain, in_decl, |
015c2c66 MM |
12379 | /*function_p=*/false, |
12380 | integral_constant_expression_p); | |
5566b478 | 12381 | } |
fd10dd09 | 12382 | |
325c3691 | 12383 | return NULL_TREE; |
015c2c66 | 12384 | #undef RECUR |
8d08fdba MS |
12385 | } |
12386 | ||
b3445994 MM |
12387 | /* T is a postfix-expression that is not being used in a function |
12388 | call. Return the substituted version of T. */ | |
12389 | ||
12390 | static tree | |
c8094d83 | 12391 | tsubst_non_call_postfix_expression (tree t, tree args, |
b3445994 MM |
12392 | tsubst_flags_t complain, |
12393 | tree in_decl) | |
12394 | { | |
12395 | if (TREE_CODE (t) == SCOPE_REF) | |
12396 | t = tsubst_qualified_id (t, args, complain, in_decl, | |
12397 | /*done=*/false, /*address_p=*/false); | |
12398 | else | |
12399 | t = tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
12400 | /*function_p=*/false, |
12401 | /*integral_constant_expression_p=*/false); | |
b3445994 MM |
12402 | |
12403 | return t; | |
12404 | } | |
12405 | ||
cc23546e | 12406 | /* Like tsubst but deals with expressions and performs semantic |
b3445994 | 12407 | analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */ |
cc23546e | 12408 | |
ee76b931 | 12409 | tree |
c8094d83 | 12410 | tsubst_copy_and_build (tree t, |
0cbd7506 MS |
12411 | tree args, |
12412 | tsubst_flags_t complain, | |
12413 | tree in_decl, | |
015c2c66 MM |
12414 | bool function_p, |
12415 | bool integral_constant_expression_p) | |
cc23546e | 12416 | { |
015c2c66 MM |
12417 | #define RECUR(NODE) \ |
12418 | tsubst_copy_and_build (NODE, args, complain, in_decl, \ | |
12419 | /*function_p=*/false, \ | |
12420 | integral_constant_expression_p) | |
b3445994 | 12421 | |
ee76b931 MM |
12422 | tree op1; |
12423 | ||
cc23546e JO |
12424 | if (t == NULL_TREE || t == error_mark_node) |
12425 | return t; | |
12426 | ||
12427 | switch (TREE_CODE (t)) | |
12428 | { | |
399dedb9 NS |
12429 | case USING_DECL: |
12430 | t = DECL_NAME (t); | |
852dcbdd | 12431 | /* Fall through. */ |
b3445994 | 12432 | case IDENTIFIER_NODE: |
cc23546e | 12433 | { |
b3445994 | 12434 | tree decl; |
b3445994 | 12435 | cp_id_kind idk; |
67c03833 | 12436 | bool non_integral_constant_expression_p; |
b3445994 MM |
12437 | const char *error_msg; |
12438 | ||
b3445994 | 12439 | if (IDENTIFIER_TYPENAME_P (t)) |
cc23546e | 12440 | { |
b3445994 MM |
12441 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
12442 | t = mangle_conv_op_name_for_type (new_type); | |
cc23546e | 12443 | } |
b3445994 MM |
12444 | |
12445 | /* Look up the name. */ | |
10e6657a | 12446 | decl = lookup_name (t); |
b3445994 MM |
12447 | |
12448 | /* By convention, expressions use ERROR_MARK_NODE to indicate | |
12449 | failure, not NULL_TREE. */ | |
12450 | if (decl == NULL_TREE) | |
12451 | decl = error_mark_node; | |
12452 | ||
10b1d5e7 | 12453 | decl = finish_id_expression (t, decl, NULL_TREE, |
b3445994 | 12454 | &idk, |
015c2c66 | 12455 | integral_constant_expression_p, |
67c03833 JM |
12456 | /*allow_non_integral_constant_expression_p=*/false, |
12457 | &non_integral_constant_expression_p, | |
02ed62dd MM |
12458 | /*template_p=*/false, |
12459 | /*done=*/true, | |
12460 | /*address_p=*/false, | |
12461 | /*template_arg_p=*/false, | |
2b7a3abf DS |
12462 | &error_msg, |
12463 | input_location); | |
b3445994 MM |
12464 | if (error_msg) |
12465 | error (error_msg); | |
12466 | if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE) | |
12467 | decl = unqualified_name_lookup_error (decl); | |
12468 | return decl; | |
cc23546e JO |
12469 | } |
12470 | ||
12471 | case TEMPLATE_ID_EXPR: | |
12472 | { | |
12473 | tree object; | |
be93747e | 12474 | tree templ = RECUR (TREE_OPERAND (t, 0)); |
bf12d54d NS |
12475 | tree targs = TREE_OPERAND (t, 1); |
12476 | ||
12477 | if (targs) | |
12478 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 12479 | |
be93747e | 12480 | if (TREE_CODE (templ) == COMPONENT_REF) |
cc23546e | 12481 | { |
be93747e KG |
12482 | object = TREE_OPERAND (templ, 0); |
12483 | templ = TREE_OPERAND (templ, 1); | |
cc23546e JO |
12484 | } |
12485 | else | |
12486 | object = NULL_TREE; | |
be93747e | 12487 | templ = lookup_template_function (templ, targs); |
c8094d83 | 12488 | |
cc23546e | 12489 | if (object) |
be93747e KG |
12490 | return build3 (COMPONENT_REF, TREE_TYPE (templ), |
12491 | object, templ, NULL_TREE); | |
cc23546e | 12492 | else |
be93747e | 12493 | return baselink_for_fns (templ); |
cc23546e JO |
12494 | } |
12495 | ||
12496 | case INDIRECT_REF: | |
db24eb1f NS |
12497 | { |
12498 | tree r = RECUR (TREE_OPERAND (t, 0)); | |
12499 | ||
12500 | if (REFERENCE_REF_P (t)) | |
12501 | { | |
e8c66fe0 NS |
12502 | /* A type conversion to reference type will be enclosed in |
12503 | such an indirect ref, but the substitution of the cast | |
12504 | will have also added such an indirect ref. */ | |
12505 | if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE) | |
12506 | r = convert_from_reference (r); | |
db24eb1f NS |
12507 | } |
12508 | else | |
dd865ef6 | 12509 | r = build_x_indirect_ref (r, RO_UNARY_STAR, complain); |
db24eb1f NS |
12510 | return r; |
12511 | } | |
cc23546e | 12512 | |
0da99d4e GB |
12513 | case NOP_EXPR: |
12514 | return build_nop | |
12515 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
12516 | RECUR (TREE_OPERAND (t, 0))); | |
12517 | ||
cc23546e | 12518 | case CAST_EXPR: |
cc23546e | 12519 | case REINTERPRET_CAST_EXPR: |
cc23546e | 12520 | case CONST_CAST_EXPR: |
cc23546e | 12521 | case DYNAMIC_CAST_EXPR: |
cc23546e | 12522 | case STATIC_CAST_EXPR: |
015c2c66 MM |
12523 | { |
12524 | tree type; | |
12525 | tree op; | |
12526 | ||
12527 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
12528 | if (integral_constant_expression_p | |
12529 | && !cast_valid_in_integral_constant_expression_p (type)) | |
12530 | { | |
5ade1ed2 DG |
12531 | if (complain & tf_error) |
12532 | error ("a cast to a type other than an integral or " | |
12533 | "enumeration type cannot appear in a constant-expression"); | |
015c2c66 MM |
12534 | return error_mark_node; |
12535 | } | |
12536 | ||
12537 | op = RECUR (TREE_OPERAND (t, 0)); | |
12538 | ||
12539 | switch (TREE_CODE (t)) | |
12540 | { | |
12541 | case CAST_EXPR: | |
5ade1ed2 | 12542 | return build_functional_cast (type, op, complain); |
015c2c66 | 12543 | case REINTERPRET_CAST_EXPR: |
5ade1ed2 | 12544 | return build_reinterpret_cast (type, op, complain); |
015c2c66 | 12545 | case CONST_CAST_EXPR: |
5ade1ed2 | 12546 | return build_const_cast (type, op, complain); |
015c2c66 | 12547 | case DYNAMIC_CAST_EXPR: |
5ade1ed2 | 12548 | return build_dynamic_cast (type, op, complain); |
015c2c66 | 12549 | case STATIC_CAST_EXPR: |
5ade1ed2 | 12550 | return build_static_cast (type, op, complain); |
015c2c66 MM |
12551 | default: |
12552 | gcc_unreachable (); | |
12553 | } | |
12554 | } | |
cc23546e | 12555 | |
cc23546e JO |
12556 | case POSTDECREMENT_EXPR: |
12557 | case POSTINCREMENT_EXPR: | |
b3445994 MM |
12558 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
12559 | args, complain, in_decl); | |
5ade1ed2 | 12560 | return build_x_unary_op (TREE_CODE (t), op1, complain); |
ee76b931 MM |
12561 | |
12562 | case PREDECREMENT_EXPR: | |
12563 | case PREINCREMENT_EXPR: | |
cc23546e JO |
12564 | case NEGATE_EXPR: |
12565 | case BIT_NOT_EXPR: | |
cc23546e | 12566 | case ABS_EXPR: |
d17811fd | 12567 | case TRUTH_NOT_EXPR: |
392e3d51 | 12568 | case UNARY_PLUS_EXPR: /* Unary + */ |
d17811fd MM |
12569 | case REALPART_EXPR: |
12570 | case IMAGPART_EXPR: | |
5ade1ed2 DG |
12571 | return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)), |
12572 | complain); | |
cc23546e | 12573 | |
cc23546e | 12574 | case ADDR_EXPR: |
ee76b931 | 12575 | op1 = TREE_OPERAND (t, 0); |
e90b1c4a JM |
12576 | if (TREE_CODE (op1) == LABEL_DECL) |
12577 | return finish_label_address_expr (DECL_NAME (op1), | |
12578 | EXPR_LOCATION (op1)); | |
ee76b931 | 12579 | if (TREE_CODE (op1) == SCOPE_REF) |
c8094d83 | 12580 | op1 = tsubst_qualified_id (op1, args, complain, in_decl, |
ee76b931 MM |
12581 | /*done=*/true, /*address_p=*/true); |
12582 | else | |
c8094d83 | 12583 | op1 = tsubst_non_call_postfix_expression (op1, args, complain, |
b3445994 | 12584 | in_decl); |
5ade1ed2 | 12585 | return build_x_unary_op (ADDR_EXPR, op1, complain); |
ee76b931 | 12586 | |
cc23546e JO |
12587 | case PLUS_EXPR: |
12588 | case MINUS_EXPR: | |
12589 | case MULT_EXPR: | |
12590 | case TRUNC_DIV_EXPR: | |
12591 | case CEIL_DIV_EXPR: | |
12592 | case FLOOR_DIV_EXPR: | |
12593 | case ROUND_DIV_EXPR: | |
12594 | case EXACT_DIV_EXPR: | |
12595 | case BIT_AND_EXPR: | |
cc23546e JO |
12596 | case BIT_IOR_EXPR: |
12597 | case BIT_XOR_EXPR: | |
12598 | case TRUNC_MOD_EXPR: | |
12599 | case FLOOR_MOD_EXPR: | |
12600 | case TRUTH_ANDIF_EXPR: | |
12601 | case TRUTH_ORIF_EXPR: | |
12602 | case TRUTH_AND_EXPR: | |
12603 | case TRUTH_OR_EXPR: | |
12604 | case RSHIFT_EXPR: | |
12605 | case LSHIFT_EXPR: | |
12606 | case RROTATE_EXPR: | |
12607 | case LROTATE_EXPR: | |
12608 | case EQ_EXPR: | |
12609 | case NE_EXPR: | |
12610 | case MAX_EXPR: | |
12611 | case MIN_EXPR: | |
12612 | case LE_EXPR: | |
12613 | case GE_EXPR: | |
12614 | case LT_EXPR: | |
12615 | case GT_EXPR: | |
12616 | case MEMBER_REF: | |
b3445994 | 12617 | case DOTSTAR_EXPR: |
cc23546e | 12618 | return build_x_binary_op |
c8094d83 | 12619 | (TREE_CODE (t), |
b3445994 | 12620 | RECUR (TREE_OPERAND (t, 0)), |
2a67bec2 ILT |
12621 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) |
12622 | ? ERROR_MARK | |
12623 | : TREE_CODE (TREE_OPERAND (t, 0))), | |
ec835fb2 | 12624 | RECUR (TREE_OPERAND (t, 1)), |
2a67bec2 ILT |
12625 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) |
12626 | ? ERROR_MARK | |
12627 | : TREE_CODE (TREE_OPERAND (t, 1))), | |
5ade1ed2 DG |
12628 | /*overloaded_p=*/NULL, |
12629 | complain); | |
cc23546e JO |
12630 | |
12631 | case SCOPE_REF: | |
ee76b931 MM |
12632 | return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, |
12633 | /*address_p=*/false); | |
cc23546e | 12634 | case ARRAY_REF: |
b3445994 MM |
12635 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
12636 | args, complain, in_decl); | |
493e377c | 12637 | return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain); |
c8094d83 | 12638 | |
cc23546e | 12639 | case SIZEOF_EXPR: |
5d80a306 | 12640 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) |
063cc99c | 12641 | return tsubst_copy (t, args, complain, in_decl); |
5d80a306 DG |
12642 | /* Fall through */ |
12643 | ||
cc23546e | 12644 | case ALIGNOF_EXPR: |
d17811fd MM |
12645 | op1 = TREE_OPERAND (t, 0); |
12646 | if (!args) | |
12647 | { | |
12648 | /* When there are no ARGS, we are trying to evaluate a | |
12649 | non-dependent expression from the parser. Trying to do | |
12650 | the substitutions may not work. */ | |
12651 | if (!TYPE_P (op1)) | |
12652 | op1 = TREE_TYPE (op1); | |
12653 | } | |
12654 | else | |
12655 | { | |
7d882b83 ILT |
12656 | ++cp_unevaluated_operand; |
12657 | ++c_inhibit_evaluation_warnings; | |
015c2c66 MM |
12658 | op1 = tsubst_copy_and_build (op1, args, complain, in_decl, |
12659 | /*function_p=*/false, | |
12660 | /*integral_constant_expression_p=*/false); | |
7d882b83 ILT |
12661 | --cp_unevaluated_operand; |
12662 | --c_inhibit_evaluation_warnings; | |
d17811fd | 12663 | } |
7a18b933 | 12664 | if (TYPE_P (op1)) |
5ade1ed2 DG |
12665 | return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), |
12666 | complain & tf_error); | |
d17811fd | 12667 | else |
5ade1ed2 DG |
12668 | return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t), |
12669 | complain & tf_error); | |
cc23546e | 12670 | |
b67bc44c NP |
12671 | case AT_ENCODE_EXPR: |
12672 | { | |
12673 | op1 = TREE_OPERAND (t, 0); | |
12674 | ++cp_unevaluated_operand; | |
12675 | ++c_inhibit_evaluation_warnings; | |
12676 | op1 = tsubst_copy_and_build (op1, args, complain, in_decl, | |
12677 | /*function_p=*/false, | |
12678 | /*integral_constant_expression_p=*/false); | |
12679 | --cp_unevaluated_operand; | |
12680 | --c_inhibit_evaluation_warnings; | |
12681 | return objc_build_encode_expr (op1); | |
12682 | } | |
12683 | ||
0a766368 JM |
12684 | case NOEXCEPT_EXPR: |
12685 | op1 = TREE_OPERAND (t, 0); | |
12686 | ++cp_unevaluated_operand; | |
12687 | ++c_inhibit_evaluation_warnings; | |
12688 | op1 = tsubst_copy_and_build (op1, args, complain, in_decl, | |
12689 | /*function_p=*/false, | |
12690 | /*integral_constant_expression_p=*/false); | |
12691 | --cp_unevaluated_operand; | |
12692 | --c_inhibit_evaluation_warnings; | |
59f9c2ed | 12693 | return finish_noexcept_expr (op1, complain); |
0a766368 | 12694 | |
cc23546e | 12695 | case MODOP_EXPR: |
e4c2c34b JM |
12696 | { |
12697 | tree r = build_x_modify_expr | |
12698 | (RECUR (TREE_OPERAND (t, 0)), | |
12699 | TREE_CODE (TREE_OPERAND (t, 1)), | |
5ade1ed2 DG |
12700 | RECUR (TREE_OPERAND (t, 2)), |
12701 | complain); | |
bcf9a914 JM |
12702 | /* TREE_NO_WARNING must be set if either the expression was |
12703 | parenthesized or it uses an operator such as >>= rather | |
12704 | than plain assignment. In the former case, it was already | |
12705 | set and must be copied. In the latter case, | |
12706 | build_x_modify_expr sets it and it must not be reset | |
12707 | here. */ | |
12708 | if (TREE_NO_WARNING (t)) | |
12709 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); | |
e4c2c34b JM |
12710 | return r; |
12711 | } | |
cc23546e JO |
12712 | |
12713 | case ARROW_EXPR: | |
b3445994 MM |
12714 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
12715 | args, complain, in_decl); | |
ee76b931 MM |
12716 | /* Remember that there was a reference to this entity. */ |
12717 | if (DECL_P (op1)) | |
12718 | mark_used (op1); | |
12719 | return build_x_arrow (op1); | |
cc23546e JO |
12720 | |
12721 | case NEW_EXPR: | |
268127ce | 12722 | { |
c166b898 | 12723 | tree placement = RECUR (TREE_OPERAND (t, 0)); |
268127ce | 12724 | tree init = RECUR (TREE_OPERAND (t, 3)); |
c166b898 ILT |
12725 | VEC(tree,gc) *placement_vec; |
12726 | VEC(tree,gc) *init_vec; | |
12727 | tree ret; | |
268127ce | 12728 | |
c166b898 ILT |
12729 | if (placement == NULL_TREE) |
12730 | placement_vec = NULL; | |
12731 | else | |
12732 | { | |
12733 | placement_vec = make_tree_vector (); | |
12734 | for (; placement != NULL_TREE; placement = TREE_CHAIN (placement)) | |
12735 | VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement)); | |
12736 | } | |
268127ce | 12737 | |
c166b898 ILT |
12738 | /* If there was an initializer in the original tree, but it |
12739 | instantiated to an empty list, then we should pass a | |
12740 | non-NULL empty vector to tell build_new that it was an | |
12741 | empty initializer() rather than no initializer. This can | |
12742 | only happen when the initializer is a pack expansion whose | |
12743 | parameter packs are of length zero. */ | |
12744 | if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE) | |
12745 | init_vec = NULL; | |
12746 | else | |
12747 | { | |
12748 | init_vec = make_tree_vector (); | |
12749 | if (init == void_zero_node) | |
12750 | gcc_assert (init_vec != NULL); | |
12751 | else | |
12752 | { | |
12753 | for (; init != NULL_TREE; init = TREE_CHAIN (init)) | |
12754 | VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init)); | |
12755 | } | |
12756 | } | |
12757 | ||
12758 | ret = build_new (&placement_vec, | |
e90b1c4a | 12759 | tsubst (TREE_OPERAND (t, 1), args, complain, in_decl), |
c166b898 ILT |
12760 | RECUR (TREE_OPERAND (t, 2)), |
12761 | &init_vec, | |
12762 | NEW_EXPR_USE_GLOBAL (t), | |
12763 | complain); | |
12764 | ||
12765 | if (placement_vec != NULL) | |
12766 | release_tree_vector (placement_vec); | |
12767 | if (init_vec != NULL) | |
12768 | release_tree_vector (init_vec); | |
12769 | ||
12770 | return ret; | |
268127ce | 12771 | } |
cc23546e JO |
12772 | |
12773 | case DELETE_EXPR: | |
12774 | return delete_sanity | |
b3445994 MM |
12775 | (RECUR (TREE_OPERAND (t, 0)), |
12776 | RECUR (TREE_OPERAND (t, 1)), | |
cc23546e JO |
12777 | DELETE_EXPR_USE_VEC (t), |
12778 | DELETE_EXPR_USE_GLOBAL (t)); | |
12779 | ||
12780 | case COMPOUND_EXPR: | |
b3445994 | 12781 | return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)), |
5ade1ed2 DG |
12782 | RECUR (TREE_OPERAND (t, 1)), |
12783 | complain); | |
cc23546e | 12784 | |
ee76b931 | 12785 | case CALL_EXPR: |
cc23546e | 12786 | { |
ee76b931 | 12787 | tree function; |
c166b898 ILT |
12788 | VEC(tree,gc) *call_args; |
12789 | unsigned int nargs, i; | |
ee76b931 | 12790 | bool qualified_p; |
ee935db4 | 12791 | bool koenig_p; |
c166b898 | 12792 | tree ret; |
ee76b931 | 12793 | |
5039610b | 12794 | function = CALL_EXPR_FN (t); |
6d80c4b9 MM |
12795 | /* When we parsed the expression, we determined whether or |
12796 | not Koenig lookup should be performed. */ | |
12797 | koenig_p = KOENIG_LOOKUP_P (t); | |
ee76b931 | 12798 | if (TREE_CODE (function) == SCOPE_REF) |
cc23546e | 12799 | { |
ee76b931 MM |
12800 | qualified_p = true; |
12801 | function = tsubst_qualified_id (function, args, complain, in_decl, | |
c8094d83 | 12802 | /*done=*/false, |
ee76b931 | 12803 | /*address_p=*/false); |
cc23546e | 12804 | } |
ee76b931 | 12805 | else |
cc23546e | 12806 | { |
61e71a9e NS |
12807 | if (TREE_CODE (function) == COMPONENT_REF) |
12808 | { | |
12809 | tree op = TREE_OPERAND (function, 1); | |
12810 | ||
12811 | qualified_p = (TREE_CODE (op) == SCOPE_REF | |
12812 | || (BASELINK_P (op) | |
12813 | && BASELINK_QUALIFIED_P (op))); | |
12814 | } | |
12815 | else | |
12816 | qualified_p = false; | |
3db45ab5 | 12817 | |
c8094d83 | 12818 | function = tsubst_copy_and_build (function, args, complain, |
b3445994 | 12819 | in_decl, |
015c2c66 MM |
12820 | !qualified_p, |
12821 | integral_constant_expression_p); | |
12822 | ||
6d80c4b9 MM |
12823 | if (BASELINK_P (function)) |
12824 | qualified_p = true; | |
cc23546e | 12825 | } |
cc23546e | 12826 | |
c166b898 ILT |
12827 | nargs = call_expr_nargs (t); |
12828 | call_args = make_tree_vector (); | |
12829 | for (i = 0; i < nargs; ++i) | |
12830 | { | |
12831 | tree arg = CALL_EXPR_ARG (t, i); | |
12832 | ||
12833 | if (!PACK_EXPANSION_P (arg)) | |
12834 | VEC_safe_push (tree, gc, call_args, | |
12835 | RECUR (CALL_EXPR_ARG (t, i))); | |
12836 | else | |
12837 | { | |
12838 | /* Expand the pack expansion and push each entry onto | |
12839 | CALL_ARGS. */ | |
12840 | arg = tsubst_pack_expansion (arg, args, complain, in_decl); | |
12841 | if (TREE_CODE (arg) == TREE_VEC) | |
12842 | { | |
12843 | unsigned int len, j; | |
12844 | ||
12845 | len = TREE_VEC_LENGTH (arg); | |
12846 | for (j = 0; j < len; ++j) | |
12847 | { | |
12848 | tree value = TREE_VEC_ELT (arg, j); | |
12849 | if (value != NULL_TREE) | |
12850 | value = convert_from_reference (value); | |
12851 | VEC_safe_push (tree, gc, call_args, value); | |
12852 | } | |
12853 | } | |
12854 | else | |
12855 | { | |
12856 | /* A partial substitution. Add one entry. */ | |
12857 | VEC_safe_push (tree, gc, call_args, arg); | |
12858 | } | |
12859 | } | |
12860 | } | |
676e33ca MM |
12861 | |
12862 | /* We do not perform argument-dependent lookup if normal | |
12863 | lookup finds a non-function, in accordance with the | |
12864 | expected resolution of DR 218. */ | |
ee935db4 | 12865 | if (koenig_p |
44370687 MM |
12866 | && ((is_overloaded_fn (function) |
12867 | /* If lookup found a member function, the Koenig lookup is | |
12868 | not appropriate, even if an unqualified-name was used | |
12869 | to denote the function. */ | |
12870 | && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) | |
5094a795 JM |
12871 | || TREE_CODE (function) == IDENTIFIER_NODE) |
12872 | /* Only do this when substitution turns a dependent call | |
12873 | into a non-dependent call. */ | |
68f7159d JM |
12874 | && type_dependent_expression_p_push (t) |
12875 | && !any_type_dependent_arguments_p (call_args)) | |
f9132eb7 | 12876 | function = perform_koenig_lookup (function, call_args, false); |
6d80c4b9 MM |
12877 | |
12878 | if (TREE_CODE (function) == IDENTIFIER_NODE) | |
b3445994 | 12879 | { |
6d80c4b9 | 12880 | unqualified_name_lookup_error (function); |
68f7159d JM |
12881 | release_tree_vector (call_args); |
12882 | return error_mark_node; | |
b3445994 MM |
12883 | } |
12884 | ||
12885 | /* Remember that there was a reference to this entity. */ | |
12886 | if (DECL_P (function)) | |
12887 | mark_used (function); | |
12888 | ||
d17811fd | 12889 | if (TREE_CODE (function) == OFFSET_REF) |
c166b898 ILT |
12890 | ret = build_offset_ref_call_from_tree (function, &call_args); |
12891 | else if (TREE_CODE (function) == COMPONENT_REF) | |
9f880ef9 | 12892 | { |
68f7159d JM |
12893 | tree instance = TREE_OPERAND (function, 0); |
12894 | tree fn = TREE_OPERAND (function, 1); | |
12895 | ||
12896 | if (processing_template_decl | |
12897 | && (type_dependent_expression_p (instance) | |
12898 | || (!BASELINK_P (fn) | |
12899 | && TREE_CODE (fn) != FIELD_DECL) | |
12900 | || type_dependent_expression_p (fn) | |
12901 | || any_type_dependent_arguments_p (call_args))) | |
12902 | ret = build_nt_call_vec (function, call_args); | |
12903 | else if (!BASELINK_P (fn)) | |
c166b898 | 12904 | ret = finish_call_expr (function, &call_args, |
9f880ef9 | 12905 | /*disallow_virtual=*/false, |
5ade1ed2 DG |
12906 | /*koenig_p=*/false, |
12907 | complain); | |
9f880ef9 | 12908 | else |
c166b898 | 12909 | ret = (build_new_method_call |
68f7159d | 12910 | (instance, fn, |
c166b898 | 12911 | &call_args, NULL_TREE, |
63c9a190 | 12912 | qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL, |
5ade1ed2 DG |
12913 | /*fn_p=*/NULL, |
12914 | complain)); | |
9f880ef9 | 12915 | } |
c166b898 ILT |
12916 | else |
12917 | ret = finish_call_expr (function, &call_args, | |
12918 | /*disallow_virtual=*/qualified_p, | |
12919 | koenig_p, | |
12920 | complain); | |
12921 | ||
12922 | release_tree_vector (call_args); | |
12923 | ||
12924 | return ret; | |
cc23546e JO |
12925 | } |
12926 | ||
12927 | case COND_EXPR: | |
12928 | return build_x_conditional_expr | |
b3445994 MM |
12929 | (RECUR (TREE_OPERAND (t, 0)), |
12930 | RECUR (TREE_OPERAND (t, 1)), | |
5ade1ed2 DG |
12931 | RECUR (TREE_OPERAND (t, 2)), |
12932 | complain); | |
cc23546e JO |
12933 | |
12934 | case PSEUDO_DTOR_EXPR: | |
c8094d83 | 12935 | return finish_pseudo_destructor_expr |
b3445994 MM |
12936 | (RECUR (TREE_OPERAND (t, 0)), |
12937 | RECUR (TREE_OPERAND (t, 1)), | |
12938 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
12939 | |
12940 | case TREE_LIST: | |
12941 | { | |
12942 | tree purpose, value, chain; | |
12943 | ||
12944 | if (t == void_list_node) | |
12945 | return t; | |
12946 | ||
5d80a306 DG |
12947 | if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t))) |
12948 | || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))) | |
12949 | { | |
12950 | /* We have pack expansions, so expand those and | |
12951 | create a new list out of it. */ | |
12952 | tree purposevec = NULL_TREE; | |
12953 | tree valuevec = NULL_TREE; | |
12954 | tree chain; | |
12955 | int i, len = -1; | |
12956 | ||
12957 | /* Expand the argument expressions. */ | |
12958 | if (TREE_PURPOSE (t)) | |
12959 | purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args, | |
12960 | complain, in_decl); | |
12961 | if (TREE_VALUE (t)) | |
12962 | valuevec = tsubst_pack_expansion (TREE_VALUE (t), args, | |
12963 | complain, in_decl); | |
12964 | ||
12965 | /* Build the rest of the list. */ | |
12966 | chain = TREE_CHAIN (t); | |
12967 | if (chain && chain != void_type_node) | |
12968 | chain = RECUR (chain); | |
12969 | ||
12970 | /* Determine the number of arguments. */ | |
12971 | if (purposevec && TREE_CODE (purposevec) == TREE_VEC) | |
12972 | { | |
12973 | len = TREE_VEC_LENGTH (purposevec); | |
12974 | gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec)); | |
12975 | } | |
12976 | else if (TREE_CODE (valuevec) == TREE_VEC) | |
12977 | len = TREE_VEC_LENGTH (valuevec); | |
12978 | else | |
12979 | { | |
12980 | /* Since we only performed a partial substitution into | |
12981 | the argument pack, we only return a single list | |
12982 | node. */ | |
12983 | if (purposevec == TREE_PURPOSE (t) | |
12984 | && valuevec == TREE_VALUE (t) | |
12985 | && chain == TREE_CHAIN (t)) | |
12986 | return t; | |
12987 | ||
12988 | return tree_cons (purposevec, valuevec, chain); | |
12989 | } | |
12990 | ||
12991 | /* Convert the argument vectors into a TREE_LIST */ | |
12992 | i = len; | |
12993 | while (i > 0) | |
12994 | { | |
12995 | /* Grab the Ith values. */ | |
12996 | i--; | |
12997 | purpose = purposevec ? TREE_VEC_ELT (purposevec, i) | |
12998 | : NULL_TREE; | |
12999 | value | |
13000 | = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i)) | |
13001 | : NULL_TREE; | |
13002 | ||
13003 | /* Build the list (backwards). */ | |
13004 | chain = tree_cons (purpose, value, chain); | |
13005 | } | |
13006 | ||
13007 | return chain; | |
13008 | } | |
13009 | ||
cc23546e JO |
13010 | purpose = TREE_PURPOSE (t); |
13011 | if (purpose) | |
b3445994 | 13012 | purpose = RECUR (purpose); |
cc23546e JO |
13013 | value = TREE_VALUE (t); |
13014 | if (value) | |
b3445994 | 13015 | value = RECUR (value); |
cc23546e JO |
13016 | chain = TREE_CHAIN (t); |
13017 | if (chain && chain != void_type_node) | |
b3445994 | 13018 | chain = RECUR (chain); |
cc23546e JO |
13019 | if (purpose == TREE_PURPOSE (t) |
13020 | && value == TREE_VALUE (t) | |
13021 | && chain == TREE_CHAIN (t)) | |
13022 | return t; | |
13023 | return tree_cons (purpose, value, chain); | |
13024 | } | |
13025 | ||
13026 | case COMPONENT_REF: | |
13027 | { | |
ee76b931 | 13028 | tree object; |
3897c0aa | 13029 | tree object_type; |
ee76b931 MM |
13030 | tree member; |
13031 | ||
b3445994 MM |
13032 | object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
13033 | args, complain, in_decl); | |
ee76b931 MM |
13034 | /* Remember that there was a reference to this entity. */ |
13035 | if (DECL_P (object)) | |
13036 | mark_used (object); | |
3897c0aa | 13037 | object_type = TREE_TYPE (object); |
ee76b931 MM |
13038 | |
13039 | member = TREE_OPERAND (t, 1); | |
13040 | if (BASELINK_P (member)) | |
c8094d83 | 13041 | member = tsubst_baselink (member, |
ee76b931 MM |
13042 | non_reference (TREE_TYPE (object)), |
13043 | args, complain, in_decl); | |
13044 | else | |
13045 | member = tsubst_copy (member, args, complain, in_decl); | |
bad1f462 KL |
13046 | if (member == error_mark_node) |
13047 | return error_mark_node; | |
3897c0aa MM |
13048 | |
13049 | if (object_type && !CLASS_TYPE_P (object_type)) | |
cc23546e | 13050 | { |
3d9b2eb6 JJ |
13051 | if (SCALAR_TYPE_P (object_type)) |
13052 | { | |
13053 | tree s = NULL_TREE; | |
13054 | tree dtor = member; | |
13055 | ||
13056 | if (TREE_CODE (dtor) == SCOPE_REF) | |
13057 | { | |
13058 | s = TREE_OPERAND (dtor, 0); | |
13059 | dtor = TREE_OPERAND (dtor, 1); | |
13060 | } | |
13061 | if (TREE_CODE (dtor) == BIT_NOT_EXPR) | |
13062 | { | |
13063 | dtor = TREE_OPERAND (dtor, 0); | |
13064 | if (TYPE_P (dtor)) | |
13065 | return finish_pseudo_destructor_expr (object, s, dtor); | |
13066 | } | |
13067 | } | |
cc23546e JO |
13068 | } |
13069 | else if (TREE_CODE (member) == SCOPE_REF | |
13070 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) | |
13071 | { | |
13072 | tree tmpl; | |
13073 | tree args; | |
c8094d83 | 13074 | |
cc23546e JO |
13075 | /* Lookup the template functions now that we know what the |
13076 | scope is. */ | |
13077 | tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); | |
13078 | args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); | |
c8094d83 | 13079 | member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl, |
12483c9f NS |
13080 | /*is_type_p=*/false, |
13081 | /*complain=*/false); | |
cc23546e | 13082 | if (BASELINK_P (member)) |
44370687 | 13083 | { |
c8094d83 | 13084 | BASELINK_FUNCTIONS (member) |
44370687 MM |
13085 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), |
13086 | args); | |
c8094d83 MS |
13087 | member = (adjust_result_of_qualified_name_lookup |
13088 | (member, BINFO_TYPE (BASELINK_BINFO (member)), | |
3897c0aa | 13089 | object_type)); |
44370687 | 13090 | } |
cc23546e JO |
13091 | else |
13092 | { | |
2b7a3abf DS |
13093 | qualified_name_lookup_error (object_type, tmpl, member, |
13094 | input_location); | |
cc23546e JO |
13095 | return error_mark_node; |
13096 | } | |
13097 | } | |
2436b51f MM |
13098 | else if (TREE_CODE (member) == SCOPE_REF |
13099 | && !CLASS_TYPE_P (TREE_OPERAND (member, 0)) | |
13100 | && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL) | |
13101 | { | |
13102 | if (complain & tf_error) | |
13103 | { | |
13104 | if (TYPE_P (TREE_OPERAND (member, 0))) | |
c8094d83 | 13105 | error ("%qT is not a class or namespace", |
2436b51f MM |
13106 | TREE_OPERAND (member, 0)); |
13107 | else | |
c8094d83 | 13108 | error ("%qD is not a class or namespace", |
2436b51f MM |
13109 | TREE_OPERAND (member, 0)); |
13110 | } | |
13111 | return error_mark_node; | |
13112 | } | |
a3f10e50 NS |
13113 | else if (TREE_CODE (member) == FIELD_DECL) |
13114 | return finish_non_static_data_member (member, object, NULL_TREE); | |
cc23546e | 13115 | |
02ed62dd | 13116 | return finish_class_member_access_expr (object, member, |
5ade1ed2 DG |
13117 | /*template_p=*/false, |
13118 | complain); | |
cc23546e JO |
13119 | } |
13120 | ||
13121 | case THROW_EXPR: | |
13122 | return build_throw | |
b3445994 | 13123 | (RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
13124 | |
13125 | case CONSTRUCTOR: | |
13126 | { | |
4038c495 GB |
13127 | VEC(constructor_elt,gc) *n; |
13128 | constructor_elt *ce; | |
13129 | unsigned HOST_WIDE_INT idx; | |
cc23546e | 13130 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
4038c495 | 13131 | bool process_index_p; |
5d80a306 DG |
13132 | int newlen; |
13133 | bool need_copy_p = false; | |
09357846 | 13134 | tree r; |
cc23546e | 13135 | |
a97728cf VR |
13136 | if (type == error_mark_node) |
13137 | return error_mark_node; | |
13138 | ||
cc23546e JO |
13139 | /* digest_init will do the wrong thing if we let it. */ |
13140 | if (type && TYPE_PTRMEMFUNC_P (type)) | |
13141 | return t; | |
13142 | ||
4038c495 | 13143 | /* We do not want to process the index of aggregate |
cc23546e JO |
13144 | initializers as they are identifier nodes which will be |
13145 | looked up by digest_init. */ | |
9e1e64ec | 13146 | process_index_p = !(type && MAYBE_CLASS_TYPE_P (type)); |
c8094d83 | 13147 | |
4038c495 | 13148 | n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t)); |
5d80a306 | 13149 | newlen = VEC_length (constructor_elt, n); |
ac47786e | 13150 | FOR_EACH_VEC_ELT (constructor_elt, n, idx, ce) |
4038c495 GB |
13151 | { |
13152 | if (ce->index && process_index_p) | |
13153 | ce->index = RECUR (ce->index); | |
5d80a306 DG |
13154 | |
13155 | if (PACK_EXPANSION_P (ce->value)) | |
13156 | { | |
13157 | /* Substitute into the pack expansion. */ | |
13158 | ce->value = tsubst_pack_expansion (ce->value, args, complain, | |
13159 | in_decl); | |
13160 | ||
4745e4eb JJ |
13161 | if (ce->value == error_mark_node) |
13162 | ; | |
13163 | else if (TREE_VEC_LENGTH (ce->value) == 1) | |
5d80a306 DG |
13164 | /* Just move the argument into place. */ |
13165 | ce->value = TREE_VEC_ELT (ce->value, 0); | |
13166 | else | |
13167 | { | |
13168 | /* Update the length of the final CONSTRUCTOR | |
13169 | arguments vector, and note that we will need to | |
13170 | copy.*/ | |
13171 | newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1; | |
13172 | need_copy_p = true; | |
13173 | } | |
13174 | } | |
13175 | else | |
13176 | ce->value = RECUR (ce->value); | |
cc23546e | 13177 | } |
c8094d83 | 13178 | |
5d80a306 DG |
13179 | if (need_copy_p) |
13180 | { | |
13181 | VEC(constructor_elt,gc) *old_n = n; | |
13182 | ||
13183 | n = VEC_alloc (constructor_elt, gc, newlen); | |
ac47786e | 13184 | FOR_EACH_VEC_ELT (constructor_elt, old_n, idx, ce) |
5d80a306 DG |
13185 | { |
13186 | if (TREE_CODE (ce->value) == TREE_VEC) | |
13187 | { | |
13188 | int i, len = TREE_VEC_LENGTH (ce->value); | |
13189 | for (i = 0; i < len; ++i) | |
13190 | CONSTRUCTOR_APPEND_ELT (n, 0, | |
13191 | TREE_VEC_ELT (ce->value, i)); | |
13192 | } | |
13193 | else | |
13194 | CONSTRUCTOR_APPEND_ELT (n, 0, ce->value); | |
13195 | } | |
13196 | } | |
13197 | ||
09357846 | 13198 | r = build_constructor (init_list_type_node, n); |
27fb09b7 | 13199 | CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t); |
09357846 | 13200 | |
05b22df9 | 13201 | if (TREE_HAS_CONSTRUCTOR (t)) |
09357846 | 13202 | return finish_compound_literal (type, r); |
cc23546e | 13203 | |
9c8b5215 | 13204 | TREE_TYPE (r) = type; |
09357846 | 13205 | return r; |
cc23546e JO |
13206 | } |
13207 | ||
13208 | case TYPEID_EXPR: | |
13209 | { | |
e90b1c4a | 13210 | tree operand_0 = TREE_OPERAND (t, 0); |
cc23546e | 13211 | if (TYPE_P (operand_0)) |
e90b1c4a JM |
13212 | { |
13213 | operand_0 = tsubst (operand_0, args, complain, in_decl); | |
13214 | return get_typeid (operand_0); | |
13215 | } | |
13216 | else | |
13217 | { | |
13218 | operand_0 = RECUR (operand_0); | |
13219 | return build_typeid (operand_0); | |
13220 | } | |
cc23546e JO |
13221 | } |
13222 | ||
cc23546e | 13223 | case VAR_DECL: |
db24eb1f NS |
13224 | if (!args) |
13225 | return t; | |
13226 | /* Fall through */ | |
c8094d83 | 13227 | |
db24eb1f NS |
13228 | case PARM_DECL: |
13229 | { | |
13230 | tree r = tsubst_copy (t, args, complain, in_decl); | |
c8094d83 | 13231 | |
db24eb1f NS |
13232 | if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE) |
13233 | /* If the original type was a reference, we'll be wrapped in | |
13234 | the appropriate INDIRECT_REF. */ | |
13235 | r = convert_from_reference (r); | |
13236 | return r; | |
13237 | } | |
cc23546e JO |
13238 | |
13239 | case VA_ARG_EXPR: | |
b3445994 | 13240 | return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)), |
c8094d83 | 13241 | tsubst_copy (TREE_TYPE (t), args, complain, |
b3445994 | 13242 | in_decl)); |
cc23546e | 13243 | |
4bceb077 | 13244 | case OFFSETOF_EXPR: |
c291f8b1 | 13245 | return finish_offsetof (RECUR (TREE_OPERAND (t, 0))); |
4bceb077 | 13246 | |
cb68ec50 PC |
13247 | case TRAIT_EXPR: |
13248 | { | |
13249 | tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args, | |
13250 | complain, in_decl); | |
13251 | ||
13252 | tree type2 = TRAIT_EXPR_TYPE2 (t); | |
13253 | if (type2) | |
13254 | type2 = tsubst_copy (type2, args, complain, in_decl); | |
13255 | ||
13256 | return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2); | |
13257 | } | |
13258 | ||
0fe0caa6 RH |
13259 | case STMT_EXPR: |
13260 | { | |
13261 | tree old_stmt_expr = cur_stmt_expr; | |
13262 | tree stmt_expr = begin_stmt_expr (); | |
13263 | ||
13264 | cur_stmt_expr = stmt_expr; | |
015c2c66 MM |
13265 | tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl, |
13266 | integral_constant_expression_p); | |
0fe0caa6 RH |
13267 | stmt_expr = finish_stmt_expr (stmt_expr, false); |
13268 | cur_stmt_expr = old_stmt_expr; | |
13269 | ||
9af66ed1 DS |
13270 | /* If the resulting list of expression statement is empty, |
13271 | fold it further into void_zero_node. */ | |
489df541 DS |
13272 | if (empty_expr_stmt_p (stmt_expr)) |
13273 | stmt_expr = void_zero_node; | |
9af66ed1 | 13274 | |
0fe0caa6 RH |
13275 | return stmt_expr; |
13276 | } | |
13277 | ||
a5201a91 MM |
13278 | case CONST_DECL: |
13279 | t = tsubst_copy (t, args, complain, in_decl); | |
13280 | /* As in finish_id_expression, we resolve enumeration constants | |
13281 | to their underlying values. */ | |
13282 | if (TREE_CODE (t) == CONST_DECL) | |
6193b8b7 DJ |
13283 | { |
13284 | used_types_insert (TREE_TYPE (t)); | |
13285 | return DECL_INITIAL (t); | |
13286 | } | |
a5201a91 MM |
13287 | return t; |
13288 | ||
d5f4eddd JM |
13289 | case LAMBDA_EXPR: |
13290 | { | |
13291 | tree r = build_lambda_expr (); | |
13292 | ||
13293 | tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE); | |
13294 | TREE_TYPE (r) = type; | |
13295 | CLASSTYPE_LAMBDA_EXPR (type) = r; | |
13296 | ||
0bbe864d JM |
13297 | LAMBDA_EXPR_LOCATION (r) |
13298 | = LAMBDA_EXPR_LOCATION (t); | |
d5f4eddd JM |
13299 | LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r) |
13300 | = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t); | |
13301 | LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t); | |
13302 | LAMBDA_EXPR_DISCRIMINATOR (r) | |
13303 | = (LAMBDA_EXPR_DISCRIMINATOR (t)); | |
13304 | LAMBDA_EXPR_CAPTURE_LIST (r) | |
13305 | = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t)); | |
13306 | LAMBDA_EXPR_THIS_CAPTURE (r) | |
13307 | = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t)); | |
13308 | LAMBDA_EXPR_EXTRA_SCOPE (r) | |
13309 | = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t)); | |
13310 | ||
13311 | /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */ | |
13312 | determine_visibility (TYPE_NAME (type)); | |
13313 | /* Now that we know visibility, instantiate the type so we have a | |
13314 | declaration of the op() for later calls to lambda_function. */ | |
13315 | complete_type (type); | |
13316 | ||
13317 | type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl); | |
13318 | if (type) | |
13319 | apply_lambda_return_type (r, type); | |
13320 | ||
13321 | return build_lambda_object (r); | |
13322 | } | |
13323 | ||
9c8b5215 JM |
13324 | case TARGET_EXPR: |
13325 | /* We can get here for a constant initializer of non-dependent type. | |
13326 | FIXME stop folding in cp_parser_initializer_clause. */ | |
13327 | gcc_assert (TREE_CONSTANT (t)); | |
13328 | return get_target_expr (RECUR (TARGET_EXPR_INITIAL (t))); | |
13329 | ||
cc23546e | 13330 | default: |
e58a9aa1 ZL |
13331 | /* Handle Objective-C++ constructs, if appropriate. */ |
13332 | { | |
13333 | tree subst | |
13334 | = objcp_tsubst_copy_and_build (t, args, complain, | |
13335 | in_decl, /*function_p=*/false); | |
13336 | if (subst) | |
13337 | return subst; | |
13338 | } | |
cc23546e JO |
13339 | return tsubst_copy (t, args, complain, in_decl); |
13340 | } | |
b3445994 MM |
13341 | |
13342 | #undef RECUR | |
cc23546e JO |
13343 | } |
13344 | ||
3e4a3562 | 13345 | /* Verify that the instantiated ARGS are valid. For type arguments, |
1eee69dd | 13346 | make sure that the type's linkage is ok. For non-type arguments, |
34cd5ae7 | 13347 | make sure they are constants if they are integral or enumerations. |
9bcb9aae | 13348 | Emit an error under control of COMPLAIN, and return TRUE on error. */ |
3e4a3562 NS |
13349 | |
13350 | static bool | |
ffaf1e05 | 13351 | check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain) |
3e4a3562 | 13352 | { |
ffaf1e05 JM |
13353 | if (ARGUMENT_PACK_P (t)) |
13354 | { | |
13355 | tree vec = ARGUMENT_PACK_ARGS (t); | |
13356 | int len = TREE_VEC_LENGTH (vec); | |
13357 | bool result = false; | |
13358 | int i; | |
3e4a3562 | 13359 | |
ffaf1e05 JM |
13360 | for (i = 0; i < len; ++i) |
13361 | if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain)) | |
13362 | result = true; | |
13363 | return result; | |
13364 | } | |
13365 | else if (TYPE_P (t)) | |
3e4a3562 | 13366 | { |
1eee69dd JM |
13367 | /* [basic.link]: A name with no linkage (notably, the name |
13368 | of a class or enumeration declared in a local scope) | |
13369 | shall not be used to declare an entity with linkage. | |
13370 | This implies that names with no linkage cannot be used as | |
13371 | template arguments | |
13372 | ||
13373 | DR 757 relaxes this restriction for C++0x. */ | |
13374 | tree nt = (cxx_dialect > cxx98 ? NULL_TREE | |
13375 | : no_linkage_check (t, /*relaxed_p=*/false)); | |
13376 | ||
13377 | if (nt) | |
13378 | { | |
13379 | /* DR 488 makes use of a type with no linkage cause | |
13380 | type deduction to fail. */ | |
13381 | if (complain & tf_error) | |
13382 | { | |
13383 | if (TYPE_ANONYMOUS_P (nt)) | |
13384 | error ("%qT is/uses anonymous type", t); | |
13385 | else | |
13386 | error ("template argument for %qD uses local type %qT", | |
13387 | tmpl, t); | |
13388 | } | |
13389 | return true; | |
13390 | } | |
13391 | /* In order to avoid all sorts of complications, we do not | |
13392 | allow variably-modified types as template arguments. */ | |
13393 | else if (variably_modified_type_p (t, NULL_TREE)) | |
3e4a3562 NS |
13394 | { |
13395 | if (complain & tf_error) | |
ffaf1e05 JM |
13396 | error ("%qT is a variably modified type", t); |
13397 | return true; | |
3e4a3562 NS |
13398 | } |
13399 | } | |
ffaf1e05 JM |
13400 | /* A non-type argument of integral or enumerated type must be a |
13401 | constant. */ | |
13402 | else if (TREE_TYPE (t) | |
13403 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) | |
13404 | && !TREE_CONSTANT (t)) | |
13405 | { | |
13406 | if (complain & tf_error) | |
13407 | error ("integral expression %qE is not constant", t); | |
13408 | return true; | |
13409 | } | |
13410 | return false; | |
13411 | } | |
13412 | ||
13413 | static bool | |
13414 | check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) | |
13415 | { | |
13416 | int ix, len = DECL_NTPARMS (tmpl); | |
13417 | bool result = false; | |
13418 | ||
13419 | for (ix = 0; ix != len; ix++) | |
13420 | { | |
13421 | if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain)) | |
13422 | result = true; | |
13423 | } | |
2010cdcd | 13424 | if (result && (complain & tf_error)) |
0f51ccfc | 13425 | error (" trying to instantiate %qD", tmpl); |
3e4a3562 NS |
13426 | return result; |
13427 | } | |
13428 | ||
6ba89f8e | 13429 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
13430 | the template arguments in TARG_PTR. */ |
13431 | ||
5566b478 | 13432 | tree |
2b110bfc | 13433 | instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain) |
8d08fdba | 13434 | { |
2b110bfc | 13435 | tree targ_ptr = orig_args; |
5566b478 | 13436 | tree fndecl; |
36a117a5 MM |
13437 | tree gen_tmpl; |
13438 | tree spec; | |
a95799ec | 13439 | HOST_WIDE_INT saved_processing_template_decl; |
5566b478 | 13440 | |
27fafc8d JM |
13441 | if (tmpl == error_mark_node) |
13442 | return error_mark_node; | |
13443 | ||
50bc768d | 13444 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
386b8a85 | 13445 | |
db9b2174 MM |
13446 | /* If this function is a clone, handle it specially. */ |
13447 | if (DECL_CLONED_FUNCTION_P (tmpl)) | |
3ad97789 | 13448 | { |
a30f62e0 | 13449 | tree spec; |
3ad97789 | 13450 | tree clone; |
c8094d83 | 13451 | |
b97e8a14 JM |
13452 | /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have |
13453 | DECL_CLONED_FUNCTION. */ | |
13454 | spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl), | |
13455 | targ_ptr, complain); | |
a30f62e0 MM |
13456 | if (spec == error_mark_node) |
13457 | return error_mark_node; | |
13458 | ||
c6002625 | 13459 | /* Look for the clone. */ |
4684cd27 | 13460 | FOR_EACH_CLONE (clone, spec) |
3ad97789 NS |
13461 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) |
13462 | return clone; | |
13463 | /* We should always have found the clone by now. */ | |
315fb5db | 13464 | gcc_unreachable (); |
3ad97789 NS |
13465 | return NULL_TREE; |
13466 | } | |
c8094d83 | 13467 | |
36a117a5 | 13468 | /* Check to see if we already have this specialization. */ |
f9a7ae04 MM |
13469 | gen_tmpl = most_general_template (tmpl); |
13470 | if (tmpl != gen_tmpl) | |
2b110bfc JM |
13471 | /* The TMPL is a partial instantiation. To get a full set of |
13472 | arguments we must add the arguments used to perform the | |
13473 | partial instantiation. */ | |
13474 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
13475 | targ_ptr); | |
36a117a5 | 13476 | |
2b110bfc JM |
13477 | /* It would be nice to avoid hashing here and then again in tsubst_decl, |
13478 | but it doesn't seem to be on the hot path. */ | |
13479 | spec = retrieve_specialization (gen_tmpl, targ_ptr, 0); | |
13480 | ||
13481 | gcc_assert (tmpl == gen_tmpl | |
13482 | || ((fndecl = retrieve_specialization (tmpl, orig_args, 0)) | |
13483 | == spec) | |
13484 | || fndecl == NULL_TREE); | |
13485 | ||
13486 | if (spec != NULL_TREE) | |
13487 | return spec; | |
386b8a85 | 13488 | |
3e4a3562 NS |
13489 | if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), |
13490 | complain)) | |
13491 | return error_mark_node; | |
c8094d83 | 13492 | |
2b907f5c KL |
13493 | /* We are building a FUNCTION_DECL, during which the access of its |
13494 | parameters and return types have to be checked. However this | |
13495 | FUNCTION_DECL which is the desired context for access checking | |
13496 | is not built yet. We solve this chicken-and-egg problem by | |
13497 | deferring all checks until we have the FUNCTION_DECL. */ | |
13498 | push_deferring_access_checks (dk_deferred); | |
5c74d5b0 | 13499 | |
a95799ec MM |
13500 | /* Although PROCESSING_TEMPLATE_DECL may be true at this point |
13501 | (because, for example, we have encountered a non-dependent | |
a1d08991 MM |
13502 | function call in the body of a template function and must now |
13503 | determine which of several overloaded functions will be called), | |
13504 | within the instantiation itself we are not processing a | |
13505 | template. */ | |
a95799ec MM |
13506 | saved_processing_template_decl = processing_template_decl; |
13507 | processing_template_decl = 0; | |
13508 | /* Substitute template parameters to obtain the specialization. */ | |
17aec3eb | 13509 | fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), |
3e4a3562 | 13510 | targ_ptr, complain, gen_tmpl); |
a95799ec | 13511 | processing_template_decl = saved_processing_template_decl; |
6b6b60af MM |
13512 | if (fndecl == error_mark_node) |
13513 | return error_mark_node; | |
5c74d5b0 | 13514 | |
2b907f5c KL |
13515 | /* Now we know the specialization, compute access previously |
13516 | deferred. */ | |
13517 | push_access_scope (fndecl); | |
d0940d56 DS |
13518 | |
13519 | /* Some typedefs referenced from within the template code need to be access | |
13520 | checked at template instantiation time, i.e now. These types were | |
13521 | added to the template at parsing time. Let's get those and perfom | |
13522 | the acces checks then. */ | |
2eb25c98 | 13523 | perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr); |
2b907f5c KL |
13524 | perform_deferred_access_checks (); |
13525 | pop_access_scope (fndecl); | |
13526 | pop_deferring_access_checks (); | |
5c74d5b0 | 13527 | |
36a117a5 MM |
13528 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
13529 | template, not the most general template. */ | |
13530 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 13531 | |
94350948 | 13532 | /* If we've just instantiated the main entry point for a function, |
3ad97789 NS |
13533 | instantiate all the alternate entry points as well. We do this |
13534 | by cloning the instantiation of the main entry point, not by | |
13535 | instantiating the template clones. */ | |
910ad8de | 13536 | if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl))) |
3ad97789 | 13537 | clone_function_decl (fndecl, /*update_method_vec_p=*/0); |
94350948 | 13538 | |
5566b478 | 13539 | return fndecl; |
8d08fdba | 13540 | } |
5566b478 | 13541 | |
c166b898 ILT |
13542 | /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with |
13543 | NARGS elements of the arguments that are being used when calling | |
13544 | it. TARGS is a vector into which the deduced template arguments | |
13545 | are placed. | |
8d08fdba MS |
13546 | |
13547 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
13548 | all the types, and 1 for complete failure. An error message will be | |
13549 | printed only for an incomplete match. | |
13550 | ||
e5214479 JM |
13551 | If FN is a conversion operator, or we are trying to produce a specific |
13552 | specialization, RETURN_TYPE is the return type desired. | |
9f54c803 MM |
13553 | |
13554 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
13555 | template-id. | |
6467930b | 13556 | |
830bfa74 MM |
13557 | The parameter STRICT is one of: |
13558 | ||
c8094d83 | 13559 | DEDUCE_CALL: |
830bfa74 MM |
13560 | We are deducing arguments for a function call, as in |
13561 | [temp.deduct.call]. | |
13562 | ||
13563 | DEDUCE_CONV: | |
c8094d83 | 13564 | We are deducing arguments for a conversion function, as in |
830bfa74 MM |
13565 | [temp.deduct.conv]. |
13566 | ||
13567 | DEDUCE_EXACT: | |
62e4a758 NS |
13568 | We are deducing arguments when doing an explicit instantiation |
13569 | as in [temp.explicit], when determining an explicit specialization | |
13570 | as in [temp.expl.spec], or when taking the address of a function | |
a34d3336 | 13571 | template, as in [temp.deduct.funcaddr]. */ |
8d08fdba MS |
13572 | |
13573 | int | |
c8094d83 | 13574 | fn_type_unification (tree fn, |
0cbd7506 MS |
13575 | tree explicit_targs, |
13576 | tree targs, | |
c166b898 ILT |
13577 | const tree *args, |
13578 | unsigned int nargs, | |
0cbd7506 | 13579 | tree return_type, |
30f86ec3 FJ |
13580 | unification_kind_t strict, |
13581 | int flags) | |
386b8a85 | 13582 | { |
4393e105 MM |
13583 | tree parms; |
13584 | tree fntype; | |
adecb3f4 | 13585 | int result; |
5d80a306 | 13586 | bool incomplete_argument_packs_p = false; |
386b8a85 | 13587 | |
50bc768d | 13588 | gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL); |
2b59fc25 | 13589 | |
4393e105 MM |
13590 | fntype = TREE_TYPE (fn); |
13591 | if (explicit_targs) | |
75650646 | 13592 | { |
4393e105 | 13593 | /* [temp.deduct] |
c8094d83 | 13594 | |
4393e105 MM |
13595 | The specified template arguments must match the template |
13596 | parameters in kind (i.e., type, nontype, template), and there | |
13597 | must not be more arguments than there are parameters; | |
13598 | otherwise type deduction fails. | |
13599 | ||
13600 | Nontype arguments must match the types of the corresponding | |
13601 | nontype template parameters, or must be convertible to the | |
13602 | types of the corresponding nontype parameters as specified in | |
13603 | _temp.arg.nontype_, otherwise type deduction fails. | |
13604 | ||
13605 | All references in the function type of the function template | |
13606 | to the corresponding template parameters are replaced by the | |
13607 | specified template argument values. If a substitution in a | |
13608 | template parameter or in the function type of the function | |
13609 | template results in an invalid type, type deduction fails. */ | |
5d80a306 DG |
13610 | tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn); |
13611 | int i, len = TREE_VEC_LENGTH (tparms); | |
4393e105 | 13612 | tree converted_args; |
5d80a306 | 13613 | bool incomplete = false; |
75650646 | 13614 | |
c8a7ed43 AO |
13615 | if (explicit_targs == error_mark_node) |
13616 | return 1; | |
13617 | ||
4393e105 | 13618 | converted_args |
5d80a306 | 13619 | = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none, |
e7e93965 MM |
13620 | /*require_all_args=*/false, |
13621 | /*use_default_args=*/false)); | |
4393e105 | 13622 | if (converted_args == error_mark_node) |
75650646 | 13623 | return 1; |
386b8a85 | 13624 | |
ffd49b19 | 13625 | /* Substitute the explicit args into the function type. This is |
0cbd7506 MS |
13626 | necessary so that, for instance, explicitly declared function |
13627 | arguments can match null pointed constants. If we were given | |
13628 | an incomplete set of explicit args, we must not do semantic | |
13629 | processing during substitution as we could create partial | |
13630 | instantiations. */ | |
5d80a306 DG |
13631 | for (i = 0; i < len; i++) |
13632 | { | |
13633 | tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
13634 | bool parameter_pack = false; | |
13635 | ||
13636 | /* Dig out the actual parm. */ | |
13637 | if (TREE_CODE (parm) == TYPE_DECL | |
13638 | || TREE_CODE (parm) == TEMPLATE_DECL) | |
13639 | { | |
13640 | parm = TREE_TYPE (parm); | |
13641 | parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm); | |
13642 | } | |
13643 | else if (TREE_CODE (parm) == PARM_DECL) | |
13644 | { | |
13645 | parm = DECL_INITIAL (parm); | |
13646 | parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm); | |
13647 | } | |
13648 | ||
13649 | if (parameter_pack) | |
13650 | { | |
13651 | int level, idx; | |
13652 | tree targ; | |
13653 | template_parm_level_and_index (parm, &level, &idx); | |
13654 | ||
13655 | /* Mark the argument pack as "incomplete". We could | |
13656 | still deduce more arguments during unification. */ | |
13657 | targ = TMPL_ARG (converted_args, level, idx); | |
b1d7b1c0 DG |
13658 | if (targ) |
13659 | { | |
13660 | ARGUMENT_PACK_INCOMPLETE_P(targ) = 1; | |
13661 | ARGUMENT_PACK_EXPLICIT_ARGS (targ) | |
13662 | = ARGUMENT_PACK_ARGS (targ); | |
13663 | } | |
5d80a306 DG |
13664 | |
13665 | /* We have some incomplete argument packs. */ | |
13666 | incomplete_argument_packs_p = true; | |
13667 | } | |
13668 | } | |
13669 | ||
13670 | if (incomplete_argument_packs_p) | |
13671 | /* Any substitution is guaranteed to be incomplete if there | |
13672 | are incomplete argument packs, because we can still deduce | |
13673 | more arguments. */ | |
13674 | incomplete = 1; | |
13675 | else | |
13676 | incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs); | |
13677 | ||
ffd49b19 | 13678 | processing_template_decl += incomplete; |
c2ea3a40 | 13679 | fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE); |
ffd49b19 | 13680 | processing_template_decl -= incomplete; |
c8094d83 | 13681 | |
4393e105 MM |
13682 | if (fntype == error_mark_node) |
13683 | return 1; | |
050367a3 | 13684 | |
4393e105 | 13685 | /* Place the explicitly specified arguments in TARGS. */ |
ffd49b19 | 13686 | for (i = NUM_TMPL_ARGS (converted_args); i--;) |
4393e105 | 13687 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); |
75650646 | 13688 | } |
c8094d83 | 13689 | |
e5214479 | 13690 | /* Never do unification on the 'this' parameter. */ |
ba139ba8 | 13691 | parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype)); |
c8094d83 | 13692 | |
8d3631f8 NS |
13693 | if (return_type) |
13694 | { | |
c166b898 ILT |
13695 | tree *new_args; |
13696 | ||
8d3631f8 | 13697 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); |
c166b898 ILT |
13698 | new_args = XALLOCAVEC (tree, nargs + 1); |
13699 | new_args[0] = return_type; | |
13700 | memcpy (new_args + 1, args, nargs * sizeof (tree)); | |
13701 | args = new_args; | |
13702 | ++nargs; | |
4393e105 MM |
13703 | } |
13704 | ||
4393e105 MM |
13705 | /* We allow incomplete unification without an error message here |
13706 | because the standard doesn't seem to explicitly prohibit it. Our | |
13707 | callers must be ready to deal with unification failures in any | |
13708 | event. */ | |
c8094d83 | 13709 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
c166b898 | 13710 | targs, parms, args, nargs, /*subr=*/0, |
30f86ec3 | 13711 | strict, flags); |
adecb3f4 | 13712 | |
5d80a306 DG |
13713 | if (result == 0 && incomplete_argument_packs_p) |
13714 | { | |
13715 | int i, len = NUM_TMPL_ARGS (targs); | |
13716 | ||
13717 | /* Clear the "incomplete" flags on all argument packs. */ | |
13718 | for (i = 0; i < len; i++) | |
13719 | { | |
13720 | tree arg = TREE_VEC_ELT (targs, i); | |
13721 | if (ARGUMENT_PACK_P (arg)) | |
13722 | { | |
13723 | ARGUMENT_PACK_INCOMPLETE_P (arg) = 0; | |
13724 | ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE; | |
13725 | } | |
13726 | } | |
13727 | } | |
13728 | ||
4cf36211 DG |
13729 | /* Now that we have bindings for all of the template arguments, |
13730 | ensure that the arguments deduced for the template template | |
13731 | parameters have compatible template parameter lists. We cannot | |
13732 | check this property before we have deduced all template | |
13733 | arguments, because the template parameter types of a template | |
13734 | template parameter might depend on prior template parameters | |
13735 | deduced after the template template parameter. The following | |
13736 | ill-formed example illustrates this issue: | |
13737 | ||
13738 | template<typename T, template<T> class C> void f(C<5>, T); | |
13739 | ||
13740 | template<int N> struct X {}; | |
13741 | ||
13742 | void g() { | |
13743 | f(X<5>(), 5l); // error: template argument deduction fails | |
13744 | } | |
13745 | ||
13746 | The template parameter list of 'C' depends on the template type | |
13747 | parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to | |
13748 | 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the | |
13749 | time that we deduce 'C'. */ | |
13750 | if (result == 0 | |
13751 | && !template_template_parm_bindings_ok_p | |
13752 | (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs)) | |
13753 | return 1; | |
13754 | ||
c8094d83 | 13755 | if (result == 0) |
adecb3f4 | 13756 | /* All is well so far. Now, check: |
c8094d83 MS |
13757 | |
13758 | [temp.deduct] | |
13759 | ||
adecb3f4 MM |
13760 | When all template arguments have been deduced, all uses of |
13761 | template parameters in nondeduced contexts are replaced with | |
13762 | the corresponding deduced argument values. If the | |
13763 | substitution results in an invalid type, as described above, | |
13764 | type deduction fails. */ | |
463ecaca JM |
13765 | { |
13766 | tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE); | |
13767 | if (substed == error_mark_node) | |
13768 | return 1; | |
13769 | ||
13770 | /* If we're looking for an exact match, check that what we got | |
13771 | is indeed an exact match. It might not be if some template | |
13772 | parameters are used in non-deduced contexts. */ | |
13773 | if (strict == DEDUCE_EXACT) | |
13774 | { | |
c166b898 ILT |
13775 | unsigned int i; |
13776 | ||
463ecaca JM |
13777 | tree sarg |
13778 | = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed)); | |
463ecaca JM |
13779 | if (return_type) |
13780 | sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg); | |
c166b898 ILT |
13781 | for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg)) |
13782 | if (!same_type_p (args[i], TREE_VALUE (sarg))) | |
463ecaca JM |
13783 | return 1; |
13784 | } | |
13785 | } | |
adecb3f4 MM |
13786 | |
13787 | return result; | |
830bfa74 MM |
13788 | } |
13789 | ||
13790 | /* Adjust types before performing type deduction, as described in | |
13791 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
13792 | sections are symmetric. PARM is the type of a function parameter |
13793 | or the return type of the conversion function. ARG is the type of | |
13794 | the argument passed to the call, or the type of the value | |
8af2fec4 RY |
13795 | initialized with the result of the conversion function. |
13796 | ARG_EXPR is the original argument expression, which may be null. */ | |
386b8a85 | 13797 | |
62e4a758 | 13798 | static int |
c8094d83 | 13799 | maybe_adjust_types_for_deduction (unification_kind_t strict, |
0cbd7506 | 13800 | tree* parm, |
8af2fec4 RY |
13801 | tree* arg, |
13802 | tree arg_expr) | |
830bfa74 | 13803 | { |
62e4a758 | 13804 | int result = 0; |
c8094d83 | 13805 | |
830bfa74 MM |
13806 | switch (strict) |
13807 | { | |
13808 | case DEDUCE_CALL: | |
13809 | break; | |
13810 | ||
13811 | case DEDUCE_CONV: | |
13812 | { | |
4c7d0dff MM |
13813 | /* Swap PARM and ARG throughout the remainder of this |
13814 | function; the handling is precisely symmetric since PARM | |
13815 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
13816 | tree* temp = parm; |
13817 | parm = arg; | |
13818 | arg = temp; | |
13819 | break; | |
13820 | } | |
13821 | ||
13822 | case DEDUCE_EXACT: | |
f7d5a7cd JM |
13823 | /* Core issue #873: Do the DR606 thing (see below) for these cases, |
13824 | too, but here handle it by stripping the reference from PARM | |
13825 | rather than by adding it to ARG. */ | |
13826 | if (TREE_CODE (*parm) == REFERENCE_TYPE | |
13827 | && TYPE_REF_IS_RVALUE (*parm) | |
13828 | && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM | |
13829 | && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED | |
13830 | && TREE_CODE (*arg) == REFERENCE_TYPE | |
13831 | && !TYPE_REF_IS_RVALUE (*arg)) | |
13832 | *parm = TREE_TYPE (*parm); | |
13833 | /* Nothing else to do in this case. */ | |
62e4a758 | 13834 | return 0; |
830bfa74 MM |
13835 | |
13836 | default: | |
315fb5db | 13837 | gcc_unreachable (); |
830bfa74 MM |
13838 | } |
13839 | ||
13840 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
13841 | { | |
13842 | /* [temp.deduct.call] | |
c8094d83 | 13843 | |
830bfa74 | 13844 | If P is not a reference type: |
c8094d83 | 13845 | |
830bfa74 MM |
13846 | --If A is an array type, the pointer type produced by the |
13847 | array-to-pointer standard conversion (_conv.array_) is | |
13848 | used in place of A for type deduction; otherwise, | |
c8094d83 | 13849 | |
830bfa74 MM |
13850 | --If A is a function type, the pointer type produced by |
13851 | the function-to-pointer standard conversion | |
13852 | (_conv.func_) is used in place of A for type deduction; | |
13853 | otherwise, | |
c8094d83 | 13854 | |
830bfa74 MM |
13855 | --If A is a cv-qualified type, the top level |
13856 | cv-qualifiers of A's type are ignored for type | |
13857 | deduction. */ | |
13858 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
13859 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 13860 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
13861 | *arg = build_pointer_type (*arg); |
13862 | else | |
13863 | *arg = TYPE_MAIN_VARIANT (*arg); | |
13864 | } | |
c8094d83 | 13865 | |
8af2fec4 RY |
13866 | /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is |
13867 | of the form T&&, where T is a template parameter, and the argument | |
13868 | is an lvalue, T is deduced as A& */ | |
13869 | if (TREE_CODE (*parm) == REFERENCE_TYPE | |
13870 | && TYPE_REF_IS_RVALUE (*parm) | |
13871 | && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM | |
13872 | && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED | |
13873 | && arg_expr && real_lvalue_p (arg_expr)) | |
13874 | *arg = build_reference_type (*arg); | |
13875 | ||
830bfa74 | 13876 | /* [temp.deduct.call] |
c8094d83 | 13877 | |
830bfa74 MM |
13878 | If P is a cv-qualified type, the top level cv-qualifiers |
13879 | of P's type are ignored for type deduction. If P is a | |
13880 | reference type, the type referred to by P is used for | |
13881 | type deduction. */ | |
13882 | *parm = TYPE_MAIN_VARIANT (*parm); | |
13883 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
62e4a758 NS |
13884 | { |
13885 | *parm = TREE_TYPE (*parm); | |
13886 | result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; | |
13887 | } | |
1c82cc90 NS |
13888 | |
13889 | /* DR 322. For conversion deduction, remove a reference type on parm | |
13890 | too (which has been swapped into ARG). */ | |
13891 | if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE) | |
13892 | *arg = TREE_TYPE (*arg); | |
c8094d83 | 13893 | |
62e4a758 | 13894 | return result; |
386b8a85 JM |
13895 | } |
13896 | ||
e5214479 | 13897 | /* Most parms like fn_type_unification. |
9f54c803 MM |
13898 | |
13899 | If SUBR is 1, we're being called recursively (to unify the | |
13900 | arguments of a function or method parameter of a function | |
38d18b1a | 13901 | template). */ |
386b8a85 | 13902 | |
4966381a | 13903 | static int |
c8094d83 | 13904 | type_unification_real (tree tparms, |
0cbd7506 MS |
13905 | tree targs, |
13906 | tree xparms, | |
c166b898 ILT |
13907 | const tree *xargs, |
13908 | unsigned int xnargs, | |
0cbd7506 | 13909 | int subr, |
30f86ec3 FJ |
13910 | unification_kind_t strict, |
13911 | int flags) | |
8d08fdba | 13912 | { |
8af2fec4 | 13913 | tree parm, arg, arg_expr; |
8d08fdba MS |
13914 | int i; |
13915 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 13916 | int sub_strict; |
bd0d5d4a | 13917 | int saw_undeduced = 0; |
c166b898 ILT |
13918 | tree parms; |
13919 | const tree *args; | |
13920 | unsigned int nargs; | |
13921 | unsigned int ia; | |
8d08fdba | 13922 | |
50bc768d NS |
13923 | gcc_assert (TREE_CODE (tparms) == TREE_VEC); |
13924 | gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST); | |
50bc768d | 13925 | gcc_assert (ntparms > 0); |
8d08fdba | 13926 | |
1a048f82 DS |
13927 | /* Reset the number of non-defaulted template arguments contained |
13928 | in in TARGS. */ | |
13929 | NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE; | |
13930 | ||
830bfa74 MM |
13931 | switch (strict) |
13932 | { | |
13933 | case DEDUCE_CALL: | |
028d1f20 | 13934 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL |
0cbd7506 | 13935 | | UNIFY_ALLOW_DERIVED); |
830bfa74 | 13936 | break; |
c8094d83 | 13937 | |
830bfa74 MM |
13938 | case DEDUCE_CONV: |
13939 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
13940 | break; | |
13941 | ||
13942 | case DEDUCE_EXACT: | |
13943 | sub_strict = UNIFY_ALLOW_NONE; | |
13944 | break; | |
c8094d83 | 13945 | |
830bfa74 | 13946 | default: |
315fb5db | 13947 | gcc_unreachable (); |
830bfa74 MM |
13948 | } |
13949 | ||
bd0d5d4a JM |
13950 | again: |
13951 | parms = xparms; | |
13952 | args = xargs; | |
c166b898 | 13953 | nargs = xnargs; |
bd0d5d4a | 13954 | |
c166b898 | 13955 | ia = 0; |
a34d3336 | 13956 | while (parms && parms != void_list_node |
c166b898 | 13957 | && ia < nargs) |
8d08fdba | 13958 | { |
5d80a306 DG |
13959 | if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) |
13960 | break; | |
13961 | ||
8d08fdba MS |
13962 | parm = TREE_VALUE (parms); |
13963 | parms = TREE_CHAIN (parms); | |
c166b898 ILT |
13964 | arg = args[ia]; |
13965 | ++ia; | |
8af2fec4 | 13966 | arg_expr = NULL; |
8d08fdba MS |
13967 | |
13968 | if (arg == error_mark_node) | |
13969 | return 1; | |
13970 | if (arg == unknown_type_node) | |
34016c81 JM |
13971 | /* We can't deduce anything from this, but we might get all the |
13972 | template args from other function args. */ | |
13973 | continue; | |
b7484fbe | 13974 | |
03e70705 JM |
13975 | /* Conversions will be performed on a function argument that |
13976 | corresponds with a function parameter that contains only | |
13977 | non-deducible template parameters and explicitly specified | |
13978 | template parameters. */ | |
c353b8e3 | 13979 | if (!uses_template_parms (parm)) |
b7484fbe | 13980 | { |
03e70705 JM |
13981 | tree type; |
13982 | ||
2f939d94 | 13983 | if (!TYPE_P (arg)) |
03e70705 JM |
13984 | type = TREE_TYPE (arg); |
13985 | else | |
c353b8e3 | 13986 | type = arg; |
03e70705 | 13987 | |
fad86f7a | 13988 | if (same_type_p (parm, type)) |
343c89cd | 13989 | continue; |
fad86f7a | 13990 | if (strict != DEDUCE_EXACT |
3db45ab5 | 13991 | && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg, |
30f86ec3 | 13992 | flags)) |
fad86f7a | 13993 | continue; |
3db45ab5 | 13994 | |
b7484fbe MS |
13995 | return 1; |
13996 | } | |
c8094d83 | 13997 | |
2f939d94 | 13998 | if (!TYPE_P (arg)) |
8d08fdba | 13999 | { |
50bc768d | 14000 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); |
34016c81 | 14001 | if (type_unknown_p (arg)) |
28cbf42c | 14002 | { |
acde59b4 | 14003 | /* [temp.deduct.type] |
34016c81 | 14004 | |
acde59b4 MM |
14005 | A template-argument can be deduced from a pointer to |
14006 | function or pointer to member function argument if | |
14007 | the set of overloaded functions does not contain | |
14008 | function templates and at most one of a set of | |
14009 | overloaded functions provides a unique match. */ | |
34016c81 | 14010 | if (resolve_overloaded_unification |
acde59b4 MM |
14011 | (tparms, targs, parm, arg, strict, sub_strict)) |
14012 | continue; | |
14013 | ||
14014 | return 1; | |
28cbf42c | 14015 | } |
8af2fec4 | 14016 | arg_expr = arg; |
f9aa54d3 | 14017 | arg = unlowered_expr_type (arg); |
08476342 NS |
14018 | if (arg == error_mark_node) |
14019 | return 1; | |
8d08fdba | 14020 | } |
c8094d83 | 14021 | |
62e4a758 | 14022 | { |
0cbd7506 | 14023 | int arg_strict = sub_strict; |
c8094d83 | 14024 | |
0cbd7506 | 14025 | if (!subr) |
8af2fec4 RY |
14026 | arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, |
14027 | arg_expr); | |
4393e105 | 14028 | |
09357846 JM |
14029 | if (arg == init_list_type_node && arg_expr) |
14030 | arg = arg_expr; | |
0cbd7506 MS |
14031 | if (unify (tparms, targs, parm, arg, arg_strict)) |
14032 | return 1; | |
62e4a758 | 14033 | } |
8d08fdba | 14034 | } |
c8094d83 | 14035 | |
5d80a306 DG |
14036 | |
14037 | if (parms | |
14038 | && parms != void_list_node | |
14039 | && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) | |
14040 | { | |
14041 | /* Unify the remaining arguments with the pack expansion type. */ | |
14042 | tree argvec; | |
14043 | tree parmvec = make_tree_vec (1); | |
5d80a306 | 14044 | |
5d80a306 | 14045 | /* Allocate a TREE_VEC and copy in all of the arguments */ |
c166b898 ILT |
14046 | argvec = make_tree_vec (nargs - ia); |
14047 | for (i = 0; ia < nargs; ++ia, ++i) | |
14048 | TREE_VEC_ELT (argvec, i) = args[ia]; | |
5d80a306 DG |
14049 | |
14050 | /* Copy the parameter into parmvec. */ | |
14051 | TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms); | |
14052 | if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict, | |
14053 | /*call_args_p=*/true, /*subr=*/subr)) | |
14054 | return 1; | |
14055 | ||
14056 | /* Advance to the end of the list of parameters. */ | |
14057 | parms = TREE_CHAIN (parms); | |
14058 | } | |
14059 | ||
8d08fdba MS |
14060 | /* Fail if we've reached the end of the parm list, and more args |
14061 | are present, and the parm list isn't variadic. */ | |
c166b898 | 14062 | if (ia < nargs && parms == void_list_node) |
8d08fdba | 14063 | return 1; |
f4f206f4 | 14064 | /* Fail if parms are left and they don't have default values. */ |
a34d3336 | 14065 | if (parms && parms != void_list_node |
8d08fdba MS |
14066 | && TREE_PURPOSE (parms) == NULL_TREE) |
14067 | return 1; | |
bd0d5d4a | 14068 | |
8d08fdba MS |
14069 | if (!subr) |
14070 | for (i = 0; i < ntparms; i++) | |
a34d3336 | 14071 | if (!TREE_VEC_ELT (targs, i)) |
8d08fdba | 14072 | { |
2d8ba2c7 LM |
14073 | tree tparm; |
14074 | ||
14075 | if (TREE_VEC_ELT (tparms, i) == error_mark_node) | |
14076 | continue; | |
14077 | ||
14078 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
bd0d5d4a JM |
14079 | |
14080 | /* If this is an undeduced nontype parameter that depends on | |
14081 | a type parameter, try another pass; its type may have been | |
14082 | deduced from a later argument than the one from which | |
14083 | this parameter can be deduced. */ | |
14084 | if (TREE_CODE (tparm) == PARM_DECL | |
14085 | && uses_template_parms (TREE_TYPE (tparm)) | |
14086 | && !saw_undeduced++) | |
14087 | goto again; | |
14088 | ||
9b7dd5e8 DG |
14089 | /* Core issue #226 (C++0x) [temp.deduct]: |
14090 | ||
14091 | If a template argument has not been deduced, its | |
14092 | default template argument, if any, is used. | |
14093 | ||
c1ae8be5 SM |
14094 | When we are in C++98 mode, TREE_PURPOSE will either |
14095 | be NULL_TREE or ERROR_MARK_NODE, so we do not need | |
14096 | to explicitly check cxx_dialect here. */ | |
9b7dd5e8 DG |
14097 | if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i))) |
14098 | { | |
23f392e0 JM |
14099 | tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); |
14100 | tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i)); | |
14101 | arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE); | |
14102 | arg = convert_template_argument (parm, arg, targs, tf_none, | |
14103 | i, NULL_TREE); | |
9b7dd5e8 DG |
14104 | if (arg == error_mark_node) |
14105 | return 1; | |
14106 | else | |
14107 | { | |
14108 | TREE_VEC_ELT (targs, i) = arg; | |
1a048f82 DS |
14109 | /* The position of the first default template argument, |
14110 | is also the number of non-defaulted arguments in TARGS. | |
14111 | Record that. */ | |
14112 | if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs)) | |
14113 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i); | |
9b7dd5e8 DG |
14114 | continue; |
14115 | } | |
14116 | } | |
14117 | ||
b1d7b1c0 DG |
14118 | /* If the type parameter is a parameter pack, then it will |
14119 | be deduced to an empty parameter pack. */ | |
14120 | if (template_parameter_pack_p (tparm)) | |
14121 | { | |
14122 | tree arg; | |
14123 | ||
14124 | if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX) | |
14125 | { | |
14126 | arg = make_node (NONTYPE_ARGUMENT_PACK); | |
14127 | TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm)); | |
14128 | TREE_CONSTANT (arg) = 1; | |
14129 | } | |
14130 | else | |
7ecbca9d | 14131 | arg = cxx_make_type (TYPE_ARGUMENT_PACK); |
b1d7b1c0 DG |
14132 | |
14133 | SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0)); | |
14134 | ||
14135 | TREE_VEC_ELT (targs, i) = arg; | |
14136 | continue; | |
14137 | } | |
14138 | ||
8d08fdba MS |
14139 | return 2; |
14140 | } | |
1a048f82 DS |
14141 | #ifdef ENABLE_CHECKING |
14142 | if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs)) | |
14143 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs)); | |
14144 | #endif | |
c8094d83 | 14145 | |
8d08fdba MS |
14146 | return 0; |
14147 | } | |
14148 | ||
acde59b4 MM |
14149 | /* Subroutine of type_unification_real. Args are like the variables |
14150 | at the call site. ARG is an overloaded function (or template-id); | |
14151 | we try deducing template args from each of the overloads, and if | |
14152 | only one succeeds, we go with that. Modifies TARGS and returns | |
14153 | true on success. */ | |
34016c81 | 14154 | |
acde59b4 | 14155 | static bool |
c8094d83 | 14156 | resolve_overloaded_unification (tree tparms, |
0cbd7506 MS |
14157 | tree targs, |
14158 | tree parm, | |
14159 | tree arg, | |
14160 | unification_kind_t strict, | |
3a978d72 | 14161 | int sub_strict) |
34016c81 JM |
14162 | { |
14163 | tree tempargs = copy_node (targs); | |
14164 | int good = 0; | |
e04c614e | 14165 | tree goodfn = NULL_TREE; |
f23fb7f5 | 14166 | bool addr_p; |
34016c81 JM |
14167 | |
14168 | if (TREE_CODE (arg) == ADDR_EXPR) | |
f23fb7f5 MM |
14169 | { |
14170 | arg = TREE_OPERAND (arg, 0); | |
14171 | addr_p = true; | |
14172 | } | |
14173 | else | |
14174 | addr_p = false; | |
9f3d9e46 | 14175 | |
d8f8dca1 MM |
14176 | if (TREE_CODE (arg) == COMPONENT_REF) |
14177 | /* Handle `&x' where `x' is some static or non-static member | |
14178 | function name. */ | |
14179 | arg = TREE_OPERAND (arg, 1); | |
14180 | ||
05e0b2f4 JM |
14181 | if (TREE_CODE (arg) == OFFSET_REF) |
14182 | arg = TREE_OPERAND (arg, 1); | |
14183 | ||
9f3d9e46 | 14184 | /* Strip baselink information. */ |
50ad9642 MM |
14185 | if (BASELINK_P (arg)) |
14186 | arg = BASELINK_FUNCTIONS (arg); | |
9f3d9e46 | 14187 | |
34016c81 JM |
14188 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
14189 | { | |
14190 | /* If we got some explicit template args, we need to plug them into | |
14191 | the affected templates before we try to unify, in case the | |
14192 | explicit args will completely resolve the templates in question. */ | |
14193 | ||
14194 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
14195 | arg = TREE_OPERAND (arg, 0); | |
14196 | ||
14197 | for (; arg; arg = OVL_NEXT (arg)) | |
14198 | { | |
14199 | tree fn = OVL_CURRENT (arg); | |
14200 | tree subargs, elem; | |
14201 | ||
14202 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
14203 | continue; | |
14204 | ||
cc85b4c5 | 14205 | ++processing_template_decl; |
a34d3336 NS |
14206 | subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn), |
14207 | expl_subargs, /*check_ret=*/false); | |
34016c81 JM |
14208 | if (subargs) |
14209 | { | |
c2ea3a40 | 14210 | elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); |
e04c614e JM |
14211 | if (try_one_overload (tparms, targs, tempargs, parm, |
14212 | elem, strict, sub_strict, addr_p) | |
14213 | && (!goodfn || !decls_match (goodfn, elem))) | |
14214 | { | |
14215 | goodfn = elem; | |
14216 | ++good; | |
14217 | } | |
34016c81 | 14218 | } |
cc85b4c5 | 14219 | --processing_template_decl; |
34016c81 JM |
14220 | } |
14221 | } | |
acde59b4 MM |
14222 | else if (TREE_CODE (arg) != OVERLOAD |
14223 | && TREE_CODE (arg) != FUNCTION_DECL) | |
14224 | /* If ARG is, for example, "(0, &f)" then its type will be unknown | |
14225 | -- but the deduction does not succeed because the expression is | |
14226 | not just the function on its own. */ | |
14227 | return false; | |
315fb5db | 14228 | else |
acde59b4 | 14229 | for (; arg; arg = OVL_NEXT (arg)) |
e04c614e JM |
14230 | if (try_one_overload (tparms, targs, tempargs, parm, |
14231 | TREE_TYPE (OVL_CURRENT (arg)), | |
14232 | strict, sub_strict, addr_p) | |
14233 | && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg)))) | |
14234 | { | |
14235 | goodfn = OVL_CURRENT (arg); | |
14236 | ++good; | |
14237 | } | |
34016c81 JM |
14238 | |
14239 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
14240 | to function or pointer to member function argument if the set of | |
14241 | overloaded functions does not contain function templates and at most | |
14242 | one of a set of overloaded functions provides a unique match. | |
14243 | ||
14244 | So if we found multiple possibilities, we return success but don't | |
14245 | deduce anything. */ | |
14246 | ||
14247 | if (good == 1) | |
14248 | { | |
14249 | int i = TREE_VEC_LENGTH (targs); | |
14250 | for (; i--; ) | |
14251 | if (TREE_VEC_ELT (tempargs, i)) | |
14252 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
14253 | } | |
14254 | if (good) | |
acde59b4 | 14255 | return true; |
34016c81 | 14256 | |
acde59b4 | 14257 | return false; |
34016c81 JM |
14258 | } |
14259 | ||
ccb05613 JM |
14260 | /* Core DR 115: In contexts where deduction is done and fails, or in |
14261 | contexts where deduction is not done, if a template argument list is | |
14262 | specified and it, along with any default template arguments, identifies | |
14263 | a single function template specialization, then the template-id is an | |
14264 | lvalue for the function template specialization. */ | |
14265 | ||
14266 | tree | |
14267 | resolve_nondeduced_context (tree orig_expr) | |
14268 | { | |
14269 | tree expr, offset, baselink; | |
14270 | bool addr; | |
14271 | ||
14272 | if (!type_unknown_p (orig_expr)) | |
14273 | return orig_expr; | |
14274 | ||
14275 | expr = orig_expr; | |
14276 | addr = false; | |
14277 | offset = NULL_TREE; | |
14278 | baselink = NULL_TREE; | |
14279 | ||
14280 | if (TREE_CODE (expr) == ADDR_EXPR) | |
14281 | { | |
14282 | expr = TREE_OPERAND (expr, 0); | |
14283 | addr = true; | |
14284 | } | |
14285 | if (TREE_CODE (expr) == OFFSET_REF) | |
14286 | { | |
14287 | offset = expr; | |
14288 | expr = TREE_OPERAND (expr, 1); | |
14289 | } | |
14290 | if (TREE_CODE (expr) == BASELINK) | |
14291 | { | |
14292 | baselink = expr; | |
14293 | expr = BASELINK_FUNCTIONS (expr); | |
14294 | } | |
14295 | ||
14296 | if (TREE_CODE (expr) == TEMPLATE_ID_EXPR) | |
14297 | { | |
14298 | int good = 0; | |
14299 | tree goodfn = NULL_TREE; | |
14300 | ||
14301 | /* If we got some explicit template args, we need to plug them into | |
14302 | the affected templates before we try to unify, in case the | |
14303 | explicit args will completely resolve the templates in question. */ | |
14304 | ||
14305 | tree expl_subargs = TREE_OPERAND (expr, 1); | |
14306 | tree arg = TREE_OPERAND (expr, 0); | |
14307 | tree badfn = NULL_TREE; | |
14308 | tree badargs = NULL_TREE; | |
14309 | ||
14310 | for (; arg; arg = OVL_NEXT (arg)) | |
14311 | { | |
14312 | tree fn = OVL_CURRENT (arg); | |
14313 | tree subargs, elem; | |
14314 | ||
14315 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
14316 | continue; | |
14317 | ||
14318 | ++processing_template_decl; | |
14319 | subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn), | |
14320 | expl_subargs, /*check_ret=*/false); | |
14321 | if (subargs && !any_dependent_template_arguments_p (subargs)) | |
14322 | { | |
14323 | elem = instantiate_template (fn, subargs, tf_none); | |
14324 | if (elem == error_mark_node) | |
14325 | { | |
14326 | badfn = fn; | |
14327 | badargs = subargs; | |
14328 | } | |
14329 | else if (elem && (!goodfn || !decls_match (goodfn, elem))) | |
14330 | { | |
14331 | goodfn = elem; | |
14332 | ++good; | |
14333 | } | |
14334 | } | |
14335 | --processing_template_decl; | |
14336 | } | |
14337 | if (good == 1) | |
14338 | { | |
14339 | expr = goodfn; | |
14340 | if (baselink) | |
14341 | expr = build_baselink (BASELINK_BINFO (baselink), | |
14342 | BASELINK_ACCESS_BINFO (baselink), | |
14343 | expr, BASELINK_OPTYPE (baselink)); | |
14344 | if (offset) | |
2e5748d2 JM |
14345 | { |
14346 | tree base | |
14347 | = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0))); | |
14348 | expr = build_offset_ref (base, expr, addr); | |
14349 | } | |
ccb05613 | 14350 | if (addr) |
2e5748d2 | 14351 | expr = cp_build_addr_expr (expr, tf_warning_or_error); |
ccb05613 JM |
14352 | return expr; |
14353 | } | |
14354 | else if (good == 0 && badargs) | |
14355 | /* There were no good options and at least one bad one, so let the | |
14356 | user know what the problem is. */ | |
14357 | instantiate_template (badfn, badargs, tf_warning_or_error); | |
14358 | } | |
14359 | return orig_expr; | |
14360 | } | |
14361 | ||
34016c81 JM |
14362 | /* Subroutine of resolve_overloaded_unification; does deduction for a single |
14363 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
14364 | different overloads deduce different arguments for a given parm. | |
f23fb7f5 MM |
14365 | ADDR_P is true if the expression for which deduction is being |
14366 | performed was of the form "& fn" rather than simply "fn". | |
14367 | ||
34016c81 JM |
14368 | Returns 1 on success. */ |
14369 | ||
14370 | static int | |
3a978d72 | 14371 | try_one_overload (tree tparms, |
0cbd7506 MS |
14372 | tree orig_targs, |
14373 | tree targs, | |
14374 | tree parm, | |
14375 | tree arg, | |
14376 | unification_kind_t strict, | |
f23fb7f5 MM |
14377 | int sub_strict, |
14378 | bool addr_p) | |
34016c81 JM |
14379 | { |
14380 | int nargs; | |
14381 | tree tempargs; | |
14382 | int i; | |
14383 | ||
14384 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
14385 | to function or pointer to member function argument if the set of | |
14386 | overloaded functions does not contain function templates and at most | |
14387 | one of a set of overloaded functions provides a unique match. | |
14388 | ||
14389 | So if this is a template, just return success. */ | |
14390 | ||
14391 | if (uses_template_parms (arg)) | |
14392 | return 1; | |
14393 | ||
f23fb7f5 MM |
14394 | if (TREE_CODE (arg) == METHOD_TYPE) |
14395 | arg = build_ptrmemfunc_type (build_pointer_type (arg)); | |
14396 | else if (addr_p) | |
14397 | arg = build_pointer_type (arg); | |
14398 | ||
8af2fec4 | 14399 | sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL); |
34016c81 JM |
14400 | |
14401 | /* We don't copy orig_targs for this because if we have already deduced | |
14402 | some template args from previous args, unify would complain when we | |
14403 | try to deduce a template parameter for the same argument, even though | |
14404 | there isn't really a conflict. */ | |
14405 | nargs = TREE_VEC_LENGTH (targs); | |
f31c0a32 | 14406 | tempargs = make_tree_vec (nargs); |
34016c81 | 14407 | |
4393e105 | 14408 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
14409 | return 0; |
14410 | ||
14411 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 14412 | explicitly specified args. */ |
34016c81 JM |
14413 | for (i = nargs; i--; ) |
14414 | { | |
14415 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 14416 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 | 14417 | |
a34d3336 NS |
14418 | if (!elt) |
14419 | /*NOP*/; | |
34016c81 | 14420 | else if (uses_template_parms (elt)) |
a34d3336 NS |
14421 | /* Since we're unifying against ourselves, we will fill in |
14422 | template args used in the function parm list with our own | |
14423 | template parms. Discard them. */ | |
14424 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
14425 | else if (oldelt && !template_args_equal (oldelt, elt)) | |
34016c81 JM |
14426 | return 0; |
14427 | } | |
14428 | ||
14429 | for (i = nargs; i--; ) | |
14430 | { | |
14431 | tree elt = TREE_VEC_ELT (tempargs, i); | |
14432 | ||
14433 | if (elt) | |
14434 | TREE_VEC_ELT (targs, i) = elt; | |
14435 | } | |
14436 | ||
14437 | return 1; | |
14438 | } | |
14439 | ||
4393e105 MM |
14440 | /* PARM is a template class (perhaps with unbound template |
14441 | parameters). ARG is a fully instantiated type. If ARG can be | |
14442 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
14443 | TARGS are as for unify. */ | |
fcfb9f96 MM |
14444 | |
14445 | static tree | |
3a978d72 | 14446 | try_class_unification (tree tparms, tree targs, tree parm, tree arg) |
4393e105 | 14447 | { |
4393e105 MM |
14448 | tree copy_of_targs; |
14449 | ||
14450 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
c8094d83 | 14451 | || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg)) |
68361a03 | 14452 | != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))) |
4393e105 MM |
14453 | return NULL_TREE; |
14454 | ||
14455 | /* We need to make a new template argument vector for the call to | |
14456 | unify. If we used TARGS, we'd clutter it up with the result of | |
14457 | the attempted unification, even if this class didn't work out. | |
14458 | We also don't want to commit ourselves to all the unifications | |
14459 | we've already done, since unification is supposed to be done on | |
14460 | an argument-by-argument basis. In other words, consider the | |
14461 | following pathological case: | |
14462 | ||
14463 | template <int I, int J, int K> | |
14464 | struct S {}; | |
c8094d83 | 14465 | |
4393e105 MM |
14466 | template <int I, int J> |
14467 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
c8094d83 | 14468 | |
4393e105 MM |
14469 | template <int I, int J, int K> |
14470 | void f(S<I, J, K>, S<I, I, I>); | |
c8094d83 | 14471 | |
4393e105 | 14472 | void g() { |
0cbd7506 MS |
14473 | S<0, 0, 0> s0; |
14474 | S<0, 1, 2> s2; | |
c8094d83 | 14475 | |
0cbd7506 | 14476 | f(s0, s2); |
4393e105 MM |
14477 | } |
14478 | ||
14479 | Now, by the time we consider the unification involving `s2', we | |
14480 | already know that we must have `f<0, 0, 0>'. But, even though | |
0e339752 | 14481 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid |
4393e105 MM |
14482 | because there are two ways to unify base classes of S<0, 1, 2> |
14483 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
14484 | would reject the possibility I=1. */ | |
f31c0a32 | 14485 | copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); |
c8094d83 | 14486 | |
4393e105 | 14487 | /* If unification failed, we're done. */ |
74601d7c KL |
14488 | if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), |
14489 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE)) | |
4393e105 | 14490 | return NULL_TREE; |
74601d7c KL |
14491 | |
14492 | return arg; | |
4393e105 MM |
14493 | } |
14494 | ||
a3a0fc7f NS |
14495 | /* Given a template type PARM and a class type ARG, find the unique |
14496 | base type in ARG that is an instance of PARM. We do not examine | |
14497 | ARG itself; only its base-classes. If there is not exactly one | |
14498 | appropriate base class, return NULL_TREE. PARM may be the type of | |
14499 | a partial specialization, as well as a plain template type. Used | |
14500 | by unify. */ | |
4393e105 MM |
14501 | |
14502 | static tree | |
a3a0fc7f | 14503 | get_template_base (tree tparms, tree targs, tree parm, tree arg) |
fcfb9f96 | 14504 | { |
a3a0fc7f NS |
14505 | tree rval = NULL_TREE; |
14506 | tree binfo; | |
14507 | ||
9e1e64ec | 14508 | gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg))); |
c8094d83 | 14509 | |
a3a0fc7f NS |
14510 | binfo = TYPE_BINFO (complete_type (arg)); |
14511 | if (!binfo) | |
14512 | /* The type could not be completed. */ | |
14513 | return NULL_TREE; | |
fcfb9f96 | 14514 | |
a3a0fc7f NS |
14515 | /* Walk in inheritance graph order. The search order is not |
14516 | important, and this avoids multiple walks of virtual bases. */ | |
14517 | for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo)) | |
fcfb9f96 | 14518 | { |
a3a0fc7f | 14519 | tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo)); |
fcfb9f96 | 14520 | |
8d83768f NS |
14521 | if (r) |
14522 | { | |
14523 | /* If there is more than one satisfactory baseclass, then: | |
fcfb9f96 | 14524 | |
8d83768f | 14525 | [temp.deduct.call] |
fcfb9f96 | 14526 | |
8d83768f NS |
14527 | If they yield more than one possible deduced A, the type |
14528 | deduction fails. | |
4393e105 | 14529 | |
8d83768f | 14530 | applies. */ |
a3a0fc7f NS |
14531 | if (rval && !same_type_p (r, rval)) |
14532 | return NULL_TREE; | |
c8094d83 | 14533 | |
a3a0fc7f | 14534 | rval = r; |
8d83768f | 14535 | } |
fcfb9f96 MM |
14536 | } |
14537 | ||
a3a0fc7f | 14538 | return rval; |
fcfb9f96 MM |
14539 | } |
14540 | ||
db2767b6 MM |
14541 | /* Returns the level of DECL, which declares a template parameter. */ |
14542 | ||
e9659ab0 | 14543 | static int |
3a978d72 | 14544 | template_decl_level (tree decl) |
db2767b6 MM |
14545 | { |
14546 | switch (TREE_CODE (decl)) | |
14547 | { | |
14548 | case TYPE_DECL: | |
14549 | case TEMPLATE_DECL: | |
14550 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
14551 | ||
14552 | case PARM_DECL: | |
14553 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
14554 | ||
14555 | default: | |
315fb5db | 14556 | gcc_unreachable (); |
db2767b6 | 14557 | } |
315fb5db | 14558 | return 0; |
db2767b6 MM |
14559 | } |
14560 | ||
830bfa74 MM |
14561 | /* Decide whether ARG can be unified with PARM, considering only the |
14562 | cv-qualifiers of each type, given STRICT as documented for unify. | |
324f9dfb | 14563 | Returns nonzero iff the unification is OK on that basis. */ |
e92cc029 | 14564 | |
e9659ab0 | 14565 | static int |
3a978d72 | 14566 | check_cv_quals_for_unify (int strict, tree arg, tree parm) |
830bfa74 | 14567 | { |
4f2b0fb2 NS |
14568 | int arg_quals = cp_type_quals (arg); |
14569 | int parm_quals = cp_type_quals (parm); | |
14570 | ||
355f774d NS |
14571 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
14572 | && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) | |
4f2b0fb2 | 14573 | { |
2e9ceb77 | 14574 | /* Although a CVR qualifier is ignored when being applied to a |
0cbd7506 | 14575 | substituted template parameter ([8.3.2]/1 for example), that |
93e1ddcf JM |
14576 | does not allow us to unify "const T" with "int&" because both |
14577 | types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type]. | |
14578 | It is ok when we're allowing additional CV qualifiers | |
0cbd7506 | 14579 | at the outer level [14.8.2.1]/3,1st bullet. */ |
2e9ceb77 NS |
14580 | if ((TREE_CODE (arg) == REFERENCE_TYPE |
14581 | || TREE_CODE (arg) == FUNCTION_TYPE | |
14582 | || TREE_CODE (arg) == METHOD_TYPE) | |
14583 | && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))) | |
14584 | return 0; | |
14585 | ||
14586 | if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM) | |
14587 | && (parm_quals & TYPE_QUAL_RESTRICT)) | |
14588 | return 0; | |
4f2b0fb2 | 14589 | } |
2e9ceb77 | 14590 | |
62e4a758 | 14591 | if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
4f2b0fb2 | 14592 | && (arg_quals & parm_quals) != parm_quals) |
ef637255 MM |
14593 | return 0; |
14594 | ||
62e4a758 | 14595 | if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) |
4f2b0fb2 | 14596 | && (parm_quals & arg_quals) != arg_quals) |
ef637255 MM |
14597 | return 0; |
14598 | ||
ef637255 | 14599 | return 1; |
830bfa74 MM |
14600 | } |
14601 | ||
5d80a306 DG |
14602 | /* Determines the LEVEL and INDEX for the template parameter PARM. */ |
14603 | void | |
14604 | template_parm_level_and_index (tree parm, int* level, int* index) | |
14605 | { | |
14606 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
14607 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
14608 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
14609 | { | |
14610 | *index = TEMPLATE_TYPE_IDX (parm); | |
14611 | *level = TEMPLATE_TYPE_LEVEL (parm); | |
14612 | } | |
14613 | else | |
14614 | { | |
14615 | *index = TEMPLATE_PARM_IDX (parm); | |
14616 | *level = TEMPLATE_PARM_LEVEL (parm); | |
14617 | } | |
14618 | } | |
14619 | ||
14620 | /* Unifies the remaining arguments in PACKED_ARGS with the pack | |
14621 | expansion at the end of PACKED_PARMS. Returns 0 if the type | |
14622 | deduction succeeds, 1 otherwise. STRICT is the same as in | |
14623 | unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function | |
14624 | call argument list. We'll need to adjust the arguments to make them | |
14625 | types. SUBR tells us if this is from a recursive call to | |
14626 | type_unification_real. */ | |
14627 | int | |
14628 | unify_pack_expansion (tree tparms, tree targs, tree packed_parms, | |
14629 | tree packed_args, int strict, bool call_args_p, | |
14630 | bool subr) | |
14631 | { | |
14632 | tree parm | |
14633 | = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1); | |
14634 | tree pattern = PACK_EXPANSION_PATTERN (parm); | |
14635 | tree pack, packs = NULL_TREE; | |
14636 | int i, start = TREE_VEC_LENGTH (packed_parms) - 1; | |
14637 | int len = TREE_VEC_LENGTH (packed_args); | |
14638 | ||
14639 | /* Determine the parameter packs we will be deducing from the | |
14640 | pattern, and record their current deductions. */ | |
14641 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm); | |
14642 | pack; pack = TREE_CHAIN (pack)) | |
14643 | { | |
14644 | tree parm_pack = TREE_VALUE (pack); | |
14645 | int idx, level; | |
14646 | ||
14647 | /* Determine the index and level of this parameter pack. */ | |
14648 | template_parm_level_and_index (parm_pack, &level, &idx); | |
14649 | ||
14650 | /* Keep track of the parameter packs and their corresponding | |
14651 | argument packs. */ | |
14652 | packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs); | |
14653 | TREE_TYPE (packs) = make_tree_vec (len - start); | |
14654 | } | |
14655 | ||
14656 | /* Loop through all of the arguments that have not yet been | |
14657 | unified and unify each with the pattern. */ | |
14658 | for (i = start; i < len; i++) | |
14659 | { | |
14660 | tree parm = pattern; | |
14661 | ||
14662 | /* For each parameter pack, clear out the deduced value so that | |
14663 | we can deduce it again. */ | |
14664 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
14665 | { | |
14666 | int idx, level; | |
14667 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
14668 | ||
14669 | TMPL_ARG (targs, level, idx) = NULL_TREE; | |
14670 | } | |
14671 | ||
14672 | /* Unify the pattern with the current argument. */ | |
14673 | { | |
14674 | tree arg = TREE_VEC_ELT (packed_args, i); | |
2afad0f6 | 14675 | tree arg_expr = NULL_TREE; |
5d80a306 DG |
14676 | int arg_strict = strict; |
14677 | bool skip_arg_p = false; | |
14678 | ||
14679 | if (call_args_p) | |
14680 | { | |
14681 | int sub_strict; | |
14682 | ||
14683 | /* This mirrors what we do in type_unification_real. */ | |
14684 | switch (strict) | |
14685 | { | |
14686 | case DEDUCE_CALL: | |
14687 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | |
14688 | | UNIFY_ALLOW_MORE_CV_QUAL | |
14689 | | UNIFY_ALLOW_DERIVED); | |
14690 | break; | |
14691 | ||
14692 | case DEDUCE_CONV: | |
14693 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
14694 | break; | |
14695 | ||
14696 | case DEDUCE_EXACT: | |
14697 | sub_strict = UNIFY_ALLOW_NONE; | |
14698 | break; | |
14699 | ||
14700 | default: | |
14701 | gcc_unreachable (); | |
14702 | } | |
14703 | ||
14704 | if (!TYPE_P (arg)) | |
14705 | { | |
14706 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); | |
14707 | if (type_unknown_p (arg)) | |
14708 | { | |
14709 | /* [temp.deduct.type] A template-argument can be | |
14710 | deduced from a pointer to function or pointer | |
14711 | to member function argument if the set of | |
14712 | overloaded functions does not contain function | |
14713 | templates and at most one of a set of | |
14714 | overloaded functions provides a unique | |
14715 | match. */ | |
14716 | ||
14717 | if (resolve_overloaded_unification | |
a916d97f ILT |
14718 | (tparms, targs, parm, arg, |
14719 | (unification_kind_t) strict, | |
14720 | sub_strict) | |
5d80a306 DG |
14721 | != 0) |
14722 | return 1; | |
14723 | skip_arg_p = true; | |
14724 | } | |
14725 | ||
14726 | if (!skip_arg_p) | |
14727 | { | |
2afad0f6 DG |
14728 | arg_expr = arg; |
14729 | arg = unlowered_expr_type (arg); | |
5d80a306 DG |
14730 | if (arg == error_mark_node) |
14731 | return 1; | |
14732 | } | |
14733 | } | |
14734 | ||
14735 | arg_strict = sub_strict; | |
14736 | ||
14737 | if (!subr) | |
14738 | arg_strict |= | |
a916d97f ILT |
14739 | maybe_adjust_types_for_deduction ((unification_kind_t) strict, |
14740 | &parm, &arg, arg_expr); | |
5d80a306 DG |
14741 | } |
14742 | ||
14743 | if (!skip_arg_p) | |
14744 | { | |
d097567d JM |
14745 | /* For deduction from an init-list we need the actual list. */ |
14746 | if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr)) | |
14747 | arg = arg_expr; | |
5d80a306 DG |
14748 | if (unify (tparms, targs, parm, arg, arg_strict)) |
14749 | return 1; | |
14750 | } | |
14751 | } | |
14752 | ||
14753 | /* For each parameter pack, collect the deduced value. */ | |
14754 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
14755 | { | |
14756 | int idx, level; | |
14757 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
14758 | ||
14759 | TREE_VEC_ELT (TREE_TYPE (pack), i - start) = | |
14760 | TMPL_ARG (targs, level, idx); | |
14761 | } | |
14762 | } | |
14763 | ||
14764 | /* Verify that the results of unification with the parameter packs | |
14765 | produce results consistent with what we've seen before, and make | |
14766 | the deduced argument packs available. */ | |
14767 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
14768 | { | |
14769 | tree old_pack = TREE_VALUE (pack); | |
14770 | tree new_args = TREE_TYPE (pack); | |
db60ff18 | 14771 | int i, len = TREE_VEC_LENGTH (new_args); |
c8f4e43a | 14772 | int idx, level; |
db60ff18 DG |
14773 | bool nondeduced_p = false; |
14774 | ||
c8f4e43a DS |
14775 | /* By default keep the original deduced argument pack. |
14776 | If necessary, more specific code is going to update the | |
14777 | resulting deduced argument later down in this function. */ | |
14778 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
14779 | TMPL_ARG (targs, level, idx) = old_pack; | |
14780 | ||
db60ff18 DG |
14781 | /* If NEW_ARGS contains any NULL_TREE entries, we didn't |
14782 | actually deduce anything. */ | |
14783 | for (i = 0; i < len && !nondeduced_p; ++i) | |
14784 | if (TREE_VEC_ELT (new_args, i) == NULL_TREE) | |
14785 | nondeduced_p = true; | |
14786 | if (nondeduced_p) | |
14787 | continue; | |
5d80a306 DG |
14788 | |
14789 | if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack)) | |
14790 | { | |
14791 | /* Prepend the explicit arguments onto NEW_ARGS. */ | |
14792 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); | |
14793 | tree old_args = new_args; | |
14794 | int i, explicit_len = TREE_VEC_LENGTH (explicit_args); | |
14795 | int len = explicit_len + TREE_VEC_LENGTH (old_args); | |
14796 | ||
14797 | /* Copy the explicit arguments. */ | |
14798 | new_args = make_tree_vec (len); | |
14799 | for (i = 0; i < explicit_len; i++) | |
14800 | TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i); | |
14801 | ||
14802 | /* Copy the deduced arguments. */ | |
14803 | for (; i < len; i++) | |
14804 | TREE_VEC_ELT (new_args, i) = | |
14805 | TREE_VEC_ELT (old_args, i - explicit_len); | |
14806 | } | |
14807 | ||
14808 | if (!old_pack) | |
14809 | { | |
14810 | tree result; | |
5d80a306 DG |
14811 | /* Build the deduced *_ARGUMENT_PACK. */ |
14812 | if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX) | |
14813 | { | |
14814 | result = make_node (NONTYPE_ARGUMENT_PACK); | |
14815 | TREE_TYPE (result) = | |
14816 | TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack))); | |
14817 | TREE_CONSTANT (result) = 1; | |
14818 | } | |
14819 | else | |
7ecbca9d | 14820 | result = cxx_make_type (TYPE_ARGUMENT_PACK); |
5d80a306 DG |
14821 | |
14822 | SET_ARGUMENT_PACK_ARGS (result, new_args); | |
14823 | ||
14824 | /* Note the deduced argument packs for this parameter | |
14825 | pack. */ | |
14826 | TMPL_ARG (targs, level, idx) = result; | |
14827 | } | |
14828 | else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack) | |
14829 | && (ARGUMENT_PACK_ARGS (old_pack) | |
14830 | == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack))) | |
14831 | { | |
14832 | /* We only had the explicitly-provided arguments before, but | |
14833 | now we have a complete set of arguments. */ | |
5d80a306 | 14834 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); |
5d80a306 DG |
14835 | |
14836 | SET_ARGUMENT_PACK_ARGS (old_pack, new_args); | |
14837 | ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1; | |
14838 | ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args; | |
14839 | } | |
14840 | else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack), | |
14841 | new_args)) | |
14842 | /* Inconsistent unification of this parameter pack. */ | |
14843 | return 1; | |
5d80a306 DG |
14844 | } |
14845 | ||
14846 | return 0; | |
14847 | } | |
14848 | ||
916b63c3 MM |
14849 | /* Deduce the value of template parameters. TPARMS is the (innermost) |
14850 | set of template parameters to a template. TARGS is the bindings | |
14851 | for those template parameters, as determined thus far; TARGS may | |
14852 | include template arguments for outer levels of template parameters | |
14853 | as well. PARM is a parameter to a template function, or a | |
14854 | subcomponent of that parameter; ARG is the corresponding argument. | |
14855 | This function attempts to match PARM with ARG in a manner | |
14856 | consistent with the existing assignments in TARGS. If more values | |
14857 | are deduced, then TARGS is updated. | |
14858 | ||
14859 | Returns 0 if the type deduction succeeds, 1 otherwise. The | |
14860 | parameter STRICT is a bitwise or of the following flags: | |
830bfa74 MM |
14861 | |
14862 | UNIFY_ALLOW_NONE: | |
14863 | Require an exact match between PARM and ARG. | |
14864 | UNIFY_ALLOW_MORE_CV_QUAL: | |
028d1f20 NS |
14865 | Allow the deduced ARG to be more cv-qualified (by qualification |
14866 | conversion) than ARG. | |
830bfa74 MM |
14867 | UNIFY_ALLOW_LESS_CV_QUAL: |
14868 | Allow the deduced ARG to be less cv-qualified than ARG. | |
14869 | UNIFY_ALLOW_DERIVED: | |
14870 | Allow the deduced ARG to be a template base class of ARG, | |
14871 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
14872 | ARG. |
14873 | UNIFY_ALLOW_INTEGER: | |
14874 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
c8094d83 | 14875 | case for more information. |
028d1f20 NS |
14876 | UNIFY_ALLOW_OUTER_LEVEL: |
14877 | This is the outermost level of a deduction. Used to determine validity | |
14878 | of qualification conversions. A valid qualification conversion must | |
14879 | have const qualified pointers leading up to the inner type which | |
14880 | requires additional CV quals, except at the outer level, where const | |
14881 | is not required [conv.qual]. It would be normal to set this flag in | |
62e4a758 NS |
14882 | addition to setting UNIFY_ALLOW_MORE_CV_QUAL. |
14883 | UNIFY_ALLOW_OUTER_MORE_CV_QUAL: | |
14884 | This is the outermost level of a deduction, and PARM can be more CV | |
14885 | qualified at this point. | |
14886 | UNIFY_ALLOW_OUTER_LESS_CV_QUAL: | |
14887 | This is the outermost level of a deduction, and PARM can be less CV | |
8baddbf1 | 14888 | qualified at this point. */ |
830bfa74 | 14889 | |
e9659ab0 | 14890 | static int |
3a978d72 | 14891 | unify (tree tparms, tree targs, tree parm, tree arg, int strict) |
8d08fdba MS |
14892 | { |
14893 | int idx; | |
050367a3 | 14894 | tree targ; |
db2767b6 | 14895 | tree tparm; |
028d1f20 | 14896 | int strict_in = strict; |
8d08fdba MS |
14897 | |
14898 | /* I don't think this will do the right thing with respect to types. | |
14899 | But the only case I've seen it in so far has been array bounds, where | |
14900 | signedness is the only information lost, and I think that will be | |
14901 | okay. */ | |
14902 | while (TREE_CODE (parm) == NOP_EXPR) | |
14903 | parm = TREE_OPERAND (parm, 0); | |
14904 | ||
14905 | if (arg == error_mark_node) | |
14906 | return 1; | |
09357846 JM |
14907 | if (arg == unknown_type_node |
14908 | || arg == init_list_type_node) | |
34016c81 JM |
14909 | /* We can't deduce anything from this, but we might get all the |
14910 | template args from other function args. */ | |
14911 | return 0; | |
14912 | ||
db2767b6 | 14913 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 14914 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
14915 | template parameters. We might need them if we're trying to |
14916 | figure out which of two things is more specialized. */ | |
14917 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
14918 | return 0; |
14919 | ||
09357846 JM |
14920 | /* Handle init lists early, so the rest of the function can assume |
14921 | we're dealing with a type. */ | |
14922 | if (BRACE_ENCLOSED_INITIALIZER_P (arg)) | |
14923 | { | |
14924 | tree elt, elttype; | |
14925 | unsigned i; | |
5f5babf1 JM |
14926 | tree orig_parm = parm; |
14927 | ||
14928 | /* Replace T with std::initializer_list<T> for deduction. */ | |
14929 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
14930 | && flag_deduce_init_list) | |
14931 | parm = listify (parm); | |
09357846 JM |
14932 | |
14933 | if (!is_std_init_list (parm)) | |
14934 | /* We can only deduce from an initializer list argument if the | |
14935 | parameter is std::initializer_list; otherwise this is a | |
14936 | non-deduced context. */ | |
14937 | return 0; | |
14938 | ||
14939 | elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0); | |
14940 | ||
14941 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt) | |
14942 | { | |
2604de9d | 14943 | int elt_strict = strict; |
68fef9f9 PC |
14944 | |
14945 | if (elt == error_mark_node) | |
14946 | return 1; | |
14947 | ||
09357846 | 14948 | if (!BRACE_ENCLOSED_INITIALIZER_P (elt)) |
2604de9d JM |
14949 | { |
14950 | tree type = TREE_TYPE (elt); | |
14951 | /* It should only be possible to get here for a call. */ | |
14952 | gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL); | |
14953 | elt_strict |= maybe_adjust_types_for_deduction | |
14954 | (DEDUCE_CALL, &elttype, &type, elt); | |
14955 | elt = type; | |
14956 | } | |
14957 | ||
14958 | if (unify (tparms, targs, elttype, elt, elt_strict)) | |
09357846 JM |
14959 | return 1; |
14960 | } | |
5f5babf1 JM |
14961 | |
14962 | /* If the std::initializer_list<T> deduction worked, replace the | |
14963 | deduced A with std::initializer_list<A>. */ | |
14964 | if (orig_parm != parm) | |
14965 | { | |
14966 | idx = TEMPLATE_TYPE_IDX (orig_parm); | |
14967 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); | |
14968 | targ = listify (targ); | |
14969 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ; | |
14970 | } | |
09357846 JM |
14971 | return 0; |
14972 | } | |
14973 | ||
830bfa74 MM |
14974 | /* Immediately reject some pairs that won't unify because of |
14975 | cv-qualification mismatches. */ | |
14976 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
2f939d94 | 14977 | && TYPE_P (arg) |
d0ab7624 | 14978 | /* It is the elements of the array which hold the cv quals of an array |
0cbd7506 MS |
14979 | type, and the elements might be template type parms. We'll check |
14980 | when we recurse. */ | |
d0ab7624 | 14981 | && TREE_CODE (arg) != ARRAY_TYPE |
830bfa74 MM |
14982 | /* We check the cv-qualifiers when unifying with template type |
14983 | parameters below. We want to allow ARG `const T' to unify with | |
14984 | PARM `T' for example, when computing which of two templates | |
14985 | is more specialized, for example. */ | |
14986 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
028d1f20 | 14987 | && !check_cv_quals_for_unify (strict_in, arg, parm)) |
49432171 JM |
14988 | return 1; |
14989 | ||
028d1f20 | 14990 | if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) |
3ea099f1 | 14991 | && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) |
028d1f20 NS |
14992 | strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; |
14993 | strict &= ~UNIFY_ALLOW_OUTER_LEVEL; | |
14994 | strict &= ~UNIFY_ALLOW_DERIVED; | |
62e4a758 NS |
14995 | strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
14996 | strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; | |
c8094d83 | 14997 | |
8d08fdba MS |
14998 | switch (TREE_CODE (parm)) |
14999 | { | |
2ca340ae | 15000 | case TYPENAME_TYPE: |
fccef71e | 15001 | case SCOPE_REF: |
b8c6534b | 15002 | case UNBOUND_CLASS_TEMPLATE: |
2ca340ae JM |
15003 | /* In a type which contains a nested-name-specifier, template |
15004 | argument values cannot be deduced for template parameters used | |
15005 | within the nested-name-specifier. */ | |
15006 | return 0; | |
15007 | ||
8d08fdba | 15008 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 15009 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 15010 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
db2767b6 | 15011 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
833be229 PC |
15012 | if (tparm == error_mark_node) |
15013 | return 1; | |
db2767b6 MM |
15014 | |
15015 | if (TEMPLATE_TYPE_LEVEL (parm) | |
15016 | != template_decl_level (tparm)) | |
15017 | /* The PARM is not one we're trying to unify. Just check | |
15018 | to see if it matches ARG. */ | |
15019 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 15020 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 15021 | idx = TEMPLATE_TYPE_IDX (parm); |
916b63c3 | 15022 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 15023 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 15024 | |
73b0fce8 | 15025 | /* Check for mixed types and values. */ |
db2767b6 MM |
15026 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
15027 | && TREE_CODE (tparm) != TYPE_DECL) | |
c8094d83 | 15028 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
db2767b6 | 15029 | && TREE_CODE (tparm) != TEMPLATE_DECL)) |
73b0fce8 KL |
15030 | return 1; |
15031 | ||
a1281f45 | 15032 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 15033 | { |
b429fdf0 KL |
15034 | /* ARG must be constructed from a template class or a template |
15035 | template parameter. */ | |
15036 | if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM | |
4e95268d | 15037 | && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) |
a1281f45 | 15038 | return 1; |
73b0fce8 | 15039 | |
a1281f45 | 15040 | { |
a1281f45 | 15041 | tree parmvec = TYPE_TI_ARGS (parm); |
6df91b00 | 15042 | tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg)); |
6f1abb06 | 15043 | tree full_argvec = add_to_template_args (targs, argvec); |
a0a5f30f | 15044 | tree parm_parms |
063cc99c | 15045 | = DECL_INNERMOST_TEMPLATE_PARMS |
a0a5f30f | 15046 | (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm)); |
4cf36211 DG |
15047 | int i, len; |
15048 | int parm_variadic_p = 0; | |
73b0fce8 | 15049 | |
e7e93965 MM |
15050 | /* The resolution to DR150 makes clear that default |
15051 | arguments for an N-argument may not be used to bind T | |
15052 | to a template template parameter with fewer than N | |
15053 | parameters. It is not safe to permit the binding of | |
15054 | default arguments as an extension, as that may change | |
15055 | the meaning of a conforming program. Consider: | |
15056 | ||
15057 | struct Dense { static const unsigned int dim = 1; }; | |
15058 | ||
15059 | template <template <typename> class View, | |
15060 | typename Block> | |
15061 | void operator+(float, View<Block> const&); | |
15062 | ||
3db45ab5 MS |
15063 | template <typename Block, |
15064 | unsigned int Dim = Block::dim> | |
e7e93965 MM |
15065 | struct Lvalue_proxy { operator float() const; }; |
15066 | ||
15067 | void | |
15068 | test_1d (void) { | |
15069 | Lvalue_proxy<Dense> p; | |
15070 | float b; | |
15071 | b + p; | |
15072 | } | |
a1281f45 | 15073 | |
e7e93965 MM |
15074 | Here, if Lvalue_proxy is permitted to bind to View, then |
15075 | the global operator+ will be used; if they are not, the | |
3db45ab5 | 15076 | Lvalue_proxy will be converted to float. */ |
a0a5f30f | 15077 | if (coerce_template_parms (parm_parms, |
6f1abb06 | 15078 | full_argvec, |
e7e93965 MM |
15079 | TYPE_TI_TEMPLATE (parm), |
15080 | tf_none, | |
15081 | /*require_all_args=*/true, | |
15082 | /*use_default_args=*/false) | |
0cbd7506 | 15083 | == error_mark_node) |
a1281f45 | 15084 | return 1; |
c8094d83 MS |
15085 | |
15086 | /* Deduce arguments T, i from TT<T> or TT<i>. | |
a1281f45 KL |
15087 | We check each element of PARMVEC and ARGVEC individually |
15088 | rather than the whole TREE_VEC since they can have | |
15089 | different number of elements. */ | |
6b9b6b15 | 15090 | |
4cf36211 DG |
15091 | parmvec = expand_template_argument_pack (parmvec); |
15092 | argvec = expand_template_argument_pack (argvec); | |
15093 | ||
15094 | len = TREE_VEC_LENGTH (parmvec); | |
15095 | ||
15096 | /* Check if the parameters end in a pack, making them | |
15097 | variadic. */ | |
15098 | if (len > 0 | |
15099 | && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1))) | |
15100 | parm_variadic_p = 1; | |
15101 | ||
15102 | if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p) | |
15103 | return 1; | |
15104 | ||
15105 | for (i = 0; i < len - parm_variadic_p; ++i) | |
a1281f45 | 15106 | { |
0cbd7506 | 15107 | if (unify (tparms, targs, |
c8094d83 MS |
15108 | TREE_VEC_ELT (parmvec, i), |
15109 | TREE_VEC_ELT (argvec, i), | |
a1281f45 KL |
15110 | UNIFY_ALLOW_NONE)) |
15111 | return 1; | |
73b0fce8 | 15112 | } |
4cf36211 DG |
15113 | |
15114 | if (parm_variadic_p | |
15115 | && unify_pack_expansion (tparms, targs, | |
15116 | parmvec, argvec, | |
15117 | UNIFY_ALLOW_NONE, | |
15118 | /*call_args_p=*/false, | |
15119 | /*subr=*/false)) | |
15120 | return 1; | |
a1281f45 | 15121 | } |
b429fdf0 | 15122 | arg = TYPE_TI_TEMPLATE (arg); |
a1281f45 KL |
15123 | |
15124 | /* Fall through to deduce template name. */ | |
15125 | } | |
15126 | ||
15127 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
15128 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
15129 | { | |
15130 | /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ | |
15131 | ||
15132 | /* Simple cases: Value already set, does match or doesn't. */ | |
15133 | if (targ != NULL_TREE && template_args_equal (targ, arg)) | |
15134 | return 0; | |
15135 | else if (targ) | |
15136 | return 1; | |
db2767b6 MM |
15137 | } |
15138 | else | |
15139 | { | |
830bfa74 MM |
15140 | /* If PARM is `const T' and ARG is only `int', we don't have |
15141 | a match unless we are allowing additional qualification. | |
15142 | If ARG is `const int' and PARM is just `T' that's OK; | |
15143 | that binds `const int' to `T'. */ | |
c8094d83 | 15144 | if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, |
830bfa74 | 15145 | arg, parm)) |
db2767b6 MM |
15146 | return 1; |
15147 | ||
830bfa74 MM |
15148 | /* Consider the case where ARG is `const volatile int' and |
15149 | PARM is `const T'. Then, T should be `volatile int'. */ | |
c2ea3a40 NS |
15150 | arg = cp_build_qualified_type_real |
15151 | (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); | |
adecb3f4 MM |
15152 | if (arg == error_mark_node) |
15153 | return 1; | |
73b0fce8 | 15154 | |
a1281f45 KL |
15155 | /* Simple cases: Value already set, does match or doesn't. */ |
15156 | if (targ != NULL_TREE && same_type_p (targ, arg)) | |
15157 | return 0; | |
15158 | else if (targ) | |
15159 | return 1; | |
61cd552e | 15160 | |
94fc547c MM |
15161 | /* Make sure that ARG is not a variable-sized array. (Note |
15162 | that were talking about variable-sized arrays (like | |
15163 | `int[n]'), rather than arrays of unknown size (like | |
15164 | `int[]').) We'll get very confused by such a type since | |
15165 | the bound of the array will not be computable in an | |
15166 | instantiation. Besides, such types are not allowed in | |
15167 | ISO C++, so we can do as we please here. */ | |
5377d5ba | 15168 | if (variably_modified_type_p (arg, NULL_TREE)) |
94fc547c | 15169 | return 1; |
a4d674fc JM |
15170 | |
15171 | /* Strip typedefs as in convert_template_argument. */ | |
cd41d410 | 15172 | arg = strip_typedefs (arg); |
94fc547c | 15173 | } |
61cd552e | 15174 | |
5d80a306 DG |
15175 | /* If ARG is a parameter pack or an expansion, we cannot unify |
15176 | against it unless PARM is also a parameter pack. */ | |
15177 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
15178 | && !template_parameter_pack_p (parm)) | |
15179 | return 1; | |
15180 | ||
111a28c2 DS |
15181 | /* If the argument deduction results is a METHOD_TYPE, |
15182 | then there is a problem. | |
15183 | METHOD_TYPE doesn't map to any real C++ type the result of | |
15184 | the deduction can not be of that type. */ | |
15185 | if (TREE_CODE (arg) == METHOD_TYPE) | |
15186 | return 1; | |
15187 | ||
916b63c3 | 15188 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
73b0fce8 KL |
15189 | return 0; |
15190 | ||
f84b4be9 | 15191 | case TEMPLATE_PARM_INDEX: |
db2767b6 | 15192 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
3e9ac7e5 VR |
15193 | if (tparm == error_mark_node) |
15194 | return 1; | |
db2767b6 | 15195 | |
c8094d83 | 15196 | if (TEMPLATE_PARM_LEVEL (parm) |
db2767b6 MM |
15197 | != template_decl_level (tparm)) |
15198 | /* The PARM is not one we're trying to unify. Just check | |
15199 | to see if it matches ARG. */ | |
c8a209ca NS |
15200 | return !(TREE_CODE (arg) == TREE_CODE (parm) |
15201 | && cp_tree_equal (parm, arg)); | |
db2767b6 | 15202 | |
f84b4be9 | 15203 | idx = TEMPLATE_PARM_IDX (parm); |
916b63c3 | 15204 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 15205 | |
050367a3 | 15206 | if (targ) |
c8a209ca | 15207 | return !cp_tree_equal (targ, arg); |
8d08fdba | 15208 | |
161c12b0 JM |
15209 | /* [temp.deduct.type] If, in the declaration of a function template |
15210 | with a non-type template-parameter, the non-type | |
15211 | template-parameter is used in an expression in the function | |
15212 | parameter-list and, if the corresponding template-argument is | |
15213 | deduced, the template-argument type shall match the type of the | |
15214 | template-parameter exactly, except that a template-argument | |
c8094d83 | 15215 | deduced from an array bound may be of any integral type. |
d7c4edd0 | 15216 | The non-type parameter might use already deduced type parameters. */ |
bd0d5d4a | 15217 | tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); |
e2005c8d KL |
15218 | if (!TREE_TYPE (arg)) |
15219 | /* Template-parameter dependent expression. Just accept it for now. | |
15220 | It will later be processed in convert_template_argument. */ | |
15221 | ; | |
15222 | else if (same_type_p (TREE_TYPE (arg), tparm)) | |
15223 | /* OK */; | |
161c12b0 | 15224 | else if ((strict & UNIFY_ALLOW_INTEGER) |
bd0d5d4a JM |
15225 | && (TREE_CODE (tparm) == INTEGER_TYPE |
15226 | || TREE_CODE (tparm) == BOOLEAN_TYPE)) | |
8baddbf1 MM |
15227 | /* Convert the ARG to the type of PARM; the deduced non-type |
15228 | template argument must exactly match the types of the | |
15229 | corresponding parameter. */ | |
ed7284dd | 15230 | arg = fold (build_nop (tparm, arg)); |
bd0d5d4a JM |
15231 | else if (uses_template_parms (tparm)) |
15232 | /* We haven't deduced the type of this parameter yet. Try again | |
15233 | later. */ | |
15234 | return 0; | |
161c12b0 JM |
15235 | else |
15236 | return 1; | |
15237 | ||
5d80a306 DG |
15238 | /* If ARG is a parameter pack or an expansion, we cannot unify |
15239 | against it unless PARM is also a parameter pack. */ | |
15240 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
15241 | && !TEMPLATE_PARM_PARAMETER_PACK (parm)) | |
15242 | return 1; | |
15243 | ||
916b63c3 | 15244 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
8d08fdba MS |
15245 | return 0; |
15246 | ||
28e8f3a0 GB |
15247 | case PTRMEM_CST: |
15248 | { | |
0cbd7506 MS |
15249 | /* A pointer-to-member constant can be unified only with |
15250 | another constant. */ | |
28e8f3a0 | 15251 | if (TREE_CODE (arg) != PTRMEM_CST) |
0cbd7506 | 15252 | return 1; |
28e8f3a0 GB |
15253 | |
15254 | /* Just unify the class member. It would be useless (and possibly | |
0cbd7506 MS |
15255 | wrong, depending on the strict flags) to unify also |
15256 | PTRMEM_CST_CLASS, because we want to be sure that both parm and | |
15257 | arg refer to the same variable, even if through different | |
15258 | classes. For instance: | |
28e8f3a0 | 15259 | |
0cbd7506 MS |
15260 | struct A { int x; }; |
15261 | struct B : A { }; | |
28e8f3a0 | 15262 | |
0cbd7506 | 15263 | Unification of &A::x and &B::x must succeed. */ |
28e8f3a0 | 15264 | return unify (tparms, targs, PTRMEM_CST_MEMBER (parm), |
0cbd7506 | 15265 | PTRMEM_CST_MEMBER (arg), strict); |
28e8f3a0 GB |
15266 | } |
15267 | ||
8d08fdba | 15268 | case POINTER_TYPE: |
830bfa74 | 15269 | { |
830bfa74 MM |
15270 | if (TREE_CODE (arg) != POINTER_TYPE) |
15271 | return 1; | |
c8094d83 | 15272 | |
830bfa74 MM |
15273 | /* [temp.deduct.call] |
15274 | ||
15275 | A can be another pointer or pointer to member type that can | |
15276 | be converted to the deduced A via a qualification | |
15277 | conversion (_conv.qual_). | |
15278 | ||
15279 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
15280 | This will allow for additional cv-qualification of the | |
028d1f20 | 15281 | pointed-to types if appropriate. */ |
c8094d83 | 15282 | |
028d1f20 | 15283 | if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) |
830bfa74 MM |
15284 | /* The derived-to-base conversion only persists through one |
15285 | level of pointers. */ | |
028d1f20 | 15286 | strict |= (strict_in & UNIFY_ALLOW_DERIVED); |
830bfa74 | 15287 | |
c8094d83 | 15288 | return unify (tparms, targs, TREE_TYPE (parm), |
028d1f20 | 15289 | TREE_TYPE (arg), strict); |
830bfa74 | 15290 | } |
8d08fdba MS |
15291 | |
15292 | case REFERENCE_TYPE: | |
830bfa74 MM |
15293 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
15294 | return 1; | |
15295 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
028d1f20 | 15296 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
15297 | |
15298 | case ARRAY_TYPE: | |
15299 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
15300 | return 1; | |
3042d5be MM |
15301 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
15302 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
15303 | return 1; | |
8baddbf1 MM |
15304 | if (TYPE_DOMAIN (parm) != NULL_TREE) |
15305 | { | |
15306 | tree parm_max; | |
15307 | tree arg_max; | |
6ee3ffe8 MM |
15308 | bool parm_cst; |
15309 | bool arg_cst; | |
8baddbf1 MM |
15310 | |
15311 | /* Our representation of array types uses "N - 1" as the | |
15312 | TYPE_MAX_VALUE for an array with "N" elements, if "N" is | |
6ee3ffe8 MM |
15313 | not an integer constant. We cannot unify arbitrarily |
15314 | complex expressions, so we eliminate the MINUS_EXPRs | |
15315 | here. */ | |
15316 | parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm)); | |
15317 | parm_cst = TREE_CODE (parm_max) == INTEGER_CST; | |
15318 | if (!parm_cst) | |
8baddbf1 | 15319 | { |
6ee3ffe8 | 15320 | gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR); |
8baddbf1 MM |
15321 | parm_max = TREE_OPERAND (parm_max, 0); |
15322 | } | |
6ee3ffe8 MM |
15323 | arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg)); |
15324 | arg_cst = TREE_CODE (arg_max) == INTEGER_CST; | |
15325 | if (!arg_cst) | |
15326 | { | |
15327 | /* The ARG_MAX may not be a simple MINUS_EXPR, if we are | |
15328 | trying to unify the type of a variable with the type | |
15329 | of a template parameter. For example: | |
15330 | ||
15331 | template <unsigned int N> | |
15332 | void f (char (&) [N]); | |
15333 | int g(); | |
15334 | void h(int i) { | |
15335 | char a[g(i)]; | |
15336 | f(a); | |
15337 | } | |
15338 | ||
15339 | Here, the type of the ARG will be "int [g(i)]", and | |
15340 | may be a SAVE_EXPR, etc. */ | |
15341 | if (TREE_CODE (arg_max) != MINUS_EXPR) | |
15342 | return 1; | |
15343 | arg_max = TREE_OPERAND (arg_max, 0); | |
15344 | } | |
15345 | ||
15346 | /* If only one of the bounds used a MINUS_EXPR, compensate | |
15347 | by adding one to the other bound. */ | |
15348 | if (parm_cst && !arg_cst) | |
db3927fb | 15349 | parm_max = fold_build2_loc (input_location, PLUS_EXPR, |
6ee3ffe8 MM |
15350 | integer_type_node, |
15351 | parm_max, | |
15352 | integer_one_node); | |
15353 | else if (arg_cst && !parm_cst) | |
db3927fb | 15354 | arg_max = fold_build2_loc (input_location, PLUS_EXPR, |
6ee3ffe8 MM |
15355 | integer_type_node, |
15356 | arg_max, | |
15357 | integer_one_node); | |
8baddbf1 MM |
15358 | |
15359 | if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER)) | |
15360 | return 1; | |
15361 | } | |
830bfa74 | 15362 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
712467a4 | 15363 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
15364 | |
15365 | case REAL_TYPE: | |
37c46b43 | 15366 | case COMPLEX_TYPE: |
c00996a3 | 15367 | case VECTOR_TYPE: |
8d08fdba | 15368 | case INTEGER_TYPE: |
42976354 | 15369 | case BOOLEAN_TYPE: |
3590f0a6 | 15370 | case ENUMERAL_TYPE: |
5ad5a526 | 15371 | case VOID_TYPE: |
f376e137 MS |
15372 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
15373 | return 1; | |
c8094d83 | 15374 | |
9edc3913 | 15375 | /* We have already checked cv-qualification at the top of the |
514a1f18 | 15376 | function. */ |
8baddbf1 | 15377 | if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) |
ca79f85d JM |
15378 | return 1; |
15379 | ||
8d08fdba MS |
15380 | /* As far as unification is concerned, this wins. Later checks |
15381 | will invalidate it if necessary. */ | |
15382 | return 0; | |
15383 | ||
15384 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 15385 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 15386 | case INTEGER_CST: |
bd6dd845 MS |
15387 | while (TREE_CODE (arg) == NOP_EXPR) |
15388 | arg = TREE_OPERAND (arg, 0); | |
15389 | ||
8d08fdba MS |
15390 | if (TREE_CODE (arg) != INTEGER_CST) |
15391 | return 1; | |
15392 | return !tree_int_cst_equal (parm, arg); | |
15393 | ||
8d08fdba MS |
15394 | case TREE_VEC: |
15395 | { | |
15396 | int i; | |
15397 | if (TREE_CODE (arg) != TREE_VEC) | |
15398 | return 1; | |
15399 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
15400 | return 1; | |
0dc09a61 | 15401 | for (i = 0; i < TREE_VEC_LENGTH (parm); ++i) |
830bfa74 | 15402 | if (unify (tparms, targs, |
8d08fdba | 15403 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 15404 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
15405 | return 1; |
15406 | return 0; | |
15407 | } | |
15408 | ||
8d08fdba | 15409 | case RECORD_TYPE: |
f181d4ae | 15410 | case UNION_TYPE: |
f181d4ae | 15411 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 15412 | return 1; |
c8094d83 | 15413 | |
a7a64a77 MM |
15414 | if (TYPE_PTRMEMFUNC_P (parm)) |
15415 | { | |
15416 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
15417 | return 1; | |
15418 | ||
c8094d83 | 15419 | return unify (tparms, targs, |
a7a64a77 MM |
15420 | TYPE_PTRMEMFUNC_FN_TYPE (parm), |
15421 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
15422 | strict); | |
15423 | } | |
15424 | ||
5db698f6 | 15425 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 15426 | { |
6467930b | 15427 | tree t = NULL_TREE; |
4393e105 | 15428 | |
028d1f20 | 15429 | if (strict_in & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
15430 | { |
15431 | /* First, we try to unify the PARM and ARG directly. */ | |
15432 | t = try_class_unification (tparms, targs, | |
15433 | parm, arg); | |
15434 | ||
15435 | if (!t) | |
15436 | { | |
15437 | /* Fallback to the special case allowed in | |
15438 | [temp.deduct.call]: | |
c8094d83 | 15439 | |
4393e105 MM |
15440 | If P is a class, and P has the form |
15441 | template-id, then A can be a derived class of | |
15442 | the deduced A. Likewise, if P is a pointer to | |
15443 | a class of the form template-id, A can be a | |
15444 | pointer to a derived class pointed to by the | |
15445 | deduced A. */ | |
8d83768f | 15446 | t = get_template_base (tparms, targs, parm, arg); |
4393e105 | 15447 | |
8d83768f | 15448 | if (!t) |
4393e105 MM |
15449 | return 1; |
15450 | } | |
15451 | } | |
c8094d83 MS |
15452 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
15453 | && (CLASSTYPE_TI_TEMPLATE (parm) | |
9fbf56f7 | 15454 | == CLASSTYPE_TI_TEMPLATE (arg))) |
6df47b06 MM |
15455 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
15456 | Then, we should unify `int' and `U'. */ | |
6467930b | 15457 | t = arg; |
4393e105 | 15458 | else |
dc957d14 | 15459 | /* There's no chance of unification succeeding. */ |
5566b478 | 15460 | return 1; |
6467930b | 15461 | |
830bfa74 | 15462 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 15463 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 15464 | } |
9edc3913 | 15465 | else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) |
5566b478 | 15466 | return 1; |
a4443a08 | 15467 | return 0; |
8d08fdba MS |
15468 | |
15469 | case METHOD_TYPE: | |
8d08fdba | 15470 | case FUNCTION_TYPE: |
c166b898 ILT |
15471 | { |
15472 | unsigned int nargs; | |
15473 | tree *args; | |
15474 | tree a; | |
15475 | unsigned int i; | |
830bfa74 | 15476 | |
c166b898 ILT |
15477 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
15478 | return 1; | |
38d18b1a | 15479 | |
c166b898 ILT |
15480 | /* CV qualifications for methods can never be deduced, they must |
15481 | match exactly. We need to check them explicitly here, | |
15482 | because type_unification_real treats them as any other | |
15483 | cv-qualified parameter. */ | |
15484 | if (TREE_CODE (parm) == METHOD_TYPE | |
15485 | && (!check_cv_quals_for_unify | |
15486 | (UNIFY_ALLOW_NONE, | |
15487 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))), | |
15488 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm)))))) | |
15489 | return 1; | |
15490 | ||
15491 | if (unify (tparms, targs, TREE_TYPE (parm), | |
15492 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) | |
15493 | return 1; | |
15494 | ||
15495 | nargs = list_length (TYPE_ARG_TYPES (arg)); | |
15496 | args = XALLOCAVEC (tree, nargs); | |
15497 | for (a = TYPE_ARG_TYPES (arg), i = 0; | |
15498 | a != NULL_TREE && a != void_list_node; | |
15499 | a = TREE_CHAIN (a), ++i) | |
15500 | args[i] = TREE_VALUE (a); | |
15501 | nargs = i; | |
15502 | ||
15503 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), | |
15504 | args, nargs, 1, DEDUCE_EXACT, | |
15505 | LOOKUP_NORMAL); | |
15506 | } | |
a4443a08 MS |
15507 | |
15508 | case OFFSET_TYPE: | |
9804209d | 15509 | /* Unify a pointer to member with a pointer to member function, which |
0cbd7506 | 15510 | deduces the type of the member as a function type. */ |
9804209d | 15511 | if (TYPE_PTRMEMFUNC_P (arg)) |
0cbd7506 MS |
15512 | { |
15513 | tree method_type; | |
15514 | tree fntype; | |
0cbd7506 MS |
15515 | |
15516 | /* Check top-level cv qualifiers */ | |
15517 | if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm)) | |
15518 | return 1; | |
15519 | ||
15520 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), | |
15521 | TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE)) | |
15522 | return 1; | |
15523 | ||
15524 | /* Determine the type of the function we are unifying against. */ | |
15525 | method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg)); | |
15526 | fntype = | |
15527 | build_function_type (TREE_TYPE (method_type), | |
15528 | TREE_CHAIN (TYPE_ARG_TYPES (method_type))); | |
15529 | ||
15530 | /* Extract the cv-qualifiers of the member function from the | |
15531 | implicit object parameter and place them on the function | |
15532 | type to be restored later. */ | |
2872152c | 15533 | fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type)); |
0cbd7506 MS |
15534 | return unify (tparms, targs, TREE_TYPE (parm), fntype, strict); |
15535 | } | |
9804209d | 15536 | |
a4443a08 MS |
15537 | if (TREE_CODE (arg) != OFFSET_TYPE) |
15538 | return 1; | |
830bfa74 | 15539 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 15540 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 15541 | return 1; |
830bfa74 | 15542 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 15543 | strict); |
a4443a08 | 15544 | |
f62dbf03 | 15545 | case CONST_DECL: |
a723baf1 MM |
15546 | if (DECL_TEMPLATE_PARM_P (parm)) |
15547 | return unify (tparms, targs, DECL_INITIAL (parm), arg, strict); | |
c8094d83 | 15548 | if (arg != integral_constant_value (parm)) |
f62dbf03 JM |
15549 | return 1; |
15550 | return 0; | |
15551 | ||
28e8f3a0 | 15552 | case FIELD_DECL: |
027905b4 KL |
15553 | case TEMPLATE_DECL: |
15554 | /* Matched cases are handled by the ARG == PARM test above. */ | |
15555 | return 1; | |
15556 | ||
5f431950 DS |
15557 | case VAR_DECL: |
15558 | /* A non-type template parameter that is a variable should be a | |
15559 | an integral constant, in which case, it whould have been | |
15560 | folded into its (constant) value. So we should not be getting | |
15561 | a variable here. */ | |
15562 | gcc_unreachable (); | |
15563 | ||
5d80a306 DG |
15564 | case TYPE_ARGUMENT_PACK: |
15565 | case NONTYPE_ARGUMENT_PACK: | |
15566 | { | |
15567 | tree packed_parms = ARGUMENT_PACK_ARGS (parm); | |
15568 | tree packed_args = ARGUMENT_PACK_ARGS (arg); | |
15569 | int i, len = TREE_VEC_LENGTH (packed_parms); | |
15570 | int argslen = TREE_VEC_LENGTH (packed_args); | |
15571 | int parm_variadic_p = 0; | |
15572 | ||
db60ff18 DG |
15573 | for (i = 0; i < len; ++i) |
15574 | { | |
15575 | if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i))) | |
15576 | { | |
15577 | if (i == len - 1) | |
15578 | /* We can unify against something with a trailing | |
15579 | parameter pack. */ | |
15580 | parm_variadic_p = 1; | |
15581 | else | |
15582 | /* Since there is something following the pack | |
15583 | expansion, we cannot unify this template argument | |
15584 | list. */ | |
15585 | return 0; | |
15586 | } | |
15587 | } | |
15588 | ||
5d80a306 DG |
15589 | |
15590 | /* If we don't have enough arguments to satisfy the parameters | |
15591 | (not counting the pack expression at the end), or we have | |
15592 | too many arguments for a parameter list that doesn't end in | |
15593 | a pack expression, we can't unify. */ | |
15594 | if (argslen < (len - parm_variadic_p) | |
15595 | || (argslen > len && !parm_variadic_p)) | |
15596 | return 1; | |
15597 | ||
15598 | /* Unify all of the parameters that precede the (optional) | |
15599 | pack expression. */ | |
15600 | for (i = 0; i < len - parm_variadic_p; ++i) | |
15601 | { | |
15602 | if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i), | |
15603 | TREE_VEC_ELT (packed_args, i), strict)) | |
15604 | return 1; | |
15605 | } | |
15606 | ||
15607 | if (parm_variadic_p) | |
15608 | return unify_pack_expansion (tparms, targs, | |
15609 | packed_parms, packed_args, | |
15610 | strict, /*call_args_p=*/false, | |
15611 | /*subr=*/false); | |
15612 | return 0; | |
15613 | } | |
15614 | ||
15615 | break; | |
15616 | ||
3ad6a8e1 DG |
15617 | case TYPEOF_TYPE: |
15618 | case DECLTYPE_TYPE: | |
15619 | /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE | |
15620 | nodes. */ | |
15621 | return 0; | |
15622 | ||
1ad8aeeb DG |
15623 | case ERROR_MARK: |
15624 | /* Unification fails if we hit an error node. */ | |
15625 | return 1; | |
15626 | ||
8d08fdba | 15627 | default: |
6615c446 | 15628 | gcc_assert (EXPR_P (parm)); |
c8094d83 | 15629 | |
98ddffc1 | 15630 | /* We must be looking at an expression. This can happen with |
c8094d83 MS |
15631 | something like: |
15632 | ||
98ddffc1 NS |
15633 | template <int I> |
15634 | void foo(S<I>, S<I + 2>); | |
050367a3 | 15635 | |
98ddffc1 | 15636 | This is a "nondeduced context": |
050367a3 | 15637 | |
98ddffc1 | 15638 | [deduct.type] |
c8094d83 | 15639 | |
98ddffc1 | 15640 | The nondeduced contexts are: |
050367a3 | 15641 | |
98ddffc1 NS |
15642 | --A type that is a template-id in which one or more of |
15643 | the template-arguments is an expression that references | |
c8094d83 | 15644 | a template-parameter. |
050367a3 | 15645 | |
98ddffc1 NS |
15646 | In these cases, we assume deduction succeeded, but don't |
15647 | actually infer any unifications. */ | |
74601d7c | 15648 | |
98ddffc1 NS |
15649 | if (!uses_template_parms (parm) |
15650 | && !template_args_equal (parm, arg)) | |
15651 | return 1; | |
15652 | else | |
15653 | return 0; | |
8d08fdba MS |
15654 | } |
15655 | } | |
8d08fdba | 15656 | \f |
4684cd27 MM |
15657 | /* Note that DECL can be defined in this translation unit, if |
15658 | required. */ | |
15659 | ||
15660 | static void | |
15661 | mark_definable (tree decl) | |
15662 | { | |
15663 | tree clone; | |
15664 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
15665 | FOR_EACH_CLONE (clone, decl) | |
15666 | DECL_NOT_REALLY_EXTERN (clone) = 1; | |
15667 | } | |
15668 | ||
03d0f4af | 15669 | /* Called if RESULT is explicitly instantiated, or is a member of an |
4684cd27 | 15670 | explicitly instantiated class. */ |
03d0f4af | 15671 | |
faae18ab | 15672 | void |
3a978d72 | 15673 | mark_decl_instantiated (tree result, int extern_p) |
faae18ab | 15674 | { |
415c974c | 15675 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
3ae18eaf | 15676 | |
1f6f0cb6 MM |
15677 | /* If this entity has already been written out, it's too late to |
15678 | make any modifications. */ | |
15679 | if (TREE_ASM_WRITTEN (result)) | |
15680 | return; | |
15681 | ||
15682 | if (TREE_CODE (result) != FUNCTION_DECL) | |
15683 | /* The TREE_PUBLIC flag for function declarations will have been | |
15684 | set correctly by tsubst. */ | |
15685 | TREE_PUBLIC (result) = 1; | |
15686 | ||
346eeb15 JM |
15687 | /* This might have been set by an earlier implicit instantiation. */ |
15688 | DECL_COMDAT (result) = 0; | |
15689 | ||
4684cd27 MM |
15690 | if (extern_p) |
15691 | DECL_NOT_REALLY_EXTERN (result) = 0; | |
15692 | else | |
faae18ab | 15693 | { |
4684cd27 | 15694 | mark_definable (result); |
1a408d07 JM |
15695 | /* Always make artificials weak. */ |
15696 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
15697 | comdat_linkage (result); | |
a7d87521 JM |
15698 | /* For WIN32 we also want to put explicit instantiations in |
15699 | linkonce sections. */ | |
1a408d07 | 15700 | else if (TREE_PUBLIC (result)) |
b385c841 | 15701 | maybe_make_one_only (result); |
faae18ab | 15702 | } |
c8094d83 | 15703 | |
4684cd27 MM |
15704 | /* If EXTERN_P, then this function will not be emitted -- unless |
15705 | followed by an explicit instantiation, at which point its linkage | |
15706 | will be adjusted. If !EXTERN_P, then this function will be | |
15707 | emitted here. In neither circumstance do we want | |
15708 | import_export_decl to adjust the linkage. */ | |
c8094d83 | 15709 | DECL_INTERFACE_KNOWN (result) = 1; |
faae18ab MS |
15710 | } |
15711 | ||
9d79aec3 JM |
15712 | /* Subroutine of more_specialized_fn: check whether TARGS is missing any |
15713 | important template arguments. If any are missing, we check whether | |
15714 | they're important by using error_mark_node for substituting into any | |
15715 | args that were used for partial ordering (the ones between ARGS and END) | |
15716 | and seeing if it bubbles up. */ | |
15717 | ||
15718 | static bool | |
15719 | check_undeduced_parms (tree targs, tree args, tree end) | |
15720 | { | |
15721 | bool found = false; | |
15722 | int i; | |
15723 | for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i) | |
15724 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) | |
15725 | { | |
15726 | found = true; | |
15727 | TREE_VEC_ELT (targs, i) = error_mark_node; | |
15728 | } | |
15729 | if (found) | |
15730 | { | |
15731 | for (; args != end; args = TREE_CHAIN (args)) | |
15732 | { | |
15733 | tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE); | |
15734 | if (substed == error_mark_node) | |
15735 | return true; | |
15736 | } | |
15737 | } | |
15738 | return false; | |
15739 | } | |
15740 | ||
e5214479 | 15741 | /* Given two function templates PAT1 and PAT2, return: |
6467930b MS |
15742 | |
15743 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
15744 | -1 if PAT2 is more specialized than PAT1. | |
e5214479 JM |
15745 | 0 if neither is more specialized. |
15746 | ||
dda04398 NS |
15747 | LEN indicates the number of parameters we should consider |
15748 | (defaulted parameters should not be considered). | |
15749 | ||
15750 | The 1998 std underspecified function template partial ordering, and | |
15751 | DR214 addresses the issue. We take pairs of arguments, one from | |
c51940a2 | 15752 | each of the templates, and deduce them against each other. One of |
dda04398 NS |
15753 | the templates will be more specialized if all the *other* |
15754 | template's arguments deduce against its arguments and at least one | |
15755 | of its arguments *does* *not* deduce against the other template's | |
15756 | corresponding argument. Deduction is done as for class templates. | |
15757 | The arguments used in deduction have reference and top level cv | |
15758 | qualifiers removed. Iff both arguments were originally reference | |
15759 | types *and* deduction succeeds in both directions, the template | |
15760 | with the more cv-qualified argument wins for that pairing (if | |
15761 | neither is more cv-qualified, they both are equal). Unlike regular | |
15762 | deduction, after all the arguments have been deduced in this way, | |
15763 | we do *not* verify the deduced template argument values can be | |
9d79aec3 JM |
15764 | substituted into non-deduced contexts. |
15765 | ||
15766 | The logic can be a bit confusing here, because we look at deduce1 and | |
15767 | targs1 to see if pat2 is at least as specialized, and vice versa; if we | |
15768 | can find template arguments for pat1 to make arg1 look like arg2, that | |
15769 | means that arg2 is at least as specialized as arg1. */ | |
c8094d83 | 15770 | |
6467930b | 15771 | int |
dda04398 NS |
15772 | more_specialized_fn (tree pat1, tree pat2, int len) |
15773 | { | |
15774 | tree decl1 = DECL_TEMPLATE_RESULT (pat1); | |
15775 | tree decl2 = DECL_TEMPLATE_RESULT (pat2); | |
15776 | tree targs1 = make_tree_vec (DECL_NTPARMS (pat1)); | |
15777 | tree targs2 = make_tree_vec (DECL_NTPARMS (pat2)); | |
15778 | tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1); | |
15779 | tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2); | |
15780 | tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); | |
15781 | tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2)); | |
9d79aec3 JM |
15782 | tree origs1, origs2; |
15783 | bool lose1 = false; | |
15784 | bool lose2 = false; | |
3db45ab5 | 15785 | |
48884537 NS |
15786 | /* Remove the this parameter from non-static member functions. If |
15787 | one is a non-static member function and the other is not a static | |
15788 | member function, remove the first parameter from that function | |
15789 | also. This situation occurs for operator functions where we | |
15790 | locate both a member function (with this pointer) and non-member | |
15791 | operator (with explicit first operand). */ | |
dda04398 | 15792 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1)) |
48884537 NS |
15793 | { |
15794 | len--; /* LEN is the number of significant arguments for DECL1 */ | |
15795 | args1 = TREE_CHAIN (args1); | |
15796 | if (!DECL_STATIC_FUNCTION_P (decl2)) | |
15797 | args2 = TREE_CHAIN (args2); | |
15798 | } | |
15799 | else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2)) | |
15800 | { | |
15801 | args2 = TREE_CHAIN (args2); | |
15802 | if (!DECL_STATIC_FUNCTION_P (decl1)) | |
15803 | { | |
15804 | len--; | |
15805 | args1 = TREE_CHAIN (args1); | |
15806 | } | |
15807 | } | |
3db45ab5 | 15808 | |
ee307009 NS |
15809 | /* If only one is a conversion operator, they are unordered. */ |
15810 | if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2)) | |
15811 | return 0; | |
c8094d83 | 15812 | |
dda04398 NS |
15813 | /* Consider the return type for a conversion function */ |
15814 | if (DECL_CONV_FN_P (decl1)) | |
15815 | { | |
dda04398 NS |
15816 | args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1); |
15817 | args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2); | |
15818 | len++; | |
15819 | } | |
c8094d83 | 15820 | |
dda04398 | 15821 | processing_template_decl++; |
c8094d83 | 15822 | |
9d79aec3 JM |
15823 | origs1 = args1; |
15824 | origs2 = args2; | |
15825 | ||
56b1a554 JJ |
15826 | while (len-- |
15827 | /* Stop when an ellipsis is seen. */ | |
15828 | && args1 != NULL_TREE && args2 != NULL_TREE) | |
dda04398 NS |
15829 | { |
15830 | tree arg1 = TREE_VALUE (args1); | |
15831 | tree arg2 = TREE_VALUE (args2); | |
15832 | int deduce1, deduce2; | |
15833 | int quals1 = -1; | |
15834 | int quals2 = -1; | |
6467930b | 15835 | |
5d80a306 DG |
15836 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
15837 | && TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
15838 | { | |
15839 | /* When both arguments are pack expansions, we need only | |
15840 | unify the patterns themselves. */ | |
15841 | arg1 = PACK_EXPANSION_PATTERN (arg1); | |
15842 | arg2 = PACK_EXPANSION_PATTERN (arg2); | |
15843 | ||
15844 | /* This is the last comparison we need to do. */ | |
15845 | len = 0; | |
15846 | } | |
15847 | ||
dda04398 NS |
15848 | if (TREE_CODE (arg1) == REFERENCE_TYPE) |
15849 | { | |
15850 | arg1 = TREE_TYPE (arg1); | |
15851 | quals1 = cp_type_quals (arg1); | |
15852 | } | |
c8094d83 | 15853 | |
dda04398 NS |
15854 | if (TREE_CODE (arg2) == REFERENCE_TYPE) |
15855 | { | |
15856 | arg2 = TREE_TYPE (arg2); | |
15857 | quals2 = cp_type_quals (arg2); | |
15858 | } | |
6467930b | 15859 | |
dda04398 NS |
15860 | if ((quals1 < 0) != (quals2 < 0)) |
15861 | { | |
15862 | /* Only of the args is a reference, see if we should apply | |
15863 | array/function pointer decay to it. This is not part of | |
15864 | DR214, but is, IMHO, consistent with the deduction rules | |
15865 | for the function call itself, and with our earlier | |
15866 | implementation of the underspecified partial ordering | |
15867 | rules. (nathan). */ | |
15868 | if (quals1 >= 0) | |
15869 | { | |
15870 | switch (TREE_CODE (arg1)) | |
15871 | { | |
15872 | case ARRAY_TYPE: | |
15873 | arg1 = TREE_TYPE (arg1); | |
15874 | /* FALLTHROUGH. */ | |
15875 | case FUNCTION_TYPE: | |
15876 | arg1 = build_pointer_type (arg1); | |
15877 | break; | |
c8094d83 | 15878 | |
dda04398 NS |
15879 | default: |
15880 | break; | |
15881 | } | |
15882 | } | |
15883 | else | |
15884 | { | |
15885 | switch (TREE_CODE (arg2)) | |
15886 | { | |
15887 | case ARRAY_TYPE: | |
15888 | arg2 = TREE_TYPE (arg2); | |
15889 | /* FALLTHROUGH. */ | |
15890 | case FUNCTION_TYPE: | |
15891 | arg2 = build_pointer_type (arg2); | |
15892 | break; | |
c8094d83 | 15893 | |
dda04398 NS |
15894 | default: |
15895 | break; | |
15896 | } | |
15897 | } | |
15898 | } | |
c8094d83 | 15899 | |
dda04398 NS |
15900 | arg1 = TYPE_MAIN_VARIANT (arg1); |
15901 | arg2 = TYPE_MAIN_VARIANT (arg2); | |
c8094d83 | 15902 | |
5d80a306 DG |
15903 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION) |
15904 | { | |
b1d7b1c0 | 15905 | int i, len2 = list_length (args2); |
5d80a306 DG |
15906 | tree parmvec = make_tree_vec (1); |
15907 | tree argvec = make_tree_vec (len2); | |
15908 | tree ta = args2; | |
15909 | ||
15910 | /* Setup the parameter vector, which contains only ARG1. */ | |
15911 | TREE_VEC_ELT (parmvec, 0) = arg1; | |
15912 | ||
15913 | /* Setup the argument vector, which contains the remaining | |
15914 | arguments. */ | |
15915 | for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta)) | |
15916 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
15917 | ||
15918 | deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec, | |
15919 | argvec, UNIFY_ALLOW_NONE, | |
15920 | /*call_args_p=*/false, | |
15921 | /*subr=*/0); | |
15922 | ||
15923 | /* We cannot deduce in the other direction, because ARG1 is | |
15924 | a pack expansion but ARG2 is not. */ | |
15925 | deduce2 = 0; | |
15926 | } | |
15927 | else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
15928 | { | |
b1d7b1c0 | 15929 | int i, len1 = list_length (args1); |
5d80a306 DG |
15930 | tree parmvec = make_tree_vec (1); |
15931 | tree argvec = make_tree_vec (len1); | |
15932 | tree ta = args1; | |
15933 | ||
15934 | /* Setup the parameter vector, which contains only ARG1. */ | |
15935 | TREE_VEC_ELT (parmvec, 0) = arg2; | |
15936 | ||
15937 | /* Setup the argument vector, which contains the remaining | |
15938 | arguments. */ | |
15939 | for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta)) | |
15940 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
15941 | ||
15942 | deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec, | |
15943 | argvec, UNIFY_ALLOW_NONE, | |
15944 | /*call_args_p=*/false, | |
15945 | /*subr=*/0); | |
15946 | ||
15947 | /* We cannot deduce in the other direction, because ARG2 is | |
15948 | a pack expansion but ARG1 is not.*/ | |
15949 | deduce1 = 0; | |
15950 | } | |
15951 | ||
15952 | else | |
15953 | { | |
15954 | /* The normal case, where neither argument is a pack | |
15955 | expansion. */ | |
15956 | deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE); | |
15957 | deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE); | |
15958 | } | |
dda04398 | 15959 | |
9d79aec3 JM |
15960 | /* If we couldn't deduce arguments for tparms1 to make arg1 match |
15961 | arg2, then arg2 is not as specialized as arg1. */ | |
dda04398 | 15962 | if (!deduce1) |
9d79aec3 | 15963 | lose2 = true; |
dda04398 | 15964 | if (!deduce2) |
9d79aec3 JM |
15965 | lose1 = true; |
15966 | ||
15967 | /* "If, for a given type, deduction succeeds in both directions | |
15968 | (i.e., the types are identical after the transformations above) | |
15969 | and if the type from the argument template is more cv-qualified | |
15970 | than the type from the parameter template (as described above) | |
15971 | that type is considered to be more specialized than the other. If | |
15972 | neither type is more cv-qualified than the other then neither type | |
ce0ecb98 | 15973 | is more specialized than the other." */ |
9d79aec3 | 15974 | |
9d79aec3 | 15975 | if (deduce1 && deduce2 |
ce0ecb98 | 15976 | && quals1 != quals2 && quals1 >= 0 && quals2 >= 0) |
dda04398 | 15977 | { |
dda04398 | 15978 | if ((quals1 & quals2) == quals2) |
9d79aec3 | 15979 | lose2 = true; |
dda04398 | 15980 | if ((quals1 & quals2) == quals1) |
9d79aec3 | 15981 | lose1 = true; |
dda04398 | 15982 | } |
9d79aec3 JM |
15983 | |
15984 | if (lose1 && lose2) | |
15985 | /* We've failed to deduce something in either direction. | |
15986 | These must be unordered. */ | |
15987 | break; | |
c8094d83 | 15988 | |
5d80a306 DG |
15989 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
15990 | || TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
15991 | /* We have already processed all of the arguments in our | |
15992 | handing of the pack expansion type. */ | |
15993 | len = 0; | |
15994 | ||
dda04398 NS |
15995 | args1 = TREE_CHAIN (args1); |
15996 | args2 = TREE_CHAIN (args2); | |
15997 | } | |
15998 | ||
9d79aec3 JM |
15999 | /* "In most cases, all template parameters must have values in order for |
16000 | deduction to succeed, but for partial ordering purposes a template | |
16001 | parameter may remain without a value provided it is not used in the | |
16002 | types being used for partial ordering." | |
16003 | ||
16004 | Thus, if we are missing any of the targs1 we need to substitute into | |
16005 | origs1, then pat2 is not as specialized as pat1. This can happen when | |
16006 | there is a nondeduced context. */ | |
16007 | if (!lose2 && check_undeduced_parms (targs1, origs1, args1)) | |
16008 | lose2 = true; | |
16009 | if (!lose1 && check_undeduced_parms (targs2, origs2, args2)) | |
16010 | lose1 = true; | |
16011 | ||
dda04398 NS |
16012 | processing_template_decl--; |
16013 | ||
5d80a306 DG |
16014 | /* All things being equal, if the next argument is a pack expansion |
16015 | for one function but not for the other, prefer the | |
9d79aec3 JM |
16016 | non-variadic function. FIXME this is bogus; see c++/41958. */ |
16017 | if (lose1 == lose2 | |
5d80a306 DG |
16018 | && args1 && TREE_VALUE (args1) |
16019 | && args2 && TREE_VALUE (args2)) | |
16020 | { | |
9d79aec3 JM |
16021 | lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION; |
16022 | lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION; | |
5d80a306 DG |
16023 | } |
16024 | ||
9d79aec3 JM |
16025 | if (lose1 == lose2) |
16026 | return 0; | |
16027 | else if (!lose1) | |
16028 | return 1; | |
16029 | else | |
16030 | return -1; | |
73aad9b9 | 16031 | } |
6467930b | 16032 | |
916b63c3 | 16033 | /* Determine which of two partial specializations is more specialized. |
6467930b | 16034 | |
916b63c3 MM |
16035 | PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding |
16036 | to the first partial specialization. The TREE_VALUE is the | |
16037 | innermost set of template parameters for the partial | |
16038 | specialization. PAT2 is similar, but for the second template. | |
16039 | ||
16040 | Return 1 if the first partial specialization is more specialized; | |
16041 | -1 if the second is more specialized; 0 if neither is more | |
16042 | specialized. | |
55ece1b3 | 16043 | |
916b63c3 | 16044 | See [temp.class.order] for information about determining which of |
3db45ab5 | 16045 | two templates is more specialized. */ |
c8094d83 | 16046 | |
b5791fdc | 16047 | static int |
916b63c3 | 16048 | more_specialized_class (tree pat1, tree pat2) |
73aad9b9 JM |
16049 | { |
16050 | tree targs; | |
916b63c3 | 16051 | tree tmpl1, tmpl2; |
73aad9b9 | 16052 | int winner = 0; |
5d80a306 | 16053 | bool any_deductions = false; |
73aad9b9 | 16054 | |
916b63c3 MM |
16055 | tmpl1 = TREE_TYPE (pat1); |
16056 | tmpl2 = TREE_TYPE (pat2); | |
16057 | ||
c8094d83 | 16058 | /* Just like what happens for functions, if we are ordering between |
baa49730 GB |
16059 | different class template specializations, we may encounter dependent |
16060 | types in the arguments, and we need our dependency check functions | |
16061 | to behave correctly. */ | |
16062 | ++processing_template_decl; | |
3db45ab5 | 16063 | targs = get_class_bindings (TREE_VALUE (pat1), |
916b63c3 MM |
16064 | CLASSTYPE_TI_ARGS (tmpl1), |
16065 | CLASSTYPE_TI_ARGS (tmpl2)); | |
73aad9b9 | 16066 | if (targs) |
5d80a306 DG |
16067 | { |
16068 | --winner; | |
16069 | any_deductions = true; | |
16070 | } | |
73aad9b9 | 16071 | |
3db45ab5 | 16072 | targs = get_class_bindings (TREE_VALUE (pat2), |
916b63c3 MM |
16073 | CLASSTYPE_TI_ARGS (tmpl2), |
16074 | CLASSTYPE_TI_ARGS (tmpl1)); | |
73aad9b9 | 16075 | if (targs) |
5d80a306 DG |
16076 | { |
16077 | ++winner; | |
16078 | any_deductions = true; | |
16079 | } | |
baa49730 | 16080 | --processing_template_decl; |
6467930b | 16081 | |
5d80a306 DG |
16082 | /* In the case of a tie where at least one of the class templates |
16083 | has a parameter pack at the end, the template with the most | |
16084 | non-packed parameters wins. */ | |
16085 | if (winner == 0 | |
16086 | && any_deductions | |
16087 | && (template_args_variadic_p (TREE_PURPOSE (pat1)) | |
16088 | || template_args_variadic_p (TREE_PURPOSE (pat2)))) | |
16089 | { | |
16090 | tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1)); | |
16091 | tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2)); | |
16092 | int len1 = TREE_VEC_LENGTH (args1); | |
16093 | int len2 = TREE_VEC_LENGTH (args2); | |
16094 | ||
16095 | /* We don't count the pack expansion at the end. */ | |
16096 | if (template_args_variadic_p (TREE_PURPOSE (pat1))) | |
16097 | --len1; | |
16098 | if (template_args_variadic_p (TREE_PURPOSE (pat2))) | |
16099 | --len2; | |
16100 | ||
16101 | if (len1 > len2) | |
16102 | return 1; | |
16103 | else if (len1 < len2) | |
16104 | return -1; | |
16105 | } | |
16106 | ||
6467930b MS |
16107 | return winner; |
16108 | } | |
73aad9b9 JM |
16109 | |
16110 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 16111 | DECL from the function template FN, with the explicit template |
a34d3336 | 16112 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must |
4393e105 | 16113 | also match. Return NULL_TREE if no satisfactory arguments could be |
a34d3336 | 16114 | found. */ |
c8094d83 | 16115 | |
76b9a14d | 16116 | static tree |
a34d3336 | 16117 | get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype) |
73aad9b9 | 16118 | { |
98c1c668 | 16119 | int ntparms = DECL_NTPARMS (fn); |
f31c0a32 | 16120 | tree targs = make_tree_vec (ntparms); |
4393e105 | 16121 | tree decl_type; |
03017874 | 16122 | tree decl_arg_types; |
c166b898 ILT |
16123 | tree *args; |
16124 | unsigned int nargs, ix; | |
16125 | tree arg; | |
98c1c668 | 16126 | |
4393e105 MM |
16127 | /* Substitute the explicit template arguments into the type of DECL. |
16128 | The call to fn_type_unification will handle substitution into the | |
16129 | FN. */ | |
16130 | decl_type = TREE_TYPE (decl); | |
16131 | if (explicit_args && uses_template_parms (decl_type)) | |
16132 | { | |
16133 | tree tmpl; | |
16134 | tree converted_args; | |
16135 | ||
16136 | if (DECL_TEMPLATE_INFO (decl)) | |
16137 | tmpl = DECL_TI_TEMPLATE (decl); | |
16138 | else | |
0e339752 | 16139 | /* We can get here for some invalid specializations. */ |
4393e105 MM |
16140 | return NULL_TREE; |
16141 | ||
16142 | converted_args | |
e7e93965 MM |
16143 | = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), |
16144 | explicit_args, NULL_TREE, | |
3db45ab5 | 16145 | tf_none, |
e7e93965 MM |
16146 | /*require_all_args=*/false, |
16147 | /*use_default_args=*/false); | |
4393e105 MM |
16148 | if (converted_args == error_mark_node) |
16149 | return NULL_TREE; | |
c8094d83 MS |
16150 | |
16151 | decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); | |
4393e105 MM |
16152 | if (decl_type == error_mark_node) |
16153 | return NULL_TREE; | |
16154 | } | |
16155 | ||
e5214479 | 16156 | /* Never do unification on the 'this' parameter. */ |
ba139ba8 MM |
16157 | decl_arg_types = skip_artificial_parms_for (decl, |
16158 | TYPE_ARG_TYPES (decl_type)); | |
d7684f2d | 16159 | |
c166b898 ILT |
16160 | nargs = list_length (decl_arg_types); |
16161 | args = XALLOCAVEC (tree, nargs); | |
16162 | for (arg = decl_arg_types, ix = 0; | |
16163 | arg != NULL_TREE && arg != void_list_node; | |
16164 | arg = TREE_CHAIN (arg), ++ix) | |
16165 | args[ix] = TREE_VALUE (arg); | |
16166 | ||
c8094d83 | 16167 | if (fn_type_unification (fn, explicit_args, targs, |
c166b898 | 16168 | args, ix, |
8d3631f8 | 16169 | (check_rettype || DECL_CONV_FN_P (fn) |
0cbd7506 | 16170 | ? TREE_TYPE (decl_type) : NULL_TREE), |
30f86ec3 | 16171 | DEDUCE_EXACT, LOOKUP_NORMAL)) |
76b9a14d JM |
16172 | return NULL_TREE; |
16173 | ||
76b9a14d JM |
16174 | return targs; |
16175 | } | |
16176 | ||
36a117a5 MM |
16177 | /* Return the innermost template arguments that, when applied to a |
16178 | template specialization whose innermost template parameters are | |
d0940d56 | 16179 | TPARMS, and whose specialization arguments are SPEC_ARGS, yield the |
c8094d83 | 16180 | ARGS. |
36a117a5 MM |
16181 | |
16182 | For example, suppose we have: | |
16183 | ||
16184 | template <class T, class U> struct S {}; | |
16185 | template <class T> struct S<T*, int> {}; | |
16186 | ||
16187 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
916b63c3 | 16188 | {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*, |
36a117a5 MM |
16189 | int}. The resulting vector will be {double}, indicating that `T' |
16190 | is bound to `double'. */ | |
16191 | ||
bd6dd845 | 16192 | static tree |
916b63c3 | 16193 | get_class_bindings (tree tparms, tree spec_args, tree args) |
73aad9b9 | 16194 | { |
3b3ba9f0 | 16195 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
916b63c3 MM |
16196 | tree deduced_args; |
16197 | tree innermost_deduced_args; | |
73aad9b9 | 16198 | |
916b63c3 MM |
16199 | innermost_deduced_args = make_tree_vec (ntparms); |
16200 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
16201 | { | |
16202 | deduced_args = copy_node (args); | |
16203 | SET_TMPL_ARGS_LEVEL (deduced_args, | |
16204 | TMPL_ARGS_DEPTH (deduced_args), | |
16205 | innermost_deduced_args); | |
16206 | } | |
16207 | else | |
3db45ab5 | 16208 | deduced_args = innermost_deduced_args; |
916b63c3 | 16209 | |
3db45ab5 MS |
16210 | if (unify (tparms, deduced_args, |
16211 | INNERMOST_TEMPLATE_ARGS (spec_args), | |
16212 | INNERMOST_TEMPLATE_ARGS (args), | |
0cbd7506 | 16213 | UNIFY_ALLOW_NONE)) |
fcfb9f96 | 16214 | return NULL_TREE; |
73aad9b9 JM |
16215 | |
16216 | for (i = 0; i < ntparms; ++i) | |
916b63c3 | 16217 | if (! TREE_VEC_ELT (innermost_deduced_args, i)) |
73aad9b9 JM |
16218 | return NULL_TREE; |
16219 | ||
916b63c3 MM |
16220 | /* Verify that nondeduced template arguments agree with the type |
16221 | obtained from argument deduction. | |
3db45ab5 | 16222 | |
916b63c3 MM |
16223 | For example: |
16224 | ||
16225 | struct A { typedef int X; }; | |
16226 | template <class T, class U> struct C {}; | |
16227 | template <class T> struct C<T, typename T::X> {}; | |
16228 | ||
16229 | Then with the instantiation `C<A, int>', we can deduce that | |
16230 | `T' is `A' but unify () does not check whether `typename T::X' | |
16231 | is `int'. */ | |
16232 | spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE); | |
16233 | if (spec_args == error_mark_node | |
16234 | /* We only need to check the innermost arguments; the other | |
16235 | arguments will always agree. */ | |
16236 | || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args), | |
16237 | INNERMOST_TEMPLATE_ARGS (args))) | |
74601d7c KL |
16238 | return NULL_TREE; |
16239 | ||
4cf36211 DG |
16240 | /* Now that we have bindings for all of the template arguments, |
16241 | ensure that the arguments deduced for the template template | |
16242 | parameters have compatible template parameter lists. See the use | |
16243 | of template_template_parm_bindings_ok_p in fn_type_unification | |
16244 | for more information. */ | |
16245 | if (!template_template_parm_bindings_ok_p (tparms, deduced_args)) | |
16246 | return NULL_TREE; | |
16247 | ||
916b63c3 | 16248 | return deduced_args; |
73aad9b9 JM |
16249 | } |
16250 | ||
7ca383e6 MM |
16251 | /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL. |
16252 | Return the TREE_LIST node with the most specialized template, if | |
16253 | any. If there is no most specialized template, the error_mark_node | |
16254 | is returned. | |
16255 | ||
16256 | Note that this function does not look at, or modify, the | |
16257 | TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node | |
16258 | returned is one of the elements of INSTANTIATIONS, callers may | |
16259 | store information in the TREE_PURPOSE or TREE_TYPE of the nodes, | |
16260 | and retrieve it from the value returned. */ | |
73aad9b9 JM |
16261 | |
16262 | tree | |
7ca383e6 | 16263 | most_specialized_instantiation (tree templates) |
73aad9b9 | 16264 | { |
104bf76a | 16265 | tree fn, champ; |
73aad9b9 | 16266 | |
dda04398 | 16267 | ++processing_template_decl; |
c8094d83 | 16268 | |
7ca383e6 MM |
16269 | champ = templates; |
16270 | for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 16271 | { |
dda04398 | 16272 | int fate = 0; |
c8094d83 | 16273 | |
a34d3336 NS |
16274 | if (get_bindings (TREE_VALUE (champ), |
16275 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
16276 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
16277 | fate--; |
16278 | ||
a34d3336 NS |
16279 | if (get_bindings (TREE_VALUE (fn), |
16280 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
16281 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 | 16282 | fate++; |
c8094d83 | 16283 | |
8ca4bf25 MM |
16284 | if (fate == -1) |
16285 | champ = fn; | |
16286 | else if (!fate) | |
73aad9b9 | 16287 | { |
8ca4bf25 MM |
16288 | /* Equally specialized, move to next function. If there |
16289 | is no next function, nothing's most specialized. */ | |
16290 | fn = TREE_CHAIN (fn); | |
7ca383e6 | 16291 | champ = fn; |
8ca4bf25 MM |
16292 | if (!fn) |
16293 | break; | |
73aad9b9 JM |
16294 | } |
16295 | } | |
c8094d83 | 16296 | |
dda04398 NS |
16297 | if (champ) |
16298 | /* Now verify that champ is better than everything earlier in the | |
16299 | instantiation list. */ | |
7ca383e6 | 16300 | for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) |
a34d3336 NS |
16301 | if (get_bindings (TREE_VALUE (champ), |
16302 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
16303 | NULL_TREE, /*check_ret=*/false) | |
16304 | || !get_bindings (TREE_VALUE (fn), | |
16305 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
16306 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
16307 | { |
16308 | champ = NULL_TREE; | |
16309 | break; | |
16310 | } | |
c8094d83 | 16311 | |
dda04398 | 16312 | processing_template_decl--; |
c8094d83 | 16313 | |
dda04398 NS |
16314 | if (!champ) |
16315 | return error_mark_node; | |
73aad9b9 | 16316 | |
7ca383e6 | 16317 | return champ; |
73aad9b9 JM |
16318 | } |
16319 | ||
36a117a5 | 16320 | /* If DECL is a specialization of some template, return the most |
f9a7ae04 MM |
16321 | general such template. Otherwise, returns NULL_TREE. |
16322 | ||
16323 | For example, given: | |
36a117a5 MM |
16324 | |
16325 | template <class T> struct S { template <class U> void f(U); }; | |
16326 | ||
16327 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
16328 | the full template. This function will not trace past partial | |
16329 | specializations, however. For example, given in addition: | |
16330 | ||
16331 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
16332 | ||
16333 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
16334 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 16335 | |
612c671a | 16336 | tree |
3a978d72 | 16337 | most_general_template (tree decl) |
73aad9b9 | 16338 | { |
f9a7ae04 MM |
16339 | /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is |
16340 | an immediate specialization. */ | |
16341 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
16342 | { | |
16343 | if (DECL_TEMPLATE_INFO (decl)) { | |
16344 | decl = DECL_TI_TEMPLATE (decl); | |
16345 | ||
16346 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a | |
16347 | template friend. */ | |
16348 | if (TREE_CODE (decl) != TEMPLATE_DECL) | |
16349 | return NULL_TREE; | |
16350 | } else | |
16351 | return NULL_TREE; | |
16352 | } | |
16353 | ||
16354 | /* Look for more and more general templates. */ | |
16355 | while (DECL_TEMPLATE_INFO (decl)) | |
16356 | { | |
10b1d5e7 MM |
16357 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases. |
16358 | (See cp-tree.h for details.) */ | |
f9a7ae04 MM |
16359 | if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
16360 | break; | |
16361 | ||
6e049fcd KL |
16362 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
16363 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) | |
16364 | break; | |
16365 | ||
f9a7ae04 MM |
16366 | /* Stop if we run into an explicitly specialized class template. */ |
16367 | if (!DECL_NAMESPACE_SCOPE_P (decl) | |
16368 | && DECL_CONTEXT (decl) | |
16369 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) | |
16370 | break; | |
16371 | ||
16372 | decl = DECL_TI_TEMPLATE (decl); | |
16373 | } | |
36a117a5 MM |
16374 | |
16375 | return decl; | |
16376 | } | |
16377 | ||
916b63c3 MM |
16378 | /* Return the most specialized of the class template partial |
16379 | specializations of TMPL which can produce TYPE, a specialization of | |
16380 | TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is | |
16381 | a _TYPE node corresponding to the partial specialization, while the | |
16382 | TREE_PURPOSE is the set of template arguments that must be | |
16383 | substituted into the TREE_TYPE in order to generate TYPE. | |
16384 | ||
16385 | If the choice of partial specialization is ambiguous, a diagnostic | |
16386 | is issued, and the error_mark_node is returned. If there are no | |
16387 | partial specializations of TMPL matching TYPE, then NULL_TREE is | |
16388 | returned. */ | |
36a117a5 | 16389 | |
e9659ab0 | 16390 | static tree |
ebc258f1 | 16391 | most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain) |
36a117a5 MM |
16392 | { |
16393 | tree list = NULL_TREE; | |
16394 | tree t; | |
16395 | tree champ; | |
73aad9b9 | 16396 | int fate; |
916b63c3 MM |
16397 | bool ambiguous_p; |
16398 | tree args; | |
dc28490d | 16399 | tree outer_args = NULL_TREE; |
73aad9b9 | 16400 | |
36a117a5 | 16401 | tmpl = most_general_template (tmpl); |
916b63c3 | 16402 | args = CLASSTYPE_TI_ARGS (type); |
dc28490d JM |
16403 | |
16404 | /* For determining which partial specialization to use, only the | |
16405 | innermost args are interesting. */ | |
16406 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
16407 | { | |
16408 | outer_args = strip_innermost_template_args (args, 1); | |
16409 | args = INNERMOST_TEMPLATE_ARGS (args); | |
16410 | } | |
16411 | ||
36a117a5 | 16412 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) |
73aad9b9 | 16413 | { |
916b63c3 MM |
16414 | tree partial_spec_args; |
16415 | tree spec_args; | |
dc28490d | 16416 | tree parms = TREE_VALUE (t); |
916b63c3 MM |
16417 | |
16418 | partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t)); | |
f857e9a4 DS |
16419 | |
16420 | ++processing_template_decl; | |
16421 | ||
dc28490d JM |
16422 | if (outer_args) |
16423 | { | |
16424 | int i; | |
16425 | ||
16426 | /* Discard the outer levels of args, and then substitute in the | |
16427 | template args from the enclosing class. */ | |
16428 | partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args); | |
16429 | partial_spec_args = tsubst_template_args | |
16430 | (partial_spec_args, outer_args, tf_none, NULL_TREE); | |
16431 | ||
16432 | /* PARMS already refers to just the innermost parms, but the | |
16433 | template parms in partial_spec_args had their levels lowered | |
16434 | by tsubst, so we need to do the same for the parm list. We | |
16435 | can't just tsubst the TREE_VEC itself, as tsubst wants to | |
16436 | treat a TREE_VEC as an argument vector. */ | |
16437 | parms = copy_node (parms); | |
16438 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) | |
16439 | TREE_VEC_ELT (parms, i) = | |
16440 | tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE); | |
fd452cef | 16441 | |
dc28490d | 16442 | } |
5f431950 DS |
16443 | |
16444 | partial_spec_args = | |
16445 | coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
16446 | add_to_template_args (outer_args, | |
16447 | partial_spec_args), | |
16448 | tmpl, tf_none, | |
16449 | /*require_all_args=*/true, | |
16450 | /*use_default_args=*/true); | |
16451 | ||
f857e9a4 DS |
16452 | --processing_template_decl; |
16453 | ||
5f431950 DS |
16454 | if (partial_spec_args == error_mark_node) |
16455 | return error_mark_node; | |
16456 | ||
dc28490d | 16457 | spec_args = get_class_bindings (parms, |
3db45ab5 | 16458 | partial_spec_args, |
916b63c3 | 16459 | args); |
36a117a5 | 16460 | if (spec_args) |
73aad9b9 | 16461 | { |
dc28490d JM |
16462 | if (outer_args) |
16463 | spec_args = add_to_template_args (outer_args, spec_args); | |
916b63c3 | 16464 | list = tree_cons (spec_args, TREE_VALUE (t), list); |
73aad9b9 JM |
16465 | TREE_TYPE (list) = TREE_TYPE (t); |
16466 | } | |
16467 | } | |
16468 | ||
16469 | if (! list) | |
16470 | return NULL_TREE; | |
16471 | ||
916b63c3 | 16472 | ambiguous_p = false; |
73aad9b9 JM |
16473 | t = list; |
16474 | champ = t; | |
16475 | t = TREE_CHAIN (t); | |
16476 | for (; t; t = TREE_CHAIN (t)) | |
16477 | { | |
916b63c3 | 16478 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
16479 | if (fate == 1) |
16480 | ; | |
16481 | else | |
16482 | { | |
16483 | if (fate == 0) | |
16484 | { | |
16485 | t = TREE_CHAIN (t); | |
16486 | if (! t) | |
916b63c3 MM |
16487 | { |
16488 | ambiguous_p = true; | |
16489 | break; | |
16490 | } | |
73aad9b9 JM |
16491 | } |
16492 | champ = t; | |
16493 | } | |
16494 | } | |
16495 | ||
916b63c3 MM |
16496 | if (!ambiguous_p) |
16497 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
16498 | { | |
16499 | fate = more_specialized_class (champ, t); | |
16500 | if (fate != 1) | |
16501 | { | |
16502 | ambiguous_p = true; | |
16503 | break; | |
16504 | } | |
16505 | } | |
16506 | ||
16507 | if (ambiguous_p) | |
73aad9b9 | 16508 | { |
6ba6f70d PB |
16509 | const char *str; |
16510 | char *spaces = NULL; | |
ebc258f1 JM |
16511 | if (!(complain & tf_error)) |
16512 | return error_mark_node; | |
916b63c3 | 16513 | error ("ambiguous class template instantiation for %q#T", type); |
6ba6f70d | 16514 | str = TREE_CHAIN (list) ? _("candidates are:") : _("candidate is:"); |
916b63c3 | 16515 | for (t = list; t; t = TREE_CHAIN (t)) |
6ba6f70d PB |
16516 | { |
16517 | error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t)); | |
16518 | spaces = spaces ? spaces : get_spaces (str); | |
16519 | } | |
16520 | free (spaces); | |
916b63c3 | 16521 | return error_mark_node; |
73aad9b9 JM |
16522 | } |
16523 | ||
16524 | return champ; | |
16525 | } | |
16526 | ||
eb8845be | 16527 | /* Explicitly instantiate DECL. */ |
e92cc029 | 16528 | |
8d08fdba | 16529 | void |
eb8845be | 16530 | do_decl_instantiation (tree decl, tree storage) |
8d08fdba | 16531 | { |
8d08fdba | 16532 | tree result = NULL_TREE; |
faae18ab | 16533 | int extern_p = 0; |
e8abc66f | 16534 | |
ad47b891 | 16535 | if (!decl || decl == error_mark_node) |
dc957d14 | 16536 | /* An error occurred, for which grokdeclarator has already issued |
3fa56191 MM |
16537 | an appropriate message. */ |
16538 | return; | |
16539 | else if (! DECL_LANG_SPECIFIC (decl)) | |
ec255269 | 16540 | { |
0f51ccfc | 16541 | error ("explicit instantiation of non-template %q#D", decl); |
ec255269 MS |
16542 | return; |
16543 | } | |
03d0f4af | 16544 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 16545 | { |
03d0f4af MM |
16546 | /* There is an asymmetry here in the way VAR_DECLs and |
16547 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
16548 | the latter, the DECL we get back will be marked as a | |
16549 | template instantiation, and the appropriate | |
16550 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
16551 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
16552 | should handle VAR_DECLs as it currently handles | |
16553 | FUNCTION_DECLs. */ | |
8259e4f5 PC |
16554 | if (!DECL_CLASS_SCOPE_P (decl)) |
16555 | { | |
16556 | error ("%qD is not a static data member of a class template", decl); | |
16557 | return; | |
16558 | } | |
86ac0575 | 16559 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); |
e0860732 | 16560 | if (!result || TREE_CODE (result) != VAR_DECL) |
03d0f4af | 16561 | { |
0f51ccfc | 16562 | error ("no matching template for %qD found", decl); |
03d0f4af MM |
16563 | return; |
16564 | } | |
8ea6dfae SM |
16565 | if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl))) |
16566 | { | |
16567 | error ("type %qT for explicit instantiation %qD does not match " | |
16568 | "declared type %qT", TREE_TYPE (result), decl, | |
16569 | TREE_TYPE (decl)); | |
16570 | return; | |
16571 | } | |
6633d636 MS |
16572 | } |
16573 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
16574 | { | |
0f51ccfc | 16575 | error ("explicit instantiation of %q#D", decl); |
6633d636 MS |
16576 | return; |
16577 | } | |
03d0f4af MM |
16578 | else |
16579 | result = decl; | |
672476cb | 16580 | |
03d0f4af | 16581 | /* Check for various error cases. Note that if the explicit |
0e339752 | 16582 | instantiation is valid the RESULT will currently be marked as an |
03d0f4af MM |
16583 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set |
16584 | until we get here. */ | |
16585 | ||
16586 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 16587 | { |
07782718 KL |
16588 | /* DR 259 [temp.spec]. |
16589 | ||
16590 | Both an explicit instantiation and a declaration of an explicit | |
16591 | specialization shall not appear in a program unless the explicit | |
16592 | instantiation follows a declaration of the explicit specialization. | |
03d0f4af | 16593 | |
07782718 KL |
16594 | For a given set of template parameters, if an explicit |
16595 | instantiation of a template appears after a declaration of an | |
16596 | explicit specialization for that template, the explicit | |
16597 | instantiation has no effect. */ | |
672476cb MM |
16598 | return; |
16599 | } | |
03d0f4af MM |
16600 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
16601 | { | |
16602 | /* [temp.spec] | |
98c1c668 | 16603 | |
03d0f4af | 16604 | No program shall explicitly instantiate any template more |
c8094d83 | 16605 | than once. |
03d0f4af | 16606 | |
4684cd27 MM |
16607 | We check DECL_NOT_REALLY_EXTERN so as not to complain when |
16608 | the first instantiation was `extern' and the second is not, | |
16609 | and EXTERN_P for the opposite case. */ | |
16610 | if (DECL_NOT_REALLY_EXTERN (result) && !extern_p) | |
cbe5f3b3 | 16611 | permerror (input_location, "duplicate explicit instantiation of %q#D", result); |
4684cd27 MM |
16612 | /* If an "extern" explicit instantiation follows an ordinary |
16613 | explicit instantiation, the template is instantiated. */ | |
16614 | if (extern_p) | |
03d0f4af MM |
16615 | return; |
16616 | } | |
16617 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 16618 | { |
0f51ccfc | 16619 | error ("no matching template for %qD found", result); |
faae18ab MS |
16620 | return; |
16621 | } | |
03d0f4af | 16622 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 16623 | { |
cbe5f3b3 | 16624 | permerror (input_location, "explicit instantiation of non-template %q#D", result); |
6633d636 MS |
16625 | return; |
16626 | } | |
16627 | ||
f0e01782 | 16628 | if (storage == NULL_TREE) |
00595019 | 16629 | ; |
faae18ab | 16630 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af | 16631 | { |
fcf73884 | 16632 | if (!in_system_header && (cxx_dialect == cxx98)) |
509c9d60 | 16633 | pedwarn (input_location, OPT_pedantic, |
fcf73884 | 16634 | "ISO C++ 1998 forbids the use of %<extern%> on explicit " |
0cbd7506 | 16635 | "instantiations"); |
03d0f4af MM |
16636 | extern_p = 1; |
16637 | } | |
f0e01782 | 16638 | else |
0f51ccfc | 16639 | error ("storage class %qD applied to template instantiation", storage); |
3db45ab5 | 16640 | |
9c12301f | 16641 | check_explicit_instantiation_namespace (result); |
5566b478 | 16642 | mark_decl_instantiated (result, extern_p); |
c91a56d2 | 16643 | if (! extern_p) |
3db45ab5 | 16644 | instantiate_decl (result, /*defer_ok=*/1, |
eba839f9 | 16645 | /*expl_inst_class_mem_p=*/false); |
7177d104 MS |
16646 | } |
16647 | ||
b5791fdc | 16648 | static void |
3a978d72 | 16649 | mark_class_instantiated (tree t, int extern_p) |
faae18ab MS |
16650 | { |
16651 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
16652 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
16653 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
16654 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; |
16655 | if (! extern_p) | |
16656 | { | |
16657 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
16658 | rest_of_type_compilation (t, 1); | |
16659 | } | |
c8094d83 | 16660 | } |
e8abc66f | 16661 | |
5e0c54e5 | 16662 | /* Called from do_type_instantiation through binding_table_foreach to |
9bcb9aae | 16663 | do recursive instantiation for the type bound in ENTRY. */ |
5e0c54e5 GDR |
16664 | static void |
16665 | bt_instantiate_type_proc (binding_entry entry, void *data) | |
16666 | { | |
16667 | tree storage = *(tree *) data; | |
16668 | ||
9e1e64ec | 16669 | if (MAYBE_CLASS_TYPE_P (entry->type) |
5e0c54e5 GDR |
16670 | && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type))) |
16671 | do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0); | |
16672 | } | |
16673 | ||
415c974c MA |
16674 | /* Called from do_type_instantiation to instantiate a member |
16675 | (a member function or a static member variable) of an | |
03fd3f84 | 16676 | explicitly instantiated class template. */ |
415c974c MA |
16677 | static void |
16678 | instantiate_class_member (tree decl, int extern_p) | |
16679 | { | |
16680 | mark_decl_instantiated (decl, extern_p); | |
415c974c | 16681 | if (! extern_p) |
3db45ab5 | 16682 | instantiate_decl (decl, /*defer_ok=*/1, |
eba839f9 | 16683 | /*expl_inst_class_mem_p=*/true); |
415c974c MA |
16684 | } |
16685 | ||
a1bcc528 JM |
16686 | /* Perform an explicit instantiation of template class T. STORAGE, if |
16687 | non-null, is the RID for extern, inline or static. COMPLAIN is | |
838dfd8a | 16688 | nonzero if this is called from the parser, zero if called recursively, |
a1bcc528 | 16689 | since the standard is unclear (as detailed below). */ |
c8094d83 | 16690 | |
7177d104 | 16691 | void |
3a978d72 | 16692 | do_type_instantiation (tree t, tree storage, tsubst_flags_t complain) |
7177d104 | 16693 | { |
e8abc66f MS |
16694 | int extern_p = 0; |
16695 | int nomem_p = 0; | |
5566b478 | 16696 | int static_p = 0; |
4746cf84 | 16697 | int previous_instantiation_extern_p = 0; |
5566b478 | 16698 | |
ca79f85d JM |
16699 | if (TREE_CODE (t) == TYPE_DECL) |
16700 | t = TREE_TYPE (t); | |
16701 | ||
7ddedda4 | 16702 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 16703 | { |
0f51ccfc | 16704 | error ("explicit instantiation of non-template type %qT", t); |
ca79f85d JM |
16705 | return; |
16706 | } | |
16707 | ||
5566b478 | 16708 | complete_type (t); |
7177d104 | 16709 | |
d0f062fb | 16710 | if (!COMPLETE_TYPE_P (t)) |
f0e01782 | 16711 | { |
c2ea3a40 | 16712 | if (complain & tf_error) |
0f51ccfc | 16713 | error ("explicit instantiation of %q#T before definition of template", |
0cbd7506 | 16714 | t); |
f0e01782 MS |
16715 | return; |
16716 | } | |
16717 | ||
03d0f4af | 16718 | if (storage != NULL_TREE) |
f0e01782 | 16719 | { |
fcf73884 | 16720 | if (!in_system_header) |
1ffe7b6e PC |
16721 | { |
16722 | if (storage == ridpointers[(int) RID_EXTERN]) | |
16723 | { | |
16724 | if (cxx_dialect == cxx98) | |
509c9d60 MLI |
16725 | pedwarn (input_location, OPT_pedantic, |
16726 | "ISO C++ 1998 forbids the use of %<extern%> on " | |
16727 | "explicit instantiations"); | |
1ffe7b6e PC |
16728 | } |
16729 | else | |
509c9d60 MLI |
16730 | pedwarn (input_location, OPT_pedantic, |
16731 | "ISO C++ forbids the use of %qE" | |
16732 | " on explicit instantiations", storage); | |
1ffe7b6e | 16733 | } |
03d0f4af MM |
16734 | |
16735 | if (storage == ridpointers[(int) RID_INLINE]) | |
16736 | nomem_p = 1; | |
16737 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
16738 | extern_p = 1; | |
16739 | else if (storage == ridpointers[(int) RID_STATIC]) | |
16740 | static_p = 1; | |
16741 | else | |
16742 | { | |
0f51ccfc | 16743 | error ("storage class %qD applied to template instantiation", |
0cbd7506 | 16744 | storage); |
03d0f4af MM |
16745 | extern_p = 0; |
16746 | } | |
f0e01782 MS |
16747 | } |
16748 | ||
370af2d5 | 16749 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
03d0f4af | 16750 | { |
07782718 | 16751 | /* DR 259 [temp.spec]. |
a292b002 | 16752 | |
07782718 KL |
16753 | Both an explicit instantiation and a declaration of an explicit |
16754 | specialization shall not appear in a program unless the explicit | |
16755 | instantiation follows a declaration of the explicit specialization. | |
16756 | ||
16757 | For a given set of template parameters, if an explicit | |
16758 | instantiation of a template appears after a declaration of an | |
16759 | explicit specialization for that template, the explicit | |
16760 | instantiation has no effect. */ | |
03d0f4af MM |
16761 | return; |
16762 | } | |
16763 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 16764 | { |
03d0f4af MM |
16765 | /* [temp.spec] |
16766 | ||
16767 | No program shall explicitly instantiate any template more | |
c8094d83 | 16768 | than once. |
03d0f4af | 16769 | |
0cbd7506 | 16770 | If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit |
4746cf84 | 16771 | instantiation was `extern'. If EXTERN_P then the second is. |
4684cd27 | 16772 | These cases are OK. */ |
4746cf84 MA |
16773 | previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t); |
16774 | ||
16775 | if (!previous_instantiation_extern_p && !extern_p | |
c2ea3a40 | 16776 | && (complain & tf_error)) |
cbe5f3b3 | 16777 | permerror (input_location, "duplicate explicit instantiation of %q#T", t); |
c8094d83 | 16778 | |
03d0f4af MM |
16779 | /* If we've already instantiated the template, just return now. */ |
16780 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
16781 | return; | |
44a8d0b3 | 16782 | } |
e8abc66f | 16783 | |
9c12301f | 16784 | check_explicit_instantiation_namespace (TYPE_NAME (t)); |
03d0f4af | 16785 | mark_class_instantiated (t, extern_p); |
03d0f4af | 16786 | |
e8abc66f MS |
16787 | if (nomem_p) |
16788 | return; | |
16789 | ||
7177d104 | 16790 | { |
db5ae43f | 16791 | tree tmp; |
5566b478 | 16792 | |
03d0f4af MM |
16793 | /* In contrast to implicit instantiation, where only the |
16794 | declarations, and not the definitions, of members are | |
16795 | instantiated, we have here: | |
16796 | ||
0cbd7506 | 16797 | [temp.explicit] |
03d0f4af MM |
16798 | |
16799 | The explicit instantiation of a class template specialization | |
16800 | implies the instantiation of all of its members not | |
16801 | previously explicitly specialized in the translation unit | |
c8094d83 | 16802 | containing the explicit instantiation. |
03d0f4af MM |
16803 | |
16804 | Of course, we can't instantiate member template classes, since | |
16805 | we don't have any arguments for them. Note that the standard | |
dc957d14 | 16806 | is unclear on whether the instantiation of the members are |
415c974c | 16807 | *explicit* instantiations or not. However, the most natural |
03fd3f84 | 16808 | interpretation is that it should be an explicit instantiation. */ |
03d0f4af | 16809 | |
5566b478 | 16810 | if (! static_p) |
910ad8de | 16811 | for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp)) |
a7d87521 | 16812 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 16813 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
415c974c | 16814 | instantiate_class_member (tmp, extern_p); |
5566b478 | 16815 | |
910ad8de | 16816 | for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp)) |
5566b478 | 16817 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) |
415c974c | 16818 | instantiate_class_member (tmp, extern_p); |
7177d104 | 16819 | |
5e0c54e5 GDR |
16820 | if (CLASSTYPE_NESTED_UTDS (t)) |
16821 | binding_table_foreach (CLASSTYPE_NESTED_UTDS (t), | |
0cbd7506 | 16822 | bt_instantiate_type_proc, &storage); |
a292b002 | 16823 | } |
8d08fdba | 16824 | } |
a28e3c7f | 16825 | |
36a117a5 MM |
16826 | /* Given a function DECL, which is a specialization of TMPL, modify |
16827 | DECL to be a re-instantiation of TMPL with the same template | |
16828 | arguments. TMPL should be the template into which tsubst'ing | |
16829 | should occur for DECL, not the most general template. | |
830bfa74 MM |
16830 | |
16831 | One reason for doing this is a scenario like this: | |
16832 | ||
16833 | template <class T> | |
16834 | void f(const T&, int i); | |
16835 | ||
16836 | void g() { f(3, 7); } | |
16837 | ||
16838 | template <class T> | |
16839 | void f(const T& t, const int i) { } | |
16840 | ||
16841 | Note that when the template is first instantiated, with | |
16842 | instantiate_template, the resulting DECL will have no name for the | |
16843 | first parameter, and the wrong type for the second. So, when we go | |
16844 | to instantiate the DECL, we regenerate it. */ | |
16845 | ||
e9659ab0 | 16846 | static void |
3a978d72 | 16847 | regenerate_decl_from_template (tree decl, tree tmpl) |
830bfa74 | 16848 | { |
f9a7ae04 MM |
16849 | /* The arguments used to instantiate DECL, from the most general |
16850 | template. */ | |
830bfa74 | 16851 | tree args; |
830bfa74 | 16852 | tree code_pattern; |
830bfa74 MM |
16853 | |
16854 | args = DECL_TI_ARGS (decl); | |
16855 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
16856 | ||
2b907f5c KL |
16857 | /* Make sure that we can see identifiers, and compute access |
16858 | correctly. */ | |
16859 | push_access_scope (decl); | |
16860 | ||
c7222c02 MM |
16861 | if (TREE_CODE (decl) == FUNCTION_DECL) |
16862 | { | |
16863 | tree decl_parm; | |
16864 | tree pattern_parm; | |
16865 | tree specs; | |
16866 | int args_depth; | |
16867 | int parms_depth; | |
c8094d83 | 16868 | |
c7222c02 | 16869 | args_depth = TMPL_ARGS_DEPTH (args); |
c8094d83 | 16870 | parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
c7222c02 MM |
16871 | if (args_depth > parms_depth) |
16872 | args = get_innermost_template_args (args, parms_depth); | |
16873 | ||
16874 | specs = tsubst_exception_specification (TREE_TYPE (code_pattern), | |
16875 | args, tf_error, NULL_TREE); | |
16876 | if (specs) | |
16877 | TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), | |
16878 | specs); | |
16879 | ||
16880 | /* Merge parameter declarations. */ | |
c8094d83 | 16881 | decl_parm = skip_artificial_parms_for (decl, |
c7222c02 | 16882 | DECL_ARGUMENTS (decl)); |
c8094d83 | 16883 | pattern_parm |
c7222c02 MM |
16884 | = skip_artificial_parms_for (code_pattern, |
16885 | DECL_ARGUMENTS (code_pattern)); | |
5d80a306 | 16886 | while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm)) |
c7222c02 MM |
16887 | { |
16888 | tree parm_type; | |
b17bba6d | 16889 | tree attributes; |
5d80a306 | 16890 | |
c7222c02 MM |
16891 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) |
16892 | DECL_NAME (decl_parm) = DECL_NAME (pattern_parm); | |
16893 | parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error, | |
16894 | NULL_TREE); | |
02bab9db | 16895 | parm_type = type_decays_to (parm_type); |
c7222c02 MM |
16896 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) |
16897 | TREE_TYPE (decl_parm) = parm_type; | |
b17bba6d MM |
16898 | attributes = DECL_ATTRIBUTES (pattern_parm); |
16899 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
16900 | { | |
16901 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
16902 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
16903 | } | |
910ad8de NF |
16904 | decl_parm = DECL_CHAIN (decl_parm); |
16905 | pattern_parm = DECL_CHAIN (pattern_parm); | |
c7222c02 | 16906 | } |
5d80a306 DG |
16907 | /* Merge any parameters that match with the function parameter |
16908 | pack. */ | |
16909 | if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm)) | |
16910 | { | |
16911 | int i, len; | |
16912 | tree expanded_types; | |
16913 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
16914 | the parameters in this function parameter pack. */ | |
16915 | expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm), | |
16916 | args, tf_error, NULL_TREE); | |
16917 | len = TREE_VEC_LENGTH (expanded_types); | |
16918 | for (i = 0; i < len; i++) | |
16919 | { | |
16920 | tree parm_type; | |
16921 | tree attributes; | |
16922 | ||
16923 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) | |
16924 | /* Rename the parameter to include the index. */ | |
16925 | DECL_NAME (decl_parm) = | |
16926 | make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i); | |
16927 | parm_type = TREE_VEC_ELT (expanded_types, i); | |
16928 | parm_type = type_decays_to (parm_type); | |
16929 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) | |
16930 | TREE_TYPE (decl_parm) = parm_type; | |
16931 | attributes = DECL_ATTRIBUTES (pattern_parm); | |
16932 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
16933 | { | |
16934 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
16935 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
16936 | } | |
910ad8de | 16937 | decl_parm = DECL_CHAIN (decl_parm); |
5d80a306 DG |
16938 | } |
16939 | } | |
c7222c02 MM |
16940 | /* Merge additional specifiers from the CODE_PATTERN. */ |
16941 | if (DECL_DECLARED_INLINE_P (code_pattern) | |
16942 | && !DECL_DECLARED_INLINE_P (decl)) | |
16943 | DECL_DECLARED_INLINE_P (decl) = 1; | |
c7222c02 MM |
16944 | } |
16945 | else if (TREE_CODE (decl) == VAR_DECL) | |
48326487 JM |
16946 | { |
16947 | DECL_INITIAL (decl) = | |
16948 | tsubst_expr (DECL_INITIAL (code_pattern), args, | |
16949 | tf_error, DECL_TI_TEMPLATE (decl), | |
16950 | /*integral_constant_expression_p=*/false); | |
16951 | if (VAR_HAD_UNKNOWN_BOUND (decl)) | |
16952 | TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args, | |
16953 | tf_error, DECL_TI_TEMPLATE (decl)); | |
16954 | } | |
c7222c02 MM |
16955 | else |
16956 | gcc_unreachable (); | |
36a117a5 | 16957 | |
2b59fc25 | 16958 | pop_access_scope (decl); |
830bfa74 MM |
16959 | } |
16960 | ||
a723baf1 MM |
16961 | /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be |
16962 | substituted to get DECL. */ | |
16963 | ||
d58b7c2d | 16964 | tree |
a723baf1 MM |
16965 | template_for_substitution (tree decl) |
16966 | { | |
16967 | tree tmpl = DECL_TI_TEMPLATE (decl); | |
16968 | ||
16969 | /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern | |
16970 | for the instantiation. This is not always the most general | |
16971 | template. Consider, for example: | |
16972 | ||
0cbd7506 | 16973 | template <class T> |
a723baf1 | 16974 | struct S { template <class U> void f(); |
0cbd7506 | 16975 | template <> void f<int>(); }; |
a723baf1 MM |
16976 | |
16977 | and an instantiation of S<double>::f<int>. We want TD to be the | |
16978 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
16979 | while (/* An instantiation cannot have a definition, so we need a | |
16980 | more general template. */ | |
16981 | DECL_TEMPLATE_INSTANTIATION (tmpl) | |
16982 | /* We must also deal with friend templates. Given: | |
16983 | ||
c8094d83 | 16984 | template <class T> struct S { |
a723baf1 MM |
16985 | template <class U> friend void f() {}; |
16986 | }; | |
16987 | ||
16988 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
16989 | so far as the language is concerned, but that's still | |
16990 | where we get the pattern for the instantiation from. On | |
16991 | other hand, if the definition comes outside the class, say: | |
16992 | ||
c8094d83 | 16993 | template <class T> struct S { |
a723baf1 MM |
16994 | template <class U> friend void f(); |
16995 | }; | |
16996 | template <class U> friend void f() {} | |
16997 | ||
16998 | we don't need to look any further. That's what the check for | |
16999 | DECL_INITIAL is for. */ | |
17000 | || (TREE_CODE (decl) == FUNCTION_DECL | |
17001 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) | |
17002 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) | |
17003 | { | |
17004 | /* The present template, TD, should not be a definition. If it | |
17005 | were a definition, we should be using it! Note that we | |
17006 | cannot restructure the loop to just keep going until we find | |
17007 | a template with a definition, since that might go too far if | |
17008 | a specialization was declared, but not defined. */ | |
50bc768d NS |
17009 | gcc_assert (TREE_CODE (decl) != VAR_DECL |
17010 | || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))); | |
c8094d83 | 17011 | |
a723baf1 MM |
17012 | /* Fetch the more general template. */ |
17013 | tmpl = DECL_TI_TEMPLATE (tmpl); | |
17014 | } | |
17015 | ||
17016 | return tmpl; | |
17017 | } | |
17018 | ||
15305a73 JM |
17019 | /* Returns true if we need to instantiate this template instance even if we |
17020 | know we aren't going to emit it.. */ | |
17021 | ||
17022 | bool | |
17023 | always_instantiate_p (tree decl) | |
17024 | { | |
17025 | /* We always instantiate inline functions so that we can inline them. An | |
17026 | explicit instantiation declaration prohibits implicit instantiation of | |
17027 | non-inline functions. With high levels of optimization, we would | |
17028 | normally inline non-inline functions -- but we're not allowed to do | |
17029 | that for "extern template" functions. Therefore, we check | |
17030 | DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */ | |
17031 | return ((TREE_CODE (decl) == FUNCTION_DECL | |
17032 | && DECL_DECLARED_INLINE_P (decl)) | |
17033 | /* And we need to instantiate static data members so that | |
17034 | their initializers are available in integral constant | |
17035 | expressions. */ | |
17036 | || (TREE_CODE (decl) == VAR_DECL | |
aef4a215 | 17037 | && decl_maybe_constant_var_p (decl))); |
15305a73 JM |
17038 | } |
17039 | ||
16d53b64 | 17040 | /* Produce the definition of D, a _DECL generated from a template. If |
838dfd8a | 17041 | DEFER_OK is nonzero, then we don't have to actually do the |
415c974c MA |
17042 | instantiation now; we just have to do it sometime. Normally it is |
17043 | an error if this is an explicit instantiation but D is undefined. | |
eba839f9 MM |
17044 | EXPL_INST_CLASS_MEM_P is true iff D is a member of an |
17045 | explicitly instantiated class template. */ | |
f84b4be9 | 17046 | |
a28e3c7f | 17047 | tree |
3db45ab5 | 17048 | instantiate_decl (tree d, int defer_ok, |
eba839f9 | 17049 | bool expl_inst_class_mem_p) |
a28e3c7f | 17050 | { |
36a117a5 | 17051 | tree tmpl = DECL_TI_TEMPLATE (d); |
65f8b0fb MM |
17052 | tree gen_args; |
17053 | tree args; | |
830bfa74 | 17054 | tree td; |
36a117a5 MM |
17055 | tree code_pattern; |
17056 | tree spec; | |
17057 | tree gen_tmpl; | |
b794e321 | 17058 | bool pattern_defined; |
31a714f6 | 17059 | int need_push; |
82a98427 | 17060 | location_t saved_loc = input_location; |
f7e4e484 | 17061 | bool external_p; |
c8094d83 | 17062 | |
36a117a5 MM |
17063 | /* This function should only be used to instantiate templates for |
17064 | functions and static member variables. */ | |
50bc768d NS |
17065 | gcc_assert (TREE_CODE (d) == FUNCTION_DECL |
17066 | || TREE_CODE (d) == VAR_DECL); | |
36a117a5 | 17067 | |
cec24319 MM |
17068 | /* Variables are never deferred; if instantiation is required, they |
17069 | are instantiated right away. That allows for better code in the | |
17070 | case that an expression refers to the value of the variable -- | |
17071 | if the variable has a constant value the referring expression can | |
17072 | take advantage of that fact. */ | |
aef4a215 JM |
17073 | if (TREE_CODE (d) == VAR_DECL |
17074 | || DECL_DECLARED_CONSTEXPR_P (d)) | |
cec24319 MM |
17075 | defer_ok = 0; |
17076 | ||
db9b2174 MM |
17077 | /* Don't instantiate cloned functions. Instead, instantiate the |
17078 | functions they cloned. */ | |
17079 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) | |
17080 | d = DECL_CLONED_FUNCTION (d); | |
17081 | ||
2b110bfc JM |
17082 | if (DECL_TEMPLATE_INSTANTIATED (d) |
17083 | || DECL_TEMPLATE_SPECIALIZATION (d)) | |
17084 | /* D has already been instantiated or explicitly specialized, so | |
17085 | there's nothing for us to do here. | |
17086 | ||
17087 | It might seem reasonable to check whether or not D is an explicit | |
17088 | instantiation, and, if so, stop here. But when an explicit | |
17089 | instantiation is deferred until the end of the compilation, | |
17090 | DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do | |
17091 | the instantiation. */ | |
36a117a5 MM |
17092 | return d; |
17093 | ||
15305a73 JM |
17094 | /* Check to see whether we know that this template will be |
17095 | instantiated in some other file, as with "extern template" | |
17096 | extension. */ | |
17097 | external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d)); | |
17098 | ||
17099 | /* In general, we do not instantiate such templates. */ | |
17100 | if (external_p && !always_instantiate_p (d)) | |
17101 | return d; | |
17102 | ||
36a117a5 | 17103 | gen_tmpl = most_general_template (tmpl); |
65f8b0fb | 17104 | gen_args = DECL_TI_ARGS (d); |
2b110bfc JM |
17105 | |
17106 | if (tmpl != gen_tmpl) | |
17107 | /* We should already have the extra args. */ | |
17108 | gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) | |
17109 | == TMPL_ARGS_DEPTH (gen_args)); | |
17110 | /* And what's in the hash table should match D. */ | |
17111 | gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d | |
17112 | || spec == NULL_TREE); | |
36a117a5 MM |
17113 | |
17114 | /* This needs to happen before any tsubsting. */ | |
17115 | if (! push_tinst_level (d)) | |
17116 | return d; | |
17117 | ||
297a5329 JM |
17118 | timevar_push (TV_PARSE); |
17119 | ||
4d85e00e | 17120 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
a723baf1 MM |
17121 | for the instantiation. */ |
17122 | td = template_for_substitution (d); | |
fee23f54 | 17123 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 17124 | |
2d22db1f | 17125 | /* We should never be trying to instantiate a member of a class |
3db45ab5 | 17126 | template or partial specialization. */ |
2d22db1f | 17127 | gcc_assert (d != code_pattern); |
3db45ab5 | 17128 | |
76d3baad KL |
17129 | if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) |
17130 | || DECL_TEMPLATE_SPECIALIZATION (td)) | |
649fc72d NS |
17131 | /* In the case of a friend template whose definition is provided |
17132 | outside the class, we may have too many arguments. Drop the | |
76d3baad | 17133 | ones we don't need. The same is true for specializations. */ |
649fc72d NS |
17134 | args = get_innermost_template_args |
17135 | (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); | |
17136 | else | |
17137 | args = gen_args; | |
65f8b0fb | 17138 | |
5566b478 | 17139 | if (TREE_CODE (d) == FUNCTION_DECL) |
39c76b4f | 17140 | pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); |
5566b478 | 17141 | else |
36a117a5 | 17142 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
b794e321 MM |
17143 | |
17144 | /* We may be in the middle of deferred access check. Disable it now. */ | |
17145 | push_deferring_access_checks (dk_no_deferred); | |
17146 | ||
4684cd27 MM |
17147 | /* Unless an explicit instantiation directive has already determined |
17148 | the linkage of D, remember that a definition is available for | |
17149 | this entity. */ | |
c8094d83 | 17150 | if (pattern_defined |
4684cd27 MM |
17151 | && !DECL_INTERFACE_KNOWN (d) |
17152 | && !DECL_NOT_REALLY_EXTERN (d)) | |
17153 | mark_definable (d); | |
de22184b | 17154 | |
3862bc3a JY |
17155 | DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern); |
17156 | DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern); | |
f31686a3 | 17157 | input_location = DECL_SOURCE_LOCATION (d); |
de22184b | 17158 | |
eba839f9 MM |
17159 | /* If D is a member of an explicitly instantiated class template, |
17160 | and no definition is available, treat it like an implicit | |
3db45ab5 MS |
17161 | instantiation. */ |
17162 | if (!pattern_defined && expl_inst_class_mem_p | |
17163 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
5566b478 | 17164 | { |
4684cd27 | 17165 | DECL_NOT_REALLY_EXTERN (d) = 0; |
eba839f9 | 17166 | DECL_INTERFACE_KNOWN (d) = 0; |
4684cd27 | 17167 | SET_DECL_IMPLICIT_INSTANTIATION (d); |
5566b478 MS |
17168 | } |
17169 | ||
d5f4eddd JM |
17170 | /* Recheck the substitutions to obtain any warning messages |
17171 | about ignoring cv qualifiers. Don't do this for artificial decls, | |
17172 | as it breaks the context-sensitive substitution for lambda op(). */ | |
17173 | if (!defer_ok && !DECL_ARTIFICIAL (d)) | |
4f2b0fb2 | 17174 | { |
4f2b0fb2 NS |
17175 | tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); |
17176 | tree type = TREE_TYPE (gen); | |
17177 | ||
0e902d98 | 17178 | /* Make sure that we can see identifiers, and compute access |
2b59fc25 KL |
17179 | correctly. D is already the target FUNCTION_DECL with the |
17180 | right context. */ | |
17181 | push_access_scope (d); | |
0e902d98 | 17182 | |
4f2b0fb2 NS |
17183 | if (TREE_CODE (gen) == FUNCTION_DECL) |
17184 | { | |
23fca1f5 | 17185 | tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d); |
5d80a306 DG |
17186 | tsubst_exception_specification (type, gen_args, tf_warning_or_error, |
17187 | d); | |
4f2b0fb2 NS |
17188 | /* Don't simply tsubst the function type, as that will give |
17189 | duplicate warnings about poor parameter qualifications. | |
17190 | The function arguments are the same as the decl_arguments | |
c6002625 | 17191 | without the top level cv qualifiers. */ |
4f2b0fb2 NS |
17192 | type = TREE_TYPE (type); |
17193 | } | |
23fca1f5 | 17194 | tsubst (type, gen_args, tf_warning_or_error, d); |
0e902d98 | 17195 | |
2b59fc25 | 17196 | pop_access_scope (d); |
4f2b0fb2 | 17197 | } |
c8094d83 | 17198 | |
16d53b64 | 17199 | /* Defer all other templates, unless we have been explicitly |
f7e4e484 MM |
17200 | forbidden from doing so. */ |
17201 | if (/* If there is no definition, we cannot instantiate the | |
17202 | template. */ | |
3db45ab5 | 17203 | ! pattern_defined |
f7e4e484 MM |
17204 | /* If it's OK to postpone instantiation, do so. */ |
17205 | || defer_ok | |
17206 | /* If this is a static data member that will be defined | |
17207 | elsewhere, we don't want to instantiate the entire data | |
17208 | member, but we do want to instantiate the initializer so that | |
17209 | we can substitute that elsewhere. */ | |
17210 | || (external_p && TREE_CODE (d) == VAR_DECL)) | |
16d53b64 | 17211 | { |
b794e321 MM |
17212 | /* The definition of the static data member is now required so |
17213 | we must substitute the initializer. */ | |
17214 | if (TREE_CODE (d) == VAR_DECL | |
3db45ab5 | 17215 | && !DECL_INITIAL (d) |
b794e321 MM |
17216 | && DECL_INITIAL (code_pattern)) |
17217 | { | |
4bff36d3 MM |
17218 | tree ns; |
17219 | tree init; | |
fa2200cb | 17220 | bool const_init = false; |
4bff36d3 MM |
17221 | |
17222 | ns = decl_namespace_context (d); | |
27a725e2 | 17223 | push_nested_namespace (ns); |
b794e321 | 17224 | push_nested_class (DECL_CONTEXT (d)); |
3db45ab5 | 17225 | init = tsubst_expr (DECL_INITIAL (code_pattern), |
4bff36d3 | 17226 | args, |
015c2c66 MM |
17227 | tf_warning_or_error, NULL_TREE, |
17228 | /*integral_constant_expression_p=*/false); | |
fa2200cb JM |
17229 | /* Make sure the initializer is still constant, in case of |
17230 | circular dependency (template/instantiate6.C). */ | |
17231 | const_init | |
17232 | = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern); | |
17233 | cp_finish_decl (d, init, /*init_const_expr_p=*/const_init, | |
d174af6c | 17234 | /*asmspec_tree=*/NULL_TREE, |
86414779 | 17235 | LOOKUP_ONLYCONVERTING); |
b794e321 | 17236 | pop_nested_class (); |
27a725e2 | 17237 | pop_nested_namespace (ns); |
b794e321 MM |
17238 | } |
17239 | ||
f7e4e484 MM |
17240 | /* We restore the source position here because it's used by |
17241 | add_pending_template. */ | |
82a98427 | 17242 | input_location = saved_loc; |
c27be9b9 | 17243 | |
c8094d83 | 17244 | if (at_eof && !pattern_defined |
da3933ba JM |
17245 | && DECL_EXPLICIT_INSTANTIATION (d) |
17246 | && DECL_NOT_REALLY_EXTERN (d)) | |
03d0f4af MM |
17247 | /* [temp.explicit] |
17248 | ||
17249 | The definition of a non-exported function template, a | |
17250 | non-exported member function template, or a non-exported | |
17251 | member function or static data member of a class template | |
17252 | shall be present in every translation unit in which it is | |
17253 | explicitly instantiated. */ | |
cbe5f3b3 MLI |
17254 | permerror (input_location, "explicit instantiation of %qD " |
17255 | "but no definition available", d); | |
03d0f4af | 17256 | |
aef4a215 JM |
17257 | /* If we're in unevaluated context, we just wanted to get the |
17258 | constant value; this isn't an odr use, so don't queue | |
17259 | a full instantiation. */ | |
17260 | if (cp_unevaluated_operand != 0) | |
17261 | goto out; | |
f7e4e484 MM |
17262 | /* ??? Historically, we have instantiated inline functions, even |
17263 | when marked as "extern template". */ | |
17264 | if (!(external_p && TREE_CODE (d) == VAR_DECL)) | |
17265 | add_pending_template (d); | |
de22184b | 17266 | goto out; |
5566b478 | 17267 | } |
4684cd27 MM |
17268 | /* Tell the repository that D is available in this translation unit |
17269 | -- and see if it is supposed to be instantiated here. */ | |
17270 | if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d)) | |
17271 | { | |
17272 | /* In a PCH file, despite the fact that the repository hasn't | |
17273 | requested instantiation in the PCH it is still possible that | |
17274 | an instantiation will be required in a file that includes the | |
17275 | PCH. */ | |
17276 | if (pch_file) | |
17277 | add_pending_template (d); | |
17278 | /* Instantiate inline functions so that the inliner can do its | |
17279 | job, even though we'll not be emitting a copy of this | |
17280 | function. */ | |
4847609e | 17281 | if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d))) |
4684cd27 MM |
17282 | goto out; |
17283 | } | |
5566b478 | 17284 | |
6de9cd9a | 17285 | need_push = !cfun || !global_bindings_p (); |
31a714f6 MM |
17286 | if (need_push) |
17287 | push_to_top_level (); | |
414ea4aa | 17288 | |
66e0c440 KL |
17289 | /* Mark D as instantiated so that recursive calls to |
17290 | instantiate_decl do not try to instantiate it again. */ | |
17291 | DECL_TEMPLATE_INSTANTIATED (d) = 1; | |
17292 | ||
2b0a63a3 MM |
17293 | /* Regenerate the declaration in case the template has been modified |
17294 | by a subsequent redeclaration. */ | |
17295 | regenerate_decl_from_template (d, td); | |
4684cd27 | 17296 | |
120722ac | 17297 | /* We already set the file and line above. Reset them now in case |
6de9cd9a | 17298 | they changed as a result of calling regenerate_decl_from_template. */ |
f31686a3 | 17299 | input_location = DECL_SOURCE_LOCATION (d); |
5156628f | 17300 | |
5566b478 MS |
17301 | if (TREE_CODE (d) == VAR_DECL) |
17302 | { | |
e92fb501 | 17303 | tree init; |
fa2200cb | 17304 | bool const_init = false; |
e92fb501 | 17305 | |
1d62c33e MM |
17306 | /* Clear out DECL_RTL; whatever was there before may not be right |
17307 | since we've reset the type of the declaration. */ | |
245763e3 | 17308 | SET_DECL_RTL (d, NULL); |
5566b478 | 17309 | DECL_IN_AGGR_P (d) = 0; |
ea56c40c | 17310 | |
e92fb501 | 17311 | /* The initializer is placed in DECL_INITIAL by |
fa2200cb JM |
17312 | regenerate_decl_from_template so we don't need to |
17313 | push/pop_access_scope again here. Pull it out so that | |
3600f678 | 17314 | cp_finish_decl can process it. */ |
e92fb501 MM |
17315 | init = DECL_INITIAL (d); |
17316 | DECL_INITIAL (d) = NULL_TREE; | |
17317 | DECL_INITIALIZED_P (d) = 0; | |
17318 | ||
4684cd27 MM |
17319 | /* Clear DECL_EXTERNAL so that cp_finish_decl will process the |
17320 | initializer. That function will defer actual emission until | |
17321 | we have a chance to determine linkage. */ | |
17322 | DECL_EXTERNAL (d) = 0; | |
17323 | ||
73a8adb6 | 17324 | /* Enter the scope of D so that access-checking works correctly. */ |
4684cd27 | 17325 | push_nested_class (DECL_CONTEXT (d)); |
fa2200cb JM |
17326 | const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern); |
17327 | cp_finish_decl (d, init, const_init, NULL_TREE, 0); | |
73a8adb6 | 17328 | pop_nested_class (); |
5566b478 MS |
17329 | } |
17330 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
17331 | { | |
6bbf1598 | 17332 | htab_t saved_local_specializations; |
a723baf1 MM |
17333 | tree subst_decl; |
17334 | tree tmpl_parm; | |
17335 | tree spec_parm; | |
6bbf1598 MM |
17336 | |
17337 | /* Save away the current list, in case we are instantiating one | |
17338 | template from within the body of another. */ | |
17339 | saved_local_specializations = local_specializations; | |
17340 | ||
6dfbb909 | 17341 | /* Set up the list of local specializations. */ |
c8094d83 | 17342 | local_specializations = htab_create (37, |
69f794a7 | 17343 | hash_local_specialization, |
a723baf1 | 17344 | eq_local_specializations, |
6dfbb909 MM |
17345 | NULL); |
17346 | ||
558475f0 | 17347 | /* Set up context. */ |
058b15c1 | 17348 | start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED); |
5566b478 | 17349 | |
a723baf1 MM |
17350 | /* Create substitution entries for the parameters. */ |
17351 | subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d)); | |
17352 | tmpl_parm = DECL_ARGUMENTS (subst_decl); | |
17353 | spec_parm = DECL_ARGUMENTS (d); | |
17354 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d)) | |
17355 | { | |
17356 | register_local_specialization (spec_parm, tmpl_parm); | |
17357 | spec_parm = skip_artificial_parms_for (d, spec_parm); | |
17358 | tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm); | |
17359 | } | |
5d80a306 | 17360 | while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
a723baf1 MM |
17361 | { |
17362 | register_local_specialization (spec_parm, tmpl_parm); | |
910ad8de NF |
17363 | tmpl_parm = DECL_CHAIN (tmpl_parm); |
17364 | spec_parm = DECL_CHAIN (spec_parm); | |
a723baf1 | 17365 | } |
5d80a306 DG |
17366 | if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
17367 | { | |
5d80a306 DG |
17368 | /* Register the (value) argument pack as a specialization of |
17369 | TMPL_PARM, then move on. */ | |
6afcfe0a | 17370 | tree argpack = make_fnparm_pack (spec_parm); |
5d80a306 | 17371 | register_local_specialization (argpack, tmpl_parm); |
910ad8de | 17372 | tmpl_parm = DECL_CHAIN (tmpl_parm); |
6afcfe0a | 17373 | spec_parm = NULL_TREE; |
5d80a306 | 17374 | } |
50bc768d | 17375 | gcc_assert (!spec_parm); |
a723baf1 | 17376 | |
558475f0 MM |
17377 | /* Substitute into the body of the function. */ |
17378 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
015c2c66 MM |
17379 | tf_warning_or_error, tmpl, |
17380 | /*integral_constant_expression_p=*/false); | |
558475f0 | 17381 | |
75407da3 SP |
17382 | /* Set the current input_location to the end of the function |
17383 | so that finish_function knows where we are. */ | |
17384 | input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus; | |
17385 | ||
6dfbb909 MM |
17386 | /* We don't need the local specializations any more. */ |
17387 | htab_delete (local_specializations); | |
6bbf1598 | 17388 | local_specializations = saved_local_specializations; |
6dfbb909 | 17389 | |
4d6abc1c | 17390 | /* Finish the function. */ |
b2dd096b | 17391 | d = finish_function (0); |
8cd2462c | 17392 | expand_or_defer_fn (d); |
5566b478 MS |
17393 | } |
17394 | ||
971cbc14 MM |
17395 | /* We're not deferring instantiation any more. */ |
17396 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; | |
17397 | ||
31a714f6 MM |
17398 | if (need_push) |
17399 | pop_from_top_level (); | |
414ea4aa | 17400 | |
de22184b | 17401 | out: |
82a98427 | 17402 | input_location = saved_loc; |
7d021397 | 17403 | pop_deferring_access_checks (); |
5566b478 | 17404 | pop_tinst_level (); |
a28e3c7f | 17405 | |
297a5329 JM |
17406 | timevar_pop (TV_PARSE); |
17407 | ||
a28e3c7f MS |
17408 | return d; |
17409 | } | |
5566b478 | 17410 | |
0aafb128 | 17411 | /* Run through the list of templates that we wish we could |
35046a54 KL |
17412 | instantiate, and instantiate any we can. RETRIES is the |
17413 | number of times we retry pending template instantiation. */ | |
0aafb128 | 17414 | |
35046a54 KL |
17415 | void |
17416 | instantiate_pending_templates (int retries) | |
0aafb128 | 17417 | { |
0aafb128 | 17418 | int reconsider; |
aad626f7 | 17419 | location_t saved_loc = input_location; |
35046a54 KL |
17420 | |
17421 | /* Instantiating templates may trigger vtable generation. This in turn | |
17422 | may require further template instantiations. We place a limit here | |
17423 | to avoid infinite loop. */ | |
17424 | if (pending_templates && retries >= max_tinst_depth) | |
17425 | { | |
e2c3721c | 17426 | tree decl = pending_templates->tinst->decl; |
96c993a8 | 17427 | |
dee15844 | 17428 | error ("template instantiation depth exceeds maximum of %d" |
96c993a8 | 17429 | " instantiating %q+D, possibly from virtual table generation" |
124e0d27 | 17430 | " (use -ftemplate-depth= to increase the maximum)", |
96c993a8 JM |
17431 | max_tinst_depth, decl); |
17432 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
17433 | /* Pretend that we defined it. */ | |
17434 | DECL_INITIAL (decl) = error_mark_node; | |
35046a54 KL |
17435 | return; |
17436 | } | |
17437 | ||
c8094d83 | 17438 | do |
0aafb128 | 17439 | { |
e2c3721c PB |
17440 | struct pending_template **t = &pending_templates; |
17441 | struct pending_template *last = NULL; | |
0aafb128 | 17442 | reconsider = 0; |
0aafb128 MM |
17443 | while (*t) |
17444 | { | |
e2c3721c PB |
17445 | tree instantiation = reopen_tinst_level ((*t)->tinst); |
17446 | bool complete = false; | |
0aafb128 | 17447 | |
2f939d94 | 17448 | if (TYPE_P (instantiation)) |
0aafb128 MM |
17449 | { |
17450 | tree fn; | |
17451 | ||
d0f062fb | 17452 | if (!COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
17453 | { |
17454 | instantiate_class_template (instantiation); | |
17455 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
c8094d83 | 17456 | for (fn = TYPE_METHODS (instantiation); |
0aafb128 MM |
17457 | fn; |
17458 | fn = TREE_CHAIN (fn)) | |
17459 | if (! DECL_ARTIFICIAL (fn)) | |
3db45ab5 | 17460 | instantiate_decl (fn, |
eba839f9 MM |
17461 | /*defer_ok=*/0, |
17462 | /*expl_inst_class_mem_p=*/false); | |
d0f062fb | 17463 | if (COMPLETE_TYPE_P (instantiation)) |
35046a54 | 17464 | reconsider = 1; |
0aafb128 MM |
17465 | } |
17466 | ||
e2c3721c | 17467 | complete = COMPLETE_TYPE_P (instantiation); |
0aafb128 MM |
17468 | } |
17469 | else | |
17470 | { | |
16d53b64 | 17471 | if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
17472 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) |
17473 | { | |
3db45ab5 | 17474 | instantiation |
eba839f9 MM |
17475 | = instantiate_decl (instantiation, |
17476 | /*defer_ok=*/0, | |
17477 | /*expl_inst_class_mem_p=*/false); | |
0aafb128 | 17478 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) |
35046a54 | 17479 | reconsider = 1; |
0aafb128 MM |
17480 | } |
17481 | ||
e2c3721c PB |
17482 | complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation) |
17483 | || DECL_TEMPLATE_INSTANTIATED (instantiation)); | |
17484 | } | |
17485 | ||
17486 | if (complete) | |
17487 | /* If INSTANTIATION has been instantiated, then we don't | |
17488 | need to consider it again in the future. */ | |
17489 | *t = (*t)->next; | |
17490 | else | |
17491 | { | |
17492 | last = *t; | |
17493 | t = &(*t)->next; | |
0aafb128 | 17494 | } |
84e5ca0f | 17495 | tinst_depth = 0; |
e2c3721c | 17496 | current_tinst_level = NULL; |
0aafb128 | 17497 | } |
46ccf50a | 17498 | last_pending_template = last; |
c8094d83 | 17499 | } |
0aafb128 MM |
17500 | while (reconsider); |
17501 | ||
aad626f7 | 17502 | input_location = saved_loc; |
0aafb128 MM |
17503 | } |
17504 | ||
fd74ca0b MM |
17505 | /* Substitute ARGVEC into T, which is a list of initializers for |
17506 | either base class or a non-static data member. The TREE_PURPOSEs | |
17507 | are DECLs, and the TREE_VALUEs are the initializer values. Used by | |
17508 | instantiate_decl. */ | |
4393e105 | 17509 | |
824b9a4c | 17510 | static tree |
3a978d72 | 17511 | tsubst_initializer_list (tree t, tree argvec) |
5566b478 | 17512 | { |
2282d28d | 17513 | tree inits = NULL_TREE; |
5566b478 MS |
17514 | |
17515 | for (; t; t = TREE_CHAIN (t)) | |
17516 | { | |
fd74ca0b MM |
17517 | tree decl; |
17518 | tree init; | |
5d80a306 DG |
17519 | tree expanded_bases = NULL_TREE; |
17520 | tree expanded_arguments = NULL_TREE; | |
17521 | int i, len = 1; | |
fd74ca0b | 17522 | |
5d80a306 DG |
17523 | if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION) |
17524 | { | |
17525 | tree expr; | |
17526 | tree arg; | |
17527 | ||
17528 | /* Expand the base class expansion type into separate base | |
17529 | classes. */ | |
17530 | expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec, | |
17531 | tf_warning_or_error, | |
17532 | NULL_TREE); | |
17533 | if (expanded_bases == error_mark_node) | |
17534 | continue; | |
17535 | ||
17536 | /* We'll be building separate TREE_LISTs of arguments for | |
17537 | each base. */ | |
17538 | len = TREE_VEC_LENGTH (expanded_bases); | |
17539 | expanded_arguments = make_tree_vec (len); | |
17540 | for (i = 0; i < len; i++) | |
17541 | TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE; | |
17542 | ||
17543 | /* Build a dummy EXPR_PACK_EXPANSION that will be used to | |
17544 | expand each argument in the TREE_VALUE of t. */ | |
17545 | expr = make_node (EXPR_PACK_EXPANSION); | |
17546 | PACK_EXPANSION_PARAMETER_PACKS (expr) = | |
17547 | PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t)); | |
17548 | ||
625b6d91 DG |
17549 | if (TREE_VALUE (t) == void_type_node) |
17550 | /* VOID_TYPE_NODE is used to indicate | |
17551 | value-initialization. */ | |
17552 | { | |
17553 | for (i = 0; i < len; i++) | |
17554 | TREE_VEC_ELT (expanded_arguments, i) = void_type_node; | |
17555 | } | |
17556 | else | |
17557 | { | |
17558 | /* Substitute parameter packs into each argument in the | |
17559 | TREE_LIST. */ | |
17560 | in_base_initializer = 1; | |
17561 | for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg)) | |
17562 | { | |
17563 | tree expanded_exprs; | |
17564 | ||
17565 | /* Expand the argument. */ | |
17566 | SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg)); | |
17567 | expanded_exprs | |
17568 | = tsubst_pack_expansion (expr, argvec, | |
17569 | tf_warning_or_error, | |
17570 | NULL_TREE); | |
aecaad79 PC |
17571 | if (expanded_exprs == error_mark_node) |
17572 | continue; | |
625b6d91 DG |
17573 | |
17574 | /* Prepend each of the expanded expressions to the | |
17575 | corresponding TREE_LIST in EXPANDED_ARGUMENTS. */ | |
17576 | for (i = 0; i < len; i++) | |
17577 | { | |
17578 | TREE_VEC_ELT (expanded_arguments, i) = | |
17579 | tree_cons (NULL_TREE, | |
17580 | TREE_VEC_ELT (expanded_exprs, i), | |
17581 | TREE_VEC_ELT (expanded_arguments, i)); | |
17582 | } | |
17583 | } | |
17584 | in_base_initializer = 0; | |
5d80a306 | 17585 | |
625b6d91 DG |
17586 | /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS, |
17587 | since we built them backwards. */ | |
17588 | for (i = 0; i < len; i++) | |
17589 | { | |
17590 | TREE_VEC_ELT (expanded_arguments, i) = | |
17591 | nreverse (TREE_VEC_ELT (expanded_arguments, i)); | |
17592 | } | |
17593 | } | |
5d80a306 | 17594 | } |
1f5a253a | 17595 | |
5d80a306 DG |
17596 | for (i = 0; i < len; ++i) |
17597 | { | |
17598 | if (expanded_bases) | |
17599 | { | |
17600 | decl = TREE_VEC_ELT (expanded_bases, i); | |
17601 | decl = expand_member_init (decl); | |
17602 | init = TREE_VEC_ELT (expanded_arguments, i); | |
17603 | } | |
17604 | else | |
17605 | { | |
17606 | decl = tsubst_copy (TREE_PURPOSE (t), argvec, | |
17607 | tf_warning_or_error, NULL_TREE); | |
17608 | ||
17609 | decl = expand_member_init (decl); | |
17610 | if (decl && !DECL_P (decl)) | |
17611 | in_base_initializer = 1; | |
17612 | ||
e90b1c4a JM |
17613 | init = TREE_VALUE (t); |
17614 | if (init != void_type_node) | |
17615 | init = tsubst_expr (init, argvec, | |
17616 | tf_warning_or_error, NULL_TREE, | |
17617 | /*integral_constant_expression_p=*/false); | |
5d80a306 DG |
17618 | in_base_initializer = 0; |
17619 | } | |
17620 | ||
17621 | if (decl) | |
17622 | { | |
17623 | init = build_tree_list (decl, init); | |
17624 | TREE_CHAIN (init) = inits; | |
17625 | inits = init; | |
17626 | } | |
17627 | } | |
5566b478 | 17628 | } |
2282d28d | 17629 | return inits; |
5566b478 MS |
17630 | } |
17631 | ||
61a127b3 MM |
17632 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
17633 | ||
17634 | static void | |
3a978d72 | 17635 | set_current_access_from_decl (tree decl) |
61a127b3 MM |
17636 | { |
17637 | if (TREE_PRIVATE (decl)) | |
17638 | current_access_specifier = access_private_node; | |
17639 | else if (TREE_PROTECTED (decl)) | |
17640 | current_access_specifier = access_protected_node; | |
17641 | else | |
17642 | current_access_specifier = access_public_node; | |
17643 | } | |
17644 | ||
dbfe2124 MM |
17645 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
17646 | is the instantiation (which should have been created with | |
17647 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 17648 | |
dbfe2124 | 17649 | static void |
3a978d72 | 17650 | tsubst_enum (tree tag, tree newtag, tree args) |
b87692e5 | 17651 | { |
dbfe2124 | 17652 | tree e; |
b87692e5 | 17653 | |
90ecaf87 RRC |
17654 | if (SCOPED_ENUM_P (newtag)) |
17655 | begin_scope (sk_scoped_enum, newtag); | |
17656 | ||
b87692e5 MS |
17657 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) |
17658 | { | |
61a127b3 | 17659 | tree value; |
7b6d72fc MM |
17660 | tree decl; |
17661 | ||
17662 | decl = TREE_VALUE (e); | |
61a127b3 MM |
17663 | /* Note that in a template enum, the TREE_VALUE is the |
17664 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
c8094d83 | 17665 | value = tsubst_expr (DECL_INITIAL (decl), |
015c2c66 MM |
17666 | args, tf_warning_or_error, NULL_TREE, |
17667 | /*integral_constant_expression_p=*/true); | |
61a127b3 MM |
17668 | |
17669 | /* Give this enumeration constant the correct access. */ | |
7b6d72fc | 17670 | set_current_access_from_decl (decl); |
61a127b3 MM |
17671 | |
17672 | /* Actually build the enumerator itself. */ | |
4cc2a722 AC |
17673 | build_enumerator |
17674 | (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl)); | |
dbfe2124 | 17675 | } |
b3d5a58b | 17676 | |
90ecaf87 RRC |
17677 | if (SCOPED_ENUM_P (newtag)) |
17678 | finish_scope (); | |
17679 | ||
17680 | finish_enum_value_list (newtag); | |
219670f1 | 17681 | finish_enum (newtag); |
90ecaf87 | 17682 | |
f31686a3 RH |
17683 | DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) |
17684 | = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); | |
b87692e5 | 17685 | } |
36a117a5 | 17686 | |
1f6e1acc AS |
17687 | /* DECL is a FUNCTION_DECL that is a template specialization. Return |
17688 | its type -- but without substituting the innermost set of template | |
17689 | arguments. So, innermost set of template parameters will appear in | |
5c74d5b0 | 17690 | the type. */ |
1f6e1acc | 17691 | |
c8094d83 | 17692 | tree |
3a978d72 | 17693 | get_mostly_instantiated_function_type (tree decl) |
1f6e1acc | 17694 | { |
1f6e1acc AS |
17695 | tree fn_type; |
17696 | tree tmpl; | |
17697 | tree targs; | |
17698 | tree tparms; | |
17699 | int parm_depth; | |
17700 | ||
17701 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
17702 | targs = DECL_TI_ARGS (decl); | |
17703 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
17704 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
17705 | ||
17706 | /* There should be as many levels of arguments as there are levels | |
17707 | of parameters. */ | |
50bc768d | 17708 | gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs)); |
1f6e1acc AS |
17709 | |
17710 | fn_type = TREE_TYPE (tmpl); | |
1f6e1acc AS |
17711 | |
17712 | if (parm_depth == 1) | |
17713 | /* No substitution is necessary. */ | |
17714 | ; | |
17715 | else | |
17716 | { | |
fae6e246 | 17717 | int i, save_access_control; |
1f6e1acc AS |
17718 | tree partial_args; |
17719 | ||
17720 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
dc957d14 | 17721 | let tsubst know not to substitute for those parameters. */ |
1f6e1acc AS |
17722 | partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); |
17723 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
17724 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
17725 | TMPL_ARGS_LEVEL (targs, i)); | |
17726 | SET_TMPL_ARGS_LEVEL (partial_args, | |
17727 | TMPL_ARGS_DEPTH (targs), | |
17728 | make_tree_vec (DECL_NTPARMS (tmpl))); | |
17729 | ||
fae6e246 RH |
17730 | /* Disable access control as this function is used only during |
17731 | name-mangling. */ | |
17732 | save_access_control = flag_access_control; | |
17733 | flag_access_control = 0; | |
5c74d5b0 | 17734 | |
9579624e | 17735 | ++processing_template_decl; |
1f6e1acc AS |
17736 | /* Now, do the (partial) substitution to figure out the |
17737 | appropriate function type. */ | |
c2ea3a40 | 17738 | fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); |
9579624e | 17739 | --processing_template_decl; |
1f6e1acc AS |
17740 | |
17741 | /* Substitute into the template parameters to obtain the real | |
17742 | innermost set of parameters. This step is important if the | |
17743 | innermost set of template parameters contains value | |
17744 | parameters whose types depend on outer template parameters. */ | |
17745 | TREE_VEC_LENGTH (partial_args)--; | |
c2ea3a40 | 17746 | tparms = tsubst_template_parms (tparms, partial_args, tf_error); |
1f6e1acc | 17747 | |
fae6e246 | 17748 | flag_access_control = save_access_control; |
5c74d5b0 | 17749 | } |
1f6e1acc AS |
17750 | |
17751 | return fn_type; | |
17752 | } | |
669ec2b4 | 17753 | |
cb753e49 | 17754 | /* Return truthvalue if we're processing a template different from |
dc957d14 | 17755 | the last one involved in diagnostics. */ |
cb753e49 | 17756 | int |
3a978d72 | 17757 | problematic_instantiation_changed (void) |
cb753e49 GDR |
17758 | { |
17759 | return last_template_error_tick != tinst_level_tick; | |
17760 | } | |
17761 | ||
17762 | /* Remember current template involved in diagnostics. */ | |
17763 | void | |
3a978d72 | 17764 | record_last_problematic_instantiation (void) |
cb753e49 GDR |
17765 | { |
17766 | last_template_error_tick = tinst_level_tick; | |
17767 | } | |
17768 | ||
e2c3721c | 17769 | struct tinst_level * |
3a978d72 | 17770 | current_instantiation (void) |
cb753e49 GDR |
17771 | { |
17772 | return current_tinst_level; | |
17773 | } | |
db3f4e4e NS |
17774 | |
17775 | /* [temp.param] Check that template non-type parm TYPE is of an allowable | |
838dfd8a | 17776 | type. Return zero for ok, nonzero for disallowed. Issue error and |
c2ea3a40 | 17777 | warning messages under control of COMPLAIN. */ |
db3f4e4e NS |
17778 | |
17779 | static int | |
3a978d72 | 17780 | invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain) |
db3f4e4e | 17781 | { |
550a799d | 17782 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
db3f4e4e NS |
17783 | return 0; |
17784 | else if (POINTER_TYPE_P (type)) | |
17785 | return 0; | |
a5ac359a | 17786 | else if (TYPE_PTR_TO_MEMBER_P (type)) |
db3f4e4e | 17787 | return 0; |
db3f4e4e NS |
17788 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
17789 | return 0; | |
17790 | else if (TREE_CODE (type) == TYPENAME_TYPE) | |
17791 | return 0; | |
c8094d83 | 17792 | |
c2ea3a40 | 17793 | if (complain & tf_error) |
0f51ccfc | 17794 | error ("%q#T is not a valid type for a template constant parameter", type); |
db3f4e4e NS |
17795 | return 1; |
17796 | } | |
e2500fed | 17797 | |
5552b43c MM |
17798 | /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. |
17799 | Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ | |
1fb3244a | 17800 | |
5552b43c MM |
17801 | static bool |
17802 | dependent_type_p_r (tree type) | |
1fb3244a MM |
17803 | { |
17804 | tree scope; | |
17805 | ||
1fb3244a MM |
17806 | /* [temp.dep.type] |
17807 | ||
17808 | A type is dependent if it is: | |
17809 | ||
6615c446 JO |
17810 | -- a template parameter. Template template parameters are types |
17811 | for us (since TYPE_P holds true for them) so we handle | |
17812 | them here. */ | |
c8094d83 | 17813 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
6bf92cb6 | 17814 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) |
1fb3244a MM |
17815 | return true; |
17816 | /* -- a qualified-id with a nested-name-specifier which contains a | |
0cbd7506 | 17817 | class-name that names a dependent type or whose unqualified-id |
1fb3244a MM |
17818 | names a dependent type. */ |
17819 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
17820 | return true; | |
17821 | /* -- a cv-qualified type where the cv-unqualified type is | |
0cbd7506 | 17822 | dependent. */ |
1fb3244a MM |
17823 | type = TYPE_MAIN_VARIANT (type); |
17824 | /* -- a compound type constructed from any dependent type. */ | |
a5ac359a | 17825 | if (TYPE_PTR_TO_MEMBER_P (type)) |
1fb3244a | 17826 | return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) |
c8094d83 | 17827 | || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE |
1fb3244a MM |
17828 | (type))); |
17829 | else if (TREE_CODE (type) == POINTER_TYPE | |
17830 | || TREE_CODE (type) == REFERENCE_TYPE) | |
17831 | return dependent_type_p (TREE_TYPE (type)); | |
17832 | else if (TREE_CODE (type) == FUNCTION_TYPE | |
17833 | || TREE_CODE (type) == METHOD_TYPE) | |
17834 | { | |
17835 | tree arg_type; | |
17836 | ||
17837 | if (dependent_type_p (TREE_TYPE (type))) | |
17838 | return true; | |
c8094d83 MS |
17839 | for (arg_type = TYPE_ARG_TYPES (type); |
17840 | arg_type; | |
1fb3244a MM |
17841 | arg_type = TREE_CHAIN (arg_type)) |
17842 | if (dependent_type_p (TREE_VALUE (arg_type))) | |
17843 | return true; | |
17844 | return false; | |
17845 | } | |
17846 | /* -- an array type constructed from any dependent type or whose | |
0cbd7506 | 17847 | size is specified by a constant expression that is |
3fdb8851 JM |
17848 | value-dependent. |
17849 | ||
17850 | We checked for type- and value-dependence of the bounds in | |
17851 | compute_array_index_type, so TYPE_DEPENDENT_P is already set. */ | |
1fb3244a MM |
17852 | if (TREE_CODE (type) == ARRAY_TYPE) |
17853 | { | |
17854 | if (TYPE_DOMAIN (type) | |
82390eb6 | 17855 | && dependent_type_p (TYPE_DOMAIN (type))) |
1fb3244a MM |
17856 | return true; |
17857 | return dependent_type_p (TREE_TYPE (type)); | |
17858 | } | |
c8094d83 | 17859 | |
1fb3244a | 17860 | /* -- a template-id in which either the template name is a template |
86306a6b NS |
17861 | parameter ... */ |
17862 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
1fb3244a | 17863 | return true; |
86306a6b | 17864 | /* ... or any of the template arguments is a dependent type or |
04c06002 | 17865 | an expression that is type-dependent or value-dependent. */ |
86306a6b | 17866 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) |
c8094d83 | 17867 | && (any_dependent_template_arguments_p |
7e99327d | 17868 | (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type))))) |
1fb3244a | 17869 | return true; |
c8094d83 | 17870 | |
3ad6a8e1 DG |
17871 | /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the |
17872 | argument of the `typeof' expression is not type-dependent, then | |
17873 | it should already been have resolved. */ | |
17874 | if (TREE_CODE (type) == TYPEOF_TYPE | |
17875 | || TREE_CODE (type) == DECLTYPE_TYPE) | |
1fb3244a | 17876 | return true; |
c8094d83 | 17877 | |
5d80a306 DG |
17878 | /* A template argument pack is dependent if any of its packed |
17879 | arguments are. */ | |
17880 | if (TREE_CODE (type) == TYPE_ARGUMENT_PACK) | |
17881 | { | |
17882 | tree args = ARGUMENT_PACK_ARGS (type); | |
17883 | int i, len = TREE_VEC_LENGTH (args); | |
17884 | for (i = 0; i < len; ++i) | |
17885 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
17886 | return true; | |
17887 | } | |
17888 | ||
17889 | /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must | |
17890 | be template parameters. */ | |
17891 | if (TREE_CODE (type) == TYPE_PACK_EXPANSION) | |
17892 | return true; | |
17893 | ||
1fb3244a MM |
17894 | /* The standard does not specifically mention types that are local |
17895 | to template functions or local classes, but they should be | |
17896 | considered dependent too. For example: | |
17897 | ||
c8094d83 | 17898 | template <int I> void f() { |
0cbd7506 | 17899 | enum E { a = I }; |
1fb3244a MM |
17900 | S<sizeof (E)> s; |
17901 | } | |
17902 | ||
17903 | The size of `E' cannot be known until the value of `I' has been | |
17904 | determined. Therefore, `E' must be considered dependent. */ | |
17905 | scope = TYPE_CONTEXT (type); | |
17906 | if (scope && TYPE_P (scope)) | |
17907 | return dependent_type_p (scope); | |
17908 | else if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
17909 | return type_dependent_expression_p (scope); | |
17910 | ||
17911 | /* Other types are non-dependent. */ | |
17912 | return false; | |
17913 | } | |
17914 | ||
5552b43c MM |
17915 | /* Returns TRUE if TYPE is dependent, in the sense of |
17916 | [temp.dep.type]. */ | |
17917 | ||
17918 | bool | |
17919 | dependent_type_p (tree type) | |
17920 | { | |
17921 | /* If there are no template parameters in scope, then there can't be | |
17922 | any dependent types. */ | |
17923 | if (!processing_template_decl) | |
e7e93965 MM |
17924 | { |
17925 | /* If we are not processing a template, then nobody should be | |
17926 | providing us with a dependent type. */ | |
17927 | gcc_assert (type); | |
0257eee5 | 17928 | gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type)); |
e7e93965 MM |
17929 | return false; |
17930 | } | |
5552b43c MM |
17931 | |
17932 | /* If the type is NULL, we have not computed a type for the entity | |
17933 | in question; in that case, the type is dependent. */ | |
17934 | if (!type) | |
17935 | return true; | |
17936 | ||
17937 | /* Erroneous types can be considered non-dependent. */ | |
17938 | if (type == error_mark_node) | |
17939 | return false; | |
17940 | ||
17941 | /* If we have not already computed the appropriate value for TYPE, | |
17942 | do so now. */ | |
17943 | if (!TYPE_DEPENDENT_P_VALID (type)) | |
17944 | { | |
17945 | TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); | |
17946 | TYPE_DEPENDENT_P_VALID (type) = 1; | |
17947 | } | |
17948 | ||
17949 | return TYPE_DEPENDENT_P (type); | |
17950 | } | |
17951 | ||
1cb801bc JM |
17952 | /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any |
17953 | lookup. In other words, a dependent type that is not the current | |
17954 | instantiation. */ | |
17955 | ||
17956 | bool | |
17957 | dependent_scope_p (tree scope) | |
17958 | { | |
b77ba909 JM |
17959 | return (scope && TYPE_P (scope) && dependent_type_p (scope) |
17960 | && !currently_open_class (scope)); | |
1cb801bc JM |
17961 | } |
17962 | ||
20929c7f | 17963 | /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of |
fe89d797 MM |
17964 | [temp.dep.constexpr]. EXPRESSION is already known to be a constant |
17965 | expression. */ | |
1fb3244a | 17966 | |
fa2200cb JM |
17967 | /* FIXME this predicate is not appropriate for general expressions; the |
17968 | predicates we want instead are "valid constant expression, value | |
17969 | dependent or not?", "really constant expression, not value dependent?" | |
17970 | and "instantiation-dependent?". Try to integrate with | |
17971 | potential_constant_expression? | |
17972 | ||
17973 | fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent. | |
17974 | (what about instantiation-dependent constant-expressions?) | |
17975 | is_late_template_attribute: defer if instantiation-dependent. | |
17976 | compute_array_index_type: proceed if constant and not t- or v-dependent | |
17977 | if instantiation-dependent, need to remember full expression | |
17978 | uses_template_parms: FIXME - need to audit callers | |
17979 | tsubst_decl [function_decl]: Why is this using value_dependent_expression_p? | |
17980 | dependent_type_p [array_type]: dependent if index type is dependent | |
17981 | (or non-constant?) | |
17982 | static_assert - instantiation-dependent */ | |
17983 | ||
14d22dd6 | 17984 | bool |
1fb3244a MM |
17985 | value_dependent_expression_p (tree expression) |
17986 | { | |
17987 | if (!processing_template_decl) | |
17988 | return false; | |
17989 | ||
17990 | /* A name declared with a dependent type. */ | |
7416ab02 | 17991 | if (DECL_P (expression) && type_dependent_expression_p (expression)) |
1fb3244a | 17992 | return true; |
c8094d83 | 17993 | |
7416ab02 NS |
17994 | switch (TREE_CODE (expression)) |
17995 | { | |
17996 | case IDENTIFIER_NODE: | |
17997 | /* A name that has not been looked up -- must be dependent. */ | |
17998 | return true; | |
17999 | ||
18000 | case TEMPLATE_PARM_INDEX: | |
18001 | /* A non-type template parm. */ | |
18002 | return true; | |
18003 | ||
18004 | case CONST_DECL: | |
18005 | /* A non-type template parm. */ | |
18006 | if (DECL_TEMPLATE_PARM_P (expression)) | |
d17811fd | 18007 | return true; |
3387721b | 18008 | return value_dependent_expression_p (DECL_INITIAL (expression)); |
d36d5600 | 18009 | |
7416ab02 | 18010 | case VAR_DECL: |
c8094d83 | 18011 | /* A constant with integral or enumeration type and is initialized |
0cbd7506 | 18012 | with an expression that is value-dependent. */ |
7416ab02 NS |
18013 | if (DECL_INITIAL (expression) |
18014 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression)) | |
18015 | && value_dependent_expression_p (DECL_INITIAL (expression))) | |
18016 | return true; | |
18017 | return false; | |
18018 | ||
18019 | case DYNAMIC_CAST_EXPR: | |
18020 | case STATIC_CAST_EXPR: | |
18021 | case CONST_CAST_EXPR: | |
18022 | case REINTERPRET_CAST_EXPR: | |
18023 | case CAST_EXPR: | |
18024 | /* These expressions are value-dependent if the type to which | |
0cbd7506 MS |
18025 | the cast occurs is dependent or the expression being casted |
18026 | is value-dependent. */ | |
7416ab02 NS |
18027 | { |
18028 | tree type = TREE_TYPE (expression); | |
c8094d83 | 18029 | |
7416ab02 NS |
18030 | if (dependent_type_p (type)) |
18031 | return true; | |
c8094d83 | 18032 | |
7416ab02 NS |
18033 | /* A functional cast has a list of operands. */ |
18034 | expression = TREE_OPERAND (expression, 0); | |
18035 | if (!expression) | |
18036 | { | |
18037 | /* If there are no operands, it must be an expression such | |
18038 | as "int()". This should not happen for aggregate types | |
18039 | because it would form non-constant expressions. */ | |
fa2200cb JM |
18040 | gcc_assert (cxx_dialect >= cxx0x |
18041 | || INTEGRAL_OR_ENUMERATION_TYPE_P (type)); | |
c8094d83 | 18042 | |
7416ab02 NS |
18043 | return false; |
18044 | } | |
c8094d83 | 18045 | |
7416ab02 | 18046 | if (TREE_CODE (expression) == TREE_LIST) |
32a11c08 | 18047 | return any_value_dependent_elements_p (expression); |
c8094d83 | 18048 | |
d17811fd | 18049 | return value_dependent_expression_p (expression); |
7416ab02 | 18050 | } |
c8094d83 | 18051 | |
7416ab02 NS |
18052 | case SIZEOF_EXPR: |
18053 | case ALIGNOF_EXPR: | |
18054 | /* A `sizeof' expression is value-dependent if the operand is | |
5d80a306 | 18055 | type-dependent or is a pack expansion. */ |
d17811fd | 18056 | expression = TREE_OPERAND (expression, 0); |
5d80a306 DG |
18057 | if (PACK_EXPANSION_P (expression)) |
18058 | return true; | |
18059 | else if (TYPE_P (expression)) | |
d17811fd MM |
18060 | return dependent_type_p (expression); |
18061 | return type_dependent_expression_p (expression); | |
100d337a | 18062 | |
c154b3d8 NP |
18063 | case AT_ENCODE_EXPR: |
18064 | /* An 'encode' expression is value-dependent if the operand is | |
18065 | type-dependent. */ | |
18066 | expression = TREE_OPERAND (expression, 0); | |
18067 | return dependent_type_p (expression); | |
18068 | ||
aa3bf45c JM |
18069 | case NOEXCEPT_EXPR: |
18070 | expression = TREE_OPERAND (expression, 0); | |
18071 | /* FIXME why check value-dependency? */ | |
18072 | return (type_dependent_expression_p (expression) | |
18073 | || value_dependent_expression_p (expression)); | |
18074 | ||
7416ab02 | 18075 | case SCOPE_REF: |
2d660b7f JM |
18076 | { |
18077 | tree name = TREE_OPERAND (expression, 1); | |
18078 | return value_dependent_expression_p (name); | |
18079 | } | |
100d337a | 18080 | |
7416ab02 NS |
18081 | case COMPONENT_REF: |
18082 | return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) | |
18083 | || value_dependent_expression_p (TREE_OPERAND (expression, 1))); | |
18084 | ||
5d80a306 DG |
18085 | case NONTYPE_ARGUMENT_PACK: |
18086 | /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument | |
18087 | is value-dependent. */ | |
18088 | { | |
18089 | tree values = ARGUMENT_PACK_ARGS (expression); | |
18090 | int i, len = TREE_VEC_LENGTH (values); | |
18091 | ||
18092 | for (i = 0; i < len; ++i) | |
18093 | if (value_dependent_expression_p (TREE_VEC_ELT (values, i))) | |
18094 | return true; | |
18095 | ||
18096 | return false; | |
18097 | } | |
18098 | ||
cb68ec50 PC |
18099 | case TRAIT_EXPR: |
18100 | { | |
18101 | tree type2 = TRAIT_EXPR_TYPE2 (expression); | |
18102 | return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)) | |
18103 | || (type2 ? dependent_type_p (type2) : false)); | |
18104 | } | |
18105 | ||
f01ec37d SM |
18106 | case MODOP_EXPR: |
18107 | return ((value_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
18108 | || (value_dependent_expression_p (TREE_OPERAND (expression, 2)))); | |
18109 | ||
d3a79fcc JM |
18110 | case ADDR_EXPR: |
18111 | { | |
18112 | tree op = TREE_OPERAND (expression, 0); | |
18113 | return (value_dependent_expression_p (op) | |
18114 | || has_value_dependent_address (op)); | |
18115 | } | |
18116 | ||
fa2200cb JM |
18117 | case CALL_EXPR: |
18118 | { | |
18119 | tree fn = get_callee_fndecl (expression); | |
18120 | int i, nargs; | |
18121 | if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression))) | |
18122 | return true; | |
18123 | nargs = call_expr_nargs (expression); | |
18124 | for (i = 0; i < nargs; ++i) | |
18125 | { | |
18126 | tree op = CALL_EXPR_ARG (expression, i); | |
18127 | /* In a call to a constexpr member function, look through the | |
18128 | implicit ADDR_EXPR on the object argument so that it doesn't | |
18129 | cause the call to be considered value-dependent. We also | |
18130 | look through it in potential_constant_expression. */ | |
18131 | if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn) | |
18132 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) | |
18133 | && TREE_CODE (op) == ADDR_EXPR) | |
18134 | op = TREE_OPERAND (op, 0); | |
18135 | if (value_dependent_expression_p (op)) | |
18136 | return true; | |
18137 | } | |
18138 | return false; | |
18139 | } | |
18140 | ||
7416ab02 NS |
18141 | default: |
18142 | /* A constant expression is value-dependent if any subexpression is | |
0cbd7506 | 18143 | value-dependent. */ |
1fb3244a MM |
18144 | switch (TREE_CODE_CLASS (TREE_CODE (expression))) |
18145 | { | |
7416ab02 | 18146 | case tcc_reference: |
6615c446 | 18147 | case tcc_unary: |
c8094d83 | 18148 | return (value_dependent_expression_p |
1fb3244a | 18149 | (TREE_OPERAND (expression, 0))); |
c8094d83 | 18150 | |
6615c446 JO |
18151 | case tcc_comparison: |
18152 | case tcc_binary: | |
c8094d83 | 18153 | return ((value_dependent_expression_p |
1fb3244a | 18154 | (TREE_OPERAND (expression, 0))) |
c8094d83 | 18155 | || (value_dependent_expression_p |
1fb3244a | 18156 | (TREE_OPERAND (expression, 1)))); |
c8094d83 | 18157 | |
6615c446 | 18158 | case tcc_expression: |
5039610b | 18159 | case tcc_vl_exp: |
1fb3244a MM |
18160 | { |
18161 | int i; | |
5039610b | 18162 | for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i) |
14d22dd6 MM |
18163 | /* In some cases, some of the operands may be missing. |
18164 | (For example, in the case of PREDECREMENT_EXPR, the | |
18165 | amount to increment by may be missing.) That doesn't | |
18166 | make the expression dependent. */ | |
18167 | if (TREE_OPERAND (expression, i) | |
18168 | && (value_dependent_expression_p | |
18169 | (TREE_OPERAND (expression, i)))) | |
1fb3244a MM |
18170 | return true; |
18171 | return false; | |
18172 | } | |
c8094d83 | 18173 | |
6615c446 | 18174 | default: |
7416ab02 | 18175 | break; |
1fb3244a MM |
18176 | } |
18177 | } | |
c8094d83 | 18178 | |
1fb3244a MM |
18179 | /* The expression is not value-dependent. */ |
18180 | return false; | |
18181 | } | |
18182 | ||
18183 | /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of | |
18184 | [temp.dep.expr]. */ | |
18185 | ||
18186 | bool | |
3a978d72 | 18187 | type_dependent_expression_p (tree expression) |
1fb3244a MM |
18188 | { |
18189 | if (!processing_template_decl) | |
18190 | return false; | |
18191 | ||
7efa3e22 NS |
18192 | if (expression == error_mark_node) |
18193 | return false; | |
10b1d5e7 MM |
18194 | |
18195 | /* An unresolved name is always dependent. */ | |
b01e6d2b JM |
18196 | if (TREE_CODE (expression) == IDENTIFIER_NODE |
18197 | || TREE_CODE (expression) == USING_DECL) | |
10b1d5e7 | 18198 | return true; |
c8094d83 | 18199 | |
1fb3244a MM |
18200 | /* Some expression forms are never type-dependent. */ |
18201 | if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR | |
18202 | || TREE_CODE (expression) == SIZEOF_EXPR | |
18203 | || TREE_CODE (expression) == ALIGNOF_EXPR | |
c154b3d8 | 18204 | || TREE_CODE (expression) == AT_ENCODE_EXPR |
0a766368 | 18205 | || TREE_CODE (expression) == NOEXCEPT_EXPR |
cb68ec50 | 18206 | || TREE_CODE (expression) == TRAIT_EXPR |
1fb3244a MM |
18207 | || TREE_CODE (expression) == TYPEID_EXPR |
18208 | || TREE_CODE (expression) == DELETE_EXPR | |
18209 | || TREE_CODE (expression) == VEC_DELETE_EXPR | |
18210 | || TREE_CODE (expression) == THROW_EXPR) | |
18211 | return false; | |
18212 | ||
18213 | /* The types of these expressions depends only on the type to which | |
18214 | the cast occurs. */ | |
18215 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR | |
18216 | || TREE_CODE (expression) == STATIC_CAST_EXPR | |
18217 | || TREE_CODE (expression) == CONST_CAST_EXPR | |
18218 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR | |
18219 | || TREE_CODE (expression) == CAST_EXPR) | |
18220 | return dependent_type_p (TREE_TYPE (expression)); | |
d17811fd | 18221 | |
1fb3244a MM |
18222 | /* The types of these expressions depends only on the type created |
18223 | by the expression. */ | |
d17811fd MM |
18224 | if (TREE_CODE (expression) == NEW_EXPR |
18225 | || TREE_CODE (expression) == VEC_NEW_EXPR) | |
09d2f85f KL |
18226 | { |
18227 | /* For NEW_EXPR tree nodes created inside a template, either | |
18228 | the object type itself or a TREE_LIST may appear as the | |
18229 | operand 1. */ | |
18230 | tree type = TREE_OPERAND (expression, 1); | |
18231 | if (TREE_CODE (type) == TREE_LIST) | |
18232 | /* This is an array type. We need to check array dimensions | |
18233 | as well. */ | |
18234 | return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) | |
18235 | || value_dependent_expression_p | |
18236 | (TREE_OPERAND (TREE_VALUE (type), 1)); | |
18237 | else | |
18238 | return dependent_type_p (type); | |
18239 | } | |
1fb3244a | 18240 | |
2d660b7f JM |
18241 | if (TREE_CODE (expression) == SCOPE_REF) |
18242 | { | |
18243 | tree scope = TREE_OPERAND (expression, 0); | |
18244 | tree name = TREE_OPERAND (expression, 1); | |
18245 | ||
18246 | /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it | |
18247 | contains an identifier associated by name lookup with one or more | |
18248 | declarations declared with a dependent type, or...a | |
18249 | nested-name-specifier or qualified-id that names a member of an | |
18250 | unknown specialization. */ | |
18251 | return (type_dependent_expression_p (name) | |
18252 | || dependent_scope_p (scope)); | |
18253 | } | |
5a57f1b2 JM |
18254 | |
18255 | if (TREE_CODE (expression) == FUNCTION_DECL | |
18256 | && DECL_LANG_SPECIFIC (expression) | |
18257 | && DECL_TEMPLATE_INFO (expression) | |
18258 | && (any_dependent_template_arguments_p | |
18259 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) | |
18260 | return true; | |
18261 | ||
18262 | if (TREE_CODE (expression) == TEMPLATE_DECL | |
18263 | && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression)) | |
18264 | return false; | |
18265 | ||
c2acde1e JM |
18266 | if (TREE_CODE (expression) == STMT_EXPR) |
18267 | expression = stmt_expr_value_expr (expression); | |
18268 | ||
86a09a9e JM |
18269 | if (BRACE_ENCLOSED_INITIALIZER_P (expression)) |
18270 | { | |
18271 | tree elt; | |
18272 | unsigned i; | |
18273 | ||
18274 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt) | |
18275 | { | |
18276 | if (type_dependent_expression_p (elt)) | |
18277 | return true; | |
18278 | } | |
18279 | return false; | |
18280 | } | |
18281 | ||
48326487 JM |
18282 | /* A static data member of the current instantiation with incomplete |
18283 | array type is type-dependent, as the definition and specializations | |
18284 | can have different bounds. */ | |
18285 | if (TREE_CODE (expression) == VAR_DECL | |
18286 | && DECL_CLASS_SCOPE_P (expression) | |
18287 | && dependent_type_p (DECL_CONTEXT (expression)) | |
18288 | && VAR_HAD_UNKNOWN_BOUND (expression)) | |
18289 | return true; | |
18290 | ||
d17811fd MM |
18291 | if (TREE_TYPE (expression) == unknown_type_node) |
18292 | { | |
18293 | if (TREE_CODE (expression) == ADDR_EXPR) | |
18294 | return type_dependent_expression_p (TREE_OPERAND (expression, 0)); | |
c353b8e3 MM |
18295 | if (TREE_CODE (expression) == COMPONENT_REF |
18296 | || TREE_CODE (expression) == OFFSET_REF) | |
6cb89308 NS |
18297 | { |
18298 | if (type_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
18299 | return true; | |
18300 | expression = TREE_OPERAND (expression, 1); | |
18301 | if (TREE_CODE (expression) == IDENTIFIER_NODE) | |
18302 | return false; | |
18303 | } | |
3601f003 KL |
18304 | /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */ |
18305 | if (TREE_CODE (expression) == SCOPE_REF) | |
18306 | return false; | |
c8094d83 | 18307 | |
d17811fd MM |
18308 | if (TREE_CODE (expression) == BASELINK) |
18309 | expression = BASELINK_FUNCTIONS (expression); | |
c8094d83 | 18310 | |
d17811fd MM |
18311 | if (TREE_CODE (expression) == TEMPLATE_ID_EXPR) |
18312 | { | |
ee3071ef NS |
18313 | if (any_dependent_template_arguments_p |
18314 | (TREE_OPERAND (expression, 1))) | |
d17811fd MM |
18315 | return true; |
18316 | expression = TREE_OPERAND (expression, 0); | |
18317 | } | |
b207d6e2 MM |
18318 | gcc_assert (TREE_CODE (expression) == OVERLOAD |
18319 | || TREE_CODE (expression) == FUNCTION_DECL); | |
c8094d83 | 18320 | |
315fb5db | 18321 | while (expression) |
d17811fd | 18322 | { |
315fb5db NS |
18323 | if (type_dependent_expression_p (OVL_CURRENT (expression))) |
18324 | return true; | |
18325 | expression = OVL_NEXT (expression); | |
d17811fd | 18326 | } |
315fb5db | 18327 | return false; |
d17811fd | 18328 | } |
c8094d83 | 18329 | |
3ce5fa4f | 18330 | gcc_assert (TREE_CODE (expression) != TYPE_DECL); |
3db45ab5 | 18331 | |
1fb3244a MM |
18332 | return (dependent_type_p (TREE_TYPE (expression))); |
18333 | } | |
5094a795 JM |
18334 | |
18335 | /* Like type_dependent_expression_p, but it also works while not processing | |
18336 | a template definition, i.e. during substitution or mangling. */ | |
18337 | ||
18338 | bool | |
18339 | type_dependent_expression_p_push (tree expr) | |
18340 | { | |
18341 | bool b; | |
18342 | ++processing_template_decl; | |
18343 | b = type_dependent_expression_p (expr); | |
18344 | --processing_template_decl; | |
18345 | return b; | |
18346 | } | |
1fb3244a | 18347 | |
c166b898 | 18348 | /* Returns TRUE if ARGS contains a type-dependent expression. */ |
1fb3244a MM |
18349 | |
18350 | bool | |
c166b898 | 18351 | any_type_dependent_arguments_p (const VEC(tree,gc) *args) |
d17811fd | 18352 | { |
c166b898 ILT |
18353 | unsigned int i; |
18354 | tree arg; | |
7efa3e22 | 18355 | |
ac47786e | 18356 | FOR_EACH_VEC_ELT (tree, args, i, arg) |
c166b898 | 18357 | { |
7efa3e22 | 18358 | if (type_dependent_expression_p (arg)) |
d17811fd | 18359 | return true; |
d17811fd MM |
18360 | } |
18361 | return false; | |
18362 | } | |
18363 | ||
32a11c08 MM |
18364 | /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are |
18365 | expressions) contains any value-dependent expressions. */ | |
18366 | ||
18367 | bool | |
58f9752a | 18368 | any_value_dependent_elements_p (const_tree list) |
32a11c08 MM |
18369 | { |
18370 | for (; list; list = TREE_CHAIN (list)) | |
18371 | if (value_dependent_expression_p (TREE_VALUE (list))) | |
18372 | return true; | |
18373 | ||
18374 | return false; | |
18375 | } | |
18376 | ||
d17811fd MM |
18377 | /* Returns TRUE if the ARG (a template argument) is dependent. */ |
18378 | ||
5d80a306 | 18379 | bool |
1fb3244a MM |
18380 | dependent_template_arg_p (tree arg) |
18381 | { | |
18382 | if (!processing_template_decl) | |
18383 | return false; | |
18384 | ||
6f1abb06 DS |
18385 | /* Assume a template argument that was wrongly written by the user |
18386 | is dependent. This is consistent with what | |
18387 | any_dependent_template_arguments_p [that calls this function] | |
18388 | does. */ | |
18389 | if (arg == error_mark_node) | |
18390 | return true; | |
18391 | ||
1fb3244a MM |
18392 | if (TREE_CODE (arg) == TEMPLATE_DECL |
18393 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
18394 | return dependent_template_p (arg); | |
5d80a306 DG |
18395 | else if (ARGUMENT_PACK_P (arg)) |
18396 | { | |
18397 | tree args = ARGUMENT_PACK_ARGS (arg); | |
18398 | int i, len = TREE_VEC_LENGTH (args); | |
18399 | for (i = 0; i < len; ++i) | |
18400 | { | |
18401 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
18402 | return true; | |
18403 | } | |
18404 | ||
18405 | return false; | |
18406 | } | |
1fb3244a MM |
18407 | else if (TYPE_P (arg)) |
18408 | return dependent_type_p (arg); | |
18409 | else | |
18410 | return (type_dependent_expression_p (arg) | |
18411 | || value_dependent_expression_p (arg)); | |
18412 | } | |
18413 | ||
06d40de8 DG |
18414 | /* Returns true if ARGS (a collection of template arguments) contains |
18415 | any types that require structural equality testing. */ | |
18416 | ||
18417 | bool | |
18418 | any_template_arguments_need_structural_equality_p (tree args) | |
18419 | { | |
18420 | int i; | |
18421 | int j; | |
18422 | ||
18423 | if (!args) | |
18424 | return false; | |
18425 | if (args == error_mark_node) | |
18426 | return true; | |
18427 | ||
18428 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) | |
18429 | { | |
18430 | tree level = TMPL_ARGS_LEVEL (args, i + 1); | |
18431 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) | |
18432 | { | |
18433 | tree arg = TREE_VEC_ELT (level, j); | |
5d80a306 DG |
18434 | tree packed_args = NULL_TREE; |
18435 | int k, len = 1; | |
18436 | ||
18437 | if (ARGUMENT_PACK_P (arg)) | |
18438 | { | |
18439 | /* Look inside the argument pack. */ | |
18440 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
18441 | len = TREE_VEC_LENGTH (packed_args); | |
18442 | } | |
18443 | ||
18444 | for (k = 0; k < len; ++k) | |
18445 | { | |
18446 | if (packed_args) | |
18447 | arg = TREE_VEC_ELT (packed_args, k); | |
18448 | ||
18449 | if (error_operand_p (arg)) | |
18450 | return true; | |
18451 | else if (TREE_CODE (arg) == TEMPLATE_DECL | |
18452 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
18453 | continue; | |
18454 | else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg)) | |
18455 | return true; | |
18456 | else if (!TYPE_P (arg) && TREE_TYPE (arg) | |
18457 | && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg))) | |
18458 | return true; | |
18459 | } | |
06d40de8 DG |
18460 | } |
18461 | } | |
18462 | ||
18463 | return false; | |
18464 | } | |
18465 | ||
d17811fd MM |
18466 | /* Returns true if ARGS (a collection of template arguments) contains |
18467 | any dependent arguments. */ | |
1fb3244a | 18468 | |
d17811fd | 18469 | bool |
58f9752a | 18470 | any_dependent_template_arguments_p (const_tree args) |
1fb3244a | 18471 | { |
bf12d54d | 18472 | int i; |
c353b8e3 MM |
18473 | int j; |
18474 | ||
d17811fd MM |
18475 | if (!args) |
18476 | return false; | |
7e497d0c VR |
18477 | if (args == error_mark_node) |
18478 | return true; | |
d17811fd | 18479 | |
c353b8e3 MM |
18480 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
18481 | { | |
58f9752a | 18482 | const_tree level = TMPL_ARGS_LEVEL (args, i + 1); |
c353b8e3 MM |
18483 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) |
18484 | if (dependent_template_arg_p (TREE_VEC_ELT (level, j))) | |
18485 | return true; | |
18486 | } | |
1fb3244a | 18487 | |
1fb3244a MM |
18488 | return false; |
18489 | } | |
18490 | ||
18491 | /* Returns TRUE if the template TMPL is dependent. */ | |
18492 | ||
18493 | bool | |
18494 | dependent_template_p (tree tmpl) | |
18495 | { | |
b95cc51a MM |
18496 | if (TREE_CODE (tmpl) == OVERLOAD) |
18497 | { | |
18498 | while (tmpl) | |
18499 | { | |
18500 | if (dependent_template_p (OVL_FUNCTION (tmpl))) | |
18501 | return true; | |
18502 | tmpl = OVL_CHAIN (tmpl); | |
18503 | } | |
18504 | return false; | |
18505 | } | |
18506 | ||
1fb3244a MM |
18507 | /* Template template parameters are dependent. */ |
18508 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) | |
18509 | || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) | |
18510 | return true; | |
27ab0504 | 18511 | /* So are names that have not been looked up. */ |
acccf788 MM |
18512 | if (TREE_CODE (tmpl) == SCOPE_REF |
18513 | || TREE_CODE (tmpl) == IDENTIFIER_NODE) | |
d17811fd | 18514 | return true; |
1fb3244a MM |
18515 | /* So are member templates of dependent classes. */ |
18516 | if (TYPE_P (CP_DECL_CONTEXT (tmpl))) | |
18517 | return dependent_type_p (DECL_CONTEXT (tmpl)); | |
18518 | return false; | |
18519 | } | |
18520 | ||
d17811fd MM |
18521 | /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ |
18522 | ||
18523 | bool | |
18524 | dependent_template_id_p (tree tmpl, tree args) | |
18525 | { | |
18526 | return (dependent_template_p (tmpl) | |
18527 | || any_dependent_template_arguments_p (args)); | |
18528 | } | |
18529 | ||
a68ab351 JJ |
18530 | /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors |
18531 | is dependent. */ | |
18532 | ||
18533 | bool | |
18534 | dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv) | |
18535 | { | |
18536 | int i; | |
18537 | ||
18538 | if (!processing_template_decl) | |
18539 | return false; | |
18540 | ||
18541 | for (i = 0; i < TREE_VEC_LENGTH (declv); i++) | |
18542 | { | |
18543 | tree decl = TREE_VEC_ELT (declv, i); | |
18544 | tree init = TREE_VEC_ELT (initv, i); | |
18545 | tree cond = TREE_VEC_ELT (condv, i); | |
18546 | tree incr = TREE_VEC_ELT (incrv, i); | |
18547 | ||
18548 | if (type_dependent_expression_p (decl)) | |
18549 | return true; | |
18550 | ||
18551 | if (init && type_dependent_expression_p (init)) | |
18552 | return true; | |
18553 | ||
18554 | if (type_dependent_expression_p (cond)) | |
18555 | return true; | |
18556 | ||
18557 | if (COMPARISON_CLASS_P (cond) | |
18558 | && (type_dependent_expression_p (TREE_OPERAND (cond, 0)) | |
18559 | || type_dependent_expression_p (TREE_OPERAND (cond, 1)))) | |
18560 | return true; | |
18561 | ||
18562 | if (TREE_CODE (incr) == MODOP_EXPR) | |
18563 | { | |
18564 | if (type_dependent_expression_p (TREE_OPERAND (incr, 0)) | |
18565 | || type_dependent_expression_p (TREE_OPERAND (incr, 2))) | |
18566 | return true; | |
18567 | } | |
18568 | else if (type_dependent_expression_p (incr)) | |
18569 | return true; | |
18570 | else if (TREE_CODE (incr) == MODIFY_EXPR) | |
18571 | { | |
18572 | if (type_dependent_expression_p (TREE_OPERAND (incr, 0))) | |
18573 | return true; | |
18574 | else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1))) | |
18575 | { | |
18576 | tree t = TREE_OPERAND (incr, 1); | |
18577 | if (type_dependent_expression_p (TREE_OPERAND (t, 0)) | |
18578 | || type_dependent_expression_p (TREE_OPERAND (t, 1))) | |
18579 | return true; | |
18580 | } | |
18581 | } | |
18582 | } | |
18583 | ||
18584 | return false; | |
18585 | } | |
18586 | ||
14d22dd6 | 18587 | /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the |
4195a767 NS |
18588 | TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if |
18589 | no such TYPE can be found. Note that this function peers inside | |
18590 | uninstantiated templates and therefore should be used only in | |
18591 | extremely limited situations. ONLY_CURRENT_P restricts this | |
18592 | peering to the currently open classes hierarchy (which is required | |
18593 | when comparing types). */ | |
14d22dd6 MM |
18594 | |
18595 | tree | |
18596 | resolve_typename_type (tree type, bool only_current_p) | |
18597 | { | |
18598 | tree scope; | |
18599 | tree name; | |
18600 | tree decl; | |
18601 | int quals; | |
4514aa8c | 18602 | tree pushed_scope; |
4195a767 | 18603 | tree result; |
14d22dd6 | 18604 | |
50bc768d | 18605 | gcc_assert (TREE_CODE (type) == TYPENAME_TYPE); |
14d22dd6 MM |
18606 | |
18607 | scope = TYPE_CONTEXT (type); | |
d0940d56 DS |
18608 | /* Usually the non-qualified identifier of a TYPENAME_TYPE is |
18609 | TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of | |
18610 | a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing | |
18611 | the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified | |
18612 | identifier of the TYPENAME_TYPE anymore. | |
18613 | So by getting the TYPE_IDENTIFIER of the _main declaration_ of the | |
18614 | TYPENAME_TYPE instead, we avoid messing up with a possible | |
18615 | typedef variant case. */ | |
18616 | name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type)); | |
14d22dd6 MM |
18617 | |
18618 | /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve | |
18619 | it first before we can figure out what NAME refers to. */ | |
18620 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
18621 | scope = resolve_typename_type (scope, only_current_p); | |
18622 | /* If we don't know what SCOPE refers to, then we cannot resolve the | |
18623 | TYPENAME_TYPE. */ | |
4195a767 NS |
18624 | if (TREE_CODE (scope) == TYPENAME_TYPE) |
18625 | return type; | |
14d22dd6 MM |
18626 | /* If the SCOPE is a template type parameter, we have no way of |
18627 | resolving the name. */ | |
18628 | if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM) | |
18629 | return type; | |
18630 | /* If the SCOPE is not the current instantiation, there's no reason | |
18631 | to look inside it. */ | |
18632 | if (only_current_p && !currently_open_class (scope)) | |
4195a767 | 18633 | return type; |
fc1e0846 JM |
18634 | /* If this is a typedef, we don't want to look inside (c++/11987). */ |
18635 | if (typedef_variant_p (type)) | |
18636 | return type; | |
7a20d689 JM |
18637 | /* If SCOPE isn't the template itself, it will not have a valid |
18638 | TYPE_FIELDS list. */ | |
18639 | if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope))) | |
18640 | /* scope is either the template itself or a compatible instantiation | |
18641 | like X<T>, so look up the name in the original template. */ | |
18642 | scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope); | |
18643 | else | |
18644 | /* scope is a partial instantiation, so we can't do the lookup or we | |
18645 | will lose the template arguments. */ | |
18646 | return type; | |
14d22dd6 MM |
18647 | /* Enter the SCOPE so that name lookup will be resolved as if we |
18648 | were in the class definition. In particular, SCOPE will no | |
18649 | longer be considered a dependent type. */ | |
4514aa8c | 18650 | pushed_scope = push_scope (scope); |
14d22dd6 | 18651 | /* Look up the declaration. */ |
86ac0575 | 18652 | decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true); |
4195a767 NS |
18653 | |
18654 | result = NULL_TREE; | |
18655 | ||
14d22dd6 MM |
18656 | /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to |
18657 | find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ | |
18658 | if (!decl) | |
4195a767 | 18659 | /*nop*/; |
14d22dd6 MM |
18660 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE |
18661 | && TREE_CODE (decl) == TYPE_DECL) | |
4195a767 NS |
18662 | { |
18663 | result = TREE_TYPE (decl); | |
18664 | if (result == error_mark_node) | |
18665 | result = NULL_TREE; | |
18666 | } | |
14d22dd6 MM |
18667 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR |
18668 | && DECL_CLASS_TEMPLATE_P (decl)) | |
18669 | { | |
18670 | tree tmpl; | |
18671 | tree args; | |
18672 | /* Obtain the template and the arguments. */ | |
18673 | tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0); | |
18674 | args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1); | |
18675 | /* Instantiate the template. */ | |
4195a767 NS |
18676 | result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, |
18677 | /*entering_scope=*/0, | |
18678 | tf_error | tf_user); | |
18679 | if (result == error_mark_node) | |
18680 | result = NULL_TREE; | |
14d22dd6 | 18681 | } |
4195a767 | 18682 | |
14d22dd6 | 18683 | /* Leave the SCOPE. */ |
4514aa8c NS |
18684 | if (pushed_scope) |
18685 | pop_scope (pushed_scope); | |
14d22dd6 | 18686 | |
4195a767 NS |
18687 | /* If we failed to resolve it, return the original typename. */ |
18688 | if (!result) | |
18689 | return type; | |
18690 | ||
18691 | /* If lookup found a typename type, resolve that too. */ | |
18692 | if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result)) | |
18693 | { | |
18694 | /* Ill-formed programs can cause infinite recursion here, so we | |
18695 | must catch that. */ | |
18696 | TYPENAME_IS_RESOLVING_P (type) = 1; | |
18697 | result = resolve_typename_type (result, only_current_p); | |
18698 | TYPENAME_IS_RESOLVING_P (type) = 0; | |
18699 | } | |
18700 | ||
18701 | /* Qualify the resulting type. */ | |
18702 | quals = cp_type_quals (type); | |
18703 | if (quals) | |
18704 | result = cp_build_qualified_type (result, cp_type_quals (result) | quals); | |
18705 | ||
18706 | return result; | |
14d22dd6 MM |
18707 | } |
18708 | ||
d17811fd MM |
18709 | /* EXPR is an expression which is not type-dependent. Return a proxy |
18710 | for EXPR that can be used to compute the types of larger | |
18711 | expressions containing EXPR. */ | |
18712 | ||
18713 | tree | |
18714 | build_non_dependent_expr (tree expr) | |
18715 | { | |
0deb916c MM |
18716 | tree inner_expr; |
18717 | ||
c8094d83 | 18718 | /* Preserve null pointer constants so that the type of things like |
d17811fd MM |
18719 | "p == 0" where "p" is a pointer can be determined. */ |
18720 | if (null_ptr_cst_p (expr)) | |
18721 | return expr; | |
18722 | /* Preserve OVERLOADs; the functions must be available to resolve | |
18723 | types. */ | |
c497db75 | 18724 | inner_expr = expr; |
c2acde1e JM |
18725 | if (TREE_CODE (inner_expr) == STMT_EXPR) |
18726 | inner_expr = stmt_expr_value_expr (inner_expr); | |
c497db75 AO |
18727 | if (TREE_CODE (inner_expr) == ADDR_EXPR) |
18728 | inner_expr = TREE_OPERAND (inner_expr, 0); | |
18729 | if (TREE_CODE (inner_expr) == COMPONENT_REF) | |
18730 | inner_expr = TREE_OPERAND (inner_expr, 1); | |
2226e997 | 18731 | if (is_overloaded_fn (inner_expr) |
6439fffd | 18732 | || TREE_CODE (inner_expr) == OFFSET_REF) |
d17811fd | 18733 | return expr; |
5ae9ba3e MM |
18734 | /* There is no need to return a proxy for a variable. */ |
18735 | if (TREE_CODE (expr) == VAR_DECL) | |
18736 | return expr; | |
7433e6d4 MM |
18737 | /* Preserve string constants; conversions from string constants to |
18738 | "char *" are allowed, even though normally a "const char *" | |
18739 | cannot be used to initialize a "char *". */ | |
18740 | if (TREE_CODE (expr) == STRING_CST) | |
18741 | return expr; | |
b7c707d1 MM |
18742 | /* Preserve arithmetic constants, as an optimization -- there is no |
18743 | reason to create a new node. */ | |
18744 | if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST) | |
18745 | return expr; | |
9b7be7b5 MM |
18746 | /* Preserve THROW_EXPRs -- all throw-expressions have type "void". |
18747 | There is at least one place where we want to know that a | |
18748 | particular expression is a throw-expression: when checking a ?: | |
18749 | expression, there are special rules if the second or third | |
878cbb73 | 18750 | argument is a throw-expression. */ |
9b7be7b5 MM |
18751 | if (TREE_CODE (expr) == THROW_EXPR) |
18752 | return expr; | |
47d4c811 NS |
18753 | |
18754 | if (TREE_CODE (expr) == COND_EXPR) | |
f293ce4b RS |
18755 | return build3 (COND_EXPR, |
18756 | TREE_TYPE (expr), | |
18757 | TREE_OPERAND (expr, 0), | |
c8094d83 | 18758 | (TREE_OPERAND (expr, 1) |
f293ce4b RS |
18759 | ? build_non_dependent_expr (TREE_OPERAND (expr, 1)) |
18760 | : build_non_dependent_expr (TREE_OPERAND (expr, 0))), | |
18761 | build_non_dependent_expr (TREE_OPERAND (expr, 2))); | |
8e1daa34 NS |
18762 | if (TREE_CODE (expr) == COMPOUND_EXPR |
18763 | && !COMPOUND_EXPR_OVERLOADED (expr)) | |
f293ce4b RS |
18764 | return build2 (COMPOUND_EXPR, |
18765 | TREE_TYPE (expr), | |
18766 | TREE_OPERAND (expr, 0), | |
18767 | build_non_dependent_expr (TREE_OPERAND (expr, 1))); | |
c8094d83 | 18768 | |
fdeff563 NS |
18769 | /* If the type is unknown, it can't really be non-dependent */ |
18770 | gcc_assert (TREE_TYPE (expr) != unknown_type_node); | |
3db45ab5 | 18771 | |
c8094d83 | 18772 | /* Otherwise, build a NON_DEPENDENT_EXPR. |
d17811fd MM |
18773 | |
18774 | REFERENCE_TYPEs are not stripped for expressions in templates | |
18775 | because doing so would play havoc with mangling. Consider, for | |
18776 | example: | |
18777 | ||
c8094d83 | 18778 | template <typename T> void f<T& g>() { g(); } |
d17811fd MM |
18779 | |
18780 | In the body of "f", the expression for "g" will have | |
18781 | REFERENCE_TYPE, even though the standard says that it should | |
18782 | not. The reason is that we must preserve the syntactic form of | |
18783 | the expression so that mangling (say) "f<g>" inside the body of | |
18784 | "f" works out correctly. Therefore, the REFERENCE_TYPE is | |
18785 | stripped here. */ | |
018a5803 | 18786 | return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr); |
d17811fd MM |
18787 | } |
18788 | ||
c166b898 ILT |
18789 | /* ARGS is a vector of expressions as arguments to a function call. |
18790 | Replace the arguments with equivalent non-dependent expressions. | |
18791 | This modifies ARGS in place. */ | |
d17811fd | 18792 | |
c166b898 ILT |
18793 | void |
18794 | make_args_non_dependent (VEC(tree,gc) *args) | |
d17811fd | 18795 | { |
c166b898 ILT |
18796 | unsigned int ix; |
18797 | tree arg; | |
d17811fd | 18798 | |
ac47786e | 18799 | FOR_EACH_VEC_ELT (tree, args, ix, arg) |
c166b898 ILT |
18800 | { |
18801 | tree newarg = build_non_dependent_expr (arg); | |
18802 | if (newarg != arg) | |
18803 | VEC_replace (tree, args, ix, newarg); | |
18804 | } | |
d17811fd MM |
18805 | } |
18806 | ||
86a09a9e JM |
18807 | /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM |
18808 | with a level one deeper than the actual template parms. */ | |
18809 | ||
18810 | tree | |
18811 | make_auto (void) | |
18812 | { | |
2e32c99e | 18813 | tree au = cxx_make_type (TEMPLATE_TYPE_PARM); |
c2255bc4 AH |
18814 | TYPE_NAME (au) = build_decl (BUILTINS_LOCATION, |
18815 | TYPE_DECL, get_identifier ("auto"), au); | |
86a09a9e JM |
18816 | TYPE_STUB_DECL (au) = TYPE_NAME (au); |
18817 | TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index | |
18818 | (0, processing_template_decl + 1, processing_template_decl + 1, | |
6f1abb06 | 18819 | 0, TYPE_NAME (au), NULL_TREE); |
86a09a9e JM |
18820 | TYPE_CANONICAL (au) = canonical_type_parameter (au); |
18821 | DECL_ARTIFICIAL (TYPE_NAME (au)) = 1; | |
18822 | SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au)); | |
18823 | ||
18824 | return au; | |
18825 | } | |
18826 | ||
5f5babf1 | 18827 | /* Given type ARG, return std::initializer_list<ARG>. */ |
86a09a9e JM |
18828 | |
18829 | static tree | |
5f5babf1 | 18830 | listify (tree arg) |
86a09a9e JM |
18831 | { |
18832 | tree std_init_list = namespace_binding | |
18833 | (get_identifier ("initializer_list"), std_node); | |
18834 | tree argvec; | |
86a09a9e JM |
18835 | if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list)) |
18836 | { | |
5f5babf1 | 18837 | error ("deducing from brace-enclosed initializer list requires " |
86a09a9e JM |
18838 | "#include <initializer_list>"); |
18839 | return error_mark_node; | |
18840 | } | |
18841 | argvec = make_tree_vec (1); | |
5f5babf1 JM |
18842 | TREE_VEC_ELT (argvec, 0) = arg; |
18843 | return lookup_template_class (std_init_list, argvec, NULL_TREE, | |
18844 | NULL_TREE, 0, tf_warning_or_error); | |
18845 | } | |
18846 | ||
18847 | /* Replace auto in TYPE with std::initializer_list<auto>. */ | |
86a09a9e | 18848 | |
5f5babf1 JM |
18849 | static tree |
18850 | listify_autos (tree type, tree auto_node) | |
18851 | { | |
18852 | tree init_auto = listify (auto_node); | |
18853 | tree argvec = make_tree_vec (1); | |
86a09a9e JM |
18854 | TREE_VEC_ELT (argvec, 0) = init_auto; |
18855 | if (processing_template_decl) | |
18856 | argvec = add_to_template_args (current_template_args (), argvec); | |
18857 | return tsubst (type, argvec, tf_warning_or_error, NULL_TREE); | |
18858 | } | |
18859 | ||
18932737 JM |
18860 | /* walk_tree helper for do_auto_deduction. */ |
18861 | ||
18862 | static tree | |
18863 | contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, | |
18864 | void *type) | |
18865 | { | |
18866 | /* Is this a variable with the type we're looking for? */ | |
18867 | if (DECL_P (*tp) | |
18868 | && TREE_TYPE (*tp) == type) | |
18869 | return *tp; | |
18870 | else | |
18871 | return NULL_TREE; | |
18872 | } | |
18873 | ||
86a09a9e JM |
18874 | /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced |
18875 | from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */ | |
18876 | ||
18877 | tree | |
18878 | do_auto_deduction (tree type, tree init, tree auto_node) | |
18879 | { | |
c166b898 ILT |
18880 | tree parms, tparms, targs; |
18881 | tree args[1]; | |
18932737 | 18882 | tree decl; |
86a09a9e JM |
18883 | int val; |
18884 | ||
18932737 JM |
18885 | /* The name of the object being declared shall not appear in the |
18886 | initializer expression. */ | |
18887 | decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type); | |
18888 | if (decl) | |
18889 | { | |
18890 | error ("variable %q#D with %<auto%> type used in its own " | |
18891 | "initializer", decl); | |
18892 | return error_mark_node; | |
18893 | } | |
18894 | ||
86a09a9e JM |
18895 | /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto |
18896 | with either a new invented type template parameter U or, if the | |
18897 | initializer is a braced-init-list (8.5.4), with | |
18898 | std::initializer_list<U>. */ | |
18899 | if (BRACE_ENCLOSED_INITIALIZER_P (init)) | |
18900 | type = listify_autos (type, auto_node); | |
18901 | ||
2e5748d2 JM |
18902 | init = resolve_nondeduced_context (init); |
18903 | ||
86a09a9e | 18904 | parms = build_tree_list (NULL_TREE, type); |
c166b898 | 18905 | args[0] = init; |
86a09a9e JM |
18906 | tparms = make_tree_vec (1); |
18907 | targs = make_tree_vec (1); | |
18908 | TREE_VEC_ELT (tparms, 0) | |
18909 | = build_tree_list (NULL_TREE, TYPE_NAME (auto_node)); | |
c166b898 | 18910 | val = type_unification_real (tparms, targs, parms, args, 1, 0, |
86a09a9e JM |
18911 | DEDUCE_CALL, LOOKUP_NORMAL); |
18912 | if (val > 0) | |
18913 | { | |
18914 | error ("unable to deduce %qT from %qE", type, init); | |
18915 | return error_mark_node; | |
18916 | } | |
18917 | ||
2e32c99e JM |
18918 | /* If the list of declarators contains more than one declarator, the type |
18919 | of each declared variable is determined as described above. If the | |
18920 | type deduced for the template parameter U is not the same in each | |
18921 | deduction, the program is ill-formed. */ | |
18922 | if (TREE_TYPE (auto_node) | |
18923 | && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0))) | |
18924 | { | |
18925 | error ("inconsistent deduction for %qT: %qT and then %qT", | |
18926 | auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)); | |
18927 | return error_mark_node; | |
18928 | } | |
18929 | TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0); | |
18930 | ||
86a09a9e JM |
18931 | if (processing_template_decl) |
18932 | targs = add_to_template_args (current_template_args (), targs); | |
18933 | return tsubst (type, targs, tf_warning_or_error, NULL_TREE); | |
18934 | } | |
18935 | ||
f57ca1ea JM |
18936 | /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the |
18937 | result. */ | |
18938 | ||
18939 | tree | |
18940 | splice_late_return_type (tree type, tree late_return_type) | |
18941 | { | |
18942 | tree argvec; | |
18943 | ||
18944 | if (late_return_type == NULL_TREE) | |
18945 | return type; | |
18946 | argvec = make_tree_vec (1); | |
18947 | TREE_VEC_ELT (argvec, 0) = late_return_type; | |
18948 | if (processing_template_decl) | |
18949 | argvec = add_to_template_args (current_template_args (), argvec); | |
18950 | return tsubst (type, argvec, tf_warning_or_error, NULL_TREE); | |
18951 | } | |
18952 | ||
86a09a9e JM |
18953 | /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */ |
18954 | ||
18955 | bool | |
18956 | is_auto (const_tree type) | |
18957 | { | |
18958 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM | |
18959 | && TYPE_IDENTIFIER (type) == get_identifier ("auto")) | |
18960 | return true; | |
18961 | else | |
18962 | return false; | |
18963 | } | |
18964 | ||
18965 | /* Returns true iff TYPE contains a use of 'auto'. Since auto can only | |
18966 | appear as a type-specifier for the declaration in question, we don't | |
18967 | have to look through the whole type. */ | |
18968 | ||
18969 | tree | |
18970 | type_uses_auto (tree type) | |
18971 | { | |
18972 | enum tree_code code; | |
18973 | if (is_auto (type)) | |
18974 | return type; | |
18975 | ||
18976 | code = TREE_CODE (type); | |
18977 | ||
18978 | if (code == POINTER_TYPE || code == REFERENCE_TYPE | |
18979 | || code == OFFSET_TYPE || code == FUNCTION_TYPE | |
18980 | || code == METHOD_TYPE || code == ARRAY_TYPE) | |
18981 | return type_uses_auto (TREE_TYPE (type)); | |
18982 | ||
18983 | if (TYPE_PTRMEMFUNC_P (type)) | |
18984 | return type_uses_auto (TREE_TYPE (TREE_TYPE | |
18985 | (TYPE_PTRMEMFUNC_FN_TYPE (type)))); | |
18986 | ||
18987 | return NULL_TREE; | |
18988 | } | |
18989 | ||
aa373032 | 18990 | /* For a given template T, return the vector of typedefs referenced |
2eb25c98 DS |
18991 | in T for which access check is needed at T instantiation time. |
18992 | T is either a FUNCTION_DECL or a RECORD_TYPE. | |
18993 | Those typedefs were added to T by the function | |
18994 | append_type_to_template_for_access_check. */ | |
18995 | ||
aa373032 | 18996 | VEC(qualified_typedef_usage_t,gc)* |
2eb25c98 DS |
18997 | get_types_needing_access_check (tree t) |
18998 | { | |
aa373032 DS |
18999 | tree ti; |
19000 | VEC(qualified_typedef_usage_t,gc) *result = NULL; | |
2eb25c98 DS |
19001 | |
19002 | if (!t || t == error_mark_node) | |
aa373032 | 19003 | return NULL; |
2eb25c98 DS |
19004 | |
19005 | if (!(ti = get_template_info (t))) | |
aa373032 | 19006 | return NULL; |
2eb25c98 | 19007 | |
9dbed529 | 19008 | if (CLASS_TYPE_P (t) |
c4efba1c | 19009 | || TREE_CODE (t) == FUNCTION_DECL) |
2eb25c98 DS |
19010 | { |
19011 | if (!TI_TEMPLATE (ti)) | |
aa373032 | 19012 | return NULL; |
2eb25c98 DS |
19013 | |
19014 | result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti); | |
19015 | } | |
19016 | ||
19017 | return result; | |
19018 | } | |
19019 | ||
19020 | /* Append the typedef TYPE_DECL used in template T to a list of typedefs | |
19021 | tied to T. That list of typedefs will be access checked at | |
19022 | T instantiation time. | |
19023 | T is either a FUNCTION_DECL or a RECORD_TYPE. | |
19024 | TYPE_DECL is a TYPE_DECL node representing a typedef. | |
19025 | SCOPE is the scope through which TYPE_DECL is accessed. | |
aa373032 | 19026 | LOCATION is the location of the usage point of TYPE_DECL. |
2eb25c98 DS |
19027 | |
19028 | This function is a subroutine of | |
19029 | append_type_to_template_for_access_check. */ | |
19030 | ||
19031 | static void | |
19032 | append_type_to_template_for_access_check_1 (tree t, | |
19033 | tree type_decl, | |
aa373032 DS |
19034 | tree scope, |
19035 | location_t location) | |
2eb25c98 | 19036 | { |
aa373032 | 19037 | qualified_typedef_usage_t typedef_usage; |
2eb25c98 DS |
19038 | tree ti; |
19039 | ||
19040 | if (!t || t == error_mark_node) | |
19041 | return; | |
19042 | ||
19043 | gcc_assert ((TREE_CODE (t) == FUNCTION_DECL | |
9dbed529 | 19044 | || CLASS_TYPE_P (t)) |
2eb25c98 DS |
19045 | && type_decl |
19046 | && TREE_CODE (type_decl) == TYPE_DECL | |
19047 | && scope); | |
19048 | ||
19049 | if (!(ti = get_template_info (t))) | |
19050 | return; | |
19051 | ||
19052 | gcc_assert (TI_TEMPLATE (ti)); | |
19053 | ||
aa373032 DS |
19054 | typedef_usage.typedef_decl = type_decl; |
19055 | typedef_usage.context = scope; | |
19056 | typedef_usage.locus = location; | |
19057 | ||
19058 | VEC_safe_push (qualified_typedef_usage_t, gc, | |
19059 | TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), | |
19060 | &typedef_usage); | |
2eb25c98 DS |
19061 | } |
19062 | ||
d0940d56 | 19063 | /* Append TYPE_DECL to the template TEMPL. |
2eb25c98 | 19064 | TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL. |
d0940d56 | 19065 | At TEMPL instanciation time, TYPE_DECL will be checked to see |
2eb25c98 | 19066 | if it can be accessed through SCOPE. |
aa373032 | 19067 | LOCATION is the location of the usage point of TYPE_DECL. |
2eb25c98 DS |
19068 | |
19069 | e.g. consider the following code snippet: | |
19070 | ||
19071 | class C | |
19072 | { | |
19073 | typedef int myint; | |
19074 | }; | |
19075 | ||
19076 | template<class U> struct S | |
19077 | { | |
aa373032 | 19078 | C::myint mi; // <-- usage point of the typedef C::myint |
2eb25c98 DS |
19079 | }; |
19080 | ||
19081 | S<char> s; | |
19082 | ||
19083 | At S<char> instantiation time, we need to check the access of C::myint | |
19084 | In other words, we need to check the access of the myint typedef through | |
19085 | the C scope. For that purpose, this function will add the myint typedef | |
19086 | and the scope C through which its being accessed to a list of typedefs | |
19087 | tied to the template S. That list will be walked at template instantiation | |
19088 | time and access check performed on each typedefs it contains. | |
19089 | Note that this particular code snippet should yield an error because | |
19090 | myint is private to C. */ | |
d0940d56 DS |
19091 | |
19092 | void | |
19093 | append_type_to_template_for_access_check (tree templ, | |
19094 | tree type_decl, | |
aa373032 DS |
19095 | tree scope, |
19096 | location_t location) | |
d0940d56 | 19097 | { |
aa373032 DS |
19098 | qualified_typedef_usage_t *iter; |
19099 | int i; | |
d0940d56 | 19100 | |
2eb25c98 | 19101 | gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL)); |
d0940d56 | 19102 | |
2eb25c98 | 19103 | /* Make sure we don't append the type to the template twice. */ |
ac47786e | 19104 | FOR_EACH_VEC_ELT (qualified_typedef_usage_t, |
aa373032 | 19105 | get_types_needing_access_check (templ), |
ac47786e | 19106 | i, iter) |
aa373032 DS |
19107 | if (iter->typedef_decl == type_decl && scope == iter->context) |
19108 | return; | |
d0940d56 | 19109 | |
aa373032 DS |
19110 | append_type_to_template_for_access_check_1 (templ, type_decl, |
19111 | scope, location); | |
d0940d56 DS |
19112 | } |
19113 | ||
2b110bfc JM |
19114 | /* Set up the hash tables for template instantiations. */ |
19115 | ||
19116 | void | |
19117 | init_template_processing (void) | |
19118 | { | |
19119 | decl_specializations = htab_create_ggc (37, | |
19120 | hash_specialization, | |
19121 | eq_specializations, | |
19122 | ggc_free); | |
19123 | type_specializations = htab_create_ggc (37, | |
19124 | hash_specialization, | |
19125 | eq_specializations, | |
19126 | ggc_free); | |
19127 | } | |
19128 | ||
7dcfe861 JM |
19129 | /* Print stats about the template hash tables for -fstats. */ |
19130 | ||
19131 | void | |
19132 | print_template_statistics (void) | |
19133 | { | |
19134 | fprintf (stderr, "decl_specializations: size %ld, %ld elements, " | |
19135 | "%f collisions\n", (long) htab_size (decl_specializations), | |
19136 | (long) htab_elements (decl_specializations), | |
19137 | htab_collisions (decl_specializations)); | |
19138 | fprintf (stderr, "type_specializations: size %ld, %ld elements, " | |
19139 | "%f collisions\n", (long) htab_size (type_specializations), | |
19140 | (long) htab_elements (type_specializations), | |
19141 | htab_collisions (type_specializations)); | |
19142 | } | |
19143 | ||
e2500fed | 19144 | #include "gt-cp-pt.h" |