]>
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, |
493e377c PC |
3 | 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009 |
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 "obstack.h" |
8d08fdba | 34 | #include "tree.h" |
0c58f841 | 35 | #include "pointer-set.h" |
8d08fdba | 36 | #include "flags.h" |
e58a9aa1 | 37 | #include "c-common.h" |
8d08fdba | 38 | #include "cp-tree.h" |
e58a9aa1 | 39 | #include "cp-objcp-common.h" |
25af8512 | 40 | #include "tree-inline.h" |
8d08fdba | 41 | #include "decl.h" |
e8abc66f | 42 | #include "output.h" |
49c249e1 | 43 | #include "except.h" |
54f92bfb | 44 | #include "toplev.h" |
3dcaad8b | 45 | #include "rtl.h" |
297a5329 | 46 | #include "timevar.h" |
325c3691 | 47 | #include "tree-iterator.h" |
53824026 | 48 | #include "vecprim.h" |
49c249e1 | 49 | |
050367a3 MM |
50 | /* The type of functions taking a tree, and some additional data, and |
51 | returning an int. */ | |
3a978d72 | 52 | typedef int (*tree_fn_t) (tree, void*); |
050367a3 | 53 | |
0aafb128 MM |
54 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose |
55 | instantiations have been deferred, either because their definitions | |
e2c3721c | 56 | were not yet available, or because we were putting off doing the work. */ |
d1b38208 | 57 | struct GTY (()) pending_template { |
e2c3721c PB |
58 | struct pending_template *next; |
59 | struct tinst_level *tinst; | |
60 | }; | |
61 | ||
62 | static GTY(()) struct pending_template *pending_templates; | |
63 | static GTY(()) struct pending_template *last_pending_template; | |
73aad9b9 | 64 | |
67ffc812 | 65 | int processing_template_parmlist; |
386b8a85 JM |
66 | static int template_header_count; |
67 | ||
e2500fed | 68 | static GTY(()) tree saved_trees; |
53824026 | 69 | static VEC(int,heap) *inline_parm_levels; |
75650646 | 70 | |
e2c3721c | 71 | static GTY(()) struct tinst_level *current_tinst_level; |
3ae18eaf | 72 | |
2b59fc25 KL |
73 | static GTY(()) tree saved_access_scope; |
74 | ||
0fe0caa6 RH |
75 | /* Live only within one (recursive) call to tsubst_expr. We use |
76 | this to pass the statement expression node from the STMT_EXPR | |
77 | to the EXPR_STMT that is its result. */ | |
78 | static tree cur_stmt_expr; | |
79 | ||
6dfbb909 MM |
80 | /* A map from local variable declarations in the body of the template |
81 | presently being instantiated to the corresponding instantiated | |
82 | local variables. */ | |
83 | static htab_t local_specializations; | |
84 | ||
2b110bfc JM |
85 | typedef struct GTY(()) spec_entry |
86 | { | |
87 | tree tmpl; | |
88 | tree args; | |
89 | tree spec; | |
90 | } spec_entry; | |
91 | ||
92 | static GTY ((param_is (spec_entry))) | |
93 | htab_t decl_specializations; | |
94 | ||
95 | static GTY ((param_is (spec_entry))) | |
96 | htab_t type_specializations; | |
97 | ||
410cf6e6 | 98 | /* Contains canonical template parameter types. The vector is indexed by |
06d40de8 DG |
99 | the TEMPLATE_TYPE_IDX of the template parameter. Each element is a |
100 | TREE_LIST, whose TREE_VALUEs contain the canonical template | |
101 | parameters of various types and levels. */ | |
102 | static GTY(()) VEC(tree,gc) *canonical_template_parms; | |
103 | ||
830bfa74 MM |
104 | #define UNIFY_ALLOW_NONE 0 |
105 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
106 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
107 | #define UNIFY_ALLOW_DERIVED 4 | |
161c12b0 | 108 | #define UNIFY_ALLOW_INTEGER 8 |
028d1f20 | 109 | #define UNIFY_ALLOW_OUTER_LEVEL 16 |
62e4a758 NS |
110 | #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 |
111 | #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 | |
830bfa74 | 112 | |
3a978d72 NN |
113 | static void push_access_scope (tree); |
114 | static void pop_access_scope (tree); | |
acde59b4 MM |
115 | static bool resolve_overloaded_unification (tree, tree, tree, tree, |
116 | unification_kind_t, int); | |
3a978d72 | 117 | static int try_one_overload (tree, tree, tree, tree, tree, |
f23fb7f5 | 118 | unification_kind_t, int, bool); |
3a978d72 NN |
119 | static int unify (tree, tree, tree, tree, int); |
120 | static void add_pending_template (tree); | |
aa9d8196 VR |
121 | static int push_tinst_level (tree); |
122 | static void pop_tinst_level (void); | |
e2c3721c | 123 | static tree reopen_tinst_level (struct tinst_level *); |
3a978d72 | 124 | static tree tsubst_initializer_list (tree, tree); |
3a978d72 | 125 | static tree get_class_bindings (tree, tree, tree); |
3db45ab5 | 126 | static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, |
e7e93965 | 127 | bool, bool); |
3a978d72 NN |
128 | static void tsubst_enum (tree, tree, tree); |
129 | static tree add_to_template_args (tree, tree); | |
130 | static tree add_outermost_template_args (tree, tree); | |
131 | static bool check_instantiated_args (tree, tree, tsubst_flags_t); | |
8af2fec4 RY |
132 | static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*, |
133 | tree); | |
c166b898 ILT |
134 | static int type_unification_real (tree, tree, tree, const tree *, |
135 | unsigned int, int, unification_kind_t, int); | |
3a978d72 | 136 | static void note_template_header (int); |
b6ab6892 | 137 | static tree convert_nontype_argument_function (tree, tree); |
3a978d72 NN |
138 | static tree convert_nontype_argument (tree, tree); |
139 | static tree convert_template_argument (tree, tree, tree, | |
140 | tsubst_flags_t, int, tree); | |
0c58f841 | 141 | static int for_each_template_parm (tree, tree_fn_t, void*, |
c095a4f8 | 142 | struct pointer_set_t*, bool); |
5d80a306 | 143 | static tree expand_template_argument_pack (tree); |
3a978d72 | 144 | static tree build_template_parm_index (int, int, int, tree, tree); |
ae95e46e | 145 | static bool inline_needs_template_parms (tree); |
3a978d72 | 146 | static void push_inline_template_parms_recursive (tree, int); |
3a978d72 | 147 | static tree retrieve_local_specialization (tree); |
3a978d72 | 148 | static void register_local_specialization (tree, tree); |
2b110bfc | 149 | static hashval_t hash_specialization (const void *p); |
ef3b7b17 | 150 | static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t); |
3a978d72 NN |
151 | static int mark_template_parm (tree, void *); |
152 | static int template_parm_this_level_p (tree, void *); | |
153 | static tree tsubst_friend_function (tree, tree); | |
154 | static tree tsubst_friend_class (tree, tree); | |
155 | static int can_complete_type_without_circularity (tree); | |
a34d3336 | 156 | static tree get_bindings (tree, tree, tree, bool); |
3a978d72 NN |
157 | static int template_decl_level (tree); |
158 | static int check_cv_quals_for_unify (int, tree, tree); | |
5d80a306 DG |
159 | static void template_parm_level_and_index (tree, int*, int*); |
160 | static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool); | |
a91db711 NS |
161 | static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); |
162 | static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); | |
3a978d72 NN |
163 | static tree tsubst_template_parms (tree, tree, tsubst_flags_t); |
164 | static void regenerate_decl_from_template (tree, tree); | |
3a978d72 | 165 | static tree most_specialized_class (tree, tree); |
3a978d72 | 166 | static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); |
3a978d72 NN |
167 | static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree); |
168 | static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); | |
9b910171 | 169 | static bool check_specialization_scope (void); |
3a978d72 NN |
170 | static tree process_partial_specialization (tree); |
171 | static void set_current_access_from_decl (tree); | |
3a978d72 | 172 | static tree get_template_base (tree, tree, tree, tree); |
3a978d72 NN |
173 | static tree try_class_unification (tree, tree, tree, tree); |
174 | static int coerce_template_template_parms (tree, tree, tsubst_flags_t, | |
175 | tree, tree); | |
4cf36211 | 176 | static bool template_template_parm_bindings_ok_p (tree, tree); |
3a978d72 NN |
177 | static int template_args_equal (tree, tree); |
178 | static void tsubst_default_arguments (tree); | |
179 | static tree for_each_template_parm_r (tree *, int *, void *); | |
180 | static tree copy_default_args_to_explicit_spec_1 (tree, tree); | |
181 | static void copy_default_args_to_explicit_spec (tree); | |
182 | static int invalid_nontype_parm_type_p (tree, tsubst_flags_t); | |
a723baf1 | 183 | static int eq_local_specializations (const void *, const void *); |
5d80a306 | 184 | static bool dependent_template_arg_p (tree); |
06d40de8 | 185 | static bool any_template_arguments_need_structural_equality_p (tree); |
5552b43c | 186 | static bool dependent_type_p_r (tree); |
015c2c66 | 187 | static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool); |
14d22dd6 | 188 | static tree tsubst_copy (tree, tree, tsubst_flags_t, tree); |
5d80a306 | 189 | static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree); |
6afcfe0a | 190 | static tree tsubst_decl (tree, tree, tsubst_flags_t); |
d0940d56 | 191 | static void perform_typedefs_access_check (tree tmpl, tree targs); |
2eb25c98 | 192 | static void append_type_to_template_for_access_check_1 (tree, tree, tree); |
2b110bfc | 193 | static hashval_t iterative_hash_template_arg (tree arg, hashval_t val); |
f9329c35 | 194 | static bool primary_template_instantiation_p (const_tree); |
5f5babf1 JM |
195 | static tree listify (tree); |
196 | static tree listify_autos (tree, tree); | |
36a117a5 | 197 | |
2b59fc25 KL |
198 | /* Make the current scope suitable for access checking when we are |
199 | processing T. T can be FUNCTION_DECL for instantiated function | |
2b907f5c KL |
200 | template, or VAR_DECL for static member variable (need by |
201 | instantiate_decl). */ | |
2b59fc25 | 202 | |
8ce33230 | 203 | static void |
2b907f5c | 204 | push_access_scope (tree t) |
2b59fc25 | 205 | { |
50bc768d NS |
206 | gcc_assert (TREE_CODE (t) == FUNCTION_DECL |
207 | || TREE_CODE (t) == VAR_DECL); | |
2b59fc25 | 208 | |
23ff7e2d KL |
209 | if (DECL_FRIEND_CONTEXT (t)) |
210 | push_nested_class (DECL_FRIEND_CONTEXT (t)); | |
211 | else if (DECL_CLASS_SCOPE_P (t)) | |
2b907f5c | 212 | push_nested_class (DECL_CONTEXT (t)); |
0f399e5f KL |
213 | else |
214 | push_to_top_level (); | |
c8094d83 | 215 | |
2b907f5c | 216 | if (TREE_CODE (t) == FUNCTION_DECL) |
0f399e5f KL |
217 | { |
218 | saved_access_scope = tree_cons | |
219 | (NULL_TREE, current_function_decl, saved_access_scope); | |
220 | current_function_decl = t; | |
221 | } | |
2b59fc25 KL |
222 | } |
223 | ||
2b59fc25 KL |
224 | /* Restore the scope set up by push_access_scope. T is the node we |
225 | are processing. */ | |
226 | ||
8ce33230 | 227 | static void |
3a978d72 | 228 | pop_access_scope (tree t) |
2b59fc25 | 229 | { |
2b907f5c | 230 | if (TREE_CODE (t) == FUNCTION_DECL) |
2b59fc25 KL |
231 | { |
232 | current_function_decl = TREE_VALUE (saved_access_scope); | |
233 | saved_access_scope = TREE_CHAIN (saved_access_scope); | |
234 | } | |
0f399e5f | 235 | |
23ff7e2d | 236 | if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t)) |
0f399e5f KL |
237 | pop_nested_class (); |
238 | else | |
239 | pop_from_top_level (); | |
2b59fc25 KL |
240 | } |
241 | ||
a723baf1 MM |
242 | /* Do any processing required when DECL (a member template |
243 | declaration) is finished. Returns the TEMPLATE_DECL corresponding | |
244 | to DECL, unless it is a specialization, in which case the DECL | |
245 | itself is returned. */ | |
e1467ff2 MM |
246 | |
247 | tree | |
3a978d72 | 248 | finish_member_template_decl (tree decl) |
e1467ff2 | 249 | { |
a723baf1 MM |
250 | if (decl == error_mark_node) |
251 | return error_mark_node; | |
252 | ||
50bc768d | 253 | gcc_assert (DECL_P (decl)); |
a723baf1 MM |
254 | |
255 | if (TREE_CODE (decl) == TYPE_DECL) | |
93cdc044 | 256 | { |
a723baf1 MM |
257 | tree type; |
258 | ||
259 | type = TREE_TYPE (decl); | |
92fab505 DG |
260 | if (type == error_mark_node) |
261 | return error_mark_node; | |
9e1e64ec | 262 | if (MAYBE_CLASS_TYPE_P (type) |
a723baf1 MM |
263 | && CLASSTYPE_TEMPLATE_INFO (type) |
264 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
93cdc044 | 265 | { |
a723baf1 | 266 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
93cdc044 JM |
267 | check_member_template (tmpl); |
268 | return tmpl; | |
269 | } | |
8d019cef | 270 | return NULL_TREE; |
93cdc044 | 271 | } |
07c88314 | 272 | else if (TREE_CODE (decl) == FIELD_DECL) |
0f51ccfc | 273 | error ("data member %qD cannot be a member template", decl); |
a1da6cba | 274 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 275 | { |
a1da6cba MM |
276 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
277 | { | |
278 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
279 | return DECL_TI_TEMPLATE (decl); | |
280 | } | |
281 | else | |
282 | return decl; | |
c8094d83 | 283 | } |
a1da6cba | 284 | else |
0f51ccfc | 285 | error ("invalid member template declaration %qD", decl); |
e1467ff2 | 286 | |
a1da6cba | 287 | return error_mark_node; |
f84b4be9 | 288 | } |
e1467ff2 | 289 | |
91a77d68 JM |
290 | /* Return the template info node corresponding to T, whatever T is. */ |
291 | ||
292 | tree | |
f9329c35 | 293 | get_template_info (const_tree t) |
91a77d68 JM |
294 | { |
295 | tree tinfo = NULL_TREE; | |
296 | ||
d40a19da DS |
297 | if (!t || t == error_mark_node) |
298 | return NULL; | |
299 | ||
91a77d68 JM |
300 | if (DECL_P (t) && DECL_LANG_SPECIFIC (t)) |
301 | tinfo = DECL_TEMPLATE_INFO (t); | |
302 | ||
d5f4eddd | 303 | if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t)) |
91a77d68 JM |
304 | t = TREE_TYPE (t); |
305 | ||
306 | if (TAGGED_TYPE_P (t)) | |
307 | tinfo = TYPE_TEMPLATE_INFO (t); | |
308 | ||
309 | return tinfo; | |
310 | } | |
311 | ||
f84b4be9 | 312 | /* Returns the template nesting level of the indicated class TYPE. |
c8094d83 | 313 | |
f84b4be9 JM |
314 | For example, in: |
315 | template <class T> | |
316 | struct A | |
317 | { | |
318 | template <class U> | |
319 | struct B {}; | |
320 | }; | |
321 | ||
c8094d83 | 322 | A<T>::B<U> has depth two, while A<T> has depth one. |
39c01e4c | 323 | Both A<T>::B<int> and A<int>::B<U> have depth one, if |
260cd73f | 324 | they are instantiations, not specializations. |
39c01e4c MM |
325 | |
326 | This function is guaranteed to return 0 if passed NULL_TREE so | |
327 | that, for example, `template_class_depth (current_class_type)' is | |
328 | always safe. */ | |
f84b4be9 | 329 | |
260cd73f VR |
330 | int |
331 | template_class_depth (tree type) | |
f84b4be9 | 332 | { |
93cdc044 | 333 | int depth; |
f84b4be9 | 334 | |
c8094d83 | 335 | for (depth = 0; |
ed44da02 | 336 | type && TREE_CODE (type) != NAMESPACE_DECL; |
c8094d83 | 337 | type = (TREE_CODE (type) == FUNCTION_DECL) |
4f1c5b7d | 338 | ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) |
ed44da02 | 339 | { |
91a77d68 JM |
340 | tree tinfo = get_template_info (type); |
341 | ||
342 | if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) | |
343 | && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))) | |
344 | ++depth; | |
ed44da02 | 345 | } |
f84b4be9 JM |
346 | |
347 | return depth; | |
e1467ff2 | 348 | } |
98c1c668 | 349 | |
ae95e46e PC |
350 | /* Subroutine of maybe_begin_member_template_processing. |
351 | Returns true if processing DECL needs us to push template parms. */ | |
cae40af6 | 352 | |
ae95e46e | 353 | static bool |
3a978d72 | 354 | inline_needs_template_parms (tree decl) |
cae40af6 JM |
355 | { |
356 | if (! DECL_TEMPLATE_INFO (decl)) | |
ae95e46e | 357 | return false; |
f84b4be9 | 358 | |
36a117a5 | 359 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
360 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
361 | } | |
362 | ||
363 | /* Subroutine of maybe_begin_member_template_processing. | |
364 | Push the template parms in PARMS, starting from LEVELS steps into the | |
365 | chain, and ending at the beginning, since template parms are listed | |
366 | innermost first. */ | |
367 | ||
368 | static void | |
3a978d72 | 369 | push_inline_template_parms_recursive (tree parmlist, int levels) |
cae40af6 JM |
370 | { |
371 | tree parms = TREE_VALUE (parmlist); | |
372 | int i; | |
373 | ||
374 | if (levels > 1) | |
375 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 376 | |
98c1c668 | 377 | ++processing_template_decl; |
cae40af6 | 378 | current_template_parms |
4890c2f4 | 379 | = tree_cons (size_int (processing_template_decl), |
98c1c668 | 380 | parms, current_template_parms); |
cae40af6 JM |
381 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
382 | ||
ac20c67a | 383 | begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, |
0cbd7506 | 384 | NULL); |
c8094d83 | 385 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
98c1c668 | 386 | { |
0f67a82f | 387 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
42b304f1 | 388 | |
0f67a82f LM |
389 | if (parm == error_mark_node) |
390 | continue; | |
42b304f1 | 391 | |
50bc768d | 392 | gcc_assert (DECL_P (parm)); |
cae40af6 | 393 | |
98c1c668 JM |
394 | switch (TREE_CODE (parm)) |
395 | { | |
786b5245 | 396 | case TYPE_DECL: |
73b0fce8 | 397 | case TEMPLATE_DECL: |
98c1c668 JM |
398 | pushdecl (parm); |
399 | break; | |
786b5245 MM |
400 | |
401 | case PARM_DECL: | |
402 | { | |
fc03edb3 MM |
403 | /* Make a CONST_DECL as is done in process_template_parm. |
404 | It is ugly that we recreate this here; the original | |
405 | version built in process_template_parm is no longer | |
406 | available. */ | |
c2255bc4 AH |
407 | tree decl = build_decl (DECL_SOURCE_LOCATION (parm), |
408 | CONST_DECL, DECL_NAME (parm), | |
786b5245 | 409 | TREE_TYPE (parm)); |
c727aa5e | 410 | DECL_ARTIFICIAL (decl) = 1; |
6de9cd9a | 411 | TREE_CONSTANT (decl) = 1; |
6de9cd9a | 412 | TREE_READONLY (decl) = 1; |
786b5245 | 413 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
cd9f6678 | 414 | SET_DECL_TEMPLATE_PARM_P (decl); |
786b5245 MM |
415 | pushdecl (decl); |
416 | } | |
cae40af6 | 417 | break; |
786b5245 | 418 | |
98c1c668 | 419 | default: |
315fb5db | 420 | gcc_unreachable (); |
98c1c668 JM |
421 | } |
422 | } | |
423 | } | |
424 | ||
cae40af6 JM |
425 | /* Restore the template parameter context for a member template or |
426 | a friend template defined in a class definition. */ | |
427 | ||
428 | void | |
3a978d72 | 429 | maybe_begin_member_template_processing (tree decl) |
cae40af6 JM |
430 | { |
431 | tree parms; | |
3dcaad8b | 432 | int levels = 0; |
cae40af6 | 433 | |
3dcaad8b MM |
434 | if (inline_needs_template_parms (decl)) |
435 | { | |
436 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
437 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
cae40af6 | 438 | |
3dcaad8b MM |
439 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
440 | { | |
441 | --levels; | |
442 | parms = TREE_CHAIN (parms); | |
443 | } | |
cae40af6 | 444 | |
3dcaad8b | 445 | push_inline_template_parms_recursive (parms, levels); |
cae40af6 JM |
446 | } |
447 | ||
3dcaad8b MM |
448 | /* Remember how many levels of template parameters we pushed so that |
449 | we can pop them later. */ | |
53824026 | 450 | VEC_safe_push (int, heap, inline_parm_levels, levels); |
cae40af6 JM |
451 | } |
452 | ||
1875c2b7 | 453 | /* Undo the effects of maybe_begin_member_template_processing. */ |
98c1c668 | 454 | |
c8094d83 | 455 | void |
3a978d72 | 456 | maybe_end_member_template_processing (void) |
98c1c668 | 457 | { |
3dcaad8b | 458 | int i; |
53824026 | 459 | int last; |
3dcaad8b | 460 | |
53824026 | 461 | if (VEC_length (int, inline_parm_levels) == 0) |
98c1c668 JM |
462 | return; |
463 | ||
53824026 KH |
464 | last = VEC_pop (int, inline_parm_levels); |
465 | for (i = 0; i < last; ++i) | |
cae40af6 JM |
466 | { |
467 | --processing_template_decl; | |
468 | current_template_parms = TREE_CHAIN (current_template_parms); | |
469 | poplevel (0, 0, 0); | |
470 | } | |
98c1c668 JM |
471 | } |
472 | ||
36a117a5 | 473 | /* Return a new template argument vector which contains all of ARGS, |
f9a7ae04 | 474 | but has as its innermost set of arguments the EXTRA_ARGS. */ |
e6f1275f JM |
475 | |
476 | static tree | |
3a978d72 | 477 | add_to_template_args (tree args, tree extra_args) |
e6f1275f | 478 | { |
36a117a5 MM |
479 | tree new_args; |
480 | int extra_depth; | |
481 | int i; | |
482 | int j; | |
e6f1275f | 483 | |
36a117a5 | 484 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
f31c0a32 | 485 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); |
e6f1275f | 486 | |
36a117a5 MM |
487 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
488 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 489 | |
36a117a5 MM |
490 | for (j = 1; j <= extra_depth; ++j, ++i) |
491 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
c8094d83 | 492 | |
e6f1275f JM |
493 | return new_args; |
494 | } | |
495 | ||
36a117a5 MM |
496 | /* Like add_to_template_args, but only the outermost ARGS are added to |
497 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
498 | (EXTRA_ARGS) levels are added. This function is used to combine | |
499 | the template arguments from a partial instantiation with the | |
500 | template arguments used to attain the full instantiation from the | |
501 | partial instantiation. */ | |
98c1c668 | 502 | |
4966381a | 503 | static tree |
3a978d72 | 504 | add_outermost_template_args (tree args, tree extra_args) |
98c1c668 JM |
505 | { |
506 | tree new_args; | |
507 | ||
e4a84209 MM |
508 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
509 | something very fishy is going on. */ | |
50bc768d | 510 | gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args)); |
e4a84209 MM |
511 | |
512 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
513 | them. */ | |
514 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
515 | return extra_args; | |
516 | ||
36a117a5 MM |
517 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
518 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
c8094d83 | 519 | |
36a117a5 | 520 | new_args = add_to_template_args (args, extra_args); |
98c1c668 | 521 | |
36a117a5 MM |
522 | /* Now, we restore ARGS to its full dimensions. */ |
523 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
524 | |
525 | return new_args; | |
526 | } | |
5566b478 | 527 | |
f9a7ae04 MM |
528 | /* Return the N levels of innermost template arguments from the ARGS. */ |
529 | ||
530 | tree | |
3a978d72 | 531 | get_innermost_template_args (tree args, int n) |
f9a7ae04 MM |
532 | { |
533 | tree new_args; | |
534 | int extra_levels; | |
535 | int i; | |
536 | ||
50bc768d | 537 | gcc_assert (n >= 0); |
f9a7ae04 MM |
538 | |
539 | /* If N is 1, just return the innermost set of template arguments. */ | |
540 | if (n == 1) | |
541 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); | |
c8094d83 | 542 | |
f9a7ae04 MM |
543 | /* If we're not removing anything, just return the arguments we were |
544 | given. */ | |
545 | extra_levels = TMPL_ARGS_DEPTH (args) - n; | |
50bc768d | 546 | gcc_assert (extra_levels >= 0); |
f9a7ae04 MM |
547 | if (extra_levels == 0) |
548 | return args; | |
549 | ||
550 | /* Make a new set of arguments, not containing the outer arguments. */ | |
551 | new_args = make_tree_vec (n); | |
552 | for (i = 1; i <= n; ++i) | |
c8094d83 | 553 | SET_TMPL_ARGS_LEVEL (new_args, i, |
f9a7ae04 MM |
554 | TMPL_ARGS_LEVEL (args, i + extra_levels)); |
555 | ||
556 | return new_args; | |
557 | } | |
558 | ||
dc28490d JM |
559 | /* The inverse of get_innermost_template_args: Return all but the innermost |
560 | EXTRA_LEVELS levels of template arguments from the ARGS. */ | |
561 | ||
562 | static tree | |
563 | strip_innermost_template_args (tree args, int extra_levels) | |
564 | { | |
565 | tree new_args; | |
566 | int n = TMPL_ARGS_DEPTH (args) - extra_levels; | |
567 | int i; | |
568 | ||
569 | gcc_assert (n >= 0); | |
570 | ||
571 | /* If N is 1, just return the outermost set of template arguments. */ | |
572 | if (n == 1) | |
573 | return TMPL_ARGS_LEVEL (args, 1); | |
574 | ||
575 | /* If we're not removing anything, just return the arguments we were | |
576 | given. */ | |
577 | gcc_assert (extra_levels >= 0); | |
578 | if (extra_levels == 0) | |
579 | return args; | |
580 | ||
581 | /* Make a new set of arguments, not containing the inner arguments. */ | |
582 | new_args = make_tree_vec (n); | |
583 | for (i = 1; i <= n; ++i) | |
584 | SET_TMPL_ARGS_LEVEL (new_args, i, | |
585 | TMPL_ARGS_LEVEL (args, i)); | |
586 | ||
587 | return new_args; | |
588 | } | |
589 | ||
5566b478 MS |
590 | /* We've got a template header coming up; push to a new level for storing |
591 | the parms. */ | |
8d08fdba | 592 | |
8d08fdba | 593 | void |
3a978d72 | 594 | begin_template_parm_list (void) |
8d08fdba | 595 | { |
6757edfe MM |
596 | /* We use a non-tag-transparent scope here, which causes pushtag to |
597 | put tags in this scope, rather than in the enclosing class or | |
598 | namespace scope. This is the right thing, since we want | |
599 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
600 | global template class, push_template_decl handles putting the | |
601 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
602 | e.g.: | |
603 | ||
604 | template <class T> struct S1 { | |
0cbd7506 | 605 | template <class T> struct S2 {}; |
6757edfe MM |
606 | }; |
607 | ||
608 | pushtag contains special code to call pushdecl_with_scope on the | |
609 | TEMPLATE_DECL for S2. */ | |
ac20c67a | 610 | begin_scope (sk_template_parms, NULL); |
5156628f | 611 | ++processing_template_decl; |
67ffc812 | 612 | ++processing_template_parmlist; |
386b8a85 JM |
613 | note_template_header (0); |
614 | } | |
615 | ||
6c30752f | 616 | /* This routine is called when a specialization is declared. If it is |
9b910171 LM |
617 | invalid to declare a specialization here, an error is reported and |
618 | false is returned, otherwise this routine will return true. */ | |
6c30752f | 619 | |
9b910171 | 620 | static bool |
3a978d72 | 621 | check_specialization_scope (void) |
6c30752f MM |
622 | { |
623 | tree scope = current_scope (); | |
3ddfb0e6 | 624 | |
c8094d83 MS |
625 | /* [temp.expl.spec] |
626 | ||
6c30752f MM |
627 | An explicit specialization shall be declared in the namespace of |
628 | which the template is a member, or, for member templates, in the | |
629 | namespace of which the enclosing class or enclosing class | |
630 | template is a member. An explicit specialization of a member | |
631 | function, member class or static data member of a class template | |
632 | shall be declared in the namespace of which the class template | |
633 | is a member. */ | |
634 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
9b910171 LM |
635 | { |
636 | error ("explicit specialization in non-namespace scope %qD", scope); | |
637 | return false; | |
638 | } | |
3ddfb0e6 | 639 | |
c8094d83 | 640 | /* [temp.expl.spec] |
6c30752f MM |
641 | |
642 | In an explicit specialization declaration for a member of a class | |
643 | template or a member template that appears in namespace scope, | |
644 | the member template and some of its enclosing class templates may | |
645 | remain unspecialized, except that the declaration shall not | |
646 | explicitly specialize a class member template if its enclosing | |
647 | class templates are not explicitly specialized as well. */ | |
c8094d83 | 648 | if (current_template_parms) |
9b910171 LM |
649 | { |
650 | error ("enclosing class templates are not explicitly specialized"); | |
651 | return false; | |
652 | } | |
653 | ||
654 | return true; | |
6c30752f MM |
655 | } |
656 | ||
c6002625 | 657 | /* We've just seen template <>. */ |
386b8a85 | 658 | |
9b910171 | 659 | bool |
3a978d72 | 660 | begin_specialization (void) |
386b8a85 | 661 | { |
ac20c67a | 662 | begin_scope (sk_template_spec, NULL); |
386b8a85 | 663 | note_template_header (1); |
9b910171 | 664 | return check_specialization_scope (); |
386b8a85 JM |
665 | } |
666 | ||
dc957d14 | 667 | /* Called at then end of processing a declaration preceded by |
386b8a85 JM |
668 | template<>. */ |
669 | ||
c8094d83 | 670 | void |
3a978d72 | 671 | end_specialization (void) |
386b8a85 | 672 | { |
74b846e0 | 673 | finish_scope (); |
386b8a85 JM |
674 | reset_specialization (); |
675 | } | |
676 | ||
386b8a85 | 677 | /* Any template <>'s that we have seen thus far are not referring to a |
c6002625 | 678 | function specialization. */ |
386b8a85 JM |
679 | |
680 | void | |
3a978d72 | 681 | reset_specialization (void) |
386b8a85 JM |
682 | { |
683 | processing_specialization = 0; | |
684 | template_header_count = 0; | |
685 | } | |
686 | ||
838dfd8a | 687 | /* We've just seen a template header. If SPECIALIZATION is nonzero, |
386b8a85 JM |
688 | it was of the form template <>. */ |
689 | ||
c8094d83 | 690 | static void |
3a978d72 | 691 | note_template_header (int specialization) |
386b8a85 JM |
692 | { |
693 | processing_specialization = specialization; | |
694 | template_header_count++; | |
695 | } | |
696 | ||
75650646 | 697 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 698 | |
75650646 | 699 | void |
3a978d72 | 700 | begin_explicit_instantiation (void) |
386b8a85 | 701 | { |
50bc768d | 702 | gcc_assert (!processing_explicit_instantiation); |
a723baf1 | 703 | processing_explicit_instantiation = true; |
75650646 | 704 | } |
386b8a85 | 705 | |
386b8a85 | 706 | |
75650646 | 707 | void |
3a978d72 | 708 | end_explicit_instantiation (void) |
75650646 | 709 | { |
50bc768d | 710 | gcc_assert (processing_explicit_instantiation); |
a723baf1 | 711 | processing_explicit_instantiation = false; |
75650646 | 712 | } |
386b8a85 | 713 | |
13a44ee0 | 714 | /* An explicit specialization or partial specialization TMPL is being |
b1cc95ce MM |
715 | declared. Check that the namespace in which the specialization is |
716 | occurring is permissible. Returns false iff it is invalid to | |
717 | specialize TMPL in the current namespace. */ | |
c8094d83 | 718 | |
b1cc95ce MM |
719 | static bool |
720 | check_specialization_namespace (tree tmpl) | |
721 | { | |
722 | tree tpl_ns = decl_namespace_context (tmpl); | |
723 | ||
724 | /* [tmpl.expl.spec] | |
c8094d83 | 725 | |
b1cc95ce MM |
726 | An explicit specialization shall be declared in the namespace of |
727 | which the template is a member, or, for member templates, in the | |
728 | namespace of which the enclosing class or enclosing class | |
729 | template is a member. An explicit specialization of a member | |
730 | function, member class or static data member of a class template | |
731 | shall be declared in the namespace of which the class template is | |
732 | a member. */ | |
733 | if (is_associated_namespace (current_namespace, tpl_ns)) | |
734 | /* Same or super-using namespace. */ | |
735 | return true; | |
736 | else | |
737 | { | |
cbe5f3b3 MLI |
738 | permerror (input_location, "specialization of %qD in different namespace", tmpl); |
739 | permerror (input_location, " from definition of %q+#D", tmpl); | |
b1cc95ce MM |
740 | return false; |
741 | } | |
742 | } | |
743 | ||
9c12301f MM |
744 | /* SPEC is an explicit instantiation. Check that it is valid to |
745 | perform this explicit instantiation in the current namespace. */ | |
746 | ||
747 | static void | |
748 | check_explicit_instantiation_namespace (tree spec) | |
749 | { | |
750 | tree ns; | |
751 | ||
752 | /* DR 275: An explicit instantiation shall appear in an enclosing | |
3db45ab5 | 753 | namespace of its template. */ |
9c12301f MM |
754 | ns = decl_namespace_context (spec); |
755 | if (!is_ancestor (current_namespace, ns)) | |
cbe5f3b3 | 756 | permerror (input_location, "explicit instantiation of %qD in namespace %qD " |
37ec60ed JW |
757 | "(which does not enclose namespace %qD)", |
758 | spec, current_namespace, ns); | |
9c12301f MM |
759 | } |
760 | ||
36a117a5 MM |
761 | /* The TYPE is being declared. If it is a template type, that means it |
762 | is a partial specialization. Do appropriate error-checking. */ | |
763 | ||
9ce387d9 | 764 | tree |
3a978d72 | 765 | maybe_process_partial_specialization (tree type) |
36a117a5 | 766 | { |
c9043d03 VR |
767 | tree context; |
768 | ||
769 | if (type == error_mark_node) | |
9ce387d9 VR |
770 | return error_mark_node; |
771 | ||
772 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
773 | { | |
774 | error ("name of class shadows template template parameter %qD", | |
775 | TYPE_NAME (type)); | |
776 | return error_mark_node; | |
777 | } | |
c9043d03 VR |
778 | |
779 | context = TYPE_CONTEXT (type); | |
6e049fcd | 780 | |
a723baf1 | 781 | if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) |
36a117a5 | 782 | { |
6e049fcd KL |
783 | /* This is for ordinary explicit specialization and partial |
784 | specialization of a template class such as: | |
785 | ||
786 | template <> class C<int>; | |
787 | ||
788 | or: | |
789 | ||
790 | template <class T> class C<T*>; | |
791 | ||
792 | Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ | |
793 | ||
36a117a5 | 794 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) |
d0f062fb | 795 | && !COMPLETE_TYPE_P (type)) |
36a117a5 | 796 | { |
b1cc95ce | 797 | check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)); |
370af2d5 | 798 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
36a117a5 | 799 | if (processing_template_decl) |
c5285718 PC |
800 | { |
801 | if (push_template_decl (TYPE_MAIN_DECL (type)) | |
802 | == error_mark_node) | |
803 | return error_mark_node; | |
804 | } | |
36a117a5 MM |
805 | } |
806 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
0f51ccfc | 807 | error ("specialization of %qT after instantiation", type); |
36a117a5 | 808 | } |
6e049fcd KL |
809 | else if (CLASS_TYPE_P (type) |
810 | && !CLASSTYPE_USE_TEMPLATE (type) | |
811 | && CLASSTYPE_TEMPLATE_INFO (type) | |
812 | && context && CLASS_TYPE_P (context) | |
813 | && CLASSTYPE_TEMPLATE_INFO (context)) | |
814 | { | |
815 | /* This is for an explicit specialization of member class | |
816 | template according to [temp.expl.spec/18]: | |
817 | ||
818 | template <> template <class U> class C<int>::D; | |
819 | ||
820 | The context `C<int>' must be an implicit instantiation. | |
821 | Otherwise this is just a member class template declared | |
822 | earlier like: | |
823 | ||
824 | template <> class C<int> { template <class U> class D; }; | |
825 | template <> template <class U> class C<int>::D; | |
826 | ||
827 | In the first case, `C<int>::D' is a specialization of `C<T>::D' | |
828 | while in the second case, `C<int>::D' is a primary template | |
829 | and `C<T>::D' may not exist. */ | |
830 | ||
831 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) | |
832 | && !COMPLETE_TYPE_P (type)) | |
833 | { | |
834 | tree t; | |
2b110bfc | 835 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
6e049fcd KL |
836 | |
837 | if (current_namespace | |
2b110bfc | 838 | != decl_namespace_context (tmpl)) |
6e049fcd | 839 | { |
cbe5f3b3 | 840 | permerror (input_location, "specializing %q#T in different namespace", type); |
2b110bfc | 841 | permerror (input_location, " from definition of %q+#D", tmpl); |
6e049fcd KL |
842 | } |
843 | ||
844 | /* Check for invalid specialization after instantiation: | |
845 | ||
846 | template <> template <> class C<int>::D<int>; | |
847 | template <> template <class U> class C<int>::D; */ | |
848 | ||
2b110bfc | 849 | for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); |
6e049fcd | 850 | t; t = TREE_CHAIN (t)) |
2b110bfc JM |
851 | { |
852 | tree inst = TREE_VALUE (t); | |
853 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)) | |
854 | { | |
855 | /* We already have a full specialization of this partial | |
856 | instantiation. Reassign it to the new member | |
857 | specialization template. */ | |
858 | spec_entry elt; | |
859 | spec_entry **slot; | |
860 | ||
861 | elt.tmpl = most_general_template (tmpl); | |
862 | elt.args = CLASSTYPE_TI_ARGS (inst); | |
863 | elt.spec = inst; | |
864 | ||
865 | htab_remove_elt (type_specializations, &elt); | |
866 | ||
867 | elt.tmpl = tmpl; | |
868 | elt.args = INNERMOST_TEMPLATE_ARGS (elt.args); | |
869 | ||
870 | slot = (spec_entry **) | |
871 | htab_find_slot (type_specializations, &elt, INSERT); | |
872 | *slot = GGC_NEW (spec_entry); | |
873 | **slot = elt; | |
874 | } | |
875 | else if (COMPLETE_TYPE_P (inst) || TYPE_BEING_DEFINED (inst)) | |
876 | /* But if we've had an implicit instantiation, that's a | |
877 | problem ([temp.expl.spec]/6). */ | |
878 | error ("specialization %qT after instantiation %qT", | |
879 | type, inst); | |
880 | } | |
6e049fcd KL |
881 | |
882 | /* Mark TYPE as a specialization. And as a result, we only | |
883 | have one level of template argument for the innermost | |
884 | class template. */ | |
885 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); | |
886 | CLASSTYPE_TI_ARGS (type) | |
887 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); | |
888 | } | |
889 | } | |
20496fa2 | 890 | else if (processing_specialization) |
9ce387d9 VR |
891 | { |
892 | error ("explicit specialization of non-template %qT", type); | |
893 | return error_mark_node; | |
894 | } | |
895 | ||
896 | return type; | |
36a117a5 MM |
897 | } |
898 | ||
ee81147e | 899 | /* Returns nonzero if we can optimize the retrieval of specializations |
c7222c02 MM |
900 | for TMPL, a TEMPLATE_DECL. In particular, for such a template, we |
901 | do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */ | |
902 | ||
c8094d83 | 903 | static inline bool |
c7222c02 MM |
904 | optimize_specialization_lookup_p (tree tmpl) |
905 | { | |
906 | return (DECL_FUNCTION_TEMPLATE_P (tmpl) | |
907 | && DECL_CLASS_SCOPE_P (tmpl) | |
908 | /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template | |
909 | parameter. */ | |
910 | && CLASS_TYPE_P (DECL_CONTEXT (tmpl)) | |
1ca939e5 MM |
911 | /* The optimized lookup depends on the fact that the |
912 | template arguments for the member function template apply | |
913 | purely to the containing class, which is not true if the | |
914 | containing class is an explicit or partial | |
915 | specialization. */ | |
916 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl)) | |
c7222c02 MM |
917 | && !DECL_MEMBER_TEMPLATE_P (tmpl) |
918 | && !DECL_CONV_FN_P (tmpl) | |
919 | /* It is possible to have a template that is not a member | |
920 | template and is not a member of a template class: | |
c8094d83 MS |
921 | |
922 | template <typename T> | |
c7222c02 | 923 | struct S { friend A::f(); }; |
c8094d83 | 924 | |
c7222c02 MM |
925 | Here, the friend function is a template, but the context does |
926 | not have template information. The optimized lookup relies | |
927 | on having ARGS be the template arguments for both the class | |
928 | and the function template. */ | |
929 | && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl))); | |
930 | } | |
931 | ||
75650646 MM |
932 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
933 | specialization is either an instantiation or an explicit | |
934 | specialization) of TMPL for the given template ARGS. If there is | |
935 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
936 | arguments, or a vector of vectors of arguments, in the case of | |
c8094d83 | 937 | templates with more than one level of parameters. |
c7222c02 MM |
938 | |
939 | If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true, | |
940 | then we search for a partial specialization matching ARGS. This | |
941 | parameter is ignored if TMPL is not a class template. */ | |
c8094d83 | 942 | |
75650646 | 943 | static tree |
2b110bfc | 944 | retrieve_specialization (tree tmpl, tree args, hashval_t hash) |
75650646 | 945 | { |
2d8ba2c7 LM |
946 | if (args == error_mark_node) |
947 | return NULL_TREE; | |
948 | ||
50bc768d | 949 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 950 | |
36a117a5 MM |
951 | /* There should be as many levels of arguments as there are |
952 | levels of parameters. */ | |
c8094d83 | 953 | gcc_assert (TMPL_ARGS_DEPTH (args) |
50bc768d | 954 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); |
c8094d83 | 955 | |
c7222c02 MM |
956 | if (optimize_specialization_lookup_p (tmpl)) |
957 | { | |
958 | tree class_template; | |
959 | tree class_specialization; | |
d4e6fecb | 960 | VEC(tree,gc) *methods; |
c7222c02 MM |
961 | tree fns; |
962 | int idx; | |
963 | ||
964 | /* The template arguments actually apply to the containing | |
965 | class. Find the class specialization with those | |
966 | arguments. */ | |
967 | class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl)); | |
c8094d83 | 968 | class_specialization |
2b110bfc | 969 | = retrieve_specialization (class_template, args, 0); |
c7222c02 MM |
970 | if (!class_specialization) |
971 | return NULL_TREE; | |
972 | /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC | |
973 | for the specialization. */ | |
974 | idx = class_method_index_for_fn (class_specialization, tmpl); | |
975 | if (idx == -1) | |
976 | return NULL_TREE; | |
977 | /* Iterate through the methods with the indicated name, looking | |
978 | for the one that has an instance of TMPL. */ | |
979 | methods = CLASSTYPE_METHOD_VEC (class_specialization); | |
980 | for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns)) | |
981 | { | |
982 | tree fn = OVL_CURRENT (fns); | |
eb5c3f05 JM |
983 | if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl |
984 | /* using-declarations can add base methods to the method vec, | |
985 | and we don't want those here. */ | |
986 | && DECL_CONTEXT (fn) == class_specialization) | |
c7222c02 MM |
987 | return fn; |
988 | } | |
989 | return NULL_TREE; | |
990 | } | |
991 | else | |
992 | { | |
2b110bfc JM |
993 | spec_entry *found; |
994 | spec_entry elt; | |
995 | htab_t specializations; | |
996 | ||
997 | elt.tmpl = tmpl; | |
998 | elt.args = args; | |
999 | elt.spec = NULL_TREE; | |
1000 | ||
1001 | if (DECL_CLASS_TEMPLATE_P (tmpl)) | |
1002 | specializations = type_specializations; | |
c7222c02 | 1003 | else |
2b110bfc | 1004 | specializations = decl_specializations; |
c8094d83 | 1005 | |
2b110bfc JM |
1006 | if (hash == 0) |
1007 | hash = hash_specialization (&elt); | |
1008 | found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash); | |
1009 | if (found) | |
1010 | return found->spec; | |
c7222c02 | 1011 | } |
75650646 MM |
1012 | |
1013 | return NULL_TREE; | |
386b8a85 JM |
1014 | } |
1015 | ||
dc957d14 | 1016 | /* Like retrieve_specialization, but for local declarations. */ |
9188c363 MM |
1017 | |
1018 | static tree | |
3a978d72 | 1019 | retrieve_local_specialization (tree tmpl) |
9188c363 | 1020 | { |
29b0d1fd JM |
1021 | tree spec; |
1022 | ||
1023 | if (local_specializations == NULL) | |
1024 | return NULL_TREE; | |
1025 | ||
1026 | spec = (tree) htab_find_with_hash (local_specializations, tmpl, | |
1027 | htab_hash_pointer (tmpl)); | |
a723baf1 | 1028 | return spec ? TREE_PURPOSE (spec) : NULL_TREE; |
9188c363 MM |
1029 | } |
1030 | ||
838dfd8a | 1031 | /* Returns nonzero iff DECL is a specialization of TMPL. */ |
6757edfe MM |
1032 | |
1033 | int | |
3a978d72 | 1034 | is_specialization_of (tree decl, tree tmpl) |
6757edfe MM |
1035 | { |
1036 | tree t; | |
1037 | ||
1038 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
1039 | { | |
c8094d83 | 1040 | for (t = decl; |
6757edfe MM |
1041 | t != NULL_TREE; |
1042 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
1043 | if (t == tmpl) | |
1044 | return 1; | |
1045 | } | |
c8094d83 | 1046 | else |
6757edfe | 1047 | { |
50bc768d | 1048 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); |
6757edfe MM |
1049 | |
1050 | for (t = TREE_TYPE (decl); | |
1051 | t != NULL_TREE; | |
1052 | t = CLASSTYPE_USE_TEMPLATE (t) | |
1053 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
9edc3913 | 1054 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) |
6757edfe | 1055 | return 1; |
c8094d83 | 1056 | } |
386b8a85 | 1057 | |
6757edfe MM |
1058 | return 0; |
1059 | } | |
75650646 | 1060 | |
d43f603d | 1061 | /* Returns nonzero iff DECL is a specialization of friend declaration |
be93747e | 1062 | FRIEND_DECL according to [temp.friend]. */ |
d43f603d KL |
1063 | |
1064 | bool | |
be93747e | 1065 | is_specialization_of_friend (tree decl, tree friend_decl) |
d43f603d KL |
1066 | { |
1067 | bool need_template = true; | |
1068 | int template_depth; | |
1069 | ||
b939a023 KL |
1070 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
1071 | || TREE_CODE (decl) == TYPE_DECL); | |
d43f603d | 1072 | |
be93747e | 1073 | /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function |
d43f603d KL |
1074 | of a template class, we want to check if DECL is a specialization |
1075 | if this. */ | |
be93747e KG |
1076 | if (TREE_CODE (friend_decl) == FUNCTION_DECL |
1077 | && DECL_TEMPLATE_INFO (friend_decl) | |
1078 | && !DECL_USE_TEMPLATE (friend_decl)) | |
d43f603d | 1079 | { |
b939a023 | 1080 | /* We want a TEMPLATE_DECL for `is_specialization_of'. */ |
be93747e | 1081 | friend_decl = DECL_TI_TEMPLATE (friend_decl); |
d43f603d KL |
1082 | need_template = false; |
1083 | } | |
be93747e KG |
1084 | else if (TREE_CODE (friend_decl) == TEMPLATE_DECL |
1085 | && !PRIMARY_TEMPLATE_P (friend_decl)) | |
b939a023 | 1086 | need_template = false; |
d43f603d KL |
1087 | |
1088 | /* There is nothing to do if this is not a template friend. */ | |
be93747e | 1089 | if (TREE_CODE (friend_decl) != TEMPLATE_DECL) |
b939a023 | 1090 | return false; |
d43f603d | 1091 | |
be93747e | 1092 | if (is_specialization_of (decl, friend_decl)) |
b939a023 | 1093 | return true; |
d43f603d KL |
1094 | |
1095 | /* [temp.friend/6] | |
1096 | A member of a class template may be declared to be a friend of a | |
1097 | non-template class. In this case, the corresponding member of | |
1098 | every specialization of the class template is a friend of the | |
1099 | class granting friendship. | |
c8094d83 | 1100 | |
d43f603d KL |
1101 | For example, given a template friend declaration |
1102 | ||
1103 | template <class T> friend void A<T>::f(); | |
1104 | ||
1105 | the member function below is considered a friend | |
1106 | ||
1107 | template <> struct A<int> { | |
1108 | void f(); | |
1109 | }; | |
1110 | ||
1111 | For this type of template friend, TEMPLATE_DEPTH below will be | |
5c425df5 | 1112 | nonzero. To determine if DECL is a friend of FRIEND, we first |
d43f603d KL |
1113 | check if the enclosing class is a specialization of another. */ |
1114 | ||
be93747e | 1115 | template_depth = template_class_depth (DECL_CONTEXT (friend_decl)); |
d43f603d KL |
1116 | if (template_depth |
1117 | && DECL_CLASS_SCOPE_P (decl) | |
c8094d83 | 1118 | && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), |
be93747e | 1119 | CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl)))) |
d43f603d KL |
1120 | { |
1121 | /* Next, we check the members themselves. In order to handle | |
be93747e | 1122 | a few tricky cases, such as when FRIEND_DECL's are |
d43f603d KL |
1123 | |
1124 | template <class T> friend void A<T>::g(T t); | |
1125 | template <class T> template <T t> friend void A<T>::h(); | |
1126 | ||
b939a023 KL |
1127 | and DECL's are |
1128 | ||
1129 | void A<int>::g(int); | |
1130 | template <int> void A<int>::h(); | |
1131 | ||
1132 | we need to figure out ARGS, the template arguments from | |
1133 | the context of DECL. This is required for template substitution | |
1134 | of `T' in the function parameter of `g' and template parameter | |
1135 | of `h' in the above examples. Here ARGS corresponds to `int'. */ | |
d43f603d KL |
1136 | |
1137 | tree context = DECL_CONTEXT (decl); | |
1138 | tree args = NULL_TREE; | |
1139 | int current_depth = 0; | |
b939a023 | 1140 | |
d43f603d KL |
1141 | while (current_depth < template_depth) |
1142 | { | |
1143 | if (CLASSTYPE_TEMPLATE_INFO (context)) | |
1144 | { | |
1145 | if (current_depth == 0) | |
1146 | args = TYPE_TI_ARGS (context); | |
1147 | else | |
1148 | args = add_to_template_args (TYPE_TI_ARGS (context), args); | |
1149 | current_depth++; | |
1150 | } | |
1151 | context = TYPE_CONTEXT (context); | |
1152 | } | |
1153 | ||
1154 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
1155 | { | |
1156 | bool is_template; | |
1157 | tree friend_type; | |
1158 | tree decl_type; | |
1159 | tree friend_args_type; | |
1160 | tree decl_args_type; | |
1161 | ||
be93747e | 1162 | /* Make sure that both DECL and FRIEND_DECL are templates or |
d43f603d KL |
1163 | non-templates. */ |
1164 | is_template = DECL_TEMPLATE_INFO (decl) | |
1165 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); | |
1166 | if (need_template ^ is_template) | |
b939a023 | 1167 | return false; |
d43f603d KL |
1168 | else if (is_template) |
1169 | { | |
da1d7781 | 1170 | /* If both are templates, check template parameter list. */ |
d43f603d | 1171 | tree friend_parms |
be93747e | 1172 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl), |
d43f603d KL |
1173 | args, tf_none); |
1174 | if (!comp_template_parms | |
1175 | (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), | |
1176 | friend_parms)) | |
b939a023 | 1177 | return false; |
d43f603d KL |
1178 | |
1179 | decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); | |
1180 | } | |
1181 | else | |
1182 | decl_type = TREE_TYPE (decl); | |
1183 | ||
be93747e | 1184 | friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args, |
d43f603d KL |
1185 | tf_none, NULL_TREE); |
1186 | if (friend_type == error_mark_node) | |
b939a023 | 1187 | return false; |
d43f603d KL |
1188 | |
1189 | /* Check if return types match. */ | |
1190 | if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) | |
b939a023 | 1191 | return false; |
d43f603d KL |
1192 | |
1193 | /* Check if function parameter types match, ignoring the | |
1194 | `this' parameter. */ | |
1195 | friend_args_type = TYPE_ARG_TYPES (friend_type); | |
1196 | decl_args_type = TYPE_ARG_TYPES (decl_type); | |
be93747e | 1197 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl)) |
d43f603d KL |
1198 | friend_args_type = TREE_CHAIN (friend_args_type); |
1199 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1200 | decl_args_type = TREE_CHAIN (decl_args_type); | |
b939a023 KL |
1201 | |
1202 | return compparms (decl_args_type, friend_args_type); | |
1203 | } | |
1204 | else | |
1205 | { | |
1206 | /* DECL is a TYPE_DECL */ | |
1207 | bool is_template; | |
1208 | tree decl_type = TREE_TYPE (decl); | |
1209 | ||
be93747e | 1210 | /* Make sure that both DECL and FRIEND_DECL are templates or |
b939a023 KL |
1211 | non-templates. */ |
1212 | is_template | |
1213 | = CLASSTYPE_TEMPLATE_INFO (decl_type) | |
1214 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type)); | |
1215 | ||
1216 | if (need_template ^ is_template) | |
1217 | return false; | |
1218 | else if (is_template) | |
1219 | { | |
1220 | tree friend_parms; | |
1221 | /* If both are templates, check the name of the two | |
1222 | TEMPLATE_DECL's first because is_friend didn't. */ | |
1223 | if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type)) | |
be93747e | 1224 | != DECL_NAME (friend_decl)) |
b939a023 KL |
1225 | return false; |
1226 | ||
1227 | /* Now check template parameter list. */ | |
1228 | friend_parms | |
be93747e | 1229 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl), |
b939a023 KL |
1230 | args, tf_none); |
1231 | return comp_template_parms | |
1232 | (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)), | |
1233 | friend_parms); | |
1234 | } | |
1235 | else | |
1236 | return (DECL_NAME (decl) | |
be93747e | 1237 | == DECL_NAME (friend_decl)); |
d43f603d KL |
1238 | } |
1239 | } | |
b939a023 | 1240 | return false; |
d43f603d KL |
1241 | } |
1242 | ||
75650646 | 1243 | /* Register the specialization SPEC as a specialization of TMPL with |
d63d5d0c ILT |
1244 | the indicated ARGS. IS_FRIEND indicates whether the specialization |
1245 | is actually just a friend declaration. Returns SPEC, or an | |
1246 | equivalent prior declaration, if available. */ | |
75650646 | 1247 | |
36a117a5 | 1248 | static tree |
2b110bfc JM |
1249 | register_specialization (tree spec, tree tmpl, tree args, bool is_friend, |
1250 | hashval_t hash) | |
75650646 | 1251 | { |
c7222c02 | 1252 | tree fn; |
2b110bfc JM |
1253 | spec_entry **slot = NULL; |
1254 | spec_entry elt; | |
75650646 | 1255 | |
2b110bfc | 1256 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec)); |
75650646 | 1257 | |
c8094d83 | 1258 | if (TREE_CODE (spec) == FUNCTION_DECL |
36a117a5 MM |
1259 | && uses_template_parms (DECL_TI_ARGS (spec))) |
1260 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
1261 | register it; we want the corresponding TEMPLATE_DECL instead. | |
1262 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
1263 | the more obvious `uses_template_parms (spec)' to avoid problems | |
1264 | with default function arguments. In particular, given | |
1265 | something like this: | |
1266 | ||
0cbd7506 | 1267 | template <class T> void f(T t1, T t = T()) |
36a117a5 MM |
1268 | |
1269 | the default argument expression is not substituted for in an | |
1270 | instantiation unless and until it is actually needed. */ | |
1271 | return spec; | |
28ba38e3 | 1272 | |
2b110bfc JM |
1273 | if (optimize_specialization_lookup_p (tmpl)) |
1274 | /* We don't put these specializations in the hash table, but we might | |
1275 | want to give an error about a mismatch. */ | |
1276 | fn = retrieve_specialization (tmpl, args, 0); | |
1277 | else | |
1278 | { | |
1279 | elt.tmpl = tmpl; | |
1280 | elt.args = args; | |
1281 | elt.spec = spec; | |
1282 | ||
1283 | if (hash == 0) | |
1284 | hash = hash_specialization (&elt); | |
1285 | ||
1286 | slot = (spec_entry **) | |
1287 | htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT); | |
1288 | if (*slot) | |
1289 | fn = (*slot)->spec; | |
1290 | else | |
1291 | fn = NULL_TREE; | |
1292 | } | |
1293 | ||
c7222c02 MM |
1294 | /* We can sometimes try to re-register a specialization that we've |
1295 | already got. In particular, regenerate_decl_from_template calls | |
1296 | duplicate_decls which will update the specialization list. But, | |
1297 | we'll still get called again here anyhow. It's more convenient | |
1298 | to simply allow this than to try to prevent it. */ | |
1299 | if (fn == spec) | |
1300 | return spec; | |
1301 | else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
22e9174f | 1302 | { |
c7222c02 | 1303 | if (DECL_TEMPLATE_INSTANTIATION (fn)) |
22e9174f | 1304 | { |
3146f36f | 1305 | if (DECL_ODR_USED (fn) |
c7222c02 | 1306 | || DECL_EXPLICIT_INSTANTIATION (fn)) |
22e9174f | 1307 | { |
c7222c02 MM |
1308 | error ("specialization of %qD after instantiation", |
1309 | fn); | |
99f4234a | 1310 | return error_mark_node; |
22e9174f | 1311 | } |
c7222c02 | 1312 | else |
b1cc95ce | 1313 | { |
a39041fa | 1314 | tree clone; |
c7222c02 MM |
1315 | /* This situation should occur only if the first |
1316 | specialization is an implicit instantiation, the | |
1317 | second is an explicit specialization, and the | |
1318 | implicit instantiation has not yet been used. That | |
1319 | situation can occur if we have implicitly | |
1320 | instantiated a member function and then specialized | |
1321 | it later. | |
c8094d83 | 1322 | |
c7222c02 MM |
1323 | We can also wind up here if a friend declaration that |
1324 | looked like an instantiation turns out to be a | |
1325 | specialization: | |
c8094d83 | 1326 | |
c7222c02 MM |
1327 | template <class T> void foo(T); |
1328 | class S { friend void foo<>(int) }; | |
c8094d83 MS |
1329 | template <> void foo(int); |
1330 | ||
c7222c02 MM |
1331 | We transform the existing DECL in place so that any |
1332 | pointers to it become pointers to the updated | |
1333 | declaration. | |
1334 | ||
1335 | If there was a definition for the template, but not | |
1336 | for the specialization, we want this to look as if | |
1337 | there were no definition, and vice versa. */ | |
1338 | DECL_INITIAL (fn) = NULL_TREE; | |
d63d5d0c | 1339 | duplicate_decls (spec, fn, is_friend); |
a39041fa | 1340 | /* The call to duplicate_decls will have applied |
3db45ab5 | 1341 | [temp.expl.spec]: |
a39041fa | 1342 | |
3db45ab5 | 1343 | An explicit specialization of a function template |
a39041fa | 1344 | is inline only if it is explicitly declared to be, |
bcf51da2 | 1345 | and independently of whether its function template |
a39041fa MM |
1346 | is. |
1347 | ||
1348 | to the primary function; now copy the inline bits to | |
3db45ab5 | 1349 | the various clones. */ |
a39041fa | 1350 | FOR_EACH_CLONE (clone, fn) |
99355518 JH |
1351 | DECL_DECLARED_INLINE_P (clone) |
1352 | = DECL_DECLARED_INLINE_P (fn); | |
9c12301f | 1353 | check_specialization_namespace (fn); |
c8094d83 | 1354 | |
b1cc95ce MM |
1355 | return fn; |
1356 | } | |
22e9174f | 1357 | } |
c7222c02 MM |
1358 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) |
1359 | { | |
d63d5d0c | 1360 | if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec)) |
c7222c02 MM |
1361 | /* Dup decl failed, but this is a new definition. Set the |
1362 | line number so any errors match this new | |
1363 | definition. */ | |
1364 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); | |
c8094d83 | 1365 | |
c7222c02 MM |
1366 | return fn; |
1367 | } | |
b1cc95ce | 1368 | } |
62e3e13a JM |
1369 | else if (fn) |
1370 | return duplicate_decls (spec, fn, is_friend); | |
b1cc95ce MM |
1371 | |
1372 | /* A specialization must be declared in the same namespace as the | |
1373 | template it is specializing. */ | |
1374 | if (DECL_TEMPLATE_SPECIALIZATION (spec) | |
1375 | && !check_specialization_namespace (tmpl)) | |
db10c6ec | 1376 | DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl); |
75650646 | 1377 | |
c7222c02 | 1378 | if (!optimize_specialization_lookup_p (tmpl)) |
2b110bfc JM |
1379 | { |
1380 | gcc_assert (tmpl && args && spec); | |
1381 | *slot = GGC_NEW (spec_entry); | |
1382 | **slot = elt; | |
1383 | if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec) | |
1384 | && PRIMARY_TEMPLATE_P (tmpl) | |
1385 | && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE) | |
1386 | /* TMPL is a forward declaration of a template function; keep a list | |
1387 | of all specializations in case we need to reassign them to a friend | |
1388 | template later in tsubst_friend_function. */ | |
1389 | DECL_TEMPLATE_INSTANTIATIONS (tmpl) | |
1390 | = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl)); | |
1391 | } | |
36a117a5 MM |
1392 | |
1393 | return spec; | |
1394 | } | |
1395 | ||
2b110bfc JM |
1396 | /* Returns true iff two spec_entry nodes are equivalent. Only compares the |
1397 | TMPL and ARGS members, ignores SPEC. */ | |
1398 | ||
1399 | static int | |
1400 | eq_specializations (const void *p1, const void *p2) | |
1401 | { | |
1402 | const spec_entry *e1 = (const spec_entry *)p1; | |
1403 | const spec_entry *e2 = (const spec_entry *)p2; | |
1404 | ||
1405 | return (e1->tmpl == e2->tmpl | |
1406 | && comp_template_args (e1->args, e2->args)); | |
1407 | } | |
1408 | ||
1409 | /* Returns a hash for a template TMPL and template arguments ARGS. */ | |
1410 | ||
1411 | static hashval_t | |
1412 | hash_tmpl_and_args (tree tmpl, tree args) | |
1413 | { | |
1414 | hashval_t val = DECL_UID (tmpl); | |
1415 | return iterative_hash_template_arg (args, val); | |
1416 | } | |
1417 | ||
1418 | /* Returns a hash for a spec_entry node based on the TMPL and ARGS members, | |
1419 | ignoring SPEC. */ | |
1420 | ||
1421 | static hashval_t | |
1422 | hash_specialization (const void *p) | |
1423 | { | |
1424 | const spec_entry *e = (const spec_entry *)p; | |
1425 | return hash_tmpl_and_args (e->tmpl, e->args); | |
1426 | } | |
1427 | ||
1428 | /* Recursively calculate a hash value for a template argument ARG, for use | |
1429 | in the hash tables of template specializations. */ | |
1430 | ||
1431 | static hashval_t | |
1432 | iterative_hash_template_arg (tree arg, hashval_t val) | |
1433 | { | |
1434 | unsigned HOST_WIDE_INT i; | |
1435 | enum tree_code code; | |
1436 | char tclass; | |
1437 | ||
1438 | if (arg == NULL_TREE) | |
1439 | return iterative_hash_object (arg, val); | |
1440 | ||
1441 | if (!TYPE_P (arg)) | |
1442 | STRIP_NOPS (arg); | |
1443 | ||
1444 | code = TREE_CODE (arg); | |
1445 | tclass = TREE_CODE_CLASS (code); | |
1446 | ||
1447 | val = iterative_hash_object (code, val); | |
1448 | ||
1449 | switch (code) | |
1450 | { | |
1451 | case ERROR_MARK: | |
1452 | return val; | |
1453 | ||
1454 | case IDENTIFIER_NODE: | |
1455 | return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val); | |
1456 | ||
1457 | case TREE_VEC: | |
1458 | { | |
1459 | int i, len = TREE_VEC_LENGTH (arg); | |
1460 | for (i = 0; i < len; ++i) | |
1461 | val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val); | |
1462 | return val; | |
1463 | } | |
1464 | ||
1465 | case TYPE_PACK_EXPANSION: | |
1466 | case EXPR_PACK_EXPANSION: | |
1467 | return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val); | |
1468 | ||
1469 | case ARGUMENT_PACK_SELECT: | |
1470 | /* We can get one of these when re-hashing a previous entry in the middle | |
1471 | of substituting into a pack expansion. Just look through it... */ | |
1472 | arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg); | |
1473 | /* ...and fall through. */ | |
1474 | case TYPE_ARGUMENT_PACK: | |
1475 | case NONTYPE_ARGUMENT_PACK: | |
1476 | return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val); | |
1477 | ||
1478 | case TREE_LIST: | |
1479 | for (; arg; arg = TREE_CHAIN (arg)) | |
1480 | val = iterative_hash_template_arg (TREE_VALUE (arg), val); | |
1481 | return val; | |
1482 | ||
1483 | case OVERLOAD: | |
1484 | for (; arg; arg = OVL_CHAIN (arg)) | |
1485 | val = iterative_hash_template_arg (OVL_FUNCTION (arg), val); | |
1486 | return val; | |
1487 | ||
1488 | case CONSTRUCTOR: | |
1489 | { | |
1490 | tree field, value; | |
1491 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value) | |
1492 | { | |
1493 | val = iterative_hash_template_arg (field, val); | |
1494 | val = iterative_hash_template_arg (value, val); | |
1495 | } | |
1496 | return val; | |
1497 | } | |
1498 | ||
1499 | case PARM_DECL: | |
ad909c97 | 1500 | val = iterative_hash_object (DECL_PARM_INDEX (arg), val); |
2b110bfc JM |
1501 | return iterative_hash_template_arg (TREE_TYPE (arg), val); |
1502 | ||
1503 | case TARGET_EXPR: | |
1504 | return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val); | |
1505 | ||
1506 | case PTRMEM_CST: | |
1507 | val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val); | |
1508 | return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val); | |
1509 | ||
1510 | case TEMPLATE_PARM_INDEX: | |
1511 | val = iterative_hash_template_arg | |
1512 | (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val); | |
1513 | val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val); | |
1514 | return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val); | |
1515 | ||
1516 | case TRAIT_EXPR: | |
1517 | val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val); | |
1518 | val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val); | |
1519 | return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val); | |
1520 | ||
1521 | case BASELINK: | |
1522 | val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)), | |
1523 | val); | |
1524 | return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)), | |
1525 | val); | |
1526 | ||
1527 | case MODOP_EXPR: | |
1528 | val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val); | |
1529 | code = TREE_CODE (TREE_OPERAND (arg, 1)); | |
1530 | val = iterative_hash_object (code, val); | |
1531 | return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val); | |
1532 | ||
1533 | default: | |
1534 | switch (tclass) | |
1535 | { | |
1536 | case tcc_type: | |
1537 | if (TYPE_CANONICAL (arg)) | |
1538 | return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)), | |
1539 | val); | |
1540 | else if (TREE_CODE (arg) == DECLTYPE_TYPE) | |
1541 | return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val); | |
1542 | /* Otherwise just compare the types during lookup. */ | |
1543 | return val; | |
1544 | ||
1545 | case tcc_declaration: | |
1546 | case tcc_constant: | |
1547 | return iterative_hash_expr (arg, val); | |
1548 | ||
1549 | default: | |
1550 | gcc_assert (IS_EXPR_CODE_CLASS (tclass)); | |
1551 | { | |
1552 | unsigned n = TREE_OPERAND_LENGTH (arg); | |
1553 | for (i = 0; i < n; ++i) | |
1554 | val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val); | |
1555 | return val; | |
1556 | } | |
1557 | } | |
1558 | } | |
1559 | gcc_unreachable (); | |
1560 | return 0; | |
1561 | } | |
1562 | ||
36a117a5 | 1563 | /* Unregister the specialization SPEC as a specialization of TMPL. |
b3445994 | 1564 | Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true |
2b110bfc JM |
1565 | if the SPEC was listed as a specialization of TMPL. |
1566 | ||
1567 | Note that SPEC has been ggc_freed, so we can't look inside it. */ | |
36a117a5 | 1568 | |
b3445994 | 1569 | bool |
2b110bfc | 1570 | reregister_specialization (tree spec, tree tinfo, tree new_spec) |
36a117a5 | 1571 | { |
2b110bfc JM |
1572 | spec_entry **slot; |
1573 | spec_entry elt; | |
36a117a5 | 1574 | |
2b110bfc JM |
1575 | elt.tmpl = most_general_template (TI_TEMPLATE (tinfo)); |
1576 | elt.args = TI_ARGS (tinfo); | |
1577 | elt.spec = NULL_TREE; | |
1578 | ||
1579 | slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT); | |
1580 | if (*slot) | |
1581 | { | |
1582 | gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec); | |
1583 | gcc_assert (new_spec != NULL_TREE); | |
1584 | (*slot)->spec = new_spec; | |
1585 | return 1; | |
1586 | } | |
36a117a5 MM |
1587 | |
1588 | return 0; | |
75650646 MM |
1589 | } |
1590 | ||
a723baf1 MM |
1591 | /* Compare an entry in the local specializations hash table P1 (which |
1592 | is really a pointer to a TREE_LIST) with P2 (which is really a | |
1593 | DECL). */ | |
1594 | ||
1595 | static int | |
1596 | eq_local_specializations (const void *p1, const void *p2) | |
1597 | { | |
741ac903 | 1598 | return TREE_VALUE ((const_tree) p1) == (const_tree) p2; |
a723baf1 MM |
1599 | } |
1600 | ||
69f794a7 MM |
1601 | /* Hash P1, an entry in the local specializations table. */ |
1602 | ||
1603 | static hashval_t | |
1604 | hash_local_specialization (const void* p1) | |
1605 | { | |
741ac903 | 1606 | return htab_hash_pointer (TREE_VALUE ((const_tree) p1)); |
69f794a7 MM |
1607 | } |
1608 | ||
6dfbb909 MM |
1609 | /* Like register_specialization, but for local declarations. We are |
1610 | registering SPEC, an instantiation of TMPL. */ | |
9188c363 | 1611 | |
414ea4aa | 1612 | static void |
3a978d72 | 1613 | register_local_specialization (tree spec, tree tmpl) |
9188c363 | 1614 | { |
6dfbb909 MM |
1615 | void **slot; |
1616 | ||
c8094d83 | 1617 | slot = htab_find_slot_with_hash (local_specializations, tmpl, |
69f794a7 | 1618 | htab_hash_pointer (tmpl), INSERT); |
a723baf1 | 1619 | *slot = build_tree_list (spec, tmpl); |
9188c363 MM |
1620 | } |
1621 | ||
9ba7a2f2 MM |
1622 | /* TYPE is a class type. Returns true if TYPE is an explicitly |
1623 | specialized class. */ | |
1624 | ||
1625 | bool | |
1626 | explicit_class_specialization_p (tree type) | |
1627 | { | |
1628 | if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
1629 | return false; | |
1630 | return !uses_template_parms (CLASSTYPE_TI_ARGS (type)); | |
1631 | } | |
1632 | ||
e1467ff2 MM |
1633 | /* Print the list of candidate FNS in an error message. */ |
1634 | ||
104bf76a | 1635 | void |
3a978d72 | 1636 | print_candidates (tree fns) |
e1467ff2 MM |
1637 | { |
1638 | tree fn; | |
1639 | ||
d8e178a0 | 1640 | const char *str = "candidates are:"; |
e1467ff2 MM |
1641 | |
1642 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
1643 | { | |
d6479fe7 MM |
1644 | tree f; |
1645 | ||
1646 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
dee15844 | 1647 | error ("%s %+#D", str, OVL_CURRENT (f)); |
e1467ff2 MM |
1648 | str = " "; |
1649 | } | |
1650 | } | |
1651 | ||
75650646 | 1652 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 1653 | which can be specialized to match the indicated DECL with the |
03017874 MM |
1654 | explicit template args given in TEMPLATE_ID. The DECL may be |
1655 | NULL_TREE if none is available. In that case, the functions in | |
1656 | TEMPLATE_ID are non-members. | |
1657 | ||
838dfd8a | 1658 | If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a |
03017874 MM |
1659 | specialization of a member template. |
1660 | ||
5fe7b654 GB |
1661 | The TEMPLATE_COUNT is the number of references to qualifying |
1662 | template classes that appeared in the name of the function. See | |
1663 | check_explicit_specialization for a more accurate description. | |
1664 | ||
29a1da1c MM |
1665 | TSK indicates what kind of template declaration (if any) is being |
1666 | declared. TSK_TEMPLATE indicates that the declaration given by | |
1667 | DECL, though a FUNCTION_DECL, has template parameters, and is | |
1668 | therefore a template function. | |
1669 | ||
03017874 MM |
1670 | The template args (those explicitly specified and those deduced) |
1671 | are output in a newly created vector *TARGS_OUT. | |
1672 | ||
1673 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 1674 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 1675 | |
e9659ab0 | 1676 | static tree |
c8094d83 | 1677 | determine_specialization (tree template_id, |
0cbd7506 MS |
1678 | tree decl, |
1679 | tree* targs_out, | |
5fe7b654 | 1680 | int need_member_template, |
29a1da1c MM |
1681 | int template_count, |
1682 | tmpl_spec_kind tsk) | |
386b8a85 | 1683 | { |
03017874 MM |
1684 | tree fns; |
1685 | tree targs; | |
1686 | tree explicit_targs; | |
1687 | tree candidates = NULL_TREE; | |
7ca383e6 MM |
1688 | /* A TREE_LIST of templates of which DECL may be a specialization. |
1689 | The TREE_VALUE of each node is a TEMPLATE_DECL. The | |
1690 | corresponding TREE_PURPOSE is the set of template arguments that, | |
1691 | when used to instantiate the template, would produce a function | |
1692 | with the signature of DECL. */ | |
03017874 | 1693 | tree templates = NULL_TREE; |
5fe7b654 GB |
1694 | int header_count; |
1695 | struct cp_binding_level *b; | |
386b8a85 | 1696 | |
e1467ff2 MM |
1697 | *targs_out = NULL_TREE; |
1698 | ||
728da672 | 1699 | if (template_id == error_mark_node || decl == error_mark_node) |
f2e48b67 BK |
1700 | return error_mark_node; |
1701 | ||
1702 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 1703 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 1704 | |
27fafc8d JM |
1705 | if (fns == error_mark_node) |
1706 | return error_mark_node; | |
1707 | ||
c6002625 | 1708 | /* Check for baselinks. */ |
91e490ab | 1709 | if (BASELINK_P (fns)) |
da15dae6 | 1710 | fns = BASELINK_FUNCTIONS (fns); |
386b8a85 | 1711 | |
91e490ab MM |
1712 | if (!is_overloaded_fn (fns)) |
1713 | { | |
0f51ccfc | 1714 | error ("%qD is not a function template", fns); |
91e490ab MM |
1715 | return error_mark_node; |
1716 | } | |
1717 | ||
5fe7b654 GB |
1718 | /* Count the number of template headers specified for this |
1719 | specialization. */ | |
1720 | header_count = 0; | |
c8094d83 | 1721 | for (b = current_binding_level; |
98f99d7f | 1722 | b->kind == sk_template_parms; |
5fe7b654 GB |
1723 | b = b->level_chain) |
1724 | ++header_count; | |
1725 | ||
2c73f9f5 | 1726 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 1727 | { |
9dfce8fd | 1728 | tree fn = OVL_CURRENT (fns); |
03017874 MM |
1729 | |
1730 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
d955f6ea KL |
1731 | { |
1732 | tree decl_arg_types; | |
0da99d4e | 1733 | tree fn_arg_types; |
d955f6ea | 1734 | |
5fe7b654 GB |
1735 | /* In case of explicit specialization, we need to check if |
1736 | the number of template headers appearing in the specialization | |
1737 | is correct. This is usually done in check_explicit_specialization, | |
1738 | but the check done there cannot be exhaustive when specializing | |
1739 | member functions. Consider the following code: | |
1740 | ||
1741 | template <> void A<int>::f(int); | |
1742 | template <> template <> void A<int>::f(int); | |
1743 | ||
1744 | Assuming that A<int> is not itself an explicit specialization | |
1745 | already, the first line specializes "f" which is a non-template | |
1746 | member function, whilst the second line specializes "f" which | |
1747 | is a template member function. So both lines are syntactically | |
1748 | correct, and check_explicit_specialization does not reject | |
1749 | them. | |
c8094d83 | 1750 | |
5fe7b654 GB |
1751 | Here, we can do better, as we are matching the specialization |
1752 | against the declarations. We count the number of template | |
1753 | headers, and we check if they match TEMPLATE_COUNT + 1 | |
1754 | (TEMPLATE_COUNT is the number of qualifying template classes, | |
1755 | plus there must be another header for the member template | |
1756 | itself). | |
c8094d83 | 1757 | |
5fe7b654 GB |
1758 | Notice that if header_count is zero, this is not a |
1759 | specialization but rather a template instantiation, so there | |
1760 | is no check we can perform here. */ | |
1761 | if (header_count && header_count != template_count + 1) | |
1762 | continue; | |
1763 | ||
98f99d7f MM |
1764 | /* Check that the number of template arguments at the |
1765 | innermost level for DECL is the same as for FN. */ | |
1766 | if (current_binding_level->kind == sk_template_parms | |
1767 | && !current_binding_level->explicit_spec_p | |
1768 | && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn)) | |
3db45ab5 | 1769 | != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS |
916b63c3 | 1770 | (current_template_parms)))) |
98f99d7f | 1771 | continue; |
c8094d83 | 1772 | |
ba139ba8 MM |
1773 | /* DECL might be a specialization of FN. */ |
1774 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1775 | fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
1776 | ||
1777 | /* For a non-static member function, we need to make sure | |
1778 | that the const qualification is the same. Since | |
1779 | get_bindings does not try to merge the "this" parameter, | |
1780 | we must do the comparison explicitly. */ | |
1781 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) | |
1782 | && !same_type_p (TREE_VALUE (fn_arg_types), | |
1783 | TREE_VALUE (decl_arg_types))) | |
1784 | continue; | |
1785 | ||
1786 | /* Skip the "this" parameter and, for constructors of | |
1787 | classes with virtual bases, the VTT parameter. A | |
1788 | full specialization of a constructor will have a VTT | |
1789 | parameter, but a template never will. */ | |
1790 | decl_arg_types | |
1791 | = skip_artificial_parms_for (decl, decl_arg_types); | |
1792 | fn_arg_types | |
1793 | = skip_artificial_parms_for (fn, fn_arg_types); | |
1794 | ||
1795 | /* Check that the number of function parameters matches. | |
1796 | For example, | |
1797 | template <class T> void f(int i = 0); | |
1798 | template <> void f<int>(); | |
1799 | The specialization f<int> is invalid but is not caught | |
1800 | by get_bindings below. */ | |
1801 | if (list_length (fn_arg_types) != list_length (decl_arg_types)) | |
1802 | continue; | |
1803 | ||
29a1da1c MM |
1804 | /* Function templates cannot be specializations; there are |
1805 | no partial specializations of functions. Therefore, if | |
1806 | the type of DECL does not match FN, there is no | |
1807 | match. */ | |
54062fc0 PC |
1808 | if (tsk == tsk_template) |
1809 | { | |
1810 | if (compparms (fn_arg_types, decl_arg_types)) | |
1811 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
1812 | continue; | |
1813 | } | |
29a1da1c | 1814 | |
d955f6ea KL |
1815 | /* See whether this function might be a specialization of this |
1816 | template. */ | |
a34d3336 | 1817 | targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true); |
d955f6ea KL |
1818 | |
1819 | if (!targs) | |
1820 | /* We cannot deduce template arguments that when used to | |
1821 | specialize TMPL will produce DECL. */ | |
1822 | continue; | |
1823 | ||
1824 | /* Save this template, and the arguments deduced. */ | |
1825 | templates = tree_cons (targs, fn, templates); | |
1826 | } | |
03017874 MM |
1827 | else if (need_member_template) |
1828 | /* FN is an ordinary member function, and we need a | |
1829 | specialization of a member template. */ | |
d955f6ea | 1830 | ; |
03017874 MM |
1831 | else if (TREE_CODE (fn) != FUNCTION_DECL) |
1832 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1833 | cases. */ | |
d955f6ea | 1834 | ; |
03017874 MM |
1835 | else if (!DECL_FUNCTION_MEMBER_P (fn)) |
1836 | /* This is just an ordinary non-member function. Nothing can | |
1837 | be a specialization of that. */ | |
d955f6ea | 1838 | ; |
3b82c249 KL |
1839 | else if (DECL_ARTIFICIAL (fn)) |
1840 | /* Cannot specialize functions that are created implicitly. */ | |
d955f6ea | 1841 | ; |
75650646 | 1842 | else |
03017874 MM |
1843 | { |
1844 | tree decl_arg_types; | |
386b8a85 | 1845 | |
03017874 MM |
1846 | /* This is an ordinary member function. However, since |
1847 | we're here, we can assume it's enclosing class is a | |
1848 | template class. For example, | |
c8094d83 | 1849 | |
03017874 MM |
1850 | template <typename T> struct S { void f(); }; |
1851 | template <> void S<int>::f() {} | |
1852 | ||
1853 | Here, S<int>::f is a non-template, but S<int> is a | |
1854 | template class. If FN has the same type as DECL, we | |
1855 | might be in business. */ | |
f5d47abd KL |
1856 | |
1857 | if (!DECL_TEMPLATE_INFO (fn)) | |
1858 | /* Its enclosing class is an explicit specialization | |
1859 | of a template class. This is not a candidate. */ | |
1860 | continue; | |
1861 | ||
03017874 MM |
1862 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
1863 | TREE_TYPE (TREE_TYPE (fn)))) | |
1864 | /* The return types differ. */ | |
1865 | continue; | |
1866 | ||
1867 | /* Adjust the type of DECL in case FN is a static member. */ | |
1868 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
c8094d83 | 1869 | if (DECL_STATIC_FUNCTION_P (fn) |
03017874 MM |
1870 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1871 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1872 | ||
c8094d83 | 1873 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), |
03017874 MM |
1874 | decl_arg_types)) |
1875 | /* They match! */ | |
1876 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
03017874 | 1877 | } |
386b8a85 | 1878 | } |
03017874 | 1879 | |
bf8f3f93 | 1880 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1881 | { |
03017874 | 1882 | /* We have: |
c8094d83 | 1883 | |
03017874 MM |
1884 | [temp.expl.spec] |
1885 | ||
1886 | It is possible for a specialization with a given function | |
1887 | signature to be instantiated from more than one function | |
1888 | template. In such cases, explicit specification of the | |
1889 | template arguments must be used to uniquely identify the | |
1890 | function template specialization being specialized. | |
1891 | ||
1892 | Note that here, there's no suggestion that we're supposed to | |
1893 | determine which of the candidate templates is most | |
1894 | specialized. However, we, also have: | |
1895 | ||
1896 | [temp.func.order] | |
1897 | ||
1898 | Partial ordering of overloaded function template | |
1899 | declarations is used in the following contexts to select | |
1900 | the function template to which a function template | |
c8094d83 | 1901 | specialization refers: |
03017874 | 1902 | |
0cbd7506 | 1903 | -- when an explicit specialization refers to a function |
c8094d83 | 1904 | template. |
03017874 MM |
1905 | |
1906 | So, we do use the partial ordering rules, at least for now. | |
0e339752 | 1907 | This extension can only serve to make invalid programs valid, |
03017874 MM |
1908 | so it's safe. And, there is strong anecdotal evidence that |
1909 | the committee intended the partial ordering rules to apply; | |
3b426391 | 1910 | the EDG front end has that behavior, and John Spicer claims |
03017874 MM |
1911 | that the committee simply forgot to delete the wording in |
1912 | [temp.expl.spec]. */ | |
7ca383e6 MM |
1913 | tree tmpl = most_specialized_instantiation (templates); |
1914 | if (tmpl != error_mark_node) | |
1915 | { | |
1916 | templates = tmpl; | |
1917 | TREE_CHAIN (templates) = NULL_TREE; | |
1918 | } | |
e1467ff2 MM |
1919 | } |
1920 | ||
03017874 | 1921 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1922 | { |
dee15844 JM |
1923 | error ("template-id %qD for %q+D does not match any template " |
1924 | "declaration", template_id, decl); | |
03017874 | 1925 | return error_mark_node; |
386b8a85 | 1926 | } |
03017874 | 1927 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1928 | || (candidates && TREE_CHAIN (candidates)) |
1929 | || (templates && candidates)) | |
386b8a85 | 1930 | { |
dee15844 JM |
1931 | error ("ambiguous template specialization %qD for %q+D", |
1932 | template_id, decl); | |
bf8f3f93 MM |
1933 | chainon (candidates, templates); |
1934 | print_candidates (candidates); | |
03017874 | 1935 | return error_mark_node; |
386b8a85 JM |
1936 | } |
1937 | ||
c6002625 | 1938 | /* We have one, and exactly one, match. */ |
03017874 MM |
1939 | if (candidates) |
1940 | { | |
29a1da1c | 1941 | tree fn = TREE_VALUE (candidates); |
62e3e13a JM |
1942 | *targs_out = copy_node (DECL_TI_ARGS (fn)); |
1943 | /* DECL is a re-declaration or partial instantiation of a template | |
1944 | function. */ | |
29a1da1c MM |
1945 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
1946 | return fn; | |
03017874 MM |
1947 | /* It was a specialization of an ordinary member function in a |
1948 | template class. */ | |
29a1da1c | 1949 | return DECL_TI_TEMPLATE (fn); |
03017874 MM |
1950 | } |
1951 | ||
1952 | /* It was a specialization of a template. */ | |
17aec3eb | 1953 | targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); |
03017874 MM |
1954 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) |
1955 | { | |
1956 | *targs_out = copy_node (targs); | |
c8094d83 | 1957 | SET_TMPL_ARGS_LEVEL (*targs_out, |
03017874 MM |
1958 | TMPL_ARGS_DEPTH (*targs_out), |
1959 | TREE_PURPOSE (templates)); | |
1960 | } | |
1961 | else | |
1962 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1963 | return TREE_VALUE (templates); |
8d08fdba | 1964 | } |
8afa707f MM |
1965 | |
1966 | /* Returns a chain of parameter types, exactly like the SPEC_TYPES, | |
1967 | but with the default argument values filled in from those in the | |
1968 | TMPL_TYPES. */ | |
c8094d83 | 1969 | |
8afa707f | 1970 | static tree |
3a978d72 NN |
1971 | copy_default_args_to_explicit_spec_1 (tree spec_types, |
1972 | tree tmpl_types) | |
8afa707f MM |
1973 | { |
1974 | tree new_spec_types; | |
1975 | ||
1976 | if (!spec_types) | |
1977 | return NULL_TREE; | |
1978 | ||
1979 | if (spec_types == void_list_node) | |
1980 | return void_list_node; | |
1981 | ||
1982 | /* Substitute into the rest of the list. */ | |
1983 | new_spec_types = | |
1984 | copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), | |
1985 | TREE_CHAIN (tmpl_types)); | |
c8094d83 | 1986 | |
8afa707f MM |
1987 | /* Add the default argument for this parameter. */ |
1988 | return hash_tree_cons (TREE_PURPOSE (tmpl_types), | |
1989 | TREE_VALUE (spec_types), | |
1990 | new_spec_types); | |
1991 | } | |
1992 | ||
1993 | /* DECL is an explicit specialization. Replicate default arguments | |
1994 | from the template it specializes. (That way, code like: | |
1995 | ||
1996 | template <class T> void f(T = 3); | |
1997 | template <> void f(double); | |
c8094d83 | 1998 | void g () { f (); } |
8afa707f MM |
1999 | |
2000 | works, as required.) An alternative approach would be to look up | |
2001 | the correct default arguments at the call-site, but this approach | |
2002 | is consistent with how implicit instantiations are handled. */ | |
2003 | ||
2004 | static void | |
3a978d72 | 2005 | copy_default_args_to_explicit_spec (tree decl) |
8afa707f MM |
2006 | { |
2007 | tree tmpl; | |
2008 | tree spec_types; | |
2009 | tree tmpl_types; | |
2010 | tree new_spec_types; | |
2011 | tree old_type; | |
2012 | tree new_type; | |
2013 | tree t; | |
08c2df0f NS |
2014 | tree object_type = NULL_TREE; |
2015 | tree in_charge = NULL_TREE; | |
e0fff4b3 | 2016 | tree vtt = NULL_TREE; |
8afa707f MM |
2017 | |
2018 | /* See if there's anything we need to do. */ | |
2019 | tmpl = DECL_TI_TEMPLATE (decl); | |
2020 | tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); | |
2021 | for (t = tmpl_types; t; t = TREE_CHAIN (t)) | |
2022 | if (TREE_PURPOSE (t)) | |
2023 | break; | |
2024 | if (!t) | |
2025 | return; | |
2026 | ||
2027 | old_type = TREE_TYPE (decl); | |
2028 | spec_types = TYPE_ARG_TYPES (old_type); | |
c8094d83 | 2029 | |
8afa707f MM |
2030 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
2031 | { | |
08c2df0f | 2032 | /* Remove the this pointer, but remember the object's type for |
0cbd7506 | 2033 | CV quals. */ |
08c2df0f | 2034 | object_type = TREE_TYPE (TREE_VALUE (spec_types)); |
8afa707f MM |
2035 | spec_types = TREE_CHAIN (spec_types); |
2036 | tmpl_types = TREE_CHAIN (tmpl_types); | |
c8094d83 | 2037 | |
8afa707f | 2038 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) |
0cbd7506 MS |
2039 | { |
2040 | /* DECL may contain more parameters than TMPL due to the extra | |
2041 | in-charge parameter in constructors and destructors. */ | |
2042 | in_charge = spec_types; | |
08c2df0f NS |
2043 | spec_types = TREE_CHAIN (spec_types); |
2044 | } | |
e0fff4b3 JM |
2045 | if (DECL_HAS_VTT_PARM_P (decl)) |
2046 | { | |
2047 | vtt = spec_types; | |
2048 | spec_types = TREE_CHAIN (spec_types); | |
2049 | } | |
8afa707f MM |
2050 | } |
2051 | ||
2052 | /* Compute the merged default arguments. */ | |
c8094d83 | 2053 | new_spec_types = |
8afa707f MM |
2054 | copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); |
2055 | ||
08c2df0f NS |
2056 | /* Compute the new FUNCTION_TYPE. */ |
2057 | if (object_type) | |
8afa707f | 2058 | { |
e0fff4b3 | 2059 | if (vtt) |
0cbd7506 MS |
2060 | new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), |
2061 | TREE_VALUE (vtt), | |
2062 | new_spec_types); | |
e0fff4b3 | 2063 | |
08c2df0f | 2064 | if (in_charge) |
0cbd7506 MS |
2065 | /* Put the in-charge parameter back. */ |
2066 | new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), | |
2067 | TREE_VALUE (in_charge), | |
2068 | new_spec_types); | |
08c2df0f | 2069 | |
43dc123f MM |
2070 | new_type = build_method_type_directly (object_type, |
2071 | TREE_TYPE (old_type), | |
2072 | new_spec_types); | |
8afa707f | 2073 | } |
8afa707f MM |
2074 | else |
2075 | new_type = build_function_type (TREE_TYPE (old_type), | |
08c2df0f | 2076 | new_spec_types); |
e9525111 MM |
2077 | new_type = cp_build_type_attribute_variant (new_type, |
2078 | TYPE_ATTRIBUTES (old_type)); | |
8afa707f MM |
2079 | new_type = build_exception_variant (new_type, |
2080 | TYPE_RAISES_EXCEPTIONS (old_type)); | |
2081 | TREE_TYPE (decl) = new_type; | |
2082 | } | |
2083 | ||
386b8a85 | 2084 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
2085 | DECLARATOR, and in DECL, is a specialization of a function |
2086 | template. We may also discover that the declaration is an explicit | |
2087 | instantiation at this point. | |
2088 | ||
e1467ff2 | 2089 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
2090 | instead if all goes well. Issues an error message if something is |
2091 | amiss. Returns error_mark_node if the error is not easily | |
2092 | recoverable. | |
c8094d83 MS |
2093 | |
2094 | FLAGS is a bitmask consisting of the following flags: | |
75650646 | 2095 | |
75650646 MM |
2096 | 2: The function has a definition. |
2097 | 4: The function is a friend. | |
75650646 MM |
2098 | |
2099 | The TEMPLATE_COUNT is the number of references to qualifying | |
2100 | template classes that appeared in the name of the function. For | |
2101 | example, in | |
2102 | ||
2103 | template <class T> struct S { void f(); }; | |
2104 | void S<int>::f(); | |
c8094d83 | 2105 | |
75650646 MM |
2106 | the TEMPLATE_COUNT would be 1. However, explicitly specialized |
2107 | classes are not counted in the TEMPLATE_COUNT, so that in | |
2108 | ||
2109 | template <class T> struct S {}; | |
2110 | template <> struct S<int> { void f(); } | |
36a117a5 | 2111 | template <> void S<int>::f(); |
75650646 MM |
2112 | |
2113 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
0e339752 | 2114 | invalid; there should be no template <>.) |
75650646 MM |
2115 | |
2116 | If the function is a specialization, it is marked as such via | |
2117 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
c8094d83 | 2118 | is set up correctly, and it is added to the list of specializations |
75650646 | 2119 | for that template. */ |
386b8a85 | 2120 | |
e1467ff2 | 2121 | tree |
c8094d83 | 2122 | check_explicit_specialization (tree declarator, |
0cbd7506 MS |
2123 | tree decl, |
2124 | int template_count, | |
2125 | int flags) | |
386b8a85 | 2126 | { |
75650646 MM |
2127 | int have_def = flags & 2; |
2128 | int is_friend = flags & 4; | |
2129 | int specialization = 0; | |
e1467ff2 | 2130 | int explicit_instantiation = 0; |
fd4de5ff | 2131 | int member_specialization = 0; |
75650646 MM |
2132 | tree ctype = DECL_CLASS_CONTEXT (decl); |
2133 | tree dname = DECL_NAME (decl); | |
74b846e0 | 2134 | tmpl_spec_kind tsk; |
386b8a85 | 2135 | |
f65b7de3 GB |
2136 | if (is_friend) |
2137 | { | |
2138 | if (!processing_specialization) | |
2139 | tsk = tsk_none; | |
2140 | else | |
2141 | tsk = tsk_excessive_parms; | |
2142 | } | |
2143 | else | |
2144 | tsk = current_tmpl_spec_kind (template_count); | |
75650646 | 2145 | |
74b846e0 MM |
2146 | switch (tsk) |
2147 | { | |
2148 | case tsk_none: | |
c8094d83 | 2149 | if (processing_specialization) |
75650646 | 2150 | { |
fd4de5ff MM |
2151 | specialization = 1; |
2152 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 2153 | } |
74b846e0 | 2154 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
386b8a85 | 2155 | { |
74b846e0 MM |
2156 | if (is_friend) |
2157 | /* This could be something like: | |
75650646 | 2158 | |
74b846e0 MM |
2159 | template <class T> void f(T); |
2160 | class S { friend void f<>(int); } */ | |
2161 | specialization = 1; | |
2162 | else | |
2163 | { | |
2164 | /* This case handles bogus declarations like template <> | |
2165 | template <class T> void f<int>(); */ | |
2166 | ||
0f51ccfc | 2167 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 2168 | declarator); |
74b846e0 MM |
2169 | return decl; |
2170 | } | |
2171 | } | |
2172 | break; | |
2173 | ||
2174 | case tsk_invalid_member_spec: | |
2175 | /* The error has already been reported in | |
2176 | check_specialization_scope. */ | |
2177 | return error_mark_node; | |
2178 | ||
2179 | case tsk_invalid_expl_inst: | |
33bd39a2 | 2180 | error ("template parameter list used in explicit instantiation"); |
74b846e0 MM |
2181 | |
2182 | /* Fall through. */ | |
2183 | ||
2184 | case tsk_expl_inst: | |
fd4de5ff | 2185 | if (have_def) |
33bd39a2 | 2186 | error ("definition provided for explicit instantiation"); |
c8094d83 | 2187 | |
fd4de5ff | 2188 | explicit_instantiation = 1; |
74b846e0 | 2189 | break; |
fd4de5ff | 2190 | |
74b846e0 | 2191 | case tsk_excessive_parms: |
f03adc6b GB |
2192 | case tsk_insufficient_parms: |
2193 | if (tsk == tsk_excessive_parms) | |
0cbd7506 | 2194 | error ("too many template parameter lists in declaration of %qD", |
f03adc6b GB |
2195 | decl); |
2196 | else if (template_header_count) | |
0f51ccfc | 2197 | error("too few template parameter lists in declaration of %qD", decl); |
f03adc6b | 2198 | else |
0f51ccfc | 2199 | error("explicit specialization of %qD must be introduced by " |
9e637a26 | 2200 | "%<template <>%>", decl); |
75650646 | 2201 | |
74b846e0 MM |
2202 | /* Fall through. */ |
2203 | case tsk_expl_spec: | |
2204 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
2205 | if (ctype) | |
2206 | member_specialization = 1; | |
2207 | else | |
2208 | specialization = 1; | |
2209 | break; | |
74b846e0 MM |
2210 | |
2211 | case tsk_template: | |
2212 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
c6f2ed0d | 2213 | { |
fd4de5ff MM |
2214 | /* This case handles bogus declarations like template <> |
2215 | template <class T> void f<int>(); */ | |
75650646 | 2216 | |
655dc6ee | 2217 | if (uses_template_parms (declarator)) |
0f51ccfc | 2218 | error ("function template partial specialization %qD " |
e1e93ad8 | 2219 | "is not allowed", declarator); |
655dc6ee | 2220 | else |
0f51ccfc | 2221 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 2222 | declarator); |
fd4de5ff | 2223 | return decl; |
386b8a85 | 2224 | } |
74b846e0 MM |
2225 | |
2226 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
2227 | /* This is a specialization of a member template, without | |
2228 | specialization the containing class. Something like: | |
2229 | ||
2230 | template <class T> struct S { | |
c8094d83 | 2231 | template <class U> void f (U); |
0cbd7506 | 2232 | }; |
74b846e0 | 2233 | template <> template <class U> void S<int>::f(U) {} |
c8094d83 | 2234 | |
74b846e0 MM |
2235 | That's a specialization -- but of the entire template. */ |
2236 | specialization = 1; | |
2237 | break; | |
2238 | ||
2239 | default: | |
315fb5db | 2240 | gcc_unreachable (); |
75650646 | 2241 | } |
386b8a85 | 2242 | |
670960ac JM |
2243 | if (specialization || member_specialization) |
2244 | { | |
2245 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
2246 | for (; t; t = TREE_CHAIN (t)) | |
2247 | if (TREE_PURPOSE (t)) | |
2248 | { | |
cbe5f3b3 MLI |
2249 | permerror (input_location, |
2250 | "default argument specified in explicit specialization"); | |
670960ac JM |
2251 | break; |
2252 | } | |
2253 | } | |
2254 | ||
e1467ff2 | 2255 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
2256 | { |
2257 | tree tmpl = NULL_TREE; | |
2258 | tree targs = NULL_TREE; | |
75650646 MM |
2259 | |
2260 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
2261 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
2262 | { | |
2263 | tree fns; | |
2264 | ||
50bc768d | 2265 | gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE); |
2f54a1db | 2266 | if (ctype) |
386b8a85 | 2267 | fns = dname; |
2f54a1db GB |
2268 | else |
2269 | { | |
2270 | /* If there is no class context, the explicit instantiation | |
0cbd7506 | 2271 | must be at namespace scope. */ |
50bc768d | 2272 | gcc_assert (DECL_NAMESPACE_SCOPE_P (decl)); |
2f54a1db GB |
2273 | |
2274 | /* Find the namespace binding, using the declaration | |
0cbd7506 | 2275 | context. */ |
ddf74938 | 2276 | fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname, |
664a90c0 | 2277 | false, true); |
97dc8e5b | 2278 | if (fns == error_mark_node || !is_overloaded_fn (fns)) |
4230cec2 NS |
2279 | { |
2280 | error ("%qD is not a template function", dname); | |
2281 | fns = error_mark_node; | |
2282 | } | |
664a90c0 JM |
2283 | else |
2284 | { | |
2285 | tree fn = OVL_CURRENT (fns); | |
ddf74938 | 2286 | if (!is_associated_namespace (CP_DECL_CONTEXT (decl), |
08d295c5 JM |
2287 | CP_DECL_CONTEXT (fn))) |
2288 | error ("%qD is not declared in %qD", | |
2289 | decl, current_namespace); | |
664a90c0 | 2290 | } |
2f54a1db | 2291 | } |
386b8a85 | 2292 | |
2f54a1db | 2293 | declarator = lookup_template_function (fns, NULL_TREE); |
386b8a85 JM |
2294 | } |
2295 | ||
f2e48b67 BK |
2296 | if (declarator == error_mark_node) |
2297 | return error_mark_node; | |
2298 | ||
75650646 MM |
2299 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
2300 | { | |
8ec2ac34 | 2301 | if (!explicit_instantiation) |
0e339752 | 2302 | /* A specialization in class scope. This is invalid, |
6c30752f MM |
2303 | but the error will already have been flagged by |
2304 | check_specialization_scope. */ | |
2305 | return error_mark_node; | |
8ec2ac34 | 2306 | else |
b370501f | 2307 | { |
0e339752 | 2308 | /* It's not valid to write an explicit instantiation in |
b370501f | 2309 | class scope, e.g.: |
8ec2ac34 | 2310 | |
0cbd7506 | 2311 | class C { template void f(); } |
8ec2ac34 | 2312 | |
b370501f KG |
2313 | This case is caught by the parser. However, on |
2314 | something like: | |
c8094d83 | 2315 | |
b370501f | 2316 | template class C { void f(); }; |
8ec2ac34 | 2317 | |
0e339752 | 2318 | (which is invalid) we can get here. The error will be |
b370501f KG |
2319 | issued later. */ |
2320 | ; | |
2321 | } | |
8ec2ac34 | 2322 | |
e1467ff2 | 2323 | return decl; |
75650646 | 2324 | } |
c8094d83 | 2325 | else if (ctype != NULL_TREE |
75650646 MM |
2326 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == |
2327 | IDENTIFIER_NODE)) | |
386b8a85 | 2328 | { |
75650646 MM |
2329 | /* Find the list of functions in ctype that have the same |
2330 | name as the declared function. */ | |
2331 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
2332 | tree fns = NULL_TREE; |
2333 | int idx; | |
2334 | ||
8ba658ee | 2335 | if (constructor_name_p (name, ctype)) |
386b8a85 | 2336 | { |
75650646 | 2337 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
c8094d83 | 2338 | |
0fcedd9c | 2339 | if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype) |
9f4faeae | 2340 | : !CLASSTYPE_DESTRUCTORS (ctype)) |
75650646 MM |
2341 | { |
2342 | /* From [temp.expl.spec]: | |
c8094d83 | 2343 | |
75650646 MM |
2344 | If such an explicit specialization for the member |
2345 | of a class template names an implicitly-declared | |
2346 | special member function (clause _special_), the | |
c8094d83 | 2347 | program is ill-formed. |
e1467ff2 MM |
2348 | |
2349 | Similar language is found in [temp.explicit]. */ | |
33bd39a2 | 2350 | error ("specialization of implicitly-declared special member function"); |
03017874 | 2351 | return error_mark_node; |
75650646 | 2352 | } |
386b8a85 | 2353 | |
42da2fd8 | 2354 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 2355 | } |
42da2fd8 | 2356 | |
421844e7 | 2357 | if (!DECL_CONV_FN_P (decl)) |
03017874 MM |
2358 | { |
2359 | idx = lookup_fnfields_1 (ctype, name); | |
2360 | if (idx >= 0) | |
aaaa46d2 | 2361 | fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx); |
03017874 MM |
2362 | } |
2363 | else | |
2364 | { | |
d4e6fecb | 2365 | VEC(tree,gc) *methods; |
aaaa46d2 | 2366 | tree ovl; |
03017874 MM |
2367 | |
2368 | /* For a type-conversion operator, we cannot do a | |
2369 | name-based lookup. We might be looking for `operator | |
2370 | int' which will be a specialization of `operator T'. | |
2371 | So, we find *all* the conversion operators, and then | |
2372 | select from them. */ | |
2373 | fns = NULL_TREE; | |
2374 | ||
2375 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
2376 | if (methods) | |
5dd236e2 | 2377 | for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; |
9ba5ff0f | 2378 | VEC_iterate (tree, methods, idx, ovl); |
aaaa46d2 | 2379 | ++idx) |
03017874 | 2380 | { |
aaaa46d2 | 2381 | if (!DECL_CONV_FN_P (OVL_CURRENT (ovl))) |
03017874 MM |
2382 | /* There are no more conversion functions. */ |
2383 | break; | |
2384 | ||
2385 | /* Glue all these conversion functions together | |
2386 | with those we already have. */ | |
2387 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
2388 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
2389 | } | |
2390 | } | |
c8094d83 MS |
2391 | |
2392 | if (fns == NULL_TREE) | |
386b8a85 | 2393 | { |
0f51ccfc | 2394 | error ("no member function %qD declared in %qT", name, ctype); |
03017874 | 2395 | return error_mark_node; |
386b8a85 JM |
2396 | } |
2397 | else | |
2398 | TREE_OPERAND (declarator, 0) = fns; | |
2399 | } | |
c8094d83 | 2400 | |
e1467ff2 MM |
2401 | /* Figure out what exactly is being specialized at this point. |
2402 | Note that for an explicit instantiation, even one for a | |
38e01259 | 2403 | member function, we cannot tell apriori whether the |
e1467ff2 | 2404 | instantiation is for a member template, or just a member |
36a117a5 MM |
2405 | function of a template class. Even if a member template is |
2406 | being instantiated, the member template arguments may be | |
2407 | elided if they can be deduced from the rest of the | |
2408 | declaration. */ | |
e1467ff2 | 2409 | tmpl = determine_specialization (declarator, decl, |
c8094d83 | 2410 | &targs, |
5fe7b654 | 2411 | member_specialization, |
29a1da1c MM |
2412 | template_count, |
2413 | tsk); | |
c8094d83 | 2414 | |
03017874 MM |
2415 | if (!tmpl || tmpl == error_mark_node) |
2416 | /* We couldn't figure out what this declaration was | |
2417 | specializing. */ | |
2418 | return error_mark_node; | |
2419 | else | |
386b8a85 | 2420 | { |
25aab5d0 | 2421 | tree gen_tmpl = most_general_template (tmpl); |
36a117a5 | 2422 | |
e1467ff2 MM |
2423 | if (explicit_instantiation) |
2424 | { | |
03d0f4af | 2425 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
c8094d83 | 2426 | is done by do_decl_instantiation later. */ |
25aab5d0 MM |
2427 | |
2428 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
2429 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
2430 | ||
2431 | if (arg_depth > parm_depth) | |
2432 | { | |
2433 | /* If TMPL is not the most general template (for | |
2434 | example, if TMPL is a friend template that is | |
2435 | injected into namespace scope), then there will | |
dc957d14 | 2436 | be too many levels of TARGS. Remove some of them |
25aab5d0 MM |
2437 | here. */ |
2438 | int i; | |
2439 | tree new_targs; | |
2440 | ||
f31c0a32 | 2441 | new_targs = make_tree_vec (parm_depth); |
25aab5d0 MM |
2442 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) |
2443 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
2444 | = TREE_VEC_ELT (targs, i); | |
2445 | targs = new_targs; | |
2446 | } | |
c8094d83 | 2447 | |
3e4a3562 | 2448 | return instantiate_template (tmpl, targs, tf_error); |
e1467ff2 | 2449 | } |
74b846e0 | 2450 | |
6c07f448 KL |
2451 | /* If we thought that the DECL was a member function, but it |
2452 | turns out to be specializing a static member function, | |
4546865e | 2453 | make DECL a static member function as well. */ |
6c07f448 KL |
2454 | if (DECL_STATIC_FUNCTION_P (tmpl) |
2455 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
4546865e | 2456 | revert_static_member_fn (decl); |
6c07f448 | 2457 | |
f9a7ae04 | 2458 | /* If this is a specialization of a member template of a |
29a1da1c MM |
2459 | template class, we want to return the TEMPLATE_DECL, not |
2460 | the specialization of it. */ | |
74b846e0 MM |
2461 | if (tsk == tsk_template) |
2462 | { | |
448545cb | 2463 | tree result = DECL_TEMPLATE_RESULT (tmpl); |
74b846e0 | 2464 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
448545cb | 2465 | DECL_INITIAL (result) = NULL_TREE; |
b190f239 NS |
2466 | if (have_def) |
2467 | { | |
448545cb | 2468 | tree parm; |
f31686a3 | 2469 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
448545cb | 2470 | DECL_SOURCE_LOCATION (result) |
f31686a3 | 2471 | = DECL_SOURCE_LOCATION (decl); |
08167d1c AO |
2472 | /* We want to use the argument list specified in the |
2473 | definition, not in the original declaration. */ | |
448545cb JM |
2474 | DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl); |
2475 | for (parm = DECL_ARGUMENTS (result); parm; | |
2476 | parm = TREE_CHAIN (parm)) | |
2477 | DECL_CONTEXT (parm) = result; | |
b190f239 | 2478 | } |
62e3e13a | 2479 | return register_specialization (tmpl, gen_tmpl, targs, |
2b110bfc | 2480 | is_friend, 0); |
74b846e0 MM |
2481 | } |
2482 | ||
36a117a5 | 2483 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
e1b3e07d | 2484 | DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); |
36a117a5 | 2485 | |
8afa707f MM |
2486 | /* Inherit default function arguments from the template |
2487 | DECL is specializing. */ | |
2488 | copy_default_args_to_explicit_spec (decl); | |
2489 | ||
c750255c MM |
2490 | /* This specialization has the same protection as the |
2491 | template it specializes. */ | |
2492 | TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); | |
2493 | TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); | |
b9e75696 | 2494 | |
736b8100 SB |
2495 | /* 7.1.1-1 [dcl.stc] |
2496 | ||
2497 | A storage-class-specifier shall not be specified in an | |
2498 | explicit specialization... | |
2499 | ||
2500 | The parser rejects these, so unless action is taken here, | |
2501 | explicit function specializations will always appear with | |
2502 | global linkage. | |
2503 | ||
2504 | The action recommended by the C++ CWG in response to C++ | |
2505 | defect report 605 is to make the storage class and linkage | |
2506 | of the explicit specialization match the templated function: | |
2507 | ||
2508 | http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605 | |
2509 | */ | |
2510 | if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl)) | |
2511 | { | |
2512 | tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl); | |
2513 | gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL); | |
2514 | ||
d732e98f | 2515 | /* This specialization has the same linkage and visibility as |
736b8100 SB |
2516 | the function template it specializes. */ |
2517 | TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func); | |
92d0af97 JJ |
2518 | if (! TREE_PUBLIC (decl)) |
2519 | { | |
2520 | DECL_INTERFACE_KNOWN (decl) = 1; | |
2521 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
2522 | } | |
736b8100 SB |
2523 | DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func); |
2524 | if (DECL_VISIBILITY_SPECIFIED (tmpl_func)) | |
2525 | { | |
2526 | DECL_VISIBILITY_SPECIFIED (decl) = 1; | |
2527 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func); | |
2528 | } | |
2529 | } | |
2530 | ||
9c12301f MM |
2531 | /* If DECL is a friend declaration, declared using an |
2532 | unqualified name, the namespace associated with DECL may | |
2533 | have been set incorrectly. For example, in: | |
3db45ab5 MS |
2534 | |
2535 | template <typename T> void f(T); | |
2536 | namespace N { | |
2537 | struct S { friend void f<int>(int); } | |
2538 | } | |
2539 | ||
2540 | we will have set the DECL_CONTEXT for the friend | |
2541 | declaration to N, rather than to the global namespace. */ | |
9c12301f MM |
2542 | if (DECL_NAMESPACE_SCOPE_P (decl)) |
2543 | DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl); | |
8d039470 | 2544 | |
386b8a85 | 2545 | if (is_friend && !have_def) |
36a117a5 MM |
2546 | /* This is not really a declaration of a specialization. |
2547 | It's just the name of an instantiation. But, it's not | |
2548 | a request for an instantiation, either. */ | |
fbf1c34b | 2549 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
08511114 KL |
2550 | else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) |
2551 | /* This is indeed a specialization. In case of constructors | |
2552 | and destructors, we need in-charge and not-in-charge | |
2553 | versions in V3 ABI. */ | |
2554 | clone_function_decl (decl, /*update_method_vec_p=*/0); | |
75650646 | 2555 | |
36a117a5 MM |
2556 | /* Register this specialization so that we can find it |
2557 | again. */ | |
2b110bfc | 2558 | decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0); |
386b8a85 JM |
2559 | } |
2560 | } | |
c8094d83 | 2561 | |
e1467ff2 | 2562 | return decl; |
386b8a85 | 2563 | } |
75650646 | 2564 | |
75650646 MM |
2565 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
2566 | parameters. These are represented in the same format used for | |
2567 | DECL_TEMPLATE_PARMS. */ | |
2568 | ||
50bc768d | 2569 | int |
58f9752a | 2570 | comp_template_parms (const_tree parms1, const_tree parms2) |
75650646 | 2571 | { |
58f9752a KG |
2572 | const_tree p1; |
2573 | const_tree p2; | |
75650646 MM |
2574 | |
2575 | if (parms1 == parms2) | |
2576 | return 1; | |
2577 | ||
c8094d83 | 2578 | for (p1 = parms1, p2 = parms2; |
75650646 MM |
2579 | p1 != NULL_TREE && p2 != NULL_TREE; |
2580 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
2581 | { | |
2582 | tree t1 = TREE_VALUE (p1); | |
2583 | tree t2 = TREE_VALUE (p2); | |
2584 | int i; | |
2585 | ||
50bc768d NS |
2586 | gcc_assert (TREE_CODE (t1) == TREE_VEC); |
2587 | gcc_assert (TREE_CODE (t2) == TREE_VEC); | |
75650646 MM |
2588 | |
2589 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
2590 | return 0; | |
2591 | ||
c8094d83 | 2592 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) |
75650646 | 2593 | { |
0f67a82f LM |
2594 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); |
2595 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
42b304f1 | 2596 | |
0f67a82f LM |
2597 | /* If either of the template parameters are invalid, assume |
2598 | they match for the sake of error recovery. */ | |
2599 | if (parm1 == error_mark_node || parm2 == error_mark_node) | |
2600 | return 1; | |
75650646 MM |
2601 | |
2602 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
2603 | return 0; | |
2604 | ||
5d80a306 DG |
2605 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM |
2606 | && (TEMPLATE_TYPE_PARAMETER_PACK (parm1) | |
2607 | == TEMPLATE_TYPE_PARAMETER_PACK (parm2))) | |
75650646 | 2608 | continue; |
3bfdc719 | 2609 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
2610 | return 0; |
2611 | } | |
2612 | } | |
2613 | ||
2614 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
2615 | /* One set of parameters has more parameters lists than the | |
2616 | other. */ | |
2617 | return 0; | |
2618 | ||
2619 | return 1; | |
2620 | } | |
2621 | ||
5d80a306 | 2622 | /* Determine whether PARM is a parameter pack. */ |
d40a19da | 2623 | |
5d80a306 | 2624 | bool |
58f9752a | 2625 | template_parameter_pack_p (const_tree parm) |
5d80a306 DG |
2626 | { |
2627 | /* Determine if we have a non-type template parameter pack. */ | |
2628 | if (TREE_CODE (parm) == PARM_DECL) | |
2629 | return (DECL_TEMPLATE_PARM_P (parm) | |
2630 | && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))); | |
2631 | ||
2632 | /* If this is a list of template parameters, we could get a | |
2633 | TYPE_DECL or a TEMPLATE_DECL. */ | |
2634 | if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL) | |
2635 | parm = TREE_TYPE (parm); | |
2636 | ||
2637 | return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
2638 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) | |
2639 | && TEMPLATE_TYPE_PARAMETER_PACK (parm)); | |
2640 | } | |
2641 | ||
d40a19da DS |
2642 | /* Determine if T is a function parameter pack. */ |
2643 | ||
2644 | bool | |
2645 | function_parameter_pack_p (const_tree t) | |
2646 | { | |
2647 | if (t && TREE_CODE (t) == PARM_DECL) | |
2648 | return FUNCTION_PARAMETER_PACK_P (t); | |
2649 | return false; | |
2650 | } | |
2651 | ||
2652 | /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST. | |
2653 | PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */ | |
2654 | ||
2655 | tree | |
2656 | get_function_template_decl (const_tree primary_func_tmpl_inst) | |
2657 | { | |
2658 | if (! primary_func_tmpl_inst | |
2659 | || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL | |
2660 | || ! primary_template_instantiation_p (primary_func_tmpl_inst)) | |
2661 | return NULL; | |
2662 | ||
2663 | return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst)); | |
2664 | } | |
2665 | ||
2666 | /* Return true iff the function parameter PARAM_DECL was expanded | |
2667 | from the function parameter pack PACK. */ | |
2668 | ||
2669 | bool | |
2670 | function_parameter_expanded_from_pack_p (tree param_decl, tree pack) | |
2671 | { | |
2672 | if (! function_parameter_pack_p (pack)) | |
2673 | return false; | |
2674 | ||
2675 | gcc_assert (DECL_NAME (param_decl) && DECL_NAME (pack)); | |
2676 | ||
2677 | /* The parameter pack and its pack arguments have the same | |
2678 | DECL_PARM_INDEX. */ | |
2679 | return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl); | |
2680 | } | |
2681 | ||
5d80a306 DG |
2682 | /* Determine whether ARGS describes a variadic template args list, |
2683 | i.e., one that is terminated by a template argument pack. */ | |
d40a19da | 2684 | |
5d80a306 DG |
2685 | static bool |
2686 | template_args_variadic_p (tree args) | |
2687 | { | |
2688 | int nargs; | |
2689 | tree last_parm; | |
2690 | ||
2691 | if (args == NULL_TREE) | |
2692 | return false; | |
2693 | ||
2694 | args = INNERMOST_TEMPLATE_ARGS (args); | |
2695 | nargs = TREE_VEC_LENGTH (args); | |
2696 | ||
2697 | if (nargs == 0) | |
2698 | return false; | |
2699 | ||
2700 | last_parm = TREE_VEC_ELT (args, nargs - 1); | |
2701 | ||
2702 | return ARGUMENT_PACK_P (last_parm); | |
2703 | } | |
2704 | ||
2705 | /* Generate a new name for the parameter pack name NAME (an | |
2706 | IDENTIFIER_NODE) that incorporates its */ | |
d40a19da | 2707 | |
5d80a306 DG |
2708 | static tree |
2709 | make_ith_pack_parameter_name (tree name, int i) | |
2710 | { | |
2711 | /* Munge the name to include the parameter index. */ | |
f9329c35 DS |
2712 | #define NUMBUF_LEN 128 |
2713 | char numbuf[NUMBUF_LEN]; | |
5d80a306 | 2714 | char* newname; |
f9329c35 DS |
2715 | int newname_len; |
2716 | ||
2717 | snprintf (numbuf, NUMBUF_LEN, "%i", i); | |
2718 | newname_len = IDENTIFIER_LENGTH (name) | |
d3bc57f2 | 2719 | + strlen (numbuf) + 2; |
f9329c35 DS |
2720 | newname = (char*)alloca (newname_len); |
2721 | snprintf (newname, newname_len, | |
2722 | "%s#%i", IDENTIFIER_POINTER (name), i); | |
5d80a306 DG |
2723 | return get_identifier (newname); |
2724 | } | |
2725 | ||
f9329c35 DS |
2726 | /* Return true if T is a primary function |
2727 | or class template instantiation. */ | |
2728 | ||
2729 | static bool | |
2730 | primary_template_instantiation_p (const_tree t) | |
2731 | { | |
2732 | if (!t) | |
2733 | return false; | |
2734 | ||
2735 | if (TREE_CODE (t) == FUNCTION_DECL) | |
2736 | return DECL_LANG_SPECIFIC (t) | |
2737 | && DECL_TEMPLATE_INSTANTIATION (t) | |
2738 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)); | |
2739 | else if (CLASS_TYPE_P (t)) | |
2740 | return CLASSTYPE_TEMPLATE_INSTANTIATION (t) | |
2741 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)); | |
2742 | return false; | |
2743 | } | |
2744 | ||
2745 | /* Return true if PARM is a template template parameter. */ | |
2746 | ||
2747 | bool | |
2748 | template_template_parameter_p (const_tree parm) | |
2749 | { | |
2750 | return DECL_TEMPLATE_TEMPLATE_PARM_P (parm); | |
2751 | } | |
2752 | ||
2753 | /* Return the template parameters of T if T is a | |
2754 | primary template instantiation, NULL otherwise. */ | |
2755 | ||
2756 | tree | |
2757 | get_primary_template_innermost_parameters (const_tree t) | |
2758 | { | |
2759 | tree parms = NULL, template_info = NULL; | |
2760 | ||
2761 | if ((template_info = get_template_info (t)) | |
2762 | && primary_template_instantiation_p (t)) | |
2763 | parms = INNERMOST_TEMPLATE_PARMS | |
2764 | (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info))); | |
2765 | ||
2766 | return parms; | |
2767 | } | |
2768 | ||
2769 | /* Returns the template arguments of T if T is a template instantiation, | |
2770 | NULL otherwise. */ | |
2771 | ||
2772 | tree | |
2773 | get_template_innermost_arguments (const_tree t) | |
2774 | { | |
2775 | tree args = NULL, template_info = NULL; | |
2776 | ||
2777 | if ((template_info = get_template_info (t)) | |
2778 | && TI_ARGS (template_info)) | |
2779 | args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info)); | |
2780 | ||
2781 | return args; | |
2782 | } | |
2783 | ||
d40a19da DS |
2784 | /* Return the argument pack elements of T if T is a template argument pack, |
2785 | NULL otherwise. */ | |
f9329c35 DS |
2786 | |
2787 | tree | |
2788 | get_template_argument_pack_elems (const_tree t) | |
2789 | { | |
2790 | if (TREE_CODE (t) != TYPE_ARGUMENT_PACK | |
2791 | && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK) | |
2792 | return NULL; | |
2793 | ||
2794 | return ARGUMENT_PACK_ARGS (t); | |
2795 | } | |
2796 | ||
1ad8aeeb | 2797 | /* Structure used to track the progress of find_parameter_packs_r. */ |
5d80a306 DG |
2798 | struct find_parameter_pack_data |
2799 | { | |
1ad8aeeb DG |
2800 | /* TREE_LIST that will contain all of the parameter packs found by |
2801 | the traversal. */ | |
5d80a306 | 2802 | tree* parameter_packs; |
1ad8aeeb DG |
2803 | |
2804 | /* Set of AST nodes that have been visited by the traversal. */ | |
5d80a306 DG |
2805 | struct pointer_set_t *visited; |
2806 | }; | |
2807 | ||
88b82314 | 2808 | /* Identifies all of the argument packs that occur in a template |
5d80a306 | 2809 | argument and appends them to the TREE_LIST inside DATA, which is a |
88b82314 | 2810 | find_parameter_pack_data structure. This is a subroutine of |
5d80a306 DG |
2811 | make_pack_expansion and uses_parameter_packs. */ |
2812 | static tree | |
2813 | find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data) | |
2814 | { | |
2815 | tree t = *tp; | |
2816 | struct find_parameter_pack_data* ppd = | |
2817 | (struct find_parameter_pack_data*)data; | |
1ad8aeeb | 2818 | bool parameter_pack_p = false; |
5d80a306 | 2819 | |
1ad8aeeb DG |
2820 | /* Identify whether this is a parameter pack or not. */ |
2821 | switch (TREE_CODE (t)) | |
2822 | { | |
2823 | case TEMPLATE_PARM_INDEX: | |
2824 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
2825 | parameter_pack_p = true; | |
2826 | break; | |
2827 | ||
2828 | case TEMPLATE_TYPE_PARM: | |
2829 | case TEMPLATE_TEMPLATE_PARM: | |
2830 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
2831 | parameter_pack_p = true; | |
2832 | break; | |
2833 | ||
2834 | case PARM_DECL: | |
2835 | if (FUNCTION_PARAMETER_PACK_P (t)) | |
2836 | { | |
2837 | /* We don't want to walk into the type of a PARM_DECL, | |
2838 | because we don't want to see the type parameter pack. */ | |
2839 | *walk_subtrees = 0; | |
2840 | parameter_pack_p = true; | |
2841 | } | |
2842 | break; | |
2843 | ||
2844 | default: | |
2845 | /* Not a parameter pack. */ | |
2846 | break; | |
2847 | } | |
2848 | ||
2849 | if (parameter_pack_p) | |
2850 | { | |
2851 | /* Add this parameter pack to the list. */ | |
2852 | *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs); | |
1ad8aeeb | 2853 | } |
7b3e2d46 | 2854 | |
1ad8aeeb DG |
2855 | if (TYPE_P (t)) |
2856 | cp_walk_tree (&TYPE_CONTEXT (t), | |
92fab505 | 2857 | &find_parameter_packs_r, ppd, ppd->visited); |
1ad8aeeb | 2858 | |
5d80a306 DG |
2859 | /* This switch statement will return immediately if we don't find a |
2860 | parameter pack. */ | |
2861 | switch (TREE_CODE (t)) | |
2862 | { | |
2863 | case TEMPLATE_PARM_INDEX: | |
5d80a306 DG |
2864 | return NULL_TREE; |
2865 | ||
2866 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
1ad8aeeb DG |
2867 | /* Check the template itself. */ |
2868 | cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)), | |
92fab505 | 2869 | &find_parameter_packs_r, ppd, ppd->visited); |
5d80a306 | 2870 | /* Check the template arguments. */ |
14588106 | 2871 | cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd, |
92fab505 | 2872 | ppd->visited); |
5d80a306 | 2873 | *walk_subtrees = 0; |
1ad8aeeb | 2874 | return NULL_TREE; |
5d80a306 DG |
2875 | |
2876 | case TEMPLATE_TYPE_PARM: | |
2877 | case TEMPLATE_TEMPLATE_PARM: | |
5d80a306 DG |
2878 | return NULL_TREE; |
2879 | ||
2880 | case PARM_DECL: | |
5d80a306 DG |
2881 | return NULL_TREE; |
2882 | ||
2883 | case RECORD_TYPE: | |
2884 | if (TYPE_PTRMEMFUNC_P (t)) | |
2885 | return NULL_TREE; | |
2886 | /* Fall through. */ | |
2887 | ||
2888 | case UNION_TYPE: | |
2889 | case ENUMERAL_TYPE: | |
2890 | if (TYPE_TEMPLATE_INFO (t)) | |
1ad8aeeb | 2891 | cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t)), |
92fab505 | 2892 | &find_parameter_packs_r, ppd, ppd->visited); |
5d80a306 DG |
2893 | |
2894 | *walk_subtrees = 0; | |
2895 | return NULL_TREE; | |
2896 | ||
2897 | case TEMPLATE_DECL: | |
1ad8aeeb | 2898 | cp_walk_tree (&TREE_TYPE (t), |
92fab505 | 2899 | &find_parameter_packs_r, ppd, ppd->visited); |
5d80a306 | 2900 | return NULL_TREE; |
e1a18c68 DG |
2901 | |
2902 | case TYPENAME_TYPE: | |
2903 | cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r, | |
92fab505 | 2904 | ppd, ppd->visited); |
e1a18c68 DG |
2905 | *walk_subtrees = 0; |
2906 | return NULL_TREE; | |
e1a18c68 | 2907 | |
5d80a306 DG |
2908 | case TYPE_PACK_EXPANSION: |
2909 | case EXPR_PACK_EXPANSION: | |
2910 | *walk_subtrees = 0; | |
2911 | return NULL_TREE; | |
2912 | ||
d393153e | 2913 | case INTEGER_TYPE: |
14588106 | 2914 | cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r, |
92fab505 | 2915 | ppd, ppd->visited); |
d393153e DG |
2916 | *walk_subtrees = 0; |
2917 | return NULL_TREE; | |
2918 | ||
4439d02f | 2919 | case IDENTIFIER_NODE: |
92fab505 DG |
2920 | cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd, |
2921 | ppd->visited); | |
4439d02f DG |
2922 | *walk_subtrees = 0; |
2923 | return NULL_TREE; | |
2924 | ||
5d80a306 DG |
2925 | default: |
2926 | return NULL_TREE; | |
2927 | } | |
5d80a306 DG |
2928 | |
2929 | return NULL_TREE; | |
2930 | } | |
2931 | ||
2932 | /* Determines if the expression or type T uses any parameter packs. */ | |
2933 | bool | |
2934 | uses_parameter_packs (tree t) | |
2935 | { | |
2936 | tree parameter_packs = NULL_TREE; | |
2937 | struct find_parameter_pack_data ppd; | |
2938 | ppd.parameter_packs = ¶meter_packs; | |
2939 | ppd.visited = pointer_set_create (); | |
92fab505 | 2940 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); |
30bcc028 | 2941 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
2942 | return parameter_packs != NULL_TREE; |
2943 | } | |
2944 | ||
2945 | /* Turn ARG, which may be an expression, type, or a TREE_LIST | |
2946 | representation a base-class initializer into a parameter pack | |
2947 | expansion. If all goes well, the resulting node will be an | |
2948 | EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST, | |
2949 | respectively. */ | |
2950 | tree | |
2951 | make_pack_expansion (tree arg) | |
2952 | { | |
2953 | tree result; | |
2954 | tree parameter_packs = NULL_TREE; | |
2955 | bool for_types = false; | |
2956 | struct find_parameter_pack_data ppd; | |
2957 | ||
2958 | if (!arg || arg == error_mark_node) | |
2959 | return arg; | |
2960 | ||
2961 | if (TREE_CODE (arg) == TREE_LIST) | |
2962 | { | |
2963 | /* The only time we will see a TREE_LIST here is for a base | |
2964 | class initializer. In this case, the TREE_PURPOSE will be a | |
2965 | _TYPE node (representing the base class expansion we're | |
2966 | initializing) and the TREE_VALUE will be a TREE_LIST | |
2967 | containing the initialization arguments. | |
2968 | ||
2969 | The resulting expansion looks somewhat different from most | |
2970 | expansions. Rather than returning just one _EXPANSION, we | |
2971 | return a TREE_LIST whose TREE_PURPOSE is a | |
2972 | TYPE_PACK_EXPANSION containing the bases that will be | |
2973 | initialized. The TREE_VALUE will be identical to the | |
2974 | original TREE_VALUE, which is a list of arguments that will | |
2975 | be passed to each base. We do not introduce any new pack | |
2976 | expansion nodes into the TREE_VALUE (although it is possible | |
2977 | that some already exist), because the TREE_PURPOSE and | |
2978 | TREE_VALUE all need to be expanded together with the same | |
2979 | _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the | |
2980 | resulting TREE_PURPOSE will mention the parameter packs in | |
2981 | both the bases and the arguments to the bases. */ | |
2982 | tree purpose; | |
2983 | tree value; | |
2984 | tree parameter_packs = NULL_TREE; | |
2985 | ||
2986 | /* Determine which parameter packs will be used by the base | |
2987 | class expansion. */ | |
2988 | ppd.visited = pointer_set_create (); | |
2989 | ppd.parameter_packs = ¶meter_packs; | |
14588106 | 2990 | cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r, |
92fab505 | 2991 | &ppd, ppd.visited); |
5d80a306 DG |
2992 | |
2993 | if (parameter_packs == NULL_TREE) | |
2994 | { | |
2995 | error ("base initializer expansion %<%T%> contains no parameter packs", arg); | |
30bcc028 | 2996 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
2997 | return error_mark_node; |
2998 | } | |
2999 | ||
3000 | if (TREE_VALUE (arg) != void_type_node) | |
3001 | { | |
3002 | /* Collect the sets of parameter packs used in each of the | |
3003 | initialization arguments. */ | |
3004 | for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value)) | |
3005 | { | |
3006 | /* Determine which parameter packs will be expanded in this | |
3007 | argument. */ | |
14588106 | 3008 | cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r, |
92fab505 | 3009 | &ppd, ppd.visited); |
5d80a306 DG |
3010 | } |
3011 | } | |
3012 | ||
30bcc028 DG |
3013 | pointer_set_destroy (ppd.visited); |
3014 | ||
5d80a306 DG |
3015 | /* Create the pack expansion type for the base type. */ |
3016 | purpose = make_node (TYPE_PACK_EXPANSION); | |
3017 | SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg)); | |
3018 | PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs; | |
3019 | ||
3020 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
3021 | they will rarely be compared to anything. */ | |
3022 | SET_TYPE_STRUCTURAL_EQUALITY (purpose); | |
3023 | ||
3024 | return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE); | |
3025 | } | |
3026 | ||
3027 | if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL) | |
3028 | for_types = true; | |
3029 | ||
3030 | /* Build the PACK_EXPANSION_* node. */ | |
3031 | result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION); | |
3032 | SET_PACK_EXPANSION_PATTERN (result, arg); | |
3033 | if (TREE_CODE (result) == EXPR_PACK_EXPANSION) | |
3034 | { | |
3035 | /* Propagate type and const-expression information. */ | |
3036 | TREE_TYPE (result) = TREE_TYPE (arg); | |
3037 | TREE_CONSTANT (result) = TREE_CONSTANT (arg); | |
3038 | } | |
3039 | else | |
3040 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; | |
3041 | they will rarely be compared to anything. */ | |
3042 | SET_TYPE_STRUCTURAL_EQUALITY (result); | |
3043 | ||
3044 | /* Determine which parameter packs will be expanded. */ | |
3045 | ppd.parameter_packs = ¶meter_packs; | |
3046 | ppd.visited = pointer_set_create (); | |
92fab505 | 3047 | cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited); |
30bcc028 | 3048 | pointer_set_destroy (ppd.visited); |
5d80a306 DG |
3049 | |
3050 | /* Make sure we found some parameter packs. */ | |
3051 | if (parameter_packs == NULL_TREE) | |
3052 | { | |
3053 | if (TYPE_P (arg)) | |
3054 | error ("expansion pattern %<%T%> contains no argument packs", arg); | |
3055 | else | |
3056 | error ("expansion pattern %<%E%> contains no argument packs", arg); | |
3057 | return error_mark_node; | |
3058 | } | |
3059 | PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs; | |
3060 | ||
3061 | return result; | |
3062 | } | |
3063 | ||
3064 | /* Checks T for any "bare" parameter packs, which have not yet been | |
3065 | expanded, and issues an error if any are found. This operation can | |
3066 | only be done on full expressions or types (e.g., an expression | |
3067 | statement, "if" condition, etc.), because we could have expressions like: | |
3068 | ||
3069 | foo(f(g(h(args)))...) | |
3070 | ||
3071 | where "args" is a parameter pack. check_for_bare_parameter_packs | |
3072 | should not be called for the subexpressions args, h(args), | |
3073 | g(h(args)), or f(g(h(args))), because we would produce erroneous | |
b1d7b1c0 DG |
3074 | error messages. |
3075 | ||
4439d02f DG |
3076 | Returns TRUE and emits an error if there were bare parameter packs, |
3077 | returns FALSE otherwise. */ | |
b1d7b1c0 | 3078 | bool |
7b3e2d46 | 3079 | check_for_bare_parameter_packs (tree t) |
5d80a306 DG |
3080 | { |
3081 | tree parameter_packs = NULL_TREE; | |
3082 | struct find_parameter_pack_data ppd; | |
3083 | ||
7b3e2d46 | 3084 | if (!processing_template_decl || !t || t == error_mark_node) |
4439d02f | 3085 | return false; |
5d80a306 | 3086 | |
7b3e2d46 DG |
3087 | if (TREE_CODE (t) == TYPE_DECL) |
3088 | t = TREE_TYPE (t); | |
5d80a306 DG |
3089 | |
3090 | ppd.parameter_packs = ¶meter_packs; | |
3091 | ppd.visited = pointer_set_create (); | |
92fab505 | 3092 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); |
30bcc028 | 3093 | pointer_set_destroy (ppd.visited); |
5d80a306 | 3094 | |
b1d7b1c0 DG |
3095 | if (parameter_packs) |
3096 | { | |
bcac2b89 | 3097 | error ("parameter packs not expanded with %<...%>:"); |
b1d7b1c0 DG |
3098 | while (parameter_packs) |
3099 | { | |
3100 | tree pack = TREE_VALUE (parameter_packs); | |
3101 | tree name = NULL_TREE; | |
3102 | ||
3103 | if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM | |
3104 | || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM) | |
3105 | name = TYPE_NAME (pack); | |
3106 | else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX) | |
3107 | name = DECL_NAME (TEMPLATE_PARM_DECL (pack)); | |
3108 | else | |
3109 | name = DECL_NAME (pack); | |
1ad8aeeb DG |
3110 | |
3111 | if (name) | |
1f5b3869 | 3112 | inform (input_location, " %qD", name); |
1ad8aeeb | 3113 | else |
1f5b3869 | 3114 | inform (input_location, " <anonymous>"); |
b1d7b1c0 DG |
3115 | |
3116 | parameter_packs = TREE_CHAIN (parameter_packs); | |
3117 | } | |
3118 | ||
4439d02f | 3119 | return true; |
b1d7b1c0 DG |
3120 | } |
3121 | ||
4439d02f | 3122 | return false; |
5d80a306 DG |
3123 | } |
3124 | ||
3125 | /* Expand any parameter packs that occur in the template arguments in | |
3126 | ARGS. */ | |
3127 | tree | |
3128 | expand_template_argument_pack (tree args) | |
3129 | { | |
3130 | tree result_args = NULL_TREE; | |
3131 | int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0; | |
3132 | int num_result_args = -1; | |
3133 | ||
3134 | /* First, determine if we need to expand anything, and the number of | |
3135 | slots we'll need. */ | |
3136 | for (in_arg = 0; in_arg < nargs; ++in_arg) | |
3137 | { | |
3138 | tree arg = TREE_VEC_ELT (args, in_arg); | |
3139 | if (ARGUMENT_PACK_P (arg)) | |
3140 | { | |
3141 | int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)); | |
3142 | if (num_result_args < 0) | |
3143 | num_result_args = in_arg + num_packed; | |
3144 | else | |
3145 | num_result_args += num_packed; | |
3146 | } | |
3147 | else | |
3148 | { | |
3149 | if (num_result_args >= 0) | |
3150 | num_result_args++; | |
3151 | } | |
3152 | } | |
3153 | ||
3154 | /* If no expansion is necessary, we're done. */ | |
3155 | if (num_result_args < 0) | |
3156 | return args; | |
3157 | ||
3158 | /* Expand arguments. */ | |
3159 | result_args = make_tree_vec (num_result_args); | |
3160 | for (in_arg = 0; in_arg < nargs; ++in_arg) | |
3161 | { | |
3162 | tree arg = TREE_VEC_ELT (args, in_arg); | |
3163 | if (ARGUMENT_PACK_P (arg)) | |
3164 | { | |
3165 | tree packed = ARGUMENT_PACK_ARGS (arg); | |
3166 | int i, num_packed = TREE_VEC_LENGTH (packed); | |
3167 | for (i = 0; i < num_packed; ++i, ++out_arg) | |
3168 | TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i); | |
3169 | } | |
3170 | else | |
3171 | { | |
3172 | TREE_VEC_ELT (result_args, out_arg) = arg; | |
3173 | ++out_arg; | |
3174 | } | |
3175 | } | |
3176 | ||
3177 | return result_args; | |
3178 | } | |
3179 | ||
157b0647 | 3180 | /* Checks if DECL shadows a template parameter. |
f3400fe2 JM |
3181 | |
3182 | [temp.local]: A template-parameter shall not be redeclared within its | |
157b0647 | 3183 | scope (including nested scopes). |
f3400fe2 | 3184 | |
157b0647 PC |
3185 | Emits an error and returns TRUE if the DECL shadows a parameter, |
3186 | returns FALSE otherwise. */ | |
3187 | ||
3188 | bool | |
3a978d72 | 3189 | check_template_shadow (tree decl) |
f3400fe2 | 3190 | { |
8f032717 MM |
3191 | tree olddecl; |
3192 | ||
b5d9b9ab MM |
3193 | /* If we're not in a template, we can't possibly shadow a template |
3194 | parameter. */ | |
3195 | if (!current_template_parms) | |
157b0647 | 3196 | return true; |
b5d9b9ab MM |
3197 | |
3198 | /* Figure out what we're shadowing. */ | |
8f032717 MM |
3199 | if (TREE_CODE (decl) == OVERLOAD) |
3200 | decl = OVL_CURRENT (decl); | |
90ea9897 | 3201 | olddecl = innermost_non_namespace_value (DECL_NAME (decl)); |
f3400fe2 | 3202 | |
b5d9b9ab MM |
3203 | /* If there's no previous binding for this name, we're not shadowing |
3204 | anything, let alone a template parameter. */ | |
3205 | if (!olddecl) | |
157b0647 | 3206 | return true; |
b5d9b9ab MM |
3207 | |
3208 | /* If we're not shadowing a template parameter, we're done. Note | |
3209 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
3210 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
3211 | node. */ | |
2f939d94 | 3212 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) |
157b0647 | 3213 | return true; |
b5d9b9ab MM |
3214 | |
3215 | /* We check for decl != olddecl to avoid bogus errors for using a | |
3216 | name inside a class. We check TPFI to avoid duplicate errors for | |
3217 | inline member templates. */ | |
c8094d83 | 3218 | if (decl == olddecl |
b5d9b9ab | 3219 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) |
157b0647 | 3220 | return true; |
b5d9b9ab | 3221 | |
dee15844 JM |
3222 | error ("declaration of %q+#D", decl); |
3223 | error (" shadows template parm %q+#D", olddecl); | |
157b0647 | 3224 | return false; |
f3400fe2 | 3225 | } |
22a7be53 | 3226 | |
f3400fe2 | 3227 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
3228 | ORIG_LEVEL, DECL, and TYPE. */ |
3229 | ||
3230 | static tree | |
c8094d83 | 3231 | build_template_parm_index (int index, |
0cbd7506 MS |
3232 | int level, |
3233 | int orig_level, | |
3234 | tree decl, | |
3235 | tree type) | |
f84b4be9 JM |
3236 | { |
3237 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
3238 | TEMPLATE_PARM_IDX (t) = index; | |
3239 | TEMPLATE_PARM_LEVEL (t) = level; | |
3240 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
3241 | TEMPLATE_PARM_DECL (t) = decl; | |
3242 | TREE_TYPE (t) = type; | |
3e4a3562 NS |
3243 | TREE_CONSTANT (t) = TREE_CONSTANT (decl); |
3244 | TREE_READONLY (t) = TREE_READONLY (decl); | |
f84b4be9 JM |
3245 | |
3246 | return t; | |
3247 | } | |
3248 | ||
06d40de8 | 3249 | /* Find the canonical type parameter for the given template type |
c3e188fc KH |
3250 | parameter. Returns the canonical type parameter, which may be TYPE |
3251 | if no such parameter existed. */ | |
06d40de8 DG |
3252 | static tree |
3253 | canonical_type_parameter (tree type) | |
3254 | { | |
3255 | tree list; | |
3256 | int idx = TEMPLATE_TYPE_IDX (type); | |
3257 | if (!canonical_template_parms) | |
3258 | canonical_template_parms = VEC_alloc (tree, gc, idx+1); | |
3259 | ||
3260 | while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx) | |
3261 | VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE); | |
3262 | ||
3263 | list = VEC_index (tree, canonical_template_parms, idx); | |
96d84882 | 3264 | while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL)) |
06d40de8 DG |
3265 | list = TREE_CHAIN (list); |
3266 | ||
3267 | if (list) | |
3268 | return TREE_VALUE (list); | |
3269 | else | |
3270 | { | |
3271 | VEC_replace(tree, canonical_template_parms, idx, | |
3272 | tree_cons (NULL_TREE, type, | |
3273 | VEC_index (tree, canonical_template_parms, idx))); | |
3274 | return type; | |
3275 | } | |
3276 | } | |
3277 | ||
f84b4be9 | 3278 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 3279 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
3280 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
3281 | new one is created. */ | |
3282 | ||
c8094d83 | 3283 | static tree |
ef3b7b17 JM |
3284 | reduce_template_parm_level (tree index, tree type, int levels, tree args, |
3285 | tsubst_flags_t complain) | |
f84b4be9 JM |
3286 | { |
3287 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
3288 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 3289 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 | 3290 | { |
3e4a3562 NS |
3291 | tree orig_decl = TEMPLATE_PARM_DECL (index); |
3292 | tree decl, t; | |
c8094d83 | 3293 | |
c2255bc4 AH |
3294 | decl = build_decl (DECL_SOURCE_LOCATION (orig_decl), |
3295 | TREE_CODE (orig_decl), DECL_NAME (orig_decl), type); | |
3e4a3562 NS |
3296 | TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); |
3297 | TREE_READONLY (decl) = TREE_READONLY (orig_decl); | |
3298 | DECL_ARTIFICIAL (decl) = 1; | |
3299 | SET_DECL_TEMPLATE_PARM_P (decl); | |
c8094d83 | 3300 | |
3e4a3562 | 3301 | t = build_template_parm_index (TEMPLATE_PARM_IDX (index), |
93cdc044 | 3302 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
3303 | TEMPLATE_PARM_ORIG_LEVEL (index), |
3304 | decl, type); | |
3305 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
5d80a306 DG |
3306 | TEMPLATE_PARM_PARAMETER_PACK (t) |
3307 | = TEMPLATE_PARM_PARAMETER_PACK (index); | |
cae40af6 | 3308 | |
820cc88f | 3309 | /* Template template parameters need this. */ |
ef3b7b17 JM |
3310 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
3311 | DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms | |
3312 | (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)), | |
3313 | args, complain); | |
f84b4be9 JM |
3314 | } |
3315 | ||
3316 | return TEMPLATE_PARM_DESCENDANTS (index); | |
3317 | } | |
3318 | ||
a14de1af | 3319 | /* Process information from new template parameter PARM and append it to the |
058b15c1 | 3320 | LIST being built. This new parameter is a non-type parameter iff |
5d80a306 | 3321 | IS_NON_TYPE is true. This new parameter is a parameter |
c2255bc4 AH |
3322 | pack iff IS_PARAMETER_PACK is true. The location of PARM is in |
3323 | PARM_LOC. */ | |
e92cc029 | 3324 | |
8d08fdba | 3325 | tree |
c2255bc4 | 3326 | process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type, |
5d80a306 | 3327 | bool is_parameter_pack) |
8d08fdba | 3328 | { |
8d08fdba | 3329 | tree decl = 0; |
a292b002 | 3330 | tree defval; |
0f67a82f | 3331 | tree err_parm_list; |
d47e3adf | 3332 | int idx = 0; |
f84b4be9 | 3333 | |
50bc768d | 3334 | gcc_assert (TREE_CODE (parm) == TREE_LIST); |
a292b002 | 3335 | defval = TREE_PURPOSE (parm); |
5566b478 MS |
3336 | |
3337 | if (list) | |
3338 | { | |
d47e3adf LM |
3339 | tree p = tree_last (list); |
3340 | ||
0f67a82f | 3341 | if (p && TREE_VALUE (p) != error_mark_node) |
d47e3adf LM |
3342 | { |
3343 | p = TREE_VALUE (p); | |
3344 | if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) | |
3345 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
3346 | else | |
3347 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); | |
3348 | } | |
5566b478 | 3349 | |
5566b478 MS |
3350 | ++idx; |
3351 | } | |
3352 | else | |
3353 | idx = 0; | |
3354 | ||
058b15c1 | 3355 | if (is_non_type) |
8d08fdba | 3356 | { |
058b15c1 MM |
3357 | parm = TREE_VALUE (parm); |
3358 | ||
833aa4c4 | 3359 | SET_DECL_TEMPLATE_PARM_P (parm); |
d490621d | 3360 | |
620188c9 | 3361 | if (TREE_TYPE (parm) == error_mark_node) |
0f67a82f LM |
3362 | { |
3363 | err_parm_list = build_tree_list (defval, parm); | |
3364 | TREE_VALUE (err_parm_list) = error_mark_node; | |
3365 | return chainon (list, err_parm_list); | |
3366 | } | |
620188c9 VR |
3367 | else |
3368 | { | |
3369 | /* [temp.param] | |
d490621d | 3370 | |
620188c9 VR |
3371 | The top-level cv-qualifiers on the template-parameter are |
3372 | ignored when determining its type. */ | |
3373 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
3374 | if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) | |
0f67a82f LM |
3375 | { |
3376 | err_parm_list = build_tree_list (defval, parm); | |
3377 | TREE_VALUE (err_parm_list) = error_mark_node; | |
3378 | return chainon (list, err_parm_list); | |
3379 | } | |
5d80a306 DG |
3380 | |
3381 | if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack) | |
3382 | { | |
3383 | /* This template parameter is not a parameter pack, but it | |
3384 | should be. Complain about "bare" parameter packs. */ | |
7b3e2d46 | 3385 | check_for_bare_parameter_packs (TREE_TYPE (parm)); |
5d80a306 DG |
3386 | |
3387 | /* Recover by calling this a parameter pack. */ | |
3388 | is_parameter_pack = true; | |
3389 | } | |
620188c9 | 3390 | } |
d490621d | 3391 | |
8d08fdba | 3392 | /* A template parameter is not modifiable. */ |
6de9cd9a | 3393 | TREE_CONSTANT (parm) = 1; |
6de9cd9a | 3394 | TREE_READONLY (parm) = 1; |
c2255bc4 AH |
3395 | decl = build_decl (parm_loc, |
3396 | CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); | |
6de9cd9a | 3397 | TREE_CONSTANT (decl) = 1; |
6de9cd9a | 3398 | TREE_READONLY (decl) = 1; |
c8094d83 | 3399 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
f84b4be9 JM |
3400 | = build_template_parm_index (idx, processing_template_decl, |
3401 | processing_template_decl, | |
3402 | decl, TREE_TYPE (parm)); | |
5d80a306 DG |
3403 | |
3404 | TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)) | |
3405 | = is_parameter_pack; | |
8d08fdba MS |
3406 | } |
3407 | else | |
3408 | { | |
73b0fce8 | 3409 | tree t; |
058b15c1 | 3410 | parm = TREE_VALUE (TREE_VALUE (parm)); |
c8094d83 | 3411 | |
73b0fce8 KL |
3412 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) |
3413 | { | |
9e1e64ec | 3414 | t = cxx_make_type (TEMPLATE_TEMPLATE_PARM); |
c8094d83 | 3415 | /* This is for distinguishing between real templates and template |
73b0fce8 KL |
3416 | template parameters */ |
3417 | TREE_TYPE (parm) = t; | |
3418 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
3419 | decl = parm; | |
3420 | } | |
3421 | else | |
3422 | { | |
9e1e64ec | 3423 | t = cxx_make_type (TEMPLATE_TYPE_PARM); |
f4f206f4 | 3424 | /* parm is either IDENTIFIER_NODE or NULL_TREE. */ |
c2255bc4 AH |
3425 | decl = build_decl (parm_loc, |
3426 | TYPE_DECL, parm, t); | |
73b0fce8 | 3427 | } |
c8094d83 | 3428 | |
d2e5ee5c MS |
3429 | TYPE_NAME (t) = decl; |
3430 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 3431 | parm = decl; |
f84b4be9 | 3432 | TEMPLATE_TYPE_PARM_INDEX (t) |
c8094d83 | 3433 | = build_template_parm_index (idx, processing_template_decl, |
f84b4be9 JM |
3434 | processing_template_decl, |
3435 | decl, TREE_TYPE (parm)); | |
5d80a306 | 3436 | TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack; |
06d40de8 | 3437 | TYPE_CANONICAL (t) = canonical_type_parameter (t); |
8d08fdba | 3438 | } |
c727aa5e | 3439 | DECL_ARTIFICIAL (decl) = 1; |
cd9f6678 | 3440 | SET_DECL_TEMPLATE_PARM_P (decl); |
8d08fdba | 3441 | pushdecl (decl); |
a292b002 | 3442 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
3443 | return chainon (list, parm); |
3444 | } | |
3445 | ||
3446 | /* The end of a template parameter list has been reached. Process the | |
3447 | tree list into a parameter vector, converting each parameter into a more | |
3448 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
3449 | as PARM_DECLs. */ | |
3450 | ||
3451 | tree | |
3a978d72 | 3452 | end_template_parm_list (tree parms) |
8d08fdba | 3453 | { |
5566b478 | 3454 | int nparms; |
9471d3e2 | 3455 | tree parm, next; |
5566b478 MS |
3456 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
3457 | ||
5566b478 | 3458 | current_template_parms |
4890c2f4 | 3459 | = tree_cons (size_int (processing_template_decl), |
5566b478 | 3460 | saved_parmlist, current_template_parms); |
8d08fdba | 3461 | |
9471d3e2 NS |
3462 | for (parm = parms, nparms = 0; parm; parm = next, nparms++) |
3463 | { | |
3464 | next = TREE_CHAIN (parm); | |
3465 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; | |
3466 | TREE_CHAIN (parm) = NULL_TREE; | |
3467 | } | |
a292b002 | 3468 | |
67ffc812 MM |
3469 | --processing_template_parmlist; |
3470 | ||
8d08fdba MS |
3471 | return saved_parmlist; |
3472 | } | |
3473 | ||
5566b478 MS |
3474 | /* end_template_decl is called after a template declaration is seen. */ |
3475 | ||
8d08fdba | 3476 | void |
3a978d72 | 3477 | end_template_decl (void) |
8d08fdba | 3478 | { |
386b8a85 JM |
3479 | reset_specialization (); |
3480 | ||
5156628f | 3481 | if (! processing_template_decl) |
73aad9b9 JM |
3482 | return; |
3483 | ||
5566b478 | 3484 | /* This matches the pushlevel in begin_template_parm_list. */ |
74b846e0 | 3485 | finish_scope (); |
8d08fdba | 3486 | |
5566b478 MS |
3487 | --processing_template_decl; |
3488 | current_template_parms = TREE_CHAIN (current_template_parms); | |
5566b478 | 3489 | } |
8d08fdba | 3490 | |
03c17ccd MM |
3491 | /* Within the declaration of a template, return all levels of template |
3492 | parameters that apply. The template parameters are represented as | |
3493 | a TREE_VEC, in the form documented in cp-tree.h for template | |
3494 | arguments. */ | |
9a3b49ac | 3495 | |
b5791fdc | 3496 | static tree |
3a978d72 | 3497 | current_template_args (void) |
5566b478 | 3498 | { |
36a117a5 | 3499 | tree header; |
b370501f | 3500 | tree args = NULL_TREE; |
36a117a5 | 3501 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
3502 | int l = length; |
3503 | ||
36a117a5 MM |
3504 | /* If there is only one level of template parameters, we do not |
3505 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
3506 | TREE_VEC containing the arguments. */ | |
3507 | if (length > 1) | |
3508 | args = make_tree_vec (length); | |
3509 | ||
3510 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 3511 | { |
5566b478 | 3512 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
3513 | int i; |
3514 | ||
5566b478 | 3515 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 3516 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 3517 | { |
98c1c668 JM |
3518 | tree t = TREE_VEC_ELT (a, i); |
3519 | ||
36a117a5 | 3520 | /* T will be a list if we are called from within a |
98c1c668 JM |
3521 | begin/end_template_parm_list pair, but a vector directly |
3522 | if within a begin/end_member_template_processing pair. */ | |
c8094d83 | 3523 | if (TREE_CODE (t) == TREE_LIST) |
98c1c668 JM |
3524 | { |
3525 | t = TREE_VALUE (t); | |
c8094d83 | 3526 | |
0b93f014 | 3527 | if (!error_operand_p (t)) |
0f67a82f LM |
3528 | { |
3529 | if (TREE_CODE (t) == TYPE_DECL | |
3530 | || TREE_CODE (t) == TEMPLATE_DECL) | |
5d80a306 DG |
3531 | { |
3532 | t = TREE_TYPE (t); | |
3533 | ||
3534 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) | |
3535 | { | |
3536 | /* Turn this argument into a TYPE_ARGUMENT_PACK | |
3537 | with a single element, which expands T. */ | |
3538 | tree vec = make_tree_vec (1); | |
3539 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3540 | ||
3541 | t = make_node (TYPE_ARGUMENT_PACK); | |
3542 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3543 | } | |
3544 | } | |
3545 | else | |
3546 | { | |
3547 | t = DECL_INITIAL (t); | |
3548 | ||
3549 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) | |
3550 | { | |
3551 | /* Turn this argument into a NONTYPE_ARGUMENT_PACK | |
3552 | with a single element, which expands T. */ | |
3553 | tree vec = make_tree_vec (1); | |
3554 | tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t)); | |
3555 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); | |
3556 | ||
3557 | t = make_node (NONTYPE_ARGUMENT_PACK); | |
3558 | SET_ARGUMENT_PACK_ARGS (t, vec); | |
3559 | TREE_TYPE (t) = type; | |
3560 | } | |
3561 | } | |
bf98d3b6 | 3562 | TREE_VEC_ELT (a, i) = t; |
5d80a306 | 3563 | } |
98c1c668 | 3564 | } |
5566b478 | 3565 | } |
36a117a5 MM |
3566 | |
3567 | if (length > 1) | |
3568 | TREE_VEC_ELT (args, --l) = a; | |
3569 | else | |
3570 | args = a; | |
8d08fdba MS |
3571 | } |
3572 | ||
9a3b49ac MS |
3573 | return args; |
3574 | } | |
75650646 | 3575 | |
e1467ff2 | 3576 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
c7222c02 | 3577 | template PARMS. If MEMBER_TEMPLATE_P is true, the new template is |
ee81147e | 3578 | a member template. Used by push_template_decl below. */ |
e1467ff2 | 3579 | |
75650646 | 3580 | static tree |
c7222c02 | 3581 | build_template_decl (tree decl, tree parms, bool member_template_p) |
75650646 MM |
3582 | { |
3583 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
3584 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
3585 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
c7222c02 | 3586 | DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; |
75650646 MM |
3587 | |
3588 | return tmpl; | |
3589 | } | |
3590 | ||
050367a3 MM |
3591 | struct template_parm_data |
3592 | { | |
6c30752f MM |
3593 | /* The level of the template parameters we are currently |
3594 | processing. */ | |
050367a3 | 3595 | int level; |
6c30752f MM |
3596 | |
3597 | /* The index of the specialization argument we are currently | |
3598 | processing. */ | |
3599 | int current_arg; | |
3600 | ||
3601 | /* An array whose size is the number of template parameters. The | |
838dfd8a | 3602 | elements are nonzero if the parameter has been used in any one |
6c30752f | 3603 | of the arguments processed so far. */ |
050367a3 | 3604 | int* parms; |
6c30752f MM |
3605 | |
3606 | /* An array whose size is the number of template arguments. The | |
838dfd8a | 3607 | elements are nonzero if the argument makes use of template |
6c30752f MM |
3608 | parameters of this level. */ |
3609 | int* arg_uses_template_parms; | |
050367a3 MM |
3610 | }; |
3611 | ||
3612 | /* Subroutine of push_template_decl used to see if each template | |
3613 | parameter in a partial specialization is used in the explicit | |
3614 | argument list. If T is of the LEVEL given in DATA (which is | |
3615 | treated as a template_parm_data*), then DATA->PARMS is marked | |
3616 | appropriately. */ | |
3617 | ||
3618 | static int | |
3a978d72 | 3619 | mark_template_parm (tree t, void* data) |
050367a3 MM |
3620 | { |
3621 | int level; | |
3622 | int idx; | |
3623 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
3624 | ||
3625 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
3626 | { | |
3627 | level = TEMPLATE_PARM_LEVEL (t); | |
3628 | idx = TEMPLATE_PARM_IDX (t); | |
3629 | } | |
3630 | else | |
3631 | { | |
3632 | level = TEMPLATE_TYPE_LEVEL (t); | |
3633 | idx = TEMPLATE_TYPE_IDX (t); | |
3634 | } | |
3635 | ||
3636 | if (level == tpd->level) | |
6c30752f MM |
3637 | { |
3638 | tpd->parms[idx] = 1; | |
3639 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
3640 | } | |
050367a3 MM |
3641 | |
3642 | /* Return zero so that for_each_template_parm will continue the | |
3643 | traversal of the tree; we want to mark *every* template parm. */ | |
3644 | return 0; | |
3645 | } | |
3646 | ||
6c30752f MM |
3647 | /* Process the partial specialization DECL. */ |
3648 | ||
e9659ab0 | 3649 | static tree |
3a978d72 | 3650 | process_partial_specialization (tree decl) |
6c30752f MM |
3651 | { |
3652 | tree type = TREE_TYPE (decl); | |
3653 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
3654 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
f9a7ae04 | 3655 | tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); |
6c30752f | 3656 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); |
b17c40be | 3657 | tree inner_parms; |
6c30752f | 3658 | int nargs = TREE_VEC_LENGTH (inner_args); |
b17c40be | 3659 | int ntparms; |
6c30752f MM |
3660 | int i; |
3661 | int did_error_intro = 0; | |
6c30752f MM |
3662 | struct template_parm_data tpd; |
3663 | struct template_parm_data tpd2; | |
3664 | ||
b17c40be PC |
3665 | gcc_assert (current_template_parms); |
3666 | ||
3667 | inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
3668 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
3669 | ||
6c30752f MM |
3670 | /* We check that each of the template parameters given in the |
3671 | partial specialization is used in the argument list to the | |
3672 | specialization. For example: | |
3673 | ||
3674 | template <class T> struct S; | |
3675 | template <class T> struct S<T*>; | |
3676 | ||
3677 | The second declaration is OK because `T*' uses the template | |
3678 | parameter T, whereas | |
3679 | ||
3680 | template <class T> struct S<int>; | |
3681 | ||
3682 | is no good. Even trickier is: | |
3683 | ||
3684 | template <class T> | |
3685 | struct S1 | |
3686 | { | |
3687 | template <class U> | |
3688 | struct S2; | |
3689 | template <class U> | |
3690 | struct S2<T>; | |
3691 | }; | |
3692 | ||
0e339752 | 3693 | The S2<T> declaration is actually invalid; it is a |
c8094d83 | 3694 | full-specialization. Of course, |
6c30752f MM |
3695 | |
3696 | template <class U> | |
3697 | struct S2<T (*)(U)>; | |
3698 | ||
3699 | or some such would have been OK. */ | |
3700 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
67f5655f | 3701 | tpd.parms = (int *) alloca (sizeof (int) * ntparms); |
fad205ff | 3702 | memset (tpd.parms, 0, sizeof (int) * ntparms); |
6c30752f | 3703 | |
67f5655f | 3704 | tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs); |
fad205ff | 3705 | memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); |
6c30752f MM |
3706 | for (i = 0; i < nargs; ++i) |
3707 | { | |
3708 | tpd.current_arg = i; | |
3709 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
3710 | &mark_template_parm, | |
4f2c9d7e | 3711 | &tpd, |
c095a4f8 DG |
3712 | NULL, |
3713 | /*include_nondeduced_p=*/false); | |
6c30752f MM |
3714 | } |
3715 | for (i = 0; i < ntparms; ++i) | |
3716 | if (tpd.parms[i] == 0) | |
3717 | { | |
3718 | /* One of the template parms was not used in the | |
f9a7ae04 | 3719 | specialization. */ |
6c30752f MM |
3720 | if (!did_error_intro) |
3721 | { | |
33bd39a2 | 3722 | error ("template parameters not used in partial specialization:"); |
6c30752f MM |
3723 | did_error_intro = 1; |
3724 | } | |
3725 | ||
0f51ccfc | 3726 | error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
6c30752f MM |
3727 | } |
3728 | ||
3729 | /* [temp.class.spec] | |
3730 | ||
3731 | The argument list of the specialization shall not be identical to | |
3732 | the implicit argument list of the primary template. */ | |
c8094d83 MS |
3733 | if (comp_template_args |
3734 | (inner_args, | |
f9a7ae04 MM |
3735 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE |
3736 | (maintmpl))))) | |
0f51ccfc | 3737 | error ("partial specialization %qT does not specialize any template arguments", type); |
6c30752f MM |
3738 | |
3739 | /* [temp.class.spec] | |
3740 | ||
3741 | A partially specialized non-type argument expression shall not | |
3742 | involve template parameters of the partial specialization except | |
3743 | when the argument expression is a simple identifier. | |
3744 | ||
3745 | The type of a template parameter corresponding to a specialized | |
3746 | non-type argument shall not be dependent on a parameter of the | |
5d80a306 DG |
3747 | specialization. |
3748 | ||
3749 | Also, we verify that pack expansions only occur at the | |
3750 | end of the argument list. */ | |
50bc768d | 3751 | gcc_assert (nargs == DECL_NTPARMS (maintmpl)); |
6c30752f MM |
3752 | tpd2.parms = 0; |
3753 | for (i = 0; i < nargs; ++i) | |
3754 | { | |
5d80a306 | 3755 | tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i)); |
6c30752f | 3756 | tree arg = TREE_VEC_ELT (inner_args, i); |
5d80a306 DG |
3757 | tree packed_args = NULL_TREE; |
3758 | int j, len = 1; | |
6c30752f | 3759 | |
5d80a306 DG |
3760 | if (ARGUMENT_PACK_P (arg)) |
3761 | { | |
3762 | /* Extract the arguments from the argument pack. We'll be | |
3763 | iterating over these in the following loop. */ | |
3764 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
3765 | len = TREE_VEC_LENGTH (packed_args); | |
3766 | } | |
3767 | ||
3768 | for (j = 0; j < len; j++) | |
3769 | { | |
3770 | if (packed_args) | |
3771 | /* Get the Jth argument in the parameter pack. */ | |
3772 | arg = TREE_VEC_ELT (packed_args, j); | |
3773 | ||
3774 | if (PACK_EXPANSION_P (arg)) | |
3775 | { | |
3776 | /* Pack expansions must come at the end of the | |
3777 | argument list. */ | |
3778 | if ((packed_args && j < len - 1) | |
3779 | || (!packed_args && i < nargs - 1)) | |
3780 | { | |
3781 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
3782 | error ("parameter pack argument %qE must be at the end of the template argument list", arg); | |
3783 | else | |
1ad8aeeb DG |
3784 | error ("parameter pack argument %qT must be at the end of the template argument list", arg); |
3785 | ||
3786 | if (packed_args) | |
3787 | TREE_VEC_ELT (packed_args, j) = error_mark_node; | |
5d80a306 DG |
3788 | } |
3789 | } | |
3790 | ||
3791 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
3792 | /* We only care about the pattern. */ | |
3793 | arg = PACK_EXPANSION_PATTERN (arg); | |
3794 | ||
3795 | if (/* These first two lines are the `non-type' bit. */ | |
3796 | !TYPE_P (arg) | |
3797 | && TREE_CODE (arg) != TEMPLATE_DECL | |
3798 | /* This next line is the `argument expression is not just a | |
3799 | simple identifier' condition and also the `specialized | |
3800 | non-type argument' bit. */ | |
3801 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
3802 | { | |
3803 | if ((!packed_args && tpd.arg_uses_template_parms[i]) | |
3804 | || (packed_args && uses_template_parms (arg))) | |
3805 | error ("template argument %qE involves template parameter(s)", | |
3806 | arg); | |
3807 | else | |
3808 | { | |
3809 | /* Look at the corresponding template parameter, | |
3810 | marking which template parameters its type depends | |
3811 | upon. */ | |
3812 | tree type = TREE_TYPE (parm); | |
3813 | ||
3814 | if (!tpd2.parms) | |
3815 | { | |
3816 | /* We haven't yet initialized TPD2. Do so now. */ | |
3817 | tpd2.arg_uses_template_parms | |
3818 | = (int *) alloca (sizeof (int) * nargs); | |
3819 | /* The number of parameters here is the number in the | |
3820 | main template, which, as checked in the assertion | |
3821 | above, is NARGS. */ | |
3822 | tpd2.parms = (int *) alloca (sizeof (int) * nargs); | |
3823 | tpd2.level = | |
3824 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
3825 | } | |
3826 | ||
3827 | /* Mark the template parameters. But this time, we're | |
3828 | looking for the template parameters of the main | |
3829 | template, not in the specialization. */ | |
3830 | tpd2.current_arg = i; | |
3831 | tpd2.arg_uses_template_parms[i] = 0; | |
3832 | memset (tpd2.parms, 0, sizeof (int) * nargs); | |
3833 | for_each_template_parm (type, | |
3834 | &mark_template_parm, | |
3835 | &tpd2, | |
c095a4f8 DG |
3836 | NULL, |
3837 | /*include_nondeduced_p=*/false); | |
5d80a306 DG |
3838 | |
3839 | if (tpd2.arg_uses_template_parms [i]) | |
3840 | { | |
3841 | /* The type depended on some template parameters. | |
3842 | If they are fully specialized in the | |
3843 | specialization, that's OK. */ | |
3844 | int j; | |
3845 | for (j = 0; j < nargs; ++j) | |
3846 | if (tpd2.parms[j] != 0 | |
3847 | && tpd.arg_uses_template_parms [j]) | |
3848 | { | |
3849 | error ("type %qT of template argument %qE depends " | |
3850 | "on template parameter(s)", | |
3851 | type, | |
3852 | arg); | |
3853 | break; | |
3854 | } | |
3855 | } | |
3856 | } | |
3857 | } | |
3858 | } | |
6c30752f MM |
3859 | } |
3860 | ||
2b110bfc JM |
3861 | /* We should only get here once. */ |
3862 | gcc_assert (!COMPLETE_TYPE_P (type)); | |
6c30752f | 3863 | |
d8b64f80 | 3864 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) |
916b63c3 | 3865 | = tree_cons (specargs, inner_parms, |
b1d7b1c0 | 3866 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); |
6c30752f MM |
3867 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
3868 | return decl; | |
3869 | } | |
3870 | ||
85d85234 DG |
3871 | /* Check that a template declaration's use of default arguments and |
3872 | parameter packs is not invalid. Here, PARMS are the template | |
3873 | parameters. IS_PRIMARY is nonzero if DECL is the thing declared by | |
3874 | a primary template. IS_PARTIAL is nonzero if DECL is a partial | |
3875 | specialization. | |
9b7dd5e8 | 3876 | |
6ba89f8e | 3877 | |
9b7dd5e8 DG |
3878 | IS_FRIEND_DECL is nonzero if DECL is a friend function template |
3879 | declaration (but not a definition); 1 indicates a declaration, 2 | |
3880 | indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are | |
3881 | emitted for extraneous default arguments. | |
3882 | ||
3883 | Returns TRUE if there were no errors found, FALSE otherwise. */ | |
3884 | ||
3885 | bool | |
3886 | check_default_tmpl_args (tree decl, tree parms, int is_primary, | |
3887 | int is_partial, int is_friend_decl) | |
6ba89f8e | 3888 | { |
d8e178a0 | 3889 | const char *msg; |
66191c20 MM |
3890 | int last_level_to_check; |
3891 | tree parm_level; | |
9b7dd5e8 | 3892 | bool no_errors = true; |
6ba89f8e | 3893 | |
c8094d83 | 3894 | /* [temp.param] |
6ba89f8e MM |
3895 | |
3896 | A default template-argument shall not be specified in a | |
3897 | function template declaration or a function template definition, nor | |
3898 | in the template-parameter-list of the definition of a member of a | |
3899 | class template. */ | |
3900 | ||
4f1c5b7d | 3901 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
e0942dcd MM |
3902 | /* You can't have a function template declaration in a local |
3903 | scope, nor you can you define a member of a class template in a | |
3904 | local scope. */ | |
9b7dd5e8 | 3905 | return true; |
e0942dcd | 3906 | |
6ba89f8e MM |
3907 | if (current_class_type |
3908 | && !TYPE_BEING_DEFINED (current_class_type) | |
3d7de1fa | 3909 | && DECL_LANG_SPECIFIC (decl) |
b97e8a14 | 3910 | && DECL_DECLARES_FUNCTION_P (decl) |
5937a6f9 MM |
3911 | /* If this is either a friend defined in the scope of the class |
3912 | or a member function. */ | |
6df5158a NS |
3913 | && (DECL_FUNCTION_MEMBER_P (decl) |
3914 | ? same_type_p (DECL_CONTEXT (decl), current_class_type) | |
3915 | : DECL_FRIEND_CONTEXT (decl) | |
3916 | ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) | |
3917 | : false) | |
5937a6f9 MM |
3918 | /* And, if it was a member function, it really was defined in |
3919 | the scope of the class. */ | |
6df5158a NS |
3920 | && (!DECL_FUNCTION_MEMBER_P (decl) |
3921 | || DECL_INITIALIZED_IN_CLASS_P (decl))) | |
6ba89f8e | 3922 | /* We already checked these parameters when the template was |
5937a6f9 MM |
3923 | declared, so there's no need to do it again now. This function |
3924 | was defined in class scope, but we're processing it's body now | |
3925 | that the class is complete. */ | |
9b7dd5e8 | 3926 | return true; |
c8094d83 | 3927 | |
9b7dd5e8 DG |
3928 | /* Core issue 226 (C++0x only): the following only applies to class |
3929 | templates. */ | |
c1ae8be5 | 3930 | if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL) |
66191c20 | 3931 | { |
9b7dd5e8 | 3932 | /* [temp.param] |
66191c20 | 3933 | |
9b7dd5e8 DG |
3934 | If a template-parameter has a default template-argument, all |
3935 | subsequent template-parameters shall have a default | |
3936 | template-argument supplied. */ | |
3937 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
3938 | { | |
3939 | tree inner_parms = TREE_VALUE (parm_level); | |
3940 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
3941 | int seen_def_arg_p = 0; | |
3942 | int i; | |
42b304f1 | 3943 | |
9b7dd5e8 DG |
3944 | for (i = 0; i < ntparms; ++i) |
3945 | { | |
3946 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
42b304f1 | 3947 | |
9b7dd5e8 DG |
3948 | if (parm == error_mark_node) |
3949 | continue; | |
3950 | ||
3951 | if (TREE_PURPOSE (parm)) | |
3952 | seen_def_arg_p = 1; | |
ffaf1e05 JM |
3953 | else if (seen_def_arg_p |
3954 | && !template_parameter_pack_p (TREE_VALUE (parm))) | |
9b7dd5e8 DG |
3955 | { |
3956 | error ("no default argument for %qD", TREE_VALUE (parm)); | |
3957 | /* For better subsequent error-recovery, we indicate that | |
3958 | there should have been a default argument. */ | |
3959 | TREE_PURPOSE (parm) = error_mark_node; | |
3960 | no_errors = false; | |
3961 | } | |
85d85234 DG |
3962 | else if (is_primary |
3963 | && !is_partial | |
3964 | && !is_friend_decl | |
ffaf1e05 JM |
3965 | /* Don't complain about an enclosing partial |
3966 | specialization. */ | |
3967 | && parm_level == parms | |
85d85234 DG |
3968 | && TREE_CODE (decl) == TYPE_DECL |
3969 | && i < ntparms - 1 | |
3970 | && template_parameter_pack_p (TREE_VALUE (parm))) | |
3971 | { | |
3972 | /* A primary class template can only have one | |
3973 | parameter pack, at the end of the template | |
3974 | parameter list. */ | |
3975 | ||
3976 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL) | |
3977 | error ("parameter pack %qE must be at the end of the" | |
3978 | " template parameter list", TREE_VALUE (parm)); | |
3979 | else | |
3980 | error ("parameter pack %qT must be at the end of the" | |
3981 | " template parameter list", | |
3982 | TREE_TYPE (TREE_VALUE (parm))); | |
3983 | ||
3984 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i)) | |
3985 | = error_mark_node; | |
3986 | no_errors = false; | |
3987 | } | |
9b7dd5e8 DG |
3988 | } |
3989 | } | |
66191c20 MM |
3990 | } |
3991 | ||
c1ae8be5 | 3992 | if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL) |
9b7dd5e8 DG |
3993 | || is_partial |
3994 | || !is_primary | |
3995 | || is_friend_decl) | |
6ba89f8e MM |
3996 | /* For an ordinary class template, default template arguments are |
3997 | allowed at the innermost level, e.g.: | |
0cbd7506 | 3998 | template <class T = int> |
6ba89f8e MM |
3999 | struct S {}; |
4000 | but, in a partial specialization, they're not allowed even | |
4001 | there, as we have in [temp.class.spec]: | |
c8094d83 | 4002 | |
6ba89f8e | 4003 | The template parameter list of a specialization shall not |
c8094d83 | 4004 | contain default template argument values. |
6ba89f8e | 4005 | |
9b7dd5e8 DG |
4006 | So, for a partial specialization, or for a function template |
4007 | (in C++98/C++03), we look at all of them. */ | |
6ba89f8e MM |
4008 | ; |
4009 | else | |
4010 | /* But, for a primary class template that is not a partial | |
4011 | specialization we look at all template parameters except the | |
4012 | innermost ones. */ | |
4013 | parms = TREE_CHAIN (parms); | |
4014 | ||
4015 | /* Figure out what error message to issue. */ | |
9b7dd5e8 DG |
4016 | if (is_friend_decl == 2) |
4017 | msg = "default template arguments may not be used in function template friend re-declaration"; | |
4018 | else if (is_friend_decl) | |
4019 | msg = "default template arguments may not be used in function template friend declarations"; | |
c1ae8be5 | 4020 | else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98)) |
8653a2c3 | 4021 | msg = "default template arguments may not be used in function templates"; |
6ba89f8e | 4022 | else if (is_partial) |
8653a2c3 | 4023 | msg = "default template arguments may not be used in partial specializations"; |
6ba89f8e | 4024 | else |
0f51ccfc | 4025 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e MM |
4026 | |
4027 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
4028 | /* If we're inside a class definition, there's no need to | |
104bf76a | 4029 | examine the parameters to the class itself. On the one |
6ba89f8e | 4030 | hand, they will be checked when the class is defined, and, |
0e339752 | 4031 | on the other, default arguments are valid in things like: |
0cbd7506 MS |
4032 | template <class T = double> |
4033 | struct S { template <class U> void f(U); }; | |
6ba89f8e MM |
4034 | Here the default argument for `S' has no bearing on the |
4035 | declaration of `f'. */ | |
4036 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
4037 | else | |
4038 | /* Check everything. */ | |
4039 | last_level_to_check = 0; | |
4040 | ||
c8094d83 MS |
4041 | for (parm_level = parms; |
4042 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
66191c20 | 4043 | parm_level = TREE_CHAIN (parm_level)) |
6ba89f8e | 4044 | { |
66191c20 MM |
4045 | tree inner_parms = TREE_VALUE (parm_level); |
4046 | int i; | |
4047 | int ntparms; | |
6ba89f8e MM |
4048 | |
4049 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
c8094d83 | 4050 | for (i = 0; i < ntparms; ++i) |
42b304f1 LM |
4051 | { |
4052 | if (TREE_VEC_ELT (inner_parms, i) == error_mark_node) | |
4053 | continue; | |
6ba89f8e | 4054 | |
42b304f1 LM |
4055 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) |
4056 | { | |
4057 | if (msg) | |
4058 | { | |
9b7dd5e8 DG |
4059 | no_errors = false; |
4060 | if (is_friend_decl == 2) | |
4061 | return no_errors; | |
4062 | ||
42b304f1 LM |
4063 | error (msg, decl); |
4064 | msg = 0; | |
4065 | } | |
4066 | ||
4067 | /* Clear out the default argument so that we are not | |
4068 | confused later. */ | |
4069 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
4070 | } | |
4071 | } | |
6ba89f8e MM |
4072 | |
4073 | /* At this point, if we're still interested in issuing messages, | |
4074 | they must apply to classes surrounding the object declared. */ | |
4075 | if (msg) | |
c8094d83 | 4076 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e | 4077 | } |
9b7dd5e8 DG |
4078 | |
4079 | return no_errors; | |
6ba89f8e MM |
4080 | } |
4081 | ||
5dd236e2 NS |
4082 | /* Worker for push_template_decl_real, called via |
4083 | for_each_template_parm. DATA is really an int, indicating the | |
4084 | level of the parameters we are interested in. If T is a template | |
838dfd8a | 4085 | parameter of that level, return nonzero. */ |
5dd236e2 NS |
4086 | |
4087 | static int | |
3a978d72 | 4088 | template_parm_this_level_p (tree t, void* data) |
5dd236e2 | 4089 | { |
6e04241f | 4090 | int this_level = *(int *)data; |
5dd236e2 NS |
4091 | int level; |
4092 | ||
4093 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
4094 | level = TEMPLATE_PARM_LEVEL (t); | |
4095 | else | |
4096 | level = TEMPLATE_TYPE_LEVEL (t); | |
4097 | return level == this_level; | |
4098 | } | |
4099 | ||
3ac3d9ea | 4100 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
4101 | parameters given by current_template_args, or reuses a |
4102 | previously existing one, if appropriate. Returns the DECL, or an | |
c8094d83 | 4103 | equivalent one, if it is replaced via a call to duplicate_decls. |
6757edfe | 4104 | |
d63d5d0c | 4105 | If IS_FRIEND is true, DECL is a friend declaration. */ |
3ac3d9ea MM |
4106 | |
4107 | tree | |
d63d5d0c | 4108 | push_template_decl_real (tree decl, bool is_friend) |
9a3b49ac MS |
4109 | { |
4110 | tree tmpl; | |
f84b4be9 | 4111 | tree args; |
9a3b49ac | 4112 | tree info; |
f84b4be9 JM |
4113 | tree ctx; |
4114 | int primary; | |
6ba89f8e | 4115 | int is_partial; |
cfe507be | 4116 | int new_template_p = 0; |
c7222c02 MM |
4117 | /* True if the template is a member template, in the sense of |
4118 | [temp.mem]. */ | |
4119 | bool member_template_p = false; | |
6ba89f8e | 4120 | |
b17c40be PC |
4121 | if (decl == error_mark_node || !current_template_parms) |
4122 | return error_mark_node; | |
c0694c4b | 4123 | |
6ba89f8e | 4124 | /* See if this is a partial specialization. */ |
9188c363 | 4125 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
6ba89f8e | 4126 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
370af2d5 | 4127 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); |
6757edfe | 4128 | |
d63d5d0c ILT |
4129 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)) |
4130 | is_friend = true; | |
f84b4be9 JM |
4131 | |
4132 | if (is_friend) | |
4133 | /* For a friend, we want the context of the friend function, not | |
4134 | the type of which it is a friend. */ | |
4135 | ctx = DECL_CONTEXT (decl); | |
4f1c5b7d MM |
4136 | else if (CP_DECL_CONTEXT (decl) |
4137 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
4138 | /* In the case of a virtual function, we want the class in which |
4139 | it is defined. */ | |
4f1c5b7d | 4140 | ctx = CP_DECL_CONTEXT (decl); |
f84b4be9 | 4141 | else |
dc957d14 | 4142 | /* Otherwise, if we're currently defining some class, the DECL |
f84b4be9 | 4143 | is assumed to be a member of the class. */ |
9188c363 | 4144 | ctx = current_scope (); |
f84b4be9 | 4145 | |
2c73f9f5 ML |
4146 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
4147 | ctx = NULL_TREE; | |
4148 | ||
4149 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 4150 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 4151 | |
6ba89f8e | 4152 | /* See if this is a primary template. */ |
c9cbfca6 JM |
4153 | if (is_friend && ctx) |
4154 | /* A friend template that specifies a class context, i.e. | |
4155 | template <typename T> friend void A<T>::f(); | |
4156 | is not primary. */ | |
4157 | primary = 0; | |
4158 | else | |
4159 | primary = template_parm_scope_p (); | |
9a3b49ac | 4160 | |
83566abf JM |
4161 | if (primary) |
4162 | { | |
c7222c02 MM |
4163 | if (DECL_CLASS_SCOPE_P (decl)) |
4164 | member_template_p = true; | |
2f1b1731 MM |
4165 | if (TREE_CODE (decl) == TYPE_DECL |
4166 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
80d7287f PC |
4167 | { |
4168 | error ("template class without a name"); | |
4169 | return error_mark_node; | |
4170 | } | |
717a7d5d | 4171 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
4b0d3cbe | 4172 | { |
717a7d5d MM |
4173 | if (DECL_DESTRUCTOR_P (decl)) |
4174 | { | |
4175 | /* [temp.mem] | |
c8094d83 | 4176 | |
0cbd7506 | 4177 | A destructor shall not be a member template. */ |
0f51ccfc | 4178 | error ("destructor %qD declared as member template", decl); |
717a7d5d MM |
4179 | return error_mark_node; |
4180 | } | |
4181 | if (NEW_DELETE_OPNAME_P (DECL_NAME (decl)) | |
4182 | && (!TYPE_ARG_TYPES (TREE_TYPE (decl)) | |
4183 | || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node | |
4184 | || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) | |
4185 | || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl)))) | |
4186 | == void_list_node))) | |
4187 | { | |
c8094d83 | 4188 | /* [basic.stc.dynamic.allocation] |
717a7d5d | 4189 | |
0cbd7506 | 4190 | An allocation function can be a function |
717a7d5d MM |
4191 | template. ... Template allocation functions shall |
4192 | have two or more parameters. */ | |
0f51ccfc | 4193 | error ("invalid template declaration of %qD", decl); |
7c60008e | 4194 | return error_mark_node; |
717a7d5d | 4195 | } |
4b0d3cbe | 4196 | } |
8ca4bf25 MM |
4197 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
4198 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
2aaf816d JM |
4199 | /* OK */; |
4200 | else | |
11325dcd | 4201 | { |
0f51ccfc | 4202 | error ("template declaration of %q#D", decl); |
11325dcd KL |
4203 | return error_mark_node; |
4204 | } | |
83566abf JM |
4205 | } |
4206 | ||
6ba89f8e MM |
4207 | /* Check to see that the rules regarding the use of default |
4208 | arguments are not being violated. */ | |
c8094d83 | 4209 | check_default_tmpl_args (decl, current_template_parms, |
9b7dd5e8 | 4210 | primary, is_partial, /*is_friend_decl=*/0); |
73aad9b9 | 4211 | |
5d80a306 DG |
4212 | /* Ensure that there are no parameter packs in the type of this |
4213 | declaration that have not been expanded. */ | |
b1d7b1c0 DG |
4214 | if (TREE_CODE (decl) == FUNCTION_DECL) |
4215 | { | |
4216 | /* Check each of the arguments individually to see if there are | |
4217 | any bare parameter packs. */ | |
4218 | tree type = TREE_TYPE (decl); | |
4219 | tree arg = DECL_ARGUMENTS (decl); | |
4220 | tree argtype = TYPE_ARG_TYPES (type); | |
4221 | ||
4222 | while (arg && argtype) | |
4223 | { | |
4224 | if (!FUNCTION_PARAMETER_PACK_P (arg) | |
7b3e2d46 | 4225 | && check_for_bare_parameter_packs (TREE_TYPE (arg))) |
b1d7b1c0 DG |
4226 | { |
4227 | /* This is a PARM_DECL that contains unexpanded parameter | |
4228 | packs. We have already complained about this in the | |
4229 | check_for_bare_parameter_packs call, so just replace | |
4230 | these types with ERROR_MARK_NODE. */ | |
4231 | TREE_TYPE (arg) = error_mark_node; | |
4232 | TREE_VALUE (argtype) = error_mark_node; | |
4233 | } | |
4234 | ||
4235 | arg = TREE_CHAIN (arg); | |
4236 | argtype = TREE_CHAIN (argtype); | |
4237 | } | |
4238 | ||
4239 | /* Check for bare parameter packs in the return type and the | |
4240 | exception specifiers. */ | |
7b3e2d46 | 4241 | if (check_for_bare_parameter_packs (TREE_TYPE (type))) |
046e4071 JJ |
4242 | /* Errors were already issued, set return type to int |
4243 | as the frontend doesn't expect error_mark_node as | |
4244 | the return type. */ | |
4245 | TREE_TYPE (type) = integer_type_node; | |
7b3e2d46 DG |
4246 | if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type))) |
4247 | TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE; | |
b1d7b1c0 | 4248 | } |
7b3e2d46 | 4249 | else if (check_for_bare_parameter_packs (TREE_TYPE (decl))) |
92fab505 DG |
4250 | { |
4251 | TREE_TYPE (decl) = error_mark_node; | |
4252 | return error_mark_node; | |
4253 | } | |
5d80a306 | 4254 | |
6ba89f8e MM |
4255 | if (is_partial) |
4256 | return process_partial_specialization (decl); | |
d32789d8 | 4257 | |
9a3b49ac MS |
4258 | args = current_template_args (); |
4259 | ||
c8094d83 | 4260 | if (!ctx |
f84b4be9 | 4261 | || TREE_CODE (ctx) == FUNCTION_DECL |
a723baf1 | 4262 | || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) |
f84b4be9 | 4263 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) |
8d08fdba | 4264 | { |
75650646 | 4265 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
4266 | && DECL_TEMPLATE_INFO (decl) |
4267 | && DECL_TI_TEMPLATE (decl)) | |
4268 | tmpl = DECL_TI_TEMPLATE (decl); | |
1c10870d AS |
4269 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
4270 | be DECL_LANG_SPECIFIC. The information equivalent to | |
4271 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
c8094d83 | 4272 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
1c10870d AS |
4273 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) |
4274 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
4275 | { | |
4276 | /* Since a template declaration already existed for this | |
4277 | class-type, we must be redeclaring it here. Make sure | |
0e339752 | 4278 | that the redeclaration is valid. */ |
1c10870d AS |
4279 | redeclare_class_template (TREE_TYPE (decl), |
4280 | current_template_parms); | |
4281 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
4282 | one we already had. */ | |
4283 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
4284 | } | |
f84b4be9 | 4285 | else |
786b5245 | 4286 | { |
c7222c02 MM |
4287 | tmpl = build_template_decl (decl, current_template_parms, |
4288 | member_template_p); | |
cfe507be MM |
4289 | new_template_p = 1; |
4290 | ||
f84b4be9 JM |
4291 | if (DECL_LANG_SPECIFIC (decl) |
4292 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
4293 | { | |
4294 | /* A specialization of a member template of a template | |
c6002625 | 4295 | class. */ |
f84b4be9 JM |
4296 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
4297 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
4298 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
4299 | } | |
786b5245 | 4300 | } |
8d08fdba MS |
4301 | } |
4302 | else | |
4303 | { | |
e1a5ccf7 | 4304 | tree a, t, current, parms; |
ba4f4e5d | 4305 | int i; |
91a77d68 | 4306 | tree tinfo = get_template_info (decl); |
6633d636 | 4307 | |
91a77d68 | 4308 | if (!tinfo) |
c91a56d2 | 4309 | { |
0f51ccfc | 4310 | error ("template definition of non-template %q#D", decl); |
3fe82414 | 4311 | return error_mark_node; |
c91a56d2 | 4312 | } |
91a77d68 JM |
4313 | |
4314 | tmpl = TI_TEMPLATE (tinfo); | |
c8094d83 | 4315 | |
c353b8e3 | 4316 | if (DECL_FUNCTION_TEMPLATE_P (tmpl) |
c8094d83 | 4317 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) |
c353b8e3 | 4318 | && DECL_TEMPLATE_SPECIALIZATION (decl) |
c7222c02 | 4319 | && DECL_MEMBER_TEMPLATE_P (tmpl)) |
98c1c668 | 4320 | { |
e1a5ccf7 JM |
4321 | tree new_tmpl; |
4322 | ||
4323 | /* The declaration is a specialization of a member | |
4324 | template, declared outside the class. Therefore, the | |
4325 | innermost template arguments will be NULL, so we | |
4326 | replace them with the arguments determined by the | |
4327 | earlier call to check_explicit_specialization. */ | |
4328 | args = DECL_TI_ARGS (decl); | |
4329 | ||
c8094d83 | 4330 | new_tmpl |
c7222c02 MM |
4331 | = build_template_decl (decl, current_template_parms, |
4332 | member_template_p); | |
e1a5ccf7 JM |
4333 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; |
4334 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
4335 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
4336 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
c8094d83 | 4337 | DECL_TEMPLATE_INFO (new_tmpl) |
e1b3e07d | 4338 | = tree_cons (tmpl, args, NULL_TREE); |
e1a5ccf7 | 4339 | |
c8094d83 MS |
4340 | register_specialization (new_tmpl, |
4341 | most_general_template (tmpl), | |
d63d5d0c | 4342 | args, |
2b110bfc | 4343 | is_friend, 0); |
e1a5ccf7 | 4344 | return decl; |
98c1c668 | 4345 | } |
98c1c668 | 4346 | |
e1a5ccf7 | 4347 | /* Make sure the template headers we got make sense. */ |
6633d636 | 4348 | |
e1a5ccf7 JM |
4349 | parms = DECL_TEMPLATE_PARMS (tmpl); |
4350 | i = TMPL_PARMS_DEPTH (parms); | |
4351 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 4352 | { |
0f51ccfc | 4353 | error ("expected %d levels of template parms for %q#D, got %d", |
0cbd7506 | 4354 | i, decl, TMPL_ARGS_DEPTH (args)); |
98c1c668 | 4355 | } |
e1a5ccf7 JM |
4356 | else |
4357 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
4358 | { | |
4359 | a = TMPL_ARGS_LEVEL (args, i); | |
4360 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
4361 | ||
4362 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
4363 | { | |
4364 | if (current == decl) | |
0f51ccfc | 4365 | error ("got %d template parameters for %q#D", |
0cbd7506 | 4366 | TREE_VEC_LENGTH (a), decl); |
e1a5ccf7 | 4367 | else |
0f51ccfc | 4368 | error ("got %d template parameters for %q#T", |
0cbd7506 | 4369 | TREE_VEC_LENGTH (a), current); |
33bd39a2 | 4370 | error (" but %d required", TREE_VEC_LENGTH (t)); |
f1cc0515 | 4371 | return error_mark_node; |
e1a5ccf7 | 4372 | } |
98c1c668 | 4373 | |
e1a5ccf7 JM |
4374 | if (current == decl) |
4375 | current = ctx; | |
4376 | else | |
ba75df2b PC |
4377 | current = (TYPE_P (current) |
4378 | ? TYPE_CONTEXT (current) | |
4379 | : DECL_CONTEXT (current)); | |
e1a5ccf7 | 4380 | } |
91a77d68 JM |
4381 | |
4382 | /* Check that the parms are used in the appropriate qualifying scopes | |
4383 | in the declarator. */ | |
4384 | if (!comp_template_args | |
4385 | (TI_ARGS (tinfo), | |
4386 | TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl))))) | |
4387 | { | |
4388 | error ("\ | |
4389 | template arguments to %qD do not match original template %qD", | |
4390 | decl, DECL_TEMPLATE_RESULT (tmpl)); | |
4391 | if (!uses_template_parms (TI_ARGS (tinfo))) | |
1f5b3869 | 4392 | inform (input_location, "use template<> for an explicit specialization"); |
91a77d68 JM |
4393 | /* Avoid crash in import_export_decl. */ |
4394 | DECL_INTERFACE_KNOWN (decl) = 1; | |
4395 | return error_mark_node; | |
4396 | } | |
5566b478 | 4397 | } |
8d08fdba | 4398 | |
5566b478 MS |
4399 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
4400 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 4401 | |
36a117a5 MM |
4402 | /* Push template declarations for global functions and types. Note |
4403 | that we do not try to push a global template friend declared in a | |
4404 | template class; such a thing may well depend on the template | |
39c01e4c | 4405 | parameters of the class. */ |
c8094d83 | 4406 | if (new_template_p && !ctx |
36a117a5 | 4407 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
c6f9f83b | 4408 | { |
d63d5d0c | 4409 | tmpl = pushdecl_namespace_level (tmpl, is_friend); |
c6f9f83b KL |
4410 | if (tmpl == error_mark_node) |
4411 | return error_mark_node; | |
bd3d082e KL |
4412 | |
4413 | /* Hide template friend classes that haven't been declared yet. */ | |
4414 | if (is_friend && TREE_CODE (decl) == TYPE_DECL) | |
4415 | { | |
4416 | DECL_ANTICIPATED (tmpl) = 1; | |
4417 | DECL_FRIEND_P (tmpl) = 1; | |
4418 | } | |
c6f9f83b | 4419 | } |
8d08fdba | 4420 | |
5566b478 | 4421 | if (primary) |
5dd236e2 | 4422 | { |
ef3b7b17 JM |
4423 | tree parms = DECL_TEMPLATE_PARMS (tmpl); |
4424 | int i; | |
4425 | ||
5dd236e2 NS |
4426 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
4427 | if (DECL_CONV_FN_P (tmpl)) | |
4428 | { | |
ef3b7b17 | 4429 | int depth = TMPL_PARMS_DEPTH (parms); |
6e04241f | 4430 | |
5dd236e2 NS |
4431 | /* It is a conversion operator. See if the type converted to |
4432 | depends on innermost template operands. */ | |
c8094d83 | 4433 | |
d43f603d KL |
4434 | if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)), |
4435 | depth)) | |
5dd236e2 NS |
4436 | DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; |
4437 | } | |
ef3b7b17 JM |
4438 | |
4439 | /* Give template template parms a DECL_CONTEXT of the template | |
4440 | for which they are a parameter. */ | |
4441 | parms = INNERMOST_TEMPLATE_PARMS (parms); | |
4442 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) | |
4443 | { | |
4444 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
4445 | if (TREE_CODE (parm) == TEMPLATE_DECL) | |
4446 | DECL_CONTEXT (parm) = tmpl; | |
4447 | } | |
5dd236e2 | 4448 | } |
5566b478 | 4449 | |
a692ad2e | 4450 | /* The DECL_TI_ARGS of DECL contains full set of arguments referring |
28ba38e3 KL |
4451 | back to its most general template. If TMPL is a specialization, |
4452 | ARGS may only have the innermost set of arguments. Add the missing | |
4453 | argument levels if necessary. */ | |
4454 | if (DECL_TEMPLATE_INFO (tmpl)) | |
4455 | args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args); | |
4456 | ||
e1b3e07d | 4457 | info = tree_cons (tmpl, args, NULL_TREE); |
5566b478 | 4458 | |
9188c363 | 4459 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
a0ad3539 | 4460 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
2aaf816d | 4461 | else if (DECL_LANG_SPECIFIC (decl)) |
5566b478 | 4462 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
4463 | |
4464 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
4465 | } |
4466 | ||
6757edfe | 4467 | tree |
3a978d72 | 4468 | push_template_decl (tree decl) |
6757edfe | 4469 | { |
d63d5d0c | 4470 | return push_template_decl_real (decl, false); |
6757edfe MM |
4471 | } |
4472 | ||
4473 | /* Called when a class template TYPE is redeclared with the indicated | |
4474 | template PARMS, e.g.: | |
7fe6899f MM |
4475 | |
4476 | template <class T> struct S; | |
4477 | template <class T> struct S {}; */ | |
4478 | ||
60feef2c | 4479 | bool |
3a978d72 | 4480 | redeclare_class_template (tree type, tree parms) |
7fe6899f | 4481 | { |
3d7de1fa | 4482 | tree tmpl; |
6757edfe | 4483 | tree tmpl_parms; |
7fe6899f MM |
4484 | int i; |
4485 | ||
3d7de1fa MM |
4486 | if (!TYPE_TEMPLATE_INFO (type)) |
4487 | { | |
0f51ccfc | 4488 | error ("%qT is not a template type", type); |
60feef2c | 4489 | return false; |
3d7de1fa MM |
4490 | } |
4491 | ||
4492 | tmpl = TYPE_TI_TEMPLATE (type); | |
7fe6899f MM |
4493 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
4494 | /* The type is nested in some template class. Nothing to worry | |
4495 | about here; there are no new template parameters for the nested | |
4496 | type. */ | |
60feef2c | 4497 | return true; |
7fe6899f | 4498 | |
ee921ddf MM |
4499 | if (!parms) |
4500 | { | |
4501 | error ("template specifiers not specified in declaration of %qD", | |
4502 | tmpl); | |
60feef2c | 4503 | return false; |
ee921ddf MM |
4504 | } |
4505 | ||
6757edfe MM |
4506 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
4507 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
4508 | ||
7fe6899f MM |
4509 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
4510 | { | |
b131ad7c MLI |
4511 | error ("redeclared with %d template parameter(s)", |
4512 | TREE_VEC_LENGTH (parms)); | |
1f5b3869 | 4513 | inform (input_location, "previous declaration %q+D used %d template parameter(s)", |
b131ad7c | 4514 | tmpl, TREE_VEC_LENGTH (tmpl_parms)); |
60feef2c | 4515 | return false; |
7fe6899f MM |
4516 | } |
4517 | ||
4518 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
4519 | { | |
2d8ba2c7 LM |
4520 | tree tmpl_parm; |
4521 | tree parm; | |
4522 | tree tmpl_default; | |
4523 | tree parm_default; | |
4524 | ||
4525 | if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node | |
4526 | || TREE_VEC_ELT (parms, i) == error_mark_node) | |
4527 | continue; | |
4528 | ||
4529 | tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
4530 | parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
4531 | tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
4532 | parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
7fe6899f | 4533 | |
2649701f KL |
4534 | /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or |
4535 | TEMPLATE_DECL. */ | |
0f67a82f | 4536 | if (tmpl_parm != error_mark_node |
a022041e DG |
4537 | && (TREE_CODE (tmpl_parm) != TREE_CODE (parm) |
4538 | || (TREE_CODE (tmpl_parm) != TYPE_DECL | |
4539 | && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))) | |
4540 | || (TREE_CODE (tmpl_parm) != PARM_DECL | |
4541 | && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm)) | |
4542 | != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))) | |
4543 | || (TREE_CODE (tmpl_parm) == PARM_DECL | |
4544 | && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm)) | |
4545 | != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))) | |
7fe6899f | 4546 | { |
dee15844 | 4547 | error ("template parameter %q+#D", tmpl_parm); |
0f51ccfc | 4548 | error ("redeclared here as %q#D", parm); |
60feef2c | 4549 | return false; |
7fe6899f MM |
4550 | } |
4551 | ||
4552 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
4553 | { | |
4554 | /* We have in [temp.param]: | |
4555 | ||
4556 | A template-parameter may not be given default arguments | |
4557 | by two different declarations in the same scope. */ | |
c5d75364 MLI |
4558 | error_at (input_location, "redefinition of default argument for %q#D", parm); |
4559 | inform (DECL_SOURCE_LOCATION (tmpl_parm), | |
4560 | "original definition appeared here"); | |
60feef2c | 4561 | return false; |
7fe6899f MM |
4562 | } |
4563 | ||
4564 | if (parm_default != NULL_TREE) | |
4565 | /* Update the previous template parameters (which are the ones | |
4566 | that will really count) with the new default value. */ | |
4567 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
7ce74c5e MM |
4568 | else if (tmpl_default != NULL_TREE) |
4569 | /* Update the new parameters, too; they'll be used as the | |
4570 | parameters for any members. */ | |
4571 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
7fe6899f | 4572 | } |
60feef2c LM |
4573 | |
4574 | return true; | |
7fe6899f | 4575 | } |
75650646 | 4576 | |
9baa27a9 MM |
4577 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
4578 | (possibly simplified) expression. */ | |
4579 | ||
4580 | tree | |
4581 | fold_non_dependent_expr (tree expr) | |
4582 | { | |
d4a200d3 SM |
4583 | if (expr == NULL_TREE) |
4584 | return NULL_TREE; | |
4585 | ||
9baa27a9 MM |
4586 | /* If we're in a template, but EXPR isn't value dependent, simplify |
4587 | it. We're supposed to treat: | |
c8094d83 | 4588 | |
9baa27a9 MM |
4589 | template <typename T> void f(T[1 + 1]); |
4590 | template <typename T> void f(T[2]); | |
c8094d83 | 4591 | |
9baa27a9 MM |
4592 | as two declarations of the same function, for example. */ |
4593 | if (processing_template_decl | |
4594 | && !type_dependent_expression_p (expr) | |
7416ab02 | 4595 | && !value_dependent_expression_p (expr)) |
9baa27a9 MM |
4596 | { |
4597 | HOST_WIDE_INT saved_processing_template_decl; | |
4598 | ||
4599 | saved_processing_template_decl = processing_template_decl; | |
4600 | processing_template_decl = 0; | |
4601 | expr = tsubst_copy_and_build (expr, | |
4602 | /*args=*/NULL_TREE, | |
4603 | tf_error, | |
4604 | /*in_decl=*/NULL_TREE, | |
015c2c66 MM |
4605 | /*function_p=*/false, |
4606 | /*integral_constant_expression_p=*/true); | |
9baa27a9 MM |
4607 | processing_template_decl = saved_processing_template_decl; |
4608 | } | |
4609 | return expr; | |
4610 | } | |
4611 | ||
b6ab6892 GB |
4612 | /* EXPR is an expression which is used in a constant-expression context. |
4613 | For instance, it could be a VAR_DECL with a constant initializer. | |
39a13be5 | 4614 | Extract the innermost constant expression. |
c8094d83 | 4615 | |
8a784e4a NS |
4616 | This is basically a more powerful version of |
4617 | integral_constant_value, which can be used also in templates where | |
4618 | initializers can maintain a syntactic rather than semantic form | |
4619 | (even if they are non-dependent, for access-checking purposes). */ | |
b6ab6892 | 4620 | |
993acaec | 4621 | static tree |
b6ab6892 GB |
4622 | fold_decl_constant_value (tree expr) |
4623 | { | |
4ef69b83 GB |
4624 | tree const_expr = expr; |
4625 | do | |
b6ab6892 | 4626 | { |
b6ab6892 | 4627 | expr = fold_non_dependent_expr (const_expr); |
4ef69b83 | 4628 | const_expr = integral_constant_value (expr); |
b6ab6892 | 4629 | } |
4ef69b83 | 4630 | while (expr != const_expr); |
b6ab6892 | 4631 | |
8a784e4a | 4632 | return expr; |
b6ab6892 GB |
4633 | } |
4634 | ||
4635 | /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which | |
4636 | must be a function or a pointer-to-function type, as specified | |
4637 | in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, | |
4638 | and check that the resulting function has external linkage. */ | |
4639 | ||
4640 | static tree | |
4641 | convert_nontype_argument_function (tree type, tree expr) | |
4642 | { | |
4643 | tree fns = expr; | |
4644 | tree fn, fn_no_ptr; | |
4645 | ||
4646 | fn = instantiate_type (type, fns, tf_none); | |
4647 | if (fn == error_mark_node) | |
4648 | return error_mark_node; | |
4649 | ||
4650 | fn_no_ptr = fn; | |
4651 | if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) | |
4652 | fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); | |
eff3a276 MM |
4653 | if (TREE_CODE (fn_no_ptr) == BASELINK) |
4654 | fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr); | |
4655 | ||
b6ab6892 GB |
4656 | /* [temp.arg.nontype]/1 |
4657 | ||
4658 | A template-argument for a non-type, non-template template-parameter | |
4659 | shall be one of: | |
4660 | [...] | |
4661 | -- the address of an object or function with external linkage. */ | |
4662 | if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr)) | |
4663 | { | |
4664 | error ("%qE is not a valid template argument for type %qT " | |
4665 | "because function %qD has not external linkage", | |
4666 | expr, type, fn_no_ptr); | |
4667 | return NULL_TREE; | |
4668 | } | |
4669 | ||
4670 | return fn; | |
4671 | } | |
4672 | ||
75650646 MM |
4673 | /* Attempt to convert the non-type template parameter EXPR to the |
4674 | indicated TYPE. If the conversion is successful, return the | |
dc957d14 | 4675 | converted value. If the conversion is unsuccessful, return |
75650646 MM |
4676 | NULL_TREE if we issued an error message, or error_mark_node if we |
4677 | did not. We issue error messages for out-and-out bad template | |
4678 | parameters, but not simply because the conversion failed, since we | |
9baa27a9 | 4679 | might be just trying to do argument deduction. Both TYPE and EXPR |
b6ab6892 GB |
4680 | must be non-dependent. |
4681 | ||
4682 | The conversion follows the special rules described in | |
4683 | [temp.arg.nontype], and it is much more strict than an implicit | |
4684 | conversion. | |
4685 | ||
4686 | This function is called twice for each template argument (see | |
4687 | lookup_template_class for a more accurate description of this | |
4688 | problem). This means that we need to handle expressions which | |
4689 | are not valid in a C++ source, but can be created from the | |
4690 | first call (for instance, casts to perform conversions). These | |
4691 | hacks can go away after we fix the double coercion problem. */ | |
75650646 MM |
4692 | |
4693 | static tree | |
3a978d72 | 4694 | convert_nontype_argument (tree type, tree expr) |
75650646 | 4695 | { |
9baa27a9 MM |
4696 | tree expr_type; |
4697 | ||
b6ab6892 GB |
4698 | /* Detect immediately string literals as invalid non-type argument. |
4699 | This special-case is not needed for correctness (we would easily | |
4700 | catch this later), but only to provide better diagnostic for this | |
4701 | common user mistake. As suggested by DR 100, we do not mention | |
4702 | linkage issues in the diagnostic as this is not the point. */ | |
4703 | if (TREE_CODE (expr) == STRING_CST) | |
4704 | { | |
4705 | error ("%qE is not a valid template argument for type %qT " | |
4706 | "because string literals can never be used in this context", | |
4707 | expr, type); | |
4708 | return NULL_TREE; | |
4709 | } | |
4710 | ||
9baa27a9 MM |
4711 | /* If we are in a template, EXPR may be non-dependent, but still |
4712 | have a syntactic, rather than semantic, form. For example, EXPR | |
4713 | might be a SCOPE_REF, rather than the VAR_DECL to which the | |
4714 | SCOPE_REF refers. Preserving the qualifying scope is necessary | |
4715 | so that access checking can be performed when the template is | |
4716 | instantiated -- but here we need the resolved form so that we can | |
4717 | convert the argument. */ | |
4718 | expr = fold_non_dependent_expr (expr); | |
b166a559 VR |
4719 | if (error_operand_p (expr)) |
4720 | return error_mark_node; | |
9baa27a9 | 4721 | expr_type = TREE_TYPE (expr); |
75650646 | 4722 | |
b6ab6892 GB |
4723 | /* HACK: Due to double coercion, we can get a |
4724 | NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here, | |
4725 | which is the tree that we built on the first call (see | |
4726 | below when coercing to reference to object or to reference to | |
4727 | function). We just strip everything and get to the arg. | |
4728 | See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C | |
4729 | for examples. */ | |
4730 | if (TREE_CODE (expr) == NOP_EXPR) | |
75650646 | 4731 | { |
b6ab6892 | 4732 | if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) |
75650646 | 4733 | { |
b6ab6892 GB |
4734 | /* ??? Maybe we could use convert_from_reference here, but we |
4735 | would need to relax its constraints because the NOP_EXPR | |
4736 | could actually change the type to something more cv-qualified, | |
4737 | and this is not folded by convert_from_reference. */ | |
4738 | tree addr = TREE_OPERAND (expr, 0); | |
4739 | gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE); | |
4740 | gcc_assert (TREE_CODE (addr) == ADDR_EXPR); | |
4741 | gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE); | |
4742 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4743 | (TREE_TYPE (expr_type), | |
4744 | TREE_TYPE (TREE_TYPE (addr)))); | |
4745 | ||
4746 | expr = TREE_OPERAND (addr, 0); | |
4747 | expr_type = TREE_TYPE (expr); | |
75650646 MM |
4748 | } |
4749 | ||
b6ab6892 GB |
4750 | /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the |
4751 | parameter is a pointer to object, through decay and | |
4752 | qualification conversion. Let's strip everything. */ | |
4753 | else if (TYPE_PTROBV_P (type)) | |
75650646 | 4754 | { |
b6ab6892 GB |
4755 | STRIP_NOPS (expr); |
4756 | gcc_assert (TREE_CODE (expr) == ADDR_EXPR); | |
4757 | gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE); | |
4758 | /* Skip the ADDR_EXPR only if it is part of the decay for | |
4759 | an array. Otherwise, it is part of the original argument | |
4760 | in the source code. */ | |
4761 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE) | |
4762 | expr = TREE_OPERAND (expr, 0); | |
4763 | expr_type = TREE_TYPE (expr); | |
f3400fe2 | 4764 | } |
75650646 | 4765 | } |
c61dce3a | 4766 | |
b6ab6892 | 4767 | /* [temp.arg.nontype]/5, bullet 1 |
75650646 | 4768 | |
b6ab6892 GB |
4769 | For a non-type template-parameter of integral or enumeration type, |
4770 | integral promotions (_conv.prom_) and integral conversions | |
4771 | (_conv.integral_) are applied. */ | |
550a799d | 4772 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
75650646 | 4773 | { |
550a799d | 4774 | if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type)) |
75650646 | 4775 | return error_mark_node; |
fddabb2c | 4776 | |
b6ab6892 GB |
4777 | expr = fold_decl_constant_value (expr); |
4778 | /* Notice that there are constant expressions like '4 % 0' which | |
4779 | do not fold into integer constants. */ | |
db02b6b9 | 4780 | if (TREE_CODE (expr) != INTEGER_CST) |
b6ab6892 GB |
4781 | { |
4782 | error ("%qE is not a valid template argument for type %qT " | |
4783 | "because it is a non-constant expression", expr, type); | |
4784 | return NULL_TREE; | |
4785 | } | |
75650646 | 4786 | |
b6ab6892 GB |
4787 | /* At this point, an implicit conversion does what we want, |
4788 | because we already know that the expression is of integral | |
4789 | type. */ | |
4790 | expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT); | |
4791 | if (expr == error_mark_node) | |
4792 | return error_mark_node; | |
75650646 | 4793 | |
b6ab6892 GB |
4794 | /* Conversion was allowed: fold it to a bare integer constant. */ |
4795 | expr = fold (expr); | |
4796 | } | |
4797 | /* [temp.arg.nontype]/5, bullet 2 | |
75650646 | 4798 | |
b6ab6892 GB |
4799 | For a non-type template-parameter of type pointer to object, |
4800 | qualification conversions (_conv.qual_) and the array-to-pointer | |
4801 | conversion (_conv.array_) are applied. */ | |
4802 | else if (TYPE_PTROBV_P (type)) | |
4803 | { | |
4804 | /* [temp.arg.nontype]/1 (TC1 version, DR 49): | |
75650646 | 4805 | |
b6ab6892 GB |
4806 | A template-argument for a non-type, non-template template-parameter |
4807 | shall be one of: [...] | |
75650646 | 4808 | |
b6ab6892 GB |
4809 | -- the name of a non-type template-parameter; |
4810 | -- the address of an object or function with external linkage, [...] | |
4811 | expressed as "& id-expression" where the & is optional if the name | |
4812 | refers to a function or array, or if the corresponding | |
4813 | template-parameter is a reference. | |
c8094d83 | 4814 | |
b6ab6892 GB |
4815 | Here, we do not care about functions, as they are invalid anyway |
4816 | for a parameter of type pointer-to-object. */ | |
1082fd10 MM |
4817 | |
4818 | if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)) | |
4819 | /* Non-type template parameters are OK. */ | |
4820 | ; | |
4821 | else if (TREE_CODE (expr) != ADDR_EXPR | |
4822 | && TREE_CODE (expr_type) != ARRAY_TYPE) | |
4823 | { | |
4824 | if (TREE_CODE (expr) == VAR_DECL) | |
4825 | { | |
4826 | error ("%qD is not a valid template argument " | |
4827 | "because %qD is a variable, not the address of " | |
4828 | "a variable", | |
4829 | expr, expr); | |
4830 | return NULL_TREE; | |
4831 | } | |
4832 | /* Other values, like integer constants, might be valid | |
4833 | non-type arguments of some other type. */ | |
4834 | return error_mark_node; | |
4835 | } | |
4836 | else | |
4837 | { | |
4838 | tree decl; | |
4839 | ||
4840 | decl = ((TREE_CODE (expr) == ADDR_EXPR) | |
4841 | ? TREE_OPERAND (expr, 0) : expr); | |
4842 | if (TREE_CODE (decl) != VAR_DECL) | |
4843 | { | |
4844 | error ("%qE is not a valid template argument of type %qT " | |
4845 | "because %qE is not a variable", | |
4846 | expr, type, decl); | |
4847 | return NULL_TREE; | |
4848 | } | |
4849 | else if (!DECL_EXTERNAL_LINKAGE_P (decl)) | |
4850 | { | |
4851 | error ("%qE is not a valid template argument of type %qT " | |
4852 | "because %qD does not have external linkage", | |
4853 | expr, type, decl); | |
4854 | return NULL_TREE; | |
4855 | } | |
4856 | } | |
b6ab6892 GB |
4857 | |
4858 | expr = decay_conversion (expr); | |
4859 | if (expr == error_mark_node) | |
4860 | return error_mark_node; | |
75650646 | 4861 | |
b6ab6892 GB |
4862 | expr = perform_qualification_conversions (type, expr); |
4863 | if (expr == error_mark_node) | |
4864 | return error_mark_node; | |
b6ab6892 GB |
4865 | } |
4866 | /* [temp.arg.nontype]/5, bullet 3 | |
4867 | ||
4868 | For a non-type template-parameter of type reference to object, no | |
4869 | conversions apply. The type referred to by the reference may be more | |
4870 | cv-qualified than the (otherwise identical) type of the | |
4871 | template-argument. The template-parameter is bound directly to the | |
4872 | template-argument, which must be an lvalue. */ | |
4873 | else if (TYPE_REF_OBJ_P (type)) | |
4874 | { | |
4875 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type), | |
4876 | expr_type)) | |
4877 | return error_mark_node; | |
75650646 | 4878 | |
b6ab6892 GB |
4879 | if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type)) |
4880 | { | |
4881 | error ("%qE is not a valid template argument for type %qT " | |
4882 | "because of conflicts in cv-qualification", expr, type); | |
4883 | return NULL_TREE; | |
4884 | } | |
c8094d83 | 4885 | |
b6ab6892 GB |
4886 | if (!real_lvalue_p (expr)) |
4887 | { | |
4888 | error ("%qE is not a valid template argument for type %qT " | |
944fd207 | 4889 | "because it is not an lvalue", expr, type); |
b6ab6892 GB |
4890 | return NULL_TREE; |
4891 | } | |
e6e174e5 | 4892 | |
b6ab6892 | 4893 | /* [temp.arg.nontype]/1 |
75650646 | 4894 | |
b6ab6892 GB |
4895 | A template-argument for a non-type, non-template template-parameter |
4896 | shall be one of: [...] | |
75650646 | 4897 | |
03fd3f84 | 4898 | -- the address of an object or function with external linkage. */ |
b6ab6892 GB |
4899 | if (!DECL_EXTERNAL_LINKAGE_P (expr)) |
4900 | { | |
4901 | error ("%qE is not a valid template argument for type %qT " | |
4902 | "because object %qD has not external linkage", | |
4903 | expr, type, expr); | |
4904 | return NULL_TREE; | |
4905 | } | |
0dc09a61 | 4906 | |
b6ab6892 GB |
4907 | expr = build_nop (type, build_address (expr)); |
4908 | } | |
4909 | /* [temp.arg.nontype]/5, bullet 4 | |
4910 | ||
4911 | For a non-type template-parameter of type pointer to function, only | |
4912 | the function-to-pointer conversion (_conv.func_) is applied. If the | |
4913 | template-argument represents a set of overloaded functions (or a | |
4914 | pointer to such), the matching function is selected from the set | |
4915 | (_over.over_). */ | |
4916 | else if (TYPE_PTRFN_P (type)) | |
4917 | { | |
4918 | /* If the argument is a template-id, we might not have enough | |
2c164de6 | 4919 | context information to decay the pointer. */ |
b6ab6892 GB |
4920 | if (!type_unknown_p (expr_type)) |
4921 | { | |
4922 | expr = decay_conversion (expr); | |
4923 | if (expr == error_mark_node) | |
4924 | return error_mark_node; | |
4925 | } | |
75650646 | 4926 | |
b6ab6892 GB |
4927 | expr = convert_nontype_argument_function (type, expr); |
4928 | if (!expr || expr == error_mark_node) | |
4929 | return expr; | |
e350dbbd PB |
4930 | |
4931 | if (TREE_CODE (expr) != ADDR_EXPR) | |
4932 | { | |
4933 | error ("%qE is not a valid template argument for type %qT", expr, type); | |
4934 | error ("it must be the address of a function with external linkage"); | |
4935 | return NULL_TREE; | |
4936 | } | |
b6ab6892 GB |
4937 | } |
4938 | /* [temp.arg.nontype]/5, bullet 5 | |
75650646 | 4939 | |
b6ab6892 GB |
4940 | For a non-type template-parameter of type reference to function, no |
4941 | conversions apply. If the template-argument represents a set of | |
4942 | overloaded functions, the matching function is selected from the set | |
4943 | (_over.over_). */ | |
4944 | else if (TYPE_REFFN_P (type)) | |
4945 | { | |
4946 | if (TREE_CODE (expr) == ADDR_EXPR) | |
4947 | { | |
4948 | error ("%qE is not a valid template argument for type %qT " | |
4949 | "because it is a pointer", expr, type); | |
1f5b3869 | 4950 | inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0)); |
b6ab6892 GB |
4951 | return NULL_TREE; |
4952 | } | |
75650646 | 4953 | |
b6ab6892 GB |
4954 | expr = convert_nontype_argument_function (TREE_TYPE (type), expr); |
4955 | if (!expr || expr == error_mark_node) | |
4956 | return expr; | |
75650646 | 4957 | |
7866705a | 4958 | expr = build_nop (type, build_address (expr)); |
b6ab6892 GB |
4959 | } |
4960 | /* [temp.arg.nontype]/5, bullet 6 | |
75650646 | 4961 | |
b6ab6892 GB |
4962 | For a non-type template-parameter of type pointer to member function, |
4963 | no conversions apply. If the template-argument represents a set of | |
4964 | overloaded member functions, the matching member function is selected | |
4965 | from the set (_over.over_). */ | |
4966 | else if (TYPE_PTRMEMFUNC_P (type)) | |
4967 | { | |
4968 | expr = instantiate_type (type, expr, tf_none); | |
4969 | if (expr == error_mark_node) | |
4970 | return error_mark_node; | |
75650646 | 4971 | |
b6ab6892 GB |
4972 | /* There is no way to disable standard conversions in |
4973 | resolve_address_of_overloaded_function (called by | |
4974 | instantiate_type). It is possible that the call succeeded by | |
4975 | converting &B::I to &D::I (where B is a base of D), so we need | |
4976 | to reject this conversion here. | |
75650646 | 4977 | |
b6ab6892 GB |
4978 | Actually, even if there was a way to disable standard conversions, |
4979 | it would still be better to reject them here so that we can | |
4980 | provide a superior diagnostic. */ | |
4981 | if (!same_type_p (TREE_TYPE (expr), type)) | |
4982 | { | |
4983 | /* Make sure we are just one standard conversion off. */ | |
4984 | gcc_assert (can_convert (type, TREE_TYPE (expr))); | |
4985 | error ("%qE is not a valid template argument for type %qT " | |
4986 | "because it is of type %qT", expr, type, | |
4987 | TREE_TYPE (expr)); | |
1f5b3869 | 4988 | inform (input_location, "standard conversions are not allowed in this context"); |
b6ab6892 GB |
4989 | return NULL_TREE; |
4990 | } | |
4991 | } | |
4992 | /* [temp.arg.nontype]/5, bullet 7 | |
59e7a901 | 4993 | |
b6ab6892 GB |
4994 | For a non-type template-parameter of type pointer to data member, |
4995 | qualification conversions (_conv.qual_) are applied. */ | |
4996 | else if (TYPE_PTRMEM_P (type)) | |
4997 | { | |
4998 | expr = perform_qualification_conversions (type, expr); | |
4999 | if (expr == error_mark_node) | |
75650646 | 5000 | return expr; |
75650646 | 5001 | } |
b6ab6892 GB |
5002 | /* A template non-type parameter must be one of the above. */ |
5003 | else | |
5004 | gcc_unreachable (); | |
75650646 | 5005 | |
b6ab6892 GB |
5006 | /* Sanity check: did we actually convert the argument to the |
5007 | right type? */ | |
5008 | gcc_assert (same_type_p (type, TREE_TYPE (expr))); | |
5009 | return expr; | |
75650646 MM |
5010 | } |
5011 | ||
a0a5f30f DG |
5012 | /* Subroutine of coerce_template_template_parms, which returns 1 if |
5013 | PARM_PARM and ARG_PARM match using the rule for the template | |
5014 | parameters of template template parameters. Both PARM and ARG are | |
5015 | template parameters; the rest of the arguments are the same as for | |
5016 | coerce_template_template_parms. | |
5017 | */ | |
5018 | static int | |
5019 | coerce_template_template_parm (tree parm, | |
5020 | tree arg, | |
5021 | tsubst_flags_t complain, | |
5022 | tree in_decl, | |
5023 | tree outer_args) | |
5024 | { | |
5025 | if (arg == NULL_TREE || arg == error_mark_node | |
5026 | || parm == NULL_TREE || parm == error_mark_node) | |
5027 | return 0; | |
5028 | ||
5029 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
5030 | return 0; | |
5031 | ||
5032 | switch (TREE_CODE (parm)) | |
5033 | { | |
5034 | case TEMPLATE_DECL: | |
5035 | /* We encounter instantiations of templates like | |
5036 | template <template <template <class> class> class TT> | |
5037 | class C; */ | |
5038 | { | |
5039 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
5040 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
5041 | ||
5042 | if (!coerce_template_template_parms | |
5043 | (parmparm, argparm, complain, in_decl, outer_args)) | |
5044 | return 0; | |
5045 | } | |
5046 | /* Fall through. */ | |
5047 | ||
5048 | case TYPE_DECL: | |
5049 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg)) | |
5050 | && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) | |
5051 | /* Argument is a parameter pack but parameter is not. */ | |
5052 | return 0; | |
5053 | break; | |
5054 | ||
5055 | case PARM_DECL: | |
5056 | /* The tsubst call is used to handle cases such as | |
5057 | ||
5058 | template <int> class C {}; | |
5059 | template <class T, template <T> class TT> class D {}; | |
5060 | D<int, C> d; | |
5061 | ||
5062 | i.e. the parameter list of TT depends on earlier parameters. */ | |
8320114a | 5063 | if (!uses_template_parms (TREE_TYPE (arg)) |
a0a5f30f DG |
5064 | && !same_type_p |
5065 | (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl), | |
5066 | TREE_TYPE (arg))) | |
5067 | return 0; | |
5068 | ||
5069 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg)) | |
5070 | && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) | |
5071 | /* Argument is a parameter pack but parameter is not. */ | |
5072 | return 0; | |
5073 | ||
5074 | break; | |
5075 | ||
5076 | default: | |
5077 | gcc_unreachable (); | |
5078 | } | |
5079 | ||
5080 | return 1; | |
5081 | } | |
5082 | ||
b6ab6892 | 5083 | |
c8094d83 MS |
5084 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
5085 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
5086 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
744fac59 | 5087 | or PARM_DECL. |
c8094d83 | 5088 | |
744fac59 | 5089 | Consider the example: |
e7e93965 MM |
5090 | template <class T> class A; |
5091 | template<template <class U> class TT> class B; | |
744fac59 | 5092 | |
e7e93965 MM |
5093 | For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are |
5094 | the parameters to A, and OUTER_ARGS contains A. */ | |
744fac59 KL |
5095 | |
5096 | static int | |
c8094d83 | 5097 | coerce_template_template_parms (tree parm_parms, |
0cbd7506 MS |
5098 | tree arg_parms, |
5099 | tsubst_flags_t complain, | |
3a978d72 | 5100 | tree in_decl, |
0cbd7506 | 5101 | tree outer_args) |
744fac59 KL |
5102 | { |
5103 | int nparms, nargs, i; | |
5104 | tree parm, arg; | |
a0a5f30f | 5105 | int variadic_p = 0; |
744fac59 | 5106 | |
50bc768d NS |
5107 | gcc_assert (TREE_CODE (parm_parms) == TREE_VEC); |
5108 | gcc_assert (TREE_CODE (arg_parms) == TREE_VEC); | |
744fac59 KL |
5109 | |
5110 | nparms = TREE_VEC_LENGTH (parm_parms); | |
5111 | nargs = TREE_VEC_LENGTH (arg_parms); | |
5112 | ||
a0a5f30f DG |
5113 | /* Determine whether we have a parameter pack at the end of the |
5114 | template template parameter's template parameter list. */ | |
5115 | if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node) | |
5116 | { | |
5117 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1)); | |
5118 | ||
8c8d4a46 L |
5119 | if (parm == error_mark_node) |
5120 | return 0; | |
5121 | ||
a0a5f30f DG |
5122 | switch (TREE_CODE (parm)) |
5123 | { | |
5124 | case TEMPLATE_DECL: | |
5125 | case TYPE_DECL: | |
5126 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) | |
5127 | variadic_p = 1; | |
5128 | break; | |
5129 | ||
5130 | case PARM_DECL: | |
5131 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) | |
5132 | variadic_p = 1; | |
5133 | break; | |
5134 | ||
5135 | default: | |
5136 | gcc_unreachable (); | |
5137 | } | |
5138 | } | |
5139 | ||
5140 | if (nargs != nparms | |
5141 | && !(variadic_p && nargs >= nparms - 1)) | |
744fac59 KL |
5142 | return 0; |
5143 | ||
a0a5f30f DG |
5144 | /* Check all of the template parameters except the parameter pack at |
5145 | the end (if any). */ | |
5146 | for (i = 0; i < nparms - variadic_p; ++i) | |
744fac59 | 5147 | { |
2d8ba2c7 LM |
5148 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node |
5149 | || TREE_VEC_ELT (arg_parms, i) == error_mark_node) | |
42b304f1 LM |
5150 | continue; |
5151 | ||
744fac59 KL |
5152 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
5153 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
5154 | ||
a0a5f30f DG |
5155 | if (!coerce_template_template_parm (parm, arg, complain, in_decl, |
5156 | outer_args)) | |
744fac59 KL |
5157 | return 0; |
5158 | ||
a0a5f30f | 5159 | } |
00bdb87f | 5160 | |
a0a5f30f DG |
5161 | if (variadic_p) |
5162 | { | |
5163 | /* Check each of the template parameters in the template | |
5164 | argument against the template parameter pack at the end of | |
5165 | the template template parameter. */ | |
5166 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node) | |
5167 | return 0; | |
5d80a306 | 5168 | |
a0a5f30f | 5169 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
c8094d83 | 5170 | |
a0a5f30f DG |
5171 | for (; i < nargs; ++i) |
5172 | { | |
5173 | if (TREE_VEC_ELT (arg_parms, i) == error_mark_node) | |
5174 | continue; | |
5175 | ||
5176 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
5177 | ||
5178 | if (!coerce_template_template_parm (parm, arg, complain, in_decl, | |
5179 | outer_args)) | |
5180 | return 0; | |
5181 | } | |
744fac59 | 5182 | } |
a0a5f30f | 5183 | |
744fac59 KL |
5184 | return 1; |
5185 | } | |
5186 | ||
4cf36211 DG |
5187 | /* Verifies that the deduced template arguments (in TARGS) for the |
5188 | template template parameters (in TPARMS) represent valid bindings, | |
5189 | by comparing the template parameter list of each template argument | |
5190 | to the template parameter list of its corresponding template | |
5191 | template parameter, in accordance with DR150. This | |
5192 | routine can only be called after all template arguments have been | |
5193 | deduced. It will return TRUE if all of the template template | |
5194 | parameter bindings are okay, FALSE otherwise. */ | |
5195 | bool | |
5196 | template_template_parm_bindings_ok_p (tree tparms, tree targs) | |
5197 | { | |
5198 | int i, ntparms = TREE_VEC_LENGTH (tparms); | |
ee34d21a JM |
5199 | bool ret = true; |
5200 | ||
5201 | /* We're dealing with template parms in this process. */ | |
5202 | ++processing_template_decl; | |
4cf36211 DG |
5203 | |
5204 | targs = INNERMOST_TEMPLATE_ARGS (targs); | |
5205 | ||
5206 | for (i = 0; i < ntparms; ++i) | |
5207 | { | |
5208 | tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
5209 | tree targ = TREE_VEC_ELT (targs, i); | |
5210 | ||
5211 | if (TREE_CODE (tparm) == TEMPLATE_DECL && targ) | |
5212 | { | |
5213 | tree packed_args = NULL_TREE; | |
5214 | int idx, len = 1; | |
5215 | ||
5216 | if (ARGUMENT_PACK_P (targ)) | |
5217 | { | |
5218 | /* Look inside the argument pack. */ | |
5219 | packed_args = ARGUMENT_PACK_ARGS (targ); | |
5220 | len = TREE_VEC_LENGTH (packed_args); | |
5221 | } | |
5222 | ||
5223 | for (idx = 0; idx < len; ++idx) | |
5224 | { | |
5225 | tree targ_parms = NULL_TREE; | |
5226 | ||
5227 | if (packed_args) | |
5228 | /* Extract the next argument from the argument | |
5229 | pack. */ | |
5230 | targ = TREE_VEC_ELT (packed_args, idx); | |
5231 | ||
5232 | if (PACK_EXPANSION_P (targ)) | |
5233 | /* Look at the pattern of the pack expansion. */ | |
5234 | targ = PACK_EXPANSION_PATTERN (targ); | |
5235 | ||
5236 | /* Extract the template parameters from the template | |
5237 | argument. */ | |
5238 | if (TREE_CODE (targ) == TEMPLATE_DECL) | |
5239 | targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ); | |
5240 | else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM) | |
5241 | targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ)); | |
5242 | ||
5243 | /* Verify that we can coerce the template template | |
5244 | parameters from the template argument to the template | |
5245 | parameter. This requires an exact match. */ | |
5246 | if (targ_parms | |
5247 | && !coerce_template_template_parms | |
5248 | (DECL_INNERMOST_TEMPLATE_PARMS (tparm), | |
5249 | targ_parms, | |
5250 | tf_none, | |
5251 | tparm, | |
5252 | targs)) | |
ee34d21a JM |
5253 | { |
5254 | ret = false; | |
5255 | goto out; | |
5256 | } | |
4cf36211 DG |
5257 | } |
5258 | } | |
5259 | } | |
5260 | ||
ee34d21a JM |
5261 | out: |
5262 | ||
5263 | --processing_template_decl; | |
5264 | return ret; | |
4cf36211 DG |
5265 | } |
5266 | ||
8b5b8b7c MM |
5267 | /* Convert the indicated template ARG as necessary to match the |
5268 | indicated template PARM. Returns the converted ARG, or | |
c2ea3a40 NS |
5269 | error_mark_node if the conversion was unsuccessful. Error and |
5270 | warning messages are issued under control of COMPLAIN. This | |
5271 | conversion is for the Ith parameter in the parameter list. ARGS is | |
5272 | the full set of template arguments deduced so far. */ | |
8b5b8b7c MM |
5273 | |
5274 | static tree | |
c8094d83 | 5275 | convert_template_argument (tree parm, |
0cbd7506 MS |
5276 | tree arg, |
5277 | tree args, | |
5278 | tsubst_flags_t complain, | |
5279 | int i, | |
5280 | tree in_decl) | |
8b5b8b7c | 5281 | { |
88b82314 | 5282 | tree orig_arg; |
8b5b8b7c | 5283 | tree val; |
8b5b8b7c | 5284 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; |
c8094d83 | 5285 | |
c8094d83 | 5286 | if (TREE_CODE (arg) == TREE_LIST |
a5ac359a | 5287 | && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF) |
c8094d83 | 5288 | { |
8b5b8b7c MM |
5289 | /* The template argument was the name of some |
5290 | member function. That's usually | |
0e339752 | 5291 | invalid, but static members are OK. In any |
8b5b8b7c MM |
5292 | case, grab the underlying fields/functions |
5293 | and issue an error later if required. */ | |
88b82314 | 5294 | orig_arg = TREE_VALUE (arg); |
8b5b8b7c MM |
5295 | TREE_TYPE (arg) = unknown_type_node; |
5296 | } | |
5297 | ||
88b82314 DG |
5298 | orig_arg = arg; |
5299 | ||
8b5b8b7c MM |
5300 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; |
5301 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
5302 | || requires_tmpl_type); | |
5303 | ||
d732e98f | 5304 | /* When determining whether an argument pack expansion is a template, |
5d80a306 | 5305 | look at the pattern. */ |
88b82314 DG |
5306 | if (TREE_CODE (arg) == TYPE_PACK_EXPANSION) |
5307 | arg = PACK_EXPANSION_PATTERN (arg); | |
5d80a306 DG |
5308 | |
5309 | is_tmpl_type = | |
88b82314 DG |
5310 | ((TREE_CODE (arg) == TEMPLATE_DECL |
5311 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
5312 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
5313 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); | |
c8094d83 | 5314 | |
b8c6534b KL |
5315 | if (is_tmpl_type |
5316 | && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
5317 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) | |
8b5b8b7c | 5318 | arg = TYPE_STUB_DECL (arg); |
8b5b8b7c | 5319 | |
2f939d94 | 5320 | is_type = TYPE_P (arg) || is_tmpl_type; |
8b5b8b7c MM |
5321 | |
5322 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
5323 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
5324 | { | |
cbe5f3b3 | 5325 | permerror (input_location, "to refer to a type member of a template parameter, " |
37ec60ed | 5326 | "use %<typename %E%>", orig_arg); |
c8094d83 | 5327 | |
88b82314 DG |
5328 | orig_arg = make_typename_type (TREE_OPERAND (arg, 0), |
5329 | TREE_OPERAND (arg, 1), | |
5330 | typename_type, | |
5331 | complain & tf_error); | |
5332 | arg = orig_arg; | |
8b5b8b7c MM |
5333 | is_type = 1; |
5334 | } | |
5335 | if (is_type != requires_type) | |
5336 | { | |
5337 | if (in_decl) | |
5338 | { | |
c2ea3a40 | 5339 | if (complain & tf_error) |
8b5b8b7c | 5340 | { |
0f51ccfc | 5341 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
5342 | "parameter list for %qD", |
5343 | i + 1, in_decl); | |
8b5b8b7c | 5344 | if (is_type) |
0f51ccfc | 5345 | error (" expected a constant of type %qT, got %qT", |
0cbd7506 | 5346 | TREE_TYPE (parm), |
1ad8aeeb | 5347 | (DECL_P (arg) ? DECL_NAME (arg) : orig_arg)); |
d12a7283 | 5348 | else if (requires_tmpl_type) |
88b82314 | 5349 | error (" expected a class template, got %qE", orig_arg); |
8b5b8b7c | 5350 | else |
88b82314 | 5351 | error (" expected a type, got %qE", orig_arg); |
8b5b8b7c MM |
5352 | } |
5353 | } | |
5354 | return error_mark_node; | |
5355 | } | |
5356 | if (is_tmpl_type ^ requires_tmpl_type) | |
5357 | { | |
c2ea3a40 | 5358 | if (in_decl && (complain & tf_error)) |
8b5b8b7c | 5359 | { |
0f51ccfc | 5360 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
5361 | "parameter list for %qD", |
5362 | i + 1, in_decl); | |
8b5b8b7c | 5363 | if (is_tmpl_type) |
0f51ccfc | 5364 | error (" expected a type, got %qT", DECL_NAME (arg)); |
8b5b8b7c | 5365 | else |
88b82314 | 5366 | error (" expected a class template, got %qT", orig_arg); |
8b5b8b7c MM |
5367 | } |
5368 | return error_mark_node; | |
5369 | } | |
c8094d83 | 5370 | |
8b5b8b7c MM |
5371 | if (is_type) |
5372 | { | |
5373 | if (requires_tmpl_type) | |
5374 | { | |
b8c6534b KL |
5375 | if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) |
5376 | /* The number of argument required is not known yet. | |
5377 | Just accept it for now. */ | |
5378 | val = TREE_TYPE (arg); | |
8b5b8b7c MM |
5379 | else |
5380 | { | |
b8c6534b | 5381 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); |
5d80a306 DG |
5382 | tree argparm; |
5383 | ||
88b82314 | 5384 | argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); |
b8c6534b KL |
5385 | |
5386 | if (coerce_template_template_parms (parmparm, argparm, | |
5387 | complain, in_decl, | |
6150b602 | 5388 | args)) |
8b5b8b7c | 5389 | { |
88b82314 | 5390 | val = orig_arg; |
c8094d83 MS |
5391 | |
5392 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
b8c6534b | 5393 | TEMPLATE_DECL. */ |
5d80a306 DG |
5394 | if (val != error_mark_node) |
5395 | { | |
5396 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
5397 | val = TREE_TYPE (val); | |
5398 | else if (TREE_CODE (val) == TYPE_PACK_EXPANSION | |
88b82314 | 5399 | && DECL_TEMPLATE_TEMPLATE_PARM_P (arg)) |
5d80a306 | 5400 | { |
88b82314 | 5401 | val = TREE_TYPE (arg); |
5d80a306 DG |
5402 | val = make_pack_expansion (val); |
5403 | } | |
5404 | } | |
8b5b8b7c | 5405 | } |
b8c6534b KL |
5406 | else |
5407 | { | |
c2ea3a40 | 5408 | if (in_decl && (complain & tf_error)) |
b8c6534b | 5409 | { |
0f51ccfc | 5410 | error ("type/value mismatch at argument %d in " |
0cbd7506 MS |
5411 | "template parameter list for %qD", |
5412 | i + 1, in_decl); | |
0f51ccfc | 5413 | error (" expected a template of type %qD, got %qD", |
88b82314 | 5414 | parm, orig_arg); |
b8c6534b | 5415 | } |
c8094d83 | 5416 | |
b8c6534b KL |
5417 | val = error_mark_node; |
5418 | } | |
8b5b8b7c MM |
5419 | } |
5420 | } | |
5421 | else | |
88b82314 | 5422 | val = orig_arg; |
685e39c2 MM |
5423 | /* We only form one instance of each template specialization. |
5424 | Therefore, if we use a non-canonical variant (i.e., a | |
3db45ab5 | 5425 | typedef), any future messages referring to the type will use |
685e39c2 MM |
5426 | the typedef, which is confusing if those future uses do not |
5427 | themselves also use the typedef. */ | |
5428 | if (TYPE_P (val)) | |
cd41d410 | 5429 | val = strip_typedefs (val); |
8b5b8b7c MM |
5430 | } |
5431 | else | |
5432 | { | |
4393e105 | 5433 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c | 5434 | |
11b810f1 | 5435 | if (invalid_nontype_parm_type_p (t, complain)) |
0cbd7506 | 5436 | return error_mark_node; |
c8094d83 | 5437 | |
ffaf1e05 JM |
5438 | if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg)) |
5439 | { | |
5440 | if (same_type_p (t, TREE_TYPE (orig_arg))) | |
5441 | val = orig_arg; | |
5442 | else | |
5443 | { | |
5444 | /* Not sure if this is reachable, but it doesn't hurt | |
5445 | to be robust. */ | |
5446 | error ("type mismatch in nontype parameter pack"); | |
5447 | val = error_mark_node; | |
5448 | } | |
5449 | } | |
5450 | else if (!uses_template_parms (orig_arg) && !uses_template_parms (t)) | |
8b5b8b7c MM |
5451 | /* We used to call digest_init here. However, digest_init |
5452 | will report errors, which we don't want when complain | |
5453 | is zero. More importantly, digest_init will try too | |
5454 | hard to convert things: for example, `0' should not be | |
5455 | converted to pointer type at this point according to | |
5456 | the standard. Accepting this is not merely an | |
5457 | extension, since deciding whether or not these | |
5458 | conversions can occur is part of determining which | |
dc957d14 | 5459 | function template to call, or whether a given explicit |
0e339752 | 5460 | argument specification is valid. */ |
88b82314 | 5461 | val = convert_nontype_argument (t, orig_arg); |
8b5b8b7c | 5462 | else |
88b82314 | 5463 | val = orig_arg; |
8b5b8b7c MM |
5464 | |
5465 | if (val == NULL_TREE) | |
5466 | val = error_mark_node; | |
c2ea3a40 | 5467 | else if (val == error_mark_node && (complain & tf_error)) |
88b82314 | 5468 | error ("could not convert template argument %qE to %qT", orig_arg, t); |
8b5b8b7c MM |
5469 | } |
5470 | ||
5471 | return val; | |
5472 | } | |
5473 | ||
b1d7b1c0 DG |
5474 | /* Coerces the remaining template arguments in INNER_ARGS (from |
5475 | ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS. | |
5476 | Returns the coerced argument pack. PARM_IDX is the position of this | |
5477 | parameter in the template parameter list. ARGS is the original | |
5478 | template argument list. */ | |
5479 | static tree | |
5480 | coerce_template_parameter_pack (tree parms, | |
5481 | int parm_idx, | |
5482 | tree args, | |
5483 | tree inner_args, | |
5484 | int arg_idx, | |
5485 | tree new_args, | |
5486 | int* lost, | |
5487 | tree in_decl, | |
5488 | tsubst_flags_t complain) | |
5489 | { | |
5490 | tree parm = TREE_VEC_ELT (parms, parm_idx); | |
5491 | int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; | |
5492 | tree packed_args; | |
5493 | tree argument_pack; | |
5494 | tree packed_types = NULL_TREE; | |
5495 | ||
5496 | if (arg_idx > nargs) | |
5497 | arg_idx = nargs; | |
5498 | ||
5499 | packed_args = make_tree_vec (nargs - arg_idx); | |
5500 | ||
5501 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL | |
5502 | && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm)))) | |
5503 | { | |
5504 | /* When the template parameter is a non-type template | |
5505 | parameter pack whose type uses parameter packs, we need | |
5506 | to look at each of the template arguments | |
5507 | separately. Build a vector of the types for these | |
5508 | non-type template parameters in PACKED_TYPES. */ | |
5509 | tree expansion | |
5510 | = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm))); | |
5511 | packed_types = tsubst_pack_expansion (expansion, args, | |
5512 | complain, in_decl); | |
5513 | ||
5514 | if (packed_types == error_mark_node) | |
5515 | return error_mark_node; | |
5516 | ||
5517 | /* Check that we have the right number of arguments. */ | |
5518 | if (arg_idx < nargs | |
5519 | && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)) | |
5520 | && nargs - arg_idx != TREE_VEC_LENGTH (packed_types)) | |
5521 | { | |
5522 | int needed_parms | |
5523 | = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types); | |
5524 | error ("wrong number of template arguments (%d, should be %d)", | |
5525 | nargs, needed_parms); | |
5526 | return error_mark_node; | |
5527 | } | |
5528 | ||
5529 | /* If we aren't able to check the actual arguments now | |
5530 | (because they haven't been expanded yet), we can at least | |
5531 | verify that all of the types used for the non-type | |
5532 | template parameter pack are, in fact, valid for non-type | |
5533 | template parameters. */ | |
5534 | if (arg_idx < nargs | |
5535 | && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))) | |
5536 | { | |
5537 | int j, len = TREE_VEC_LENGTH (packed_types); | |
5538 | for (j = 0; j < len; ++j) | |
5539 | { | |
5540 | tree t = TREE_VEC_ELT (packed_types, j); | |
5541 | if (invalid_nontype_parm_type_p (t, complain)) | |
5542 | return error_mark_node; | |
5543 | } | |
5544 | } | |
5545 | } | |
5546 | ||
5547 | /* Convert the remaining arguments, which will be a part of the | |
5548 | parameter pack "parm". */ | |
5549 | for (; arg_idx < nargs; ++arg_idx) | |
5550 | { | |
5551 | tree arg = TREE_VEC_ELT (inner_args, arg_idx); | |
5552 | tree actual_parm = TREE_VALUE (parm); | |
5553 | ||
5554 | if (packed_types && !PACK_EXPANSION_P (arg)) | |
5555 | { | |
5556 | /* When we have a vector of types (corresponding to the | |
5557 | non-type template parameter pack that uses parameter | |
5558 | packs in its type, as mention above), and the | |
5559 | argument is not an expansion (which expands to a | |
5560 | currently unknown number of arguments), clone the | |
5561 | parm and give it the next type in PACKED_TYPES. */ | |
5562 | actual_parm = copy_node (actual_parm); | |
5563 | TREE_TYPE (actual_parm) = | |
5564 | TREE_VEC_ELT (packed_types, arg_idx - parm_idx); | |
5565 | } | |
5566 | ||
c343d5a7 PC |
5567 | if (arg != error_mark_node) |
5568 | arg = convert_template_argument (actual_parm, | |
5569 | arg, new_args, complain, parm_idx, | |
5570 | in_decl); | |
b1d7b1c0 DG |
5571 | if (arg == error_mark_node) |
5572 | (*lost)++; | |
5573 | TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg; | |
5574 | } | |
5575 | ||
5576 | if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL | |
5577 | || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL) | |
5578 | argument_pack = make_node (TYPE_ARGUMENT_PACK); | |
5579 | else | |
5580 | { | |
5581 | argument_pack = make_node (NONTYPE_ARGUMENT_PACK); | |
88b82314 | 5582 | TREE_TYPE (argument_pack) |
cdcae745 | 5583 | = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl); |
b1d7b1c0 DG |
5584 | TREE_CONSTANT (argument_pack) = 1; |
5585 | } | |
5586 | ||
5587 | SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args); | |
5588 | return argument_pack; | |
5589 | } | |
5590 | ||
8b5b8b7c MM |
5591 | /* Convert all template arguments to their appropriate types, and |
5592 | return a vector containing the innermost resulting template | |
c2ea3a40 | 5593 | arguments. If any error occurs, return error_mark_node. Error and |
3e4a3562 | 5594 | warning messages are issued under control of COMPLAIN. |
75650646 | 5595 | |
e7e93965 | 5596 | If REQUIRE_ALL_ARGS is false, argument deduction will be performed |
84dc00e8 | 5597 | for arguments not specified in ARGS. Otherwise, if |
e7e93965 MM |
5598 | USE_DEFAULT_ARGS is true, default arguments will be used to fill in |
5599 | unspecified arguments. If REQUIRE_ALL_ARGS is true, but | |
5600 | USE_DEFAULT_ARGS is false, then all arguments must be specified in | |
5601 | ARGS. */ | |
c8094d83 | 5602 | |
8d08fdba | 5603 | static tree |
c8094d83 | 5604 | coerce_template_parms (tree parms, |
0cbd7506 MS |
5605 | tree args, |
5606 | tree in_decl, | |
3a978d72 | 5607 | tsubst_flags_t complain, |
e7e93965 MM |
5608 | bool require_all_args, |
5609 | bool use_default_args) | |
8d08fdba | 5610 | { |
b1d7b1c0 | 5611 | int nparms, nargs, parm_idx, arg_idx, lost = 0; |
e4a84209 | 5612 | tree inner_args; |
8b5b8b7c MM |
5613 | tree new_args; |
5614 | tree new_inner_args; | |
7d882b83 ILT |
5615 | int saved_unevaluated_operand; |
5616 | int saved_inhibit_evaluation_warnings; | |
a292b002 | 5617 | |
5d80a306 DG |
5618 | /* When used as a boolean value, indicates whether this is a |
5619 | variadic template parameter list. Since it's an int, we can also | |
5620 | subtract it from nparms to get the number of non-variadic | |
5621 | parameters. */ | |
b1d7b1c0 | 5622 | int variadic_p = 0; |
5d80a306 | 5623 | |
a292b002 MS |
5624 | nparms = TREE_VEC_LENGTH (parms); |
5625 | ||
b1d7b1c0 DG |
5626 | /* Determine if there are any parameter packs. */ |
5627 | for (parm_idx = 0; parm_idx < nparms; ++parm_idx) | |
5628 | { | |
5629 | tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx)); | |
5630 | if (template_parameter_pack_p (tparm)) | |
ffaf1e05 | 5631 | ++variadic_p; |
b1d7b1c0 DG |
5632 | } |
5633 | ||
ffaf1e05 JM |
5634 | inner_args = INNERMOST_TEMPLATE_ARGS (args); |
5635 | /* If there are 0 or 1 parameter packs, we need to expand any argument | |
5636 | packs so that we can deduce a parameter pack from some non-packed args | |
5637 | followed by an argument pack, as in variadic85.C. If there are more | |
5638 | than that, we need to leave argument packs intact so the arguments are | |
5639 | assigned to the right parameter packs. This should only happen when | |
5640 | dealing with a nested class inside a partial specialization of a class | |
5641 | template, as in variadic92.C. */ | |
5642 | if (variadic_p <= 1) | |
5643 | inner_args = expand_template_argument_pack (inner_args); | |
5644 | ||
5645 | nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; | |
5646 | if ((nargs > nparms && !variadic_p) | |
5d80a306 | 5647 | || (nargs < nparms - variadic_p |
e7e93965 MM |
5648 | && require_all_args |
5649 | && (!use_default_args | |
42b304f1 LM |
5650 | || (TREE_VEC_ELT (parms, nargs) != error_mark_node |
5651 | && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))) | |
8d08fdba | 5652 | { |
c8094d83 | 5653 | if (complain & tf_error) |
75650646 | 5654 | { |
5d80a306 DG |
5655 | const char *or_more = ""; |
5656 | if (variadic_p) | |
5657 | { | |
5658 | or_more = " or more"; | |
5659 | --nparms; | |
5660 | } | |
5661 | ||
5662 | error ("wrong number of template arguments (%d, should be %d%s)", | |
5663 | nargs, nparms, or_more); | |
c8094d83 | 5664 | |
75650646 | 5665 | if (in_decl) |
dee15844 | 5666 | error ("provided for %q+D", in_decl); |
75650646 MM |
5667 | } |
5668 | ||
8d08fdba MS |
5669 | return error_mark_node; |
5670 | } | |
5671 | ||
a2c5ed87 MM |
5672 | /* We need to evaluate the template arguments, even though this |
5673 | template-id may be nested within a "sizeof". */ | |
7d882b83 ILT |
5674 | saved_unevaluated_operand = cp_unevaluated_operand; |
5675 | cp_unevaluated_operand = 0; | |
5676 | saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; | |
5677 | c_inhibit_evaluation_warnings = 0; | |
f31c0a32 | 5678 | new_inner_args = make_tree_vec (nparms); |
8b5b8b7c | 5679 | new_args = add_outermost_template_args (args, new_inner_args); |
b1d7b1c0 | 5680 | for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++) |
8d08fdba | 5681 | { |
8b5b8b7c MM |
5682 | tree arg; |
5683 | tree parm; | |
e4a84209 | 5684 | |
8b5b8b7c | 5685 | /* Get the Ith template parameter. */ |
b1d7b1c0 | 5686 | parm = TREE_VEC_ELT (parms, parm_idx); |
42b304f1 LM |
5687 | |
5688 | if (parm == error_mark_node) | |
2d8ba2c7 | 5689 | { |
b1d7b1c0 | 5690 | TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node; |
42b304f1 | 5691 | continue; |
2d8ba2c7 | 5692 | } |
75650646 | 5693 | |
b1d7b1c0 | 5694 | /* Calculate the next argument. */ |
ffaf1e05 JM |
5695 | if (arg_idx < nargs) |
5696 | arg = TREE_VEC_ELT (inner_args, arg_idx); | |
5697 | else | |
5698 | arg = NULL_TREE; | |
5699 | ||
5700 | if (template_parameter_pack_p (TREE_VALUE (parm)) | |
5701 | && !(arg && ARGUMENT_PACK_P (arg))) | |
5d80a306 | 5702 | { |
ffaf1e05 JM |
5703 | /* All remaining arguments will be placed in the |
5704 | template parameter pack PARM. */ | |
5705 | arg = coerce_template_parameter_pack (parms, parm_idx, args, | |
5706 | inner_args, arg_idx, | |
5707 | new_args, &lost, | |
5708 | in_decl, complain); | |
5709 | ||
b1d7b1c0 DG |
5710 | /* Store this argument. */ |
5711 | if (arg == error_mark_node) | |
5712 | lost++; | |
5713 | TREE_VEC_ELT (new_inner_args, parm_idx) = arg; | |
5714 | ||
ffaf1e05 JM |
5715 | /* We are done with all of the arguments. */ |
5716 | arg_idx = nargs; | |
5717 | ||
b1d7b1c0 DG |
5718 | continue; |
5719 | } | |
ffaf1e05 JM |
5720 | else if (arg) |
5721 | { | |
5722 | if (PACK_EXPANSION_P (arg)) | |
5d80a306 | 5723 | { |
4cf36211 DG |
5724 | if (complain & tf_error) |
5725 | { | |
ffaf1e05 JM |
5726 | /* FIXME this restriction was removed by N2555; see |
5727 | bug 35722. */ | |
4cf36211 DG |
5728 | /* If ARG is a pack expansion, but PARM is not a |
5729 | template parameter pack (if it were, we would have | |
5730 | handled it above), we're trying to expand into a | |
5731 | fixed-length argument list. */ | |
5732 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) | |
ffaf1e05 | 5733 | sorry ("cannot expand %<%E%> into a fixed-length " |
4cf36211 DG |
5734 | "argument list", arg); |
5735 | else | |
ffaf1e05 | 5736 | sorry ("cannot expand %<%T%> into a fixed-length " |
4cf36211 DG |
5737 | "argument list", arg); |
5738 | } | |
c343d5a7 | 5739 | return error_mark_node; |
5d80a306 DG |
5740 | } |
5741 | } | |
e7e93965 | 5742 | else if (require_all_args) |
b1d7b1c0 DG |
5743 | /* There must be a default arg in this case. */ |
5744 | arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args, | |
5745 | complain, in_decl); | |
ffd49b19 NS |
5746 | else |
5747 | break; | |
c8094d83 | 5748 | |
ffd49b19 | 5749 | if (arg == error_mark_node) |
e34b0922 KL |
5750 | { |
5751 | if (complain & tf_error) | |
b1d7b1c0 | 5752 | error ("template argument %d is invalid", arg_idx + 1); |
e34b0922 | 5753 | } |
b1d7b1c0 DG |
5754 | else if (!arg) |
5755 | /* This only occurs if there was an error in the template | |
5756 | parameter list itself (which we would already have | |
5757 | reported) that we are trying to recover from, e.g., a class | |
5758 | template with a parameter list such as | |
5759 | template<typename..., typename>. */ | |
5760 | return error_mark_node; | |
c8094d83 MS |
5761 | else |
5762 | arg = convert_template_argument (TREE_VALUE (parm), | |
b1d7b1c0 DG |
5763 | arg, new_args, complain, |
5764 | parm_idx, in_decl); | |
c8094d83 | 5765 | |
8b5b8b7c | 5766 | if (arg == error_mark_node) |
8d08fdba | 5767 | lost++; |
b1d7b1c0 | 5768 | TREE_VEC_ELT (new_inner_args, arg_idx) = arg; |
8d08fdba | 5769 | } |
7d882b83 ILT |
5770 | cp_unevaluated_operand = saved_unevaluated_operand; |
5771 | c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings; | |
8b5b8b7c | 5772 | |
8d08fdba MS |
5773 | if (lost) |
5774 | return error_mark_node; | |
8b5b8b7c MM |
5775 | |
5776 | return new_inner_args; | |
8d08fdba MS |
5777 | } |
5778 | ||
34016c81 JM |
5779 | /* Returns 1 if template args OT and NT are equivalent. */ |
5780 | ||
d8e178a0 | 5781 | static int |
3a978d72 | 5782 | template_args_equal (tree ot, tree nt) |
34016c81 JM |
5783 | { |
5784 | if (nt == ot) | |
5785 | return 1; | |
74601d7c | 5786 | |
34016c81 JM |
5787 | if (TREE_CODE (nt) == TREE_VEC) |
5788 | /* For member templates */ | |
74601d7c | 5789 | return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); |
5d80a306 DG |
5790 | else if (PACK_EXPANSION_P (ot)) |
5791 | return PACK_EXPANSION_P (nt) | |
5792 | && template_args_equal (PACK_EXPANSION_PATTERN (ot), | |
5793 | PACK_EXPANSION_PATTERN (nt)); | |
ffaf1e05 JM |
5794 | else if (ARGUMENT_PACK_P (ot)) |
5795 | { | |
5796 | int i, len; | |
5797 | tree opack, npack; | |
5798 | ||
5799 | if (!ARGUMENT_PACK_P (nt)) | |
5800 | return 0; | |
5801 | ||
5802 | opack = ARGUMENT_PACK_ARGS (ot); | |
5803 | npack = ARGUMENT_PACK_ARGS (nt); | |
5804 | len = TREE_VEC_LENGTH (opack); | |
5805 | if (TREE_VEC_LENGTH (npack) != len) | |
5806 | return 0; | |
5807 | for (i = 0; i < len; ++i) | |
5808 | if (!template_args_equal (TREE_VEC_ELT (opack, i), | |
5809 | TREE_VEC_ELT (npack, i))) | |
5810 | return 0; | |
5811 | return 1; | |
5812 | } | |
74601d7c | 5813 | else if (TYPE_P (nt)) |
660845bf | 5814 | return TYPE_P (ot) && same_type_p (ot, nt); |
74601d7c KL |
5815 | else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) |
5816 | return 0; | |
34016c81 | 5817 | else |
c8a209ca | 5818 | return cp_tree_equal (ot, nt); |
34016c81 JM |
5819 | } |
5820 | ||
5821 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
5822 | of template arguments. Returns 0 otherwise. */ |
5823 | ||
6757edfe | 5824 | int |
3a978d72 | 5825 | comp_template_args (tree oldargs, tree newargs) |
5566b478 MS |
5826 | { |
5827 | int i; | |
5828 | ||
386b8a85 JM |
5829 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
5830 | return 0; | |
5831 | ||
5566b478 MS |
5832 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
5833 | { | |
5834 | tree nt = TREE_VEC_ELT (newargs, i); | |
5835 | tree ot = TREE_VEC_ELT (oldargs, i); | |
5836 | ||
34016c81 | 5837 | if (! template_args_equal (ot, nt)) |
61a127b3 | 5838 | return 0; |
5566b478 MS |
5839 | } |
5840 | return 1; | |
5841 | } | |
5842 | ||
5566b478 | 5843 | static void |
3a978d72 | 5844 | add_pending_template (tree d) |
5566b478 | 5845 | { |
3ae18eaf JM |
5846 | tree ti = (TYPE_P (d) |
5847 | ? CLASSTYPE_TEMPLATE_INFO (d) | |
5848 | : DECL_TEMPLATE_INFO (d)); | |
e2c3721c | 5849 | struct pending_template *pt; |
3ae18eaf | 5850 | int level; |
e92cc029 | 5851 | |
824b9a4c | 5852 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
5853 | return; |
5854 | ||
3ae18eaf JM |
5855 | /* We are called both from instantiate_decl, where we've already had a |
5856 | tinst_level pushed, and instantiate_template, where we haven't. | |
5857 | Compensate. */ | |
e2c3721c | 5858 | level = !current_tinst_level || current_tinst_level->decl != d; |
3ae18eaf JM |
5859 | |
5860 | if (level) | |
5861 | push_tinst_level (d); | |
5862 | ||
e2c3721c PB |
5863 | pt = GGC_NEW (struct pending_template); |
5864 | pt->next = NULL; | |
5865 | pt->tinst = current_tinst_level; | |
46ccf50a | 5866 | if (last_pending_template) |
e2c3721c | 5867 | last_pending_template->next = pt; |
46ccf50a JM |
5868 | else |
5869 | pending_templates = pt; | |
5870 | ||
5871 | last_pending_template = pt; | |
5872 | ||
824b9a4c | 5873 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
3ae18eaf JM |
5874 | |
5875 | if (level) | |
5876 | pop_tinst_level (); | |
5566b478 MS |
5877 | } |
5878 | ||
386b8a85 | 5879 | |
4ba126e4 MM |
5880 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and |
5881 | ARGLIST. Valid choices for FNS are given in the cp-tree.def | |
5882 | documentation for TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
5883 | |
5884 | tree | |
3a978d72 | 5885 | lookup_template_function (tree fns, tree arglist) |
386b8a85 | 5886 | { |
2c73f9f5 | 5887 | tree type; |
050367a3 | 5888 | |
4ba126e4 MM |
5889 | if (fns == error_mark_node || arglist == error_mark_node) |
5890 | return error_mark_node; | |
5891 | ||
50bc768d | 5892 | gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); |
4230cec2 NS |
5893 | gcc_assert (fns && (is_overloaded_fn (fns) |
5894 | || TREE_CODE (fns) == IDENTIFIER_NODE)); | |
4ba126e4 | 5895 | |
50ad9642 MM |
5896 | if (BASELINK_P (fns)) |
5897 | { | |
f293ce4b RS |
5898 | BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR, |
5899 | unknown_type_node, | |
5900 | BASELINK_FUNCTIONS (fns), | |
5901 | arglist); | |
50ad9642 MM |
5902 | return fns; |
5903 | } | |
5904 | ||
2c73f9f5 ML |
5905 | type = TREE_TYPE (fns); |
5906 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
5907 | type = unknown_type_node; | |
c8094d83 | 5908 | |
f293ce4b | 5909 | return build2 (TEMPLATE_ID_EXPR, type, fns, arglist); |
386b8a85 JM |
5910 | } |
5911 | ||
a2b60a0e MM |
5912 | /* Within the scope of a template class S<T>, the name S gets bound |
5913 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
5914 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
5915 | or one of its enclosing classes, and that type is a template, | |
5916 | return the associated TEMPLATE_DECL. Otherwise, the original | |
5917 | DECL is returned. */ | |
5918 | ||
a723baf1 | 5919 | tree |
3a978d72 | 5920 | maybe_get_template_decl_from_type_decl (tree decl) |
a2b60a0e MM |
5921 | { |
5922 | return (decl != NULL_TREE | |
c8094d83 | 5923 | && TREE_CODE (decl) == TYPE_DECL |
a2b60a0e | 5924 | && DECL_ARTIFICIAL (decl) |
511b60ff | 5925 | && CLASS_TYPE_P (TREE_TYPE (decl)) |
c8094d83 | 5926 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
a2b60a0e MM |
5927 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; |
5928 | } | |
386b8a85 | 5929 | |
8d08fdba MS |
5930 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
5931 | parameters, find the desired type. | |
5932 | ||
5933 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
8d08fdba MS |
5934 | |
5935 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
c8094d83 | 5936 | instantiate. |
75650646 | 5937 | |
838dfd8a | 5938 | If ENTERING_SCOPE is nonzero, we are about to enter the scope of |
36a117a5 | 5939 | the class we are looking up. |
c8094d83 | 5940 | |
c2ea3a40 | 5941 | Issue error and warning messages under control of COMPLAIN. |
36a117a5 | 5942 | |
75650646 MM |
5943 | If the template class is really a local class in a template |
5944 | function, then the FUNCTION_CONTEXT is the function in which it is | |
c8094d83 | 5945 | being instantiated. |
b6ab6892 GB |
5946 | |
5947 | ??? Note that this function is currently called *twice* for each | |
5948 | template-id: the first time from the parser, while creating the | |
5949 | incomplete type (finish_template_type), and the second type during the | |
5950 | real instantiation (instantiate_template_class). This is surely something | |
5951 | that we want to avoid. It also causes some problems with argument | |
5952 | coercion (see convert_nontype_argument for more information on this). */ | |
e92cc029 | 5953 | |
8d08fdba | 5954 | tree |
c8094d83 | 5955 | lookup_template_class (tree d1, |
0cbd7506 MS |
5956 | tree arglist, |
5957 | tree in_decl, | |
5958 | tree context, | |
5959 | int entering_scope, | |
5960 | tsubst_flags_t complain) | |
8d08fdba | 5961 | { |
be93747e | 5962 | tree templ = NULL_TREE, parmlist; |
dbfe2124 | 5963 | tree t; |
2b110bfc JM |
5964 | spec_entry **slot; |
5965 | spec_entry *entry; | |
5966 | spec_entry elt; | |
5967 | hashval_t hash; | |
c8094d83 | 5968 | |
fd295cb2 | 5969 | timevar_push (TV_NAME_LOOKUP); |
c8094d83 | 5970 | |
5566b478 MS |
5971 | if (TREE_CODE (d1) == IDENTIFIER_NODE) |
5972 | { | |
90ea9897 MM |
5973 | tree value = innermost_non_namespace_value (d1); |
5974 | if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) | |
be93747e | 5975 | templ = value; |
73b0fce8 KL |
5976 | else |
5977 | { | |
2c73f9f5 ML |
5978 | if (context) |
5979 | push_decl_namespace (context); | |
be93747e KG |
5980 | templ = lookup_name (d1); |
5981 | templ = maybe_get_template_decl_from_type_decl (templ); | |
2c73f9f5 ML |
5982 | if (context) |
5983 | pop_decl_namespace (); | |
73b0fce8 | 5984 | } |
be93747e KG |
5985 | if (templ) |
5986 | context = DECL_CONTEXT (templ); | |
5566b478 | 5987 | } |
9e1e64ec | 5988 | else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1))) |
c91a56d2 | 5989 | { |
802dbc34 JM |
5990 | tree type = TREE_TYPE (d1); |
5991 | ||
5992 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
5993 | an implicit typename for the second A. Deal with it. */ | |
5994 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
5995 | type = TREE_TYPE (type); | |
c8094d83 | 5996 | |
802dbc34 | 5997 | if (CLASSTYPE_TEMPLATE_INFO (type)) |
f3400fe2 | 5998 | { |
be93747e KG |
5999 | templ = CLASSTYPE_TI_TEMPLATE (type); |
6000 | d1 = DECL_NAME (templ); | |
f3400fe2 | 6001 | } |
c91a56d2 | 6002 | } |
c8094d83 | 6003 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
9e1e64ec | 6004 | || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1))) |
5566b478 | 6005 | { |
be93747e KG |
6006 | templ = TYPE_TI_TEMPLATE (d1); |
6007 | d1 = DECL_NAME (templ); | |
5566b478 | 6008 | } |
93cdc044 | 6009 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
b55276c8 | 6010 | && DECL_TEMPLATE_RESULT (d1) |
17aec3eb | 6011 | && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) |
93cdc044 | 6012 | { |
be93747e KG |
6013 | templ = d1; |
6014 | d1 = DECL_NAME (templ); | |
6015 | context = DECL_CONTEXT (templ); | |
93cdc044 | 6016 | } |
8d08fdba | 6017 | |
90ea9897 | 6018 | /* Issue an error message if we didn't find a template. */ |
be93747e | 6019 | if (! templ) |
f3400fe2 | 6020 | { |
c2ea3a40 | 6021 | if (complain & tf_error) |
0cbd7506 | 6022 | error ("%qT is not a template", d1); |
fd295cb2 | 6023 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
f3400fe2 | 6024 | } |
2c73f9f5 | 6025 | |
be93747e | 6026 | if (TREE_CODE (templ) != TEMPLATE_DECL |
0cbd7506 | 6027 | /* Make sure it's a user visible template, if it was named by |
42eaed49 | 6028 | the user. */ |
be93747e KG |
6029 | || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ) |
6030 | && !PRIMARY_TEMPLATE_P (templ))) | |
8d08fdba | 6031 | { |
c2ea3a40 | 6032 | if (complain & tf_error) |
0cbd7506 MS |
6033 | { |
6034 | error ("non-template type %qT used as a template", d1); | |
6035 | if (in_decl) | |
dee15844 | 6036 | error ("for template declaration %q+D", in_decl); |
f9c244b8 | 6037 | } |
fd295cb2 | 6038 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
8d08fdba | 6039 | } |
8d08fdba | 6040 | |
42eaed49 | 6041 | complain &= ~tf_user; |
c8094d83 | 6042 | |
be93747e | 6043 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ)) |
73b0fce8 KL |
6044 | { |
6045 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
0cbd7506 | 6046 | template arguments */ |
73b0fce8 | 6047 | |
1899c3a4 | 6048 | tree parm; |
73b0fce8 | 6049 | tree arglist2; |
ef3b7b17 | 6050 | tree outer; |
73b0fce8 | 6051 | |
be93747e | 6052 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ); |
73b0fce8 | 6053 | |
4f96ff63 KL |
6054 | /* Consider an example where a template template parameter declared as |
6055 | ||
6056 | template <class T, class U = std::allocator<T> > class TT | |
6057 | ||
c8094d83 MS |
6058 | The template parameter level of T and U are one level larger than |
6059 | of TT. To proper process the default argument of U, say when an | |
4f96ff63 | 6060 | instantiation `TT<int>' is seen, we need to build the full |
342cea95 KL |
6061 | arguments containing {int} as the innermost level. Outer levels, |
6062 | available when not appearing as default template argument, can be | |
ef3b7b17 | 6063 | obtained from the arguments of the enclosing template. |
4f96ff63 | 6064 | |
342cea95 KL |
6065 | Suppose that TT is later substituted with std::vector. The above |
6066 | instantiation is `TT<int, std::allocator<T> >' with TT at | |
6067 | level 1, and T at level 2, while the template arguments at level 1 | |
6068 | becomes {std::vector} and the inner level 2 is {int}. */ | |
6069 | ||
be93747e | 6070 | outer = DECL_CONTEXT (templ); |
ef3b7b17 JM |
6071 | if (outer) |
6072 | outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer))); | |
6073 | else if (current_template_parms) | |
6074 | /* This is an argument of the current template, so we haven't set | |
6075 | DECL_CONTEXT yet. */ | |
6076 | outer = current_template_args (); | |
6077 | ||
6078 | if (outer) | |
6079 | arglist = add_to_template_args (outer, arglist); | |
4f96ff63 | 6080 | |
be93747e | 6081 | arglist2 = coerce_template_parms (parmlist, arglist, templ, |
3db45ab5 | 6082 | complain, |
e7e93965 MM |
6083 | /*require_all_args=*/true, |
6084 | /*use_default_args=*/true); | |
3e4a3562 | 6085 | if (arglist2 == error_mark_node |
544aef8c | 6086 | || (!uses_template_parms (arglist2) |
be93747e | 6087 | && check_instantiated_args (templ, arglist2, complain))) |
0cbd7506 | 6088 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
73b0fce8 | 6089 | |
be93747e | 6090 | parm = bind_template_template_parm (TREE_TYPE (templ), arglist2); |
fd295cb2 | 6091 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm); |
73b0fce8 | 6092 | } |
c8094d83 | 6093 | else |
8d08fdba | 6094 | { |
be93747e | 6095 | tree template_type = TREE_TYPE (templ); |
7ac7b28f | 6096 | tree gen_tmpl; |
36a117a5 MM |
6097 | tree type_decl; |
6098 | tree found = NULL_TREE; | |
6099 | int arg_depth; | |
6100 | int parm_depth; | |
dbfe2124 | 6101 | int is_partial_instantiation; |
830bfa74 | 6102 | |
be93747e | 6103 | gen_tmpl = most_general_template (templ); |
7ac7b28f | 6104 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); |
36a117a5 MM |
6105 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
6106 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
6107 | ||
6108 | if (arg_depth == 1 && parm_depth > 1) | |
6109 | { | |
39c01e4c | 6110 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
6111 | For example, given: |
6112 | ||
6113 | template <class T> struct S1 { | |
0cbd7506 | 6114 | template <class U> struct S2 {}; |
36a117a5 | 6115 | template <class U> struct S2<U*> {}; |
0cbd7506 | 6116 | }; |
c8094d83 | 6117 | |
36a117a5 MM |
6118 | we will be called with an ARGLIST of `U*', but the |
6119 | TEMPLATE will be `template <class T> template | |
6120 | <class U> struct S1<T>::S2'. We must fill in the missing | |
6121 | arguments. */ | |
c8094d83 | 6122 | arglist |
be93747e | 6123 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)), |
7ac7b28f | 6124 | arglist); |
36a117a5 MM |
6125 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
6126 | } | |
5566b478 | 6127 | |
41f5d4b1 | 6128 | /* Now we should have enough arguments. */ |
50bc768d | 6129 | gcc_assert (parm_depth == arg_depth); |
c8094d83 | 6130 | |
7ac7b28f MM |
6131 | /* From here on, we're only interested in the most general |
6132 | template. */ | |
7ac7b28f | 6133 | |
36a117a5 MM |
6134 | /* Calculate the BOUND_ARGS. These will be the args that are |
6135 | actually tsubst'd into the definition to create the | |
6136 | instantiation. */ | |
6137 | if (parm_depth > 1) | |
830bfa74 MM |
6138 | { |
6139 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 6140 | int i; |
e4a84209 | 6141 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 6142 | |
f31c0a32 | 6143 | tree bound_args = make_tree_vec (parm_depth); |
c8094d83 | 6144 | |
e4a84209 | 6145 | for (i = saved_depth, |
2b110bfc | 6146 | t = DECL_TEMPLATE_PARMS (gen_tmpl); |
e4a84209 | 6147 | i > 0 && t != NULL_TREE; |
830bfa74 | 6148 | --i, t = TREE_CHAIN (t)) |
e4a84209 | 6149 | { |
ee3071ef | 6150 | tree a = coerce_template_parms (TREE_VALUE (t), |
2b110bfc | 6151 | arglist, gen_tmpl, |
3db45ab5 | 6152 | complain, |
e7e93965 MM |
6153 | /*require_all_args=*/true, |
6154 | /*use_default_args=*/true); | |
88e98cfe KL |
6155 | |
6156 | /* Don't process further if one of the levels fails. */ | |
6157 | if (a == error_mark_node) | |
6158 | { | |
6159 | /* Restore the ARGLIST to its full size. */ | |
6160 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
6161 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
6162 | } | |
c8094d83 | 6163 | |
e4a84209 MM |
6164 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); |
6165 | ||
6166 | /* We temporarily reduce the length of the ARGLIST so | |
6167 | that coerce_template_parms will see only the arguments | |
6168 | corresponding to the template parameters it is | |
6169 | examining. */ | |
6170 | TREE_VEC_LENGTH (arglist)--; | |
6171 | } | |
6172 | ||
6173 | /* Restore the ARGLIST to its full size. */ | |
6174 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
6175 | ||
36a117a5 | 6176 | arglist = bound_args; |
830bfa74 MM |
6177 | } |
6178 | else | |
36a117a5 MM |
6179 | arglist |
6180 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
f9a7ae04 | 6181 | INNERMOST_TEMPLATE_ARGS (arglist), |
2b110bfc | 6182 | gen_tmpl, |
3db45ab5 | 6183 | complain, |
e7e93965 MM |
6184 | /*require_all_args=*/true, |
6185 | /*use_default_args=*/true); | |
36a117a5 | 6186 | |
c353b8e3 | 6187 | if (arglist == error_mark_node) |
36a117a5 | 6188 | /* We were unable to bind the arguments. */ |
fd295cb2 | 6189 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
5566b478 | 6190 | |
36a117a5 MM |
6191 | /* In the scope of a template class, explicit references to the |
6192 | template class refer to the type of the template, not any | |
6193 | instantiation of it. For example, in: | |
c8094d83 | 6194 | |
36a117a5 MM |
6195 | template <class T> class C { void f(C<T>); } |
6196 | ||
6197 | the `C<T>' is just the same as `C'. Outside of the | |
6198 | class, however, such a reference is an instantiation. */ | |
fdfacfa1 | 6199 | if ((entering_scope |
2b110bfc | 6200 | || !PRIMARY_TEMPLATE_P (gen_tmpl) |
fdfacfa1 JM |
6201 | || currently_open_class (template_type)) |
6202 | /* comp_template_args is expensive, check it last. */ | |
6203 | && comp_template_args (TYPE_TI_ARGS (template_type), | |
6204 | arglist)) | |
6205 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type); | |
414ea4aa | 6206 | |
c7222c02 | 6207 | /* If we already have this specialization, return it. */ |
2b110bfc JM |
6208 | elt.tmpl = gen_tmpl; |
6209 | elt.args = arglist; | |
6210 | hash = hash_specialization (&elt); | |
6211 | entry = (spec_entry *) htab_find_with_hash (type_specializations, | |
6212 | &elt, hash); | |
6213 | ||
6214 | if (entry) | |
6215 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec); | |
5566b478 | 6216 | |
dbfe2124 | 6217 | /* This type is a "partial instantiation" if any of the template |
ab097535 | 6218 | arguments still involve template parameters. Note that we set |
486e4077 MM |
6219 | IS_PARTIAL_INSTANTIATION for partial specializations as |
6220 | well. */ | |
dbfe2124 MM |
6221 | is_partial_instantiation = uses_template_parms (arglist); |
6222 | ||
c353b8e3 MM |
6223 | /* If the deduced arguments are invalid, then the binding |
6224 | failed. */ | |
6225 | if (!is_partial_instantiation | |
2b110bfc | 6226 | && check_instantiated_args (gen_tmpl, |
c353b8e3 MM |
6227 | INNERMOST_TEMPLATE_ARGS (arglist), |
6228 | complain)) | |
6229 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
c8094d83 MS |
6230 | |
6231 | if (!is_partial_instantiation | |
2b110bfc | 6232 | && !PRIMARY_TEMPLATE_P (gen_tmpl) |
d5f4eddd | 6233 | && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl)) |
2b110bfc | 6234 | && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL) |
3ebc5c52 | 6235 | { |
2b110bfc JM |
6236 | found = xref_tag_from_type (TREE_TYPE (gen_tmpl), |
6237 | DECL_NAME (gen_tmpl), | |
29ef83de | 6238 | /*tag_scope=*/ts_global); |
fd295cb2 | 6239 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
3ebc5c52 | 6240 | } |
c8094d83 | 6241 | |
2b110bfc | 6242 | context = tsubst (DECL_CONTEXT (gen_tmpl), arglist, |
c2ea3a40 | 6243 | complain, in_decl); |
95ee998c MM |
6244 | if (!context) |
6245 | context = global_namespace; | |
6246 | ||
36a117a5 | 6247 | /* Create the type. */ |
ed44da02 MM |
6248 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
6249 | { | |
dbfe2124 | 6250 | if (!is_partial_instantiation) |
92777ce4 NS |
6251 | { |
6252 | set_current_access_from_decl (TYPE_NAME (template_type)); | |
adf2edec DG |
6253 | t = start_enum (TYPE_IDENTIFIER (template_type), |
6254 | tsubst (ENUM_UNDERLYING_TYPE (template_type), | |
6255 | arglist, complain, in_decl), | |
6256 | SCOPED_ENUM_P (template_type)); | |
92777ce4 | 6257 | } |
ed44da02 | 6258 | else |
adf2edec DG |
6259 | { |
6260 | /* We don't want to call start_enum for this type, since | |
6261 | the values for the enumeration constants may involve | |
6262 | template parameters. And, no one should be interested | |
6263 | in the enumeration constants for such a type. */ | |
6264 | t = make_node (ENUMERAL_TYPE); | |
6265 | SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type)); | |
6266 | } | |
ed44da02 MM |
6267 | } |
6268 | else | |
6269 | { | |
9e1e64ec | 6270 | t = make_class_type (TREE_CODE (template_type)); |
c8094d83 | 6271 | CLASSTYPE_DECLARED_CLASS (t) |
ed44da02 | 6272 | = CLASSTYPE_DECLARED_CLASS (template_type); |
ed44da02 | 6273 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); |
f668f160 | 6274 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ae673f14 JM |
6275 | |
6276 | /* A local class. Make sure the decl gets registered properly. */ | |
6277 | if (context == current_function_decl) | |
2b110bfc | 6278 | pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current); |
06d40de8 DG |
6279 | |
6280 | if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist)) | |
6281 | /* This instantiation is another name for the primary | |
6282 | template type. Set the TYPE_CANONICAL field | |
6283 | appropriately. */ | |
6284 | TYPE_CANONICAL (t) = template_type; | |
6285 | else if (any_template_arguments_need_structural_equality_p (arglist)) | |
6286 | /* Some of the template arguments require structural | |
6287 | equality testing, so this template class requires | |
6288 | structural equality testing. */ | |
6289 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
ed44da02 MM |
6290 | } |
6291 | ||
ae673f14 JM |
6292 | /* If we called start_enum or pushtag above, this information |
6293 | will already be set up. */ | |
ed44da02 MM |
6294 | if (!TYPE_NAME (t)) |
6295 | { | |
6296 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
c8094d83 | 6297 | |
2b110bfc | 6298 | type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t); |
ed44da02 | 6299 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
9188c363 | 6300 | TYPE_STUB_DECL (t) = type_decl; |
c8094d83 | 6301 | DECL_SOURCE_LOCATION (type_decl) |
f31686a3 | 6302 | = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); |
ed44da02 MM |
6303 | } |
6304 | else | |
6305 | type_decl = TYPE_NAME (t); | |
36a117a5 | 6306 | |
cab7a9a3 KL |
6307 | TREE_PRIVATE (type_decl) |
6308 | = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); | |
6309 | TREE_PROTECTED (type_decl) | |
6310 | = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); | |
8d039470 MS |
6311 | if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type)) |
6312 | { | |
6313 | DECL_VISIBILITY_SPECIFIED (type_decl) = 1; | |
6314 | DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type); | |
6315 | } | |
cab7a9a3 | 6316 | |
9fbf56f7 MM |
6317 | /* Set up the template information. We have to figure out which |
6318 | template is the immediate parent if this is a full | |
6319 | instantiation. */ | |
6320 | if (parm_depth == 1 || is_partial_instantiation | |
2b110bfc | 6321 | || !PRIMARY_TEMPLATE_P (gen_tmpl)) |
9fbf56f7 | 6322 | /* This case is easy; there are no member templates involved. */ |
2b110bfc | 6323 | found = gen_tmpl; |
9fbf56f7 MM |
6324 | else |
6325 | { | |
2b110bfc JM |
6326 | /* This is a full instantiation of a member template. Find |
6327 | the partial instantiation of which this is an instance. */ | |
6328 | ||
6329 | /* Temporarily reduce by one the number of levels in the ARGLIST | |
6330 | so as to avoid comparing the last set of arguments. */ | |
6331 | TREE_VEC_LENGTH (arglist)--; | |
6332 | found = tsubst (gen_tmpl, arglist, complain, NULL_TREE); | |
6333 | TREE_VEC_LENGTH (arglist)++; | |
6334 | found = CLASSTYPE_TI_TEMPLATE (found); | |
9fbf56f7 MM |
6335 | } |
6336 | ||
c8094d83 | 6337 | SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE)); |
2b110bfc JM |
6338 | |
6339 | elt.spec = t; | |
6340 | slot = (spec_entry **) htab_find_slot_with_hash (type_specializations, | |
6341 | &elt, hash, INSERT); | |
6342 | *slot = GGC_NEW (spec_entry); | |
6343 | **slot = elt; | |
6344 | ||
6345 | /* Note this use of the partial instantiation so we can check it | |
6346 | later in maybe_process_partial_specialization. */ | |
be93747e | 6347 | DECL_TEMPLATE_INSTANTIATIONS (templ) |
c8094d83 | 6348 | = tree_cons (arglist, t, |
be93747e | 6349 | DECL_TEMPLATE_INSTANTIATIONS (templ)); |
dbfe2124 | 6350 | |
c8094d83 | 6351 | if (TREE_CODE (t) == ENUMERAL_TYPE |
dbfe2124 | 6352 | && !is_partial_instantiation) |
61a127b3 MM |
6353 | /* Now that the type has been registered on the instantiations |
6354 | list, we set up the enumerators. Because the enumeration | |
6355 | constants may involve the enumeration type itself, we make | |
6356 | sure to register the type first, and then create the | |
6357 | constants. That way, doing tsubst_expr for the enumeration | |
6358 | constants won't result in recursive calls here; we'll find | |
6359 | the instantiation and exit above. */ | |
6360 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 | 6361 | |
7813d14c | 6362 | if (is_partial_instantiation) |
077e7015 MM |
6363 | /* If the type makes use of template parameters, the |
6364 | code that generates debugging information will crash. */ | |
6365 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 6366 | |
b9e75696 JM |
6367 | /* Possibly limit visibility based on template args. */ |
6368 | TREE_PUBLIC (type_decl) = 1; | |
6369 | determine_visibility (type_decl); | |
6370 | ||
fd295cb2 | 6371 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
36a117a5 | 6372 | } |
fd295cb2 | 6373 | timevar_pop (TV_NAME_LOOKUP); |
8d08fdba MS |
6374 | } |
6375 | \f | |
c8094d83 | 6376 | struct pair_fn_data |
8d08fdba | 6377 | { |
8dfaeb63 MM |
6378 | tree_fn_t fn; |
6379 | void *data; | |
c095a4f8 DG |
6380 | /* True when we should also visit template parameters that occur in |
6381 | non-deduced contexts. */ | |
6382 | bool include_nondeduced_p; | |
0c58f841 | 6383 | struct pointer_set_t *visited; |
8dfaeb63 MM |
6384 | }; |
6385 | ||
6386 | /* Called from for_each_template_parm via walk_tree. */ | |
581d38d0 | 6387 | |
8dfaeb63 | 6388 | static tree |
350fae66 | 6389 | for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) |
8dfaeb63 MM |
6390 | { |
6391 | tree t = *tp; | |
6392 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; | |
6393 | tree_fn_t fn = pfd->fn; | |
6394 | void *data = pfd->data; | |
4f2c9d7e | 6395 | |
2f939d94 | 6396 | if (TYPE_P (t) |
c095a4f8 DG |
6397 | && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE) |
6398 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited, | |
6399 | pfd->include_nondeduced_p)) | |
8dfaeb63 | 6400 | return error_mark_node; |
581d38d0 | 6401 | |
8d08fdba MS |
6402 | switch (TREE_CODE (t)) |
6403 | { | |
8d08fdba | 6404 | case RECORD_TYPE: |
9076e292 | 6405 | if (TYPE_PTRMEMFUNC_P (t)) |
8dfaeb63 | 6406 | break; |
ed44da02 MM |
6407 | /* Fall through. */ |
6408 | ||
8d08fdba | 6409 | case UNION_TYPE: |
ed44da02 | 6410 | case ENUMERAL_TYPE: |
8dfaeb63 MM |
6411 | if (!TYPE_TEMPLATE_INFO (t)) |
6412 | *walk_subtrees = 0; | |
6413 | else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)), | |
c095a4f8 DG |
6414 | fn, data, pfd->visited, |
6415 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
6416 | return error_mark_node; |
6417 | break; | |
6418 | ||
8f6e6bf3 EB |
6419 | case INTEGER_TYPE: |
6420 | if (for_each_template_parm (TYPE_MIN_VALUE (t), | |
c095a4f8 DG |
6421 | fn, data, pfd->visited, |
6422 | pfd->include_nondeduced_p) | |
8f6e6bf3 | 6423 | || for_each_template_parm (TYPE_MAX_VALUE (t), |
c095a4f8 DG |
6424 | fn, data, pfd->visited, |
6425 | pfd->include_nondeduced_p)) | |
8f6e6bf3 EB |
6426 | return error_mark_node; |
6427 | break; | |
6428 | ||
588c2d1c | 6429 | case METHOD_TYPE: |
8dfaeb63 MM |
6430 | /* Since we're not going to walk subtrees, we have to do this |
6431 | explicitly here. */ | |
4f2c9d7e | 6432 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, |
c095a4f8 | 6433 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 | 6434 | return error_mark_node; |
4890c2f4 | 6435 | /* Fall through. */ |
588c2d1c MM |
6436 | |
6437 | case FUNCTION_TYPE: | |
8dfaeb63 | 6438 | /* Check the return type. */ |
c095a4f8 DG |
6439 | if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited, |
6440 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
6441 | return error_mark_node; |
6442 | ||
588c2d1c MM |
6443 | /* Check the parameter types. Since default arguments are not |
6444 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
6445 | contain expressions that involve template parameters. But, | |
6446 | no-one should be looking at them yet. And, once they're | |
6447 | instantiated, they don't contain template parameters, so | |
6448 | there's no point in looking at them then, either. */ | |
6449 | { | |
6450 | tree parm; | |
6451 | ||
6452 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4f2c9d7e | 6453 | if (for_each_template_parm (TREE_VALUE (parm), fn, data, |
c095a4f8 | 6454 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 | 6455 | return error_mark_node; |
5566b478 | 6456 | |
8dfaeb63 MM |
6457 | /* Since we've already handled the TYPE_ARG_TYPES, we don't |
6458 | want walk_tree walking into them itself. */ | |
6459 | *walk_subtrees = 0; | |
6460 | } | |
6461 | break; | |
3ac3d9ea | 6462 | |
a723baf1 | 6463 | case TYPEOF_TYPE: |
c095a4f8 DG |
6464 | if (pfd->include_nondeduced_p |
6465 | && for_each_template_parm (TYPE_FIELDS (t), fn, data, | |
6466 | pfd->visited, | |
6467 | pfd->include_nondeduced_p)) | |
a723baf1 MM |
6468 | return error_mark_node; |
6469 | break; | |
6470 | ||
8d08fdba | 6471 | case FUNCTION_DECL: |
5566b478 | 6472 | case VAR_DECL: |
5566b478 | 6473 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
4f2c9d7e | 6474 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data, |
c095a4f8 | 6475 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 MM |
6476 | return error_mark_node; |
6477 | /* Fall through. */ | |
6478 | ||
8d08fdba | 6479 | case PARM_DECL: |
a723baf1 MM |
6480 | case CONST_DECL: |
6481 | if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t) | |
6482 | && for_each_template_parm (DECL_INITIAL (t), fn, data, | |
c095a4f8 | 6483 | pfd->visited, pfd->include_nondeduced_p)) |
a723baf1 | 6484 | return error_mark_node; |
c8094d83 | 6485 | if (DECL_CONTEXT (t) |
c095a4f8 | 6486 | && pfd->include_nondeduced_p |
4f2c9d7e | 6487 | && for_each_template_parm (DECL_CONTEXT (t), fn, data, |
c095a4f8 | 6488 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 MM |
6489 | return error_mark_node; |
6490 | break; | |
8d08fdba | 6491 | |
a1281f45 | 6492 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
744fac59 | 6493 | /* Record template parameters such as `T' inside `TT<T>'. */ |
c095a4f8 DG |
6494 | if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited, |
6495 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
6496 | return error_mark_node; |
6497 | /* Fall through. */ | |
6498 | ||
a1281f45 | 6499 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 6500 | case TEMPLATE_TYPE_PARM: |
f84b4be9 | 6501 | case TEMPLATE_PARM_INDEX: |
8dfaeb63 MM |
6502 | if (fn && (*fn)(t, data)) |
6503 | return error_mark_node; | |
6504 | else if (!fn) | |
6505 | return error_mark_node; | |
6506 | break; | |
5156628f | 6507 | |
8dfaeb63 | 6508 | case TEMPLATE_DECL: |
f4f206f4 | 6509 | /* A template template parameter is encountered. */ |
8dfaeb63 | 6510 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) |
c095a4f8 DG |
6511 | && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited, |
6512 | pfd->include_nondeduced_p)) | |
8dfaeb63 | 6513 | return error_mark_node; |
db5ae43f | 6514 | |
8dfaeb63 MM |
6515 | /* Already substituted template template parameter */ |
6516 | *walk_subtrees = 0; | |
6517 | break; | |
b8865407 | 6518 | |
4699c561 | 6519 | case TYPENAME_TYPE: |
c8094d83 | 6520 | if (!fn |
4f2c9d7e | 6521 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, |
c095a4f8 DG |
6522 | data, pfd->visited, |
6523 | pfd->include_nondeduced_p)) | |
8dfaeb63 MM |
6524 | return error_mark_node; |
6525 | break; | |
4699c561 | 6526 | |
8dfaeb63 MM |
6527 | case CONSTRUCTOR: |
6528 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) | |
c095a4f8 | 6529 | && pfd->include_nondeduced_p |
8dfaeb63 | 6530 | && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE |
4f2c9d7e | 6531 | (TREE_TYPE (t)), fn, data, |
c095a4f8 | 6532 | pfd->visited, pfd->include_nondeduced_p)) |
8dfaeb63 MM |
6533 | return error_mark_node; |
6534 | break; | |
c8094d83 | 6535 | |
b8865407 MM |
6536 | case INDIRECT_REF: |
6537 | case COMPONENT_REF: | |
4699c561 | 6538 | /* If there's no type, then this thing must be some expression |
b8865407 | 6539 | involving template parameters. */ |
4699c561 | 6540 | if (!fn && !TREE_TYPE (t)) |
8dfaeb63 MM |
6541 | return error_mark_node; |
6542 | break; | |
b8865407 | 6543 | |
42976354 BK |
6544 | case MODOP_EXPR: |
6545 | case CAST_EXPR: | |
6546 | case REINTERPRET_CAST_EXPR: | |
6547 | case CONST_CAST_EXPR: | |
6548 | case STATIC_CAST_EXPR: | |
6549 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
6550 | case ARROW_EXPR: |
6551 | case DOTSTAR_EXPR: | |
6552 | case TYPEID_EXPR: | |
40242ccf | 6553 | case PSEUDO_DTOR_EXPR: |
b8865407 | 6554 | if (!fn) |
8dfaeb63 MM |
6555 | return error_mark_node; |
6556 | break; | |
abff8e06 | 6557 | |
8d08fdba | 6558 | default: |
8dfaeb63 | 6559 | break; |
8d08fdba | 6560 | } |
8dfaeb63 MM |
6561 | |
6562 | /* We didn't find any template parameters we liked. */ | |
6563 | return NULL_TREE; | |
6564 | } | |
6565 | ||
c8094d83 MS |
6566 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, |
6567 | BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, | |
a1281f45 | 6568 | call FN with the parameter and the DATA. |
838dfd8a | 6569 | If FN returns nonzero, the iteration is terminated, and |
8dfaeb63 | 6570 | for_each_template_parm returns 1. Otherwise, the iteration |
838dfd8a | 6571 | continues. If FN never returns a nonzero value, the value |
8dfaeb63 | 6572 | returned by for_each_template_parm is 0. If FN is NULL, it is |
c095a4f8 DG |
6573 | considered to be the function which always returns 1. |
6574 | ||
6575 | If INCLUDE_NONDEDUCED_P, then this routine will also visit template | |
6576 | parameters that occur in non-deduced contexts. When false, only | |
6577 | visits those template parameters that can be deduced. */ | |
8dfaeb63 MM |
6578 | |
6579 | static int | |
0c58f841 | 6580 | for_each_template_parm (tree t, tree_fn_t fn, void* data, |
c095a4f8 DG |
6581 | struct pointer_set_t *visited, |
6582 | bool include_nondeduced_p) | |
8dfaeb63 MM |
6583 | { |
6584 | struct pair_fn_data pfd; | |
ad2ae3b2 | 6585 | int result; |
8dfaeb63 MM |
6586 | |
6587 | /* Set up. */ | |
6588 | pfd.fn = fn; | |
6589 | pfd.data = data; | |
c095a4f8 | 6590 | pfd.include_nondeduced_p = include_nondeduced_p; |
8dfaeb63 | 6591 | |
4890c2f4 MM |
6592 | /* Walk the tree. (Conceptually, we would like to walk without |
6593 | duplicates, but for_each_template_parm_r recursively calls | |
6594 | for_each_template_parm, so we would need to reorganize a fair | |
4f2c9d7e MM |
6595 | bit to use walk_tree_without_duplicates, so we keep our own |
6596 | visited list.) */ | |
6597 | if (visited) | |
6598 | pfd.visited = visited; | |
6599 | else | |
0c58f841 | 6600 | pfd.visited = pointer_set_create (); |
14588106 RG |
6601 | result = cp_walk_tree (&t, |
6602 | for_each_template_parm_r, | |
6603 | &pfd, | |
6604 | pfd.visited) != NULL_TREE; | |
ad2ae3b2 MM |
6605 | |
6606 | /* Clean up. */ | |
6607 | if (!visited) | |
0c58f841 MA |
6608 | { |
6609 | pointer_set_destroy (pfd.visited); | |
6610 | pfd.visited = 0; | |
6611 | } | |
ad2ae3b2 MM |
6612 | |
6613 | return result; | |
8d08fdba MS |
6614 | } |
6615 | ||
d43f603d KL |
6616 | /* Returns true if T depends on any template parameter. */ |
6617 | ||
050367a3 | 6618 | int |
3a978d72 | 6619 | uses_template_parms (tree t) |
050367a3 | 6620 | { |
c353b8e3 MM |
6621 | bool dependent_p; |
6622 | int saved_processing_template_decl; | |
6623 | ||
6624 | saved_processing_template_decl = processing_template_decl; | |
6625 | if (!saved_processing_template_decl) | |
6626 | processing_template_decl = 1; | |
6627 | if (TYPE_P (t)) | |
6628 | dependent_p = dependent_type_p (t); | |
6629 | else if (TREE_CODE (t) == TREE_VEC) | |
6630 | dependent_p = any_dependent_template_arguments_p (t); | |
6631 | else if (TREE_CODE (t) == TREE_LIST) | |
6632 | dependent_p = (uses_template_parms (TREE_VALUE (t)) | |
6633 | || uses_template_parms (TREE_CHAIN (t))); | |
3ce5fa4f NS |
6634 | else if (TREE_CODE (t) == TYPE_DECL) |
6635 | dependent_p = dependent_type_p (TREE_TYPE (t)); | |
c8094d83 MS |
6636 | else if (DECL_P (t) |
6637 | || EXPR_P (t) | |
c353b8e3 MM |
6638 | || TREE_CODE (t) == TEMPLATE_PARM_INDEX |
6639 | || TREE_CODE (t) == OVERLOAD | |
6640 | || TREE_CODE (t) == BASELINK | |
b207d6e2 | 6641 | || TREE_CODE (t) == IDENTIFIER_NODE |
cb68ec50 | 6642 | || TREE_CODE (t) == TRAIT_EXPR |
c1b93f2b | 6643 | || TREE_CODE (t) == CONSTRUCTOR |
6615c446 | 6644 | || CONSTANT_CLASS_P (t)) |
c353b8e3 MM |
6645 | dependent_p = (type_dependent_expression_p (t) |
6646 | || value_dependent_expression_p (t)); | |
315fb5db NS |
6647 | else |
6648 | { | |
6649 | gcc_assert (t == error_mark_node); | |
6650 | dependent_p = false; | |
6651 | } | |
c8094d83 | 6652 | |
c353b8e3 MM |
6653 | processing_template_decl = saved_processing_template_decl; |
6654 | ||
6655 | return dependent_p; | |
050367a3 MM |
6656 | } |
6657 | ||
d43f603d KL |
6658 | /* Returns true if T depends on any template parameter with level LEVEL. */ |
6659 | ||
6660 | int | |
6661 | uses_template_parms_level (tree t, int level) | |
6662 | { | |
c095a4f8 DG |
6663 | return for_each_template_parm (t, template_parm_this_level_p, &level, NULL, |
6664 | /*include_nondeduced_p=*/true); | |
d43f603d KL |
6665 | } |
6666 | ||
27fafc8d | 6667 | static int tinst_depth; |
e9f32eb5 | 6668 | extern int max_tinst_depth; |
5566b478 | 6669 | #ifdef GATHER_STATISTICS |
27fafc8d | 6670 | int depth_reached; |
5566b478 | 6671 | #endif |
8dfaeb63 MM |
6672 | static int tinst_level_tick; |
6673 | static int last_template_error_tick; | |
8d08fdba | 6674 | |
3ae18eaf JM |
6675 | /* We're starting to instantiate D; record the template instantiation context |
6676 | for diagnostics and to restore it later. */ | |
6677 | ||
aa9d8196 | 6678 | static int |
3a978d72 | 6679 | push_tinst_level (tree d) |
8d08fdba | 6680 | { |
be93747e | 6681 | struct tinst_level *new_level; |
8d08fdba | 6682 | |
7215f9a0 MS |
6683 | if (tinst_depth >= max_tinst_depth) |
6684 | { | |
8adf5b5e JM |
6685 | /* If the instantiation in question still has unbound template parms, |
6686 | we don't really care if we can't instantiate it, so just return. | |
0cbd7506 | 6687 | This happens with base instantiation for implicit `typename'. */ |
8adf5b5e JM |
6688 | if (uses_template_parms (d)) |
6689 | return 0; | |
6690 | ||
1139b3d8 | 6691 | last_template_error_tick = tinst_level_tick; |
0f51ccfc | 6692 | error ("template instantiation depth exceeds maximum of %d (use " |
0cbd7506 | 6693 | "-ftemplate-depth-NN to increase the maximum) instantiating %qD", |
b4f4233d | 6694 | max_tinst_depth, d); |
5566b478 | 6695 | |
cb753e49 | 6696 | print_instantiation_context (); |
5566b478 | 6697 | |
7215f9a0 MS |
6698 | return 0; |
6699 | } | |
6700 | ||
be93747e KG |
6701 | new_level = GGC_NEW (struct tinst_level); |
6702 | new_level->decl = d; | |
6703 | new_level->locus = input_location; | |
6704 | new_level->in_system_header_p = in_system_header; | |
6705 | new_level->next = current_tinst_level; | |
6706 | current_tinst_level = new_level; | |
5566b478 | 6707 | |
7215f9a0 | 6708 | ++tinst_depth; |
5566b478 MS |
6709 | #ifdef GATHER_STATISTICS |
6710 | if (tinst_depth > depth_reached) | |
6711 | depth_reached = tinst_depth; | |
6712 | #endif | |
6713 | ||
27fafc8d | 6714 | ++tinst_level_tick; |
7215f9a0 | 6715 | return 1; |
8d08fdba MS |
6716 | } |
6717 | ||
3ae18eaf JM |
6718 | /* We're done instantiating this template; return to the instantiation |
6719 | context. */ | |
6720 | ||
aa9d8196 | 6721 | static void |
3a978d72 | 6722 | pop_tinst_level (void) |
8d08fdba | 6723 | { |
ae58fa02 MM |
6724 | /* Restore the filename and line number stashed away when we started |
6725 | this instantiation. */ | |
e2c3721c | 6726 | input_location = current_tinst_level->locus; |
e2c3721c | 6727 | current_tinst_level = current_tinst_level->next; |
7215f9a0 | 6728 | --tinst_depth; |
27fafc8d | 6729 | ++tinst_level_tick; |
8d08fdba MS |
6730 | } |
6731 | ||
3ae18eaf JM |
6732 | /* We're instantiating a deferred template; restore the template |
6733 | instantiation context in which the instantiation was requested, which | |
e2c3721c | 6734 | is one step out from LEVEL. Return the corresponding DECL or TYPE. */ |
3ae18eaf | 6735 | |
e2c3721c PB |
6736 | static tree |
6737 | reopen_tinst_level (struct tinst_level *level) | |
3ae18eaf | 6738 | { |
e2c3721c | 6739 | struct tinst_level *t; |
3ae18eaf JM |
6740 | |
6741 | tinst_depth = 0; | |
e2c3721c | 6742 | for (t = level; t; t = t->next) |
3ae18eaf JM |
6743 | ++tinst_depth; |
6744 | ||
6745 | current_tinst_level = level; | |
6746 | pop_tinst_level (); | |
e2c3721c | 6747 | return level->decl; |
3ae18eaf JM |
6748 | } |
6749 | ||
61172206 JM |
6750 | /* Returns the TINST_LEVEL which gives the original instantiation |
6751 | context. */ | |
6752 | ||
e2c3721c | 6753 | struct tinst_level * |
61172206 JM |
6754 | outermost_tinst_level (void) |
6755 | { | |
e2c3721c PB |
6756 | struct tinst_level *level = current_tinst_level; |
6757 | if (level) | |
6758 | while (level->next) | |
6759 | level = level->next; | |
6760 | return level; | |
61172206 JM |
6761 | } |
6762 | ||
172a4594 DS |
6763 | /* Returns TRUE if PARM is a parameter of the template TEMPL. */ |
6764 | ||
6765 | bool | |
6766 | parameter_of_template_p (tree parm, tree templ) | |
6767 | { | |
6768 | tree parms; | |
6769 | int i; | |
6770 | ||
6771 | if (!parm || !templ) | |
6772 | return false; | |
6773 | ||
6774 | gcc_assert (DECL_TEMPLATE_PARM_P (parm)); | |
6775 | gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL); | |
6776 | ||
6777 | parms = DECL_TEMPLATE_PARMS (templ); | |
6778 | parms = INNERMOST_TEMPLATE_PARMS (parms); | |
6779 | ||
6780 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) | |
6781 | if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i))) | |
6782 | return true; | |
6783 | ||
6784 | return false; | |
6785 | } | |
6786 | ||
f84b4be9 JM |
6787 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
6788 | vector of template arguments, as for tsubst. | |
6789 | ||
dc957d14 | 6790 | Returns an appropriate tsubst'd friend declaration. */ |
f84b4be9 JM |
6791 | |
6792 | static tree | |
3a978d72 | 6793 | tsubst_friend_function (tree decl, tree args) |
f84b4be9 JM |
6794 | { |
6795 | tree new_friend; | |
27fafc8d | 6796 | |
c8094d83 | 6797 | if (TREE_CODE (decl) == FUNCTION_DECL |
f84b4be9 JM |
6798 | && DECL_TEMPLATE_INSTANTIATION (decl) |
6799 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
6800 | /* This was a friend declared with an explicit template | |
6801 | argument list, e.g.: | |
c8094d83 | 6802 | |
f84b4be9 | 6803 | friend void f<>(T); |
c8094d83 | 6804 | |
f84b4be9 JM |
6805 | to indicate that f was a template instantiation, not a new |
6806 | function declaration. Now, we have to figure out what | |
6807 | instantiation of what template. */ | |
6808 | { | |
76e57b45 | 6809 | tree template_id, arglist, fns; |
f84b4be9 JM |
6810 | tree new_args; |
6811 | tree tmpl; | |
ed2fa432 | 6812 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); |
c8094d83 | 6813 | |
76e57b45 | 6814 | /* Friend functions are looked up in the containing namespace scope. |
0cbd7506 | 6815 | We must enter that scope, to avoid finding member functions of the |
39a13be5 | 6816 | current class with same name. */ |
76e57b45 NS |
6817 | push_nested_namespace (ns); |
6818 | fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, | |
015c2c66 MM |
6819 | tf_warning_or_error, NULL_TREE, |
6820 | /*integral_constant_expression_p=*/false); | |
76e57b45 NS |
6821 | pop_nested_namespace (ns); |
6822 | arglist = tsubst (DECL_TI_ARGS (decl), args, | |
23fca1f5 | 6823 | tf_warning_or_error, NULL_TREE); |
76e57b45 | 6824 | template_id = lookup_template_function (fns, arglist); |
c8094d83 | 6825 | |
23fca1f5 | 6826 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
36a117a5 | 6827 | tmpl = determine_specialization (template_id, new_friend, |
c8094d83 | 6828 | &new_args, |
5fe7b654 | 6829 | /*need_member_template=*/0, |
29a1da1c MM |
6830 | TREE_VEC_LENGTH (args), |
6831 | tsk_none); | |
6e432b31 | 6832 | return instantiate_template (tmpl, new_args, tf_error); |
f84b4be9 | 6833 | } |
36a117a5 | 6834 | |
23fca1f5 | 6835 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
c8094d83 | 6836 | |
36a117a5 | 6837 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
6838 | compiler, but is not an instantiation from the point of view of |
6839 | the language. For example, we might have had: | |
c8094d83 | 6840 | |
f84b4be9 JM |
6841 | template <class T> struct S { |
6842 | template <class U> friend void f(T, U); | |
6843 | }; | |
c8094d83 | 6844 | |
f84b4be9 JM |
6845 | Then, in S<int>, template <class U> void f(int, U) is not an |
6846 | instantiation of anything. */ | |
ac2b3222 AP |
6847 | if (new_friend == error_mark_node) |
6848 | return error_mark_node; | |
c8094d83 | 6849 | |
f84b4be9 JM |
6850 | DECL_USE_TEMPLATE (new_friend) = 0; |
6851 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
655dc6ee JM |
6852 | { |
6853 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
6854 | DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) | |
6855 | = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); | |
6856 | } | |
36a117a5 | 6857 | |
92643fea MM |
6858 | /* The mangled name for the NEW_FRIEND is incorrect. The function |
6859 | is not a template instantiation and should not be mangled like | |
6860 | one. Therefore, we forget the mangling here; we'll recompute it | |
6861 | later if we need it. */ | |
36a117a5 MM |
6862 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) |
6863 | { | |
19e7881c | 6864 | SET_DECL_RTL (new_friend, NULL_RTX); |
92643fea | 6865 | SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); |
36a117a5 | 6866 | } |
c8094d83 | 6867 | |
6eb3bb27 | 6868 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 6869 | { |
36a117a5 | 6870 | tree old_decl; |
fbf1c34b MM |
6871 | tree new_friend_template_info; |
6872 | tree new_friend_result_template_info; | |
92da7074 | 6873 | tree ns; |
fbf1c34b MM |
6874 | int new_friend_is_defn; |
6875 | ||
6876 | /* We must save some information from NEW_FRIEND before calling | |
6877 | duplicate decls since that function will free NEW_FRIEND if | |
6878 | possible. */ | |
6879 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
5bd61841 | 6880 | new_friend_is_defn = |
c8094d83 | 6881 | (DECL_INITIAL (DECL_TEMPLATE_RESULT |
5bd61841 MM |
6882 | (template_for_substitution (new_friend))) |
6883 | != NULL_TREE); | |
f84b4be9 | 6884 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
6885 | { |
6886 | /* This declaration is a `primary' template. */ | |
6887 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
c8094d83 | 6888 | |
fbf1c34b | 6889 | new_friend_result_template_info |
17aec3eb | 6890 | = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); |
fbf1c34b MM |
6891 | } |
6892 | else | |
5bd61841 | 6893 | new_friend_result_template_info = NULL_TREE; |
36a117a5 | 6894 | |
b01e6d2b JM |
6895 | /* Make the init_value nonzero so pushdecl knows this is a defn. */ |
6896 | if (new_friend_is_defn) | |
6897 | DECL_INITIAL (new_friend) = error_mark_node; | |
6898 | ||
1c227897 MM |
6899 | /* Inside pushdecl_namespace_level, we will push into the |
6900 | current namespace. However, the friend function should go | |
c6002625 | 6901 | into the namespace of the template. */ |
92da7074 ML |
6902 | ns = decl_namespace_context (new_friend); |
6903 | push_nested_namespace (ns); | |
d63d5d0c | 6904 | old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true); |
92da7074 | 6905 | pop_nested_namespace (ns); |
36a117a5 | 6906 | |
0014c247 VR |
6907 | if (old_decl == error_mark_node) |
6908 | return error_mark_node; | |
6909 | ||
36a117a5 MM |
6910 | if (old_decl != new_friend) |
6911 | { | |
6912 | /* This new friend declaration matched an existing | |
6913 | declaration. For example, given: | |
6914 | ||
6915 | template <class T> void f(T); | |
c8094d83 MS |
6916 | template <class U> class C { |
6917 | template <class T> friend void f(T) {} | |
36a117a5 MM |
6918 | }; |
6919 | ||
6920 | the friend declaration actually provides the definition | |
6921 | of `f', once C has been instantiated for some type. So, | |
6922 | old_decl will be the out-of-class template declaration, | |
6923 | while new_friend is the in-class definition. | |
6924 | ||
6925 | But, if `f' was called before this point, the | |
6926 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
6927 | to `T' but not to `U', references to which might appear | |
6928 | in the definition of `f'. Previously, the most general | |
6929 | template for an instantiation of `f' was the out-of-class | |
6930 | version; now it is the in-class version. Therefore, we | |
6931 | run through all specialization of `f', adding to their | |
6932 | DECL_TI_ARGS appropriately. In particular, they need a | |
6933 | new set of outer arguments, corresponding to the | |
c8094d83 | 6934 | arguments for this class instantiation. |
36a117a5 MM |
6935 | |
6936 | The same situation can arise with something like this: | |
6937 | ||
6938 | friend void f(int); | |
c8094d83 | 6939 | template <class T> class C { |
0cbd7506 MS |
6940 | friend void f(T) {} |
6941 | }; | |
36a117a5 MM |
6942 | |
6943 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
6944 | in the class. */ | |
6945 | ||
fbf1c34b MM |
6946 | if (!new_friend_is_defn) |
6947 | /* On the other hand, if the in-class declaration does | |
6948 | *not* provide a definition, then we don't want to alter | |
6949 | existing definitions. We can just leave everything | |
6950 | alone. */ | |
36a117a5 | 6951 | ; |
fbf1c34b | 6952 | else |
36a117a5 | 6953 | { |
2b110bfc JM |
6954 | tree new_template = TI_TEMPLATE (new_friend_template_info); |
6955 | tree new_args = TI_ARGS (new_friend_template_info); | |
6956 | ||
fbf1c34b MM |
6957 | /* Overwrite whatever template info was there before, if |
6958 | any, with the new template information pertaining to | |
6959 | the declaration. */ | |
6960 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
6961 | ||
6962 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
2b110bfc JM |
6963 | /* We should have called reregister_specialization in |
6964 | duplicate_decls. */ | |
6965 | gcc_assert (retrieve_specialization (new_template, | |
6966 | new_args, 0) | |
6967 | == old_decl); | |
c8094d83 | 6968 | else |
36a117a5 | 6969 | { |
fbf1c34b | 6970 | tree t; |
fbf1c34b | 6971 | |
2b110bfc JM |
6972 | /* Indicate that the old function template is a partial |
6973 | instantiation. */ | |
c8094d83 | 6974 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) |
fbf1c34b | 6975 | = new_friend_result_template_info; |
c8094d83 | 6976 | |
2b110bfc JM |
6977 | gcc_assert (new_template |
6978 | == most_general_template (new_template)); | |
6979 | gcc_assert (new_template != old_decl); | |
6980 | ||
6981 | /* Reassign any specializations already in the hash table | |
6982 | to the new more general template, and add the | |
6983 | additional template args. */ | |
6984 | for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl); | |
fbf1c34b MM |
6985 | t != NULL_TREE; |
6986 | t = TREE_CHAIN (t)) | |
6987 | { | |
6988 | tree spec = TREE_VALUE (t); | |
2b110bfc JM |
6989 | spec_entry elt; |
6990 | ||
6991 | elt.tmpl = old_decl; | |
6992 | elt.args = DECL_TI_ARGS (spec); | |
6993 | elt.spec = NULL_TREE; | |
6994 | ||
6995 | htab_remove_elt (decl_specializations, &elt); | |
c8094d83 MS |
6996 | |
6997 | DECL_TI_ARGS (spec) | |
2b110bfc | 6998 | = add_outermost_template_args (new_args, |
fbf1c34b | 6999 | DECL_TI_ARGS (spec)); |
fbf1c34b | 7000 | |
2b110bfc JM |
7001 | register_specialization |
7002 | (spec, new_template, DECL_TI_ARGS (spec), true, 0); | |
7003 | ||
fbf1c34b | 7004 | } |
2b110bfc | 7005 | DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE; |
36a117a5 MM |
7006 | } |
7007 | } | |
7008 | ||
7009 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
7010 | by duplicate_decls. */ | |
7011 | new_friend = old_decl; | |
7012 | } | |
f84b4be9 | 7013 | } |
6e432b31 | 7014 | else |
f84b4be9 | 7015 | { |
6e432b31 KL |
7016 | tree context = DECL_CONTEXT (new_friend); |
7017 | bool dependent_p; | |
7018 | ||
7019 | /* In the code | |
7020 | template <class T> class C { | |
7021 | template <class U> friend void C1<U>::f (); // case 1 | |
7022 | friend void C2<T>::f (); // case 2 | |
7023 | }; | |
7024 | we only need to make sure CONTEXT is a complete type for | |
7025 | case 2. To distinguish between the two cases, we note that | |
7026 | CONTEXT of case 1 remains dependent type after tsubst while | |
7027 | this isn't true for case 2. */ | |
7028 | ++processing_template_decl; | |
7029 | dependent_p = dependent_type_p (context); | |
7030 | --processing_template_decl; | |
7031 | ||
7032 | if (!dependent_p | |
7033 | && !complete_type_or_else (context, NULL_TREE)) | |
7034 | return error_mark_node; | |
7035 | ||
7036 | if (COMPLETE_TYPE_P (context)) | |
7037 | { | |
7038 | /* Check to see that the declaration is really present, and, | |
7039 | possibly obtain an improved declaration. */ | |
7040 | tree fn = check_classfn (context, | |
7041 | new_friend, NULL_TREE); | |
7042 | ||
684939ce | 7043 | if (fn) |
6e432b31 KL |
7044 | new_friend = fn; |
7045 | } | |
f84b4be9 JM |
7046 | } |
7047 | ||
7048 | return new_friend; | |
7049 | } | |
7050 | ||
1aed5355 MM |
7051 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
7052 | template arguments, as for tsubst. | |
6757edfe | 7053 | |
dc957d14 | 7054 | Returns an appropriate tsubst'd friend type or error_mark_node on |
4b054b80 | 7055 | failure. */ |
6757edfe MM |
7056 | |
7057 | static tree | |
3a978d72 | 7058 | tsubst_friend_class (tree friend_tmpl, tree args) |
6757edfe | 7059 | { |
1aed5355 | 7060 | tree friend_type; |
25aab5d0 | 7061 | tree tmpl; |
3e0ec82f | 7062 | tree context; |
6757edfe | 7063 | |
3e0ec82f MM |
7064 | context = DECL_CONTEXT (friend_tmpl); |
7065 | ||
7066 | if (context) | |
77adef84 | 7067 | { |
3e0ec82f MM |
7068 | if (TREE_CODE (context) == NAMESPACE_DECL) |
7069 | push_nested_namespace (context); | |
7070 | else | |
c8094d83 | 7071 | push_nested_class (tsubst (context, args, tf_none, NULL_TREE)); |
3e0ec82f | 7072 | } |
25aab5d0 | 7073 | |
105d72c5 MM |
7074 | /* Look for a class template declaration. We look for hidden names |
7075 | because two friend declarations of the same template are the | |
7076 | same. For example, in: | |
7077 | ||
7078 | struct A { | |
7079 | template <typename> friend class F; | |
7080 | }; | |
7081 | template <typename> struct B { | |
7082 | template <typename> friend class F; | |
7083 | }; | |
7084 | ||
7085 | both F templates are the same. */ | |
7086 | tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0, | |
7087 | /*block_p=*/true, 0, | |
7088 | LOOKUP_COMPLAIN | LOOKUP_HIDDEN); | |
25aab5d0 | 7089 | |
3e0ec82f MM |
7090 | /* But, if we don't find one, it might be because we're in a |
7091 | situation like this: | |
77adef84 | 7092 | |
3e0ec82f MM |
7093 | template <class T> |
7094 | struct S { | |
7095 | template <class U> | |
7096 | friend struct S; | |
7097 | }; | |
7098 | ||
7099 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
7100 | for `S<int>', not the TEMPLATE_DECL. */ | |
7101 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
7102 | { | |
10e6657a | 7103 | tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1); |
3e0ec82f | 7104 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); |
25aab5d0 | 7105 | } |
6757edfe | 7106 | |
25aab5d0 | 7107 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
6757edfe MM |
7108 | { |
7109 | /* The friend template has already been declared. Just | |
36a117a5 MM |
7110 | check to see that the declarations match, and install any new |
7111 | default parameters. We must tsubst the default parameters, | |
7112 | of course. We only need the innermost template parameters | |
7113 | because that is all that redeclare_class_template will look | |
7114 | at. */ | |
3e0ec82f MM |
7115 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) |
7116 | > TMPL_ARGS_DEPTH (args)) | |
7117 | { | |
7118 | tree parms; | |
b131ad7c | 7119 | location_t saved_input_location; |
3e0ec82f | 7120 | parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), |
23fca1f5 | 7121 | args, tf_warning_or_error); |
b131ad7c MLI |
7122 | |
7123 | saved_input_location = input_location; | |
7124 | input_location = DECL_SOURCE_LOCATION (friend_tmpl); | |
3e0ec82f | 7125 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
b131ad7c MLI |
7126 | input_location = saved_input_location; |
7127 | ||
3e0ec82f MM |
7128 | } |
7129 | ||
6757edfe MM |
7130 | friend_type = TREE_TYPE (tmpl); |
7131 | } | |
7132 | else | |
7133 | { | |
7134 | /* The friend template has not already been declared. In this | |
7135 | case, the instantiation of the template class will cause the | |
7136 | injection of this template into the global scope. */ | |
23fca1f5 | 7137 | tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE); |
dca56f77 VR |
7138 | if (tmpl == error_mark_node) |
7139 | return error_mark_node; | |
6757edfe MM |
7140 | |
7141 | /* The new TMPL is not an instantiation of anything, so we | |
0cbd7506 | 7142 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for |
6757edfe MM |
7143 | the new type because that is supposed to be the corresponding |
7144 | template decl, i.e., TMPL. */ | |
7145 | DECL_USE_TEMPLATE (tmpl) = 0; | |
7146 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
7147 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
68c78847 KL |
7148 | CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) |
7149 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); | |
6757edfe MM |
7150 | |
7151 | /* Inject this template into the global scope. */ | |
d63d5d0c | 7152 | friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true)); |
6757edfe MM |
7153 | } |
7154 | ||
c8094d83 | 7155 | if (context) |
3e0ec82f MM |
7156 | { |
7157 | if (TREE_CODE (context) == NAMESPACE_DECL) | |
7158 | pop_nested_namespace (context); | |
7159 | else | |
7160 | pop_nested_class (); | |
7161 | } | |
7162 | ||
6757edfe MM |
7163 | return friend_type; |
7164 | } | |
f84b4be9 | 7165 | |
17f29637 KL |
7166 | /* Returns zero if TYPE cannot be completed later due to circularity. |
7167 | Otherwise returns one. */ | |
7168 | ||
d5372501 | 7169 | static int |
3a978d72 | 7170 | can_complete_type_without_circularity (tree type) |
17f29637 KL |
7171 | { |
7172 | if (type == NULL_TREE || type == error_mark_node) | |
7173 | return 0; | |
7174 | else if (COMPLETE_TYPE_P (type)) | |
7175 | return 1; | |
7176 | else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) | |
7177 | return can_complete_type_without_circularity (TREE_TYPE (type)); | |
8c6ab2db NS |
7178 | else if (CLASS_TYPE_P (type) |
7179 | && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) | |
17f29637 KL |
7180 | return 0; |
7181 | else | |
7182 | return 1; | |
7183 | } | |
7184 | ||
4f4141ff JM |
7185 | /* Apply any attributes which had to be deferred until instantiation |
7186 | time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes; | |
7187 | ARGS, COMPLAIN, IN_DECL are as tsubst. */ | |
7188 | ||
7189 | static void | |
7190 | apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags, | |
7191 | tree args, tsubst_flags_t complain, tree in_decl) | |
7192 | { | |
5818c8e4 | 7193 | tree last_dep = NULL_TREE; |
4f4141ff | 7194 | tree t; |
5818c8e4 JM |
7195 | tree *p; |
7196 | ||
7197 | for (t = attributes; t; t = TREE_CHAIN (t)) | |
7198 | if (ATTR_IS_DEPENDENT (t)) | |
7199 | { | |
7200 | last_dep = t; | |
7201 | attributes = copy_list (attributes); | |
7202 | break; | |
7203 | } | |
4f4141ff JM |
7204 | |
7205 | if (DECL_P (*decl_p)) | |
823e5f7f JJ |
7206 | { |
7207 | if (TREE_TYPE (*decl_p) == error_mark_node) | |
7208 | return; | |
7209 | p = &DECL_ATTRIBUTES (*decl_p); | |
7210 | } | |
4f4141ff | 7211 | else |
5818c8e4 | 7212 | p = &TYPE_ATTRIBUTES (*decl_p); |
4f4141ff | 7213 | |
5818c8e4 JM |
7214 | if (last_dep) |
7215 | { | |
7216 | tree late_attrs = NULL_TREE; | |
7217 | tree *q = &late_attrs; | |
7218 | ||
7219 | for (*p = attributes; *p; ) | |
7220 | { | |
7221 | t = *p; | |
7222 | if (ATTR_IS_DEPENDENT (t)) | |
7223 | { | |
7224 | *p = TREE_CHAIN (t); | |
7225 | TREE_CHAIN (t) = NULL_TREE; | |
ff2f1c5f JJ |
7226 | /* If the first attribute argument is an identifier, don't |
7227 | pass it through tsubst. Attributes like mode, format, | |
7228 | cleanup and several target specific attributes expect it | |
7229 | unmodified. */ | |
7230 | if (TREE_VALUE (t) | |
7231 | && TREE_CODE (TREE_VALUE (t)) == TREE_LIST | |
7232 | && TREE_VALUE (TREE_VALUE (t)) | |
7233 | && (TREE_CODE (TREE_VALUE (TREE_VALUE (t))) | |
7234 | == IDENTIFIER_NODE)) | |
7235 | { | |
7236 | tree chain | |
7237 | = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain, | |
7238 | in_decl, | |
7239 | /*integral_constant_expression_p=*/false); | |
7240 | if (chain != TREE_CHAIN (TREE_VALUE (t))) | |
7241 | TREE_VALUE (t) | |
7242 | = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)), | |
7243 | chain); | |
7244 | } | |
7245 | else | |
7246 | TREE_VALUE (t) | |
7247 | = tsubst_expr (TREE_VALUE (t), args, complain, in_decl, | |
7248 | /*integral_constant_expression_p=*/false); | |
5818c8e4 JM |
7249 | *q = t; |
7250 | q = &TREE_CHAIN (t); | |
7251 | } | |
7252 | else | |
7253 | p = &TREE_CHAIN (t); | |
7254 | } | |
4f4141ff | 7255 | |
5818c8e4 JM |
7256 | cplus_decl_attributes (decl_p, late_attrs, attr_flags); |
7257 | } | |
4f4141ff JM |
7258 | } |
7259 | ||
d0940d56 DS |
7260 | /* Perform (or defer) access check for typedefs that were referenced |
7261 | from within the template TMPL code. | |
7262 | This is a subroutine of instantiate_template and instantiate_class_template. | |
7263 | TMPL is the template to consider and TARGS is the list of arguments of | |
7264 | that template. */ | |
7265 | ||
7266 | static void | |
7267 | perform_typedefs_access_check (tree tmpl, tree targs) | |
7268 | { | |
7269 | tree t; | |
7270 | ||
2eb25c98 | 7271 | if (!tmpl |
9dbed529 | 7272 | || (!CLASS_TYPE_P (tmpl) |
2eb25c98 | 7273 | && TREE_CODE (tmpl) != FUNCTION_DECL)) |
d0940d56 DS |
7274 | return; |
7275 | ||
2eb25c98 | 7276 | for (t = get_types_needing_access_check (tmpl); t; t = TREE_CHAIN (t)) |
d0940d56 DS |
7277 | { |
7278 | tree type_decl = TREE_PURPOSE (t); | |
7279 | tree type_scope = TREE_VALUE (t); | |
7280 | ||
7281 | if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope)) | |
7282 | continue; | |
7283 | ||
7284 | if (uses_template_parms (type_decl)) | |
7285 | type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE); | |
7286 | if (uses_template_parms (type_scope)) | |
7287 | type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE); | |
7288 | ||
2eb25c98 DS |
7289 | perform_or_defer_access_check (TYPE_BINFO (type_scope), |
7290 | type_decl, type_decl); | |
d0940d56 DS |
7291 | } |
7292 | } | |
7293 | ||
8d08fdba | 7294 | tree |
3a978d72 | 7295 | instantiate_class_template (tree type) |
8d08fdba | 7296 | { |
be93747e | 7297 | tree templ, args, pattern, t, member; |
36a117a5 | 7298 | tree typedecl; |
dbbf88d1 | 7299 | tree pbinfo; |
cad7e87b | 7300 | tree base_list; |
c8094d83 | 7301 | |
5566b478 | 7302 | if (type == error_mark_node) |
8d08fdba MS |
7303 | return error_mark_node; |
7304 | ||
c8094d83 | 7305 | if (TYPE_BEING_DEFINED (type) |
ca099ac8 MM |
7306 | || COMPLETE_TYPE_P (type) |
7307 | || dependent_type_p (type)) | |
5566b478 MS |
7308 | return type; |
7309 | ||
6bdb985f | 7310 | /* Figure out which template is being instantiated. */ |
be93747e KG |
7311 | templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
7312 | gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL); | |
73aad9b9 | 7313 | |
4c571114 MM |
7314 | /* Determine what specialization of the original template to |
7315 | instantiate. */ | |
be93747e | 7316 | t = most_specialized_class (type, templ); |
8fbc5ae7 | 7317 | if (t == error_mark_node) |
73aad9b9 | 7318 | { |
8fbc5ae7 MM |
7319 | TYPE_BEING_DEFINED (type) = 1; |
7320 | return error_mark_node; | |
73aad9b9 | 7321 | } |
916b63c3 MM |
7322 | else if (t) |
7323 | { | |
7324 | /* This TYPE is actually an instantiation of a partial | |
7325 | specialization. We replace the innermost set of ARGS with | |
7326 | the arguments appropriate for substitution. For example, | |
7327 | given: | |
3db45ab5 MS |
7328 | |
7329 | template <class T> struct S {}; | |
916b63c3 | 7330 | template <class T> struct S<T*> {}; |
6bdb985f | 7331 | |
916b63c3 | 7332 | and supposing that we are instantiating S<int*>, ARGS will |
3db45ab5 | 7333 | presently be {int*} -- but we need {int}. */ |
916b63c3 MM |
7334 | pattern = TREE_TYPE (t); |
7335 | args = TREE_PURPOSE (t); | |
7336 | } | |
73aad9b9 | 7337 | else |
916b63c3 | 7338 | { |
be93747e | 7339 | pattern = TREE_TYPE (templ); |
916b63c3 MM |
7340 | args = CLASSTYPE_TI_ARGS (type); |
7341 | } | |
5566b478 | 7342 | |
4c571114 MM |
7343 | /* If the template we're instantiating is incomplete, then clearly |
7344 | there's nothing we can do. */ | |
d0f062fb | 7345 | if (!COMPLETE_TYPE_P (pattern)) |
f31c0a32 | 7346 | return type; |
5566b478 | 7347 | |
4c571114 MM |
7348 | /* If we've recursively instantiated too many templates, stop. */ |
7349 | if (! push_tinst_level (type)) | |
f31c0a32 | 7350 | return type; |
4c571114 MM |
7351 | |
7352 | /* Now we're really doing the instantiation. Mark the type as in | |
7353 | the process of being defined. */ | |
7354 | TYPE_BEING_DEFINED (type) = 1; | |
7355 | ||
78757caa KL |
7356 | /* We may be in the middle of deferred access check. Disable |
7357 | it now. */ | |
7358 | push_deferring_access_checks (dk_no_deferred); | |
7359 | ||
c353b8e3 | 7360 | push_to_top_level (); |
4c571114 | 7361 | |
7813d14c | 7362 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
8d08fdba | 7363 | |
2eb25c98 DS |
7364 | /* Set the input location to the most specialized template definition. |
7365 | This is needed if tsubsting causes an error. */ | |
7366 | typedecl = TYPE_MAIN_DECL (pattern); | |
12af7ba3 | 7367 | input_location = DECL_SOURCE_LOCATION (typedecl); |
68ea098a | 7368 | |
0fcedd9c | 7369 | TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern); |
834c6dff MM |
7370 | TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); |
7371 | TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); | |
f7da6097 | 7372 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); |
f7da6097 MS |
7373 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); |
7374 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
f7da6097 MS |
7375 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); |
7376 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
7377 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
7378 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
f7da6097 MS |
7379 | TYPE_PACKED (type) = TYPE_PACKED (pattern); |
7380 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
11cf4d18 | 7381 | TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); |
eff71ab0 | 7382 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
6bdb8141 JM |
7383 | if (ANON_AGGR_TYPE_P (pattern)) |
7384 | SET_ANON_AGGR_TYPE_P (type); | |
8d039470 MS |
7385 | if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern)) |
7386 | { | |
7387 | CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1; | |
7388 | CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern); | |
7389 | } | |
f7da6097 | 7390 | |
dbbf88d1 | 7391 | pbinfo = TYPE_BINFO (pattern); |
1456deaf | 7392 | |
315fb5db NS |
7393 | /* We should never instantiate a nested class before its enclosing |
7394 | class; we need to look up the nested class by name before we can | |
7395 | instantiate it, and that lookup should instantiate the enclosing | |
7396 | class. */ | |
7397 | gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) | |
7398 | || COMPLETE_TYPE_P (TYPE_CONTEXT (type)) | |
7399 | || TYPE_BEING_DEFINED (TYPE_CONTEXT (type))); | |
1456deaf | 7400 | |
cad7e87b | 7401 | base_list = NULL_TREE; |
fa743e8c | 7402 | if (BINFO_N_BASE_BINFOS (pbinfo)) |
3fd71a52 | 7403 | { |
fa743e8c | 7404 | tree pbase_binfo; |
a2507277 | 7405 | tree context = TYPE_CONTEXT (type); |
4514aa8c | 7406 | tree pushed_scope; |
3fd71a52 | 7407 | int i; |
5566b478 | 7408 | |
a2507277 NS |
7409 | /* We must enter the scope containing the type, as that is where |
7410 | the accessibility of types named in dependent bases are | |
7411 | looked up from. */ | |
4514aa8c | 7412 | pushed_scope = push_scope (context ? context : global_namespace); |
c8094d83 | 7413 | |
3fd71a52 MM |
7414 | /* Substitute into each of the bases to determine the actual |
7415 | basetypes. */ | |
fa743e8c | 7416 | for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) |
3fd71a52 MM |
7417 | { |
7418 | tree base; | |
fa743e8c | 7419 | tree access = BINFO_BASE_ACCESS (pbinfo, i); |
5d80a306 DG |
7420 | tree expanded_bases = NULL_TREE; |
7421 | int idx, len = 1; | |
7422 | ||
7423 | if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo))) | |
7424 | { | |
7425 | expanded_bases = | |
7426 | tsubst_pack_expansion (BINFO_TYPE (pbase_binfo), | |
7427 | args, tf_error, NULL_TREE); | |
7428 | if (expanded_bases == error_mark_node) | |
7429 | continue; | |
7430 | ||
7431 | len = TREE_VEC_LENGTH (expanded_bases); | |
7432 | } | |
7433 | ||
7434 | for (idx = 0; idx < len; idx++) | |
7435 | { | |
7436 | if (expanded_bases) | |
7437 | /* Extract the already-expanded base class. */ | |
7438 | base = TREE_VEC_ELT (expanded_bases, idx); | |
7439 | else | |
7440 | /* Substitute to figure out the base class. */ | |
7441 | base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, | |
7442 | NULL_TREE); | |
7443 | ||
7444 | if (base == error_mark_node) | |
7445 | continue; | |
7446 | ||
7447 | base_list = tree_cons (access, base, base_list); | |
7448 | if (BINFO_VIRTUAL_P (pbase_binfo)) | |
7449 | TREE_TYPE (base_list) = integer_type_node; | |
7450 | } | |
3fd71a52 | 7451 | } |
dfbcd65a | 7452 | |
3fd71a52 MM |
7453 | /* The list is now in reverse order; correct that. */ |
7454 | base_list = nreverse (base_list); | |
7455 | ||
4514aa8c NS |
7456 | if (pushed_scope) |
7457 | pop_scope (pushed_scope); | |
3fd71a52 | 7458 | } |
cad7e87b NS |
7459 | /* Now call xref_basetypes to set up all the base-class |
7460 | information. */ | |
7461 | xref_basetypes (type, base_list); | |
7462 | ||
4f4141ff JM |
7463 | apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern), |
7464 | (int) ATTR_FLAG_TYPE_IN_PLACE, | |
7465 | args, tf_error, NULL_TREE); | |
5566b478 | 7466 | |
b74a0560 MM |
7467 | /* Now that our base classes are set up, enter the scope of the |
7468 | class, so that name lookups into base classes, etc. will work | |
dc957d14 | 7469 | correctly. This is precisely analogous to what we do in |
b74a0560 | 7470 | begin_class_definition when defining an ordinary non-template |
56d0c6e3 JM |
7471 | class, except we also need to push the enclosing classes. */ |
7472 | push_nested_class (type); | |
b74a0560 | 7473 | |
7088fca9 | 7474 | /* Now members are processed in the order of declaration. */ |
8c6ab2db NS |
7475 | for (member = CLASSTYPE_DECL_LIST (pattern); |
7476 | member; member = TREE_CHAIN (member)) | |
8d08fdba | 7477 | { |
7088fca9 | 7478 | tree t = TREE_VALUE (member); |
5566b478 | 7479 | |
7088fca9 | 7480 | if (TREE_PURPOSE (member)) |
ed44da02 | 7481 | { |
7088fca9 KL |
7482 | if (TYPE_P (t)) |
7483 | { | |
5e0c54e5 | 7484 | /* Build new CLASSTYPE_NESTED_UTDS. */ |
8d08fdba | 7485 | |
7088fca9 | 7486 | tree newtag; |
883a2bff MM |
7487 | bool class_template_p; |
7488 | ||
2678bae8 VR |
7489 | class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE |
7490 | && TYPE_LANG_SPECIFIC (t) | |
7491 | && CLASSTYPE_IS_TEMPLATE (t)); | |
883a2bff | 7492 | /* If the member is a class template, then -- even after |
6c745393 | 7493 | substitution -- there may be dependent types in the |
883a2bff MM |
7494 | template argument list for the class. We increment |
7495 | PROCESSING_TEMPLATE_DECL so that dependent_type_p, as | |
7496 | that function will assume that no types are dependent | |
7497 | when outside of a template. */ | |
7498 | if (class_template_p) | |
7499 | ++processing_template_decl; | |
2678bae8 | 7500 | newtag = tsubst (t, args, tf_error, NULL_TREE); |
883a2bff MM |
7501 | if (class_template_p) |
7502 | --processing_template_decl; | |
2620d095 KL |
7503 | if (newtag == error_mark_node) |
7504 | continue; | |
7505 | ||
7088fca9 KL |
7506 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
7507 | { | |
2678bae8 VR |
7508 | tree name = TYPE_IDENTIFIER (t); |
7509 | ||
883a2bff | 7510 | if (class_template_p) |
7088fca9 KL |
7511 | /* Unfortunately, lookup_template_class sets |
7512 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
3afd2e20 NS |
7513 | instantiation (i.e., for the type of a member |
7514 | template class nested within a template class.) | |
7515 | This behavior is required for | |
7516 | maybe_process_partial_specialization to work | |
7517 | correctly, but is not accurate in this case; | |
7518 | the TAG is not an instantiation of anything. | |
7519 | (The corresponding TEMPLATE_DECL is an | |
7520 | instantiation, but the TYPE is not.) */ | |
7088fca9 KL |
7521 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; |
7522 | ||
7523 | /* Now, we call pushtag to put this NEWTAG into the scope of | |
7524 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
7525 | pushtag calling push_template_decl. We don't have to do | |
7526 | this for enums because it will already have been done in | |
7527 | tsubst_enum. */ | |
7528 | if (name) | |
7529 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
bd3d082e | 7530 | pushtag (name, newtag, /*tag_scope=*/ts_current); |
7088fca9 KL |
7531 | } |
7532 | } | |
c8094d83 | 7533 | else if (TREE_CODE (t) == FUNCTION_DECL |
7088fca9 KL |
7534 | || DECL_FUNCTION_TEMPLATE_P (t)) |
7535 | { | |
7536 | /* Build new TYPE_METHODS. */ | |
fecafe5e | 7537 | tree r; |
c8094d83 | 7538 | |
fecafe5e | 7539 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 7540 | ++processing_template_decl; |
fecafe5e NS |
7541 | r = tsubst (t, args, tf_error, NULL_TREE); |
7542 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
9579624e | 7543 | --processing_template_decl; |
7088fca9 | 7544 | set_current_access_from_decl (r); |
7088fca9 KL |
7545 | finish_member_declaration (r); |
7546 | } | |
7547 | else | |
7548 | { | |
7549 | /* Build new TYPE_FIELDS. */ | |
55a3debe DG |
7550 | if (TREE_CODE (t) == STATIC_ASSERT) |
7551 | { | |
7552 | tree condition = | |
7553 | tsubst_expr (STATIC_ASSERT_CONDITION (t), args, | |
7554 | tf_warning_or_error, NULL_TREE, | |
7555 | /*integral_constant_expression_p=*/true); | |
7556 | finish_static_assert (condition, | |
7557 | STATIC_ASSERT_MESSAGE (t), | |
7558 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
7559 | /*member_p=*/true); | |
7560 | } | |
7561 | else if (TREE_CODE (t) != CONST_DECL) | |
7088fca9 KL |
7562 | { |
7563 | tree r; | |
fa8d6e85 | 7564 | |
39a13be5 | 7565 | /* The file and line for this declaration, to |
d479d37f NS |
7566 | assist in error message reporting. Since we |
7567 | called push_tinst_level above, we don't need to | |
7568 | restore these. */ | |
f31686a3 | 7569 | input_location = DECL_SOURCE_LOCATION (t); |
fa8d6e85 | 7570 | |
fb5ce3c9 | 7571 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 7572 | ++processing_template_decl; |
23fca1f5 | 7573 | r = tsubst (t, args, tf_warning_or_error, NULL_TREE); |
fb5ce3c9 | 7574 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 7575 | --processing_template_decl; |
7088fca9 KL |
7576 | if (TREE_CODE (r) == VAR_DECL) |
7577 | { | |
b794e321 MM |
7578 | /* In [temp.inst]: |
7579 | ||
7580 | [t]he initialization (and any associated | |
7581 | side-effects) of a static data member does | |
7582 | not occur unless the static data member is | |
7583 | itself used in a way that requires the | |
7584 | definition of the static data member to | |
3db45ab5 | 7585 | exist. |
b794e321 MM |
7586 | |
7587 | Therefore, we do not substitute into the | |
3db45ab5 MS |
7588 | initialized for the static data member here. */ |
7589 | finish_static_data_member_decl | |
7590 | (r, | |
7591 | /*init=*/NULL_TREE, | |
d174af6c | 7592 | /*init_const_expr_p=*/false, |
3db45ab5 | 7593 | /*asmspec_tree=*/NULL_TREE, |
b794e321 | 7594 | /*flags=*/0); |
7088fca9 KL |
7595 | if (DECL_INITIALIZED_IN_CLASS_P (r)) |
7596 | check_static_variable_definition (r, TREE_TYPE (r)); | |
7597 | } | |
7598 | else if (TREE_CODE (r) == FIELD_DECL) | |
7599 | { | |
7600 | /* Determine whether R has a valid type and can be | |
7601 | completed later. If R is invalid, then it is | |
7602 | replaced by error_mark_node so that it will not be | |
7603 | added to TYPE_FIELDS. */ | |
7604 | tree rtype = TREE_TYPE (r); | |
7605 | if (can_complete_type_without_circularity (rtype)) | |
7606 | complete_type (rtype); | |
7607 | ||
7608 | if (!COMPLETE_TYPE_P (rtype)) | |
7609 | { | |
7610 | cxx_incomplete_type_error (r, rtype); | |
0cbd7506 | 7611 | r = error_mark_node; |
7088fca9 KL |
7612 | } |
7613 | } | |
5566b478 | 7614 | |
7088fca9 KL |
7615 | /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, |
7616 | such a thing will already have been added to the field | |
7617 | list by tsubst_enum in finish_member_declaration in the | |
5e0c54e5 | 7618 | CLASSTYPE_NESTED_UTDS case above. */ |
7088fca9 KL |
7619 | if (!(TREE_CODE (r) == TYPE_DECL |
7620 | && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE | |
43b26a01 | 7621 | && DECL_ARTIFICIAL (r))) |
7088fca9 KL |
7622 | { |
7623 | set_current_access_from_decl (r); | |
7624 | finish_member_declaration (r); | |
7625 | } | |
0cbd7506 | 7626 | } |
7088fca9 | 7627 | } |
61fc8c9e | 7628 | } |
7088fca9 KL |
7629 | else |
7630 | { | |
7631 | if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) | |
7632 | { | |
7633 | /* Build new CLASSTYPE_FRIEND_CLASSES. */ | |
7634 | ||
7635 | tree friend_type = t; | |
b939a023 | 7636 | bool adjust_processing_template_decl = false; |
1aed5355 | 7637 | |
7088fca9 | 7638 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
b939a023 | 7639 | { |
5a24482e | 7640 | /* template <class T> friend class C; */ |
b939a023 | 7641 | friend_type = tsubst_friend_class (friend_type, args); |
0cbd7506 | 7642 | adjust_processing_template_decl = true; |
b939a023 KL |
7643 | } |
7644 | else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE) | |
7645 | { | |
5a24482e | 7646 | /* template <class T> friend class C::D; */ |
b939a023 | 7647 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 7648 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
7649 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
7650 | friend_type = TREE_TYPE (friend_type); | |
0cbd7506 | 7651 | adjust_processing_template_decl = true; |
b939a023 KL |
7652 | } |
7653 | else if (TREE_CODE (friend_type) == TYPENAME_TYPE) | |
7654 | { | |
5a24482e KL |
7655 | /* This could be either |
7656 | ||
7657 | friend class T::C; | |
7658 | ||
7659 | when dependent_type_p is false or | |
7660 | ||
7661 | template <class U> friend class T::C; | |
7662 | ||
7663 | otherwise. */ | |
b939a023 | 7664 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 7665 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
7666 | /* Bump processing_template_decl for correct |
7667 | dependent_type_p calculation. */ | |
7668 | ++processing_template_decl; | |
7669 | if (dependent_type_p (friend_type)) | |
7670 | adjust_processing_template_decl = true; | |
7671 | --processing_template_decl; | |
7672 | } | |
5a24482e KL |
7673 | else if (!CLASSTYPE_USE_TEMPLATE (friend_type) |
7674 | && hidden_name_p (TYPE_NAME (friend_type))) | |
7088fca9 | 7675 | { |
5a24482e KL |
7676 | /* friend class C; |
7677 | ||
7678 | where C hasn't been declared yet. Let's lookup name | |
7679 | from namespace scope directly, bypassing any name that | |
7680 | come from dependent base class. */ | |
7088fca9 KL |
7681 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); |
7682 | ||
7683 | /* The call to xref_tag_from_type does injection for friend | |
7684 | classes. */ | |
7685 | push_nested_namespace (ns); | |
c8094d83 MS |
7686 | friend_type = |
7687 | xref_tag_from_type (friend_type, NULL_TREE, | |
5a24482e | 7688 | /*tag_scope=*/ts_current); |
7088fca9 KL |
7689 | pop_nested_namespace (ns); |
7690 | } | |
5a24482e KL |
7691 | else if (uses_template_parms (friend_type)) |
7692 | /* friend class C<T>; */ | |
7693 | friend_type = tsubst (friend_type, args, | |
23fca1f5 | 7694 | tf_warning_or_error, NULL_TREE); |
5a24482e KL |
7695 | /* Otherwise it's |
7696 | ||
7697 | friend class C; | |
7698 | ||
7699 | where C is already declared or | |
7700 | ||
7701 | friend class C<int>; | |
7702 | ||
0cbd7506 | 7703 | We don't have to do anything in these cases. */ |
1aed5355 | 7704 | |
b939a023 | 7705 | if (adjust_processing_template_decl) |
7088fca9 KL |
7706 | /* Trick make_friend_class into realizing that the friend |
7707 | we're adding is a template, not an ordinary class. It's | |
7708 | important that we use make_friend_class since it will | |
7709 | perform some error-checking and output cross-reference | |
7710 | information. */ | |
7711 | ++processing_template_decl; | |
fc378698 | 7712 | |
b939a023 | 7713 | if (friend_type != error_mark_node) |
0cbd7506 | 7714 | make_friend_class (type, friend_type, /*complain=*/false); |
fc378698 | 7715 | |
b939a023 | 7716 | if (adjust_processing_template_decl) |
7088fca9 KL |
7717 | --processing_template_decl; |
7718 | } | |
7719 | else | |
9579624e KL |
7720 | { |
7721 | /* Build new DECL_FRIENDLIST. */ | |
7722 | tree r; | |
7723 | ||
39a13be5 | 7724 | /* The file and line for this declaration, to |
6e432b31 KL |
7725 | assist in error message reporting. Since we |
7726 | called push_tinst_level above, we don't need to | |
7727 | restore these. */ | |
7728 | input_location = DECL_SOURCE_LOCATION (t); | |
7729 | ||
9579624e | 7730 | if (TREE_CODE (t) == TEMPLATE_DECL) |
cad7e87b NS |
7731 | { |
7732 | ++processing_template_decl; | |
7733 | push_deferring_access_checks (dk_no_check); | |
7734 | } | |
c8094d83 | 7735 | |
9579624e | 7736 | r = tsubst_friend_function (t, args); |
9579624e | 7737 | add_friend (type, r, /*complain=*/false); |
cad7e87b NS |
7738 | if (TREE_CODE (t) == TEMPLATE_DECL) |
7739 | { | |
7740 | pop_deferring_access_checks (); | |
7741 | --processing_template_decl; | |
7742 | } | |
9579624e | 7743 | } |
7088fca9 KL |
7744 | } |
7745 | } | |
5566b478 | 7746 | |
61a127b3 MM |
7747 | /* Set the file and line number information to whatever is given for |
7748 | the class itself. This puts error messages involving generated | |
7749 | implicit functions at a predictable point, and the same point | |
7750 | that would be used for non-template classes. */ | |
f31686a3 | 7751 | input_location = DECL_SOURCE_LOCATION (typedecl); |
6de9cd9a | 7752 | |
61a127b3 | 7753 | unreverse_member_declarations (type); |
9f33663b | 7754 | finish_struct_1 (type); |
5524676d | 7755 | TYPE_BEING_DEFINED (type) = 0; |
8d08fdba | 7756 | |
9188c363 MM |
7757 | /* Now that the class is complete, instantiate default arguments for |
7758 | any member functions. We don't do this earlier because the | |
7759 | default arguments may reference members of the class. */ | |
be93747e | 7760 | if (!PRIMARY_TEMPLATE_P (templ)) |
9188c363 | 7761 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) |
c8094d83 | 7762 | if (TREE_CODE (t) == FUNCTION_DECL |
dc957d14 | 7763 | /* Implicitly generated member functions will not have template |
9188c363 MM |
7764 | information; they are not instantiations, but instead are |
7765 | created "fresh" for each instantiation. */ | |
7766 | && DECL_TEMPLATE_INFO (t)) | |
7767 | tsubst_default_arguments (t); | |
7768 | ||
d0940d56 DS |
7769 | /* Some typedefs referenced from within the template code need to be access |
7770 | checked at template instantiation time, i.e now. These types were | |
7771 | added to the template at parsing time. Let's get those and perform | |
2eb25c98 DS |
7772 | the access checks then. */ |
7773 | perform_typedefs_access_check (pattern, args); | |
d0940d56 | 7774 | perform_deferred_access_checks (); |
56d0c6e3 | 7775 | pop_nested_class (); |
5566b478 | 7776 | pop_from_top_level (); |
78757caa | 7777 | pop_deferring_access_checks (); |
5566b478 MS |
7778 | pop_tinst_level (); |
7779 | ||
4684cd27 MM |
7780 | /* The vtable for a template class can be emitted in any translation |
7781 | unit in which the class is instantiated. When there is no key | |
7782 | method, however, finish_struct_1 will already have added TYPE to | |
7783 | the keyed_classes list. */ | |
7784 | if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type)) | |
9aad8f83 MA |
7785 | keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); |
7786 | ||
5566b478 | 7787 | return type; |
8d08fdba MS |
7788 | } |
7789 | ||
00d3396f | 7790 | static tree |
a91db711 | 7791 | tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
00d3396f | 7792 | { |
a91db711 | 7793 | tree r; |
c8094d83 | 7794 | |
a91db711 NS |
7795 | if (!t) |
7796 | r = t; | |
7797 | else if (TYPE_P (t)) | |
7798 | r = tsubst (t, args, complain, in_decl); | |
7799 | else | |
00d3396f | 7800 | { |
015c2c66 MM |
7801 | r = tsubst_expr (t, args, complain, in_decl, |
7802 | /*integral_constant_expression_p=*/true); | |
6f25cb35 | 7803 | r = fold_non_dependent_expr (r); |
bd83b409 | 7804 | } |
5d80a306 DG |
7805 | return r; |
7806 | } | |
7807 | ||
6afcfe0a JM |
7808 | /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a |
7809 | NONTYPE_ARGUMENT_PACK. */ | |
7810 | ||
7811 | static tree | |
7812 | make_fnparm_pack (tree spec_parm) | |
7813 | { | |
7814 | /* Collect all of the extra "packed" parameters into an | |
7815 | argument pack. */ | |
7816 | tree parmvec; | |
7817 | tree parmtypevec; | |
7818 | tree argpack = make_node (NONTYPE_ARGUMENT_PACK); | |
7819 | tree argtypepack = make_node (TYPE_ARGUMENT_PACK); | |
7820 | int i, len = list_length (spec_parm); | |
7821 | ||
7822 | /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */ | |
7823 | parmvec = make_tree_vec (len); | |
7824 | parmtypevec = make_tree_vec (len); | |
7825 | for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm)) | |
7826 | { | |
7827 | TREE_VEC_ELT (parmvec, i) = spec_parm; | |
7828 | TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm); | |
7829 | } | |
7830 | ||
7831 | /* Build the argument packs. */ | |
7832 | SET_ARGUMENT_PACK_ARGS (argpack, parmvec); | |
7833 | SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec); | |
7834 | TREE_TYPE (argpack) = argtypepack; | |
7835 | ||
7836 | return argpack; | |
7837 | } | |
7838 | ||
5d80a306 DG |
7839 | /* Substitute ARGS into T, which is an pack expansion |
7840 | (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a | |
7841 | TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node | |
7842 | (if only a partial substitution could be performed) or | |
7843 | ERROR_MARK_NODE if there was an error. */ | |
7844 | tree | |
7845 | tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, | |
7846 | tree in_decl) | |
7847 | { | |
7848 | tree pattern; | |
7849 | tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE; | |
7850 | tree first_arg_pack; int i, len = -1; | |
7851 | tree result; | |
7852 | int incomplete = 0; | |
6afcfe0a | 7853 | bool very_local_specializations = false; |
5d80a306 DG |
7854 | |
7855 | gcc_assert (PACK_EXPANSION_P (t)); | |
7856 | pattern = PACK_EXPANSION_PATTERN (t); | |
7857 | ||
7858 | /* Determine the argument packs that will instantiate the parameter | |
7859 | packs used in the expansion expression. While we're at it, | |
7860 | compute the number of arguments to be expanded and make sure it | |
7861 | is consistent. */ | |
7862 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack; | |
7863 | pack = TREE_CHAIN (pack)) | |
7864 | { | |
7865 | tree parm_pack = TREE_VALUE (pack); | |
7866 | tree arg_pack = NULL_TREE; | |
7867 | tree orig_arg = NULL_TREE; | |
7868 | ||
7869 | if (TREE_CODE (parm_pack) == PARM_DECL) | |
6afcfe0a JM |
7870 | { |
7871 | arg_pack = retrieve_local_specialization (parm_pack); | |
7872 | if (arg_pack == NULL_TREE) | |
7873 | { | |
7874 | /* This can happen for a parameter name used later in a function | |
7875 | declaration (such as in a late-specified return type). Just | |
7876 | make a dummy decl, since it's only used for its type. */ | |
7d882b83 | 7877 | gcc_assert (cp_unevaluated_operand != 0); |
6afcfe0a JM |
7878 | arg_pack = tsubst_decl (parm_pack, args, complain); |
7879 | arg_pack = make_fnparm_pack (arg_pack); | |
7880 | } | |
7881 | } | |
5d80a306 DG |
7882 | else |
7883 | { | |
7884 | int level, idx, levels; | |
7885 | template_parm_level_and_index (parm_pack, &level, &idx); | |
7886 | ||
7887 | levels = TMPL_ARGS_DEPTH (args); | |
7888 | if (level <= levels) | |
7889 | arg_pack = TMPL_ARG (args, level, idx); | |
7890 | } | |
7891 | ||
7892 | orig_arg = arg_pack; | |
7893 | if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT) | |
7894 | arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack); | |
7895 | ||
d393153e DG |
7896 | if (arg_pack && !ARGUMENT_PACK_P (arg_pack)) |
7897 | /* This can only happen if we forget to expand an argument | |
7898 | pack somewhere else. Just return an error, silently. */ | |
7899 | { | |
7900 | result = make_tree_vec (1); | |
7901 | TREE_VEC_ELT (result, 0) = error_mark_node; | |
7902 | return result; | |
7903 | } | |
7904 | ||
88b82314 DG |
7905 | if (arg_pack |
7906 | && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1 | |
7907 | && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0))) | |
7908 | { | |
7909 | tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0); | |
7910 | tree pattern = PACK_EXPANSION_PATTERN (expansion); | |
7911 | if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack)) | |
7912 | || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern))) | |
7913 | /* The argument pack that the parameter maps to is just an | |
7914 | expansion of the parameter itself, such as one would | |
7915 | find in the implicit typedef of a class inside the | |
7916 | class itself. Consider this parameter "unsubstituted", | |
7917 | so that we will maintain the outer pack expansion. */ | |
7918 | arg_pack = NULL_TREE; | |
7919 | } | |
7920 | ||
5d80a306 DG |
7921 | if (arg_pack) |
7922 | { | |
7923 | int my_len = | |
7924 | TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)); | |
7925 | ||
7926 | /* It's all-or-nothing with incomplete argument packs. */ | |
7927 | if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
7928 | return error_mark_node; | |
7929 | ||
7930 | if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) | |
7931 | incomplete = 1; | |
7932 | ||
7933 | if (len < 0) | |
7934 | { | |
7935 | len = my_len; | |
7936 | first_arg_pack = arg_pack; | |
7937 | } | |
7938 | else if (len != my_len) | |
7939 | { | |
7940 | if (TREE_CODE (t) == TYPE_PACK_EXPANSION) | |
7941 | error ("mismatched argument pack lengths while expanding " | |
7942 | "%<%T%>", | |
7943 | pattern); | |
7944 | else | |
7945 | error ("mismatched argument pack lengths while expanding " | |
7946 | "%<%E%>", | |
7947 | pattern); | |
7948 | return error_mark_node; | |
7949 | } | |
7950 | ||
7951 | /* Keep track of the parameter packs and their corresponding | |
7952 | argument packs. */ | |
7953 | packs = tree_cons (parm_pack, arg_pack, packs); | |
7954 | TREE_TYPE (packs) = orig_arg; | |
7955 | } | |
7956 | else | |
7957 | /* We can't substitute for this parameter pack. */ | |
7958 | unsubstituted_packs = tree_cons (TREE_PURPOSE (pack), | |
7959 | TREE_VALUE (pack), | |
7960 | unsubstituted_packs); | |
7961 | } | |
7962 | ||
7963 | /* We cannot expand this expansion expression, because we don't have | |
7964 | all of the argument packs we need. Substitute into the pattern | |
7965 | and return a PACK_EXPANSION_*. The caller will need to deal with | |
7966 | that. */ | |
7967 | if (unsubstituted_packs) | |
0db906f1 JM |
7968 | { |
7969 | tree new_pat; | |
7970 | if (TREE_CODE (t) == EXPR_PACK_EXPANSION) | |
7971 | new_pat = tsubst_expr (pattern, args, complain, in_decl, | |
7972 | /*integral_constant_expression_p=*/false); | |
7973 | else | |
7974 | new_pat = tsubst (pattern, args, complain, in_decl); | |
7975 | return make_pack_expansion (new_pat); | |
7976 | } | |
5d80a306 DG |
7977 | |
7978 | /* We could not find any argument packs that work. */ | |
7979 | if (len < 0) | |
7980 | return error_mark_node; | |
7981 | ||
6afcfe0a JM |
7982 | if (!local_specializations) |
7983 | { | |
7984 | /* We're in a late-specified return type, so we don't have a local | |
7985 | specializations table. Create one for doing this expansion. */ | |
7986 | very_local_specializations = true; | |
7987 | local_specializations = htab_create (37, | |
7988 | hash_local_specialization, | |
7989 | eq_local_specializations, | |
7990 | NULL); | |
7991 | } | |
7992 | ||
5d80a306 DG |
7993 | /* For each argument in each argument pack, substitute into the |
7994 | pattern. */ | |
7995 | result = make_tree_vec (len + incomplete); | |
7996 | for (i = 0; i < len + incomplete; ++i) | |
7997 | { | |
7998 | /* For parameter pack, change the substitution of the parameter | |
7999 | pack to the ith argument in its argument pack, then expand | |
8000 | the pattern. */ | |
8001 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
8002 | { | |
8003 | tree parm = TREE_PURPOSE (pack); | |
8004 | ||
8005 | if (TREE_CODE (parm) == PARM_DECL) | |
8006 | { | |
8007 | /* Select the Ith argument from the pack. */ | |
8008 | tree arg = make_node (ARGUMENT_PACK_SELECT); | |
8009 | ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack); | |
8010 | ARGUMENT_PACK_SELECT_INDEX (arg) = i; | |
8011 | mark_used (parm); | |
8012 | register_local_specialization (arg, parm); | |
8013 | } | |
8014 | else | |
8015 | { | |
8016 | tree value = parm; | |
8017 | int idx, level; | |
8018 | template_parm_level_and_index (parm, &level, &idx); | |
8019 | ||
8020 | if (i < len) | |
8021 | { | |
8022 | /* Select the Ith argument from the pack. */ | |
8023 | value = make_node (ARGUMENT_PACK_SELECT); | |
8024 | ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack); | |
8025 | ARGUMENT_PACK_SELECT_INDEX (value) = i; | |
8026 | } | |
8027 | ||
8028 | /* Update the corresponding argument. */ | |
8029 | TMPL_ARG (args, level, idx) = value; | |
8030 | } | |
8031 | } | |
8032 | ||
8033 | /* Substitute into the PATTERN with the altered arguments. */ | |
8034 | if (TREE_CODE (t) == EXPR_PACK_EXPANSION) | |
8035 | TREE_VEC_ELT (result, i) = | |
8036 | tsubst_expr (pattern, args, complain, in_decl, | |
8037 | /*integral_constant_expression_p=*/false); | |
8038 | else | |
8039 | TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl); | |
8040 | ||
8041 | if (i == len) | |
8042 | /* When we have incomplete argument packs, the last "expanded" | |
8043 | result is itself a pack expansion, which allows us | |
8044 | to deduce more arguments. */ | |
8045 | TREE_VEC_ELT (result, i) = | |
8046 | make_pack_expansion (TREE_VEC_ELT (result, i)); | |
8047 | ||
8048 | if (TREE_VEC_ELT (result, i) == error_mark_node) | |
8049 | { | |
8050 | result = error_mark_node; | |
8051 | break; | |
8052 | } | |
8053 | } | |
6afcfe0a | 8054 | |
5d80a306 DG |
8055 | /* Update ARGS to restore the substitution from parameter packs to |
8056 | their argument packs. */ | |
8057 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
8058 | { | |
8059 | tree parm = TREE_PURPOSE (pack); | |
8060 | ||
8061 | if (TREE_CODE (parm) == PARM_DECL) | |
8062 | register_local_specialization (TREE_TYPE (pack), parm); | |
8063 | else | |
8064 | { | |
8065 | int idx, level; | |
8066 | template_parm_level_and_index (parm, &level, &idx); | |
8067 | ||
8068 | /* Update the corresponding argument. */ | |
8069 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
8070 | TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) = | |
8071 | TREE_TYPE (pack); | |
8072 | else | |
8073 | TREE_VEC_ELT (args, idx) = TREE_TYPE (pack); | |
8074 | } | |
8075 | } | |
8076 | ||
6afcfe0a JM |
8077 | if (very_local_specializations) |
8078 | { | |
8079 | htab_delete (local_specializations); | |
8080 | local_specializations = NULL; | |
8081 | } | |
8082 | ||
5d80a306 | 8083 | return result; |
bd83b409 NS |
8084 | } |
8085 | ||
5f5babf1 JM |
8086 | /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template |
8087 | TMPL. We do this using DECL_PARM_INDEX, which should work even with | |
8088 | parameter packs; all parms generated from a function parameter pack will | |
8089 | have the same DECL_PARM_INDEX. */ | |
8090 | ||
8091 | tree | |
8092 | get_pattern_parm (tree parm, tree tmpl) | |
8093 | { | |
8094 | tree pattern = DECL_TEMPLATE_RESULT (tmpl); | |
8095 | tree patparm; | |
8096 | ||
8097 | if (DECL_ARTIFICIAL (parm)) | |
8098 | { | |
8099 | for (patparm = DECL_ARGUMENTS (pattern); | |
8100 | patparm; patparm = TREE_CHAIN (patparm)) | |
8101 | if (DECL_ARTIFICIAL (patparm) | |
8102 | && DECL_NAME (parm) == DECL_NAME (patparm)) | |
8103 | break; | |
8104 | } | |
8105 | else | |
8106 | { | |
8107 | patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl)); | |
8108 | patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm); | |
8109 | gcc_assert (DECL_PARM_INDEX (patparm) | |
8110 | == DECL_PARM_INDEX (parm)); | |
8111 | } | |
8112 | ||
8113 | return patparm; | |
8114 | } | |
8115 | ||
a91db711 | 8116 | /* Substitute ARGS into the vector or list of template arguments T. */ |
830bfa74 | 8117 | |
e9659ab0 | 8118 | static tree |
a91db711 | 8119 | tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
830bfa74 | 8120 | { |
5d80a306 | 8121 | tree orig_t = t; |
bf12d54d | 8122 | int len = TREE_VEC_LENGTH (t); |
5d80a306 | 8123 | int need_new = 0, i, expanded_len_adjust = 0, out; |
67f5655f | 8124 | tree *elts = (tree *) alloca (len * sizeof (tree)); |
c8094d83 | 8125 | |
830bfa74 MM |
8126 | for (i = 0; i < len; i++) |
8127 | { | |
bf12d54d NS |
8128 | tree orig_arg = TREE_VEC_ELT (t, i); |
8129 | tree new_arg; | |
a91db711 | 8130 | |
bf12d54d NS |
8131 | if (TREE_CODE (orig_arg) == TREE_VEC) |
8132 | new_arg = tsubst_template_args (orig_arg, args, complain, in_decl); | |
5d80a306 DG |
8133 | else if (PACK_EXPANSION_P (orig_arg)) |
8134 | { | |
8135 | /* Substitute into an expansion expression. */ | |
8136 | new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl); | |
8137 | ||
8138 | if (TREE_CODE (new_arg) == TREE_VEC) | |
8139 | /* Add to the expanded length adjustment the number of | |
8140 | expanded arguments. We subtract one from this | |
8141 | measurement, because the argument pack expression | |
8142 | itself is already counted as 1 in | |
8143 | LEN. EXPANDED_LEN_ADJUST can actually be negative, if | |
8144 | the argument pack is empty. */ | |
8145 | expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1; | |
8146 | } | |
8147 | else if (ARGUMENT_PACK_P (orig_arg)) | |
8148 | { | |
8149 | /* Substitute into each of the arguments. */ | |
8150 | new_arg = make_node (TREE_CODE (orig_arg)); | |
8151 | ||
8152 | SET_ARGUMENT_PACK_ARGS ( | |
8153 | new_arg, | |
8154 | tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg), | |
8155 | args, complain, in_decl)); | |
8156 | ||
8157 | if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node) | |
8158 | new_arg = error_mark_node; | |
8159 | ||
8160 | if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) { | |
8161 | TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args, | |
8162 | complain, in_decl); | |
8163 | TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg); | |
8164 | ||
8165 | if (TREE_TYPE (new_arg) == error_mark_node) | |
8166 | new_arg = error_mark_node; | |
8167 | } | |
8168 | } | |
830bfa74 | 8169 | else |
a91db711 | 8170 | new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl); |
c8094d83 | 8171 | |
a91db711 | 8172 | if (new_arg == error_mark_node) |
08e72a19 JM |
8173 | return error_mark_node; |
8174 | ||
a91db711 NS |
8175 | elts[i] = new_arg; |
8176 | if (new_arg != orig_arg) | |
830bfa74 MM |
8177 | need_new = 1; |
8178 | } | |
c8094d83 | 8179 | |
830bfa74 MM |
8180 | if (!need_new) |
8181 | return t; | |
a91db711 | 8182 | |
5d80a306 DG |
8183 | /* Make space for the expanded arguments coming from template |
8184 | argument packs. */ | |
8185 | t = make_tree_vec (len + expanded_len_adjust); | |
8186 | for (i = 0, out = 0; i < len; i++) | |
8187 | { | |
8188 | if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i)) | |
8189 | || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i))) | |
8190 | && TREE_CODE (elts[i]) == TREE_VEC) | |
8191 | { | |
8192 | int idx; | |
8193 | ||
8194 | /* Now expand the template argument pack "in place". */ | |
8195 | for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++) | |
8196 | TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx); | |
8197 | } | |
8198 | else | |
8199 | { | |
8200 | TREE_VEC_ELT (t, out) = elts[i]; | |
8201 | out++; | |
8202 | } | |
8203 | } | |
c8094d83 | 8204 | |
830bfa74 MM |
8205 | return t; |
8206 | } | |
8207 | ||
36a117a5 MM |
8208 | /* Return the result of substituting ARGS into the template parameters |
8209 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
8210 | PARMS, then the result will contain n levels of PARMS. For | |
8211 | example, if PARMS is `template <class T> template <class U> | |
8212 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
8213 | result will be `template <int*, double, class V>'. */ | |
8214 | ||
e9659ab0 | 8215 | static tree |
3a978d72 | 8216 | tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain) |
36a117a5 | 8217 | { |
f71f87f9 MM |
8218 | tree r = NULL_TREE; |
8219 | tree* new_parms; | |
36a117a5 | 8220 | |
8ca4bf25 MM |
8221 | /* When substituting into a template, we must set |
8222 | PROCESSING_TEMPLATE_DECL as the template parameters may be | |
8223 | dependent if they are based on one-another, and the dependency | |
8224 | predicates are short-circuit outside of templates. */ | |
8225 | ++processing_template_decl; | |
8226 | ||
36a117a5 MM |
8227 | for (new_parms = &r; |
8228 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
8229 | new_parms = &(TREE_CHAIN (*new_parms)), | |
8230 | parms = TREE_CHAIN (parms)) | |
8231 | { | |
c8094d83 | 8232 | tree new_vec = |
36a117a5 MM |
8233 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); |
8234 | int i; | |
c8094d83 | 8235 | |
36a117a5 MM |
8236 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) |
8237 | { | |
42b304f1 LM |
8238 | tree tuple; |
8239 | tree default_value; | |
8240 | tree parm_decl; | |
8241 | ||
8242 | if (parms == error_mark_node) | |
8243 | continue; | |
8244 | ||
8245 | tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); | |
8246 | ||
8247 | if (tuple == error_mark_node) | |
8248 | continue; | |
8249 | ||
8250 | default_value = TREE_PURPOSE (tuple); | |
8251 | parm_decl = TREE_VALUE (tuple); | |
833aa4c4 NS |
8252 | |
8253 | parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); | |
a207780f VR |
8254 | if (TREE_CODE (parm_decl) == PARM_DECL |
8255 | && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain)) | |
8256 | parm_decl = error_mark_node; | |
a91db711 NS |
8257 | default_value = tsubst_template_arg (default_value, args, |
8258 | complain, NULL_TREE); | |
c8094d83 | 8259 | |
a91db711 | 8260 | tuple = build_tree_list (default_value, parm_decl); |
833aa4c4 | 8261 | TREE_VEC_ELT (new_vec, i) = tuple; |
36a117a5 | 8262 | } |
c8094d83 MS |
8263 | |
8264 | *new_parms = | |
8265 | tree_cons (size_int (TMPL_PARMS_DEPTH (parms) | |
4890c2f4 | 8266 | - TMPL_ARGS_DEPTH (args)), |
36a117a5 MM |
8267 | new_vec, NULL_TREE); |
8268 | } | |
8269 | ||
8ca4bf25 MM |
8270 | --processing_template_decl; |
8271 | ||
36a117a5 MM |
8272 | return r; |
8273 | } | |
8274 | ||
ed44da02 MM |
8275 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
8276 | type T. If T is not an aggregate or enumeration type, it is | |
8277 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
838dfd8a | 8278 | ENTERING_SCOPE is nonzero, T is the context for a template which |
dc957d14 | 8279 | we are presently tsubst'ing. Return the substituted value. */ |
36a117a5 | 8280 | |
e9659ab0 | 8281 | static tree |
c8094d83 | 8282 | tsubst_aggr_type (tree t, |
0cbd7506 MS |
8283 | tree args, |
8284 | tsubst_flags_t complain, | |
8285 | tree in_decl, | |
8286 | int entering_scope) | |
36a117a5 MM |
8287 | { |
8288 | if (t == NULL_TREE) | |
8289 | return NULL_TREE; | |
8290 | ||
8291 | switch (TREE_CODE (t)) | |
8292 | { | |
8293 | case RECORD_TYPE: | |
8294 | if (TYPE_PTRMEMFUNC_P (t)) | |
46c895ac | 8295 | return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); |
36a117a5 | 8296 | |
f4f206f4 | 8297 | /* Else fall through. */ |
ed44da02 | 8298 | case ENUMERAL_TYPE: |
36a117a5 | 8299 | case UNION_TYPE: |
82390eb6 | 8300 | if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t)) |
36a117a5 MM |
8301 | { |
8302 | tree argvec; | |
8303 | tree context; | |
8304 | tree r; | |
7d882b83 ILT |
8305 | int saved_unevaluated_operand; |
8306 | int saved_inhibit_evaluation_warnings; | |
3489ea0c MM |
8307 | |
8308 | /* In "sizeof(X<I>)" we need to evaluate "I". */ | |
7d882b83 ILT |
8309 | saved_unevaluated_operand = cp_unevaluated_operand; |
8310 | cp_unevaluated_operand = 0; | |
8311 | saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; | |
8312 | c_inhibit_evaluation_warnings = 0; | |
36a117a5 MM |
8313 | |
8314 | /* First, determine the context for the type we are looking | |
8315 | up. */ | |
4f7847ca NS |
8316 | context = TYPE_CONTEXT (t); |
8317 | if (context) | |
29b0d1fd JM |
8318 | { |
8319 | context = tsubst_aggr_type (context, args, complain, | |
8320 | in_decl, /*entering_scope=*/1); | |
8321 | /* If context is a nested class inside a class template, | |
8322 | it may still need to be instantiated (c++/33959). */ | |
8323 | if (TYPE_P (context)) | |
8324 | context = complete_type (context); | |
8325 | } | |
36a117a5 MM |
8326 | |
8327 | /* Then, figure out what arguments are appropriate for the | |
8328 | type we are trying to find. For example, given: | |
8329 | ||
8330 | template <class T> struct S; | |
8331 | template <class T, class U> void f(T, U) { S<U> su; } | |
8332 | ||
8333 | and supposing that we are instantiating f<int, double>, | |
8334 | then our ARGS will be {int, double}, but, when looking up | |
8335 | S we only want {double}. */ | |
a91db711 NS |
8336 | argvec = tsubst_template_args (TYPE_TI_ARGS (t), args, |
8337 | complain, in_decl); | |
08e72a19 | 8338 | if (argvec == error_mark_node) |
3489ea0c MM |
8339 | r = error_mark_node; |
8340 | else | |
8341 | { | |
8342 | r = lookup_template_class (t, argvec, in_decl, context, | |
8343 | entering_scope, complain); | |
8344 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); | |
8345 | } | |
3db45ab5 | 8346 | |
7d882b83 ILT |
8347 | cp_unevaluated_operand = saved_unevaluated_operand; |
8348 | c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings; | |
36a117a5 | 8349 | |
3489ea0c | 8350 | return r; |
36a117a5 | 8351 | } |
c8094d83 | 8352 | else |
36a117a5 MM |
8353 | /* This is not a template type, so there's nothing to do. */ |
8354 | return t; | |
8355 | ||
8356 | default: | |
4393e105 | 8357 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
8358 | } |
8359 | } | |
8360 | ||
9188c363 MM |
8361 | /* Substitute into the default argument ARG (a default argument for |
8362 | FN), which has the indicated TYPE. */ | |
8363 | ||
8364 | tree | |
3a978d72 | 8365 | tsubst_default_argument (tree fn, tree type, tree arg) |
9188c363 | 8366 | { |
2436b51f MM |
8367 | tree saved_class_ptr = NULL_TREE; |
8368 | tree saved_class_ref = NULL_TREE; | |
8369 | ||
9188c363 MM |
8370 | /* This default argument came from a template. Instantiate the |
8371 | default argument here, not in tsubst. In the case of | |
c8094d83 MS |
8372 | something like: |
8373 | ||
9188c363 MM |
8374 | template <class T> |
8375 | struct S { | |
8376 | static T t(); | |
8377 | void f(T = t()); | |
8378 | }; | |
c8094d83 | 8379 | |
9188c363 | 8380 | we must be careful to do name lookup in the scope of S<T>, |
d5a10cf0 | 8381 | rather than in the current class. */ |
2b59fc25 | 8382 | push_access_scope (fn); |
2436b51f MM |
8383 | /* The "this" pointer is not valid in a default argument. */ |
8384 | if (cfun) | |
8385 | { | |
8386 | saved_class_ptr = current_class_ptr; | |
8387 | cp_function_chain->x_current_class_ptr = NULL_TREE; | |
8388 | saved_class_ref = current_class_ref; | |
8389 | cp_function_chain->x_current_class_ref = NULL_TREE; | |
8390 | } | |
9188c363 | 8391 | |
d5a10cf0 | 8392 | push_deferring_access_checks(dk_no_deferred); |
5a8613b2 MM |
8393 | /* The default argument expression may cause implicitly defined |
8394 | member functions to be synthesized, which will result in garbage | |
8395 | collection. We must treat this situation as if we were within | |
8396 | the body of function so as to avoid collecting live data on the | |
8397 | stack. */ | |
8398 | ++function_depth; | |
c2ea3a40 | 8399 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), |
015c2c66 MM |
8400 | tf_warning_or_error, NULL_TREE, |
8401 | /*integral_constant_expression_p=*/false); | |
5a8613b2 | 8402 | --function_depth; |
d5a10cf0 MM |
8403 | pop_deferring_access_checks(); |
8404 | ||
2436b51f MM |
8405 | /* Restore the "this" pointer. */ |
8406 | if (cfun) | |
8407 | { | |
8408 | cp_function_chain->x_current_class_ptr = saved_class_ptr; | |
8409 | cp_function_chain->x_current_class_ref = saved_class_ref; | |
8410 | } | |
8411 | ||
9188c363 MM |
8412 | /* Make sure the default argument is reasonable. */ |
8413 | arg = check_default_argument (type, arg); | |
8414 | ||
3693f266 DS |
8415 | pop_access_scope (fn); |
8416 | ||
9188c363 MM |
8417 | return arg; |
8418 | } | |
8419 | ||
8420 | /* Substitute into all the default arguments for FN. */ | |
8421 | ||
8422 | static void | |
3a978d72 | 8423 | tsubst_default_arguments (tree fn) |
9188c363 MM |
8424 | { |
8425 | tree arg; | |
8426 | tree tmpl_args; | |
8427 | ||
8428 | tmpl_args = DECL_TI_ARGS (fn); | |
8429 | ||
8430 | /* If this function is not yet instantiated, we certainly don't need | |
8431 | its default arguments. */ | |
8432 | if (uses_template_parms (tmpl_args)) | |
8433 | return; | |
8434 | ||
c8094d83 MS |
8435 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
8436 | arg; | |
9188c363 MM |
8437 | arg = TREE_CHAIN (arg)) |
8438 | if (TREE_PURPOSE (arg)) | |
c8094d83 | 8439 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, |
9188c363 MM |
8440 | TREE_VALUE (arg), |
8441 | TREE_PURPOSE (arg)); | |
8442 | } | |
8443 | ||
fc6a28d7 MM |
8444 | /* Substitute the ARGS into the T, which is a _DECL. Return the |
8445 | result of the substitution. Issue error and warning messages under | |
8446 | control of COMPLAIN. */ | |
00d3396f | 8447 | |
e9659ab0 | 8448 | static tree |
fc6a28d7 | 8449 | tsubst_decl (tree t, tree args, tsubst_flags_t complain) |
8d08fdba | 8450 | { |
82a98427 | 8451 | location_t saved_loc; |
b370501f | 8452 | tree r = NULL_TREE; |
4b2811e9 | 8453 | tree in_decl = t; |
2b110bfc | 8454 | hashval_t hash = 0; |
830bfa74 | 8455 | |
ae58fa02 | 8456 | /* Set the filename and linenumber to improve error-reporting. */ |
82a98427 | 8457 | saved_loc = input_location; |
f31686a3 | 8458 | input_location = DECL_SOURCE_LOCATION (t); |
b7484fbe | 8459 | |
8d08fdba MS |
8460 | switch (TREE_CODE (t)) |
8461 | { | |
98c1c668 JM |
8462 | case TEMPLATE_DECL: |
8463 | { | |
28e42b7e | 8464 | /* We can get here when processing a member function template, |
438c0fa8 | 8465 | member class template, or template template parameter. */ |
98c1c668 | 8466 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 8467 | tree spec; |
28e42b7e KL |
8468 | tree tmpl_args; |
8469 | tree full_args; | |
98c1c668 | 8470 | |
28e42b7e | 8471 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
db2767b6 | 8472 | { |
28e42b7e KL |
8473 | /* Template template parameter is treated here. */ |
8474 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
8475 | if (new_type == error_mark_node) | |
8476 | return error_mark_node; | |
36a117a5 | 8477 | |
28e42b7e KL |
8478 | r = copy_decl (t); |
8479 | TREE_CHAIN (r) = NULL_TREE; | |
8480 | TREE_TYPE (r) = new_type; | |
8481 | DECL_TEMPLATE_RESULT (r) | |
c2255bc4 AH |
8482 | = build_decl (DECL_SOURCE_LOCATION (decl), |
8483 | TYPE_DECL, DECL_NAME (decl), new_type); | |
c8094d83 | 8484 | DECL_TEMPLATE_PARMS (r) |
28e42b7e KL |
8485 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
8486 | complain); | |
8487 | TYPE_NAME (new_type) = r; | |
8488 | break; | |
8489 | } | |
36a117a5 | 8490 | |
28e42b7e KL |
8491 | /* We might already have an instance of this template. |
8492 | The ARGS are for the surrounding class type, so the | |
8493 | full args contain the tsubst'd args for the context, | |
8494 | plus the innermost args from the template decl. */ | |
c8094d83 | 8495 | tmpl_args = DECL_CLASS_TEMPLATE_P (t) |
28e42b7e KL |
8496 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) |
8497 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); | |
8ca4bf25 MM |
8498 | /* Because this is a template, the arguments will still be |
8499 | dependent, even after substitution. If | |
8500 | PROCESSING_TEMPLATE_DECL is not set, the dependency | |
8501 | predicates will short-circuit. */ | |
8502 | ++processing_template_decl; | |
28e42b7e KL |
8503 | full_args = tsubst_template_args (tmpl_args, args, |
8504 | complain, in_decl); | |
8ca4bf25 | 8505 | --processing_template_decl; |
bf2f7328 VR |
8506 | if (full_args == error_mark_node) |
8507 | return error_mark_node; | |
28e42b7e | 8508 | |
438c0fa8 JM |
8509 | /* If this is a default template template argument, |
8510 | tsubst might not have changed anything. */ | |
8511 | if (full_args == tmpl_args) | |
8512 | return t; | |
28e42b7e | 8513 | |
2b110bfc JM |
8514 | hash = hash_tmpl_and_args (t, full_args); |
8515 | spec = retrieve_specialization (t, full_args, hash); | |
28e42b7e KL |
8516 | if (spec != NULL_TREE) |
8517 | { | |
8518 | r = spec; | |
8519 | break; | |
db2767b6 | 8520 | } |
98c1c668 JM |
8521 | |
8522 | /* Make a new template decl. It will be similar to the | |
c8094d83 | 8523 | original, but will record the current template arguments. |
98c1c668 JM |
8524 | We also create a new function declaration, which is just |
8525 | like the old one, but points to this new template, rather | |
8526 | than the old one. */ | |
0acf7199 | 8527 | r = copy_decl (t); |
50bc768d | 8528 | gcc_assert (DECL_LANG_SPECIFIC (r) != 0); |
ae58fa02 | 8529 | TREE_CHAIN (r) = NULL_TREE; |
db2767b6 | 8530 | |
ae58fa02 | 8531 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
8532 | |
8533 | if (TREE_CODE (decl) == TYPE_DECL) | |
8534 | { | |
8ca4bf25 MM |
8535 | tree new_type; |
8536 | ++processing_template_decl; | |
8537 | new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
3db45ab5 | 8538 | --processing_template_decl; |
2620d095 KL |
8539 | if (new_type == error_mark_node) |
8540 | return error_mark_node; | |
8541 | ||
ae58fa02 MM |
8542 | TREE_TYPE (r) = new_type; |
8543 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
17aec3eb | 8544 | DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); |
ae58fa02 | 8545 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); |
8ca4bf25 | 8546 | DECL_CONTEXT (r) = TYPE_CONTEXT (new_type); |
93cdc044 JM |
8547 | } |
8548 | else | |
8549 | { | |
8ca4bf25 MM |
8550 | tree new_decl; |
8551 | ++processing_template_decl; | |
8552 | new_decl = tsubst (decl, args, complain, in_decl); | |
8553 | --processing_template_decl; | |
caec1dc0 KL |
8554 | if (new_decl == error_mark_node) |
8555 | return error_mark_node; | |
17aec3eb RK |
8556 | |
8557 | DECL_TEMPLATE_RESULT (r) = new_decl; | |
ae58fa02 MM |
8558 | DECL_TI_TEMPLATE (new_decl) = r; |
8559 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
8560 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
3db45ab5 | 8561 | DECL_CONTEXT (r) = DECL_CONTEXT (new_decl); |
93cdc044 JM |
8562 | } |
8563 | ||
ae58fa02 MM |
8564 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
8565 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
8566 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
8567 | |
8568 | /* The template parameters for this new template are all the | |
8569 | template parameters for the old template, except the | |
c6002625 | 8570 | outermost level of parameters. */ |
c8094d83 | 8571 | DECL_TEMPLATE_PARMS (r) |
4393e105 | 8572 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
c2ea3a40 | 8573 | complain); |
98c1c668 | 8574 | |
93cdc044 | 8575 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 8576 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 8577 | |
8c6ab2db NS |
8578 | if (TREE_CODE (decl) != TYPE_DECL) |
8579 | /* Record this non-type partial instantiation. */ | |
c8094d83 | 8580 | register_specialization (r, t, |
d63d5d0c | 8581 | DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)), |
2b110bfc | 8582 | false, hash); |
98c1c668 | 8583 | } |
ae58fa02 | 8584 | break; |
8d08fdba MS |
8585 | |
8586 | case FUNCTION_DECL: | |
8587 | { | |
386b8a85 | 8588 | tree ctx; |
87603ed0 | 8589 | tree argvec = NULL_TREE; |
cf38f48a | 8590 | tree *friends; |
36a117a5 | 8591 | tree gen_tmpl; |
fc6a28d7 | 8592 | tree type; |
5566b478 | 8593 | int member; |
d8c4447d MM |
8594 | int args_depth; |
8595 | int parms_depth; | |
5566b478 | 8596 | |
36a117a5 | 8597 | /* Nobody should be tsubst'ing into non-template functions. */ |
50bc768d | 8598 | gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE); |
36a117a5 MM |
8599 | |
8600 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
8601 | { | |
8602 | tree spec; | |
00cf3e31 MM |
8603 | bool dependent_p; |
8604 | ||
8605 | /* If T is not dependent, just return it. We have to | |
8606 | increment PROCESSING_TEMPLATE_DECL because | |
8607 | value_dependent_expression_p assumes that nothing is | |
8608 | dependent when PROCESSING_TEMPLATE_DECL is zero. */ | |
8609 | ++processing_template_decl; | |
8610 | dependent_p = value_dependent_expression_p (t); | |
8611 | --processing_template_decl; | |
8612 | if (!dependent_p) | |
8613 | return t; | |
36a117a5 MM |
8614 | |
8615 | /* Calculate the most general template of which R is a | |
8616 | specialization, and the complete set of arguments used to | |
8617 | specialize R. */ | |
8618 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
c8094d83 | 8619 | argvec = tsubst_template_args (DECL_TI_ARGS |
a91db711 | 8620 | (DECL_TEMPLATE_RESULT (gen_tmpl)), |
c8094d83 | 8621 | args, complain, in_decl); |
36a117a5 MM |
8622 | |
8623 | /* Check to see if we already have this specialization. */ | |
2b110bfc JM |
8624 | hash = hash_tmpl_and_args (gen_tmpl, argvec); |
8625 | spec = retrieve_specialization (gen_tmpl, argvec, hash); | |
7ddedda4 | 8626 | |
36a117a5 | 8627 | if (spec) |
ae58fa02 MM |
8628 | { |
8629 | r = spec; | |
8630 | break; | |
8631 | } | |
d8c4447d | 8632 | |
f9a7ae04 MM |
8633 | /* We can see more levels of arguments than parameters if |
8634 | there was a specialization of a member template, like | |
8635 | this: | |
8636 | ||
0cbd7506 | 8637 | template <class T> struct S { template <class U> void f(); } |
c8094d83 | 8638 | template <> template <class U> void S<int>::f(U); |
f9a7ae04 | 8639 | |
dc957d14 | 8640 | Here, we'll be substituting into the specialization, |
f9a7ae04 MM |
8641 | because that's where we can find the code we actually |
8642 | want to generate, but we'll have enough arguments for | |
c8094d83 | 8643 | the most general template. |
f9a7ae04 MM |
8644 | |
8645 | We also deal with the peculiar case: | |
d8c4447d | 8646 | |
c8094d83 | 8647 | template <class T> struct S { |
d8c4447d MM |
8648 | template <class U> friend void f(); |
8649 | }; | |
74b846e0 | 8650 | template <class U> void f() {} |
d8c4447d MM |
8651 | template S<int>; |
8652 | template void f<double>(); | |
8653 | ||
8654 | Here, the ARGS for the instantiation of will be {int, | |
8655 | double}. But, we only need as many ARGS as there are | |
8656 | levels of template parameters in CODE_PATTERN. We are | |
8657 | careful not to get fooled into reducing the ARGS in | |
8658 | situations like: | |
8659 | ||
8660 | template <class T> struct S { template <class U> void f(U); } | |
8661 | template <class T> template <> void S<T>::f(int) {} | |
8662 | ||
8663 | which we can spot because the pattern will be a | |
8664 | specialization in this case. */ | |
8665 | args_depth = TMPL_ARGS_DEPTH (args); | |
c8094d83 MS |
8666 | parms_depth = |
8667 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
d8c4447d MM |
8668 | if (args_depth > parms_depth |
8669 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
f9a7ae04 | 8670 | args = get_innermost_template_args (args, parms_depth); |
36a117a5 MM |
8671 | } |
8672 | else | |
8673 | { | |
8674 | /* This special case arises when we have something like this: | |
8675 | ||
0cbd7506 | 8676 | template <class T> struct S { |
c8094d83 | 8677 | friend void f<int>(int, double); |
36a117a5 MM |
8678 | }; |
8679 | ||
8680 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
10b1d5e7 MM |
8681 | will be an IDENTIFIER_NODE. We are being called from |
8682 | tsubst_friend_function, and we want only to create a | |
8683 | new decl (R) with appropriate types so that we can call | |
8684 | determine_specialization. */ | |
36a117a5 MM |
8685 | gen_tmpl = NULL_TREE; |
8686 | } | |
8687 | ||
6eb3bb27 | 8688 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 8689 | { |
5566b478 MS |
8690 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
8691 | member = 2; | |
8692 | else | |
8693 | member = 1; | |
c8094d83 | 8694 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
8c6ab2db | 8695 | complain, t, /*entering_scope=*/1); |
5566b478 MS |
8696 | } |
8697 | else | |
8698 | { | |
8699 | member = 0; | |
4f1c5b7d | 8700 | ctx = DECL_CONTEXT (t); |
5566b478 | 8701 | } |
fc6a28d7 | 8702 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
b3da7bb1 MM |
8703 | if (type == error_mark_node) |
8704 | return error_mark_node; | |
8d08fdba | 8705 | |
5566b478 | 8706 | /* We do NOT check for matching decls pushed separately at this |
0cbd7506 MS |
8707 | point, as they may not represent instantiations of this |
8708 | template, and in any case are considered separate under the | |
8709 | discrete model. */ | |
0acf7199 | 8710 | r = copy_decl (t); |
e1467ff2 | 8711 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 | 8712 | TREE_TYPE (r) = type; |
92643fea MM |
8713 | /* Clear out the mangled name and RTL for the instantiation. */ |
8714 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
8715 | SET_DECL_RTL (r, NULL_RTX); | |
9b26c96e JM |
8716 | /* Leave DECL_INITIAL set on deleted instantiations. */ |
8717 | if (!DECL_DELETED_FN (r)) | |
8718 | DECL_INITIAL (r) = NULL_TREE; | |
4f1c5b7d | 8719 | DECL_CONTEXT (r) = ctx; |
5566b478 | 8720 | |
c8094d83 | 8721 | if (member && DECL_CONV_FN_P (r)) |
1f84ec23 MM |
8722 | /* Type-conversion operator. Reconstruct the name, in |
8723 | case it's the name of one of the template's parameters. */ | |
8724 | DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); | |
5566b478 | 8725 | |
4393e105 | 8726 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
c2ea3a40 | 8727 | complain, t); |
477f6664 | 8728 | DECL_RESULT (r) = NULL_TREE; |
711734a9 JM |
8729 | |
8730 | TREE_STATIC (r) = 0; | |
8731 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
8732 | DECL_EXTERNAL (r) = 1; | |
4684cd27 MM |
8733 | /* If this is an instantiation of a function with internal |
8734 | linkage, we already know what object file linkage will be | |
8735 | assigned to the instantiation. */ | |
8736 | DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r); | |
711734a9 JM |
8737 | DECL_DEFER_OUTPUT (r) = 0; |
8738 | TREE_CHAIN (r) = NULL_TREE; | |
8739 | DECL_PENDING_INLINE_INFO (r) = 0; | |
59026e79 | 8740 | DECL_PENDING_INLINE_P (r) = 0; |
655dc6ee | 8741 | DECL_SAVED_TREE (r) = NULL_TREE; |
29d7f409 | 8742 | DECL_STRUCT_FUNCTION (r) = NULL; |
711734a9 | 8743 | TREE_USED (r) = 0; |
b97e8a14 JM |
8744 | /* We'll re-clone as appropriate in instantiate_template. */ |
8745 | DECL_CLONED_FUNCTION (r) = NULL_TREE; | |
711734a9 | 8746 | |
92643fea MM |
8747 | /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do |
8748 | this in the special friend case mentioned above where | |
8749 | GEN_TMPL is NULL. */ | |
36a117a5 | 8750 | if (gen_tmpl) |
386b8a85 | 8751 | { |
c8094d83 | 8752 | DECL_TEMPLATE_INFO (r) |
e1b3e07d | 8753 | = tree_cons (gen_tmpl, argvec, NULL_TREE); |
36a117a5 | 8754 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
2b110bfc | 8755 | register_specialization (r, gen_tmpl, argvec, false, hash); |
36a117a5 | 8756 | |
9188c363 MM |
8757 | /* We're not supposed to instantiate default arguments |
8758 | until they are called, for a template. But, for a | |
8759 | declaration like: | |
8760 | ||
0cbd7506 MS |
8761 | template <class T> void f () |
8762 | { extern void g(int i = T()); } | |
c8094d83 | 8763 | |
9188c363 MM |
8764 | we should do the substitution when the template is |
8765 | instantiated. We handle the member function case in | |
8766 | instantiate_class_template since the default arguments | |
8767 | might refer to other members of the class. */ | |
8768 | if (!member | |
8769 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
8770 | && !uses_template_parms (argvec)) | |
8771 | tsubst_default_arguments (r); | |
386b8a85 | 8772 | } |
c43e95f8 MM |
8773 | else |
8774 | DECL_TEMPLATE_INFO (r) = NULL_TREE; | |
f181d4ae | 8775 | |
cf38f48a MM |
8776 | /* Copy the list of befriending classes. */ |
8777 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
8778 | *friends; | |
c8094d83 | 8779 | friends = &TREE_CHAIN (*friends)) |
cf38f48a MM |
8780 | { |
8781 | *friends = copy_node (*friends); | |
8782 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
c2ea3a40 | 8783 | args, complain, |
cf38f48a MM |
8784 | in_decl); |
8785 | } | |
8786 | ||
212e7048 | 8787 | if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) |
f181d4ae MM |
8788 | { |
8789 | maybe_retrofit_in_chrg (r); | |
212e7048 MM |
8790 | if (DECL_CONSTRUCTOR_P (r)) |
8791 | grok_ctor_properties (ctx, r); | |
2be678ff JM |
8792 | /* If this is an instantiation of a member template, clone it. |
8793 | If it isn't, that'll be handled by | |
8794 | clone_constructors_and_destructors. */ | |
5e818b93 | 8795 | if (PRIMARY_TEMPLATE_P (gen_tmpl)) |
271e6f02 | 8796 | clone_function_decl (r, /*update_method_vec_p=*/0); |
f181d4ae | 8797 | } |
398cd199 VR |
8798 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)) |
8799 | && !grok_op_properties (r, (complain & tf_error) != 0)) | |
8800 | return error_mark_node; | |
c8b2e872 MM |
8801 | |
8802 | if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t)) | |
8803 | SET_DECL_FRIEND_CONTEXT (r, | |
8804 | tsubst (DECL_FRIEND_CONTEXT (t), | |
8805 | args, complain, in_decl)); | |
b9e75696 JM |
8806 | |
8807 | /* Possibly limit visibility based on template args. */ | |
8808 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
8809 | if (DECL_VISIBILITY_SPECIFIED (t)) |
8810 | { | |
8811 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
8812 | DECL_ATTRIBUTES (r) | |
8813 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
8814 | } | |
b9e75696 | 8815 | determine_visibility (r); |
4f4141ff JM |
8816 | |
8817 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, | |
8818 | args, complain, in_decl); | |
8d08fdba | 8819 | } |
ae58fa02 | 8820 | break; |
8d08fdba MS |
8821 | |
8822 | case PARM_DECL: | |
8823 | { | |
5d80a306 DG |
8824 | tree type = NULL_TREE; |
8825 | int i, len = 1; | |
8826 | tree expanded_types = NULL_TREE; | |
8827 | tree prev_r = NULL_TREE; | |
8828 | tree first_r = NULL_TREE; | |
1b8899d1 | 8829 | |
5d80a306 DG |
8830 | if (FUNCTION_PARAMETER_PACK_P (t)) |
8831 | { | |
8832 | /* If there is a local specialization that isn't a | |
8833 | parameter pack, it means that we're doing a "simple" | |
8834 | substitution from inside tsubst_pack_expansion. Just | |
a757b5c5 | 8835 | return the local specialization (which will be a single |
5d80a306 | 8836 | parm). */ |
29b0d1fd | 8837 | tree spec = retrieve_local_specialization (t); |
5d80a306 DG |
8838 | if (spec |
8839 | && TREE_CODE (spec) == PARM_DECL | |
8840 | && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION) | |
8841 | return spec; | |
8842 | ||
8843 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
8844 | the parameters in this function parameter pack. */ | |
8845 | expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args, | |
8846 | complain, in_decl); | |
8847 | if (TREE_CODE (expanded_types) == TREE_VEC) | |
8848 | { | |
8849 | len = TREE_VEC_LENGTH (expanded_types); | |
8850 | ||
8851 | /* Zero-length parameter packs are boring. Just substitute | |
8852 | into the chain. */ | |
8853 | if (len == 0) | |
8854 | return tsubst (TREE_CHAIN (t), args, complain, | |
8855 | TREE_CHAIN (t)); | |
8856 | } | |
8857 | else | |
8858 | { | |
8859 | /* All we did was update the type. Make a note of that. */ | |
8860 | type = expanded_types; | |
8861 | expanded_types = NULL_TREE; | |
8862 | } | |
8863 | } | |
db2767b6 | 8864 | |
5d80a306 DG |
8865 | /* Loop through all of the parameter's we'll build. When T is |
8866 | a function parameter pack, LEN is the number of expanded | |
8867 | types in EXPANDED_TYPES; otherwise, LEN is 1. */ | |
8868 | r = NULL_TREE; | |
8869 | for (i = 0; i < len; ++i) | |
8870 | { | |
8871 | prev_r = r; | |
8872 | r = copy_node (t); | |
8873 | if (DECL_TEMPLATE_PARM_P (t)) | |
8874 | SET_DECL_TEMPLATE_PARM_P (r); | |
8875 | ||
d40a19da DS |
8876 | /* An argument of a function parameter pack is not a parameter |
8877 | pack. */ | |
8878 | FUNCTION_PARAMETER_PACK_P (r) = false; | |
8879 | ||
5d80a306 DG |
8880 | if (expanded_types) |
8881 | /* We're on the Ith parameter of the function parameter | |
8882 | pack. */ | |
8883 | { | |
8884 | /* Get the Ith type. */ | |
8885 | type = TREE_VEC_ELT (expanded_types, i); | |
8886 | ||
8887 | if (DECL_NAME (r)) | |
8888 | /* Rename the parameter to include the index. */ | |
8889 | DECL_NAME (r) = | |
8890 | make_ith_pack_parameter_name (DECL_NAME (r), i); | |
8891 | } | |
8892 | else if (!type) | |
8893 | /* We're dealing with a normal parameter. */ | |
8894 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
8895 | ||
8896 | type = type_decays_to (type); | |
8897 | TREE_TYPE (r) = type; | |
8898 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); | |
8899 | ||
8900 | if (DECL_INITIAL (r)) | |
8901 | { | |
8902 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) | |
8903 | DECL_INITIAL (r) = TREE_TYPE (r); | |
8904 | else | |
8905 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, | |
8906 | complain, in_decl); | |
8907 | } | |
8908 | ||
8909 | DECL_CONTEXT (r) = NULL_TREE; | |
8910 | ||
8911 | if (!DECL_TEMPLATE_PARM_P (r)) | |
8912 | DECL_ARG_TYPE (r) = type_passed_as (type); | |
8913 | ||
4f4141ff JM |
8914 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, |
8915 | args, complain, in_decl); | |
8916 | ||
5d80a306 DG |
8917 | /* Keep track of the first new parameter we |
8918 | generate. That's what will be returned to the | |
8919 | caller. */ | |
8920 | if (!first_r) | |
8921 | first_r = r; | |
8922 | ||
8923 | /* Build a proper chain of parameters when substituting | |
8924 | into a function parameter pack. */ | |
8925 | if (prev_r) | |
8926 | TREE_CHAIN (prev_r) = r; | |
8927 | } | |
8e51619a | 8928 | |
8d08fdba | 8929 | if (TREE_CHAIN (t)) |
4393e105 | 8930 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
c2ea3a40 | 8931 | complain, TREE_CHAIN (t)); |
5d80a306 DG |
8932 | |
8933 | /* FIRST_R contains the start of the chain we've built. */ | |
8934 | r = first_r; | |
8d08fdba | 8935 | } |
ae58fa02 | 8936 | break; |
8d08fdba | 8937 | |
5566b478 MS |
8938 | case FIELD_DECL: |
8939 | { | |
fc6a28d7 MM |
8940 | tree type; |
8941 | ||
0acf7199 | 8942 | r = copy_decl (t); |
fc6a28d7 MM |
8943 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
8944 | if (type == error_mark_node) | |
8945 | return error_mark_node; | |
1b8899d1 | 8946 | TREE_TYPE (r) = type; |
9804209d | 8947 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
1b8899d1 | 8948 | |
015c2c66 MM |
8949 | /* DECL_INITIAL gives the number of bits in a bit-field. */ |
8950 | DECL_INITIAL (r) | |
8951 | = tsubst_expr (DECL_INITIAL (t), args, | |
8952 | complain, in_decl, | |
8953 | /*integral_constant_expression_p=*/true); | |
1b8899d1 MM |
8954 | /* We don't have to set DECL_CONTEXT here; it is set by |
8955 | finish_member_declaration. */ | |
5566b478 | 8956 | TREE_CHAIN (r) = NULL_TREE; |
c8094d83 | 8957 | if (VOID_TYPE_P (type)) |
dee15844 | 8958 | error ("instantiation of %q+D as type %qT", r, type); |
4f4141ff JM |
8959 | |
8960 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, | |
8961 | args, complain, in_decl); | |
5566b478 | 8962 | } |
ae58fa02 | 8963 | break; |
5566b478 MS |
8964 | |
8965 | case USING_DECL: | |
98ed9dae NS |
8966 | /* We reach here only for member using decls. */ |
8967 | if (DECL_DEPENDENT_P (t)) | |
8968 | { | |
8969 | r = do_class_using_decl | |
8970 | (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl), | |
8971 | tsubst_copy (DECL_NAME (t), args, complain, in_decl)); | |
8972 | if (!r) | |
8973 | r = error_mark_node; | |
db8470f6 JM |
8974 | else |
8975 | { | |
8976 | TREE_PROTECTED (r) = TREE_PROTECTED (t); | |
8977 | TREE_PRIVATE (r) = TREE_PRIVATE (t); | |
8978 | } | |
98ed9dae NS |
8979 | } |
8980 | else | |
8981 | { | |
8982 | r = copy_node (t); | |
8983 | TREE_CHAIN (r) = NULL_TREE; | |
8984 | } | |
ae58fa02 | 8985 | break; |
5566b478 | 8986 | |
9188c363 | 8987 | case TYPE_DECL: |
5566b478 MS |
8988 | case VAR_DECL: |
8989 | { | |
1cea0434 KG |
8990 | tree argvec = NULL_TREE; |
8991 | tree gen_tmpl = NULL_TREE; | |
36a117a5 | 8992 | tree spec; |
1cea0434 | 8993 | tree tmpl = NULL_TREE; |
9188c363 | 8994 | tree ctx; |
fc6a28d7 | 8995 | tree type = NULL_TREE; |
f604fc3b | 8996 | bool local_p; |
9188c363 | 8997 | |
56d0c6e3 | 8998 | if (TREE_CODE (t) == TYPE_DECL |
a7f6bc8c | 8999 | && t == TYPE_MAIN_DECL (TREE_TYPE (t))) |
fc6a28d7 | 9000 | { |
a7f6bc8c JM |
9001 | /* If this is the canonical decl, we don't have to |
9002 | mess with instantiations, and often we can't (for | |
9003 | typename, template type parms and such). Note that | |
9004 | TYPE_NAME is not correct for the above test if | |
9005 | we've copied the type for a typedef. */ | |
fc6a28d7 | 9006 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
c343d5a7 PC |
9007 | if (type == error_mark_node) |
9008 | return error_mark_node; | |
a7f6bc8c JM |
9009 | r = TYPE_NAME (type); |
9010 | break; | |
fc6a28d7 | 9011 | } |
c8094d83 | 9012 | |
f604fc3b MM |
9013 | /* Check to see if we already have the specialization we |
9014 | need. */ | |
9015 | spec = NULL_TREE; | |
9016 | if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)) | |
9017 | { | |
9018 | /* T is a static data member or namespace-scope entity. | |
9019 | We have to substitute into namespace-scope variables | |
9020 | (even though such entities are never templates) because | |
9021 | of cases like: | |
9022 | ||
9023 | template <class T> void f() { extern T t; } | |
9024 | ||
9025 | where the entity referenced is not known until | |
9026 | instantiation time. */ | |
9027 | local_p = false; | |
9028 | ctx = DECL_CONTEXT (t); | |
9029 | if (DECL_CLASS_SCOPE_P (t)) | |
9030 | { | |
9031 | ctx = tsubst_aggr_type (ctx, args, | |
9032 | complain, | |
9033 | in_decl, /*entering_scope=*/1); | |
9034 | /* If CTX is unchanged, then T is in fact the | |
9035 | specialization we want. That situation occurs when | |
9036 | referencing a static data member within in its own | |
9037 | class. We can use pointer equality, rather than | |
9038 | same_type_p, because DECL_CONTEXT is always | |
9039 | canonical. */ | |
9040 | if (ctx == DECL_CONTEXT (t)) | |
9041 | spec = t; | |
9042 | } | |
5566b478 | 9043 | |
f604fc3b MM |
9044 | if (!spec) |
9045 | { | |
9046 | tmpl = DECL_TI_TEMPLATE (t); | |
9047 | gen_tmpl = most_general_template (tmpl); | |
9048 | argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); | |
2b110bfc JM |
9049 | hash = hash_tmpl_and_args (gen_tmpl, argvec); |
9050 | spec = retrieve_specialization (gen_tmpl, argvec, hash); | |
f604fc3b MM |
9051 | } |
9052 | } | |
9188c363 | 9053 | else |
6dfbb909 | 9054 | { |
f604fc3b MM |
9055 | /* A local variable. */ |
9056 | local_p = true; | |
6dfbb909 MM |
9057 | /* Subsequent calls to pushdecl will fill this in. */ |
9058 | ctx = NULL_TREE; | |
f604fc3b | 9059 | spec = retrieve_local_specialization (t); |
6dfbb909 | 9060 | } |
f604fc3b MM |
9061 | /* If we already have the specialization we need, there is |
9062 | nothing more to do. */ | |
36a117a5 | 9063 | if (spec) |
ae58fa02 MM |
9064 | { |
9065 | r = spec; | |
9066 | break; | |
9067 | } | |
5566b478 | 9068 | |
f604fc3b | 9069 | /* Create a new node for the specialization we need. */ |
0acf7199 | 9070 | r = copy_decl (t); |
56d0c6e3 JM |
9071 | if (type == NULL_TREE) |
9072 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
edebf865 | 9073 | if (TREE_CODE (r) == VAR_DECL) |
39703eb9 | 9074 | { |
8b0a8500 MM |
9075 | /* Even if the original location is out of scope, the |
9076 | newly substituted one is not. */ | |
9077 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
9078 | DECL_INITIALIZED_P (r) = 0; | |
9079 | DECL_TEMPLATE_INSTANTIATED (r) = 0; | |
fc6a28d7 MM |
9080 | if (type == error_mark_node) |
9081 | return error_mark_node; | |
2c05d05e MM |
9082 | if (TREE_CODE (type) == FUNCTION_TYPE) |
9083 | { | |
9084 | /* It may seem that this case cannot occur, since: | |
9085 | ||
9086 | typedef void f(); | |
9087 | void g() { f x; } | |
9088 | ||
9089 | declares a function, not a variable. However: | |
9090 | ||
9091 | typedef void f(); | |
9092 | template <typename T> void g() { T t; } | |
9093 | template void g<f>(); | |
9094 | ||
9095 | is an attempt to declare a variable with function | |
9096 | type. */ | |
9097 | error ("variable %qD has function type", | |
9098 | /* R is not yet sufficiently initialized, so we | |
9099 | just use its name. */ | |
9100 | DECL_NAME (r)); | |
9101 | return error_mark_node; | |
9102 | } | |
39703eb9 MM |
9103 | type = complete_type (type); |
9104 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) | |
9105 | = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t); | |
62e19030 | 9106 | type = check_var_type (DECL_NAME (r), type); |
56b4ea3d RH |
9107 | |
9108 | if (DECL_HAS_VALUE_EXPR_P (t)) | |
9109 | { | |
9110 | tree ve = DECL_VALUE_EXPR (t); | |
015c2c66 MM |
9111 | ve = tsubst_expr (ve, args, complain, in_decl, |
9112 | /*constant_expression_p=*/false); | |
3db45ab5 | 9113 | SET_DECL_VALUE_EXPR (r, ve); |
56b4ea3d | 9114 | } |
39703eb9 | 9115 | } |
a3d87771 MM |
9116 | else if (DECL_SELF_REFERENCE_P (t)) |
9117 | SET_DECL_SELF_REFERENCE_P (r); | |
01f4137f | 9118 | TREE_TYPE (r) = type; |
9804209d | 9119 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
5566b478 | 9120 | DECL_CONTEXT (r) = ctx; |
92643fea MM |
9121 | /* Clear out the mangled name and RTL for the instantiation. */ |
9122 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
820cc88f DB |
9123 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
9124 | SET_DECL_RTL (r, NULL_RTX); | |
8b0a8500 MM |
9125 | /* The initializer must not be expanded until it is required; |
9126 | see [temp.inst]. */ | |
d11ad92e | 9127 | DECL_INITIAL (r) = NULL_TREE; |
820cc88f DB |
9128 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
9129 | SET_DECL_RTL (r, NULL_RTX); | |
06ceef4e | 9130 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; |
b9e75696 JM |
9131 | if (TREE_CODE (r) == VAR_DECL) |
9132 | { | |
9133 | /* Possibly limit visibility based on template args. */ | |
9134 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
9135 | if (DECL_VISIBILITY_SPECIFIED (t)) |
9136 | { | |
9137 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
9138 | DECL_ATTRIBUTES (r) | |
9139 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
9140 | } | |
b9e75696 JM |
9141 | determine_visibility (r); |
9142 | } | |
5044ab0e JM |
9143 | /* Preserve a typedef that names a type. */ |
9144 | else if (TREE_CODE (r) == TYPE_DECL | |
9145 | && DECL_ORIGINAL_TYPE (t) | |
9146 | && type != error_mark_node) | |
9147 | { | |
9148 | DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t), | |
9149 | args, complain, in_decl); | |
9150 | TREE_TYPE (r) = type = build_variant_type_copy (type); | |
9151 | TYPE_NAME (type) = r; | |
9152 | } | |
5566b478 | 9153 | |
6dfbb909 MM |
9154 | if (!local_p) |
9155 | { | |
9156 | /* A static data member declaration is always marked | |
9157 | external when it is declared in-class, even if an | |
9158 | initializer is present. We mimic the non-template | |
9159 | processing here. */ | |
9160 | DECL_EXTERNAL (r) = 1; | |
fa8d6e85 | 9161 | |
2b110bfc | 9162 | register_specialization (r, gen_tmpl, argvec, false, hash); |
6dfbb909 MM |
9163 | DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); |
9164 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
9165 | } | |
d5f4eddd JM |
9166 | else if (cp_unevaluated_operand) |
9167 | { | |
9168 | /* We're substituting this var in a decltype outside of its | |
9169 | scope, such as for a lambda return type. Don't add it to | |
9170 | local_specializations, do perform auto deduction. */ | |
9171 | tree auto_node = type_uses_auto (type); | |
9172 | tree init | |
9173 | = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl, | |
9174 | /*constant_expression_p=*/false); | |
9175 | ||
9176 | if (auto_node && init && describable_type (init)) | |
9177 | { | |
9178 | type = do_auto_deduction (type, init, auto_node); | |
9179 | TREE_TYPE (r) = type; | |
9180 | } | |
9181 | } | |
9188c363 | 9182 | else |
6dfbb909 | 9183 | register_local_specialization (r, t); |
5566b478 | 9184 | |
5566b478 | 9185 | TREE_CHAIN (r) = NULL_TREE; |
4f4141ff | 9186 | |
5044ab0e JM |
9187 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), |
9188 | (int) ATTR_FLAG_TYPE_IN_PLACE, | |
4f4141ff | 9189 | args, complain, in_decl); |
edebf865 | 9190 | layout_decl (r, 0); |
5566b478 | 9191 | } |
ae58fa02 | 9192 | break; |
5566b478 | 9193 | |
ae58fa02 | 9194 | default: |
315fb5db | 9195 | gcc_unreachable (); |
c8094d83 | 9196 | } |
ae58fa02 MM |
9197 | |
9198 | /* Restore the file and line information. */ | |
82a98427 | 9199 | input_location = saved_loc; |
ae58fa02 MM |
9200 | |
9201 | return r; | |
9202 | } | |
9203 | ||
34cd5ae7 | 9204 | /* Substitute into the ARG_TYPES of a function type. */ |
cabc336a | 9205 | |
e9659ab0 | 9206 | static tree |
c8094d83 | 9207 | tsubst_arg_types (tree arg_types, |
0cbd7506 MS |
9208 | tree args, |
9209 | tsubst_flags_t complain, | |
9210 | tree in_decl) | |
cabc336a MM |
9211 | { |
9212 | tree remaining_arg_types; | |
5d80a306 DG |
9213 | tree type = NULL_TREE; |
9214 | int i = 1; | |
9215 | tree expanded_args = NULL_TREE; | |
5e97d404 | 9216 | tree default_arg; |
cabc336a MM |
9217 | |
9218 | if (!arg_types || arg_types == void_list_node) | |
9219 | return arg_types; | |
c8094d83 | 9220 | |
cabc336a | 9221 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), |
4393e105 MM |
9222 | args, complain, in_decl); |
9223 | if (remaining_arg_types == error_mark_node) | |
9224 | return error_mark_node; | |
9225 | ||
5d80a306 | 9226 | if (PACK_EXPANSION_P (TREE_VALUE (arg_types))) |
4b2811e9 | 9227 | { |
5d80a306 DG |
9228 | /* For a pack expansion, perform substitution on the |
9229 | entire expression. Later on, we'll handle the arguments | |
9230 | one-by-one. */ | |
9231 | expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types), | |
9232 | args, complain, in_decl); | |
cabc336a | 9233 | |
5d80a306 DG |
9234 | if (TREE_CODE (expanded_args) == TREE_VEC) |
9235 | /* So that we'll spin through the parameters, one by one. */ | |
9236 | i = TREE_VEC_LENGTH (expanded_args); | |
9237 | else | |
9238 | { | |
9239 | /* We only partially substituted into the parameter | |
9240 | pack. Our type is TYPE_PACK_EXPANSION. */ | |
9241 | type = expanded_args; | |
9242 | expanded_args = NULL_TREE; | |
9243 | } | |
9244 | } | |
cabc336a | 9245 | |
5d80a306 DG |
9246 | while (i > 0) { |
9247 | --i; | |
9248 | ||
9249 | if (expanded_args) | |
9250 | type = TREE_VEC_ELT (expanded_args, i); | |
9251 | else if (!type) | |
9252 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
c8094d83 | 9253 | |
5d80a306 DG |
9254 | if (type == error_mark_node) |
9255 | return error_mark_node; | |
9256 | if (VOID_TYPE_P (type)) | |
9257 | { | |
9258 | if (complain & tf_error) | |
9259 | { | |
9260 | error ("invalid parameter type %qT", type); | |
9261 | if (in_decl) | |
9262 | error ("in declaration %q+D", in_decl); | |
9263 | } | |
9264 | return error_mark_node; | |
5e97d404 | 9265 | } |
5d80a306 DG |
9266 | |
9267 | /* Do array-to-pointer, function-to-pointer conversion, and ignore | |
9268 | top-level qualifiers as required. */ | |
9269 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
c8094d83 | 9270 | |
5d80a306 DG |
9271 | /* We do not substitute into default arguments here. The standard |
9272 | mandates that they be instantiated only when needed, which is | |
9273 | done in build_over_call. */ | |
9274 | default_arg = TREE_PURPOSE (arg_types); | |
9275 | ||
9276 | if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG) | |
9277 | { | |
9278 | /* We've instantiated a template before its default arguments | |
9279 | have been parsed. This can happen for a nested template | |
9280 | class, and is not an error unless we require the default | |
9281 | argument in a call of this function. */ | |
9282 | remaining_arg_types = | |
9283 | tree_cons (default_arg, type, remaining_arg_types); | |
9284 | VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), | |
9285 | remaining_arg_types); | |
9286 | } | |
9287 | else | |
9288 | remaining_arg_types = | |
9289 | hash_tree_cons (default_arg, type, remaining_arg_types); | |
9290 | } | |
9291 | ||
9292 | return remaining_arg_types; | |
cabc336a MM |
9293 | } |
9294 | ||
4393e105 MM |
9295 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
9296 | *not* handle the exception-specification for FNTYPE, because the | |
9297 | initial substitution of explicitly provided template parameters | |
9298 | during argument deduction forbids substitution into the | |
9299 | exception-specification: | |
9300 | ||
9301 | [temp.deduct] | |
9302 | ||
9303 | All references in the function type of the function template to the | |
9304 | corresponding template parameters are replaced by the specified tem- | |
9305 | plate argument values. If a substitution in a template parameter or | |
9306 | in the function type of the function template results in an invalid | |
9307 | type, type deduction fails. [Note: The equivalent substitution in | |
9308 | exception specifications is done only when the function is instanti- | |
9309 | ated, at which point a program is ill-formed if the substitution | |
9310 | results in an invalid type.] */ | |
9311 | ||
9312 | static tree | |
c8094d83 | 9313 | tsubst_function_type (tree t, |
0cbd7506 MS |
9314 | tree args, |
9315 | tsubst_flags_t complain, | |
9316 | tree in_decl) | |
4393e105 MM |
9317 | { |
9318 | tree return_type; | |
9319 | tree arg_types; | |
9320 | tree fntype; | |
9321 | ||
8dd3f57a | 9322 | /* The TYPE_CONTEXT is not used for function/method types. */ |
50bc768d | 9323 | gcc_assert (TYPE_CONTEXT (t) == NULL_TREE); |
4393e105 | 9324 | |
46c895ac | 9325 | /* Substitute the return type. */ |
4393e105 MM |
9326 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
9327 | if (return_type == error_mark_node) | |
9328 | return error_mark_node; | |
6e2993bf MM |
9329 | /* The standard does not presently indicate that creation of a |
9330 | function type with an invalid return type is a deduction failure. | |
270af55d | 9331 | However, that is clearly analogous to creating an array of "void" |
c8094d83 | 9332 | or a reference to a reference. This is core issue #486. */ |
6e2993bf MM |
9333 | if (TREE_CODE (return_type) == ARRAY_TYPE |
9334 | || TREE_CODE (return_type) == FUNCTION_TYPE) | |
9335 | { | |
9336 | if (complain & tf_error) | |
9337 | { | |
9338 | if (TREE_CODE (return_type) == ARRAY_TYPE) | |
9339 | error ("function returning an array"); | |
9340 | else | |
9341 | error ("function returning a function"); | |
9342 | } | |
9343 | return error_mark_node; | |
9344 | } | |
4393e105 | 9345 | |
34cd5ae7 | 9346 | /* Substitute the argument types. */ |
4393e105 | 9347 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, |
c8094d83 | 9348 | complain, in_decl); |
4393e105 MM |
9349 | if (arg_types == error_mark_node) |
9350 | return error_mark_node; | |
c8094d83 | 9351 | |
4393e105 MM |
9352 | /* Construct a new type node and return it. */ |
9353 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
9354 | fntype = build_function_type (return_type, arg_types); | |
9355 | else | |
9356 | { | |
9357 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
9e1e64ec | 9358 | if (! MAYBE_CLASS_TYPE_P (r)) |
4393e105 MM |
9359 | { |
9360 | /* [temp.deduct] | |
c8094d83 | 9361 | |
4393e105 MM |
9362 | Type deduction may fail for any of the following |
9363 | reasons: | |
c8094d83 | 9364 | |
4393e105 MM |
9365 | -- Attempting to create "pointer to member of T" when T |
9366 | is not a class type. */ | |
c2ea3a40 | 9367 | if (complain & tf_error) |
0f51ccfc | 9368 | error ("creating pointer to member function of non-class type %qT", |
4393e105 MM |
9369 | r); |
9370 | return error_mark_node; | |
9371 | } | |
c8094d83 MS |
9372 | |
9373 | fntype = build_method_type_directly (r, return_type, | |
43dc123f | 9374 | TREE_CHAIN (arg_types)); |
4393e105 | 9375 | } |
c2ea3a40 | 9376 | fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain); |
e9525111 | 9377 | fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
c8094d83 MS |
9378 | |
9379 | return fntype; | |
4393e105 MM |
9380 | } |
9381 | ||
c7222c02 MM |
9382 | /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template |
9383 | ARGS into that specification, and return the substituted | |
9384 | specification. If there is no specification, return NULL_TREE. */ | |
9385 | ||
9386 | static tree | |
c8094d83 MS |
9387 | tsubst_exception_specification (tree fntype, |
9388 | tree args, | |
c7222c02 MM |
9389 | tsubst_flags_t complain, |
9390 | tree in_decl) | |
9391 | { | |
9392 | tree specs; | |
9393 | tree new_specs; | |
9394 | ||
9395 | specs = TYPE_RAISES_EXCEPTIONS (fntype); | |
9396 | new_specs = NULL_TREE; | |
9397 | if (specs) | |
9398 | { | |
9399 | if (! TREE_VALUE (specs)) | |
9400 | new_specs = specs; | |
9401 | else | |
9402 | while (specs) | |
9403 | { | |
9404 | tree spec; | |
5d80a306 DG |
9405 | int i, len = 1; |
9406 | tree expanded_specs = NULL_TREE; | |
9407 | ||
9408 | if (PACK_EXPANSION_P (TREE_VALUE (specs))) | |
9409 | { | |
9410 | /* Expand the pack expansion type. */ | |
9411 | expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs), | |
9412 | args, complain, | |
9413 | in_decl); | |
2f93f02c DG |
9414 | |
9415 | if (expanded_specs == error_mark_node) | |
9416 | return error_mark_node; | |
9417 | else if (TREE_CODE (expanded_specs) == TREE_VEC) | |
9418 | len = TREE_VEC_LENGTH (expanded_specs); | |
9419 | else | |
9420 | { | |
9421 | /* We're substituting into a member template, so | |
9422 | we got a TYPE_PACK_EXPANSION back. Add that | |
9423 | expansion and move on. */ | |
9424 | gcc_assert (TREE_CODE (expanded_specs) | |
9425 | == TYPE_PACK_EXPANSION); | |
9426 | new_specs = add_exception_specifier (new_specs, | |
9427 | expanded_specs, | |
9428 | complain); | |
9429 | specs = TREE_CHAIN (specs); | |
9430 | continue; | |
9431 | } | |
5d80a306 DG |
9432 | } |
9433 | ||
9434 | for (i = 0; i < len; ++i) | |
9435 | { | |
9436 | if (expanded_specs) | |
9437 | spec = TREE_VEC_ELT (expanded_specs, i); | |
9438 | else | |
9439 | spec = tsubst (TREE_VALUE (specs), args, complain, in_decl); | |
9440 | if (spec == error_mark_node) | |
9441 | return spec; | |
9442 | new_specs = add_exception_specifier (new_specs, spec, | |
9443 | complain); | |
9444 | } | |
9445 | ||
9446 | specs = TREE_CHAIN (specs); | |
c7222c02 MM |
9447 | } |
9448 | } | |
9449 | return new_specs; | |
9450 | } | |
9451 | ||
4393e105 MM |
9452 | /* Take the tree structure T and replace template parameters used |
9453 | therein with the argument vector ARGS. IN_DECL is an associated | |
9454 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
c2ea3a40 NS |
9455 | Issue error and warning messages under control of COMPLAIN. Note |
9456 | that we must be relatively non-tolerant of extensions here, in | |
9457 | order to preserve conformance; if we allow substitutions that | |
9458 | should not be allowed, we may allow argument deductions that should | |
9459 | not succeed, and therefore report ambiguous overload situations | |
9460 | where there are none. In theory, we could allow the substitution, | |
9461 | but indicate that it should have failed, and allow our caller to | |
9462 | make sure that the right thing happens, but we don't try to do this | |
9463 | yet. | |
4393e105 MM |
9464 | |
9465 | This function is used for dealing with types, decls and the like; | |
9466 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 | 9467 | |
d5c8be27 | 9468 | tree |
3a978d72 | 9469 | tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
ae58fa02 | 9470 | { |
0ecfe0b4 | 9471 | tree type, r; |
ae58fa02 MM |
9472 | |
9473 | if (t == NULL_TREE || t == error_mark_node | |
9474 | || t == integer_type_node | |
9475 | || t == void_type_node | |
9476 | || t == char_type_node | |
539599c1 | 9477 | || t == unknown_type_node |
ae58fa02 MM |
9478 | || TREE_CODE (t) == NAMESPACE_DECL) |
9479 | return t; | |
9480 | ||
fc6a28d7 MM |
9481 | if (DECL_P (t)) |
9482 | return tsubst_decl (t, args, complain); | |
9483 | ||
5fa0e853 SE |
9484 | if (args == NULL_TREE) |
9485 | return t; | |
9486 | ||
ae58fa02 MM |
9487 | if (TREE_CODE (t) == IDENTIFIER_NODE) |
9488 | type = IDENTIFIER_TYPE_VALUE (t); | |
9489 | else | |
9490 | type = TREE_TYPE (t); | |
399dedb9 | 9491 | |
50bc768d | 9492 | gcc_assert (type != unknown_type_node); |
ae58fa02 | 9493 | |
56d0c6e3 | 9494 | /* Reuse typedefs. We need to do this to handle dependent attributes, |
a7f6bc8c | 9495 | such as attribute aligned. */ |
5044ab0e JM |
9496 | if (TYPE_P (t) |
9497 | && TYPE_NAME (t) | |
a7f6bc8c | 9498 | && TYPE_NAME (t) != TYPE_MAIN_DECL (t)) |
5044ab0e JM |
9499 | { |
9500 | tree decl = TYPE_NAME (t); | |
56d0c6e3 | 9501 | |
a7f6bc8c | 9502 | if (DECL_CLASS_SCOPE_P (decl) |
02e52ae5 JM |
9503 | && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl)) |
9504 | && uses_template_parms (DECL_CONTEXT (decl))) | |
56d0c6e3 JM |
9505 | { |
9506 | tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
9507 | tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl); | |
2b110bfc | 9508 | r = retrieve_specialization (tmpl, gen_args, 0); |
56d0c6e3 | 9509 | } |
29b0d1fd | 9510 | else if (DECL_FUNCTION_SCOPE_P (decl) |
02e52ae5 JM |
9511 | && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl)) |
9512 | && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl)))) | |
56d0c6e3 JM |
9513 | r = retrieve_local_specialization (decl); |
9514 | else | |
29b0d1fd JM |
9515 | /* The typedef is from a non-template context. */ |
9516 | return t; | |
9517 | ||
56d0c6e3 JM |
9518 | if (r) |
9519 | { | |
9520 | r = TREE_TYPE (r); | |
9521 | r = cp_build_qualified_type_real | |
9522 | (r, cp_type_quals (t) | cp_type_quals (r), | |
9523 | complain | tf_ignore_bad_quals); | |
9524 | return r; | |
9525 | } | |
9526 | /* Else we must be instantiating the typedef, so fall through. */ | |
5044ab0e JM |
9527 | } |
9528 | ||
fc6a28d7 | 9529 | if (type |
ae58fa02 | 9530 | && TREE_CODE (t) != TYPENAME_TYPE |
4393e105 MM |
9531 | && TREE_CODE (t) != IDENTIFIER_NODE |
9532 | && TREE_CODE (t) != FUNCTION_TYPE | |
9533 | && TREE_CODE (t) != METHOD_TYPE) | |
9534 | type = tsubst (type, args, complain, in_decl); | |
9535 | if (type == error_mark_node) | |
9536 | return error_mark_node; | |
ae58fa02 | 9537 | |
ae58fa02 MM |
9538 | switch (TREE_CODE (t)) |
9539 | { | |
9540 | case RECORD_TYPE: | |
9541 | case UNION_TYPE: | |
9542 | case ENUMERAL_TYPE: | |
4393e105 MM |
9543 | return tsubst_aggr_type (t, args, complain, in_decl, |
9544 | /*entering_scope=*/0); | |
ae58fa02 MM |
9545 | |
9546 | case ERROR_MARK: | |
9547 | case IDENTIFIER_NODE: | |
ae58fa02 MM |
9548 | case VOID_TYPE: |
9549 | case REAL_TYPE: | |
9550 | case COMPLEX_TYPE: | |
c00996a3 | 9551 | case VECTOR_TYPE: |
ae58fa02 MM |
9552 | case BOOLEAN_TYPE: |
9553 | case INTEGER_CST: | |
9554 | case REAL_CST: | |
9555 | case STRING_CST: | |
9556 | return t; | |
9557 | ||
9558 | case INTEGER_TYPE: | |
9559 | if (t == integer_type_node) | |
9560 | return t; | |
9561 | ||
9562 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
9563 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
9564 | return t; | |
d2e5ee5c | 9565 | |
5566b478 | 9566 | { |
ddce3528 | 9567 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 9568 | |
6ee3ffe8 MM |
9569 | max = tsubst_expr (omax, args, complain, in_decl, |
9570 | /*integral_constant_expression_p=*/false); | |
5bbca039 JJ |
9571 | |
9572 | /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if | |
9573 | needed. */ | |
9574 | if (TREE_CODE (max) == NOP_EXPR | |
9575 | && TREE_SIDE_EFFECTS (omax) | |
9576 | && !TREE_TYPE (max)) | |
9577 | TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0)); | |
9578 | ||
4ef69b83 | 9579 | max = fold_decl_constant_value (max); |
8dd3f57a | 9580 | |
c1165535 JM |
9581 | /* If we're in a partial instantiation, preserve the magic NOP_EXPR |
9582 | with TREE_SIDE_EFFECTS that indicates this is not an integral | |
9583 | constant expression. */ | |
9584 | if (processing_template_decl | |
9585 | && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR) | |
9586 | { | |
9587 | gcc_assert (TREE_CODE (max) == NOP_EXPR); | |
9588 | TREE_SIDE_EFFECTS (max) = 1; | |
9589 | } | |
9590 | ||
6a279e92 JJ |
9591 | if (TREE_CODE (max) != INTEGER_CST |
9592 | && !at_function_scope_p () | |
c1165535 | 9593 | && !TREE_SIDE_EFFECTS (max) |
6a279e92 | 9594 | && !value_dependent_expression_p (max)) |
eff3a276 MM |
9595 | { |
9596 | if (complain & tf_error) | |
9597 | error ("array bound is not an integer constant"); | |
9598 | return error_mark_node; | |
9599 | } | |
9600 | ||
95cd6f6f | 9601 | /* [temp.deduct] |
4393e105 | 9602 | |
95cd6f6f JC |
9603 | Type deduction may fail for any of the following |
9604 | reasons: | |
4393e105 | 9605 | |
95cd6f6f JC |
9606 | Attempting to create an array with a size that is |
9607 | zero or negative. */ | |
9608 | if (integer_zerop (max) && !(complain & tf_error)) | |
9609 | /* We must fail if performing argument deduction (as | |
9610 | indicated by the state of complain), so that | |
9611 | another substitution can be found. */ | |
9612 | return error_mark_node; | |
95cd6f6f JC |
9613 | else if (TREE_CODE (max) == INTEGER_CST |
9614 | && INT_CST_LT (max, integer_zero_node)) | |
9615 | { | |
c2ea3a40 | 9616 | if (complain & tf_error) |
95cd6f6f | 9617 | error ("creating array with negative size (%qE)", max); |
4393e105 MM |
9618 | |
9619 | return error_mark_node; | |
0ecfe0b4 JM |
9620 | } |
9621 | ||
c95cd22e | 9622 | return compute_array_index_type (NULL_TREE, max); |
ae58fa02 MM |
9623 | } |
9624 | ||
9625 | case TEMPLATE_TYPE_PARM: | |
9626 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 9627 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
ae58fa02 MM |
9628 | case TEMPLATE_PARM_INDEX: |
9629 | { | |
9630 | int idx; | |
9631 | int level; | |
9632 | int levels; | |
315fb5db | 9633 | tree arg = NULL_TREE; |
0ecfe0b4 JM |
9634 | |
9635 | r = NULL_TREE; | |
ae58fa02 | 9636 | |
315fb5db | 9637 | gcc_assert (TREE_VEC_LENGTH (args) > 0); |
f4205442 | 9638 | template_parm_level_and_index (t, &level, &idx); |
ae58fa02 | 9639 | |
315fb5db NS |
9640 | levels = TMPL_ARGS_DEPTH (args); |
9641 | if (level <= levels) | |
5d80a306 DG |
9642 | { |
9643 | arg = TMPL_ARG (args, level, idx); | |
9644 | ||
9645 | if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT) | |
9646 | /* See through ARGUMENT_PACK_SELECT arguments. */ | |
9647 | arg = ARGUMENT_PACK_SELECT_ARG (arg); | |
9648 | } | |
ae58fa02 | 9649 | |
315fb5db NS |
9650 | if (arg == error_mark_node) |
9651 | return error_mark_node; | |
9652 | else if (arg != NULL_TREE) | |
9653 | { | |
5d80a306 DG |
9654 | if (ARGUMENT_PACK_P (arg)) |
9655 | /* If ARG is an argument pack, we don't actually want to | |
9656 | perform a substitution here, because substitutions | |
9657 | for argument packs are only done | |
9658 | element-by-element. We can get to this point when | |
9659 | substituting the type of a non-type template | |
9660 | parameter pack, when that type actually contains | |
9661 | template parameter packs from an outer template, e.g., | |
9662 | ||
9663 | template<typename... Types> struct A { | |
9664 | template<Types... Values> struct B { }; | |
9665 | }; */ | |
9666 | return t; | |
9667 | ||
315fb5db | 9668 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) |
ae58fa02 | 9669 | { |
9180c238 | 9670 | int quals; |
315fb5db | 9671 | gcc_assert (TYPE_P (arg)); |
9180c238 JM |
9672 | |
9673 | /* cv-quals from the template are discarded when | |
9674 | substituting in a function or reference type. */ | |
9675 | if (TREE_CODE (arg) == FUNCTION_TYPE | |
9676 | || TREE_CODE (arg) == METHOD_TYPE | |
9677 | || TREE_CODE (arg) == REFERENCE_TYPE) | |
9678 | quals = cp_type_quals (arg); | |
9679 | else | |
9680 | quals = cp_type_quals (arg) | cp_type_quals (t); | |
9681 | ||
315fb5db | 9682 | return cp_build_qualified_type_real |
9180c238 | 9683 | (arg, quals, complain | tf_ignore_bad_quals); |
315fb5db NS |
9684 | } |
9685 | else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
9686 | { | |
9687 | /* We are processing a type constructed from a | |
9688 | template template parameter. */ | |
9689 | tree argvec = tsubst (TYPE_TI_ARGS (t), | |
9690 | args, complain, in_decl); | |
9691 | if (argvec == error_mark_node) | |
9692 | return error_mark_node; | |
c8094d83 | 9693 | |
315fb5db NS |
9694 | /* We can get a TEMPLATE_TEMPLATE_PARM here when we |
9695 | are resolving nested-types in the signature of a | |
9696 | member function templates. Otherwise ARG is a | |
9697 | TEMPLATE_DECL and is the real template to be | |
9698 | instantiated. */ | |
9699 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
9700 | arg = TYPE_NAME (arg); | |
c8094d83 MS |
9701 | |
9702 | r = lookup_template_class (arg, | |
9703 | argvec, in_decl, | |
315fb5db NS |
9704 | DECL_CONTEXT (arg), |
9705 | /*entering_scope=*/0, | |
9706 | complain); | |
9707 | return cp_build_qualified_type_real | |
9708 | (r, TYPE_QUALS (t), complain); | |
ae58fa02 | 9709 | } |
315fb5db NS |
9710 | else |
9711 | /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ | |
9712 | return arg; | |
ae58fa02 MM |
9713 | } |
9714 | ||
9715 | if (level == 1) | |
9716 | /* This can happen during the attempted tsubst'ing in | |
9717 | unify. This means that we don't yet have any information | |
9718 | about the template parameter in question. */ | |
9719 | return t; | |
9720 | ||
9721 | /* If we get here, we must have been looking at a parm for a | |
9722 | more deeply nested template. Make a new version of this | |
9723 | template parameter, but with a lower level. */ | |
9724 | switch (TREE_CODE (t)) | |
9725 | { | |
9726 | case TEMPLATE_TYPE_PARM: | |
9727 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 9728 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
89d684bb | 9729 | if (cp_type_quals (t)) |
ae58fa02 | 9730 | { |
9ccf6541 | 9731 | r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); |
0cbd7506 MS |
9732 | r = cp_build_qualified_type_real |
9733 | (r, cp_type_quals (t), | |
4f2b0fb2 NS |
9734 | complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
9735 | ? tf_ignore_bad_quals : 0)); | |
9ccf6541 MM |
9736 | } |
9737 | else | |
9738 | { | |
11e74ea6 | 9739 | r = copy_type (t); |
9ccf6541 MM |
9740 | TEMPLATE_TYPE_PARM_INDEX (r) |
9741 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
ef3b7b17 | 9742 | r, levels, args, complain); |
9ccf6541 MM |
9743 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); |
9744 | TYPE_MAIN_VARIANT (r) = r; | |
9745 | TYPE_POINTER_TO (r) = NULL_TREE; | |
9746 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
9747 | ||
06d40de8 DG |
9748 | if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM) |
9749 | /* We have reduced the level of the template | |
9750 | template parameter, but not the levels of its | |
9751 | template parameters, so canonical_type_parameter | |
9752 | will not be able to find the canonical template | |
9753 | template parameter for this level. Thus, we | |
9754 | require structural equality checking to compare | |
9755 | TEMPLATE_TEMPLATE_PARMs. */ | |
9756 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
3d761c46 DG |
9757 | else if (TYPE_STRUCTURAL_EQUALITY_P (t)) |
9758 | SET_TYPE_STRUCTURAL_EQUALITY (r); | |
06d40de8 DG |
9759 | else |
9760 | TYPE_CANONICAL (r) = canonical_type_parameter (r); | |
9761 | ||
a1281f45 | 9762 | if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
9ccf6541 MM |
9763 | { |
9764 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, | |
c8094d83 | 9765 | complain, in_decl); |
9ccf6541 MM |
9766 | if (argvec == error_mark_node) |
9767 | return error_mark_node; | |
4393e105 | 9768 | |
9ccf6541 MM |
9769 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
9770 | = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE); | |
9771 | } | |
ae58fa02 MM |
9772 | } |
9773 | break; | |
9774 | ||
9775 | case TEMPLATE_PARM_INDEX: | |
ef3b7b17 | 9776 | r = reduce_template_parm_level (t, type, levels, args, complain); |
ae58fa02 | 9777 | break; |
c8094d83 | 9778 | |
ae58fa02 | 9779 | default: |
315fb5db | 9780 | gcc_unreachable (); |
ae58fa02 MM |
9781 | } |
9782 | ||
5566b478 | 9783 | return r; |
ae58fa02 | 9784 | } |
5566b478 | 9785 | |
8d08fdba MS |
9786 | case TREE_LIST: |
9787 | { | |
058b15c1 | 9788 | tree purpose, value, chain; |
8d08fdba MS |
9789 | |
9790 | if (t == void_list_node) | |
9791 | return t; | |
9792 | ||
8d08fdba MS |
9793 | purpose = TREE_PURPOSE (t); |
9794 | if (purpose) | |
4393e105 MM |
9795 | { |
9796 | purpose = tsubst (purpose, args, complain, in_decl); | |
9797 | if (purpose == error_mark_node) | |
9798 | return error_mark_node; | |
9799 | } | |
8d08fdba MS |
9800 | value = TREE_VALUE (t); |
9801 | if (value) | |
4393e105 MM |
9802 | { |
9803 | value = tsubst (value, args, complain, in_decl); | |
9804 | if (value == error_mark_node) | |
9805 | return error_mark_node; | |
9806 | } | |
8d08fdba MS |
9807 | chain = TREE_CHAIN (t); |
9808 | if (chain && chain != void_type_node) | |
4393e105 MM |
9809 | { |
9810 | chain = tsubst (chain, args, complain, in_decl); | |
9811 | if (chain == error_mark_node) | |
9812 | return error_mark_node; | |
9813 | } | |
8d08fdba MS |
9814 | if (purpose == TREE_PURPOSE (t) |
9815 | && value == TREE_VALUE (t) | |
9816 | && chain == TREE_CHAIN (t)) | |
9817 | return t; | |
058b15c1 | 9818 | return hash_tree_cons (purpose, value, chain); |
8d08fdba | 9819 | } |
c8094d83 | 9820 | |
95b4aca6 | 9821 | case TREE_BINFO: |
bd7eccc4 | 9822 | /* We should never be tsubsting a binfo. */ |
315fb5db | 9823 | gcc_unreachable (); |
85b71cf2 | 9824 | |
95b4aca6 NS |
9825 | case TREE_VEC: |
9826 | /* A vector of template arguments. */ | |
50bc768d | 9827 | gcc_assert (!type); |
a91db711 | 9828 | return tsubst_template_args (t, args, complain, in_decl); |
8d08fdba | 9829 | |
8d08fdba MS |
9830 | case POINTER_TYPE: |
9831 | case REFERENCE_TYPE: | |
9832 | { | |
8d08fdba | 9833 | enum tree_code code; |
79a7c7fa | 9834 | |
46c895ac | 9835 | if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) |
8d08fdba MS |
9836 | return t; |
9837 | ||
9838 | code = TREE_CODE (t); | |
4393e105 MM |
9839 | |
9840 | ||
9841 | /* [temp.deduct] | |
c8094d83 | 9842 | |
4393e105 | 9843 | Type deduction may fail for any of the following |
c8094d83 | 9844 | reasons: |
4393e105 MM |
9845 | |
9846 | -- Attempting to create a pointer to reference type. | |
9847 | -- Attempting to create a reference to a reference type or | |
8af2fec4 RY |
9848 | a reference to void. |
9849 | ||
9850 | Core issue 106 says that creating a reference to a reference | |
9851 | during instantiation is no longer a cause for failure. We | |
9852 | only enforce this check in strict C++98 mode. */ | |
9853 | if ((TREE_CODE (type) == REFERENCE_TYPE | |
c1ae8be5 | 9854 | && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE)) |
0ecfe0b4 | 9855 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) |
79a7c7fa | 9856 | { |
82a98427 | 9857 | static location_t last_loc; |
79a7c7fa JM |
9858 | |
9859 | /* We keep track of the last time we issued this error | |
9860 | message to avoid spewing a ton of messages during a | |
9861 | single bad template instantiation. */ | |
c2ea3a40 | 9862 | if (complain & tf_error |
2d593c86 | 9863 | && last_loc != input_location) |
79a7c7fa | 9864 | { |
0ecfe0b4 | 9865 | if (TREE_CODE (type) == VOID_TYPE) |
33bd39a2 | 9866 | error ("forming reference to void"); |
0ecfe0b4 | 9867 | else |
0f51ccfc | 9868 | error ("forming %s to reference type %qT", |
0cbd7506 MS |
9869 | (code == POINTER_TYPE) ? "pointer" : "reference", |
9870 | type); | |
82a98427 | 9871 | last_loc = input_location; |
79a7c7fa JM |
9872 | } |
9873 | ||
4393e105 | 9874 | return error_mark_node; |
79a7c7fa JM |
9875 | } |
9876 | else if (code == POINTER_TYPE) | |
46c895ac NS |
9877 | { |
9878 | r = build_pointer_type (type); | |
9879 | if (TREE_CODE (type) == METHOD_TYPE) | |
9880 | r = build_ptrmemfunc_type (r); | |
9881 | } | |
8af2fec4 RY |
9882 | else if (TREE_CODE (type) == REFERENCE_TYPE) |
9883 | /* In C++0x, during template argument substitution, when there is an | |
9884 | attempt to create a reference to a reference type, reference | |
9885 | collapsing is applied as described in [14.3.1/4 temp.arg.type]: | |
9886 | ||
9887 | "If a template-argument for a template-parameter T names a type | |
9888 | that is a reference to a type A, an attempt to create the type | |
9889 | 'lvalue reference to cv T' creates the type 'lvalue reference to | |
9890 | A,' while an attempt to create the type type rvalue reference to | |
9891 | cv T' creates the type T" | |
9892 | */ | |
9893 | r = cp_build_reference_type | |
9894 | (TREE_TYPE (type), | |
9895 | TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type)); | |
8d08fdba | 9896 | else |
8af2fec4 | 9897 | r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t)); |
adecb3f4 | 9898 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
79a7c7fa | 9899 | |
a5f1c5f8 NS |
9900 | if (r != error_mark_node) |
9901 | /* Will this ever be needed for TYPE_..._TO values? */ | |
9902 | layout_type (r); | |
c8094d83 | 9903 | |
8d08fdba MS |
9904 | return r; |
9905 | } | |
a4443a08 | 9906 | case OFFSET_TYPE: |
0ecfe0b4 | 9907 | { |
4393e105 | 9908 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
9e1e64ec | 9909 | if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r)) |
4393e105 MM |
9910 | { |
9911 | /* [temp.deduct] | |
9912 | ||
9913 | Type deduction may fail for any of the following | |
9914 | reasons: | |
c8094d83 | 9915 | |
4393e105 | 9916 | -- Attempting to create "pointer to member of T" when T |
0cbd7506 | 9917 | is not a class type. */ |
c2ea3a40 | 9918 | if (complain & tf_error) |
0f51ccfc | 9919 | error ("creating pointer to member of non-class type %qT", r); |
4393e105 MM |
9920 | return error_mark_node; |
9921 | } | |
46c895ac NS |
9922 | if (TREE_CODE (type) == REFERENCE_TYPE) |
9923 | { | |
4f09be91 | 9924 | if (complain & tf_error) |
0f51ccfc | 9925 | error ("creating pointer to member reference type %qT", type); |
cb6d4a9f VR |
9926 | return error_mark_node; |
9927 | } | |
9928 | if (TREE_CODE (type) == VOID_TYPE) | |
9929 | { | |
9930 | if (complain & tf_error) | |
9931 | error ("creating pointer to member of type void"); | |
46c895ac NS |
9932 | return error_mark_node; |
9933 | } | |
50bc768d | 9934 | gcc_assert (TREE_CODE (type) != METHOD_TYPE); |
46c895ac | 9935 | if (TREE_CODE (type) == FUNCTION_TYPE) |
a5ac359a | 9936 | { |
0cbd7506 MS |
9937 | /* The type of the implicit object parameter gets its |
9938 | cv-qualifiers from the FUNCTION_TYPE. */ | |
a5ac359a | 9939 | tree method_type; |
0cbd7506 MS |
9940 | tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r), |
9941 | cp_type_quals (type)); | |
9942 | tree memptr; | |
9943 | method_type = build_method_type_directly (this_type, | |
43dc123f MM |
9944 | TREE_TYPE (type), |
9945 | TYPE_ARG_TYPES (type)); | |
0cbd7506 MS |
9946 | memptr = build_ptrmemfunc_type (build_pointer_type (method_type)); |
9947 | return cp_build_qualified_type_real (memptr, cp_type_quals (t), | |
9948 | complain); | |
a5ac359a | 9949 | } |
46c895ac | 9950 | else |
b7a78333 MM |
9951 | return cp_build_qualified_type_real (build_ptrmem_type (r, type), |
9952 | TYPE_QUALS (t), | |
9953 | complain); | |
0ecfe0b4 | 9954 | } |
8d08fdba MS |
9955 | case FUNCTION_TYPE: |
9956 | case METHOD_TYPE: | |
9957 | { | |
c11b6f21 | 9958 | tree fntype; |
c7222c02 | 9959 | tree specs; |
4393e105 MM |
9960 | fntype = tsubst_function_type (t, args, complain, in_decl); |
9961 | if (fntype == error_mark_node) | |
9962 | return error_mark_node; | |
cabc336a | 9963 | |
34cd5ae7 | 9964 | /* Substitute the exception specification. */ |
c8094d83 | 9965 | specs = tsubst_exception_specification (t, args, complain, |
c7222c02 | 9966 | in_decl); |
9f6206d9 VR |
9967 | if (specs == error_mark_node) |
9968 | return error_mark_node; | |
c7222c02 MM |
9969 | if (specs) |
9970 | fntype = build_exception_variant (fntype, specs); | |
c11b6f21 | 9971 | return fntype; |
8d08fdba MS |
9972 | } |
9973 | case ARRAY_TYPE: | |
9974 | { | |
4393e105 MM |
9975 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
9976 | if (domain == error_mark_node) | |
9977 | return error_mark_node; | |
9978 | ||
9979 | /* As an optimization, we avoid regenerating the array type if | |
9980 | it will obviously be the same as T. */ | |
8d08fdba MS |
9981 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
9982 | return t; | |
0ecfe0b4 | 9983 | |
c8094d83 | 9984 | /* These checks should match the ones in grokdeclarator. |
4393e105 | 9985 | |
c8094d83 MS |
9986 | [temp.deduct] |
9987 | ||
9988 | The deduction may fail for any of the following reasons: | |
4393e105 MM |
9989 | |
9990 | -- Attempting to create an array with an element type that | |
c8094d83 | 9991 | is void, a function type, or a reference type, or [DR337] |
cfb91b67 | 9992 | an abstract class type. */ |
c8094d83 | 9993 | if (TREE_CODE (type) == VOID_TYPE |
4393e105 MM |
9994 | || TREE_CODE (type) == FUNCTION_TYPE |
9995 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 9996 | { |
c2ea3a40 | 9997 | if (complain & tf_error) |
0f51ccfc | 9998 | error ("creating array of %qT", type); |
4393e105 | 9999 | return error_mark_node; |
0ecfe0b4 | 10000 | } |
cfb91b67 GB |
10001 | if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type)) |
10002 | { | |
10003 | if (complain & tf_error) | |
c8094d83 | 10004 | error ("creating array of %qT, which is an abstract class type", |
cfb91b67 | 10005 | type); |
c8094d83 | 10006 | return error_mark_node; |
cfb91b67 | 10007 | } |
0ecfe0b4 | 10008 | |
8d08fdba | 10009 | r = build_cplus_array_type (type, domain); |
5044ab0e JM |
10010 | |
10011 | if (TYPE_USER_ALIGN (t)) | |
10012 | { | |
10013 | TYPE_ALIGN (r) = TYPE_ALIGN (t); | |
10014 | TYPE_USER_ALIGN (r) = 1; | |
10015 | } | |
10016 | ||
8d08fdba MS |
10017 | return r; |
10018 | } | |
10019 | ||
8d08fdba | 10020 | case PLUS_EXPR: |
5566b478 | 10021 | case MINUS_EXPR: |
4393e105 | 10022 | { |
c2ea3a40 NS |
10023 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
10024 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
4393e105 MM |
10025 | |
10026 | if (e1 == error_mark_node || e2 == error_mark_node) | |
10027 | return error_mark_node; | |
10028 | ||
db3927fb AH |
10029 | return fold_build2_loc (input_location, |
10030 | TREE_CODE (t), TREE_TYPE (t), e1, e2); | |
4393e105 | 10031 | } |
8d08fdba MS |
10032 | |
10033 | case NEGATE_EXPR: | |
10034 | case NOP_EXPR: | |
4393e105 | 10035 | { |
c2ea3a40 | 10036 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
10037 | if (e == error_mark_node) |
10038 | return error_mark_node; | |
10039 | ||
db3927fb | 10040 | return fold_build1_loc (input_location, TREE_CODE (t), TREE_TYPE (t), e); |
4393e105 | 10041 | } |
8d08fdba | 10042 | |
5566b478 MS |
10043 | case TYPENAME_TYPE: |
10044 | { | |
4393e105 MM |
10045 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
10046 | in_decl, /*entering_scope=*/1); | |
10047 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
c8094d83 | 10048 | complain, in_decl); |
4393e105 MM |
10049 | |
10050 | if (ctx == error_mark_node || f == error_mark_node) | |
10051 | return error_mark_node; | |
ae58fa02 | 10052 | |
9e1e64ec | 10053 | if (!MAYBE_CLASS_TYPE_P (ctx)) |
bf8f3f93 | 10054 | { |
c2ea3a40 | 10055 | if (complain & tf_error) |
0f51ccfc | 10056 | error ("%qT is not a class, struct, or union type", ctx); |
bf8f3f93 MM |
10057 | return error_mark_node; |
10058 | } | |
10059 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
10060 | { | |
10061 | /* Normally, make_typename_type does not require that the CTX | |
10062 | have complete type in order to allow things like: | |
c8094d83 | 10063 | |
0cbd7506 | 10064 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 10065 | |
bf8f3f93 MM |
10066 | But, such constructs have already been resolved by this |
10067 | point, so here CTX really should have complete type, unless | |
10068 | it's a partial instantiation. */ | |
8b334f7b JM |
10069 | if (!(complain & tf_no_class_instantiations)) |
10070 | ctx = complete_type (ctx); | |
d0f062fb | 10071 | if (!COMPLETE_TYPE_P (ctx)) |
4393e105 | 10072 | { |
c2ea3a40 | 10073 | if (complain & tf_error) |
7a228918 | 10074 | cxx_incomplete_type_error (NULL_TREE, ctx); |
4393e105 MM |
10075 | return error_mark_node; |
10076 | } | |
10077 | } | |
ae58fa02 | 10078 | |
fc6a28d7 | 10079 | f = make_typename_type (ctx, f, typename_type, |
4f2b0fb2 | 10080 | (complain & tf_error) | tf_keep_type_decl); |
f0bcd168 MM |
10081 | if (f == error_mark_node) |
10082 | return f; | |
0cbd7506 MS |
10083 | if (TREE_CODE (f) == TYPE_DECL) |
10084 | { | |
4f2b0fb2 | 10085 | complain |= tf_ignore_bad_quals; |
0cbd7506 MS |
10086 | f = TREE_TYPE (f); |
10087 | } | |
c8094d83 | 10088 | |
fc6a28d7 MM |
10089 | if (TREE_CODE (f) != TYPENAME_TYPE) |
10090 | { | |
10091 | if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE) | |
c8094d83 | 10092 | error ("%qT resolves to %qT, which is not an enumeration type", |
fc6a28d7 MM |
10093 | t, f); |
10094 | else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f)) | |
c8094d83 | 10095 | error ("%qT resolves to %qT, which is is not a class type", |
fc6a28d7 MM |
10096 | t, f); |
10097 | } | |
10098 | ||
0cbd7506 MS |
10099 | return cp_build_qualified_type_real |
10100 | (f, cp_type_quals (f) | cp_type_quals (t), complain); | |
5566b478 | 10101 | } |
c8094d83 | 10102 | |
b8c6534b KL |
10103 | case UNBOUND_CLASS_TEMPLATE: |
10104 | { | |
10105 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, | |
10106 | in_decl, /*entering_scope=*/1); | |
10107 | tree name = TYPE_IDENTIFIER (t); | |
b939a023 | 10108 | tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t)); |
b8c6534b KL |
10109 | |
10110 | if (ctx == error_mark_node || name == error_mark_node) | |
10111 | return error_mark_node; | |
10112 | ||
b939a023 KL |
10113 | if (parm_list) |
10114 | parm_list = tsubst_template_parms (parm_list, args, complain); | |
10115 | return make_unbound_class_template (ctx, name, parm_list, complain); | |
b8c6534b KL |
10116 | } |
10117 | ||
5566b478 | 10118 | case INDIRECT_REF: |
5566b478 | 10119 | case ADDR_EXPR: |
058b15c1 | 10120 | case CALL_EXPR: |
315fb5db | 10121 | gcc_unreachable (); |
5566b478 MS |
10122 | |
10123 | case ARRAY_REF: | |
4393e105 | 10124 | { |
c2ea3a40 | 10125 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
015c2c66 MM |
10126 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl, |
10127 | /*integral_constant_expression_p=*/false); | |
4393e105 MM |
10128 | if (e1 == error_mark_node || e2 == error_mark_node) |
10129 | return error_mark_node; | |
10130 | ||
44de5aeb | 10131 | return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE); |
4393e105 | 10132 | } |
5566b478 | 10133 | |
fc378698 | 10134 | case SCOPE_REF: |
4393e105 | 10135 | { |
c2ea3a40 | 10136 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
10137 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); |
10138 | if (e1 == error_mark_node || e2 == error_mark_node) | |
10139 | return error_mark_node; | |
10140 | ||
02ed62dd MM |
10141 | return build_qualified_name (/*type=*/NULL_TREE, |
10142 | e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t)); | |
4393e105 | 10143 | } |
fc378698 | 10144 | |
b894fc05 | 10145 | case TYPEOF_TYPE: |
4393e105 | 10146 | { |
b830b74c | 10147 | tree type; |
4393e105 | 10148 | |
015c2c66 MM |
10149 | type = finish_typeof (tsubst_expr |
10150 | (TYPEOF_TYPE_EXPR (t), args, | |
10151 | complain, in_decl, | |
10152 | /*integral_constant_expression_p=*/false)); | |
b830b74c | 10153 | return cp_build_qualified_type_real (type, |
dce50630 | 10154 | cp_type_quals (t) |
b830b74c | 10155 | | cp_type_quals (type), |
dce50630 | 10156 | complain); |
4393e105 | 10157 | } |
b894fc05 | 10158 | |
3ad6a8e1 DG |
10159 | case DECLTYPE_TYPE: |
10160 | { | |
10161 | tree type; | |
10162 | ||
7d882b83 ILT |
10163 | ++cp_unevaluated_operand; |
10164 | ++c_inhibit_evaluation_warnings; | |
b344d949 JM |
10165 | |
10166 | type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args, | |
10167 | complain, in_decl, | |
10168 | /*integral_constant_expression_p=*/false); | |
10169 | ||
7d882b83 ILT |
10170 | --cp_unevaluated_operand; |
10171 | --c_inhibit_evaluation_warnings; | |
b344d949 | 10172 | |
d5f4eddd JM |
10173 | if (DECLTYPE_FOR_LAMBDA_CAPTURE (t)) |
10174 | type = lambda_capture_field_type (type); | |
10175 | else if (DECLTYPE_FOR_LAMBDA_RETURN (t)) | |
10176 | type = lambda_return_type (type); | |
10177 | else | |
10178 | type = finish_decltype_type | |
10179 | (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t)); | |
3ad6a8e1 DG |
10180 | return cp_build_qualified_type_real (type, |
10181 | cp_type_quals (t) | |
10182 | | cp_type_quals (type), | |
10183 | complain); | |
10184 | } | |
10185 | ||
5d80a306 DG |
10186 | case TYPE_ARGUMENT_PACK: |
10187 | case NONTYPE_ARGUMENT_PACK: | |
10188 | { | |
10189 | tree r = make_node (TREE_CODE (t)); | |
10190 | tree packed_out = | |
10191 | tsubst_template_args (ARGUMENT_PACK_ARGS (t), | |
10192 | args, | |
10193 | complain, | |
10194 | in_decl); | |
10195 | SET_ARGUMENT_PACK_ARGS (r, packed_out); | |
10196 | ||
10197 | /* For template nontype argument packs, also substitute into | |
10198 | the type. */ | |
10199 | if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK) | |
10200 | TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
10201 | ||
10202 | return r; | |
10203 | } | |
10204 | break; | |
10205 | ||
8d08fdba | 10206 | default: |
9e637a26 | 10207 | sorry ("use of %qs in template", |
8d08fdba MS |
10208 | tree_code_name [(int) TREE_CODE (t)]); |
10209 | return error_mark_node; | |
10210 | } | |
10211 | } | |
10212 | ||
ee76b931 MM |
10213 | /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the |
10214 | type of the expression on the left-hand side of the "." or "->" | |
10215 | operator. */ | |
10216 | ||
10217 | static tree | |
10218 | tsubst_baselink (tree baselink, tree object_type, | |
10219 | tree args, tsubst_flags_t complain, tree in_decl) | |
10220 | { | |
10221 | tree name; | |
10222 | tree qualifying_scope; | |
10223 | tree fns; | |
6f67f000 | 10224 | tree optype; |
ee76b931 MM |
10225 | tree template_args = 0; |
10226 | bool template_id_p = false; | |
10227 | ||
51a203d9 MM |
10228 | /* A baselink indicates a function from a base class. Both the |
10229 | BASELINK_ACCESS_BINFO and the base class referenced may | |
10230 | indicate bases of the template class, rather than the | |
10231 | instantiated class. In addition, lookups that were not | |
10232 | ambiguous before may be ambiguous now. Therefore, we perform | |
10233 | the lookup again. */ | |
ee76b931 | 10234 | qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)); |
51a203d9 MM |
10235 | qualifying_scope = tsubst (qualifying_scope, args, |
10236 | complain, in_decl); | |
ee76b931 | 10237 | fns = BASELINK_FUNCTIONS (baselink); |
6f67f000 | 10238 | optype = BASELINK_OPTYPE (baselink); |
ee76b931 MM |
10239 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) |
10240 | { | |
10241 | template_id_p = true; | |
10242 | template_args = TREE_OPERAND (fns, 1); | |
10243 | fns = TREE_OPERAND (fns, 0); | |
bf12d54d NS |
10244 | if (template_args) |
10245 | template_args = tsubst_template_args (template_args, args, | |
10246 | complain, in_decl); | |
ee76b931 MM |
10247 | } |
10248 | name = DECL_NAME (get_first_fn (fns)); | |
10249 | baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1); | |
c8094d83 | 10250 | |
9b60dfd7 MM |
10251 | /* If lookup found a single function, mark it as used at this |
10252 | point. (If it lookup found multiple functions the one selected | |
10253 | later by overload resolution will be marked as used at that | |
10254 | point.) */ | |
10255 | if (BASELINK_P (baselink)) | |
10256 | fns = BASELINK_FUNCTIONS (baselink); | |
10257 | if (!template_id_p && !really_overloaded_fn (fns)) | |
10258 | mark_used (OVL_CURRENT (fns)); | |
10259 | ||
10260 | /* Add back the template arguments, if present. */ | |
ee76b931 | 10261 | if (BASELINK_P (baselink) && template_id_p) |
c8094d83 | 10262 | BASELINK_FUNCTIONS (baselink) |
ee76b931 MM |
10263 | = build_nt (TEMPLATE_ID_EXPR, |
10264 | BASELINK_FUNCTIONS (baselink), | |
10265 | template_args); | |
6f67f000 MM |
10266 | /* Update the conversion operator type. */ |
10267 | BASELINK_OPTYPE (baselink) | |
10268 | = tsubst (optype, args, complain, in_decl); | |
9b60dfd7 | 10269 | |
ee76b931 MM |
10270 | if (!object_type) |
10271 | object_type = current_class_type; | |
c8094d83 | 10272 | return adjust_result_of_qualified_name_lookup (baselink, |
ee76b931 MM |
10273 | qualifying_scope, |
10274 | object_type); | |
10275 | } | |
10276 | ||
10277 | /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is | |
10278 | true if the qualified-id will be a postfix-expression in-and-of | |
10279 | itself; false if more of the postfix-expression follows the | |
10280 | QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand | |
10281 | of "&". */ | |
10282 | ||
10283 | static tree | |
c8094d83 | 10284 | tsubst_qualified_id (tree qualified_id, tree args, |
ee76b931 MM |
10285 | tsubst_flags_t complain, tree in_decl, |
10286 | bool done, bool address_p) | |
10287 | { | |
10288 | tree expr; | |
10289 | tree scope; | |
10290 | tree name; | |
10291 | bool is_template; | |
10292 | tree template_args; | |
10293 | ||
50bc768d | 10294 | gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF); |
ee76b931 | 10295 | |
ee76b931 MM |
10296 | /* Figure out what name to look up. */ |
10297 | name = TREE_OPERAND (qualified_id, 1); | |
10298 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
10299 | { | |
10300 | is_template = true; | |
bf12d54d NS |
10301 | template_args = TREE_OPERAND (name, 1); |
10302 | if (template_args) | |
10303 | template_args = tsubst_template_args (template_args, args, | |
10304 | complain, in_decl); | |
ee76b931 MM |
10305 | name = TREE_OPERAND (name, 0); |
10306 | } | |
10307 | else | |
10308 | { | |
10309 | is_template = false; | |
10310 | template_args = NULL_TREE; | |
10311 | } | |
10312 | ||
6eeba0cc MM |
10313 | /* Substitute into the qualifying scope. When there are no ARGS, we |
10314 | are just trying to simplify a non-dependent expression. In that | |
10315 | case the qualifying scope may be dependent, and, in any case, | |
10316 | substituting will not help. */ | |
10317 | scope = TREE_OPERAND (qualified_id, 0); | |
10318 | if (args) | |
10319 | { | |
10320 | scope = tsubst (scope, args, complain, in_decl); | |
10321 | expr = tsubst_copy (name, args, complain, in_decl); | |
10322 | } | |
10323 | else | |
10324 | expr = name; | |
10b1d5e7 | 10325 | |
ab73670a | 10326 | if (dependent_type_p (scope)) |
7e361ae6 JM |
10327 | { |
10328 | tree type = NULL_TREE; | |
10329 | if (DECL_P (expr) && !dependent_scope_p (scope)) | |
10330 | type = TREE_TYPE (expr); | |
10331 | return build_qualified_name (type, scope, expr, | |
10332 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id)); | |
10333 | } | |
c8094d83 | 10334 | |
5e08432e | 10335 | if (!BASELINK_P (name) && !DECL_P (expr)) |
12483c9f | 10336 | { |
8ca4bf25 | 10337 | if (TREE_CODE (expr) == BIT_NOT_EXPR) |
7e361ae6 JM |
10338 | { |
10339 | /* A BIT_NOT_EXPR is used to represent a destructor. */ | |
10340 | if (!check_dtor_name (scope, TREE_OPERAND (expr, 0))) | |
10341 | { | |
10342 | error ("qualifying type %qT does not match destructor name ~%qT", | |
10343 | scope, TREE_OPERAND (expr, 0)); | |
10344 | expr = error_mark_node; | |
10345 | } | |
10346 | else | |
10347 | expr = lookup_qualified_name (scope, complete_dtor_identifier, | |
10348 | /*is_type_p=*/0, false); | |
10349 | } | |
8ca4bf25 MM |
10350 | else |
10351 | expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false); | |
12483c9f NS |
10352 | if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL |
10353 | ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL) | |
10354 | { | |
10355 | if (complain & tf_error) | |
b2693faf | 10356 | { |
0f51ccfc | 10357 | error ("dependent-name %qE is parsed as a non-type, but " |
b2693faf | 10358 | "instantiation yields a type", qualified_id); |
1f5b3869 | 10359 | inform (input_location, "say %<typename %E%> if a type is meant", qualified_id); |
b2693faf | 10360 | } |
12483c9f NS |
10361 | return error_mark_node; |
10362 | } | |
10363 | } | |
c8094d83 | 10364 | |
279b8466 | 10365 | if (DECL_P (expr)) |
8f78f01f MM |
10366 | { |
10367 | check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE, | |
10368 | scope); | |
10369 | /* Remember that there was a reference to this entity. */ | |
10370 | mark_used (expr); | |
10371 | } | |
10372 | ||
10373 | if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST) | |
10374 | { | |
10375 | if (complain & tf_error) | |
c8094d83 | 10376 | qualified_name_lookup_error (scope, |
8f78f01f | 10377 | TREE_OPERAND (qualified_id, 1), |
2b7a3abf | 10378 | expr, input_location); |
8f78f01f MM |
10379 | return error_mark_node; |
10380 | } | |
ee76b931 MM |
10381 | |
10382 | if (is_template) | |
10b1d5e7 | 10383 | expr = lookup_template_function (expr, template_args); |
ee76b931 | 10384 | |
22038b2c | 10385 | if (expr == error_mark_node && complain & tf_error) |
8f78f01f | 10386 | qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1), |
2b7a3abf | 10387 | expr, input_location); |
22038b2c | 10388 | else if (TYPE_P (scope)) |
ee76b931 | 10389 | { |
c8094d83 | 10390 | expr = (adjust_result_of_qualified_name_lookup |
ee76b931 | 10391 | (expr, scope, current_class_type)); |
3db45ab5 | 10392 | expr = (finish_qualified_id_expr |
02ed62dd MM |
10393 | (scope, expr, done, address_p, |
10394 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id), | |
10395 | /*template_arg_p=*/false)); | |
ee76b931 | 10396 | } |
c8094d83 | 10397 | |
015c2c66 MM |
10398 | /* Expressions do not generally have reference type. */ |
10399 | if (TREE_CODE (expr) != SCOPE_REF | |
10400 | /* However, if we're about to form a pointer-to-member, we just | |
10401 | want the referenced member referenced. */ | |
10402 | && TREE_CODE (expr) != OFFSET_REF) | |
aec5ba60 | 10403 | expr = convert_from_reference (expr); |
ee76b931 MM |
10404 | |
10405 | return expr; | |
10406 | } | |
10407 | ||
00d3396f JM |
10408 | /* Like tsubst, but deals with expressions. This function just replaces |
10409 | template parms; to finish processing the resultant expression, use | |
10410 | tsubst_expr. */ | |
10411 | ||
14d22dd6 | 10412 | static tree |
3a978d72 | 10413 | tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
5566b478 MS |
10414 | { |
10415 | enum tree_code code; | |
8452b1d3 | 10416 | tree r; |
8d08fdba | 10417 | |
b77ba909 | 10418 | if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE) |
5566b478 MS |
10419 | return t; |
10420 | ||
10421 | code = TREE_CODE (t); | |
b7484fbe | 10422 | |
5566b478 MS |
10423 | switch (code) |
10424 | { | |
10425 | case PARM_DECL: | |
a723baf1 | 10426 | r = retrieve_local_specialization (t); |
b344d949 JM |
10427 | |
10428 | if (r == NULL) | |
10429 | { | |
c7cb9f42 | 10430 | tree c; |
b344d949 | 10431 | /* This can happen for a parameter name used later in a function |
448545cb JM |
10432 | declaration (such as in a late-specified return type). Just |
10433 | make a dummy decl, since it's only used for its type. */ | |
7d882b83 | 10434 | gcc_assert (cp_unevaluated_operand != 0); |
c7cb9f42 DS |
10435 | /* We copy T because want to tsubst the PARM_DECL only, |
10436 | not the following PARM_DECLs that are chained to T. */ | |
10437 | c = copy_node (t); | |
10438 | r = tsubst_decl (c, args, complain); | |
448545cb JM |
10439 | /* Give it the template pattern as its context; its true context |
10440 | hasn't been instantiated yet and this is good enough for | |
10441 | mangling. */ | |
10442 | DECL_CONTEXT (r) = DECL_CONTEXT (t); | |
b344d949 JM |
10443 | } |
10444 | ||
5d80a306 DG |
10445 | if (TREE_CODE (r) == ARGUMENT_PACK_SELECT) |
10446 | r = ARGUMENT_PACK_SELECT_ARG (r); | |
c0694c4b | 10447 | mark_used (r); |
a723baf1 | 10448 | return r; |
5566b478 MS |
10449 | |
10450 | case CONST_DECL: | |
ed44da02 MM |
10451 | { |
10452 | tree enum_type; | |
10453 | tree v; | |
10454 | ||
a723baf1 MM |
10455 | if (DECL_TEMPLATE_PARM_P (t)) |
10456 | return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); | |
7b6d72fc MM |
10457 | /* There is no need to substitute into namespace-scope |
10458 | enumerators. */ | |
10459 | if (DECL_NAMESPACE_SCOPE_P (t)) | |
ed44da02 | 10460 | return t; |
d5a10cf0 MM |
10461 | /* If ARGS is NULL, then T is known to be non-dependent. */ |
10462 | if (args == NULL_TREE) | |
8a784e4a | 10463 | return integral_constant_value (t); |
ed44da02 MM |
10464 | |
10465 | /* Unfortunately, we cannot just call lookup_name here. | |
9188c363 | 10466 | Consider: |
c8094d83 | 10467 | |
9188c363 MM |
10468 | template <int I> int f() { |
10469 | enum E { a = I }; | |
10470 | struct S { void g() { E e = a; } }; | |
10471 | }; | |
c8094d83 | 10472 | |
9188c363 MM |
10473 | When we instantiate f<7>::S::g(), say, lookup_name is not |
10474 | clever enough to find f<7>::a. */ | |
c8094d83 MS |
10475 | enum_type |
10476 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, | |
ed44da02 MM |
10477 | /*entering_scope=*/0); |
10478 | ||
c8094d83 MS |
10479 | for (v = TYPE_VALUES (enum_type); |
10480 | v != NULL_TREE; | |
ed44da02 MM |
10481 | v = TREE_CHAIN (v)) |
10482 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
10483 | return TREE_VALUE (v); | |
10484 | ||
10485 | /* We didn't find the name. That should never happen; if | |
10486 | name-lookup found it during preliminary parsing, we | |
10487 | should find it again here during instantiation. */ | |
315fb5db | 10488 | gcc_unreachable (); |
ed44da02 | 10489 | } |
db04386f | 10490 | return t; |
ed44da02 | 10491 | |
5566b478 MS |
10492 | case FIELD_DECL: |
10493 | if (DECL_CONTEXT (t)) | |
10494 | { | |
0978790f | 10495 | tree ctx; |
0978790f | 10496 | |
4393e105 | 10497 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 10498 | /*entering_scope=*/1); |
0978790f | 10499 | if (ctx != DECL_CONTEXT (t)) |
bad1f462 KL |
10500 | { |
10501 | tree r = lookup_field (ctx, DECL_NAME (t), 0, false); | |
10502 | if (!r) | |
10503 | { | |
10504 | if (complain & tf_error) | |
10505 | error ("using invalid field %qD", t); | |
10506 | return error_mark_node; | |
10507 | } | |
10508 | return r; | |
10509 | } | |
5566b478 | 10510 | } |
c8094d83 | 10511 | |
5566b478 MS |
10512 | return t; |
10513 | ||
10514 | case VAR_DECL: | |
10515 | case FUNCTION_DECL: | |
a723baf1 MM |
10516 | if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) |
10517 | || local_variable_p (t)) | |
4393e105 | 10518 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
10519 | mark_used (t); |
10520 | return t; | |
10521 | ||
a723baf1 | 10522 | case BASELINK: |
ee76b931 | 10523 | return tsubst_baselink (t, current_class_type, args, complain, in_decl); |
a723baf1 | 10524 | |
98c1c668 | 10525 | case TEMPLATE_DECL: |
a723baf1 | 10526 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
c8094d83 | 10527 | return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), |
a723baf1 | 10528 | args, complain, in_decl); |
c7222c02 | 10529 | else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t)) |
4393e105 | 10530 | return tsubst (t, args, complain, in_decl); |
fcea7401 KL |
10531 | else if (DECL_CLASS_SCOPE_P (t) |
10532 | && uses_template_parms (DECL_CONTEXT (t))) | |
10533 | { | |
10534 | /* Template template argument like the following example need | |
10535 | special treatment: | |
10536 | ||
10537 | template <template <class> class TT> struct C {}; | |
10538 | template <class T> struct D { | |
10539 | template <class U> struct E {}; | |
0cbd7506 | 10540 | C<E> c; // #1 |
fcea7401 KL |
10541 | }; |
10542 | D<int> d; // #2 | |
10543 | ||
10544 | We are processing the template argument `E' in #1 for | |
10545 | the template instantiation #2. Originally, `E' is a | |
10546 | TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we | |
10547 | have to substitute this with one having context `D<int>'. */ | |
10548 | ||
10549 | tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl); | |
10550 | return lookup_field (context, DECL_NAME(t), 0, false); | |
10551 | } | |
98c1c668 | 10552 | else |
fcea7401 | 10553 | /* Ordinary template template argument. */ |
98c1c668 JM |
10554 | return t; |
10555 | ||
5566b478 MS |
10556 | case CAST_EXPR: |
10557 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
10558 | case CONST_CAST_EXPR: |
10559 | case STATIC_CAST_EXPR: | |
10560 | case DYNAMIC_CAST_EXPR: | |
51924768 | 10561 | case NOP_EXPR: |
5566b478 | 10562 | return build1 |
4393e105 MM |
10563 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
10564 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 | 10565 | |
5d80a306 DG |
10566 | case SIZEOF_EXPR: |
10567 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) | |
10568 | { | |
10569 | /* We only want to compute the number of arguments. */ | |
10570 | tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args, | |
10571 | complain, in_decl); | |
e68c63e3 | 10572 | int len = 0; |
063cc99c DG |
10573 | |
10574 | if (TREE_CODE (expanded) == TREE_VEC) | |
10575 | len = TREE_VEC_LENGTH (expanded); | |
10576 | ||
4745e4eb JJ |
10577 | if (expanded == error_mark_node) |
10578 | return error_mark_node; | |
063cc99c DG |
10579 | else if (PACK_EXPANSION_P (expanded) |
10580 | || (TREE_CODE (expanded) == TREE_VEC | |
10581 | && len > 0 | |
10582 | && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1)))) | |
10583 | { | |
10584 | if (TREE_CODE (expanded) == TREE_VEC) | |
10585 | expanded = TREE_VEC_ELT (expanded, len - 1); | |
10586 | ||
10587 | if (TYPE_P (expanded)) | |
10588 | return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR, | |
10589 | complain & tf_error); | |
10590 | else | |
10591 | return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR, | |
10592 | complain & tf_error); | |
10593 | } | |
10594 | else | |
10595 | return build_int_cst (size_type_node, len); | |
5d80a306 DG |
10596 | } |
10597 | /* Fall through */ | |
10598 | ||
5566b478 | 10599 | case INDIRECT_REF: |
5566b478 MS |
10600 | case NEGATE_EXPR: |
10601 | case TRUTH_NOT_EXPR: | |
b87692e5 | 10602 | case BIT_NOT_EXPR: |
5566b478 | 10603 | case ADDR_EXPR: |
392e3d51 | 10604 | case UNARY_PLUS_EXPR: /* Unary + */ |
abff8e06 | 10605 | case ALIGNOF_EXPR: |
5566b478 | 10606 | case ARROW_EXPR: |
fc378698 | 10607 | case THROW_EXPR: |
5156628f | 10608 | case TYPEID_EXPR: |
f5733617 SS |
10609 | case REALPART_EXPR: |
10610 | case IMAGPART_EXPR: | |
5566b478 | 10611 | return build1 |
6a629cac | 10612 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 10613 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 | 10614 | |
a723baf1 MM |
10615 | case COMPONENT_REF: |
10616 | { | |
10617 | tree object; | |
10618 | tree name; | |
10619 | ||
10620 | object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
10621 | name = TREE_OPERAND (t, 1); | |
c8094d83 | 10622 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
a723baf1 MM |
10623 | { |
10624 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
10625 | complain, in_decl); | |
10626 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
10627 | } | |
10628 | else if (TREE_CODE (name) == SCOPE_REF | |
10629 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
10630 | { | |
10631 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, | |
10632 | complain, in_decl); | |
10633 | name = TREE_OPERAND (name, 1); | |
10634 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
10635 | complain, in_decl); | |
10636 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
02ed62dd | 10637 | name = build_qualified_name (/*type=*/NULL_TREE, |
3db45ab5 | 10638 | base, name, |
02ed62dd | 10639 | /*template_p=*/false); |
a723baf1 | 10640 | } |
ee76b931 | 10641 | else if (TREE_CODE (name) == BASELINK) |
c8094d83 MS |
10642 | name = tsubst_baselink (name, |
10643 | non_reference (TREE_TYPE (object)), | |
10644 | args, complain, | |
ee76b931 | 10645 | in_decl); |
a723baf1 | 10646 | else |
ee76b931 | 10647 | name = tsubst_copy (name, args, complain, in_decl); |
44de5aeb | 10648 | return build_nt (COMPONENT_REF, object, name, NULL_TREE); |
a723baf1 MM |
10649 | } |
10650 | ||
5566b478 MS |
10651 | case PLUS_EXPR: |
10652 | case MINUS_EXPR: | |
10653 | case MULT_EXPR: | |
10654 | case TRUNC_DIV_EXPR: | |
10655 | case CEIL_DIV_EXPR: | |
10656 | case FLOOR_DIV_EXPR: | |
10657 | case ROUND_DIV_EXPR: | |
10658 | case EXACT_DIV_EXPR: | |
10659 | case BIT_AND_EXPR: | |
5566b478 MS |
10660 | case BIT_IOR_EXPR: |
10661 | case BIT_XOR_EXPR: | |
10662 | case TRUNC_MOD_EXPR: | |
10663 | case FLOOR_MOD_EXPR: | |
10664 | case TRUTH_ANDIF_EXPR: | |
10665 | case TRUTH_ORIF_EXPR: | |
10666 | case TRUTH_AND_EXPR: | |
10667 | case TRUTH_OR_EXPR: | |
10668 | case RSHIFT_EXPR: | |
10669 | case LSHIFT_EXPR: | |
10670 | case RROTATE_EXPR: | |
10671 | case LROTATE_EXPR: | |
10672 | case EQ_EXPR: | |
10673 | case NE_EXPR: | |
10674 | case MAX_EXPR: | |
10675 | case MIN_EXPR: | |
10676 | case LE_EXPR: | |
10677 | case GE_EXPR: | |
10678 | case LT_EXPR: | |
10679 | case GT_EXPR: | |
5566b478 | 10680 | case COMPOUND_EXPR: |
5566b478 MS |
10681 | case DOTSTAR_EXPR: |
10682 | case MEMBER_REF: | |
519c9806 MM |
10683 | case PREDECREMENT_EXPR: |
10684 | case PREINCREMENT_EXPR: | |
10685 | case POSTDECREMENT_EXPR: | |
10686 | case POSTINCREMENT_EXPR: | |
5566b478 | 10687 | return build_nt |
4393e105 MM |
10688 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
10689 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 | 10690 | |
02ed62dd MM |
10691 | case SCOPE_REF: |
10692 | return build_qualified_name (/*type=*/NULL_TREE, | |
10693 | tsubst_copy (TREE_OPERAND (t, 0), | |
10694 | args, complain, in_decl), | |
10695 | tsubst_copy (TREE_OPERAND (t, 1), | |
10696 | args, complain, in_decl), | |
10697 | QUALIFIED_NAME_IS_TEMPLATE (t)); | |
10698 | ||
d8987adb NS |
10699 | case ARRAY_REF: |
10700 | return build_nt | |
10701 | (ARRAY_REF, | |
10702 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
10703 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
10704 | NULL_TREE, NULL_TREE); | |
10705 | ||
5566b478 | 10706 | case CALL_EXPR: |
5039610b SL |
10707 | { |
10708 | int n = VL_EXP_OPERAND_LENGTH (t); | |
10709 | tree result = build_vl_exp (CALL_EXPR, n); | |
10710 | int i; | |
10711 | for (i = 0; i < n; i++) | |
10712 | TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args, | |
10713 | complain, in_decl); | |
10714 | return result; | |
10715 | } | |
5566b478 | 10716 | |
5566b478 MS |
10717 | case COND_EXPR: |
10718 | case MODOP_EXPR: | |
40242ccf | 10719 | case PSEUDO_DTOR_EXPR: |
67da3287 | 10720 | { |
8452b1d3 | 10721 | r = build_nt |
4393e105 MM |
10722 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
10723 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
10724 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
e4c2c34b | 10725 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); |
67da3287 MM |
10726 | return r; |
10727 | } | |
5566b478 MS |
10728 | |
10729 | case NEW_EXPR: | |
10730 | { | |
8452b1d3 | 10731 | r = build_nt |
4393e105 MM |
10732 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
10733 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
10734 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
10735 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
10736 | return r; | |
10737 | } | |
10738 | ||
10739 | case DELETE_EXPR: | |
10740 | { | |
8452b1d3 | 10741 | r = build_nt |
4393e105 MM |
10742 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
10743 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
10744 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
10745 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
10746 | return r; | |
10747 | } | |
10748 | ||
386b8a85 JM |
10749 | case TEMPLATE_ID_EXPR: |
10750 | { | |
0cbd7506 | 10751 | /* Substituted template arguments */ |
a91db711 NS |
10752 | tree fn = TREE_OPERAND (t, 0); |
10753 | tree targs = TREE_OPERAND (t, 1); | |
856216bb | 10754 | |
a91db711 | 10755 | fn = tsubst_copy (fn, args, complain, in_decl); |
bf12d54d NS |
10756 | if (targs) |
10757 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 10758 | |
a91db711 | 10759 | return lookup_template_function (fn, targs); |
386b8a85 JM |
10760 | } |
10761 | ||
5566b478 MS |
10762 | case TREE_LIST: |
10763 | { | |
10764 | tree purpose, value, chain; | |
10765 | ||
10766 | if (t == void_list_node) | |
10767 | return t; | |
10768 | ||
10769 | purpose = TREE_PURPOSE (t); | |
10770 | if (purpose) | |
4393e105 | 10771 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
10772 | value = TREE_VALUE (t); |
10773 | if (value) | |
4393e105 | 10774 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
10775 | chain = TREE_CHAIN (t); |
10776 | if (chain && chain != void_type_node) | |
4393e105 | 10777 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
10778 | if (purpose == TREE_PURPOSE (t) |
10779 | && value == TREE_VALUE (t) | |
10780 | && chain == TREE_CHAIN (t)) | |
10781 | return t; | |
10782 | return tree_cons (purpose, value, chain); | |
10783 | } | |
10784 | ||
10785 | case RECORD_TYPE: | |
10786 | case UNION_TYPE: | |
10787 | case ENUMERAL_TYPE: | |
10788 | case INTEGER_TYPE: | |
10789 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 10790 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 10791 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 10792 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
10793 | case POINTER_TYPE: |
10794 | case REFERENCE_TYPE: | |
10795 | case OFFSET_TYPE: | |
10796 | case FUNCTION_TYPE: | |
10797 | case METHOD_TYPE: | |
10798 | case ARRAY_TYPE: | |
10799 | case TYPENAME_TYPE: | |
b8c6534b | 10800 | case UNBOUND_CLASS_TEMPLATE: |
2a2b1d56 | 10801 | case TYPEOF_TYPE: |
3ad6a8e1 | 10802 | case DECLTYPE_TYPE: |
f84b4be9 | 10803 | case TYPE_DECL: |
4393e105 | 10804 | return tsubst (t, args, complain, in_decl); |
5566b478 | 10805 | |
e92cc029 | 10806 | case IDENTIFIER_NODE: |
421844e7 | 10807 | if (IDENTIFIER_TYPENAME_P (t)) |
1f6e1acc AS |
10808 | { |
10809 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
1f84ec23 | 10810 | return mangle_conv_op_name_for_type (new_type); |
1f6e1acc | 10811 | } |
e92cc029 MS |
10812 | else |
10813 | return t; | |
10814 | ||
5156628f | 10815 | case CONSTRUCTOR: |
4038c495 GB |
10816 | /* This is handled by tsubst_copy_and_build. */ |
10817 | gcc_unreachable (); | |
5156628f | 10818 | |
371534a9 | 10819 | case VA_ARG_EXPR: |
ea333e1c | 10820 | return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, |
c2ea3a40 NS |
10821 | in_decl), |
10822 | tsubst (TREE_TYPE (t), args, complain, in_decl)); | |
f9817201 | 10823 | |
543a0daa RH |
10824 | case CLEANUP_POINT_EXPR: |
10825 | /* We shouldn't have built any of these during initial template | |
10826 | generation. Instead, they should be built during instantiation | |
10827 | in response to the saved STMT_IS_FULL_EXPR_P setting. */ | |
315fb5db | 10828 | gcc_unreachable (); |
543a0daa | 10829 | |
7eab6e7b MM |
10830 | case OFFSET_REF: |
10831 | mark_used (TREE_OPERAND (t, 1)); | |
10832 | return t; | |
10833 | ||
5d80a306 DG |
10834 | case EXPR_PACK_EXPANSION: |
10835 | error ("invalid use of pack expansion expression"); | |
10836 | return error_mark_node; | |
10837 | ||
10838 | case NONTYPE_ARGUMENT_PACK: | |
10839 | error ("use %<...%> to expand argument pack"); | |
10840 | return error_mark_node; | |
10841 | ||
5566b478 MS |
10842 | default: |
10843 | return t; | |
10844 | } | |
10845 | } | |
10846 | ||
1799e5d5 RH |
10847 | /* Like tsubst_copy, but specifically for OpenMP clauses. */ |
10848 | ||
10849 | static tree | |
10850 | tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain, | |
10851 | tree in_decl) | |
10852 | { | |
10853 | tree new_clauses = NULL, nc, oc; | |
10854 | ||
10855 | for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc)) | |
10856 | { | |
10857 | nc = copy_node (oc); | |
10858 | OMP_CLAUSE_CHAIN (nc) = new_clauses; | |
10859 | new_clauses = nc; | |
10860 | ||
10861 | switch (OMP_CLAUSE_CODE (nc)) | |
10862 | { | |
a68ab351 JJ |
10863 | case OMP_CLAUSE_LASTPRIVATE: |
10864 | if (OMP_CLAUSE_LASTPRIVATE_STMT (oc)) | |
10865 | { | |
10866 | OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list (); | |
10867 | tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain, | |
10868 | in_decl, /*integral_constant_expression_p=*/false); | |
10869 | OMP_CLAUSE_LASTPRIVATE_STMT (nc) | |
10870 | = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc)); | |
10871 | } | |
10872 | /* FALLTHRU */ | |
1799e5d5 RH |
10873 | case OMP_CLAUSE_PRIVATE: |
10874 | case OMP_CLAUSE_SHARED: | |
10875 | case OMP_CLAUSE_FIRSTPRIVATE: | |
1799e5d5 RH |
10876 | case OMP_CLAUSE_REDUCTION: |
10877 | case OMP_CLAUSE_COPYIN: | |
10878 | case OMP_CLAUSE_COPYPRIVATE: | |
10879 | case OMP_CLAUSE_IF: | |
10880 | case OMP_CLAUSE_NUM_THREADS: | |
10881 | case OMP_CLAUSE_SCHEDULE: | |
a68ab351 | 10882 | case OMP_CLAUSE_COLLAPSE: |
1799e5d5 | 10883 | OMP_CLAUSE_OPERAND (nc, 0) |
015c2c66 MM |
10884 | = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain, |
10885 | in_decl, /*integral_constant_expression_p=*/false); | |
1799e5d5 RH |
10886 | break; |
10887 | case OMP_CLAUSE_NOWAIT: | |
10888 | case OMP_CLAUSE_ORDERED: | |
10889 | case OMP_CLAUSE_DEFAULT: | |
a68ab351 | 10890 | case OMP_CLAUSE_UNTIED: |
1799e5d5 RH |
10891 | break; |
10892 | default: | |
10893 | gcc_unreachable (); | |
10894 | } | |
10895 | } | |
10896 | ||
10897 | return finish_omp_clauses (nreverse (new_clauses)); | |
10898 | } | |
10899 | ||
f74dcfb7 JJ |
10900 | /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */ |
10901 | ||
10902 | static tree | |
10903 | tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain, | |
10904 | tree in_decl) | |
10905 | { | |
10906 | #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl) | |
10907 | ||
10908 | tree purpose, value, chain; | |
10909 | ||
10910 | if (t == NULL) | |
10911 | return t; | |
10912 | ||
10913 | if (TREE_CODE (t) != TREE_LIST) | |
10914 | return tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
10915 | /*function_p=*/false, |
10916 | /*integral_constant_expression_p=*/false); | |
f74dcfb7 JJ |
10917 | |
10918 | if (t == void_list_node) | |
10919 | return t; | |
10920 | ||
10921 | purpose = TREE_PURPOSE (t); | |
10922 | if (purpose) | |
10923 | purpose = RECUR (purpose); | |
10924 | value = TREE_VALUE (t); | |
1c384bf1 | 10925 | if (value && TREE_CODE (value) != LABEL_DECL) |
f74dcfb7 JJ |
10926 | value = RECUR (value); |
10927 | chain = TREE_CHAIN (t); | |
10928 | if (chain && chain != void_type_node) | |
10929 | chain = RECUR (chain); | |
10930 | return tree_cons (purpose, value, chain); | |
10931 | #undef RECUR | |
10932 | } | |
10933 | ||
a68ab351 JJ |
10934 | /* Substitute one OMP_FOR iterator. */ |
10935 | ||
10936 | static void | |
10937 | tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv, | |
10938 | tree condv, tree incrv, tree *clauses, | |
10939 | tree args, tsubst_flags_t complain, tree in_decl, | |
10940 | bool integral_constant_expression_p) | |
10941 | { | |
10942 | #define RECUR(NODE) \ | |
10943 | tsubst_expr ((NODE), args, complain, in_decl, \ | |
10944 | integral_constant_expression_p) | |
a2d4cdc9 | 10945 | tree decl, init, cond, incr, auto_node; |
a68ab351 JJ |
10946 | |
10947 | init = TREE_VEC_ELT (OMP_FOR_INIT (t), i); | |
10948 | gcc_assert (TREE_CODE (init) == MODIFY_EXPR); | |
10949 | decl = RECUR (TREE_OPERAND (init, 0)); | |
10950 | init = TREE_OPERAND (init, 1); | |
a2d4cdc9 JJ |
10951 | auto_node = type_uses_auto (TREE_TYPE (decl)); |
10952 | if (auto_node && init) | |
10953 | { | |
10954 | tree init_expr = init; | |
a2d4cdc9 JJ |
10955 | if (TREE_CODE (init_expr) == DECL_EXPR) |
10956 | init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr)); | |
749db154 | 10957 | init_expr = RECUR (init_expr); |
a2d4cdc9 JJ |
10958 | TREE_TYPE (decl) |
10959 | = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node); | |
a2d4cdc9 | 10960 | } |
a68ab351 JJ |
10961 | gcc_assert (!type_dependent_expression_p (decl)); |
10962 | ||
10963 | if (!CLASS_TYPE_P (TREE_TYPE (decl))) | |
10964 | { | |
10965 | cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i)); | |
10966 | incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); | |
10967 | if (TREE_CODE (incr) == MODIFY_EXPR) | |
10968 | incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR, | |
10969 | RECUR (TREE_OPERAND (incr, 1)), | |
10970 | complain); | |
10971 | else | |
10972 | incr = RECUR (incr); | |
10973 | TREE_VEC_ELT (declv, i) = decl; | |
10974 | TREE_VEC_ELT (initv, i) = init; | |
10975 | TREE_VEC_ELT (condv, i) = cond; | |
10976 | TREE_VEC_ELT (incrv, i) = incr; | |
10977 | return; | |
10978 | } | |
10979 | ||
10980 | if (init && TREE_CODE (init) != DECL_EXPR) | |
10981 | { | |
10982 | tree c; | |
10983 | for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c)) | |
10984 | { | |
10985 | if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE | |
10986 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE) | |
10987 | && OMP_CLAUSE_DECL (c) == decl) | |
10988 | break; | |
10989 | else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE | |
10990 | && OMP_CLAUSE_DECL (c) == decl) | |
10991 | error ("iteration variable %qD should not be firstprivate", decl); | |
10992 | else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION | |
10993 | && OMP_CLAUSE_DECL (c) == decl) | |
10994 | error ("iteration variable %qD should not be reduction", decl); | |
10995 | } | |
10996 | if (c == NULL) | |
10997 | { | |
c2255bc4 | 10998 | c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE); |
a68ab351 JJ |
10999 | OMP_CLAUSE_DECL (c) = decl; |
11000 | c = finish_omp_clauses (c); | |
11001 | if (c) | |
11002 | { | |
11003 | OMP_CLAUSE_CHAIN (c) = *clauses; | |
11004 | *clauses = c; | |
11005 | } | |
11006 | } | |
11007 | } | |
11008 | cond = TREE_VEC_ELT (OMP_FOR_COND (t), i); | |
11009 | if (COMPARISON_CLASS_P (cond)) | |
11010 | cond = build2 (TREE_CODE (cond), boolean_type_node, | |
11011 | RECUR (TREE_OPERAND (cond, 0)), | |
11012 | RECUR (TREE_OPERAND (cond, 1))); | |
11013 | else | |
11014 | cond = RECUR (cond); | |
11015 | incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); | |
11016 | switch (TREE_CODE (incr)) | |
11017 | { | |
11018 | case PREINCREMENT_EXPR: | |
11019 | case PREDECREMENT_EXPR: | |
11020 | case POSTINCREMENT_EXPR: | |
11021 | case POSTDECREMENT_EXPR: | |
11022 | incr = build2 (TREE_CODE (incr), TREE_TYPE (decl), | |
11023 | RECUR (TREE_OPERAND (incr, 0)), NULL_TREE); | |
11024 | break; | |
11025 | case MODIFY_EXPR: | |
11026 | if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR | |
11027 | || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) | |
11028 | { | |
11029 | tree rhs = TREE_OPERAND (incr, 1); | |
11030 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), | |
11031 | RECUR (TREE_OPERAND (incr, 0)), | |
11032 | build2 (TREE_CODE (rhs), TREE_TYPE (decl), | |
11033 | RECUR (TREE_OPERAND (rhs, 0)), | |
11034 | RECUR (TREE_OPERAND (rhs, 1)))); | |
11035 | } | |
11036 | else | |
11037 | incr = RECUR (incr); | |
11038 | break; | |
11039 | case MODOP_EXPR: | |
11040 | if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR | |
11041 | || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) | |
11042 | { | |
11043 | tree lhs = RECUR (TREE_OPERAND (incr, 0)); | |
11044 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs, | |
11045 | build2 (TREE_CODE (TREE_OPERAND (incr, 1)), | |
11046 | TREE_TYPE (decl), lhs, | |
11047 | RECUR (TREE_OPERAND (incr, 2)))); | |
11048 | } | |
11049 | else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR | |
11050 | && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR | |
11051 | || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR))) | |
11052 | { | |
11053 | tree rhs = TREE_OPERAND (incr, 2); | |
11054 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), | |
11055 | RECUR (TREE_OPERAND (incr, 0)), | |
11056 | build2 (TREE_CODE (rhs), TREE_TYPE (decl), | |
11057 | RECUR (TREE_OPERAND (rhs, 0)), | |
11058 | RECUR (TREE_OPERAND (rhs, 1)))); | |
11059 | } | |
11060 | else | |
11061 | incr = RECUR (incr); | |
11062 | break; | |
11063 | default: | |
11064 | incr = RECUR (incr); | |
11065 | break; | |
11066 | } | |
11067 | ||
11068 | TREE_VEC_ELT (declv, i) = decl; | |
11069 | TREE_VEC_ELT (initv, i) = init; | |
11070 | TREE_VEC_ELT (condv, i) = cond; | |
11071 | TREE_VEC_ELT (incrv, i) = incr; | |
11072 | #undef RECUR | |
11073 | } | |
11074 | ||
cc23546e JO |
11075 | /* Like tsubst_copy for expressions, etc. but also does semantic |
11076 | processing. */ | |
00d3396f | 11077 | |
14d22dd6 | 11078 | static tree |
015c2c66 MM |
11079 | tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, |
11080 | bool integral_constant_expression_p) | |
5566b478 | 11081 | { |
015c2c66 MM |
11082 | #define RECUR(NODE) \ |
11083 | tsubst_expr ((NODE), args, complain, in_decl, \ | |
11084 | integral_constant_expression_p) | |
11085 | ||
fd10dd09 | 11086 | tree stmt, tmp; |
558475f0 | 11087 | |
5566b478 MS |
11088 | if (t == NULL_TREE || t == error_mark_node) |
11089 | return t; | |
11090 | ||
93409b8c PB |
11091 | if (EXPR_HAS_LOCATION (t)) |
11092 | input_location = EXPR_LOCATION (t); | |
7c34ced1 RH |
11093 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
11094 | current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t); | |
11095 | ||
5566b478 | 11096 | switch (TREE_CODE (t)) |
8d08fdba | 11097 | { |
325c3691 RH |
11098 | case STATEMENT_LIST: |
11099 | { | |
11100 | tree_stmt_iterator i; | |
11101 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
015c2c66 | 11102 | RECUR (tsi_stmt (i)); |
325c3691 RH |
11103 | break; |
11104 | } | |
11105 | ||
558475f0 | 11106 | case CTOR_INITIALIZER: |
c8094d83 | 11107 | finish_mem_initializers (tsubst_initializer_list |
2282d28d MM |
11108 | (TREE_OPERAND (t, 0), args)); |
11109 | break; | |
558475f0 | 11110 | |
5088b058 | 11111 | case RETURN_EXPR: |
015c2c66 | 11112 | finish_return_stmt (RECUR (TREE_OPERAND (t, 0))); |
5566b478 MS |
11113 | break; |
11114 | ||
11115 | case EXPR_STMT: | |
015c2c66 | 11116 | tmp = RECUR (EXPR_STMT_EXPR (t)); |
7c34ced1 RH |
11117 | if (EXPR_STMT_STMT_EXPR_RESULT (t)) |
11118 | finish_stmt_expr_expr (tmp, cur_stmt_expr); | |
11119 | else | |
11120 | finish_expr_stmt (tmp); | |
11121 | break; | |
5566b478 | 11122 | |
9da99f7d | 11123 | case USING_STMT: |
015c2c66 | 11124 | do_using_directive (RECUR (USING_STMT_NAMESPACE (t))); |
9da99f7d | 11125 | break; |
c8094d83 | 11126 | |
350fae66 | 11127 | case DECL_EXPR: |
5566b478 | 11128 | { |
e0942dcd MM |
11129 | tree decl; |
11130 | tree init; | |
5566b478 | 11131 | |
350fae66 | 11132 | decl = DECL_EXPR_DECL (t); |
acef433b MM |
11133 | if (TREE_CODE (decl) == LABEL_DECL) |
11134 | finish_label_decl (DECL_NAME (decl)); | |
fbfe8c9e NS |
11135 | else if (TREE_CODE (decl) == USING_DECL) |
11136 | { | |
98ed9dae | 11137 | tree scope = USING_DECL_SCOPE (decl); |
fbfe8c9e | 11138 | tree name = DECL_NAME (decl); |
22038b2c | 11139 | tree decl; |
c8094d83 | 11140 | |
015c2c66 | 11141 | scope = RECUR (scope); |
22038b2c | 11142 | decl = lookup_qualified_name (scope, name, |
12483c9f NS |
11143 | /*is_type_p=*/false, |
11144 | /*complain=*/false); | |
8f78f01f | 11145 | if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) |
2b7a3abf | 11146 | qualified_name_lookup_error (scope, name, decl, input_location); |
22038b2c | 11147 | else |
ed5f054f | 11148 | do_local_using_decl (decl, scope, name); |
fbfe8c9e | 11149 | } |
acef433b MM |
11150 | else |
11151 | { | |
11152 | init = DECL_INITIAL (decl); | |
11153 | decl = tsubst (decl, args, complain, in_decl); | |
ce2e5191 NS |
11154 | if (decl != error_mark_node) |
11155 | { | |
0cbd7506 MS |
11156 | /* By marking the declaration as instantiated, we avoid |
11157 | trying to instantiate it. Since instantiate_decl can't | |
11158 | handle local variables, and since we've already done | |
11159 | all that needs to be done, that's the right thing to | |
11160 | do. */ | |
11161 | if (TREE_CODE (decl) == VAR_DECL) | |
11162 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
edebf865 MM |
11163 | if (TREE_CODE (decl) == VAR_DECL |
11164 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) | |
11165 | /* Anonymous aggregates are a special case. */ | |
11166 | finish_anon_union (decl); | |
c8094d83 | 11167 | else |
ed2fa432 | 11168 | { |
edebf865 | 11169 | maybe_push_decl (decl); |
39703eb9 MM |
11170 | if (TREE_CODE (decl) == VAR_DECL |
11171 | && DECL_PRETTY_FUNCTION_P (decl)) | |
edebf865 MM |
11172 | { |
11173 | /* For __PRETTY_FUNCTION__ we have to adjust the | |
11174 | initializer. */ | |
11175 | const char *const name | |
11176 | = cxx_printable_name (current_function_decl, 2); | |
bb885938 | 11177 | init = cp_fname_init (name, &TREE_TYPE (decl)); |
edebf865 MM |
11178 | } |
11179 | else | |
268127ce DG |
11180 | { |
11181 | tree t = RECUR (init); | |
11182 | ||
11183 | if (init && !t) | |
11184 | /* If we had an initializer but it | |
11185 | instantiated to nothing, | |
11186 | value-initialize the object. This will | |
11187 | only occur when the initializer was a | |
11188 | pack expansion where the parameter packs | |
11189 | used in that expansion were of length | |
11190 | zero. */ | |
9dbd4406 | 11191 | init = build_value_init (TREE_TYPE (decl)); |
268127ce DG |
11192 | else |
11193 | init = t; | |
11194 | } | |
11195 | ||
3600f678 | 11196 | cp_finish_decl (decl, init, false, NULL_TREE, 0); |
ed2fa432 | 11197 | } |
ce2e5191 | 11198 | } |
acef433b | 11199 | } |
fd10dd09 | 11200 | |
350fae66 | 11201 | /* A DECL_EXPR can also be used as an expression, in the condition |
325c3691 RH |
11202 | clause of an if/for/while construct. */ |
11203 | return decl; | |
5566b478 | 11204 | } |
8d08fdba | 11205 | |
5566b478 | 11206 | case FOR_STMT: |
7c34ced1 | 11207 | stmt = begin_for_stmt (); |
015c2c66 | 11208 | RECUR (FOR_INIT_STMT (t)); |
7c34ced1 | 11209 | finish_for_init_stmt (stmt); |
015c2c66 | 11210 | tmp = RECUR (FOR_COND (t)); |
7c34ced1 | 11211 | finish_for_cond (tmp, stmt); |
015c2c66 | 11212 | tmp = RECUR (FOR_EXPR (t)); |
7c34ced1 | 11213 | finish_for_expr (tmp, stmt); |
015c2c66 | 11214 | RECUR (FOR_BODY (t)); |
7c34ced1 | 11215 | finish_for_stmt (stmt); |
5566b478 | 11216 | break; |
8d08fdba | 11217 | |
5566b478 | 11218 | case WHILE_STMT: |
7c34ced1 | 11219 | stmt = begin_while_stmt (); |
015c2c66 | 11220 | tmp = RECUR (WHILE_COND (t)); |
7c34ced1 | 11221 | finish_while_stmt_cond (tmp, stmt); |
015c2c66 | 11222 | RECUR (WHILE_BODY (t)); |
7c34ced1 | 11223 | finish_while_stmt (stmt); |
5566b478 | 11224 | break; |
8d08fdba | 11225 | |
5566b478 | 11226 | case DO_STMT: |
7c34ced1 | 11227 | stmt = begin_do_stmt (); |
015c2c66 | 11228 | RECUR (DO_BODY (t)); |
7c34ced1 | 11229 | finish_do_body (stmt); |
015c2c66 | 11230 | tmp = RECUR (DO_COND (t)); |
7c34ced1 | 11231 | finish_do_stmt (tmp, stmt); |
5566b478 | 11232 | break; |
a0a33927 | 11233 | |
5566b478 | 11234 | case IF_STMT: |
7c34ced1 | 11235 | stmt = begin_if_stmt (); |
015c2c66 | 11236 | tmp = RECUR (IF_COND (t)); |
7c34ced1 | 11237 | finish_if_stmt_cond (tmp, stmt); |
015c2c66 | 11238 | RECUR (THEN_CLAUSE (t)); |
7c34ced1 | 11239 | finish_then_clause (stmt); |
8d08fdba | 11240 | |
7c34ced1 RH |
11241 | if (ELSE_CLAUSE (t)) |
11242 | { | |
11243 | begin_else_clause (stmt); | |
015c2c66 | 11244 | RECUR (ELSE_CLAUSE (t)); |
7c34ced1 RH |
11245 | finish_else_clause (stmt); |
11246 | } | |
11247 | ||
11248 | finish_if_stmt (stmt); | |
5566b478 | 11249 | break; |
8d08fdba | 11250 | |
5882f0f3 | 11251 | case BIND_EXPR: |
7c34ced1 RH |
11252 | if (BIND_EXPR_BODY_BLOCK (t)) |
11253 | stmt = begin_function_body (); | |
11254 | else | |
11255 | stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t) | |
11256 | ? BCS_TRY_BLOCK : 0); | |
ade3dc07 | 11257 | |
015c2c66 | 11258 | RECUR (BIND_EXPR_BODY (t)); |
ade3dc07 | 11259 | |
7c34ced1 RH |
11260 | if (BIND_EXPR_BODY_BLOCK (t)) |
11261 | finish_function_body (stmt); | |
11262 | else | |
11263 | finish_compound_stmt (stmt); | |
5566b478 | 11264 | break; |
8d08fdba | 11265 | |
5566b478 | 11266 | case BREAK_STMT: |
ad321293 | 11267 | finish_break_stmt (); |
5566b478 | 11268 | break; |
8d08fdba | 11269 | |
6467930b | 11270 | case CONTINUE_STMT: |
ad321293 | 11271 | finish_continue_stmt (); |
6467930b MS |
11272 | break; |
11273 | ||
5566b478 | 11274 | case SWITCH_STMT: |
7c34ced1 | 11275 | stmt = begin_switch_stmt (); |
015c2c66 | 11276 | tmp = RECUR (SWITCH_STMT_COND (t)); |
7c34ced1 | 11277 | finish_switch_cond (tmp, stmt); |
015c2c66 | 11278 | RECUR (SWITCH_STMT_BODY (t)); |
7c34ced1 | 11279 | finish_switch_stmt (stmt); |
5566b478 MS |
11280 | break; |
11281 | ||
8c161995 | 11282 | case CASE_LABEL_EXPR: |
c2255bc4 AH |
11283 | finish_case_label (EXPR_LOCATION (t), |
11284 | RECUR (CASE_LOW (t)), | |
015c2c66 | 11285 | RECUR (CASE_HIGH (t))); |
5566b478 MS |
11286 | break; |
11287 | ||
9e14e18f | 11288 | case LABEL_EXPR: |
5bca4e80 ILT |
11289 | { |
11290 | tree decl = LABEL_EXPR_LABEL (t); | |
11291 | tree label; | |
11292 | ||
11293 | label = finish_label_stmt (DECL_NAME (decl)); | |
11294 | if (DECL_ATTRIBUTES (decl) != NULL_TREE) | |
11295 | cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0); | |
11296 | } | |
5566b478 MS |
11297 | break; |
11298 | ||
9e14e18f | 11299 | case GOTO_EXPR: |
fd10dd09 JM |
11300 | tmp = GOTO_DESTINATION (t); |
11301 | if (TREE_CODE (tmp) != LABEL_DECL) | |
aa09da44 MM |
11302 | /* Computed goto's must be tsubst'd into. On the other hand, |
11303 | non-computed gotos must not be; the identifier in question | |
11304 | will have no binding. */ | |
015c2c66 | 11305 | tmp = RECUR (tmp); |
3fa56191 | 11306 | else |
fd10dd09 JM |
11307 | tmp = DECL_NAME (tmp); |
11308 | finish_goto_stmt (tmp); | |
ad321293 MM |
11309 | break; |
11310 | ||
e130a54b | 11311 | case ASM_EXPR: |
c87978aa | 11312 | tmp = finish_asm_stmt |
6de9cd9a | 11313 | (ASM_VOLATILE_P (t), |
015c2c66 | 11314 | RECUR (ASM_STRING (t)), |
f74dcfb7 JJ |
11315 | tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl), |
11316 | tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl), | |
1c384bf1 RH |
11317 | tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl), |
11318 | tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl)); | |
131263fa AP |
11319 | { |
11320 | tree asm_expr = tmp; | |
11321 | if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR) | |
11322 | asm_expr = TREE_OPERAND (asm_expr, 0); | |
11323 | ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t); | |
11324 | } | |
5566b478 | 11325 | break; |
faf5394a MS |
11326 | |
11327 | case TRY_BLOCK: | |
f1dedc31 | 11328 | if (CLEANUP_P (t)) |
62409b39 | 11329 | { |
57b52417 | 11330 | stmt = begin_try_block (); |
015c2c66 | 11331 | RECUR (TRY_STMTS (t)); |
62409b39 | 11332 | finish_cleanup_try_block (stmt); |
015c2c66 | 11333 | finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt); |
62409b39 | 11334 | } |
f1dedc31 MM |
11335 | else |
11336 | { | |
eaf6fb90 MM |
11337 | tree compound_stmt = NULL_TREE; |
11338 | ||
62409b39 | 11339 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 11340 | stmt = begin_function_try_block (&compound_stmt); |
62409b39 MM |
11341 | else |
11342 | stmt = begin_try_block (); | |
11343 | ||
015c2c66 | 11344 | RECUR (TRY_STMTS (t)); |
62409b39 MM |
11345 | |
11346 | if (FN_TRY_BLOCK_P (t)) | |
11347 | finish_function_try_block (stmt); | |
11348 | else | |
11349 | finish_try_block (stmt); | |
11350 | ||
015c2c66 | 11351 | RECUR (TRY_HANDLERS (t)); |
84df082b | 11352 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 11353 | finish_function_handler_sequence (stmt, compound_stmt); |
84df082b MM |
11354 | else |
11355 | finish_handler_sequence (stmt); | |
f1dedc31 | 11356 | } |
faf5394a | 11357 | break; |
c8094d83 | 11358 | |
faf5394a | 11359 | case HANDLER: |
b35d4555 | 11360 | { |
3a2419a7 | 11361 | tree decl = HANDLER_PARMS (t); |
b35d4555 | 11362 | |
3a2419a7 | 11363 | if (decl) |
b35d4555 | 11364 | { |
b35d4555 | 11365 | decl = tsubst (decl, args, complain, in_decl); |
f8191e64 MM |
11366 | /* Prevent instantiate_decl from trying to instantiate |
11367 | this variable. We've already done all that needs to be | |
11368 | done. */ | |
3a2419a7 VR |
11369 | if (decl != error_mark_node) |
11370 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
b35d4555 | 11371 | } |
3a2419a7 | 11372 | stmt = begin_handler (); |
1a6025b4 | 11373 | finish_handler_parms (decl, stmt); |
015c2c66 | 11374 | RECUR (HANDLER_BODY (t)); |
1a6025b4 | 11375 | finish_handler (stmt); |
b35d4555 | 11376 | } |
faf5394a MS |
11377 | break; |
11378 | ||
b87692e5 | 11379 | case TAG_DEFN: |
fd10dd09 | 11380 | tsubst (TREE_TYPE (t), args, complain, NULL_TREE); |
b87692e5 | 11381 | break; |
55a3debe DG |
11382 | |
11383 | case STATIC_ASSERT: | |
11384 | { | |
11385 | tree condition = | |
11386 | tsubst_expr (STATIC_ASSERT_CONDITION (t), | |
11387 | args, | |
11388 | complain, in_decl, | |
11389 | /*integral_constant_expression_p=*/true); | |
11390 | finish_static_assert (condition, | |
11391 | STATIC_ASSERT_MESSAGE (t), | |
11392 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
11393 | /*member_p=*/false); | |
11394 | } | |
11395 | break; | |
b87692e5 | 11396 | |
1799e5d5 RH |
11397 | case OMP_PARALLEL: |
11398 | tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), | |
11399 | args, complain, in_decl); | |
11400 | stmt = begin_omp_parallel (); | |
015c2c66 | 11401 | RECUR (OMP_PARALLEL_BODY (t)); |
761041be JJ |
11402 | OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt)) |
11403 | = OMP_PARALLEL_COMBINED (t); | |
1799e5d5 RH |
11404 | break; |
11405 | ||
a68ab351 JJ |
11406 | case OMP_TASK: |
11407 | tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), | |
11408 | args, complain, in_decl); | |
11409 | stmt = begin_omp_task (); | |
11410 | RECUR (OMP_TASK_BODY (t)); | |
11411 | finish_omp_task (tmp, stmt); | |
11412 | break; | |
11413 | ||
1799e5d5 RH |
11414 | case OMP_FOR: |
11415 | { | |
a68ab351 JJ |
11416 | tree clauses, body, pre_body; |
11417 | tree declv, initv, condv, incrv; | |
11418 | int i; | |
1799e5d5 RH |
11419 | |
11420 | clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), | |
11421 | args, complain, in_decl); | |
a68ab351 JJ |
11422 | declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); |
11423 | initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); | |
11424 | condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); | |
11425 | incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); | |
11426 | ||
11427 | for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++) | |
11428 | tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv, | |
11429 | &clauses, args, complain, in_decl, | |
11430 | integral_constant_expression_p); | |
1799e5d5 RH |
11431 | |
11432 | stmt = begin_omp_structured_block (); | |
11433 | ||
a68ab351 JJ |
11434 | for (i = 0; i < TREE_VEC_LENGTH (initv); i++) |
11435 | if (TREE_VEC_ELT (initv, i) == NULL | |
11436 | || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR) | |
11437 | TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i)); | |
11438 | else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i)))) | |
11439 | { | |
11440 | tree init = RECUR (TREE_VEC_ELT (initv, i)); | |
11441 | gcc_assert (init == TREE_VEC_ELT (declv, i)); | |
11442 | TREE_VEC_ELT (initv, i) = NULL_TREE; | |
11443 | } | |
11444 | else | |
11445 | { | |
11446 | tree decl_expr = TREE_VEC_ELT (initv, i); | |
11447 | tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr)); | |
11448 | gcc_assert (init != NULL); | |
11449 | TREE_VEC_ELT (initv, i) = RECUR (init); | |
11450 | DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL; | |
11451 | RECUR (decl_expr); | |
11452 | DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init; | |
11453 | } | |
11454 | ||
1799e5d5 | 11455 | pre_body = push_stmt_list (); |
015c2c66 | 11456 | RECUR (OMP_FOR_PRE_BODY (t)); |
1799e5d5 RH |
11457 | pre_body = pop_stmt_list (pre_body); |
11458 | ||
11459 | body = push_stmt_list (); | |
015c2c66 | 11460 | RECUR (OMP_FOR_BODY (t)); |
1799e5d5 RH |
11461 | body = pop_stmt_list (body); |
11462 | ||
a68ab351 JJ |
11463 | t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv, |
11464 | body, pre_body, clauses); | |
1799e5d5 RH |
11465 | |
11466 | add_stmt (finish_omp_structured_block (stmt)); | |
11467 | } | |
11468 | break; | |
11469 | ||
11470 | case OMP_SECTIONS: | |
11471 | case OMP_SINGLE: | |
11472 | tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl); | |
11473 | stmt = push_stmt_list (); | |
015c2c66 | 11474 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
11475 | stmt = pop_stmt_list (stmt); |
11476 | ||
11477 | t = copy_node (t); | |
11478 | OMP_BODY (t) = stmt; | |
11479 | OMP_CLAUSES (t) = tmp; | |
11480 | add_stmt (t); | |
11481 | break; | |
11482 | ||
11483 | case OMP_SECTION: | |
11484 | case OMP_CRITICAL: | |
11485 | case OMP_MASTER: | |
11486 | case OMP_ORDERED: | |
11487 | stmt = push_stmt_list (); | |
015c2c66 | 11488 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
11489 | stmt = pop_stmt_list (stmt); |
11490 | ||
11491 | t = copy_node (t); | |
11492 | OMP_BODY (t) = stmt; | |
11493 | add_stmt (t); | |
11494 | break; | |
11495 | ||
11496 | case OMP_ATOMIC: | |
239371f9 JJ |
11497 | gcc_assert (OMP_ATOMIC_DEPENDENT_P (t)); |
11498 | { | |
11499 | tree op1 = TREE_OPERAND (t, 1); | |
11500 | tree lhs = RECUR (TREE_OPERAND (op1, 0)); | |
11501 | tree rhs = RECUR (TREE_OPERAND (op1, 1)); | |
11502 | finish_omp_atomic (TREE_CODE (op1), lhs, rhs); | |
11503 | } | |
1799e5d5 RH |
11504 | break; |
11505 | ||
5d80a306 DG |
11506 | case EXPR_PACK_EXPANSION: |
11507 | error ("invalid use of pack expansion expression"); | |
11508 | return error_mark_node; | |
11509 | ||
11510 | case NONTYPE_ARGUMENT_PACK: | |
11511 | error ("use %<...%> to expand argument pack"); | |
11512 | return error_mark_node; | |
11513 | ||
5566b478 | 11514 | default: |
315fb5db | 11515 | gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); |
c8094d83 | 11516 | |
315fb5db | 11517 | return tsubst_copy_and_build (t, args, complain, in_decl, |
015c2c66 MM |
11518 | /*function_p=*/false, |
11519 | integral_constant_expression_p); | |
5566b478 | 11520 | } |
fd10dd09 | 11521 | |
325c3691 | 11522 | return NULL_TREE; |
015c2c66 | 11523 | #undef RECUR |
8d08fdba MS |
11524 | } |
11525 | ||
b3445994 MM |
11526 | /* T is a postfix-expression that is not being used in a function |
11527 | call. Return the substituted version of T. */ | |
11528 | ||
11529 | static tree | |
c8094d83 | 11530 | tsubst_non_call_postfix_expression (tree t, tree args, |
b3445994 MM |
11531 | tsubst_flags_t complain, |
11532 | tree in_decl) | |
11533 | { | |
11534 | if (TREE_CODE (t) == SCOPE_REF) | |
11535 | t = tsubst_qualified_id (t, args, complain, in_decl, | |
11536 | /*done=*/false, /*address_p=*/false); | |
11537 | else | |
11538 | t = tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
11539 | /*function_p=*/false, |
11540 | /*integral_constant_expression_p=*/false); | |
b3445994 MM |
11541 | |
11542 | return t; | |
11543 | } | |
11544 | ||
cc23546e | 11545 | /* Like tsubst but deals with expressions and performs semantic |
b3445994 | 11546 | analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */ |
cc23546e | 11547 | |
ee76b931 | 11548 | tree |
c8094d83 | 11549 | tsubst_copy_and_build (tree t, |
0cbd7506 MS |
11550 | tree args, |
11551 | tsubst_flags_t complain, | |
11552 | tree in_decl, | |
015c2c66 MM |
11553 | bool function_p, |
11554 | bool integral_constant_expression_p) | |
cc23546e | 11555 | { |
015c2c66 MM |
11556 | #define RECUR(NODE) \ |
11557 | tsubst_copy_and_build (NODE, args, complain, in_decl, \ | |
11558 | /*function_p=*/false, \ | |
11559 | integral_constant_expression_p) | |
b3445994 | 11560 | |
ee76b931 MM |
11561 | tree op1; |
11562 | ||
cc23546e JO |
11563 | if (t == NULL_TREE || t == error_mark_node) |
11564 | return t; | |
11565 | ||
11566 | switch (TREE_CODE (t)) | |
11567 | { | |
399dedb9 NS |
11568 | case USING_DECL: |
11569 | t = DECL_NAME (t); | |
852dcbdd | 11570 | /* Fall through. */ |
b3445994 | 11571 | case IDENTIFIER_NODE: |
cc23546e | 11572 | { |
b3445994 | 11573 | tree decl; |
b3445994 | 11574 | cp_id_kind idk; |
67c03833 | 11575 | bool non_integral_constant_expression_p; |
b3445994 MM |
11576 | const char *error_msg; |
11577 | ||
b3445994 | 11578 | if (IDENTIFIER_TYPENAME_P (t)) |
cc23546e | 11579 | { |
b3445994 MM |
11580 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
11581 | t = mangle_conv_op_name_for_type (new_type); | |
cc23546e | 11582 | } |
b3445994 MM |
11583 | |
11584 | /* Look up the name. */ | |
10e6657a | 11585 | decl = lookup_name (t); |
b3445994 MM |
11586 | |
11587 | /* By convention, expressions use ERROR_MARK_NODE to indicate | |
11588 | failure, not NULL_TREE. */ | |
11589 | if (decl == NULL_TREE) | |
11590 | decl = error_mark_node; | |
11591 | ||
10b1d5e7 | 11592 | decl = finish_id_expression (t, decl, NULL_TREE, |
b3445994 | 11593 | &idk, |
015c2c66 | 11594 | integral_constant_expression_p, |
67c03833 JM |
11595 | /*allow_non_integral_constant_expression_p=*/false, |
11596 | &non_integral_constant_expression_p, | |
02ed62dd MM |
11597 | /*template_p=*/false, |
11598 | /*done=*/true, | |
11599 | /*address_p=*/false, | |
11600 | /*template_arg_p=*/false, | |
2b7a3abf DS |
11601 | &error_msg, |
11602 | input_location); | |
b3445994 MM |
11603 | if (error_msg) |
11604 | error (error_msg); | |
11605 | if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE) | |
11606 | decl = unqualified_name_lookup_error (decl); | |
11607 | return decl; | |
cc23546e JO |
11608 | } |
11609 | ||
11610 | case TEMPLATE_ID_EXPR: | |
11611 | { | |
11612 | tree object; | |
be93747e | 11613 | tree templ = RECUR (TREE_OPERAND (t, 0)); |
bf12d54d NS |
11614 | tree targs = TREE_OPERAND (t, 1); |
11615 | ||
11616 | if (targs) | |
11617 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 11618 | |
be93747e | 11619 | if (TREE_CODE (templ) == COMPONENT_REF) |
cc23546e | 11620 | { |
be93747e KG |
11621 | object = TREE_OPERAND (templ, 0); |
11622 | templ = TREE_OPERAND (templ, 1); | |
cc23546e JO |
11623 | } |
11624 | else | |
11625 | object = NULL_TREE; | |
be93747e | 11626 | templ = lookup_template_function (templ, targs); |
c8094d83 | 11627 | |
cc23546e | 11628 | if (object) |
be93747e KG |
11629 | return build3 (COMPONENT_REF, TREE_TYPE (templ), |
11630 | object, templ, NULL_TREE); | |
cc23546e | 11631 | else |
be93747e | 11632 | return baselink_for_fns (templ); |
cc23546e JO |
11633 | } |
11634 | ||
11635 | case INDIRECT_REF: | |
db24eb1f NS |
11636 | { |
11637 | tree r = RECUR (TREE_OPERAND (t, 0)); | |
11638 | ||
11639 | if (REFERENCE_REF_P (t)) | |
11640 | { | |
e8c66fe0 NS |
11641 | /* A type conversion to reference type will be enclosed in |
11642 | such an indirect ref, but the substitution of the cast | |
11643 | will have also added such an indirect ref. */ | |
11644 | if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE) | |
11645 | r = convert_from_reference (r); | |
db24eb1f NS |
11646 | } |
11647 | else | |
5ade1ed2 | 11648 | r = build_x_indirect_ref (r, "unary *", complain); |
db24eb1f NS |
11649 | return r; |
11650 | } | |
cc23546e | 11651 | |
0da99d4e GB |
11652 | case NOP_EXPR: |
11653 | return build_nop | |
11654 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
11655 | RECUR (TREE_OPERAND (t, 0))); | |
11656 | ||
cc23546e | 11657 | case CAST_EXPR: |
cc23546e | 11658 | case REINTERPRET_CAST_EXPR: |
cc23546e | 11659 | case CONST_CAST_EXPR: |
cc23546e | 11660 | case DYNAMIC_CAST_EXPR: |
cc23546e | 11661 | case STATIC_CAST_EXPR: |
015c2c66 MM |
11662 | { |
11663 | tree type; | |
11664 | tree op; | |
11665 | ||
11666 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
11667 | if (integral_constant_expression_p | |
11668 | && !cast_valid_in_integral_constant_expression_p (type)) | |
11669 | { | |
5ade1ed2 DG |
11670 | if (complain & tf_error) |
11671 | error ("a cast to a type other than an integral or " | |
11672 | "enumeration type cannot appear in a constant-expression"); | |
015c2c66 MM |
11673 | return error_mark_node; |
11674 | } | |
11675 | ||
11676 | op = RECUR (TREE_OPERAND (t, 0)); | |
11677 | ||
11678 | switch (TREE_CODE (t)) | |
11679 | { | |
11680 | case CAST_EXPR: | |
5ade1ed2 | 11681 | return build_functional_cast (type, op, complain); |
015c2c66 | 11682 | case REINTERPRET_CAST_EXPR: |
5ade1ed2 | 11683 | return build_reinterpret_cast (type, op, complain); |
015c2c66 | 11684 | case CONST_CAST_EXPR: |
5ade1ed2 | 11685 | return build_const_cast (type, op, complain); |
015c2c66 | 11686 | case DYNAMIC_CAST_EXPR: |
5ade1ed2 | 11687 | return build_dynamic_cast (type, op, complain); |
015c2c66 | 11688 | case STATIC_CAST_EXPR: |
5ade1ed2 | 11689 | return build_static_cast (type, op, complain); |
015c2c66 MM |
11690 | default: |
11691 | gcc_unreachable (); | |
11692 | } | |
11693 | } | |
cc23546e | 11694 | |
cc23546e JO |
11695 | case POSTDECREMENT_EXPR: |
11696 | case POSTINCREMENT_EXPR: | |
b3445994 MM |
11697 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
11698 | args, complain, in_decl); | |
5ade1ed2 | 11699 | return build_x_unary_op (TREE_CODE (t), op1, complain); |
ee76b931 MM |
11700 | |
11701 | case PREDECREMENT_EXPR: | |
11702 | case PREINCREMENT_EXPR: | |
cc23546e JO |
11703 | case NEGATE_EXPR: |
11704 | case BIT_NOT_EXPR: | |
cc23546e | 11705 | case ABS_EXPR: |
d17811fd | 11706 | case TRUTH_NOT_EXPR: |
392e3d51 | 11707 | case UNARY_PLUS_EXPR: /* Unary + */ |
d17811fd MM |
11708 | case REALPART_EXPR: |
11709 | case IMAGPART_EXPR: | |
5ade1ed2 DG |
11710 | return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)), |
11711 | complain); | |
cc23546e | 11712 | |
cc23546e | 11713 | case ADDR_EXPR: |
ee76b931 MM |
11714 | op1 = TREE_OPERAND (t, 0); |
11715 | if (TREE_CODE (op1) == SCOPE_REF) | |
c8094d83 | 11716 | op1 = tsubst_qualified_id (op1, args, complain, in_decl, |
ee76b931 MM |
11717 | /*done=*/true, /*address_p=*/true); |
11718 | else | |
c8094d83 | 11719 | op1 = tsubst_non_call_postfix_expression (op1, args, complain, |
b3445994 | 11720 | in_decl); |
fc2b8477 | 11721 | if (TREE_CODE (op1) == LABEL_DECL) |
6a3799eb AH |
11722 | return finish_label_address_expr (DECL_NAME (op1), |
11723 | EXPR_LOCATION (op1)); | |
5ade1ed2 | 11724 | return build_x_unary_op (ADDR_EXPR, op1, complain); |
ee76b931 | 11725 | |
cc23546e JO |
11726 | case PLUS_EXPR: |
11727 | case MINUS_EXPR: | |
11728 | case MULT_EXPR: | |
11729 | case TRUNC_DIV_EXPR: | |
11730 | case CEIL_DIV_EXPR: | |
11731 | case FLOOR_DIV_EXPR: | |
11732 | case ROUND_DIV_EXPR: | |
11733 | case EXACT_DIV_EXPR: | |
11734 | case BIT_AND_EXPR: | |
cc23546e JO |
11735 | case BIT_IOR_EXPR: |
11736 | case BIT_XOR_EXPR: | |
11737 | case TRUNC_MOD_EXPR: | |
11738 | case FLOOR_MOD_EXPR: | |
11739 | case TRUTH_ANDIF_EXPR: | |
11740 | case TRUTH_ORIF_EXPR: | |
11741 | case TRUTH_AND_EXPR: | |
11742 | case TRUTH_OR_EXPR: | |
11743 | case RSHIFT_EXPR: | |
11744 | case LSHIFT_EXPR: | |
11745 | case RROTATE_EXPR: | |
11746 | case LROTATE_EXPR: | |
11747 | case EQ_EXPR: | |
11748 | case NE_EXPR: | |
11749 | case MAX_EXPR: | |
11750 | case MIN_EXPR: | |
11751 | case LE_EXPR: | |
11752 | case GE_EXPR: | |
11753 | case LT_EXPR: | |
11754 | case GT_EXPR: | |
11755 | case MEMBER_REF: | |
b3445994 | 11756 | case DOTSTAR_EXPR: |
cc23546e | 11757 | return build_x_binary_op |
c8094d83 | 11758 | (TREE_CODE (t), |
b3445994 | 11759 | RECUR (TREE_OPERAND (t, 0)), |
2a67bec2 ILT |
11760 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) |
11761 | ? ERROR_MARK | |
11762 | : TREE_CODE (TREE_OPERAND (t, 0))), | |
ec835fb2 | 11763 | RECUR (TREE_OPERAND (t, 1)), |
2a67bec2 ILT |
11764 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) |
11765 | ? ERROR_MARK | |
11766 | : TREE_CODE (TREE_OPERAND (t, 1))), | |
5ade1ed2 DG |
11767 | /*overloaded_p=*/NULL, |
11768 | complain); | |
cc23546e JO |
11769 | |
11770 | case SCOPE_REF: | |
ee76b931 MM |
11771 | return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, |
11772 | /*address_p=*/false); | |
cc23546e | 11773 | case ARRAY_REF: |
b3445994 MM |
11774 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
11775 | args, complain, in_decl); | |
493e377c | 11776 | return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain); |
c8094d83 | 11777 | |
cc23546e | 11778 | case SIZEOF_EXPR: |
5d80a306 | 11779 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) |
063cc99c | 11780 | return tsubst_copy (t, args, complain, in_decl); |
5d80a306 DG |
11781 | /* Fall through */ |
11782 | ||
cc23546e | 11783 | case ALIGNOF_EXPR: |
d17811fd MM |
11784 | op1 = TREE_OPERAND (t, 0); |
11785 | if (!args) | |
11786 | { | |
11787 | /* When there are no ARGS, we are trying to evaluate a | |
11788 | non-dependent expression from the parser. Trying to do | |
11789 | the substitutions may not work. */ | |
11790 | if (!TYPE_P (op1)) | |
11791 | op1 = TREE_TYPE (op1); | |
11792 | } | |
11793 | else | |
11794 | { | |
7d882b83 ILT |
11795 | ++cp_unevaluated_operand; |
11796 | ++c_inhibit_evaluation_warnings; | |
015c2c66 MM |
11797 | op1 = tsubst_copy_and_build (op1, args, complain, in_decl, |
11798 | /*function_p=*/false, | |
11799 | /*integral_constant_expression_p=*/false); | |
7d882b83 ILT |
11800 | --cp_unevaluated_operand; |
11801 | --c_inhibit_evaluation_warnings; | |
d17811fd | 11802 | } |
7a18b933 | 11803 | if (TYPE_P (op1)) |
5ade1ed2 DG |
11804 | return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), |
11805 | complain & tf_error); | |
d17811fd | 11806 | else |
5ade1ed2 DG |
11807 | return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t), |
11808 | complain & tf_error); | |
cc23546e JO |
11809 | |
11810 | case MODOP_EXPR: | |
e4c2c34b JM |
11811 | { |
11812 | tree r = build_x_modify_expr | |
11813 | (RECUR (TREE_OPERAND (t, 0)), | |
11814 | TREE_CODE (TREE_OPERAND (t, 1)), | |
5ade1ed2 DG |
11815 | RECUR (TREE_OPERAND (t, 2)), |
11816 | complain); | |
bcf9a914 JM |
11817 | /* TREE_NO_WARNING must be set if either the expression was |
11818 | parenthesized or it uses an operator such as >>= rather | |
11819 | than plain assignment. In the former case, it was already | |
11820 | set and must be copied. In the latter case, | |
11821 | build_x_modify_expr sets it and it must not be reset | |
11822 | here. */ | |
11823 | if (TREE_NO_WARNING (t)) | |
11824 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); | |
e4c2c34b JM |
11825 | return r; |
11826 | } | |
cc23546e JO |
11827 | |
11828 | case ARROW_EXPR: | |
b3445994 MM |
11829 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
11830 | args, complain, in_decl); | |
ee76b931 MM |
11831 | /* Remember that there was a reference to this entity. */ |
11832 | if (DECL_P (op1)) | |
11833 | mark_used (op1); | |
11834 | return build_x_arrow (op1); | |
cc23546e JO |
11835 | |
11836 | case NEW_EXPR: | |
268127ce | 11837 | { |
c166b898 | 11838 | tree placement = RECUR (TREE_OPERAND (t, 0)); |
268127ce | 11839 | tree init = RECUR (TREE_OPERAND (t, 3)); |
c166b898 ILT |
11840 | VEC(tree,gc) *placement_vec; |
11841 | VEC(tree,gc) *init_vec; | |
11842 | tree ret; | |
268127ce | 11843 | |
c166b898 ILT |
11844 | if (placement == NULL_TREE) |
11845 | placement_vec = NULL; | |
11846 | else | |
11847 | { | |
11848 | placement_vec = make_tree_vector (); | |
11849 | for (; placement != NULL_TREE; placement = TREE_CHAIN (placement)) | |
11850 | VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement)); | |
11851 | } | |
268127ce | 11852 | |
c166b898 ILT |
11853 | /* If there was an initializer in the original tree, but it |
11854 | instantiated to an empty list, then we should pass a | |
11855 | non-NULL empty vector to tell build_new that it was an | |
11856 | empty initializer() rather than no initializer. This can | |
11857 | only happen when the initializer is a pack expansion whose | |
11858 | parameter packs are of length zero. */ | |
11859 | if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE) | |
11860 | init_vec = NULL; | |
11861 | else | |
11862 | { | |
11863 | init_vec = make_tree_vector (); | |
11864 | if (init == void_zero_node) | |
11865 | gcc_assert (init_vec != NULL); | |
11866 | else | |
11867 | { | |
11868 | for (; init != NULL_TREE; init = TREE_CHAIN (init)) | |
11869 | VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init)); | |
11870 | } | |
11871 | } | |
11872 | ||
11873 | ret = build_new (&placement_vec, | |
11874 | RECUR (TREE_OPERAND (t, 1)), | |
11875 | RECUR (TREE_OPERAND (t, 2)), | |
11876 | &init_vec, | |
11877 | NEW_EXPR_USE_GLOBAL (t), | |
11878 | complain); | |
11879 | ||
11880 | if (placement_vec != NULL) | |
11881 | release_tree_vector (placement_vec); | |
11882 | if (init_vec != NULL) | |
11883 | release_tree_vector (init_vec); | |
11884 | ||
11885 | return ret; | |
268127ce | 11886 | } |
cc23546e JO |
11887 | |
11888 | case DELETE_EXPR: | |
11889 | return delete_sanity | |
b3445994 MM |
11890 | (RECUR (TREE_OPERAND (t, 0)), |
11891 | RECUR (TREE_OPERAND (t, 1)), | |
cc23546e JO |
11892 | DELETE_EXPR_USE_VEC (t), |
11893 | DELETE_EXPR_USE_GLOBAL (t)); | |
11894 | ||
11895 | case COMPOUND_EXPR: | |
b3445994 | 11896 | return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)), |
5ade1ed2 DG |
11897 | RECUR (TREE_OPERAND (t, 1)), |
11898 | complain); | |
cc23546e | 11899 | |
ee76b931 | 11900 | case CALL_EXPR: |
cc23546e | 11901 | { |
ee76b931 | 11902 | tree function; |
c166b898 ILT |
11903 | VEC(tree,gc) *call_args; |
11904 | unsigned int nargs, i; | |
ee76b931 | 11905 | bool qualified_p; |
ee935db4 | 11906 | bool koenig_p; |
c166b898 | 11907 | tree ret; |
ee76b931 | 11908 | |
5039610b | 11909 | function = CALL_EXPR_FN (t); |
6d80c4b9 MM |
11910 | /* When we parsed the expression, we determined whether or |
11911 | not Koenig lookup should be performed. */ | |
11912 | koenig_p = KOENIG_LOOKUP_P (t); | |
ee76b931 | 11913 | if (TREE_CODE (function) == SCOPE_REF) |
cc23546e | 11914 | { |
ee76b931 MM |
11915 | qualified_p = true; |
11916 | function = tsubst_qualified_id (function, args, complain, in_decl, | |
c8094d83 | 11917 | /*done=*/false, |
ee76b931 | 11918 | /*address_p=*/false); |
cc23546e | 11919 | } |
ee76b931 | 11920 | else |
cc23546e | 11921 | { |
61e71a9e NS |
11922 | if (TREE_CODE (function) == COMPONENT_REF) |
11923 | { | |
11924 | tree op = TREE_OPERAND (function, 1); | |
11925 | ||
11926 | qualified_p = (TREE_CODE (op) == SCOPE_REF | |
11927 | || (BASELINK_P (op) | |
11928 | && BASELINK_QUALIFIED_P (op))); | |
11929 | } | |
11930 | else | |
11931 | qualified_p = false; | |
3db45ab5 | 11932 | |
c8094d83 | 11933 | function = tsubst_copy_and_build (function, args, complain, |
b3445994 | 11934 | in_decl, |
015c2c66 MM |
11935 | !qualified_p, |
11936 | integral_constant_expression_p); | |
11937 | ||
6d80c4b9 MM |
11938 | if (BASELINK_P (function)) |
11939 | qualified_p = true; | |
cc23546e | 11940 | } |
cc23546e | 11941 | |
c166b898 ILT |
11942 | nargs = call_expr_nargs (t); |
11943 | call_args = make_tree_vector (); | |
11944 | for (i = 0; i < nargs; ++i) | |
11945 | { | |
11946 | tree arg = CALL_EXPR_ARG (t, i); | |
11947 | ||
11948 | if (!PACK_EXPANSION_P (arg)) | |
11949 | VEC_safe_push (tree, gc, call_args, | |
11950 | RECUR (CALL_EXPR_ARG (t, i))); | |
11951 | else | |
11952 | { | |
11953 | /* Expand the pack expansion and push each entry onto | |
11954 | CALL_ARGS. */ | |
11955 | arg = tsubst_pack_expansion (arg, args, complain, in_decl); | |
11956 | if (TREE_CODE (arg) == TREE_VEC) | |
11957 | { | |
11958 | unsigned int len, j; | |
11959 | ||
11960 | len = TREE_VEC_LENGTH (arg); | |
11961 | for (j = 0; j < len; ++j) | |
11962 | { | |
11963 | tree value = TREE_VEC_ELT (arg, j); | |
11964 | if (value != NULL_TREE) | |
11965 | value = convert_from_reference (value); | |
11966 | VEC_safe_push (tree, gc, call_args, value); | |
11967 | } | |
11968 | } | |
11969 | else | |
11970 | { | |
11971 | /* A partial substitution. Add one entry. */ | |
11972 | VEC_safe_push (tree, gc, call_args, arg); | |
11973 | } | |
11974 | } | |
11975 | } | |
676e33ca MM |
11976 | |
11977 | /* We do not perform argument-dependent lookup if normal | |
11978 | lookup finds a non-function, in accordance with the | |
11979 | expected resolution of DR 218. */ | |
ee935db4 | 11980 | if (koenig_p |
44370687 MM |
11981 | && ((is_overloaded_fn (function) |
11982 | /* If lookup found a member function, the Koenig lookup is | |
11983 | not appropriate, even if an unqualified-name was used | |
11984 | to denote the function. */ | |
11985 | && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) | |
5094a795 JM |
11986 | || TREE_CODE (function) == IDENTIFIER_NODE) |
11987 | /* Only do this when substitution turns a dependent call | |
11988 | into a non-dependent call. */ | |
11989 | && type_dependent_expression_p_push (t) | |
11990 | && !any_type_dependent_arguments_p (call_args)) | |
6d80c4b9 MM |
11991 | function = perform_koenig_lookup (function, call_args); |
11992 | ||
11993 | if (TREE_CODE (function) == IDENTIFIER_NODE) | |
b3445994 | 11994 | { |
6d80c4b9 | 11995 | unqualified_name_lookup_error (function); |
c166b898 | 11996 | release_tree_vector (call_args); |
6d80c4b9 | 11997 | return error_mark_node; |
b3445994 MM |
11998 | } |
11999 | ||
12000 | /* Remember that there was a reference to this entity. */ | |
12001 | if (DECL_P (function)) | |
12002 | mark_used (function); | |
12003 | ||
d17811fd | 12004 | if (TREE_CODE (function) == OFFSET_REF) |
c166b898 ILT |
12005 | ret = build_offset_ref_call_from_tree (function, &call_args); |
12006 | else if (TREE_CODE (function) == COMPONENT_REF) | |
9f880ef9 MM |
12007 | { |
12008 | if (!BASELINK_P (TREE_OPERAND (function, 1))) | |
c166b898 | 12009 | ret = finish_call_expr (function, &call_args, |
9f880ef9 | 12010 | /*disallow_virtual=*/false, |
5ade1ed2 DG |
12011 | /*koenig_p=*/false, |
12012 | complain); | |
9f880ef9 | 12013 | else |
c166b898 | 12014 | ret = (build_new_method_call |
9f880ef9 MM |
12015 | (TREE_OPERAND (function, 0), |
12016 | TREE_OPERAND (function, 1), | |
c166b898 | 12017 | &call_args, NULL_TREE, |
63c9a190 | 12018 | qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL, |
5ade1ed2 DG |
12019 | /*fn_p=*/NULL, |
12020 | complain)); | |
9f880ef9 | 12021 | } |
c166b898 ILT |
12022 | else |
12023 | ret = finish_call_expr (function, &call_args, | |
12024 | /*disallow_virtual=*/qualified_p, | |
12025 | koenig_p, | |
12026 | complain); | |
12027 | ||
12028 | release_tree_vector (call_args); | |
12029 | ||
12030 | return ret; | |
cc23546e JO |
12031 | } |
12032 | ||
12033 | case COND_EXPR: | |
12034 | return build_x_conditional_expr | |
b3445994 MM |
12035 | (RECUR (TREE_OPERAND (t, 0)), |
12036 | RECUR (TREE_OPERAND (t, 1)), | |
5ade1ed2 DG |
12037 | RECUR (TREE_OPERAND (t, 2)), |
12038 | complain); | |
cc23546e JO |
12039 | |
12040 | case PSEUDO_DTOR_EXPR: | |
c8094d83 | 12041 | return finish_pseudo_destructor_expr |
b3445994 MM |
12042 | (RECUR (TREE_OPERAND (t, 0)), |
12043 | RECUR (TREE_OPERAND (t, 1)), | |
12044 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
12045 | |
12046 | case TREE_LIST: | |
12047 | { | |
12048 | tree purpose, value, chain; | |
12049 | ||
12050 | if (t == void_list_node) | |
12051 | return t; | |
12052 | ||
5d80a306 DG |
12053 | if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t))) |
12054 | || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))) | |
12055 | { | |
12056 | /* We have pack expansions, so expand those and | |
12057 | create a new list out of it. */ | |
12058 | tree purposevec = NULL_TREE; | |
12059 | tree valuevec = NULL_TREE; | |
12060 | tree chain; | |
12061 | int i, len = -1; | |
12062 | ||
12063 | /* Expand the argument expressions. */ | |
12064 | if (TREE_PURPOSE (t)) | |
12065 | purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args, | |
12066 | complain, in_decl); | |
12067 | if (TREE_VALUE (t)) | |
12068 | valuevec = tsubst_pack_expansion (TREE_VALUE (t), args, | |
12069 | complain, in_decl); | |
12070 | ||
12071 | /* Build the rest of the list. */ | |
12072 | chain = TREE_CHAIN (t); | |
12073 | if (chain && chain != void_type_node) | |
12074 | chain = RECUR (chain); | |
12075 | ||
12076 | /* Determine the number of arguments. */ | |
12077 | if (purposevec && TREE_CODE (purposevec) == TREE_VEC) | |
12078 | { | |
12079 | len = TREE_VEC_LENGTH (purposevec); | |
12080 | gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec)); | |
12081 | } | |
12082 | else if (TREE_CODE (valuevec) == TREE_VEC) | |
12083 | len = TREE_VEC_LENGTH (valuevec); | |
12084 | else | |
12085 | { | |
12086 | /* Since we only performed a partial substitution into | |
12087 | the argument pack, we only return a single list | |
12088 | node. */ | |
12089 | if (purposevec == TREE_PURPOSE (t) | |
12090 | && valuevec == TREE_VALUE (t) | |
12091 | && chain == TREE_CHAIN (t)) | |
12092 | return t; | |
12093 | ||
12094 | return tree_cons (purposevec, valuevec, chain); | |
12095 | } | |
12096 | ||
12097 | /* Convert the argument vectors into a TREE_LIST */ | |
12098 | i = len; | |
12099 | while (i > 0) | |
12100 | { | |
12101 | /* Grab the Ith values. */ | |
12102 | i--; | |
12103 | purpose = purposevec ? TREE_VEC_ELT (purposevec, i) | |
12104 | : NULL_TREE; | |
12105 | value | |
12106 | = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i)) | |
12107 | : NULL_TREE; | |
12108 | ||
12109 | /* Build the list (backwards). */ | |
12110 | chain = tree_cons (purpose, value, chain); | |
12111 | } | |
12112 | ||
12113 | return chain; | |
12114 | } | |
12115 | ||
cc23546e JO |
12116 | purpose = TREE_PURPOSE (t); |
12117 | if (purpose) | |
b3445994 | 12118 | purpose = RECUR (purpose); |
cc23546e JO |
12119 | value = TREE_VALUE (t); |
12120 | if (value) | |
b3445994 | 12121 | value = RECUR (value); |
cc23546e JO |
12122 | chain = TREE_CHAIN (t); |
12123 | if (chain && chain != void_type_node) | |
b3445994 | 12124 | chain = RECUR (chain); |
cc23546e JO |
12125 | if (purpose == TREE_PURPOSE (t) |
12126 | && value == TREE_VALUE (t) | |
12127 | && chain == TREE_CHAIN (t)) | |
12128 | return t; | |
12129 | return tree_cons (purpose, value, chain); | |
12130 | } | |
12131 | ||
12132 | case COMPONENT_REF: | |
12133 | { | |
ee76b931 | 12134 | tree object; |
3897c0aa | 12135 | tree object_type; |
ee76b931 MM |
12136 | tree member; |
12137 | ||
b3445994 MM |
12138 | object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
12139 | args, complain, in_decl); | |
ee76b931 MM |
12140 | /* Remember that there was a reference to this entity. */ |
12141 | if (DECL_P (object)) | |
12142 | mark_used (object); | |
3897c0aa | 12143 | object_type = TREE_TYPE (object); |
ee76b931 MM |
12144 | |
12145 | member = TREE_OPERAND (t, 1); | |
12146 | if (BASELINK_P (member)) | |
c8094d83 | 12147 | member = tsubst_baselink (member, |
ee76b931 MM |
12148 | non_reference (TREE_TYPE (object)), |
12149 | args, complain, in_decl); | |
12150 | else | |
12151 | member = tsubst_copy (member, args, complain, in_decl); | |
bad1f462 KL |
12152 | if (member == error_mark_node) |
12153 | return error_mark_node; | |
3897c0aa MM |
12154 | |
12155 | if (object_type && !CLASS_TYPE_P (object_type)) | |
cc23546e | 12156 | { |
3d9b2eb6 JJ |
12157 | if (SCALAR_TYPE_P (object_type)) |
12158 | { | |
12159 | tree s = NULL_TREE; | |
12160 | tree dtor = member; | |
12161 | ||
12162 | if (TREE_CODE (dtor) == SCOPE_REF) | |
12163 | { | |
12164 | s = TREE_OPERAND (dtor, 0); | |
12165 | dtor = TREE_OPERAND (dtor, 1); | |
12166 | } | |
12167 | if (TREE_CODE (dtor) == BIT_NOT_EXPR) | |
12168 | { | |
12169 | dtor = TREE_OPERAND (dtor, 0); | |
12170 | if (TYPE_P (dtor)) | |
12171 | return finish_pseudo_destructor_expr (object, s, dtor); | |
12172 | } | |
12173 | } | |
cc23546e JO |
12174 | } |
12175 | else if (TREE_CODE (member) == SCOPE_REF | |
12176 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) | |
12177 | { | |
12178 | tree tmpl; | |
12179 | tree args; | |
c8094d83 | 12180 | |
cc23546e JO |
12181 | /* Lookup the template functions now that we know what the |
12182 | scope is. */ | |
12183 | tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); | |
12184 | args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); | |
c8094d83 | 12185 | member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl, |
12483c9f NS |
12186 | /*is_type_p=*/false, |
12187 | /*complain=*/false); | |
cc23546e | 12188 | if (BASELINK_P (member)) |
44370687 | 12189 | { |
c8094d83 | 12190 | BASELINK_FUNCTIONS (member) |
44370687 MM |
12191 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), |
12192 | args); | |
c8094d83 MS |
12193 | member = (adjust_result_of_qualified_name_lookup |
12194 | (member, BINFO_TYPE (BASELINK_BINFO (member)), | |
3897c0aa | 12195 | object_type)); |
44370687 | 12196 | } |
cc23546e JO |
12197 | else |
12198 | { | |
2b7a3abf DS |
12199 | qualified_name_lookup_error (object_type, tmpl, member, |
12200 | input_location); | |
cc23546e JO |
12201 | return error_mark_node; |
12202 | } | |
12203 | } | |
2436b51f MM |
12204 | else if (TREE_CODE (member) == SCOPE_REF |
12205 | && !CLASS_TYPE_P (TREE_OPERAND (member, 0)) | |
12206 | && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL) | |
12207 | { | |
12208 | if (complain & tf_error) | |
12209 | { | |
12210 | if (TYPE_P (TREE_OPERAND (member, 0))) | |
c8094d83 | 12211 | error ("%qT is not a class or namespace", |
2436b51f MM |
12212 | TREE_OPERAND (member, 0)); |
12213 | else | |
c8094d83 | 12214 | error ("%qD is not a class or namespace", |
2436b51f MM |
12215 | TREE_OPERAND (member, 0)); |
12216 | } | |
12217 | return error_mark_node; | |
12218 | } | |
a3f10e50 NS |
12219 | else if (TREE_CODE (member) == FIELD_DECL) |
12220 | return finish_non_static_data_member (member, object, NULL_TREE); | |
cc23546e | 12221 | |
02ed62dd | 12222 | return finish_class_member_access_expr (object, member, |
5ade1ed2 DG |
12223 | /*template_p=*/false, |
12224 | complain); | |
cc23546e JO |
12225 | } |
12226 | ||
12227 | case THROW_EXPR: | |
12228 | return build_throw | |
b3445994 | 12229 | (RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
12230 | |
12231 | case CONSTRUCTOR: | |
12232 | { | |
4038c495 GB |
12233 | VEC(constructor_elt,gc) *n; |
12234 | constructor_elt *ce; | |
12235 | unsigned HOST_WIDE_INT idx; | |
cc23546e | 12236 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
4038c495 | 12237 | bool process_index_p; |
5d80a306 DG |
12238 | int newlen; |
12239 | bool need_copy_p = false; | |
09357846 | 12240 | tree r; |
cc23546e | 12241 | |
a97728cf VR |
12242 | if (type == error_mark_node) |
12243 | return error_mark_node; | |
12244 | ||
cc23546e JO |
12245 | /* digest_init will do the wrong thing if we let it. */ |
12246 | if (type && TYPE_PTRMEMFUNC_P (type)) | |
12247 | return t; | |
12248 | ||
4038c495 | 12249 | /* We do not want to process the index of aggregate |
cc23546e JO |
12250 | initializers as they are identifier nodes which will be |
12251 | looked up by digest_init. */ | |
9e1e64ec | 12252 | process_index_p = !(type && MAYBE_CLASS_TYPE_P (type)); |
c8094d83 | 12253 | |
4038c495 | 12254 | n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t)); |
5d80a306 | 12255 | newlen = VEC_length (constructor_elt, n); |
4038c495 GB |
12256 | for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++) |
12257 | { | |
12258 | if (ce->index && process_index_p) | |
12259 | ce->index = RECUR (ce->index); | |
5d80a306 DG |
12260 | |
12261 | if (PACK_EXPANSION_P (ce->value)) | |
12262 | { | |
12263 | /* Substitute into the pack expansion. */ | |
12264 | ce->value = tsubst_pack_expansion (ce->value, args, complain, | |
12265 | in_decl); | |
12266 | ||
4745e4eb JJ |
12267 | if (ce->value == error_mark_node) |
12268 | ; | |
12269 | else if (TREE_VEC_LENGTH (ce->value) == 1) | |
5d80a306 DG |
12270 | /* Just move the argument into place. */ |
12271 | ce->value = TREE_VEC_ELT (ce->value, 0); | |
12272 | else | |
12273 | { | |
12274 | /* Update the length of the final CONSTRUCTOR | |
12275 | arguments vector, and note that we will need to | |
12276 | copy.*/ | |
12277 | newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1; | |
12278 | need_copy_p = true; | |
12279 | } | |
12280 | } | |
12281 | else | |
12282 | ce->value = RECUR (ce->value); | |
cc23546e | 12283 | } |
c8094d83 | 12284 | |
5d80a306 DG |
12285 | if (need_copy_p) |
12286 | { | |
12287 | VEC(constructor_elt,gc) *old_n = n; | |
12288 | ||
12289 | n = VEC_alloc (constructor_elt, gc, newlen); | |
12290 | for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce); | |
12291 | idx++) | |
12292 | { | |
12293 | if (TREE_CODE (ce->value) == TREE_VEC) | |
12294 | { | |
12295 | int i, len = TREE_VEC_LENGTH (ce->value); | |
12296 | for (i = 0; i < len; ++i) | |
12297 | CONSTRUCTOR_APPEND_ELT (n, 0, | |
12298 | TREE_VEC_ELT (ce->value, i)); | |
12299 | } | |
12300 | else | |
12301 | CONSTRUCTOR_APPEND_ELT (n, 0, ce->value); | |
12302 | } | |
12303 | } | |
12304 | ||
09357846 | 12305 | r = build_constructor (init_list_type_node, n); |
27fb09b7 | 12306 | CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t); |
09357846 | 12307 | |
05b22df9 | 12308 | if (TREE_HAS_CONSTRUCTOR (t)) |
09357846 | 12309 | return finish_compound_literal (type, r); |
cc23546e | 12310 | |
09357846 | 12311 | return r; |
cc23546e JO |
12312 | } |
12313 | ||
12314 | case TYPEID_EXPR: | |
12315 | { | |
b3445994 | 12316 | tree operand_0 = RECUR (TREE_OPERAND (t, 0)); |
cc23546e JO |
12317 | if (TYPE_P (operand_0)) |
12318 | return get_typeid (operand_0); | |
12319 | return build_typeid (operand_0); | |
12320 | } | |
12321 | ||
cc23546e | 12322 | case VAR_DECL: |
db24eb1f NS |
12323 | if (!args) |
12324 | return t; | |
12325 | /* Fall through */ | |
c8094d83 | 12326 | |
db24eb1f NS |
12327 | case PARM_DECL: |
12328 | { | |
12329 | tree r = tsubst_copy (t, args, complain, in_decl); | |
c8094d83 | 12330 | |
db24eb1f NS |
12331 | if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE) |
12332 | /* If the original type was a reference, we'll be wrapped in | |
12333 | the appropriate INDIRECT_REF. */ | |
12334 | r = convert_from_reference (r); | |
12335 | return r; | |
12336 | } | |
cc23546e JO |
12337 | |
12338 | case VA_ARG_EXPR: | |
b3445994 | 12339 | return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)), |
c8094d83 | 12340 | tsubst_copy (TREE_TYPE (t), args, complain, |
b3445994 | 12341 | in_decl)); |
cc23546e | 12342 | |
4bceb077 | 12343 | case OFFSETOF_EXPR: |
c291f8b1 | 12344 | return finish_offsetof (RECUR (TREE_OPERAND (t, 0))); |
4bceb077 | 12345 | |
cb68ec50 PC |
12346 | case TRAIT_EXPR: |
12347 | { | |
12348 | tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args, | |
12349 | complain, in_decl); | |
12350 | ||
12351 | tree type2 = TRAIT_EXPR_TYPE2 (t); | |
12352 | if (type2) | |
12353 | type2 = tsubst_copy (type2, args, complain, in_decl); | |
12354 | ||
12355 | return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2); | |
12356 | } | |
12357 | ||
0fe0caa6 RH |
12358 | case STMT_EXPR: |
12359 | { | |
12360 | tree old_stmt_expr = cur_stmt_expr; | |
12361 | tree stmt_expr = begin_stmt_expr (); | |
12362 | ||
12363 | cur_stmt_expr = stmt_expr; | |
015c2c66 MM |
12364 | tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl, |
12365 | integral_constant_expression_p); | |
0fe0caa6 RH |
12366 | stmt_expr = finish_stmt_expr (stmt_expr, false); |
12367 | cur_stmt_expr = old_stmt_expr; | |
12368 | ||
12369 | return stmt_expr; | |
12370 | } | |
12371 | ||
a5201a91 MM |
12372 | case CONST_DECL: |
12373 | t = tsubst_copy (t, args, complain, in_decl); | |
12374 | /* As in finish_id_expression, we resolve enumeration constants | |
12375 | to their underlying values. */ | |
12376 | if (TREE_CODE (t) == CONST_DECL) | |
6193b8b7 DJ |
12377 | { |
12378 | used_types_insert (TREE_TYPE (t)); | |
12379 | return DECL_INITIAL (t); | |
12380 | } | |
a5201a91 MM |
12381 | return t; |
12382 | ||
d5f4eddd JM |
12383 | case LAMBDA_EXPR: |
12384 | { | |
12385 | tree r = build_lambda_expr (); | |
12386 | ||
12387 | tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE); | |
12388 | TREE_TYPE (r) = type; | |
12389 | CLASSTYPE_LAMBDA_EXPR (type) = r; | |
12390 | ||
12391 | LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r) | |
12392 | = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t); | |
12393 | LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t); | |
12394 | LAMBDA_EXPR_DISCRIMINATOR (r) | |
12395 | = (LAMBDA_EXPR_DISCRIMINATOR (t)); | |
12396 | LAMBDA_EXPR_CAPTURE_LIST (r) | |
12397 | = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t)); | |
12398 | LAMBDA_EXPR_THIS_CAPTURE (r) | |
12399 | = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t)); | |
12400 | LAMBDA_EXPR_EXTRA_SCOPE (r) | |
12401 | = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t)); | |
12402 | ||
12403 | /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */ | |
12404 | determine_visibility (TYPE_NAME (type)); | |
12405 | /* Now that we know visibility, instantiate the type so we have a | |
12406 | declaration of the op() for later calls to lambda_function. */ | |
12407 | complete_type (type); | |
12408 | ||
12409 | type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl); | |
12410 | if (type) | |
12411 | apply_lambda_return_type (r, type); | |
12412 | ||
12413 | return build_lambda_object (r); | |
12414 | } | |
12415 | ||
cc23546e | 12416 | default: |
e58a9aa1 ZL |
12417 | /* Handle Objective-C++ constructs, if appropriate. */ |
12418 | { | |
12419 | tree subst | |
12420 | = objcp_tsubst_copy_and_build (t, args, complain, | |
12421 | in_decl, /*function_p=*/false); | |
12422 | if (subst) | |
12423 | return subst; | |
12424 | } | |
cc23546e JO |
12425 | return tsubst_copy (t, args, complain, in_decl); |
12426 | } | |
b3445994 MM |
12427 | |
12428 | #undef RECUR | |
cc23546e JO |
12429 | } |
12430 | ||
3e4a3562 | 12431 | /* Verify that the instantiated ARGS are valid. For type arguments, |
e8f43da6 | 12432 | make sure that the type is not variably modified. For non-type arguments, |
34cd5ae7 | 12433 | make sure they are constants if they are integral or enumerations. |
9bcb9aae | 12434 | Emit an error under control of COMPLAIN, and return TRUE on error. */ |
3e4a3562 NS |
12435 | |
12436 | static bool | |
ffaf1e05 | 12437 | check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain) |
3e4a3562 | 12438 | { |
ffaf1e05 JM |
12439 | if (ARGUMENT_PACK_P (t)) |
12440 | { | |
12441 | tree vec = ARGUMENT_PACK_ARGS (t); | |
12442 | int len = TREE_VEC_LENGTH (vec); | |
12443 | bool result = false; | |
12444 | int i; | |
3e4a3562 | 12445 | |
ffaf1e05 JM |
12446 | for (i = 0; i < len; ++i) |
12447 | if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain)) | |
12448 | result = true; | |
12449 | return result; | |
12450 | } | |
12451 | else if (TYPE_P (t)) | |
3e4a3562 | 12452 | { |
e8f43da6 | 12453 | if (variably_modified_type_p (t, NULL_TREE)) |
3e4a3562 NS |
12454 | { |
12455 | if (complain & tf_error) | |
ffaf1e05 JM |
12456 | error ("%qT is a variably modified type", t); |
12457 | return true; | |
3e4a3562 NS |
12458 | } |
12459 | } | |
ffaf1e05 JM |
12460 | /* A non-type argument of integral or enumerated type must be a |
12461 | constant. */ | |
12462 | else if (TREE_TYPE (t) | |
12463 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) | |
12464 | && !TREE_CONSTANT (t)) | |
12465 | { | |
12466 | if (complain & tf_error) | |
12467 | error ("integral expression %qE is not constant", t); | |
12468 | return true; | |
12469 | } | |
12470 | return false; | |
12471 | } | |
12472 | ||
12473 | static bool | |
12474 | check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) | |
12475 | { | |
12476 | int ix, len = DECL_NTPARMS (tmpl); | |
12477 | bool result = false; | |
12478 | ||
12479 | for (ix = 0; ix != len; ix++) | |
12480 | { | |
12481 | if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain)) | |
12482 | result = true; | |
12483 | } | |
2010cdcd | 12484 | if (result && (complain & tf_error)) |
0f51ccfc | 12485 | error (" trying to instantiate %qD", tmpl); |
3e4a3562 NS |
12486 | return result; |
12487 | } | |
12488 | ||
6ba89f8e | 12489 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
12490 | the template arguments in TARG_PTR. */ |
12491 | ||
5566b478 | 12492 | tree |
2b110bfc | 12493 | instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain) |
8d08fdba | 12494 | { |
2b110bfc | 12495 | tree targ_ptr = orig_args; |
5566b478 | 12496 | tree fndecl; |
36a117a5 MM |
12497 | tree gen_tmpl; |
12498 | tree spec; | |
a95799ec | 12499 | HOST_WIDE_INT saved_processing_template_decl; |
5566b478 | 12500 | |
27fafc8d JM |
12501 | if (tmpl == error_mark_node) |
12502 | return error_mark_node; | |
12503 | ||
50bc768d | 12504 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
386b8a85 | 12505 | |
db9b2174 MM |
12506 | /* If this function is a clone, handle it specially. */ |
12507 | if (DECL_CLONED_FUNCTION_P (tmpl)) | |
3ad97789 | 12508 | { |
a30f62e0 | 12509 | tree spec; |
3ad97789 | 12510 | tree clone; |
c8094d83 | 12511 | |
b97e8a14 JM |
12512 | /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have |
12513 | DECL_CLONED_FUNCTION. */ | |
12514 | spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl), | |
12515 | targ_ptr, complain); | |
a30f62e0 MM |
12516 | if (spec == error_mark_node) |
12517 | return error_mark_node; | |
12518 | ||
c6002625 | 12519 | /* Look for the clone. */ |
4684cd27 | 12520 | FOR_EACH_CLONE (clone, spec) |
3ad97789 NS |
12521 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) |
12522 | return clone; | |
12523 | /* We should always have found the clone by now. */ | |
315fb5db | 12524 | gcc_unreachable (); |
3ad97789 NS |
12525 | return NULL_TREE; |
12526 | } | |
c8094d83 | 12527 | |
36a117a5 | 12528 | /* Check to see if we already have this specialization. */ |
f9a7ae04 MM |
12529 | gen_tmpl = most_general_template (tmpl); |
12530 | if (tmpl != gen_tmpl) | |
2b110bfc JM |
12531 | /* The TMPL is a partial instantiation. To get a full set of |
12532 | arguments we must add the arguments used to perform the | |
12533 | partial instantiation. */ | |
12534 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
12535 | targ_ptr); | |
36a117a5 | 12536 | |
2b110bfc JM |
12537 | /* It would be nice to avoid hashing here and then again in tsubst_decl, |
12538 | but it doesn't seem to be on the hot path. */ | |
12539 | spec = retrieve_specialization (gen_tmpl, targ_ptr, 0); | |
12540 | ||
12541 | gcc_assert (tmpl == gen_tmpl | |
12542 | || ((fndecl = retrieve_specialization (tmpl, orig_args, 0)) | |
12543 | == spec) | |
12544 | || fndecl == NULL_TREE); | |
12545 | ||
12546 | if (spec != NULL_TREE) | |
12547 | return spec; | |
386b8a85 | 12548 | |
3e4a3562 NS |
12549 | if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), |
12550 | complain)) | |
12551 | return error_mark_node; | |
c8094d83 | 12552 | |
2b907f5c KL |
12553 | /* We are building a FUNCTION_DECL, during which the access of its |
12554 | parameters and return types have to be checked. However this | |
12555 | FUNCTION_DECL which is the desired context for access checking | |
12556 | is not built yet. We solve this chicken-and-egg problem by | |
12557 | deferring all checks until we have the FUNCTION_DECL. */ | |
12558 | push_deferring_access_checks (dk_deferred); | |
5c74d5b0 | 12559 | |
a95799ec MM |
12560 | /* Although PROCESSING_TEMPLATE_DECL may be true at this point |
12561 | (because, for example, we have encountered a non-dependent | |
a1d08991 MM |
12562 | function call in the body of a template function and must now |
12563 | determine which of several overloaded functions will be called), | |
12564 | within the instantiation itself we are not processing a | |
12565 | template. */ | |
a95799ec MM |
12566 | saved_processing_template_decl = processing_template_decl; |
12567 | processing_template_decl = 0; | |
12568 | /* Substitute template parameters to obtain the specialization. */ | |
17aec3eb | 12569 | fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), |
3e4a3562 | 12570 | targ_ptr, complain, gen_tmpl); |
a95799ec | 12571 | processing_template_decl = saved_processing_template_decl; |
6b6b60af MM |
12572 | if (fndecl == error_mark_node) |
12573 | return error_mark_node; | |
5c74d5b0 | 12574 | |
2b907f5c KL |
12575 | /* Now we know the specialization, compute access previously |
12576 | deferred. */ | |
12577 | push_access_scope (fndecl); | |
d0940d56 DS |
12578 | |
12579 | /* Some typedefs referenced from within the template code need to be access | |
12580 | checked at template instantiation time, i.e now. These types were | |
12581 | added to the template at parsing time. Let's get those and perfom | |
12582 | the acces checks then. */ | |
2eb25c98 | 12583 | perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr); |
2b907f5c KL |
12584 | perform_deferred_access_checks (); |
12585 | pop_access_scope (fndecl); | |
12586 | pop_deferring_access_checks (); | |
5c74d5b0 | 12587 | |
36a117a5 MM |
12588 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
12589 | template, not the most general template. */ | |
12590 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 12591 | |
94350948 | 12592 | /* If we've just instantiated the main entry point for a function, |
3ad97789 NS |
12593 | instantiate all the alternate entry points as well. We do this |
12594 | by cloning the instantiation of the main entry point, not by | |
12595 | instantiating the template clones. */ | |
12596 | if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl))) | |
12597 | clone_function_decl (fndecl, /*update_method_vec_p=*/0); | |
94350948 | 12598 | |
5566b478 | 12599 | return fndecl; |
8d08fdba | 12600 | } |
5566b478 | 12601 | |
c166b898 ILT |
12602 | /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with |
12603 | NARGS elements of the arguments that are being used when calling | |
12604 | it. TARGS is a vector into which the deduced template arguments | |
12605 | are placed. | |
8d08fdba MS |
12606 | |
12607 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
12608 | all the types, and 1 for complete failure. An error message will be | |
12609 | printed only for an incomplete match. | |
12610 | ||
e5214479 JM |
12611 | If FN is a conversion operator, or we are trying to produce a specific |
12612 | specialization, RETURN_TYPE is the return type desired. | |
9f54c803 MM |
12613 | |
12614 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
12615 | template-id. | |
6467930b | 12616 | |
830bfa74 MM |
12617 | The parameter STRICT is one of: |
12618 | ||
c8094d83 | 12619 | DEDUCE_CALL: |
830bfa74 MM |
12620 | We are deducing arguments for a function call, as in |
12621 | [temp.deduct.call]. | |
12622 | ||
12623 | DEDUCE_CONV: | |
c8094d83 | 12624 | We are deducing arguments for a conversion function, as in |
830bfa74 MM |
12625 | [temp.deduct.conv]. |
12626 | ||
12627 | DEDUCE_EXACT: | |
62e4a758 NS |
12628 | We are deducing arguments when doing an explicit instantiation |
12629 | as in [temp.explicit], when determining an explicit specialization | |
12630 | as in [temp.expl.spec], or when taking the address of a function | |
a34d3336 | 12631 | template, as in [temp.deduct.funcaddr]. */ |
8d08fdba MS |
12632 | |
12633 | int | |
c8094d83 | 12634 | fn_type_unification (tree fn, |
0cbd7506 MS |
12635 | tree explicit_targs, |
12636 | tree targs, | |
c166b898 ILT |
12637 | const tree *args, |
12638 | unsigned int nargs, | |
0cbd7506 | 12639 | tree return_type, |
30f86ec3 FJ |
12640 | unification_kind_t strict, |
12641 | int flags) | |
386b8a85 | 12642 | { |
4393e105 MM |
12643 | tree parms; |
12644 | tree fntype; | |
adecb3f4 | 12645 | int result; |
5d80a306 | 12646 | bool incomplete_argument_packs_p = false; |
386b8a85 | 12647 | |
50bc768d | 12648 | gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL); |
2b59fc25 | 12649 | |
4393e105 MM |
12650 | fntype = TREE_TYPE (fn); |
12651 | if (explicit_targs) | |
75650646 | 12652 | { |
4393e105 | 12653 | /* [temp.deduct] |
c8094d83 | 12654 | |
4393e105 MM |
12655 | The specified template arguments must match the template |
12656 | parameters in kind (i.e., type, nontype, template), and there | |
12657 | must not be more arguments than there are parameters; | |
12658 | otherwise type deduction fails. | |
12659 | ||
12660 | Nontype arguments must match the types of the corresponding | |
12661 | nontype template parameters, or must be convertible to the | |
12662 | types of the corresponding nontype parameters as specified in | |
12663 | _temp.arg.nontype_, otherwise type deduction fails. | |
12664 | ||
12665 | All references in the function type of the function template | |
12666 | to the corresponding template parameters are replaced by the | |
12667 | specified template argument values. If a substitution in a | |
12668 | template parameter or in the function type of the function | |
12669 | template results in an invalid type, type deduction fails. */ | |
5d80a306 DG |
12670 | tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn); |
12671 | int i, len = TREE_VEC_LENGTH (tparms); | |
4393e105 | 12672 | tree converted_args; |
5d80a306 | 12673 | bool incomplete = false; |
75650646 | 12674 | |
c8a7ed43 AO |
12675 | if (explicit_targs == error_mark_node) |
12676 | return 1; | |
12677 | ||
4393e105 | 12678 | converted_args |
5d80a306 | 12679 | = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none, |
e7e93965 MM |
12680 | /*require_all_args=*/false, |
12681 | /*use_default_args=*/false)); | |
4393e105 | 12682 | if (converted_args == error_mark_node) |
75650646 | 12683 | return 1; |
386b8a85 | 12684 | |
ffd49b19 | 12685 | /* Substitute the explicit args into the function type. This is |
0cbd7506 MS |
12686 | necessary so that, for instance, explicitly declared function |
12687 | arguments can match null pointed constants. If we were given | |
12688 | an incomplete set of explicit args, we must not do semantic | |
12689 | processing during substitution as we could create partial | |
12690 | instantiations. */ | |
5d80a306 DG |
12691 | for (i = 0; i < len; i++) |
12692 | { | |
12693 | tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
12694 | bool parameter_pack = false; | |
12695 | ||
12696 | /* Dig out the actual parm. */ | |
12697 | if (TREE_CODE (parm) == TYPE_DECL | |
12698 | || TREE_CODE (parm) == TEMPLATE_DECL) | |
12699 | { | |
12700 | parm = TREE_TYPE (parm); | |
12701 | parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm); | |
12702 | } | |
12703 | else if (TREE_CODE (parm) == PARM_DECL) | |
12704 | { | |
12705 | parm = DECL_INITIAL (parm); | |
12706 | parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm); | |
12707 | } | |
12708 | ||
12709 | if (parameter_pack) | |
12710 | { | |
12711 | int level, idx; | |
12712 | tree targ; | |
12713 | template_parm_level_and_index (parm, &level, &idx); | |
12714 | ||
12715 | /* Mark the argument pack as "incomplete". We could | |
12716 | still deduce more arguments during unification. */ | |
12717 | targ = TMPL_ARG (converted_args, level, idx); | |
b1d7b1c0 DG |
12718 | if (targ) |
12719 | { | |
12720 | ARGUMENT_PACK_INCOMPLETE_P(targ) = 1; | |
12721 | ARGUMENT_PACK_EXPLICIT_ARGS (targ) | |
12722 | = ARGUMENT_PACK_ARGS (targ); | |
12723 | } | |
5d80a306 DG |
12724 | |
12725 | /* We have some incomplete argument packs. */ | |
12726 | incomplete_argument_packs_p = true; | |
12727 | } | |
12728 | } | |
12729 | ||
12730 | if (incomplete_argument_packs_p) | |
12731 | /* Any substitution is guaranteed to be incomplete if there | |
12732 | are incomplete argument packs, because we can still deduce | |
12733 | more arguments. */ | |
12734 | incomplete = 1; | |
12735 | else | |
12736 | incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs); | |
12737 | ||
ffd49b19 | 12738 | processing_template_decl += incomplete; |
c2ea3a40 | 12739 | fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE); |
ffd49b19 | 12740 | processing_template_decl -= incomplete; |
c8094d83 | 12741 | |
4393e105 MM |
12742 | if (fntype == error_mark_node) |
12743 | return 1; | |
050367a3 | 12744 | |
4393e105 | 12745 | /* Place the explicitly specified arguments in TARGS. */ |
ffd49b19 | 12746 | for (i = NUM_TMPL_ARGS (converted_args); i--;) |
4393e105 | 12747 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); |
75650646 | 12748 | } |
c8094d83 | 12749 | |
e5214479 | 12750 | /* Never do unification on the 'this' parameter. */ |
ba139ba8 | 12751 | parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype)); |
c8094d83 | 12752 | |
8d3631f8 NS |
12753 | if (return_type) |
12754 | { | |
c166b898 ILT |
12755 | tree *new_args; |
12756 | ||
8d3631f8 | 12757 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); |
c166b898 ILT |
12758 | new_args = XALLOCAVEC (tree, nargs + 1); |
12759 | new_args[0] = return_type; | |
12760 | memcpy (new_args + 1, args, nargs * sizeof (tree)); | |
12761 | args = new_args; | |
12762 | ++nargs; | |
4393e105 MM |
12763 | } |
12764 | ||
4393e105 MM |
12765 | /* We allow incomplete unification without an error message here |
12766 | because the standard doesn't seem to explicitly prohibit it. Our | |
12767 | callers must be ready to deal with unification failures in any | |
12768 | event. */ | |
c8094d83 | 12769 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
c166b898 | 12770 | targs, parms, args, nargs, /*subr=*/0, |
30f86ec3 | 12771 | strict, flags); |
adecb3f4 | 12772 | |
5d80a306 DG |
12773 | if (result == 0 && incomplete_argument_packs_p) |
12774 | { | |
12775 | int i, len = NUM_TMPL_ARGS (targs); | |
12776 | ||
12777 | /* Clear the "incomplete" flags on all argument packs. */ | |
12778 | for (i = 0; i < len; i++) | |
12779 | { | |
12780 | tree arg = TREE_VEC_ELT (targs, i); | |
12781 | if (ARGUMENT_PACK_P (arg)) | |
12782 | { | |
12783 | ARGUMENT_PACK_INCOMPLETE_P (arg) = 0; | |
12784 | ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE; | |
12785 | } | |
12786 | } | |
12787 | } | |
12788 | ||
4cf36211 DG |
12789 | /* Now that we have bindings for all of the template arguments, |
12790 | ensure that the arguments deduced for the template template | |
12791 | parameters have compatible template parameter lists. We cannot | |
12792 | check this property before we have deduced all template | |
12793 | arguments, because the template parameter types of a template | |
12794 | template parameter might depend on prior template parameters | |
12795 | deduced after the template template parameter. The following | |
12796 | ill-formed example illustrates this issue: | |
12797 | ||
12798 | template<typename T, template<T> class C> void f(C<5>, T); | |
12799 | ||
12800 | template<int N> struct X {}; | |
12801 | ||
12802 | void g() { | |
12803 | f(X<5>(), 5l); // error: template argument deduction fails | |
12804 | } | |
12805 | ||
12806 | The template parameter list of 'C' depends on the template type | |
12807 | parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to | |
12808 | 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the | |
12809 | time that we deduce 'C'. */ | |
12810 | if (result == 0 | |
12811 | && !template_template_parm_bindings_ok_p | |
12812 | (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs)) | |
12813 | return 1; | |
12814 | ||
c8094d83 | 12815 | if (result == 0) |
adecb3f4 | 12816 | /* All is well so far. Now, check: |
c8094d83 MS |
12817 | |
12818 | [temp.deduct] | |
12819 | ||
adecb3f4 MM |
12820 | When all template arguments have been deduced, all uses of |
12821 | template parameters in nondeduced contexts are replaced with | |
12822 | the corresponding deduced argument values. If the | |
12823 | substitution results in an invalid type, as described above, | |
12824 | type deduction fails. */ | |
463ecaca JM |
12825 | { |
12826 | tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE); | |
12827 | if (substed == error_mark_node) | |
12828 | return 1; | |
12829 | ||
12830 | /* If we're looking for an exact match, check that what we got | |
12831 | is indeed an exact match. It might not be if some template | |
12832 | parameters are used in non-deduced contexts. */ | |
12833 | if (strict == DEDUCE_EXACT) | |
12834 | { | |
c166b898 ILT |
12835 | unsigned int i; |
12836 | ||
463ecaca JM |
12837 | tree sarg |
12838 | = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed)); | |
463ecaca JM |
12839 | if (return_type) |
12840 | sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg); | |
c166b898 ILT |
12841 | for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg)) |
12842 | if (!same_type_p (args[i], TREE_VALUE (sarg))) | |
463ecaca JM |
12843 | return 1; |
12844 | } | |
12845 | } | |
adecb3f4 MM |
12846 | |
12847 | return result; | |
830bfa74 MM |
12848 | } |
12849 | ||
12850 | /* Adjust types before performing type deduction, as described in | |
12851 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
12852 | sections are symmetric. PARM is the type of a function parameter |
12853 | or the return type of the conversion function. ARG is the type of | |
12854 | the argument passed to the call, or the type of the value | |
8af2fec4 RY |
12855 | initialized with the result of the conversion function. |
12856 | ARG_EXPR is the original argument expression, which may be null. */ | |
386b8a85 | 12857 | |
62e4a758 | 12858 | static int |
c8094d83 | 12859 | maybe_adjust_types_for_deduction (unification_kind_t strict, |
0cbd7506 | 12860 | tree* parm, |
8af2fec4 RY |
12861 | tree* arg, |
12862 | tree arg_expr) | |
830bfa74 | 12863 | { |
62e4a758 | 12864 | int result = 0; |
c8094d83 | 12865 | |
830bfa74 MM |
12866 | switch (strict) |
12867 | { | |
12868 | case DEDUCE_CALL: | |
12869 | break; | |
12870 | ||
12871 | case DEDUCE_CONV: | |
12872 | { | |
4c7d0dff MM |
12873 | /* Swap PARM and ARG throughout the remainder of this |
12874 | function; the handling is precisely symmetric since PARM | |
12875 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
12876 | tree* temp = parm; |
12877 | parm = arg; | |
12878 | arg = temp; | |
12879 | break; | |
12880 | } | |
12881 | ||
12882 | case DEDUCE_EXACT: | |
12883 | /* There is nothing to do in this case. */ | |
62e4a758 | 12884 | return 0; |
830bfa74 MM |
12885 | |
12886 | default: | |
315fb5db | 12887 | gcc_unreachable (); |
830bfa74 MM |
12888 | } |
12889 | ||
12890 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
12891 | { | |
12892 | /* [temp.deduct.call] | |
c8094d83 | 12893 | |
830bfa74 | 12894 | If P is not a reference type: |
c8094d83 | 12895 | |
830bfa74 MM |
12896 | --If A is an array type, the pointer type produced by the |
12897 | array-to-pointer standard conversion (_conv.array_) is | |
12898 | used in place of A for type deduction; otherwise, | |
c8094d83 | 12899 | |
830bfa74 MM |
12900 | --If A is a function type, the pointer type produced by |
12901 | the function-to-pointer standard conversion | |
12902 | (_conv.func_) is used in place of A for type deduction; | |
12903 | otherwise, | |
c8094d83 | 12904 | |
830bfa74 MM |
12905 | --If A is a cv-qualified type, the top level |
12906 | cv-qualifiers of A's type are ignored for type | |
12907 | deduction. */ | |
12908 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
12909 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 12910 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
12911 | *arg = build_pointer_type (*arg); |
12912 | else | |
12913 | *arg = TYPE_MAIN_VARIANT (*arg); | |
12914 | } | |
c8094d83 | 12915 | |
8af2fec4 RY |
12916 | /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is |
12917 | of the form T&&, where T is a template parameter, and the argument | |
12918 | is an lvalue, T is deduced as A& */ | |
12919 | if (TREE_CODE (*parm) == REFERENCE_TYPE | |
12920 | && TYPE_REF_IS_RVALUE (*parm) | |
12921 | && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM | |
12922 | && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED | |
12923 | && arg_expr && real_lvalue_p (arg_expr)) | |
12924 | *arg = build_reference_type (*arg); | |
12925 | ||
830bfa74 | 12926 | /* [temp.deduct.call] |
c8094d83 | 12927 | |
830bfa74 MM |
12928 | If P is a cv-qualified type, the top level cv-qualifiers |
12929 | of P's type are ignored for type deduction. If P is a | |
12930 | reference type, the type referred to by P is used for | |
12931 | type deduction. */ | |
12932 | *parm = TYPE_MAIN_VARIANT (*parm); | |
12933 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
62e4a758 NS |
12934 | { |
12935 | *parm = TREE_TYPE (*parm); | |
12936 | result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; | |
12937 | } | |
1c82cc90 NS |
12938 | |
12939 | /* DR 322. For conversion deduction, remove a reference type on parm | |
12940 | too (which has been swapped into ARG). */ | |
12941 | if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE) | |
12942 | *arg = TREE_TYPE (*arg); | |
c8094d83 | 12943 | |
62e4a758 | 12944 | return result; |
386b8a85 JM |
12945 | } |
12946 | ||
e5214479 | 12947 | /* Most parms like fn_type_unification. |
9f54c803 MM |
12948 | |
12949 | If SUBR is 1, we're being called recursively (to unify the | |
12950 | arguments of a function or method parameter of a function | |
38d18b1a | 12951 | template). */ |
386b8a85 | 12952 | |
4966381a | 12953 | static int |
c8094d83 | 12954 | type_unification_real (tree tparms, |
0cbd7506 MS |
12955 | tree targs, |
12956 | tree xparms, | |
c166b898 ILT |
12957 | const tree *xargs, |
12958 | unsigned int xnargs, | |
0cbd7506 | 12959 | int subr, |
30f86ec3 FJ |
12960 | unification_kind_t strict, |
12961 | int flags) | |
8d08fdba | 12962 | { |
8af2fec4 | 12963 | tree parm, arg, arg_expr; |
8d08fdba MS |
12964 | int i; |
12965 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 12966 | int sub_strict; |
bd0d5d4a | 12967 | int saw_undeduced = 0; |
c166b898 ILT |
12968 | tree parms; |
12969 | const tree *args; | |
12970 | unsigned int nargs; | |
12971 | unsigned int ia; | |
8d08fdba | 12972 | |
50bc768d NS |
12973 | gcc_assert (TREE_CODE (tparms) == TREE_VEC); |
12974 | gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST); | |
50bc768d | 12975 | gcc_assert (ntparms > 0); |
8d08fdba | 12976 | |
830bfa74 MM |
12977 | switch (strict) |
12978 | { | |
12979 | case DEDUCE_CALL: | |
028d1f20 | 12980 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL |
0cbd7506 | 12981 | | UNIFY_ALLOW_DERIVED); |
830bfa74 | 12982 | break; |
c8094d83 | 12983 | |
830bfa74 MM |
12984 | case DEDUCE_CONV: |
12985 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
12986 | break; | |
12987 | ||
12988 | case DEDUCE_EXACT: | |
12989 | sub_strict = UNIFY_ALLOW_NONE; | |
12990 | break; | |
c8094d83 | 12991 | |
830bfa74 | 12992 | default: |
315fb5db | 12993 | gcc_unreachable (); |
830bfa74 MM |
12994 | } |
12995 | ||
bd0d5d4a JM |
12996 | again: |
12997 | parms = xparms; | |
12998 | args = xargs; | |
c166b898 | 12999 | nargs = xnargs; |
bd0d5d4a | 13000 | |
c166b898 | 13001 | ia = 0; |
a34d3336 | 13002 | while (parms && parms != void_list_node |
c166b898 | 13003 | && ia < nargs) |
8d08fdba | 13004 | { |
5d80a306 DG |
13005 | if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) |
13006 | break; | |
13007 | ||
8d08fdba MS |
13008 | parm = TREE_VALUE (parms); |
13009 | parms = TREE_CHAIN (parms); | |
c166b898 ILT |
13010 | arg = args[ia]; |
13011 | ++ia; | |
8af2fec4 | 13012 | arg_expr = NULL; |
8d08fdba MS |
13013 | |
13014 | if (arg == error_mark_node) | |
13015 | return 1; | |
13016 | if (arg == unknown_type_node) | |
34016c81 JM |
13017 | /* We can't deduce anything from this, but we might get all the |
13018 | template args from other function args. */ | |
13019 | continue; | |
b7484fbe | 13020 | |
03e70705 JM |
13021 | /* Conversions will be performed on a function argument that |
13022 | corresponds with a function parameter that contains only | |
13023 | non-deducible template parameters and explicitly specified | |
13024 | template parameters. */ | |
c353b8e3 | 13025 | if (!uses_template_parms (parm)) |
b7484fbe | 13026 | { |
03e70705 JM |
13027 | tree type; |
13028 | ||
2f939d94 | 13029 | if (!TYPE_P (arg)) |
03e70705 JM |
13030 | type = TREE_TYPE (arg); |
13031 | else | |
c353b8e3 | 13032 | type = arg; |
03e70705 | 13033 | |
fad86f7a | 13034 | if (same_type_p (parm, type)) |
343c89cd | 13035 | continue; |
fad86f7a | 13036 | if (strict != DEDUCE_EXACT |
3db45ab5 | 13037 | && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg, |
30f86ec3 | 13038 | flags)) |
fad86f7a | 13039 | continue; |
3db45ab5 | 13040 | |
b7484fbe MS |
13041 | return 1; |
13042 | } | |
c8094d83 | 13043 | |
2f939d94 | 13044 | if (!TYPE_P (arg)) |
8d08fdba | 13045 | { |
50bc768d | 13046 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); |
34016c81 | 13047 | if (type_unknown_p (arg)) |
28cbf42c | 13048 | { |
acde59b4 | 13049 | /* [temp.deduct.type] |
34016c81 | 13050 | |
acde59b4 MM |
13051 | A template-argument can be deduced from a pointer to |
13052 | function or pointer to member function argument if | |
13053 | the set of overloaded functions does not contain | |
13054 | function templates and at most one of a set of | |
13055 | overloaded functions provides a unique match. */ | |
34016c81 | 13056 | if (resolve_overloaded_unification |
acde59b4 MM |
13057 | (tparms, targs, parm, arg, strict, sub_strict)) |
13058 | continue; | |
13059 | ||
13060 | return 1; | |
28cbf42c | 13061 | } |
8af2fec4 | 13062 | arg_expr = arg; |
f9aa54d3 | 13063 | arg = unlowered_expr_type (arg); |
08476342 NS |
13064 | if (arg == error_mark_node) |
13065 | return 1; | |
8d08fdba | 13066 | } |
c8094d83 | 13067 | |
62e4a758 | 13068 | { |
0cbd7506 | 13069 | int arg_strict = sub_strict; |
c8094d83 | 13070 | |
0cbd7506 | 13071 | if (!subr) |
8af2fec4 RY |
13072 | arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, |
13073 | arg_expr); | |
4393e105 | 13074 | |
09357846 JM |
13075 | if (arg == init_list_type_node && arg_expr) |
13076 | arg = arg_expr; | |
0cbd7506 MS |
13077 | if (unify (tparms, targs, parm, arg, arg_strict)) |
13078 | return 1; | |
62e4a758 | 13079 | } |
8d08fdba | 13080 | } |
c8094d83 | 13081 | |
5d80a306 DG |
13082 | |
13083 | if (parms | |
13084 | && parms != void_list_node | |
13085 | && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) | |
13086 | { | |
13087 | /* Unify the remaining arguments with the pack expansion type. */ | |
13088 | tree argvec; | |
13089 | tree parmvec = make_tree_vec (1); | |
5d80a306 | 13090 | |
5d80a306 | 13091 | /* Allocate a TREE_VEC and copy in all of the arguments */ |
c166b898 ILT |
13092 | argvec = make_tree_vec (nargs - ia); |
13093 | for (i = 0; ia < nargs; ++ia, ++i) | |
13094 | TREE_VEC_ELT (argvec, i) = args[ia]; | |
5d80a306 DG |
13095 | |
13096 | /* Copy the parameter into parmvec. */ | |
13097 | TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms); | |
13098 | if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict, | |
13099 | /*call_args_p=*/true, /*subr=*/subr)) | |
13100 | return 1; | |
13101 | ||
13102 | /* Advance to the end of the list of parameters. */ | |
13103 | parms = TREE_CHAIN (parms); | |
13104 | } | |
13105 | ||
8d08fdba MS |
13106 | /* Fail if we've reached the end of the parm list, and more args |
13107 | are present, and the parm list isn't variadic. */ | |
c166b898 | 13108 | if (ia < nargs && parms == void_list_node) |
8d08fdba | 13109 | return 1; |
f4f206f4 | 13110 | /* Fail if parms are left and they don't have default values. */ |
a34d3336 | 13111 | if (parms && parms != void_list_node |
8d08fdba MS |
13112 | && TREE_PURPOSE (parms) == NULL_TREE) |
13113 | return 1; | |
bd0d5d4a | 13114 | |
8d08fdba MS |
13115 | if (!subr) |
13116 | for (i = 0; i < ntparms; i++) | |
a34d3336 | 13117 | if (!TREE_VEC_ELT (targs, i)) |
8d08fdba | 13118 | { |
2d8ba2c7 LM |
13119 | tree tparm; |
13120 | ||
13121 | if (TREE_VEC_ELT (tparms, i) == error_mark_node) | |
13122 | continue; | |
13123 | ||
13124 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
bd0d5d4a JM |
13125 | |
13126 | /* If this is an undeduced nontype parameter that depends on | |
13127 | a type parameter, try another pass; its type may have been | |
13128 | deduced from a later argument than the one from which | |
13129 | this parameter can be deduced. */ | |
13130 | if (TREE_CODE (tparm) == PARM_DECL | |
13131 | && uses_template_parms (TREE_TYPE (tparm)) | |
13132 | && !saw_undeduced++) | |
13133 | goto again; | |
13134 | ||
9b7dd5e8 DG |
13135 | /* Core issue #226 (C++0x) [temp.deduct]: |
13136 | ||
13137 | If a template argument has not been deduced, its | |
13138 | default template argument, if any, is used. | |
13139 | ||
c1ae8be5 SM |
13140 | When we are in C++98 mode, TREE_PURPOSE will either |
13141 | be NULL_TREE or ERROR_MARK_NODE, so we do not need | |
13142 | to explicitly check cxx_dialect here. */ | |
9b7dd5e8 DG |
13143 | if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i))) |
13144 | { | |
8be2c871 DS |
13145 | tree arg = tsubst_template_arg |
13146 | (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)), | |
13147 | targs, tf_none, NULL_TREE); | |
9b7dd5e8 DG |
13148 | if (arg == error_mark_node) |
13149 | return 1; | |
13150 | else | |
13151 | { | |
13152 | TREE_VEC_ELT (targs, i) = arg; | |
13153 | continue; | |
13154 | } | |
13155 | } | |
13156 | ||
b1d7b1c0 DG |
13157 | /* If the type parameter is a parameter pack, then it will |
13158 | be deduced to an empty parameter pack. */ | |
13159 | if (template_parameter_pack_p (tparm)) | |
13160 | { | |
13161 | tree arg; | |
13162 | ||
13163 | if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX) | |
13164 | { | |
13165 | arg = make_node (NONTYPE_ARGUMENT_PACK); | |
13166 | TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm)); | |
13167 | TREE_CONSTANT (arg) = 1; | |
13168 | } | |
13169 | else | |
13170 | arg = make_node (TYPE_ARGUMENT_PACK); | |
13171 | ||
13172 | SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0)); | |
13173 | ||
13174 | TREE_VEC_ELT (targs, i) = arg; | |
13175 | continue; | |
13176 | } | |
13177 | ||
8d08fdba MS |
13178 | return 2; |
13179 | } | |
c8094d83 | 13180 | |
8d08fdba MS |
13181 | return 0; |
13182 | } | |
13183 | ||
acde59b4 MM |
13184 | /* Subroutine of type_unification_real. Args are like the variables |
13185 | at the call site. ARG is an overloaded function (or template-id); | |
13186 | we try deducing template args from each of the overloads, and if | |
13187 | only one succeeds, we go with that. Modifies TARGS and returns | |
13188 | true on success. */ | |
34016c81 | 13189 | |
acde59b4 | 13190 | static bool |
c8094d83 | 13191 | resolve_overloaded_unification (tree tparms, |
0cbd7506 MS |
13192 | tree targs, |
13193 | tree parm, | |
13194 | tree arg, | |
13195 | unification_kind_t strict, | |
3a978d72 | 13196 | int sub_strict) |
34016c81 JM |
13197 | { |
13198 | tree tempargs = copy_node (targs); | |
13199 | int good = 0; | |
e04c614e | 13200 | tree goodfn = NULL_TREE; |
f23fb7f5 | 13201 | bool addr_p; |
34016c81 JM |
13202 | |
13203 | if (TREE_CODE (arg) == ADDR_EXPR) | |
f23fb7f5 MM |
13204 | { |
13205 | arg = TREE_OPERAND (arg, 0); | |
13206 | addr_p = true; | |
13207 | } | |
13208 | else | |
13209 | addr_p = false; | |
9f3d9e46 | 13210 | |
d8f8dca1 MM |
13211 | if (TREE_CODE (arg) == COMPONENT_REF) |
13212 | /* Handle `&x' where `x' is some static or non-static member | |
13213 | function name. */ | |
13214 | arg = TREE_OPERAND (arg, 1); | |
13215 | ||
05e0b2f4 JM |
13216 | if (TREE_CODE (arg) == OFFSET_REF) |
13217 | arg = TREE_OPERAND (arg, 1); | |
13218 | ||
9f3d9e46 | 13219 | /* Strip baselink information. */ |
50ad9642 MM |
13220 | if (BASELINK_P (arg)) |
13221 | arg = BASELINK_FUNCTIONS (arg); | |
9f3d9e46 | 13222 | |
34016c81 JM |
13223 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
13224 | { | |
13225 | /* If we got some explicit template args, we need to plug them into | |
13226 | the affected templates before we try to unify, in case the | |
13227 | explicit args will completely resolve the templates in question. */ | |
13228 | ||
13229 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
13230 | arg = TREE_OPERAND (arg, 0); | |
13231 | ||
13232 | for (; arg; arg = OVL_NEXT (arg)) | |
13233 | { | |
13234 | tree fn = OVL_CURRENT (arg); | |
13235 | tree subargs, elem; | |
13236 | ||
13237 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
13238 | continue; | |
13239 | ||
cc85b4c5 | 13240 | ++processing_template_decl; |
a34d3336 NS |
13241 | subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn), |
13242 | expl_subargs, /*check_ret=*/false); | |
34016c81 JM |
13243 | if (subargs) |
13244 | { | |
c2ea3a40 | 13245 | elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); |
e04c614e JM |
13246 | if (try_one_overload (tparms, targs, tempargs, parm, |
13247 | elem, strict, sub_strict, addr_p) | |
13248 | && (!goodfn || !decls_match (goodfn, elem))) | |
13249 | { | |
13250 | goodfn = elem; | |
13251 | ++good; | |
13252 | } | |
34016c81 | 13253 | } |
cc85b4c5 | 13254 | --processing_template_decl; |
34016c81 JM |
13255 | } |
13256 | } | |
acde59b4 MM |
13257 | else if (TREE_CODE (arg) != OVERLOAD |
13258 | && TREE_CODE (arg) != FUNCTION_DECL) | |
13259 | /* If ARG is, for example, "(0, &f)" then its type will be unknown | |
13260 | -- but the deduction does not succeed because the expression is | |
13261 | not just the function on its own. */ | |
13262 | return false; | |
315fb5db | 13263 | else |
acde59b4 | 13264 | for (; arg; arg = OVL_NEXT (arg)) |
e04c614e JM |
13265 | if (try_one_overload (tparms, targs, tempargs, parm, |
13266 | TREE_TYPE (OVL_CURRENT (arg)), | |
13267 | strict, sub_strict, addr_p) | |
13268 | && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg)))) | |
13269 | { | |
13270 | goodfn = OVL_CURRENT (arg); | |
13271 | ++good; | |
13272 | } | |
34016c81 JM |
13273 | |
13274 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
13275 | to function or pointer to member function argument if the set of | |
13276 | overloaded functions does not contain function templates and at most | |
13277 | one of a set of overloaded functions provides a unique match. | |
13278 | ||
13279 | So if we found multiple possibilities, we return success but don't | |
13280 | deduce anything. */ | |
13281 | ||
13282 | if (good == 1) | |
13283 | { | |
13284 | int i = TREE_VEC_LENGTH (targs); | |
13285 | for (; i--; ) | |
13286 | if (TREE_VEC_ELT (tempargs, i)) | |
13287 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
13288 | } | |
13289 | if (good) | |
acde59b4 | 13290 | return true; |
34016c81 | 13291 | |
acde59b4 | 13292 | return false; |
34016c81 JM |
13293 | } |
13294 | ||
13295 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
13296 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
13297 | different overloads deduce different arguments for a given parm. | |
f23fb7f5 MM |
13298 | ADDR_P is true if the expression for which deduction is being |
13299 | performed was of the form "& fn" rather than simply "fn". | |
13300 | ||
34016c81 JM |
13301 | Returns 1 on success. */ |
13302 | ||
13303 | static int | |
3a978d72 | 13304 | try_one_overload (tree tparms, |
0cbd7506 MS |
13305 | tree orig_targs, |
13306 | tree targs, | |
13307 | tree parm, | |
13308 | tree arg, | |
13309 | unification_kind_t strict, | |
f23fb7f5 MM |
13310 | int sub_strict, |
13311 | bool addr_p) | |
34016c81 JM |
13312 | { |
13313 | int nargs; | |
13314 | tree tempargs; | |
13315 | int i; | |
13316 | ||
13317 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
13318 | to function or pointer to member function argument if the set of | |
13319 | overloaded functions does not contain function templates and at most | |
13320 | one of a set of overloaded functions provides a unique match. | |
13321 | ||
13322 | So if this is a template, just return success. */ | |
13323 | ||
13324 | if (uses_template_parms (arg)) | |
13325 | return 1; | |
13326 | ||
f23fb7f5 MM |
13327 | if (TREE_CODE (arg) == METHOD_TYPE) |
13328 | arg = build_ptrmemfunc_type (build_pointer_type (arg)); | |
13329 | else if (addr_p) | |
13330 | arg = build_pointer_type (arg); | |
13331 | ||
8af2fec4 | 13332 | sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL); |
34016c81 JM |
13333 | |
13334 | /* We don't copy orig_targs for this because if we have already deduced | |
13335 | some template args from previous args, unify would complain when we | |
13336 | try to deduce a template parameter for the same argument, even though | |
13337 | there isn't really a conflict. */ | |
13338 | nargs = TREE_VEC_LENGTH (targs); | |
f31c0a32 | 13339 | tempargs = make_tree_vec (nargs); |
34016c81 | 13340 | |
4393e105 | 13341 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
13342 | return 0; |
13343 | ||
13344 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 13345 | explicitly specified args. */ |
34016c81 JM |
13346 | for (i = nargs; i--; ) |
13347 | { | |
13348 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 13349 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 | 13350 | |
a34d3336 NS |
13351 | if (!elt) |
13352 | /*NOP*/; | |
34016c81 | 13353 | else if (uses_template_parms (elt)) |
a34d3336 NS |
13354 | /* Since we're unifying against ourselves, we will fill in |
13355 | template args used in the function parm list with our own | |
13356 | template parms. Discard them. */ | |
13357 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
13358 | else if (oldelt && !template_args_equal (oldelt, elt)) | |
34016c81 JM |
13359 | return 0; |
13360 | } | |
13361 | ||
13362 | for (i = nargs; i--; ) | |
13363 | { | |
13364 | tree elt = TREE_VEC_ELT (tempargs, i); | |
13365 | ||
13366 | if (elt) | |
13367 | TREE_VEC_ELT (targs, i) = elt; | |
13368 | } | |
13369 | ||
13370 | return 1; | |
13371 | } | |
13372 | ||
4393e105 MM |
13373 | /* PARM is a template class (perhaps with unbound template |
13374 | parameters). ARG is a fully instantiated type. If ARG can be | |
13375 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
13376 | TARGS are as for unify. */ | |
fcfb9f96 MM |
13377 | |
13378 | static tree | |
3a978d72 | 13379 | try_class_unification (tree tparms, tree targs, tree parm, tree arg) |
4393e105 | 13380 | { |
4393e105 MM |
13381 | tree copy_of_targs; |
13382 | ||
13383 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
c8094d83 | 13384 | || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg)) |
68361a03 | 13385 | != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))) |
4393e105 MM |
13386 | return NULL_TREE; |
13387 | ||
13388 | /* We need to make a new template argument vector for the call to | |
13389 | unify. If we used TARGS, we'd clutter it up with the result of | |
13390 | the attempted unification, even if this class didn't work out. | |
13391 | We also don't want to commit ourselves to all the unifications | |
13392 | we've already done, since unification is supposed to be done on | |
13393 | an argument-by-argument basis. In other words, consider the | |
13394 | following pathological case: | |
13395 | ||
13396 | template <int I, int J, int K> | |
13397 | struct S {}; | |
c8094d83 | 13398 | |
4393e105 MM |
13399 | template <int I, int J> |
13400 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
c8094d83 | 13401 | |
4393e105 MM |
13402 | template <int I, int J, int K> |
13403 | void f(S<I, J, K>, S<I, I, I>); | |
c8094d83 | 13404 | |
4393e105 | 13405 | void g() { |
0cbd7506 MS |
13406 | S<0, 0, 0> s0; |
13407 | S<0, 1, 2> s2; | |
c8094d83 | 13408 | |
0cbd7506 | 13409 | f(s0, s2); |
4393e105 MM |
13410 | } |
13411 | ||
13412 | Now, by the time we consider the unification involving `s2', we | |
13413 | already know that we must have `f<0, 0, 0>'. But, even though | |
0e339752 | 13414 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid |
4393e105 MM |
13415 | because there are two ways to unify base classes of S<0, 1, 2> |
13416 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
13417 | would reject the possibility I=1. */ | |
f31c0a32 | 13418 | copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); |
c8094d83 | 13419 | |
4393e105 | 13420 | /* If unification failed, we're done. */ |
74601d7c KL |
13421 | if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), |
13422 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE)) | |
4393e105 | 13423 | return NULL_TREE; |
74601d7c KL |
13424 | |
13425 | return arg; | |
4393e105 MM |
13426 | } |
13427 | ||
a3a0fc7f NS |
13428 | /* Given a template type PARM and a class type ARG, find the unique |
13429 | base type in ARG that is an instance of PARM. We do not examine | |
13430 | ARG itself; only its base-classes. If there is not exactly one | |
13431 | appropriate base class, return NULL_TREE. PARM may be the type of | |
13432 | a partial specialization, as well as a plain template type. Used | |
13433 | by unify. */ | |
4393e105 MM |
13434 | |
13435 | static tree | |
a3a0fc7f | 13436 | get_template_base (tree tparms, tree targs, tree parm, tree arg) |
fcfb9f96 | 13437 | { |
a3a0fc7f NS |
13438 | tree rval = NULL_TREE; |
13439 | tree binfo; | |
13440 | ||
9e1e64ec | 13441 | gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg))); |
c8094d83 | 13442 | |
a3a0fc7f NS |
13443 | binfo = TYPE_BINFO (complete_type (arg)); |
13444 | if (!binfo) | |
13445 | /* The type could not be completed. */ | |
13446 | return NULL_TREE; | |
fcfb9f96 | 13447 | |
a3a0fc7f NS |
13448 | /* Walk in inheritance graph order. The search order is not |
13449 | important, and this avoids multiple walks of virtual bases. */ | |
13450 | for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo)) | |
fcfb9f96 | 13451 | { |
a3a0fc7f | 13452 | tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo)); |
fcfb9f96 | 13453 | |
8d83768f NS |
13454 | if (r) |
13455 | { | |
13456 | /* If there is more than one satisfactory baseclass, then: | |
fcfb9f96 | 13457 | |
8d83768f | 13458 | [temp.deduct.call] |
fcfb9f96 | 13459 | |
8d83768f NS |
13460 | If they yield more than one possible deduced A, the type |
13461 | deduction fails. | |
4393e105 | 13462 | |
8d83768f | 13463 | applies. */ |
a3a0fc7f NS |
13464 | if (rval && !same_type_p (r, rval)) |
13465 | return NULL_TREE; | |
c8094d83 | 13466 | |
a3a0fc7f | 13467 | rval = r; |
8d83768f | 13468 | } |
fcfb9f96 MM |
13469 | } |
13470 | ||
a3a0fc7f | 13471 | return rval; |
fcfb9f96 MM |
13472 | } |
13473 | ||
db2767b6 MM |
13474 | /* Returns the level of DECL, which declares a template parameter. */ |
13475 | ||
e9659ab0 | 13476 | static int |
3a978d72 | 13477 | template_decl_level (tree decl) |
db2767b6 MM |
13478 | { |
13479 | switch (TREE_CODE (decl)) | |
13480 | { | |
13481 | case TYPE_DECL: | |
13482 | case TEMPLATE_DECL: | |
13483 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
13484 | ||
13485 | case PARM_DECL: | |
13486 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
13487 | ||
13488 | default: | |
315fb5db | 13489 | gcc_unreachable (); |
db2767b6 | 13490 | } |
315fb5db | 13491 | return 0; |
db2767b6 MM |
13492 | } |
13493 | ||
830bfa74 MM |
13494 | /* Decide whether ARG can be unified with PARM, considering only the |
13495 | cv-qualifiers of each type, given STRICT as documented for unify. | |
324f9dfb | 13496 | Returns nonzero iff the unification is OK on that basis. */ |
e92cc029 | 13497 | |
e9659ab0 | 13498 | static int |
3a978d72 | 13499 | check_cv_quals_for_unify (int strict, tree arg, tree parm) |
830bfa74 | 13500 | { |
4f2b0fb2 NS |
13501 | int arg_quals = cp_type_quals (arg); |
13502 | int parm_quals = cp_type_quals (parm); | |
13503 | ||
355f774d NS |
13504 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
13505 | && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) | |
4f2b0fb2 | 13506 | { |
2e9ceb77 | 13507 | /* Although a CVR qualifier is ignored when being applied to a |
0cbd7506 MS |
13508 | substituted template parameter ([8.3.2]/1 for example), that |
13509 | does not apply during deduction [14.8.2.4]/1, (even though | |
13510 | that is not explicitly mentioned, [14.8.2.4]/9 indicates | |
13511 | this). Except when we're allowing additional CV qualifiers | |
13512 | at the outer level [14.8.2.1]/3,1st bullet. */ | |
2e9ceb77 NS |
13513 | if ((TREE_CODE (arg) == REFERENCE_TYPE |
13514 | || TREE_CODE (arg) == FUNCTION_TYPE | |
13515 | || TREE_CODE (arg) == METHOD_TYPE) | |
13516 | && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))) | |
13517 | return 0; | |
13518 | ||
13519 | if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM) | |
13520 | && (parm_quals & TYPE_QUAL_RESTRICT)) | |
13521 | return 0; | |
4f2b0fb2 | 13522 | } |
2e9ceb77 | 13523 | |
62e4a758 | 13524 | if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
4f2b0fb2 | 13525 | && (arg_quals & parm_quals) != parm_quals) |
ef637255 MM |
13526 | return 0; |
13527 | ||
62e4a758 | 13528 | if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) |
4f2b0fb2 | 13529 | && (parm_quals & arg_quals) != arg_quals) |
ef637255 MM |
13530 | return 0; |
13531 | ||
ef637255 | 13532 | return 1; |
830bfa74 MM |
13533 | } |
13534 | ||
5d80a306 DG |
13535 | /* Determines the LEVEL and INDEX for the template parameter PARM. */ |
13536 | void | |
13537 | template_parm_level_and_index (tree parm, int* level, int* index) | |
13538 | { | |
13539 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
13540 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
13541 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
13542 | { | |
13543 | *index = TEMPLATE_TYPE_IDX (parm); | |
13544 | *level = TEMPLATE_TYPE_LEVEL (parm); | |
13545 | } | |
13546 | else | |
13547 | { | |
13548 | *index = TEMPLATE_PARM_IDX (parm); | |
13549 | *level = TEMPLATE_PARM_LEVEL (parm); | |
13550 | } | |
13551 | } | |
13552 | ||
13553 | /* Unifies the remaining arguments in PACKED_ARGS with the pack | |
13554 | expansion at the end of PACKED_PARMS. Returns 0 if the type | |
13555 | deduction succeeds, 1 otherwise. STRICT is the same as in | |
13556 | unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function | |
13557 | call argument list. We'll need to adjust the arguments to make them | |
13558 | types. SUBR tells us if this is from a recursive call to | |
13559 | type_unification_real. */ | |
13560 | int | |
13561 | unify_pack_expansion (tree tparms, tree targs, tree packed_parms, | |
13562 | tree packed_args, int strict, bool call_args_p, | |
13563 | bool subr) | |
13564 | { | |
13565 | tree parm | |
13566 | = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1); | |
13567 | tree pattern = PACK_EXPANSION_PATTERN (parm); | |
13568 | tree pack, packs = NULL_TREE; | |
13569 | int i, start = TREE_VEC_LENGTH (packed_parms) - 1; | |
13570 | int len = TREE_VEC_LENGTH (packed_args); | |
13571 | ||
13572 | /* Determine the parameter packs we will be deducing from the | |
13573 | pattern, and record their current deductions. */ | |
13574 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm); | |
13575 | pack; pack = TREE_CHAIN (pack)) | |
13576 | { | |
13577 | tree parm_pack = TREE_VALUE (pack); | |
13578 | int idx, level; | |
13579 | ||
13580 | /* Determine the index and level of this parameter pack. */ | |
13581 | template_parm_level_and_index (parm_pack, &level, &idx); | |
13582 | ||
13583 | /* Keep track of the parameter packs and their corresponding | |
13584 | argument packs. */ | |
13585 | packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs); | |
13586 | TREE_TYPE (packs) = make_tree_vec (len - start); | |
13587 | } | |
13588 | ||
13589 | /* Loop through all of the arguments that have not yet been | |
13590 | unified and unify each with the pattern. */ | |
13591 | for (i = start; i < len; i++) | |
13592 | { | |
13593 | tree parm = pattern; | |
13594 | ||
13595 | /* For each parameter pack, clear out the deduced value so that | |
13596 | we can deduce it again. */ | |
13597 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
13598 | { | |
13599 | int idx, level; | |
13600 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
13601 | ||
13602 | TMPL_ARG (targs, level, idx) = NULL_TREE; | |
13603 | } | |
13604 | ||
13605 | /* Unify the pattern with the current argument. */ | |
13606 | { | |
13607 | tree arg = TREE_VEC_ELT (packed_args, i); | |
2afad0f6 | 13608 | tree arg_expr = NULL_TREE; |
5d80a306 DG |
13609 | int arg_strict = strict; |
13610 | bool skip_arg_p = false; | |
13611 | ||
13612 | if (call_args_p) | |
13613 | { | |
13614 | int sub_strict; | |
13615 | ||
13616 | /* This mirrors what we do in type_unification_real. */ | |
13617 | switch (strict) | |
13618 | { | |
13619 | case DEDUCE_CALL: | |
13620 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | |
13621 | | UNIFY_ALLOW_MORE_CV_QUAL | |
13622 | | UNIFY_ALLOW_DERIVED); | |
13623 | break; | |
13624 | ||
13625 | case DEDUCE_CONV: | |
13626 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
13627 | break; | |
13628 | ||
13629 | case DEDUCE_EXACT: | |
13630 | sub_strict = UNIFY_ALLOW_NONE; | |
13631 | break; | |
13632 | ||
13633 | default: | |
13634 | gcc_unreachable (); | |
13635 | } | |
13636 | ||
13637 | if (!TYPE_P (arg)) | |
13638 | { | |
13639 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); | |
13640 | if (type_unknown_p (arg)) | |
13641 | { | |
13642 | /* [temp.deduct.type] A template-argument can be | |
13643 | deduced from a pointer to function or pointer | |
13644 | to member function argument if the set of | |
13645 | overloaded functions does not contain function | |
13646 | templates and at most one of a set of | |
13647 | overloaded functions provides a unique | |
13648 | match. */ | |
13649 | ||
13650 | if (resolve_overloaded_unification | |
a916d97f ILT |
13651 | (tparms, targs, parm, arg, |
13652 | (unification_kind_t) strict, | |
13653 | sub_strict) | |
5d80a306 DG |
13654 | != 0) |
13655 | return 1; | |
13656 | skip_arg_p = true; | |
13657 | } | |
13658 | ||
13659 | if (!skip_arg_p) | |
13660 | { | |
2afad0f6 DG |
13661 | arg_expr = arg; |
13662 | arg = unlowered_expr_type (arg); | |
5d80a306 DG |
13663 | if (arg == error_mark_node) |
13664 | return 1; | |
13665 | } | |
13666 | } | |
13667 | ||
13668 | arg_strict = sub_strict; | |
13669 | ||
13670 | if (!subr) | |
13671 | arg_strict |= | |
a916d97f ILT |
13672 | maybe_adjust_types_for_deduction ((unification_kind_t) strict, |
13673 | &parm, &arg, arg_expr); | |
5d80a306 DG |
13674 | } |
13675 | ||
13676 | if (!skip_arg_p) | |
13677 | { | |
13678 | if (unify (tparms, targs, parm, arg, arg_strict)) | |
13679 | return 1; | |
13680 | } | |
13681 | } | |
13682 | ||
13683 | /* For each parameter pack, collect the deduced value. */ | |
13684 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
13685 | { | |
13686 | int idx, level; | |
13687 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
13688 | ||
13689 | TREE_VEC_ELT (TREE_TYPE (pack), i - start) = | |
13690 | TMPL_ARG (targs, level, idx); | |
13691 | } | |
13692 | } | |
13693 | ||
13694 | /* Verify that the results of unification with the parameter packs | |
13695 | produce results consistent with what we've seen before, and make | |
13696 | the deduced argument packs available. */ | |
13697 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) | |
13698 | { | |
13699 | tree old_pack = TREE_VALUE (pack); | |
13700 | tree new_args = TREE_TYPE (pack); | |
db60ff18 DG |
13701 | int i, len = TREE_VEC_LENGTH (new_args); |
13702 | bool nondeduced_p = false; | |
13703 | ||
13704 | /* If NEW_ARGS contains any NULL_TREE entries, we didn't | |
13705 | actually deduce anything. */ | |
13706 | for (i = 0; i < len && !nondeduced_p; ++i) | |
13707 | if (TREE_VEC_ELT (new_args, i) == NULL_TREE) | |
13708 | nondeduced_p = true; | |
13709 | if (nondeduced_p) | |
13710 | continue; | |
5d80a306 DG |
13711 | |
13712 | if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack)) | |
13713 | { | |
13714 | /* Prepend the explicit arguments onto NEW_ARGS. */ | |
13715 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); | |
13716 | tree old_args = new_args; | |
13717 | int i, explicit_len = TREE_VEC_LENGTH (explicit_args); | |
13718 | int len = explicit_len + TREE_VEC_LENGTH (old_args); | |
13719 | ||
13720 | /* Copy the explicit arguments. */ | |
13721 | new_args = make_tree_vec (len); | |
13722 | for (i = 0; i < explicit_len; i++) | |
13723 | TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i); | |
13724 | ||
13725 | /* Copy the deduced arguments. */ | |
13726 | for (; i < len; i++) | |
13727 | TREE_VEC_ELT (new_args, i) = | |
13728 | TREE_VEC_ELT (old_args, i - explicit_len); | |
13729 | } | |
13730 | ||
13731 | if (!old_pack) | |
13732 | { | |
13733 | tree result; | |
13734 | int idx, level; | |
13735 | ||
13736 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
13737 | ||
13738 | /* Build the deduced *_ARGUMENT_PACK. */ | |
13739 | if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX) | |
13740 | { | |
13741 | result = make_node (NONTYPE_ARGUMENT_PACK); | |
13742 | TREE_TYPE (result) = | |
13743 | TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack))); | |
13744 | TREE_CONSTANT (result) = 1; | |
13745 | } | |
13746 | else | |
13747 | result = make_node (TYPE_ARGUMENT_PACK); | |
13748 | ||
13749 | SET_ARGUMENT_PACK_ARGS (result, new_args); | |
13750 | ||
13751 | /* Note the deduced argument packs for this parameter | |
13752 | pack. */ | |
13753 | TMPL_ARG (targs, level, idx) = result; | |
13754 | } | |
13755 | else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack) | |
13756 | && (ARGUMENT_PACK_ARGS (old_pack) | |
13757 | == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack))) | |
13758 | { | |
13759 | /* We only had the explicitly-provided arguments before, but | |
13760 | now we have a complete set of arguments. */ | |
13761 | int idx, level; | |
13762 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); | |
13763 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
13764 | ||
13765 | /* Keep the original deduced argument pack. */ | |
13766 | TMPL_ARG (targs, level, idx) = old_pack; | |
13767 | ||
13768 | SET_ARGUMENT_PACK_ARGS (old_pack, new_args); | |
13769 | ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1; | |
13770 | ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args; | |
13771 | } | |
13772 | else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack), | |
13773 | new_args)) | |
13774 | /* Inconsistent unification of this parameter pack. */ | |
13775 | return 1; | |
13776 | else | |
13777 | { | |
13778 | int idx, level; | |
13779 | ||
13780 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); | |
13781 | ||
13782 | /* Keep the original deduced argument pack. */ | |
13783 | TMPL_ARG (targs, level, idx) = old_pack; | |
13784 | } | |
13785 | } | |
13786 | ||
13787 | return 0; | |
13788 | } | |
13789 | ||
916b63c3 MM |
13790 | /* Deduce the value of template parameters. TPARMS is the (innermost) |
13791 | set of template parameters to a template. TARGS is the bindings | |
13792 | for those template parameters, as determined thus far; TARGS may | |
13793 | include template arguments for outer levels of template parameters | |
13794 | as well. PARM is a parameter to a template function, or a | |
13795 | subcomponent of that parameter; ARG is the corresponding argument. | |
13796 | This function attempts to match PARM with ARG in a manner | |
13797 | consistent with the existing assignments in TARGS. If more values | |
13798 | are deduced, then TARGS is updated. | |
13799 | ||
13800 | Returns 0 if the type deduction succeeds, 1 otherwise. The | |
13801 | parameter STRICT is a bitwise or of the following flags: | |
830bfa74 MM |
13802 | |
13803 | UNIFY_ALLOW_NONE: | |
13804 | Require an exact match between PARM and ARG. | |
13805 | UNIFY_ALLOW_MORE_CV_QUAL: | |
028d1f20 NS |
13806 | Allow the deduced ARG to be more cv-qualified (by qualification |
13807 | conversion) than ARG. | |
830bfa74 MM |
13808 | UNIFY_ALLOW_LESS_CV_QUAL: |
13809 | Allow the deduced ARG to be less cv-qualified than ARG. | |
13810 | UNIFY_ALLOW_DERIVED: | |
13811 | Allow the deduced ARG to be a template base class of ARG, | |
13812 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
13813 | ARG. |
13814 | UNIFY_ALLOW_INTEGER: | |
13815 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
c8094d83 | 13816 | case for more information. |
028d1f20 NS |
13817 | UNIFY_ALLOW_OUTER_LEVEL: |
13818 | This is the outermost level of a deduction. Used to determine validity | |
13819 | of qualification conversions. A valid qualification conversion must | |
13820 | have const qualified pointers leading up to the inner type which | |
13821 | requires additional CV quals, except at the outer level, where const | |
13822 | is not required [conv.qual]. It would be normal to set this flag in | |
62e4a758 NS |
13823 | addition to setting UNIFY_ALLOW_MORE_CV_QUAL. |
13824 | UNIFY_ALLOW_OUTER_MORE_CV_QUAL: | |
13825 | This is the outermost level of a deduction, and PARM can be more CV | |
13826 | qualified at this point. | |
13827 | UNIFY_ALLOW_OUTER_LESS_CV_QUAL: | |
13828 | This is the outermost level of a deduction, and PARM can be less CV | |
8baddbf1 | 13829 | qualified at this point. */ |
830bfa74 | 13830 | |
e9659ab0 | 13831 | static int |
3a978d72 | 13832 | unify (tree tparms, tree targs, tree parm, tree arg, int strict) |
8d08fdba MS |
13833 | { |
13834 | int idx; | |
050367a3 | 13835 | tree targ; |
db2767b6 | 13836 | tree tparm; |
028d1f20 | 13837 | int strict_in = strict; |
8d08fdba MS |
13838 | |
13839 | /* I don't think this will do the right thing with respect to types. | |
13840 | But the only case I've seen it in so far has been array bounds, where | |
13841 | signedness is the only information lost, and I think that will be | |
13842 | okay. */ | |
13843 | while (TREE_CODE (parm) == NOP_EXPR) | |
13844 | parm = TREE_OPERAND (parm, 0); | |
13845 | ||
13846 | if (arg == error_mark_node) | |
13847 | return 1; | |
09357846 JM |
13848 | if (arg == unknown_type_node |
13849 | || arg == init_list_type_node) | |
34016c81 JM |
13850 | /* We can't deduce anything from this, but we might get all the |
13851 | template args from other function args. */ | |
13852 | return 0; | |
13853 | ||
db2767b6 | 13854 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 13855 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
13856 | template parameters. We might need them if we're trying to |
13857 | figure out which of two things is more specialized. */ | |
13858 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
13859 | return 0; |
13860 | ||
09357846 JM |
13861 | /* Handle init lists early, so the rest of the function can assume |
13862 | we're dealing with a type. */ | |
13863 | if (BRACE_ENCLOSED_INITIALIZER_P (arg)) | |
13864 | { | |
13865 | tree elt, elttype; | |
13866 | unsigned i; | |
5f5babf1 JM |
13867 | tree orig_parm = parm; |
13868 | ||
13869 | /* Replace T with std::initializer_list<T> for deduction. */ | |
13870 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
13871 | && flag_deduce_init_list) | |
13872 | parm = listify (parm); | |
09357846 JM |
13873 | |
13874 | if (!is_std_init_list (parm)) | |
13875 | /* We can only deduce from an initializer list argument if the | |
13876 | parameter is std::initializer_list; otherwise this is a | |
13877 | non-deduced context. */ | |
13878 | return 0; | |
13879 | ||
13880 | elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0); | |
13881 | ||
13882 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt) | |
13883 | { | |
2604de9d | 13884 | int elt_strict = strict; |
09357846 | 13885 | if (!BRACE_ENCLOSED_INITIALIZER_P (elt)) |
2604de9d JM |
13886 | { |
13887 | tree type = TREE_TYPE (elt); | |
13888 | /* It should only be possible to get here for a call. */ | |
13889 | gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL); | |
13890 | elt_strict |= maybe_adjust_types_for_deduction | |
13891 | (DEDUCE_CALL, &elttype, &type, elt); | |
13892 | elt = type; | |
13893 | } | |
13894 | ||
13895 | if (unify (tparms, targs, elttype, elt, elt_strict)) | |
09357846 JM |
13896 | return 1; |
13897 | } | |
5f5babf1 JM |
13898 | |
13899 | /* If the std::initializer_list<T> deduction worked, replace the | |
13900 | deduced A with std::initializer_list<A>. */ | |
13901 | if (orig_parm != parm) | |
13902 | { | |
13903 | idx = TEMPLATE_TYPE_IDX (orig_parm); | |
13904 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); | |
13905 | targ = listify (targ); | |
13906 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ; | |
13907 | } | |
09357846 JM |
13908 | return 0; |
13909 | } | |
13910 | ||
830bfa74 MM |
13911 | /* Immediately reject some pairs that won't unify because of |
13912 | cv-qualification mismatches. */ | |
13913 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
2f939d94 | 13914 | && TYPE_P (arg) |
d0ab7624 | 13915 | /* It is the elements of the array which hold the cv quals of an array |
0cbd7506 MS |
13916 | type, and the elements might be template type parms. We'll check |
13917 | when we recurse. */ | |
d0ab7624 | 13918 | && TREE_CODE (arg) != ARRAY_TYPE |
830bfa74 MM |
13919 | /* We check the cv-qualifiers when unifying with template type |
13920 | parameters below. We want to allow ARG `const T' to unify with | |
13921 | PARM `T' for example, when computing which of two templates | |
13922 | is more specialized, for example. */ | |
13923 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
028d1f20 | 13924 | && !check_cv_quals_for_unify (strict_in, arg, parm)) |
49432171 JM |
13925 | return 1; |
13926 | ||
028d1f20 | 13927 | if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) |
3ea099f1 | 13928 | && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) |
028d1f20 NS |
13929 | strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; |
13930 | strict &= ~UNIFY_ALLOW_OUTER_LEVEL; | |
13931 | strict &= ~UNIFY_ALLOW_DERIVED; | |
62e4a758 NS |
13932 | strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
13933 | strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; | |
c8094d83 | 13934 | |
8d08fdba MS |
13935 | switch (TREE_CODE (parm)) |
13936 | { | |
2ca340ae | 13937 | case TYPENAME_TYPE: |
fccef71e | 13938 | case SCOPE_REF: |
b8c6534b | 13939 | case UNBOUND_CLASS_TEMPLATE: |
2ca340ae JM |
13940 | /* In a type which contains a nested-name-specifier, template |
13941 | argument values cannot be deduced for template parameters used | |
13942 | within the nested-name-specifier. */ | |
13943 | return 0; | |
13944 | ||
8d08fdba | 13945 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 13946 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 13947 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
db2767b6 | 13948 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
833be229 PC |
13949 | if (tparm == error_mark_node) |
13950 | return 1; | |
db2767b6 MM |
13951 | |
13952 | if (TEMPLATE_TYPE_LEVEL (parm) | |
13953 | != template_decl_level (tparm)) | |
13954 | /* The PARM is not one we're trying to unify. Just check | |
13955 | to see if it matches ARG. */ | |
13956 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 13957 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 13958 | idx = TEMPLATE_TYPE_IDX (parm); |
916b63c3 | 13959 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 13960 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 13961 | |
73b0fce8 | 13962 | /* Check for mixed types and values. */ |
db2767b6 MM |
13963 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
13964 | && TREE_CODE (tparm) != TYPE_DECL) | |
c8094d83 | 13965 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
db2767b6 | 13966 | && TREE_CODE (tparm) != TEMPLATE_DECL)) |
73b0fce8 KL |
13967 | return 1; |
13968 | ||
a1281f45 | 13969 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 13970 | { |
b429fdf0 KL |
13971 | /* ARG must be constructed from a template class or a template |
13972 | template parameter. */ | |
13973 | if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM | |
4e95268d | 13974 | && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) |
a1281f45 | 13975 | return 1; |
73b0fce8 | 13976 | |
a1281f45 | 13977 | { |
a1281f45 | 13978 | tree parmvec = TYPE_TI_ARGS (parm); |
6df91b00 | 13979 | tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg)); |
a0a5f30f | 13980 | tree parm_parms |
063cc99c | 13981 | = DECL_INNERMOST_TEMPLATE_PARMS |
a0a5f30f | 13982 | (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm)); |
4cf36211 DG |
13983 | int i, len; |
13984 | int parm_variadic_p = 0; | |
73b0fce8 | 13985 | |
e7e93965 MM |
13986 | /* The resolution to DR150 makes clear that default |
13987 | arguments for an N-argument may not be used to bind T | |
13988 | to a template template parameter with fewer than N | |
13989 | parameters. It is not safe to permit the binding of | |
13990 | default arguments as an extension, as that may change | |
13991 | the meaning of a conforming program. Consider: | |
13992 | ||
13993 | struct Dense { static const unsigned int dim = 1; }; | |
13994 | ||
13995 | template <template <typename> class View, | |
13996 | typename Block> | |
13997 | void operator+(float, View<Block> const&); | |
13998 | ||
3db45ab5 MS |
13999 | template <typename Block, |
14000 | unsigned int Dim = Block::dim> | |
e7e93965 MM |
14001 | struct Lvalue_proxy { operator float() const; }; |
14002 | ||
14003 | void | |
14004 | test_1d (void) { | |
14005 | Lvalue_proxy<Dense> p; | |
14006 | float b; | |
14007 | b + p; | |
14008 | } | |
a1281f45 | 14009 | |
e7e93965 MM |
14010 | Here, if Lvalue_proxy is permitted to bind to View, then |
14011 | the global operator+ will be used; if they are not, the | |
3db45ab5 | 14012 | Lvalue_proxy will be converted to float. */ |
a0a5f30f DG |
14013 | if (coerce_template_parms (parm_parms, |
14014 | argvec, | |
e7e93965 MM |
14015 | TYPE_TI_TEMPLATE (parm), |
14016 | tf_none, | |
14017 | /*require_all_args=*/true, | |
14018 | /*use_default_args=*/false) | |
0cbd7506 | 14019 | == error_mark_node) |
a1281f45 | 14020 | return 1; |
c8094d83 MS |
14021 | |
14022 | /* Deduce arguments T, i from TT<T> or TT<i>. | |
a1281f45 KL |
14023 | We check each element of PARMVEC and ARGVEC individually |
14024 | rather than the whole TREE_VEC since they can have | |
14025 | different number of elements. */ | |
6b9b6b15 | 14026 | |
4cf36211 DG |
14027 | parmvec = expand_template_argument_pack (parmvec); |
14028 | argvec = expand_template_argument_pack (argvec); | |
14029 | ||
14030 | len = TREE_VEC_LENGTH (parmvec); | |
14031 | ||
14032 | /* Check if the parameters end in a pack, making them | |
14033 | variadic. */ | |
14034 | if (len > 0 | |
14035 | && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1))) | |
14036 | parm_variadic_p = 1; | |
14037 | ||
14038 | if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p) | |
14039 | return 1; | |
14040 | ||
14041 | for (i = 0; i < len - parm_variadic_p; ++i) | |
a1281f45 | 14042 | { |
0cbd7506 | 14043 | if (unify (tparms, targs, |
c8094d83 MS |
14044 | TREE_VEC_ELT (parmvec, i), |
14045 | TREE_VEC_ELT (argvec, i), | |
a1281f45 KL |
14046 | UNIFY_ALLOW_NONE)) |
14047 | return 1; | |
73b0fce8 | 14048 | } |
4cf36211 DG |
14049 | |
14050 | if (parm_variadic_p | |
14051 | && unify_pack_expansion (tparms, targs, | |
14052 | parmvec, argvec, | |
14053 | UNIFY_ALLOW_NONE, | |
14054 | /*call_args_p=*/false, | |
14055 | /*subr=*/false)) | |
14056 | return 1; | |
a1281f45 | 14057 | } |
b429fdf0 | 14058 | arg = TYPE_TI_TEMPLATE (arg); |
a1281f45 KL |
14059 | |
14060 | /* Fall through to deduce template name. */ | |
14061 | } | |
14062 | ||
14063 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
14064 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
14065 | { | |
14066 | /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ | |
14067 | ||
14068 | /* Simple cases: Value already set, does match or doesn't. */ | |
14069 | if (targ != NULL_TREE && template_args_equal (targ, arg)) | |
14070 | return 0; | |
14071 | else if (targ) | |
14072 | return 1; | |
db2767b6 MM |
14073 | } |
14074 | else | |
14075 | { | |
830bfa74 MM |
14076 | /* If PARM is `const T' and ARG is only `int', we don't have |
14077 | a match unless we are allowing additional qualification. | |
14078 | If ARG is `const int' and PARM is just `T' that's OK; | |
14079 | that binds `const int' to `T'. */ | |
c8094d83 | 14080 | if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, |
830bfa74 | 14081 | arg, parm)) |
db2767b6 MM |
14082 | return 1; |
14083 | ||
830bfa74 MM |
14084 | /* Consider the case where ARG is `const volatile int' and |
14085 | PARM is `const T'. Then, T should be `volatile int'. */ | |
c2ea3a40 NS |
14086 | arg = cp_build_qualified_type_real |
14087 | (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); | |
adecb3f4 MM |
14088 | if (arg == error_mark_node) |
14089 | return 1; | |
73b0fce8 | 14090 | |
a1281f45 KL |
14091 | /* Simple cases: Value already set, does match or doesn't. */ |
14092 | if (targ != NULL_TREE && same_type_p (targ, arg)) | |
14093 | return 0; | |
14094 | else if (targ) | |
14095 | return 1; | |
61cd552e | 14096 | |
94fc547c MM |
14097 | /* Make sure that ARG is not a variable-sized array. (Note |
14098 | that were talking about variable-sized arrays (like | |
14099 | `int[n]'), rather than arrays of unknown size (like | |
14100 | `int[]').) We'll get very confused by such a type since | |
14101 | the bound of the array will not be computable in an | |
14102 | instantiation. Besides, such types are not allowed in | |
14103 | ISO C++, so we can do as we please here. */ | |
5377d5ba | 14104 | if (variably_modified_type_p (arg, NULL_TREE)) |
94fc547c | 14105 | return 1; |
a4d674fc JM |
14106 | |
14107 | /* Strip typedefs as in convert_template_argument. */ | |
cd41d410 | 14108 | arg = strip_typedefs (arg); |
94fc547c | 14109 | } |
61cd552e | 14110 | |
5d80a306 DG |
14111 | /* If ARG is a parameter pack or an expansion, we cannot unify |
14112 | against it unless PARM is also a parameter pack. */ | |
14113 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
14114 | && !template_parameter_pack_p (parm)) | |
14115 | return 1; | |
14116 | ||
111a28c2 DS |
14117 | /* If the argument deduction results is a METHOD_TYPE, |
14118 | then there is a problem. | |
14119 | METHOD_TYPE doesn't map to any real C++ type the result of | |
14120 | the deduction can not be of that type. */ | |
14121 | if (TREE_CODE (arg) == METHOD_TYPE) | |
14122 | return 1; | |
14123 | ||
916b63c3 | 14124 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
73b0fce8 KL |
14125 | return 0; |
14126 | ||
f84b4be9 | 14127 | case TEMPLATE_PARM_INDEX: |
db2767b6 | 14128 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
3e9ac7e5 VR |
14129 | if (tparm == error_mark_node) |
14130 | return 1; | |
db2767b6 | 14131 | |
c8094d83 | 14132 | if (TEMPLATE_PARM_LEVEL (parm) |
db2767b6 MM |
14133 | != template_decl_level (tparm)) |
14134 | /* The PARM is not one we're trying to unify. Just check | |
14135 | to see if it matches ARG. */ | |
c8a209ca NS |
14136 | return !(TREE_CODE (arg) == TREE_CODE (parm) |
14137 | && cp_tree_equal (parm, arg)); | |
db2767b6 | 14138 | |
f84b4be9 | 14139 | idx = TEMPLATE_PARM_IDX (parm); |
916b63c3 | 14140 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 14141 | |
050367a3 | 14142 | if (targ) |
c8a209ca | 14143 | return !cp_tree_equal (targ, arg); |
8d08fdba | 14144 | |
161c12b0 JM |
14145 | /* [temp.deduct.type] If, in the declaration of a function template |
14146 | with a non-type template-parameter, the non-type | |
14147 | template-parameter is used in an expression in the function | |
14148 | parameter-list and, if the corresponding template-argument is | |
14149 | deduced, the template-argument type shall match the type of the | |
14150 | template-parameter exactly, except that a template-argument | |
c8094d83 | 14151 | deduced from an array bound may be of any integral type. |
d7c4edd0 | 14152 | The non-type parameter might use already deduced type parameters. */ |
bd0d5d4a | 14153 | tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); |
e2005c8d KL |
14154 | if (!TREE_TYPE (arg)) |
14155 | /* Template-parameter dependent expression. Just accept it for now. | |
14156 | It will later be processed in convert_template_argument. */ | |
14157 | ; | |
14158 | else if (same_type_p (TREE_TYPE (arg), tparm)) | |
14159 | /* OK */; | |
161c12b0 | 14160 | else if ((strict & UNIFY_ALLOW_INTEGER) |
bd0d5d4a JM |
14161 | && (TREE_CODE (tparm) == INTEGER_TYPE |
14162 | || TREE_CODE (tparm) == BOOLEAN_TYPE)) | |
8baddbf1 MM |
14163 | /* Convert the ARG to the type of PARM; the deduced non-type |
14164 | template argument must exactly match the types of the | |
14165 | corresponding parameter. */ | |
ed7284dd | 14166 | arg = fold (build_nop (tparm, arg)); |
bd0d5d4a JM |
14167 | else if (uses_template_parms (tparm)) |
14168 | /* We haven't deduced the type of this parameter yet. Try again | |
14169 | later. */ | |
14170 | return 0; | |
161c12b0 JM |
14171 | else |
14172 | return 1; | |
14173 | ||
5d80a306 DG |
14174 | /* If ARG is a parameter pack or an expansion, we cannot unify |
14175 | against it unless PARM is also a parameter pack. */ | |
14176 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) | |
14177 | && !TEMPLATE_PARM_PARAMETER_PACK (parm)) | |
14178 | return 1; | |
14179 | ||
916b63c3 | 14180 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
8d08fdba MS |
14181 | return 0; |
14182 | ||
28e8f3a0 GB |
14183 | case PTRMEM_CST: |
14184 | { | |
0cbd7506 MS |
14185 | /* A pointer-to-member constant can be unified only with |
14186 | another constant. */ | |
28e8f3a0 | 14187 | if (TREE_CODE (arg) != PTRMEM_CST) |
0cbd7506 | 14188 | return 1; |
28e8f3a0 GB |
14189 | |
14190 | /* Just unify the class member. It would be useless (and possibly | |
0cbd7506 MS |
14191 | wrong, depending on the strict flags) to unify also |
14192 | PTRMEM_CST_CLASS, because we want to be sure that both parm and | |
14193 | arg refer to the same variable, even if through different | |
14194 | classes. For instance: | |
28e8f3a0 | 14195 | |
0cbd7506 MS |
14196 | struct A { int x; }; |
14197 | struct B : A { }; | |
28e8f3a0 | 14198 | |
0cbd7506 | 14199 | Unification of &A::x and &B::x must succeed. */ |
28e8f3a0 | 14200 | return unify (tparms, targs, PTRMEM_CST_MEMBER (parm), |
0cbd7506 | 14201 | PTRMEM_CST_MEMBER (arg), strict); |
28e8f3a0 GB |
14202 | } |
14203 | ||
8d08fdba | 14204 | case POINTER_TYPE: |
830bfa74 | 14205 | { |
830bfa74 MM |
14206 | if (TREE_CODE (arg) != POINTER_TYPE) |
14207 | return 1; | |
c8094d83 | 14208 | |
830bfa74 MM |
14209 | /* [temp.deduct.call] |
14210 | ||
14211 | A can be another pointer or pointer to member type that can | |
14212 | be converted to the deduced A via a qualification | |
14213 | conversion (_conv.qual_). | |
14214 | ||
14215 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
14216 | This will allow for additional cv-qualification of the | |
028d1f20 | 14217 | pointed-to types if appropriate. */ |
c8094d83 | 14218 | |
028d1f20 | 14219 | if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) |
830bfa74 MM |
14220 | /* The derived-to-base conversion only persists through one |
14221 | level of pointers. */ | |
028d1f20 | 14222 | strict |= (strict_in & UNIFY_ALLOW_DERIVED); |
830bfa74 | 14223 | |
c8094d83 | 14224 | return unify (tparms, targs, TREE_TYPE (parm), |
028d1f20 | 14225 | TREE_TYPE (arg), strict); |
830bfa74 | 14226 | } |
8d08fdba MS |
14227 | |
14228 | case REFERENCE_TYPE: | |
830bfa74 MM |
14229 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
14230 | return 1; | |
14231 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
028d1f20 | 14232 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
14233 | |
14234 | case ARRAY_TYPE: | |
14235 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
14236 | return 1; | |
3042d5be MM |
14237 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
14238 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
14239 | return 1; | |
8baddbf1 MM |
14240 | if (TYPE_DOMAIN (parm) != NULL_TREE) |
14241 | { | |
14242 | tree parm_max; | |
14243 | tree arg_max; | |
6ee3ffe8 MM |
14244 | bool parm_cst; |
14245 | bool arg_cst; | |
8baddbf1 MM |
14246 | |
14247 | /* Our representation of array types uses "N - 1" as the | |
14248 | TYPE_MAX_VALUE for an array with "N" elements, if "N" is | |
6ee3ffe8 MM |
14249 | not an integer constant. We cannot unify arbitrarily |
14250 | complex expressions, so we eliminate the MINUS_EXPRs | |
14251 | here. */ | |
14252 | parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm)); | |
14253 | parm_cst = TREE_CODE (parm_max) == INTEGER_CST; | |
14254 | if (!parm_cst) | |
8baddbf1 | 14255 | { |
6ee3ffe8 | 14256 | gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR); |
8baddbf1 MM |
14257 | parm_max = TREE_OPERAND (parm_max, 0); |
14258 | } | |
6ee3ffe8 MM |
14259 | arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg)); |
14260 | arg_cst = TREE_CODE (arg_max) == INTEGER_CST; | |
14261 | if (!arg_cst) | |
14262 | { | |
14263 | /* The ARG_MAX may not be a simple MINUS_EXPR, if we are | |
14264 | trying to unify the type of a variable with the type | |
14265 | of a template parameter. For example: | |
14266 | ||
14267 | template <unsigned int N> | |
14268 | void f (char (&) [N]); | |
14269 | int g(); | |
14270 | void h(int i) { | |
14271 | char a[g(i)]; | |
14272 | f(a); | |
14273 | } | |
14274 | ||
14275 | Here, the type of the ARG will be "int [g(i)]", and | |
14276 | may be a SAVE_EXPR, etc. */ | |
14277 | if (TREE_CODE (arg_max) != MINUS_EXPR) | |
14278 | return 1; | |
14279 | arg_max = TREE_OPERAND (arg_max, 0); | |
14280 | } | |
14281 | ||
14282 | /* If only one of the bounds used a MINUS_EXPR, compensate | |
14283 | by adding one to the other bound. */ | |
14284 | if (parm_cst && !arg_cst) | |
db3927fb | 14285 | parm_max = fold_build2_loc (input_location, PLUS_EXPR, |
6ee3ffe8 MM |
14286 | integer_type_node, |
14287 | parm_max, | |
14288 | integer_one_node); | |
14289 | else if (arg_cst && !parm_cst) | |
db3927fb | 14290 | arg_max = fold_build2_loc (input_location, PLUS_EXPR, |
6ee3ffe8 MM |
14291 | integer_type_node, |
14292 | arg_max, | |
14293 | integer_one_node); | |
8baddbf1 MM |
14294 | |
14295 | if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER)) | |
14296 | return 1; | |
14297 | } | |
830bfa74 | 14298 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
712467a4 | 14299 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
14300 | |
14301 | case REAL_TYPE: | |
37c46b43 | 14302 | case COMPLEX_TYPE: |
c00996a3 | 14303 | case VECTOR_TYPE: |
8d08fdba | 14304 | case INTEGER_TYPE: |
42976354 | 14305 | case BOOLEAN_TYPE: |
3590f0a6 | 14306 | case ENUMERAL_TYPE: |
5ad5a526 | 14307 | case VOID_TYPE: |
f376e137 MS |
14308 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
14309 | return 1; | |
c8094d83 | 14310 | |
9edc3913 | 14311 | /* We have already checked cv-qualification at the top of the |
514a1f18 | 14312 | function. */ |
8baddbf1 | 14313 | if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) |
ca79f85d JM |
14314 | return 1; |
14315 | ||
8d08fdba MS |
14316 | /* As far as unification is concerned, this wins. Later checks |
14317 | will invalidate it if necessary. */ | |
14318 | return 0; | |
14319 | ||
14320 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 14321 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 14322 | case INTEGER_CST: |
bd6dd845 MS |
14323 | while (TREE_CODE (arg) == NOP_EXPR) |
14324 | arg = TREE_OPERAND (arg, 0); | |
14325 | ||
8d08fdba MS |
14326 | if (TREE_CODE (arg) != INTEGER_CST) |
14327 | return 1; | |
14328 | return !tree_int_cst_equal (parm, arg); | |
14329 | ||
8d08fdba MS |
14330 | case TREE_VEC: |
14331 | { | |
14332 | int i; | |
14333 | if (TREE_CODE (arg) != TREE_VEC) | |
14334 | return 1; | |
14335 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
14336 | return 1; | |
0dc09a61 | 14337 | for (i = 0; i < TREE_VEC_LENGTH (parm); ++i) |
830bfa74 | 14338 | if (unify (tparms, targs, |
8d08fdba | 14339 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 14340 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
14341 | return 1; |
14342 | return 0; | |
14343 | } | |
14344 | ||
8d08fdba | 14345 | case RECORD_TYPE: |
f181d4ae | 14346 | case UNION_TYPE: |
f181d4ae | 14347 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 14348 | return 1; |
c8094d83 | 14349 | |
a7a64a77 MM |
14350 | if (TYPE_PTRMEMFUNC_P (parm)) |
14351 | { | |
14352 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
14353 | return 1; | |
14354 | ||
c8094d83 | 14355 | return unify (tparms, targs, |
a7a64a77 MM |
14356 | TYPE_PTRMEMFUNC_FN_TYPE (parm), |
14357 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
14358 | strict); | |
14359 | } | |
14360 | ||
5db698f6 | 14361 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 14362 | { |
6467930b | 14363 | tree t = NULL_TREE; |
4393e105 | 14364 | |
028d1f20 | 14365 | if (strict_in & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
14366 | { |
14367 | /* First, we try to unify the PARM and ARG directly. */ | |
14368 | t = try_class_unification (tparms, targs, | |
14369 | parm, arg); | |
14370 | ||
14371 | if (!t) | |
14372 | { | |
14373 | /* Fallback to the special case allowed in | |
14374 | [temp.deduct.call]: | |
c8094d83 | 14375 | |
4393e105 MM |
14376 | If P is a class, and P has the form |
14377 | template-id, then A can be a derived class of | |
14378 | the deduced A. Likewise, if P is a pointer to | |
14379 | a class of the form template-id, A can be a | |
14380 | pointer to a derived class pointed to by the | |
14381 | deduced A. */ | |
8d83768f | 14382 | t = get_template_base (tparms, targs, parm, arg); |
4393e105 | 14383 | |
8d83768f | 14384 | if (!t) |
4393e105 MM |
14385 | return 1; |
14386 | } | |
14387 | } | |
c8094d83 MS |
14388 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
14389 | && (CLASSTYPE_TI_TEMPLATE (parm) | |
9fbf56f7 | 14390 | == CLASSTYPE_TI_TEMPLATE (arg))) |
6df47b06 MM |
14391 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
14392 | Then, we should unify `int' and `U'. */ | |
6467930b | 14393 | t = arg; |
4393e105 | 14394 | else |
dc957d14 | 14395 | /* There's no chance of unification succeeding. */ |
5566b478 | 14396 | return 1; |
6467930b | 14397 | |
830bfa74 | 14398 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 14399 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 14400 | } |
9edc3913 | 14401 | else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) |
5566b478 | 14402 | return 1; |
a4443a08 | 14403 | return 0; |
8d08fdba MS |
14404 | |
14405 | case METHOD_TYPE: | |
8d08fdba | 14406 | case FUNCTION_TYPE: |
c166b898 ILT |
14407 | { |
14408 | unsigned int nargs; | |
14409 | tree *args; | |
14410 | tree a; | |
14411 | unsigned int i; | |
830bfa74 | 14412 | |
c166b898 ILT |
14413 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
14414 | return 1; | |
38d18b1a | 14415 | |
c166b898 ILT |
14416 | /* CV qualifications for methods can never be deduced, they must |
14417 | match exactly. We need to check them explicitly here, | |
14418 | because type_unification_real treats them as any other | |
14419 | cv-qualified parameter. */ | |
14420 | if (TREE_CODE (parm) == METHOD_TYPE | |
14421 | && (!check_cv_quals_for_unify | |
14422 | (UNIFY_ALLOW_NONE, | |
14423 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))), | |
14424 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm)))))) | |
14425 | return 1; | |
14426 | ||
14427 | if (unify (tparms, targs, TREE_TYPE (parm), | |
14428 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) | |
14429 | return 1; | |
14430 | ||
14431 | nargs = list_length (TYPE_ARG_TYPES (arg)); | |
14432 | args = XALLOCAVEC (tree, nargs); | |
14433 | for (a = TYPE_ARG_TYPES (arg), i = 0; | |
14434 | a != NULL_TREE && a != void_list_node; | |
14435 | a = TREE_CHAIN (a), ++i) | |
14436 | args[i] = TREE_VALUE (a); | |
14437 | nargs = i; | |
14438 | ||
14439 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), | |
14440 | args, nargs, 1, DEDUCE_EXACT, | |
14441 | LOOKUP_NORMAL); | |
14442 | } | |
a4443a08 MS |
14443 | |
14444 | case OFFSET_TYPE: | |
9804209d | 14445 | /* Unify a pointer to member with a pointer to member function, which |
0cbd7506 | 14446 | deduces the type of the member as a function type. */ |
9804209d | 14447 | if (TYPE_PTRMEMFUNC_P (arg)) |
0cbd7506 MS |
14448 | { |
14449 | tree method_type; | |
14450 | tree fntype; | |
14451 | cp_cv_quals cv_quals; | |
14452 | ||
14453 | /* Check top-level cv qualifiers */ | |
14454 | if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm)) | |
14455 | return 1; | |
14456 | ||
14457 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), | |
14458 | TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE)) | |
14459 | return 1; | |
14460 | ||
14461 | /* Determine the type of the function we are unifying against. */ | |
14462 | method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg)); | |
14463 | fntype = | |
14464 | build_function_type (TREE_TYPE (method_type), | |
14465 | TREE_CHAIN (TYPE_ARG_TYPES (method_type))); | |
14466 | ||
14467 | /* Extract the cv-qualifiers of the member function from the | |
14468 | implicit object parameter and place them on the function | |
14469 | type to be restored later. */ | |
14470 | cv_quals = | |
14471 | cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type)))); | |
14472 | fntype = build_qualified_type (fntype, cv_quals); | |
14473 | return unify (tparms, targs, TREE_TYPE (parm), fntype, strict); | |
14474 | } | |
9804209d | 14475 | |
a4443a08 MS |
14476 | if (TREE_CODE (arg) != OFFSET_TYPE) |
14477 | return 1; | |
830bfa74 | 14478 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 14479 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 14480 | return 1; |
830bfa74 | 14481 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 14482 | strict); |
a4443a08 | 14483 | |
f62dbf03 | 14484 | case CONST_DECL: |
a723baf1 MM |
14485 | if (DECL_TEMPLATE_PARM_P (parm)) |
14486 | return unify (tparms, targs, DECL_INITIAL (parm), arg, strict); | |
c8094d83 | 14487 | if (arg != integral_constant_value (parm)) |
f62dbf03 JM |
14488 | return 1; |
14489 | return 0; | |
14490 | ||
28e8f3a0 | 14491 | case FIELD_DECL: |
027905b4 KL |
14492 | case TEMPLATE_DECL: |
14493 | /* Matched cases are handled by the ARG == PARM test above. */ | |
14494 | return 1; | |
14495 | ||
5d80a306 DG |
14496 | case TYPE_ARGUMENT_PACK: |
14497 | case NONTYPE_ARGUMENT_PACK: | |
14498 | { | |
14499 | tree packed_parms = ARGUMENT_PACK_ARGS (parm); | |
14500 | tree packed_args = ARGUMENT_PACK_ARGS (arg); | |
14501 | int i, len = TREE_VEC_LENGTH (packed_parms); | |
14502 | int argslen = TREE_VEC_LENGTH (packed_args); | |
14503 | int parm_variadic_p = 0; | |
14504 | ||
db60ff18 DG |
14505 | for (i = 0; i < len; ++i) |
14506 | { | |
14507 | if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i))) | |
14508 | { | |
14509 | if (i == len - 1) | |
14510 | /* We can unify against something with a trailing | |
14511 | parameter pack. */ | |
14512 | parm_variadic_p = 1; | |
14513 | else | |
14514 | /* Since there is something following the pack | |
14515 | expansion, we cannot unify this template argument | |
14516 | list. */ | |
14517 | return 0; | |
14518 | } | |
14519 | } | |
14520 | ||
5d80a306 DG |
14521 | |
14522 | /* If we don't have enough arguments to satisfy the parameters | |
14523 | (not counting the pack expression at the end), or we have | |
14524 | too many arguments for a parameter list that doesn't end in | |
14525 | a pack expression, we can't unify. */ | |
14526 | if (argslen < (len - parm_variadic_p) | |
14527 | || (argslen > len && !parm_variadic_p)) | |
14528 | return 1; | |
14529 | ||
14530 | /* Unify all of the parameters that precede the (optional) | |
14531 | pack expression. */ | |
14532 | for (i = 0; i < len - parm_variadic_p; ++i) | |
14533 | { | |
14534 | if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i), | |
14535 | TREE_VEC_ELT (packed_args, i), strict)) | |
14536 | return 1; | |
14537 | } | |
14538 | ||
14539 | if (parm_variadic_p) | |
14540 | return unify_pack_expansion (tparms, targs, | |
14541 | packed_parms, packed_args, | |
14542 | strict, /*call_args_p=*/false, | |
14543 | /*subr=*/false); | |
14544 | return 0; | |
14545 | } | |
14546 | ||
14547 | break; | |
14548 | ||
3ad6a8e1 DG |
14549 | case TYPEOF_TYPE: |
14550 | case DECLTYPE_TYPE: | |
14551 | /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE | |
14552 | nodes. */ | |
14553 | return 0; | |
14554 | ||
1ad8aeeb DG |
14555 | case ERROR_MARK: |
14556 | /* Unification fails if we hit an error node. */ | |
14557 | return 1; | |
14558 | ||
8d08fdba | 14559 | default: |
6615c446 | 14560 | gcc_assert (EXPR_P (parm)); |
c8094d83 | 14561 | |
98ddffc1 | 14562 | /* We must be looking at an expression. This can happen with |
c8094d83 MS |
14563 | something like: |
14564 | ||
98ddffc1 NS |
14565 | template <int I> |
14566 | void foo(S<I>, S<I + 2>); | |
050367a3 | 14567 | |
98ddffc1 | 14568 | This is a "nondeduced context": |
050367a3 | 14569 | |
98ddffc1 | 14570 | [deduct.type] |
c8094d83 | 14571 | |
98ddffc1 | 14572 | The nondeduced contexts are: |
050367a3 | 14573 | |
98ddffc1 NS |
14574 | --A type that is a template-id in which one or more of |
14575 | the template-arguments is an expression that references | |
c8094d83 | 14576 | a template-parameter. |
050367a3 | 14577 | |
98ddffc1 NS |
14578 | In these cases, we assume deduction succeeded, but don't |
14579 | actually infer any unifications. */ | |
74601d7c | 14580 | |
98ddffc1 NS |
14581 | if (!uses_template_parms (parm) |
14582 | && !template_args_equal (parm, arg)) | |
14583 | return 1; | |
14584 | else | |
14585 | return 0; | |
8d08fdba MS |
14586 | } |
14587 | } | |
8d08fdba | 14588 | \f |
4684cd27 MM |
14589 | /* Note that DECL can be defined in this translation unit, if |
14590 | required. */ | |
14591 | ||
14592 | static void | |
14593 | mark_definable (tree decl) | |
14594 | { | |
14595 | tree clone; | |
14596 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
14597 | FOR_EACH_CLONE (clone, decl) | |
14598 | DECL_NOT_REALLY_EXTERN (clone) = 1; | |
14599 | } | |
14600 | ||
03d0f4af | 14601 | /* Called if RESULT is explicitly instantiated, or is a member of an |
4684cd27 | 14602 | explicitly instantiated class. */ |
03d0f4af | 14603 | |
faae18ab | 14604 | void |
3a978d72 | 14605 | mark_decl_instantiated (tree result, int extern_p) |
faae18ab | 14606 | { |
415c974c | 14607 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
3ae18eaf | 14608 | |
1f6f0cb6 MM |
14609 | /* If this entity has already been written out, it's too late to |
14610 | make any modifications. */ | |
14611 | if (TREE_ASM_WRITTEN (result)) | |
14612 | return; | |
14613 | ||
14614 | if (TREE_CODE (result) != FUNCTION_DECL) | |
14615 | /* The TREE_PUBLIC flag for function declarations will have been | |
14616 | set correctly by tsubst. */ | |
14617 | TREE_PUBLIC (result) = 1; | |
14618 | ||
346eeb15 JM |
14619 | /* This might have been set by an earlier implicit instantiation. */ |
14620 | DECL_COMDAT (result) = 0; | |
14621 | ||
4684cd27 MM |
14622 | if (extern_p) |
14623 | DECL_NOT_REALLY_EXTERN (result) = 0; | |
14624 | else | |
faae18ab | 14625 | { |
4684cd27 | 14626 | mark_definable (result); |
1a408d07 JM |
14627 | /* Always make artificials weak. */ |
14628 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
14629 | comdat_linkage (result); | |
a7d87521 JM |
14630 | /* For WIN32 we also want to put explicit instantiations in |
14631 | linkonce sections. */ | |
1a408d07 | 14632 | else if (TREE_PUBLIC (result)) |
b385c841 | 14633 | maybe_make_one_only (result); |
faae18ab | 14634 | } |
c8094d83 | 14635 | |
4684cd27 MM |
14636 | /* If EXTERN_P, then this function will not be emitted -- unless |
14637 | followed by an explicit instantiation, at which point its linkage | |
14638 | will be adjusted. If !EXTERN_P, then this function will be | |
14639 | emitted here. In neither circumstance do we want | |
14640 | import_export_decl to adjust the linkage. */ | |
c8094d83 | 14641 | DECL_INTERFACE_KNOWN (result) = 1; |
faae18ab MS |
14642 | } |
14643 | ||
e5214479 | 14644 | /* Given two function templates PAT1 and PAT2, return: |
6467930b MS |
14645 | |
14646 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
14647 | -1 if PAT2 is more specialized than PAT1. | |
e5214479 JM |
14648 | 0 if neither is more specialized. |
14649 | ||
dda04398 NS |
14650 | LEN indicates the number of parameters we should consider |
14651 | (defaulted parameters should not be considered). | |
14652 | ||
14653 | The 1998 std underspecified function template partial ordering, and | |
14654 | DR214 addresses the issue. We take pairs of arguments, one from | |
c51940a2 | 14655 | each of the templates, and deduce them against each other. One of |
dda04398 NS |
14656 | the templates will be more specialized if all the *other* |
14657 | template's arguments deduce against its arguments and at least one | |
14658 | of its arguments *does* *not* deduce against the other template's | |
14659 | corresponding argument. Deduction is done as for class templates. | |
14660 | The arguments used in deduction have reference and top level cv | |
14661 | qualifiers removed. Iff both arguments were originally reference | |
14662 | types *and* deduction succeeds in both directions, the template | |
14663 | with the more cv-qualified argument wins for that pairing (if | |
14664 | neither is more cv-qualified, they both are equal). Unlike regular | |
14665 | deduction, after all the arguments have been deduced in this way, | |
14666 | we do *not* verify the deduced template argument values can be | |
14667 | substituted into non-deduced contexts, nor do we have to verify | |
14668 | that all template arguments have been deduced. */ | |
c8094d83 | 14669 | |
6467930b | 14670 | int |
dda04398 NS |
14671 | more_specialized_fn (tree pat1, tree pat2, int len) |
14672 | { | |
14673 | tree decl1 = DECL_TEMPLATE_RESULT (pat1); | |
14674 | tree decl2 = DECL_TEMPLATE_RESULT (pat2); | |
14675 | tree targs1 = make_tree_vec (DECL_NTPARMS (pat1)); | |
14676 | tree targs2 = make_tree_vec (DECL_NTPARMS (pat2)); | |
14677 | tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1); | |
14678 | tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2); | |
14679 | tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); | |
14680 | tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2)); | |
14681 | int better1 = 0; | |
14682 | int better2 = 0; | |
3db45ab5 | 14683 | |
48884537 NS |
14684 | /* Remove the this parameter from non-static member functions. If |
14685 | one is a non-static member function and the other is not a static | |
14686 | member function, remove the first parameter from that function | |
14687 | also. This situation occurs for operator functions where we | |
14688 | locate both a member function (with this pointer) and non-member | |
14689 | operator (with explicit first operand). */ | |
dda04398 | 14690 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1)) |
48884537 NS |
14691 | { |
14692 | len--; /* LEN is the number of significant arguments for DECL1 */ | |
14693 | args1 = TREE_CHAIN (args1); | |
14694 | if (!DECL_STATIC_FUNCTION_P (decl2)) | |
14695 | args2 = TREE_CHAIN (args2); | |
14696 | } | |
14697 | else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2)) | |
14698 | { | |
14699 | args2 = TREE_CHAIN (args2); | |
14700 | if (!DECL_STATIC_FUNCTION_P (decl1)) | |
14701 | { | |
14702 | len--; | |
14703 | args1 = TREE_CHAIN (args1); | |
14704 | } | |
14705 | } | |
3db45ab5 | 14706 | |
ee307009 NS |
14707 | /* If only one is a conversion operator, they are unordered. */ |
14708 | if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2)) | |
14709 | return 0; | |
c8094d83 | 14710 | |
dda04398 NS |
14711 | /* Consider the return type for a conversion function */ |
14712 | if (DECL_CONV_FN_P (decl1)) | |
14713 | { | |
dda04398 NS |
14714 | args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1); |
14715 | args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2); | |
14716 | len++; | |
14717 | } | |
c8094d83 | 14718 | |
dda04398 | 14719 | processing_template_decl++; |
c8094d83 | 14720 | |
56b1a554 JJ |
14721 | while (len-- |
14722 | /* Stop when an ellipsis is seen. */ | |
14723 | && args1 != NULL_TREE && args2 != NULL_TREE) | |
dda04398 NS |
14724 | { |
14725 | tree arg1 = TREE_VALUE (args1); | |
14726 | tree arg2 = TREE_VALUE (args2); | |
14727 | int deduce1, deduce2; | |
14728 | int quals1 = -1; | |
14729 | int quals2 = -1; | |
6467930b | 14730 | |
5d80a306 DG |
14731 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
14732 | && TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
14733 | { | |
14734 | /* When both arguments are pack expansions, we need only | |
14735 | unify the patterns themselves. */ | |
14736 | arg1 = PACK_EXPANSION_PATTERN (arg1); | |
14737 | arg2 = PACK_EXPANSION_PATTERN (arg2); | |
14738 | ||
14739 | /* This is the last comparison we need to do. */ | |
14740 | len = 0; | |
14741 | } | |
14742 | ||
dda04398 NS |
14743 | if (TREE_CODE (arg1) == REFERENCE_TYPE) |
14744 | { | |
14745 | arg1 = TREE_TYPE (arg1); | |
14746 | quals1 = cp_type_quals (arg1); | |
14747 | } | |
c8094d83 | 14748 | |
dda04398 NS |
14749 | if (TREE_CODE (arg2) == REFERENCE_TYPE) |
14750 | { | |
14751 | arg2 = TREE_TYPE (arg2); | |
14752 | quals2 = cp_type_quals (arg2); | |
14753 | } | |
6467930b | 14754 | |
dda04398 NS |
14755 | if ((quals1 < 0) != (quals2 < 0)) |
14756 | { | |
14757 | /* Only of the args is a reference, see if we should apply | |
14758 | array/function pointer decay to it. This is not part of | |
14759 | DR214, but is, IMHO, consistent with the deduction rules | |
14760 | for the function call itself, and with our earlier | |
14761 | implementation of the underspecified partial ordering | |
14762 | rules. (nathan). */ | |
14763 | if (quals1 >= 0) | |
14764 | { | |
14765 | switch (TREE_CODE (arg1)) | |
14766 | { | |
14767 | case ARRAY_TYPE: | |
14768 | arg1 = TREE_TYPE (arg1); | |
14769 | /* FALLTHROUGH. */ | |
14770 | case FUNCTION_TYPE: | |
14771 | arg1 = build_pointer_type (arg1); | |
14772 | break; | |
c8094d83 | 14773 | |
dda04398 NS |
14774 | default: |
14775 | break; | |
14776 | } | |
14777 | } | |
14778 | else | |
14779 | { | |
14780 | switch (TREE_CODE (arg2)) | |
14781 | { | |
14782 | case ARRAY_TYPE: | |
14783 | arg2 = TREE_TYPE (arg2); | |
14784 | /* FALLTHROUGH. */ | |
14785 | case FUNCTION_TYPE: | |
14786 | arg2 = build_pointer_type (arg2); | |
14787 | break; | |
c8094d83 | 14788 | |
dda04398 NS |
14789 | default: |
14790 | break; | |
14791 | } | |
14792 | } | |
14793 | } | |
c8094d83 | 14794 | |
dda04398 NS |
14795 | arg1 = TYPE_MAIN_VARIANT (arg1); |
14796 | arg2 = TYPE_MAIN_VARIANT (arg2); | |
c8094d83 | 14797 | |
5d80a306 DG |
14798 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION) |
14799 | { | |
b1d7b1c0 | 14800 | int i, len2 = list_length (args2); |
5d80a306 DG |
14801 | tree parmvec = make_tree_vec (1); |
14802 | tree argvec = make_tree_vec (len2); | |
14803 | tree ta = args2; | |
14804 | ||
14805 | /* Setup the parameter vector, which contains only ARG1. */ | |
14806 | TREE_VEC_ELT (parmvec, 0) = arg1; | |
14807 | ||
14808 | /* Setup the argument vector, which contains the remaining | |
14809 | arguments. */ | |
14810 | for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta)) | |
14811 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
14812 | ||
14813 | deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec, | |
14814 | argvec, UNIFY_ALLOW_NONE, | |
14815 | /*call_args_p=*/false, | |
14816 | /*subr=*/0); | |
14817 | ||
14818 | /* We cannot deduce in the other direction, because ARG1 is | |
14819 | a pack expansion but ARG2 is not. */ | |
14820 | deduce2 = 0; | |
14821 | } | |
14822 | else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
14823 | { | |
b1d7b1c0 | 14824 | int i, len1 = list_length (args1); |
5d80a306 DG |
14825 | tree parmvec = make_tree_vec (1); |
14826 | tree argvec = make_tree_vec (len1); | |
14827 | tree ta = args1; | |
14828 | ||
14829 | /* Setup the parameter vector, which contains only ARG1. */ | |
14830 | TREE_VEC_ELT (parmvec, 0) = arg2; | |
14831 | ||
14832 | /* Setup the argument vector, which contains the remaining | |
14833 | arguments. */ | |
14834 | for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta)) | |
14835 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); | |
14836 | ||
14837 | deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec, | |
14838 | argvec, UNIFY_ALLOW_NONE, | |
14839 | /*call_args_p=*/false, | |
14840 | /*subr=*/0); | |
14841 | ||
14842 | /* We cannot deduce in the other direction, because ARG2 is | |
14843 | a pack expansion but ARG1 is not.*/ | |
14844 | deduce1 = 0; | |
14845 | } | |
14846 | ||
14847 | else | |
14848 | { | |
14849 | /* The normal case, where neither argument is a pack | |
14850 | expansion. */ | |
14851 | deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE); | |
14852 | deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE); | |
14853 | } | |
dda04398 NS |
14854 | |
14855 | if (!deduce1) | |
14856 | better2 = -1; | |
14857 | if (!deduce2) | |
14858 | better1 = -1; | |
14859 | if (better1 < 0 && better2 < 0) | |
14860 | /* We've failed to deduce something in either direction. | |
14861 | These must be unordered. */ | |
14862 | break; | |
c8094d83 | 14863 | |
dda04398 NS |
14864 | if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0) |
14865 | { | |
14866 | /* Deduces in both directions, see if quals can | |
14867 | disambiguate. Pretend the worse one failed to deduce. */ | |
14868 | if ((quals1 & quals2) == quals2) | |
14869 | deduce1 = 0; | |
14870 | if ((quals1 & quals2) == quals1) | |
14871 | deduce2 = 0; | |
14872 | } | |
14873 | if (deduce1 && !deduce2 && !better2) | |
14874 | better2 = 1; | |
14875 | if (deduce2 && !deduce1 && !better1) | |
14876 | better1 = 1; | |
c8094d83 | 14877 | |
5d80a306 DG |
14878 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
14879 | || TREE_CODE (arg2) == TYPE_PACK_EXPANSION) | |
14880 | /* We have already processed all of the arguments in our | |
14881 | handing of the pack expansion type. */ | |
14882 | len = 0; | |
14883 | ||
dda04398 NS |
14884 | args1 = TREE_CHAIN (args1); |
14885 | args2 = TREE_CHAIN (args2); | |
14886 | } | |
14887 | ||
14888 | processing_template_decl--; | |
14889 | ||
5d80a306 DG |
14890 | /* All things being equal, if the next argument is a pack expansion |
14891 | for one function but not for the other, prefer the | |
14892 | non-variadic function. */ | |
14893 | if ((better1 > 0) - (better2 > 0) == 0 | |
14894 | && args1 && TREE_VALUE (args1) | |
14895 | && args2 && TREE_VALUE (args2)) | |
14896 | { | |
14897 | if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION) | |
14898 | return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1; | |
14899 | else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION) | |
14900 | return 1; | |
14901 | } | |
14902 | ||
dda04398 | 14903 | return (better1 > 0) - (better2 > 0); |
73aad9b9 | 14904 | } |
6467930b | 14905 | |
916b63c3 | 14906 | /* Determine which of two partial specializations is more specialized. |
6467930b | 14907 | |
916b63c3 MM |
14908 | PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding |
14909 | to the first partial specialization. The TREE_VALUE is the | |
14910 | innermost set of template parameters for the partial | |
14911 | specialization. PAT2 is similar, but for the second template. | |
14912 | ||
14913 | Return 1 if the first partial specialization is more specialized; | |
14914 | -1 if the second is more specialized; 0 if neither is more | |
14915 | specialized. | |
55ece1b3 | 14916 | |
916b63c3 | 14917 | See [temp.class.order] for information about determining which of |
3db45ab5 | 14918 | two templates is more specialized. */ |
c8094d83 | 14919 | |
b5791fdc | 14920 | static int |
916b63c3 | 14921 | more_specialized_class (tree pat1, tree pat2) |
73aad9b9 JM |
14922 | { |
14923 | tree targs; | |
916b63c3 | 14924 | tree tmpl1, tmpl2; |
73aad9b9 | 14925 | int winner = 0; |
5d80a306 | 14926 | bool any_deductions = false; |
73aad9b9 | 14927 | |
916b63c3 MM |
14928 | tmpl1 = TREE_TYPE (pat1); |
14929 | tmpl2 = TREE_TYPE (pat2); | |
14930 | ||
c8094d83 | 14931 | /* Just like what happens for functions, if we are ordering between |
baa49730 GB |
14932 | different class template specializations, we may encounter dependent |
14933 | types in the arguments, and we need our dependency check functions | |
14934 | to behave correctly. */ | |
14935 | ++processing_template_decl; | |
3db45ab5 | 14936 | targs = get_class_bindings (TREE_VALUE (pat1), |
916b63c3 MM |
14937 | CLASSTYPE_TI_ARGS (tmpl1), |
14938 | CLASSTYPE_TI_ARGS (tmpl2)); | |
73aad9b9 | 14939 | if (targs) |
5d80a306 DG |
14940 | { |
14941 | --winner; | |
14942 | any_deductions = true; | |
14943 | } | |
73aad9b9 | 14944 | |
3db45ab5 | 14945 | targs = get_class_bindings (TREE_VALUE (pat2), |
916b63c3 MM |
14946 | CLASSTYPE_TI_ARGS (tmpl2), |
14947 | CLASSTYPE_TI_ARGS (tmpl1)); | |
73aad9b9 | 14948 | if (targs) |
5d80a306 DG |
14949 | { |
14950 | ++winner; | |
14951 | any_deductions = true; | |
14952 | } | |
baa49730 | 14953 | --processing_template_decl; |
6467930b | 14954 | |
5d80a306 DG |
14955 | /* In the case of a tie where at least one of the class templates |
14956 | has a parameter pack at the end, the template with the most | |
14957 | non-packed parameters wins. */ | |
14958 | if (winner == 0 | |
14959 | && any_deductions | |
14960 | && (template_args_variadic_p (TREE_PURPOSE (pat1)) | |
14961 | || template_args_variadic_p (TREE_PURPOSE (pat2)))) | |
14962 | { | |
14963 | tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1)); | |
14964 | tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2)); | |
14965 | int len1 = TREE_VEC_LENGTH (args1); | |
14966 | int len2 = TREE_VEC_LENGTH (args2); | |
14967 | ||
14968 | /* We don't count the pack expansion at the end. */ | |
14969 | if (template_args_variadic_p (TREE_PURPOSE (pat1))) | |
14970 | --len1; | |
14971 | if (template_args_variadic_p (TREE_PURPOSE (pat2))) | |
14972 | --len2; | |
14973 | ||
14974 | if (len1 > len2) | |
14975 | return 1; | |
14976 | else if (len1 < len2) | |
14977 | return -1; | |
14978 | } | |
14979 | ||
6467930b MS |
14980 | return winner; |
14981 | } | |
73aad9b9 JM |
14982 | |
14983 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 14984 | DECL from the function template FN, with the explicit template |
a34d3336 | 14985 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must |
4393e105 | 14986 | also match. Return NULL_TREE if no satisfactory arguments could be |
a34d3336 | 14987 | found. */ |
c8094d83 | 14988 | |
76b9a14d | 14989 | static tree |
a34d3336 | 14990 | get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype) |
73aad9b9 | 14991 | { |
98c1c668 | 14992 | int ntparms = DECL_NTPARMS (fn); |
f31c0a32 | 14993 | tree targs = make_tree_vec (ntparms); |
4393e105 | 14994 | tree decl_type; |
03017874 | 14995 | tree decl_arg_types; |
c166b898 ILT |
14996 | tree *args; |
14997 | unsigned int nargs, ix; | |
14998 | tree arg; | |
98c1c668 | 14999 | |
4393e105 MM |
15000 | /* Substitute the explicit template arguments into the type of DECL. |
15001 | The call to fn_type_unification will handle substitution into the | |
15002 | FN. */ | |
15003 | decl_type = TREE_TYPE (decl); | |
15004 | if (explicit_args && uses_template_parms (decl_type)) | |
15005 | { | |
15006 | tree tmpl; | |
15007 | tree converted_args; | |
15008 | ||
15009 | if (DECL_TEMPLATE_INFO (decl)) | |
15010 | tmpl = DECL_TI_TEMPLATE (decl); | |
15011 | else | |
0e339752 | 15012 | /* We can get here for some invalid specializations. */ |
4393e105 MM |
15013 | return NULL_TREE; |
15014 | ||
15015 | converted_args | |
e7e93965 MM |
15016 | = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), |
15017 | explicit_args, NULL_TREE, | |
3db45ab5 | 15018 | tf_none, |
e7e93965 MM |
15019 | /*require_all_args=*/false, |
15020 | /*use_default_args=*/false); | |
4393e105 MM |
15021 | if (converted_args == error_mark_node) |
15022 | return NULL_TREE; | |
c8094d83 MS |
15023 | |
15024 | decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); | |
4393e105 MM |
15025 | if (decl_type == error_mark_node) |
15026 | return NULL_TREE; | |
15027 | } | |
15028 | ||
e5214479 | 15029 | /* Never do unification on the 'this' parameter. */ |
ba139ba8 MM |
15030 | decl_arg_types = skip_artificial_parms_for (decl, |
15031 | TYPE_ARG_TYPES (decl_type)); | |
d7684f2d | 15032 | |
c166b898 ILT |
15033 | nargs = list_length (decl_arg_types); |
15034 | args = XALLOCAVEC (tree, nargs); | |
15035 | for (arg = decl_arg_types, ix = 0; | |
15036 | arg != NULL_TREE && arg != void_list_node; | |
15037 | arg = TREE_CHAIN (arg), ++ix) | |
15038 | args[ix] = TREE_VALUE (arg); | |
15039 | ||
c8094d83 | 15040 | if (fn_type_unification (fn, explicit_args, targs, |
c166b898 | 15041 | args, ix, |
8d3631f8 | 15042 | (check_rettype || DECL_CONV_FN_P (fn) |
0cbd7506 | 15043 | ? TREE_TYPE (decl_type) : NULL_TREE), |
30f86ec3 | 15044 | DEDUCE_EXACT, LOOKUP_NORMAL)) |
76b9a14d JM |
15045 | return NULL_TREE; |
15046 | ||
76b9a14d JM |
15047 | return targs; |
15048 | } | |
15049 | ||
36a117a5 MM |
15050 | /* Return the innermost template arguments that, when applied to a |
15051 | template specialization whose innermost template parameters are | |
d0940d56 | 15052 | TPARMS, and whose specialization arguments are SPEC_ARGS, yield the |
c8094d83 | 15053 | ARGS. |
36a117a5 MM |
15054 | |
15055 | For example, suppose we have: | |
15056 | ||
15057 | template <class T, class U> struct S {}; | |
15058 | template <class T> struct S<T*, int> {}; | |
15059 | ||
15060 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
916b63c3 | 15061 | {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*, |
36a117a5 MM |
15062 | int}. The resulting vector will be {double}, indicating that `T' |
15063 | is bound to `double'. */ | |
15064 | ||
bd6dd845 | 15065 | static tree |
916b63c3 | 15066 | get_class_bindings (tree tparms, tree spec_args, tree args) |
73aad9b9 | 15067 | { |
3b3ba9f0 | 15068 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
916b63c3 MM |
15069 | tree deduced_args; |
15070 | tree innermost_deduced_args; | |
73aad9b9 | 15071 | |
916b63c3 MM |
15072 | innermost_deduced_args = make_tree_vec (ntparms); |
15073 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
15074 | { | |
15075 | deduced_args = copy_node (args); | |
15076 | SET_TMPL_ARGS_LEVEL (deduced_args, | |
15077 | TMPL_ARGS_DEPTH (deduced_args), | |
15078 | innermost_deduced_args); | |
15079 | } | |
15080 | else | |
3db45ab5 | 15081 | deduced_args = innermost_deduced_args; |
916b63c3 | 15082 | |
3db45ab5 MS |
15083 | if (unify (tparms, deduced_args, |
15084 | INNERMOST_TEMPLATE_ARGS (spec_args), | |
15085 | INNERMOST_TEMPLATE_ARGS (args), | |
0cbd7506 | 15086 | UNIFY_ALLOW_NONE)) |
fcfb9f96 | 15087 | return NULL_TREE; |
73aad9b9 JM |
15088 | |
15089 | for (i = 0; i < ntparms; ++i) | |
916b63c3 | 15090 | if (! TREE_VEC_ELT (innermost_deduced_args, i)) |
73aad9b9 JM |
15091 | return NULL_TREE; |
15092 | ||
916b63c3 MM |
15093 | /* Verify that nondeduced template arguments agree with the type |
15094 | obtained from argument deduction. | |
3db45ab5 | 15095 | |
916b63c3 MM |
15096 | For example: |
15097 | ||
15098 | struct A { typedef int X; }; | |
15099 | template <class T, class U> struct C {}; | |
15100 | template <class T> struct C<T, typename T::X> {}; | |
15101 | ||
15102 | Then with the instantiation `C<A, int>', we can deduce that | |
15103 | `T' is `A' but unify () does not check whether `typename T::X' | |
15104 | is `int'. */ | |
15105 | spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE); | |
15106 | if (spec_args == error_mark_node | |
15107 | /* We only need to check the innermost arguments; the other | |
15108 | arguments will always agree. */ | |
15109 | || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args), | |
15110 | INNERMOST_TEMPLATE_ARGS (args))) | |
74601d7c KL |
15111 | return NULL_TREE; |
15112 | ||
4cf36211 DG |
15113 | /* Now that we have bindings for all of the template arguments, |
15114 | ensure that the arguments deduced for the template template | |
15115 | parameters have compatible template parameter lists. See the use | |
15116 | of template_template_parm_bindings_ok_p in fn_type_unification | |
15117 | for more information. */ | |
15118 | if (!template_template_parm_bindings_ok_p (tparms, deduced_args)) | |
15119 | return NULL_TREE; | |
15120 | ||
916b63c3 | 15121 | return deduced_args; |
73aad9b9 JM |
15122 | } |
15123 | ||
7ca383e6 MM |
15124 | /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL. |
15125 | Return the TREE_LIST node with the most specialized template, if | |
15126 | any. If there is no most specialized template, the error_mark_node | |
15127 | is returned. | |
15128 | ||
15129 | Note that this function does not look at, or modify, the | |
15130 | TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node | |
15131 | returned is one of the elements of INSTANTIATIONS, callers may | |
15132 | store information in the TREE_PURPOSE or TREE_TYPE of the nodes, | |
15133 | and retrieve it from the value returned. */ | |
73aad9b9 JM |
15134 | |
15135 | tree | |
7ca383e6 | 15136 | most_specialized_instantiation (tree templates) |
73aad9b9 | 15137 | { |
104bf76a | 15138 | tree fn, champ; |
73aad9b9 | 15139 | |
dda04398 | 15140 | ++processing_template_decl; |
c8094d83 | 15141 | |
7ca383e6 MM |
15142 | champ = templates; |
15143 | for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 15144 | { |
dda04398 | 15145 | int fate = 0; |
c8094d83 | 15146 | |
a34d3336 NS |
15147 | if (get_bindings (TREE_VALUE (champ), |
15148 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
15149 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
15150 | fate--; |
15151 | ||
a34d3336 NS |
15152 | if (get_bindings (TREE_VALUE (fn), |
15153 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
15154 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 | 15155 | fate++; |
c8094d83 | 15156 | |
8ca4bf25 MM |
15157 | if (fate == -1) |
15158 | champ = fn; | |
15159 | else if (!fate) | |
73aad9b9 | 15160 | { |
8ca4bf25 MM |
15161 | /* Equally specialized, move to next function. If there |
15162 | is no next function, nothing's most specialized. */ | |
15163 | fn = TREE_CHAIN (fn); | |
7ca383e6 | 15164 | champ = fn; |
8ca4bf25 MM |
15165 | if (!fn) |
15166 | break; | |
73aad9b9 JM |
15167 | } |
15168 | } | |
c8094d83 | 15169 | |
dda04398 NS |
15170 | if (champ) |
15171 | /* Now verify that champ is better than everything earlier in the | |
15172 | instantiation list. */ | |
7ca383e6 | 15173 | for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) |
a34d3336 NS |
15174 | if (get_bindings (TREE_VALUE (champ), |
15175 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
15176 | NULL_TREE, /*check_ret=*/false) | |
15177 | || !get_bindings (TREE_VALUE (fn), | |
15178 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
15179 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
15180 | { |
15181 | champ = NULL_TREE; | |
15182 | break; | |
15183 | } | |
c8094d83 | 15184 | |
dda04398 | 15185 | processing_template_decl--; |
c8094d83 | 15186 | |
dda04398 NS |
15187 | if (!champ) |
15188 | return error_mark_node; | |
73aad9b9 | 15189 | |
7ca383e6 | 15190 | return champ; |
73aad9b9 JM |
15191 | } |
15192 | ||
36a117a5 | 15193 | /* If DECL is a specialization of some template, return the most |
f9a7ae04 MM |
15194 | general such template. Otherwise, returns NULL_TREE. |
15195 | ||
15196 | For example, given: | |
36a117a5 MM |
15197 | |
15198 | template <class T> struct S { template <class U> void f(U); }; | |
15199 | ||
15200 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
15201 | the full template. This function will not trace past partial | |
15202 | specializations, however. For example, given in addition: | |
15203 | ||
15204 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
15205 | ||
15206 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
15207 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 15208 | |
612c671a | 15209 | tree |
3a978d72 | 15210 | most_general_template (tree decl) |
73aad9b9 | 15211 | { |
f9a7ae04 MM |
15212 | /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is |
15213 | an immediate specialization. */ | |
15214 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
15215 | { | |
15216 | if (DECL_TEMPLATE_INFO (decl)) { | |
15217 | decl = DECL_TI_TEMPLATE (decl); | |
15218 | ||
15219 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a | |
15220 | template friend. */ | |
15221 | if (TREE_CODE (decl) != TEMPLATE_DECL) | |
15222 | return NULL_TREE; | |
15223 | } else | |
15224 | return NULL_TREE; | |
15225 | } | |
15226 | ||
15227 | /* Look for more and more general templates. */ | |
15228 | while (DECL_TEMPLATE_INFO (decl)) | |
15229 | { | |
10b1d5e7 MM |
15230 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases. |
15231 | (See cp-tree.h for details.) */ | |
f9a7ae04 MM |
15232 | if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
15233 | break; | |
15234 | ||
6e049fcd KL |
15235 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
15236 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) | |
15237 | break; | |
15238 | ||
f9a7ae04 MM |
15239 | /* Stop if we run into an explicitly specialized class template. */ |
15240 | if (!DECL_NAMESPACE_SCOPE_P (decl) | |
15241 | && DECL_CONTEXT (decl) | |
15242 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) | |
15243 | break; | |
15244 | ||
15245 | decl = DECL_TI_TEMPLATE (decl); | |
15246 | } | |
36a117a5 MM |
15247 | |
15248 | return decl; | |
15249 | } | |
15250 | ||
916b63c3 MM |
15251 | /* Return the most specialized of the class template partial |
15252 | specializations of TMPL which can produce TYPE, a specialization of | |
15253 | TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is | |
15254 | a _TYPE node corresponding to the partial specialization, while the | |
15255 | TREE_PURPOSE is the set of template arguments that must be | |
15256 | substituted into the TREE_TYPE in order to generate TYPE. | |
15257 | ||
15258 | If the choice of partial specialization is ambiguous, a diagnostic | |
15259 | is issued, and the error_mark_node is returned. If there are no | |
15260 | partial specializations of TMPL matching TYPE, then NULL_TREE is | |
15261 | returned. */ | |
36a117a5 | 15262 | |
e9659ab0 | 15263 | static tree |
916b63c3 | 15264 | most_specialized_class (tree type, tree tmpl) |
36a117a5 MM |
15265 | { |
15266 | tree list = NULL_TREE; | |
15267 | tree t; | |
15268 | tree champ; | |
73aad9b9 | 15269 | int fate; |
916b63c3 MM |
15270 | bool ambiguous_p; |
15271 | tree args; | |
dc28490d | 15272 | tree outer_args = NULL_TREE; |
73aad9b9 | 15273 | |
36a117a5 | 15274 | tmpl = most_general_template (tmpl); |
916b63c3 | 15275 | args = CLASSTYPE_TI_ARGS (type); |
dc28490d JM |
15276 | |
15277 | /* For determining which partial specialization to use, only the | |
15278 | innermost args are interesting. */ | |
15279 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
15280 | { | |
15281 | outer_args = strip_innermost_template_args (args, 1); | |
15282 | args = INNERMOST_TEMPLATE_ARGS (args); | |
15283 | } | |
15284 | ||
36a117a5 | 15285 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) |
73aad9b9 | 15286 | { |
916b63c3 MM |
15287 | tree partial_spec_args; |
15288 | tree spec_args; | |
dc28490d | 15289 | tree parms = TREE_VALUE (t); |
916b63c3 MM |
15290 | |
15291 | partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t)); | |
dc28490d JM |
15292 | if (outer_args) |
15293 | { | |
15294 | int i; | |
15295 | ||
fd452cef JM |
15296 | ++processing_template_decl; |
15297 | ||
dc28490d JM |
15298 | /* Discard the outer levels of args, and then substitute in the |
15299 | template args from the enclosing class. */ | |
15300 | partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args); | |
15301 | partial_spec_args = tsubst_template_args | |
15302 | (partial_spec_args, outer_args, tf_none, NULL_TREE); | |
15303 | ||
15304 | /* PARMS already refers to just the innermost parms, but the | |
15305 | template parms in partial_spec_args had their levels lowered | |
15306 | by tsubst, so we need to do the same for the parm list. We | |
15307 | can't just tsubst the TREE_VEC itself, as tsubst wants to | |
15308 | treat a TREE_VEC as an argument vector. */ | |
15309 | parms = copy_node (parms); | |
15310 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) | |
15311 | TREE_VEC_ELT (parms, i) = | |
15312 | tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE); | |
fd452cef JM |
15313 | |
15314 | --processing_template_decl; | |
dc28490d JM |
15315 | } |
15316 | spec_args = get_class_bindings (parms, | |
3db45ab5 | 15317 | partial_spec_args, |
916b63c3 | 15318 | args); |
36a117a5 | 15319 | if (spec_args) |
73aad9b9 | 15320 | { |
dc28490d JM |
15321 | if (outer_args) |
15322 | spec_args = add_to_template_args (outer_args, spec_args); | |
916b63c3 | 15323 | list = tree_cons (spec_args, TREE_VALUE (t), list); |
73aad9b9 JM |
15324 | TREE_TYPE (list) = TREE_TYPE (t); |
15325 | } | |
15326 | } | |
15327 | ||
15328 | if (! list) | |
15329 | return NULL_TREE; | |
15330 | ||
916b63c3 | 15331 | ambiguous_p = false; |
73aad9b9 JM |
15332 | t = list; |
15333 | champ = t; | |
15334 | t = TREE_CHAIN (t); | |
15335 | for (; t; t = TREE_CHAIN (t)) | |
15336 | { | |
916b63c3 | 15337 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
15338 | if (fate == 1) |
15339 | ; | |
15340 | else | |
15341 | { | |
15342 | if (fate == 0) | |
15343 | { | |
15344 | t = TREE_CHAIN (t); | |
15345 | if (! t) | |
916b63c3 MM |
15346 | { |
15347 | ambiguous_p = true; | |
15348 | break; | |
15349 | } | |
73aad9b9 JM |
15350 | } |
15351 | champ = t; | |
15352 | } | |
15353 | } | |
15354 | ||
916b63c3 MM |
15355 | if (!ambiguous_p) |
15356 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
15357 | { | |
15358 | fate = more_specialized_class (champ, t); | |
15359 | if (fate != 1) | |
15360 | { | |
15361 | ambiguous_p = true; | |
15362 | break; | |
15363 | } | |
15364 | } | |
15365 | ||
15366 | if (ambiguous_p) | |
73aad9b9 | 15367 | { |
916b63c3 MM |
15368 | const char *str = "candidates are:"; |
15369 | error ("ambiguous class template instantiation for %q#T", type); | |
15370 | for (t = list; t; t = TREE_CHAIN (t)) | |
15371 | { | |
15372 | error ("%s %+#T", str, TREE_TYPE (t)); | |
15373 | str = " "; | |
15374 | } | |
15375 | return error_mark_node; | |
73aad9b9 JM |
15376 | } |
15377 | ||
15378 | return champ; | |
15379 | } | |
15380 | ||
eb8845be | 15381 | /* Explicitly instantiate DECL. */ |
e92cc029 | 15382 | |
8d08fdba | 15383 | void |
eb8845be | 15384 | do_decl_instantiation (tree decl, tree storage) |
8d08fdba | 15385 | { |
8d08fdba | 15386 | tree result = NULL_TREE; |
faae18ab | 15387 | int extern_p = 0; |
e8abc66f | 15388 | |
ad47b891 | 15389 | if (!decl || decl == error_mark_node) |
dc957d14 | 15390 | /* An error occurred, for which grokdeclarator has already issued |
3fa56191 MM |
15391 | an appropriate message. */ |
15392 | return; | |
15393 | else if (! DECL_LANG_SPECIFIC (decl)) | |
ec255269 | 15394 | { |
0f51ccfc | 15395 | error ("explicit instantiation of non-template %q#D", decl); |
ec255269 MS |
15396 | return; |
15397 | } | |
03d0f4af | 15398 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 15399 | { |
03d0f4af MM |
15400 | /* There is an asymmetry here in the way VAR_DECLs and |
15401 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
15402 | the latter, the DECL we get back will be marked as a | |
15403 | template instantiation, and the appropriate | |
15404 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
15405 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
15406 | should handle VAR_DECLs as it currently handles | |
15407 | FUNCTION_DECLs. */ | |
8259e4f5 PC |
15408 | if (!DECL_CLASS_SCOPE_P (decl)) |
15409 | { | |
15410 | error ("%qD is not a static data member of a class template", decl); | |
15411 | return; | |
15412 | } | |
86ac0575 | 15413 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); |
e0860732 | 15414 | if (!result || TREE_CODE (result) != VAR_DECL) |
03d0f4af | 15415 | { |
0f51ccfc | 15416 | error ("no matching template for %qD found", decl); |
03d0f4af MM |
15417 | return; |
15418 | } | |
8ea6dfae SM |
15419 | if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl))) |
15420 | { | |
15421 | error ("type %qT for explicit instantiation %qD does not match " | |
15422 | "declared type %qT", TREE_TYPE (result), decl, | |
15423 | TREE_TYPE (decl)); | |
15424 | return; | |
15425 | } | |
6633d636 MS |
15426 | } |
15427 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
15428 | { | |
0f51ccfc | 15429 | error ("explicit instantiation of %q#D", decl); |
6633d636 MS |
15430 | return; |
15431 | } | |
03d0f4af MM |
15432 | else |
15433 | result = decl; | |
672476cb | 15434 | |
03d0f4af | 15435 | /* Check for various error cases. Note that if the explicit |
0e339752 | 15436 | instantiation is valid the RESULT will currently be marked as an |
03d0f4af MM |
15437 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set |
15438 | until we get here. */ | |
15439 | ||
15440 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 15441 | { |
07782718 KL |
15442 | /* DR 259 [temp.spec]. |
15443 | ||
15444 | Both an explicit instantiation and a declaration of an explicit | |
15445 | specialization shall not appear in a program unless the explicit | |
15446 | instantiation follows a declaration of the explicit specialization. | |
03d0f4af | 15447 | |
07782718 KL |
15448 | For a given set of template parameters, if an explicit |
15449 | instantiation of a template appears after a declaration of an | |
15450 | explicit specialization for that template, the explicit | |
15451 | instantiation has no effect. */ | |
672476cb MM |
15452 | return; |
15453 | } | |
03d0f4af MM |
15454 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
15455 | { | |
15456 | /* [temp.spec] | |
98c1c668 | 15457 | |
03d0f4af | 15458 | No program shall explicitly instantiate any template more |
c8094d83 | 15459 | than once. |
03d0f4af | 15460 | |
4684cd27 MM |
15461 | We check DECL_NOT_REALLY_EXTERN so as not to complain when |
15462 | the first instantiation was `extern' and the second is not, | |
15463 | and EXTERN_P for the opposite case. */ | |
15464 | if (DECL_NOT_REALLY_EXTERN (result) && !extern_p) | |
cbe5f3b3 | 15465 | permerror (input_location, "duplicate explicit instantiation of %q#D", result); |
4684cd27 MM |
15466 | /* If an "extern" explicit instantiation follows an ordinary |
15467 | explicit instantiation, the template is instantiated. */ | |
15468 | if (extern_p) | |
03d0f4af MM |
15469 | return; |
15470 | } | |
15471 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 15472 | { |
0f51ccfc | 15473 | error ("no matching template for %qD found", result); |
faae18ab MS |
15474 | return; |
15475 | } | |
03d0f4af | 15476 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 15477 | { |
cbe5f3b3 | 15478 | permerror (input_location, "explicit instantiation of non-template %q#D", result); |
6633d636 MS |
15479 | return; |
15480 | } | |
15481 | ||
f0e01782 | 15482 | if (storage == NULL_TREE) |
00595019 | 15483 | ; |
faae18ab | 15484 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af | 15485 | { |
fcf73884 | 15486 | if (!in_system_header && (cxx_dialect == cxx98)) |
509c9d60 | 15487 | pedwarn (input_location, OPT_pedantic, |
fcf73884 | 15488 | "ISO C++ 1998 forbids the use of %<extern%> on explicit " |
0cbd7506 | 15489 | "instantiations"); |
03d0f4af MM |
15490 | extern_p = 1; |
15491 | } | |
f0e01782 | 15492 | else |
0f51ccfc | 15493 | error ("storage class %qD applied to template instantiation", storage); |
3db45ab5 | 15494 | |
9c12301f | 15495 | check_explicit_instantiation_namespace (result); |
5566b478 | 15496 | mark_decl_instantiated (result, extern_p); |
c91a56d2 | 15497 | if (! extern_p) |
3db45ab5 | 15498 | instantiate_decl (result, /*defer_ok=*/1, |
eba839f9 | 15499 | /*expl_inst_class_mem_p=*/false); |
7177d104 MS |
15500 | } |
15501 | ||
b5791fdc | 15502 | static void |
3a978d72 | 15503 | mark_class_instantiated (tree t, int extern_p) |
faae18ab MS |
15504 | { |
15505 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
15506 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
15507 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
15508 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; |
15509 | if (! extern_p) | |
15510 | { | |
15511 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
15512 | rest_of_type_compilation (t, 1); | |
15513 | } | |
c8094d83 | 15514 | } |
e8abc66f | 15515 | |
5e0c54e5 | 15516 | /* Called from do_type_instantiation through binding_table_foreach to |
9bcb9aae | 15517 | do recursive instantiation for the type bound in ENTRY. */ |
5e0c54e5 GDR |
15518 | static void |
15519 | bt_instantiate_type_proc (binding_entry entry, void *data) | |
15520 | { | |
15521 | tree storage = *(tree *) data; | |
15522 | ||
9e1e64ec | 15523 | if (MAYBE_CLASS_TYPE_P (entry->type) |
5e0c54e5 GDR |
15524 | && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type))) |
15525 | do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0); | |
15526 | } | |
15527 | ||
415c974c MA |
15528 | /* Called from do_type_instantiation to instantiate a member |
15529 | (a member function or a static member variable) of an | |
03fd3f84 | 15530 | explicitly instantiated class template. */ |
415c974c MA |
15531 | static void |
15532 | instantiate_class_member (tree decl, int extern_p) | |
15533 | { | |
15534 | mark_decl_instantiated (decl, extern_p); | |
415c974c | 15535 | if (! extern_p) |
3db45ab5 | 15536 | instantiate_decl (decl, /*defer_ok=*/1, |
eba839f9 | 15537 | /*expl_inst_class_mem_p=*/true); |
415c974c MA |
15538 | } |
15539 | ||
a1bcc528 JM |
15540 | /* Perform an explicit instantiation of template class T. STORAGE, if |
15541 | non-null, is the RID for extern, inline or static. COMPLAIN is | |
838dfd8a | 15542 | nonzero if this is called from the parser, zero if called recursively, |
a1bcc528 | 15543 | since the standard is unclear (as detailed below). */ |
c8094d83 | 15544 | |
7177d104 | 15545 | void |
3a978d72 | 15546 | do_type_instantiation (tree t, tree storage, tsubst_flags_t complain) |
7177d104 | 15547 | { |
e8abc66f MS |
15548 | int extern_p = 0; |
15549 | int nomem_p = 0; | |
5566b478 | 15550 | int static_p = 0; |
4746cf84 | 15551 | int previous_instantiation_extern_p = 0; |
5566b478 | 15552 | |
ca79f85d JM |
15553 | if (TREE_CODE (t) == TYPE_DECL) |
15554 | t = TREE_TYPE (t); | |
15555 | ||
7ddedda4 | 15556 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 15557 | { |
0f51ccfc | 15558 | error ("explicit instantiation of non-template type %qT", t); |
ca79f85d JM |
15559 | return; |
15560 | } | |
15561 | ||
5566b478 | 15562 | complete_type (t); |
7177d104 | 15563 | |
d0f062fb | 15564 | if (!COMPLETE_TYPE_P (t)) |
f0e01782 | 15565 | { |
c2ea3a40 | 15566 | if (complain & tf_error) |
0f51ccfc | 15567 | error ("explicit instantiation of %q#T before definition of template", |
0cbd7506 | 15568 | t); |
f0e01782 MS |
15569 | return; |
15570 | } | |
15571 | ||
03d0f4af | 15572 | if (storage != NULL_TREE) |
f0e01782 | 15573 | { |
fcf73884 | 15574 | if (!in_system_header) |
1ffe7b6e PC |
15575 | { |
15576 | if (storage == ridpointers[(int) RID_EXTERN]) | |
15577 | { | |
15578 | if (cxx_dialect == cxx98) | |
509c9d60 MLI |
15579 | pedwarn (input_location, OPT_pedantic, |
15580 | "ISO C++ 1998 forbids the use of %<extern%> on " | |
15581 | "explicit instantiations"); | |
1ffe7b6e PC |
15582 | } |
15583 | else | |
509c9d60 MLI |
15584 | pedwarn (input_location, OPT_pedantic, |
15585 | "ISO C++ forbids the use of %qE" | |
15586 | " on explicit instantiations", storage); | |
1ffe7b6e | 15587 | } |
03d0f4af MM |
15588 | |
15589 | if (storage == ridpointers[(int) RID_INLINE]) | |
15590 | nomem_p = 1; | |
15591 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
15592 | extern_p = 1; | |
15593 | else if (storage == ridpointers[(int) RID_STATIC]) | |
15594 | static_p = 1; | |
15595 | else | |
15596 | { | |
0f51ccfc | 15597 | error ("storage class %qD applied to template instantiation", |
0cbd7506 | 15598 | storage); |
03d0f4af MM |
15599 | extern_p = 0; |
15600 | } | |
f0e01782 MS |
15601 | } |
15602 | ||
370af2d5 | 15603 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
03d0f4af | 15604 | { |
07782718 | 15605 | /* DR 259 [temp.spec]. |
a292b002 | 15606 | |
07782718 KL |
15607 | Both an explicit instantiation and a declaration of an explicit |
15608 | specialization shall not appear in a program unless the explicit | |
15609 | instantiation follows a declaration of the explicit specialization. | |
15610 | ||
15611 | For a given set of template parameters, if an explicit | |
15612 | instantiation of a template appears after a declaration of an | |
15613 | explicit specialization for that template, the explicit | |
15614 | instantiation has no effect. */ | |
03d0f4af MM |
15615 | return; |
15616 | } | |
15617 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 15618 | { |
03d0f4af MM |
15619 | /* [temp.spec] |
15620 | ||
15621 | No program shall explicitly instantiate any template more | |
c8094d83 | 15622 | than once. |
03d0f4af | 15623 | |
0cbd7506 | 15624 | If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit |
4746cf84 | 15625 | instantiation was `extern'. If EXTERN_P then the second is. |
4684cd27 | 15626 | These cases are OK. */ |
4746cf84 MA |
15627 | previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t); |
15628 | ||
15629 | if (!previous_instantiation_extern_p && !extern_p | |
c2ea3a40 | 15630 | && (complain & tf_error)) |
cbe5f3b3 | 15631 | permerror (input_location, "duplicate explicit instantiation of %q#T", t); |
c8094d83 | 15632 | |
03d0f4af MM |
15633 | /* If we've already instantiated the template, just return now. */ |
15634 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
15635 | return; | |
44a8d0b3 | 15636 | } |
e8abc66f | 15637 | |
9c12301f | 15638 | check_explicit_instantiation_namespace (TYPE_NAME (t)); |
03d0f4af | 15639 | mark_class_instantiated (t, extern_p); |
03d0f4af | 15640 | |
e8abc66f MS |
15641 | if (nomem_p) |
15642 | return; | |
15643 | ||
7177d104 | 15644 | { |
db5ae43f | 15645 | tree tmp; |
5566b478 | 15646 | |
03d0f4af MM |
15647 | /* In contrast to implicit instantiation, where only the |
15648 | declarations, and not the definitions, of members are | |
15649 | instantiated, we have here: | |
15650 | ||
0cbd7506 | 15651 | [temp.explicit] |
03d0f4af MM |
15652 | |
15653 | The explicit instantiation of a class template specialization | |
15654 | implies the instantiation of all of its members not | |
15655 | previously explicitly specialized in the translation unit | |
c8094d83 | 15656 | containing the explicit instantiation. |
03d0f4af MM |
15657 | |
15658 | Of course, we can't instantiate member template classes, since | |
15659 | we don't have any arguments for them. Note that the standard | |
dc957d14 | 15660 | is unclear on whether the instantiation of the members are |
415c974c | 15661 | *explicit* instantiations or not. However, the most natural |
03fd3f84 | 15662 | interpretation is that it should be an explicit instantiation. */ |
03d0f4af | 15663 | |
5566b478 MS |
15664 | if (! static_p) |
15665 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 15666 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 15667 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
415c974c | 15668 | instantiate_class_member (tmp, extern_p); |
5566b478 MS |
15669 | |
15670 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
15671 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
415c974c | 15672 | instantiate_class_member (tmp, extern_p); |
7177d104 | 15673 | |
5e0c54e5 GDR |
15674 | if (CLASSTYPE_NESTED_UTDS (t)) |
15675 | binding_table_foreach (CLASSTYPE_NESTED_UTDS (t), | |
0cbd7506 | 15676 | bt_instantiate_type_proc, &storage); |
a292b002 | 15677 | } |
8d08fdba | 15678 | } |
a28e3c7f | 15679 | |
36a117a5 MM |
15680 | /* Given a function DECL, which is a specialization of TMPL, modify |
15681 | DECL to be a re-instantiation of TMPL with the same template | |
15682 | arguments. TMPL should be the template into which tsubst'ing | |
15683 | should occur for DECL, not the most general template. | |
830bfa74 MM |
15684 | |
15685 | One reason for doing this is a scenario like this: | |
15686 | ||
15687 | template <class T> | |
15688 | void f(const T&, int i); | |
15689 | ||
15690 | void g() { f(3, 7); } | |
15691 | ||
15692 | template <class T> | |
15693 | void f(const T& t, const int i) { } | |
15694 | ||
15695 | Note that when the template is first instantiated, with | |
15696 | instantiate_template, the resulting DECL will have no name for the | |
15697 | first parameter, and the wrong type for the second. So, when we go | |
15698 | to instantiate the DECL, we regenerate it. */ | |
15699 | ||
e9659ab0 | 15700 | static void |
3a978d72 | 15701 | regenerate_decl_from_template (tree decl, tree tmpl) |
830bfa74 | 15702 | { |
f9a7ae04 MM |
15703 | /* The arguments used to instantiate DECL, from the most general |
15704 | template. */ | |
830bfa74 | 15705 | tree args; |
830bfa74 | 15706 | tree code_pattern; |
830bfa74 MM |
15707 | |
15708 | args = DECL_TI_ARGS (decl); | |
15709 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
15710 | ||
2b907f5c KL |
15711 | /* Make sure that we can see identifiers, and compute access |
15712 | correctly. */ | |
15713 | push_access_scope (decl); | |
15714 | ||
c7222c02 MM |
15715 | if (TREE_CODE (decl) == FUNCTION_DECL) |
15716 | { | |
15717 | tree decl_parm; | |
15718 | tree pattern_parm; | |
15719 | tree specs; | |
15720 | int args_depth; | |
15721 | int parms_depth; | |
c8094d83 | 15722 | |
c7222c02 | 15723 | args_depth = TMPL_ARGS_DEPTH (args); |
c8094d83 | 15724 | parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
c7222c02 MM |
15725 | if (args_depth > parms_depth) |
15726 | args = get_innermost_template_args (args, parms_depth); | |
15727 | ||
15728 | specs = tsubst_exception_specification (TREE_TYPE (code_pattern), | |
15729 | args, tf_error, NULL_TREE); | |
15730 | if (specs) | |
15731 | TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), | |
15732 | specs); | |
15733 | ||
15734 | /* Merge parameter declarations. */ | |
c8094d83 | 15735 | decl_parm = skip_artificial_parms_for (decl, |
c7222c02 | 15736 | DECL_ARGUMENTS (decl)); |
c8094d83 | 15737 | pattern_parm |
c7222c02 MM |
15738 | = skip_artificial_parms_for (code_pattern, |
15739 | DECL_ARGUMENTS (code_pattern)); | |
5d80a306 | 15740 | while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm)) |
c7222c02 MM |
15741 | { |
15742 | tree parm_type; | |
b17bba6d | 15743 | tree attributes; |
5d80a306 | 15744 | |
c7222c02 MM |
15745 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) |
15746 | DECL_NAME (decl_parm) = DECL_NAME (pattern_parm); | |
15747 | parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error, | |
15748 | NULL_TREE); | |
02bab9db | 15749 | parm_type = type_decays_to (parm_type); |
c7222c02 MM |
15750 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) |
15751 | TREE_TYPE (decl_parm) = parm_type; | |
b17bba6d MM |
15752 | attributes = DECL_ATTRIBUTES (pattern_parm); |
15753 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
15754 | { | |
15755 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
15756 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
15757 | } | |
c7222c02 MM |
15758 | decl_parm = TREE_CHAIN (decl_parm); |
15759 | pattern_parm = TREE_CHAIN (pattern_parm); | |
15760 | } | |
5d80a306 DG |
15761 | /* Merge any parameters that match with the function parameter |
15762 | pack. */ | |
15763 | if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm)) | |
15764 | { | |
15765 | int i, len; | |
15766 | tree expanded_types; | |
15767 | /* Expand the TYPE_PACK_EXPANSION that provides the types for | |
15768 | the parameters in this function parameter pack. */ | |
15769 | expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm), | |
15770 | args, tf_error, NULL_TREE); | |
15771 | len = TREE_VEC_LENGTH (expanded_types); | |
15772 | for (i = 0; i < len; i++) | |
15773 | { | |
15774 | tree parm_type; | |
15775 | tree attributes; | |
15776 | ||
15777 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) | |
15778 | /* Rename the parameter to include the index. */ | |
15779 | DECL_NAME (decl_parm) = | |
15780 | make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i); | |
15781 | parm_type = TREE_VEC_ELT (expanded_types, i); | |
15782 | parm_type = type_decays_to (parm_type); | |
15783 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) | |
15784 | TREE_TYPE (decl_parm) = parm_type; | |
15785 | attributes = DECL_ATTRIBUTES (pattern_parm); | |
15786 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
15787 | { | |
15788 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
15789 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
15790 | } | |
15791 | decl_parm = TREE_CHAIN (decl_parm); | |
15792 | } | |
15793 | } | |
c7222c02 MM |
15794 | /* Merge additional specifiers from the CODE_PATTERN. */ |
15795 | if (DECL_DECLARED_INLINE_P (code_pattern) | |
15796 | && !DECL_DECLARED_INLINE_P (decl)) | |
15797 | DECL_DECLARED_INLINE_P (decl) = 1; | |
c7222c02 MM |
15798 | } |
15799 | else if (TREE_CODE (decl) == VAR_DECL) | |
b794e321 MM |
15800 | DECL_INITIAL (decl) = |
15801 | tsubst_expr (DECL_INITIAL (code_pattern), args, | |
015c2c66 MM |
15802 | tf_error, DECL_TI_TEMPLATE (decl), |
15803 | /*integral_constant_expression_p=*/false); | |
c7222c02 MM |
15804 | else |
15805 | gcc_unreachable (); | |
36a117a5 | 15806 | |
2b59fc25 | 15807 | pop_access_scope (decl); |
830bfa74 MM |
15808 | } |
15809 | ||
a723baf1 MM |
15810 | /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be |
15811 | substituted to get DECL. */ | |
15812 | ||
d58b7c2d | 15813 | tree |
a723baf1 MM |
15814 | template_for_substitution (tree decl) |
15815 | { | |
15816 | tree tmpl = DECL_TI_TEMPLATE (decl); | |
15817 | ||
15818 | /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern | |
15819 | for the instantiation. This is not always the most general | |
15820 | template. Consider, for example: | |
15821 | ||
0cbd7506 | 15822 | template <class T> |
a723baf1 | 15823 | struct S { template <class U> void f(); |
0cbd7506 | 15824 | template <> void f<int>(); }; |
a723baf1 MM |
15825 | |
15826 | and an instantiation of S<double>::f<int>. We want TD to be the | |
15827 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
15828 | while (/* An instantiation cannot have a definition, so we need a | |
15829 | more general template. */ | |
15830 | DECL_TEMPLATE_INSTANTIATION (tmpl) | |
15831 | /* We must also deal with friend templates. Given: | |
15832 | ||
c8094d83 | 15833 | template <class T> struct S { |
a723baf1 MM |
15834 | template <class U> friend void f() {}; |
15835 | }; | |
15836 | ||
15837 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
15838 | so far as the language is concerned, but that's still | |
15839 | where we get the pattern for the instantiation from. On | |
15840 | other hand, if the definition comes outside the class, say: | |
15841 | ||
c8094d83 | 15842 | template <class T> struct S { |
a723baf1 MM |
15843 | template <class U> friend void f(); |
15844 | }; | |
15845 | template <class U> friend void f() {} | |
15846 | ||
15847 | we don't need to look any further. That's what the check for | |
15848 | DECL_INITIAL is for. */ | |
15849 | || (TREE_CODE (decl) == FUNCTION_DECL | |
15850 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) | |
15851 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) | |
15852 | { | |
15853 | /* The present template, TD, should not be a definition. If it | |
15854 | were a definition, we should be using it! Note that we | |
15855 | cannot restructure the loop to just keep going until we find | |
15856 | a template with a definition, since that might go too far if | |
15857 | a specialization was declared, but not defined. */ | |
50bc768d NS |
15858 | gcc_assert (TREE_CODE (decl) != VAR_DECL |
15859 | || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))); | |
c8094d83 | 15860 | |
a723baf1 MM |
15861 | /* Fetch the more general template. */ |
15862 | tmpl = DECL_TI_TEMPLATE (tmpl); | |
15863 | } | |
15864 | ||
15865 | return tmpl; | |
15866 | } | |
15867 | ||
15305a73 JM |
15868 | /* Returns true if we need to instantiate this template instance even if we |
15869 | know we aren't going to emit it.. */ | |
15870 | ||
15871 | bool | |
15872 | always_instantiate_p (tree decl) | |
15873 | { | |
15874 | /* We always instantiate inline functions so that we can inline them. An | |
15875 | explicit instantiation declaration prohibits implicit instantiation of | |
15876 | non-inline functions. With high levels of optimization, we would | |
15877 | normally inline non-inline functions -- but we're not allowed to do | |
15878 | that for "extern template" functions. Therefore, we check | |
15879 | DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */ | |
15880 | return ((TREE_CODE (decl) == FUNCTION_DECL | |
15881 | && DECL_DECLARED_INLINE_P (decl)) | |
15882 | /* And we need to instantiate static data members so that | |
15883 | their initializers are available in integral constant | |
15884 | expressions. */ | |
15885 | || (TREE_CODE (decl) == VAR_DECL | |
15886 | && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))); | |
15887 | } | |
15888 | ||
16d53b64 | 15889 | /* Produce the definition of D, a _DECL generated from a template. If |
838dfd8a | 15890 | DEFER_OK is nonzero, then we don't have to actually do the |
415c974c MA |
15891 | instantiation now; we just have to do it sometime. Normally it is |
15892 | an error if this is an explicit instantiation but D is undefined. | |
eba839f9 MM |
15893 | EXPL_INST_CLASS_MEM_P is true iff D is a member of an |
15894 | explicitly instantiated class template. */ | |
f84b4be9 | 15895 | |
a28e3c7f | 15896 | tree |
3db45ab5 | 15897 | instantiate_decl (tree d, int defer_ok, |
eba839f9 | 15898 | bool expl_inst_class_mem_p) |
a28e3c7f | 15899 | { |
36a117a5 | 15900 | tree tmpl = DECL_TI_TEMPLATE (d); |
65f8b0fb MM |
15901 | tree gen_args; |
15902 | tree args; | |
830bfa74 | 15903 | tree td; |
36a117a5 MM |
15904 | tree code_pattern; |
15905 | tree spec; | |
15906 | tree gen_tmpl; | |
b794e321 | 15907 | bool pattern_defined; |
31a714f6 | 15908 | int need_push; |
82a98427 | 15909 | location_t saved_loc = input_location; |
f7e4e484 | 15910 | bool external_p; |
c8094d83 | 15911 | |
36a117a5 MM |
15912 | /* This function should only be used to instantiate templates for |
15913 | functions and static member variables. */ | |
50bc768d NS |
15914 | gcc_assert (TREE_CODE (d) == FUNCTION_DECL |
15915 | || TREE_CODE (d) == VAR_DECL); | |
36a117a5 | 15916 | |
cec24319 MM |
15917 | /* Variables are never deferred; if instantiation is required, they |
15918 | are instantiated right away. That allows for better code in the | |
15919 | case that an expression refers to the value of the variable -- | |
15920 | if the variable has a constant value the referring expression can | |
15921 | take advantage of that fact. */ | |
15922 | if (TREE_CODE (d) == VAR_DECL) | |
15923 | defer_ok = 0; | |
15924 | ||
db9b2174 MM |
15925 | /* Don't instantiate cloned functions. Instead, instantiate the |
15926 | functions they cloned. */ | |
15927 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) | |
15928 | d = DECL_CLONED_FUNCTION (d); | |
15929 | ||
2b110bfc JM |
15930 | if (DECL_TEMPLATE_INSTANTIATED (d) |
15931 | || DECL_TEMPLATE_SPECIALIZATION (d)) | |
15932 | /* D has already been instantiated or explicitly specialized, so | |
15933 | there's nothing for us to do here. | |
15934 | ||
15935 | It might seem reasonable to check whether or not D is an explicit | |
15936 | instantiation, and, if so, stop here. But when an explicit | |
15937 | instantiation is deferred until the end of the compilation, | |
15938 | DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do | |
15939 | the instantiation. */ | |
36a117a5 MM |
15940 | return d; |
15941 | ||
15305a73 JM |
15942 | /* Check to see whether we know that this template will be |
15943 | instantiated in some other file, as with "extern template" | |
15944 | extension. */ | |
15945 | external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d)); | |
15946 | ||
15947 | /* In general, we do not instantiate such templates. */ | |
15948 | if (external_p && !always_instantiate_p (d)) | |
15949 | return d; | |
15950 | ||
36a117a5 | 15951 | gen_tmpl = most_general_template (tmpl); |
65f8b0fb | 15952 | gen_args = DECL_TI_ARGS (d); |
2b110bfc JM |
15953 | |
15954 | if (tmpl != gen_tmpl) | |
15955 | /* We should already have the extra args. */ | |
15956 | gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) | |
15957 | == TMPL_ARGS_DEPTH (gen_args)); | |
15958 | /* And what's in the hash table should match D. */ | |
15959 | gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d | |
15960 | || spec == NULL_TREE); | |
36a117a5 MM |
15961 | |
15962 | /* This needs to happen before any tsubsting. */ | |
15963 | if (! push_tinst_level (d)) | |
15964 | return d; | |
15965 | ||
297a5329 JM |
15966 | timevar_push (TV_PARSE); |
15967 | ||
4d85e00e | 15968 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
a723baf1 MM |
15969 | for the instantiation. */ |
15970 | td = template_for_substitution (d); | |
fee23f54 | 15971 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 15972 | |
2d22db1f | 15973 | /* We should never be trying to instantiate a member of a class |
3db45ab5 | 15974 | template or partial specialization. */ |
2d22db1f | 15975 | gcc_assert (d != code_pattern); |
3db45ab5 | 15976 | |
76d3baad KL |
15977 | if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) |
15978 | || DECL_TEMPLATE_SPECIALIZATION (td)) | |
649fc72d NS |
15979 | /* In the case of a friend template whose definition is provided |
15980 | outside the class, we may have too many arguments. Drop the | |
76d3baad | 15981 | ones we don't need. The same is true for specializations. */ |
649fc72d NS |
15982 | args = get_innermost_template_args |
15983 | (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); | |
15984 | else | |
15985 | args = gen_args; | |
65f8b0fb | 15986 | |
5566b478 | 15987 | if (TREE_CODE (d) == FUNCTION_DECL) |
39c76b4f | 15988 | pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); |
5566b478 | 15989 | else |
36a117a5 | 15990 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
b794e321 MM |
15991 | |
15992 | /* We may be in the middle of deferred access check. Disable it now. */ | |
15993 | push_deferring_access_checks (dk_no_deferred); | |
15994 | ||
4684cd27 MM |
15995 | /* Unless an explicit instantiation directive has already determined |
15996 | the linkage of D, remember that a definition is available for | |
15997 | this entity. */ | |
c8094d83 | 15998 | if (pattern_defined |
4684cd27 MM |
15999 | && !DECL_INTERFACE_KNOWN (d) |
16000 | && !DECL_NOT_REALLY_EXTERN (d)) | |
16001 | mark_definable (d); | |
de22184b | 16002 | |
f31686a3 | 16003 | input_location = DECL_SOURCE_LOCATION (d); |
de22184b | 16004 | |
eba839f9 MM |
16005 | /* If D is a member of an explicitly instantiated class template, |
16006 | and no definition is available, treat it like an implicit | |
3db45ab5 MS |
16007 | instantiation. */ |
16008 | if (!pattern_defined && expl_inst_class_mem_p | |
16009 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
5566b478 | 16010 | { |
4684cd27 | 16011 | DECL_NOT_REALLY_EXTERN (d) = 0; |
eba839f9 | 16012 | DECL_INTERFACE_KNOWN (d) = 0; |
4684cd27 | 16013 | SET_DECL_IMPLICIT_INSTANTIATION (d); |
5566b478 MS |
16014 | } |
16015 | ||
d5f4eddd JM |
16016 | /* Recheck the substitutions to obtain any warning messages |
16017 | about ignoring cv qualifiers. Don't do this for artificial decls, | |
16018 | as it breaks the context-sensitive substitution for lambda op(). */ | |
16019 | if (!defer_ok && !DECL_ARTIFICIAL (d)) | |
4f2b0fb2 | 16020 | { |
4f2b0fb2 NS |
16021 | tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); |
16022 | tree type = TREE_TYPE (gen); | |
16023 | ||
0e902d98 | 16024 | /* Make sure that we can see identifiers, and compute access |
2b59fc25 KL |
16025 | correctly. D is already the target FUNCTION_DECL with the |
16026 | right context. */ | |
16027 | push_access_scope (d); | |
0e902d98 | 16028 | |
4f2b0fb2 NS |
16029 | if (TREE_CODE (gen) == FUNCTION_DECL) |
16030 | { | |
23fca1f5 | 16031 | tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d); |
5d80a306 DG |
16032 | tsubst_exception_specification (type, gen_args, tf_warning_or_error, |
16033 | d); | |
4f2b0fb2 NS |
16034 | /* Don't simply tsubst the function type, as that will give |
16035 | duplicate warnings about poor parameter qualifications. | |
16036 | The function arguments are the same as the decl_arguments | |
c6002625 | 16037 | without the top level cv qualifiers. */ |
4f2b0fb2 NS |
16038 | type = TREE_TYPE (type); |
16039 | } | |
23fca1f5 | 16040 | tsubst (type, gen_args, tf_warning_or_error, d); |
0e902d98 | 16041 | |
2b59fc25 | 16042 | pop_access_scope (d); |
4f2b0fb2 | 16043 | } |
c8094d83 | 16044 | |
16d53b64 | 16045 | /* Defer all other templates, unless we have been explicitly |
f7e4e484 MM |
16046 | forbidden from doing so. */ |
16047 | if (/* If there is no definition, we cannot instantiate the | |
16048 | template. */ | |
3db45ab5 | 16049 | ! pattern_defined |
f7e4e484 MM |
16050 | /* If it's OK to postpone instantiation, do so. */ |
16051 | || defer_ok | |
16052 | /* If this is a static data member that will be defined | |
16053 | elsewhere, we don't want to instantiate the entire data | |
16054 | member, but we do want to instantiate the initializer so that | |
16055 | we can substitute that elsewhere. */ | |
16056 | || (external_p && TREE_CODE (d) == VAR_DECL)) | |
16d53b64 | 16057 | { |
b794e321 MM |
16058 | /* The definition of the static data member is now required so |
16059 | we must substitute the initializer. */ | |
16060 | if (TREE_CODE (d) == VAR_DECL | |
3db45ab5 | 16061 | && !DECL_INITIAL (d) |
b794e321 MM |
16062 | && DECL_INITIAL (code_pattern)) |
16063 | { | |
4bff36d3 MM |
16064 | tree ns; |
16065 | tree init; | |
16066 | ||
16067 | ns = decl_namespace_context (d); | |
27a725e2 | 16068 | push_nested_namespace (ns); |
b794e321 | 16069 | push_nested_class (DECL_CONTEXT (d)); |
3db45ab5 | 16070 | init = tsubst_expr (DECL_INITIAL (code_pattern), |
4bff36d3 | 16071 | args, |
015c2c66 MM |
16072 | tf_warning_or_error, NULL_TREE, |
16073 | /*integral_constant_expression_p=*/false); | |
d174af6c MM |
16074 | cp_finish_decl (d, init, /*init_const_expr_p=*/false, |
16075 | /*asmspec_tree=*/NULL_TREE, | |
86414779 | 16076 | LOOKUP_ONLYCONVERTING); |
b794e321 | 16077 | pop_nested_class (); |
27a725e2 | 16078 | pop_nested_namespace (ns); |
b794e321 MM |
16079 | } |
16080 | ||
f7e4e484 MM |
16081 | /* We restore the source position here because it's used by |
16082 | add_pending_template. */ | |
82a98427 | 16083 | input_location = saved_loc; |
c27be9b9 | 16084 | |
c8094d83 | 16085 | if (at_eof && !pattern_defined |
da3933ba JM |
16086 | && DECL_EXPLICIT_INSTANTIATION (d) |
16087 | && DECL_NOT_REALLY_EXTERN (d)) | |
03d0f4af MM |
16088 | /* [temp.explicit] |
16089 | ||
16090 | The definition of a non-exported function template, a | |
16091 | non-exported member function template, or a non-exported | |
16092 | member function or static data member of a class template | |
16093 | shall be present in every translation unit in which it is | |
16094 | explicitly instantiated. */ | |
cbe5f3b3 MLI |
16095 | permerror (input_location, "explicit instantiation of %qD " |
16096 | "but no definition available", d); | |
03d0f4af | 16097 | |
f7e4e484 MM |
16098 | /* ??? Historically, we have instantiated inline functions, even |
16099 | when marked as "extern template". */ | |
16100 | if (!(external_p && TREE_CODE (d) == VAR_DECL)) | |
16101 | add_pending_template (d); | |
de22184b | 16102 | goto out; |
5566b478 | 16103 | } |
4684cd27 MM |
16104 | /* Tell the repository that D is available in this translation unit |
16105 | -- and see if it is supposed to be instantiated here. */ | |
16106 | if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d)) | |
16107 | { | |
16108 | /* In a PCH file, despite the fact that the repository hasn't | |
16109 | requested instantiation in the PCH it is still possible that | |
16110 | an instantiation will be required in a file that includes the | |
16111 | PCH. */ | |
16112 | if (pch_file) | |
16113 | add_pending_template (d); | |
16114 | /* Instantiate inline functions so that the inliner can do its | |
16115 | job, even though we'll not be emitting a copy of this | |
16116 | function. */ | |
4847609e | 16117 | if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d))) |
4684cd27 MM |
16118 | goto out; |
16119 | } | |
5566b478 | 16120 | |
6de9cd9a | 16121 | need_push = !cfun || !global_bindings_p (); |
31a714f6 MM |
16122 | if (need_push) |
16123 | push_to_top_level (); | |
414ea4aa | 16124 | |
66e0c440 KL |
16125 | /* Mark D as instantiated so that recursive calls to |
16126 | instantiate_decl do not try to instantiate it again. */ | |
16127 | DECL_TEMPLATE_INSTANTIATED (d) = 1; | |
16128 | ||
2b0a63a3 MM |
16129 | /* Regenerate the declaration in case the template has been modified |
16130 | by a subsequent redeclaration. */ | |
16131 | regenerate_decl_from_template (d, td); | |
4684cd27 | 16132 | |
120722ac | 16133 | /* We already set the file and line above. Reset them now in case |
6de9cd9a | 16134 | they changed as a result of calling regenerate_decl_from_template. */ |
f31686a3 | 16135 | input_location = DECL_SOURCE_LOCATION (d); |
5156628f | 16136 | |
5566b478 MS |
16137 | if (TREE_CODE (d) == VAR_DECL) |
16138 | { | |
e92fb501 MM |
16139 | tree init; |
16140 | ||
1d62c33e MM |
16141 | /* Clear out DECL_RTL; whatever was there before may not be right |
16142 | since we've reset the type of the declaration. */ | |
16143 | SET_DECL_RTL (d, NULL_RTX); | |
5566b478 | 16144 | DECL_IN_AGGR_P (d) = 0; |
ea56c40c | 16145 | |
e92fb501 MM |
16146 | /* The initializer is placed in DECL_INITIAL by |
16147 | regenerate_decl_from_template. Pull it out so that | |
3600f678 | 16148 | cp_finish_decl can process it. */ |
e92fb501 MM |
16149 | init = DECL_INITIAL (d); |
16150 | DECL_INITIAL (d) = NULL_TREE; | |
16151 | DECL_INITIALIZED_P (d) = 0; | |
16152 | ||
4684cd27 MM |
16153 | /* Clear DECL_EXTERNAL so that cp_finish_decl will process the |
16154 | initializer. That function will defer actual emission until | |
16155 | we have a chance to determine linkage. */ | |
16156 | DECL_EXTERNAL (d) = 0; | |
16157 | ||
73a8adb6 | 16158 | /* Enter the scope of D so that access-checking works correctly. */ |
4684cd27 | 16159 | push_nested_class (DECL_CONTEXT (d)); |
3600f678 | 16160 | cp_finish_decl (d, init, false, NULL_TREE, 0); |
73a8adb6 | 16161 | pop_nested_class (); |
5566b478 MS |
16162 | } |
16163 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
16164 | { | |
6bbf1598 | 16165 | htab_t saved_local_specializations; |
a723baf1 MM |
16166 | tree subst_decl; |
16167 | tree tmpl_parm; | |
16168 | tree spec_parm; | |
6bbf1598 MM |
16169 | |
16170 | /* Save away the current list, in case we are instantiating one | |
16171 | template from within the body of another. */ | |
16172 | saved_local_specializations = local_specializations; | |
16173 | ||
6dfbb909 | 16174 | /* Set up the list of local specializations. */ |
c8094d83 | 16175 | local_specializations = htab_create (37, |
69f794a7 | 16176 | hash_local_specialization, |
a723baf1 | 16177 | eq_local_specializations, |
6dfbb909 MM |
16178 | NULL); |
16179 | ||
558475f0 | 16180 | /* Set up context. */ |
058b15c1 | 16181 | start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED); |
5566b478 | 16182 | |
a723baf1 MM |
16183 | /* Create substitution entries for the parameters. */ |
16184 | subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d)); | |
16185 | tmpl_parm = DECL_ARGUMENTS (subst_decl); | |
16186 | spec_parm = DECL_ARGUMENTS (d); | |
16187 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d)) | |
16188 | { | |
16189 | register_local_specialization (spec_parm, tmpl_parm); | |
16190 | spec_parm = skip_artificial_parms_for (d, spec_parm); | |
16191 | tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm); | |
16192 | } | |
5d80a306 | 16193 | while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
a723baf1 MM |
16194 | { |
16195 | register_local_specialization (spec_parm, tmpl_parm); | |
16196 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
16197 | spec_parm = TREE_CHAIN (spec_parm); | |
16198 | } | |
5d80a306 DG |
16199 | if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
16200 | { | |
5d80a306 DG |
16201 | /* Register the (value) argument pack as a specialization of |
16202 | TMPL_PARM, then move on. */ | |
6afcfe0a | 16203 | tree argpack = make_fnparm_pack (spec_parm); |
5d80a306 DG |
16204 | register_local_specialization (argpack, tmpl_parm); |
16205 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
6afcfe0a | 16206 | spec_parm = NULL_TREE; |
5d80a306 | 16207 | } |
50bc768d | 16208 | gcc_assert (!spec_parm); |
a723baf1 | 16209 | |
558475f0 MM |
16210 | /* Substitute into the body of the function. */ |
16211 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
015c2c66 MM |
16212 | tf_warning_or_error, tmpl, |
16213 | /*integral_constant_expression_p=*/false); | |
558475f0 | 16214 | |
75407da3 SP |
16215 | /* Set the current input_location to the end of the function |
16216 | so that finish_function knows where we are. */ | |
16217 | input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus; | |
16218 | ||
6dfbb909 MM |
16219 | /* We don't need the local specializations any more. */ |
16220 | htab_delete (local_specializations); | |
6bbf1598 | 16221 | local_specializations = saved_local_specializations; |
6dfbb909 | 16222 | |
4d6abc1c | 16223 | /* Finish the function. */ |
b2dd096b | 16224 | d = finish_function (0); |
8cd2462c | 16225 | expand_or_defer_fn (d); |
5566b478 MS |
16226 | } |
16227 | ||
971cbc14 MM |
16228 | /* We're not deferring instantiation any more. */ |
16229 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; | |
16230 | ||
31a714f6 MM |
16231 | if (need_push) |
16232 | pop_from_top_level (); | |
414ea4aa | 16233 | |
de22184b | 16234 | out: |
82a98427 | 16235 | input_location = saved_loc; |
7d021397 | 16236 | pop_deferring_access_checks (); |
5566b478 | 16237 | pop_tinst_level (); |
a28e3c7f | 16238 | |
297a5329 JM |
16239 | timevar_pop (TV_PARSE); |
16240 | ||
a28e3c7f MS |
16241 | return d; |
16242 | } | |
5566b478 | 16243 | |
0aafb128 | 16244 | /* Run through the list of templates that we wish we could |
35046a54 KL |
16245 | instantiate, and instantiate any we can. RETRIES is the |
16246 | number of times we retry pending template instantiation. */ | |
0aafb128 | 16247 | |
35046a54 KL |
16248 | void |
16249 | instantiate_pending_templates (int retries) | |
0aafb128 | 16250 | { |
0aafb128 | 16251 | int reconsider; |
aad626f7 | 16252 | location_t saved_loc = input_location; |
35046a54 KL |
16253 | |
16254 | /* Instantiating templates may trigger vtable generation. This in turn | |
16255 | may require further template instantiations. We place a limit here | |
16256 | to avoid infinite loop. */ | |
16257 | if (pending_templates && retries >= max_tinst_depth) | |
16258 | { | |
e2c3721c | 16259 | tree decl = pending_templates->tinst->decl; |
96c993a8 | 16260 | |
dee15844 | 16261 | error ("template instantiation depth exceeds maximum of %d" |
96c993a8 JM |
16262 | " instantiating %q+D, possibly from virtual table generation" |
16263 | " (use -ftemplate-depth-NN to increase the maximum)", | |
16264 | max_tinst_depth, decl); | |
16265 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
16266 | /* Pretend that we defined it. */ | |
16267 | DECL_INITIAL (decl) = error_mark_node; | |
35046a54 KL |
16268 | return; |
16269 | } | |
16270 | ||
c8094d83 | 16271 | do |
0aafb128 | 16272 | { |
e2c3721c PB |
16273 | struct pending_template **t = &pending_templates; |
16274 | struct pending_template *last = NULL; | |
0aafb128 | 16275 | reconsider = 0; |
0aafb128 MM |
16276 | while (*t) |
16277 | { | |
e2c3721c PB |
16278 | tree instantiation = reopen_tinst_level ((*t)->tinst); |
16279 | bool complete = false; | |
0aafb128 | 16280 | |
2f939d94 | 16281 | if (TYPE_P (instantiation)) |
0aafb128 MM |
16282 | { |
16283 | tree fn; | |
16284 | ||
d0f062fb | 16285 | if (!COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
16286 | { |
16287 | instantiate_class_template (instantiation); | |
16288 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
c8094d83 | 16289 | for (fn = TYPE_METHODS (instantiation); |
0aafb128 MM |
16290 | fn; |
16291 | fn = TREE_CHAIN (fn)) | |
16292 | if (! DECL_ARTIFICIAL (fn)) | |
3db45ab5 | 16293 | instantiate_decl (fn, |
eba839f9 MM |
16294 | /*defer_ok=*/0, |
16295 | /*expl_inst_class_mem_p=*/false); | |
d0f062fb | 16296 | if (COMPLETE_TYPE_P (instantiation)) |
35046a54 | 16297 | reconsider = 1; |
0aafb128 MM |
16298 | } |
16299 | ||
e2c3721c | 16300 | complete = COMPLETE_TYPE_P (instantiation); |
0aafb128 MM |
16301 | } |
16302 | else | |
16303 | { | |
16d53b64 | 16304 | if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
16305 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) |
16306 | { | |
3db45ab5 | 16307 | instantiation |
eba839f9 MM |
16308 | = instantiate_decl (instantiation, |
16309 | /*defer_ok=*/0, | |
16310 | /*expl_inst_class_mem_p=*/false); | |
0aafb128 | 16311 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) |
35046a54 | 16312 | reconsider = 1; |
0aafb128 MM |
16313 | } |
16314 | ||
e2c3721c PB |
16315 | complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation) |
16316 | || DECL_TEMPLATE_INSTANTIATED (instantiation)); | |
16317 | } | |
16318 | ||
16319 | if (complete) | |
16320 | /* If INSTANTIATION has been instantiated, then we don't | |
16321 | need to consider it again in the future. */ | |
16322 | *t = (*t)->next; | |
16323 | else | |
16324 | { | |
16325 | last = *t; | |
16326 | t = &(*t)->next; | |
0aafb128 | 16327 | } |
84e5ca0f | 16328 | tinst_depth = 0; |
e2c3721c | 16329 | current_tinst_level = NULL; |
0aafb128 | 16330 | } |
46ccf50a | 16331 | last_pending_template = last; |
c8094d83 | 16332 | } |
0aafb128 MM |
16333 | while (reconsider); |
16334 | ||
aad626f7 | 16335 | input_location = saved_loc; |
0aafb128 MM |
16336 | } |
16337 | ||
fd74ca0b MM |
16338 | /* Substitute ARGVEC into T, which is a list of initializers for |
16339 | either base class or a non-static data member. The TREE_PURPOSEs | |
16340 | are DECLs, and the TREE_VALUEs are the initializer values. Used by | |
16341 | instantiate_decl. */ | |
4393e105 | 16342 | |
824b9a4c | 16343 | static tree |
3a978d72 | 16344 | tsubst_initializer_list (tree t, tree argvec) |
5566b478 | 16345 | { |
2282d28d | 16346 | tree inits = NULL_TREE; |
5566b478 MS |
16347 | |
16348 | for (; t; t = TREE_CHAIN (t)) | |
16349 | { | |
fd74ca0b MM |
16350 | tree decl; |
16351 | tree init; | |
5d80a306 DG |
16352 | tree expanded_bases = NULL_TREE; |
16353 | tree expanded_arguments = NULL_TREE; | |
16354 | int i, len = 1; | |
fd74ca0b | 16355 | |
5d80a306 DG |
16356 | if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION) |
16357 | { | |
16358 | tree expr; | |
16359 | tree arg; | |
16360 | ||
16361 | /* Expand the base class expansion type into separate base | |
16362 | classes. */ | |
16363 | expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec, | |
16364 | tf_warning_or_error, | |
16365 | NULL_TREE); | |
16366 | if (expanded_bases == error_mark_node) | |
16367 | continue; | |
16368 | ||
16369 | /* We'll be building separate TREE_LISTs of arguments for | |
16370 | each base. */ | |
16371 | len = TREE_VEC_LENGTH (expanded_bases); | |
16372 | expanded_arguments = make_tree_vec (len); | |
16373 | for (i = 0; i < len; i++) | |
16374 | TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE; | |
16375 | ||
16376 | /* Build a dummy EXPR_PACK_EXPANSION that will be used to | |
16377 | expand each argument in the TREE_VALUE of t. */ | |
16378 | expr = make_node (EXPR_PACK_EXPANSION); | |
16379 | PACK_EXPANSION_PARAMETER_PACKS (expr) = | |
16380 | PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t)); | |
16381 | ||
625b6d91 DG |
16382 | if (TREE_VALUE (t) == void_type_node) |
16383 | /* VOID_TYPE_NODE is used to indicate | |
16384 | value-initialization. */ | |
16385 | { | |
16386 | for (i = 0; i < len; i++) | |
16387 | TREE_VEC_ELT (expanded_arguments, i) = void_type_node; | |
16388 | } | |
16389 | else | |
16390 | { | |
16391 | /* Substitute parameter packs into each argument in the | |
16392 | TREE_LIST. */ | |
16393 | in_base_initializer = 1; | |
16394 | for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg)) | |
16395 | { | |
16396 | tree expanded_exprs; | |
16397 | ||
16398 | /* Expand the argument. */ | |
16399 | SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg)); | |
16400 | expanded_exprs | |
16401 | = tsubst_pack_expansion (expr, argvec, | |
16402 | tf_warning_or_error, | |
16403 | NULL_TREE); | |
aecaad79 PC |
16404 | if (expanded_exprs == error_mark_node) |
16405 | continue; | |
625b6d91 DG |
16406 | |
16407 | /* Prepend each of the expanded expressions to the | |
16408 | corresponding TREE_LIST in EXPANDED_ARGUMENTS. */ | |
16409 | for (i = 0; i < len; i++) | |
16410 | { | |
16411 | TREE_VEC_ELT (expanded_arguments, i) = | |
16412 | tree_cons (NULL_TREE, | |
16413 | TREE_VEC_ELT (expanded_exprs, i), | |
16414 | TREE_VEC_ELT (expanded_arguments, i)); | |
16415 | } | |
16416 | } | |
16417 | in_base_initializer = 0; | |
5d80a306 | 16418 | |
625b6d91 DG |
16419 | /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS, |
16420 | since we built them backwards. */ | |
16421 | for (i = 0; i < len; i++) | |
16422 | { | |
16423 | TREE_VEC_ELT (expanded_arguments, i) = | |
16424 | nreverse (TREE_VEC_ELT (expanded_arguments, i)); | |
16425 | } | |
16426 | } | |
5d80a306 | 16427 | } |
1f5a253a | 16428 | |
5d80a306 DG |
16429 | for (i = 0; i < len; ++i) |
16430 | { | |
16431 | if (expanded_bases) | |
16432 | { | |
16433 | decl = TREE_VEC_ELT (expanded_bases, i); | |
16434 | decl = expand_member_init (decl); | |
16435 | init = TREE_VEC_ELT (expanded_arguments, i); | |
16436 | } | |
16437 | else | |
16438 | { | |
16439 | decl = tsubst_copy (TREE_PURPOSE (t), argvec, | |
16440 | tf_warning_or_error, NULL_TREE); | |
16441 | ||
16442 | decl = expand_member_init (decl); | |
16443 | if (decl && !DECL_P (decl)) | |
16444 | in_base_initializer = 1; | |
16445 | ||
16446 | init = tsubst_expr (TREE_VALUE (t), argvec, | |
16447 | tf_warning_or_error, NULL_TREE, | |
16448 | /*integral_constant_expression_p=*/false); | |
16449 | in_base_initializer = 0; | |
16450 | } | |
16451 | ||
16452 | if (decl) | |
16453 | { | |
16454 | init = build_tree_list (decl, init); | |
16455 | TREE_CHAIN (init) = inits; | |
16456 | inits = init; | |
16457 | } | |
16458 | } | |
5566b478 | 16459 | } |
2282d28d | 16460 | return inits; |
5566b478 MS |
16461 | } |
16462 | ||
61a127b3 MM |
16463 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
16464 | ||
16465 | static void | |
3a978d72 | 16466 | set_current_access_from_decl (tree decl) |
61a127b3 MM |
16467 | { |
16468 | if (TREE_PRIVATE (decl)) | |
16469 | current_access_specifier = access_private_node; | |
16470 | else if (TREE_PROTECTED (decl)) | |
16471 | current_access_specifier = access_protected_node; | |
16472 | else | |
16473 | current_access_specifier = access_public_node; | |
16474 | } | |
16475 | ||
dbfe2124 MM |
16476 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
16477 | is the instantiation (which should have been created with | |
16478 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 16479 | |
dbfe2124 | 16480 | static void |
3a978d72 | 16481 | tsubst_enum (tree tag, tree newtag, tree args) |
b87692e5 | 16482 | { |
dbfe2124 | 16483 | tree e; |
b87692e5 MS |
16484 | |
16485 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
16486 | { | |
61a127b3 | 16487 | tree value; |
7b6d72fc MM |
16488 | tree decl; |
16489 | ||
16490 | decl = TREE_VALUE (e); | |
61a127b3 MM |
16491 | /* Note that in a template enum, the TREE_VALUE is the |
16492 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
c8094d83 | 16493 | value = tsubst_expr (DECL_INITIAL (decl), |
015c2c66 MM |
16494 | args, tf_warning_or_error, NULL_TREE, |
16495 | /*integral_constant_expression_p=*/true); | |
61a127b3 MM |
16496 | |
16497 | /* Give this enumeration constant the correct access. */ | |
7b6d72fc | 16498 | set_current_access_from_decl (decl); |
61a127b3 MM |
16499 | |
16500 | /* Actually build the enumerator itself. */ | |
c8094d83 | 16501 | build_enumerator (DECL_NAME (decl), value, newtag); |
dbfe2124 | 16502 | } |
b3d5a58b | 16503 | |
219670f1 | 16504 | finish_enum (newtag); |
f31686a3 RH |
16505 | DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) |
16506 | = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); | |
b87692e5 | 16507 | } |
36a117a5 | 16508 | |
1f6e1acc AS |
16509 | /* DECL is a FUNCTION_DECL that is a template specialization. Return |
16510 | its type -- but without substituting the innermost set of template | |
16511 | arguments. So, innermost set of template parameters will appear in | |
5c74d5b0 | 16512 | the type. */ |
1f6e1acc | 16513 | |
c8094d83 | 16514 | tree |
3a978d72 | 16515 | get_mostly_instantiated_function_type (tree decl) |
1f6e1acc | 16516 | { |
1f6e1acc AS |
16517 | tree fn_type; |
16518 | tree tmpl; | |
16519 | tree targs; | |
16520 | tree tparms; | |
16521 | int parm_depth; | |
16522 | ||
16523 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
16524 | targs = DECL_TI_ARGS (decl); | |
16525 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
16526 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
16527 | ||
16528 | /* There should be as many levels of arguments as there are levels | |
16529 | of parameters. */ | |
50bc768d | 16530 | gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs)); |
1f6e1acc AS |
16531 | |
16532 | fn_type = TREE_TYPE (tmpl); | |
1f6e1acc AS |
16533 | |
16534 | if (parm_depth == 1) | |
16535 | /* No substitution is necessary. */ | |
16536 | ; | |
16537 | else | |
16538 | { | |
fae6e246 | 16539 | int i, save_access_control; |
1f6e1acc AS |
16540 | tree partial_args; |
16541 | ||
16542 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
dc957d14 | 16543 | let tsubst know not to substitute for those parameters. */ |
1f6e1acc AS |
16544 | partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); |
16545 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
16546 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
16547 | TMPL_ARGS_LEVEL (targs, i)); | |
16548 | SET_TMPL_ARGS_LEVEL (partial_args, | |
16549 | TMPL_ARGS_DEPTH (targs), | |
16550 | make_tree_vec (DECL_NTPARMS (tmpl))); | |
16551 | ||
fae6e246 RH |
16552 | /* Disable access control as this function is used only during |
16553 | name-mangling. */ | |
16554 | save_access_control = flag_access_control; | |
16555 | flag_access_control = 0; | |
5c74d5b0 | 16556 | |
9579624e | 16557 | ++processing_template_decl; |
1f6e1acc AS |
16558 | /* Now, do the (partial) substitution to figure out the |
16559 | appropriate function type. */ | |
c2ea3a40 | 16560 | fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); |
9579624e | 16561 | --processing_template_decl; |
1f6e1acc AS |
16562 | |
16563 | /* Substitute into the template parameters to obtain the real | |
16564 | innermost set of parameters. This step is important if the | |
16565 | innermost set of template parameters contains value | |
16566 | parameters whose types depend on outer template parameters. */ | |
16567 | TREE_VEC_LENGTH (partial_args)--; | |
c2ea3a40 | 16568 | tparms = tsubst_template_parms (tparms, partial_args, tf_error); |
1f6e1acc | 16569 | |
fae6e246 | 16570 | flag_access_control = save_access_control; |
5c74d5b0 | 16571 | } |
1f6e1acc AS |
16572 | |
16573 | return fn_type; | |
16574 | } | |
669ec2b4 | 16575 | |
cb753e49 | 16576 | /* Return truthvalue if we're processing a template different from |
dc957d14 | 16577 | the last one involved in diagnostics. */ |
cb753e49 | 16578 | int |
3a978d72 | 16579 | problematic_instantiation_changed (void) |
cb753e49 GDR |
16580 | { |
16581 | return last_template_error_tick != tinst_level_tick; | |
16582 | } | |
16583 | ||
16584 | /* Remember current template involved in diagnostics. */ | |
16585 | void | |
3a978d72 | 16586 | record_last_problematic_instantiation (void) |
cb753e49 GDR |
16587 | { |
16588 | last_template_error_tick = tinst_level_tick; | |
16589 | } | |
16590 | ||
e2c3721c | 16591 | struct tinst_level * |
3a978d72 | 16592 | current_instantiation (void) |
cb753e49 GDR |
16593 | { |
16594 | return current_tinst_level; | |
16595 | } | |
db3f4e4e NS |
16596 | |
16597 | /* [temp.param] Check that template non-type parm TYPE is of an allowable | |
838dfd8a | 16598 | type. Return zero for ok, nonzero for disallowed. Issue error and |
c2ea3a40 | 16599 | warning messages under control of COMPLAIN. */ |
db3f4e4e NS |
16600 | |
16601 | static int | |
3a978d72 | 16602 | invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain) |
db3f4e4e | 16603 | { |
550a799d | 16604 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
db3f4e4e NS |
16605 | return 0; |
16606 | else if (POINTER_TYPE_P (type)) | |
16607 | return 0; | |
a5ac359a | 16608 | else if (TYPE_PTR_TO_MEMBER_P (type)) |
db3f4e4e | 16609 | return 0; |
db3f4e4e NS |
16610 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
16611 | return 0; | |
16612 | else if (TREE_CODE (type) == TYPENAME_TYPE) | |
16613 | return 0; | |
c8094d83 | 16614 | |
c2ea3a40 | 16615 | if (complain & tf_error) |
0f51ccfc | 16616 | error ("%q#T is not a valid type for a template constant parameter", type); |
db3f4e4e NS |
16617 | return 1; |
16618 | } | |
e2500fed | 16619 | |
5552b43c MM |
16620 | /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. |
16621 | Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ | |
1fb3244a | 16622 | |
5552b43c MM |
16623 | static bool |
16624 | dependent_type_p_r (tree type) | |
1fb3244a MM |
16625 | { |
16626 | tree scope; | |
16627 | ||
1fb3244a MM |
16628 | /* [temp.dep.type] |
16629 | ||
16630 | A type is dependent if it is: | |
16631 | ||
6615c446 JO |
16632 | -- a template parameter. Template template parameters are types |
16633 | for us (since TYPE_P holds true for them) so we handle | |
16634 | them here. */ | |
c8094d83 | 16635 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
6bf92cb6 | 16636 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) |
1fb3244a MM |
16637 | return true; |
16638 | /* -- a qualified-id with a nested-name-specifier which contains a | |
0cbd7506 | 16639 | class-name that names a dependent type or whose unqualified-id |
1fb3244a MM |
16640 | names a dependent type. */ |
16641 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
16642 | return true; | |
16643 | /* -- a cv-qualified type where the cv-unqualified type is | |
0cbd7506 | 16644 | dependent. */ |
1fb3244a MM |
16645 | type = TYPE_MAIN_VARIANT (type); |
16646 | /* -- a compound type constructed from any dependent type. */ | |
a5ac359a | 16647 | if (TYPE_PTR_TO_MEMBER_P (type)) |
1fb3244a | 16648 | return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) |
c8094d83 | 16649 | || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE |
1fb3244a MM |
16650 | (type))); |
16651 | else if (TREE_CODE (type) == POINTER_TYPE | |
16652 | || TREE_CODE (type) == REFERENCE_TYPE) | |
16653 | return dependent_type_p (TREE_TYPE (type)); | |
16654 | else if (TREE_CODE (type) == FUNCTION_TYPE | |
16655 | || TREE_CODE (type) == METHOD_TYPE) | |
16656 | { | |
16657 | tree arg_type; | |
16658 | ||
16659 | if (dependent_type_p (TREE_TYPE (type))) | |
16660 | return true; | |
c8094d83 MS |
16661 | for (arg_type = TYPE_ARG_TYPES (type); |
16662 | arg_type; | |
1fb3244a MM |
16663 | arg_type = TREE_CHAIN (arg_type)) |
16664 | if (dependent_type_p (TREE_VALUE (arg_type))) | |
16665 | return true; | |
16666 | return false; | |
16667 | } | |
16668 | /* -- an array type constructed from any dependent type or whose | |
0cbd7506 | 16669 | size is specified by a constant expression that is |
1fb3244a MM |
16670 | value-dependent. */ |
16671 | if (TREE_CODE (type) == ARRAY_TYPE) | |
16672 | { | |
16673 | if (TYPE_DOMAIN (type) | |
82390eb6 | 16674 | && dependent_type_p (TYPE_DOMAIN (type))) |
1fb3244a MM |
16675 | return true; |
16676 | return dependent_type_p (TREE_TYPE (type)); | |
16677 | } | |
82390eb6 JM |
16678 | else if (TREE_CODE (type) == INTEGER_TYPE |
16679 | && !TREE_CONSTANT (TYPE_MAX_VALUE (type))) | |
16680 | { | |
16681 | /* If this is the TYPE_DOMAIN of an array type, consider it | |
c1165535 JM |
16682 | dependent. We already checked for value-dependence in |
16683 | compute_array_index_type. */ | |
16684 | return type_dependent_expression_p (TYPE_MAX_VALUE (type)); | |
82390eb6 | 16685 | } |
c8094d83 | 16686 | |
1fb3244a | 16687 | /* -- a template-id in which either the template name is a template |
86306a6b NS |
16688 | parameter ... */ |
16689 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
1fb3244a | 16690 | return true; |
86306a6b | 16691 | /* ... or any of the template arguments is a dependent type or |
04c06002 | 16692 | an expression that is type-dependent or value-dependent. */ |
86306a6b | 16693 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) |
c8094d83 | 16694 | && (any_dependent_template_arguments_p |
7e99327d | 16695 | (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type))))) |
1fb3244a | 16696 | return true; |
c8094d83 | 16697 | |
3ad6a8e1 DG |
16698 | /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the |
16699 | argument of the `typeof' expression is not type-dependent, then | |
16700 | it should already been have resolved. */ | |
16701 | if (TREE_CODE (type) == TYPEOF_TYPE | |
16702 | || TREE_CODE (type) == DECLTYPE_TYPE) | |
1fb3244a | 16703 | return true; |
c8094d83 | 16704 | |
5d80a306 DG |
16705 | /* A template argument pack is dependent if any of its packed |
16706 | arguments are. */ | |
16707 | if (TREE_CODE (type) == TYPE_ARGUMENT_PACK) | |
16708 | { | |
16709 | tree args = ARGUMENT_PACK_ARGS (type); | |
16710 | int i, len = TREE_VEC_LENGTH (args); | |
16711 | for (i = 0; i < len; ++i) | |
16712 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
16713 | return true; | |
16714 | } | |
16715 | ||
16716 | /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must | |
16717 | be template parameters. */ | |
16718 | if (TREE_CODE (type) == TYPE_PACK_EXPANSION) | |
16719 | return true; | |
16720 | ||
1fb3244a MM |
16721 | /* The standard does not specifically mention types that are local |
16722 | to template functions or local classes, but they should be | |
16723 | considered dependent too. For example: | |
16724 | ||
c8094d83 | 16725 | template <int I> void f() { |
0cbd7506 | 16726 | enum E { a = I }; |
1fb3244a MM |
16727 | S<sizeof (E)> s; |
16728 | } | |
16729 | ||
16730 | The size of `E' cannot be known until the value of `I' has been | |
16731 | determined. Therefore, `E' must be considered dependent. */ | |
16732 | scope = TYPE_CONTEXT (type); | |
16733 | if (scope && TYPE_P (scope)) | |
16734 | return dependent_type_p (scope); | |
16735 | else if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
16736 | return type_dependent_expression_p (scope); | |
16737 | ||
16738 | /* Other types are non-dependent. */ | |
16739 | return false; | |
16740 | } | |
16741 | ||
5552b43c MM |
16742 | /* Returns TRUE if TYPE is dependent, in the sense of |
16743 | [temp.dep.type]. */ | |
16744 | ||
16745 | bool | |
16746 | dependent_type_p (tree type) | |
16747 | { | |
16748 | /* If there are no template parameters in scope, then there can't be | |
16749 | any dependent types. */ | |
16750 | if (!processing_template_decl) | |
e7e93965 MM |
16751 | { |
16752 | /* If we are not processing a template, then nobody should be | |
16753 | providing us with a dependent type. */ | |
16754 | gcc_assert (type); | |
0257eee5 | 16755 | gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type)); |
e7e93965 MM |
16756 | return false; |
16757 | } | |
5552b43c MM |
16758 | |
16759 | /* If the type is NULL, we have not computed a type for the entity | |
16760 | in question; in that case, the type is dependent. */ | |
16761 | if (!type) | |
16762 | return true; | |
16763 | ||
16764 | /* Erroneous types can be considered non-dependent. */ | |
16765 | if (type == error_mark_node) | |
16766 | return false; | |
16767 | ||
16768 | /* If we have not already computed the appropriate value for TYPE, | |
16769 | do so now. */ | |
16770 | if (!TYPE_DEPENDENT_P_VALID (type)) | |
16771 | { | |
16772 | TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); | |
16773 | TYPE_DEPENDENT_P_VALID (type) = 1; | |
16774 | } | |
16775 | ||
16776 | return TYPE_DEPENDENT_P (type); | |
16777 | } | |
16778 | ||
1cb801bc JM |
16779 | /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any |
16780 | lookup. In other words, a dependent type that is not the current | |
16781 | instantiation. */ | |
16782 | ||
16783 | bool | |
16784 | dependent_scope_p (tree scope) | |
16785 | { | |
b77ba909 JM |
16786 | return (scope && TYPE_P (scope) && dependent_type_p (scope) |
16787 | && !currently_open_class (scope)); | |
1cb801bc JM |
16788 | } |
16789 | ||
8d83f792 MM |
16790 | /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */ |
16791 | ||
16792 | static bool | |
16793 | dependent_scope_ref_p (tree expression, bool criterion (tree)) | |
16794 | { | |
16795 | tree scope; | |
16796 | tree name; | |
16797 | ||
50bc768d | 16798 | gcc_assert (TREE_CODE (expression) == SCOPE_REF); |
8d83f792 MM |
16799 | |
16800 | if (!TYPE_P (TREE_OPERAND (expression, 0))) | |
16801 | return true; | |
16802 | ||
16803 | scope = TREE_OPERAND (expression, 0); | |
16804 | name = TREE_OPERAND (expression, 1); | |
16805 | ||
16806 | /* [temp.dep.expr] | |
16807 | ||
16808 | An id-expression is type-dependent if it contains a | |
16809 | nested-name-specifier that contains a class-name that names a | |
16810 | dependent type. */ | |
1cb801bc | 16811 | /* The suggested resolution to Core Issue 224 implies that if the |
8d83f792 MM |
16812 | qualifying type is the current class, then we must peek |
16813 | inside it. */ | |
c8094d83 | 16814 | if (DECL_P (name) |
8d83f792 MM |
16815 | && currently_open_class (scope) |
16816 | && !criterion (name)) | |
16817 | return false; | |
16818 | if (dependent_type_p (scope)) | |
16819 | return true; | |
16820 | ||
16821 | return false; | |
16822 | } | |
16823 | ||
20929c7f | 16824 | /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of |
fe89d797 MM |
16825 | [temp.dep.constexpr]. EXPRESSION is already known to be a constant |
16826 | expression. */ | |
1fb3244a | 16827 | |
14d22dd6 | 16828 | bool |
1fb3244a MM |
16829 | value_dependent_expression_p (tree expression) |
16830 | { | |
16831 | if (!processing_template_decl) | |
16832 | return false; | |
16833 | ||
16834 | /* A name declared with a dependent type. */ | |
7416ab02 | 16835 | if (DECL_P (expression) && type_dependent_expression_p (expression)) |
1fb3244a | 16836 | return true; |
c8094d83 | 16837 | |
7416ab02 NS |
16838 | switch (TREE_CODE (expression)) |
16839 | { | |
16840 | case IDENTIFIER_NODE: | |
16841 | /* A name that has not been looked up -- must be dependent. */ | |
16842 | return true; | |
16843 | ||
16844 | case TEMPLATE_PARM_INDEX: | |
16845 | /* A non-type template parm. */ | |
16846 | return true; | |
16847 | ||
16848 | case CONST_DECL: | |
16849 | /* A non-type template parm. */ | |
16850 | if (DECL_TEMPLATE_PARM_P (expression)) | |
d17811fd | 16851 | return true; |
3387721b | 16852 | return value_dependent_expression_p (DECL_INITIAL (expression)); |
d36d5600 | 16853 | |
7416ab02 | 16854 | case VAR_DECL: |
c8094d83 | 16855 | /* A constant with integral or enumeration type and is initialized |
0cbd7506 | 16856 | with an expression that is value-dependent. */ |
7416ab02 NS |
16857 | if (DECL_INITIAL (expression) |
16858 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression)) | |
16859 | && value_dependent_expression_p (DECL_INITIAL (expression))) | |
16860 | return true; | |
16861 | return false; | |
16862 | ||
16863 | case DYNAMIC_CAST_EXPR: | |
16864 | case STATIC_CAST_EXPR: | |
16865 | case CONST_CAST_EXPR: | |
16866 | case REINTERPRET_CAST_EXPR: | |
16867 | case CAST_EXPR: | |
16868 | /* These expressions are value-dependent if the type to which | |
0cbd7506 MS |
16869 | the cast occurs is dependent or the expression being casted |
16870 | is value-dependent. */ | |
7416ab02 NS |
16871 | { |
16872 | tree type = TREE_TYPE (expression); | |
c8094d83 | 16873 | |
7416ab02 NS |
16874 | if (dependent_type_p (type)) |
16875 | return true; | |
c8094d83 | 16876 | |
7416ab02 NS |
16877 | /* A functional cast has a list of operands. */ |
16878 | expression = TREE_OPERAND (expression, 0); | |
16879 | if (!expression) | |
16880 | { | |
16881 | /* If there are no operands, it must be an expression such | |
16882 | as "int()". This should not happen for aggregate types | |
16883 | because it would form non-constant expressions. */ | |
16884 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); | |
c8094d83 | 16885 | |
7416ab02 NS |
16886 | return false; |
16887 | } | |
c8094d83 | 16888 | |
7416ab02 | 16889 | if (TREE_CODE (expression) == TREE_LIST) |
32a11c08 | 16890 | return any_value_dependent_elements_p (expression); |
c8094d83 | 16891 | |
d17811fd | 16892 | return value_dependent_expression_p (expression); |
7416ab02 | 16893 | } |
c8094d83 | 16894 | |
7416ab02 NS |
16895 | case SIZEOF_EXPR: |
16896 | case ALIGNOF_EXPR: | |
16897 | /* A `sizeof' expression is value-dependent if the operand is | |
5d80a306 | 16898 | type-dependent or is a pack expansion. */ |
d17811fd | 16899 | expression = TREE_OPERAND (expression, 0); |
5d80a306 DG |
16900 | if (PACK_EXPANSION_P (expression)) |
16901 | return true; | |
16902 | else if (TYPE_P (expression)) | |
d17811fd MM |
16903 | return dependent_type_p (expression); |
16904 | return type_dependent_expression_p (expression); | |
100d337a | 16905 | |
7416ab02 NS |
16906 | case SCOPE_REF: |
16907 | return dependent_scope_ref_p (expression, value_dependent_expression_p); | |
100d337a | 16908 | |
7416ab02 NS |
16909 | case COMPONENT_REF: |
16910 | return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) | |
16911 | || value_dependent_expression_p (TREE_OPERAND (expression, 1))); | |
16912 | ||
16913 | case CALL_EXPR: | |
fe89d797 MM |
16914 | /* A CALL_EXPR may appear in a constant expression if it is a |
16915 | call to a builtin function, e.g., __builtin_constant_p. All | |
16916 | such calls are value-dependent. */ | |
16917 | return true; | |
7416ab02 | 16918 | |
5d80a306 DG |
16919 | case NONTYPE_ARGUMENT_PACK: |
16920 | /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument | |
16921 | is value-dependent. */ | |
16922 | { | |
16923 | tree values = ARGUMENT_PACK_ARGS (expression); | |
16924 | int i, len = TREE_VEC_LENGTH (values); | |
16925 | ||
16926 | for (i = 0; i < len; ++i) | |
16927 | if (value_dependent_expression_p (TREE_VEC_ELT (values, i))) | |
16928 | return true; | |
16929 | ||
16930 | return false; | |
16931 | } | |
16932 | ||
cb68ec50 PC |
16933 | case TRAIT_EXPR: |
16934 | { | |
16935 | tree type2 = TRAIT_EXPR_TYPE2 (expression); | |
16936 | return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)) | |
16937 | || (type2 ? dependent_type_p (type2) : false)); | |
16938 | } | |
16939 | ||
f01ec37d SM |
16940 | case MODOP_EXPR: |
16941 | return ((value_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
16942 | || (value_dependent_expression_p (TREE_OPERAND (expression, 2)))); | |
16943 | ||
7416ab02 NS |
16944 | default: |
16945 | /* A constant expression is value-dependent if any subexpression is | |
0cbd7506 | 16946 | value-dependent. */ |
1fb3244a MM |
16947 | switch (TREE_CODE_CLASS (TREE_CODE (expression))) |
16948 | { | |
7416ab02 | 16949 | case tcc_reference: |
6615c446 | 16950 | case tcc_unary: |
c8094d83 | 16951 | return (value_dependent_expression_p |
1fb3244a | 16952 | (TREE_OPERAND (expression, 0))); |
c8094d83 | 16953 | |
6615c446 JO |
16954 | case tcc_comparison: |
16955 | case tcc_binary: | |
c8094d83 | 16956 | return ((value_dependent_expression_p |
1fb3244a | 16957 | (TREE_OPERAND (expression, 0))) |
c8094d83 | 16958 | || (value_dependent_expression_p |
1fb3244a | 16959 | (TREE_OPERAND (expression, 1)))); |
c8094d83 | 16960 | |
6615c446 | 16961 | case tcc_expression: |
5039610b | 16962 | case tcc_vl_exp: |
1fb3244a MM |
16963 | { |
16964 | int i; | |
5039610b | 16965 | for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i) |
14d22dd6 MM |
16966 | /* In some cases, some of the operands may be missing. |
16967 | (For example, in the case of PREDECREMENT_EXPR, the | |
16968 | amount to increment by may be missing.) That doesn't | |
16969 | make the expression dependent. */ | |
16970 | if (TREE_OPERAND (expression, i) | |
16971 | && (value_dependent_expression_p | |
16972 | (TREE_OPERAND (expression, i)))) | |
1fb3244a MM |
16973 | return true; |
16974 | return false; | |
16975 | } | |
c8094d83 | 16976 | |
6615c446 | 16977 | default: |
7416ab02 | 16978 | break; |
1fb3244a MM |
16979 | } |
16980 | } | |
c8094d83 | 16981 | |
1fb3244a MM |
16982 | /* The expression is not value-dependent. */ |
16983 | return false; | |
16984 | } | |
16985 | ||
16986 | /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of | |
16987 | [temp.dep.expr]. */ | |
16988 | ||
16989 | bool | |
3a978d72 | 16990 | type_dependent_expression_p (tree expression) |
1fb3244a MM |
16991 | { |
16992 | if (!processing_template_decl) | |
16993 | return false; | |
16994 | ||
7efa3e22 NS |
16995 | if (expression == error_mark_node) |
16996 | return false; | |
10b1d5e7 MM |
16997 | |
16998 | /* An unresolved name is always dependent. */ | |
b01e6d2b JM |
16999 | if (TREE_CODE (expression) == IDENTIFIER_NODE |
17000 | || TREE_CODE (expression) == USING_DECL) | |
10b1d5e7 | 17001 | return true; |
c8094d83 | 17002 | |
1fb3244a MM |
17003 | /* Some expression forms are never type-dependent. */ |
17004 | if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR | |
17005 | || TREE_CODE (expression) == SIZEOF_EXPR | |
17006 | || TREE_CODE (expression) == ALIGNOF_EXPR | |
cb68ec50 | 17007 | || TREE_CODE (expression) == TRAIT_EXPR |
1fb3244a MM |
17008 | || TREE_CODE (expression) == TYPEID_EXPR |
17009 | || TREE_CODE (expression) == DELETE_EXPR | |
17010 | || TREE_CODE (expression) == VEC_DELETE_EXPR | |
17011 | || TREE_CODE (expression) == THROW_EXPR) | |
17012 | return false; | |
17013 | ||
17014 | /* The types of these expressions depends only on the type to which | |
17015 | the cast occurs. */ | |
17016 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR | |
17017 | || TREE_CODE (expression) == STATIC_CAST_EXPR | |
17018 | || TREE_CODE (expression) == CONST_CAST_EXPR | |
17019 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR | |
17020 | || TREE_CODE (expression) == CAST_EXPR) | |
17021 | return dependent_type_p (TREE_TYPE (expression)); | |
d17811fd | 17022 | |
1fb3244a MM |
17023 | /* The types of these expressions depends only on the type created |
17024 | by the expression. */ | |
d17811fd MM |
17025 | if (TREE_CODE (expression) == NEW_EXPR |
17026 | || TREE_CODE (expression) == VEC_NEW_EXPR) | |
09d2f85f KL |
17027 | { |
17028 | /* For NEW_EXPR tree nodes created inside a template, either | |
17029 | the object type itself or a TREE_LIST may appear as the | |
17030 | operand 1. */ | |
17031 | tree type = TREE_OPERAND (expression, 1); | |
17032 | if (TREE_CODE (type) == TREE_LIST) | |
17033 | /* This is an array type. We need to check array dimensions | |
17034 | as well. */ | |
17035 | return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) | |
17036 | || value_dependent_expression_p | |
17037 | (TREE_OPERAND (TREE_VALUE (type), 1)); | |
17038 | else | |
17039 | return dependent_type_p (type); | |
17040 | } | |
1fb3244a | 17041 | |
5a57f1b2 JM |
17042 | if (TREE_CODE (expression) == SCOPE_REF |
17043 | && dependent_scope_ref_p (expression, | |
17044 | type_dependent_expression_p)) | |
17045 | return true; | |
17046 | ||
17047 | if (TREE_CODE (expression) == FUNCTION_DECL | |
17048 | && DECL_LANG_SPECIFIC (expression) | |
17049 | && DECL_TEMPLATE_INFO (expression) | |
17050 | && (any_dependent_template_arguments_p | |
17051 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) | |
17052 | return true; | |
17053 | ||
17054 | if (TREE_CODE (expression) == TEMPLATE_DECL | |
17055 | && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression)) | |
17056 | return false; | |
17057 | ||
c2acde1e JM |
17058 | if (TREE_CODE (expression) == STMT_EXPR) |
17059 | expression = stmt_expr_value_expr (expression); | |
17060 | ||
86a09a9e JM |
17061 | if (BRACE_ENCLOSED_INITIALIZER_P (expression)) |
17062 | { | |
17063 | tree elt; | |
17064 | unsigned i; | |
17065 | ||
17066 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt) | |
17067 | { | |
17068 | if (type_dependent_expression_p (elt)) | |
17069 | return true; | |
17070 | } | |
17071 | return false; | |
17072 | } | |
17073 | ||
d17811fd MM |
17074 | if (TREE_TYPE (expression) == unknown_type_node) |
17075 | { | |
17076 | if (TREE_CODE (expression) == ADDR_EXPR) | |
17077 | return type_dependent_expression_p (TREE_OPERAND (expression, 0)); | |
c353b8e3 MM |
17078 | if (TREE_CODE (expression) == COMPONENT_REF |
17079 | || TREE_CODE (expression) == OFFSET_REF) | |
6cb89308 NS |
17080 | { |
17081 | if (type_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
17082 | return true; | |
17083 | expression = TREE_OPERAND (expression, 1); | |
17084 | if (TREE_CODE (expression) == IDENTIFIER_NODE) | |
17085 | return false; | |
17086 | } | |
3601f003 KL |
17087 | /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */ |
17088 | if (TREE_CODE (expression) == SCOPE_REF) | |
17089 | return false; | |
c8094d83 | 17090 | |
d17811fd MM |
17091 | if (TREE_CODE (expression) == BASELINK) |
17092 | expression = BASELINK_FUNCTIONS (expression); | |
c8094d83 | 17093 | |
d17811fd MM |
17094 | if (TREE_CODE (expression) == TEMPLATE_ID_EXPR) |
17095 | { | |
ee3071ef NS |
17096 | if (any_dependent_template_arguments_p |
17097 | (TREE_OPERAND (expression, 1))) | |
d17811fd MM |
17098 | return true; |
17099 | expression = TREE_OPERAND (expression, 0); | |
17100 | } | |
b207d6e2 MM |
17101 | gcc_assert (TREE_CODE (expression) == OVERLOAD |
17102 | || TREE_CODE (expression) == FUNCTION_DECL); | |
c8094d83 | 17103 | |
315fb5db | 17104 | while (expression) |
d17811fd | 17105 | { |
315fb5db NS |
17106 | if (type_dependent_expression_p (OVL_CURRENT (expression))) |
17107 | return true; | |
17108 | expression = OVL_NEXT (expression); | |
d17811fd | 17109 | } |
315fb5db | 17110 | return false; |
d17811fd | 17111 | } |
c8094d83 | 17112 | |
3ce5fa4f | 17113 | gcc_assert (TREE_CODE (expression) != TYPE_DECL); |
3db45ab5 | 17114 | |
1fb3244a MM |
17115 | return (dependent_type_p (TREE_TYPE (expression))); |
17116 | } | |
5094a795 JM |
17117 | |
17118 | /* Like type_dependent_expression_p, but it also works while not processing | |
17119 | a template definition, i.e. during substitution or mangling. */ | |
17120 | ||
17121 | bool | |
17122 | type_dependent_expression_p_push (tree expr) | |
17123 | { | |
17124 | bool b; | |
17125 | ++processing_template_decl; | |
17126 | b = type_dependent_expression_p (expr); | |
17127 | --processing_template_decl; | |
17128 | return b; | |
17129 | } | |
1fb3244a | 17130 | |
c166b898 | 17131 | /* Returns TRUE if ARGS contains a type-dependent expression. */ |
1fb3244a MM |
17132 | |
17133 | bool | |
c166b898 | 17134 | any_type_dependent_arguments_p (const VEC(tree,gc) *args) |
d17811fd | 17135 | { |
c166b898 ILT |
17136 | unsigned int i; |
17137 | tree arg; | |
7efa3e22 | 17138 | |
c166b898 ILT |
17139 | for (i = 0; VEC_iterate (tree, args, i, arg); ++i) |
17140 | { | |
7efa3e22 | 17141 | if (type_dependent_expression_p (arg)) |
d17811fd | 17142 | return true; |
d17811fd MM |
17143 | } |
17144 | return false; | |
17145 | } | |
17146 | ||
32a11c08 MM |
17147 | /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are |
17148 | expressions) contains any value-dependent expressions. */ | |
17149 | ||
17150 | bool | |
58f9752a | 17151 | any_value_dependent_elements_p (const_tree list) |
32a11c08 MM |
17152 | { |
17153 | for (; list; list = TREE_CHAIN (list)) | |
17154 | if (value_dependent_expression_p (TREE_VALUE (list))) | |
17155 | return true; | |
17156 | ||
17157 | return false; | |
17158 | } | |
17159 | ||
d17811fd MM |
17160 | /* Returns TRUE if the ARG (a template argument) is dependent. */ |
17161 | ||
5d80a306 | 17162 | bool |
1fb3244a MM |
17163 | dependent_template_arg_p (tree arg) |
17164 | { | |
17165 | if (!processing_template_decl) | |
17166 | return false; | |
17167 | ||
17168 | if (TREE_CODE (arg) == TEMPLATE_DECL | |
17169 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
17170 | return dependent_template_p (arg); | |
5d80a306 DG |
17171 | else if (ARGUMENT_PACK_P (arg)) |
17172 | { | |
17173 | tree args = ARGUMENT_PACK_ARGS (arg); | |
17174 | int i, len = TREE_VEC_LENGTH (args); | |
17175 | for (i = 0; i < len; ++i) | |
17176 | { | |
17177 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
17178 | return true; | |
17179 | } | |
17180 | ||
17181 | return false; | |
17182 | } | |
1fb3244a MM |
17183 | else if (TYPE_P (arg)) |
17184 | return dependent_type_p (arg); | |
17185 | else | |
17186 | return (type_dependent_expression_p (arg) | |
17187 | || value_dependent_expression_p (arg)); | |
17188 | } | |
17189 | ||
06d40de8 DG |
17190 | /* Returns true if ARGS (a collection of template arguments) contains |
17191 | any types that require structural equality testing. */ | |
17192 | ||
17193 | bool | |
17194 | any_template_arguments_need_structural_equality_p (tree args) | |
17195 | { | |
17196 | int i; | |
17197 | int j; | |
17198 | ||
17199 | if (!args) | |
17200 | return false; | |
17201 | if (args == error_mark_node) | |
17202 | return true; | |
17203 | ||
17204 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) | |
17205 | { | |
17206 | tree level = TMPL_ARGS_LEVEL (args, i + 1); | |
17207 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) | |
17208 | { | |
17209 | tree arg = TREE_VEC_ELT (level, j); | |
5d80a306 DG |
17210 | tree packed_args = NULL_TREE; |
17211 | int k, len = 1; | |
17212 | ||
17213 | if (ARGUMENT_PACK_P (arg)) | |
17214 | { | |
17215 | /* Look inside the argument pack. */ | |
17216 | packed_args = ARGUMENT_PACK_ARGS (arg); | |
17217 | len = TREE_VEC_LENGTH (packed_args); | |
17218 | } | |
17219 | ||
17220 | for (k = 0; k < len; ++k) | |
17221 | { | |
17222 | if (packed_args) | |
17223 | arg = TREE_VEC_ELT (packed_args, k); | |
17224 | ||
17225 | if (error_operand_p (arg)) | |
17226 | return true; | |
17227 | else if (TREE_CODE (arg) == TEMPLATE_DECL | |
17228 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
17229 | continue; | |
17230 | else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg)) | |
17231 | return true; | |
17232 | else if (!TYPE_P (arg) && TREE_TYPE (arg) | |
17233 | && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg))) | |
17234 | return true; | |
17235 | } | |
06d40de8 DG |
17236 | } |
17237 | } | |
17238 | ||
17239 | return false; | |
17240 | } | |
17241 | ||
d17811fd MM |
17242 | /* Returns true if ARGS (a collection of template arguments) contains |
17243 | any dependent arguments. */ | |
1fb3244a | 17244 | |
d17811fd | 17245 | bool |
58f9752a | 17246 | any_dependent_template_arguments_p (const_tree args) |
1fb3244a | 17247 | { |
bf12d54d | 17248 | int i; |
c353b8e3 MM |
17249 | int j; |
17250 | ||
d17811fd MM |
17251 | if (!args) |
17252 | return false; | |
7e497d0c VR |
17253 | if (args == error_mark_node) |
17254 | return true; | |
d17811fd | 17255 | |
c353b8e3 MM |
17256 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
17257 | { | |
58f9752a | 17258 | const_tree level = TMPL_ARGS_LEVEL (args, i + 1); |
c353b8e3 MM |
17259 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) |
17260 | if (dependent_template_arg_p (TREE_VEC_ELT (level, j))) | |
17261 | return true; | |
17262 | } | |
1fb3244a | 17263 | |
1fb3244a MM |
17264 | return false; |
17265 | } | |
17266 | ||
17267 | /* Returns TRUE if the template TMPL is dependent. */ | |
17268 | ||
17269 | bool | |
17270 | dependent_template_p (tree tmpl) | |
17271 | { | |
b95cc51a MM |
17272 | if (TREE_CODE (tmpl) == OVERLOAD) |
17273 | { | |
17274 | while (tmpl) | |
17275 | { | |
17276 | if (dependent_template_p (OVL_FUNCTION (tmpl))) | |
17277 | return true; | |
17278 | tmpl = OVL_CHAIN (tmpl); | |
17279 | } | |
17280 | return false; | |
17281 | } | |
17282 | ||
1fb3244a MM |
17283 | /* Template template parameters are dependent. */ |
17284 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) | |
17285 | || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) | |
17286 | return true; | |
27ab0504 | 17287 | /* So are names that have not been looked up. */ |
acccf788 MM |
17288 | if (TREE_CODE (tmpl) == SCOPE_REF |
17289 | || TREE_CODE (tmpl) == IDENTIFIER_NODE) | |
d17811fd | 17290 | return true; |
1fb3244a MM |
17291 | /* So are member templates of dependent classes. */ |
17292 | if (TYPE_P (CP_DECL_CONTEXT (tmpl))) | |
17293 | return dependent_type_p (DECL_CONTEXT (tmpl)); | |
17294 | return false; | |
17295 | } | |
17296 | ||
d17811fd MM |
17297 | /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ |
17298 | ||
17299 | bool | |
17300 | dependent_template_id_p (tree tmpl, tree args) | |
17301 | { | |
17302 | return (dependent_template_p (tmpl) | |
17303 | || any_dependent_template_arguments_p (args)); | |
17304 | } | |
17305 | ||
a68ab351 JJ |
17306 | /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors |
17307 | is dependent. */ | |
17308 | ||
17309 | bool | |
17310 | dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv) | |
17311 | { | |
17312 | int i; | |
17313 | ||
17314 | if (!processing_template_decl) | |
17315 | return false; | |
17316 | ||
17317 | for (i = 0; i < TREE_VEC_LENGTH (declv); i++) | |
17318 | { | |
17319 | tree decl = TREE_VEC_ELT (declv, i); | |
17320 | tree init = TREE_VEC_ELT (initv, i); | |
17321 | tree cond = TREE_VEC_ELT (condv, i); | |
17322 | tree incr = TREE_VEC_ELT (incrv, i); | |
17323 | ||
17324 | if (type_dependent_expression_p (decl)) | |
17325 | return true; | |
17326 | ||
17327 | if (init && type_dependent_expression_p (init)) | |
17328 | return true; | |
17329 | ||
17330 | if (type_dependent_expression_p (cond)) | |
17331 | return true; | |
17332 | ||
17333 | if (COMPARISON_CLASS_P (cond) | |
17334 | && (type_dependent_expression_p (TREE_OPERAND (cond, 0)) | |
17335 | || type_dependent_expression_p (TREE_OPERAND (cond, 1)))) | |
17336 | return true; | |
17337 | ||
17338 | if (TREE_CODE (incr) == MODOP_EXPR) | |
17339 | { | |
17340 | if (type_dependent_expression_p (TREE_OPERAND (incr, 0)) | |
17341 | || type_dependent_expression_p (TREE_OPERAND (incr, 2))) | |
17342 | return true; | |
17343 | } | |
17344 | else if (type_dependent_expression_p (incr)) | |
17345 | return true; | |
17346 | else if (TREE_CODE (incr) == MODIFY_EXPR) | |
17347 | { | |
17348 | if (type_dependent_expression_p (TREE_OPERAND (incr, 0))) | |
17349 | return true; | |
17350 | else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1))) | |
17351 | { | |
17352 | tree t = TREE_OPERAND (incr, 1); | |
17353 | if (type_dependent_expression_p (TREE_OPERAND (t, 0)) | |
17354 | || type_dependent_expression_p (TREE_OPERAND (t, 1))) | |
17355 | return true; | |
17356 | } | |
17357 | } | |
17358 | } | |
17359 | ||
17360 | return false; | |
17361 | } | |
17362 | ||
14d22dd6 | 17363 | /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the |
4195a767 NS |
17364 | TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if |
17365 | no such TYPE can be found. Note that this function peers inside | |
17366 | uninstantiated templates and therefore should be used only in | |
17367 | extremely limited situations. ONLY_CURRENT_P restricts this | |
17368 | peering to the currently open classes hierarchy (which is required | |
17369 | when comparing types). */ | |
14d22dd6 MM |
17370 | |
17371 | tree | |
17372 | resolve_typename_type (tree type, bool only_current_p) | |
17373 | { | |
17374 | tree scope; | |
17375 | tree name; | |
17376 | tree decl; | |
17377 | int quals; | |
4514aa8c | 17378 | tree pushed_scope; |
4195a767 | 17379 | tree result; |
14d22dd6 | 17380 | |
50bc768d | 17381 | gcc_assert (TREE_CODE (type) == TYPENAME_TYPE); |
14d22dd6 MM |
17382 | |
17383 | scope = TYPE_CONTEXT (type); | |
d0940d56 DS |
17384 | /* Usually the non-qualified identifier of a TYPENAME_TYPE is |
17385 | TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of | |
17386 | a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing | |
17387 | the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified | |
17388 | identifier of the TYPENAME_TYPE anymore. | |
17389 | So by getting the TYPE_IDENTIFIER of the _main declaration_ of the | |
17390 | TYPENAME_TYPE instead, we avoid messing up with a possible | |
17391 | typedef variant case. */ | |
17392 | name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type)); | |
14d22dd6 MM |
17393 | |
17394 | /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve | |
17395 | it first before we can figure out what NAME refers to. */ | |
17396 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
17397 | scope = resolve_typename_type (scope, only_current_p); | |
17398 | /* If we don't know what SCOPE refers to, then we cannot resolve the | |
17399 | TYPENAME_TYPE. */ | |
4195a767 NS |
17400 | if (TREE_CODE (scope) == TYPENAME_TYPE) |
17401 | return type; | |
14d22dd6 MM |
17402 | /* If the SCOPE is a template type parameter, we have no way of |
17403 | resolving the name. */ | |
17404 | if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM) | |
17405 | return type; | |
17406 | /* If the SCOPE is not the current instantiation, there's no reason | |
17407 | to look inside it. */ | |
17408 | if (only_current_p && !currently_open_class (scope)) | |
4195a767 | 17409 | return type; |
7a20d689 JM |
17410 | /* If SCOPE isn't the template itself, it will not have a valid |
17411 | TYPE_FIELDS list. */ | |
17412 | if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope))) | |
17413 | /* scope is either the template itself or a compatible instantiation | |
17414 | like X<T>, so look up the name in the original template. */ | |
17415 | scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope); | |
17416 | else | |
17417 | /* scope is a partial instantiation, so we can't do the lookup or we | |
17418 | will lose the template arguments. */ | |
17419 | return type; | |
14d22dd6 MM |
17420 | /* Enter the SCOPE so that name lookup will be resolved as if we |
17421 | were in the class definition. In particular, SCOPE will no | |
17422 | longer be considered a dependent type. */ | |
4514aa8c | 17423 | pushed_scope = push_scope (scope); |
14d22dd6 | 17424 | /* Look up the declaration. */ |
86ac0575 | 17425 | decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true); |
4195a767 NS |
17426 | |
17427 | result = NULL_TREE; | |
17428 | ||
14d22dd6 MM |
17429 | /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to |
17430 | find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ | |
17431 | if (!decl) | |
4195a767 | 17432 | /*nop*/; |
14d22dd6 MM |
17433 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE |
17434 | && TREE_CODE (decl) == TYPE_DECL) | |
4195a767 NS |
17435 | { |
17436 | result = TREE_TYPE (decl); | |
17437 | if (result == error_mark_node) | |
17438 | result = NULL_TREE; | |
17439 | } | |
14d22dd6 MM |
17440 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR |
17441 | && DECL_CLASS_TEMPLATE_P (decl)) | |
17442 | { | |
17443 | tree tmpl; | |
17444 | tree args; | |
17445 | /* Obtain the template and the arguments. */ | |
17446 | tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0); | |
17447 | args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1); | |
17448 | /* Instantiate the template. */ | |
4195a767 NS |
17449 | result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, |
17450 | /*entering_scope=*/0, | |
17451 | tf_error | tf_user); | |
17452 | if (result == error_mark_node) | |
17453 | result = NULL_TREE; | |
14d22dd6 | 17454 | } |
4195a767 | 17455 | |
14d22dd6 | 17456 | /* Leave the SCOPE. */ |
4514aa8c NS |
17457 | if (pushed_scope) |
17458 | pop_scope (pushed_scope); | |
14d22dd6 | 17459 | |
4195a767 NS |
17460 | /* If we failed to resolve it, return the original typename. */ |
17461 | if (!result) | |
17462 | return type; | |
17463 | ||
17464 | /* If lookup found a typename type, resolve that too. */ | |
17465 | if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result)) | |
17466 | { | |
17467 | /* Ill-formed programs can cause infinite recursion here, so we | |
17468 | must catch that. */ | |
17469 | TYPENAME_IS_RESOLVING_P (type) = 1; | |
17470 | result = resolve_typename_type (result, only_current_p); | |
17471 | TYPENAME_IS_RESOLVING_P (type) = 0; | |
17472 | } | |
17473 | ||
17474 | /* Qualify the resulting type. */ | |
17475 | quals = cp_type_quals (type); | |
17476 | if (quals) | |
17477 | result = cp_build_qualified_type (result, cp_type_quals (result) | quals); | |
17478 | ||
17479 | return result; | |
14d22dd6 MM |
17480 | } |
17481 | ||
d17811fd MM |
17482 | /* EXPR is an expression which is not type-dependent. Return a proxy |
17483 | for EXPR that can be used to compute the types of larger | |
17484 | expressions containing EXPR. */ | |
17485 | ||
17486 | tree | |
17487 | build_non_dependent_expr (tree expr) | |
17488 | { | |
0deb916c MM |
17489 | tree inner_expr; |
17490 | ||
c8094d83 | 17491 | /* Preserve null pointer constants so that the type of things like |
d17811fd MM |
17492 | "p == 0" where "p" is a pointer can be determined. */ |
17493 | if (null_ptr_cst_p (expr)) | |
17494 | return expr; | |
17495 | /* Preserve OVERLOADs; the functions must be available to resolve | |
17496 | types. */ | |
c497db75 | 17497 | inner_expr = expr; |
c2acde1e JM |
17498 | if (TREE_CODE (inner_expr) == STMT_EXPR) |
17499 | inner_expr = stmt_expr_value_expr (inner_expr); | |
c497db75 AO |
17500 | if (TREE_CODE (inner_expr) == ADDR_EXPR) |
17501 | inner_expr = TREE_OPERAND (inner_expr, 0); | |
17502 | if (TREE_CODE (inner_expr) == COMPONENT_REF) | |
17503 | inner_expr = TREE_OPERAND (inner_expr, 1); | |
2226e997 | 17504 | if (is_overloaded_fn (inner_expr) |
6439fffd | 17505 | || TREE_CODE (inner_expr) == OFFSET_REF) |
d17811fd | 17506 | return expr; |
5ae9ba3e MM |
17507 | /* There is no need to return a proxy for a variable. */ |
17508 | if (TREE_CODE (expr) == VAR_DECL) | |
17509 | return expr; | |
7433e6d4 MM |
17510 | /* Preserve string constants; conversions from string constants to |
17511 | "char *" are allowed, even though normally a "const char *" | |
17512 | cannot be used to initialize a "char *". */ | |
17513 | if (TREE_CODE (expr) == STRING_CST) | |
17514 | return expr; | |
b7c707d1 MM |
17515 | /* Preserve arithmetic constants, as an optimization -- there is no |
17516 | reason to create a new node. */ | |
17517 | if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST) | |
17518 | return expr; | |
9b7be7b5 MM |
17519 | /* Preserve THROW_EXPRs -- all throw-expressions have type "void". |
17520 | There is at least one place where we want to know that a | |
17521 | particular expression is a throw-expression: when checking a ?: | |
17522 | expression, there are special rules if the second or third | |
878cbb73 | 17523 | argument is a throw-expression. */ |
9b7be7b5 MM |
17524 | if (TREE_CODE (expr) == THROW_EXPR) |
17525 | return expr; | |
47d4c811 NS |
17526 | |
17527 | if (TREE_CODE (expr) == COND_EXPR) | |
f293ce4b RS |
17528 | return build3 (COND_EXPR, |
17529 | TREE_TYPE (expr), | |
17530 | TREE_OPERAND (expr, 0), | |
c8094d83 | 17531 | (TREE_OPERAND (expr, 1) |
f293ce4b RS |
17532 | ? build_non_dependent_expr (TREE_OPERAND (expr, 1)) |
17533 | : build_non_dependent_expr (TREE_OPERAND (expr, 0))), | |
17534 | build_non_dependent_expr (TREE_OPERAND (expr, 2))); | |
8e1daa34 NS |
17535 | if (TREE_CODE (expr) == COMPOUND_EXPR |
17536 | && !COMPOUND_EXPR_OVERLOADED (expr)) | |
f293ce4b RS |
17537 | return build2 (COMPOUND_EXPR, |
17538 | TREE_TYPE (expr), | |
17539 | TREE_OPERAND (expr, 0), | |
17540 | build_non_dependent_expr (TREE_OPERAND (expr, 1))); | |
c8094d83 | 17541 | |
fdeff563 NS |
17542 | /* If the type is unknown, it can't really be non-dependent */ |
17543 | gcc_assert (TREE_TYPE (expr) != unknown_type_node); | |
3db45ab5 | 17544 | |
c8094d83 | 17545 | /* Otherwise, build a NON_DEPENDENT_EXPR. |
d17811fd MM |
17546 | |
17547 | REFERENCE_TYPEs are not stripped for expressions in templates | |
17548 | because doing so would play havoc with mangling. Consider, for | |
17549 | example: | |
17550 | ||
c8094d83 | 17551 | template <typename T> void f<T& g>() { g(); } |
d17811fd MM |
17552 | |
17553 | In the body of "f", the expression for "g" will have | |
17554 | REFERENCE_TYPE, even though the standard says that it should | |
17555 | not. The reason is that we must preserve the syntactic form of | |
17556 | the expression so that mangling (say) "f<g>" inside the body of | |
17557 | "f" works out correctly. Therefore, the REFERENCE_TYPE is | |
17558 | stripped here. */ | |
018a5803 | 17559 | return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr); |
d17811fd MM |
17560 | } |
17561 | ||
c166b898 ILT |
17562 | /* ARGS is a vector of expressions as arguments to a function call. |
17563 | Replace the arguments with equivalent non-dependent expressions. | |
17564 | This modifies ARGS in place. */ | |
d17811fd | 17565 | |
c166b898 ILT |
17566 | void |
17567 | make_args_non_dependent (VEC(tree,gc) *args) | |
d17811fd | 17568 | { |
c166b898 ILT |
17569 | unsigned int ix; |
17570 | tree arg; | |
d17811fd | 17571 | |
c166b898 ILT |
17572 | for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix) |
17573 | { | |
17574 | tree newarg = build_non_dependent_expr (arg); | |
17575 | if (newarg != arg) | |
17576 | VEC_replace (tree, args, ix, newarg); | |
17577 | } | |
d17811fd MM |
17578 | } |
17579 | ||
86a09a9e JM |
17580 | /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM |
17581 | with a level one deeper than the actual template parms. */ | |
17582 | ||
17583 | tree | |
17584 | make_auto (void) | |
17585 | { | |
17586 | tree au; | |
17587 | ||
17588 | /* ??? Is it worth caching this for multiple autos at the same level? */ | |
17589 | au = cxx_make_type (TEMPLATE_TYPE_PARM); | |
c2255bc4 AH |
17590 | TYPE_NAME (au) = build_decl (BUILTINS_LOCATION, |
17591 | TYPE_DECL, get_identifier ("auto"), au); | |
86a09a9e JM |
17592 | TYPE_STUB_DECL (au) = TYPE_NAME (au); |
17593 | TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index | |
17594 | (0, processing_template_decl + 1, processing_template_decl + 1, | |
17595 | TYPE_NAME (au), NULL_TREE); | |
17596 | TYPE_CANONICAL (au) = canonical_type_parameter (au); | |
17597 | DECL_ARTIFICIAL (TYPE_NAME (au)) = 1; | |
17598 | SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au)); | |
17599 | ||
17600 | return au; | |
17601 | } | |
17602 | ||
5f5babf1 | 17603 | /* Given type ARG, return std::initializer_list<ARG>. */ |
86a09a9e JM |
17604 | |
17605 | static tree | |
5f5babf1 | 17606 | listify (tree arg) |
86a09a9e JM |
17607 | { |
17608 | tree std_init_list = namespace_binding | |
17609 | (get_identifier ("initializer_list"), std_node); | |
17610 | tree argvec; | |
86a09a9e JM |
17611 | if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list)) |
17612 | { | |
5f5babf1 | 17613 | error ("deducing from brace-enclosed initializer list requires " |
86a09a9e JM |
17614 | "#include <initializer_list>"); |
17615 | return error_mark_node; | |
17616 | } | |
17617 | argvec = make_tree_vec (1); | |
5f5babf1 JM |
17618 | TREE_VEC_ELT (argvec, 0) = arg; |
17619 | return lookup_template_class (std_init_list, argvec, NULL_TREE, | |
17620 | NULL_TREE, 0, tf_warning_or_error); | |
17621 | } | |
17622 | ||
17623 | /* Replace auto in TYPE with std::initializer_list<auto>. */ | |
86a09a9e | 17624 | |
5f5babf1 JM |
17625 | static tree |
17626 | listify_autos (tree type, tree auto_node) | |
17627 | { | |
17628 | tree init_auto = listify (auto_node); | |
17629 | tree argvec = make_tree_vec (1); | |
86a09a9e JM |
17630 | TREE_VEC_ELT (argvec, 0) = init_auto; |
17631 | if (processing_template_decl) | |
17632 | argvec = add_to_template_args (current_template_args (), argvec); | |
17633 | return tsubst (type, argvec, tf_warning_or_error, NULL_TREE); | |
17634 | } | |
17635 | ||
17636 | /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced | |
17637 | from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */ | |
17638 | ||
17639 | tree | |
17640 | do_auto_deduction (tree type, tree init, tree auto_node) | |
17641 | { | |
c166b898 ILT |
17642 | tree parms, tparms, targs; |
17643 | tree args[1]; | |
86a09a9e JM |
17644 | int val; |
17645 | ||
17646 | /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto | |
17647 | with either a new invented type template parameter U or, if the | |
17648 | initializer is a braced-init-list (8.5.4), with | |
17649 | std::initializer_list<U>. */ | |
17650 | if (BRACE_ENCLOSED_INITIALIZER_P (init)) | |
17651 | type = listify_autos (type, auto_node); | |
17652 | ||
17653 | parms = build_tree_list (NULL_TREE, type); | |
c166b898 | 17654 | args[0] = init; |
86a09a9e JM |
17655 | tparms = make_tree_vec (1); |
17656 | targs = make_tree_vec (1); | |
17657 | TREE_VEC_ELT (tparms, 0) | |
17658 | = build_tree_list (NULL_TREE, TYPE_NAME (auto_node)); | |
c166b898 | 17659 | val = type_unification_real (tparms, targs, parms, args, 1, 0, |
86a09a9e JM |
17660 | DEDUCE_CALL, LOOKUP_NORMAL); |
17661 | if (val > 0) | |
17662 | { | |
17663 | error ("unable to deduce %qT from %qE", type, init); | |
17664 | return error_mark_node; | |
17665 | } | |
17666 | ||
17667 | if (processing_template_decl) | |
17668 | targs = add_to_template_args (current_template_args (), targs); | |
17669 | return tsubst (type, targs, tf_warning_or_error, NULL_TREE); | |
17670 | } | |
17671 | ||
f57ca1ea JM |
17672 | /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the |
17673 | result. */ | |
17674 | ||
17675 | tree | |
17676 | splice_late_return_type (tree type, tree late_return_type) | |
17677 | { | |
17678 | tree argvec; | |
17679 | ||
17680 | if (late_return_type == NULL_TREE) | |
17681 | return type; | |
17682 | argvec = make_tree_vec (1); | |
17683 | TREE_VEC_ELT (argvec, 0) = late_return_type; | |
17684 | if (processing_template_decl) | |
17685 | argvec = add_to_template_args (current_template_args (), argvec); | |
17686 | return tsubst (type, argvec, tf_warning_or_error, NULL_TREE); | |
17687 | } | |
17688 | ||
86a09a9e JM |
17689 | /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */ |
17690 | ||
17691 | bool | |
17692 | is_auto (const_tree type) | |
17693 | { | |
17694 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM | |
17695 | && TYPE_IDENTIFIER (type) == get_identifier ("auto")) | |
17696 | return true; | |
17697 | else | |
17698 | return false; | |
17699 | } | |
17700 | ||
17701 | /* Returns true iff TYPE contains a use of 'auto'. Since auto can only | |
17702 | appear as a type-specifier for the declaration in question, we don't | |
17703 | have to look through the whole type. */ | |
17704 | ||
17705 | tree | |
17706 | type_uses_auto (tree type) | |
17707 | { | |
17708 | enum tree_code code; | |
17709 | if (is_auto (type)) | |
17710 | return type; | |
17711 | ||
17712 | code = TREE_CODE (type); | |
17713 | ||
17714 | if (code == POINTER_TYPE || code == REFERENCE_TYPE | |
17715 | || code == OFFSET_TYPE || code == FUNCTION_TYPE | |
17716 | || code == METHOD_TYPE || code == ARRAY_TYPE) | |
17717 | return type_uses_auto (TREE_TYPE (type)); | |
17718 | ||
17719 | if (TYPE_PTRMEMFUNC_P (type)) | |
17720 | return type_uses_auto (TREE_TYPE (TREE_TYPE | |
17721 | (TYPE_PTRMEMFUNC_FN_TYPE (type)))); | |
17722 | ||
17723 | return NULL_TREE; | |
17724 | } | |
17725 | ||
2eb25c98 DS |
17726 | /* For a given template T, return the list of typedefs referenced |
17727 | in T for which access check is needed at T instantiation time. | |
17728 | T is either a FUNCTION_DECL or a RECORD_TYPE. | |
17729 | Those typedefs were added to T by the function | |
17730 | append_type_to_template_for_access_check. */ | |
17731 | ||
17732 | tree | |
17733 | get_types_needing_access_check (tree t) | |
17734 | { | |
17735 | tree ti, result = NULL_TREE; | |
17736 | ||
17737 | if (!t || t == error_mark_node) | |
17738 | return t; | |
17739 | ||
17740 | if (!(ti = get_template_info (t))) | |
17741 | return NULL_TREE; | |
17742 | ||
9dbed529 | 17743 | if (CLASS_TYPE_P (t) |
c4efba1c | 17744 | || TREE_CODE (t) == FUNCTION_DECL) |
2eb25c98 DS |
17745 | { |
17746 | if (!TI_TEMPLATE (ti)) | |
17747 | return NULL_TREE; | |
17748 | ||
17749 | result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti); | |
17750 | } | |
17751 | ||
17752 | return result; | |
17753 | } | |
17754 | ||
17755 | /* Append the typedef TYPE_DECL used in template T to a list of typedefs | |
17756 | tied to T. That list of typedefs will be access checked at | |
17757 | T instantiation time. | |
17758 | T is either a FUNCTION_DECL or a RECORD_TYPE. | |
17759 | TYPE_DECL is a TYPE_DECL node representing a typedef. | |
17760 | SCOPE is the scope through which TYPE_DECL is accessed. | |
17761 | ||
17762 | This function is a subroutine of | |
17763 | append_type_to_template_for_access_check. */ | |
17764 | ||
17765 | static void | |
17766 | append_type_to_template_for_access_check_1 (tree t, | |
17767 | tree type_decl, | |
17768 | tree scope) | |
17769 | { | |
17770 | tree ti; | |
17771 | ||
17772 | if (!t || t == error_mark_node) | |
17773 | return; | |
17774 | ||
17775 | gcc_assert ((TREE_CODE (t) == FUNCTION_DECL | |
9dbed529 | 17776 | || CLASS_TYPE_P (t)) |
2eb25c98 DS |
17777 | && type_decl |
17778 | && TREE_CODE (type_decl) == TYPE_DECL | |
17779 | && scope); | |
17780 | ||
17781 | if (!(ti = get_template_info (t))) | |
17782 | return; | |
17783 | ||
17784 | gcc_assert (TI_TEMPLATE (ti)); | |
17785 | ||
17786 | TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti) = | |
17787 | tree_cons (type_decl, scope, TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti)); | |
17788 | } | |
17789 | ||
d0940d56 | 17790 | /* Append TYPE_DECL to the template TEMPL. |
2eb25c98 | 17791 | TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL. |
d0940d56 | 17792 | At TEMPL instanciation time, TYPE_DECL will be checked to see |
2eb25c98 DS |
17793 | if it can be accessed through SCOPE. |
17794 | ||
17795 | e.g. consider the following code snippet: | |
17796 | ||
17797 | class C | |
17798 | { | |
17799 | typedef int myint; | |
17800 | }; | |
17801 | ||
17802 | template<class U> struct S | |
17803 | { | |
17804 | C::myint mi; | |
17805 | }; | |
17806 | ||
17807 | S<char> s; | |
17808 | ||
17809 | At S<char> instantiation time, we need to check the access of C::myint | |
17810 | In other words, we need to check the access of the myint typedef through | |
17811 | the C scope. For that purpose, this function will add the myint typedef | |
17812 | and the scope C through which its being accessed to a list of typedefs | |
17813 | tied to the template S. That list will be walked at template instantiation | |
17814 | time and access check performed on each typedefs it contains. | |
17815 | Note that this particular code snippet should yield an error because | |
17816 | myint is private to C. */ | |
d0940d56 DS |
17817 | |
17818 | void | |
17819 | append_type_to_template_for_access_check (tree templ, | |
17820 | tree type_decl, | |
17821 | tree scope) | |
17822 | { | |
2eb25c98 | 17823 | tree node; |
d0940d56 | 17824 | |
2eb25c98 | 17825 | gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL)); |
d0940d56 | 17826 | |
2eb25c98 DS |
17827 | /* Make sure we don't append the type to the template twice. */ |
17828 | for (node = get_types_needing_access_check (templ); | |
d0940d56 DS |
17829 | node; |
17830 | node = TREE_CHAIN (node)) | |
17831 | { | |
17832 | tree decl = TREE_PURPOSE (node); | |
17833 | tree type_scope = TREE_VALUE (node); | |
17834 | ||
17835 | if (decl == type_decl && type_scope == scope) | |
17836 | return; | |
17837 | } | |
17838 | ||
2eb25c98 | 17839 | append_type_to_template_for_access_check_1 (templ, type_decl, scope); |
d0940d56 DS |
17840 | } |
17841 | ||
2b110bfc JM |
17842 | /* Set up the hash tables for template instantiations. */ |
17843 | ||
17844 | void | |
17845 | init_template_processing (void) | |
17846 | { | |
17847 | decl_specializations = htab_create_ggc (37, | |
17848 | hash_specialization, | |
17849 | eq_specializations, | |
17850 | ggc_free); | |
17851 | type_specializations = htab_create_ggc (37, | |
17852 | hash_specialization, | |
17853 | eq_specializations, | |
17854 | ggc_free); | |
17855 | } | |
17856 | ||
e2500fed | 17857 | #include "gt-cp-pt.h" |